1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2014 SUSE. All Rights Reserved.
4 * Copyright (c) 2018 CTERA Networks. All Rights Reserved.
5 *
6 * Started by Jan Kara <jack@suse.cz>
7 * Forked from fanotify06.c by Amir Goldstein <amir73il@gmail.com>
8 *
9 * DESCRIPTION
10 * Check that fanotify properly merges ignore mask of a mount mark
11 * with a mask of an inode mark on the same group. Unlike the
12 * prototype test fanotify06, do not use FAN_MODIFY event for the
13 * test mask, because it hides the bug.
14 *
15 * This is a regression test for commit:
16 *
17 * 9bdda4e9cf2d fsnotify: fix ignore mask logic in fsnotify()
18 */
19 #define _GNU_SOURCE
20 #include "config.h"
21
22 #include <stdio.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <sys/mount.h>
31 #include <sys/syscall.h>
32 #include "tst_test.h"
33 #include "fanotify.h"
34
35 #if defined(HAVE_SYS_FANOTIFY_H)
36 #include <sys/fanotify.h>
37
38 #define EVENT_MAX 1024
39 /* size of the event structure, not counting name */
40 #define EVENT_SIZE (sizeof (struct fanotify_event_metadata))
41 /* reasonable guess as to size of 1024 events */
42 #define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE)
43
44 static unsigned int fanotify_prio[] = {
45 FAN_CLASS_PRE_CONTENT,
46 FAN_CLASS_CONTENT,
47 FAN_CLASS_NOTIF
48 };
49 #define FANOTIFY_PRIORITIES ARRAY_SIZE(fanotify_prio)
50
51 #define GROUPS_PER_PRIO 3
52
53 static int fd_notify[FANOTIFY_PRIORITIES][GROUPS_PER_PRIO];
54
55 static char event_buf[EVENT_BUF_LEN];
56
57 #define MOUNT_PATH "fs_mnt"
58 #define MNT2_PATH "mntpoint"
59 #define FILE_NAME "testfile"
60 #define FILE2_NAME "testfile2"
61 #define TEST_APP "fanotify_child"
62 #define TEST_APP2 "fanotify_child2"
63 #define FILE_PATH MOUNT_PATH"/"FILE_NAME
64 #define FILE2_PATH MOUNT_PATH"/"FILE2_NAME
65 #define FILE_EXEC_PATH MOUNT_PATH"/"TEST_APP
66 #define FILE2_EXEC_PATH MOUNT_PATH"/"TEST_APP2
67 #define FILE_MNT2 MNT2_PATH"/"FILE_NAME
68 #define FILE2_MNT2 MNT2_PATH"/"FILE2_NAME
69 #define FILE_EXEC_PATH2 MNT2_PATH"/"TEST_APP
70 #define FILE2_EXEC_PATH2 MNT2_PATH"/"TEST_APP2
71
72 static pid_t child_pid;
73 static int bind_mount_created;
74
75 enum {
76 FANOTIFY_INODE,
77 FANOTIFY_MOUNT,
78 FANOTIFY_FILESYSTEM,
79 };
80
81 static struct fanotify_mark_type fanotify_mark_types[] = {
82 INIT_FANOTIFY_MARK_TYPE(INODE),
83 INIT_FANOTIFY_MARK_TYPE(MOUNT),
84 INIT_FANOTIFY_MARK_TYPE(FILESYSTEM),
85 };
86
87 static struct tcase {
88 const char *tname;
89 const char *mark_path;
90 int mark_type;
91 const char *ignore_path;
92 int ignore_mark_type;
93 const char *event_path;
94 unsigned long long expected_mask_with_ignore;
95 unsigned long long expected_mask_without_ignore;
96 } tcases[] = {
97 {
98 "ignore mount events created on a specific file",
99 MOUNT_PATH, FANOTIFY_MOUNT,
100 FILE_MNT2, FANOTIFY_INODE,
101 FILE_PATH, 0, FAN_OPEN
102 },
103 {
104 "ignore exec mount events created on a specific file",
105 MOUNT_PATH, FANOTIFY_MOUNT,
106 FILE_EXEC_PATH2, FANOTIFY_INODE,
107 FILE_EXEC_PATH, FAN_OPEN_EXEC, FAN_OPEN | FAN_OPEN_EXEC
108 },
109 {
110 "don't ignore mount events created on another file",
111 MOUNT_PATH, FANOTIFY_MOUNT,
112 FILE_PATH, FANOTIFY_INODE,
113 FILE2_PATH, FAN_OPEN, FAN_OPEN
114 },
115 {
116 "don't ignore exec mount events created on another file",
117 MOUNT_PATH, FANOTIFY_MOUNT,
118 FILE_EXEC_PATH, FANOTIFY_INODE,
119 FILE2_EXEC_PATH, FAN_OPEN | FAN_OPEN_EXEC,
120 FAN_OPEN | FAN_OPEN_EXEC
121 },
122 {
123 "ignore inode events created on a specific mount point",
124 FILE_PATH, FANOTIFY_INODE,
125 MNT2_PATH, FANOTIFY_MOUNT,
126 FILE_MNT2, 0, FAN_OPEN
127 },
128 {
129 "ignore exec inode events created on a specific mount point",
130 FILE_EXEC_PATH, FANOTIFY_INODE,
131 MNT2_PATH, FANOTIFY_MOUNT,
132 FILE_EXEC_PATH2, FAN_OPEN_EXEC, FAN_OPEN | FAN_OPEN_EXEC
133 },
134 {
135 "don't ignore inode events created on another mount point",
136 FILE_MNT2, FANOTIFY_INODE,
137 MNT2_PATH, FANOTIFY_MOUNT,
138 FILE_PATH, FAN_OPEN, FAN_OPEN
139 },
140 {
141 "don't ignore exec inode events created on another mount point",
142 FILE_EXEC_PATH2, FANOTIFY_INODE,
143 MNT2_PATH, FANOTIFY_MOUNT,
144 FILE_EXEC_PATH, FAN_OPEN | FAN_OPEN_EXEC,
145 FAN_OPEN | FAN_OPEN_EXEC
146 },
147 {
148 "ignore fs events created on a specific file",
149 MOUNT_PATH, FANOTIFY_FILESYSTEM,
150 FILE_PATH, FANOTIFY_INODE,
151 FILE_PATH, 0, FAN_OPEN
152 },
153 {
154 "ignore exec fs events created on a specific file",
155 MOUNT_PATH, FANOTIFY_FILESYSTEM,
156 FILE_EXEC_PATH, FANOTIFY_INODE,
157 FILE_EXEC_PATH, FAN_OPEN_EXEC, FAN_OPEN | FAN_OPEN_EXEC
158 },
159 {
160 "don't ignore mount events created on another file",
161 MOUNT_PATH, FANOTIFY_FILESYSTEM,
162 FILE_PATH, FANOTIFY_INODE,
163 FILE2_PATH, FAN_OPEN, FAN_OPEN
164 },
165 {
166 "don't ignore exec mount events created on another file",
167 MOUNT_PATH, FANOTIFY_FILESYSTEM,
168 FILE_EXEC_PATH, FANOTIFY_INODE,
169 FILE2_EXEC_PATH, FAN_OPEN | FAN_OPEN_EXEC,
170 FAN_OPEN | FAN_OPEN_EXEC
171 },
172 {
173 "ignore fs events created on a specific mount point",
174 MOUNT_PATH, FANOTIFY_FILESYSTEM,
175 MNT2_PATH, FANOTIFY_MOUNT,
176 FILE_MNT2, 0, FAN_OPEN
177 },
178 {
179 "ignore exec fs events created on a specific mount point",
180 MOUNT_PATH, FANOTIFY_FILESYSTEM,
181 MNT2_PATH, FANOTIFY_MOUNT,
182 FILE_EXEC_PATH2, FAN_OPEN_EXEC, FAN_OPEN | FAN_OPEN_EXEC
183 },
184 {
185 "don't ignore fs events created on another mount point",
186 MOUNT_PATH, FANOTIFY_FILESYSTEM,
187 MNT2_PATH, FANOTIFY_MOUNT,
188 FILE_PATH, FAN_OPEN, FAN_OPEN
189 },
190 {
191 "don't ignore exec fs events created on another mount point",
192 MOUNT_PATH, FANOTIFY_FILESYSTEM,
193 MNT2_PATH, FANOTIFY_MOUNT,
194 FILE_EXEC_PATH, FAN_OPEN | FAN_OPEN_EXEC,
195 FAN_OPEN | FAN_OPEN_EXEC
196 }
197 };
198
create_fanotify_groups(unsigned int n)199 static int create_fanotify_groups(unsigned int n)
200 {
201 struct tcase *tc = &tcases[n];
202 struct fanotify_mark_type *mark, *ignore_mark;
203 unsigned int p, i;
204 int ret;
205
206 mark = &fanotify_mark_types[tc->mark_type];
207 ignore_mark = &fanotify_mark_types[tc->ignore_mark_type];
208
209 for (p = 0; p < FANOTIFY_PRIORITIES; p++) {
210 for (i = 0; i < GROUPS_PER_PRIO; i++) {
211 fd_notify[p][i] = SAFE_FANOTIFY_INIT(fanotify_prio[p] |
212 FAN_NONBLOCK,
213 O_RDONLY);
214
215 /* Add mark for each group */
216 ret = fanotify_mark(fd_notify[p][i],
217 FAN_MARK_ADD | mark->flag,
218 tc->expected_mask_without_ignore,
219 AT_FDCWD, tc->mark_path);
220 if (ret < 0) {
221 if (errno == EINVAL &&
222 tc->expected_mask_without_ignore &
223 FAN_OPEN_EXEC) {
224 tst_res(TCONF,
225 "FAN_OPEN_EXEC not supported "
226 "by kernel?");
227 return -1;
228 } else if (errno == EINVAL &&
229 tc->mark_type == FANOTIFY_FILESYSTEM) {
230 tst_res(TCONF,
231 "FAN_MARK_FILESYSTEM not "
232 "supported in kernel?");
233 return -1;
234 }
235 tst_brk(TBROK | TERRNO,
236 "fanotify_mark(%d, FAN_MARK_ADD | %s,"
237 "FAN_OPEN, AT_FDCWD, %s) failed",
238 fd_notify[p][i], mark->name,
239 tc->mark_path);
240 }
241 /* Add ignore mark for groups with higher priority */
242 if (p == 0)
243 continue;
244 ret = fanotify_mark(fd_notify[p][i],
245 FAN_MARK_ADD | ignore_mark->flag |
246 FAN_MARK_IGNORED_MASK |
247 FAN_MARK_IGNORED_SURV_MODIFY,
248 FAN_OPEN, AT_FDCWD,
249 tc->ignore_path);
250 if (ret < 0) {
251 tst_brk(TBROK | TERRNO,
252 "fanotify_mark(%d, FAN_MARK_ADD | %s | "
253 "FAN_MARK_IGNORED_MASK | "
254 "FAN_MARK_IGNORED_SURV_MODIFY, "
255 "FAN_OPEN, AT_FDCWD, %s) failed",
256 fd_notify[p][i], ignore_mark->name,
257 tc->ignore_path);
258 }
259 }
260 }
261 return 0;
262 }
263
cleanup_fanotify_groups(void)264 static void cleanup_fanotify_groups(void)
265 {
266 unsigned int i, p;
267
268 for (p = 0; p < FANOTIFY_PRIORITIES; p++) {
269 for (i = 0; i < GROUPS_PER_PRIO; i++) {
270 if (fd_notify[p][i] > 0)
271 SAFE_CLOSE(fd_notify[p][i]);
272 }
273 }
274 }
275
verify_event(int group,struct fanotify_event_metadata * event,unsigned long long expected_mask)276 static void verify_event(int group, struct fanotify_event_metadata *event,
277 unsigned long long expected_mask)
278 {
279 if (event->mask != expected_mask) {
280 tst_res(TFAIL, "group %d got event: mask %llx (expected %llx) "
281 "pid=%u fd=%u", group, (unsigned long long)event->mask,
282 (unsigned long long) expected_mask,
283 (unsigned)event->pid, event->fd);
284 } else if (event->pid != child_pid) {
285 tst_res(TFAIL, "group %d got event: mask %llx pid=%u "
286 "(expected %u) fd=%u", group,
287 (unsigned long long)event->mask, (unsigned)event->pid,
288 (unsigned)getpid(), event->fd);
289 } else {
290 tst_res(TPASS, "group %d got event: mask %llx pid=%u fd=%u",
291 group, (unsigned long long)event->mask,
292 (unsigned)event->pid, event->fd);
293 }
294 }
295
generate_event(const char * event_path,unsigned long long expected_mask)296 static int generate_event(const char *event_path,
297 unsigned long long expected_mask)
298 {
299 int fd, status;
300
301 child_pid = SAFE_FORK();
302
303 if (child_pid == 0) {
304 if (expected_mask & FAN_OPEN_EXEC) {
305 SAFE_EXECL(event_path, event_path, NULL);
306 } else {
307 fd = SAFE_OPEN(event_path, O_RDONLY);
308
309 if (fd > 0)
310 SAFE_CLOSE(fd);
311 }
312
313 exit(0);
314 }
315
316 SAFE_WAITPID(child_pid, &status, 0);
317
318 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
319 return 1;
320 return 0;
321 }
322
test_fanotify(unsigned int n)323 static void test_fanotify(unsigned int n)
324 {
325 struct tcase *tc = &tcases[n];
326 struct fanotify_mark_type *mark, *ignore_mark;
327 int ret;
328 unsigned int p, i;
329 struct fanotify_event_metadata *event;
330
331 tst_res(TINFO, "Test #%d: %s", n, tc->tname);
332
333 if (create_fanotify_groups(n) != 0)
334 goto cleanup;
335
336 mark = &fanotify_mark_types[tc->mark_type];
337 ignore_mark = &fanotify_mark_types[tc->ignore_mark_type];
338
339 /* Generate event in child process */
340 if (!generate_event(tc->event_path, tc->expected_mask_with_ignore))
341 tst_brk(TBROK, "Child process terminated incorrectly");
342
343 /* First verify all groups without matching ignore mask got the event */
344 for (p = 0; p < FANOTIFY_PRIORITIES; p++) {
345 if (p > 0 && !tc->expected_mask_with_ignore)
346 break;
347
348 for (i = 0; i < GROUPS_PER_PRIO; i++) {
349 ret = read(fd_notify[p][i], event_buf, EVENT_BUF_LEN);
350 if (ret < 0) {
351 if (errno == EAGAIN) {
352 tst_res(TFAIL, "group %d (prio %d) "
353 "with %s did not get event",
354 i, p, mark->name);
355 }
356 tst_brk(TBROK | TERRNO,
357 "reading fanotify events failed");
358 }
359 if (ret < (int)FAN_EVENT_METADATA_LEN) {
360 tst_brk(TBROK,
361 "short read when reading fanotify "
362 "events (%d < %d)", ret,
363 (int)EVENT_BUF_LEN);
364 }
365 event = (struct fanotify_event_metadata *)event_buf;
366 if (ret > (int)event->event_len) {
367 tst_res(TFAIL, "group %d (prio %d) with %s "
368 "got more than one event (%d > %d)",
369 i, p, mark->name, ret,
370 event->event_len);
371 } else {
372 verify_event(i, event, p == 0 ?
373 tc->expected_mask_without_ignore :
374 tc->expected_mask_with_ignore);
375 }
376 if (event->fd != FAN_NOFD)
377 SAFE_CLOSE(event->fd);
378 }
379 }
380 /* Then verify all groups with matching ignore mask did got the event */
381 for (p = 1; p < FANOTIFY_PRIORITIES &&
382 !tc->expected_mask_with_ignore; p++) {
383 for (i = 0; i < GROUPS_PER_PRIO; i++) {
384 ret = read(fd_notify[p][i], event_buf, EVENT_BUF_LEN);
385 if (ret == 0) {
386 tst_brk(TBROK,
387 "zero length read from fanotify fd");
388 }
389 if (ret > 0) {
390 tst_res(TFAIL, "group %d (prio %d) with %s and "
391 "%s ignore mask got event",
392 i, p, mark->name, ignore_mark->name);
393 if (event->fd != FAN_NOFD)
394 SAFE_CLOSE(event->fd);
395 } else if (errno == EAGAIN) {
396 tst_res(TPASS, "group %d (prio %d) with %s and "
397 "%s ignore mask got no event",
398 i, p, mark->name, ignore_mark->name);
399 } else {
400 tst_brk(TBROK | TERRNO,
401 "reading fanotify events failed");
402 }
403 }
404 }
405 cleanup:
406 cleanup_fanotify_groups();
407 }
408
setup(void)409 static void setup(void)
410 {
411 /* Create another bind mount at another path for generating events */
412 SAFE_MKDIR(MNT2_PATH, 0755);
413 SAFE_MOUNT(MOUNT_PATH, MNT2_PATH, "none", MS_BIND, NULL);
414 bind_mount_created = 1;
415
416 SAFE_FILE_PRINTF(FILE_PATH, "1");
417 SAFE_FILE_PRINTF(FILE2_PATH, "1");
418
419 SAFE_CP(TEST_APP, FILE_EXEC_PATH);
420 SAFE_CP(TEST_APP, FILE2_EXEC_PATH);
421 }
422
cleanup(void)423 static void cleanup(void)
424 {
425 cleanup_fanotify_groups();
426
427 if (bind_mount_created && tst_umount(MNT2_PATH) < 0)
428 tst_brk(TBROK | TERRNO, "bind umount failed");
429 }
430
431 static const char *const resource_files[] = {
432 TEST_APP,
433 NULL
434 };
435
436 static struct tst_test test = {
437 .test = test_fanotify,
438 .tcnt = ARRAY_SIZE(tcases),
439 .setup = setup,
440 .cleanup = cleanup,
441 .mount_device = 1,
442 .mntpoint = MOUNT_PATH,
443 .needs_root = 1,
444 .forks_child = 1,
445 .resource_files = resource_files,
446 .tags = (const struct tst_tag[]) {
447 {"linux-git", "9bdda4e9cf2d"},
448 {}
449 }
450 };
451
452 #else
453 TST_TEST_TCONF("system doesn't have required fanotify support");
454 #endif
455