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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/1.6.x/ccs-tools/ccstools/kernel_test/tomoyo_new_file_test.c revision 1996 by kumaneko, Mon Dec 22 06:08:22 2008 UTC branches/ccs-tools/ccstools/kernel_test/ccs_new_file_test.c revision 3871 by kumaneko, Sun Aug 1 01:42:05 2010 UTC
# Line 1  Line 1 
1  /*  /*
2   * tomoyo_file_test.c   * ccs_new_file_test.c
3   *   *
4   * Testing program for fs/tomoyo_file.c   * Copyright (C) 2005-2010  NTT DATA CORPORATION
5   *   *
6   * Copyright (C) 2005-2008  NTT DATA CORPORATION   * Version: 1.8.0-pre   2010/08/01
  *  
  * Version: 1.6.6-pre   2008/12/22  
7   *   *
8   */   */
9  #include "include.h"  #include "include.h"
10    
 static int domain_fd = EOF;  
 static int exception_fd = EOF;  
11  static const char *policy = "";  static const char *policy = "";
 static char self_domain[4096] = "";  
12    
13    #if 0
14  static int write_policy(void)  static int write_policy(void)
15  {  {
16          FILE *fp;          FILE *fp;
17          char buffer[8192];          char buffer[8192];
         char *cp;  
18          int domain_found = 0;          int domain_found = 0;
19          int policy_found = 0;          int policy_found = 0;
20          memset(buffer, 0, sizeof(buffer));          memset(buffer, 0, sizeof(buffer));
21          cp = "255-MAC_FOR_FILE=disabled\n";          set_profile(0, "file::open");
         write(profile_fd, cp, strlen(cp));  
22          fp = fopen(proc_policy_domain_policy, "r");          fp = fopen(proc_policy_domain_policy, "r");
23          cp = "255-MAC_FOR_FILE=enforcing\n";          set_profile(3, "file::open");
24          write(profile_fd, cp, strlen(cp));          fprintf(domain_fp, "%s\n", policy);
         write(domain_fd, policy, strlen(policy));  
         write(domain_fd, "\n", 1);  
25          if (!fp) {          if (!fp) {
26                  printf("%s : BUG: policy read failed\n", policy);                  printf("%s : BUG: policy read failed\n", policy);
27                  return 0;                  return 0;
28          }          }
29          while (fgets(buffer, sizeof(buffer) - 1, fp)) {          while (fgets(buffer, sizeof(buffer) - 1, fp)) {
30                  cp = strchr(buffer, '\n');                  char *cp = strchr(buffer, '\n');
31                  if (cp)                  if (cp)
32                          *cp = '\0';                          *cp = '\0';
33                  if (!strncmp(buffer, "<kernel>", 8))                  if (!strncmp(buffer, "<kernel>", 8))
34                          domain_found = !strcmp(self_domain, buffer);                          domain_found = !strcmp(self_domain, buffer);
35                  if (domain_found) {                  if (!domain_found)
36                          /* printf("<%s>\n", buffer); */                          continue;
37                          if (!strcmp(buffer, policy)) {                  /* printf("<%s>\n", buffer); */
38                                  policy_found = 1;                  if (!strcmp(buffer, policy)) {
39                                  break;                          policy_found = 1;
40                          }                          break;
41                  }                  }
42          }          }
43          fclose(fp);          fclose(fp);
# Line 56  static int write_policy(void) Line 48  static int write_policy(void)
48          errno = 0;          errno = 0;
49          return 1;          return 1;
50  }  }
51    #endif
 static void delete_policy(void)  
 {  
         write(domain_fd, "delete ", 7);  
         write(domain_fd, policy, strlen(policy));  
         write(domain_fd, "\n", 1);  
 }  
