• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 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_BY_KERNEL(FAN_REPORT_PIDFD);
123 
124 	fanotify_fd = SAFE_FANOTIFY_INIT(FAN_REPORT_PIDFD, O_RDONLY);
125 	SAFE_FANOTIFY_MARK(fanotify_fd, FAN_MARK_ADD, FAN_OPEN, AT_FDCWD,
126 			   TEST_FILE);
127 
128 	pidfd = SAFE_PIDFD_OPEN(getpid(), 0);
129 
130 	self_pidfd_fdinfo = read_pidfd_fdinfo(pidfd);
131 	if (self_pidfd_fdinfo == NULL) {
132 		tst_brk(TBROK,
133 			"pidfd=%d, failed to read pidfd fdinfo",
134 			pidfd);
135 	}
136 }
137 
do_test(unsigned int num)138 static void do_test(unsigned int num)
139 {
140 	int i = 0, len;
141 	struct test_case_t *tc = &test_cases[num];
142 
143 	tst_res(TINFO, "Test #%d: %s", num, tc->name);
144 
145 	/*
146 	 * Generate the event in either self or a child process. Event
147 	 * generation in a child process is done so that the FAN_NOPIDFD case
148 	 * can be verified.
149 	 */
150 	if (tc->fork)
151 		do_fork();
152 	else
153 		generate_event();
154 
155 	/*
156 	 * Read all of the queued events into the provided event
157 	 * buffer.
158 	 */
159 	len = SAFE_READ(0, fanotify_fd, event_buf, sizeof(event_buf));
160 	while (i < len) {
161 		struct fanotify_event_metadata *event;
162 		struct fanotify_event_info_pidfd *info;
163 		struct pidfd_fdinfo_t *event_pidfd_fdinfo = NULL;
164 
165 		event = (struct fanotify_event_metadata *)&event_buf[i];
166 		info = (struct fanotify_event_info_pidfd *)(event + 1);
167 
168 		/*
169 		 * Checks ensuring that pidfd information record object header
170 		 * fields are set correctly.
171 		 */
172 		if (info->hdr.info_type != FAN_EVENT_INFO_TYPE_PIDFD) {
173 			tst_res(TFAIL,
174 				"unexpected info_type received in info "
175 				"header (expected: %d, got: %d",
176 				FAN_EVENT_INFO_TYPE_PIDFD,
177 				info->hdr.info_type);
178 			info = NULL;
179 			goto next_event;
180 		} else if (info->hdr.len !=
181 			   sizeof(struct fanotify_event_info_pidfd)) {
182 			tst_res(TFAIL,
183 				"unexpected info object length "
184 				"(expected: %lu, got: %d",
185 				sizeof(struct fanotify_event_info_pidfd),
186 				info->hdr.len);
187 			info = NULL;
188 			goto next_event;
189 		}
190 
191 		/*
192 		 * Check if pidfd information object reported any errors during
193 		 * creation and whether they're expected.
194 		 */
195 		if (info->pidfd < 0 && !tc->want_pidfd_err) {
196 			tst_res(TFAIL,
197 				"pidfd creation failed for pid: %u with pidfd error value "
198 				"set to: %d",
199 				(unsigned int)event->pid,
200 				info->pidfd);
201 			goto next_event;
202 		} else if (tc->want_pidfd_err &&
203 			   info->pidfd != tc->want_pidfd_err) {
204 			tst_res(TFAIL,
205 				"pidfd set to an unexpected error: %d for pid: %u",
206 				info->pidfd,
207 				(unsigned int)event->pid);
208 			goto next_event;
209 		} else if (tc->want_pidfd_err &&
210 			   info->pidfd == tc->want_pidfd_err) {
211 			tst_res(TPASS,
212 				"pid: %u terminated before pidfd was created, "
213 				"pidfd set to the value of: %d, as expected",
214 				(unsigned int)event->pid,
215 				FAN_NOPIDFD);
216 			goto next_event;
217 		}
218 
219 		/*
220 		 * No pidfd errors occurred, continue with verifying pidfd
221 		 * fdinfo validity.
222 		 */
223 		event_pidfd_fdinfo = read_pidfd_fdinfo(info->pidfd);
224 		if (event_pidfd_fdinfo == NULL) {
225 			tst_brk(TBROK,
226 				"reading fdinfo for pidfd: %d "
227 				"describing pid: %u failed",
228 				info->pidfd,
229 				(unsigned int)event->pid);
230 			goto next_event;
231 		} else if (event_pidfd_fdinfo->pid != event->pid) {
232 			tst_res(TFAIL,
233 				"pidfd provided for incorrect pid "
234 				"(expected pidfd for pid: %u, got pidfd for "
235 				"pid: %u)",
236 				(unsigned int)event->pid,
237 				(unsigned int)event_pidfd_fdinfo->pid);
238 			goto next_event;
239 		} else if (memcmp(event_pidfd_fdinfo, self_pidfd_fdinfo,
240 				  sizeof(struct pidfd_fdinfo_t))) {
241 			tst_res(TFAIL,
242 				"pidfd fdinfo values for self and event differ "
243 				"(expected pos: %d, flags: %x, mnt_id: %d, "
244 				"pid: %d, ns_pid: %d, got pos: %d, "
245 				"flags: %x, mnt_id: %d, pid: %d, ns_pid: %d",
246 				self_pidfd_fdinfo->pos,
247 				self_pidfd_fdinfo->flags,
248 				self_pidfd_fdinfo->mnt_id,
249 				self_pidfd_fdinfo->pid,
250 				self_pidfd_fdinfo->ns_pid,
251 				event_pidfd_fdinfo->pos,
252 				event_pidfd_fdinfo->flags,
253 				event_pidfd_fdinfo->mnt_id,
254 				event_pidfd_fdinfo->pid,
255 				event_pidfd_fdinfo->ns_pid);
256 			goto next_event;
257 		} else {
258 			tst_res(TPASS,
259 				"got an event with a valid pidfd info record, "
260 				"mask: %lld, pid: %u, fd: %d, "
261 				"pidfd: %d, info_type: %d, info_len: %d",
262 				(unsigned long long)event->mask,
263 				(unsigned int)event->pid,
264 				event->fd,
265 				info->pidfd,
266 				info->hdr.info_type,
267 				info->hdr.len);
268 		}
269 
270 next_event:
271 		i += event->event_len;
272 		if (event->fd >= 0)
273 			SAFE_CLOSE(event->fd);
274 
275 		if (info && info->pidfd >= 0)
276 			SAFE_CLOSE(info->pidfd);
277 
278 		if (event_pidfd_fdinfo)
279 			free(event_pidfd_fdinfo);
280 	}
281 }
282 
do_cleanup(void)283 static void do_cleanup(void)
284 {
285 	if (fanotify_fd >= 0)
286 		SAFE_CLOSE(fanotify_fd);
287 
288 	if (self_pidfd_fdinfo)
289 		free(self_pidfd_fdinfo);
290 }
291 
292 static struct tst_test test = {
293 	.setup = do_setup,
294 	.test = do_test,
295 	.tcnt = ARRAY_SIZE(test_cases),
296 	.cleanup = do_cleanup,
297 	.all_filesystems = 1,
298 	.needs_root = 1,
299 	.mntpoint = MOUNT_PATH,
300 	.forks_child = 1,
301 };
302 
303 #else
304 	TST_TEST_TCONF("system doesn't have required fanotify support");
305 #endif /* HAVE_SYS_FANOTIFY_H */
306