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

Subversion リポジトリの参照

Annotation of /branches/ccs-tools/ccstools/kernel_test/ccs_new_file_test.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2563 - (hide annotations) (download) (as text)
Tue May 19 05:41:29 2009 UTC (15 years 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: 21306 byte(s)


1 kumaneko 1066 /*
2     * tomoyo_file_test.c
3     *
4     * Testing program for fs/tomoyo_file.c
5     *
6 kumaneko 2030 * Copyright (C) 2005-2009 NTT DATA CORPORATION
7 kumaneko 1066 *
8 kumaneko 2403 * Version: 1.6.7+ 2009/04/08
9 kumaneko 1066 *
10     */
11     #include "include.h"
12    
13     static int domain_fd = EOF;
14 kumaneko 1067 static int exception_fd = EOF;
15 kumaneko 1066 static const char *policy = "";
16     static char self_domain[4096] = "";
17 kumaneko 2194 static _Bool has_cond = 1;
18 kumaneko 1066
19 kumaneko 1726 static int write_policy(void)
20     {
21 kumaneko 1066 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 kumaneko 1726 if (cp)
41     *cp = '\0';
42     if (!strncmp(buffer, "<kernel>", 8))
43     domain_found = !strcmp(self_domain, buffer);
44 kumaneko 1066 if (domain_found) {
45 kumaneko 1726 /* printf("<%s>\n", buffer); */
46 kumaneko 1066 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 kumaneko 1726 static void delete_policy(void)
62     {
63 kumaneko 1066 write(domain_fd, "delete ", 7);
64     write(domain_fd, policy, strlen(policy));
65     write(domain_fd, "\n", 1);
66     }
67    
68 kumaneko 1726 static void show_result(int result, char should_success)
69     {
70 kumaneko 1066 int err = errno;
71     printf("%s : ", policy);
72     if (should_success) {
73 kumaneko 1726 if (result != EOF)
74     printf("OK\n");
75     else
76     printf("FAILED: %s\n", strerror(err));
77 kumaneko 1066 } else {
78     if (result == EOF) {
79 kumaneko 1726 if (err == EPERM)
80     printf("OK: Permission denied.\n");
81     else
82     printf("FAILED: %s\n", strerror(err));
83 kumaneko 1066 } else {
84     printf("BUG: didn't fail.\n");
85     }
86     }
87     }
88    
89 kumaneko 1726 static void create2(const char *pathname)
90     {
91 kumaneko 1066 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 kumaneko 1148 errno = 0;
97 kumaneko 1066 }
98    
99 kumaneko 1726 static void mkdir2(const char *pathname)
100     {
101 kumaneko 1066 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 kumaneko 1148 errno = 0;
107 kumaneko 1066 }
108    
109 kumaneko 1726 static void unlink2(const char *pathname)
110     {
111 kumaneko 1066 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 kumaneko 1148 errno = 0;
117 kumaneko 1726 }
118 kumaneko 1066
119 kumaneko 1726 static void rmdir2(const char *pathname)
120     {
121 kumaneko 1066 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 kumaneko 1148 errno = 0;
127 kumaneko 1066 }
128    
129 kumaneko 2563 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 kumaneko 1744 static void stage_file_test(void)
140 kumaneko 1726 {
141 kumaneko 1066 char *filename = "";
142 kumaneko 1726 policy = "allow_read /proc/sys/net/ipv4/ip_local_port_range "
143     "if task.uid=0 task.gid=0";
144 kumaneko 2194 if (!has_cond)
145     policy = "allow_read /proc/sys/net/ipv4/ip_local_port_range";
146 kumaneko 1066 if (write_policy()) {
147 kumaneko 1726 static int name[] = { CTL_NET, NET_IPV4,
148     NET_IPV4_LOCAL_PORT_RANGE };
149 kumaneko 1066 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 kumaneko 1726 policy = "allow_write /proc/sys/net/ipv4/ip_local_port_range "
156     "if task.euid=0 0=0 1-100=10-1000";
157 kumaneko 2194 if (!has_cond)
158     policy = "allow_write /proc/sys/net/ipv4/ip_local_port_range";
159 kumaneko 1066 if (write_policy()) {
160 kumaneko 1726 static int name[] = { CTL_NET, NET_IPV4,
161     NET_IPV4_LOCAL_PORT_RANGE };
162 kumaneko 1066 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 kumaneko 1726 policy = "allow_read/write /proc/sys/net/ipv4/ip_local_port_range "
169     "if 1!=10-100";
170 kumaneko 2194 if (!has_cond)
171     policy = "allow_read/write /proc/sys/net/ipv4/ip_local_port_range";
172 kumaneko 1066 if (write_policy()) {
173 kumaneko 1726 static int name[] = { CTL_NET, NET_IPV4,
174     NET_IPV4_LOCAL_PORT_RANGE };
175 kumaneko 1066 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 kumaneko 1726 policy = "allow_read /bin/true "
183     "if path1.uid=0 path1.parent.uid=0 10=10-100";
184 kumaneko 2194 if (!has_cond)
185     policy = "allow_read /bin/true";
186 kumaneko 1066 if (write_policy()) {
187     show_result(uselib("/bin/true"), 1);
188     delete_policy();
189     show_result(uselib("/bin/true"), 0);
190     }
191    
192 kumaneko 1664 policy = "allow_execute /bin/true if task.uid!=10 path1.parent.uid=0";
193 kumaneko 2194 if (!has_cond)
194     policy = "allow_execute /bin/true";
195 kumaneko 1066 if (write_policy()) {
196     int pipe_fd[2] = { EOF, EOF };
197     int err = 0;
198 kumaneko 1726 fflush(stdout);
199     fflush(stderr);
200 kumaneko 1066 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 kumaneko 1726 fflush(stdout);
215     fflush(stderr);
216 kumaneko 1066 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 kumaneko 1996 policy = "allow_read /dev/null if path1.type=char path1.dev_major=1 "
232     "path1.dev_minor=3";
233 kumaneko 2194 if (!has_cond)
234     policy = "allow_read /dev/null";
235 kumaneko 1995 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 kumaneko 2194 if (!has_cond)
249     policy = "allow_read /dev/null";
250 kumaneko 1995 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 kumaneko 1996 policy = "allow_read /dev/null if path1.perm!=0777";
263 kumaneko 2194 if (!has_cond)
264     policy = "allow_read /dev/null";
265 kumaneko 1995 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 kumaneko 1996 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 kumaneko 2194 if (!has_cond)
284     policy = "allow_read /dev/null";
285 kumaneko 1996 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 kumaneko 2194 if (!has_cond)
301     policy = "allow_mkfifo /tmp/mknod_fifo_test";
302 kumaneko 1996 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 kumaneko 2194 if (!has_cond)
322     snprintf(buffer, sizeof(buffer) - 1,
323     "allow_write %s", filename);
324 kumaneko 1996 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 kumaneko 2563 policy = "allow_read/write /tmp/fifo if path1.type=fifo";
339 kumaneko 2194 if (!has_cond)
340 kumaneko 2563 policy = "allow_read/write /tmp/fifo";
341     mkfifo2("/tmp/fifo");
342 kumaneko 1995 if (write_policy()) {
343 kumaneko 2563 int fd = open("/tmp/fifo", O_RDWR);
344 kumaneko 1995 show_result(fd, 1);
345     if (fd != EOF)
346     close(fd);
347     delete_policy();
348 kumaneko 2563 fd = open("/tmp/fifo", O_RDWR);
349 kumaneko 1995 show_result(fd, 0);
350     if (fd != EOF)
351     close(fd);
352     }
353 kumaneko 1996
354 kumaneko 1664 policy = "allow_read /dev/null if path1.parent.ino=path1.parent.ino";
355 kumaneko 2194 if (!has_cond)
356     policy = "allow_read /dev/null";
357 kumaneko 1066 if (write_policy()) {
358     int fd = open("/dev/null", O_RDONLY);
359     show_result(fd, 1);
360 kumaneko 1726 if (fd != EOF)
361     close(fd);
362 kumaneko 1066 delete_policy();
363     fd = open("/dev/null", O_RDONLY);
364     show_result(fd, 0);
365 kumaneko 1726 if (fd != EOF)
366     close(fd);
367 kumaneko 1066 }
368    
369 kumaneko 1664 policy = "allow_write /dev/null if path1.uid=path1.gid";
370 kumaneko 2194 if (!has_cond)
371     policy = "allow_write /dev/null";
372 kumaneko 1066 if (write_policy()) {
373     int fd = open("/dev/null", O_WRONLY);
374     show_result(fd, 1);
375 kumaneko 1726 if (fd != EOF)
376     close(fd);
377 kumaneko 1066 delete_policy();
378     fd = open("/dev/null", O_WRONLY);
379     show_result(fd, 0);
380 kumaneko 1726 if (fd != EOF)
381     close(fd);
382 kumaneko 1066 }
383    
384 kumaneko 1664 policy = "allow_read/write /dev/null if task.uid=path1.parent.uid";
385 kumaneko 2194 if (!has_cond)
386 kumaneko 2199 policy = "allow_read/write /dev/null";
387 kumaneko 1066 if (write_policy()) {
388     int fd = open("/dev/null", O_RDWR);
389     show_result(fd, 1);
390 kumaneko 1726 if (fd != EOF)
391     close(fd);
392 kumaneko 1066 delete_policy();
393     fd = open("/dev/null", O_RDWR);
394     show_result(fd, 0);
395 kumaneko 1726 if (fd != EOF)
396     close(fd);
397 kumaneko 1066 }
398    
399 kumaneko 1664 policy = "allow_create /tmp/open_test if path1.parent.uid=task.uid";
400 kumaneko 2194 if (!has_cond)
401     policy = "allow_create /tmp/open_test";
402 kumaneko 1066 if (write_policy()) {
403 kumaneko 1664 policy = "allow_write /tmp/open_test if path1.parent.uid=0";
404 kumaneko 2194 if (!has_cond)
405     policy = "allow_write /tmp/open_test";
406 kumaneko 1066 if (write_policy()) {
407 kumaneko 1726 int fd = open("/tmp/open_test",
408     O_WRONLY | O_CREAT | O_EXCL, 0666);
409 kumaneko 1066 show_result(fd, 1);
410 kumaneko 1726 if (fd != EOF)
411     close(fd);
412 kumaneko 1066 unlink2("/tmp/open_test");
413     delete_policy();
414 kumaneko 1726 fd = open("/tmp/open_test",
415     O_WRONLY | O_CREAT | O_EXCL, 0666);
416 kumaneko 1066 show_result(fd, 0);
417 kumaneko 1726 if (fd != EOF)
418     close(fd);
419 kumaneko 1066 unlink2("/tmp/open_test");
420     }
421 kumaneko 1726 policy = "allow_create /tmp/open_test "
422 kumaneko 2194 "if path1.parent.uid=task.uid";
423     if (!has_cond)
424     policy = "allow_create /tmp/open_test";
425 kumaneko 1066 delete_policy();
426     }
427    
428 kumaneko 1664 policy = "allow_write /tmp/open_test if task.uid=0 path1.ino!=0";
429 kumaneko 2199 if (!has_cond)
430     policy = "allow_write /tmp/open_test";
431 kumaneko 1066 if (write_policy()) {
432 kumaneko 1664 policy = "allow_create /tmp/open_test if 0=0";
433 kumaneko 2199 if (!has_cond)
434     policy = "allow_create /tmp/open_test";
435 kumaneko 1066 if (write_policy()) {
436 kumaneko 1726 int fd = open("/tmp/open_test",
437     O_WRONLY | O_CREAT | O_EXCL, 0666);
438 kumaneko 1066 show_result(fd, 1);
439 kumaneko 1726 if (fd != EOF)
440     close(fd);
441 kumaneko 1066 unlink2("/tmp/open_test");
442     delete_policy();
443 kumaneko 1726 fd = open("/tmp/open_test",
444     O_WRONLY | O_CREAT | O_EXCL, 0666);
445 kumaneko 1066 show_result(fd, 0);
446 kumaneko 1726 if (fd != EOF)
447     close(fd);
448 kumaneko 1066 unlink2("/tmp/open_test");
449     }
450 kumaneko 1726 policy = "allow_write /tmp/open_test "
451 kumaneko 2194 "if task.uid=0 path1.ino!=0";
452     if (!has_cond)
453     policy = "allow_write /tmp/open_test";
454 kumaneko 1066 delete_policy();
455     }
456    
457     filename = "/tmp/truncate_test";
458     create2(filename);
459    
460 kumaneko 1664 policy = "allow_truncate /tmp/truncate_test if task.uid=path1.uid";
461 kumaneko 2199 if (!has_cond)
462     policy = "allow_truncate /tmp/truncate_test";
463 kumaneko 1066 if (write_policy()) {
464 kumaneko 1664 policy = "allow_write /tmp/truncate_test if 1!=100-1000000";
465 kumaneko 2199 if (!has_cond)
466     policy = "allow_write /tmp/truncate_test";
467 kumaneko 1066 if (write_policy()) {
468     int fd = open(filename, O_WRONLY | O_TRUNC);
469     show_result(fd, 1);
470 kumaneko 1726 if (fd != EOF)
471     close(fd);
472 kumaneko 1066 delete_policy();
473     fd = open(filename, O_WRONLY | O_TRUNC);
474     show_result(fd, 0);
475 kumaneko 1726 if (fd != EOF)
476     close(fd);
477 kumaneko 1066 }
478 kumaneko 1726 policy = "allow_truncate /tmp/truncate_test "
479     "if task.uid=path1.uid";
480 kumaneko 2199 if (!has_cond)
481     policy = "allow_truncate /tmp/truncate_test";
482 kumaneko 1066 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 kumaneko 1726 if (fd != EOF)
492     close(fd);
493 kumaneko 1066 delete_policy();
494     fd = open(filename, O_WRONLY | O_TRUNC);
495     show_result(fd, 0);
496 kumaneko 1726 if (fd != EOF)
497     close(fd);
498 kumaneko 1066 }
499 kumaneko 2194 policy = "allow_write /tmp/truncate_test";
500 kumaneko 1066 delete_policy();
501     }
502 kumaneko 1726
503 kumaneko 1066 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 kumaneko 1726 if (fd != EOF)
522     close(fd);
523 kumaneko 1066 }
524 kumaneko 1726
525 kumaneko 1066 unlink2(filename);
526 kumaneko 1726
527 kumaneko 1066 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 kumaneko 1726
572 kumaneko 1066 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 kumaneko 1726
581 kumaneko 1066 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 kumaneko 1726
592 kumaneko 1066 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 kumaneko 1726
603 kumaneko 1066 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 kumaneko 1726
612 kumaneko 2563 policy = "allow_symlink /tmp/symlink_source_test if symlink.target=\"/tmp/symlink_\\*_test\"";
613     if (write_policy()) {
614     filename = "/tmp/symlink_source_test";
615     show_result(symlink("/tmp/symlink_dest_test", filename), 1);
616     delete_policy();
617     unlink2(filename);
618     show_result(symlink("/tmp/symlink_dest_test", filename), 0);
619     }
620    
621     policy = "allow_symlink /tmp/symlink_source_test if task.uid=0 symlink.target=\"/tmp/symlink_\\*_test\"";
622     if (write_policy()) {
623     filename = "/tmp/symlink_source_test";
624     show_result(symlink("/tmp/symlink_dest_test", filename), 1);
625     delete_policy();
626     unlink2(filename);
627     show_result(symlink("/tmp/symlink_dest_test", filename), 0);
628     }
629    
630     policy = "allow_symlink /tmp/symlink_source_test if symlink.target!=\"\\*\"";
631     if (write_policy()) {
632     filename = "/tmp/symlink_source_test";
633     show_result(symlink("/tmp/symlink_dest_test", filename), 1);
634     delete_policy();
635     unlink2(filename);
636     show_result(symlink("/tmp/symlink_dest_test", filename), 0);
637     }
638    
639     policy = "allow_symlink /tmp/symlink_source_test if symlink.target!=\"/tmp/symlink_\\*_test\"";
640     if (write_policy()) {
641     filename = "/tmp/symlink_source_test";
642     show_result(symlink("/tmp/symlink_dest_test", filename), 0);
643     delete_policy();
644     unlink2(filename);
645     show_result(symlink("/tmp/symlink_dest_test", filename), 0);
646     }
647    
648 kumaneko 1066 policy = "allow_link /tmp/link_source_test /tmp/link_dest_test";
649     if (write_policy()) {
650     filename = "/tmp/link_source_test";
651     create2(filename);
652     show_result(link(filename, "/tmp/link_dest_test"), 1);
653     delete_policy();
654     unlink2("/tmp/link_dest_test");
655     show_result(link(filename, "/tmp/link_dest_test"), 0);
656     unlink2(filename);
657     }
658    
659     policy = "allow_rename /tmp/rename_source_test /tmp/rename_dest_test";
660     if (write_policy()) {
661     filename = "/tmp/rename_source_test";
662     create2(filename);
663     show_result(rename(filename, "/tmp/rename_dest_test"), 1);
664     delete_policy();
665     unlink2("/tmp/rename_dest_test");
666     create2(filename);
667     show_result(rename(filename, "/tmp/rename_dest_test"), 0);
668     unlink2(filename);
669     }
670    
671     policy = "allow_mksock /tmp/socket_test";
672     if (write_policy()) {
673     struct sockaddr_un addr;
674     int fd;
675     filename = "/tmp/socket_test";
676     memset(&addr, 0, sizeof(addr));
677     addr.sun_family = AF_UNIX;
678     strncpy(addr.sun_path, filename, sizeof(addr.sun_path) - 1);
679     fd = socket(AF_UNIX, SOCK_STREAM, 0);
680 kumaneko 1726 show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)),
681     1);
682     if (fd != EOF)
683     close(fd);
684 kumaneko 1066 delete_policy();
685     unlink2(filename);
686     fd = socket(AF_UNIX, SOCK_STREAM, 0);
687 kumaneko 1726 show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)),
688     0);
689     if (fd != EOF)
690     close(fd);
691 kumaneko 1066 }
692 kumaneko 1067
693     filename = "/tmp/rewrite_test";
694     create2(filename);
695     policy = "allow_read/write /tmp/rewrite_test";
696     if (write_policy()) {
697     char *cp = "deny_rewrite /tmp/rewrite_test\n";
698     write(exception_fd, cp, strlen(cp));
699     policy = "allow_truncate /tmp/rewrite_test";
700     if (write_policy()) {
701     int fd;
702    
703     fd = open(filename, O_RDONLY);
704     show_result(fd, 1);
705 kumaneko 1726 if (fd != EOF)
706     close(fd);
707    
708 kumaneko 1067 fd = open(filename, O_WRONLY | O_APPEND);
709     show_result(fd, 1);
710 kumaneko 1726 if (fd != EOF)
711     close(fd);
712    
713 kumaneko 1067 fd = open(filename, O_WRONLY);
714     show_result(fd, 0);
715 kumaneko 1726 if (fd != EOF)
716     close(fd);
717    
718 kumaneko 1067 fd = open(filename, O_WRONLY | O_TRUNC);
719     show_result(fd, 0);
720 kumaneko 1726 if (fd != EOF)
721     close(fd);
722    
723 kumaneko 1067 fd = open(filename, O_WRONLY | O_TRUNC | O_APPEND);
724     show_result(fd, 0);
725 kumaneko 1726 if (fd != EOF)
726     close(fd);
727    
728 kumaneko 1067 show_result(truncate(filename, 0), 0);
729 kumaneko 1726
730 kumaneko 1067 cp = "255-MAC_FOR_FILE=disabled\n";
731     write(profile_fd, cp, strlen(cp));
732     fd = open(filename, O_WRONLY | O_APPEND);
733     cp = "255-MAC_FOR_FILE=enforcing\n";
734     write(profile_fd, cp, strlen(cp));
735     show_result(ftruncate(fd, 0), 0);
736    
737 kumaneko 1726 show_result(fcntl(fd, F_SETFL,
738     fcntl(fd, F_GETFL) & ~O_APPEND), 0);
739     if (fd != EOF)
740     close(fd);
741    
742 kumaneko 1067 delete_policy();
743     }
744     policy = "allow_read/write /tmp/rewrite_test";
745     delete_policy();
746     cp = "delete deny_rewrite /tmp/rewrite_test\n";
747     write(exception_fd, cp, strlen(cp));
748     }
749     unlink2(filename);
750 kumaneko 2301
751     if (has_cond) {
752     const char *cp = "255-MAC_FOR_IOCTL=enforcing\n";
753     write(profile_fd, cp, strlen(cp));
754 kumaneko 2302 policy = "allow_ioctl socket:[family=2:type=2:protocol=17] "
755     "35122-35124 if task.uid=0";
756 kumaneko 2301 if (write_policy()) {
757 kumaneko 2302 struct ifreq ifreq;
758 kumaneko 2301 int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
759 kumaneko 2302 memset(&ifreq, 0, sizeof(ifreq));
760     snprintf(ifreq.ifr_name, sizeof(ifreq.ifr_name) - 1, "lo");
761     show_result(ioctl(fd, 35123, &ifreq), 1);
762 kumaneko 2301 delete_policy();
763 kumaneko 2302 policy = "allow_ioctl socket:[family=2:type=2:protocol=17] "
764     "0-35122";
765     if (write_policy()) {
766     show_result(ioctl(fd, 35123, &ifreq), 0);
767     delete_policy();
768     }
769 kumaneko 2301 if (fd != EOF)
770     close(fd);
771     }
772     cp = "255-MAC_FOR_IOCTL=disabled\n";
773     write(profile_fd, cp, strlen(cp));
774     }
775 kumaneko 1066 }
776    
777 kumaneko 1726 int main(int argc, char *argv[])
778     {
779 kumaneko 1066 char *cp;
780 kumaneko 1744 ccs_test_init();
781 kumaneko 1066 domain_fd = open(proc_policy_domain_policy, O_WRONLY);
782 kumaneko 2314 if (domain_fd == EOF && errno == ENOENT) {
783     fprintf(stderr, "You can't use this program for this kernel."
784     "\n");
785 kumaneko 2313 return 1;
786 kumaneko 2314 }
787 kumaneko 1067 exception_fd = open(proc_policy_exception_policy, O_WRONLY);
788 kumaneko 1066 {
789     int self_fd = open(proc_policy_self_domain, O_RDONLY);
790     memset(self_domain, 0, sizeof(self_domain));
791     read(self_fd, self_domain, sizeof(self_domain) - 1);
792     close(self_fd);
793     write(domain_fd, self_domain, strlen(self_domain));
794     cp = " /bin/true\n";
795     write(domain_fd, cp, strlen(cp));
796     write(domain_fd, self_domain, strlen(self_domain));
797     write(domain_fd, "\n", 1);
798     cp = "use_profile 255\n";
799     write(domain_fd, cp, strlen(cp));
800     }
801 kumaneko 2194 has_cond = !access("/proc/ccs/version", F_OK);
802 kumaneko 1066 cp = "255-MAX_REJECT_LOG=1024\n";
803     write(profile_fd, cp, strlen(cp));
804 kumaneko 1744 stage_file_test();
805 kumaneko 1066 cp = "use_profile 0\n";
806     write(domain_fd, cp, strlen(cp));
807 kumaneko 1744 clear_status();
808 kumaneko 1066 return 0;
809     }

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