52    
53  static void show_result(int result, char should_success)  static void show_result(int result, char should_success)
54  {  {
# Line 87  static void show_result(int result, char Line 73  static void show_result(int result, char
73    
74  static void create2(const char *pathname)  static void create2(const char *pathname)
75  {  {
76          const char *cp = "255-MAC_FOR_FILE=disabled\n";          set_profile(0, "file::create");
77          write(profile_fd, cp, strlen(cp));          set_profile(0, "file::open");
78          close(creat(pathname, 0600));          close(creat(pathname, 0600));
79          cp = "255-MAC_FOR_FILE=enforcing\n";          set_profile(3, "file::create");
80          write(profile_fd, cp, strlen(cp));          set_profile(3, "file::open");
81          errno = 0;          errno = 0;
82  }  }
83    
84  static void mkdir2(const char *pathname)  static void mkdir2(const char *pathname)
85  {  {
86          const char *cp = "255-MAC_FOR_FILE=disabled\n";          set_profile(0, "file::mkdir");
         write(profile_fd, cp, strlen(cp));  
87          mkdir(pathname, 0600);          mkdir(pathname, 0600);
88          cp = "255-MAC_FOR_FILE=enforcing\n";          set_profile(3, "file::mkdir");
         write(profile_fd, cp, strlen(cp));  
89          errno = 0;          errno = 0;
90  }  }
91    
92  static void unlink2(const char *pathname)  static void unlink2(const char *pathname)
93  {  {
94          const char *cp = "255-MAC_FOR_FILE=disabled\n";          set_profile(0, "file::unlink");
         write(profile_fd, cp, strlen(cp));  
95          unlink(pathname);          unlink(pathname);
96          cp = "255-MAC_FOR_FILE=enforcing\n";          set_profile(3, "file::unlink");
         write(profile_fd, cp, strlen(cp));  
97          errno = 0;          errno = 0;
98  }  }
99    
100  static void rmdir2(const char *pathname)  static void rmdir2(const char *pathname)
101  {  {
102          const char *cp = "255-MAC_FOR_FILE=disabled\n";          set_profile(0, "file::rmdir");
         write(profile_fd, cp, strlen(cp));  
103          rmdir(pathname);          rmdir(pathname);
104          cp = "255-MAC_FOR_FILE=enforcing\n";          set_profile(3, "file::rmdir");
105          write(profile_fd, cp, strlen(cp));          errno = 0;
106    }
107    
108    static void mkfifo2(const char *pathname)
109    {
110            set_profile(0, "file::mkfifo");
111            mkfifo(pathname, 0600);
112            set_profile(3, "file::mkfifo");
113          errno = 0;          errno = 0;
114  }  }
115    
116  static void stage_file_test(void)  static void stage_file_test(void)
117  {  {
118            static int name[] = { CTL_NET, NET_IPV4, NET_IPV4_LOCAL_PORT_RANGE };
119            int buffer[2] = { 32768, 61000 };
120            size_t size = sizeof(buffer);
121            int pipe_fd[2] = { EOF, EOF };
122            int err = 0;
123            int fd;
124            char pbuffer[1024];
125            struct stat sbuf;
126            struct sockaddr_un addr;
127            struct ifreq ifreq;
128          char *filename = "";          char *filename = "";
129          policy = "allow_read /proc/sys/net/ipv4/ip_local_port_range "          set_profile(3, "file::execute");
130            set_profile(3, "file::open");
131            set_profile(3, "file::create");
132            set_profile(3, "file::unlink");
133            set_profile(3, "file::mkdir");
134            set_profile(3, "file::rmdir");
135            set_profile(3, "file::mkfifo");
136            set_profile(3, "file::mksock");
137            set_profile(3, "file::truncate");
138            set_profile(3, "file::symlink");
139            set_profile(3, "file::mkblock");
140            set_profile(3, "file::mkchar");
141            set_profile(3, "file::link");
142            set_profile(3, "file::rename");
143            set_profile(3, "file::chmod");
144            set_profile(3, "file::chown");
145            set_profile(3, "file::chgrp");
146            set_profile(3, "file::ioctl");
147            set_profile(3, "file::chroot");
148            set_profile(3, "file::mount");
149            set_profile(3, "file::umount");
150            set_profile(3, "file::pivot_root");
151    
152            policy = "file read proc:/sys/net/ipv4/ip_local_port_range "
153                  "if task.uid=0 task.gid=0";                  "if task.uid=0 task.gid=0";
154          if (write_policy()) {          write_domain_policy(policy, 0);
155                  static int name[] = { CTL_NET, NET_IPV4,          show_result(sysctl(name, 3, buffer, &size, 0, 0), 1);
156                                        NET_IPV4_LOCAL_PORT_RANGE };          write_domain_policy(policy, 1);
157                  int buffer[2] = { 32768, 61000 };          show_result(sysctl(name, 3, buffer, &size, 0, 0), 0);
158                  size_t size = sizeof(buffer);  
159                  show_result(sysctl(name, 3, buffer, &size, 0, 0), 1);          policy = "file write proc:/sys/net/ipv4/ip_local_port_range "
                 delete_policy();  
                 show_result(sysctl(name, 3, buffer, &size, 0, 0), 0);  
         }  
         policy = "allow_write /proc/sys/net/ipv4/ip_local_port_range "  
160                  "if task.euid=0 0=0 1-100=10-1000";                  "if task.euid=0 0=0 1-100=10-1000";
161          if (write_policy()) {          write_domain_policy(policy, 0);
162                  static int name[] = { CTL_NET, NET_IPV4,          show_result(sysctl(name, 3, 0, 0, buffer, size), 1);
163                                        NET_IPV4_LOCAL_PORT_RANGE };          write_domain_policy(policy, 1);
164                  int buffer[2] = { 32768, 61000 };          show_result(sysctl(name, 3, 0, 0, buffer, size), 0);
165                  size_t size = sizeof(buffer);  
166                  show_result(sysctl(name, 3, 0, 0, buffer, size), 1);          policy = "file read proc:/sys/net/ipv4/ip_local_port_range "
                 delete_policy();  
                 show_result(sysctl(name, 3, 0, 0, buffer, size), 0);  
         }  
         policy = "allow_read/write /proc/sys/net/ipv4/ip_local_port_range "  
167                  "if 1!=10-100";                  "if 1!=10-100";
168          if (write_policy()) {          write_domain_policy(policy, 0);
169                  static int name[] = { CTL_NET, NET_IPV4,          policy = "file write proc:/sys/net/ipv4/ip_local_port_range "
170                                        NET_IPV4_LOCAL_PORT_RANGE };                  "if 1!=10-100";
171                  int buffer[2] = { 32768, 61000 };          write_domain_policy(policy, 0);
172                  size_t size = sizeof(buffer);          show_result(sysctl(name, 3, buffer, &size, buffer, size), 1);
173                  show_result(sysctl(name, 3, buffer, &size, buffer, size), 1);          policy = "file read proc:/sys/net/ipv4/ip_local_port_range "
174                  delete_policy();                  "if 1!=10-100";
175                  show_result(sysctl(name, 3, buffer, &size, buffer, size), 0);          write_domain_policy(policy, 1);
176          }          policy = "file write proc:/sys/net/ipv4/ip_local_port_range "
177                    "if 1!=10-100";
178            write_domain_policy(policy, 1);
179            show_result(sysctl(name, 3, buffer, &size, buffer, size), 0);
180    
181          policy = "allow_read /bin/true "          policy = "file read /bin/true "
182                  "if path1.uid=0 path1.parent.uid=0 10=10-100";                  "if path1.uid=0 path1.parent.uid=0 10=10-100";
183          if (write_policy()) {          write_domain_policy(policy, 0);
184                  show_result(uselib("/bin/true"), 1);          show_result(uselib("/bin/true"), 1);
185                  delete_policy();          write_domain_policy(policy, 1);
186                  show_result(uselib("/bin/true"), 0);          show_result(uselib("/bin/true"), 0);
187          }  
188            policy = "file execute /bin/true if task.uid!=10 path1.parent.uid=0";
189            write_domain_policy(policy, 0);
190            fflush(stdout);
191            fflush(stderr);
192            pipe(pipe_fd);
193            if (fork() == 0) {
194                    execl("/bin/true", "/bin/true", NULL);
195                    err = errno;
196                    write(pipe_fd[1], &err, sizeof(err));
197                    _exit(0);
198            }
199            close(pipe_fd[1]);
200            read(pipe_fd[0], &err, sizeof(err));
201            close(pipe_fd[0]);
202            wait(NULL);
203            errno = err;
204            show_result(err ? EOF : 0, 1);
205            write_domain_policy(policy, 1);
206            fflush(stdout);
207            fflush(stderr);
208            pipe(pipe_fd);
209            if (fork() == 0) {
210                    execl("/bin/true", "/bin/true", NULL);
211                    err = errno;
212                    write(pipe_fd[1], &err, sizeof(err));
213                    _exit(0);
214            }
215            close(pipe_fd[1]);
216            read(pipe_fd[0], &err, sizeof(err));
217            close(pipe_fd[0]);
218            wait(NULL);
219            errno = err;
220            show_result(err ? EOF : 0, 0);
221    
222          policy = "allow_execute /bin/true if task.uid!=10 path1.parent.uid=0";          policy = "file read /dev/null if path1.type=char path1.dev_major=1 "
         if (write_policy()) {  
                 int pipe_fd[2] = { EOF, EOF };  
                 int err = 0;  
                 fflush(stdout);  
                 fflush(stderr);  
                 pipe(pipe_fd);  
                 if (fork() == 0) {  
                         execl("/bin/true", "/bin/true", NULL);  
                         err = errno;  
                         write(pipe_fd[1], &err, sizeof(err));  
                         _exit(0);  
                 }  
                 close(pipe_fd[1]);  
                 read(pipe_fd[0], &err, sizeof(err));  
                 close(pipe_fd[0]);  
                 wait(NULL);  
                 errno = err;  
                 show_result(err ? EOF : 0, 1);  
                 delete_policy();  
                 fflush(stdout);  
                 fflush(stderr);  
                 pipe(pipe_fd);  
                 if (fork() == 0) {  
                         execl("/bin/true", "/bin/true", NULL);  
                         err = errno;  
                         write(pipe_fd[1], &err, sizeof(err));  
                         _exit(0);  
                 }  
                 close(pipe_fd[1]);  
                 read(pipe_fd[0], &err, sizeof(err));  
                 close(pipe_fd[0]);  
                 wait(NULL);  
                 errno = err;  
                 show_result(err ? EOF : 0, 0);  
         }  
   
         policy = "allow_read /dev/null if path1.type=char path1.dev_major=1 "  
223                  "path1.dev_minor=3";                  "path1.dev_minor=3";
224          if (write_policy()) {          write_domain_policy(policy, 0);
225                  int fd = open("/dev/null", O_RDONLY);          fd = open("/dev/null", O_RDONLY);
226                  show_result(fd, 1);          show_result(fd, 1);
227                  if (fd != EOF)          if (fd != EOF)
228                          close(fd);                  close(fd);
229                  delete_policy();          write_domain_policy(policy, 1);
230                  fd = open("/dev/null", O_RDONLY);          fd = open("/dev/null", O_RDONLY);
231                  show_result(fd, 0);          show_result(fd, 0);
232                  if (fd != EOF)          if (fd != EOF)
233                          close(fd);                  close(fd);
234          }  
235            policy = "file read /dev/null if path1.perm=0666";
236            write_domain_policy(policy, 0);
237            fd = open("/dev/null", O_RDONLY);
238            show_result(fd, 1);
239            if (fd != EOF)
240                    close(fd);
241            write_domain_policy(policy, 1);
242            fd = open("/dev/null", O_RDONLY);
243            show_result(fd, 0);
244            if (fd != EOF)
245                    close(fd);
246    
247            policy = "file read /dev/null if path1.perm!=0777";
248            write_domain_policy(policy, 0);
249            fd = open("/dev/null", O_RDONLY);
250            show_result(fd, 1);
251            if (fd != EOF)
252                    close(fd);
253            write_domain_policy(policy, 1);
254            fd = open("/dev/null", O_RDONLY);
255            show_result(fd, 0);
256            if (fd != EOF)
257                    close(fd);
258    
259          policy = "allow_read /dev/null if path1.perm=0666";          policy = "file read /dev/null if path1.perm=owner_read "
         if (write_policy()) {  
                 int fd = open("/dev/null", O_RDONLY);  
                 show_result(fd, 1);  
                 if (fd != EOF)  
                         close(fd);  
                 delete_policy();  
                 fd = open("/dev/null", O_RDONLY);  
                 show_result(fd, 0);  
                 if (fd != EOF)  
                         close(fd);  
         }  
   
         policy = "allow_read /dev/null if path1.perm!=0777";  
         if (write_policy()) {  
                 int fd = open("/dev/null", O_RDONLY);  
                 show_result(fd, 1);  
                 if (fd != EOF)  
                         close(fd);  
                 delete_policy();  
                 fd = open("/dev/null", O_RDONLY);  
                 show_result(fd, 0);  
                 if (fd != EOF)  
                         close(fd);  
         }  
   
         policy = "allow_read /dev/null if path1.perm=owner_read "  
260                  "path1.perm=owner_write path1.perm!=owner_execute "                  "path1.perm=owner_write path1.perm!=owner_execute "
261                  "path1.perm=group_read path1.perm=group_write "                  "path1.perm=group_read path1.perm=group_write "
262                  "path1.perm!=group_execute path1.perm=others_read "                  "path1.perm!=group_execute path1.perm=others_read "
263                  "path1.perm=others_write path1.perm!=others_execute "                  "path1.perm=others_write path1.perm!=others_execute "
264                  "path1.perm!=setuid path1.perm!=setgid path1.perm!=sticky";                  "path1.perm!=setuid path1.perm!=setgid path1.perm!=sticky";
265          if (write_policy()) {          write_domain_policy(policy, 0);
266                  int fd = open("/dev/null", O_RDONLY);          fd = open("/dev/null", O_RDONLY);
267                  show_result(fd, 1);          show_result(fd, 1);
268                  if (fd != EOF)          if (fd != EOF)
269                          close(fd);                  close(fd);
270                  delete_policy();          write_domain_policy(policy, 1);
271                  fd = open("/dev/null", O_RDONLY);          fd = open("/dev/null", O_RDONLY);
272                  show_result(fd, 0);          show_result(fd, 0);
273                  if (fd != EOF)          if (fd != EOF)
274                          close(fd);                  close(fd);
         }  
275    
276          policy = "allow_mkfifo /tmp/mknod_fifo_test "          set_profile(3, "file::mkfifo");
277            policy = "file mkfifo /tmp/mknod_fifo_test 0644 "
278                  "if path1.parent.perm=01777 path1.parent.perm=sticky "                  "if path1.parent.perm=01777 path1.parent.perm=sticky "
279                  "path1.parent.uid=0 path1.parent.gid=0";                  "path1.parent.uid=0 path1.parent.gid=0";
280          if (write_policy()) {          write_domain_policy(policy, 0);
281                  filename = "/tmp/mknod_fifo_test";          filename = "/tmp/mknod_fifo_test";
282                  show_result(mknod(filename, S_IFIFO, 0), 1);          show_result(mknod(filename, S_IFIFO | 0644, 0), 1);
283                  delete_policy();          write_domain_policy(policy, 1);
284                  unlink2(filename);          unlink2(filename);
285                  show_result(mknod(filename, S_IFIFO, 0), 0);          show_result(mknod(filename, S_IFIFO | 0644, 0), 0);
         }  
   
         {  
                 char buffer[1024];  
                 struct stat sbuf;  
                 memset(buffer, 0, sizeof(buffer));  
                 memset(&sbuf, 0, sizeof(sbuf));  
                 filename = "/dev/null";  
                 stat(filename, &sbuf);  
                 snprintf(buffer, sizeof(buffer) - 1,  
                          "allow_write %s if path1.major=%u path1.minor=%u",  
                          filename, (unsigned int) MAJOR(sbuf.st_dev),  
                          (unsigned int) MINOR(sbuf.st_dev));  
                 policy = buffer;  
                 if (write_policy()) {  
                         int fd = open(filename, O_WRONLY);  
                         show_result(fd, 1);  
                         if (fd != EOF)  
                                 close(fd);  
                         delete_policy();  
                         fd = open(filename, O_WRONLY);  
                         show_result(fd, 0);  
                         if (fd != EOF)  
                                 close(fd);  
                 }  
         }  
   
         policy = "allow_read /dev/initctl if path1.type=fifo";  
         if (write_policy()) {  
                 int fd = open("/dev/initctl", O_RDONLY);  
                 show_result(fd, 1);  
                 if (fd != EOF)  
                         close(fd);  
                 delete_policy();  
                 fd = open("/dev/initctl", O_RDONLY);  
                 show_result(fd, 0);  
                 if (fd != EOF)  
                         close(fd);  
         }  
   
         policy = "allow_read /dev/null if path1.parent.ino=path1.parent.ino";  
         if (write_policy()) {  
                 int fd = open("/dev/null", O_RDONLY);  
                 show_result(fd, 1);  
                 if (fd != EOF)  
                         close(fd);  
                 delete_policy();  
                 fd = open("/dev/null", O_RDONLY);  
                 show_result(fd, 0);  
                 if (fd != EOF)  
                         close(fd);  
         }  
   
         policy = "allow_write /dev/null if path1.uid=path1.gid";  
         if (write_policy()) {  
                 int fd = open("/dev/null", O_WRONLY);  
                 show_result(fd, 1);  
                 if (fd != EOF)  
                         close(fd);  
                 delete_policy();  
                 fd = open("/dev/null", O_WRONLY);  
                 show_result(fd, 0);  
                 if (fd != EOF)  
                         close(fd);  
         }  
   
         policy = "allow_read/write /dev/null if task.uid=path1.parent.uid";  
         if (write_policy()) {  
                 int fd = open("/dev/null", O_RDWR);  
                 show_result(fd, 1);  
                 if (fd != EOF)  
                         close(fd);  
                 delete_policy();  
                 fd = open("/dev/null", O_RDWR);  
                 show_result(fd, 0);  
                 if (fd != EOF)  
                         close(fd);  
         }  
286    
287          policy = "allow_create /tmp/open_test if path1.parent.uid=task.uid";          memset(pbuffer, 0, sizeof(pbuffer));
288          if (write_policy()) {          memset(&sbuf, 0, sizeof(sbuf));
289                  policy = "allow_write /tmp/open_test if path1.parent.uid=0";          filename = "/dev/null";
290                  if (write_policy()) {          stat(filename, &sbuf);
291                          int fd = open("/tmp/open_test",          snprintf(pbuffer, sizeof(pbuffer) - 1,
292                                        O_WRONLY | O_CREAT | O_EXCL, 0666);                   "file write %s if path1.major=%u path1.minor=%u",
293                          show_result(fd, 1);                   filename, (unsigned int) MAJOR(sbuf.st_dev),
294                          if (fd != EOF)                   (unsigned int) MINOR(sbuf.st_dev));
295                                  close(fd);          policy = pbuffer;
296                          unlink2("/tmp/open_test");          write_domain_policy(policy, 0);
297                          delete_policy();          fd = open(filename, O_WRONLY);
298                          fd = open("/tmp/open_test",          show_result(fd, 1);
299                                    O_WRONLY | O_CREAT | O_EXCL, 0666);          if (fd != EOF)
300                          show_result(fd, 0);                  close(fd);
301                          if (fd != EOF)          write_domain_policy(policy, 1);
302                                  close(fd);          fd = open(filename, O_WRONLY);
303                          unlink2("/tmp/open_test");          show_result(fd, 0);
304                  }          if (fd != EOF)
305                  policy = "allow_create /tmp/open_test "                  close(fd);
306                          "if path1.parent.uid=task.uid\n";  
307                  delete_policy();          policy = "file read/write /tmp/fifo if path1.type=fifo";
308          }          mkfifo2("/tmp/fifo");
309            write_domain_policy(policy, 0);
310          policy = "allow_write /tmp/open_test if task.uid=0 path1.ino!=0";          fd = open("/tmp/fifo", O_RDWR);
311          if (write_policy()) {          show_result(fd, 1);
312                  policy = "allow_create /tmp/open_test if 0=0";          if (fd != EOF)
313                  if (write_policy()) {                  close(fd);
314                          int fd = open("/tmp/open_test",          write_domain_policy(policy, 1);
315                                        O_WRONLY | O_CREAT | O_EXCL, 0666);          fd = open("/tmp/fifo", O_RDWR);
316                          show_result(fd, 1);          show_result(fd, 0);
317                          if (fd != EOF)          if (fd != EOF)
318                                  close(fd);                  close(fd);
319                          unlink2("/tmp/open_test");  
320                          delete_policy();          policy = "file read /dev/null if path1.parent.ino=path1.parent.ino";
321                          fd = open("/tmp/open_test",          write_domain_policy(policy, 0);
322                                    O_WRONLY | O_CREAT | O_EXCL, 0666);          fd = open("/dev/null", O_RDONLY);
323                          show_result(fd, 0);          show_result(fd, 1);
324                          if (fd != EOF)          if (fd != EOF)
325                                  close(fd);                  close(fd);
326                          unlink2("/tmp/open_test");          write_domain_policy(policy, 1);
327                  }          fd = open("/dev/null", O_RDONLY);
328                  policy = "allow_write /tmp/open_test "          show_result(fd, 0);
329                          "if task.uid=0 path1.ino!=0\n";          if (fd != EOF)
330                  delete_policy();                  close(fd);
331          }  
332            policy = "file write /dev/null if path1.uid=path1.gid";
333            write_domain_policy(policy, 0);
334            fd = open("/dev/null", O_WRONLY);
335            show_result(fd, 1);
336            if (fd != EOF)
337                    close(fd);
338            write_domain_policy(policy, 1);
339            fd = open("/dev/null", O_WRONLY);
340            show_result(fd, 0);
341            if (fd != EOF)
342                    close(fd);
343    
344            policy = "file read/write /dev/null if task.uid=path1.parent.uid";
345            write_domain_policy(policy, 0);
346            fd = open("/dev/null", O_RDWR);
347            show_result(fd, 1);
348            if (fd != EOF)
349                    close(fd);
350            write_domain_policy(policy, 1);
351            fd = open("/dev/null", O_RDWR);
352            show_result(fd, 0);
353            if (fd != EOF)
354                    close(fd);
355    
356            policy = "file create /tmp/open_test 0644 "
357                    "if path1.parent.uid=task.uid";
358            write_domain_policy(policy, 0);
359            policy = "file write /tmp/open_test if path1.parent.uid=0";
360            write_domain_policy(policy, 0);
361            fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0644);
362            show_result(fd, 1);
363            if (fd != EOF)
364                    close(fd);
365            unlink2("/tmp/open_test");
366            write_domain_policy(policy, 1);
367            fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0644);
368            show_result(fd, 0);
369            if (fd != EOF)
370                    close(fd);
371            unlink2("/tmp/open_test");
372    
373            policy = "file create /tmp/open_test 0644 "
374                    "if path1.parent.uid=task.uid";
375            write_domain_policy(policy, 1);
376    
377            policy = "file write /tmp/open_test if task.uid=0 path1.ino!=0";
378            write_domain_policy(policy, 0);
379            policy = "file create /tmp/open_test 0644 if 0=0";
380            write_domain_policy(policy, 0);
381            fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0644);
382            show_result(fd, 1);
383            if (fd != EOF)
384                    close(fd);
385            unlink2("/tmp/open_test");
386            write_domain_policy(policy, 1);
387            fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0644);
388            show_result(fd, 0);
389            if (fd != EOF)
390                    close(fd);
391            unlink2("/tmp/open_test");
392            policy = "file write /tmp/open_test if task.uid=0 path1.ino!=0";
393            write_domain_policy(policy, 1);
394    
395          filename = "/tmp/truncate_test";          filename = "/tmp/truncate_test";
396          create2(filename);          create2(filename);
397    
398          policy = "allow_truncate /tmp/truncate_test if task.uid=path1.uid";          policy = "file truncate /tmp/truncate_test if task.uid=path1.uid";
399          if (write_policy()) {          write_domain_policy(policy, 0);
400                  policy = "allow_write /tmp/truncate_test if 1!=100-1000000";          policy = "file write /tmp/truncate_test if 1!=100-1000000";
401                  if (write_policy()) {          write_domain_policy(policy, 0);
402                          int fd = open(filename, O_WRONLY | O_TRUNC);          fd = open(filename, O_WRONLY | O_TRUNC);
403                          show_result(fd, 1);          show_result(fd, 1);
404                          if (fd != EOF)          if (fd != EOF)
405                                  close(fd);                  close(fd);
406                          delete_policy();          write_domain_policy(policy, 1);
407                          fd = open(filename, O_WRONLY | O_TRUNC);          fd = open(filename, O_WRONLY | O_TRUNC);
408                          show_result(fd, 0);          show_result(fd, 0);
409                          if (fd != EOF)          if (fd != EOF)
410                                  close(fd);                  close(fd);
411                  }          policy = "file truncate /tmp/truncate_test "
412                  policy = "allow_truncate /tmp/truncate_test "                  "if task.uid=path1.uid";
413                          "if task.uid=path1.uid";          write_domain_policy(policy, 1);
414                  delete_policy();  
415          }          policy = "file write /tmp/truncate_test";
416            write_domain_policy(policy, 0);
417          policy = "allow_write /tmp/truncate_test";          policy = "file truncate /tmp/truncate_test";
418          if (write_policy()) {          write_domain_policy(policy, 0);
419                  policy = "allow_truncate /tmp/truncate_test";          fd = open(filename, O_WRONLY | O_TRUNC);
420                  if (write_policy()) {          show_result(fd, 1);
421                          int fd = open(filename, O_WRONLY | O_TRUNC);          if (fd != EOF)
422                          show_result(fd, 1);                  close(fd);
423                          if (fd != EOF)          write_domain_policy(policy, 1);
424                                  close(fd);          fd = open(filename, O_WRONLY | O_TRUNC);
425                          delete_policy();          show_result(fd, 0);
426                          fd = open(filename, O_WRONLY | O_TRUNC);          if (fd != EOF)
427                          show_result(fd, 0);                  close(fd);
428                          if (fd != EOF)          policy = "file write /tmp/truncate_test";
429                                  close(fd);          write_domain_policy(policy, 1);
430                  }  
431                  policy = "allow_write /tmp/truncate_test\n";          policy = "file truncate /tmp/truncate_test";
432                  delete_policy();          write_domain_policy(policy, 0);
433          }          show_result(truncate(filename, 0), 1);
434            write_domain_policy(policy, 1);
435          policy = "allow_truncate /tmp/truncate_test";          show_result(truncate(filename, 0), 0);
436          if (write_policy()) {  
437                  show_result(truncate(filename, 0), 1);          policy = "file truncate /tmp/truncate_test";
438                  delete_policy();          write_domain_policy(policy, 0);
439                  show_result(truncate(filename, 0), 0);          set_profile(0, "file::open");
440          }          fd = open(filename, O_WRONLY);
441            set_profile(3, "file::open");
442          policy = "allow_truncate /tmp/truncate_test";          show_result(ftruncate(fd, 0), 1);
443          if (write_policy()) {          write_domain_policy(policy, 1);
444                  int fd;          show_result(ftruncate(fd, 0), 0);
445                  const char *cp = "255-MAC_FOR_FILE=disabled\n";          if (fd != EOF)
446                  write(profile_fd, cp, strlen(cp));                  close(fd);
                 fd = open(filename, O_WRONLY);  
                 cp = "255-MAC_FOR_FILE=enforcing\n";  
                 write(profile_fd, cp, strlen(cp));  
                 show_result(ftruncate(fd, 0), 1);  
                 delete_policy();  
                 show_result(ftruncate(fd, 0), 0);  
                 if (fd != EOF)  
                         close(fd);  
         }  
