Re: [PATCH bpf-next v7 5/5] selftests/bpf: Test mm_struct user memory kfuncs with linux_binprm
Andrii Nakryiko <[email protected]>
| Newsgroups | org.kernel.vger.linux-fsdevel,org.kernel.vger.bpf,org.kernel.vger.linux-kernel,org.kvack.linux-mm |
|---|---|
| Message-ID | <CAEf4BzZui5ywrdEz1Sz+XuSEOyFVBnGR_F0SU0vNuhBpaGpqGQ@mail.gmail.com> |
On Tue, Sep 15, 2026 at 1:03 AM Anastasios Papagiannis <[email protected]> wrote: > > Add a sleepable BPF LSM program attached to bprm_check_security to test > bpf_copy_from_user_mm() and bpf_copy_from_user_mm_str() on CONFIG_MMU > kernels. > > Starting at bprm->p, verify that bpf_copy_from_user_mm() can copy the > contiguous NUL-separated argument and environment data. Then use > bpf_copy_from_user_mm_str() to read each argument and environment string > separately, advancing the offset by the length returned from each call. > > Skip the test on !CONFIG_MMU. In that configuration, exec argument and > environment strings remain in bprm->page[] until the binary loader > transfers them to the new process stack, so they are not accessible > through bprm->mm at the bprm_check_security hook. > > Signed-off-by: Anastasios Papagiannis <[email protected]> > --- > .../bpf/prog_tests/copy_from_user_bprm.c | 72 +++++++++++ > .../selftests/bpf/progs/copy_from_user_bprm.c | 122 ++++++++++++++++++ > 2 files changed, 194 insertions(+) > create mode 100644 tools/testing/selftests/bpf/prog_tests/copy_from_user_bprm.c > create mode 100644 tools/testing/selftests/bpf/progs/copy_from_user_bprm.c > > diff --git a/tools/testing/selftests/bpf/prog_tests/copy_from_user_bprm.c b/tools/testing/selftests/bpf/prog_tests/copy_from_user_bprm.c > new file mode 100644 > index 000000000000..ef351cda9348 > --- /dev/null > +++ b/tools/testing/selftests/bpf/prog_tests/copy_from_user_bprm.c > @@ -0,0 +1,72 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +#include <errno.h> > +#include <sys/wait.h> > +#include <unistd.h> > + > +#include <test_progs.h> > + > +#include "copy_from_user_bprm.skel.h" > + > +void test_copy_from_user_bprm(void) > +{ > + char arg0[] = "first"; > + char arg1[] = "second-argument"; > + char env0[] = "SOME_ENV=a"; > + char env1[] = "OTHER_ENV=something"; > + struct copy_from_user_bprm *skel; > + pid_t child; > + int status; > + > + skel = copy_from_user_bprm__open_and_load(); > + if (!ASSERT_OK_PTR(skel, "open_and_load")) > + return; > + > + /* > + * On !CONFIG_MMU, exec strings are held in bprm->page[] rather than > + * being mapped in bprm->mm. > + */ > + if (!skel->kconfig->CONFIG_MMU) { > + printf("%s:SKIP: test requires CONFIG_MMU\n", __func__); > + test__skip(); > + goto out; > + } I'm not sure I'd bother with this !CONFIG_MMU support, tbh > + > + if (!ASSERT_OK(copy_from_user_bprm__attach(skel), "attach")) > + goto out; > + > + child = fork(); > + if (!ASSERT_GE(child, 0, "fork")) > + goto out; > + > + if (!child) { > + char *const argv[] = { arg0, arg1, NULL }; > + char *const envp[] = { env0, env1, NULL }; > + > + skel->bss->monitored_pid = getpid(); > + execvpe("true", argv, envp); > + _exit(errno); > + } > + > + if (!ASSERT_EQ(waitpid(child, &status, 0), child, "waitpid")) > + goto out; > + > + if (ASSERT_TRUE(WIFEXITED(status), "child_exited")) > + ASSERT_EQ(WEXITSTATUS(status), EPERM, "exec_errno"); > + > + ASSERT_EQ(skel->bss->bprm_argc, 2, "bprm_argc"); > + ASSERT_EQ(skel->bss->bprm_envc, 2, "bprm_envc"); > + ASSERT_EQ(skel->bss->data_len_match, 1, "data_len_match"); > + ASSERT_EQ(skel->bss->invalid_flags_ret, -EINVAL, "invalid_flags_ret"); > + ASSERT_EQ(skel->bss->copy_ret, 0, "copy_ret"); > + ASSERT_EQ(skel->bss->str_arg0_ret, sizeof(arg0), "str_arg0_ret"); > + ASSERT_EQ(skel->bss->str_arg1_ret, sizeof(arg1), "str_arg1_ret"); > + ASSERT_EQ(skel->bss->str_env0_ret, sizeof(env0), "str_env0_ret"); > + ASSERT_EQ(skel->bss->str_env1_ret, sizeof(env1), "str_env1_ret"); > + ASSERT_EQ(skel->bss->data_match, 1, "data_match"); > + ASSERT_EQ(skel->bss->str_args_match, 1, "str_args_match"); > + ASSERT_EQ(skel->bss->str_envs_match, 1, "str_envs_match"); > + > +out: > + copy_from_user_bprm__destroy(skel); > +} > diff --git a/tools/testing/selftests/bpf/progs/copy_from_user_bprm.c b/tools/testing/selftests/bpf/progs/copy_from_user_bprm.c > new file mode 100644 > index 000000000000..07ca96d9db45 > --- /dev/null > +++ b/tools/testing/selftests/bpf/progs/copy_from_user_bprm.c > @@ -0,0 +1,122 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +#include "vmlinux.h" > + > +#include <bpf/bpf_helpers.h> > +#include <bpf/bpf_tracing.h> > +#include <errno.h> > + > +char _license[] SEC("license") = "GPL"; > + > +static const char expected_data[] = "first\0second-argument\0" > + "SOME_ENV=a\0OTHER_ENV=something"; > +static const char expected_arg0[] = "first"; > +static const char expected_arg1[] = "second-argument"; > +static const char expected_env0[] = "SOME_ENV=a"; > +static const char expected_env1[] = "OTHER_ENV=something"; > + > +int monitored_pid; > +int bprm_argc; > +int bprm_envc; > +int data_len_match; > +int invalid_flags_ret; > +int copy_ret; > +int str_arg0_ret; > +int str_arg1_ret; > +int str_env0_ret; > +int str_env1_ret; > +int data_match; > +int str_args_match; > +int str_envs_match; > + > +extern bool CONFIG_MMU __kconfig __weak; > + > +extern int bpf_copy_from_user_mm(void *dst, u32 dst__sz, > + const void *unsafe_ptr__ign, > + struct mm_struct *mm, u64 flags) __ksym; > + > +extern int bpf_copy_from_user_mm_str(void *dst, u32 dst__sz, > + const void *unsafe_ptr__ign, > + struct mm_struct *mm, u64 flags) __ksym; > + these should be already coming from vmlinux.h, no need to explicitly define them, please drop (and CONFIG_MMU parts as well) > +SEC("lsm.s/bprm_check_security") > +int BPF_PROG(check_exec_args, struct linux_binprm *bprm) > +{ > + u32 pid = bpf_get_current_pid_tgid() >> 32; > + char data[sizeof(expected_data)] = {}; > + struct mm_struct *mm; > + char arg0[32] = {}; > + char arg1[32] = {}; > + char env0[32] = {}; > + char env1[32] = {}; > + u64 offset = 0; > + u64 data_len; > + > + if (!CONFIG_MMU) > + return 0; > + > + if (pid != monitored_pid) > + return 0; > + > + mm = bprm->mm; > + if (!mm) > + return 0; > + > + bprm_argc = bprm->argc; > + bprm_envc = bprm->envc; > + > + /* this is the total size of args and envs starting from bprm->p */ > + data_len = bprm->exec - bprm->p; > + data_len_match = data_len == sizeof(expected_data); > + > + invalid_flags_ret = bpf_copy_from_user_mm(data, sizeof(data), > + (void *)bprm->p, mm, ~0ULL); > + > + copy_ret = bpf_copy_from_user_mm(data, sizeof(data), (void *)bprm->p, > + mm, 0); single line (limit is 120, not 80), please reformat the rest if it fits under 120 > + if (copy_ret) > + return 0; > + > + data_match = > + !__builtin_memcmp(data, expected_data, sizeof(expected_data)); nit: keep single line > + > + /* arg0 is at bprm->p */ > + str_arg0_ret = bpf_copy_from_user_mm_str(arg0, sizeof(arg0), > + (void *)(bprm->p + offset), > + mm, BPF_F_PAD_ZEROS); > + if (str_arg0_ret != sizeof(expected_arg0)) > + return 0; > + offset += str_arg0_ret; > + > + /* arg1 is at bprm->p + sizeof(arg0) */ > + str_arg1_ret = bpf_copy_from_user_mm_str(arg1, sizeof(arg1), > + (void *)(bprm->p + offset), > + mm, BPF_F_PAD_ZEROS); > + if (str_arg1_ret != sizeof(expected_arg1)) > + return 0; > + offset += str_arg1_ret; > + > + /* env0 is at bprm->p + sizeof(arg0) + sizeof(arg1) */ > + str_env0_ret = bpf_copy_from_user_mm_str(env0, sizeof(env0), > + (void *)(bprm->p + offset), > + mm, BPF_F_PAD_ZEROS); > + if (str_env0_ret != sizeof(expected_env0)) > + return 0; > + offset += str_env0_ret; > + > + /* env1 is at bprm->p + sizeof(arg0) + sizeof(arg1) + sizeof(env0) */ > + str_env1_ret = bpf_copy_from_user_mm_str(env1, sizeof(env1), > + (void *)(bprm->p + offset), > + mm, BPF_F_PAD_ZEROS); > + if (str_env1_ret != sizeof(expected_env1)) > + return 0; > + > + str_args_match = > + !__builtin_memcmp(arg0, expected_arg0, sizeof(expected_arg0)) && > + !__builtin_memcmp(arg1, expected_arg1, sizeof(expected_arg1)); > + str_envs_match = > + !__builtin_memcmp(env0, expected_env0, sizeof(expected_env0)) && > + !__builtin_memcmp(env1, expected_env1, sizeof(expected_env1)); I'd put env1, env2, etc into global variables and do comparison in user space not that this is broken, but there is no point doing this in BPF code > + > + return data_match && str_args_match && str_envs_match ? -EPERM : 0; > +} > -- > 2.55.0 >