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 various fanotify special flags.
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 /* size of the event structure, not counting name */
28 #define EVENT_SIZE (sizeof(struct fanotify_event_metadata))
29
30 #define BUF_SIZE 256
31 #define TST_TOTAL 9
32
33 static char fname[BUF_SIZE];
34 static char sname[BUF_SIZE];
35 static char dir[BUF_SIZE];
36 static int fd_notify;
37 static char event_buf[EVENT_SIZE];
38
expect_str_fail(int expect)39 static char *expect_str_fail(int expect)
40 {
41 if (expect == 0)
42 return "failed";
43 return "unexpectedly succeeded";
44 }
45
expect_str_pass(int expect)46 static char *expect_str_pass(int expect)
47 {
48 if (expect == 0)
49 return "succeeded";
50 return "failed";
51 }
52
check_mark(char * file,unsigned long long flag,char * flagstr,int expect,void (* test_event)(char *))53 static void check_mark(char *file, unsigned long long flag, char *flagstr,
54 int expect, void (*test_event)(char *))
55 {
56 if (fanotify_mark(fd_notify, FAN_MARK_ADD | flag, FAN_OPEN, AT_FDCWD,
57 file) != expect) {
58 tst_res(TFAIL,
59 "fanotify_mark (%d, FAN_MARK_ADD | %s, FAN_OPEN, "
60 "AT_FDCWD, '%s') %s", fd_notify, flagstr, file,
61 expect_str_fail(expect));
62 } else {
63 tst_res(TPASS,
64 "fanotify_mark (%d, FAN_MARK_ADD | %s, FAN_OPEN, "
65 "AT_FDCWD, '%s') %s", fd_notify, flagstr, file,
66 expect_str_pass(expect));
67
68 /* If we expected failure there's nothing to clean up */
69 if (expect == -1)
70 return;
71
72 if (test_event)
73 test_event(file);
74
75 SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_REMOVE | flag,
76 FAN_OPEN, AT_FDCWD, file);
77 }
78 }
79
80 #define CHECK_MARK(file, flag, expect, func) check_mark(file, flag, #flag, expect, func)
81
do_open(char * file,int flag)82 static void do_open(char *file, int flag)
83 {
84 int fd;
85
86 fd = SAFE_OPEN(file, O_RDONLY | flag);
87 SAFE_CLOSE(fd);
88 }
89
open_file(char * file)90 static void open_file(char *file)
91 {
92 do_open(file, 0);
93 }
94
open_dir(char * file)95 static void open_dir(char *file)
96 {
97 do_open(file, O_DIRECTORY);
98 }
99
verify_event(int mask)100 static void verify_event(int mask)
101 {
102 struct fanotify_event_metadata *event;
103 struct stat st;
104
105 /* Read the event */
106 SAFE_READ(0, fd_notify, event_buf, EVENT_SIZE);
107 event = (struct fanotify_event_metadata *)&event_buf;
108 if (event->mask != FAN_OPEN) {
109 tst_res(TFAIL, "got unexpected event %llx",
110 (unsigned long long)event->mask);
111 } else if (fstat(event->fd, &st) < 0) {
112 tst_res(TFAIL, "failed to stat event->fd (%s)",
113 strerror(errno));
114 } else if ((int)(st.st_mode & S_IFMT) != mask) {
115 tst_res(TFAIL, "event->fd points to object of different type "
116 "(%o != %o)", st.st_mode & S_IFMT, mask);
117 } else {
118 tst_res(TPASS, "event generated properly for type %o", mask);
119 }
120 if (event->fd != FAN_NOFD)
121 SAFE_CLOSE(event->fd);
122 }
123
do_open_test(char * file,int flag,int mask)124 static void do_open_test(char *file, int flag, int mask)
125 {
126 do_open(file, flag);
127
128 verify_event(mask);
129 }
130
test_open_file(char * file)131 static void test_open_file(char *file)
132 {
133 do_open_test(file, 0, S_IFREG);
134 }
135
verify_no_event(void)136 static void verify_no_event(void)
137 {
138 int ret;
139
140 ret = read(fd_notify, event_buf, EVENT_SIZE);
141 if (ret != -1) {
142 struct fanotify_event_metadata *event;
143
144 event = (struct fanotify_event_metadata *)&event_buf;
145 tst_res(TFAIL, "seen unexpected event (mask %llx)",
146 (unsigned long long)event->mask);
147 /* Cleanup fd from the event */
148 if (event->fd != FAN_NOFD)
149 SAFE_CLOSE(event->fd);
150 } else if (errno != EAGAIN) {
151 tst_res(TFAIL | TERRNO, "read(%d, buf, %zu) failed", fd_notify,
152 EVENT_SIZE);
153 } else {
154 tst_res(TPASS, "No event as expected");
155 }
156 }
157
test_open_symlink(char * file)158 static void test_open_symlink(char *file)
159 {
160 /* Since mark is on a symlink, no event should be generated by opening a file */
161 do_open(file, 0);
162 verify_no_event();
163 }
164
test01(void)165 static void test01(void)
166 {
167 /* Check ONLYDIR on a directory */
168 CHECK_MARK(".", FAN_MARK_ONLYDIR, 0, NULL);
169
170 /* Check ONLYDIR without a directory */
171 CHECK_MARK(fname, FAN_MARK_ONLYDIR, -1, NULL);
172
173 /* Check DONT_FOLLOW for a symlink */
174 CHECK_MARK(sname, FAN_MARK_DONT_FOLLOW, 0, test_open_symlink);
175
176 /* Check without DONT_FOLLOW for a symlink */
177 CHECK_MARK(sname, 0, 0, test_open_file);
178
179 /* Verify FAN_MARK_FLUSH destroys all inode marks */
180 SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD, FAN_OPEN, AT_FDCWD, fname);
181 SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD, FAN_OPEN | FAN_ONDIR,
182 AT_FDCWD, dir);
183 open_file(fname);
184 verify_event(S_IFREG);
185 open_dir(dir);
186 verify_event(S_IFDIR);
187 SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_FLUSH, 0, AT_FDCWD, ".");
188
189 open_dir(dir);
190 verify_no_event();
191 }
192
setup(void)193 static void setup(void)
194 {
195 int fd;
196
197 sprintf(fname, "fname_%d", getpid());
198 fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0644);
199 SAFE_CLOSE(fd);
200
201 sprintf(sname, "symlink_%d", getpid());
202 SAFE_SYMLINK(fname, sname);
203
204 sprintf(dir, "dir_%d", getpid());
205 SAFE_MKDIR(dir, 0755);
206
207 fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF | FAN_NONBLOCK,
208 O_RDONLY);
209 }
210
cleanup(void)211 static void cleanup(void)
212 {
213 if (fd_notify > 0)
214 SAFE_CLOSE(fd_notify);
215 }
216
217 static struct tst_test test = {
218 .test_all = test01,
219 .setup = setup,
220 .cleanup = cleanup,
221 .needs_tmpdir = 1,
222 .needs_root = 1
223 };
224
225 #else
226 TST_TEST_TCONF("system doesn't have required fanotify support");
227 #endif
228