• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2020 SUSE
4  *
5  * Test transmitting data over a PTY/TTY line discipline and reading from the
6  * virtual netdev created by the line discipline. Also hangup the PTY while
7  * data is in flight to try to cause a race between the netdev being deleted
8  * and the discipline receive function writing to the netdev.
9  *
10  * For SLCAN we check stack data is not leaked in the frame padding
11  * (CVE-2020-11494).
12  *
13  * Test flow:
14  * 1. Create PTY with ldisc X which creates netdev Y
15  * 2. Open raw packet socket and bind to netdev Y
16  * 3. Send data on ptmx and read packets from socket
17  * 4. Hangup while transmission in progress
18  *
19  * Note that not all line disciplines call unthrottle when they are ready to
20  * read more bytes. So it is possible to fill all the write buffers causing
21  * write to block forever (because once write sleeps it needs unthrottle to
22  * wake it). So we write with O_NONBLOCK.
23  *
24  * Also the max buffer size for PTYs is 8192, so even if the protocol MTU is
25  * greater everything may still be processed in 8129 byte chunks. At least
26  * until we are in the netdev code which can have a bigger buffer. Of course
27  * the MTU still decides exactly where the packet delimiter goes, this just
28  * concerns choosing the best packet size to cause a race.
29  *
30  * Note on line discipline encapsulation formats:
31  * - For SLIP frames we just write the data followed by a delimiter char
32  * - SLCAN we write some ASCII described in drivers/net/can/slcan.c which is
33  *   converted to the actual frame by the kernel
34  */
35 
36 #define _GNU_SOURCE
37 #include "config.h"
38 #include "tst_test.h"
39 #include "tst_buffers.h"
40 #include "lapi/tty.h"
41 
42 #if defined(HAVE_LINUX_IF_PACKET_H) && defined(HAVE_LINUX_IF_ETHER_H)
43 
44 #include <linux/if_packet.h>
45 #include <linux/if_ether.h>
46 #include <linux/tty.h>
47 
48 /*
49  * define instead of including <linux/can.h> to support kernel headers
50  * before change from v4.2-rc1
51  * a2f11835994e ("can.h: make padding given by gcc explicit").
52  */
53 
54 #define CAN_MTU		(sizeof(struct can_frame))
55 #define CAN_MAX_DLEN 8
56 
57 typedef uint32_t canid_t;
58 
59 struct can_frame {
60 	canid_t can_id;
61 	uint8_t can_dlc;
62 	uint8_t __pad;
63 	uint8_t __res0;
64 	uint8_t __res1;
65 	uint8_t data[CAN_MAX_DLEN] __attribute__((aligned(8)));
66 };
67 
68 #include <stddef.h>
69 #include <stdlib.h>
70 #include <stdio.h>
71 #include <errno.h>
72 #include <unistd.h>
73 #include <termios.h>
74 #include <sys/types.h>
75 #include <sys/socket.h>
76 #include <net/if.h>
77 #include "lapi/ioctl.h"
78 
79 #include "tst_safe_stdio.h"
80 
81 #define str(s) #s
82 #define SLCAN_FRAME "t00185f5f5f5f5f5f5f5f\r"
83 
84 struct ldisc_info {
85 	int n;
86 	char *name;
87 	int mtu;
88 };
89 
90 static struct ldisc_info ldiscs[] = {
91 	{N_SLIP, "N_SLIP", 8192},
92 	{N_SLCAN, "N_SLCAN", CAN_MTU},
93 };
94 
95 static int ptmx, pts, sk, mtu, no_check;
96 
set_ldisc(int tty,const struct ldisc_info * ldisc)97 static int set_ldisc(int tty, const struct ldisc_info *ldisc)
98 {
99 	TEST(ioctl(tty, TIOCSETD, &ldisc->n));
100 
101 	if (!TST_RET)
102 		return 0;
103 
104 	if (TST_ERR == EINVAL) {
105 		tst_res(TCONF | TTERRNO,
106 			"You don't appear to have the %s TTY line discipline",
107 			ldisc->name);
108 	} else {
109 		tst_res(TFAIL | TTERRNO,
110 			"Failed to set the %s line discipline", ldisc->name);
111 	}
112 
113 	return 1;
114 }
115 
open_pty(const struct ldisc_info * ldisc)116 static int open_pty(const struct ldisc_info *ldisc)
117 {
118 	char pts_path[PATH_MAX];
119 
120 	ptmx = SAFE_OPEN("/dev/ptmx", O_RDWR);
121 	if (grantpt(ptmx))
122 		tst_brk(TBROK | TERRNO, "grantpt(ptmx)");
123 	if (unlockpt(ptmx))
124 		tst_brk(TBROK | TERRNO, "unlockpt(ptmx)");
125 	if (ptsname_r(ptmx, pts_path, sizeof(pts_path)))
126 		tst_brk(TBROK | TERRNO, "ptsname_r(ptmx, ...)");
127 
128 	SAFE_FCNTL(ptmx, F_SETFL, O_NONBLOCK);
129 
130 	tst_res(TINFO, "PTS path is %s", pts_path);
131 	pts = SAFE_OPEN(pts_path, O_RDWR);
132 
133 	return set_ldisc(pts, ldisc);
134 }
135 
try_async_write(int fd,const char * data,ssize_t size,ssize_t * done)136 static ssize_t try_async_write(int fd, const char *data, ssize_t size,
137 			       ssize_t *done)
138 {
139 	ssize_t off = done ? *done : 0;
140 	ssize_t ret = write(fd, data + off, size - off);
141 
142 	if (ret < 0)
143 		return -(errno != EAGAIN);
144 
145 	if (!done)
146 		return 1;
147 
148 	*done += ret;
149 	return *done >= size;
150 }
151 
try_async_read(int fd,char * data,ssize_t size,ssize_t * done)152 static ssize_t try_async_read(int fd, char *data, ssize_t size,
153 			      ssize_t *done)
154 {
155 	ssize_t off = done ? *done : 0;
156 	ssize_t ret = read(fd, data + off, size - off);
157 
158 	if (ret < 0)
159 		return -(errno != EAGAIN);
160 
161 	if (!done)
162 		return 1;
163 
164 	*done += ret;
165 	return *done >= size;
166 }
167 
retry_async_write(int fd,const char * data,ssize_t size)168 static ssize_t retry_async_write(int fd, const char *data, ssize_t size)
169 {
170 	ssize_t done = 0;
171 
172 	return TST_RETRY_FUNC(try_async_write(fd, data, size, &done),
173 			      TST_RETVAL_NOTNULL);
174 }
175 
retry_async_read(int fd,char * data,ssize_t size)176 static ssize_t retry_async_read(int fd, char *data, ssize_t size)
177 {
178 	ssize_t done = 0;
179 
180 	return TST_RETRY_FUNC(try_async_read(fd, data, size, &done),
181 			      TST_RETVAL_NOTNULL);
182 }
183 
do_pty(const struct ldisc_info * ldisc)184 static void do_pty(const struct ldisc_info *ldisc)
185 {
186 	char *data;
187 	ssize_t ret;
188 	size_t len = 0;
189 
190 	switch (ldisc->n) {
191 	case N_SLIP:
192 		len = mtu;
193 		break;
194 	case N_SLCAN:
195 		len = sizeof(SLCAN_FRAME) - 1;
196 		break;
197 	}
198 
199 	data = tst_alloc(len);
200 
201 	switch (ldisc->n) {
202 	case N_SLIP:
203 		memset(data, '_', len - 1);
204 		data[len - 1] = 0300;
205 		break;
206 	case N_SLCAN:
207 		memcpy(data, SLCAN_FRAME, len);
208 		break;
209 	}
210 
211 	ret = retry_async_write(ptmx, data, len);
212 	if (ret < 0)
213 		tst_brk(TBROK | TERRNO, "Failed 1st write to PTY");
214 	tst_res(TPASS, "Wrote PTY %s %d (1)", ldisc->name, ptmx);
215 
216 	ret = retry_async_write(ptmx, data, len);
217 	if (ret < 0)
218 		tst_brk(TBROK | TERRNO, "Failed 2nd write to PTY");
219 
220 	if (tcflush(ptmx, TCIFLUSH))
221 		tst_brk(TBROK | TERRNO, "tcflush(ptmx, TCIFLUSH)");
222 
223 	tst_res(TPASS, "Wrote PTY %s %d (2)", ldisc->name, ptmx);
224 
225 	ret = retry_async_read(ptmx, data, len);
226 	if (ret < 0)
227 		tst_brk(TBROK | TERRNO, "Failed read of PTY");
228 
229 	tst_res(TPASS, "Read PTY %s %d", ldisc->name, ptmx);
230 	TST_CHECKPOINT_WAKE(0);
231 
232 	while (1) {
233 		if (retry_async_read(ptmx, data, len) < 0)
234 			break;
235 
236 		if (retry_async_write(ptmx, data, len) < 0)
237 			break;
238 	}
239 
240 	tst_res(TPASS, "Transmission on PTY interrupted by hangup");
241 
242 	tst_free_all();
243 }
244 
open_netdev(const struct ldisc_info * ldisc)245 static void open_netdev(const struct ldisc_info *ldisc)
246 {
247 	struct ifreq ifreq = { 0 };
248 	struct sockaddr_ll lla = { 0 };
249 
250 	SAFE_IOCTL(pts, SIOCGIFNAME, ifreq.ifr_name);
251 	tst_res(TINFO, "Netdev is %s", ifreq.ifr_name);
252 
253 	sk = SAFE_SOCKET(PF_PACKET, SOCK_RAW, 0);
254 
255 	ifreq.ifr_mtu = ldisc->mtu;
256 	if (ioctl(sk, SIOCSIFMTU, &ifreq))
257 		tst_res(TWARN | TERRNO, "Failed to set netdev MTU to maximum");
258 	SAFE_IOCTL(sk, SIOCGIFMTU, &ifreq);
259 	mtu = ifreq.ifr_mtu;
260 	tst_res(TINFO, "Netdev MTU is %d (we set %d)", mtu, ldisc->mtu);
261 
262 	SAFE_IOCTL(sk, SIOCGIFFLAGS, &ifreq);
263 	ifreq.ifr_flags |= IFF_UP | IFF_RUNNING;
264 	SAFE_IOCTL(sk, SIOCSIFFLAGS, &ifreq);
265 	SAFE_IOCTL(sk, SIOCGIFFLAGS, &ifreq);
266 
267 	if (!(ifreq.ifr_flags & IFF_UP))
268 		tst_brk(TBROK, "Netdev did not come up");
269 
270 	SAFE_IOCTL(sk, SIOCGIFINDEX, &ifreq);
271 
272 	lla.sll_family = PF_PACKET;
273 	lla.sll_protocol = htons(ETH_P_ALL);
274 	lla.sll_ifindex = ifreq.ifr_ifindex;
275 	SAFE_BIND(sk, (struct sockaddr *)&lla, sizeof(struct sockaddr_ll));
276 
277 	tst_res(TINFO, "Bound netdev %d to socket %d", ifreq.ifr_ifindex, sk);
278 }
279 
check_data(const struct ldisc_info * ldisc,const char * data,ssize_t len)280 static void check_data(const struct ldisc_info *ldisc,
281 		       const char *data, ssize_t len)
282 {
283 	ssize_t i = 0, j;
284 	struct can_frame frm;
285 
286 	if (no_check)
287 		return;
288 
289 	if (ldisc->n == N_SLCAN) {
290 		memcpy(&frm, data, len);
291 
292 		if (frm.can_id != 1) {
293 			tst_res(TFAIL, "can_id = %d != 1",
294 				frm.can_id);
295 			no_check = 1;
296 		}
297 
298 		if (frm.can_dlc != CAN_MAX_DLEN) {
299 			tst_res(TFAIL, "can_dlc = %d != " str(CAN_MAX_DLEN),
300 				frm.can_dlc);
301 			no_check = 1;
302 		}
303 
304 		i = offsetof(struct can_frame, __pad);
305 		if (frm.__pad != frm.__res0 || frm.__res0 != frm.__res1) {
306 			tst_res_hexd(TFAIL, data + i,
307 				     offsetof(struct can_frame, data) - i,
308 				     "Padding bytes may contain stack data");
309 			no_check = 1;
310 		}
311 
312 		i = offsetof(struct can_frame, data);
313 	}
314 
315 	do {
316 		if (i >= len)
317 			return;
318 	} while (data[i++] == '_');
319 
320 	j = i--;
321 
322 	while (j < len && j - i < 65 && data[j++] != '_')
323 		;
324 	j--;
325 
326 	tst_res_hexd(TFAIL, data + i, j - i,
327 		     "Corrupt data (max 64 of %ld bytes shown): data[%ld..%ld] = ",
328 		     len, i, j);
329 	no_check = 1;
330 
331 	if (no_check)
332 		tst_res(TINFO, "Will continue test without data checking");
333 }
334 
try_sync_read(int fd,char * data,ssize_t size)335 static ssize_t try_sync_read(int fd, char *data, ssize_t size)
336 {
337 	ssize_t ret, n = 0;
338 	int retry = mtu;
339 
340 	while (retry--) {
341 		ret = read(fd, data + n, size - n);
342 
343 		if (ret < 0)
344 			return ret;
345 
346 		if ((n += ret) >= size)
347 			return ret;
348 	}
349 
350 	tst_brk(TBROK | TERRNO, "Only read %zd of %zd bytes", n, size);
351 
352 	return n;
353 }
354 
try_sync_write(int fd,const char * data,ssize_t size)355 static ssize_t try_sync_write(int fd, const char *data, ssize_t size)
356 {
357 	ssize_t ret, n = 0;
358 	int retry = mtu;
359 
360 	while (retry--) {
361 		ret = write(fd, data + n, size - n);
362 
363 		if (ret < 0)
364 			return ret;
365 
366 		if ((n += ret) >= size)
367 			return ret;
368 	}
369 
370 	tst_brk(TBROK | TERRNO, "Only wrote %zd of %zd bytes", n, size);
371 
372 	return n;
373 }
374 
read_netdev(const struct ldisc_info * ldisc)375 static void read_netdev(const struct ldisc_info *ldisc)
376 {
377 	int rlen, plen = 0;
378 	char *data;
379 
380 	switch (ldisc->n) {
381 	case N_SLIP:
382 		plen = mtu - 1;
383 		break;
384 	case N_SLCAN:
385 		plen = CAN_MTU;
386 		break;
387 	}
388 	data = tst_alloc(plen);
389 
390 	tst_res(TINFO, "Reading from socket %d", sk);
391 
392 	TEST(try_sync_read(sk, data, plen));
393 	if (TST_RET < 0)
394 		tst_brk(TBROK | TTERRNO, "Read netdev %s %d (1)", ldisc->name, sk);
395 	check_data(ldisc, data, plen);
396 	tst_res(TPASS, "Read netdev %s %d (1)", ldisc->name, sk);
397 
398 	TEST(try_sync_read(sk, data, plen));
399 	if (TST_RET < 0)
400 		tst_brk(TBROK | TTERRNO, "Read netdev %s %d (2)", ldisc->name, sk);
401 	check_data(ldisc, data, plen);
402 	tst_res(TPASS, "Read netdev %s %d (2)", ldisc->name, sk);
403 
404 	TEST(try_sync_write(sk, data, plen));
405 	if (TST_RET < 0)
406 		tst_brk(TBROK | TTERRNO, "Write netdev %s %d", ldisc->name, sk);
407 
408 	tst_res(TPASS, "Write netdev %s %d", ldisc->name, sk);
409 
410 	while (1) {
411 		if (try_sync_write(sk, data, plen) < 0)
412 			break;
413 
414 		if ((rlen = try_sync_read(sk, data, plen)) < 0)
415 			break;
416 		check_data(ldisc, data, rlen);
417 	}
418 
419 	tst_res(TPASS, "Data transmission on netdev interrupted by hangup");
420 
421 	close(sk);
422 	tst_free_all();
423 }
424 
do_test(unsigned int n)425 static void do_test(unsigned int n)
426 {
427 	struct ldisc_info *ldisc = &ldiscs[n];
428 
429 	if (open_pty(ldisc))
430 		return;
431 
432 	open_netdev(ldisc);
433 
434 	if (!SAFE_FORK()) {
435 		read_netdev(ldisc);
436 		return;
437 	}
438 
439 	if (!SAFE_FORK()) {
440 		do_pty(ldisc);
441 		return;
442 	}
443 
444 	if (!SAFE_FORK()) {
445 		TST_CHECKPOINT_WAIT2(0, 100000);
446 		SAFE_IOCTL(pts, TIOCVHANGUP);
447 		tst_res(TINFO, "Sent hangup ioctl to PTS");
448 		SAFE_IOCTL(ptmx, TIOCVHANGUP);
449 		tst_res(TINFO, "Sent hangup ioctl to PTM");
450 		return;
451 	}
452 
453 	tst_reap_children();
454 }
455 
cleanup(void)456 static void cleanup(void)
457 {
458 	ioctl(pts, TIOCVHANGUP);
459 	ioctl(ptmx, TIOCVHANGUP);
460 	close(sk);
461 
462 	tst_reap_children();
463 }
464 
465 static struct tst_test test = {
466 	.test = do_test,
467 	.cleanup = cleanup,
468 	.tcnt = 2,
469 	.forks_child = 1,
470 	.needs_checkpoints = 1,
471 	.needs_root = 1,
472 	.tags = (const struct tst_tag[]){
473 		{"linux-git", "b9258a2cece4ec1f020715fe3554bc2e360f6264"},
474 		{"CVE", "CVE-2020-11494"},
475 		{}
476 	}
477 };
478 
479 #else
480 
481 TST_TEST_TCONF("Need <linux/if_packet.h> and <linux/if_ether.h>");
482 
483 #endif
484