オープンソース・ソフトウェアの開発とダウンロード

Subversion リポジトリの参照

Contents of /branches/kernel_test/ccs_new_file_test.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1996 - (show annotations) (download) (as text)
Mon Dec 22 06:08:22 2008 UTC (15 years, 4 months ago) by kumaneko
Original Path: trunk/1.6.x/ccs-tools/ccstools/kernel_test/tomoyo_new_file_test.c
File MIME type: text/x-csrc
File size: 17300 byte(s)


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.6-pre 2008/12/22
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
18 static int write_policy(void)
19 {
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)
40 *cp = '\0';
41 if (!strncmp(buffer, "<kernel>", 8))
42 domain_found = !strcmp(self_domain, buffer);
43 if (domain_found) {
44 /* printf("<%s>\n", buffer); */
45 if (!strcmp(buffer, policy)) {
46 policy_found = 1;
47 break;
48 }
49 }
50 }
51 fclose(fp);
52 if (!policy_found) {
53 printf("%s : BUG: policy write failed\n", policy);
54 return 0;
55 }
56 errno = 0;
57 return 1;
58 }
59
60 static void delete_policy(void)
61 {
62 write(domain_fd, "delete ", 7);
63 write(domain_fd, policy, strlen(policy));
64 write(domain_fd, "\n", 1);
65 }
66
67 static void show_result(int result, char should_success)
68 {
69 int err = errno;
70 printf("%s : ", policy);
71 if (should_success) {
72 if (result != EOF)
73 printf("OK\n");
74 else
75 printf("FAILED: %s\n", strerror(err));
76 } else {
77 if (result == EOF) {
78 if (err == EPERM)
79 printf("OK: Permission denied.\n");
80 else
81 printf("FAILED: %s\n", strerror(err));
82 } else {
83 printf("BUG: didn't fail.\n");
84 }
85 }
86 }
87
88 static void create2(const char *pathname)
89 {
90 const char *cp = "255-MAC_FOR_FILE=disabled\n";
91 write(profile_fd, cp, strlen(cp));
92 close(creat(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 mkdir2(const char *pathname)
99 {
100 const char *cp = "255-MAC_FOR_FILE=disabled\n";
101 write(profile_fd, cp, strlen(cp));
102 mkdir(pathname, 0600);
103 cp = "255-MAC_FOR_FILE=enforcing\n";
104 write(profile_fd, cp, strlen(cp));
105 errno = 0;
106 }
107
108 static void unlink2(const char *pathname)
109 {
110 const char *cp = "255-MAC_FOR_FILE=disabled\n";
111 write(profile_fd, cp, strlen(cp));
112 unlink(pathname);
113 cp = "255-MAC_FOR_FILE=enforcing\n";
114 write(profile_fd, cp, strlen(cp));
115 errno = 0;
116 }
117
118 static void rmdir2(const char *pathname)
119 {
120 const char *cp = "255-MAC_FOR_FILE=disabled\n";
121 write(profile_fd, cp, strlen(cp));
122 rmdir(pathname);
123 cp = "255-MAC_FOR_FILE=enforcing\n";
124 write(profile_fd, cp, strlen(cp));
125 errno = 0;
126 }
127
128 static void stage_file_test(void)
129 {
130 char *filename = "";
131 policy = "allow_read /proc/sys/net/ipv4/ip_local_port_range "
132 "if task.uid=0 task.gid=0";
133 if (write_policy()) {
134 static int name[] = { CTL_NET, NET_IPV4,
135 NET_IPV4_LOCAL_PORT_RANGE };
136 int buffer[2] = { 32768, 61000 };
137 size_t size = sizeof(buffer);
138 show_result(sysctl(name, 3, buffer, &size, 0, 0), 1);
139 delete_policy();
140 show_result(sysctl(name, 3, buffer, &size, 0, 0), 0);
141 }
142 policy = "allow_write /proc/sys/net/ipv4/ip_local_port_range "
143 "if task.euid=0 0=0 1-100=10-1000";
144 if (write_policy()) {
145 static int name[] = { CTL_NET, NET_IPV4,
146 NET_IPV4_LOCAL_PORT_RANGE };
147 int buffer[2] = { 32768, 61000 };
148 size_t size = sizeof(buffer);
149 show_result(sysctl(name, 3, 0, 0, buffer, size), 1);
150 delete_policy();
151 show_result(sysctl(name, 3, 0, 0, buffer, size), 0);
152 }
153 policy = "allow_read/write /proc/sys/net/ipv4/ip_local_port_range "
154 "if 1!=10-100";
155 if (write_policy()) {
156 static int name[] = { CTL_NET, NET_IPV4,
157 NET_IPV4_LOCAL_PORT_RANGE };
158 int buffer[2] = { 32768, 61000 };
159 size_t size = sizeof(buffer);
160 show_result(sysctl(name, 3, buffer, &size, buffer, size), 1);
161 delete_policy();
162 show_result(sysctl(name, 3, buffer, &size, buffer, size), 0);
163 }
164
165 policy = "allow_read /bin/true "
166 "if path1.uid=0 path1.parent.uid=0 10=10-100";
167 if (write_policy()) {
168 show_result(uselib("/bin/true"), 1);
169 delete_policy();
170 show_result(uselib("/bin/true"), 0);
171 }
172
173 policy = "allow_execute /bin/true if task.uid!=10 path1.parent.uid=0";
174 if (write_policy()) {
175 int pipe_fd[2] = { EOF, EOF };
176 int err = 0;
177 fflush(stdout);
178 fflush(stderr);
179 pipe(pipe_fd);
180 if (fork() == 0) {
181 execl("/bin/true", "/bin/true", NULL);
182 err = errno;
183 write(pipe_fd[1], &err, sizeof(err));
184 _exit(0);
185 }
186 close(pipe_fd[1]);
187 read(pipe_fd[0], &err, sizeof(err));
188 close(pipe_fd[0]);
189 wait(NULL);
190 errno = err;
191 show_result(err ? EOF : 0, 1);
192 delete_policy();
193 fflush(stdout);
194 fflush(stderr);
195 pipe(pipe_fd);
196 if (fork() == 0) {
197 execl("/bin/true", "/bin/true", NULL);
198 err = errno;
199 write(pipe_fd[1], &err, sizeof(err));
200 _exit(0);
201 }
202 close(pipe_fd[1]);
203 read(pipe_fd[0], &err, sizeof(err));
204 close(pipe_fd[0]);
205 wait(NULL);
206 errno = err;
207 show_result(err ? EOF : 0, 0);
208 }
209
210 policy = "allow_read /dev/null if path1.type=char path1.dev_major=1 "
211 "path1.dev_minor=3";
212 if (write_policy()) {
213 int fd = open("/dev/null", O_RDONLY);
214 show_result(fd, 1);
215 if (fd != EOF)
216 close(fd);
217 delete_policy();
218 fd = open("/dev/null", O_RDONLY);
219 show_result(fd, 0);
220 if (fd != EOF)
221 close(fd);
222 }
223
224 policy = "allow_read /dev/null if path1.perm=0666";
225 if (write_policy()) {
226 int fd = open("/dev/null", O_RDONLY);
227 show_result(fd, 1);
228 if (fd != EOF)
229 close(fd);
230 delete_policy();
231 fd = open("/dev/null", O_RDONLY);
232 show_result(fd, 0);
233 if (fd != EOF)
234 close(fd);
235 }
236
237 policy = "allow_read /dev/null if path1.perm!=0777";
238 if (write_policy()) {
239 int fd = open("/dev/null", O_RDONLY);
240 show_result(fd, 1);
241 if (fd != EOF)
242 close(fd);
243 delete_policy();
244 fd = open("/dev/null", O_RDONLY);
245 show_result(fd, 0);
246 if (fd != EOF)
247 close(fd);
248 }
249
250 policy = "allow_read /dev/null if path1.perm=owner_read "
251 "path1.perm=owner_write path1.perm!=owner_execute "
252 "path1.perm=group_read path1.perm=group_write "
253 "path1.perm!=group_execute path1.perm=others_read "
254 "path1.perm=others_write path1.perm!=others_execute "
255 "path1.perm!=setuid path1.perm!=setgid path1.perm!=sticky";
256 if (write_policy()) {
257 int fd = open("/dev/null", O_RDONLY);
258 show_result(fd, 1);
259 if (fd != EOF)
260 close(fd);
261 delete_policy();
262 fd = open("/dev/null", O_RDONLY);
263 show_result(fd, 0);
264 if (fd != EOF)
265 close(fd);
266 }
267
268 policy = "allow_mkfifo /tmp/mknod_fifo_test "
269 "if path1.parent.perm=01777 path1.parent.perm=sticky "
270 "path1.parent.uid=0 path1.parent.gid=0";
271 if (write_policy()) {
272 filename = "/tmp/mknod_fifo_test";
273 show_result(mknod(filename, S_IFIFO, 0), 1);
274 delete_policy();
275 unlink2(filename);
276 show_result(mknod(filename, S_IFIFO, 0), 0);
277 }
278
279 {
280 char buffer[1024];
281 struct stat sbuf;
282 memset(buffer, 0, sizeof(buffer));
283 memset(&sbuf, 0, sizeof(sbuf));
284 filename = "/dev/null";
285 stat(filename, &sbuf);
286 snprintf(buffer, sizeof(buffer) - 1,
287 "allow_write %s if path1.major=%u path1.minor=%u",
288 filename, (unsigned int) MAJOR(sbuf.st_dev),
289 (unsigned int) MINOR(sbuf.st_dev));
290 policy = buffer;
291 if (write_policy()) {
292 int fd = open(filename, O_WRONLY);
293 show_result(fd, 1);
294 if (fd != EOF)
295 close(fd);
296 delete_policy();
297 fd = open(filename, O_WRONLY);
298 show_result(fd, 0);
299 if (fd != EOF)
300 close(fd);
301 }
302 }
303
304 policy = "allow_read /dev/initctl if path1.type=fifo";
305 if (write_policy()) {
306 int fd = open("/dev/initctl", O_RDONLY);
307 show_result(fd, 1);
308 if (fd != EOF)
309 close(fd);
310 delete_policy();
311 fd = open("/dev/initctl", O_RDONLY);
312 show_result(fd, 0);
313 if (fd != EOF)
314 close(fd);
315 }
316
317 policy = "allow_read /dev/null if path1.parent.ino=path1.parent.ino";
318 if (write_policy()) {
319 int fd = open("/dev/null", O_RDONLY);
320 show_result(fd, 1);
321 if (fd != EOF)
322 close(fd);
323 delete_policy();
324 fd = open("/dev/null", O_RDONLY);
325 show_result(fd, 0);
326 if (fd != EOF)
327 close(fd);
328 }
329
330 policy = "allow_write /dev/null if path1.uid=path1.gid";
331 if (write_policy()) {
332 int fd = open("/dev/null", O_WRONLY);
333 show_result(fd, 1);
334 if (fd != EOF)
335 close(fd);
336 delete_policy();
337 fd = open("/dev/null", O_WRONLY);
338 show_result(fd, 0);
339 if (fd != EOF)
340 close(fd);
341 }
342
343 policy = "allow_read/write /dev/null if task.uid=path1.parent.uid";
344 if (write_policy()) {
345 int fd = open("/dev/null", O_RDWR);
346 show_result(fd, 1);
347 if (fd != EOF)
348 close(fd);
349 delete_policy();
350 fd = open("/dev/null", O_RDWR);
351 show_result(fd, 0);
352 if (fd != EOF)
353 close(fd);
354 }
355
356 policy = "allow_create /tmp/open_test if path1.parent.uid=task.uid";
357 if (write_policy()) {
358 policy = "allow_write /tmp/open_test if path1.parent.uid=0";
359 if (write_policy()) {
360 int fd = open("/tmp/open_test",
361 O_WRONLY | O_CREAT | O_EXCL, 0666);
362 show_result(fd, 1);
363 if (fd != EOF)
364 close(fd);
365 unlink2("/tmp/open_test");
366 delete_policy();
367 fd = open("/tmp/open_test",
368 O_WRONLY | O_CREAT | O_EXCL, 0666);
369 show_result(fd, 0);
370 if (fd != EOF)
371 close(fd);
372 unlink2("/tmp/open_test");
373 }
374 policy = "allow_create /tmp/open_test "
375 "if path1.parent.uid=task.uid\n";
376 delete_policy();
377 }
378
379 policy = "allow_write /tmp/open_test if task.uid=0 path1.ino!=0";
380 if (write_policy()) {
381 policy = "allow_create /tmp/open_test if 0=0";
382 if (write_policy()) {
383 int fd = open("/tmp/open_test",
384 O_WRONLY | O_CREAT | O_EXCL, 0666);
385 show_result(fd, 1);
386 if (fd != EOF)
387 close(fd);
388 unlink2("/tmp/open_test");
389 delete_policy();
390 fd = open("/tmp/open_test",
391 O_WRONLY | O_CREAT | O_EXCL, 0666);
392 show_result(fd, 0);
393 if (fd != EOF)
394 close(fd);
395 unlink2("/tmp/open_test");
396 }
397 policy = "allow_write /tmp/open_test "
398 "if task.uid=0 path1.ino!=0\n";
399 delete_policy();
400 }
401
402 filename = "/tmp/truncate_test";
403 create2(filename);
404
405 policy = "allow_truncate /tmp/truncate_test if task.uid=path1.uid";
406 if (write_policy()) {
407 policy = "allow_write /tmp/truncate_test if 1!=100-1000000";
408 if (write_policy()) {
409 int fd = open(filename, O_WRONLY | O_TRUNC);
410 show_result(fd, 1);
411 if (fd != EOF)
412 close(fd);
413 delete_policy();
414 fd = open(filename, O_WRONLY | O_TRUNC);
415 show_result(fd, 0);
416 if (fd != EOF)
417 close(fd);
418 }
419 policy = "allow_truncate /tmp/truncate_test "
420 "if task.uid=path1.uid";
421 delete_policy();
422 }
423
424 policy = "allow_write /tmp/truncate_test";
425 if (write_policy()) {
426 policy = "allow_truncate /tmp/truncate_test";
427 if (write_policy()) {
428 int fd = open(filename, O_WRONLY | O_TRUNC);
429 show_result(fd, 1);
430 if (fd != EOF)
431 close(fd);
432 delete_policy();
433 fd = open(filename, O_WRONLY | O_TRUNC);
434 show_result(fd, 0);
435 if (fd != EOF)
436 close(fd);
437 }
438 policy = "allow_write /tmp/truncate_test\n";
439 delete_policy();
440 }
441
442 policy = "allow_truncate /tmp/truncate_test";
443 if (write_policy()) {
444 show_result(truncate(filename, 0), 1);
445 delete_policy();
446 show_result(truncate(filename, 0), 0);
447 }
448
449 policy = "allow_truncate /tmp/truncate_test";
450 if (write_policy()) {
451 int fd;
452 const char *cp = "255-MAC_FOR_FILE=disabled\n";
453 write(profile_fd, cp, strlen(cp));
454 fd = open(filename, O_WRONLY);
455 cp = "255-MAC_FOR_FILE=enforcing\n";
456 write(profile_fd, cp, strlen(cp));
457 show_result(ftruncate(fd, 0), 1);
458 delete_policy();
459 show_result(ftruncate(fd, 0), 0);
460 if (fd != EOF)
461 close(fd);
462 }
463
464 unlink2(filename);
465
466 policy = "allow_create /tmp/mknod_reg_test";
467 if (write_policy()) {
468 filename = "/tmp/mknod_reg_test";
469 show_result(mknod(filename, S_IFREG, 0), 1);
470 delete_policy();
471 unlink2(filename);
472 show_result(mknod(filename, S_IFREG, 0), 0);
473 }
474
475 policy = "allow_mkchar /tmp/mknod_chr_test";
476 if (write_policy()) {
477 filename = "/tmp/mknod_chr_test";
478 show_result(mknod(filename, S_IFCHR, MKDEV(1, 3)), 1);
479 delete_policy();
480 unlink2(filename);
481 show_result(mknod(filename, S_IFCHR, MKDEV(1, 3)), 0);
482 }
483
484 policy = "allow_mkblock /tmp/mknod_blk_test";
485 if (write_policy()) {
486 filename = "/tmp/mknod_blk_test";
487 show_result(mknod(filename, S_IFBLK, MKDEV(1, 0)), 1);
488 delete_policy();
489 unlink2(filename);
490 show_result(mknod(filename, S_IFBLK, MKDEV(1, 0)), 0);
491 }
492
493 policy = "allow_mkfifo /tmp/mknod_fifo_test";
494 if (write_policy()) {
495 filename = "/tmp/mknod_fifo_test";
496 show_result(mknod(filename, S_IFIFO, 0), 1);
497 delete_policy();
498 unlink2(filename);
499 show_result(mknod(filename, S_IFIFO, 0), 0);
500 }
501
502 policy = "allow_mksock /tmp/mknod_sock_test";
503 if (write_policy()) {
504 filename = "/tmp/mknod_sock_test";
505 show_result(mknod(filename, S_IFSOCK, 0), 1);
506 delete_policy();
507 unlink2(filename);
508 show_result(mknod(filename, S_IFSOCK, 0), 0);
509 }
510
511 policy = "allow_mkdir /tmp/mkdir_test/";
512 if (write_policy()) {
513 filename = "/tmp/mkdir_test";
514 show_result(mkdir(filename, 0600), 1);
515 delete_policy();
516 rmdir2(filename);
517 show_result(mkdir(filename, 0600), 0);
518 }
519
520 policy = "allow_rmdir /tmp/rmdir_test/";
521 if (write_policy()) {
522 filename = "/tmp/rmdir_test";
523 mkdir2(filename);
524 show_result(rmdir(filename), 1);
525 delete_policy();
526 mkdir2(filename);
527 show_result(rmdir(filename), 0);
528 rmdir2(filename);
529 }
530
531 policy = "allow_unlink /tmp/unlink_test";
532 if (write_policy()) {
533 filename = "/tmp/unlink_test";
534 create2(filename);
535 show_result(unlink(filename), 1);
536 delete_policy();
537 create2(filename);
538 show_result(unlink(filename), 0);
539 unlink2(filename);
540 }
541
542 policy = "allow_symlink /tmp/symlink_source_test";
543 if (write_policy()) {
544 filename = "/tmp/symlink_source_test";
545 show_result(symlink("/tmp/symlink_dest_test", filename), 1);
546 delete_policy();
547 unlink2(filename);
548 show_result(symlink("/tmp/symlink_dest_test", filename), 0);
549 }
550
551 policy = "allow_link /tmp/link_source_test /tmp/link_dest_test";
552 if (write_policy()) {
553 filename = "/tmp/link_source_test";
554 create2(filename);
555 show_result(link(filename, "/tmp/link_dest_test"), 1);
556 delete_policy();
557 unlink2("/tmp/link_dest_test");
558 show_result(link(filename, "/tmp/link_dest_test"), 0);
559 unlink2(filename);
560 }
561
562 policy = "allow_rename /tmp/rename_source_test /tmp/rename_dest_test";
563 if (write_policy()) {
564 filename = "/tmp/rename_source_test";
565 create2(filename);
566 show_result(rename(filename, "/tmp/rename_dest_test"), 1);
567 delete_policy();
568 unlink2("/tmp/rename_dest_test");
569 create2(filename);
570 show_result(rename(filename, "/tmp/rename_dest_test"), 0);
571 unlink2(filename);
572 }
573
574 policy = "allow_mksock /tmp/socket_test";
575 if (write_policy()) {
576 struct sockaddr_un addr;
577 int fd;
578 filename = "/tmp/socket_test";
579 memset(&addr, 0, sizeof(addr));
580 addr.sun_family = AF_UNIX;
581 strncpy(addr.sun_path, filename, sizeof(addr.sun_path) - 1);
582 fd = socket(AF_UNIX, SOCK_STREAM, 0);
583 show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)),
584 1);
585 if (fd != EOF)
586 close(fd);
587 delete_policy();
588 unlink2(filename);
589 fd = socket(AF_UNIX, SOCK_STREAM, 0);
590 show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)),
591 0);
592 if (fd != EOF)
593 close(fd);
594 }
595
596 filename = "/tmp/rewrite_test";
597 create2(filename);
598 policy = "allow_read/write /tmp/rewrite_test";
599 if (write_policy()) {
600 char *cp = "deny_rewrite /tmp/rewrite_test\n";
601 write(exception_fd, cp, strlen(cp));
602 policy = "allow_truncate /tmp/rewrite_test";
603 if (write_policy()) {
604 int fd;
605
606 fd = open(filename, O_RDONLY);
607 show_result(fd, 1);
608 if (fd != EOF)
609 close(fd);
610
611 fd = open(filename, O_WRONLY | O_APPEND);
612 show_result(fd, 1);
613 if (fd != EOF)
614 close(fd);
615
616 fd = open(filename, O_WRONLY);
617 show_result(fd, 0);
618 if (fd != EOF)
619 close(fd);
620
621 fd = open(filename, O_WRONLY | O_TRUNC);
622 show_result(fd, 0);
623 if (fd != EOF)
624 close(fd);
625
626 fd = open(filename, O_WRONLY | O_TRUNC | O_APPEND);
627 show_result(fd, 0);
628 if (fd != EOF)
629 close(fd);
630
631 show_result(truncate(filename, 0), 0);
632
633 cp = "255-MAC_FOR_FILE=disabled\n";
634 write(profile_fd, cp, strlen(cp));
635 fd = open(filename, O_WRONLY | O_APPEND);
636 cp = "255-MAC_FOR_FILE=enforcing\n";
637 write(profile_fd, cp, strlen(cp));
638 show_result(ftruncate(fd, 0), 0);
639
640 show_result(fcntl(fd, F_SETFL,
641 fcntl(fd, F_GETFL) & ~O_APPEND), 0);
642 if (fd != EOF)
643 close(fd);
644
645 delete_policy();
646 }
647 policy = "allow_read/write /tmp/rewrite_test";
648 delete_policy();
649 cp = "delete deny_rewrite /tmp/rewrite_test\n";
650 write(exception_fd, cp, strlen(cp));
651 }
652 unlink2(filename);
653 }
654
655 int main(int argc, char *argv[])
656 {
657 char *cp;
658 ccs_test_init();
659 domain_fd = open(proc_policy_domain_policy, O_WRONLY);
660 exception_fd = open(proc_policy_exception_policy, O_WRONLY);
661 {
662 int self_fd = open(proc_policy_self_domain, O_RDONLY);
663 memset(self_domain, 0, sizeof(self_domain));
664 read(self_fd, self_domain, sizeof(self_domain) - 1);
665 close(self_fd);
666 write(domain_fd, self_domain, strlen(self_domain));
667 cp = " /bin/true\n";
668 write(domain_fd, cp, strlen(cp));
669 write(domain_fd, self_domain, strlen(self_domain));
670 write(domain_fd, "\n", 1);
671 cp = "use_profile 255\n";
672 write(domain_fd, cp, strlen(cp));
673 }
674 cp = "255-MAX_REJECT_LOG=1024\n";
675 write(profile_fd, cp, strlen(cp));
676 stage_file_test();
677 cp = "use_profile 0\n";
678 write(domain_fd, cp, strlen(cp));
679 clear_status();
680 return 0;
681 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26