1 // SPDX-License-Identifier: GPL-2.0
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 * Validate that the newly introduced FAN_OPEN_EXEC mask functions as expected.
11 * The idea is to generate a sequence of open related actions to ensure that
12 * the correct event flags are being set depending on what event mask was
13 * requested when the object was marked.
14 */
15
16 #define _GNU_SOURCE
17 #include "config.h"
18
19 #include <stdio.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <sys/wait.h>
25 #include "tst_test.h"
26
27 #ifdef HAVE_SYS_FANOTIFY_H
28 #include "fanotify.h"
29
30 #define EVENT_MAX 1024
31 #define EVENT_SIZE (sizeof (struct fanotify_event_metadata))
32 #define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE)
33 #define EVENT_SET_BUF 32
34
35 #define BUF_SIZE 256
36 #define TEST_APP "fanotify_child"
37
38 static pid_t child_pid;
39 static char fname[BUF_SIZE];
40 static volatile int fd_notify;
41 static volatile int complete;
42 static char event_buf[EVENT_BUF_LEN];
43 static int exec_events_unsupported;
44
45 static struct test_case_t {
46 const char *tname;
47 struct fanotify_mark_type mark;
48 unsigned long long mask;
49 unsigned long long ignore_mask;
50 int event_count;
51 unsigned long long event_set[EVENT_SET_BUF];
52 } test_cases[] = {
53 {
54 "inode mark, FAN_OPEN events",
55 INIT_FANOTIFY_MARK_TYPE(INODE),
56 FAN_OPEN,
57 0,
58 2,
59 {FAN_OPEN, FAN_OPEN}
60 },
61 {
62 "inode mark, FAN_OPEN_EXEC events",
63 INIT_FANOTIFY_MARK_TYPE(INODE),
64 FAN_OPEN_EXEC,
65 0,
66 1,
67 {FAN_OPEN_EXEC}
68 },
69 {
70 "inode mark, FAN_OPEN | FAN_OPEN_EXEC events",
71 INIT_FANOTIFY_MARK_TYPE(INODE),
72 FAN_OPEN | FAN_OPEN_EXEC,
73 0,
74 2,
75 {FAN_OPEN, FAN_OPEN | FAN_OPEN_EXEC}
76 },
77 {
78 "inode mark, FAN_OPEN events, ignore FAN_OPEN_EXEC",
79 INIT_FANOTIFY_MARK_TYPE(INODE),
80 FAN_OPEN,
81 FAN_OPEN_EXEC,
82 2,
83 {FAN_OPEN, FAN_OPEN}
84 },
85 {
86 "inode mark, FAN_OPEN_EXEC events, ignore FAN_OPEN",
87 INIT_FANOTIFY_MARK_TYPE(INODE),
88 FAN_OPEN_EXEC,
89 FAN_OPEN,
90 1,
91 {FAN_OPEN_EXEC}
92 },
93 {
94 "inode mark, FAN_OPEN | FAN_OPEN_EXEC events, ignore "
95 "FAN_OPEN_EXEC",
96 INIT_FANOTIFY_MARK_TYPE(INODE),
97 FAN_OPEN | FAN_OPEN_EXEC,
98 FAN_OPEN_EXEC,
99 2,
100 {FAN_OPEN, FAN_OPEN}
101 }
102 };
103
generate_events(void)104 static int generate_events(void)
105 {
106 int fd, status;
107
108 child_pid = SAFE_FORK();
109
110 /*
111 * Generate a sequence of events
112 */
113 if (child_pid == 0) {
114 SAFE_CLOSE(fd_notify);
115
116 fd = SAFE_OPEN(fname, O_RDONLY);
117
118 if (fd > 0)
119 SAFE_CLOSE(fd);
120
121 SAFE_EXECL(TEST_APP, TEST_APP, NULL);
122 exit(1);
123 }
124
125 SAFE_WAITPID(child_pid, &status, 0);
126
127 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
128 return 1;
129 return 0;
130 }
131
setup_mark(unsigned int n)132 static int setup_mark(unsigned int n)
133 {
134 unsigned int i = 0;
135 struct test_case_t *tc = &test_cases[n];
136 struct fanotify_mark_type *mark = &tc->mark;
137 const char *const files[] = {fname, TEST_APP};
138
139 tst_res(TINFO, "Test #%d: %s", n, tc->tname);
140
141 if (exec_events_unsupported && ((tc->mask & FAN_OPEN_EXEC) ||
142 tc->ignore_mask & FAN_OPEN_EXEC)) {
143 tst_res(TCONF, "FAN_OPEN_EXEC not supported in kernel?");
144 return -1;
145 }
146
147 fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF, O_RDONLY);
148
149 for (; i < ARRAY_SIZE(files); i++) {
150 /* Setup normal mark on object */
151 SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD | mark->flag,
152 tc->mask, AT_FDCWD, files[i]);
153
154 /* Setup ignore mark on object */
155 if (tc->ignore_mask) {
156 SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD | mark->flag
157 | FAN_MARK_IGNORED_MASK,
158 tc->ignore_mask, AT_FDCWD,
159 files[i]);
160 }
161 }
162
163 return 0;
164 }
165
do_test(unsigned int n)166 static void do_test(unsigned int n)
167 {
168 int len = 0, event_num = 0;
169 struct test_case_t *tc = &test_cases[n];
170 struct fanotify_event_metadata *event;
171
172 /* Place a mark on the object */
173 if (setup_mark(n) != 0)
174 goto cleanup;
175
176 /* Generate events in child process */
177 if (!generate_events())
178 goto cleanup;
179
180 /* Read available events into buffer */
181 len = SAFE_READ(0, fd_notify, event_buf, EVENT_BUF_LEN);
182
183 event = (struct fanotify_event_metadata *) event_buf;
184
185 /* Process events */
186 while (FAN_EVENT_OK(event, len) && event_num < tc->event_count) {
187 if (event->mask != *(tc->event_set + event_num)) {
188 tst_res(TFAIL,
189 "Received event: mask=%llx (expected %llx, "
190 "pid=%u, fd=%d",
191 (unsigned long long) event->mask,
192 *(tc->event_set + event_num),
193 (unsigned) event->pid,
194 event->fd);
195 } else if (event->pid != child_pid) {
196 tst_res(TFAIL,
197 "Received event: mask=%llx, pid=%u (expected "
198 "%u), fd=%d",
199 (unsigned long long) event->mask,
200 (unsigned) event->pid,
201 (unsigned) child_pid,
202 event->fd);
203 } else {
204 tst_res(TPASS,
205 "Received event: mask=%llx, pid=%u, fd=%d",
206 (unsigned long long) event->mask,
207 (unsigned) event->pid,
208 event->fd);
209 }
210
211 if (event->fd != FAN_NOFD)
212 SAFE_CLOSE(event->fd);
213
214 event_num++;
215 event = FAN_EVENT_NEXT(event, len);
216 }
217
218 cleanup:
219 if (fd_notify > 0)
220 SAFE_CLOSE(fd_notify);
221 }
222
do_setup(void)223 static void do_setup(void)
224 {
225 exec_events_unsupported = fanotify_events_supported_by_kernel(FAN_OPEN_EXEC,
226 FAN_CLASS_NOTIF, 0);
227
228 sprintf(fname, "fname_%d", getpid());
229 SAFE_FILE_PRINTF(fname, "1");
230 }
231
do_cleanup(void)232 static void do_cleanup(void)
233 {
234 if (fd_notify > 0)
235 SAFE_CLOSE(fd_notify);
236 }
237
238 static const char *const resource_files[] = {
239 TEST_APP,
240 NULL
241 };
242
243 static struct tst_test test = {
244 .setup = do_setup,
245 .test = do_test,
246 .tcnt = ARRAY_SIZE(test_cases),
247 .cleanup = do_cleanup,
248 .forks_child = 1,
249 .needs_root = 1,
250 .resource_files = resource_files
251 };
252 #else
253 TST_TEST_TCONF("System does not contain required fanotify support");
254 #endif
255