Skip to content

Commit a8a7179

Browse files
olsajiriAlexei Starovoitov
authored andcommitted
selftests/bpf: Fix stat probe in d_path test
Some kernels builds might inline vfs_getattr call within fstat syscall code path, so fentry/vfs_getattr trampoline is not called. Add security_inode_getattr to allowlist and switch the d_path test stat trampoline to security_inode_getattr. Keeping dentry_open and filp_close, because they are in their own files, so unlikely to be inlined, but in case they are, adding security_file_open. Adding flags that indicate trampolines were called and failing the test if any of them got missed, so it's easier to identify the issue next time. Fixes: e4d1af4 ("selftests/bpf: Add test for d_path helper") Suggested-by: Alexei Starovoitov <[email protected]> Signed-off-by: Jiri Olsa <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent c69d2dd commit a8a7179

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

kernel/trace/bpf_trace.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,14 @@ BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz)
11141114
}
11151115

11161116
BTF_SET_START(btf_allowlist_d_path)
1117+
#ifdef CONFIG_SECURITY
1118+
BTF_ID(func, security_file_permission)
1119+
BTF_ID(func, security_inode_getattr)
1120+
BTF_ID(func, security_file_open)
1121+
#endif
1122+
#ifdef CONFIG_SECURITY_PATH
1123+
BTF_ID(func, security_path_truncate)
1124+
#endif
11171125
BTF_ID(func, vfs_truncate)
11181126
BTF_ID(func, vfs_fallocate)
11191127
BTF_ID(func, dentry_open)

tools/testing/selftests/bpf/prog_tests/d_path.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ void test_d_path(void)
120120
if (err < 0)
121121
goto cleanup;
122122

123+
if (CHECK(!bss->called_stat,
124+
"stat",
125+
"trampoline for security_inode_getattr was not called\n"))
126+
goto cleanup;
127+
128+
if (CHECK(!bss->called_close,
129+
"close",
130+
"trampoline for filp_close was not called\n"))
131+
goto cleanup;
132+
123133
for (int i = 0; i < MAX_FILES; i++) {
124134
CHECK(strncmp(src.paths[i], bss->paths_stat[i], MAX_PATH_LEN),
125135
"check",

tools/testing/selftests/bpf/progs/test_d_path.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@ char paths_close[MAX_FILES][MAX_PATH_LEN] = {};
1515
int rets_stat[MAX_FILES] = {};
1616
int rets_close[MAX_FILES] = {};
1717

18-
SEC("fentry/vfs_getattr")
18+
int called_stat = 0;
19+
int called_close = 0;
20+
21+
SEC("fentry/security_inode_getattr")
1922
int BPF_PROG(prog_stat, struct path *path, struct kstat *stat,
2023
__u32 request_mask, unsigned int query_flags)
2124
{
2225
pid_t pid = bpf_get_current_pid_tgid() >> 32;
2326
__u32 cnt = cnt_stat;
2427
int ret;
2528

29+
called_stat = 1;
30+
2631
if (pid != my_pid)
2732
return 0;
2833

@@ -42,6 +47,8 @@ int BPF_PROG(prog_close, struct file *file, void *id)
4247
__u32 cnt = cnt_close;
4348
int ret;
4449

50+
called_close = 1;
51+
4552
if (pid != my_pid)
4653
return 0;
4754

0 commit comments

Comments
 (0)