• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2021 SUSE LLC
4  * Author: Nicolai Stange <nstange@suse.de>
5  * LTP port: Martin Doucha <mdoucha@suse.cz>
6  *
7  * CVE-2020-29373
8  *
9  * Check that io_uring does not bypass chroot. Fixed in:
10  *
11  *  commit 9392a27d88b9707145d713654eb26f0c29789e50
12  *  Author: Jens Axboe <axboe@kernel.dk>
13  *  Date:   Thu Feb 6 21:42:51 2020 -0700
14  *
15  *  io-wq: add support for inheriting ->fs
16  *
17  *  commit ff002b30181d30cdfbca316dadd099c3ca0d739c
18  *  Author: Jens Axboe <axboe@kernel.dk>
19  *  Date:   Fri Feb 7 16:05:21 2020 -0700
20  *
21  *  io_uring: grab ->fs as part of async preparation
22  *
23  * stable 5.4 specific backport:
24  *
25  *  commit c4a23c852e80a3921f56c6fbc851a21c84a6d06b
26  *  Author: Nicolai Stange <nstange@suse.de>
27  *  Date:   Wed Jan 27 14:34:43 2021 +0100
28  */
29 
30 #include <stdio.h>
31 #include <sys/socket.h>
32 #include <sys/un.h>
33 #include "tst_test.h"
34 #include "tst_safe_io_uring.h"
35 
36 #define CHROOT_DIR "test_root"
37 #define SOCK_NAME "sock"
38 #define SPAM_MARK 0xfa7
39 #define BEEF_MARK 0xbeef
40 
41 static struct sockaddr_un addr;
42 static int sendsock = -1, recvsock = -1, sockpair[2] = {-1, -1};
43 static struct io_uring_params params;
44 static struct tst_io_uring uring = { .fd = -1 };
45 static char buf[16];
46 static struct iovec iov = {
47 	.iov_base = buf,
48 	.iov_len = sizeof(buf)
49 };
50 
51 static struct msghdr spam_header = {
52 	.msg_name = NULL,
53 	.msg_namelen = 0,
54 	.msg_iov = &iov,
55 	.msg_iovlen = 1
56 };
57 
58 static struct msghdr beef_header = {
59 	.msg_name = &addr,
60 	.msg_namelen = sizeof(addr),
61 	.msg_iov = &iov,
62 	.msg_iovlen = 1
63 };
64 
setup(void)65 static void setup(void)
66 {
67 	char *tmpdir = tst_tmpdir_path();
68 	int ret;
69 
70 	addr.sun_family = AF_UNIX;
71 	ret = snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s", tmpdir,
72 		SOCK_NAME);
73 
74 	if (ret >= (int)sizeof(addr.sun_path))
75 		tst_brk(TBROK, "Tempdir path is too long");
76 
77 	io_uring_setup_supported_by_kernel();
78 
79 	sendsock = SAFE_SOCKET(AF_UNIX, SOCK_DGRAM, 0);
80 	recvsock = SAFE_SOCKET(AF_UNIX, SOCK_DGRAM, 0);
81 	SAFE_BIND(recvsock, (struct sockaddr *)&addr, sizeof(addr));
82 
83 	SAFE_MKDIR(CHROOT_DIR, 0755);
84 	SAFE_CHROOT(CHROOT_DIR);
85 }
86 
drain_fallback(void)87 static void drain_fallback(void)
88 {
89 	uint32_t i, count, tail;
90 	int beef_found = 0;
91 	struct io_uring_sqe *sqe_ptr = uring.sqr_entries;
92 	const struct io_uring_cqe *cqe_ptr;
93 
94 	SAFE_SOCKETPAIR(AF_UNIX, SOCK_DGRAM, 0, sockpair);
95 	SAFE_SETSOCKOPT_INT(sockpair[0], SOL_SOCKET, SO_SNDBUF,
96 		32+sizeof(buf));
97 	SAFE_FCNTL(sockpair[0], F_SETFL, O_NONBLOCK);
98 
99 	/* Add spam requests to force async processing of the real test */
100 	for (i = 0, tail = *uring.sqr_tail; i < 255; i++, tail++, sqe_ptr++) {
101 		memset(sqe_ptr, 0, sizeof(*sqe_ptr));
102 		sqe_ptr->opcode = IORING_OP_SENDMSG;
103 		sqe_ptr->flags = IOSQE_IO_DRAIN;
104 		sqe_ptr->fd = sockpair[0];
105 		sqe_ptr->addr = (__u64)&spam_header;
106 		sqe_ptr->user_data = SPAM_MARK;
107 		uring.sqr_array[tail & *uring.sqr_mask] = i;
108 	}
109 
110 	/* Add the real test to queue */
111 	memset(sqe_ptr, 0, sizeof(*sqe_ptr));
112 	sqe_ptr->opcode = IORING_OP_SENDMSG;
113 	sqe_ptr->flags = IOSQE_IO_DRAIN;
114 	sqe_ptr->fd = sendsock;
115 	sqe_ptr->addr = (__u64)&beef_header;
116 	sqe_ptr->user_data = BEEF_MARK;
117 	uring.sqr_array[tail & *uring.sqr_mask] = i;
118 	count = ++i;
119 	tail++;
120 
121 	__atomic_store(uring.sqr_tail, &tail, __ATOMIC_RELEASE);
122 	SAFE_IO_URING_ENTER(1, uring.fd, count, count, IORING_ENTER_GETEVENTS,
123 		NULL);
124 
125 	/* Check test results */
126 	__atomic_load(uring.cqr_tail, &tail, __ATOMIC_ACQUIRE);
127 
128 	for (i = *uring.cqr_head; i != tail; i++, count--) {
129 		cqe_ptr = uring.cqr_entries + (i & *uring.cqr_mask);
130 		TST_ERR = -cqe_ptr->res;
131 
132 		if (cqe_ptr->user_data == SPAM_MARK) {
133 			if (cqe_ptr->res >= 0 || cqe_ptr->res == -EAGAIN)
134 				continue;
135 
136 			tst_res(TFAIL | TTERRNO,
137 				"Spam request failed unexpectedly");
138 			continue;
139 		}
140 
141 		if (cqe_ptr->user_data != BEEF_MARK) {
142 			tst_res(TFAIL, "Unexpected entry in completion queue");
143 			count++;
144 			continue;
145 		}
146 
147 		beef_found = 1;
148 
149 		if (cqe_ptr->res >= 0) {
150 			tst_res(TFAIL, "Write outside chroot succeeded.");
151 		} else if (cqe_ptr->res != -ENOENT) {
152 			tst_res(TFAIL | TTERRNO,
153 				"Write outside chroot failed unexpectedly");
154 		} else {
155 			tst_res(TPASS | TTERRNO,
156 				"Write outside chroot failed as expected");
157 		}
158 	}
159 
160 	__atomic_store(uring.cqr_head, &i, __ATOMIC_RELEASE);
161 
162 	if (!beef_found)
163 		tst_res(TFAIL, "Write outside chroot result not found");
164 
165 	if (count)
166 		tst_res(TFAIL, "Wrong number of entries in completion queue");
167 
168 	SAFE_CLOSE(sockpair[0]);
169 	SAFE_CLOSE(sockpair[1]);
170 }
171 
check_result(void)172 static void check_result(void)
173 {
174 	const struct io_uring_cqe *cqe_ptr;
175 
176 	cqe_ptr = uring.cqr_entries + (*uring.cqr_head & *uring.cqr_mask);
177 	++*uring.cqr_head;
178 	TST_ERR = -cqe_ptr->res;
179 
180 	if (cqe_ptr->user_data != BEEF_MARK) {
181 		tst_res(TFAIL, "Unexpected entry in completion queue");
182 		return;
183 	}
184 
185 	if (cqe_ptr->res == -EINVAL) {
186 		tst_res(TINFO, "IOSQE_ASYNC is not supported, using fallback");
187 		drain_fallback();
188 		return;
189 	}
190 
191 	tst_res(TINFO, "IOSQE_ASYNC is supported");
192 
193 	if (cqe_ptr->res >= 0) {
194 		tst_res(TFAIL, "Write outside chroot succeeded.");
195 		return;
196 	}
197 
198 	if (cqe_ptr->res != -ENOENT) {
199 		tst_res(TFAIL | TTERRNO,
200 			"Write outside chroot failed unexpectedly");
201 		return;
202 	}
203 
204 	tst_res(TPASS | TTERRNO, "Write outside chroot failed as expected");
205 }
206 
run(void)207 static void run(void)
208 {
209 	uint32_t tail;
210 	struct io_uring_sqe *sqe_ptr;
211 
212 	SAFE_IO_URING_INIT(512, &params, &uring);
213 	sqe_ptr = uring.sqr_entries;
214 	tail = *uring.sqr_tail;
215 
216 	memset(sqe_ptr, 0, sizeof(*sqe_ptr));
217 	sqe_ptr->opcode = IORING_OP_SENDMSG;
218 	sqe_ptr->flags = IOSQE_ASYNC;
219 	sqe_ptr->fd = sendsock;
220 	sqe_ptr->addr = (__u64)&beef_header;
221 	sqe_ptr->user_data = BEEF_MARK;
222 	uring.sqr_array[tail & *uring.sqr_mask] = 0;
223 	tail++;
224 
225 	__atomic_store(uring.sqr_tail, &tail, __ATOMIC_RELEASE);
226 	SAFE_IO_URING_ENTER(1, uring.fd, 1, 1, IORING_ENTER_GETEVENTS, NULL);
227 	check_result();
228 	SAFE_IO_URING_CLOSE(&uring);
229 }
230 
cleanup(void)231 static void cleanup(void)
232 {
233 	if (uring.fd >= 0)
234 		SAFE_IO_URING_CLOSE(&uring);
235 
236 	if (sockpair[0] >= 0) {
237 		SAFE_CLOSE(sockpair[0]);
238 		SAFE_CLOSE(sockpair[1]);
239 	}
240 
241 	if (recvsock >= 0)
242 		SAFE_CLOSE(recvsock);
243 
244 	if (sendsock >= 0)
245 		SAFE_CLOSE(sendsock);
246 }
247 
248 static struct tst_test test = {
249 	.test_all = run,
250 	.setup = setup,
251 	.cleanup = cleanup,
252 	.needs_tmpdir = 1,
253 	.caps = (struct tst_cap []) {
254 		TST_CAP(TST_CAP_REQ, CAP_SYS_CHROOT),
255 		{}
256 	},
257 	.save_restore = (const struct tst_path_val[]) {
258 		{"/proc/sys/kernel/io_uring_disabled", "0",
259 			TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
260 		{}
261 	},
262 	.tags = (const struct tst_tag[]) {
263 		{"linux-git", "9392a27d88b9"},
264 		{"linux-git", "ff002b30181d"},
265 		{"linux-git", "d87683620489"},
266 		{"linux-stable-git", "c4a23c852e80"},
267 		{"linux-stable-git", "cac68d12c531"},
268 		{"CVE", "2020-29373"},
269 		{}
270 	}
271 };
272