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

Subversion リポジトリの参照

Contents of /trunk/1.6.x/ccs-tools/ccstools/kernel_test/tomoyo_new_file_test.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1995 - (show annotations) (download) (as text)
Mon Dec 22 05:07:13 2008 UTC (15 years, 5 months ago) by kumaneko
File MIME type: text/x-csrc
File size: 16064 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 #if 0
131 char *filename = "";
132 policy = "allow_read /proc/sys/net/ipv4/ip_local_port_range "
133 "if task.uid=0 task.gid=0";
134 if (write_policy()) {
135 static int name[] = { CTL_NET, NET_IPV4,
136 NET_IPV4_LOCAL_PORT_RANGE };
137 int buffer[2] = { 32768, 61000 };
138 size_t size = sizeof(buffer);
139 show_result(sysctl(name, 3, buffer, &size, 0, 0), 1);
140 delete_policy();
141 show_result(sysctl(name, 3, buffer, &size, 0, 0), 0);
142 }
143 policy = "allow_write /proc/sys/net/ipv4/ip_local_port_range "
144 "if task.euid=0 0=0 1-100=10-1000";
145 if (write_policy()) {
146 static int name[] = { CTL_NET, NET_IPV4,
147 NET_IPV4_LOCAL_PORT_RANGE };
148 int buffer[2] = { 32768, 61000 };
149 size_t size = sizeof(buffer);
150 show_result(sysctl(name, 3, 0, 0, buffer, size), 1);
151 delete_policy();
152 show_result(sysctl(name, 3, 0, 0, buffer, size), 0);
153 }
154 policy = "allow_read/write /proc/sys/net/ipv4/ip_local_port_range "
155 "if 1!=10-100";
156 if (write_policy()) {
157 static int name[] = { CTL_NET, NET_IPV4,
158 NET_IPV4_LOCAL_PORT_RANGE };
159 int buffer[2] = { 32768, 61000 };
160 size_t size = sizeof(buffer);
161 show_result(sysctl(name, 3, buffer, &size, buffer, size), 1);
162 delete_policy();
163 show_result(sysctl(name, 3, buffer, &size, buffer, size), 0);
164 }
165
166 policy = "allow_read /bin/true "
167 "if path1.uid=0 path1.parent.uid=0 10=10-100";
168 if (write_policy()) {
169 show_result(uselib("/bin/true"), 1);
170 delete_policy();
171 show_result(uselib("/bin/true"), 0);
172 }
173
174 policy = "allow_execute /bin/true if task.uid!=10 path1.parent.uid=0";
175 if (write_policy()) {
176 int pipe_fd[2] = { EOF, EOF };
177 int err = 0;
178 fflush(stdout);
179 fflush(stderr);
180 pipe(pipe_fd);
181 if (fork() == 0) {
182 execl("/bin/true", "/bin/true", NULL);
183 err = errno;
184 write(pipe_fd[1], &err, sizeof(err));
185 _exit(0);
186 }
187 close(pipe_fd[1]);
188 read(pipe_fd[0], &err, sizeof(err));
189 close(pipe_fd[0]);
190 wait(NULL);
191 errno = err;
192 show_result(err ? EOF : 0, 1);
193 delete_policy();
194 fflush(stdout);
195 fflush(stderr);
196 pipe(pipe_fd);
197 if (fork() == 0) {
198 execl("/bin/true", "/bin/true", NULL);
199 err = errno;
200 write(pipe_fd[1], &err, sizeof(err));
201 _exit(0);
202 }
203 close(pipe_fd[1]);
204 read(pipe_fd[0], &err, sizeof(err));
205 close(pipe_fd[0]);
206 wait(NULL);
207 errno = err;
208 show_result(err ? EOF : 0, 0);
209 }
210 #endif
211
212 policy = "allow_read /dev/null if path1.type=char path1.dev_major=1 path1.dev_minor=3";
213 if (write_policy()) {
214 int fd = open("/dev/null", O_RDONLY);
215 show_result(fd, 1);
216 if (fd != EOF)
217 close(fd);
218 delete_policy();
219 fd = open("/dev/null", O_RDONLY);
220 show_result(fd, 0);
221 if (fd != EOF)
222 close(fd);
223 }
224
225 policy = "allow_read /dev/null if path1.perm=0666";
226 if (write_policy()) {
227 int fd = open("/dev/null", O_RDONLY);
228 show_result(fd, 1);
229 if (fd != EOF)
230 close(fd);
231 delete_policy();
232 fd = open("/dev/null", O_RDONLY);
233 show_result(fd, 0);
234 if (fd != EOF)
235 close(fd);
236 }
237
238 policy = "allow_read /dev/null if path1.perm=owner_read path1.perm=owner_write path1.perm!=owner_execute path1.perm=group_read path1.perm=group_write path1.perm!=group_execute path1.perm=others_read path1.perm=others_write path1.perm!=others_execute path1.perm!=setuid path1.perm!=setgid path1.perm!=sticky";
239 if (write_policy()) {
240 int fd = open("/dev/null", O_RDONLY);
241 show_result(fd, 1);
242 if (fd != EOF)
243 close(fd);
244 delete_policy();
245 fd = open("/dev/null", O_RDONLY);
246 show_result(fd, 0);
247 if (fd != EOF)
248 close(fd);
249 }
250
251 policy = "allow_read /dev/initctl if path1.type=fifo";
252 if (write_policy()) {
253 int fd = open("/dev/initctl", O_RDONLY);
254 show_result(fd, 1);
255 if (fd != EOF)
256 close(fd);
257 delete_policy();
258 fd = open("/dev/initctl", O_RDONLY);
259 show_result(fd, 0);
260 if (fd != EOF)
261 close(fd);
262 }
263 #if 0
264 policy = "allow_read /dev/null if path1.parent.ino=path1.parent.ino";
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_write /dev/null if path1.uid=path1.gid";
278 if (write_policy()) {
279 int fd = open("/dev/null", O_WRONLY);
280 show_result(fd, 1);
281 if (fd != EOF)
282 close(fd);
283 delete_policy();
284 fd = open("/dev/null", O_WRONLY);
285 show_result(fd, 0);
286 if (fd != EOF)
287 close(fd);
288 }
289
290 policy = "allow_read/write /dev/null if task.uid=path1.parent.uid";
291 if (write_policy()) {
292 int fd = open("/dev/null", O_RDWR);
293 show_result(fd, 1);
294 if (fd != EOF)
295 close(fd);
296 delete_policy();
297 fd = open("/dev/null", O_RDWR);
298 show_result(fd, 0);
299 if (fd != EOF)
300 close(fd);
301 }
302
303 policy = "allow_create /tmp/open_test if path1.parent.uid=task.uid";
304 if (write_policy()) {
305 policy = "allow_write /tmp/open_test if path1.parent.uid=0";
306 if (write_policy()) {
307 int fd = open("/tmp/open_test",
308 O_WRONLY | O_CREAT | O_EXCL, 0666);
309 show_result(fd, 1);
310 if (fd != EOF)
311 close(fd);
312 unlink2("/tmp/open_test");
313 delete_policy();
314 fd = open("/tmp/open_test",
315 O_WRONLY | O_CREAT | O_EXCL, 0666);
316 show_result(fd, 0);
317 if (fd != EOF)
318 close(fd);
319 unlink2("/tmp/open_test");
320 }
321 policy = "allow_create /tmp/open_test "
322 "if path1.parent.uid=task.uid\n";
323 delete_policy();
324 }
325
326 policy = "allow_write /tmp/open_test if task.uid=0 path1.ino!=0";
327 if (write_policy()) {
328 policy = "allow_create /tmp/open_test if 0=0";
329 if (write_policy()) {
330 int fd = open("/tmp/open_test",
331 O_WRONLY | O_CREAT | O_EXCL, 0666);
332 show_result(fd, 1);
333 if (fd != EOF)
334 close(fd);
335 unlink2("/tmp/open_test");
336 delete_policy();
337 fd = open("/tmp/open_test",
338 O_WRONLY | O_CREAT | O_EXCL, 0666);
339 show_result(fd, 0);
340 if (fd != EOF)
341 close(fd);
342 unlink2("/tmp/open_test");
343 }
344 policy = "allow_write /tmp/open_test "
345 "if task.uid=0 path1.ino!=0\n";
346 delete_policy();
347 }
348
349 filename = "/tmp/truncate_test";
350 create2(filename);
351
352 policy = "allow_truncate /tmp/truncate_test if task.uid=path1.uid";
353 if (write_policy()) {
354 policy = "allow_write /tmp/truncate_test if 1!=100-1000000";
355 if (write_policy()) {
356 int fd = open(filename, O_WRONLY | O_TRUNC);
357 show_result(fd, 1);
358 if (fd != EOF)
359 close(fd);
360 delete_policy();
361 fd = open(filename, O_WRONLY | O_TRUNC);
362 show_result(fd, 0);
363 if (fd != EOF)
364 close(fd);
365 }
366 policy = "allow_truncate /tmp/truncate_test "
367 "if task.uid=path1.uid";
368 delete_policy();
369 }
370
371 policy = "allow_write /tmp/truncate_test";
372 if (write_policy()) {
373 policy = "allow_truncate /tmp/truncate_test";
374 if (write_policy()) {
375 int fd = open(filename, O_WRONLY | O_TRUNC);
376 show_result(fd, 1);
377 if (fd != EOF)
378 close(fd);
379 delete_policy();
380 fd = open(filename, O_WRONLY | O_TRUNC);
381 show_result(fd, 0);
382 if (fd != EOF)
383 close(fd);
384 }
385 policy = "allow_write /tmp/truncate_test\n";
386 delete_policy();
387 }
388
389 policy = "allow_truncate /tmp/truncate_test";
390 if (write_policy()) {
391 show_result(truncate(filename, 0), 1);
392 delete_policy();
393 show_result(truncate(filename, 0), 0);
394 }
395
396 policy = "allow_truncate /tmp/truncate_test";
397 if (write_policy()) {
398 int fd;
399 const char *cp = "255-MAC_FOR_FILE=disabled\n";
400 write(profile_fd, cp, strlen(cp));
401 fd = open(filename, O_WRONLY);
402 cp = "255-MAC_FOR_FILE=enforcing\n";
403 write(profile_fd, cp, strlen(cp));
404 show_result(ftruncate(fd, 0), 1);
405 delete_policy();
406 show_result(ftruncate(fd, 0), 0);
407 if (fd != EOF)
408 close(fd);
409 }
410
411 unlink2(filename);
412
413 policy = "allow_create /tmp/mknod_reg_test";
414 if (write_policy()) {
415 filename = "/tmp/mknod_reg_test";
416 show_result(mknod(filename, S_IFREG, 0), 1);
417 delete_policy();
418 unlink2(filename);
419 show_result(mknod(filename, S_IFREG, 0), 0);
420 }
421
422 policy = "allow_mkchar /tmp/mknod_chr_test";
423 if (write_policy()) {
424 filename = "/tmp/mknod_chr_test";
425 show_result(mknod(filename, S_IFCHR, MKDEV(1, 3)), 1);
426 delete_policy();
427 unlink2(filename);
428 show_result(mknod(filename, S_IFCHR, MKDEV(1, 3)), 0);
429 }
430
431 policy = "allow_mkblock /tmp/mknod_blk_test";
432 if (write_policy()) {
433 filename = "/tmp/mknod_blk_test";
434 show_result(mknod(filename, S_IFBLK, MKDEV(1, 0)), 1);
435 delete_policy();
436 unlink2(filename);
437 show_result(mknod(filename, S_IFBLK, MKDEV(1, 0)), 0);
438 }
439
440 policy = "allow_mkfifo /tmp/mknod_fifo_test";
441 if (write_policy()) {
442 filename = "/tmp/mknod_fifo_test";
443 show_result(mknod(filename, S_IFIFO, 0), 1);
444 delete_policy();
445 unlink2(filename);
446 show_result(mknod(filename, S_IFIFO, 0), 0);
447 }
448
449 policy = "allow_mksock /tmp/mknod_sock_test";
450 if (write_policy()) {
451 filename = "/tmp/mknod_sock_test";
452 show_result(mknod(filename, S_IFSOCK, 0), 1);
453 delete_policy();
454 unlink2(filename);
455 show_result(mknod(filename, S_IFSOCK, 0), 0);
456 }
457
458 policy = "allow_mkdir /tmp/mkdir_test/";
459 if (write_policy()) {
460 filename = "/tmp/mkdir_test";
461 show_result(mkdir(filename, 0600), 1);
462 delete_policy();
463 rmdir2(filename);
464 show_result(mkdir(filename, 0600), 0);
465 }
466
467 policy = "allow_rmdir /tmp/rmdir_test/";
468 if (write_policy()) {
469 filename = "/tmp/rmdir_test";
470 mkdir2(filename);
471 show_result(rmdir(filename), 1);
472 delete_policy();
473 mkdir2(filename);
474 show_result(rmdir(filename), 0);
475 rmdir2(filename);
476 }
477
478 policy = "allow_unlink /tmp/unlink_test";
479 if (write_policy()) {
480 filename = "/tmp/unlink_test";
481 create2(filename);
482 show_result(unlink(filename), 1);
483 delete_policy();
484 create2(filename);
485 show_result(unlink(filename), 0);
486 unlink2(filename);
487 }
488
489 policy = "allow_symlink /tmp/symlink_source_test";
490 if (write_policy()) {
491 filename = "/tmp/symlink_source_test";
492 show_result(symlink("/tmp/symlink_dest_test", filename), 1);
493 delete_policy();
494 unlink2(filename);
495 show_result(symlink("/tmp/symlink_dest_test", filename), 0);
496 }
497
498 policy = "allow_link /tmp/link_source_test /tmp/link_dest_test";
499 if (write_policy()) {
500 filename = "/tmp/link_source_test";
501 create2(filename);
502 show_result(link(filename, "/tmp/link_dest_test"), 1);
503 delete_policy();
504 unlink2("/tmp/link_dest_test");
505 show_result(link(filename, "/tmp/link_dest_test"), 0);
506 unlink2(filename);
507 }
508
509 policy = "allow_rename /tmp/rename_source_test /tmp/rename_dest_test";
510 if (write_policy()) {
511 filename = "/tmp/rename_source_test";
512 create2(filename);
513 show_result(rename(filename, "/tmp/rename_dest_test"), 1);
514 delete_policy();
515 unlink2("/tmp/rename_dest_test");
516 create2(filename);
517 show_result(rename(filename, "/tmp/rename_dest_test"), 0);
518 unlink2(filename);
519 }
520
521 policy = "allow_mksock /tmp/socket_test";
522 if (write_policy()) {
523 struct sockaddr_un addr;
524 int fd;
525 filename = "/tmp/socket_test";
526 memset(&addr, 0, sizeof(addr));
527 addr.sun_family = AF_UNIX;
528 strncpy(addr.sun_path, filename, sizeof(addr.sun_path) - 1);
529 fd = socket(AF_UNIX, SOCK_STREAM, 0);
530 show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)),
531 1);
532 if (fd != EOF)
533 close(fd);
534 delete_policy();
535 unlink2(filename);
536 fd = socket(AF_UNIX, SOCK_STREAM, 0);
537 show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)),
538 0);
539 if (fd != EOF)
540 close(fd);
541 }
542
543 filename = "/tmp/rewrite_test";
544 create2(filename);
545 policy = "allow_read/write /tmp/rewrite_test";
546 if (write_policy()) {
547 char *cp = "deny_rewrite /tmp/rewrite_test\n";
548 write(exception_fd, cp, strlen(cp));
549 policy = "allow_truncate /tmp/rewrite_test";
550 if (write_policy()) {
551 int fd;
552
553 fd = open(filename, O_RDONLY);
554 show_result(fd, 1);
555 if (fd != EOF)
556 close(fd);
557
558 fd = open(filename, O_WRONLY | O_APPEND);
559 show_result(fd, 1);
560 if (fd != EOF)
561 close(fd);
562
563 fd = open(filename, O_WRONLY);
564 show_result(fd, 0);
565 if (fd != EOF)
566 close(fd);
567
568 fd = open(filename, O_WRONLY | O_TRUNC);
569 show_result(fd, 0);
570 if (fd != EOF)
571 close(fd);
572
573 fd = open(filename, O_WRONLY | O_TRUNC | O_APPEND);
574 show_result(fd, 0);
575 if (fd != EOF)
576 close(fd);
577
578 show_result(truncate(filename, 0), 0);
579
580 cp = "255-MAC_FOR_FILE=disabled\n";
581 write(profile_fd, cp, strlen(cp));
582 fd = open(filename, O_WRONLY | O_APPEND);
583 cp = "255-MAC_FOR_FILE=enforcing\n";
584 write(profile_fd, cp, strlen(cp));
585 show_result(ftruncate(fd, 0), 0);
586
587 show_result(fcntl(fd, F_SETFL,
588 fcntl(fd, F_GETFL) & ~O_APPEND), 0);
589 if (fd != EOF)
590 close(fd);
591
592 delete_policy();
593 }
594 policy = "allow_read/write /tmp/rewrite_test";
595 delete_policy();
596 cp = "delete deny_rewrite /tmp/rewrite_test\n";
597 write(exception_fd, cp, strlen(cp));
598 }
599 unlink2(filename);
600 #endif
601 }
602
603 int main(int argc, char *argv[])
604 {
605 char *cp;
606 ccs_test_init();
607 domain_fd = open(proc_policy_domain_policy, O_WRONLY);
608 exception_fd = open(proc_policy_exception_policy, O_WRONLY);
609 {
610 int self_fd = open(proc_policy_self_domain, O_RDONLY);
611 memset(self_domain, 0, sizeof(self_domain));
612 read(self_fd, self_domain, sizeof(self_domain) - 1);
613 close(self_fd);
614 write(domain_fd, self_domain, strlen(self_domain));
615 cp = " /bin/true\n";
616 write(domain_fd, cp, strlen(cp));
617 write(domain_fd, self_domain, strlen(self_domain));
618 write(domain_fd, "\n", 1);
619 cp = "use_profile 255\n";
620 write(domain_fd, cp, strlen(cp));
621 }
622 cp = "255-MAX_REJECT_LOG=1024\n";
623 write(profile_fd, cp, strlen(cp));
624 stage_file_test();
625 cp = "use_profile 0\n";
626 write(domain_fd, cp, strlen(cp));
627 clear_status();
628 return 0;
629 }

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