Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf/selftests: Test bpf_d_path on rdonly_mem.

The second parameter of bpf_d_path() can only accept writable
memories. Rdonly_mem obtained from bpf_per_cpu_ptr() can not
be passed into bpf_d_path for modification. This patch adds
a selftest to verify this behavior.

Signed-off-by: Hao Luo <haoluo@google.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20220106205525.2116218-1-haoluo@google.com

authored by

Hao Luo and committed by
Andrii Nakryiko
44bab87d e59618f0

+49 -1
+21 -1
tools/testing/selftests/bpf/prog_tests/d_path.c
··· 9 9 #define MAX_FILES 7 10 10 11 11 #include "test_d_path.skel.h" 12 + #include "test_d_path_check_rdonly_mem.skel.h" 12 13 13 14 static int duration; 14 15 ··· 100 99 return ret; 101 100 } 102 101 103 - void test_d_path(void) 102 + static void test_d_path_basic(void) 104 103 { 105 104 struct test_d_path__bss *bss; 106 105 struct test_d_path *skel; ··· 155 154 156 155 cleanup: 157 156 test_d_path__destroy(skel); 157 + } 158 + 159 + static void test_d_path_check_rdonly_mem(void) 160 + { 161 + struct test_d_path_check_rdonly_mem *skel; 162 + 163 + skel = test_d_path_check_rdonly_mem__open_and_load(); 164 + ASSERT_ERR_PTR(skel, "unexpected_load_overwriting_rdonly_mem"); 165 + 166 + test_d_path_check_rdonly_mem__destroy(skel); 167 + } 168 + 169 + void test_d_path(void) 170 + { 171 + if (test__start_subtest("basic")) 172 + test_d_path_basic(); 173 + 174 + if (test__start_subtest("check_rdonly_mem")) 175 + test_d_path_check_rdonly_mem(); 158 176 }
+28
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Copyright (c) 2022 Google */ 3 + 4 + #include "vmlinux.h" 5 + #include <bpf/bpf_helpers.h> 6 + #include <bpf/bpf_tracing.h> 7 + 8 + extern const int bpf_prog_active __ksym; 9 + 10 + SEC("fentry/security_inode_getattr") 11 + int BPF_PROG(d_path_check_rdonly_mem, struct path *path, struct kstat *stat, 12 + __u32 request_mask, unsigned int query_flags) 13 + { 14 + void *active; 15 + __u32 cpu; 16 + 17 + cpu = bpf_get_smp_processor_id(); 18 + active = (void *)bpf_per_cpu_ptr(&bpf_prog_active, cpu); 19 + if (active) { 20 + /* FAIL here! 'active' points to readonly memory. bpf helpers 21 + * that update its arguments can not write into it. 22 + */ 23 + bpf_d_path(path, active, sizeof(int)); 24 + } 25 + return 0; 26 + } 27 + 28 + char _license[] SEC("license") = "GPL";