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

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

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

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