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 * Test based on Syzkaller reproducer:
10*49cdfc7eSAndroid Build Coastguard Worker * https://syzkaller.appspot.com/bug?extid=522643ab5729b0421998
11*49cdfc7eSAndroid Build Coastguard Worker *
12*49cdfc7eSAndroid Build Coastguard Worker * The VT_DISALLOCATE ioctl can free a virtual console while tty_release() is
13*49cdfc7eSAndroid Build Coastguard Worker * still running, causing a use-after-free in con_shutdown(). This occurs
14*49cdfc7eSAndroid Build Coastguard Worker * because VT_DISALLOCATE only considers a virtual console to be in-use if it
15*49cdfc7eSAndroid Build Coastguard Worker * has a tty_struct with count > 0. But actually when count == 0, the tty is
16*49cdfc7eSAndroid Build Coastguard Worker * still in the process of being closed.
17*49cdfc7eSAndroid Build Coastguard Worker *
18*49cdfc7eSAndroid Build Coastguard Worker * Fixed by commit:
19*49cdfc7eSAndroid Build Coastguard Worker *
20*49cdfc7eSAndroid Build Coastguard Worker * commit ca4463bf8438b403596edd0ec961ca0d4fbe0220
21*49cdfc7eSAndroid Build Coastguard Worker * Author: Eric Biggers <[email protected]>
22*49cdfc7eSAndroid Build Coastguard Worker * Date: Sat Mar 21 20:43:04 2020 -0700
23*49cdfc7eSAndroid Build Coastguard Worker *
24*49cdfc7eSAndroid Build Coastguard Worker * vt: vt_ioctl: fix VT_DISALLOCATE freeing in-use virtual console
25*49cdfc7eSAndroid Build Coastguard Worker */
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <termios.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <linux/vt.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/ioctl.h"
35*49cdfc7eSAndroid Build Coastguard Worker
36*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
37*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_stdio.h"
38*49cdfc7eSAndroid Build Coastguard Worker #include "tst_fuzzy_sync.h"
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker #define BUF_SIZE 256
41*49cdfc7eSAndroid Build Coastguard Worker static char tty_path_a[BUF_SIZE];
42*49cdfc7eSAndroid Build Coastguard Worker static char tty_path_b[BUF_SIZE];
43*49cdfc7eSAndroid Build Coastguard Worker static int test_tty_port = 8;
44*49cdfc7eSAndroid Build Coastguard Worker static struct tst_fzsync_pair fzp;
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 while (tst_fzsync_run_b(&fzp)) {
49*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_start_race_b(&fzp);
50*49cdfc7eSAndroid Build Coastguard Worker int fd = SAFE_OPEN(tty_path_b, O_RDWR);
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
53*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_end_race_b(&fzp);
54*49cdfc7eSAndroid Build Coastguard Worker }
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker return unused;
57*49cdfc7eSAndroid Build Coastguard Worker }
58*49cdfc7eSAndroid Build Coastguard Worker
do_test(void)59*49cdfc7eSAndroid Build Coastguard Worker static void do_test(void)
60*49cdfc7eSAndroid Build Coastguard Worker {
61*49cdfc7eSAndroid Build Coastguard Worker int fd = SAFE_OPEN(tty_path_a, O_RDWR);
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_pair_reset(&fzp, open_close);
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker while (tst_fzsync_run_a(&fzp)) {
66*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_start_race_a(&fzp);
67*49cdfc7eSAndroid Build Coastguard Worker ioctl(fd, VT_DISALLOCATE, test_tty_port);
68*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_end_race_a(&fzp);
69*49cdfc7eSAndroid Build Coastguard Worker if (tst_taint_check()) {
70*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Kernel is vulnerable");
71*49cdfc7eSAndroid Build Coastguard Worker return;
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker }
74*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
75*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Did not crash with VT_DISALLOCATE");
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker
setup(void)78*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
79*49cdfc7eSAndroid Build Coastguard Worker {
80*49cdfc7eSAndroid Build Coastguard Worker sprintf(tty_path_a, "/dev/tty%d", test_tty_port + 1);
81*49cdfc7eSAndroid Build Coastguard Worker sprintf(tty_path_b, "/dev/tty%d", test_tty_port);
82*49cdfc7eSAndroid Build Coastguard Worker
83*49cdfc7eSAndroid Build Coastguard Worker if (access(tty_path_a, F_OK) || access(tty_path_b, F_OK))
84*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TCONF, "TTY(s) under test not available in system");
85*49cdfc7eSAndroid Build Coastguard Worker
86*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_pair_init(&fzp);
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)89*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
90*49cdfc7eSAndroid Build Coastguard Worker {
91*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_pair_cleanup(&fzp);
92*49cdfc7eSAndroid Build Coastguard Worker }
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
95*49cdfc7eSAndroid Build Coastguard Worker .test_all = do_test,
96*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
97*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
98*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
99*49cdfc7eSAndroid Build Coastguard Worker .taint_check = TST_TAINT_W | TST_TAINT_D,
100*49cdfc7eSAndroid Build Coastguard Worker .max_runtime = 150,
101*49cdfc7eSAndroid Build Coastguard Worker .tags = (const struct tst_tag[]) {
102*49cdfc7eSAndroid Build Coastguard Worker {"CVE", "2020-36557"},
103*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "ca4463bf8438"},
104*49cdfc7eSAndroid Build Coastguard Worker {}
105*49cdfc7eSAndroid Build Coastguard Worker }
106*49cdfc7eSAndroid Build Coastguard Worker };
107