Skip to contentSkip to conversation

bpf: Add seccomp program type

PATCH In review Hengqi Chen 4 participants 14 messages Oct 31, 2023 – Nov 3, 2023

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

cover bpf: Add seccomp program type

This patchset introduces seccomp program type which can be used to attach to the existing seccomp framework.

The motivation is to enable sharing of seccomp filter through bpf prog fd and bpffs. With this in place, we can eliminate a hot path of JITing cBPF program (seccomp filter) where we apply the same seccomp filter to thousands of micro VMs on a bare metal instance.

This also allows us to write seccomp filter in an intuitive way, see selftests for reference.

Hengqi Chen (6):
bpf: Introduce BPF_PROG_TYPE_SECCOMP
bpf: Add test_run support for seccomp program type
seccomp: Refactor filter copy/create for reuse
seccomp: Support attaching BPF_PROG_TYPE_SECCOMP progs selftests/bpf: Add seccomp verifier tests selftests/bpf: Test BPF_PROG_TYPE_SECCOMP

 include/linux/bpf.h                           |   3 +
 include/linux/bpf_types.h                     |   4 +
 include/linux/seccomp.h                       |   3 +-
 include/uapi/linux/bpf.h                      |   1 +
 include/uapi/linux/seccomp.h                  |   2 +
 kernel/seccomp.c                              | 142 ++++++++++++++--
 net/bpf/test_run.c                            |  27 +++
 tools/include/uapi/linux/bpf.h                |   1 +
 tools/include/uapi/linux/seccomp.h            |   2 +
 tools/lib/bpf/libbpf.c                        |   2 +
 tools/lib/bpf/libbpf_probes.c                 |   1 +
 .../selftests/bpf/prog_tests/seccomp.c        |  40 +++++
 .../selftests/bpf/prog_tests/verifier.c       |   2 +
 .../selftests/bpf/progs/test_seccomp.c        |  24 +++
 .../selftests/bpf/progs/verifier_seccomp.c    | 154 ++++++++++++++++++
 15 files changed, 390 insertions(+), 18 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/seccomp.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_seccomp.c
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_seccomp.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/6 bpf: Introduce BPF_PROG_TYPE_SECCOMP

This adds minimal support for seccomp eBPF programs which can be hooked into the existing seccomp framework. This allows users to write seccomp filter in eBPF language and enables seccomp filter reuse through bpf prog fd and bpffs. Currently, no helper calls are allowed just like its cBPF version.

---
 include/linux/bpf_types.h     |  4 +++
 include/uapi/linux/bpf.h      |  1 +
 kernel/seccomp.c              | 54 +++++++++++++++++++++++++++++++++++
 tools/lib/bpf/libbpf.c        |  2 ++
 tools/lib/bpf/libbpf_probes.c |  1 +
 5 files changed, 62 insertions(+)
include/linux/bpf_types.h +4 -0
index fc0d6f32c687..7c0a9fc0b150 100644
@@ -83,6 +83,10 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_SYSCALL, bpf_syscall,
 BPF_PROG_TYPE(BPF_PROG_TYPE_NETFILTER, netfilter,
 	      struct bpf_nf_ctx, struct bpf_nf_ctx)
 #endif
+#ifdef CONFIG_SECCOMP_FILTER
+BPF_PROG_TYPE(BPF_PROG_TYPE_SECCOMP, seccomp,
+	      struct seccomp_data, struct seccomp_data)
+#endif
 
 BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY, array_map_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_PERCPU_ARRAY, percpu_array_map_ops)
