summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/landlock/trace_fs_test.c
blob: 5220f6a4bee17730dc0247dcb219259c1c381886 (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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// SPDX-License-Identifier: GPL-2.0
/*
 * Landlock tests - Filesystem tracepoints
 *
 * Copyright © 2026 Cloudflare, Inc.
 */

#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <linux/landlock.h>
#include <sched.h>
#include <stdio.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include "common.h"
#include "trace.h"

#define TRACE_TASK "trace_fs_test"

/*
 * Like REGEX_DENY_ACCESS_FS(), but pins the logged field to a specific value
 * ("0" or "1") so a test can tell a suppressed (quiet) denial from a logged
 * one.  The tracepoint fires for every denial; logged carries the audit
 * verdict.
 */
#define REGEX_DENY_ACCESS_FS_LOGGED(task, log) \
	TRACE_PREFIX(task)                     \
	"landlock_deny_access_fs: "            \
	"domain=[0-9a-f]\\+ "                  \
	"same_exec=[01] "                      \
	"logged=" log " "                      \
	"blockers=[a-z_|]* "                   \
	"dev=[0-9]\\+:[0-9]\\+ "               \
	"ino=[0-9]\\+ "                        \
	"path=[^ ]*$"

/* clang-format off */
FIXTURE(trace_fs) {
	/* clang-format on */
	int tracefs_ok;
};

FIXTURE_SETUP(trace_fs)
{
	int ret;

	set_cap(_metadata, CAP_SYS_ADMIN);
	ASSERT_EQ(0, unshare(CLONE_NEWNS));
	ASSERT_EQ(0, mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL));

	ret = tracefs_fixture_setup();
	if (ret) {
		clear_cap(_metadata, CAP_SYS_ADMIN);
		self->tracefs_ok = 0;
		SKIP(return, "tracefs not available");
	}
	self->tracefs_ok = 1;

	ASSERT_EQ(0, tracefs_enable_event(TRACEFS_ADD_RULE_FS_ENABLE, true));
	ASSERT_EQ(0, tracefs_enable_event(TRACEFS_CHECK_RULE_FS_ENABLE, true));
	ASSERT_EQ(0, tracefs_enable_event(TRACEFS_DENY_ACCESS_FS_ENABLE, true));
	ASSERT_EQ(0, tracefs_clear());
	clear_cap(_metadata, CAP_SYS_ADMIN);
}

FIXTURE_TEARDOWN(trace_fs)
{
	if (!self->tracefs_ok)
		return;

	set_cap(_metadata, CAP_SYS_ADMIN);
	tracefs_enable_event(TRACEFS_ADD_RULE_FS_ENABLE, false);
	tracefs_enable_event(TRACEFS_CHECK_RULE_FS_ENABLE, false);
	tracefs_enable_event(TRACEFS_DENY_ACCESS_FS_ENABLE, false);
	tracefs_fixture_teardown();
	clear_cap(_metadata, CAP_SYS_ADMIN);
}

/*
 * Baseline: verifies that without Landlock, the operation succeeds and no
 * check_rule or deny_access trace events fire.
 */
