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