1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2021 Google. All Rights Reserved.
4 *
5 * Started by Matthew Bobrowski <[email protected]>
6 */
7
8 /*\
9 * [Description]
10 *
11 * A test which verifies whether the returned struct
12 * fanotify_event_info_pidfd in FAN_REPORT_PIDFD mode contains the
13 * expected set of information.
14 *
15 * NOTE: FAN_REPORT_PIDFD support was added in v5.15-rc1 in af579beb666a
16 * ("fanotify: add pidfd support to the fanotify API").
17 */
18
19 #define _GNU_SOURCE
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "tst_test.h"
25 #include "tst_safe_stdio.h"
26 #include "tst_safe_macros.h"
27 #include "lapi/pidfd.h"
28
29 #ifdef HAVE_SYS_FANOTIFY_H
30 #include "fanotify.h"
31
32 #define BUF_SZ 4096
33 #define MOUNT_PATH "fs_mnt"
34 #define TEST_FILE MOUNT_PATH "/testfile"
35
36 static struct pidfd_fdinfo_t {
37 int pos;
38 int flags;
39 int mnt_id;
40 int pid;
41 int ns_pid;
42 };
43
44 static struct test_case_t {
45 char *name;
46 int fork;
47 int want_pidfd_err;
48 } test_cases[] = {
49 {
50 "return a valid pidfd for event created by self",
51 0,
52 0,
53 },
54 {
55 "return invalid pidfd for event created by terminated child",
56 1,
57 FAN_NOPIDFD,
58 },
59 };
60
61 static int fanotify_fd;
62 static char event_buf[BUF_SZ];
63 static struct pidfd_fdinfo_t *self_pidfd_fdinfo;
64
read_pidfd_fdinfo(int pidfd)65 static struct pidfd_fdinfo_t *read_pidfd_fdinfo(int pidfd)
66 {
67 char *fdinfo_path;
68 struct pidfd_fdinfo_t *pidfd_fdinfo;
69
70 pidfd_fdinfo = SAFE_MALLOC(sizeof(struct pidfd_fdinfo_t));
71
72 SAFE_ASPRINTF(&fdinfo_path, "/proc/self/fdinfo/%d", pidfd);
73 SAFE_FILE_LINES_SCANF(fdinfo_path, "pos: %d", &pidfd_fdinfo->pos);
74 SAFE_FILE_LINES_SCANF(fdinfo_path, "flags: %d", &pidfd_fdinfo->flags);
75 SAFE_FILE_LINES_SCANF(fdinfo_path, "mnt_id: %d", &pidfd_fdinfo->mnt_id);
76 SAFE_FILE_LINES_SCANF(fdinfo_path, "Pid: %d", &pidfd_fdinfo->pid);
77 SAFE_FILE_LINES_SCANF(fdinfo_path, "NSpid: %d", &pidfd_fdinfo->ns_pid);
78
79 free(fdinfo_path);
80
81 return pidfd_fdinfo;
82 }
83
generate_event(void)84 static void generate_event(void)
85 {
86 int fd;
87
88 /* Generate a single FAN_OPEN event on the watched object. */
89 fd = SAFE_OPEN(TEST_FILE, O_RDONLY);
90 SAFE_CLOSE(fd);
91 }
92
do_fork(void)93 static void do_fork(void)
94 {
95 int status;
96 pid_t child;
97
98 child = SAFE_FORK();
99 if (child == 0) {
100 SAFE_CLOSE(fanotify_fd);
101 generate_event();
102 exit(EXIT_SUCCESS);
103 }
104
105 SAFE_WAITPID(child, &status, 0);
106 if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
107 tst_brk(TBROK,
108 "child process terminated incorrectly");
109 }
110
do_setup(void)111 static void do_setup(void)
112 {
113 int pidfd;
114
115 SAFE_TOUCH(TEST_FILE, 0666, NULL);
116
117 /*
118 * An explicit check for FAN_REPORT_PIDFD is performed early
119 * on in the test initialization as it's a prerequisite for
120 * all test cases.
121 */
122 REQUIRE_FANOTIFY_INIT_FLAGS_SUPPORTED_ON_FS(FAN_REPORT_PIDFD,
123 TEST_FILE);
124
125 fanotify_fd = SAFE_FANOTIFY_INIT(FAN_REPORT_PIDFD, O_RDONLY);
126 SAFE_FANOTIFY_MARK(fanotify_fd, FAN_MARK_ADD, FAN_OPEN, AT_FDCWD,
127 TEST_FILE);
128
129 pidfd = SAFE_PIDFD_OPEN(getpid(), 0);
130
131 self_pidfd_fdinfo = read_pidfd_fdinfo(pidfd);
132 if (self_pidfd_fdinfo == NULL) {
133 tst_brk(TBROK,
134 "pidfd=%d, failed to read pidfd fdinfo",
135 pidfd);
136 }
137 }
138
do_test(unsigned int num)139 static void do_test(unsigned int num)
140 {
141 int i = 0, len;
142 struct test_case_t *tc = &test_cases[num];
143
144 tst_res(TINFO, "Test #%d: %s", num, tc->name);
145
146 /*
147 * Generate the event in either self or a child process. Event
148 * generation in a child process is done so that the FAN_NOPIDFD case
149 * can be verified.
150 */
151 if (tc->fork)
152 do_fork();
153 else
154 generate_event();
155
156 /*
157 * Read all of the queued events into the provided event
158 * buffer.
159 */
160 len = SAFE_READ(0, fanotify_fd, event_buf, sizeof(event_buf));
161 while (i < len) {
162 struct fanotify_event_metadata *event;
163 struct fanotify_event_info_pidfd *info;
164 struct pidfd_fdinfo_t *event_pidfd_fdinfo = NULL;
165
166 event = (struct fanotify_event_metadata *)&event_buf[i];
167 info = (struct fanotify_event_info_pidfd *)(event + 1);
168
169 /*
170 * Checks ensuring that pidfd information record object header
171 * fields are set correctly.
172 */
173 if (info->hdr.info_type != FAN_EVENT_INFO_TYPE_PIDFD) {
174 tst_res(TFAIL,
175 "unexpected info_type received in info "
176 "header (expected: %d, got: %d",
177 FAN_EVENT_INFO_TYPE_PIDFD,
178 info->hdr.info_type);
179 info = NULL;
180 goto next_event;
181 } else if (info->hdr.len !=
182 sizeof(struct fanotify_event_info_pidfd)) {
183 tst_res(TFAIL,
184 "unexpected info object length "
185 "(expected: %lu, got: %d",
186 sizeof(struct fanotify_event_info_pidfd),
187 info->hdr.len);
188 info = NULL;
189 goto next_event;
190 }
191
192 /*
193 * Check if pidfd information object reported any errors during
194 * creation and whether they're expected.
195 */
196 if (info->pidfd < 0 && !tc->want_pidfd_err) {
197 tst_res(TFAIL,
198 "pidfd creation failed for pid: %u with pidfd error value "
199 "set to: %d",
200 (unsigned int)event->pid,
201 info->pidfd);
202 goto next_event;
203 } else if (tc->want_pidfd_err &&
204 info->pidfd != tc->want_pidfd_err) {
205 tst_res(TFAIL,
206 "pidfd set to an unexpected error: %d for pid: %u",
207 info->pidfd,
208 (unsigned int)event->pid);
209 goto next_event;
210 } else if (tc->want_pidfd_err &&
211 info->pidfd == tc->want_pidfd_err) {
212 tst_res(TPASS,
213 "pid: %u terminated before pidfd was created, "
214 "pidfd set to the value of: %d, as expected",
215 (unsigned int)event->pid,
216 FAN_NOPIDFD);
217 goto next_event;
218 }
219
220 /*
221 * No pidfd errors occurred, continue with verifying pidfd
222 * fdinfo validity.
223 */
224 event_pidfd_fdinfo = read_pidfd_fdinfo(info->pidfd);
225 if (event_pidfd_fdinfo == NULL) {
226 tst_brk(TBROK,
227 "reading fdinfo for pidfd: %d "
228 "describing pid: %u failed",
229 info->pidfd,
230 (unsigned int)event->pid);
231 goto next_event;
232 } else if (event_pidfd_fdinfo->pid != event->pid) {
233 tst_res(TFAIL,
234 "pidfd provided for incorrect pid "
235 "(expected pidfd for pid: %u, got pidfd for "
236 "pid: %u)",
237 (unsigned int)event->pid,
238 (unsigned int)event_pidfd_fdinfo->pid);
239 goto next_event;
240 } else if (memcmp(event_pidfd_fdinfo, self_pidfd_fdinfo,
241 sizeof(struct pidfd_fdinfo_t))) {
242 tst_res(TFAIL,
243 "pidfd fdinfo values for self and event differ "
244 "(expected pos: %d, flags: %x, mnt_id: %d, "
245 "pid: %d, ns_pid: %d, got pos: %d, "
246 "flags: %x, mnt_id: %d, pid: %d, ns_pid: %d",
247 self_pidfd_fdinfo->pos,
248 self_pidfd_fdinfo->flags,
249 self_pidfd_fdinfo->mnt_id,
250 self_pidfd_fdinfo->pid,
251 self_pidfd_fdinfo->ns_pid,
252 event_pidfd_fdinfo->pos,
253 event_pidfd_fdinfo->flags,
254 event_pidfd_fdinfo->mnt_id,
255 event_pidfd_fdinfo->pid,
256 event_pidfd_fdinfo->ns_pid);
257 goto next_event;
258 } else {
259 tst_res(TPASS,
260 "got an event with a valid pidfd info record, "
261 "mask: %lld, pid: %u, fd: %d, "
262 "pidfd: %d, info_type: %d, info_len: %d",
263 (unsigned long long)event->mask,
264 (unsigned int)event->pid,
265 event->fd,
266 info->pidfd,
267 info->hdr.info_type,
268 info->hdr.len);
269 }
270
271 next_event:
272 i += event->event_len;
273 if (event->fd >= 0)
274 SAFE_CLOSE(event->fd);
275
276 if (info && info->pidfd >= 0)
277 SAFE_CLOSE(info->pidfd);
278
279 if (event_pidfd_fdinfo)
280 free(event_pidfd_fdinfo);
281 }
282 }
283
do_cleanup(void)284 static void do_cleanup(void)
285 {
286 if (fanotify_fd >= 0)
287 SAFE_CLOSE(fanotify_fd);
288
289 if (self_pidfd_fdinfo)
290 free(self_pidfd_fdinfo);
291 }
292
293 static struct tst_test test = {
294 .setup = do_setup,
295 .test = do_test,
296 .tcnt = ARRAY_SIZE(test_cases),
297 .cleanup = do_cleanup,
298 .all_filesystems = 1,
299 .needs_root = 1,
300 .mntpoint = MOUNT_PATH,
301 .forks_child = 1,
302 };
303
304 #else
305 TST_TEST_TCONF("system doesn't have required fanotify support");
306 #endif /* HAVE_SYS_FANOTIFY_H */
307