447    
448          unlink2(filename);          unlink2(filename);
449    
450          policy = "allow_create /tmp/mknod_reg_test";          policy = "file create /tmp/mknod_reg_test 0644";
451          if (write_policy()) {          write_domain_policy(policy, 0);
452                  filename = "/tmp/mknod_reg_test";          filename = "/tmp/mknod_reg_test";
453                  show_result(mknod(filename, S_IFREG, 0), 1);          show_result(mknod(filename, S_IFREG | 0644, 0), 1);
454                  delete_policy();          write_domain_policy(policy, 1);
455                  unlink2(filename);          unlink2(filename);
456                  show_result(mknod(filename, S_IFREG, 0), 0);          show_result(mknod(filename, S_IFREG | 0644, 0), 0);
         }  
457    
458          policy = "allow_mkchar /tmp/mknod_chr_test";          policy = "file mkchar /tmp/mknod_chr_test 0644 1 3";
459          if (write_policy()) {          write_domain_policy(policy, 0);
460                  filename = "/tmp/mknod_chr_test";          filename = "/tmp/mknod_chr_test";
461                  show_result(mknod(filename, S_IFCHR, MKDEV(1, 3)), 1);          show_result(mknod(filename, S_IFCHR | 0644, MKDEV(1, 3)), 1);
462                  delete_policy();          write_domain_policy(policy, 1);
463                  unlink2(filename);          unlink2(filename);
464                  show_result(mknod(filename, S_IFCHR, MKDEV(1, 3)), 0);          show_result(mknod(filename, S_IFCHR | 0644, MKDEV(1, 3)), 0);
         }  
