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

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

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

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