1 |
/* |
2 |
* tomoyo_file_test.c |
3 |
* |
4 |
* Testing program for fs/tomoyo_file.c |
5 |
* |
6 |
* Copyright (C) 2005-2008 NTT DATA CORPORATION |
7 |
* |
8 |
* Version: 1.6.1-rc 2008/04/27 |
9 |
* |
10 |
*/ |
11 |
#include "include.h" |
12 |
|
13 |
static int profile_fd = EOF; |
14 |
static int domain_fd = EOF; |
15 |
static int exception_fd = EOF; |
16 |
static const char *policy = ""; |
17 |
static char self_domain[4096] = ""; |
18 |
|
19 |
static int write_policy(void) { |
20 |
FILE *fp; |
21 |
char buffer[8192]; |
22 |
char *cp; |
23 |
int domain_found = 0; |
24 |
int policy_found = 0; |
25 |
memset(buffer, 0, sizeof(buffer)); |
26 |
cp = "255-MAC_FOR_FILE=disabled\n"; |
27 |
write(profile_fd, cp, strlen(cp)); |
28 |
fp = fopen(proc_policy_domain_policy, "r"); |
29 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
30 |
write(profile_fd, cp, strlen(cp)); |
31 |
write(domain_fd, policy, strlen(policy)); |
32 |
write(domain_fd, "\n", 1); |
33 |
if (!fp) { |
34 |
printf("%s : BUG: policy read failed\n", policy); |
35 |
return 0; |
36 |
} |
37 |
while (fgets(buffer, sizeof(buffer) - 1, fp)) { |
38 |
cp = strchr(buffer, '\n'); |
39 |
if (cp) *cp = '\0'; |
40 |
if (!strncmp(buffer, "<kernel>", 8)) domain_found = !strcmp(self_domain, buffer); |
41 |
if (domain_found) { |
42 |
//printf("<%s>\n", buffer); |
43 |
if (!strcmp(buffer, policy)) { |
44 |
policy_found = 1; |
45 |
break; |
46 |
} |
47 |
} |
48 |
} |
49 |
fclose(fp); |
50 |
if (!policy_found) { |
51 |
printf("%s : BUG: policy write failed\n", policy); |
52 |
return 0; |
53 |
} |
54 |
errno = 0; |
55 |
return 1; |
56 |
} |
57 |
|
58 |
static void delete_policy(void) { |
59 |
write(domain_fd, "delete ", 7); |
60 |
write(domain_fd, policy, strlen(policy)); |
61 |
write(domain_fd, "\n", 1); |
62 |
} |
63 |
|
64 |
static void show_result(int result, char should_success) { |
65 |
int err = errno; |
66 |
printf("%s : ", policy); |
67 |
if (should_success) { |
68 |
if (result != EOF) printf("OK\n"); |
69 |
else printf("FAILED: %s\n", strerror(err)); |
70 |
} else { |
71 |
if (result == EOF) { |
72 |
if (err == EPERM) printf("OK: Permission denied.\n"); |
73 |
else printf("FAILED: %s\n", strerror(err)); |
74 |
} else { |
75 |
printf("BUG: didn't fail.\n"); |
76 |
} |
77 |
} |
78 |
} |
79 |
|
80 |
static void create2(const char *pathname) { |
81 |
const char *cp = "255-MAC_FOR_FILE=disabled\n"; |
82 |
write(profile_fd, cp, strlen(cp)); |
83 |
close(creat(pathname, 0600)); |
84 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
85 |
write(profile_fd, cp, strlen(cp)); |
86 |
errno = 0; |
87 |
} |
88 |
|
89 |
static void mkdir2(const char *pathname) { |
90 |
const char *cp = "255-MAC_FOR_FILE=disabled\n"; |
91 |
write(profile_fd, cp, strlen(cp)); |
92 |
mkdir(pathname, 0600); |
93 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
94 |
write(profile_fd, cp, strlen(cp)); |
95 |
errno = 0; |
96 |
} |
97 |
|
98 |
static void unlink2(const char *pathname) { |
99 |
const char *cp = "255-MAC_FOR_FILE=disabled\n"; |
100 |
write(profile_fd, cp, strlen(cp)); |
101 |
unlink(pathname); |
102 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
103 |
write(profile_fd, cp, strlen(cp)); |
104 |
errno = 0; |
105 |
} |
106 |
|
107 |
static void rmdir2(const char *pathname) { |
108 |
const char *cp = "255-MAC_FOR_FILE=disabled\n"; |
109 |
write(profile_fd, cp, strlen(cp)); |
110 |
rmdir(pathname); |
111 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
112 |
write(profile_fd, cp, strlen(cp)); |
113 |
errno = 0; |
114 |
} |
115 |
|
116 |
static void StageFileTest(void) { |
117 |
char *filename = ""; |
118 |
policy = "allow_read /proc/sys/net/ipv4/ip_local_port_range"; |
119 |
if (write_policy()) { |
120 |
static int name[] = { CTL_NET, NET_IPV4, NET_IPV4_LOCAL_PORT_RANGE }; |
121 |
int buffer[2] = { 32768, 61000 }; |
122 |
size_t size = sizeof(buffer); |
123 |
show_result(sysctl(name, 3, buffer, &size, 0, 0), 1); |
124 |
delete_policy(); |
125 |
show_result(sysctl(name, 3, buffer, &size, 0, 0), 0); |
126 |
} |
127 |
policy = "allow_write /proc/sys/net/ipv4/ip_local_port_range"; |
128 |
if (write_policy()) { |
129 |
static int name[] = { CTL_NET, NET_IPV4, NET_IPV4_LOCAL_PORT_RANGE }; |
130 |
int buffer[2] = { 32768, 61000 }; |
131 |
size_t size = sizeof(buffer); |
132 |
show_result(sysctl(name, 3, 0, 0, buffer, size), 1); |
133 |
delete_policy(); |
134 |
show_result(sysctl(name, 3, 0, 0, buffer, size), 0); |
135 |
} |
136 |
policy = "allow_read/write /proc/sys/net/ipv4/ip_local_port_range"; |
137 |
if (write_policy()) { |
138 |
static int name[] = { CTL_NET, NET_IPV4, NET_IPV4_LOCAL_PORT_RANGE }; |
139 |
int buffer[2] = { 32768, 61000 }; |
140 |
size_t size = sizeof(buffer); |
141 |
show_result(sysctl(name, 3, buffer, &size, buffer, size), 1); |
142 |
delete_policy(); |
143 |
show_result(sysctl(name, 3, buffer, &size, buffer, size), 0); |
144 |
} |
145 |
|
146 |
policy = "allow_read /bin/true"; |
147 |
if (write_policy()) { |
148 |
show_result(uselib("/bin/true"), 1); |
149 |
delete_policy(); |
150 |
show_result(uselib("/bin/true"), 0); |
151 |
} |
152 |
|
153 |
policy = "allow_execute /bin/true"; |
154 |
if (write_policy()) { |
155 |
int pipe_fd[2] = { EOF, EOF }; |
156 |
int err = 0; |
157 |
fflush(stdout); fflush(stderr); |
158 |
pipe(pipe_fd); |
159 |
if (fork() == 0) { |
160 |
execl("/bin/true", "/bin/true", NULL); |
161 |
err = errno; |
162 |
write(pipe_fd[1], &err, sizeof(err)); |
163 |
_exit(0); |
164 |
} |
165 |
close(pipe_fd[1]); |
166 |
read(pipe_fd[0], &err, sizeof(err)); |
167 |
close(pipe_fd[0]); |
168 |
wait(NULL); |
169 |
errno = err; |
170 |
show_result(err ? EOF : 0, 1); |
171 |
delete_policy(); |
172 |
fflush(stdout); fflush(stderr); |
173 |
pipe(pipe_fd); |
174 |
if (fork() == 0) { |
175 |
execl("/bin/true", "/bin/true", NULL); |
176 |
err = errno; |
177 |
write(pipe_fd[1], &err, sizeof(err)); |
178 |
_exit(0); |
179 |
} |
180 |
close(pipe_fd[1]); |
181 |
read(pipe_fd[0], &err, sizeof(err)); |
182 |
close(pipe_fd[0]); |
183 |
wait(NULL); |
184 |
errno = err; |
185 |
show_result(err ? EOF : 0, 0); |
186 |
} |
187 |
|
188 |
policy = "allow_read /dev/null"; |
189 |
if (write_policy()) { |
190 |
int fd = open("/dev/null", O_RDONLY); |
191 |
show_result(fd, 1); |
192 |
if (fd != EOF) close(fd); |
193 |
delete_policy(); |
194 |
fd = open("/dev/null", O_RDONLY); |
195 |
show_result(fd, 0); |
196 |
if (fd != EOF) close(fd); |
197 |
} |
198 |
|
199 |
policy = "allow_write /dev/null"; |
200 |
if (write_policy()) { |
201 |
int fd = open("/dev/null", O_WRONLY); |
202 |
show_result(fd, 1); |
203 |
if (fd != EOF) close(fd); |
204 |
delete_policy(); |
205 |
fd = open("/dev/null", O_WRONLY); |
206 |
show_result(fd, 0); |
207 |
if (fd != EOF) close(fd); |
208 |
} |
209 |
|
210 |
policy = "allow_read/write /dev/null"; |
211 |
if (write_policy()) { |
212 |
int fd = open("/dev/null", O_RDWR); |
213 |
show_result(fd, 1); |
214 |
if (fd != EOF) close(fd); |
215 |
delete_policy(); |
216 |
fd = open("/dev/null", O_RDWR); |
217 |
show_result(fd, 0); |
218 |
if (fd != EOF) close(fd); |
219 |
} |
220 |
|
221 |
policy = "allow_create /tmp/open_test"; |
222 |
if (write_policy()) { |
223 |
policy = "allow_write /tmp/open_test"; |
224 |
if (write_policy()) { |
225 |
int fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0666); |
226 |
show_result(fd, 1); |
227 |
if (fd != EOF) close(fd); |
228 |
unlink2("/tmp/open_test"); |
229 |
delete_policy(); |
230 |
fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0666); |
231 |
show_result(fd, 0); |
232 |
if (fd != EOF) close(fd); |
233 |
unlink2("/tmp/open_test"); |
234 |
} |
235 |
policy = "allow_create /tmp/open_test\n"; |
236 |
delete_policy(); |
237 |
} |
238 |
|
239 |
policy = "allow_write /tmp/open_test"; |
240 |
if (write_policy()) { |
241 |
policy = "allow_create /tmp/open_test"; |
242 |
if (write_policy()) { |
243 |
int fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0666); |
244 |
show_result(fd, 1); |
245 |
if (fd != EOF) close(fd); |
246 |
unlink2("/tmp/open_test"); |
247 |
delete_policy(); |
248 |
fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0666); |
249 |
show_result(fd, 0); |
250 |
if (fd != EOF) close(fd); |
251 |
unlink2("/tmp/open_test"); |
252 |
} |
253 |
policy = "allow_write /tmp/open_test\n"; |
254 |
delete_policy(); |
255 |
} |
256 |
|
257 |
filename = "/tmp/truncate_test"; |
258 |
create2(filename); |
259 |
|
260 |
policy = "allow_truncate /tmp/truncate_test"; |
261 |
if (write_policy()) { |
262 |
policy = "allow_write /tmp/truncate_test"; |
263 |
if (write_policy()) { |
264 |
int fd = open(filename, O_WRONLY | O_TRUNC); |
265 |
show_result(fd, 1); |
266 |
if (fd != EOF) close(fd); |
267 |
delete_policy(); |
268 |
fd = open(filename, O_WRONLY | O_TRUNC); |
269 |
show_result(fd, 0); |
270 |
if (fd != EOF) close(fd); |
271 |
} |
272 |
policy = "allow_truncate /tmp/truncate_test"; |
273 |
delete_policy(); |
274 |
} |
275 |
|
276 |
policy = "allow_write /tmp/truncate_test"; |
277 |
if (write_policy()) { |
278 |
policy = "allow_truncate /tmp/truncate_test"; |
279 |
if (write_policy()) { |
280 |
int fd = open(filename, O_WRONLY | O_TRUNC); |
281 |
show_result(fd, 1); |
282 |
if (fd != EOF) close(fd); |
283 |
delete_policy(); |
284 |
fd = open(filename, O_WRONLY | O_TRUNC); |
285 |
show_result(fd, 0); |
286 |
if (fd != EOF) close(fd); |
287 |
} |
288 |
policy = "allow_write /tmp/truncate_test\n"; |
289 |
delete_policy(); |
290 |
} |
291 |
|
292 |
policy = "allow_truncate /tmp/truncate_test"; |
293 |
if (write_policy()) { |
294 |
show_result(truncate(filename, 0), 1); |
295 |
delete_policy(); |
296 |
show_result(truncate(filename, 0), 0); |
297 |
} |
298 |
|
299 |
policy = "allow_truncate /tmp/truncate_test"; |
300 |
if (write_policy()) { |
301 |
int fd; |
302 |
const char *cp = "255-MAC_FOR_FILE=disabled\n"; |
303 |
write(profile_fd, cp, strlen(cp)); |
304 |
fd = open(filename, O_WRONLY); |
305 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
306 |
write(profile_fd, cp, strlen(cp)); |
307 |
show_result(ftruncate(fd, 0), 1); |
308 |
delete_policy(); |
309 |
show_result(ftruncate(fd, 0), 0); |
310 |
if (fd != EOF) close(fd); |
311 |
} |
312 |
|
313 |
unlink2(filename); |
314 |
|
315 |
policy = "allow_create /tmp/mknod_reg_test"; |
316 |
if (write_policy()) { |
317 |
filename = "/tmp/mknod_reg_test"; |
318 |
show_result(mknod(filename, S_IFREG, 0), 1); |
319 |
delete_policy(); |
320 |
unlink2(filename); |
321 |
show_result(mknod(filename, S_IFREG, 0), 0); |
322 |
} |
323 |
|
324 |
policy = "allow_mkchar /tmp/mknod_chr_test"; |
325 |
if (write_policy()) { |
326 |
filename = "/tmp/mknod_chr_test"; |
327 |
show_result(mknod(filename, S_IFCHR, MKDEV(1, 3)), 1); |
328 |
delete_policy(); |
329 |
unlink2(filename); |
330 |
show_result(mknod(filename, S_IFCHR, MKDEV(1, 3)), 0); |
331 |
} |
332 |
|
333 |
policy = "allow_mkblock /tmp/mknod_blk_test"; |
334 |
if (write_policy()) { |
335 |
filename = "/tmp/mknod_blk_test"; |
336 |
show_result(mknod(filename, S_IFBLK, MKDEV(1, 0)), 1); |
337 |
delete_policy(); |
338 |
unlink2(filename); |
339 |
show_result(mknod(filename, S_IFBLK, MKDEV(1, 0)), 0); |
340 |
} |
341 |
|
342 |
policy = "allow_mkfifo /tmp/mknod_fifo_test"; |
343 |
if (write_policy()) { |
344 |
filename = "/tmp/mknod_fifo_test"; |
345 |
show_result(mknod(filename, S_IFIFO, 0), 1); |
346 |
delete_policy(); |
347 |
unlink2(filename); |
348 |
show_result(mknod(filename, S_IFIFO, 0), 0); |
349 |
} |
350 |
|
351 |
policy = "allow_mksock /tmp/mknod_sock_test"; |
352 |
if (write_policy()) { |
353 |
filename = "/tmp/mknod_sock_test"; |
354 |
show_result(mknod(filename, S_IFSOCK, 0), 1); |
355 |
delete_policy(); |
356 |
unlink2(filename); |
357 |
show_result(mknod(filename, S_IFSOCK, 0), 0); |
358 |
} |
359 |
|
360 |
policy = "allow_mkdir /tmp/mkdir_test/"; |
361 |
if (write_policy()) { |
362 |
filename = "/tmp/mkdir_test"; |
363 |
show_result(mkdir(filename, 0600), 1); |
364 |
delete_policy(); |
365 |
rmdir2(filename); |
366 |
show_result(mkdir(filename, 0600), 0); |
367 |
} |
368 |
|
369 |
policy = "allow_rmdir /tmp/rmdir_test/"; |
370 |
if (write_policy()) { |
371 |
filename = "/tmp/rmdir_test"; |
372 |
mkdir2(filename); |
373 |
show_result(rmdir(filename), 1); |
374 |
delete_policy(); |
375 |
mkdir2(filename); |
376 |
show_result(rmdir(filename), 0); |
377 |
rmdir2(filename); |
378 |
} |
379 |
|
380 |
policy = "allow_unlink /tmp/unlink_test"; |
381 |
if (write_policy()) { |
382 |
filename = "/tmp/unlink_test"; |
383 |
create2(filename); |
384 |
show_result(unlink(filename), 1); |
385 |
delete_policy(); |
386 |
create2(filename); |
387 |
show_result(unlink(filename), 0); |
388 |
unlink2(filename); |
389 |
} |
390 |
|
391 |
policy = "allow_symlink /tmp/symlink_source_test"; |
392 |
if (write_policy()) { |
393 |
filename = "/tmp/symlink_source_test"; |
394 |
show_result(symlink("/tmp/symlink_dest_test", filename), 1); |
395 |
delete_policy(); |
396 |
unlink2(filename); |
397 |
show_result(symlink("/tmp/symlink_dest_test", filename), 0); |
398 |
} |
399 |
|
400 |
policy = "allow_link /tmp/link_source_test /tmp/link_dest_test"; |
401 |
if (write_policy()) { |
402 |
filename = "/tmp/link_source_test"; |
403 |
create2(filename); |
404 |
show_result(link(filename, "/tmp/link_dest_test"), 1); |
405 |
delete_policy(); |
406 |
unlink2("/tmp/link_dest_test"); |
407 |
show_result(link(filename, "/tmp/link_dest_test"), 0); |
408 |
unlink2(filename); |
409 |
} |
410 |
|
411 |
policy = "allow_rename /tmp/rename_source_test /tmp/rename_dest_test"; |
412 |
if (write_policy()) { |
413 |
filename = "/tmp/rename_source_test"; |
414 |
create2(filename); |
415 |
show_result(rename(filename, "/tmp/rename_dest_test"), 1); |
416 |
delete_policy(); |
417 |
unlink2("/tmp/rename_dest_test"); |
418 |
create2(filename); |
419 |
show_result(rename(filename, "/tmp/rename_dest_test"), 0); |
420 |
unlink2(filename); |
421 |
} |
422 |
|
423 |
policy = "allow_mksock /tmp/socket_test"; |
424 |
if (write_policy()) { |
425 |
struct sockaddr_un addr; |
426 |
int fd; |
427 |
filename = "/tmp/socket_test"; |
428 |
memset(&addr, 0, sizeof(addr)); |
429 |
addr.sun_family = AF_UNIX; |
430 |
strncpy(addr.sun_path, filename, sizeof(addr.sun_path) - 1); |
431 |
fd = socket(AF_UNIX, SOCK_STREAM, 0); |
432 |
show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)), 1); |
433 |
if (fd != EOF) close(fd); |
434 |
delete_policy(); |
435 |
unlink2(filename); |
436 |
fd = socket(AF_UNIX, SOCK_STREAM, 0); |
437 |
show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)), 0); |
438 |
if (fd != EOF) close(fd); |
439 |
} |
440 |
|
441 |
filename = "/tmp/rewrite_test"; |
442 |
create2(filename); |
443 |
policy = "allow_read/write /tmp/rewrite_test"; |
444 |
if (write_policy()) { |
445 |
char *cp = "deny_rewrite /tmp/rewrite_test\n"; |
446 |
write(exception_fd, cp, strlen(cp)); |
447 |
policy = "allow_truncate /tmp/rewrite_test"; |
448 |
if (write_policy()) { |
449 |
int fd; |
450 |
|
451 |
fd = open(filename, O_RDONLY); |
452 |
show_result(fd, 1); |
453 |
if (fd != EOF) close(fd); |
454 |
|
455 |
fd = open(filename, O_WRONLY | O_APPEND); |
456 |
show_result(fd, 1); |
457 |
if (fd != EOF) close(fd); |
458 |
|
459 |
fd = open(filename, O_WRONLY); |
460 |
show_result(fd, 0); |
461 |
if (fd != EOF) close(fd); |
462 |
|
463 |
fd = open(filename, O_WRONLY | O_TRUNC); |
464 |
show_result(fd, 0); |
465 |
if (fd != EOF) close(fd); |
466 |
|
467 |
fd = open(filename, O_WRONLY | O_TRUNC | O_APPEND); |
468 |
show_result(fd, 0); |
469 |
if (fd != EOF) close(fd); |
470 |
|
471 |
show_result(truncate(filename, 0), 0); |
472 |
|
473 |
cp = "255-MAC_FOR_FILE=disabled\n"; |
474 |
write(profile_fd, cp, strlen(cp)); |
475 |
fd = open(filename, O_WRONLY | O_APPEND); |
476 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
477 |
write(profile_fd, cp, strlen(cp)); |
478 |
show_result(ftruncate(fd, 0), 0); |
479 |
|
480 |
show_result(fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_APPEND), 0); |
481 |
if (fd != EOF) close(fd); |
482 |
|
483 |
delete_policy(); |
484 |
} |
485 |
policy = "allow_read/write /tmp/rewrite_test"; |
486 |
delete_policy(); |
487 |
cp = "delete deny_rewrite /tmp/rewrite_test\n"; |
488 |
write(exception_fd, cp, strlen(cp)); |
489 |
} |
490 |
unlink2(filename); |
491 |
} |
492 |
|
493 |
int main(int argc, char *argv[]) { |
494 |
char *cp; |
495 |
Init(); |
496 |
profile_fd = open(proc_policy_profile, O_WRONLY); |
497 |
domain_fd = open(proc_policy_domain_policy, O_WRONLY); |
498 |
exception_fd = open(proc_policy_exception_policy, O_WRONLY); |
499 |
{ |
500 |
int self_fd = open(proc_policy_self_domain, O_RDONLY); |
501 |
memset(self_domain, 0, sizeof(self_domain)); |
502 |
read(self_fd, self_domain, sizeof(self_domain) - 1); |
503 |
close(self_fd); |
504 |
write(domain_fd, self_domain, strlen(self_domain)); |
505 |
cp = " /bin/true\n"; |
506 |
write(domain_fd, cp, strlen(cp)); |
507 |
write(domain_fd, self_domain, strlen(self_domain)); |
508 |
write(domain_fd, "\n", 1); |
509 |
cp = "use_profile 255\n"; |
510 |
write(domain_fd, cp, strlen(cp)); |
511 |
} |
512 |
cp = "255-MAX_REJECT_LOG=1024\n"; |
513 |
write(profile_fd, cp, strlen(cp)); |
514 |
StageFileTest(); |
515 |
cp = "use_profile 0\n"; |
516 |
write(domain_fd, cp, strlen(cp)); |
517 |
ClearStatus(); |
518 |
return 0; |
519 |
} |