TEST_F(trace_fs, unsandboxed)
{
	char *buf;
	int count, status, fd;
	pid_t pid;

	ASSERT_EQ(0, tracefs_clear_buf());

	pid = fork();
	ASSERT_LE(0, pid);

	if (pid == 0) {
		/*
		 * No sandbox: verify that a normal FS access does not produce
		 * Landlock trace events.
		 */
		fd = open("/usr", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
		if (fd >= 0)
			close(fd);
		_exit(0);
	}

	ASSERT_EQ(pid, waitpid(pid, &status, 0));
	ASSERT_TRUE(WIFEXITED(status));
	EXPECT_EQ(0, WEXITSTATUS(status));

	buf = tracefs_read_buf();
	ASSERT_NE(NULL, buf);

	count = tracefs_count_matches(buf, REGEX_CHECK_RULE_FS(TRACE_TASK));
	EXPECT_EQ(0, count);
	count = tracefs_count_matches(buf, REGEX_DENY_ACCESS_FS(TRACE_TASK));
	EXPECT_EQ(0, count);

	free(buf);
}

/*
 * Verifies that adding a filesystem rule emits a landlock_add_rule_fs trace
 * event with the expected path and field values: ruleset ID is non-zero,
 * access_rights is non-zero, and path matches.
 */
TEST_F(trace_fs, add_rule_fs)
{
	struct landlock_ruleset_attr ruleset_attr = {
		.handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE |
				     LANDLOCK_ACCESS_FS_WRITE_FILE |
				     LANDLOCK_ACCESS_FS_READ_DIR,
	};
	struct landlock_path_beneath_attr path_beneath = {
		.allowed_access = LANDLOCK_ACCESS_FS_READ_FILE,
	};
	char *buf, field_buf[64];
	int ruleset_fd, count;

	ruleset_fd =
		landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
	ASSERT_LE(0, ruleset_fd);

	path_beneath.parent_fd = open("/usr", O_PATH | O_DIRECTORY | O_CLOEXEC);
	ASSERT_LE(0, path_beneath.parent_fd);

	ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
				       &path_beneath, 0));
	ASSERT_EQ(0, close(path_beneath.parent_fd));
	ASSERT_EQ(0, close(ruleset_fd));

	buf = tracefs_read_buf();
	ASSERT_NE(NULL, buf);

	count = tracefs_count_matches(buf, REGEX_ADD_RULE_FS(TRACE_TASK));
	EXPECT_EQ(1, count)
	{
		TH_LOG("Expected 1 add_rule_fs event, got %d\n%s", count, buf);
	}

	/* Ruleset ID should be non-zero. */
	ASSERT_EQ(0, tracefs_extract_field(buf, REGEX_ADD_RULE_FS(TRACE_TASK),
					   "ruleset", field_buf,
					   sizeof(field_buf)));
	EXPECT_STRNE("0", field_buf);

	/* Access rights should be non-zero. */
	ASSERT_EQ(0, tracefs_extract_field(buf, REGEX_ADD_RULE_FS(TRACE_TASK),
					   "access_rights", field_buf,
					   sizeof(field_buf)));
	EXPECT_STRNE("", field_buf);

	/* Path should be /usr. */
	ASSERT_EQ(0,
		  tracefs_extract_field(buf, REGEX_ADD_RULE_FS(TRACE_TASK),
					"path", field_buf, sizeof(field_buf)));
	EXPECT_STREQ("/usr", field_buf);

	free(buf);
}

/*
 * Verifies that an allowed access emits check_rule events (rule matched during
 * pathwalk) but does NOT emit deny_access events (no denial).
 */
TEST_F(trace_fs, allowed_access)
{
	char *buf, field_buf[64];
	int count;

	ASSERT_EQ(0, tracefs_clear_buf());

	/* Rule allows READ_DIR for /usr, access /usr which is allowed. */
	sandbox_child_fs_access(_metadata, "/usr", LANDLOCK_ACCESS_FS_READ_DIR,
				LANDLOCK_ACCESS_FS_READ_DIR, "/usr");

	buf = tracefs_read_buf();
	ASSERT_NE(NULL, buf);

	count = tracefs_count_matches(buf, REGEX_CHECK_RULE_FS(TRACE_TASK));
	EXPECT_LE(1, count);

	/* Single-layer grants array, intersected with the request. */
	ASSERT_EQ(0, tracefs_extract_field(buf, REGEX_CHECK_RULE_FS(TRACE_TASK),
					   "grants", field_buf,
					   sizeof(field_buf)));
	EXPECT_STREQ("{read_dir}", field_buf);

	count = tracefs_count_matches(buf, REGEX_DENY_ACCESS_FS(TRACE_TASK));
	EXPECT_EQ(0, count);

	free(buf);
}

/*
 * Verifies that accessing a path whose access type is not in the handled set
 * does not emit landlock_check_rule events.  The ruleset handles READ_FILE, but
 * the directory open checks READ_DIR which is unhandled; Landlock has no
 * opinion and no rule evaluation occurs.
 */
TEST_F(trace_fs, check_rule_unhandled)
{
	char *buf;
	int count;

	ASSERT_EQ(0, tracefs_clear_buf());

	/* Handles READ_FILE only; READ_DIR is unhandled. */
	sandbox_child_fs_access(_metadata, "/usr", LANDLOCK_ACCESS_FS_READ_FILE,
				LANDLOCK_ACCESS_FS_READ_FILE, "/tmp");

	buf = tracefs_read_buf();
	ASSERT_NE(NULL, buf);

	/* No check_rule events because READ_DIR is not in the handled set. */
	count = tracefs_count_matches(buf, REGEX_CHECK_RULE_FS(TRACE_TASK));
	EXPECT_EQ(0, count);

	free(buf);
}

