• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2022 CTERA Networks. All Rights Reserved.
4  *
5  * Started by Amir Goldstein <amir73il@gmail.com>
6  * based on reproducer from Ivan Delalande <colona@arista.com>
7  */
8 
9 /*\
10  * [Description]
11  * Test opening files after receiving IN_DELETE.
12  *
13  * Kernel v5.13 has a regression allowing files to be open after IN_DELETE.
14  *
15  * The problem has been fixed by commit:
16  *  a37d9a17f099 "fsnotify: invalidate dcache before IN_DELETE event".
17  */
18 
19 #include "config.h"
20 
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <signal.h>
25 #include <sys/wait.h>
26 
27 #include "tst_test.h"
28 #include "tst_safe_macros.h"
29 #include "inotify.h"
30 
31 #if defined(HAVE_SYS_INOTIFY_H)
32 #include <sys/inotify.h>
33 
34 /* Number of files to test */
35 #define CHURN_FILES 9999
36 
37 #define EVENT_MAX 32
38 /* Size of the event structure, not including the name */
39 #define EVENT_SIZE	(sizeof(struct inotify_event))
40 #define EVENT_BUF_LEN	(EVENT_MAX * (EVENT_SIZE + 16))
41 
42 static pid_t pid;
43 
44 static char event_buf[EVENT_BUF_LEN];
45 
churn(void)46 static void churn(void)
47 {
48 	char path[10];
49 	int i;
50 
51 	for (i = 0; i <= CHURN_FILES; ++i) {
52 		snprintf(path, sizeof(path), "%d", i);
53 		SAFE_FILE_PRINTF(path, "1");
54 		SAFE_UNLINK(path);
55 	}
56 }
57 
verify_inotify(void)58 static void verify_inotify(void)
59 {
60 	int nevents = 0, opened = 0;
61 	struct inotify_event *event;
62 	int inotify_fd;
63 
64 	inotify_fd = SAFE_MYINOTIFY_INIT();
65 	SAFE_MYINOTIFY_ADD_WATCH(inotify_fd, ".", IN_DELETE);
66 
67 	pid = SAFE_FORK();
68 	if (pid == 0) {
69 		SAFE_CLOSE(inotify_fd);
70 		churn();
71 		return;
72 	}
73 
74 	while (!opened && nevents < CHURN_FILES) {
75 		int i, fd, len;
76 
77 		len = SAFE_READ(0, inotify_fd, event_buf, EVENT_BUF_LEN);
78 
79 		for (i = 0; i < len; i += EVENT_SIZE + event->len) {
80 			event = (struct inotify_event *)&event_buf[i];
81 
82 			if (!(event->mask & IN_DELETE))
83 				continue;
84 
85 			nevents++;
86 
87 			/* Open file after IN_DELETE should fail */
88 			fd = open(event->name, O_RDONLY);
89 			if (fd < 0)
90 				continue;
91 
92 			tst_res(TFAIL, "File %s opened after IN_DELETE", event->name);
93 			SAFE_CLOSE(fd);
94 			opened = 1;
95 			break;
96 		}
97 	}
98 
99 	SAFE_CLOSE(inotify_fd);
100 
101 	if (!nevents)
102 		tst_res(TFAIL, "Didn't get any IN_DELETE events");
103 	else if (!opened)
104 		tst_res(TPASS, "Got %d IN_DELETE events", nevents);
105 
106 	/* Kill the child creating / deleting files and wait for it */
107 	SAFE_KILL(pid, SIGKILL);
108 	pid = 0;
109 	SAFE_WAIT(NULL);
110 }
111 
cleanup(void)112 static void cleanup(void)
113 {
114 	if (pid) {
115 		SAFE_KILL(pid, SIGKILL);
116 		SAFE_WAIT(NULL);
117 	}
118 }
119 
120 static struct tst_test test = {
121 	.needs_tmpdir = 1,
122 	.forks_child = 1,
123 	.cleanup = cleanup,
124 	.test_all = verify_inotify,
125 	.tags = (const struct tst_tag[]) {
126 		{"linux-git", "a37d9a17f099"},
127 		{}
128 	}
129 };
130 
131 #else
132 	TST_TEST_TCONF("system doesn't have required inotify support");
133 #endif
134