aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/syscalls/fanotify/fanotify12.c
blob: 7f8e97b178bc16c30a8cef8dfded32580e38e936 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2018 Matthew Bobrowski. All Rights Reserved.
 *
 * Started by Matthew Bobrowski <mbobrowski@mbobrowski.org>
 */

/*\
 * [Description]
 * Validate that the newly introduced FAN_OPEN_EXEC mask functions as expected.
 * The idea is to generate a sequence of open related actions to ensure that
 * the correct event flags are being set depending on what event mask was
 * requested when the object was marked.
 */

#define _GNU_SOURCE
#include "config.h"

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "tst_test.h"

#ifdef HAVE_SYS_FANOTIFY_H
#include "fanotify.h"

#define EVENT_MAX 1024
#define EVENT_SIZE (sizeof(struct fanotify_event_metadata))
#define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE)
#define EVENT_SET_BUF 32

#define BUF_SIZE 256
#define TEST_APP "fanotify_child"

static pid_t child_pid;
static char fname[BUF_SIZE];
static volatile int fd_notify;
static volatile int complete;
static char event_buf[EVENT_BUF_LEN];
static int exec_events_unsupported;

static struct test_case_t {
	const char *tname;
	struct fanotify_mark_type mark;
	unsigned long long mask;
	unsigned long long ignore_mask;
	int event_count;
	unsigned long long event_set[EVENT_SET_BUF];
} test_cases[] = {
	{
		"inode mark, FAN_OPEN events",
		INIT_FANOTIFY_MARK_TYPE(INODE),
		FAN_OPEN,
		0,
		2,
		{FAN_OPEN, FAN_OPEN}
	},
	{
		"inode mark, FAN_OPEN_EXEC events",
		INIT_FANOTIFY_MARK_TYPE(INODE),
		FAN_OPEN_EXEC,
		0,
		1,
		{FAN_OPEN_EXEC}
	},
	{
		"inode mark, FAN_OPEN | FAN_OPEN_EXEC events",
		INIT_FANOTIFY_MARK_TYPE(INODE),
		FAN_OPEN | FAN_OPEN_EXEC,
		0,
		2,
		{FAN_OPEN, FAN_OPEN | FAN_OPEN_EXEC}
	},
	{
		"inode mark, FAN_OPEN events, ignore FAN_OPEN_EXEC",
		INIT_FANOTIFY_MARK_TYPE(INODE),
		FAN_OPEN,
		FAN_OPEN_EXEC,
		2,
		{FAN_OPEN, FAN_OPEN}
	},
	{
		"inode mark, FAN_OPEN_EXEC events, ignore FAN_OPEN",
		INIT_FANOTIFY_MARK_TYPE(INODE),
		FAN_OPEN_EXEC,
		FAN_OPEN,
		1,
		{FAN_OPEN_EXEC}
	},
	{
		"inode mark, FAN_OPEN | FAN_OPEN_EXEC events, ignore "
		"FAN_OPEN_EXEC",
		INIT_FANOTIFY_MARK_TYPE(INODE),
		FAN_OPEN | FAN_OPEN_EXEC,
		FAN_OPEN_EXEC,
		2,
		{FAN_OPEN, FAN_OPEN}
	}
};

static int generate_events(void)
{
	int fd, status;

	child_pid = SAFE_FORK();

	/*
	 * Generate a sequence of events
	 */
	if (child_pid == 0) {
		SAFE_CLOSE(fd_notify);

		fd = SAFE_OPEN(fname, O_RDONLY);

		if (fd > 0)
			SAFE_CLOSE(fd);

		SAFE_EXECL(TEST_APP, TEST_APP, NULL);
		exit(1);
	}

	SAFE_WAITPID(child_pid, &status, 0);

	if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
		return 1;
	return 0;
}

static int setup_mark(unsigned int n)
{
	unsigned int i = 0;
	struct test_case_t *tc = &test_cases[n];
	struct fanotify_mark_type *mark = &tc->mark;
	const char *const files[] = {fname, TEST_APP};

	tst_res(TINFO, "Test #%d: %s", n, tc->tname);

	if (exec_events_unsupported && ((tc->mask & FAN_OPEN_EXEC) ||
					tc->ignore_mask & FAN_OPEN_EXEC)) {
		tst_res(TCONF, "FAN_OPEN_EXEC not supported in kernel?");
		return -1;
	}

	fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF, O_RDONLY);

	for (; i < ARRAY_SIZE(files); i++) {
		/* Setup normal mark on object */
		SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD | mark->flag,
					tc->mask, AT_FDCWD, files[i]);

		/* Setup ignore mark on object */
		if (tc->ignore_mask) {
			SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD | mark->flag
						| FAN_MARK_IGNORED_MASK,
						tc->ignore_mask, AT_FDCWD,
						files[i]);
		}
	}

	return 0;
}

static void do_test(unsigned int n)
{
	int len = 0, event_num = 0;
	struct test_case_t *tc = &test_cases[n];
	struct fanotify_event_metadata *event;

	/* Place a mark on the object */
	if (setup_mark(n) != 0)
		goto cleanup;

	/* Generate events in child process */
	if (!generate_events())
		goto cleanup;

	/* Read available events into buffer */
	len = SAFE_READ(0, fd_notify, event_buf, EVENT_BUF_LEN);

	event = (struct fanotify_event_metadata *) event_buf;

	/* Process events */
	while (FAN_EVENT_OK(event, len) && event_num < tc->event_count) {
		if (event->mask != *(tc->event_set + event_num)) {
			tst_res(TFAIL,
				"Received event: mask=%llx (expected %llx, "
				"pid=%u, fd=%d",
				(unsigned long long) event->mask,
				*(tc->event_set + event_num),
				(unsigned int) event->pid,
				event->fd);
		} else if (event->pid != child_pid) {
			tst_res(TFAIL,
				"Received event: mask=%llx, pid=%u (expected "
				"%u), fd=%d",
				(unsigned long long) event->mask,
				(unsigned int) event->pid,
				(unsigned int) child_pid,
				event->fd);
		} else {
			tst_res(TPASS,
				"Received event: mask=%llx, pid=%u, fd=%d",
				(unsigned long long) event->mask,
				(unsigned int) event->pid,
				event->fd);
		}

		if (event->fd != FAN_NOFD)
			SAFE_CLOSE(event->fd);

		event_num++;
		event = FAN_EVENT_NEXT(event, len);
	}

cleanup:
	if (fd_notify > 0)
		SAFE_CLOSE(fd_notify);
}

static void do_setup(void)
{
	exec_events_unsupported = fanotify_events_supported_by_kernel(FAN_OPEN_EXEC,
								      FAN_CLASS_NOTIF, 0);

	sprintf(fname, "fname_%d", getpid());
	SAFE_FILE_PRINTF(fname, "1");
}

static void do_cleanup(void)
{
	if (fd_notify > 0)
		SAFE_CLOSE(fd_notify);
}

static struct tst_test test = {
	.setup = do_setup,
	.test = do_test,
	.tcnt = ARRAY_SIZE(test_cases),
	.cleanup = do_cleanup,
	.forks_child = 1,
	.needs_root = 1,
	.resource_files = (const char *const []) {
		TEST_APP,
		NULL
	}
};
#else
	TST_TEST_TCONF("System does not contain required fanotify support");
#endif