1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2013 SUSE. All Rights Reserved.
4 *
5 * Started by Jan Kara <[email protected]>
6 */
7
8 /*\
9 * [Description]
10 * Check that fanotify work for a file.
11 */
12
13 #define _GNU_SOURCE
14 #include "config.h"
15
16 #include <stdio.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <sys/syscall.h>
22 #include "tst_test.h"
23
24 #ifdef HAVE_SYS_FANOTIFY_H
25 #include "fanotify.h"
26
27 #define EVENT_MAX 1024
28 /* size of the event structure, not counting name */
29 #define EVENT_SIZE (sizeof(struct fanotify_event_metadata))
30 /* reasonable guess as to size of 1024 events */
31 #define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE)
32
33 #define BUF_SIZE 256
34 #define TST_TOTAL 12
35
36 #define MOUNT_PATH "fs_mnt"
37
38 static struct tcase {
39 const char *tname;
40 struct fanotify_mark_type mark;
41 unsigned int init_flags;
42 } tcases[] = {
43 {
44 "inode mark events",
45 INIT_FANOTIFY_MARK_TYPE(INODE),
46 FAN_CLASS_NOTIF
47 },
48 {
49 "mount mark events",
50 INIT_FANOTIFY_MARK_TYPE(MOUNT),
51 FAN_CLASS_NOTIF
52 },
53 {
54 "filesystem mark events",
55 INIT_FANOTIFY_MARK_TYPE(FILESYSTEM),
56 FAN_CLASS_NOTIF
57 },
58 {
59 "inode mark events (FAN_REPORT_FID)",
60 INIT_FANOTIFY_MARK_TYPE(INODE),
61 FAN_CLASS_NOTIF|FAN_REPORT_FID
62 },
63 {
64 "mount mark events (FAN_REPORT_FID)",
65 INIT_FANOTIFY_MARK_TYPE(MOUNT),
66 FAN_CLASS_NOTIF|FAN_REPORT_FID
67 },
68 {
69 "filesystem mark events (FAN_REPORT_FID)",
70 INIT_FANOTIFY_MARK_TYPE(FILESYSTEM),
71 FAN_CLASS_NOTIF|FAN_REPORT_FID
72 },
73 };
74
75 static char fname[BUF_SIZE];
76 static char buf[BUF_SIZE];
77 static int fd_notify;
78 static int fan_report_fid_unsupported;
79 static int mount_mark_fid_unsupported;
80 static int inode_mark_fid_xdev;
81 static int filesystem_mark_unsupported;
82
83 static unsigned long long event_set[EVENT_MAX];
84
85 static char event_buf[EVENT_BUF_LEN];
86
test_fanotify(unsigned int n)87 static void test_fanotify(unsigned int n)
88 {
89 struct tcase *tc = &tcases[n];
90 struct fanotify_mark_type *mark = &tc->mark;
91 int fd, ret, len, i = 0, test_num = 0;
92 int tst_count = 0;
93 int report_fid = (tc->init_flags & FAN_REPORT_FID);
94
95 tst_res(TINFO, "Test #%d: %s", n, tc->tname);
96
97 if (fan_report_fid_unsupported && report_fid) {
98 FANOTIFY_INIT_FLAGS_ERR_MSG(FAN_REPORT_FID, fan_report_fid_unsupported);
99 return;
100 }
101
102 if (filesystem_mark_unsupported && mark->flag == FAN_MARK_FILESYSTEM) {
103 FANOTIFY_MARK_FLAGS_ERR_MSG(mark, filesystem_mark_unsupported);
104 return;
105 }
106
107 if (mount_mark_fid_unsupported && report_fid && mark->flag != FAN_MARK_INODE) {
108 FANOTIFY_MARK_FLAGS_ERR_MSG(mark, mount_mark_fid_unsupported);
109 return;
110 }
111
112 fd_notify = SAFE_FANOTIFY_INIT(tc->init_flags, O_RDONLY);
113
114 SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD | mark->flag,
115 FAN_ACCESS | FAN_MODIFY | FAN_CLOSE | FAN_OPEN, AT_FDCWD, fname);
116
117 /*
118 * generate sequence of events
119 */
120 fd = SAFE_OPEN(fname, O_RDONLY);
121 event_set[tst_count] = FAN_OPEN;
122 tst_count++;
123
124 SAFE_READ(0, fd, buf, BUF_SIZE);
125 event_set[tst_count] = FAN_ACCESS;
126 tst_count++;
127
128 SAFE_CLOSE(fd);
129 event_set[tst_count] = FAN_CLOSE_NOWRITE;
130 tst_count++;
131
132 /*
133 * Get list of events so far. We get events here to avoid
134 * merging of following events with the previous ones.
135 */
136 ret = SAFE_READ(0, fd_notify, event_buf, EVENT_BUF_LEN);
137 len = ret;
138
139 fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700);
140 event_set[tst_count] = FAN_OPEN;
141 tst_count++;
142
143 SAFE_WRITE(SAFE_WRITE_ALL, fd, fname, strlen(fname));
144 event_set[tst_count] = FAN_MODIFY;
145 tst_count++;
146
147 SAFE_CLOSE(fd);
148 event_set[tst_count] = FAN_CLOSE_WRITE;
149 tst_count++;
150
151 /*
152 * get another list of events
153 */
154 ret = SAFE_READ(0, fd_notify, event_buf + len,
155 EVENT_BUF_LEN - len);
156 len += ret;
157
158 /*
159 * Ignore mask testing
160 */
161
162 /* Ignore access events */
163 SAFE_FANOTIFY_MARK(fd_notify,
164 FAN_MARK_ADD | mark->flag | FAN_MARK_IGNORED_MASK,
165 FAN_ACCESS, AT_FDCWD, fname);
166
167 fd = SAFE_OPEN(fname, O_RDWR);
168 event_set[tst_count] = FAN_OPEN;
169 tst_count++;
170
171 /* This event should be ignored */
172 SAFE_READ(0, fd, buf, BUF_SIZE);
173
174 /*
175 * get another list of events to verify the last one got ignored
176 */
177 ret = SAFE_READ(0, fd_notify, event_buf + len,
178 EVENT_BUF_LEN - len);
179 len += ret;
180
181 SAFE_LSEEK(fd, 0, SEEK_SET);
182 /* Generate modify event to clear ignore mask */
183 SAFE_WRITE(SAFE_WRITE_ALL, fd, fname, 1);
184 event_set[tst_count] = FAN_MODIFY;
185 tst_count++;
186
187 /*
188 * This event shouldn't be ignored because previous modification
189 * should have removed the ignore mask
190 */
191 SAFE_READ(0, fd, buf, BUF_SIZE);
192 event_set[tst_count] = FAN_ACCESS;
193 tst_count++;
194
195 SAFE_CLOSE(fd);
196 event_set[tst_count] = FAN_CLOSE_WRITE;
197 tst_count++;
198
199 /* Read events to verify previous access was properly generated */
200 ret = SAFE_READ(0, fd_notify, event_buf + len,
201 EVENT_BUF_LEN - len);
202 len += ret;
203
204 /*
205 * Now ignore open & close events regardless of file
206 * modifications
207 */
208 SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD | mark->flag |
209 FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY,
210 FAN_OPEN | FAN_CLOSE, AT_FDCWD, fname);
211
212 /* This event should be ignored */
213 fd = SAFE_OPEN(fname, O_RDWR);
214
215 SAFE_WRITE(SAFE_WRITE_ALL, fd, fname, 1);
216 event_set[tst_count] = FAN_MODIFY;
217 tst_count++;
218
219 /* This event should be still ignored */
220 SAFE_CLOSE(fd);
221
222 /* This event should still be ignored */
223 fd = SAFE_OPEN(fname, O_RDWR);
224
225 /* Read events to verify open & close were ignored */
226 ret = SAFE_READ(0, fd_notify, event_buf + len,
227 EVENT_BUF_LEN - len);
228 len += ret;
229
230 /* Now remove open and close from ignored mask */
231 SAFE_FANOTIFY_MARK(fd_notify,
232 FAN_MARK_REMOVE | mark->flag | FAN_MARK_IGNORED_MASK,
233 FAN_OPEN | FAN_CLOSE, AT_FDCWD, fname);
234
235 SAFE_CLOSE(fd);
236 event_set[tst_count] = FAN_CLOSE_WRITE;
237 tst_count++;
238
239 /* Read events to verify close was generated */
240 ret = SAFE_READ(0, fd_notify, event_buf + len,
241 EVENT_BUF_LEN - len);
242 len += ret;
243
244 if (TST_TOTAL != tst_count) {
245 tst_brk(TBROK,
246 "TST_TOTAL (%d) and tst_count (%d) are not "
247 "equal", TST_TOTAL, tst_count);
248 }
249 tst_count = 0;
250
251 /*
252 * check events
253 */
254 while (i < len) {
255 struct fanotify_event_metadata *event;
256
257 event = (struct fanotify_event_metadata *)&event_buf[i];
258 if (test_num >= TST_TOTAL) {
259 tst_res(TFAIL,
260 "got unnecessary event: mask=%llx "
261 "pid=%u fd=%d",
262 (unsigned long long)event->mask,
263 (unsigned int)event->pid, event->fd);
264 } else if (!(event->mask & event_set[test_num])) {
265 tst_res(TFAIL,
266 "got event: mask=%llx (expected %llx) "
267 "pid=%u fd=%d",
268 (unsigned long long)event->mask,
269 event_set[test_num],
270 (unsigned int)event->pid, event->fd);
271 } else if (event->pid != getpid()) {
272 tst_res(TFAIL,
273 "got event: mask=%llx pid=%u "
274 "(expected %u) fd=%d",
275 (unsigned long long)event->mask,
276 (unsigned int)event->pid,
277 (unsigned int)getpid(),
278 event->fd);
279 } else {
280 if (event->fd == -2 || (event->fd == FAN_NOFD &&
281 (tc->init_flags & FAN_REPORT_FID)))
282 goto pass;
283 ret = read(event->fd, buf, BUF_SIZE);
284 if (ret != (int)strlen(fname)) {
285 tst_res(TFAIL,
286 "cannot read from returned fd "
287 "of event: mask=%llx pid=%u "
288 "fd=%d ret=%d (errno=%d)",
289 (unsigned long long)event->mask,
290 (unsigned int)event->pid,
291 event->fd, ret, errno);
292 } else if (memcmp(buf, fname, strlen(fname))) {
293 tst_res(TFAIL,
294 "wrong data read from returned fd "
295 "of event: mask=%llx pid=%u "
296 "fd=%d",
297 (unsigned long long)event->mask,
298 (unsigned int)event->pid,
299 event->fd);
300 } else {
301 pass:
302 tst_res(TPASS,
303 "got event: mask=%llx pid=%u fd=%d",
304 (unsigned long long)event->mask,
305 (unsigned int)event->pid, event->fd);
306 }
307 }
308
309 /*
310 * We have verified the data now so close fd and
311 * invalidate it so that we don't check it again
312 * unnecessarily
313 */
314 if (event->fd >= 0)
315 SAFE_CLOSE(event->fd);
316 event->fd = -2;
317 event->mask &= ~event_set[test_num];
318
319 /* No events left in current mask? Go for next event */
320 if (event->mask == 0)
321 i += event->event_len;
322
323 test_num++;
324 }
325
326 for (; test_num < TST_TOTAL; test_num++) {
327 tst_res(TFAIL, "didn't get event: mask=%llx",
328 event_set[test_num]);
329
330 }
331
332
333 /*
334 * Try to setup a bogus mark on test tmp dir, to check if marks on
335 * different filesystems are supported.
336 * When tested fs has zero fsid (e.g. fuse) and events are reported
337 * with fsid+fid, watching different filesystems is not supported.
338 */
339 ret = report_fid ? inode_mark_fid_xdev : 0;
340 TST_EXP_FD_OR_FAIL(fanotify_mark(fd_notify, FAN_MARK_ADD, FAN_CLOSE_WRITE,
341 AT_FDCWD, "."), ret);
342
343 /* Remove mark to clear FAN_MARK_IGNORED_SURV_MODIFY */
344 SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_REMOVE | mark->flag,
345 FAN_ACCESS | FAN_MODIFY | FAN_CLOSE | FAN_OPEN,
346 AT_FDCWD, fname);
347
348 SAFE_CLOSE(fd_notify);
349 }
350
setup(void)351 static void setup(void)
352 {
353 int fd;
354
355 /* Check for kernel fanotify support */
356 fd = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF, O_RDONLY);
357 SAFE_CLOSE(fd);
358
359 sprintf(fname, MOUNT_PATH"/tfile_%d", getpid());
360 SAFE_FILE_PRINTF(fname, "1");
361
362 fan_report_fid_unsupported = fanotify_init_flags_supported_on_fs(FAN_REPORT_FID, fname);
363 filesystem_mark_unsupported = fanotify_mark_supported_on_fs(FAN_MARK_FILESYSTEM, fname);
364 mount_mark_fid_unsupported = fanotify_flags_supported_on_fs(FAN_REPORT_FID,
365 FAN_MARK_MOUNT,
366 FAN_OPEN, fname);
367 /*
368 * When mount mark is not supported due to zero fsid (e.g. fuse) or if TMPDIR has
369 * non-uniform fsid (e.g. btrfs subvol), multi fs inode marks are not supported.
370 */
371 if (mount_mark_fid_unsupported && errno == ENODEV) {
372 tst_res(TINFO, "filesystem %s does not support reporting events with fid from multi fs",
373 tst_device->fs_type);
374 inode_mark_fid_xdev = EXDEV;
375 }
376
377 if (fanotify_flags_supported_on_fs(FAN_REPORT_FID, FAN_MARK_MOUNT, FAN_OPEN, ".")) {
378 inode_mark_fid_xdev = errno;
379 tst_res(TINFO, "TMPDIR does not support reporting events with fid from multi fs");
380 }
381 }
382
cleanup(void)383 static void cleanup(void)
384 {
385 if (fd_notify > 0)
386 SAFE_CLOSE(fd_notify);
387 }
388
389 static struct tst_test test = {
390 .test = test_fanotify,
391 .tcnt = ARRAY_SIZE(tcases),
392 .setup = setup,
393 .cleanup = cleanup,
394 .needs_root = 1,
395 .mount_device = 1,
396 .mntpoint = MOUNT_PATH,
397 .all_filesystems = 1,
398 };
399
400 #else
401 TST_TEST_TCONF("system doesn't have required fanotify support");
402 #endif
403