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 2/3] seccomp: Introduce new flag SECCOMP_FILTER_FLAG_FILTER_FD
Date: Wed, 29 Nov 2023 05:34:39 +0000	[thread overview]
Message-ID: <20231129053440.41522-3-hengqi.chen@gmail.com> (raw)
In-Reply-To: <20231129053440.41522-1-hengqi.chen@gmail.com>

Add a new flag SECCOMP_FILTER_FLAG_FILTER_FD for SECCOMP_SET_MODE_FILTER.
This indicates user supply a seccomp filter fd, not a sock_fprog.
It allows us to attach the seccomp filter that is previously loaded via
SECCOMP_LOAD_FILTER.

Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
---
 include/linux/seccomp.h      |  3 ++-
 include/uapi/linux/seccomp.h |  2 ++
 kernel/seccomp.c             | 44 +++++++++++++++++++++++++++++++++---
 3 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/include/linux/seccomp.h b/include/linux/seccomp.h
index 175079552f68..3257042c35fc 100644
--- a/include/linux/seccomp.h
+++ b/include/linux/seccomp.h
@@ -9,7 +9,8 @@
 					 SECCOMP_FILTER_FLAG_SPEC_ALLOW | \
 					 SECCOMP_FILTER_FLAG_NEW_LISTENER | \
 					 SECCOMP_FILTER_FLAG_TSYNC_ESRCH | \
-					 SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV)
+					 SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV | \
+					 SECCOMP_FILTER_FLAG_FILTER_FD)
 
 /* sizeof() the first published struct seccomp_notif_addfd */
 #define SECCOMP_NOTIFY_ADDFD_SIZE_VER0 24
diff --git a/include/uapi/linux/seccomp.h b/include/uapi/linux/seccomp.h
index ee2c83697810..41e0ca56ef1c 100644
--- a/include/uapi/linux/seccomp.h
+++ b/include/uapi/linux/seccomp.h
@@ -26,6 +26,8 @@
 #define SECCOMP_FILTER_FLAG_TSYNC_ESRCH		(1UL << 4)
 /* Received notifications wait in killable state (only respond to fatal signals) */
 #define SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV	(1UL << 5)
+/* Indicates that the filter is a fd obtained from SECCOMP_LOAD_FILTER */
+#define SECCOMP_FILTER_FLAG_FILTER_FD		(1UL << 6)
 
 /*
  * All BPF programs must return a 32-bit value.
diff --git a/kernel/seccomp.c b/kernel/seccomp.c
index ef956c3d15c7..a43a6a6b6b77 100644
--- a/kernel/seccomp.c
+++ b/kernel/seccomp.c
@@ -883,8 +883,11 @@ static long seccomp_attach_filter(unsigned int flags,
 
 	/* Validate resulting filter length. */
 	total_insns = filter->prog->len;
-	for (walker = current->seccomp.filter; walker; walker = walker->prev)
+	for (walker = current->seccomp.filter; walker; walker = walker->prev) {
 		total_insns += walker->prog->len + 4;  /* 4 instr penalty */
+		if (walker == filter)
+			return -EEXIST;
+	}
 	if (total_insns > MAX_INSNS_PER_PATH)
 		return -ENOMEM;
 
@@ -1897,6 +1900,38 @@ static const struct file_operations seccomp_filter_fops = {
 	.release = seccomp_filter_put,
 };
 
+/**
+ * seccomp_prepare_filter_from_fd - prepares filter from a user-supplied fd
+ * @ufd: pointer to fd that refers to a seccomp filter.
+ *
+ * Returns filter on success or an ERR_PTR on failure.
+ */
+static struct seccomp_filter *
+seccomp_prepare_filter_from_fd(const char __user *ufd)
+{
+	struct seccomp_filter *sfilter;
+	struct fd f;
+	int fd;
+
+	if (copy_from_user(&fd, ufd, sizeof(fd)))
+		return ERR_PTR(-EFAULT);
+
+	f = fdget(fd);
+	if (!f.file)
+		return ERR_PTR(-EBADF);
+
+	if (f.file->f_op != &seccomp_filter_fops) {
+		fdput(f);
+		return ERR_PTR(-EINVAL);
+	}
+
+	sfilter = f.file->private_data;
+	__get_seccomp_filter(sfilter);
+
+	fdput(f);
+	return sfilter;
+}
+
 /**
  * seccomp_set_mode_filter: internal function for setting seccomp filter
  * @flags:  flags to change filter behavior
@@ -1944,7 +1979,10 @@ static long seccomp_set_mode_filter(unsigned int flags,
 		return -EINVAL;
 
 	/* Prepare the new filter before holding any locks. */
-	prepared = seccomp_prepare_user_filter(filter);
+	if (flags & SECCOMP_FILTER_FLAG_FILTER_FD)
+		prepared = seccomp_prepare_filter_from_fd(filter);
+	else
+		prepared = seccomp_prepare_user_filter(filter);
 	if (IS_ERR(prepared))
 		return PTR_ERR(prepared);
 
@@ -2005,7 +2043,7 @@ static long seccomp_set_mode_filter(unsigned int flags,
 		}
 	}
 out_free:
-	seccomp_filter_free(prepared);
+	__put_seccomp_filter(prepared);
 	return ret;
 }
 
-- 
2.34.1


  parent 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 ` [PATCH v3 1/3] seccomp: Introduce SECCOMP_LOAD_FILTER operation Hengqi Chen
2023-11-29  5:34 ` Hengqi Chen [this message]
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-3-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