/*
 * Verifies that nested domains (child sandboxed under a parent domain) emit
 * check_rule events from both layers and produce a deny_access event when the
 * inner domain's rule does not cover the access.
 */
TEST_F(trace_fs, check_rule_nested)
{
	char *buf, field_buf[64], *comma;
	size_t first_len, second_len;
	int count_rule, count_access, status;
	pid_t pid;

	ASSERT_EQ(0, tracefs_clear_buf());

	pid = fork();
	ASSERT_LE(0, pid);

	if (pid == 0) {
		struct landlock_ruleset_attr ruleset_attr = {
			.handled_access_fs = LANDLOCK_ACCESS_FS_READ_DIR,
		};
		struct landlock_path_beneath_attr path_beneath = {
			.allowed_access = LANDLOCK_ACCESS_FS_READ_DIR,
		};
		int ruleset_fd, fd;

		/* First layer: allow /usr. */
		ruleset_fd = landlock_create_ruleset(&ruleset_attr,
						     sizeof(ruleset_attr), 0);
		if (ruleset_fd < 0)
			_exit(1);

		path_beneath.parent_fd =
			open("/usr", O_PATH | O_DIRECTORY | O_CLOEXEC);
		if (path_beneath.parent_fd < 0) {
			close(ruleset_fd);
			_exit(1);
		}

		if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
				      &path_beneath, 0)) {
			close(path_beneath.parent_fd);
			close(ruleset_fd);
			_exit(1);
		}
		close(path_beneath.parent_fd);

		prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
		if (landlock_restrict_self(ruleset_fd, 0)) {
			close(ruleset_fd);
			_exit(1);
		}
		close(ruleset_fd);

		/* Second layer: also allow /usr. */
		ruleset_fd = landlock_create_ruleset(&ruleset_attr,
						     sizeof(ruleset_attr), 0);
		if (ruleset_fd < 0)
			_exit(1);

		path_beneath.parent_fd =
			open("/usr", O_PATH | O_DIRECTORY | O_CLOEXEC);
		if (path_beneath.parent_fd < 0) {
			close(ruleset_fd);
			_exit(1);
		}

		if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
				      &path_beneath, 0)) {
			close(path_beneath.parent_fd);
			close(ruleset_fd);
			_exit(1);
		}
		close(path_beneath.parent_fd);

		if (landlock_restrict_self(ruleset_fd, 0)) {
			close(ruleset_fd);
			_exit(1);
		}
		close(ruleset_fd);

		/* Access /usr which is allowed by both layers. */
		fd = open("/usr", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
		if (fd >= 0)
			close(fd);

		/* Access /tmp which has no rule in either layer. */
		fd = open("/tmp", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
		if (fd >= 0)
			close(fd);

		_exit(0);
	}

	ASSERT_EQ(pid, waitpid(pid, &status, 0));
	ASSERT_TRUE(WIFEXITED(status));
	EXPECT_EQ(0, WEXITSTATUS(status));

	buf = tracefs_read_buf();
	ASSERT_NE(NULL, buf);

	count_rule =
		tracefs_count_matches(buf, REGEX_CHECK_RULE_FS(TRACE_TASK));
	EXPECT_LE(1, count_rule);

	/*
	 * Both layers have the same rule, so the grants array must have two
	 * identical symbolic entries, e.g. {read_dir,read_dir}.
	 */
	ASSERT_EQ(0, tracefs_extract_field(buf, REGEX_CHECK_RULE_FS(TRACE_TASK),
					   "grants", field_buf,
					   sizeof(field_buf)));
	comma = strchr(field_buf, ',');
	EXPECT_NE(0, !!comma);
	if (comma) {
		/*
		 * Verify both entries are identical: compare the substring
		 * before the comma with the substring after it (stripping the
		 * braces).
		 */
		first_len = comma - field_buf - 1;
		second_len = strlen(comma + 1) - 1;
		EXPECT_EQ(first_len, second_len);
		EXPECT_EQ(0, strncmp(field_buf + 1, comma + 1, first_len));
	}

	count_access =
		tracefs_count_matches(buf, REGEX_DENY_ACCESS_FS(TRACE_TASK));
	EXPECT_LE(1, count_access);

	free(buf);
}

/*
 * Verifies that a denied FS access emits a landlock_deny_access_fs trace event
 * with the blocked access and path.
 */
TEST_F(trace_fs, deny_access_fs_denied)
{
	char *buf;
	int count;

	ASSERT_EQ(0, tracefs_clear_buf());

	/*
	 * Rule allows READ_DIR for /usr, but access /tmp which has no rule.
	 * READ_DIR access to /tmp is denied by absence and should emit a
	 * deny_access_fs event.
	 */
	sandbox_child_fs_access(_metadata, "/usr", LANDLOCK_ACCESS_FS_READ_DIR,
				LANDLOCK_ACCESS_FS_READ_DIR, "/tmp");

	buf = tracefs_read_buf();
	ASSERT_NE(NULL, buf);

	count = tracefs_count_matches(buf, REGEX_DENY_ACCESS_FS(TRACE_TASK));
	EXPECT_LE(1, count);

	free(buf);
}

/*
 * A denied FS access covered by a quiet rule (LANDLOCK_ADD_RULE_QUIET with the
 * access listed in quiet_access_fs) still emits a landlock_deny_access_fs
 * event, but with logged=0, the same audit-logging verdict audit would apply to
 * suppress the record.
 */
TEST_F(trace_fs, deny_access_fs_quiet)
{
	char *buf, field[64];
	pid_t pid;
	int status;

	ASSERT_EQ(0, tracefs_clear_buf());

	pid = fork();
	ASSERT_LE(0, pid);
	if (pid == 0) {
		struct landlock_ruleset_attr ruleset_attr = {
			.handled_access_fs = LANDLOCK_ACCESS_FS_READ_DIR,
			.quiet_access_fs = LANDLOCK_ACCESS_FS_READ_DIR,
		};
		struct landlock_path_beneath_attr path_beneath = {
			.allowed_access = 0,
		};
		int ruleset_fd, fd;

		ruleset_fd = landlock_create_ruleset(&ruleset_attr,
						     sizeof(ruleset_attr), 0);
		if (ruleset_fd < 0)
			_exit(1);

		/* Marks /tmp quiet without granting any access. */
		path_beneath.parent_fd =
			open("/tmp", O_PATH | O_DIRECTORY | O_CLOEXEC);
		if (path_beneath.parent_fd < 0) {
			close(ruleset_fd);
			_exit(1);
		}
		if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
				      &path_beneath, LANDLOCK_ADD_RULE_QUIET)) {
			close(path_beneath.parent_fd);
			close(ruleset_fd);
			_exit(1);
		}
		close(path_beneath.parent_fd);

		prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
		if (landlock_restrict_self(ruleset_fd, 0)) {
			close(ruleset_fd);
			_exit(1);
		}
		close(ruleset_fd);

		/* Denied READ_DIR on the quiet /tmp: suppressed, logged=0. */
		fd = open("/tmp", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
		if (fd >= 0)
			close(fd);
		_exit(0);
	}
	ASSERT_EQ(pid, waitpid(pid, &status, 0));
	ASSERT_TRUE(WIFEXITED(status));
	EXPECT_EQ(0, WEXITSTATUS(status));

	buf = tracefs_read_buf();
	ASSERT_NE(NULL, buf);

	/* The event fires with the suppressed verdict. */
	EXPECT_LE(1, tracefs_count_matches(buf, REGEX_DENY_ACCESS_FS_LOGGED(
							TRACE_TASK, "0")));
	/* The quiet rule must not leave the denial logged. */
	EXPECT_EQ(0, tracefs_count_matches(buf, REGEX_DENY_ACCESS_FS_LOGGED(
							TRACE_TASK, "1")));

	/*
	 * Quiet suppresses only the logged verdict: the rest of the denial
	 * event stays populated (non-zero domain, non-empty blockers).
	 */
	ASSERT_EQ(0, tracefs_extract_field(
			     buf, REGEX_DENY_ACCESS_FS_LOGGED(TRACE_TASK, "0"),
			     "domain", field, sizeof(field)));
	EXPECT_STRNE("0", field);
	ASSERT_EQ(0, tracefs_extract_field(
			     buf, REGEX_DENY_ACCESS_FS_LOGGED(TRACE_TASK, "0"),
			     "blockers", field, sizeof(field)));
	EXPECT_STRNE("", field);

	free(buf);
}

TEST_HARNESS_MAIN