• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2014 SUSE.  All Rights Reserved.
4  *
5  * Started by Jan Kara <jack@suse.cz>
6  *
7  * DESCRIPTION
8  *     Check that fanotify properly merges ignore mask of an inode and
9  *     mountpoint.
10  *
11  * This is a regression test for:
12  *
13  *  commit 8edc6e1688fc8f02c8c1f53a2ec4928cb1055f4d
14  *  Author: Jan Kara <jack@suse.cz>
15  *  Date:   Thu Nov 13 15:19:33 2014 -0800
16  *
17  *      fanotify: fix notification of groups with inode & mount marks
18  */
19 #define _GNU_SOURCE
20 #include "config.h"
21 
22 #include <stdio.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <sys/mount.h>
29 #include <sys/syscall.h>
30 #include "tst_test.h"
31 #include "fanotify.h"
32 
33 #if defined(HAVE_SYS_FANOTIFY_H)
34 #include <sys/fanotify.h>
35 
36 #define EVENT_MAX 1024
37 /* size of the event structure, not counting name */
38 #define EVENT_SIZE  (sizeof (struct fanotify_event_metadata))
39 /* reasonable guess as to size of 1024 events */
40 #define EVENT_BUF_LEN        (EVENT_MAX * EVENT_SIZE)
41 
42 unsigned int fanotify_prio[] = {
43 	FAN_CLASS_PRE_CONTENT,
44 	FAN_CLASS_CONTENT,
45 	FAN_CLASS_NOTIF
46 };
47 #define FANOTIFY_PRIORITIES ARRAY_SIZE(fanotify_prio)
48 
49 #define GROUPS_PER_PRIO 3
50 
51 #define BUF_SIZE 256
52 static char fname[BUF_SIZE];
53 static int fd_notify[FANOTIFY_PRIORITIES][GROUPS_PER_PRIO];
54 
55 static char event_buf[EVENT_BUF_LEN];
56 
57 #define MOUNT_NAME "mntpoint"
58 static int mount_created;
59 
create_fanotify_groups(void)60 static void create_fanotify_groups(void)
61 {
62 	unsigned int p, i;
63 	int ret;
64 
65 	for (p = 0; p < FANOTIFY_PRIORITIES; p++) {
66 		for (i = 0; i < GROUPS_PER_PRIO; i++) {
67 			fd_notify[p][i] = SAFE_FANOTIFY_INIT(fanotify_prio[p] |
68 							     FAN_NONBLOCK,
69 							     O_RDONLY);
70 
71 			/* Add mount mark for each group */
72 			ret = fanotify_mark(fd_notify[p][i],
73 					    FAN_MARK_ADD | FAN_MARK_MOUNT,
74 					    FAN_MODIFY,
75 					    AT_FDCWD, ".");
76 			if (ret < 0) {
77 				tst_brk(TBROK | TERRNO,
78 					"fanotify_mark(%d, FAN_MARK_ADD | "
79 					"FAN_MARK_MOUNT, FAN_MODIFY, AT_FDCWD,"
80 					" '.') failed", fd_notify[p][i]);
81 			}
82 			/* Add ignore mark for groups with higher priority */
83 			if (p == 0)
84 				continue;
85 			ret = fanotify_mark(fd_notify[p][i],
86 					    FAN_MARK_ADD |
87 					    FAN_MARK_IGNORED_MASK |
88 					    FAN_MARK_IGNORED_SURV_MODIFY,
89 					    FAN_MODIFY, AT_FDCWD, fname);
90 			if (ret < 0) {
91 				tst_brk(TBROK | TERRNO,
92 					"fanotify_mark(%d, FAN_MARK_ADD | "
93 					"FAN_MARK_IGNORED_MASK | "
94 					"FAN_MARK_IGNORED_SURV_MODIFY, "
95 					"FAN_MODIFY, AT_FDCWD, %s) failed",
96 					fd_notify[p][i], fname);
97 			}
98 		}
99 	}
100 }
101 
cleanup_fanotify_groups(void)102 static void cleanup_fanotify_groups(void)
103 {
104 	unsigned int i, p;
105 
106 	for (p = 0; p < FANOTIFY_PRIORITIES; p++) {
107 		for (i = 0; i < GROUPS_PER_PRIO; i++) {
108 			if (fd_notify[p][i] > 0)
109 				SAFE_CLOSE(fd_notify[p][i]);
110 		}
111 	}
112 }
113 
verify_event(int group,struct fanotify_event_metadata * event)114 static void verify_event(int group, struct fanotify_event_metadata *event)
115 {
116 	if (event->mask != FAN_MODIFY) {
117 		tst_res(TFAIL, "group %d got event: mask %llx (expected %llx) "
118 			"pid=%u fd=%d", group, (unsigned long long)event->mask,
119 			(unsigned long long)FAN_MODIFY,
120 			(unsigned)event->pid, event->fd);
121 	} else if (event->pid != getpid()) {
122 		tst_res(TFAIL, "group %d got event: mask %llx pid=%u "
123 			"(expected %u) fd=%d", group,
124 			(unsigned long long)event->mask, (unsigned)event->pid,
125 			(unsigned)getpid(), event->fd);
126 	} else {
127 		tst_res(TPASS, "group %d got event: mask %llx pid=%u fd=%d",
128 			group, (unsigned long long)event->mask,
129 			(unsigned)event->pid, event->fd);
130 	}
131 }
132 
test01(void)133 void test01(void)
134 {
135 	int ret;
136 	unsigned int p, i;
137 	struct fanotify_event_metadata *event;
138 
139 	create_fanotify_groups();
140 
141 	/*
142 	 * generate sequence of events
143 	 */
144 	SAFE_FILE_PRINTF(fname, "1");
145 
146 	/* First verify all groups without ignore mask got the event */
147 	for (i = 0; i < GROUPS_PER_PRIO; i++) {
148 		ret = read(fd_notify[0][i], event_buf, EVENT_BUF_LEN);
149 		if (ret < 0) {
150 			if (errno == EAGAIN) {
151 				tst_res(TFAIL, "group %d did not get "
152 					"event", i);
153 			}
154 			tst_brk(TBROK | TERRNO,
155 				"reading fanotify events failed");
156 		}
157 		if (ret < (int)FAN_EVENT_METADATA_LEN) {
158 			tst_brk(TBROK,
159 				"short read when reading fanotify "
160 				"events (%d < %d)", ret,
161 				(int)EVENT_BUF_LEN);
162 		}
163 		event = (struct fanotify_event_metadata *)event_buf;
164 		if (ret > (int)event->event_len) {
165 			tst_res(TFAIL, "group %d got more than one "
166 				"event (%d > %d)", i, ret,
167 				event->event_len);
168 		} else {
169 			verify_event(i, event);
170 		}
171 		if (event->fd != FAN_NOFD)
172 			SAFE_CLOSE(event->fd);
173 	}
174 	for (p = 1; p < FANOTIFY_PRIORITIES; p++) {
175 		for (i = 0; i < GROUPS_PER_PRIO; i++) {
176 			ret = read(fd_notify[p][i], event_buf, EVENT_BUF_LEN);
177 			if (ret > 0) {
178 				tst_res(TFAIL, "group %d got event",
179 					p*GROUPS_PER_PRIO + i);
180 				if (event->fd != FAN_NOFD)
181 					SAFE_CLOSE(event->fd);
182 			} else if (ret == 0) {
183 				tst_brk(TBROK, "zero length "
184 					"read from fanotify fd");
185 			} else if (errno != EAGAIN) {
186 				tst_brk(TBROK | TERRNO,
187 					"reading fanotify events failed");
188 			} else {
189 				tst_res(TPASS, "group %d got no event",
190 					p*GROUPS_PER_PRIO + i);
191 			}
192 		}
193 	}
194 	cleanup_fanotify_groups();
195 }
196 
setup(void)197 static void setup(void)
198 {
199 	SAFE_MKDIR(MOUNT_NAME, 0755);
200 	SAFE_MOUNT(MOUNT_NAME, MOUNT_NAME, "none", MS_BIND, NULL);
201 	mount_created = 1;
202 	SAFE_CHDIR(MOUNT_NAME);
203 
204 	sprintf(fname, "tfile_%d", getpid());
205 	SAFE_FILE_PRINTF(fname, "1");
206 }
207 
cleanup(void)208 static void cleanup(void)
209 {
210 	cleanup_fanotify_groups();
211 
212 	SAFE_CHDIR("../");
213 
214 	if (mount_created && tst_umount(MOUNT_NAME) < 0)
215 		tst_brk(TBROK | TERRNO, "umount failed");
216 }
217 
218 static struct tst_test test = {
219 	.test_all = test01,
220 	.setup = setup,
221 	.cleanup = cleanup,
222 	.needs_tmpdir = 1,
223 	.needs_root = 1
224 };
225 
226 #else
227 	TST_TEST_TCONF("system doesn't have required fanotify support");
228 #endif
229