1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
/* $OpenBSD: kqueue-regress.c,v 1.4 2020/03/08 09:40:52 visa Exp $ */
/*
* Written by Anton Lindqvist <anton@openbsd.org> 2018 Public Domain
*/
#include <sys/types.h>
#include <sys/event.h>
#include <sys/resource.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <assert.h>
#include <err.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "main.h"
static int do_regress1(void);
static int do_regress2(void);
static int do_regress3(void);
static int do_regress4(void);
static int do_regress5(void);
static void make_chain(int);
int
do_regress(int n)
{
switch (n) {
case 1:
return do_regress1();
case 2:
return do_regress2();
case 3:
return do_regress3();
case 4:
return do_regress4();
case 5:
return do_regress5();
default:
errx(1, "unknown regress test number %d", n);
}
}
/*
* Regression test for NULL-deref in knote_processexit().
*/
static int
do_regress1(void)
{
struct kevent kev[2];
int kq;
ASS((kq = kqueue()) >= 0,
warn("kqueue"));
EV_SET(&kev[0], kq, EVFILT_READ, EV_ADD, 0, 0, NULL);
EV_SET(&kev[1], SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
ASS(kevent(kq, kev, 2, NULL, 0, NULL) == 0,
warn("can't register events on kqueue"));
/* kq intentionally left open */
return 0;
}
/*
* Regression test for use-after-free in kqueue_close().
*/
static int
do_regress2(void)
{
pid_t pid;
int i, status;
/* Run twice in order to trigger the panic faster, if still present. */
for (i = 0; i < 2; i++) {
pid = fork();
if (pid == -1)
err(1, "fork");
if (pid == 0) {
struct kevent kev[1];
int p0[2], p1[2];
int kq;
if (pipe(p0) == -1)
err(1, "pipe");
if (pipe(p1) == -1)
err(1, "pipe");
kq = kqueue();
if (kq == -1)
err(1, "kqueue");
EV_SET(&kev[0], p0[0], EVFILT_READ, EV_ADD, 0, 0, NULL);
if (kevent(kq, kev, 1, NULL, 0, NULL) == -1)
err(1, "kevent");
EV_SET(&kev[0], p1[1], EVFILT_READ, EV_ADD, 0, 0, NULL);
if (kevent(kq, kev, 1, NULL, 0, NULL) == -1)
err(1, "kevent");
EV_SET(&kev[0], p1[0], EVFILT_READ, EV_ADD, 0, 0, NULL);
if (kevent(kq, kev, 1, NULL, 0, NULL) == -1)
err(1, "kevent");
_exit(0);
}
if (waitpid(pid, &status, 0) == -1)
err(1, "waitpid");
assert(WIFEXITED(status));
assert(WEXITSTATUS(status) == 0);
}
return 0;
}
/*
* Regression test for kernel stack exhaustion.
*/
static int
do_regress3(void)
{
pid_t pid;
int dir, status;
for (dir = 0; dir < 2; dir++) {
pid = fork();
if (pid == -1)
err(1, "fork");
if (pid == 0) {
make_chain(dir);
_exit(0);
}
if (waitpid(pid, &status, 0) == -1)
err(1, "waitpid");
assert(WIFEXITED(status));
assert(WEXITSTATUS(status) == 0);
}
return 0;
}
static void
make_chain(int dir)
{
struct kevent kev[1];
int i, kq, prev;
/*
* Build a chain of kqueues and leave the files open.
* If the chain is long enough and properly oriented, a broken kernel
* can exhaust the stack when this process exits.
*/
for (i = 0, prev = -1; i < 120; i++, prev = kq) {
kq = kqueue();
if (kq == -1)
err(1, "kqueue");
if (prev == -1)
continue;
if (dir == 0) {
EV_SET(&kev[0], prev, EVFILT_READ, EV_ADD, 0, 0, NULL);
if (kevent(kq, kev, 1, NULL, 0, NULL) == -1)
err(1, "kevent");
} else {
EV_SET(&kev[0], kq, EVFILT_READ, EV_ADD, 0, 0, NULL);
if (kevent(prev, kev, 1, NULL, 0, NULL) == -1)
err(1, "kevent");
}
}
}
/*
* Regression test for kernel stack exhaustion.
*/
static int
do_regress4(void)
{
static const int nkqueues = 500;
struct kevent kev[1];
struct rlimit rlim;
struct timespec ts;
int fds[2], i, kq = -1, prev;
if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
err(1, "getrlimit");
if (rlim.rlim_cur < nkqueues + 8) {
rlim.rlim_cur = nkqueues + 8;
if (setrlimit(RLIMIT_NOFILE, &rlim) == -1) {
printf("RLIMIT_NOFILE is too low and can't raise it\n");
printf("SKIPPED\n");
exit(0);
}
}
if (pipe(fds) == -1)
err(1, "pipe");
/* Build a chain of kqueus. The first kqueue refers to the pipe. */
for (i = 0, prev = fds[0]; i < nkqueues; i++, prev = kq) {
kq = kqueue();
if (kq == -1)
err(1, "kqueue");
EV_SET(&kev[0], prev, EVFILT_READ, EV_ADD, 0, 0, NULL);
if (kevent(kq, kev, 1, NULL, 0, NULL) == -1)
err(1, "kevent");
}
/*
* Trigger a cascading event through the chain.
* If the chain is long enough, a broken kernel can run out
* of kernel stack space.
*/
write(fds[1], "x", 1);
/*
* Check that the event gets propagated.
* The propagation is not instantaneous, so allow a brief pause.
*/
ts.tv_sec = 5;
ts.tv_nsec = 0;
assert(kevent(kq, NULL, 0, kev, 1, NULL) == 1);
return 0;
}
/*
* Regression test for select and poll with kqueue.
*/
static int
do_regress5(void)
{
fd_set fdset;
struct kevent kev[1];
struct pollfd pfd[1];
struct timeval tv;
int fds[2], kq, ret;
if (pipe(fds) == -1)
err(1, "pipe");
kq = kqueue();
if (kq == -1)
err(1, "kqueue");
EV_SET(&kev[0], fds[0], EVFILT_READ, EV_ADD, 0, 0, NULL);
if (kevent(kq, kev, 1, NULL, 0, NULL) == -1)
err(1, "kevent");
/* Check that no event is reported. */
FD_ZERO(&fdset);
FD_SET(kq, &fdset);
tv.tv_sec = 0;
tv.tv_usec = 0;
ret = select(kq + 1, &fdset, NULL, NULL, &tv);
if (ret == -1)
err(1, "select");
assert(ret == 0);
pfd[0].fd = kq;
pfd[0].events = POLLIN;
pfd[0].revents = 0;
ret = poll(pfd, 1, 0);
if (ret == -1)
err(1, "poll");
assert(ret == 0);
/* Trigger an event. */
write(fds[1], "x", 1);
/* Check that the event gets reported. */
FD_ZERO(&fdset);
FD_SET(kq, &fdset);
tv.tv_sec = 5;
tv.tv_usec = 0;
ret = select(kq + 1, &fdset, NULL, NULL, &tv);
if (ret == -1)
err(1, "select");
assert(ret == 1);
assert(FD_ISSET(kq, &fdset));
pfd[0].fd = kq;
pfd[0].events = POLLIN;
pfd[0].revents = 0;
ret = poll(pfd, 1, 5000);
if (ret == -1)
err(1, "poll");
assert(ret == 1);
assert(pfd[0].revents & POLLIN);
return 0;
}
|