• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <fcntl.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <sys/mount.h>
29 #include <sys/syscall.h>
30 #include "tst_test.h"
31 #include "fanotify.h"
32 
33 #if defined(HAVE_SYS_FANOTIFY_H)
34 #include <sys/fanotify.h>
35 
36 #define EVENT_MAX 1024
37 /* size of the event structure, not counting name */
38 #define EVENT_SIZE  (sizeof (struct fanotify_event_metadata))
39 /* reasonable guess as to size of 1024 events */
40 #define EVENT_BUF_LEN        (EVENT_MAX * EVENT_SIZE)
41 
42 static unsigned int fanotify_prio[] = {
43 	FAN_CLASS_PRE_CONTENT,
44 	FAN_CLASS_CONTENT,
45 	FAN_CLASS_NOTIF
46 };
47 #define FANOTIFY_PRIORITIES ARRAY_SIZE(fanotify_prio)
48 
49 #define GROUPS_PER_PRIO 3
50 
51 static int fd_notify[FANOTIFY_PRIORITIES][GROUPS_PER_PRIO];
52 
53 static char event_buf[EVENT_BUF_LEN];
54 
55 #define MOUNT_PATH "fs_mnt"
56 #define MNT2_PATH "mntpoint"
57 #define FILE_NAME "testfile"
58 #define FILE2_NAME "testfile2"
59 #define FILE_PATH MOUNT_PATH"/"FILE_NAME
60 #define FILE2_PATH MOUNT_PATH"/"FILE2_NAME
61 #define FILE_MNT2 MNT2_PATH"/"FILE_NAME
62 #define FILE2_MNT2 MNT2_PATH"/"FILE2_NAME
63 
64 static int bind_mount_created;
65 
66 enum {
67 	FANOTIFY_INODE,
68 	FANOTIFY_MOUNT,
69 	FANOTIFY_FILESYSTEM,
70 };
71 
72 static struct fanotify_mark_type fanotify_mark_types[] = {
73 	INIT_FANOTIFY_MARK_TYPE(INODE),
74 	INIT_FANOTIFY_MARK_TYPE(MOUNT),
75 	INIT_FANOTIFY_MARK_TYPE(FILESYSTEM),
76 };
77 
78 static struct tcase {
79 	const char *tname;
80 	const char *mark_path;
81 	int mark_type;
82 	const char *ignore_path;
83 	int ignore_mark_type;
84 	const char *event_path;
85 	int expect_event;
86 } tcases[] = {
87 	{
88 		"ignore mount events created on a specific file",
89 		MOUNT_PATH, FANOTIFY_MOUNT,
90 		FILE_MNT2, FANOTIFY_INODE,
91 		FILE_PATH, 0
92 	},
93 	{
94 		"don't ignore mount events created on another file",
95 		MOUNT_PATH, FANOTIFY_MOUNT,
96 		FILE_PATH, FANOTIFY_INODE,
97 		FILE2_PATH, 1
98 	},
99 	{
100 		"ignore inode events created on a specific mount point",
101 		FILE_PATH, FANOTIFY_INODE,
102 		MNT2_PATH, FANOTIFY_MOUNT,
103 		FILE_MNT2, 0
104 	},
105 	{
106 		"don't ignore inode events created on another mount point",
107 		FILE_MNT2, FANOTIFY_INODE,
108 		MNT2_PATH, FANOTIFY_MOUNT,
109 		FILE_PATH, 1
110 	},
111 	{
112 		"ignore fs events created on a specific file",
113 		MOUNT_PATH, FANOTIFY_FILESYSTEM,
114 		FILE_PATH, FANOTIFY_INODE,
115 		FILE_PATH, 0
116 	},
117 	{
118 		"don't ignore mount events created on another file",
119 		MOUNT_PATH, FANOTIFY_FILESYSTEM,
120 		FILE_PATH, FANOTIFY_INODE,
121 		FILE2_PATH, 1
122 	},
123 	{
124 		"ignore fs events created on a specific mount point",
125 		MOUNT_PATH, FANOTIFY_FILESYSTEM,
126 		MNT2_PATH, FANOTIFY_MOUNT,
127 		FILE_MNT2, 0
128 	},
129 	{
130 		"don't ignore fs events created on another mount point",
131 		MOUNT_PATH, FANOTIFY_FILESYSTEM,
132 		MNT2_PATH, FANOTIFY_MOUNT,
133 		FILE_PATH, 1
134 	},
135 };
136 
create_fanotify_groups(unsigned int n)137 static int create_fanotify_groups(unsigned int n)
138 {
139 	struct tcase *tc = &tcases[n];
140 	struct fanotify_mark_type *mark, *ignore_mark;
141 	unsigned int p, i;
142 	int ret;
143 
144 	mark = &fanotify_mark_types[tc->mark_type];
145 	ignore_mark = &fanotify_mark_types[tc->ignore_mark_type];
146 
147 	for (p = 0; p < FANOTIFY_PRIORITIES; p++) {
148 		for (i = 0; i < GROUPS_PER_PRIO; i++) {
149 			fd_notify[p][i] = SAFE_FANOTIFY_INIT(fanotify_prio[p] |
150 							     FAN_NONBLOCK,
151 							     O_RDONLY);
152 
153 			/* Add mark for each group */
154 			ret = fanotify_mark(fd_notify[p][i],
155 					    FAN_MARK_ADD | mark->flag,
156 					    FAN_OPEN, AT_FDCWD, tc->mark_path);
157 			if (ret < 0) {
158 				if (errno == EINVAL &&
159 				    tc->mark_type == FANOTIFY_FILESYSTEM) {
160 					tst_res(TCONF,
161 						"FAN_MARK_FILESYSTEM not "
162 						"supported in kernel?");
163 					return -1;
164 				}
165 				tst_brk(TBROK | TERRNO,
166 					"fanotify_mark(%d, FAN_MARK_ADD | %s,"
167 					"FAN_OPEN, AT_FDCWD, %s) failed",
168 					fd_notify[p][i], mark->name,
169 					tc->mark_path);
170 			}
171 			/* Add ignore mark for groups with higher priority */
172 			if (p == 0)
173 				continue;
174 			ret = fanotify_mark(fd_notify[p][i],
175 					    FAN_MARK_ADD | ignore_mark->flag |
176 					    FAN_MARK_IGNORED_MASK |
177 					    FAN_MARK_IGNORED_SURV_MODIFY,
178 					    FAN_OPEN, AT_FDCWD,
179 					    tc->ignore_path);
180 			if (ret < 0) {
181 				tst_brk(TBROK | TERRNO,
182 					"fanotify_mark(%d, FAN_MARK_ADD | %s | "
183 					"FAN_MARK_IGNORED_MASK | "
184 					"FAN_MARK_IGNORED_SURV_MODIFY, "
185 					"FAN_OPEN, AT_FDCWD, %s) failed",
186 					fd_notify[p][i], ignore_mark->name,
187 					tc->ignore_path);
188 			}
189 		}
190 	}
191 	return 0;
192 }
193 
cleanup_fanotify_groups(void)194 static void cleanup_fanotify_groups(void)
195 {
196 	unsigned int i, p;
197 
198 	for (p = 0; p < FANOTIFY_PRIORITIES; p++) {
199 		for (i = 0; i < GROUPS_PER_PRIO; i++) {
200 			if (fd_notify[p][i] > 0)
201 				SAFE_CLOSE(fd_notify[p][i]);
202 		}
203 	}
204 }
205 
verify_event(int group,struct fanotify_event_metadata * event)206 static void verify_event(int group, struct fanotify_event_metadata *event)
207 {
208 	if (event->mask != FAN_OPEN) {
209 		tst_res(TFAIL, "group %d got event: mask %llx (expected %llx) "
210 			"pid=%u fd=%u", group, (unsigned long long)event->mask,
211 			(unsigned long long)FAN_OPEN,
212 			(unsigned)event->pid, event->fd);
213 	} else if (event->pid != getpid()) {
214 		tst_res(TFAIL, "group %d got event: mask %llx pid=%u "
215 			"(expected %u) fd=%u", group,
216 			(unsigned long long)event->mask, (unsigned)event->pid,
217 			(unsigned)getpid(), event->fd);
218 	} else {
219 		tst_res(TPASS, "group %d got event: mask %llx pid=%u fd=%u",
220 			group, (unsigned long long)event->mask,
221 			(unsigned)event->pid, event->fd);
222 	}
223 }
224 
test_fanotify(unsigned int n)225 static void test_fanotify(unsigned int n)
226 {
227 	struct tcase *tc = &tcases[n];
228 	struct fanotify_mark_type *mark, *ignore_mark;
229 	int ret, fd;
230 	unsigned int p, i;
231 	struct fanotify_event_metadata *event;
232 
233 	tst_res(TINFO, "Test #%d: %s", n, tc->tname);
234 
235 	if (create_fanotify_groups(n) != 0)
236 		goto cleanup;
237 
238 	mark = &fanotify_mark_types[tc->mark_type];
239 	ignore_mark = &fanotify_mark_types[tc->ignore_mark_type];
240 
241 	/*
242 	 * generate sequence of events
243 	 */
244 	fd = SAFE_OPEN(tc->event_path, O_RDONLY);
245 	SAFE_CLOSE(fd);
246 
247 	/* First verify all groups without matching ignore mask got the event */
248 	for (p = 0; p < FANOTIFY_PRIORITIES; p++) {
249 		if (p > 0 && !tc->expect_event)
250 			break;
251 
252 		for (i = 0; i < GROUPS_PER_PRIO; i++) {
253 			ret = read(fd_notify[p][i], event_buf, EVENT_BUF_LEN);
254 			if (ret < 0) {
255 				if (errno == EAGAIN) {
256 					tst_res(TFAIL, "group %d (prio %d) "
257 						"with %s did not get event",
258 						i, p, mark->name);
259 				}
260 				tst_brk(TBROK | TERRNO,
261 					"reading fanotify events failed");
262 			}
263 			if (ret < (int)FAN_EVENT_METADATA_LEN) {
264 				tst_brk(TBROK,
265 					"short read when reading fanotify "
266 					"events (%d < %d)", ret,
267 					(int)EVENT_BUF_LEN);
268 			}
269 			event = (struct fanotify_event_metadata *)event_buf;
270 			if (ret > (int)event->event_len) {
271 				tst_res(TFAIL, "group %d (prio %d) with %s "
272 					"got more than one event (%d > %d)",
273 					i, p, mark->name, ret,
274 					event->event_len);
275 			} else {
276 				verify_event(i, event);
277 			}
278 			if (event->fd != FAN_NOFD)
279 				SAFE_CLOSE(event->fd);
280 		}
281 	}
282 	/* Then verify all groups with matching ignore mask did got the event */
283 	for (p = 1; p < FANOTIFY_PRIORITIES && !tc->expect_event; p++) {
284 		for (i = 0; i < GROUPS_PER_PRIO; i++) {
285 			ret = read(fd_notify[p][i], event_buf, EVENT_BUF_LEN);
286 			if (ret == 0) {
287 				tst_brk(TBROK,
288 					"zero length read from fanotify fd");
289 			}
290 			if (ret > 0) {
291 				tst_res(TFAIL, "group %d (prio %d) with %s and "
292 					"%s ignore mask got event",
293 					i, p, mark->name, ignore_mark->name);
294 				if (event->fd != FAN_NOFD)
295 					SAFE_CLOSE(event->fd);
296 			} else if (errno == EAGAIN) {
297 				tst_res(TPASS, "group %d (prio %d) with %s and "
298 					"%s ignore mask got no event",
299 					i, p, mark->name, ignore_mark->name);
300 			} else {
301 				tst_brk(TBROK | TERRNO,
302 					"reading fanotify events failed");
303 			}
304 		}
305 	}
306 cleanup:
307 	cleanup_fanotify_groups();
308 }
309 
setup(void)310 static void setup(void)
311 {
312 	/* Create another bind mount at another path for generating events */
313 	SAFE_MKDIR(MNT2_PATH, 0755);
314 	SAFE_MOUNT(MOUNT_PATH, MNT2_PATH, "none", MS_BIND, NULL);
315 	bind_mount_created = 1;
316 
317 	SAFE_FILE_PRINTF(FILE_PATH, "1");
318 	SAFE_FILE_PRINTF(FILE2_PATH, "1");
319 }
320 
cleanup(void)321 static void cleanup(void)
322 {
323 	cleanup_fanotify_groups();
324 
325 	if (bind_mount_created && tst_umount(MNT2_PATH) < 0)
326 		tst_brk(TBROK | TERRNO, "bind umount failed");
327 }
328 
329 static struct tst_test test = {
330 	.test = test_fanotify,
331 	.tcnt = ARRAY_SIZE(tcases),
332 	.setup = setup,
333 	.cleanup = cleanup,
334 	.mount_device = 1,
335 	.mntpoint = MOUNT_PATH,
336 	.needs_tmpdir = 1,
337 	.needs_root = 1
338 };
339 
340 #else
341 	TST_TEST_TCONF("system doesn't have required fanotify support");
342 #endif
343