Re: [PATCH bpf-next] bpf/selftests: Test bpf_d_path on rdonly_mem.
From: Yonghong Song <hidden>
Date: 2021-12-21 22:29:51
On 12/21/21 12:16 PM, Hao Luo wrote:
On Mon, Dec 20, 2021 at 8:28 PM Yonghong Song [off-list ref] wrote:quoted
On 12/20/21 12:12 PM, Hao Luo wrote:quoted
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 <redacted> --- .../testing/selftests/bpf/prog_tests/d_path.c | 22 +++++++++++++- .../bpf/progs/test_d_path_check_rdonly_mem.c | 30 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.cdiff --git a/tools/testing/selftests/bpf/prog_tests/d_path.c b/tools/testing/selftests/bpf/prog_tests/d_path.c index 0a577a248d34..f8d8c5a5dfba 100644 --- a/tools/testing/selftests/bpf/prog_tests/d_path.c +++ b/tools/testing/selftests/bpf/prog_tests/d_path.c@@ -9,6 +9,7 @@ #define MAX_FILES 7 #include "test_d_path.skel.h" +#include "test_d_path_check_rdonly_mem.skel.h" static int duration;@@ -99,7 +100,7 @@ static int trigger_fstat_events(pid_t pid) return ret; }
[...]
quoted
quoted
+ +extern const int bpf_prog_active __ksym; + +SEC("fentry/security_inode_getattr") +int BPF_PROG(d_path_check_rdonly_mem, struct path *path, struct kstat *stat, + __u32 request_mask, unsigned int query_flags) +{ + char *active;int *active? It may not matter since the program is rejected by the kernel but with making it conforms to kernel definition we have one less thing to worry about the verification.Because bpf_d_path() accepts 'char *' instead of 'int *', I need to cast 'active' to 'char *' somewhere, otherwise the compiler will issue a warning. To combine with your comment, maybe the following: int *active; active = (int *)bpf_per_cpu_ptr(...); ... bpf_d_path(path, (char *)active, sizeof(int));
This is fine. Thanks!
quoted
quoted
+ __u32 cpu; + + cpu = bpf_get_smp_processor_id(); + active = (char *)bpf_per_cpu_ptr(&bpf_prog_active, cpu);int *quoted
+ if (active) { + /* FAIL here! 'active' is a rdonly_mem. bpf helpers that'active' points to readonly memory.Ack.
[...]