465    
466          policy = "allow_mkblock /tmp/mknod_blk_test";          policy = "file mkblock /tmp/mknod_blk_test 0644 1 0";
467          if (write_policy()) {          write_domain_policy(policy, 0);
468                  filename = "/tmp/mknod_blk_test";          filename = "/tmp/mknod_blk_test";
469                  show_result(mknod(filename, S_IFBLK, MKDEV(1, 0)), 1);          show_result(mknod(filename, S_IFBLK | 0644, MKDEV(1, 0)), 1);
470                  delete_policy();          write_domain_policy(policy, 1);
471                  unlink2(filename);          unlink2(filename);
472                  show_result(mknod(filename, S_IFBLK, MKDEV(1, 0)), 0);          show_result(mknod(filename, S_IFBLK | 0644, MKDEV(1, 0)), 0);
         }  
473    
474          policy = "allow_mkfifo /tmp/mknod_fifo_test";          policy = "file mkfifo /tmp/mknod_fifo_test 0644";
475          if (write_policy()) {          write_domain_policy(policy, 0);
476                  filename = "/tmp/mknod_fifo_test";          filename = "/tmp/mknod_fifo_test";
477                  show_result(mknod(filename, S_IFIFO, 0), 1);          show_result(mknod(filename, S_IFIFO | 0644, 0), 1);
478                  delete_policy();          write_domain_policy(policy, 1);
479                  unlink2(filename);          unlink2(filename);
480                  show_result(mknod(filename, S_IFIFO, 0), 0);          show_result(mknod(filename, S_IFIFO | 0644, 0), 0);
         }  
