[PATCH v3 1/3] seccomp: Introduce SECCOMP_LOAD_FILTER operation

From: Hengqi Chen
Date: Wed Nov 29 2023 - 00:35:20 EST


This patch adds a new operation named SECCOMP_LOAD_FILTER.
It accepts a sock_fprog the same as SECCOMP_SET_MODE_FILTER
but only performs the loading process. If succeed, returns a
new fd associated with the seccomp filter. The seccomp filter
thus can be reused to attach to different processes.

Signed-off-by: Hengqi Chen <hengqi.chen@xxxxxxxxx>
---
include/uapi/linux/seccomp.h | 1 +
kernel/seccomp.c | 34 ++++++++++++++++++++++++++++++++++
2 files changed, 35 insertions(+)

diff --git a/include/uapi/linux/seccomp.h b/include/uapi/linux/seccomp.h
index dbfc9b37fcae..ee2c83697810 100644
--- a/include/uapi/linux/seccomp.h
+++ b/include/uapi/linux/seccomp.h
@@ -16,6 +16,7 @@
#define SECCOMP_SET_MODE_FILTER 1
#define SECCOMP_GET_ACTION_AVAIL 2
#define SECCOMP_GET_NOTIF_SIZES 3
+#define SECCOMP_LOAD_FILTER 4

/* Valid flags for SECCOMP_SET_MODE_FILTER */
#define SECCOMP_FILTER_FLAG_TSYNC (1UL << 0)
diff --git a/kernel/seccomp.c b/kernel/seccomp.c
index 255999ba9190..ef956c3d15c7 100644
--- a/kernel/seccomp.c
+++ b/kernel/seccomp.c
@@ -1885,6 +1885,18 @@ static bool has_duplicate_listener(struct seccomp_filter *new_child)
return false;
}

+static int seccomp_filter_put(struct inode *inode, struct file *file)
+{
+ struct seccomp_filter *filter = file->private_data;
+
+ __put_seccomp_filter(filter);
+ return 0;
+}
+
+static const struct file_operations seccomp_filter_fops = {
+ .release = seccomp_filter_put,
+};
+
/**
* seccomp_set_mode_filter: internal function for setting seccomp filter
* @flags: flags to change filter behavior
@@ -1996,12 +2008,29 @@ static long seccomp_set_mode_filter(unsigned int flags,
seccomp_filter_free(prepared);
return ret;
}
+
+static long seccomp_load_filter(const char __user *filter)
+{
+ struct seccomp_filter *sfilter;
+
+ sfilter = seccomp_prepare_user_filter(filter);
+ if (IS_ERR(sfilter))
+ return PTR_ERR(sfilter);
+
+ return anon_inode_getfd("seccomp-filter", &seccomp_filter_fops,
+ sfilter, O_CLOEXEC);
+}
#else
static inline long seccomp_set_mode_filter(unsigned int flags,
const char __user *filter)
{
return -EINVAL;
}
+
+static inline long seccomp_load_filter(const char __user *filter)
+{
+ return -EINVAL;
+}
#endif

static long seccomp_get_action_avail(const char __user *uaction)
@@ -2063,6 +2092,11 @@ static long do_seccomp(unsigned int op, unsigned int flags,
return -EINVAL;

return seccomp_get_notif_sizes(uargs);
+ case SECCOMP_LOAD_FILTER:
+ if (flags != 0)
+ return -EINVAL;
+
+ return seccomp_load_filter(uargs);
default:
return -EINVAL;
}
--
2.34.1