aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/syscalls/fanotify/fanotify22.c
blob: a654e891235be8e3b9222e28c5fd5272b1283cc3 (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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2021 Collabora Ltd.
 *
 * Author: Gabriel Krisman Bertazi <gabriel@krisman.be>
 * Based on previous work by Amir Goldstein <amir73il@gmail.com>
 */

/*\
 * [Description]
 * Check fanotify FAN_ERROR_FS events triggered by intentionally
 * corrupted filesystems:
 *
 * - Generate a broken filesystem
 * - Start FAN_FS_ERROR monitoring group
 * - Make the file system notice the error through ordinary operations
 * - Observe the event generated
 */

#define _GNU_SOURCE
#include "config.h"

#include <stdio.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/syscall.h>
#include "tst_test.h"
#include <sys/types.h>

#ifdef HAVE_SYS_FANOTIFY_H
#include "fanotify.h"

#ifndef EFSCORRUPTED
#define EFSCORRUPTED    EUCLEAN         /* Filesystem is corrupted */
#endif

#define BUF_SIZE 256

#define MOUNT_PATH "test_mnt"
#define BASE_DIR "internal_dir"
#define BAD_DIR BASE_DIR"/bad_dir"
#define BAD_LINK BASE_DIR"/bad_link"

#ifdef HAVE_NAME_TO_HANDLE_AT

static char event_buf[BUF_SIZE];
static int fd_notify;

/* These expected FIDs are common to multiple tests */
static struct fanotify_fid_t null_fid;
static struct fanotify_fid_t bad_file_fid;
static struct fanotify_fid_t bad_link_fid;

static void trigger_fs_abort(void)
{
	SAFE_MOUNT(tst_device->dev, MOUNT_PATH, tst_device->fs_type,
		   MS_REMOUNT|MS_RDONLY, "abort");
}

static void do_debugfs_request(const char *dev, char *request)
{
	const char *const cmd[] = {"debugfs", "-w", dev, "-R", request, NULL};

	SAFE_CMD(cmd, NULL, NULL);
}

static void trigger_bad_file_lookup(void)
{
	int ret;

	/* SAFE_OPEN cannot be used here because we expect it to fail. */
	ret = open(MOUNT_PATH"/"BAD_DIR, O_RDONLY, 0);
	if (ret != -1 && errno != EUCLEAN)
		tst_res(TFAIL, "Unexpected lookup result(%d) of %s (%d!=%d)",
			ret, BAD_DIR, errno, EUCLEAN);
}

static void trigger_bad_link_lookup(void)
{
	int ret;

	/* SAFE_OPEN cannot be used here because we expect it to fail. */
	ret = open(MOUNT_PATH"/"BAD_LINK, O_RDONLY, 0);
	if (ret != -1 && errno != EUCLEAN)
		tst_res(TFAIL, "Unexpected open result(%d) of %s (%d!=%d)",
			ret, BAD_LINK, errno, EUCLEAN);
}


static void tcase3_trigger(void)
{
	trigger_bad_link_lookup();
	trigger_bad_file_lookup();
}

static void tcase4_trigger(void)
{
	trigger_bad_file_lookup();
	trigger_fs_abort();
}

static struct test_case {
	char *name;
	int error;
	unsigned int error_count;
	struct fanotify_fid_t *fid;
	void (*trigger_error)(void);
} testcases[] = {
	{
		.name = "Trigger abort",
		.trigger_error = &trigger_fs_abort,
		.error_count = 1,
		.error = ESHUTDOWN,
		.fid = &null_fid,
	},
	{
		.name = "Lookup of inode with invalid mode",
		.trigger_error = &trigger_bad_file_lookup,
		.error_count = 1,
		.error = EFSCORRUPTED,
		.fid = &bad_file_fid,
	},
	{
		.name = "Multiple error submission",
		.trigger_error = &tcase3_trigger,
		.error_count = 2,
		.error = EFSCORRUPTED,
		.fid = &bad_link_fid,
	},
	{
		.name = "Multiple error submission 2",
		.trigger_error = &tcase4_trigger,
		.error_count = 2,
		.error = EFSCORRUPTED,
		.fid = &bad_file_fid,
	}
};

static int check_error_event_info_fid(struct fanotify_event_info_fid *fid,
				 const struct test_case *ex)
{
	struct file_handle *fh = (struct file_handle *) &fid->handle;

	if (memcmp(&fid->fsid, &ex->fid->fsid, sizeof(fid->fsid))) {
		tst_res(TFAIL, "%s: Received bad FSID type (%x...!=%x...)",
			ex->name, FSID_VAL_MEMBER(fid->fsid, 0),
			ex->fid->fsid.val[0]);

		return 1;
	}
	if (fh->handle_type != ex->fid->handle.handle_type) {
		tst_res(TFAIL, "%s: Received bad file_handle type (%d!=%d)",
			ex->name, fh->handle_type, ex->fid->handle.handle_type);
		return 1;
	}

	if (fh->handle_bytes != ex->fid->handle.handle_bytes) {
		tst_res(TFAIL, "%s: Received bad file_handle len (%d!=%d)",
			ex->name, fh->handle_bytes, ex->fid->handle.handle_bytes);
		return 1;
	}

	if (memcmp(fh->f_handle, ex->fid->handle.f_handle, fh->handle_bytes)) {
		tst_res(TFAIL, "%s: Received wrong handle. "
			"Expected (%x...) got (%x...) ", ex->name,
			*(int *)ex->fid->handle.f_handle, *(int *)fh->f_handle);
		return 1;
	}
	return 0;
}

static int check_error_event_info_error(struct fanotify_event_info_error *info_error,
				 const struct test_case *ex)
{
	int fail = 0;

	if (info_error->error_count != ex->error_count) {
		tst_res(TFAIL, "%s: Unexpected error_count (%d!=%d)",
			ex->name, info_error->error_count, ex->error_count);
		fail++;
	}

	if (info_error->error != ex->error) {
		tst_res(TFAIL, "%s: Unexpected error code value (%d!=%d)",
			ex->name, info_error->error, ex->error);
		fail++;
	}

	return fail;
}

static int check_error_event_metadata(struct fanotify_event_metadata *event)
{
	int fail = 0;

	if (event->mask != FAN_FS_ERROR) {
		fail++;
		tst_res(TFAIL, "got unexpected event %llx",
			(unsigned long long)event->mask);
	}

	if (event->fd != FAN_NOFD) {
		fail++;
		tst_res(TFAIL, "Weird FAN_FD %llx",
			(unsigned long long)event->mask);
	}
	return fail;
}

static void check_event(char *buf, size_t len, const struct test_case *ex)
{
	struct fanotify_event_metadata *event =
		(struct fanotify_event_metadata *) buf;
	struct fanotify_event_info_error *info_error;
	struct fanotify_event_info_fid *info_fid;
	int fail = 0;

	if (len < FAN_EVENT_METADATA_LEN) {
		tst_res(TFAIL, "No event metadata found");
		return;
	}

	if (check_error_event_metadata(event))
		return;

	info_error = get_event_info_error(event);
	if (info_error)
		fail += check_error_event_info_error(info_error, ex);
	else {
		tst_res(TFAIL, "Generic error record not found");
		fail++;
	}

	info_fid = get_event_info_fid(event);
	if (info_fid)
		fail += check_error_event_info_fid(info_fid, ex);
	else {
		tst_res(TFAIL, "FID record not found");
		fail++;
	}

	if (!fail)
		tst_res(TPASS, "Successfully received: %s", ex->name);
}

static void do_test(unsigned int i)
{
	const struct test_case *tcase = &testcases[i];
	size_t read_len;

	SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD|FAN_MARK_FILESYSTEM,
			   FAN_FS_ERROR, AT_FDCWD, MOUNT_PATH);

	tcase->trigger_error();

	read_len = SAFE_READ(0, fd_notify, event_buf, BUF_SIZE);

	SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_REMOVE|FAN_MARK_FILESYSTEM,
			   FAN_FS_ERROR, AT_FDCWD, MOUNT_PATH);

	check_event(event_buf, read_len, tcase);
	/* Unmount and mount the filesystem to get it out of the error state */
	SAFE_UMOUNT(MOUNT_PATH);
	SAFE_MOUNT(tst_device->dev, MOUNT_PATH, tst_device->fs_type, 0, NULL);
}

