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

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

revision 1996 by kumaneko, Mon Dec 22 06:08:22 2008 UTC revision 2592 by kumaneko, Wed May 27 07:32:57 2009 UTC
# Line 3  Line 3 
3   *   *
4   * Testing program for fs/tomoyo_file.c   * Testing program for fs/tomoyo_file.c
5   *   *
6   * Copyright (C) 2005-2008  NTT DATA CORPORATION   * Copyright (C) 2005-2009  NTT DATA CORPORATION
7   *   *
8   * Version: 1.6.6-pre   2008/12/22   * Version: 1.6.8   2009/05/28
9   *   *
10   */   */
11  #include "include.h"  #include "include.h"
# Line 14  static int domain_fd = EOF; Line 14  static int domain_fd = EOF;
14  static int exception_fd = EOF;  static int exception_fd = EOF;
15  static const char *policy = "";  static const char *policy = "";
16  static char self_domain[4096] = "";  static char self_domain[4096] = "";
17    static _Bool has_cond = 1;
18    
19  static int write_policy(void)  static int write_policy(void)
20  {  {
# Line 125  static void rmdir2(const char *pathname) Line 126  static void rmdir2(const char *pathname)
126          errno = 0;          errno = 0;
127  }  }
128    
129    static void mkfifo2(const char *pathname)
130    {
131            const char *cp = "255-MAC_FOR_FILE=disabled\n";
132            write(profile_fd, cp, strlen(cp));
133            mkfifo(pathname, 0600);
134            cp = "255-MAC_FOR_FILE=enforcing\n";
135            write(profile_fd, cp, strlen(cp));
136            errno = 0;
137    }
138    
139  static void stage_file_test(void)  static void stage_file_test(void)
140  {  {
141          char *filename = "";          char *filename = "";
142          policy = "allow_read /proc/sys/net/ipv4/ip_local_port_range "          policy = "allow_read /proc/sys/net/ipv4/ip_local_port_range "
143                  "if task.uid=0 task.gid=0";                  "if task.uid=0 task.gid=0";
144            if (!has_cond)
145                    policy = "allow_read /proc/sys/net/ipv4/ip_local_port_range";
146          if (write_policy()) {          if (write_policy()) {
147                  static int name[] = { CTL_NET, NET_IPV4,                  static int name[] = { CTL_NET, NET_IPV4,
148                                        NET_IPV4_LOCAL_PORT_RANGE };                                        NET_IPV4_LOCAL_PORT_RANGE };
# Line 141  static void stage_file_test(void) Line 154  static void stage_file_test(void)
154          }          }
155          policy = "allow_write /proc/sys/net/ipv4/ip_local_port_range "          policy = "allow_write /proc/sys/net/ipv4/ip_local_port_range "
156                  "if task.euid=0 0=0 1-100=10-1000";                  "if task.euid=0 0=0 1-100=10-1000";
157            if (!has_cond)
158                    policy = "allow_write /proc/sys/net/ipv4/ip_local_port_range";
159          if (write_policy()) {          if (write_policy()) {
160                  static int name[] = { CTL_NET, NET_IPV4,                  static int name[] = { CTL_NET, NET_IPV4,
161                                        NET_IPV4_LOCAL_PORT_RANGE };                                        NET_IPV4_LOCAL_PORT_RANGE };
# Line 152  static void stage_file_test(void) Line 167  static void stage_file_test(void)
167          }          }
168          policy = "allow_read/write /proc/sys/net/ipv4/ip_local_port_range "          policy = "allow_read/write /proc/sys/net/ipv4/ip_local_port_range "
169                  "if 1!=10-100";                  "if 1!=10-100";
170            if (!has_cond)
171                    policy = "allow_read/write /proc/sys/net/ipv4/ip_local_port_range";
172          if (write_policy()) {          if (write_policy()) {
173                  static int name[] = { CTL_NET, NET_IPV4,                  static int name[] = { CTL_NET, NET_IPV4,
174                                        NET_IPV4_LOCAL_PORT_RANGE };                                        NET_IPV4_LOCAL_PORT_RANGE };
# Line 164  static void stage_file_test(void) Line 181  static void stage_file_test(void)
181    
182          policy = "allow_read /bin/true "          policy = "allow_read /bin/true "
183                  "if path1.uid=0 path1.parent.uid=0 10=10-100";                  "if path1.uid=0 path1.parent.uid=0 10=10-100";
184            if (!has_cond)
185                    policy = "allow_read /bin/true";
186          if (write_policy()) {          if (write_policy()) {
187                  show_result(uselib("/bin/true"), 1);                  show_result(uselib("/bin/true"), 1);
188                  delete_policy();                  delete_policy();
# Line 171  static void stage_file_test(void) Line 190  static void stage_file_test(void)
190          }          }
191    
192          policy = "allow_execute /bin/true if task.uid!=10 path1.parent.uid=0";          policy = "allow_execute /bin/true if task.uid!=10 path1.parent.uid=0";
193            if (!has_cond)
194                    policy = "allow_execute /bin/true";
195          if (write_policy()) {          if (write_policy()) {
196                  int pipe_fd[2] = { EOF, EOF };                  int pipe_fd[2] = { EOF, EOF };
197                  int err = 0;                  int err = 0;
# Line 209  static void stage_file_test(void) Line 230  static void stage_file_test(void)
230    
231          policy = "allow_read /dev/null if path1.type=char path1.dev_major=1 "          policy = "allow_read /dev/null if path1.type=char path1.dev_major=1 "
232                  "path1.dev_minor=3";                  "path1.dev_minor=3";
233            if (!has_cond)
234                    policy = "allow_read /dev/null";
235          if (write_policy()) {          if (write_policy()) {
236                  int fd = open("/dev/null", O_RDONLY);                  int fd = open("/dev/null", O_RDONLY);
237                  show_result(fd, 1);                  show_result(fd, 1);
# Line 222  static void stage_file_test(void) Line 245  static void stage_file_test(void)
245          }          }
246    
247          policy = "allow_read /dev/null if path1.perm=0666";          policy = "allow_read /dev/null if path1.perm=0666";
248            if (!has_cond)
249                    policy = "allow_read /dev/null";
250          if (write_policy()) {          if (write_policy()) {
251                  int fd = open("/dev/null", O_RDONLY);                  int fd = open("/dev/null", O_RDONLY);
252                  show_result(fd, 1);                  show_result(fd, 1);
# Line 235  static void stage_file_test(void) Line 260  static void stage_file_test(void)
260          }          }
261    
262          policy = "allow_read /dev/null if path1.perm!=0777";          policy = "allow_read /dev/null if path1.perm!=0777";
263            if (!has_cond)
264                    policy = "allow_read /dev/null";
265          if (write_policy()) {          if (write_policy()) {
266                  int fd = open("/dev/null", O_RDONLY);                  int fd = open("/dev/null", O_RDONLY);
267                  show_result(fd, 1);                  show_result(fd, 1);
# Line 253  static void stage_file_test(void) Line 280  static void stage_file_test(void)
280                  "path1.perm!=group_execute path1.perm=others_read "                  "path1.perm!=group_execute path1.perm=others_read "
281                  "path1.perm=others_write path1.perm!=others_execute "                  "path1.perm=others_write path1.perm!=others_execute "
282                  "path1.perm!=setuid path1.perm!=setgid path1.perm!=sticky";                  "path1.perm!=setuid path1.perm!=setgid path1.perm!=sticky";
283            if (!has_cond)
284                    policy = "allow_read /dev/null";
285          if (write_policy()) {          if (write_policy()) {
286                  int fd = open("/dev/null", O_RDONLY);                  int fd = open("/dev/null", O_RDONLY);
287                  show_result(fd, 1);                  show_result(fd, 1);
# Line 268  static void stage_file_test(void) Line 297  static void stage_file_test(void)
297          policy = "allow_mkfifo /tmp/mknod_fifo_test "          policy = "allow_mkfifo /tmp/mknod_fifo_test "
298                  "if path1.parent.perm=01777 path1.parent.perm=sticky "                  "if path1.parent.perm=01777 path1.parent.perm=sticky "
299                  "path1.parent.uid=0 path1.parent.gid=0";                  "path1.parent.uid=0 path1.parent.gid=0";
300            if (!has_cond)
301                    policy = "allow_mkfifo /tmp/mknod_fifo_test";
302          if (write_policy()) {          if (write_policy()) {
303                  filename = "/tmp/mknod_fifo_test";                  filename = "/tmp/mknod_fifo_test";
304                  show_result(mknod(filename, S_IFIFO, 0), 1);                  show_result(mknod(filename, S_IFIFO, 0), 1);
# Line 287  static void stage_file_test(void) Line 318  static void stage_file_test(void)
318                           "allow_write %s if path1.major=%u path1.minor=%u",                           "allow_write %s if path1.major=%u path1.minor=%u",
319                           filename, (unsigned int) MAJOR(sbuf.st_dev),                           filename, (unsigned int) MAJOR(sbuf.st_dev),
320                           (unsigned int) MINOR(sbuf.st_dev));                           (unsigned int) MINOR(sbuf.st_dev));
321                    if (!has_cond)
322                            snprintf(buffer, sizeof(buffer) - 1,
323                                     "allow_write %s", filename);
324                  policy = buffer;                  policy = buffer;
325                  if (write_policy()) {                  if (write_policy()) {
326                          int fd = open(filename, O_WRONLY);                          int fd = open(filename, O_WRONLY);
# Line 301  static void stage_file_test(void) Line 335  static void stage_file_test(void)
335                  }                  }
336          }          }
337    
338          policy = "allow_read /dev/initctl if path1.type=fifo";          policy = "allow_read/write /tmp/fifo if path1.type=fifo";
339            if (!has_cond)
340                    policy = "allow_read/write /tmp/fifo";
341            mkfifo2("/tmp/fifo");
342          if (write_policy()) {          if (write_policy()) {
343                  int fd = open("/dev/initctl", O_RDONLY);                  int fd = open("/tmp/fifo", O_RDWR);
344                  show_result(fd, 1);                  show_result(fd, 1);
345                  if (fd != EOF)                  if (fd != EOF)
346                          close(fd);                          close(fd);
347                  delete_policy();                  delete_policy();
348                  fd = open("/dev/initctl", O_RDONLY);                  fd = open("/tmp/fifo", O_RDWR);
349                  show_result(fd, 0);                  show_result(fd, 0);
350                  if (fd != EOF)                  if (fd != EOF)
351                          close(fd);                          close(fd);
352          }          }
353    
354          policy = "allow_read /dev/null if path1.parent.ino=path1.parent.ino";          policy = "allow_read /dev/null if path1.parent.ino=path1.parent.ino";
355            if (!has_cond)
356                    policy = "allow_read /dev/null";
357          if (write_policy()) {          if (write_policy()) {
358                  int fd = open("/dev/null", O_RDONLY);                  int fd = open("/dev/null", O_RDONLY);
359                  show_result(fd, 1);                  show_result(fd, 1);
# Line 328  static void stage_file_test(void) Line 367  static void stage_file_test(void)
367          }          }
368    
369          policy = "allow_write /dev/null if path1.uid=path1.gid";          policy = "allow_write /dev/null if path1.uid=path1.gid";
370            if (!has_cond)
371                    policy = "allow_write /dev/null";
372          if (write_policy()) {          if (write_policy()) {
373                  int fd = open("/dev/null", O_WRONLY);                  int fd = open("/dev/null", O_WRONLY);
374                  show_result(fd, 1);                  show_result(fd, 1);
# Line 341  static void stage_file_test(void) Line 382  static void stage_file_test(void)
382          }          }
383    
384          policy = "allow_read/write /dev/null if task.uid=path1.parent.uid";          policy = "allow_read/write /dev/null if task.uid=path1.parent.uid";
385            if (!has_cond)
386                    policy = "allow_read/write /dev/null";
387          if (write_policy()) {          if (write_policy()) {
388                  int fd = open("/dev/null", O_RDWR);                  int fd = open("/dev/null", O_RDWR);
389                  show_result(fd, 1);                  show_result(fd, 1);
# Line 354  static void stage_file_test(void) Line 397  static void stage_file_test(void)
397          }          }
398    
399          policy = "allow_create /tmp/open_test if path1.parent.uid=task.uid";          policy = "allow_create /tmp/open_test if path1.parent.uid=task.uid";
400            if (!has_cond)
401                    policy = "allow_create /tmp/open_test";
402          if (write_policy()) {          if (write_policy()) {
403                  policy = "allow_write /tmp/open_test if path1.parent.uid=0";                  policy = "allow_write /tmp/open_test if path1.parent.uid=0";
404                    if (!has_cond)
405                            policy = "allow_write /tmp/open_test";
406                  if (write_policy()) {                  if (write_policy()) {
407                          int fd = open("/tmp/open_test",                          int fd = open("/tmp/open_test",
408                                        O_WRONLY | O_CREAT | O_EXCL, 0666);                                        O_WRONLY | O_CREAT | O_EXCL, 0666);
# Line 372  static void stage_file_test(void) Line 419  static void stage_file_test(void)
419                          unlink2("/tmp/open_test");                          unlink2("/tmp/open_test");
420                  }                  }
421                  policy = "allow_create /tmp/open_test "                  policy = "allow_create /tmp/open_test "
422                          "if path1.parent.uid=task.uid\n";                          "if path1.parent.uid=task.uid";
423                    if (!has_cond)
424                            policy = "allow_create /tmp/open_test";
425                  delete_policy();                  delete_policy();
426          }          }
427    
428          policy = "allow_write /tmp/open_test if task.uid=0 path1.ino!=0";          policy = "allow_write /tmp/open_test if task.uid=0 path1.ino!=0";
429            if (!has_cond)
430                    policy = "allow_write /tmp/open_test";
431          if (write_policy()) {          if (write_policy()) {
432                  policy = "allow_create /tmp/open_test if 0=0";                  policy = "allow_create /tmp/open_test if 0=0";
433                    if (!has_cond)
434                            policy = "allow_create /tmp/open_test";
435                  if (write_policy()) {                  if (write_policy()) {
436                          int fd = open("/tmp/open_test",                          int fd = open("/tmp/open_test",
437                                        O_WRONLY | O_CREAT | O_EXCL, 0666);                                        O_WRONLY | O_CREAT | O_EXCL, 0666);
# Line 395  static void stage_file_test(void) Line 448  static void stage_file_test(void)
448                          unlink2("/tmp/open_test");                          unlink2("/tmp/open_test");
449                  }                  }
450                  policy = "allow_write /tmp/open_test "                  policy = "allow_write /tmp/open_test "
451                          "if task.uid=0 path1.ino!=0\n";                          "if task.uid=0 path1.ino!=0";
452                    if (!has_cond)
453                            policy = "allow_write /tmp/open_test";
454                  delete_policy();                  delete_policy();
455          }          }
456    
# Line 403  static void stage_file_test(void) Line 458  static void stage_file_test(void)
458          create2(filename);          create2(filename);
459    
460          policy = "allow_truncate /tmp/truncate_test if task.uid=path1.uid";          policy = "allow_truncate /tmp/truncate_test if task.uid=path1.uid";
461            if (!has_cond)
462                    policy = "allow_truncate /tmp/truncate_test";
463          if (write_policy()) {          if (write_policy()) {
464                  policy = "allow_write /tmp/truncate_test if 1!=100-1000000";                  policy = "allow_write /tmp/truncate_test if 1!=100-1000000";
465                    if (!has_cond)
466                            policy = "allow_write /tmp/truncate_test";
467                  if (write_policy()) {                  if (write_policy()) {
468                          int fd = open(filename, O_WRONLY | O_TRUNC);                          int fd = open(filename, O_WRONLY | O_TRUNC);
469                          show_result(fd, 1);                          show_result(fd, 1);
# Line 418  static void stage_file_test(void) Line 477  static void stage_file_test(void)
477                  }                  }
478                  policy = "allow_truncate /tmp/truncate_test "                  policy = "allow_truncate /tmp/truncate_test "
479                          "if task.uid=path1.uid";                          "if task.uid=path1.uid";
480                    if (!has_cond)
481                            policy = "allow_truncate /tmp/truncate_test";
482                  delete_policy();                  delete_policy();
483          }          }
484    
# Line 435  static void stage_file_test(void) Line 496  static void stage_file_test(void)
496                          if (fd != EOF)                          if (fd != EOF)
497                                  close(fd);                                  close(fd);
498                  }                  }
499                  policy = "allow_write /tmp/truncate_test\n";                  policy = "allow_write /tmp/truncate_test";
500                  delete_policy();                  delete_policy();
501          }          }
502    
# Line 548  static void stage_file_test(void) Line 609  static void stage_file_test(void)
609                  show_result(symlink("/tmp/symlink_dest_test", filename), 0);                  show_result(symlink("/tmp/symlink_dest_test", filename), 0);
610          }          }
611    
612            if (!has_cond)
613                    goto skip_symlink_target;
614    
615            policy = "allow_symlink /tmp/symlink_source_test "
616                    "if symlink.target=\"/tmp/symlink_\\*_test\"";
617            if (write_policy()) {
618                    filename = "/tmp/symlink_source_test";
619                    show_result(symlink("/tmp/symlink_dest_test", filename), 1);
620                    delete_policy();
621                    unlink2(filename);
622                    show_result(symlink("/tmp/symlink_dest_test", filename), 0);
623            }
624    
625            policy = "allow_symlink /tmp/symlink_source_test "
626                    "if task.uid=0 symlink.target=\"/tmp/symlink_\\*_test\"";
627            if (write_policy()) {
628                    filename = "/tmp/symlink_source_test";
629                    show_result(symlink("/tmp/symlink_dest_test", filename), 1);
630                    delete_policy();
631                    unlink2(filename);
632                    show_result(symlink("/tmp/symlink_dest_test", filename), 0);
633            }
634    
635            policy = "allow_symlink /tmp/symlink_source_test "
636                    "if symlink.target!=\"\\*\"";
637            if (write_policy()) {
638                    filename = "/tmp/symlink_source_test";
639                    show_result(symlink("/tmp/symlink_dest_test", filename), 1);
640                    delete_policy();
641                    unlink2(filename);
642                    show_result(symlink("/tmp/symlink_dest_test", filename), 0);
643            }
644    
645            policy = "allow_symlink /tmp/symlink_source_test "
646                    "if symlink.target!=\"/tmp/symlink_\\*_test\"";
647            if (write_policy()) {
648                    filename = "/tmp/symlink_source_test";
649                    show_result(symlink("/tmp/symlink_dest_test", filename), 0);
650                    delete_policy();
651                    unlink2(filename);
652                    show_result(symlink("/tmp/symlink_dest_test", filename), 0);
653            }
654    
655     skip_symlink_target:
656    
657          policy = "allow_link /tmp/link_source_test /tmp/link_dest_test";          policy = "allow_link /tmp/link_source_test /tmp/link_dest_test";
658          if (write_policy()) {          if (write_policy()) {
659                  filename = "/tmp/link_source_test";                  filename = "/tmp/link_source_test";
# Line 650  static void stage_file_test(void) Line 756  static void stage_file_test(void)
756                  write(exception_fd, cp, strlen(cp));                  write(exception_fd, cp, strlen(cp));
757          }          }
758          unlink2(filename);          unlink2(filename);
759    
760            if (has_cond) {
761                    const char *cp = "255-MAC_FOR_IOCTL=enforcing\n";
762                    write(profile_fd, cp, strlen(cp));
763                    policy = "allow_ioctl socket:[family=2:type=2:protocol=17] "
764                            "35122-35124 if task.uid=0";
765                    if (write_policy()) {
766                            struct ifreq ifreq;
767                            int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
768                            memset(&ifreq, 0, sizeof(ifreq));
769                            snprintf(ifreq.ifr_name, sizeof(ifreq.ifr_name) - 1, "lo");
770                            show_result(ioctl(fd, 35123, &ifreq), 1);
771                            delete_policy();
772                            policy = "allow_ioctl socket:[family=2:type=2:protocol=17] "
773                                    "0-35122";
774                            if (write_policy()) {
775                                    show_result(ioctl(fd, 35123, &ifreq), 0);
776                                    delete_policy();
777                            }
778                            if (fd != EOF)
779                                    close(fd);
780                    }
781                    cp = "255-MAC_FOR_IOCTL=disabled\n";
782                    write(profile_fd, cp, strlen(cp));
783            }
784  }  }
785    
786  int main(int argc, char *argv[])  int main(int argc, char *argv[])
# Line 657  int main(int argc, char *argv[]) Line 788  int main(int argc, char *argv[])
788          char *cp;          char *cp;
789          ccs_test_init();          ccs_test_init();
790          domain_fd = open(proc_policy_domain_policy, O_WRONLY);          domain_fd = open(proc_policy_domain_policy, O_WRONLY);
791            if (domain_fd == EOF && errno == ENOENT) {
792                    fprintf(stderr, "You can't use this program for this kernel."
793                            "\n");
794                    return 1;
795            }
796          exception_fd = open(proc_policy_exception_policy, O_WRONLY);          exception_fd = open(proc_policy_exception_policy, O_WRONLY);
797          {          {
798                  int self_fd = open(proc_policy_self_domain, O_RDONLY);                  int self_fd = open(proc_policy_self_domain, O_RDONLY);
# Line 671  int main(int argc, char *argv[]) Line 807  int main(int argc, char *argv[])
807                  cp = "use_profile 255\n";                  cp = "use_profile 255\n";
808                  write(domain_fd, cp, strlen(cp));                  write(domain_fd, cp, strlen(cp));
809          }          }
810            has_cond = !access("/proc/ccs/version", F_OK);
811          cp = "255-MAX_REJECT_LOG=1024\n";          cp = "255-MAX_REJECT_LOG=1024\n";
812          write(profile_fd, cp, strlen(cp));          write(profile_fd, cp, strlen(cp));
813          stage_file_test();          stage_file_test();

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

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