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
|
/* $OpenBSD: main.c,v 1.6 2015/09/27 17:55:39 semarie 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/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "manager.h"
static void
test_nop()
{
/* nop */
}
static void
test_inet()
{
int fd = socket(AF_INET, SOCK_STREAM, 0);
int saved_errno = errno;
close(fd);
errno = saved_errno ? saved_errno : errno;
}
static void
test_kill()
{
kill(0, SIGINT);
}
static void
test_allowed_syscalls()
{
clock_getres(CLOCK_MONOTONIC, NULL);
clock_gettime(CLOCK_MONOTONIC, NULL);
/* fchdir(); */
getdtablecount();
getegid();
geteuid();
getgid();
getgroups(0, NULL);
getitimer(ITIMER_REAL, NULL);
getlogin();
getpgid(0);
getpgrp();
getpid();
getppid();
/* getresgid(); */
/* getresuid(); */
{ struct rlimit rl; getrlimit(RLIMIT_CORE, &rl); }
getsid(0);
getthrid();
{ struct timeval tp; gettimeofday(&tp, NULL); }
getuid();
geteuid();
issetugid();
/* nanosleep(); */
/* sigreturn(); */
umask(0000);
/* wait4(); */
}
static void
open_close(const char *filename)
{
int fd;
int saved_errno;
errno = 0;
printf("\n open_close(\"%s\")", filename);
fd = open(filename, O_RDONLY);
saved_errno = errno;
printf(" fd=%d errno=%d", fd, errno);
if (fd != -1)
close(fd);
errno = saved_errno;
}
static void
test_wpaths()
{
/* absolute file */
open_close("/etc/passwd");
/* relative */
open_close("generic");
/* relative */
open_close("../../../../../../../../../../../../../../../etc/passwd");
/* ENOENT */
open_close("/nonexistent");
/* calling exit to flush stdout */
printf("\n");
exit(EXIT_SUCCESS);
}
static void
test_tame()
{
const char *wpaths[] = { "/sbin", NULL };
if (tame("stdio rpath", wpaths) != 0)
_exit(errno);
}
static void
do_stat(const char *path)
{
char resolved[PATH_MAX];
struct stat sb;
printf("\n stat(\"%s\"):", path);
/* call realpath(3) */
errno = 0;
if (realpath(path, resolved) != NULL)
printf(" realpath=\"%s\"", resolved);
else
printf(" realpath=failed(%d)", errno);
/* call stat(2) */
errno = 0;
if (stat(path, &sb) == 0)
printf(" uid=%d gid=%d mode=%04o", sb.st_uid, sb.st_gid,
sb.st_mode);
else
printf(" errno=%d", errno);
}
static void
test_stat()
{
/* in whitelisted path */
do_stat("/usr/share/man/man8/afterboot.8");
do_stat("/usr/share/man/man8/");
do_stat("/usr/share/man");
/* parent of whitelisted path */
do_stat("/usr/share");
do_stat("/usr");
do_stat("/");
/* outside whitelisted path */
do_stat("/usr/bin/gzip");
/* calling exit to flush stdout */
printf("\n");
exit(EXIT_SUCCESS);
}
int
main(int argc, char *argv[])
{
int ret = EXIT_SUCCESS;
if (argc != 1)
errx(1, "usage: %s", argv[0]);
/*
* testsuite
*/
/* _exit is always allowed, and nothing else under flags=0 */
start_test(&ret, "", NULL, test_nop);
start_test(&ret, "", NULL, test_inet);
/* test coredump */
start_test(&ret, "abort", NULL, test_inet);
/* inet under inet is ok */
start_test(&ret, "inet", NULL, test_inet);
/* kill under inet is forbidden */
start_test(&ret, "inet", NULL, test_kill);
/* kill under proc is allowed */
start_test(&ret, "proc", NULL, test_kill);
/* tests TAME_SELF for permitted syscalls */
start_test(&ret, "malloc", NULL, test_allowed_syscalls);
start_test(&ret, "rw", NULL, test_allowed_syscalls);
start_test(&ret, "stdio", NULL, test_allowed_syscalls);
start_test(&ret, "rpath", NULL, test_allowed_syscalls);
start_test(&ret, "wpath", NULL, test_allowed_syscalls);
start_test(&ret, "tmppath", NULL, test_allowed_syscalls);
start_test(&ret, "inet", NULL, test_allowed_syscalls);
start_test(&ret, "unix", NULL, test_allowed_syscalls);
start_test(&ret, "cmsg", NULL, test_allowed_syscalls);
start_test(&ret, "dns", NULL, test_allowed_syscalls);
start_test(&ret, "getpw", NULL, test_allowed_syscalls);
/* tests req without TAME_SELF for "permitted syscalls" */
// XXX it is a documentation bug
start_test(&ret, "ioctl", NULL, test_allowed_syscalls);
start_test(&ret, "proc", NULL, test_allowed_syscalls);
start_test(&ret, "cpath", NULL, test_allowed_syscalls);
start_test(&ret, "abort", NULL, test_allowed_syscalls);
start_test(&ret, "fattr", NULL, test_allowed_syscalls);
/*
* test whitelist path
*/
start_test(&ret, "stdio rpath", NULL, test_wpaths);
start_test1(&ret, "stdio rpath", NULL, test_wpaths);
// XXX start_test1(&ret, "stdio rpath", "/", test_wpaths);
start_test1(&ret, "stdio rpath", "/etc", test_wpaths);
start_test1(&ret, "stdio rpath", "/etc/", test_wpaths);
start_test1(&ret, "stdio rpath", "/etc/passwd", test_wpaths);
// XXX start_test1(&ret, "stdio rpath", "/etc/passwd/", test_wpaths);
start_test1(&ret, "stdio rpath", "/bin", test_wpaths);
start_test1(&ret, "stdio rpath", "generic", test_wpaths);
start_test1(&ret, "stdio rpath", "", test_wpaths);
start_test1(&ret, "stdio rpath", ".", test_wpaths);
/*
* test tame(2) arguments
*/
/* same request */
start_test(&ret, "stdio rpath", NULL, test_tame);
/* same request (stdio = malloc rw) */
start_test(&ret, "malloc rw rpath", NULL, test_tame);
/* reduce request */
start_test(&ret, "stdio rpath wpath", NULL, test_tame);
/* reduce request (with same/other wpaths) */
start_test1(&ret, "stdio rpath wpath", "/sbin", test_tame);
start_test1(&ret, "stdio rpath wpath", "/", test_tame);
/* add request */
start_test(&ret, "stdio", NULL, test_tame);
/* change request */
start_test(&ret, "cmsg", NULL, test_tame);
/* test stat(2) */
start_test1(&ret, "stdio rpath", "/usr/share/man", test_stat);
return (ret);
}
|