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