1 |
/* |
2 |
* tomoyo_file_test.c |
3 |
* |
4 |
* Testing program for fs/tomoyo_file.c |
5 |
* |
6 |
* Copyright (C) 2005-2009 NTT DATA CORPORATION |
7 |
* |
8 |
* Version: 1.6.7+ 2009/04/08 |
9 |
* |
10 |
*/ |
11 |
#include "include.h" |
12 |
|
13 |
static int domain_fd = EOF; |
14 |
static int exception_fd = EOF; |
15 |
static const char *policy = ""; |
16 |
static char self_domain[4096] = ""; |
17 |
static _Bool has_cond = 1; |
18 |
|
19 |
static int write_policy(void) |
20 |
{ |
21 |
FILE *fp; |
22 |
char buffer[8192]; |
23 |
char *cp; |
24 |
int domain_found = 0; |
25 |
int policy_found = 0; |
26 |
memset(buffer, 0, sizeof(buffer)); |
27 |
cp = "255-MAC_FOR_FILE=disabled\n"; |
28 |
write(profile_fd, cp, strlen(cp)); |
29 |
fp = fopen(proc_policy_domain_policy, "r"); |
30 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
31 |
write(profile_fd, cp, strlen(cp)); |
32 |
write(domain_fd, policy, strlen(policy)); |
33 |
write(domain_fd, "\n", 1); |
34 |
if (!fp) { |
35 |
printf("%s : BUG: policy read failed\n", policy); |
36 |
return 0; |
37 |
} |
38 |
while (fgets(buffer, sizeof(buffer) - 1, fp)) { |
39 |
cp = strchr(buffer, '\n'); |
40 |
if (cp) |
41 |
*cp = '\0'; |
42 |
if (!strncmp(buffer, "<kernel>", 8)) |
43 |
domain_found = !strcmp(self_domain, buffer); |
44 |
if (domain_found) { |
45 |
/* printf("<%s>\n", buffer); */ |
46 |
if (!strcmp(buffer, policy)) { |
47 |
policy_found = 1; |
48 |
break; |
49 |
} |
50 |
} |
51 |
} |
52 |
fclose(fp); |
53 |
if (!policy_found) { |
54 |
printf("%s : BUG: policy write failed\n", policy); |
55 |
return 0; |
56 |
} |
57 |
errno = 0; |
58 |
return 1; |
59 |
} |
60 |
|
61 |
static void delete_policy(void) |
62 |
{ |
63 |
write(domain_fd, "delete ", 7); |
64 |
write(domain_fd, policy, strlen(policy)); |
65 |
write(domain_fd, "\n", 1); |
66 |
} |
67 |
|
68 |
static void show_result(int result, char should_success) |
69 |
{ |
70 |
int err = errno; |
71 |
printf("%s : ", policy); |
72 |
if (should_success) { |
73 |
if (result != EOF) |
74 |
printf("OK\n"); |
75 |
else |
76 |
printf("FAILED: %s\n", strerror(err)); |
77 |
} else { |
78 |
if (result == EOF) { |
79 |
if (err == EPERM) |
80 |
printf("OK: Permission denied.\n"); |
81 |
else |
82 |
printf("FAILED: %s\n", strerror(err)); |
83 |
} else { |
84 |
printf("BUG: didn't fail.\n"); |
85 |
} |
86 |
} |
87 |
} |
88 |
|
89 |
static void create2(const char *pathname) |
90 |
{ |
91 |
const char *cp = "255-MAC_FOR_FILE=disabled\n"; |
92 |
write(profile_fd, cp, strlen(cp)); |
93 |
close(creat(pathname, 0600)); |
94 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
95 |
write(profile_fd, cp, strlen(cp)); |
96 |
errno = 0; |
97 |
} |
98 |
|
99 |
static void mkdir2(const char *pathname) |
100 |
{ |
101 |
const char *cp = "255-MAC_FOR_FILE=disabled\n"; |
102 |
write(profile_fd, cp, strlen(cp)); |
103 |
mkdir(pathname, 0600); |
104 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
105 |
write(profile_fd, cp, strlen(cp)); |
106 |
errno = 0; |
107 |
} |
108 |
|
109 |
static void unlink2(const char *pathname) |
110 |
{ |
111 |
const char *cp = "255-MAC_FOR_FILE=disabled\n"; |
112 |
write(profile_fd, cp, strlen(cp)); |
113 |
unlink(pathname); |
114 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
115 |
write(profile_fd, cp, strlen(cp)); |
116 |
errno = 0; |
117 |
} |
118 |
|
119 |
static void rmdir2(const char *pathname) |
120 |
{ |
121 |
const char *cp = "255-MAC_FOR_FILE=disabled\n"; |
122 |
write(profile_fd, cp, strlen(cp)); |
123 |
rmdir(pathname); |
124 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
125 |
write(profile_fd, cp, strlen(cp)); |
126 |
errno = 0; |
127 |
} |
128 |
|
129 |
static void mkfifo2(const char *pathname) |
130 |
{ |
131 |
const char *cp = "255-MAC_FOR_FILE=disabled\n"; |
132 |
write(profile_fd, cp, strlen(cp)); |
133 |
mkfifo(pathname, 0600); |
134 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
135 |
write(profile_fd, cp, strlen(cp)); |
136 |
errno = 0; |
137 |
} |
138 |
|
139 |
static void stage_file_test(void) |
140 |
{ |
141 |
char *filename = ""; |
142 |
policy = "allow_read /proc/sys/net/ipv4/ip_local_port_range " |
143 |
"if task.uid=0 task.gid=0"; |
144 |
if (!has_cond) |
145 |
policy = "allow_read /proc/sys/net/ipv4/ip_local_port_range"; |
146 |
if (write_policy()) { |
147 |
static int name[] = { CTL_NET, NET_IPV4, |
148 |
NET_IPV4_LOCAL_PORT_RANGE }; |
149 |
int buffer[2] = { 32768, 61000 }; |
150 |
size_t size = sizeof(buffer); |
151 |
show_result(sysctl(name, 3, buffer, &size, 0, 0), 1); |
152 |
delete_policy(); |
153 |
show_result(sysctl(name, 3, buffer, &size, 0, 0), 0); |
154 |
} |
155 |
policy = "allow_write /proc/sys/net/ipv4/ip_local_port_range " |
156 |
"if task.euid=0 0=0 1-100=10-1000"; |
157 |
if (!has_cond) |
158 |
policy = "allow_write /proc/sys/net/ipv4/ip_local_port_range"; |
159 |
if (write_policy()) { |
160 |
static int name[] = { CTL_NET, NET_IPV4, |
161 |
NET_IPV4_LOCAL_PORT_RANGE }; |
162 |
int buffer[2] = { 32768, 61000 }; |
163 |
size_t size = sizeof(buffer); |
164 |
show_result(sysctl(name, 3, 0, 0, buffer, size), 1); |
165 |
delete_policy(); |
166 |
show_result(sysctl(name, 3, 0, 0, buffer, size), 0); |
167 |
} |
168 |
policy = "allow_read/write /proc/sys/net/ipv4/ip_local_port_range " |
169 |
"if 1!=10-100"; |
170 |
if (!has_cond) |
171 |
policy = "allow_read/write /proc/sys/net/ipv4/ip_local_port_range"; |
172 |
if (write_policy()) { |
173 |
static int name[] = { CTL_NET, NET_IPV4, |
174 |
NET_IPV4_LOCAL_PORT_RANGE }; |
175 |
int buffer[2] = { 32768, 61000 }; |
176 |
size_t size = sizeof(buffer); |
177 |
show_result(sysctl(name, 3, buffer, &size, buffer, size), 1); |
178 |
delete_policy(); |
179 |
show_result(sysctl(name, 3, buffer, &size, buffer, size), 0); |
180 |
} |
181 |
|
182 |
policy = "allow_read /bin/true " |
183 |
"if path1.uid=0 path1.parent.uid=0 10=10-100"; |
184 |
if (!has_cond) |
185 |
policy = "allow_read /bin/true"; |
186 |
if (write_policy()) { |
187 |
show_result(uselib("/bin/true"), 1); |
188 |
delete_policy(); |
189 |
show_result(uselib("/bin/true"), 0); |
190 |
} |
191 |
|
192 |
policy = "allow_execute /bin/true if task.uid!=10 path1.parent.uid=0"; |
193 |
if (!has_cond) |
194 |
policy = "allow_execute /bin/true"; |
195 |
if (write_policy()) { |
196 |
int pipe_fd[2] = { EOF, EOF }; |
197 |
int err = 0; |
198 |
fflush(stdout); |
199 |
fflush(stderr); |
200 |
pipe(pipe_fd); |
201 |
if (fork() == 0) { |
202 |
execl("/bin/true", "/bin/true", NULL); |
203 |
err = errno; |
204 |
write(pipe_fd[1], &err, sizeof(err)); |
205 |
_exit(0); |
206 |
} |
207 |
close(pipe_fd[1]); |
208 |
read(pipe_fd[0], &err, sizeof(err)); |
209 |
close(pipe_fd[0]); |
210 |
wait(NULL); |
211 |
errno = err; |
212 |
show_result(err ? EOF : 0, 1); |
213 |
delete_policy(); |
214 |
fflush(stdout); |
215 |
fflush(stderr); |
216 |
pipe(pipe_fd); |
217 |
if (fork() == 0) { |
218 |
execl("/bin/true", "/bin/true", NULL); |
219 |
err = errno; |
220 |
write(pipe_fd[1], &err, sizeof(err)); |
221 |
_exit(0); |
222 |
} |
223 |
close(pipe_fd[1]); |
224 |
read(pipe_fd[0], &err, sizeof(err)); |
225 |
close(pipe_fd[0]); |
226 |
wait(NULL); |
227 |
errno = err; |
228 |
show_result(err ? EOF : 0, 0); |
229 |
} |
230 |
|
231 |
policy = "allow_read /dev/null if path1.type=char path1.dev_major=1 " |
232 |
"path1.dev_minor=3"; |
233 |
if (!has_cond) |
234 |
policy = "allow_read /dev/null"; |
235 |
if (write_policy()) { |
236 |
int fd = open("/dev/null", O_RDONLY); |
237 |
show_result(fd, 1); |
238 |
if (fd != EOF) |
239 |
close(fd); |
240 |
delete_policy(); |
241 |
fd = open("/dev/null", O_RDONLY); |
242 |
show_result(fd, 0); |
243 |
if (fd != EOF) |
244 |
close(fd); |
245 |
} |
246 |
|
247 |
policy = "allow_read /dev/null if path1.perm=0666"; |
248 |
if (!has_cond) |
249 |
policy = "allow_read /dev/null"; |
250 |
if (write_policy()) { |
251 |
int fd = open("/dev/null", O_RDONLY); |
252 |
show_result(fd, 1); |
253 |
if (fd != EOF) |
254 |
close(fd); |
255 |
delete_policy(); |
256 |
fd = open("/dev/null", O_RDONLY); |
257 |
show_result(fd, 0); |
258 |
if (fd != EOF) |
259 |
close(fd); |
260 |
} |
261 |
|
262 |
policy = "allow_read /dev/null if path1.perm!=0777"; |
263 |
if (!has_cond) |
264 |
policy = "allow_read /dev/null"; |
265 |
if (write_policy()) { |
266 |
int fd = open("/dev/null", O_RDONLY); |
267 |
show_result(fd, 1); |
268 |
if (fd != EOF) |
269 |
close(fd); |
270 |
delete_policy(); |
271 |
fd = open("/dev/null", O_RDONLY); |
272 |
show_result(fd, 0); |
273 |
if (fd != EOF) |
274 |
close(fd); |
275 |
} |
276 |
|
277 |
policy = "allow_read /dev/null if path1.perm=owner_read " |
278 |
"path1.perm=owner_write path1.perm!=owner_execute " |
279 |
"path1.perm=group_read path1.perm=group_write " |
280 |
"path1.perm!=group_execute path1.perm=others_read " |
281 |
"path1.perm=others_write path1.perm!=others_execute " |
282 |
"path1.perm!=setuid path1.perm!=setgid path1.perm!=sticky"; |
283 |
if (!has_cond) |
284 |
policy = "allow_read /dev/null"; |
285 |
if (write_policy()) { |
286 |
int fd = open("/dev/null", O_RDONLY); |
287 |
show_result(fd, 1); |
288 |
if (fd != EOF) |
289 |
close(fd); |
290 |
delete_policy(); |
291 |
fd = open("/dev/null", O_RDONLY); |
292 |
show_result(fd, 0); |
293 |
if (fd != EOF) |
294 |
close(fd); |
295 |
} |
296 |
|
297 |
policy = "allow_mkfifo /tmp/mknod_fifo_test " |
298 |
"if path1.parent.perm=01777 path1.parent.perm=sticky " |
299 |
"path1.parent.uid=0 path1.parent.gid=0"; |
300 |
if (!has_cond) |
301 |
policy = "allow_mkfifo /tmp/mknod_fifo_test"; |
302 |
if (write_policy()) { |
303 |
filename = "/tmp/mknod_fifo_test"; |
304 |
show_result(mknod(filename, S_IFIFO, 0), 1); |
305 |
delete_policy(); |
306 |
unlink2(filename); |
307 |
show_result(mknod(filename, S_IFIFO, 0), 0); |
308 |
} |
309 |
|
310 |
{ |
311 |
char buffer[1024]; |
312 |
struct stat sbuf; |
313 |
memset(buffer, 0, sizeof(buffer)); |
314 |
memset(&sbuf, 0, sizeof(sbuf)); |
315 |
filename = "/dev/null"; |
316 |
stat(filename, &sbuf); |
317 |
snprintf(buffer, sizeof(buffer) - 1, |
318 |
"allow_write %s if path1.major=%u path1.minor=%u", |
319 |
filename, (unsigned int) MAJOR(sbuf.st_dev), |
320 |
(unsigned int) MINOR(sbuf.st_dev)); |
321 |
if (!has_cond) |
322 |
snprintf(buffer, sizeof(buffer) - 1, |
323 |
"allow_write %s", filename); |
324 |
policy = buffer; |
325 |
if (write_policy()) { |
326 |
int fd = open(filename, O_WRONLY); |
327 |
show_result(fd, 1); |
328 |
if (fd != EOF) |
329 |
close(fd); |
330 |
delete_policy(); |
331 |
fd = open(filename, O_WRONLY); |
332 |
show_result(fd, 0); |
333 |
if (fd != EOF) |
334 |
close(fd); |
335 |
} |
336 |
} |
337 |
|
338 |
policy = "allow_read/write /tmp/fifo if path1.type=fifo"; |
339 |
if (!has_cond) |
340 |
policy = "allow_read/write /tmp/fifo"; |
341 |
mkfifo2("/tmp/fifo"); |
342 |
if (write_policy()) { |
343 |
int fd = open("/tmp/fifo", O_RDWR); |
344 |
show_result(fd, 1); |
345 |
if (fd != EOF) |
346 |
close(fd); |
347 |
delete_policy(); |
348 |
fd = open("/tmp/fifo", O_RDWR); |
349 |
show_result(fd, 0); |
350 |
if (fd != EOF) |
351 |
close(fd); |
352 |
} |
353 |
|
354 |
policy = "allow_read /dev/null if path1.parent.ino=path1.parent.ino"; |
355 |
if (!has_cond) |
356 |
policy = "allow_read /dev/null"; |
357 |
if (write_policy()) { |
358 |
int fd = open("/dev/null", O_RDONLY); |
359 |
show_result(fd, 1); |
360 |
if (fd != EOF) |
361 |
close(fd); |
362 |
delete_policy(); |
363 |
fd = open("/dev/null", O_RDONLY); |
364 |
show_result(fd, 0); |
365 |
if (fd != EOF) |
366 |
close(fd); |
367 |
} |
368 |
|
369 |
policy = "allow_write /dev/null if path1.uid=path1.gid"; |
370 |
if (!has_cond) |
371 |
policy = "allow_write /dev/null"; |
372 |
if (write_policy()) { |
373 |
int fd = open("/dev/null", O_WRONLY); |
374 |
show_result(fd, 1); |
375 |
if (fd != EOF) |
376 |
close(fd); |
377 |
delete_policy(); |
378 |
fd = open("/dev/null", O_WRONLY); |
379 |
show_result(fd, 0); |
380 |
if (fd != EOF) |
381 |
close(fd); |
382 |
} |
383 |
|
384 |
policy = "allow_read/write /dev/null if task.uid=path1.parent.uid"; |
385 |
if (!has_cond) |
386 |
policy = "allow_read/write /dev/null"; |
387 |
if (write_policy()) { |
388 |
int fd = open("/dev/null", O_RDWR); |
389 |
show_result(fd, 1); |
390 |
if (fd != EOF) |
391 |
close(fd); |
392 |
delete_policy(); |
393 |
fd = open("/dev/null", O_RDWR); |
394 |
show_result(fd, 0); |
395 |
if (fd != EOF) |
396 |
close(fd); |
397 |
} |
398 |
|
399 |
policy = "allow_create /tmp/open_test if path1.parent.uid=task.uid"; |
400 |
if (!has_cond) |
401 |
policy = "allow_create /tmp/open_test"; |
402 |
if (write_policy()) { |
403 |
policy = "allow_write /tmp/open_test if path1.parent.uid=0"; |
404 |
if (!has_cond) |
405 |
policy = "allow_write /tmp/open_test"; |
406 |
if (write_policy()) { |
407 |
int fd = open("/tmp/open_test", |
408 |
O_WRONLY | O_CREAT | O_EXCL, 0666); |
409 |
show_result(fd, 1); |
410 |
if (fd != EOF) |
411 |
close(fd); |
412 |
unlink2("/tmp/open_test"); |
413 |
delete_policy(); |
414 |
fd = open("/tmp/open_test", |
415 |
O_WRONLY | O_CREAT | O_EXCL, 0666); |
416 |
show_result(fd, 0); |
417 |
if (fd != EOF) |
418 |
close(fd); |
419 |
unlink2("/tmp/open_test"); |
420 |
} |
421 |
policy = "allow_create /tmp/open_test " |
422 |
"if path1.parent.uid=task.uid"; |
423 |
if (!has_cond) |
424 |
policy = "allow_create /tmp/open_test"; |
425 |
delete_policy(); |
426 |
} |
427 |
|
428 |
policy = "allow_write /tmp/open_test if task.uid=0 path1.ino!=0"; |
429 |
if (!has_cond) |
430 |
policy = "allow_write /tmp/open_test"; |
431 |
if (write_policy()) { |
432 |
policy = "allow_create /tmp/open_test if 0=0"; |
433 |
if (!has_cond) |
434 |
policy = "allow_create /tmp/open_test"; |
435 |
if (write_policy()) { |
436 |
int fd = open("/tmp/open_test", |
437 |
O_WRONLY | O_CREAT | O_EXCL, 0666); |
438 |
show_result(fd, 1); |
439 |
if (fd != EOF) |
440 |
close(fd); |
441 |
unlink2("/tmp/open_test"); |
442 |
delete_policy(); |
443 |
fd = open("/tmp/open_test", |
444 |
O_WRONLY | O_CREAT | O_EXCL, 0666); |
445 |
show_result(fd, 0); |
446 |
if (fd != EOF) |
447 |
close(fd); |
448 |
unlink2("/tmp/open_test"); |
449 |
} |
450 |
policy = "allow_write /tmp/open_test " |
451 |
"if task.uid=0 path1.ino!=0"; |
452 |
if (!has_cond) |
453 |
policy = "allow_write /tmp/open_test"; |
454 |
delete_policy(); |
455 |
} |
456 |
|
457 |
filename = "/tmp/truncate_test"; |
458 |
create2(filename); |
459 |
|
460 |
policy = "allow_truncate /tmp/truncate_test if task.uid=path1.uid"; |
461 |
if (!has_cond) |
462 |
policy = "allow_truncate /tmp/truncate_test"; |
463 |
if (write_policy()) { |
464 |
policy = "allow_write /tmp/truncate_test if 1!=100-1000000"; |
465 |
if (!has_cond) |
466 |
policy = "allow_write /tmp/truncate_test"; |
467 |
if (write_policy()) { |
468 |
int fd = open(filename, O_WRONLY | O_TRUNC); |
469 |
show_result(fd, 1); |
470 |
if (fd != EOF) |
471 |
close(fd); |
472 |
delete_policy(); |
473 |
fd = open(filename, O_WRONLY | O_TRUNC); |
474 |
show_result(fd, 0); |
475 |
if (fd != EOF) |
476 |
close(fd); |
477 |
} |
478 |
policy = "allow_truncate /tmp/truncate_test " |
479 |
"if task.uid=path1.uid"; |
480 |
if (!has_cond) |
481 |
policy = "allow_truncate /tmp/truncate_test"; |
482 |
delete_policy(); |
483 |
} |
484 |
|
485 |
policy = "allow_write /tmp/truncate_test"; |
486 |
if (write_policy()) { |
487 |
policy = "allow_truncate /tmp/truncate_test"; |
488 |
if (write_policy()) { |
489 |
int fd = open(filename, O_WRONLY | O_TRUNC); |
490 |
show_result(fd, 1); |
491 |
if (fd != EOF) |
492 |
close(fd); |
493 |
delete_policy(); |
494 |
fd = open(filename, O_WRONLY | O_TRUNC); |
495 |
show_result(fd, 0); |
496 |
if (fd != EOF) |
497 |
close(fd); |
498 |
} |
499 |
policy = "allow_write /tmp/truncate_test"; |
500 |
delete_policy(); |
501 |
} |
502 |
|
503 |
policy = "allow_truncate /tmp/truncate_test"; |
504 |
if (write_policy()) { |
505 |
show_result(truncate(filename, 0), 1); |
506 |
delete_policy(); |
507 |
show_result(truncate(filename, 0), 0); |
508 |
} |
509 |
|
510 |
policy = "allow_truncate /tmp/truncate_test"; |
511 |
if (write_policy()) { |
512 |
int fd; |
513 |
const char *cp = "255-MAC_FOR_FILE=disabled\n"; |
514 |
write(profile_fd, cp, strlen(cp)); |
515 |
fd = open(filename, O_WRONLY); |
516 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
517 |
write(profile_fd, cp, strlen(cp)); |
518 |
show_result(ftruncate(fd, 0), 1); |
519 |
delete_policy(); |
520 |
show_result(ftruncate(fd, 0), 0); |
521 |
if (fd != EOF) |
522 |
close(fd); |
523 |
} |
524 |
|
525 |
unlink2(filename); |
526 |
|
527 |
policy = "allow_create /tmp/mknod_reg_test"; |
528 |
if (write_policy()) { |
529 |
filename = "/tmp/mknod_reg_test"; |
530 |
show_result(mknod(filename, S_IFREG, 0), 1); |
531 |
delete_policy(); |
532 |
unlink2(filename); |
533 |
show_result(mknod(filename, S_IFREG, 0), 0); |
534 |
} |
535 |
|
536 |
policy = "allow_mkchar /tmp/mknod_chr_test"; |
537 |
if (write_policy()) { |
538 |
filename = "/tmp/mknod_chr_test"; |
539 |
show_result(mknod(filename, S_IFCHR, MKDEV(1, 3)), 1); |
540 |
delete_policy(); |
541 |
unlink2(filename); |
542 |
show_result(mknod(filename, S_IFCHR, MKDEV(1, 3)), 0); |
543 |
} |
544 |
|
545 |
policy = "allow_mkblock /tmp/mknod_blk_test"; |
546 |
if (write_policy()) { |
547 |
filename = "/tmp/mknod_blk_test"; |
548 |
show_result(mknod(filename, S_IFBLK, MKDEV(1, 0)), 1); |
549 |
delete_policy(); |
550 |
unlink2(filename); |
551 |
show_result(mknod(filename, S_IFBLK, MKDEV(1, 0)), 0); |
552 |
} |
553 |
|
554 |
policy = "allow_mkfifo /tmp/mknod_fifo_test"; |
555 |
if (write_policy()) { |
556 |
filename = "/tmp/mknod_fifo_test"; |
557 |
show_result(mknod(filename, S_IFIFO, 0), 1); |
558 |
delete_policy(); |
559 |
unlink2(filename); |
560 |
show_result(mknod(filename, S_IFIFO, 0), 0); |
561 |
} |
562 |
|
563 |
policy = "allow_mksock /tmp/mknod_sock_test"; |
564 |
if (write_policy()) { |
565 |
filename = "/tmp/mknod_sock_test"; |
566 |
show_result(mknod(filename, S_IFSOCK, 0), 1); |
567 |
delete_policy(); |
568 |
unlink2(filename); |
569 |
show_result(mknod(filename, S_IFSOCK, 0), 0); |
570 |
} |
571 |
|
572 |
policy = "allow_mkdir /tmp/mkdir_test/"; |
573 |
if (write_policy()) { |
574 |
filename = "/tmp/mkdir_test"; |
575 |
show_result(mkdir(filename, 0600), 1); |
576 |
delete_policy(); |
577 |
rmdir2(filename); |
578 |
show_result(mkdir(filename, 0600), 0); |
579 |
} |
580 |
|
581 |
policy = "allow_rmdir /tmp/rmdir_test/"; |
582 |
if (write_policy()) { |
583 |
filename = "/tmp/rmdir_test"; |
584 |
mkdir2(filename); |
585 |
show_result(rmdir(filename), 1); |
586 |
delete_policy(); |
587 |
mkdir2(filename); |
588 |
show_result(rmdir(filename), 0); |
589 |
rmdir2(filename); |
590 |
} |
591 |
|
592 |
policy = "allow_unlink /tmp/unlink_test"; |
593 |
if (write_policy()) { |
594 |
filename = "/tmp/unlink_test"; |
595 |
create2(filename); |
596 |
show_result(unlink(filename), 1); |
597 |
delete_policy(); |
598 |
create2(filename); |
599 |
show_result(unlink(filename), 0); |
600 |
unlink2(filename); |
601 |
} |
602 |
|
603 |
policy = "allow_symlink /tmp/symlink_source_test"; |
604 |
if (write_policy()) { |
605 |
filename = "/tmp/symlink_source_test"; |
606 |
show_result(symlink("/tmp/symlink_dest_test", filename), 1); |
607 |
delete_policy(); |
608 |
unlink2(filename); |
609 |
show_result(symlink("/tmp/symlink_dest_test", filename), 0); |
610 |
} |
611 |
|
612 |
if (!has_cond) |
613 |
goto skip_symlink_target; |
614 |
|
615 |
policy = "allow_symlink /tmp/symlink_source_test " |
616 |
"if symlink.target=\"/tmp/symlink_\\*_test\""; |
617 |
if (write_policy()) { |
618 |
filename = "/tmp/symlink_source_test"; |
619 |
show_result(symlink("/tmp/symlink_dest_test", filename), 1); |
620 |
delete_policy(); |
621 |
unlink2(filename); |
622 |
show_result(symlink("/tmp/symlink_dest_test", filename), 0); |
623 |
} |
624 |
|
625 |
policy = "allow_symlink /tmp/symlink_source_test " |
626 |
"if task.uid=0 symlink.target=\"/tmp/symlink_\\*_test\""; |
627 |
if (write_policy()) { |
628 |
filename = "/tmp/symlink_source_test"; |
629 |
show_result(symlink("/tmp/symlink_dest_test", filename), 1); |
630 |
delete_policy(); |
631 |
unlink2(filename); |
632 |
show_result(symlink("/tmp/symlink_dest_test", filename), 0); |
633 |
} |
634 |
|
635 |
policy = "allow_symlink /tmp/symlink_source_test " |
636 |
"if symlink.target!=\"\\*\""; |
637 |
if (write_policy()) { |
638 |
filename = "/tmp/symlink_source_test"; |
639 |
show_result(symlink("/tmp/symlink_dest_test", filename), 1); |
640 |
delete_policy(); |
641 |
unlink2(filename); |
642 |
show_result(symlink("/tmp/symlink_dest_test", filename), 0); |
643 |
} |
644 |
|
645 |
policy = "allow_symlink /tmp/symlink_source_test " |
646 |
"if symlink.target!=\"/tmp/symlink_\\*_test\""; |
647 |
if (write_policy()) { |
648 |
filename = "/tmp/symlink_source_test"; |
649 |
show_result(symlink("/tmp/symlink_dest_test", filename), 0); |
650 |
delete_policy(); |
651 |
unlink2(filename); |
652 |
show_result(symlink("/tmp/symlink_dest_test", filename), 0); |
653 |
} |
654 |
|
655 |
skip_symlink_target: |
656 |
|
657 |
policy = "allow_link /tmp/link_source_test /tmp/link_dest_test"; |
658 |
if (write_policy()) { |
659 |
filename = "/tmp/link_source_test"; |
660 |
create2(filename); |
661 |
show_result(link(filename, "/tmp/link_dest_test"), 1); |
662 |
delete_policy(); |
663 |
unlink2("/tmp/link_dest_test"); |
664 |
show_result(link(filename, "/tmp/link_dest_test"), 0); |
665 |
unlink2(filename); |
666 |
} |
667 |
|
668 |
policy = "allow_rename /tmp/rename_source_test /tmp/rename_dest_test"; |
669 |
if (write_policy()) { |
670 |
filename = "/tmp/rename_source_test"; |
671 |
create2(filename); |
672 |
show_result(rename(filename, "/tmp/rename_dest_test"), 1); |
673 |
delete_policy(); |
674 |
unlink2("/tmp/rename_dest_test"); |
675 |
create2(filename); |
676 |
show_result(rename(filename, "/tmp/rename_dest_test"), 0); |
677 |
unlink2(filename); |
678 |
} |
679 |
|
680 |
policy = "allow_mksock /tmp/socket_test"; |
681 |
if (write_policy()) { |
682 |
struct sockaddr_un addr; |
683 |
int fd; |
684 |
filename = "/tmp/socket_test"; |
685 |
memset(&addr, 0, sizeof(addr)); |
686 |
addr.sun_family = AF_UNIX; |
687 |
strncpy(addr.sun_path, filename, sizeof(addr.sun_path) - 1); |
688 |
fd = socket(AF_UNIX, SOCK_STREAM, 0); |
689 |
show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)), |
690 |
1); |
691 |
if (fd != EOF) |
692 |
close(fd); |
693 |
delete_policy(); |
694 |
unlink2(filename); |
695 |
fd = socket(AF_UNIX, SOCK_STREAM, 0); |
696 |
show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)), |
697 |
0); |
698 |
if (fd != EOF) |
699 |
close(fd); |
700 |
} |
701 |
|
702 |
filename = "/tmp/rewrite_test"; |
703 |
create2(filename); |
704 |
policy = "allow_read/write /tmp/rewrite_test"; |
705 |
if (write_policy()) { |
706 |
char *cp = "deny_rewrite /tmp/rewrite_test\n"; |
707 |
write(exception_fd, cp, strlen(cp)); |
708 |
policy = "allow_truncate /tmp/rewrite_test"; |
709 |
if (write_policy()) { |
710 |
int fd; |
711 |
|
712 |
fd = open(filename, O_RDONLY); |
713 |
show_result(fd, 1); |
714 |
if (fd != EOF) |
715 |
close(fd); |
716 |
|
717 |
fd = open(filename, O_WRONLY | O_APPEND); |
718 |
show_result(fd, 1); |
719 |
if (fd != EOF) |
720 |
close(fd); |
721 |
|
722 |
fd = open(filename, O_WRONLY); |
723 |
show_result(fd, 0); |
724 |
if (fd != EOF) |
725 |
close(fd); |
726 |
|
727 |
fd = open(filename, O_WRONLY | O_TRUNC); |
728 |
show_result(fd, 0); |
729 |
if (fd != EOF) |
730 |
close(fd); |
731 |
|
732 |
fd = open(filename, O_WRONLY | O_TRUNC | O_APPEND); |
733 |
show_result(fd, 0); |
734 |
if (fd != EOF) |
735 |
close(fd); |
736 |
|
737 |
show_result(truncate(filename, 0), 0); |
738 |
|
739 |
cp = "255-MAC_FOR_FILE=disabled\n"; |
740 |
write(profile_fd, cp, strlen(cp)); |
741 |
fd = open(filename, O_WRONLY | O_APPEND); |
742 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
743 |
write(profile_fd, cp, strlen(cp)); |
744 |
show_result(ftruncate(fd, 0), 0); |
745 |
|
746 |
show_result(fcntl(fd, F_SETFL, |
747 |
fcntl(fd, F_GETFL) & ~O_APPEND), 0); |
748 |
if (fd != EOF) |
749 |
close(fd); |
750 |
|
751 |
delete_policy(); |
752 |
} |
753 |
policy = "allow_read/write /tmp/rewrite_test"; |
754 |
delete_policy(); |
755 |
cp = "delete deny_rewrite /tmp/rewrite_test\n"; |
756 |
write(exception_fd, cp, strlen(cp)); |
757 |
} |
758 |
unlink2(filename); |
759 |
|
760 |
if (has_cond) { |
761 |
const char *cp = "255-MAC_FOR_IOCTL=enforcing\n"; |
762 |
write(profile_fd, cp, strlen(cp)); |
763 |
policy = "allow_ioctl socket:[family=2:type=2:protocol=17] " |
764 |
"35122-35124 if task.uid=0"; |
765 |
if (write_policy()) { |
766 |
struct ifreq ifreq; |
767 |
int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
768 |
memset(&ifreq, 0, sizeof(ifreq)); |
769 |
snprintf(ifreq.ifr_name, sizeof(ifreq.ifr_name) - 1, "lo"); |
770 |
show_result(ioctl(fd, 35123, &ifreq), 1); |
771 |
delete_policy(); |
772 |
policy = "allow_ioctl socket:[family=2:type=2:protocol=17] " |
773 |
"0-35122"; |
774 |
if (write_policy()) { |
775 |
show_result(ioctl(fd, 35123, &ifreq), 0); |
776 |
delete_policy(); |
777 |
} |
778 |
if (fd != EOF) |
779 |
close(fd); |
780 |
} |
781 |
cp = "255-MAC_FOR_IOCTL=disabled\n"; |
782 |
write(profile_fd, cp, strlen(cp)); |
783 |
} |
784 |
} |
785 |
|
786 |
int main(int argc, char *argv[]) |
787 |
{ |
788 |
char *cp; |
789 |
ccs_test_init(); |
790 |
domain_fd = open(proc_policy_domain_policy, O_WRONLY); |
791 |
if (domain_fd == EOF && errno == ENOENT) { |
792 |
fprintf(stderr, "You can't use this program for this kernel." |
793 |
"\n"); |
794 |
return 1; |
795 |
} |
796 |
exception_fd = open(proc_policy_exception_policy, O_WRONLY); |
797 |
{ |
798 |
int self_fd = open(proc_policy_self_domain, O_RDONLY); |
799 |
memset(self_domain, 0, sizeof(self_domain)); |
800 |
read(self_fd, self_domain, sizeof(self_domain) - 1); |
801 |
close(self_fd); |
802 |
write(domain_fd, self_domain, strlen(self_domain)); |
803 |
cp = " /bin/true\n"; |
804 |
write(domain_fd, cp, strlen(cp)); |
805 |
write(domain_fd, self_domain, strlen(self_domain)); |
806 |
write(domain_fd, "\n", 1); |
807 |
cp = "use_profile 255\n"; |
808 |
write(domain_fd, cp, strlen(cp)); |
809 |
} |
810 |
has_cond = !access("/proc/ccs/version", F_OK); |
811 |
cp = "255-MAX_REJECT_LOG=1024\n"; |
812 |
write(profile_fd, cp, strlen(cp)); |
813 |
stage_file_test(); |
814 |
cp = "use_profile 0\n"; |
815 |
write(domain_fd, cp, strlen(cp)); |
816 |
clear_status(); |
817 |
return 0; |
818 |
} |