include/uapi/linux/bpf.h +1 -0
index 0f6cdf52b1da..f0fcfe0ccb2e 100644
@@ -995,6 +995,7 @@ enum bpf_prog_type {
 	BPF_PROG_TYPE_SK_LOOKUP,
 	BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */
 	BPF_PROG_TYPE_NETFILTER,
+	BPF_PROG_TYPE_SECCOMP,
 };
 
 enum bpf_attach_type {
kernel/seccomp.c +54 -0

63 lines not on this page · raw message

tools/lib/bpf/libbpf.c +2 -0
index e067be95da3c..455d733f7315 100644
@@ -217,6 +217,7 @@ static const char * const prog_type_name[] = {
 	[BPF_PROG_TYPE_SK_LOOKUP]		= "sk_lookup",
 	[BPF_PROG_TYPE_SYSCALL]			= "syscall",
 	[BPF_PROG_TYPE_NETFILTER]		= "netfilter",
+	[BPF_PROG_TYPE_SECCOMP]			= "seccomp",
 };
 
 static int __base_pr(enum libbpf_print_level level, const char *format,
@@ -8991,6 +8992,7 @@ static const struct bpf_sec_def section_defs[] = {
 	SEC_DEF("struct_ops.s+",	STRUCT_OPS, 0, SEC_SLEEPABLE),
 	SEC_DEF("sk_lookup",		SK_LOOKUP, BPF_SK_LOOKUP, SEC_ATTACHABLE),
 	SEC_DEF("netfilter",		NETFILTER, BPF_NETFILTER, SEC_NONE),
+	SEC_DEF("seccomp",		SECCOMP, 0, SEC_NONE),
 };
 
 int libbpf_register_prog_handler(const char *sec,
tools/lib/bpf/libbpf_probes.c +1 -0
index 9c4db90b92b6..b3ef3c0747be 100644
@@ -180,6 +180,7 @@ static int probe_prog_load(enum bpf_prog_type prog_type,
 	case BPF_PROG_TYPE_SK_REUSEPORT:
 	case BPF_PROG_TYPE_FLOW_DISSECTOR:
 	case BPF_PROG_TYPE_CGROUP_SYSCTL:
+	case BPF_PROG_TYPE_SECCOMP:
 		break;
 	case BPF_PROG_TYPE_NETFILTER:
 		opts.expected_attach_type = BPF_NETFILTER;
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH bpf-next 1/6] bpf: Introduce BPF_PROG_TYPE_SECCOMP

Quoted 8 lines · Hengqi Chen

This adds minimal support for seccomp eBPF programs which can be hooked into the existing seccomp framework. This allows users to write seccomp filter in eBPF language and enables seccomp filter reuse through bpf prog fd and bpffs. Currently, no helper calls are allowed just like its cBPF version.

Quoted 9 lines
---
 include/linux/bpf_types.h     |  4 +++
 include/uapi/linux/bpf.h      |  1 +
 kernel/seccomp.c              | 54 +++++++++++++++++++++++++++++++++++
 tools/lib/bpf/libbpf.c        |  2 ++
 tools/lib/bpf/libbpf_probes.c |  1 +
 5 files changed, 62 insertions(+)

Let's make sure that bpf_prog_load_check_attach() errors out on non-zero attach type for this new program type?

Ideally, if you can, let's refactor bpf_prog_load_check_attach() in such a way as to default to failing on non-zero attach type for any new program type. You'll need to explicitly list program types for which we don't enforce attach type.

Thanks!

Quoted 131 lines
include/linux/bpf_types.h +4 -0
index fc0d6f32c687..7c0a9fc0b150 100644
@@ -83,6 +83,10 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_SYSCALL, bpf_syscall,
 BPF_PROG_TYPE(BPF_PROG_TYPE_NETFILTER, netfilter,
              struct bpf_nf_ctx, struct bpf_nf_ctx)
 #endif
+#ifdef CONFIG_SECCOMP_FILTER
+BPF_PROG_TYPE(BPF_PROG_TYPE_SECCOMP, seccomp,
+             struct seccomp_data, struct seccomp_data)
+#endif
 
 BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY, array_map_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_PERCPU_ARRAY, percpu_array_map_ops)
include/uapi/linux/bpf.h +1 -0
index 0f6cdf52b1da..f0fcfe0ccb2e 100644
@@ -995,6 +995,7 @@ enum bpf_prog_type {
        BPF_PROG_TYPE_SK_LOOKUP,
        BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */
        BPF_PROG_TYPE_NETFILTER,
+       BPF_PROG_TYPE_SECCOMP,
 };
 
 enum bpf_attach_type {
kernel/seccomp.c +54 -0
index 255999ba9190..5a6ed8630566 100644
@@ -15,6 +15,7 @@
  */
 #define pr_fmt(fmt) "seccomp: " fmt
 
+#include <linux/bpf.h>
 #include <linux/refcount.h>
 #include <linux/audit.h>
 #include <linux/compat.h>
@@ -2513,3 +2514,56 @@ int proc_pid_seccomp_cache(struct seq_file *m, struct pid_namespace *ns,
        return 0;
 }
 #endif /* CONFIG_SECCOMP_CACHE_DEBUG */
+
+#if defined(CONFIG_SECCOMP_FILTER) && defined(CONFIG_BPF_SYSCALL)
+const struct bpf_prog_ops seccomp_prog_ops = {
+};
+
+static bool seccomp_is_valid_access(int off, int size, enum bpf_access_type type,
+                                   const struct bpf_prog *prog,
+                                   struct bpf_insn_access_aux *info)
+{
+       if (off < 0 || off >= sizeof(struct seccomp_data))
+               return false;
+
+       if (off % size != 0)
+               return false;
+
+       if (type == BPF_WRITE)
+               return false;
+
+       switch (off) {
+       case bpf_ctx_range(struct seccomp_data, nr):
+               if (size != sizeof_field(struct seccomp_data, nr))
+                       return false;
+               return true;
+       case bpf_ctx_range(struct seccomp_data, arch):
+               if (size != sizeof_field(struct seccomp_data, arch))
+                       return false;
+               return true;
+       case bpf_ctx_range(struct seccomp_data, instruction_pointer):
+               if (size != sizeof_field(struct seccomp_data, instruction_pointer))
+                       return false;
+               return true;
+       case bpf_ctx_range(struct seccomp_data, args):
+               if (size != sizeof(__u64))
+                       return false;
+               return true;
+       default:
+               return false;
+       }
+
+       return false;
+}
+
+static const struct bpf_func_proto *
+bpf_seccomp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
+{
+       return NULL;
+}
+
+const struct bpf_verifier_ops seccomp_verifier_ops = {
+       .is_valid_access = seccomp_is_valid_access,
+       .get_func_proto  = bpf_seccomp_func_proto,
+};
+#endif /* CONFIG_SECCOMP_FILTER && CONFIG_BPF_SYSCALL */
tools/lib/bpf/libbpf.c +2 -0
index e067be95da3c..455d733f7315 100644
@@ -217,6 +217,7 @@ static const char * const prog_type_name[] = {
        [BPF_PROG_TYPE_SK_LOOKUP]               = "sk_lookup",
        [BPF_PROG_TYPE_SYSCALL]                 = "syscall",
        [BPF_PROG_TYPE_NETFILTER]               = "netfilter",
+       [BPF_PROG_TYPE_SECCOMP]                 = "seccomp",
 };
 
 static int __base_pr(enum libbpf_print_level level, const char *format,
@@ -8991,6 +8992,7 @@ static const struct bpf_sec_def section_defs[] = {
        SEC_DEF("struct_ops.s+",        STRUCT_OPS, 0, SEC_SLEEPABLE),
        SEC_DEF("sk_lookup",            SK_LOOKUP, BPF_SK_LOOKUP, SEC_ATTACHABLE),
        SEC_DEF("netfilter",            NETFILTER, BPF_NETFILTER, SEC_NONE),
+       SEC_DEF("seccomp",              SECCOMP, 0, SEC_NONE),
 };
 
 int libbpf_register_prog_handler(const char *sec,
tools/lib/bpf/libbpf_probes.c +1 -1
index 9c4db90b92b6..b3ef3c0747be 100644
@@ -180,6 +180,7 @@ static int probe_prog_load(enum bpf_prog_type prog_type,
        case BPF_PROG_TYPE_SK_REUSEPORT:
        case BPF_PROG_TYPE_FLOW_DISSECTOR:
        case BPF_PROG_TYPE_CGROUP_SYSCTL:
+       case BPF_PROG_TYPE_SECCOMP:
                break;
        case BPF_PROG_TYPE_NETFILTER:
                opts.expected_attach_type = BPF_NETFILTER;
--

2.34.1

maintainer
#3 lore raw

Re: [PATCH bpf-next 1/6] bpf: Introduce BPF_PROG_TYPE_SECCOMP

In reply to Hengqi Chen
Quoted 6 lines · Hengqi Chen

This adds minimal support for seccomp eBPF programs which can be hooked into the existing seccomp framework. This allows users to write seccomp filter in eBPF language and enables seccomp filter reuse through bpf prog fd and bpffs. Currently, no helper calls are allowed just like its cBPF version.

I think this is bypassing the seccomp bitmap generation pass, so this will break (at least) performance.

I continue to prefer sticking to only cBPF for seccomp, so let's just use the seccomp syscall to generate the fds.

-Kees

Signature · 1 line
Kees Cook
#4 lore raw

Re: [PATCH bpf-next 1/6] bpf: Introduce BPF_PROG_TYPE_SECCOMP

Quoted 11 lines · Kees Cook
Quoted 6 lines · Hengqi Chen

This adds minimal support for seccomp eBPF programs which can be hooked into the existing seccomp framework. This allows users to write seccomp filter in eBPF language and enables seccomp filter reuse through bpf prog fd and bpffs. Currently, no helper calls are allowed just like its cBPF version.

Quoted 3 lines

I think this is bypassing the seccomp bitmap generation pass, so this will break (at least) performance.

I continue to prefer sticking to only cBPF for seccomp, so let's just use the seccomp syscall to generate the fds.

That's fine, but let's not mix old things with bpffs, bpftool, etc. If you want an anon_fd then go ahead and allocate it standalone. It shouldn't be confused with eBPF fd-s.
No bpffs treatment and no bpftool visibility.

maintainer
#5 lore raw

Re: [PATCH bpf-next 1/6] bpf: Introduce BPF_PROG_TYPE_SECCOMP

Quoted 12 lines · Alexei Starovoitov
Quoted 11 lines · Kees Cook
Quoted 6 lines · Hengqi Chen

This adds minimal support for seccomp eBPF programs which can be hooked into the existing seccomp framework. This allows users to write seccomp filter in eBPF language and enables seccomp filter reuse through bpf prog fd and bpffs. Currently, no helper calls are allowed just like its cBPF version.

Quoted 8 lines
Quoted 3 lines

I think this is bypassing the seccomp bitmap generation pass, so this will break (at least) performance.

I continue to prefer sticking to only cBPF for seccomp, so let's just use the seccomp syscall to generate the fds.

That's fine, but let's not mix old things with bpffs, bpftool, etc. If you want an anon_fd then go ahead and allocate it standalone. It shouldn't be confused with eBPF fd-s.
No bpffs treatment and no bpftool visibility.

Agreed. Let's just emit an anon_fd from the seccomp syscall.

Signature · 1 line
Kees Cook
#6 lore raw

Re: [PATCH bpf-next 1/6] bpf: Introduce BPF_PROG_TYPE_SECCOMP

In reply to Kees Cook

Hi, Kees:

Quoted 4 lines · Kees Cook

On October 30, 2023 6:24:02 PM PDT, Hengqi Chen <[email protected]> wrote:

Quoted 9 lines
Quoted 6 lines

This adds minimal support for seccomp eBPF programs which can be hooked into the existing seccomp framework. This allows users to write seccomp filter in eBPF language and enables seccomp filter reuse through bpf prog fd and bpffs. Currently, no helper calls are allowed just like its cBPF version.

I think this is bypassing the seccomp bitmap generation pass, so this will break (at least) performance.

What if we did the same for eBPF, a bit harder though, does that address your concerns ?

Quoted 2 lines

I continue to prefer sticking to only cBPF for seccomp, so let's just use the seccomp syscall to generate the fds.

That's an alternative. But as Alexei said, there would be no more bpffs things. AFAIK, we could only share the filter via UDS.

Quoted 4 lines

-Kees

--
Kees Cook

#7 lore raw

Re: [PATCH bpf-next 1/6] bpf: Introduce BPF_PROG_TYPE_SECCOMP

Quoted 18 lines · Hengqi Chen

Hi, Kees:

Quoted 4 lines · Kees Cook

On October 30, 2023 6:24:02 PM PDT, Hengqi Chen <[email protected]> wrote:

Quoted 9 lines
Quoted 6 lines

This adds minimal support for seccomp eBPF programs which can be hooked into the existing seccomp framework. This allows users to write seccomp filter in eBPF language and enables seccomp filter reuse through bpf prog fd and bpffs. Currently, no helper calls are allowed just like its cBPF version.

I think this is bypassing the seccomp bitmap generation pass, so this will break (at least) performance.

Quoted 9 lines

What if we did the same for eBPF, a bit harder though, does that address your concerns ?

Quoted 2 lines

I continue to prefer sticking to only cBPF for seccomp, so let's just use the seccomp syscall to generate the fds.

That's an alternative. But as Alexei said, there would be no more bpffs things. AFAIK, we could only share the filter via UDS.

Just take a deeper look, there are too many
registers/instructions/states in eBPF,
stick to cBPF would be easier for now.

Quoted 4 lines
Quoted 4 lines

-Kees

--
Kees Cook

#8 lore raw

PATCH 2/6 bpf: Add test_run support for seccomp program type

In reply to Hengqi Chen

Implement test_run for seccomp program type. Default is to use an empty struct seccomp_data as bpf_context, but can be overridden by userspace. This will be used in selftests.

---
 include/linux/bpf.h |  3 +++
 kernel/seccomp.c    |  1 +
 net/bpf/test_run.c  | 27 +++++++++++++++++++++++++++
 3 files changed, 31 insertions(+)
include/linux/bpf.h +3 -0
index b4825d3cdb29..e25338e67ec4 100644
@@ -2376,6 +2376,9 @@ int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
 int bpf_prog_test_run_nf(struct bpf_prog *prog,
 			 const union bpf_attr *kattr,
 			 union bpf_attr __user *uattr);
+int bpf_prog_test_run_seccomp(struct bpf_prog *prog,
+			      const union bpf_attr *kattr,
+			      union bpf_attr __user *uattr);
 bool btf_ctx_access(int off, int size, enum bpf_access_type type,
 		    const struct bpf_prog *prog,
 		    struct bpf_insn_access_aux *info);
kernel/seccomp.c +1 -0
index 5a6ed8630566..1fa2312654a5 100644
@@ -2517,6 +2517,7 @@ int proc_pid_seccomp_cache(struct seq_file *m, struct pid_namespace *ns,
 
 #if defined(CONFIG_SECCOMP_FILTER) && defined(CONFIG_BPF_SYSCALL)
 const struct bpf_prog_ops seccomp_prog_ops = {
+	.test_run = bpf_prog_test_run_seccomp,
 };
 
 static bool seccomp_is_valid_access(int off, int size, enum bpf_access_type type,
net/bpf/test_run.c +27 -0
index 0841f8d82419..db159b9c56ca 100644
@@ -20,6 +20,7 @@
 #include <linux/smp.h>
 #include <linux/sock_diag.h>
 #include <linux/netfilter.h>
+#include <linux/seccomp.h>
 #include <net/netdev_rx_queue.h>
 #include <net/xdp.h>
 #include <net/netfilter/nf_bpf_link.h>
@@ -1665,6 +1666,32 @@ int bpf_prog_test_run_nf(struct bpf_prog *prog,
 	return ret;
 }
 
+int bpf_prog_test_run_seccomp(struct bpf_prog *prog,
+			      const union bpf_attr *kattr,
+			      union bpf_attr __user *uattr)
+{
+	void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
+	__u32 ctx_size_in = kattr->test.ctx_size_in;
+	struct seccomp_data ctx = {};
+	__u32 retval;
+
+	if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
+		return -EINVAL;
+
+	if (ctx_size_in && ctx_size_in < sizeof(ctx))
+		return -EINVAL;
+
+	if (ctx_size_in && copy_from_user(&ctx, ctx_in, sizeof(ctx)))
+		return -EFAULT;
+
+	retval = bpf_prog_run_pin_on_cpu(prog, &ctx);
+
+	if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
+		return -EFAULT;
+
+	return 0;
+}
+
 static const struct btf_kfunc_id_set bpf_prog_test_kfunc_set = {
 	.owner = THIS_MODULE,
 	.set   = &test_sk_check_kfunc_ids,
Signature · 1 line
2.34.1
#9 lore raw

Re: [PATCH bpf-next 2/6] bpf: Add test_run support for seccomp program type

Quoted 57 lines · Hengqi Chen

Implement test_run for seccomp program type. Default is to use an empty struct seccomp_data as bpf_context, but can be overridden by userspace. This will be used in selftests.

---
 include/linux/bpf.h |  3 +++
 kernel/seccomp.c    |  1 +
 net/bpf/test_run.c  | 27 +++++++++++++++++++++++++++
 3 files changed, 31 insertions(+)
include/linux/bpf.h +3 -0
index b4825d3cdb29..e25338e67ec4 100644
@@ -2376,6 +2376,9 @@ int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
 int bpf_prog_test_run_nf(struct bpf_prog *prog,
                         const union bpf_attr *kattr,
                         union bpf_attr __user *uattr);
+int bpf_prog_test_run_seccomp(struct bpf_prog *prog,
+                             const union bpf_attr *kattr,
+                             union bpf_attr __user *uattr);
 bool btf_ctx_access(int off, int size, enum bpf_access_type type,
                    const struct bpf_prog *prog,
                    struct bpf_insn_access_aux *info);
kernel/seccomp.c +1 -0
index 5a6ed8630566..1fa2312654a5 100644
@@ -2517,6 +2517,7 @@ int proc_pid_seccomp_cache(struct seq_file *m, struct pid_namespace *ns,
 
 #if defined(CONFIG_SECCOMP_FILTER) && defined(CONFIG_BPF_SYSCALL)
 const struct bpf_prog_ops seccomp_prog_ops = {
+       .test_run = bpf_prog_test_run_seccomp,
 };
 
 static bool seccomp_is_valid_access(int off, int size, enum bpf_access_type type,
net/bpf/test_run.c +3 -0
index 0841f8d82419..db159b9c56ca 100644
@@ -20,6 +20,7 @@
 #include <linux/smp.h>
 #include <linux/sock_diag.h>
 #include <linux/netfilter.h>
+#include <linux/seccomp.h>
 #include <net/netdev_rx_queue.h>
 #include <net/xdp.h>
 #include <net/netfilter/nf_bpf_link.h>
@@ -1665,6 +1666,32 @@ int bpf_prog_test_run_nf(struct bpf_prog *prog,
        return ret;
 }
 
+int bpf_prog_test_run_seccomp(struct bpf_prog *prog,
+                             const union bpf_attr *kattr,

net/bpf/test_run.c int bpf_prog_test_run_nf(struct bpf_prog *prog, · line 1680

Quoted 10 lines
net/bpf/test_run.c +10 -0
+                             union bpf_attr __user *uattr)
+{
+       void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
+       __u32 ctx_size_in = kattr->test.ctx_size_in;
+       struct seccomp_data ctx = {};
+       __u32 retval;
+
+       if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
+               return -EINVAL;
+

what about ctx_out, ctx_size_out, data_size_in/data_size_out, etc, etc. Should we enforce that they all stay zero? Similar questions to repeat and duration.

Quoted 20 lines
net/bpf/test_run.c +14 -1
+       if (ctx_size_in && ctx_size_in < sizeof(ctx))
+               return -EINVAL;
+
+       if (ctx_size_in && copy_from_user(&ctx, ctx_in, sizeof(ctx)))
+               return -EFAULT;
+
+       retval = bpf_prog_run_pin_on_cpu(prog, &ctx);
+
+       if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
+               return -EFAULT;
+
+       return 0;
+}
+
 static const struct btf_kfunc_id_set bpf_prog_test_kfunc_set = {
        .owner = THIS_MODULE,
        .set   = &test_sk_check_kfunc_ids,
--

2.34.1

#10 lore raw

PATCH 3/6 seccomp: Refactor filter copy/create for reuse

In reply to Hengqi Chen

This refactors seccomp_prepare_filter() for reuse in subsequent additions. No functional change intended.

---
 kernel/seccomp.c | 50 +++++++++++++++++++++++++++++++++---------------
 1 file changed, 35 insertions(+), 15 deletions(-)
kernel/seccomp.c +35 -15
index 1fa2312654a5..2a724690a627 100644
@@ -641,14 +641,14 @@ static inline void seccomp_sync_threads(unsigned long flags)
 }
 
 /**
- * seccomp_prepare_filter: Prepares a seccomp filter for use.
- * @fprog: BPF program to install
+ * seccomp_prepare_prog - prepares a JITed BPF filter for use.
+ * @pfp: the unattached filter that is created
+ * @fprog: the filter program
  *
- * Returns filter on success or an ERR_PTR on failure.
+ * Returns 0 on success and non-zero otherwise.
  */
-static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
+static int seccomp_prepare_prog(struct bpf_prog **pfp, struct sock_fprog *fprog)
 {
-	struct seccomp_filter *sfilter;
 	int ret;
 	const bool save_orig =
 #if defined(CONFIG_CHECKPOINT_RESTORE) || defined(SECCOMP_ARCH_NATIVE)
@@ -658,10 +658,27 @@ static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
 #endif
 
 	if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
-		return ERR_PTR(-EINVAL);
+		return -EINVAL;
 
 	BUG_ON(INT_MAX / fprog->len < sizeof(struct sock_filter));
 
+	ret = bpf_prog_create_from_user(pfp, fprog, seccomp_check_filter, save_orig);
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+
+/**
+ * seccomp_prepare_filter: Prepares a seccomp filter for use.
+ * @prog: BPF program to install
+ *
+ * Returns filter on success or an ERR_PTR on failure.
+ */
+static struct seccomp_filter *seccomp_prepare_filter(struct bpf_prog *prog)
+{
+	struct seccomp_filter *sfilter;
+
 	/*
 	 * Installing a seccomp filter requires that the task has
 	 * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
@@ -678,13 +695,7 @@ static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
 		return ERR_PTR(-ENOMEM);
 
 	mutex_init(&sfilter->notify_lock);
-	ret = bpf_prog_create_from_user(&sfilter->prog, fprog,
-					seccomp_check_filter, save_orig);
-	if (ret < 0) {
-		kfree(sfilter);
-		return ERR_PTR(ret);
-	}
-
+	sfilter->prog = prog;
 	refcount_set(&sfilter->refs, 1);
 	refcount_set(&sfilter->users, 1);
 	init_waitqueue_head(&sfilter->wqh);
@@ -701,8 +712,10 @@ static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
 static struct seccomp_filter *
 seccomp_prepare_user_filter(const char __user *user_filter)
 {
-	struct sock_fprog fprog;
 	struct seccomp_filter *filter = ERR_PTR(-EFAULT);
+	struct sock_fprog fprog;
+	struct bpf_prog *prog;
+	int ret;
 
 #ifdef CONFIG_COMPAT
 	if (in_compat_syscall()) {
@@ -715,7 +728,14 @@ seccomp_prepare_user_filter(const char __user *user_filter)
 #endif
 	if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
 		goto out;
-	filter = seccomp_prepare_filter(&fprog);
+
+	ret = seccomp_prepare_prog(&prog, &fprog);
+	if (ret)
+		return ERR_PTR(ret);
+
+	filter = seccomp_prepare_filter(prog);
+	if (IS_ERR(filter))
+		bpf_prog_destroy(prog);
 out:
 	return filter;
 }
Signature · 1 line
2.34.1
#11 lore raw

PATCH 4/6 seccomp: Support attaching BPF_PROG_TYPE_SECCOMP progs

In reply to Hengqi Chen

Add a new flag SECCOMP_FILTER_FLAG_BPF_PROG_FD for SECCOMP_SET_MODE_FILTER, which indicates the seccomp filter is a seccomp bpf prog fd, not a sock_fprog. This allows us to attach seccomp filter that is previously loaded via bpf syscall.

---
 include/linux/seccomp.h      |  3 ++-
 include/uapi/linux/seccomp.h |  2 ++
 kernel/seccomp.c             | 37 ++++++++++++++++++++++++++++++++++--
 3 files changed, 39 insertions(+), 3 deletions(-)
include/linux/seccomp.h +2 -1
index 175079552f68..7caa53b629d9 100644
@@ -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_BPF_PROG_FD)
 
 /* sizeof() the first published struct seccomp_notif_addfd */
 #define SECCOMP_NOTIFY_ADDFD_SIZE_VER0 24
include/uapi/linux/seccomp.h +2 -0
index dbfc9b37fcae..db792dc96b5a 100644
@@ -25,6 +25,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 in form of bpf prog fd */
+#define SECCOMP_FILTER_FLAG_BPF_PROG_FD		(1UL << 6)
 
 /*
  * All BPF programs must return a 32-bit value.
kernel/seccomp.c +35 -2
index 2a724690a627..f88dc7880cfa 100644
@@ -524,7 +524,10 @@ static inline pid_t seccomp_can_sync_threads(void)
 static inline void seccomp_filter_free(struct seccomp_filter *filter)
 {
 	if (filter) {
-		bpf_prog_destroy(filter->prog);
+		if (filter->prog->type == BPF_PROG_TYPE_SECCOMP)
+			bpf_prog_put(filter->prog);
+		else
+			bpf_prog_destroy(filter->prog);
 		kfree(filter);
 	}
 }
@@ -740,6 +743,33 @@ seccomp_prepare_user_filter(const char __user *user_filter)
 	return filter;
 }
 
+/**
+ * seccomp_prepare_filter_from_fd - prepares filter from a user-supplied fd
+ * @ufd: pointer to fd that refers to a seccomp bpf prog.
+ *
+ * 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 bpf_prog *prog;
+	int fd;
+
+	if (copy_from_user(&fd, ufd, sizeof(fd)))
+		return ERR_PTR(-EFAULT);
+
+	prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SECCOMP);
+	if (IS_ERR(prog))
+		return ERR_PTR(-EBADF);
+
+	sfilter = seccomp_prepare_filter(prog);
+	if (IS_ERR(sfilter))
+		bpf_prog_put(prog);
+
+	return sfilter;
+}
+
 #ifdef SECCOMP_ARCH_NATIVE
 /**
  * seccomp_is_const_allow - check if filter is constant allow with given data
@@ -1953,7 +1983,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_BPF_PROG_FD)
+		prepared = seccomp_prepare_filter_from_fd(filter);
+	else
+		prepared = seccomp_prepare_user_filter(filter);
 	if (IS_ERR(prepared))
 		return PTR_ERR(prepared);
 
Signature · 1 line
2.34.1
#12 lore raw

PATCH 5/6 selftests/bpf: Add seccomp verifier tests

In reply to Hengqi Chen

This tests seccomp context access and helper call
restriction.

  # ./test_progs -t verifier_seccomp
  #375/1   verifier_seccomp/seccomp no helper call:OK
  #375/2   verifier_seccomp/seccomp invalid ctx access, write:OK
  #375/3   verifier_seccomp/seccomp invalid ctx access, out of range:OK
  #375/4   verifier_seccomp/seccomp invalid ctx access, size too short:OK
  #375/5   verifier_seccomp/seccomp invalid ctx access, size too short:OK
  #375/6   verifier_seccomp/seccomp invalid ctx access, size too short:OK
  #375/7   verifier_seccomp/seccomp invalid ctx access, size too short:OK
  #375/8   verifier_seccomp/seccomp invalid ctx access, size too large:OK
  #375/9   verifier_seccomp/seccomp ctx access, valid:OK
  #375     verifier_seccomp:OK
  Summary: 1/9 PASSED, 0 SKIPPED, 0 FAILED
---
 .../selftests/bpf/prog_tests/verifier.c       |   2 +
 .../selftests/bpf/progs/verifier_seccomp.c    | 154 ++++++++++++++++++
 2 files changed, 156 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_seccomp.c
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
index e3e68c97b40c..dfb40a11939e 100644
@@ -57,6 +57,7 @@
 #include "verifier_scalar_ids.skel.h"
 #include "verifier_sdiv.skel.h"
 #include "verifier_search_pruning.skel.h"
+#include "verifier_seccomp.skel.h"
 #include "verifier_sock.skel.h"
 #include "verifier_spill_fill.skel.h"
 #include "verifier_spin_lock.skel.h"
@@ -164,6 +165,7 @@ void test_verifier_runtime_jit(void)          { RUN(verifier_runtime_jit); }
 void test_verifier_scalar_ids(void)           { RUN(verifier_scalar_ids); }
 void test_verifier_sdiv(void)                 { RUN(verifier_sdiv); }
 void test_verifier_search_pruning(void)       { RUN(verifier_search_pruning); }
+void test_verifier_seccomp(void)              { RUN(verifier_seccomp); }
 void test_verifier_sock(void)                 { RUN(verifier_sock); }
 void test_verifier_spill_fill(void)           { RUN(verifier_spill_fill); }
 void test_verifier_spin_lock(void)            { RUN(verifier_spin_lock); }
tools/testing/selftests/bpf/progs/verifier_seccomp.c new file +154 -0

154 lines not on this page · raw message

Signature · 1 line
2.34.1
#13 lore raw

PATCH 6/6 selftests/bpf: Test BPF_PROG_TYPE_SECCOMP

In reply to Hengqi Chen

Add a testcase to exercise BPF_PROG_TYPE_SECCOMP.

  # ./test_progs -n 194
  #194     seccomp:OK
  Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
---
 tools/include/uapi/linux/bpf.h                |  1 +
 tools/include/uapi/linux/seccomp.h            |  2 +
 .../selftests/bpf/prog_tests/seccomp.c        | 40 +++++++++++++++++++
 .../selftests/bpf/progs/test_seccomp.c        | 24 +++++++++++
 4 files changed, 67 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/seccomp.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_seccomp.c
tools/include/uapi/linux/bpf.h +1 -0
index 0f6cdf52b1da..f0fcfe0ccb2e 100644
@@ -995,6 +995,7 @@ enum bpf_prog_type {
 	BPF_PROG_TYPE_SK_LOOKUP,
 	BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */
 	BPF_PROG_TYPE_NETFILTER,
+	BPF_PROG_TYPE_SECCOMP,
 };
 
 enum bpf_attach_type {
tools/include/uapi/linux/seccomp.h +2 -0
index dbfc9b37fcae..db792dc96b5a 100644
@@ -25,6 +25,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 in form of bpf prog fd */
+#define SECCOMP_FILTER_FLAG_BPF_PROG_FD		(1UL << 6)
 
 /*
  * All BPF programs must return a 32-bit value.
tools/testing/selftests/bpf/prog_tests/seccomp.c new file +40 -0
new file mode 100644
index 000000000000..fc7db6af7d64
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2023 Hengqi Chen */
+
+#include <test_progs.h>
+#include <linux/seccomp.h>
+#include "test_seccomp.skel.h"
+
+static int seccomp(unsigned int op, unsigned int flags, void *args)
+{
+	errno = 0;
+	return syscall(__NR_seccomp, op, flags, args);
+}
+
+void test_seccomp(void)
+{
+	struct test_seccomp *skel;
+	int fd, flags, ret;
+
+	skel = test_seccomp__open();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return;
+
+	skel->rodata->seccomp_syscall_nr = __NR_seccomp;
+	skel->rodata->seccomp_errno = 99;
+
+	ret = test_seccomp__load(skel);
+	if (!ASSERT_OK(ret, "skel_load"))
+		goto cleanup;
+
+	fd = bpf_program__fd(skel->progs.seccomp_prog);
+	flags = SECCOMP_FILTER_FLAG_BPF_PROG_FD;
+	ret = seccomp(SECCOMP_SET_MODE_FILTER, flags, &fd);
+	ASSERT_OK(ret, "seccomp_set_bpf_prog");
+	ret = seccomp(SECCOMP_SET_MODE_FILTER, flags, &fd);
+	ASSERT_EQ(ret, -1, "seccomp should fail");
+	ASSERT_EQ(errno, 99, "errno not equal to 99");
+
+cleanup:
+	test_seccomp__destroy(skel);
+}
tools/testing/selftests/bpf/progs/test_seccomp.c new file +24 -0
new file mode 100644
index 000000000000..c53e75b8c0ec
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2023 Hengqi Chen */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+#define SECCOMP_RET_ERRNO	0x00050000U
+#define SECCOMP_RET_ALLOW	0x7fff0000U
+#define SECCOMP_RET_DATA	0x0000ffffU
+
+const volatile int seccomp_syscall_nr = 0;
+const volatile __u32 seccomp_errno = 0;
+
+SEC("seccomp")
+int seccomp_prog(struct seccomp_data *ctx)
+{
+	if (ctx->nr != seccomp_syscall_nr)
+		return SECCOMP_RET_ALLOW;
+
+	return SECCOMP_RET_ERRNO | (seccomp_errno & SECCOMP_RET_DATA);
+}
+
+char _license[] SEC("license") = "GPL";
Signature · 1 line
2.34.1