1e4d1af4bSJiri Olsa // SPDX-License-Identifier: GPL-2.0
2e4d1af4bSJiri Olsa #define _GNU_SOURCE
3e4d1af4bSJiri Olsa #include <test_progs.h>
4e4d1af4bSJiri Olsa #include <sys/stat.h>
5e4d1af4bSJiri Olsa #include <linux/sched.h>
6e4d1af4bSJiri Olsa #include <sys/syscall.h>
7e4d1af4bSJiri Olsa
8e4d1af4bSJiri Olsa #define MAX_PATH_LEN 128
9e4d1af4bSJiri Olsa #define MAX_FILES 7
10e4d1af4bSJiri Olsa
11e4d1af4bSJiri Olsa #include "test_d_path.skel.h"
1244bab87dSHao Luo #include "test_d_path_check_rdonly_mem.skel.h"
1337c8d480SDaniel Borkmann #include "test_d_path_check_types.skel.h"
14e4d1af4bSJiri Olsa
15*d11ae1b1SJiri Olsa /* sys_close_range is not around for long time, so let's
16*d11ae1b1SJiri Olsa * make sure we can call it on systems with older glibc
17*d11ae1b1SJiri Olsa */
18*d11ae1b1SJiri Olsa #ifndef __NR_close_range
19*d11ae1b1SJiri Olsa #ifdef __alpha__
20*d11ae1b1SJiri Olsa #define __NR_close_range 546
21*d11ae1b1SJiri Olsa #else
22*d11ae1b1SJiri Olsa #define __NR_close_range 436
23*d11ae1b1SJiri Olsa #endif
24*d11ae1b1SJiri Olsa #endif
25*d11ae1b1SJiri Olsa
26e4d1af4bSJiri Olsa static int duration;
27e4d1af4bSJiri Olsa
28e4d1af4bSJiri Olsa static struct {
29e4d1af4bSJiri Olsa __u32 cnt;
30e4d1af4bSJiri Olsa char paths[MAX_FILES][MAX_PATH_LEN];
31e4d1af4bSJiri Olsa } src;
32e4d1af4bSJiri Olsa
set_pathname(int fd,pid_t pid)33e4d1af4bSJiri Olsa static int set_pathname(int fd, pid_t pid)
34e4d1af4bSJiri Olsa {
35e4d1af4bSJiri Olsa char buf[MAX_PATH_LEN];
36e4d1af4bSJiri Olsa
37e4d1af4bSJiri Olsa snprintf(buf, MAX_PATH_LEN, "/proc/%d/fd/%d", pid, fd);
38e4d1af4bSJiri Olsa return readlink(buf, src.paths[src.cnt++], MAX_PATH_LEN);
39e4d1af4bSJiri Olsa }
40e4d1af4bSJiri Olsa
trigger_fstat_events(pid_t pid)41e4d1af4bSJiri Olsa static int trigger_fstat_events(pid_t pid)
42e4d1af4bSJiri Olsa {
43e4d1af4bSJiri Olsa int sockfd = -1, procfd = -1, devfd = -1;
44e4d1af4bSJiri Olsa int localfd = -1, indicatorfd = -1;
45e4d1af4bSJiri Olsa int pipefd[2] = { -1, -1 };
46e4d1af4bSJiri Olsa struct stat fileStat;
47e4d1af4bSJiri Olsa int ret = -1;
48e4d1af4bSJiri Olsa
49e4d1af4bSJiri Olsa /* unmountable pseudo-filesystems */
50e4d1af4bSJiri Olsa if (CHECK(pipe(pipefd) < 0, "trigger", "pipe failed\n"))
51e4d1af4bSJiri Olsa return ret;
52e4d1af4bSJiri Olsa /* unmountable pseudo-filesystems */
53e4d1af4bSJiri Olsa sockfd = socket(AF_INET, SOCK_STREAM, 0);
547100ff7cSColin Ian King if (CHECK(sockfd < 0, "trigger", "socket failed\n"))
55e4d1af4bSJiri Olsa goto out_close;
56e4d1af4bSJiri Olsa /* mountable pseudo-filesystems */
57e4d1af4bSJiri Olsa procfd = open("/proc/self/comm", O_RDONLY);
58e4d1af4bSJiri Olsa if (CHECK(procfd < 0, "trigger", "open /proc/self/comm failed\n"))
59e4d1af4bSJiri Olsa goto out_close;
60e4d1af4bSJiri Olsa devfd = open("/dev/urandom", O_RDONLY);
61e4d1af4bSJiri Olsa if (CHECK(devfd < 0, "trigger", "open /dev/urandom failed\n"))
62e4d1af4bSJiri Olsa goto out_close;
63d8397176SJiri Olsa localfd = open("/tmp/d_path_loadgen.txt", O_CREAT | O_RDONLY, 0644);
64e4d1af4bSJiri Olsa if (CHECK(localfd < 0, "trigger", "open /tmp/d_path_loadgen.txt failed\n"))
65e4d1af4bSJiri Olsa goto out_close;
66e4d1af4bSJiri Olsa /* bpf_d_path will return path with (deleted) */
67e4d1af4bSJiri Olsa remove("/tmp/d_path_loadgen.txt");
68e4d1af4bSJiri Olsa indicatorfd = open("/tmp/", O_PATH);
69e4d1af4bSJiri Olsa if (CHECK(indicatorfd < 0, "trigger", "open /tmp/ failed\n"))
70e4d1af4bSJiri Olsa goto out_close;
71e4d1af4bSJiri Olsa
72e4d1af4bSJiri Olsa ret = set_pathname(pipefd[0], pid);
73e4d1af4bSJiri Olsa if (CHECK(ret < 0, "trigger", "set_pathname failed for pipe[0]\n"))
74e4d1af4bSJiri Olsa goto out_close;
75e4d1af4bSJiri Olsa ret = set_pathname(pipefd[1], pid);
76e4d1af4bSJiri Olsa if (CHECK(ret < 0, "trigger", "set_pathname failed for pipe[1]\n"))
77e4d1af4bSJiri Olsa goto out_close;
78e4d1af4bSJiri Olsa ret = set_pathname(sockfd, pid);
79e4d1af4bSJiri Olsa if (CHECK(ret < 0, "trigger", "set_pathname failed for socket\n"))
80e4d1af4bSJiri Olsa goto out_close;
81e4d1af4bSJiri Olsa ret = set_pathname(procfd, pid);
82e4d1af4bSJiri Olsa if (CHECK(ret < 0, "trigger", "set_pathname failed for proc\n"))
83e4d1af4bSJiri Olsa goto out_close;
84e4d1af4bSJiri Olsa ret = set_pathname(devfd, pid);
85e4d1af4bSJiri Olsa if (CHECK(ret < 0, "trigger", "set_pathname failed for dev\n"))
86e4d1af4bSJiri Olsa goto out_close;
87e4d1af4bSJiri Olsa ret = set_pathname(localfd, pid);
88e4d1af4bSJiri Olsa if (CHECK(ret < 0, "trigger", "set_pathname failed for file\n"))
89e4d1af4bSJiri Olsa goto out_close;
90e4d1af4bSJiri Olsa ret = set_pathname(indicatorfd, pid);
91e4d1af4bSJiri Olsa if (CHECK(ret < 0, "trigger", "set_pathname failed for dir\n"))
92e4d1af4bSJiri Olsa goto out_close;
93e4d1af4bSJiri Olsa
94e4d1af4bSJiri Olsa /* triggers vfs_getattr */
95e4d1af4bSJiri Olsa fstat(pipefd[0], &fileStat);
96e4d1af4bSJiri Olsa fstat(pipefd[1], &fileStat);
97e4d1af4bSJiri Olsa fstat(sockfd, &fileStat);
98e4d1af4bSJiri Olsa fstat(procfd, &fileStat);
99e4d1af4bSJiri Olsa fstat(devfd, &fileStat);
100e4d1af4bSJiri Olsa fstat(localfd, &fileStat);
101e4d1af4bSJiri Olsa fstat(indicatorfd, &fileStat);
102e4d1af4bSJiri Olsa
103e4d1af4bSJiri Olsa out_close:
104*d11ae1b1SJiri Olsa /* sys_close no longer triggers filp_close, but we can
105*d11ae1b1SJiri Olsa * call sys_close_range instead which still does
106*d11ae1b1SJiri Olsa */
107*d11ae1b1SJiri Olsa #define close(fd) syscall(__NR_close_range, fd, fd, 0)
108*d11ae1b1SJiri Olsa
109e4d1af4bSJiri Olsa close(pipefd[0]);
110e4d1af4bSJiri Olsa close(pipefd[1]);
111e4d1af4bSJiri Olsa close(sockfd);
112e4d1af4bSJiri Olsa close(procfd);
113e4d1af4bSJiri Olsa close(devfd);
114e4d1af4bSJiri Olsa close(localfd);
115e4d1af4bSJiri Olsa close(indicatorfd);
116*d11ae1b1SJiri Olsa
117*d11ae1b1SJiri Olsa #undef close
118e4d1af4bSJiri Olsa return ret;
119e4d1af4bSJiri Olsa }
120e4d1af4bSJiri Olsa
test_d_path_basic(void)12144bab87dSHao Luo static void test_d_path_basic(void)
122e4d1af4bSJiri Olsa {
123e4d1af4bSJiri Olsa struct test_d_path__bss *bss;
124e4d1af4bSJiri Olsa struct test_d_path *skel;
125e4d1af4bSJiri Olsa int err;
126e4d1af4bSJiri Olsa
127e4d1af4bSJiri Olsa skel = test_d_path__open_and_load();
128e4d1af4bSJiri Olsa if (CHECK(!skel, "setup", "d_path skeleton failed\n"))
129e4d1af4bSJiri Olsa goto cleanup;
130e4d1af4bSJiri Olsa
131e4d1af4bSJiri Olsa err = test_d_path__attach(skel);
132e4d1af4bSJiri Olsa if (CHECK(err, "setup", "attach failed: %d\n", err))
133e4d1af4bSJiri Olsa goto cleanup;
134e4d1af4bSJiri Olsa
135e4d1af4bSJiri Olsa bss = skel->bss;
136e4d1af4bSJiri Olsa bss->my_pid = getpid();
137e4d1af4bSJiri Olsa
138e4d1af4bSJiri Olsa err = trigger_fstat_events(bss->my_pid);
139e4d1af4bSJiri Olsa if (err < 0)
140e4d1af4bSJiri Olsa goto cleanup;
141e4d1af4bSJiri Olsa
142a8a71796SJiri Olsa if (CHECK(!bss->called_stat,
143a8a71796SJiri Olsa "stat",
144a8a71796SJiri Olsa "trampoline for security_inode_getattr was not called\n"))
145a8a71796SJiri Olsa goto cleanup;
146a8a71796SJiri Olsa
147a8a71796SJiri Olsa if (CHECK(!bss->called_close,
148a8a71796SJiri Olsa "close",
149a8a71796SJiri Olsa "trampoline for filp_close was not called\n"))
150a8a71796SJiri Olsa goto cleanup;
151a8a71796SJiri Olsa
152e4d1af4bSJiri Olsa for (int i = 0; i < MAX_FILES; i++) {
153e4d1af4bSJiri Olsa CHECK(strncmp(src.paths[i], bss->paths_stat[i], MAX_PATH_LEN),
154e4d1af4bSJiri Olsa "check",
155e4d1af4bSJiri Olsa "failed to get stat path[%d]: %s vs %s\n",
156e4d1af4bSJiri Olsa i, src.paths[i], bss->paths_stat[i]);
157e4d1af4bSJiri Olsa CHECK(strncmp(src.paths[i], bss->paths_close[i], MAX_PATH_LEN),
158e4d1af4bSJiri Olsa "check",
159e4d1af4bSJiri Olsa "failed to get close path[%d]: %s vs %s\n",
160e4d1af4bSJiri Olsa i, src.paths[i], bss->paths_close[i]);
161e4d1af4bSJiri Olsa /* The d_path helper returns size plus NUL char, hence + 1 */
162e4d1af4bSJiri Olsa CHECK(bss->rets_stat[i] != strlen(bss->paths_stat[i]) + 1,
163e4d1af4bSJiri Olsa "check",
164e4d1af4bSJiri Olsa "failed to match stat return [%d]: %d vs %zd [%s]\n",
165e4d1af4bSJiri Olsa i, bss->rets_stat[i], strlen(bss->paths_stat[i]) + 1,
166e4d1af4bSJiri Olsa bss->paths_stat[i]);
167e4d1af4bSJiri Olsa CHECK(bss->rets_close[i] != strlen(bss->paths_stat[i]) + 1,
168e4d1af4bSJiri Olsa "check",
169e4d1af4bSJiri Olsa "failed to match stat return [%d]: %d vs %zd [%s]\n",
170e4d1af4bSJiri Olsa i, bss->rets_close[i], strlen(bss->paths_close[i]) + 1,
171e4d1af4bSJiri Olsa bss->paths_stat[i]);
172e4d1af4bSJiri Olsa }
173e4d1af4bSJiri Olsa
174e4d1af4bSJiri Olsa cleanup:
175e4d1af4bSJiri Olsa test_d_path__destroy(skel);
176e4d1af4bSJiri Olsa }
17744bab87dSHao Luo
test_d_path_check_rdonly_mem(void)17844bab87dSHao Luo static void test_d_path_check_rdonly_mem(void)
17944bab87dSHao Luo {
18044bab87dSHao Luo struct test_d_path_check_rdonly_mem *skel;
18144bab87dSHao Luo
18244bab87dSHao Luo skel = test_d_path_check_rdonly_mem__open_and_load();
18344bab87dSHao Luo ASSERT_ERR_PTR(skel, "unexpected_load_overwriting_rdonly_mem");
18444bab87dSHao Luo
18544bab87dSHao Luo test_d_path_check_rdonly_mem__destroy(skel);
18644bab87dSHao Luo }
18744bab87dSHao Luo
test_d_path_check_types(void)18837c8d480SDaniel Borkmann static void test_d_path_check_types(void)
18937c8d480SDaniel Borkmann {
19037c8d480SDaniel Borkmann struct test_d_path_check_types *skel;
19137c8d480SDaniel Borkmann
19237c8d480SDaniel Borkmann skel = test_d_path_check_types__open_and_load();
19337c8d480SDaniel Borkmann ASSERT_ERR_PTR(skel, "unexpected_load_passing_wrong_type");
19437c8d480SDaniel Borkmann
19537c8d480SDaniel Borkmann test_d_path_check_types__destroy(skel);
19637c8d480SDaniel Borkmann }
19737c8d480SDaniel Borkmann
test_d_path(void)19844bab87dSHao Luo void test_d_path(void)
19944bab87dSHao Luo {
20044bab87dSHao Luo if (test__start_subtest("basic"))
20144bab87dSHao Luo test_d_path_basic();
20244bab87dSHao Luo
20344bab87dSHao Luo if (test__start_subtest("check_rdonly_mem"))
20444bab87dSHao Luo test_d_path_check_rdonly_mem();
20537c8d480SDaniel Borkmann
20637c8d480SDaniel Borkmann if (test__start_subtest("check_alloc_mem"))
20737c8d480SDaniel Borkmann test_d_path_check_types();
20844bab87dSHao Luo }
209