481    
482          policy = "allow_mksock /tmp/mknod_sock_test";          policy = "file mksock /tmp/mknod_sock_test 0644";
483          if (write_policy()) {          write_domain_policy(policy, 0);
484                  filename = "/tmp/mknod_sock_test";          filename = "/tmp/mknod_sock_test";
485                  show_result(mknod(filename, S_IFSOCK, 0), 1);          show_result(mknod(filename, S_IFSOCK | 0644, 0), 1);
486                  delete_policy();          write_domain_policy(policy, 1);
487                  unlink2(filename);          unlink2(filename);
488                  show_result(mknod(filename, S_IFSOCK, 0), 0);          show_result(mknod(filename, S_IFSOCK | 0644, 0), 0);
         }  
489    
490          policy = "allow_mkdir /tmp/mkdir_test/";          policy = "file mkdir /tmp/mkdir_test/ 0600";
491          if (write_policy()) {          write_domain_policy(policy, 0);
492                  filename = "/tmp/mkdir_test";          filename = "/tmp/mkdir_test";
493                  show_result(mkdir(filename, 0600), 1);          show_result(mkdir(filename, 0600), 1);
494                  delete_policy();          write_domain_policy(policy, 1);
495                  rmdir2(filename);          rmdir2(filename);
496                  show_result(mkdir(filename, 0600), 0);          show_result(mkdir(filename, 0600), 0);
497          }  
498            policy = "file rmdir /tmp/rmdir_test/";
499            write_domain_policy(policy, 0);
500            filename = "/tmp/rmdir_test";
501            mkdir2(filename);
502            show_result(rmdir(filename), 1);
503            write_domain_policy(policy, 1);
504            mkdir2(filename);
505            show_result(rmdir(filename), 0);
506            rmdir2(filename);
507    
508            policy = "file unlink /tmp/unlink_test";
509            write_domain_policy(policy, 0);
510            filename = "/tmp/unlink_test";
511            create2(filename);
512            show_result(unlink(filename), 1);
513            write_domain_policy(policy, 1);
514            create2(filename);
515            show_result(unlink(filename), 0);
516            unlink2(filename);
517    
518          policy = "allow_rmdir /tmp/rmdir_test/";          policy = "file symlink /tmp/symlink_source_test";
519          if (write_policy()) {          write_domain_policy(policy, 0);
520                  filename = "/tmp/rmdir_test";          filename = "/tmp/symlink_source_test";
521                  mkdir2(filename);          show_result(symlink("/tmp/symlink_dest_test", filename), 1);
522                  show_result(rmdir(filename), 1);          write_domain_policy(policy, 1);
523                  delete_policy();          unlink2(filename);
524                  mkdir2(filename);          show_result(symlink("/tmp/symlink_dest_test", filename), 0);
                 show_result(rmdir(filename), 0);  
                 rmdir2(filename);  
         }  
525    
526          policy = "allow_unlink /tmp/unlink_test";          policy = "file symlink /tmp/symlink_source_test "
527          if (write_policy()) {                  "if symlink.target=\"/tmp/symlink_\\*_test\"";
528                  filename = "/tmp/unlink_test";          write_domain_policy(policy, 0);
529                  create2(filename);          filename = "/tmp/symlink_source_test";
530                  show_result(unlink(filename), 1);          show_result(symlink("/tmp/symlink_dest_test", filename), 1);
531                  delete_policy();          write_domain_policy(policy, 1);
532                  create2(filename);          unlink2(filename);
533                  show_result(unlink(filename), 0);          show_result(symlink("/tmp/symlink_dest_test", filename), 0);
                 unlink2(filename);  
         }  
534    
535          policy = "allow_symlink /tmp/symlink_source_test";          policy = "file symlink /tmp/symlink_source_test "
536          if (write_policy()) {                  "if task.uid=0 symlink.target=\"/tmp/symlink_\\*_test\"";
537                  filename = "/tmp/symlink_source_test";          write_domain_policy(policy, 0);
538                  show_result(symlink("/tmp/symlink_dest_test", filename), 1);          filename = "/tmp/symlink_source_test";
539                  delete_policy();          show_result(symlink("/tmp/symlink_dest_test", filename), 1);
540                  unlink2(filename);          write_domain_policy(policy, 1);
541                  show_result(symlink("/tmp/symlink_dest_test", filename), 0);          unlink2(filename);
542          }          show_result(symlink("/tmp/symlink_dest_test", filename), 0);
543    
544          policy = "allow_link /tmp/link_source_test /tmp/link_dest_test";          policy = "file symlink /tmp/symlink_source_test "
545          if (write_policy()) {                  "if symlink.target!=\"\\*\"";
546                  filename = "/tmp/link_source_test";          write_domain_policy(policy, 0);
547                  create2(filename);          filename = "/tmp/symlink_source_test";
548                  show_result(link(filename, "/tmp/link_dest_test"), 1);          show_result(symlink("/tmp/symlink_dest_test", filename), 1);
549                  delete_policy();          write_domain_policy(policy, 1);
550                  unlink2("/tmp/link_dest_test");          unlink2(filename);
551                  show_result(link(filename, "/tmp/link_dest_test"), 0);          show_result(symlink("/tmp/symlink_dest_test", filename), 0);
                 unlink2(filename);  
         }  
552    
553          policy = "allow_rename /tmp/rename_source_test /tmp/rename_dest_test";          policy = "file symlink /tmp/symlink_source_test "
554          if (write_policy()) {                  "if symlink.target!=\"/tmp/symlink_\\*_test\"";
555                  filename = "/tmp/rename_source_test";          write_domain_policy(policy, 0);
556                  create2(filename);          filename = "/tmp/symlink_source_test";
557                  show_result(rename(filename, "/tmp/rename_dest_test"), 1);          show_result(symlink("/tmp/symlink_dest_test", filename), 0);
558                  delete_policy();          write_domain_policy(policy, 1);
559                  unlink2("/tmp/rename_dest_test");          unlink2(filename);
560                  create2(filename);          show_result(symlink("/tmp/symlink_dest_test", filename), 0);
                 show_result(rename(filename, "/tmp/rename_dest_test"), 0);  
                 unlink2(filename);  
         }  
561    
562          policy = "allow_mksock /tmp/socket_test";          policy = "file link /tmp/link_source_test /tmp/link_dest_test";
563          if (write_policy()) {          write_domain_policy(policy, 0);
564                  struct sockaddr_un addr;          filename = "/tmp/link_source_test";
565                  int fd;          create2(filename);
566                  filename = "/tmp/socket_test";          show_result(link(filename, "/tmp/link_dest_test"), 1);
567                  memset(&addr, 0, sizeof(addr));          write_domain_policy(policy, 1);
568                  addr.sun_family = AF_UNIX;          unlink2("/tmp/link_dest_test");
569                  strncpy(addr.sun_path, filename, sizeof(addr.sun_path) - 1);          show_result(link(filename, "/tmp/link_dest_test"), 0);
570                  fd = socket(AF_UNIX, SOCK_STREAM, 0);          unlink2(filename);
                 show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)),  
                             1);  
                 if (fd != EOF)  
                         close(fd);  
                 delete_policy();  
                 unlink2(filename);  
                 fd = socket(AF_UNIX, SOCK_STREAM, 0);  
                 show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)),  
                             0);  
                 if (fd != EOF)  
                         close(fd);  
         }  
571    
572          filename = "/tmp/rewrite_test";          policy = "file rename /tmp/rename_source_test /tmp/rename_dest_test";
573            write_domain_policy(policy, 0);
574            filename = "/tmp/rename_source_test";
575            create2(filename);
576            show_result(rename(filename, "/tmp/rename_dest_test"), 1);
577            write_domain_policy(policy, 1);
578            unlink2("/tmp/rename_dest_test");
579          create2(filename);          create2(filename);
580          policy = "allow_read/write /tmp/rewrite_test";          show_result(rename(filename, "/tmp/rename_dest_test"), 0);
581          if (write_policy()) {          unlink2(filename);
                 char *cp = "deny_rewrite /tmp/rewrite_test\n";  
                 write(exception_fd, cp, strlen(cp));  
                 policy = "allow_truncate /tmp/rewrite_test";  
                 if (write_policy()) {  
                         int fd;  
   
                         fd = open(filename, O_RDONLY);  
                         show_result(fd, 1);  
                         if (fd != EOF)  
                                 close(fd);  
   
                         fd = open(filename, O_WRONLY | O_APPEND);  
                         show_result(fd, 1);  
                         if (fd != EOF)  
                                 close(fd);  
   
                         fd = open(filename, O_WRONLY);  
                         show_result(fd, 0);  
                         if (fd != EOF)  
                                 close(fd);  
   
                         fd = open(filename, O_WRONLY | O_TRUNC);  
                         show_result(fd, 0);  
                         if (fd != EOF)  
                                 close(fd);  
   
                         fd = open(filename, O_WRONLY | O_TRUNC | O_APPEND);  
                         show_result(fd, 0);  
                         if (fd != EOF)  
                                 close(fd);  
   
                         show_result(truncate(filename, 0), 0);  
   
                         cp = "255-MAC_FOR_FILE=disabled\n";  
                         write(profile_fd, cp, strlen(cp));  
                         fd = open(filename, O_WRONLY | O_APPEND);  
                         cp = "255-MAC_FOR_FILE=enforcing\n";  
                         write(profile_fd, cp, strlen(cp));  
                         show_result(ftruncate(fd, 0), 0);  
   
                         show_result(fcntl(fd, F_SETFL,  
                                           fcntl(fd, F_GETFL) & ~O_APPEND), 0);  
                         if (fd != EOF)  
                                 close(fd);  
582    
583                          delete_policy();          policy = "file mksock /tmp/socket_test 0755";
584                  }          write_domain_policy(policy, 0);
585                  policy = "allow_read/write /tmp/rewrite_test";          filename = "/tmp/socket_test";
586                  delete_policy();          memset(&addr, 0, sizeof(addr));
587                  cp = "delete deny_rewrite /tmp/rewrite_test\n";          addr.sun_family = AF_UNIX;
588                  write(exception_fd, cp, strlen(cp));          strncpy(addr.sun_path, filename, sizeof(addr.sun_path) - 1);
589          }          fd = socket(AF_UNIX, SOCK_STREAM, 0);
590            show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)),
591                        1);
592            if (fd != EOF)
593                    close(fd);
594            write_domain_policy(policy, 1);
595            unlink2(filename);
596            fd = socket(AF_UNIX, SOCK_STREAM, 0);
597            show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)),
598                        0);
599            if (fd != EOF)
600                    close(fd);
601          unlink2(filename);          unlink2(filename);
602    
603            policy = "file ioctl socket:[family=2:type=2:protocol=17] "
604                    "35122-35124 if task.uid=0";
605            write_domain_policy(policy, 0);
606            fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
607            memset(&ifreq, 0, sizeof(ifreq));
608            snprintf(ifreq.ifr_name, sizeof(ifreq.ifr_name) - 1,
609                     "lo");
610            show_result(ioctl(fd, 35123, &ifreq), 1);
611            write_domain_policy(policy, 1);
612            policy = "file ioctl "
613                    "socket:[family=2:type=2:protocol=17] 0-35122";
614            write_domain_policy(policy, 0);
615            show_result(ioctl(fd, 35123, &ifreq), 0);
616            write_domain_policy(policy, 1);
617            if (fd != EOF)
618                    close(fd);
619  }  }
620    
621  int main(int argc, char *argv[])  int main(int argc, char *argv[])
622  {  {
         char *cp;  
623          ccs_test_init();          ccs_test_init();
624          domain_fd = open(proc_policy_domain_policy, O_WRONLY);          fprintf(domain_fp, "%s /bin/true\n", self_domain);
625          exception_fd = open(proc_policy_exception_policy, O_WRONLY);          fprintf(domain_fp, "use_profile 255\n");
626          {          fprintf(domain_fp, "select pid=%u\n", pid);
627                  int self_fd = open(proc_policy_self_domain, O_RDONLY);          fprintf(profile_fp, "255-PREFERENCE::audit={ max_reject_log=1024 }\n");
                 memset(self_domain, 0, sizeof(self_domain));  
                 read(self_fd, self_domain, sizeof(self_domain) - 1);  
                 close(self_fd);  
                 write(domain_fd, self_domain, strlen(self_domain));  
                 cp = " /bin/true\n";  
                 write(domain_fd, cp, strlen(cp));  
                 write(domain_fd, self_domain, strlen(self_domain));  
                 write(domain_fd, "\n", 1);  
                 cp = "use_profile 255\n";  
                 write(domain_fd, cp, strlen(cp));  
         }  
         cp = "255-MAX_REJECT_LOG=1024\n";  
         write(profile_fd, cp, strlen(cp));  
628          stage_file_test();          stage_file_test();
629          cp = "use_profile 0\n";          fprintf(domain_fp, "use_profile 0\n");
         write(domain_fd, cp, strlen(cp));  
630          clear_status();          clear_status();
631          return 0;          return 0;
632  }  }

Legend:
Removed from v.1996  
changed lines
  Added in v.3871

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