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

Subversion リポジトリの参照

Annotation of /trunk/2.4.x/tomoyo-tools/kernel_test/tomoyo_new_file_test.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1721 - (hide annotations) (download) (as text)
Mon Oct 20 05:56:25 2008 UTC (15 years, 6 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: 14399 byte(s)


1 kumaneko 1066 /*
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 kumaneko 1664 * Version: 1.6.5-pre 2008/10/07
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    
18     static int write_policy(void) {
19     FILE *fp;
20     char buffer[8192];
21     char *cp;
22     int domain_found = 0;
23     int policy_found = 0;
24     memset(buffer, 0, sizeof(buffer));
25     cp = "255-MAC_FOR_FILE=disabled\n";
26     write(profile_fd, cp, strlen(cp));
27     fp = fopen(proc_policy_domain_policy, "r");
28     cp = "255-MAC_FOR_FILE=enforcing\n";
29     write(profile_fd, cp, strlen(cp));
30     write(domain_fd, policy, strlen(policy));
31     write(domain_fd, "\n", 1);
32     if (!fp) {
33     printf("%s : BUG: policy read failed\n", policy);
34     return 0;
35     }
36     while (fgets(buffer, sizeof(buffer) - 1, fp)) {
37     cp = strchr(buffer, '\n');
38     if (cp) *cp = '\0';
39     if (!strncmp(buffer, "<kernel>", 8)) domain_found = !strcmp(self_domain, buffer);
40     if (domain_found) {
41     //printf("<%s>\n", buffer);
42     if (!strcmp(buffer, policy)) {
43     policy_found = 1;
44     break;
45     }
46     }
47     }
48     fclose(fp);
49     if (!policy_found) {
50     printf("%s : BUG: policy write failed\n", policy);
51     return 0;
52     }
53     errno = 0;
54     return 1;
55     }
56    
57     static void delete_policy(void) {
58     write(domain_fd, "delete ", 7);
59     write(domain_fd, policy, strlen(policy));
60     write(domain_fd, "\n", 1);
61     }
62    
63     static void show_result(int result, char should_success) {
64     int err = errno;
65     printf("%s : ", policy);
66     if (should_success) {
67     if (result != EOF) printf("OK\n");
68     else printf("FAILED: %s\n", strerror(err));
69     } else {
70     if (result == EOF) {
71     if (err == EPERM) printf("OK: Permission denied.\n");
72     else printf("FAILED: %s\n", strerror(err));
73     } else {
74     printf("BUG: didn't fail.\n");
75     }
76     }
77     }
78    
79     static void create2(const char *pathname) {
80     const char *cp = "255-MAC_FOR_FILE=disabled\n";
81     write(profile_fd, cp, strlen(cp));
82     close(creat(pathname, 0600));
83     cp = "255-MAC_FOR_FILE=enforcing\n";
84     write(profile_fd, cp, strlen(cp));
85 kumaneko 1148 errno = 0;
86 kumaneko 1066 }
87    
88     static void mkdir2(const char *pathname) {
89     const char *cp = "255-MAC_FOR_FILE=disabled\n";
90     write(profile_fd, cp, strlen(cp));
91     mkdir(pathname, 0600);
92     cp = "255-MAC_FOR_FILE=enforcing\n";
93     write(profile_fd, cp, strlen(cp));
94 kumaneko 1148 errno = 0;
95 kumaneko 1066 }
96    
97     static void unlink2(const char *pathname) {
98     const char *cp = "255-MAC_FOR_FILE=disabled\n";
99     write(profile_fd, cp, strlen(cp));
100     unlink(pathname);
101     cp = "255-MAC_FOR_FILE=enforcing\n";
102     write(profile_fd, cp, strlen(cp));
103 kumaneko 1148 errno = 0;
104 kumaneko 1066 }
105    
106     static void rmdir2(const char *pathname) {
107     const char *cp = "255-MAC_FOR_FILE=disabled\n";
108     write(profile_fd, cp, strlen(cp));
109     rmdir(pathname);
110     cp = "255-MAC_FOR_FILE=enforcing\n";
111     write(profile_fd, cp, strlen(cp));
112 kumaneko 1148 errno = 0;
113 kumaneko 1066 }
114    
115     static void StageFileTest(void) {
116     char *filename = "";
117 kumaneko 1664 policy = "allow_read /proc/sys/net/ipv4/ip_local_port_range if task.uid=0 task.gid=0";
118 kumaneko 1066 if (write_policy()) {
119     static int name[] = { CTL_NET, NET_IPV4, NET_IPV4_LOCAL_PORT_RANGE };
120     int buffer[2] = { 32768, 61000 };
121     size_t size = sizeof(buffer);
122     show_result(sysctl(name, 3, buffer, &size, 0, 0), 1);
123     delete_policy();
124     show_result(sysctl(name, 3, buffer, &size, 0, 0), 0);
125     }
126 kumaneko 1664 policy = "allow_write /proc/sys/net/ipv4/ip_local_port_range if task.euid=0 0=0 1-100=10-1000";
127 kumaneko 1066 if (write_policy()) {
128     static int name[] = { CTL_NET, NET_IPV4, NET_IPV4_LOCAL_PORT_RANGE };
129     int buffer[2] = { 32768, 61000 };
130     size_t size = sizeof(buffer);
131     show_result(sysctl(name, 3, 0, 0, buffer, size), 1);
132     delete_policy();
133     show_result(sysctl(name, 3, 0, 0, buffer, size), 0);
134     }
135 kumaneko 1664 policy = "allow_read/write /proc/sys/net/ipv4/ip_local_port_range if 1!=10-100";
136 kumaneko 1066 if (write_policy()) {
137     static int name[] = { CTL_NET, NET_IPV4, NET_IPV4_LOCAL_PORT_RANGE };
138     int buffer[2] = { 32768, 61000 };
139     size_t size = sizeof(buffer);
140     show_result(sysctl(name, 3, buffer, &size, buffer, size), 1);
141     delete_policy();
142     show_result(sysctl(name, 3, buffer, &size, buffer, size), 0);
143     }
144    
145 kumaneko 1664 policy = "allow_read /bin/true if path1.uid=0 path1.parent.uid=0 10=10-100";
146 kumaneko 1066 if (write_policy()) {
147     show_result(uselib("/bin/true"), 1);
148     delete_policy();
149     show_result(uselib("/bin/true"), 0);
150     }
151    
152 kumaneko 1664 policy = "allow_execute /bin/true if task.uid!=10 path1.parent.uid=0";
153 kumaneko 1066 if (write_policy()) {
154     int pipe_fd[2] = { EOF, EOF };
155     int err = 0;
156     fflush(stdout); fflush(stderr);
157     pipe(pipe_fd);
158     if (fork() == 0) {
159     execl("/bin/true", "/bin/true", NULL);
160     err = errno;
161     write(pipe_fd[1], &err, sizeof(err));
162     _exit(0);
163     }
164     close(pipe_fd[1]);
165     read(pipe_fd[0], &err, sizeof(err));
166     close(pipe_fd[0]);
167     wait(NULL);
168     errno = err;
169     show_result(err ? EOF : 0, 1);
170     delete_policy();
171     fflush(stdout); fflush(stderr);
172     pipe(pipe_fd);
173     if (fork() == 0) {
174     execl("/bin/true", "/bin/true", NULL);
175     err = errno;
176     write(pipe_fd[1], &err, sizeof(err));
177     _exit(0);
178     }
179     close(pipe_fd[1]);
180     read(pipe_fd[0], &err, sizeof(err));
181     close(pipe_fd[0]);
182     wait(NULL);
183     errno = err;
184     show_result(err ? EOF : 0, 0);
185     }
186    
187 kumaneko 1664 policy = "allow_read /dev/null if path1.parent.ino=path1.parent.ino";
188 kumaneko 1066 if (write_policy()) {
189     int fd = open("/dev/null", O_RDONLY);
190     show_result(fd, 1);
191     if (fd != EOF) close(fd);
192     delete_policy();
193     fd = open("/dev/null", O_RDONLY);
194     show_result(fd, 0);
195     if (fd != EOF) close(fd);
196     }
197    
198 kumaneko 1664 policy = "allow_write /dev/null if path1.uid=path1.gid";
199 kumaneko 1066 if (write_policy()) {
200     int fd = open("/dev/null", O_WRONLY);
201     show_result(fd, 1);
202     if (fd != EOF) close(fd);
203     delete_policy();
204     fd = open("/dev/null", O_WRONLY);
205     show_result(fd, 0);
206     if (fd != EOF) close(fd);
207     }
208    
209 kumaneko 1664 policy = "allow_read/write /dev/null if task.uid=path1.parent.uid";
210 kumaneko 1066 if (write_policy()) {
211     int fd = open("/dev/null", O_RDWR);
212     show_result(fd, 1);
213     if (fd != EOF) close(fd);
214     delete_policy();
215     fd = open("/dev/null", O_RDWR);
216     show_result(fd, 0);
217     if (fd != EOF) close(fd);
218     }
219    
220 kumaneko 1664 policy = "allow_create /tmp/open_test if path1.parent.uid=task.uid";
221 kumaneko 1066 if (write_policy()) {
222 kumaneko 1664 policy = "allow_write /tmp/open_test if path1.parent.uid=0";
223 kumaneko 1066 if (write_policy()) {
224     int fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0666);
225     show_result(fd, 1);
226     if (fd != EOF) close(fd);
227     unlink2("/tmp/open_test");
228     delete_policy();
229     fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0666);
230     show_result(fd, 0);
231     if (fd != EOF) close(fd);
232     unlink2("/tmp/open_test");
233     }
234 kumaneko 1664 policy = "allow_create /tmp/open_test if path1.parent.uid=task.uid\n";
235 kumaneko 1066 delete_policy();
236     }
237    
238 kumaneko 1664 policy = "allow_write /tmp/open_test if task.uid=0 path1.ino!=0";
239 kumaneko 1066 if (write_policy()) {
240 kumaneko 1664 policy = "allow_create /tmp/open_test if 0=0";
241 kumaneko 1066 if (write_policy()) {
242     int fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0666);
243     show_result(fd, 1);
244     if (fd != EOF) close(fd);
245     unlink2("/tmp/open_test");
246     delete_policy();
247     fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0666);
248     show_result(fd, 0);
249     if (fd != EOF) close(fd);
250     unlink2("/tmp/open_test");
251     }
252 kumaneko 1664 policy = "allow_write /tmp/open_test if task.uid=0 path1.ino!=0\n";
253 kumaneko 1066 delete_policy();
254     }
255    
256     filename = "/tmp/truncate_test";
257     create2(filename);
258    
259 kumaneko 1664 policy = "allow_truncate /tmp/truncate_test if task.uid=path1.uid";
260 kumaneko 1066 if (write_policy()) {
261 kumaneko 1664 policy = "allow_write /tmp/truncate_test if 1!=100-1000000";
262 kumaneko 1066 if (write_policy()) {
263     int fd = open(filename, O_WRONLY | O_TRUNC);
264     show_result(fd, 1);
265     if (fd != EOF) close(fd);
266     delete_policy();
267     fd = open(filename, O_WRONLY | O_TRUNC);
268     show_result(fd, 0);
269     if (fd != EOF) close(fd);
270     }
271 kumaneko 1664 policy = "allow_truncate /tmp/truncate_test if task.uid=path1.uid";
272 kumaneko 1066 delete_policy();
273     }
274    
275     policy = "allow_write /tmp/truncate_test";
276     if (write_policy()) {
277     policy = "allow_truncate /tmp/truncate_test";
278     if (write_policy()) {
279     int fd = open(filename, O_WRONLY | O_TRUNC);
280     show_result(fd, 1);
281     if (fd != EOF) close(fd);
282     delete_policy();
283     fd = open(filename, O_WRONLY | O_TRUNC);
284     show_result(fd, 0);
285     if (fd != EOF) close(fd);
286     }
287     policy = "allow_write /tmp/truncate_test\n";
288     delete_policy();
289     }
290    
291     policy = "allow_truncate /tmp/truncate_test";
292     if (write_policy()) {
293     show_result(truncate(filename, 0), 1);
294     delete_policy();
295     show_result(truncate(filename, 0), 0);
296     }
297    
298     policy = "allow_truncate /tmp/truncate_test";
299     if (write_policy()) {
300     int fd;
301     const char *cp = "255-MAC_FOR_FILE=disabled\n";
302     write(profile_fd, cp, strlen(cp));
303     fd = open(filename, O_WRONLY);
304     cp = "255-MAC_FOR_FILE=enforcing\n";
305     write(profile_fd, cp, strlen(cp));
306     show_result(ftruncate(fd, 0), 1);
307     delete_policy();
308     show_result(ftruncate(fd, 0), 0);
309     if (fd != EOF) close(fd);
310     }
311    
312     unlink2(filename);
313    
314     policy = "allow_create /tmp/mknod_reg_test";
315     if (write_policy()) {
316     filename = "/tmp/mknod_reg_test";
317     show_result(mknod(filename, S_IFREG, 0), 1);
318     delete_policy();
319     unlink2(filename);
320     show_result(mknod(filename, S_IFREG, 0), 0);
321     }
322    
323     policy = "allow_mkchar /tmp/mknod_chr_test";
324     if (write_policy()) {
325     filename = "/tmp/mknod_chr_test";
326     show_result(mknod(filename, S_IFCHR, MKDEV(1, 3)), 1);
327     delete_policy();
328     unlink2(filename);
329     show_result(mknod(filename, S_IFCHR, MKDEV(1, 3)), 0);
330     }
331    
332     policy = "allow_mkblock /tmp/mknod_blk_test";
333     if (write_policy()) {
334     filename = "/tmp/mknod_blk_test";
335     show_result(mknod(filename, S_IFBLK, MKDEV(1, 0)), 1);
336     delete_policy();
337     unlink2(filename);
338     show_result(mknod(filename, S_IFBLK, MKDEV(1, 0)), 0);
339     }
340    
341     policy = "allow_mkfifo /tmp/mknod_fifo_test";
342     if (write_policy()) {
343     filename = "/tmp/mknod_fifo_test";
344     show_result(mknod(filename, S_IFIFO, 0), 1);
345     delete_policy();
346     unlink2(filename);
347     show_result(mknod(filename, S_IFIFO, 0), 0);
348     }
349    
350     policy = "allow_mksock /tmp/mknod_sock_test";
351     if (write_policy()) {
352     filename = "/tmp/mknod_sock_test";
353     show_result(mknod(filename, S_IFSOCK, 0), 1);
354     delete_policy();
355     unlink2(filename);
356     show_result(mknod(filename, S_IFSOCK, 0), 0);
357     }
358    
359     policy = "allow_mkdir /tmp/mkdir_test/";
360     if (write_policy()) {
361     filename = "/tmp/mkdir_test";
362     show_result(mkdir(filename, 0600), 1);
363     delete_policy();
364     rmdir2(filename);
365     show_result(mkdir(filename, 0600), 0);
366     }
367    
368     policy = "allow_rmdir /tmp/rmdir_test/";
369     if (write_policy()) {
370     filename = "/tmp/rmdir_test";
371     mkdir2(filename);
372     show_result(rmdir(filename), 1);
373     delete_policy();
374     mkdir2(filename);
375     show_result(rmdir(filename), 0);
376     rmdir2(filename);
377     }
378    
379     policy = "allow_unlink /tmp/unlink_test";
380     if (write_policy()) {
381     filename = "/tmp/unlink_test";
382     create2(filename);
383     show_result(unlink(filename), 1);
384     delete_policy();
385     create2(filename);
386     show_result(unlink(filename), 0);
387     unlink2(filename);
388     }
389    
390     policy = "allow_symlink /tmp/symlink_source_test";
391     if (write_policy()) {
392     filename = "/tmp/symlink_source_test";
393     show_result(symlink("/tmp/symlink_dest_test", filename), 1);
394     delete_policy();
395     unlink2(filename);
396     show_result(symlink("/tmp/symlink_dest_test", filename), 0);
397     }
398    
399     policy = "allow_link /tmp/link_source_test /tmp/link_dest_test";
400     if (write_policy()) {
401     filename = "/tmp/link_source_test";
402     create2(filename);
403     show_result(link(filename, "/tmp/link_dest_test"), 1);
404     delete_policy();
405     unlink2("/tmp/link_dest_test");
406     show_result(link(filename, "/tmp/link_dest_test"), 0);
407     unlink2(filename);
408     }
409    
410     policy = "allow_rename /tmp/rename_source_test /tmp/rename_dest_test";
411     if (write_policy()) {
412     filename = "/tmp/rename_source_test";
413     create2(filename);
414     show_result(rename(filename, "/tmp/rename_dest_test"), 1);
415     delete_policy();
416     unlink2("/tmp/rename_dest_test");
417     create2(filename);
418     show_result(rename(filename, "/tmp/rename_dest_test"), 0);
419     unlink2(filename);
420     }
421    
422     policy = "allow_mksock /tmp/socket_test";
423     if (write_policy()) {
424     struct sockaddr_un addr;
425     int fd;
426     filename = "/tmp/socket_test";
427     memset(&addr, 0, sizeof(addr));
428     addr.sun_family = AF_UNIX;
429     strncpy(addr.sun_path, filename, sizeof(addr.sun_path) - 1);
430     fd = socket(AF_UNIX, SOCK_STREAM, 0);
431     show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)), 1);
432     if (fd != EOF) close(fd);
433     delete_policy();
434     unlink2(filename);
435     fd = socket(AF_UNIX, SOCK_STREAM, 0);
436     show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)), 0);
437     if (fd != EOF) close(fd);
438     }
439 kumaneko 1067
440     filename = "/tmp/rewrite_test";
441     create2(filename);
442     policy = "allow_read/write /tmp/rewrite_test";
443     if (write_policy()) {
444     char *cp = "deny_rewrite /tmp/rewrite_test\n";
445     write(exception_fd, cp, strlen(cp));
446     policy = "allow_truncate /tmp/rewrite_test";
447     if (write_policy()) {
448     int fd;
449    
450     fd = open(filename, O_RDONLY);
451     show_result(fd, 1);
452     if (fd != EOF) close(fd);
453    
454     fd = open(filename, O_WRONLY | O_APPEND);
455     show_result(fd, 1);
456     if (fd != EOF) close(fd);
457    
458     fd = open(filename, O_WRONLY);
459     show_result(fd, 0);
460     if (fd != EOF) close(fd);
461    
462     fd = open(filename, O_WRONLY | O_TRUNC);
463     show_result(fd, 0);
464     if (fd != EOF) close(fd);
465    
466     fd = open(filename, O_WRONLY | O_TRUNC | O_APPEND);
467     show_result(fd, 0);
468     if (fd != EOF) close(fd);
469    
470     show_result(truncate(filename, 0), 0);
471    
472     cp = "255-MAC_FOR_FILE=disabled\n";
473     write(profile_fd, cp, strlen(cp));
474     fd = open(filename, O_WRONLY | O_APPEND);
475     cp = "255-MAC_FOR_FILE=enforcing\n";
476     write(profile_fd, cp, strlen(cp));
477     show_result(ftruncate(fd, 0), 0);
478    
479     show_result(fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_APPEND), 0);
480     if (fd != EOF) close(fd);
481    
482     delete_policy();
483     }
484     policy = "allow_read/write /tmp/rewrite_test";
485     delete_policy();
486     cp = "delete deny_rewrite /tmp/rewrite_test\n";
487     write(exception_fd, cp, strlen(cp));
488     }
489     unlink2(filename);
490 kumaneko 1066 }
491    
492     int main(int argc, char *argv[]) {
493     char *cp;
494     Init();
495     domain_fd = open(proc_policy_domain_policy, O_WRONLY);
496 kumaneko 1067 exception_fd = open(proc_policy_exception_policy, O_WRONLY);
497 kumaneko 1066 {
498     int self_fd = open(proc_policy_self_domain, O_RDONLY);
499     memset(self_domain, 0, sizeof(self_domain));
500     read(self_fd, self_domain, sizeof(self_domain) - 1);
501     close(self_fd);
502     write(domain_fd, self_domain, strlen(self_domain));
503     cp = " /bin/true\n";
504     write(domain_fd, cp, strlen(cp));
505     write(domain_fd, self_domain, strlen(self_domain));
506     write(domain_fd, "\n", 1);
507     cp = "use_profile 255\n";
508     write(domain_fd, cp, strlen(cp));
509     }
510     cp = "255-MAX_REJECT_LOG=1024\n";
511     write(profile_fd, cp, strlen(cp));
512     StageFileTest();
513     cp = "use_profile 0\n";
514     write(domain_fd, cp, strlen(cp));
515     ClearStatus();
516     return 0;
517     }

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