public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Hengqi Chen <hengqi.chen@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: keescook@chromium.org, luto@amacapital.net, wad@chromium.org,
	alexyonghe@tencent.com, hengqi.chen@gmail.com
Subject: [PATCH v3 1/3] seccomp: Introduce SECCOMP_LOAD_FILTER operation
Date: Wed, 29 Nov 2023 05:34:38 +0000	[thread overview]
Message-ID: <20231129053440.41522-2-hengqi.chen@gmail.com> (raw)
In-Reply-To: <20231129053440.41522-1-hengqi.chen@gmail.com>

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@gmail.com>
---
 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


  reply	other threads:[~2023-11-29  5:35 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-29  5:34 [PATCH v3 0/3] seccomp: Make seccomp filter reusable Hengqi Chen
2023-11-29  5:34 ` Hengqi Chen [this message]
2023-11-29  5:34 ` [PATCH v3 2/3] seccomp: Introduce new flag SECCOMP_FILTER_FLAG_FILTER_FD Hengqi Chen
2023-11-29  5:34 ` [PATCH v3 3/3] selftests/seccomp: Test seccomp filter load and attach Hengqi Chen
2023-12-05  8:48   ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231129053440.41522-2-hengqi.chen@gmail.com \
    --to=hengqi.chen@gmail.com \
    --cc=alexyonghe@tencent.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=wad@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox