• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Landlock test helpers
4  *
5  * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net>
6  * Copyright © 2019-2020 ANSSI
7  * Copyright © 2021 Microsoft Corporation
8  */
9 
10 #include <errno.h>
11 #include <linux/landlock.h>
12 #include <sys/capability.h>
13 #include <sys/syscall.h>
14 #include <sys/types.h>
15 #include <sys/wait.h>
16 #include <unistd.h>
17 
18 #include "../kselftest_harness.h"
19 
20 #ifndef ARRAY_SIZE
21 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
22 #endif
23 
24 /*
25  * TEST_F_FORK() is useful when a test drop privileges but the corresponding
26  * FIXTURE_TEARDOWN() requires them (e.g. to remove files from a directory
27  * where write actions are denied).  For convenience, FIXTURE_TEARDOWN() is
28  * also called when the test failed, but not when FIXTURE_SETUP() failed.  For
29  * this to be possible, we must not call abort() but instead exit smoothly
30  * (hence the step print).
31  */
32 /* clang-format off */
33 #define TEST_F_FORK(fixture_name, test_name) \
34 	static void fixture_name##_##test_name##_child( \
35 		struct __test_metadata *_metadata, \
36 		FIXTURE_DATA(fixture_name) *self, \
37 		const FIXTURE_VARIANT(fixture_name) *variant); \
38 	TEST_F(fixture_name, test_name) \
39 	{ \
40 		int status; \
41 		const pid_t child = fork(); \
42 		if (child < 0) \
43 			abort(); \
44 		if (child == 0) { \
45 			_metadata->no_print = 1; \
46 			fixture_name##_##test_name##_child(_metadata, self, variant); \
47 			if (_metadata->skip) \
48 				_exit(255); \
49 			if (_metadata->passed) \
50 				_exit(0); \
51 			_exit(_metadata->step); \
52 		} \
53 		if (child != waitpid(child, &status, 0)) \
54 			abort(); \
55 		if (WIFSIGNALED(status) || !WIFEXITED(status)) { \
56 			_metadata->passed = 0; \
57 			_metadata->step = 1; \
58 			return; \
59 		} \
60 		switch (WEXITSTATUS(status)) { \
61 		case 0: \
62 			_metadata->passed = 1; \
63 			break; \
64 		case 255: \
65 			_metadata->passed = 1; \
66 			_metadata->skip = 1; \
67 			break; \
68 		default: \
69 			_metadata->passed = 0; \
70 			_metadata->step = WEXITSTATUS(status); \
71 			break; \
72 		} \
73 	} \
74 	static void fixture_name##_##test_name##_child( \
75 		struct __test_metadata __attribute__((unused)) *_metadata, \
76 		FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
77 		const FIXTURE_VARIANT(fixture_name) \
78 			__attribute__((unused)) *variant)
79 /* clang-format on */
80 
81 #ifndef landlock_create_ruleset
82 static inline int
landlock_create_ruleset(const struct landlock_ruleset_attr * const attr,const size_t size,const __u32 flags)83 landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,
84 			const size_t size, const __u32 flags)
85 {
86 	return syscall(__NR_landlock_create_ruleset, attr, size, flags);
87 }
88 #endif
89 
90 #ifndef landlock_add_rule
landlock_add_rule(const int ruleset_fd,const enum landlock_rule_type rule_type,const void * const rule_attr,const __u32 flags)91 static inline int landlock_add_rule(const int ruleset_fd,
92 				    const enum landlock_rule_type rule_type,
93 				    const void *const rule_attr,
94 				    const __u32 flags)
95 {
96 	return syscall(__NR_landlock_add_rule, ruleset_fd, rule_type, rule_attr,
97 		       flags);
98 }
99 #endif
100 
101 #ifndef landlock_restrict_self
landlock_restrict_self(const int ruleset_fd,const __u32 flags)102 static inline int landlock_restrict_self(const int ruleset_fd,
103 					 const __u32 flags)
104 {
105 	return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
106 }
107 #endif
108 
_init_caps(struct __test_metadata * const _metadata,bool drop_all)109 static void _init_caps(struct __test_metadata *const _metadata, bool drop_all)
110 {
111 	cap_t cap_p;
112 	/* Only these three capabilities are useful for the tests. */
113 	const cap_value_t caps[] = {
114 		CAP_DAC_OVERRIDE,
115 		CAP_MKNOD,
116 		CAP_SYS_ADMIN,
117 		CAP_SYS_CHROOT,
118 	};
119 
120 	cap_p = cap_get_proc();
121 	EXPECT_NE(NULL, cap_p)
122 	{
123 		TH_LOG("Failed to cap_get_proc: %s", strerror(errno));
124 	}
125 	EXPECT_NE(-1, cap_clear(cap_p))
126 	{
127 		TH_LOG("Failed to cap_clear: %s", strerror(errno));
128 	}
129 	if (!drop_all) {
130 		EXPECT_NE(-1, cap_set_flag(cap_p, CAP_PERMITTED,
131 					   ARRAY_SIZE(caps), caps, CAP_SET))
132 		{
133 			TH_LOG("Failed to cap_set_flag: %s", strerror(errno));
134 		}
135 	}
136 	EXPECT_NE(-1, cap_set_proc(cap_p))
137 	{
138 		TH_LOG("Failed to cap_set_proc: %s", strerror(errno));
139 	}
140 	EXPECT_NE(-1, cap_free(cap_p))
141 	{
142 		TH_LOG("Failed to cap_free: %s", strerror(errno));
143 	}
144 }
145 
146 /* We cannot put such helpers in a library because of kselftest_harness.h . */
147 __attribute__((__unused__)) static void
disable_caps(struct __test_metadata * const _metadata)148 disable_caps(struct __test_metadata *const _metadata)
149 {
150 	_init_caps(_metadata, false);
151 }
152 
153 __attribute__((__unused__)) static void
drop_caps(struct __test_metadata * const _metadata)154 drop_caps(struct __test_metadata *const _metadata)
155 {
156 	_init_caps(_metadata, true);
157 }
158 
_effective_cap(struct __test_metadata * const _metadata,const cap_value_t caps,const cap_flag_value_t value)159 static void _effective_cap(struct __test_metadata *const _metadata,
160 			   const cap_value_t caps, const cap_flag_value_t value)
161 {
162 	cap_t cap_p;
163 
164 	cap_p = cap_get_proc();
165 	EXPECT_NE(NULL, cap_p)
166 	{
167 		TH_LOG("Failed to cap_get_proc: %s", strerror(errno));
168 	}
169 	EXPECT_NE(-1, cap_set_flag(cap_p, CAP_EFFECTIVE, 1, &caps, value))
170 	{
171 		TH_LOG("Failed to cap_set_flag: %s", strerror(errno));
172 	}
173 	EXPECT_NE(-1, cap_set_proc(cap_p))
174 	{
175 		TH_LOG("Failed to cap_set_proc: %s", strerror(errno));
176 	}
177 	EXPECT_NE(-1, cap_free(cap_p))
178 	{
179 		TH_LOG("Failed to cap_free: %s", strerror(errno));
180 	}
181 }
182 
183 __attribute__((__unused__)) static void
set_cap(struct __test_metadata * const _metadata,const cap_value_t caps)184 set_cap(struct __test_metadata *const _metadata, const cap_value_t caps)
185 {
186 	_effective_cap(_metadata, caps, CAP_SET);
187 }
188 
189 __attribute__((__unused__)) static void
clear_cap(struct __test_metadata * const _metadata,const cap_value_t caps)190 clear_cap(struct __test_metadata *const _metadata, const cap_value_t caps)
191 {
192 	_effective_cap(_metadata, caps, CAP_CLEAR);
193 }
194