1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Landlock tests - Common user space base
4  *
5  * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net>
6  * Copyright © 2019-2020 ANSSI
7  */
8 
9 #define _GNU_SOURCE
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <linux/landlock.h>
13 #include <string.h>
14 #include <sys/prctl.h>
15 #include <sys/socket.h>
16 #include <sys/types.h>
17 
18 #include "common.h"
19 
20 #ifndef O_PATH
21 #define O_PATH 010000000
22 #endif
23 
24 TEST(inconsistent_attr)
25 {
26 	const long page_size = sysconf(_SC_PAGESIZE);
27 	char *const buf = malloc(page_size + 1);
28 	struct landlock_ruleset_attr *const ruleset_attr = (void *)buf;
29 
30 	ASSERT_NE(NULL, buf);
31 
32 	/* Checks copy_from_user(). */
33 	ASSERT_EQ(-1, landlock_create_ruleset(ruleset_attr, 0, 0));
34 	/* The size if less than sizeof(struct landlock_attr_enforce). */
35 	ASSERT_EQ(EINVAL, errno);
36 	ASSERT_EQ(-1, landlock_create_ruleset(ruleset_attr, 1, 0));
37 	ASSERT_EQ(EINVAL, errno);
38 	ASSERT_EQ(-1, landlock_create_ruleset(ruleset_attr, 7, 0));
39 	ASSERT_EQ(EINVAL, errno);
40 
41 	ASSERT_EQ(-1, landlock_create_ruleset(NULL, 1, 0));
42 	/* The size if less than sizeof(struct landlock_attr_enforce). */
43 	ASSERT_EQ(EFAULT, errno);
44 
45 	ASSERT_EQ(-1, landlock_create_ruleset(
46 			      NULL, sizeof(struct landlock_ruleset_attr), 0));
47 	ASSERT_EQ(EFAULT, errno);
48 
49 	ASSERT_EQ(-1, landlock_create_ruleset(ruleset_attr, page_size + 1, 0));
50 	ASSERT_EQ(E2BIG, errno);
51 
52 	/* Checks minimal valid attribute size. */
53 	ASSERT_EQ(-1, landlock_create_ruleset(ruleset_attr, 8, 0));
54 	ASSERT_EQ(ENOMSG, errno);
55 	ASSERT_EQ(-1, landlock_create_ruleset(
56 			      ruleset_attr,
57 			      sizeof(struct landlock_ruleset_attr), 0));
58 	ASSERT_EQ(ENOMSG, errno);
59 	ASSERT_EQ(-1, landlock_create_ruleset(ruleset_attr, page_size, 0));
60 	ASSERT_EQ(ENOMSG, errno);
61 
62 	/* Checks non-zero value. */
63 	buf[page_size - 2] = '.';
64 	ASSERT_EQ(-1, landlock_create_ruleset(ruleset_attr, page_size, 0));
65 	ASSERT_EQ(E2BIG, errno);
66 
67 	ASSERT_EQ(-1, landlock_create_ruleset(ruleset_attr, page_size + 1, 0));
68 	ASSERT_EQ(E2BIG, errno);
69 
70 	free(buf);
71 }
72 
73 TEST(abi_version)
74 {
75 	const struct landlock_ruleset_attr ruleset_attr = {
76 		.handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE,
77 	};
78 	ASSERT_EQ(1, landlock_create_ruleset(NULL, 0,
79 					     LANDLOCK_CREATE_RULESET_VERSION));
80 
81 	ASSERT_EQ(-1, landlock_create_ruleset(&ruleset_attr, 0,
82 					      LANDLOCK_CREATE_RULESET_VERSION));
83 	ASSERT_EQ(EINVAL, errno);
84 
85 	ASSERT_EQ(-1, landlock_create_ruleset(NULL, sizeof(ruleset_attr),
86 					      LANDLOCK_CREATE_RULESET_VERSION));
87 	ASSERT_EQ(EINVAL, errno);
88 
89 	ASSERT_EQ(-1,
90 		  landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr),
91 					  LANDLOCK_CREATE_RULESET_VERSION));
92 	ASSERT_EQ(EINVAL, errno);
93 
94 	ASSERT_EQ(-1, landlock_create_ruleset(NULL, 0,
95 					      LANDLOCK_CREATE_RULESET_VERSION |
96 						      1 << 31));
97 	ASSERT_EQ(EINVAL, errno);
98 }
99 
100 TEST(inval_create_ruleset_flags)
101 {
102 	const int last_flag = LANDLOCK_CREATE_RULESET_VERSION;
103 	const int invalid_flag = last_flag << 1;
104 	const struct landlock_ruleset_attr ruleset_attr = {
105 		.handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE,
106 	};
107 
108 	ASSERT_EQ(-1, landlock_create_ruleset(NULL, 0, invalid_flag));
109 	ASSERT_EQ(EINVAL, errno);
110 
111 	ASSERT_EQ(-1, landlock_create_ruleset(&ruleset_attr, 0, invalid_flag));
112 	ASSERT_EQ(EINVAL, errno);
113 
114 	ASSERT_EQ(-1, landlock_create_ruleset(NULL, sizeof(ruleset_attr),
115 					      invalid_flag));
116 	ASSERT_EQ(EINVAL, errno);
117 
118 	ASSERT_EQ(-1,
119 		  landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr),
120 					  invalid_flag));
121 	ASSERT_EQ(EINVAL, errno);
122 }
123 
124 TEST(empty_path_beneath_attr)
125 {
126 	const struct landlock_ruleset_attr ruleset_attr = {
127 		.handled_access_fs = LANDLOCK_ACCESS_FS_EXECUTE,
128 	};
129 	const int ruleset_fd =
130 		landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
131 
132 	ASSERT_LE(0, ruleset_fd);
133 
134 	/* Similar to struct landlock_path_beneath_attr.parent_fd = 0 */
135 	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
136 					NULL, 0));
137 	ASSERT_EQ(EFAULT, errno);
138 	ASSERT_EQ(0, close(ruleset_fd));
139 }
140 
141 TEST(inval_fd_enforce)
142 {
143 	ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0));
144 
145 	ASSERT_EQ(-1, landlock_restrict_self(-1, 0));
146 	ASSERT_EQ(EBADF, errno);
147 }
148 
149 TEST(unpriv_enforce_without_no_new_privs)
150 {
151 	int err;
152 
153 	drop_caps(_metadata);
154 	err = landlock_restrict_self(-1, 0);
155 	ASSERT_EQ(EPERM, errno);
156 	ASSERT_EQ(err, -1);
157 }
158 
159 TEST(ruleset_fd_io)
160 {
161 	struct landlock_ruleset_attr ruleset_attr = {
162 		.handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE,
163 	};
164 	int ruleset_fd;
165 	char buf;
166 
167 	drop_caps(_metadata);
168 	ruleset_fd =
169 		landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
170 	ASSERT_LE(0, ruleset_fd);
171 
172 	ASSERT_EQ(-1, write(ruleset_fd, ".", 1));
173 	ASSERT_EQ(EINVAL, errno);
174 	ASSERT_EQ(-1, read(ruleset_fd, &buf, 1));
175 	ASSERT_EQ(EINVAL, errno);
176 
177 	ASSERT_EQ(0, close(ruleset_fd));
178 }
179 
180 /* Tests enforcement of a ruleset FD transferred through a UNIX socket. */
181 TEST(ruleset_fd_transfer)
182 {
183 	struct landlock_ruleset_attr ruleset_attr = {
184 		.handled_access_fs = LANDLOCK_ACCESS_FS_READ_DIR,
185 	};
186 	struct landlock_path_beneath_attr path_beneath_attr = {
187 		.allowed_access = LANDLOCK_ACCESS_FS_READ_DIR,
188 	};
189 	int ruleset_fd_tx, dir_fd;
190 	union {
191 		/* Aligned ancillary data buffer. */
192 		char buf[CMSG_SPACE(sizeof(ruleset_fd_tx))];
193 		struct cmsghdr _align;
194 	} cmsg_tx = {};
195 	char data_tx = '.';
196 	struct iovec io = {
197 		.iov_base = &data_tx,
198 		.iov_len = sizeof(data_tx),
199 	};
200 	struct msghdr msg = {
201 		.msg_iov = &io,
202 		.msg_iovlen = 1,
203 		.msg_control = &cmsg_tx.buf,
204 		.msg_controllen = sizeof(cmsg_tx.buf),
205 	};
206 	struct cmsghdr *cmsg;
207 	int socket_fds[2];
208 	pid_t child;
209 	int status;
210 
211 	drop_caps(_metadata);
212 
213 	/* Creates a test ruleset with a simple rule. */
214 	ruleset_fd_tx =
215 		landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
216 	ASSERT_LE(0, ruleset_fd_tx);
217 	path_beneath_attr.parent_fd =
218 		open("/tmp", O_PATH | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
219 	ASSERT_LE(0, path_beneath_attr.parent_fd);
220 	ASSERT_EQ(0,
221 		  landlock_add_rule(ruleset_fd_tx, LANDLOCK_RULE_PATH_BENEATH,
222 				    &path_beneath_attr, 0));
223 	ASSERT_EQ(0, close(path_beneath_attr.parent_fd));
224 
225 	cmsg = CMSG_FIRSTHDR(&msg);
226 	ASSERT_NE(NULL, cmsg);
227 	cmsg->cmsg_len = CMSG_LEN(sizeof(ruleset_fd_tx));
228 	cmsg->cmsg_level = SOL_SOCKET;
229 	cmsg->cmsg_type = SCM_RIGHTS;
230 	memcpy(CMSG_DATA(cmsg), &ruleset_fd_tx, sizeof(ruleset_fd_tx));
231 
232 	/* Sends the ruleset FD over a socketpair and then close it. */
233 	ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0,
234 				socket_fds));
235 	ASSERT_EQ(sizeof(data_tx), sendmsg(socket_fds[0], &msg, 0));
236 	ASSERT_EQ(0, close(socket_fds[0]));
237 	ASSERT_EQ(0, close(ruleset_fd_tx));
238 
239 	child = fork();
240 	ASSERT_LE(0, child);
241 	if (child == 0) {
242 		int ruleset_fd_rx;
243 
244 		*(char *)msg.msg_iov->iov_base = '\0';
245 		ASSERT_EQ(sizeof(data_tx),
246 			  recvmsg(socket_fds[1], &msg, MSG_CMSG_CLOEXEC));
247 		ASSERT_EQ('.', *(char *)msg.msg_iov->iov_base);
248 		ASSERT_EQ(0, close(socket_fds[1]));
249 		cmsg = CMSG_FIRSTHDR(&msg);
250 		ASSERT_EQ(cmsg->cmsg_len, CMSG_LEN(sizeof(ruleset_fd_tx)));
251 		memcpy(&ruleset_fd_rx, CMSG_DATA(cmsg), sizeof(ruleset_fd_tx));
252 
253 		/* Enforces the received ruleset on the child. */
254 		ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0));
255 		ASSERT_EQ(0, landlock_restrict_self(ruleset_fd_rx, 0));
256 		ASSERT_EQ(0, close(ruleset_fd_rx));
257 
258 		/* Checks that the ruleset enforcement. */
259 		ASSERT_EQ(-1, open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
260 		ASSERT_EQ(EACCES, errno);
261 		dir_fd = open("/tmp", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
262 		ASSERT_LE(0, dir_fd);
263 		ASSERT_EQ(0, close(dir_fd));
264 		_exit(_metadata->passed ? EXIT_SUCCESS : EXIT_FAILURE);
265 		return;
266 	}
267 
268 	ASSERT_EQ(0, close(socket_fds[1]));
269 
270 	/* Checks that the parent is unrestricted. */
271 	dir_fd = open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
272 	ASSERT_LE(0, dir_fd);
273 	ASSERT_EQ(0, close(dir_fd));
274 	dir_fd = open("/tmp", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
275 	ASSERT_LE(0, dir_fd);
276 	ASSERT_EQ(0, close(dir_fd));
277 
278 	ASSERT_EQ(child, waitpid(child, &status, 0));
279 	ASSERT_EQ(1, WIFEXITED(status));
280 	ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
281 }
282 
283 TEST_HARNESS_MAIN
284