static void pre_corrupt_fs(void)
{
	SAFE_MKDIR(MOUNT_PATH"/"BASE_DIR, 0777);
	SAFE_MKDIR(MOUNT_PATH"/"BAD_DIR, 0777);

	fanotify_save_fid(MOUNT_PATH"/"BAD_DIR, &bad_file_fid);
	fanotify_save_fid(MOUNT_PATH"/"BASE_DIR, &bad_link_fid);

	SAFE_UMOUNT(MOUNT_PATH);
	do_debugfs_request(tst_device->dev, "sif " BAD_DIR " mode 0xff");
	do_debugfs_request(tst_device->dev, "ln <1> " BAD_LINK);
	SAFE_MOUNT(tst_device->dev, MOUNT_PATH, tst_device->fs_type, 0, NULL);
}

static void init_null_fid(void)
{
	/* Use fanotify_save_fid to fill the fsid and overwrite the
	 * file_handler to create a null_fid
	 */
	fanotify_save_fid(MOUNT_PATH, &null_fid);

	null_fid.handle.handle_type = FILEID_INVALID;
	null_fid.handle.handle_bytes = 0;
}

static void setup(void)
{
	REQUIRE_FANOTIFY_EVENTS_SUPPORTED_ON_FS(FAN_CLASS_NOTIF|FAN_REPORT_FID,
						FAN_MARK_FILESYSTEM,
						FAN_FS_ERROR, ".");
	pre_corrupt_fs();

	fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF|FAN_REPORT_FID,
				       O_RDONLY);

	init_null_fid();
}

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

static struct tst_test test = {
	.test = do_test,
	.tcnt = ARRAY_SIZE(testcases),
	.setup = setup,
	.cleanup = cleanup,
	.mount_device = 1,
	.mntpoint = MOUNT_PATH,
	.needs_root = 1,
	.dev_fs_type = "ext4",
	.tags = (const struct tst_tag[]) {
		{"linux-git", "124e7c61deb2"},
		{}
	},
	.needs_cmds = (const char *[]) {
		"debugfs",
		NULL
	}
};

#else
	TST_TEST_TCONF("system does not have required name_to_handle_at() support");
#endif
#else
	TST_TEST_TCONF("system doesn't have required fanotify support");
#endif