-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Hao Luo
authored and
Andrii Nakryiko
committed
Jan 6, 2022
1 parent
e59618f
commit 44bab87
Showing
2 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2022 Google */ | ||
|
||
#include "vmlinux.h" | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
|
||
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) | ||
{ | ||
void *active; | ||
__u32 cpu; | ||
|
||
cpu = bpf_get_smp_processor_id(); | ||
active = (void *)bpf_per_cpu_ptr(&bpf_prog_active, cpu); | ||
if (active) { | ||
/* FAIL here! 'active' points to readonly memory. bpf helpers | ||
* that update its arguments can not write into it. | ||
*/ | ||
bpf_d_path(path, active, sizeof(int)); | ||
} | ||
return 0; | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; |