1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2018 Matthew Bobrowski. All Rights Reserved.
4 *
5 * Started by Matthew Bobrowski <mbobrowski@mbobrowski.org>
6 */
7
8 /*\
9 * [Description]
10 * This set of tests is to ensure that the unprivileged listener feature of
11 * fanotify is functioning as expected. The objective of this test file is
12 * to generate a sequence of events and ensure that the returned events
13 * contain the limited values that an unprivileged listener is expected
14 * to receive.
15 */
16
17 #define _GNU_SOURCE
18 #include "config.h"
19
20 #include <pwd.h>
21 #include <stdio.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <sys/wait.h>
25
26 #include "tst_test.h"
27
28 #ifdef HAVE_SYS_FANOTIFY_H
29 #include "fanotify.h"
30
31 #define EVENT_MAX 1024
32 #define EVENT_SIZE (sizeof (struct fanotify_event_metadata))
33 #define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE)
34 #define EVENT_SET_MAX 48
35
36 #define BUF_SIZE 256
37
38 #define MOUNT_PATH "fs_mnt"
39 #define TEST_FILE MOUNT_PATH "/testfile"
40
41 static uid_t euid;
42 static int fd_notify;
43 static char buf[BUF_SIZE];
44 static struct fanotify_event_metadata event_buf[EVENT_BUF_LEN];
45
46 static struct test_case_t {
47 const char *name;
48 unsigned int fork;
49 unsigned int elevate;
50 unsigned int event_count;
51 unsigned long long event_set[EVENT_SET_MAX];
52 } test_cases[] = {
53 {
54 "unprivileged listener - events by self",
55 0,
56 0,
57 4,
58 {
59 FAN_OPEN,
60 FAN_ACCESS,
61 FAN_MODIFY,
62 FAN_CLOSE,
63 }
64 },
65 {
66 "unprivileged lisneter - events by child",
67 1,
68 0,
69 4,
70 {
71 FAN_OPEN,
72 FAN_ACCESS,
73 FAN_MODIFY,
74 FAN_CLOSE,
75 }
76 },
77 {
78 "unprivileged listener, privileged reader - events by self",
79 0,
80 1,
81 4,
82 {
83 FAN_OPEN,
84 FAN_ACCESS,
85 FAN_MODIFY,
86 FAN_CLOSE,
87 }
88 },
89 {
90 "unprivileged lisneter, privileged reader - events by child",
91 1,
92 1,
93 4,
94 {
95 FAN_OPEN,
96 FAN_ACCESS,
97 FAN_MODIFY,
98 FAN_CLOSE,
99 }
100 },
101 };
102
generate_events(void)103 static void generate_events(void)
104 {
105 int fd;
106
107 /* FAN_OPEN */
108 fd = SAFE_OPEN(TEST_FILE, O_RDWR);
109
110 /* FAN_ACCESS */
111 SAFE_READ(0, fd, buf, BUF_SIZE);
112
113 /* FAN_MODIFY */
114 SAFE_WRITE(1, fd, TEST_FILE, 1);
115
116 /* FAN_CLOSE */
117 SAFE_CLOSE(fd);
118 }
119
do_fork(void)120 static void do_fork(void)
121 {
122 int status;
123 pid_t child;
124
125 child = SAFE_FORK();
126
127 if (child == 0) {
128 SAFE_CLOSE(fd_notify);
129 generate_events();
130 exit(0);
131 }
132
133 SAFE_WAITPID(child, &status, 0);
134
135 if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
136 tst_brk(TBROK, "Child process terminated incorrectly. Aborting");
137 }
138
test_fanotify(unsigned int n)139 static void test_fanotify(unsigned int n)
140 {
141 int len = 0;
142 pid_t pid = getpid();
143 unsigned int test_number = 0;
144 struct fanotify_event_metadata *event;
145 struct test_case_t *tc = &test_cases[n];
146
147 tst_res(TINFO, "Test #%d %s", n, tc->name);
148
149 /* Relinquish privileged user */
150 if (euid == 0) {
151 tst_res(TINFO, "Running as privileged user, revoking");
152 struct passwd *nobody = SAFE_GETPWNAM("nobody");
153 SAFE_SETEUID(nobody->pw_uid);
154 }
155
156 /* Initialize fanotify */
157 fd_notify = fanotify_init(FANOTIFY_REQUIRED_USER_INIT_FLAGS, O_RDONLY);
158
159 if (fd_notify < 0) {
160 if (errno == EPERM || errno == EINVAL) {
161 tst_res(TCONF,
162 "unprivileged fanotify not supported by kernel?");
163 return;
164 } else {
165 tst_brk(TBROK | TERRNO,
166 "fanotify_init(FAN_CLASS_NOTIF, O_RDONLY) failed");
167 }
168 }
169
170 /* Place mark on object */
171 if (fanotify_mark(fd_notify, FAN_MARK_ADD, FAN_ALL_EVENTS,
172 AT_FDCWD, TEST_FILE) < 0) {
173 tst_brk(TBROK | TERRNO,
174 "fanotify_mark(%d, FAN_MARK_ADD, %d, "
175 "AT_FDCWD, %s) failed",
176 fd_notify,
177 FAN_ALL_EVENTS,
178 TEST_FILE);
179 }
180
181 /* Generate events in either child or listening process */
182 if (tc->fork)
183 do_fork();
184 else
185 generate_events();
186
187 /* Restore privileges */
188 if (euid == 0 && tc->elevate) {
189 tst_res(TINFO, "Restoring privileged user");
190 SAFE_SETEUID(0);
191 }
192
193 /* Read events from queue */
194 len = SAFE_READ(0, fd_notify, event_buf + len, EVENT_BUF_LEN - len);
195
196 event = event_buf;
197
198 /* Iterate over and validate events against expected result set */
199 while (FAN_EVENT_OK(event, len) && test_number < tc->event_count) {
200 if (!(event->mask & tc->event_set[test_number])) {
201 tst_res(TFAIL,
202 "Received unexpected event mask: mask=%llx "
203 "pid=%u fd=%d",
204 (unsigned long long) event->mask,
205 (unsigned) event->pid,
206 event->fd);
207 } else if ((!tc->fork && event->pid != pid) ||
208 (tc->fork && event->pid != 0)) {
209 tst_res(TFAIL,
210 "Received unexpected pid in event: "
211 "mask=%llx pid=%u (expected %u) fd=%d",
212 (unsigned long long) event->mask,
213 (unsigned) event->pid,
214 (tc->fork ? 0 : pid),
215 event->fd);
216 } else if (event->fd != FAN_NOFD) {
217 tst_res(TFAIL,
218 "Received unexpected file descriptor: "
219 "mask=%llx pid=%u fd=%d (expected %d)",
220 (unsigned long long) event->pid,
221 (unsigned) event->pid,
222 event->fd,
223 FAN_NOFD);
224 SAFE_CLOSE(event->fd);
225 } else {
226 tst_res(TPASS,
227 "Received event: mask=%llx, pid=%u fd=%d",
228 (unsigned long long) event->mask,
229 (unsigned) event->pid,
230 event->fd);
231 }
232
233 /* Non-permission events can be merged into a single event. */
234 event->mask &= ~tc->event_set[test_number];
235
236 if (event->mask == 0)
237 event = FAN_EVENT_NEXT(event, len);
238 test_number++;
239 }
240
241 /*
242 * Determine whether there is still unprocessed events remaining in the
243 * buffer. This is to cover the basis whereby the event processing loop
244 * terminates prematurely. In that case, we need to ensure that any
245 * event file descriptor that is open is closed so that the temporary
246 * filesystem can be unmounted.
247 */
248 if (FAN_EVENT_OK(event, len)) {
249 tst_res(TFAIL,
250 "Event processing loop exited prematurely. Did NOT "
251 "finish processing events in buffer. Cleaning up.");
252 while (FAN_EVENT_OK(event, len)) {
253 if (event->fd != FAN_NOFD)
254 SAFE_CLOSE(event->fd);
255 event = FAN_EVENT_NEXT(event, len);
256 }
257 }
258
259 SAFE_CLOSE(fd_notify);
260 }
261
setup(void)262 static void setup(void)
263 {
264 SAFE_FILE_PRINTF(TEST_FILE, "1");
265 SAFE_CHMOD(TEST_FILE, 0666);
266
267 /* Check for kernel fanotify support */
268 REQUIRE_FANOTIFY_INIT_FLAGS_SUPPORTED_ON_FS(FAN_REPORT_FID, TEST_FILE);
269
270 euid = geteuid();
271 }
272
cleanup(void)273 static void cleanup(void)
274 {
275 if (fd_notify > 0)
276 SAFE_CLOSE(fd_notify);
277 }
278
279 static struct tst_test test = {
280 .test = test_fanotify,
281 .tcnt = ARRAY_SIZE(test_cases),
282 .setup = setup,
283 .cleanup = cleanup,
284 .forks_child = 1,
285 .needs_root = 1,
286 .mount_device = 1,
287 .mntpoint = MOUNT_PATH,
288 .tags = (const struct tst_tag[]) {
289 {"linux-git", "a8b98c808eab"},
290 {}
291 }
292 };
293
294 #else
295 TST_TEST_TCONF("system doesn't have required fanotify support");
296 #endif
297