Skip to content

Commit b7cb4a4

Browse files
electronlsrKernel Patches Daemon
authored andcommitted
selftests/bpf: add regression test for bpf_d_path()
Add a regression test for bpf_d_path() to cover incorrect verifier assumptions caused by an incorrect function prototype. The test attaches to the fallocate hook, calls bpf_d_path() and verifies that a simple prefix comparison on the returned pathname behaves correctly after the fix in patch 1. It ensures the verifier does not assume the buffer remains unwritten. Co-developed-by: Zesen Liu <[email protected]> Signed-off-by: Zesen Liu <[email protected]> Co-developed-by: Peili Gao <[email protected]> Signed-off-by: Peili Gao <[email protected]> Co-developed-by: Haoran Ni <[email protected]> Signed-off-by: Haoran Ni <[email protected]> Signed-off-by: Shuran Liu <[email protected]>
1 parent dc29696 commit b7cb4a4

File tree

2 files changed

+94
-17
lines changed

2 files changed

+94
-17
lines changed

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

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ static int set_pathname(int fd, pid_t pid)
3838
return readlink(buf, src.paths[src.cnt++], MAX_PATH_LEN);
3939
}
4040

41+
static inline long syscall_close(int fd)
42+
{
43+
return syscall(__NR_close_range,
44+
(unsigned int)fd,
45+
(unsigned int)fd,
46+
0u);
47+
}
48+
4149
static int trigger_fstat_events(pid_t pid)
4250
{
4351
int sockfd = -1, procfd = -1, devfd = -1;
@@ -104,18 +112,34 @@ static int trigger_fstat_events(pid_t pid)
104112
/* sys_close no longer triggers filp_close, but we can
105113
* call sys_close_range instead which still does
106114
*/
107-
#define close(fd) syscall(__NR_close_range, fd, fd, 0)
115+
syscall_close(pipefd[0]);
116+
syscall_close(pipefd[1]);
117+
syscall_close(sockfd);
118+
syscall_close(procfd);
119+
syscall_close(devfd);
120+
syscall_close(localfd);
121+
syscall_close(indicatorfd);
122+
return ret;
123+
}
108124

109-
close(pipefd[0]);
110-
close(pipefd[1]);
111-
close(sockfd);
112-
close(procfd);
113-
close(devfd);
114-
close(localfd);
115-
close(indicatorfd);
125+
static void attach_and_load(struct test_d_path **skel)
126+
{
127+
int err;
116128

117-
#undef close
118-
return ret;
129+
*skel = test_d_path__open_and_load();
130+
if (CHECK(!*skel, "setup", "d_path skeleton failed\n"))
131+
goto cleanup;
132+
133+
err = test_d_path__attach(*skel);
134+
if (CHECK(err, "setup", "attach failed: %d\n", err))
135+
goto cleanup;
136+
137+
(*skel)->bss->my_pid = getpid();
138+
return;
139+
140+
cleanup:
141+
test_d_path__destroy(*skel);
142+
*skel = NULL;
119143
}
120144

121145
static void test_d_path_basic(void)
@@ -124,16 +148,11 @@ static void test_d_path_basic(void)
124148
struct test_d_path *skel;
125149
int err;
126150

127-
skel = test_d_path__open_and_load();
128-
if (CHECK(!skel, "setup", "d_path skeleton failed\n"))
129-
goto cleanup;
130-
131-
err = test_d_path__attach(skel);
132-
if (CHECK(err, "setup", "attach failed: %d\n", err))
151+
attach_and_load(&skel);
152+
if (!skel)
133153
goto cleanup;
134154

135155
bss = skel->bss;
136-
bss->my_pid = getpid();
137156

138157
err = trigger_fstat_events(bss->my_pid);
139158
if (err < 0)
@@ -195,6 +214,38 @@ static void test_d_path_check_types(void)
195214
test_d_path_check_types__destroy(skel);
196215
}
197216

217+
/* Check if the verifier correctly generates code for
218+
* accessing the memory modified by d_path helper.
219+
*/
220+
static void test_d_path_mem_access(void)
221+
{
222+
int localfd = -1;
223+
struct test_d_path__bss *bss;
224+
struct test_d_path *skel;
225+
226+
attach_and_load(&skel);
227+
if (!skel)
228+
goto cleanup;
229+
230+
bss = skel->bss;
231+
232+
localfd = open("/tmp/d_path_loadgen.txt", O_CREAT | O_RDWR, 0644);
233+
if (CHECK(localfd < 0, "trigger", "open /tmp/d_path_loadgen.txt failed\n"))
234+
goto cleanup;
235+
236+
if (CHECK(fallocate(localfd, 0, 0, 1024) < 0, "trigger", "fallocate failed\n"))
237+
goto cleanup;
238+
remove("/tmp/d_path_loadgen.txt");
239+
240+
if (CHECK(!bss->path_match_fallocate, "check",
241+
"failed to match actual opened path"))
242+
goto cleanup;
243+
244+
cleanup:
245+
syscall_close(localfd);
246+
test_d_path__destroy(skel);
247+
}
248+
198249
void test_d_path(void)
199250
{
200251
if (test__start_subtest("basic"))
@@ -205,4 +256,7 @@ void test_d_path(void)
205256

206257
if (test__start_subtest("check_alloc_mem"))
207258
test_d_path_check_types();
259+
260+
if (test__start_subtest("check_mem_access"))
261+
test_d_path_mem_access();
208262
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ int rets_close[MAX_FILES] = {};
1717

1818
int called_stat = 0;
1919
int called_close = 0;
20+
int path_match_fallocate = 0;
2021

2122
SEC("fentry/security_inode_getattr")
2223
int BPF_PROG(prog_stat, struct path *path, struct kstat *stat,
@@ -62,4 +63,26 @@ int BPF_PROG(prog_close, struct file *file, void *id)
6263
return 0;
6364
}
6465

66+
SEC("fentry/vfs_fallocate")
67+
int BPF_PROG(prog_fallocate, struct file *file, int mode, loff_t offset, loff_t len)
68+
{
69+
pid_t pid = bpf_get_current_pid_tgid() >> 32;
70+
int ret = 0;
71+
char path_fallocate[MAX_PATH_LEN] = {};
72+
73+
if (pid != my_pid)
74+
return 0;
75+
76+
ret = bpf_d_path(&file->f_path,
77+
path_fallocate, MAX_PATH_LEN);
78+
if (ret < 0)
79+
return 0;
80+
81+
if (path_fallocate[0] != '/')
82+
return 0;
83+
84+
path_match_fallocate = 1;
85+
return 0;
86+
}
87+
6588
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)