xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fanotify/fanotify07.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) 2017 SUSE.  All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * Started by Jan Kara <[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  * Check that fanotify permission events are handled properly on instance destruction.
11*49cdfc7eSAndroid Build Coastguard Worker  */
12*49cdfc7eSAndroid Build Coastguard Worker 
13*49cdfc7eSAndroid Build Coastguard Worker /*
14*49cdfc7eSAndroid Build Coastguard Worker  * Kernel crashes should be fixed by:
15*49cdfc7eSAndroid Build Coastguard Worker  *  96d41019e3ac "fanotify: fix list corruption in fanotify_get_response()"
16*49cdfc7eSAndroid Build Coastguard Worker  *
17*49cdfc7eSAndroid Build Coastguard Worker  * Kernel hangs should be fixed by:
18*49cdfc7eSAndroid Build Coastguard Worker  *  05f0e38724e8 "fanotify: Release SRCU lock when waiting for userspace response"
19*49cdfc7eSAndroid Build Coastguard Worker  */
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
22*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <sys/syscall.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
35*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SYS_FANOTIFY_H
38*49cdfc7eSAndroid Build Coastguard Worker #include "fanotify.h"
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker #define BUF_SIZE 256
41*49cdfc7eSAndroid Build Coastguard Worker static char fname[BUF_SIZE];
42*49cdfc7eSAndroid Build Coastguard Worker static char buf[BUF_SIZE];
43*49cdfc7eSAndroid Build Coastguard Worker static volatile int fd_notify;
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker /* Number of children we start */
46*49cdfc7eSAndroid Build Coastguard Worker #define MAX_CHILDREN 16
47*49cdfc7eSAndroid Build Coastguard Worker static pid_t child_pid[MAX_CHILDREN];
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker /* Number of children we don't respond to before stopping */
50*49cdfc7eSAndroid Build Coastguard Worker #define MAX_NOT_RESPONDED 4
51*49cdfc7eSAndroid Build Coastguard Worker 
generate_events(void)52*49cdfc7eSAndroid Build Coastguard Worker static void generate_events(void)
53*49cdfc7eSAndroid Build Coastguard Worker {
54*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	/*
57*49cdfc7eSAndroid Build Coastguard Worker 	 * generate sequence of events
58*49cdfc7eSAndroid Build Coastguard Worker 	 */
59*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700);
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 	/* Run until killed... */
62*49cdfc7eSAndroid Build Coastguard Worker 	while (1) {
63*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_LSEEK(fd, 0, SEEK_SET);
64*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_READ(0, fd, buf, BUF_SIZE);
65*49cdfc7eSAndroid Build Coastguard Worker 	}
66*49cdfc7eSAndroid Build Coastguard Worker }
67*49cdfc7eSAndroid Build Coastguard Worker 
run_children(void)68*49cdfc7eSAndroid Build Coastguard Worker static void run_children(void)
69*49cdfc7eSAndroid Build Coastguard Worker {
70*49cdfc7eSAndroid Build Coastguard Worker 	int i;
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < MAX_CHILDREN; i++) {
73*49cdfc7eSAndroid Build Coastguard Worker 		child_pid[i] = SAFE_FORK();
74*49cdfc7eSAndroid Build Coastguard Worker 		if (!child_pid[i]) {
75*49cdfc7eSAndroid Build Coastguard Worker 			/* Child will generate events now */
76*49cdfc7eSAndroid Build Coastguard Worker 			SAFE_CLOSE(fd_notify);
77*49cdfc7eSAndroid Build Coastguard Worker 			generate_events();
78*49cdfc7eSAndroid Build Coastguard Worker 			exit(0);
79*49cdfc7eSAndroid Build Coastguard Worker 		}
80*49cdfc7eSAndroid Build Coastguard Worker 	}
81*49cdfc7eSAndroid Build Coastguard Worker }
82*49cdfc7eSAndroid Build Coastguard Worker 
stop_children(void)83*49cdfc7eSAndroid Build Coastguard Worker static int stop_children(void)
84*49cdfc7eSAndroid Build Coastguard Worker {
85*49cdfc7eSAndroid Build Coastguard Worker 	int child_ret;
86*49cdfc7eSAndroid Build Coastguard Worker 	int i, ret = 0;
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < MAX_CHILDREN; i++) {
89*49cdfc7eSAndroid Build Coastguard Worker 		if (!child_pid[i])
90*49cdfc7eSAndroid Build Coastguard Worker 			continue;
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_KILL(child_pid[i], SIGKILL);
93*49cdfc7eSAndroid Build Coastguard Worker 	}
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < MAX_CHILDREN; i++) {
96*49cdfc7eSAndroid Build Coastguard Worker 		if (!child_pid[i])
97*49cdfc7eSAndroid Build Coastguard Worker 			continue;
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_WAITPID(child_pid[i], &child_ret, 0);
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker 		if (!WIFSIGNALED(child_ret))
102*49cdfc7eSAndroid Build Coastguard Worker 			ret = 1;
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker 		child_pid[i] = 0;
105*49cdfc7eSAndroid Build Coastguard Worker 	}
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 	return ret;
108*49cdfc7eSAndroid Build Coastguard Worker }
109*49cdfc7eSAndroid Build Coastguard Worker 
setup_instance(void)110*49cdfc7eSAndroid Build Coastguard Worker static int setup_instance(void)
111*49cdfc7eSAndroid Build Coastguard Worker {
112*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_FANOTIFY_INIT(FAN_CLASS_CONTENT, O_RDONLY);
115*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FANOTIFY_MARK(fd, FAN_MARK_ADD, FAN_ACCESS_PERM, AT_FDCWD, fname);
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 	return fd;
118*49cdfc7eSAndroid Build Coastguard Worker }
119*49cdfc7eSAndroid Build Coastguard Worker 
loose_fanotify_events(void)120*49cdfc7eSAndroid Build Coastguard Worker static void loose_fanotify_events(void)
121*49cdfc7eSAndroid Build Coastguard Worker {
122*49cdfc7eSAndroid Build Coastguard Worker 	int not_responded = 0;
123*49cdfc7eSAndroid Build Coastguard Worker 
124*49cdfc7eSAndroid Build Coastguard Worker 	/*
125*49cdfc7eSAndroid Build Coastguard Worker 	 * check events
126*49cdfc7eSAndroid Build Coastguard Worker 	 */
127*49cdfc7eSAndroid Build Coastguard Worker 	while (not_responded < MAX_NOT_RESPONDED) {
128*49cdfc7eSAndroid Build Coastguard Worker 		struct fanotify_event_metadata event;
129*49cdfc7eSAndroid Build Coastguard Worker 		struct fanotify_response resp;
130*49cdfc7eSAndroid Build Coastguard Worker 
131*49cdfc7eSAndroid Build Coastguard Worker 		/* Get more events */
132*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_READ(1, fd_notify, &event, sizeof(event));
133*49cdfc7eSAndroid Build Coastguard Worker 
134*49cdfc7eSAndroid Build Coastguard Worker 		if (event.mask != FAN_ACCESS_PERM) {
135*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL,
136*49cdfc7eSAndroid Build Coastguard Worker 				"got event: mask=%llx (expected %llx) "
137*49cdfc7eSAndroid Build Coastguard Worker 				"pid=%u fd=%d",
138*49cdfc7eSAndroid Build Coastguard Worker 				(unsigned long long)event.mask,
139*49cdfc7eSAndroid Build Coastguard Worker 				(unsigned long long)FAN_ACCESS_PERM,
140*49cdfc7eSAndroid Build Coastguard Worker 				(unsigned int)event.pid, event.fd);
141*49cdfc7eSAndroid Build Coastguard Worker 			break;
142*49cdfc7eSAndroid Build Coastguard Worker 		}
143*49cdfc7eSAndroid Build Coastguard Worker 
144*49cdfc7eSAndroid Build Coastguard Worker 		/* We respond to permission event with 95% percent probability. */
145*49cdfc7eSAndroid Build Coastguard Worker 		if (random() % 100 > 5) {
146*49cdfc7eSAndroid Build Coastguard Worker 			/* Write response to permission event */
147*49cdfc7eSAndroid Build Coastguard Worker 			resp.fd = event.fd;
148*49cdfc7eSAndroid Build Coastguard Worker 			resp.response = FAN_ALLOW;
149*49cdfc7eSAndroid Build Coastguard Worker 			SAFE_WRITE(SAFE_WRITE_ALL, fd_notify, &resp, sizeof(resp));
150*49cdfc7eSAndroid Build Coastguard Worker 		} else {
151*49cdfc7eSAndroid Build Coastguard Worker 			not_responded++;
152*49cdfc7eSAndroid Build Coastguard Worker 		}
153*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(event.fd);
154*49cdfc7eSAndroid Build Coastguard Worker 	}
155*49cdfc7eSAndroid Build Coastguard Worker }
156*49cdfc7eSAndroid Build Coastguard Worker 
test_fanotify(void)157*49cdfc7eSAndroid Build Coastguard Worker static void test_fanotify(void)
158*49cdfc7eSAndroid Build Coastguard Worker {
159*49cdfc7eSAndroid Build Coastguard Worker 	int newfd;
160*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
161*49cdfc7eSAndroid Build Coastguard Worker 
162*49cdfc7eSAndroid Build Coastguard Worker 	fd_notify = setup_instance();
163*49cdfc7eSAndroid Build Coastguard Worker 	run_children();
164*49cdfc7eSAndroid Build Coastguard Worker 	loose_fanotify_events();
165*49cdfc7eSAndroid Build Coastguard Worker 
166*49cdfc7eSAndroid Build Coastguard Worker 	/*
167*49cdfc7eSAndroid Build Coastguard Worker 	 * Create and destroy another instance. This may hang if
168*49cdfc7eSAndroid Build Coastguard Worker 	 * unanswered fanotify events block notification subsystem.
169*49cdfc7eSAndroid Build Coastguard Worker 	 */
170*49cdfc7eSAndroid Build Coastguard Worker 	newfd = setup_instance();
171*49cdfc7eSAndroid Build Coastguard Worker 
172*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(newfd);
173*49cdfc7eSAndroid Build Coastguard Worker 
174*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "second instance destroyed successfully");
175*49cdfc7eSAndroid Build Coastguard Worker 
176*49cdfc7eSAndroid Build Coastguard Worker 	/*
177*49cdfc7eSAndroid Build Coastguard Worker 	 * Now destroy the fanotify instance while there are permission
178*49cdfc7eSAndroid Build Coastguard Worker 	 * events at various stages of processing. This may provoke
179*49cdfc7eSAndroid Build Coastguard Worker 	 * kernel hangs or crashes.
180*49cdfc7eSAndroid Build Coastguard Worker 	 */
181*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd_notify);
182*49cdfc7eSAndroid Build Coastguard Worker 
183*49cdfc7eSAndroid Build Coastguard Worker 	ret = stop_children();
184*49cdfc7eSAndroid Build Coastguard Worker 	if (ret)
185*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "child exited for unexpected reason");
186*49cdfc7eSAndroid Build Coastguard Worker 	else
187*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "all children exited successfully");
188*49cdfc7eSAndroid Build Coastguard Worker }
189*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)190*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
191*49cdfc7eSAndroid Build Coastguard Worker {
192*49cdfc7eSAndroid Build Coastguard Worker 	sprintf(fname, "fname_%d", getpid());
193*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_PRINTF(fname, "%s", fname);
194*49cdfc7eSAndroid Build Coastguard Worker 	require_fanotify_access_permissions_supported_on_fs(fname);
195*49cdfc7eSAndroid Build Coastguard Worker }
196*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)197*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
198*49cdfc7eSAndroid Build Coastguard Worker {
199*49cdfc7eSAndroid Build Coastguard Worker 	stop_children();
200*49cdfc7eSAndroid Build Coastguard Worker 
201*49cdfc7eSAndroid Build Coastguard Worker 	if (fd_notify > 0)
202*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd_notify);
203*49cdfc7eSAndroid Build Coastguard Worker }
204*49cdfc7eSAndroid Build Coastguard Worker 
205*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
206*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = test_fanotify,
207*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
208*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
209*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
210*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
211*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
212*49cdfc7eSAndroid Build Coastguard Worker };
213*49cdfc7eSAndroid Build Coastguard Worker 
214*49cdfc7eSAndroid Build Coastguard Worker #else
215*49cdfc7eSAndroid Build Coastguard Worker 	TST_TEST_TCONF("system doesn't have required fanotify support");
216*49cdfc7eSAndroid Build Coastguard Worker #endif
217