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