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

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

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

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