3 |
* |
* |
4 |
* Testing program for fs/tomoyo_file.c |
* Testing program for fs/tomoyo_file.c |
5 |
* |
* |
6 |
* Copyright (C) 2005-2008 NTT DATA CORPORATION |
* Copyright (C) 2005-2009 NTT DATA CORPORATION |
7 |
* |
* |
8 |
* Version: 1.6.6-pre 2008/12/22 |
* Version: 1.6.8 2009/05/28 |
9 |
* |
* |
10 |
*/ |
*/ |
11 |
#include "include.h" |
#include "include.h" |
14 |
static int exception_fd = EOF; |
static int exception_fd = EOF; |
15 |
static const char *policy = ""; |
static const char *policy = ""; |
16 |
static char self_domain[4096] = ""; |
static char self_domain[4096] = ""; |
17 |
|
static _Bool has_cond = 1; |
18 |
|
|
19 |
static int write_policy(void) |
static int write_policy(void) |
20 |
{ |
{ |
126 |
errno = 0; |
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) |
static void stage_file_test(void) |
140 |
{ |
{ |
141 |
char *filename = ""; |
char *filename = ""; |
142 |
policy = "allow_read /proc/sys/net/ipv4/ip_local_port_range " |
policy = "allow_read /proc/sys/net/ipv4/ip_local_port_range " |
143 |
"if task.uid=0 task.gid=0"; |
"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()) { |
if (write_policy()) { |
147 |
static int name[] = { CTL_NET, NET_IPV4, |
static int name[] = { CTL_NET, NET_IPV4, |
148 |
NET_IPV4_LOCAL_PORT_RANGE }; |
NET_IPV4_LOCAL_PORT_RANGE }; |
154 |
} |
} |
155 |
policy = "allow_write /proc/sys/net/ipv4/ip_local_port_range " |
policy = "allow_write /proc/sys/net/ipv4/ip_local_port_range " |
156 |
"if task.euid=0 0=0 1-100=10-1000"; |
"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()) { |
if (write_policy()) { |
160 |
static int name[] = { CTL_NET, NET_IPV4, |
static int name[] = { CTL_NET, NET_IPV4, |
161 |
NET_IPV4_LOCAL_PORT_RANGE }; |
NET_IPV4_LOCAL_PORT_RANGE }; |
167 |
} |
} |
168 |
policy = "allow_read/write /proc/sys/net/ipv4/ip_local_port_range " |
policy = "allow_read/write /proc/sys/net/ipv4/ip_local_port_range " |
169 |
"if 1!=10-100"; |
"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()) { |
if (write_policy()) { |
174 |
static int name[] = { CTL_NET, NET_IPV4, |
static int name[] = { CTL_NET, NET_IPV4, |
175 |
NET_IPV4_LOCAL_PORT_RANGE }; |
NET_IPV4_LOCAL_PORT_RANGE }; |
182 |
|
|
183 |
policy = "allow_read /bin/true " |
policy = "allow_read /bin/true " |
184 |
"if path1.uid=0 path1.parent.uid=0 10=10-100"; |
"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()) { |
if (write_policy()) { |
188 |
show_result(uselib("/bin/true"), 1); |
show_result(uselib("/bin/true"), 1); |
189 |
delete_policy(); |
delete_policy(); |
191 |
} |
} |
192 |
|
|
193 |
policy = "allow_execute /bin/true if task.uid!=10 path1.parent.uid=0"; |
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()) { |
if (write_policy()) { |
197 |
int pipe_fd[2] = { EOF, EOF }; |
int pipe_fd[2] = { EOF, EOF }; |
198 |
int err = 0; |
int err = 0; |
231 |
|
|
232 |
policy = "allow_read /dev/null if path1.type=char path1.dev_major=1 " |
policy = "allow_read /dev/null if path1.type=char path1.dev_major=1 " |
233 |
"path1.dev_minor=3"; |
"path1.dev_minor=3"; |
234 |
|
if (!has_cond) |
235 |
|
policy = "allow_read /dev/null"; |
236 |
if (write_policy()) { |
if (write_policy()) { |
237 |
int fd = open("/dev/null", O_RDONLY); |
int fd = open("/dev/null", O_RDONLY); |
238 |
show_result(fd, 1); |
show_result(fd, 1); |
246 |
} |
} |
247 |
|
|
248 |
policy = "allow_read /dev/null if path1.perm=0666"; |
policy = "allow_read /dev/null if path1.perm=0666"; |
249 |
|
if (!has_cond) |
250 |
|
policy = "allow_read /dev/null"; |
251 |
if (write_policy()) { |
if (write_policy()) { |
252 |
int fd = open("/dev/null", O_RDONLY); |
int fd = open("/dev/null", O_RDONLY); |
253 |
show_result(fd, 1); |
show_result(fd, 1); |
261 |
} |
} |
262 |
|
|
263 |
policy = "allow_read /dev/null if path1.perm!=0777"; |
policy = "allow_read /dev/null if path1.perm!=0777"; |
264 |
|
if (!has_cond) |
265 |
|
policy = "allow_read /dev/null"; |
266 |
if (write_policy()) { |
if (write_policy()) { |
267 |
int fd = open("/dev/null", O_RDONLY); |
int fd = open("/dev/null", O_RDONLY); |
268 |
show_result(fd, 1); |
show_result(fd, 1); |
281 |
"path1.perm!=group_execute path1.perm=others_read " |
"path1.perm!=group_execute path1.perm=others_read " |
282 |
"path1.perm=others_write path1.perm!=others_execute " |
"path1.perm=others_write path1.perm!=others_execute " |
283 |
"path1.perm!=setuid path1.perm!=setgid path1.perm!=sticky"; |
"path1.perm!=setuid path1.perm!=setgid path1.perm!=sticky"; |
284 |
|
if (!has_cond) |
285 |
|
policy = "allow_read /dev/null"; |
286 |
if (write_policy()) { |
if (write_policy()) { |
287 |
int fd = open("/dev/null", O_RDONLY); |
int fd = open("/dev/null", O_RDONLY); |
288 |
show_result(fd, 1); |
show_result(fd, 1); |
298 |
policy = "allow_mkfifo /tmp/mknod_fifo_test " |
policy = "allow_mkfifo /tmp/mknod_fifo_test " |
299 |
"if path1.parent.perm=01777 path1.parent.perm=sticky " |
"if path1.parent.perm=01777 path1.parent.perm=sticky " |
300 |
"path1.parent.uid=0 path1.parent.gid=0"; |
"path1.parent.uid=0 path1.parent.gid=0"; |
301 |
|
if (!has_cond) |
302 |
|
policy = "allow_mkfifo /tmp/mknod_fifo_test"; |
303 |
if (write_policy()) { |
if (write_policy()) { |
304 |
filename = "/tmp/mknod_fifo_test"; |
filename = "/tmp/mknod_fifo_test"; |
305 |
show_result(mknod(filename, S_IFIFO, 0), 1); |
show_result(mknod(filename, S_IFIFO, 0), 1); |
319 |
"allow_write %s if path1.major=%u path1.minor=%u", |
"allow_write %s if path1.major=%u path1.minor=%u", |
320 |
filename, (unsigned int) MAJOR(sbuf.st_dev), |
filename, (unsigned int) MAJOR(sbuf.st_dev), |
321 |
(unsigned int) MINOR(sbuf.st_dev)); |
(unsigned int) MINOR(sbuf.st_dev)); |
322 |
|
if (!has_cond) |
323 |
|
snprintf(buffer, sizeof(buffer) - 1, |
324 |
|
"allow_write %s", filename); |
325 |
policy = buffer; |
policy = buffer; |
326 |
if (write_policy()) { |
if (write_policy()) { |
327 |
int fd = open(filename, O_WRONLY); |
int fd = open(filename, O_WRONLY); |
336 |
} |
} |
337 |
} |
} |
338 |
|
|
339 |
policy = "allow_read /dev/initctl if path1.type=fifo"; |
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()) { |
if (write_policy()) { |
344 |
int fd = open("/dev/initctl", O_RDONLY); |
int fd = open("/tmp/fifo", O_RDWR); |
345 |
show_result(fd, 1); |
show_result(fd, 1); |
346 |
if (fd != EOF) |
if (fd != EOF) |
347 |
close(fd); |
close(fd); |
348 |
delete_policy(); |
delete_policy(); |
349 |
fd = open("/dev/initctl", O_RDONLY); |
fd = open("/tmp/fifo", O_RDWR); |
350 |
show_result(fd, 0); |
show_result(fd, 0); |
351 |
if (fd != EOF) |
if (fd != EOF) |
352 |
close(fd); |
close(fd); |
353 |
} |
} |
354 |
|
|
355 |
policy = "allow_read /dev/null if path1.parent.ino=path1.parent.ino"; |
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()) { |
if (write_policy()) { |
359 |
int fd = open("/dev/null", O_RDONLY); |
int fd = open("/dev/null", O_RDONLY); |
360 |
show_result(fd, 1); |
show_result(fd, 1); |
368 |
} |
} |
369 |
|
|
370 |
policy = "allow_write /dev/null if path1.uid=path1.gid"; |
policy = "allow_write /dev/null if path1.uid=path1.gid"; |
371 |
|
if (!has_cond) |
372 |
|
policy = "allow_write /dev/null"; |
373 |
if (write_policy()) { |
if (write_policy()) { |
374 |
int fd = open("/dev/null", O_WRONLY); |
int fd = open("/dev/null", O_WRONLY); |
375 |
show_result(fd, 1); |
show_result(fd, 1); |
383 |
} |
} |
384 |
|
|
385 |
policy = "allow_read/write /dev/null if task.uid=path1.parent.uid"; |
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()) { |
if (write_policy()) { |
389 |
int fd = open("/dev/null", O_RDWR); |
int fd = open("/dev/null", O_RDWR); |
390 |
show_result(fd, 1); |
show_result(fd, 1); |
398 |
} |
} |
399 |
|
|
400 |
policy = "allow_create /tmp/open_test if path1.parent.uid=task.uid"; |
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()) { |
if (write_policy()) { |
404 |
policy = "allow_write /tmp/open_test if path1.parent.uid=0"; |
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()) { |
if (write_policy()) { |
408 |
int fd = open("/tmp/open_test", |
int fd = open("/tmp/open_test", |
409 |
O_WRONLY | O_CREAT | O_EXCL, 0666); |
O_WRONLY | O_CREAT | O_EXCL, 0666); |
420 |
unlink2("/tmp/open_test"); |
unlink2("/tmp/open_test"); |
421 |
} |
} |
422 |
policy = "allow_create /tmp/open_test " |
policy = "allow_create /tmp/open_test " |
423 |
"if path1.parent.uid=task.uid\n"; |
"if path1.parent.uid=task.uid"; |
424 |
|
if (!has_cond) |
425 |
|
policy = "allow_create /tmp/open_test"; |
426 |
delete_policy(); |
delete_policy(); |
427 |
} |
} |
428 |
|
|
429 |
policy = "allow_write /tmp/open_test if task.uid=0 path1.ino!=0"; |
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()) { |
if (write_policy()) { |
433 |
policy = "allow_create /tmp/open_test if 0=0"; |
policy = "allow_create /tmp/open_test if 0=0"; |
434 |
|
if (!has_cond) |
435 |
|
policy = "allow_create /tmp/open_test"; |
436 |
if (write_policy()) { |
if (write_policy()) { |
437 |
int fd = open("/tmp/open_test", |
int fd = open("/tmp/open_test", |
438 |
O_WRONLY | O_CREAT | O_EXCL, 0666); |
O_WRONLY | O_CREAT | O_EXCL, 0666); |
449 |
unlink2("/tmp/open_test"); |
unlink2("/tmp/open_test"); |
450 |
} |
} |
451 |
policy = "allow_write /tmp/open_test " |
policy = "allow_write /tmp/open_test " |
452 |
"if task.uid=0 path1.ino!=0\n"; |
"if task.uid=0 path1.ino!=0"; |
453 |
|
if (!has_cond) |
454 |
|
policy = "allow_write /tmp/open_test"; |
455 |
delete_policy(); |
delete_policy(); |
456 |
} |
} |
457 |
|
|
459 |
create2(filename); |
create2(filename); |
460 |
|
|
461 |
policy = "allow_truncate /tmp/truncate_test if task.uid=path1.uid"; |
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()) { |
if (write_policy()) { |
465 |
policy = "allow_write /tmp/truncate_test if 1!=100-1000000"; |
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()) { |
if (write_policy()) { |
469 |
int fd = open(filename, O_WRONLY | O_TRUNC); |
int fd = open(filename, O_WRONLY | O_TRUNC); |
470 |
show_result(fd, 1); |
show_result(fd, 1); |
478 |
} |
} |
479 |
policy = "allow_truncate /tmp/truncate_test " |
policy = "allow_truncate /tmp/truncate_test " |
480 |
"if task.uid=path1.uid"; |
"if task.uid=path1.uid"; |
481 |
|
if (!has_cond) |
482 |
|
policy = "allow_truncate /tmp/truncate_test"; |
483 |
delete_policy(); |
delete_policy(); |
484 |
} |
} |
485 |
|
|
497 |
if (fd != EOF) |
if (fd != EOF) |
498 |
close(fd); |
close(fd); |
499 |
} |
} |
500 |
policy = "allow_write /tmp/truncate_test\n"; |
policy = "allow_write /tmp/truncate_test"; |
501 |
delete_policy(); |
delete_policy(); |
502 |
} |
} |
503 |
|
|
610 |
show_result(symlink("/tmp/symlink_dest_test", filename), 0); |
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"; |
policy = "allow_link /tmp/link_source_test /tmp/link_dest_test"; |
659 |
if (write_policy()) { |
if (write_policy()) { |
660 |
filename = "/tmp/link_source_test"; |
filename = "/tmp/link_source_test"; |
757 |
write(exception_fd, cp, strlen(cp)); |
write(exception_fd, cp, strlen(cp)); |
758 |
} |
} |
759 |
unlink2(filename); |
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[]) |
int main(int argc, char *argv[]) |
790 |
char *cp; |
char *cp; |
791 |
ccs_test_init(); |
ccs_test_init(); |
792 |
domain_fd = open(proc_policy_domain_policy, O_WRONLY); |
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); |
exception_fd = open(proc_policy_exception_policy, O_WRONLY); |
799 |
{ |
{ |
800 |
int self_fd = open(proc_policy_self_domain, O_RDONLY); |
int self_fd = open(proc_policy_self_domain, O_RDONLY); |
809 |
cp = "use_profile 255\n"; |
cp = "use_profile 255\n"; |
810 |
write(domain_fd, cp, strlen(cp)); |
write(domain_fd, cp, strlen(cp)); |
811 |
} |
} |
812 |
|
has_cond = !access("/proc/ccs/version", F_OK); |
813 |
cp = "255-MAX_REJECT_LOG=1024\n"; |
cp = "255-MAX_REJECT_LOG=1024\n"; |
814 |
write(profile_fd, cp, strlen(cp)); |
write(profile_fd, cp, strlen(cp)); |
815 |
stage_file_test(); |
stage_file_test(); |