Add a test kfunc that takes a trusted struct ns_common and check that: - namespace_init and namespace_free arguments can't be passed to it - namespace_install arguments can The test fails if namespace_init and namespace_free are treated as trusted. Signed-off-by: Christian Brauner (Amutable) --- tools/testing/selftests/bpf/prog_tests/verifier.c | 2 ++ .../selftests/bpf/progs/verifier_lsm_namespace.c | 38 ++++++++++++++++++++++ .../testing/selftests/bpf/test_kmods/bpf_testmod.c | 5 +++ .../selftests/bpf/test_kmods/bpf_testmod_kfunc.h | 1 + 4 files changed, 46 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c index 04740ba37e91..3963a4f71d98 100644 --- a/tools/testing/selftests/bpf/prog_tests/verifier.c +++ b/tools/testing/selftests/bpf/prog_tests/verifier.c @@ -147,6 +147,7 @@ #include "verifier_set_retval.skel.h" #include "verifier_lsm.skel.h" #include "verifier_lsm_init_xattr.skel.h" +#include "verifier_lsm_namespace.skel.h" #include "verifier_jit_inline.skel.h" #include "irq.skel.h" #include "verifier_ctx_ptr_param.skel.h" @@ -337,6 +338,7 @@ void test_verifier_bits_iter(void) { RUN(verifier_bits_iter); } void test_verifier_set_retval(void) { RUN(verifier_set_retval); } void test_verifier_lsm(void) { RUN(verifier_lsm); } void test_verifier_lsm_init_xattr(void) { RUN(verifier_lsm_init_xattr); } +void test_verifier_lsm_namespace(void) { RUN_TESTS(verifier_lsm_namespace); } void test_irq(void) { RUN(irq); } void test_verifier_mtu(void) { RUN(verifier_mtu); } void test_verifier_jit_inline(void) { RUN(verifier_jit_inline); } diff --git a/tools/testing/selftests/bpf/progs/verifier_lsm_namespace.c b/tools/testing/selftests/bpf/progs/verifier_lsm_namespace.c new file mode 100644 index 000000000000..d14fa2363dcc --- /dev/null +++ b/tools/testing/selftests/bpf/progs/verifier_lsm_namespace.c @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include + +#include "bpf_misc.h" +#include "../test_kmods/bpf_testmod_kfunc.h" + +/* The namespace hasn't been set up yet. */ +SEC("lsm/namespace_init") +__failure __msg("R1 must be referenced or trusted") +int BPF_PROG(namespace_init_untrusted, struct ns_common *ns) +{ + bpf_kfunc_trusted_ns_test(ns); + return 0; +} + +/* The namespace is being torn down. */ +SEC("lsm/namespace_free") +__failure __msg("R1 must be referenced or trusted") +int BPF_PROG(namespace_free_untrusted, struct ns_common *ns) +{ + bpf_kfunc_trusted_ns_test(ns); + return 0; +} + +/* setns() holds a reference to a fully set up namespace. */ +SEC("lsm/namespace_install") +__success +int BPF_PROG(namespace_install_trusted, const struct nsset *nsset, + struct ns_common *ns) +{ + bpf_kfunc_trusted_ns_test(ns); + return 0; +} + +char _license[] SEC("license") = "GPL"; diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c index 6380f52f4dd2..8d6bd2237f34 100644 --- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c @@ -350,6 +350,10 @@ __bpf_kfunc void bpf_kfunc_trusted_task_test(struct task_struct *ptr) { } +__bpf_kfunc void bpf_kfunc_trusted_ns_test(struct ns_common *ptr) +{ +} + __bpf_kfunc struct task_struct *bpf_kfunc_trusted_null_test(void) { return NULL; @@ -1030,6 +1034,7 @@ BTF_ID_FLAGS(func, bpf_kfunc_nested_acquire_zero_offset_test, KF_ACQUIRE) BTF_ID_FLAGS(func, bpf_kfunc_nested_release_test, KF_RELEASE) BTF_ID_FLAGS(func, bpf_kfunc_trusted_vma_test) BTF_ID_FLAGS(func, bpf_kfunc_trusted_task_test) +BTF_ID_FLAGS(func, bpf_kfunc_trusted_ns_test) BTF_ID_FLAGS(func, bpf_kfunc_trusted_null_test, KF_RET_NULL) BTF_ID_FLAGS(func, bpf_kfunc_trusted_nullable_test) BTF_ID_FLAGS(func, bpf_kfunc_trusted_num_test) diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h index 3b810f1e438f..bc32c4078d27 100644 --- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h +++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h @@ -355,6 +355,7 @@ int bpf_kfunc_st_ops_inc10(struct st_ops_args *args) __ksym; void bpf_kfunc_trusted_vma_test(struct vm_area_struct *ptr) __ksym; void bpf_kfunc_trusted_task_test(struct task_struct *ptr) __ksym; +void bpf_kfunc_trusted_ns_test(struct ns_common *ptr) __ksym; struct task_struct *bpf_kfunc_trusted_null_test(void) __ksym; void bpf_kfunc_trusted_nullable_test(struct task_struct *ptr__nullable) __ksym; void bpf_kfunc_trusted_num_test(int *ptr) __ksym; -- 2.53.0