xref: /aosp_15_r20/external/ltp/testcases/kernel/pty/pty07.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2022 xiaoshoukui <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * The VT_DISALLOCATE ioctl can free a virtual console while VT_RESIZEX ioctl is
10*49cdfc7eSAndroid Build Coastguard Worker  * still running, causing a use-after-free in vt_ioctl(). Because VT_RESIZEX ioctl
11*49cdfc7eSAndroid Build Coastguard Worker  * have not make sure vc_cons[i].d is not NULL after grabbing console_lock().
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * Fixed by commit:
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *  commit 6cd1ed50efd88261298577cd92a14f2768eddeeb
16*49cdfc7eSAndroid Build Coastguard Worker  *  Author: Eric Dumazet <[email protected]>
17*49cdfc7eSAndroid Build Coastguard Worker  *  Date:   Mon Feb 10 11:07:21 2020 -0800
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  *    vt: vt_ioctl: fix race in VT_RESIZEX
20*49cdfc7eSAndroid Build Coastguard Worker  */
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <termios.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <linux/vt.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/ioctl.h"
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
32*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_stdio.h"
33*49cdfc7eSAndroid Build Coastguard Worker #include "tst_fuzzy_sync.h"
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker #define BUF_SIZE 256
36*49cdfc7eSAndroid Build Coastguard Worker #define MAX_NR_CONSOLES 63
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker static char tty_path[BUF_SIZE];
39*49cdfc7eSAndroid Build Coastguard Worker static int test_tty_port = 8;
40*49cdfc7eSAndroid Build Coastguard Worker static int fd = -1;
41*49cdfc7eSAndroid Build Coastguard Worker static struct tst_fzsync_pair fzp;
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker static struct vt_consize consize;
44*49cdfc7eSAndroid Build Coastguard Worker static unsigned short vt_active;
45*49cdfc7eSAndroid Build Coastguard Worker 
open_close(void * unused)46*49cdfc7eSAndroid Build Coastguard Worker static void *open_close(void *unused)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker 	int i;
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	while (tst_fzsync_run_b(&fzp)) {
51*49cdfc7eSAndroid Build Coastguard Worker 		tst_fzsync_start_race_b(&fzp);
52*49cdfc7eSAndroid Build Coastguard Worker 		for (i = test_tty_port; i < MAX_NR_CONSOLES; i++) {
53*49cdfc7eSAndroid Build Coastguard Worker 			ioctl(fd, VT_ACTIVATE, i);
54*49cdfc7eSAndroid Build Coastguard Worker 			ioctl(fd, VT_DISALLOCATE, i);
55*49cdfc7eSAndroid Build Coastguard Worker 		}
56*49cdfc7eSAndroid Build Coastguard Worker 		tst_fzsync_end_race_b(&fzp);
57*49cdfc7eSAndroid Build Coastguard Worker 	}
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 	return unused;
60*49cdfc7eSAndroid Build Coastguard Worker }
61*49cdfc7eSAndroid Build Coastguard Worker 
do_test(void)62*49cdfc7eSAndroid Build Coastguard Worker static void do_test(void)
63*49cdfc7eSAndroid Build Coastguard Worker {
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	tst_fzsync_pair_reset(&fzp, open_close);
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	while (tst_fzsync_run_a(&fzp)) {
68*49cdfc7eSAndroid Build Coastguard Worker 		tst_fzsync_start_race_a(&fzp);
69*49cdfc7eSAndroid Build Coastguard Worker 		ioctl(fd, VT_RESIZEX, &consize);
70*49cdfc7eSAndroid Build Coastguard Worker 		tst_fzsync_end_race_a(&fzp);
71*49cdfc7eSAndroid Build Coastguard Worker 		if (tst_taint_check()) {
72*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "Kernel is buggy");
73*49cdfc7eSAndroid Build Coastguard Worker 			break;
74*49cdfc7eSAndroid Build Coastguard Worker 		}
75*49cdfc7eSAndroid Build Coastguard Worker 	}
76*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "Did not crash with VT_RESIZE");
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)79*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
80*49cdfc7eSAndroid Build Coastguard Worker {
81*49cdfc7eSAndroid Build Coastguard Worker 	struct vt_stat stat;
82*49cdfc7eSAndroid Build Coastguard Worker 	struct winsize wsize;
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 	sprintf(tty_path, "/dev/tty%d", test_tty_port);
85*49cdfc7eSAndroid Build Coastguard Worker 	if (access(tty_path, F_OK))
86*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "TTY (/dev/tty%d) under test not available in system", test_tty_port);
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(tty_path, O_RDWR);
89*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(fd, VT_GETSTATE, &stat);
90*49cdfc7eSAndroid Build Coastguard Worker 	vt_active = stat.v_active;
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Saving active console %i", vt_active);
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(fd, TIOCGWINSZ, &wsize);
95*49cdfc7eSAndroid Build Coastguard Worker 	consize.v_rows = wsize.ws_row;
96*49cdfc7eSAndroid Build Coastguard Worker 	consize.v_cols = wsize.ws_col;
97*49cdfc7eSAndroid Build Coastguard Worker 	tst_fzsync_pair_init(&fzp);
98*49cdfc7eSAndroid Build Coastguard Worker }
99*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)100*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
101*49cdfc7eSAndroid Build Coastguard Worker {
102*49cdfc7eSAndroid Build Coastguard Worker 	tst_fzsync_pair_cleanup(&fzp);
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker 	if (fd >= 0) {
105*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "Restoring active console");
106*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_IOCTL(fd, VT_ACTIVATE, vt_active);
107*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
108*49cdfc7eSAndroid Build Coastguard Worker 	}
109*49cdfc7eSAndroid Build Coastguard Worker }
110*49cdfc7eSAndroid Build Coastguard Worker 
111*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
112*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = do_test,
113*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
114*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
115*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
116*49cdfc7eSAndroid Build Coastguard Worker 	.taint_check = TST_TAINT_W | TST_TAINT_D,
117*49cdfc7eSAndroid Build Coastguard Worker 	.max_runtime = 150,
118*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
119*49cdfc7eSAndroid Build Coastguard Worker 		{ "linux-git", "6cd1ed50efd8"},
120*49cdfc7eSAndroid Build Coastguard Worker 		{}
121*49cdfc7eSAndroid Build Coastguard Worker 	}
122*49cdfc7eSAndroid Build Coastguard Worker };
123