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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/* $OpenBSD: manager.c,v 1.7 2017/12/15 14:45:51 bluhm Exp $ */
/*
* Copyright (c) 2015 Sebastien Marie <semarie@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/syslimits.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <regex.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "manager.h"
extern char *__progname;
static const char *
coredump_name()
{
static char coredump[PATH_MAX] = "";
if (*coredump)
return (coredump);
if (strlcpy(coredump, __progname, sizeof(coredump)) >= sizeof(coredump))
errx(1, "coredump: strlcpy");
if (strlcat(coredump, ".core", sizeof(coredump)) >= sizeof(coredump))
errx(1, "coredump: strlcat");
return (coredump);
}
static int
check_coredump()
{
const char *coredump = coredump_name();
int fd;
if ((fd = open(coredump, O_RDONLY)) == -1) {
if (errno == ENOENT)
return (1); /* coredump not found */
else
return (-1); /* error */
}
(void)close(fd);
return (0); /* coredump found */
}
static int
clear_coredump(int *ret, const char *test_name)
{
int saved_errno = errno;
int u;
if (((u = unlink(coredump_name())) != 0) && (errno != ENOENT)) {
warn("test(%s): clear_coredump", test_name);
*ret = EXIT_FAILURE;
return (-1);
}
errno = saved_errno;
return (0);
}
static int
grab_syscall(pid_t pid)
{
int ret = -1;
char *pattern;
regex_t regex;
regmatch_t matches[2];
FILE *fd;
char line[1024];
int error;
const char *errstr;
/* build searched string */
error = asprintf(&pattern,
"^%s\\[%d\\]: pledge \"[a-z]+\", syscall ([0-9]+)\n?$",
__progname, pid);
if (error <= 0) {
warn("asprintf pattern");
return (-1);
}
error = regcomp(®ex, pattern, REG_EXTENDED);
if (error) {
warnx("regcomp pattern=%s error=%d", pattern, error);
free(pattern);
return (-1);
}
if (regex.re_nsub != 1) {
warnx("regcomp pattern=%s nsub=%zu", pattern, regex.re_nsub);
goto out;
}
/* call dmesg */
if ((fd = popen("/sbin/dmesg", "r")) == NULL) {
warn("popen /sbin/dmesg");
goto out;
}
/* search the string */
while (1) {
/* read a line */
fgets(line, sizeof(line), fd);
/* error checking */
if (ferror(fd)) {
ret = -1;
goto out;
}
/* quit */
if (feof(fd))
break;
/* check if found */
error = regexec(®ex, line, 2, matches, 0);
if (error == REG_NOMATCH)
continue;
if (error) {
warnx("regexec pattern=%s line=%s error=%d",
pattern, line, error);
ret = -1;
goto out;
}
/* convert it */
line[matches[1].rm_eo] = '\0';
ret = strtonum(&line[matches[1].rm_so], 0, 255, &errstr);
if (errstr) {
warnx("strtonum: number=%s error=%s",
&line[matches[1].rm_so], errstr);
ret = -1;
goto out;
}
}
/* cleanup */
if (pclose(fd) == -1)
goto out;
/* not found */
if (ret == -1)
ret = 0;
out:
free(pattern);
regfree(®ex);
return (ret);
}
/* mainly stolen from src/bin/cat/cat.c */
static int
drainfd(int rfd, int wfd)
{
char buf[1024];
ssize_t nr, nw, off;
while ((nr = read(rfd, buf, sizeof(buf))) != -1 && nr != 0)
for (off = 0; nr; nr -= nw, off += nw)
if ((nw = write(wfd, buf + off, (size_t)nr)) == 0 ||
nw == -1)
return (-1);
if (nr < 0)
return (-1);
return (0);
}
void
_start_test(int *ret, const char *test_name, const char *request,
void (*test_func)(void))
{
int fildes[2];
pid_t pid;
int status;
/* early print testname */
printf("test(%s): pledge=", test_name);
if (request) {
printf("(\"%s\",", request);
printf("NULL)");
} else
printf("skip");
/* unlink previous coredump (if exists) */
if (clear_coredump(ret, test_name) == -1)
return;
/* flush outputs (for STDOUT_FILENO manipulation) */
if (fflush(NULL) != 0) {
warn("test(%s) fflush", test_name);
*ret = EXIT_FAILURE;
return;
}
/* make pipe to grab output */
if (pipe(fildes) != 0) {
warn("test(%s) pipe", test_name);
*ret = EXIT_FAILURE;
return;
}
/* fork and launch the test */
switch (pid = fork()) {
case -1:
(void)close(fildes[0]);
(void)close(fildes[1]);
warn("test(%s) fork", test_name);
*ret = EXIT_FAILURE;
return;
case 0:
/* output to pipe */
(void)close(fildes[0]);
while (dup2(fildes[1], STDOUT_FILENO) == -1)
if (errno != EINTR)
err(errno, "dup2");
/* create a new session (for kill) */
setsid();
/* set pledge policy */
if (request && pledge(request, NULL) != 0)
err(errno, "pledge");
/* reset errno and launch test */
errno = 0;
test_func();
if (errno != 0)
_exit(errno);
_exit(EXIT_SUCCESS);
/* NOTREACHED */
}
/* copy pipe to output */
(void)close(fildes[1]);
if (drainfd(fildes[0], STDOUT_FILENO) != 0) {
warn("test(%s): drainfd", test_name);
*ret = EXIT_FAILURE;
return;
}
if (close(fildes[0]) != 0) {
warn("test(%s): close", test_name);
*ret = EXIT_FAILURE;
return;
}
/* wait for test to terminate */
while (waitpid(pid, &status, 0) < 0) {
if (errno == EAGAIN)
continue;
warn("test(%s): waitpid", test_name);
*ret = EXIT_FAILURE;
return;
}
/* show status and details */
printf(" status=%d", status);
if (WIFCONTINUED(status))
printf(" continued");
if (WIFEXITED(status)) {
int e = WEXITSTATUS(status);
printf(" exit=%d", e);
if (e > 0 && e <= ELAST)
printf(" (errno: \"%s\")", strerror(e));
}
if (WIFSIGNALED(status)) {
int signal = WTERMSIG(status);
printf(" signal=%d", signal);
/* check if core file is really here ? */
if (WCOREDUMP(status)) {
int coredump = check_coredump();
switch(coredump) {
case -1: /* error */
warn("test(%s): check_coredump", test_name);
*ret = EXIT_FAILURE;
return;
case 0: /* found */
printf(" coredump=present");
break;
case 1: /* not found */
printf(" coredump=absent");
break;
default:
warnx("test(%s): unknown coredump code %d",
test_name, coredump);
*ret = EXIT_FAILURE;
return;
}
}
/* grab pledged syscall from dmesg */
if ((signal == SIGKILL) || (signal = SIGABRT)) {
int syscall = grab_syscall(pid);
switch (syscall) {
case -1: /* error */
warn("test(%s): grab_syscall pid=%d", test_name,
pid);
*ret = EXIT_FAILURE;
return;
case 0: /* not found */
printf(" pledged_syscall=not_found");
break;
default:
printf(" pledged_syscall=%d", syscall);
}
}
}
if (WIFSTOPPED(status))
printf(" stop=%d", WSTOPSIG(status));
printf("\n");
}
|