[PATCH v3 2/3] seccomp: Introduce new flag SECCOMP_FILTER_FLAG_FILTER_FD

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


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@xxxxxxxxx>
---
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