• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2018 Google, Inc.
4  */
5 
6 /*
7  * Regression test for commit 966031f340185 ("n_tty: fix EXTPROC vs ICANON
8  * interaction with TIOCINQ (aka FIONREAD)").  The test reproduces a hang
9  * (infinite loop in the kernel) after a pseudoterminal is put in both canonical
10  * (ICANON) and external processing (EXTPROC) mode, some data is written to the
11  * master and read from the slave, and the FIONREAD ioctl is called on the
12  * slave.  This is simplified from a syzkaller-generated reproducer.
13  */
14 
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <termio.h>
18 
19 #include "tst_test.h"
20 #include "lapi/termbits.h"
21 
do_test(void)22 static void do_test(void)
23 {
24 	struct termios io;
25 	int ptmx, pts;
26 	char c = 'A';
27 	int nbytes;
28 
29 	ptmx = SAFE_OPEN("/dev/ptmx", O_WRONLY);
30 
31 	if (tcgetattr(ptmx, &io) != 0)
32 		tst_brk(TBROK | TERRNO, "tcgetattr() failed");
33 
34 	io.c_lflag = EXTPROC | ICANON;
35 
36 	TEST(tcsetattr(ptmx, TCSANOW, &io));
37 	if (TST_RET == -1) {
38 		if (TST_ERR == EINVAL)
39 			tst_brk(TCONF, "tcsetattr(, , EXTPROC | ICANON) is not supported");
40 		tst_brk(TBROK | TERRNO, "tcsetattr() failed");
41 	}
42 
43 	if (unlockpt(ptmx) != 0)
44 		tst_brk(TBROK | TERRNO, "unlockpt() failed");
45 
46 	pts = SAFE_OPEN(ptsname(ptmx), O_RDONLY);
47 	/* write newline to ptmx to avoid read() on pts to block */
48 	SAFE_WRITE(1, ptmx, "A\n", 2);
49 	SAFE_READ(1, pts, &c, 1);
50 
51 	tst_res(TINFO, "Calling FIONREAD, this will hang in n_tty_ioctl() if the bug is present...");
52 	SAFE_IOCTL(pts, FIONREAD, &nbytes);
53 
54 	SAFE_CLOSE(ptmx);
55 	SAFE_CLOSE(pts);
56 
57 	tst_res(TPASS, "Got to the end without hanging");
58 }
59 
60 static struct tst_test test = {
61 	.test_all = do_test,
62 	.tags = (const struct tst_tag[]) {
63 		{"linux-git", "966031f34018"},
64 		{}
65 	}
66 };
67