]> mj.ucw.cz Git - moe.git/blobdiff - box/box.c
Created documentation
[moe.git] / box / box.c
index f574f864dbd3fb0eb6909dd18eaecbcc38b6526c..892cc53eb9b8bee98fb369f7b4e8810a342375ad 100644 (file)
--- a/box/box.c
+++ b/box/box.c
@@ -24,6 +24,7 @@
 #include <sys/sysinfo.h>
 #include <sys/syscall.h>
 #include <sys/resource.h>
 #include <sys/sysinfo.h>
 #include <sys/syscall.h>
 #include <sys/resource.h>
+#include <linux/ptrace.h>
 
 #define NONRET __attribute__((noreturn))
 #define UNUSED __attribute__((unused))
 
 #define NONRET __attribute__((noreturn))
 #define UNUSED __attribute__((unused))
 static int filter_syscalls;            /* 0=off, 1=liberal, 2=totalitarian */
 static int timeout;                    /* milliseconds */
 static int wall_timeout;
 static int filter_syscalls;            /* 0=off, 1=liberal, 2=totalitarian */
 static int timeout;                    /* milliseconds */
 static int wall_timeout;
+static int extra_timeout;
 static int pass_environ;
 static int file_access;
 static int verbose;
 static int memory_limit;
 static int pass_environ;
 static int file_access;
 static int verbose;
 static int memory_limit;
+static int stack_limit;
 static char *redir_stdin, *redir_stdout, *redir_stderr;
 static char *set_cwd;
 
 static char *redir_stdin, *redir_stdout, *redir_stderr;
 static char *set_cwd;
 
@@ -116,12 +119,15 @@ box_exit(int rc)
        ptrace(PTRACE_KILL, box_pid);
       kill(-box_pid, SIGKILL);
       kill(box_pid, SIGKILL);
        ptrace(PTRACE_KILL, box_pid);
       kill(-box_pid, SIGKILL);
       kill(box_pid, SIGKILL);
+      meta_printf("killed:1\n");
 
       struct rusage rus;
 
       struct rusage rus;
-      int stat;
-      int p = wait4(box_pid, &stat, 0, &rus);
+      int p, stat;
+      do
+       p = wait4(box_pid, &stat, 0, &rus);
+      while (p < 0 && errno == EINTR);
       if (p < 0)
       if (p < 0)
-       fprintf(stderr, "UGH: Lost track of the process\n");
+       fprintf(stderr, "UGH: Lost track of the process (%m)\n");
       else
        final_stats(&rus);
     }
       else
        final_stats(&rus);
     }
@@ -212,6 +218,7 @@ enum action {
   A_YES,               // Always permit
   A_FILENAME,          // Permit if arg1 is a known filename
   A_ACTION_MASK = 15,
   A_YES,               // Always permit
   A_FILENAME,          // Permit if arg1 is a known filename
   A_ACTION_MASK = 15,
+  A_NO_RETVAL = 32,    // Does not return a value
   A_SAMPLE_MEM = 64,   // Sample memory usage before the syscall
   A_LIBERAL = 128,     // Valid only in liberal mode
   // Must fit in a unsigned char
   A_SAMPLE_MEM = 64,   // Sample memory usage before the syscall
   A_LIBERAL = 128,     // Valid only in liberal mode
   // Must fit in a unsigned char
@@ -298,7 +305,7 @@ static unsigned char syscall_action[NUM_ACTIONS] = {
     S(readdir) = A_YES | A_LIBERAL,
     S(setitimer) = A_YES | A_LIBERAL,
     S(getitimer) = A_YES | A_LIBERAL,
     S(readdir) = A_YES | A_LIBERAL,
     S(setitimer) = A_YES | A_LIBERAL,
     S(getitimer) = A_YES | A_LIBERAL,
-    S(sigreturn) = A_YES | A_LIBERAL,
+    S(sigreturn) = A_YES | A_LIBERAL | A_NO_RETVAL,
     S(mprotect) = A_YES | A_LIBERAL,
     S(sigprocmask) = A_YES | A_LIBERAL,
     S(getdents) = A_YES | A_LIBERAL,
     S(mprotect) = A_YES | A_LIBERAL,
     S(sigprocmask) = A_YES | A_LIBERAL,
     S(getdents) = A_YES | A_LIBERAL,
@@ -309,7 +316,7 @@ static unsigned char syscall_action[NUM_ACTIONS] = {
     S(poll) = A_YES | A_LIBERAL,
     S(getcwd) = A_YES | A_LIBERAL,
     S(nanosleep) = A_YES | A_LIBERAL,
     S(poll) = A_YES | A_LIBERAL,
     S(getcwd) = A_YES | A_LIBERAL,
     S(nanosleep) = A_YES | A_LIBERAL,
-    S(rt_sigreturn) = A_YES | A_LIBERAL,
+    S(rt_sigreturn) = A_YES | A_LIBERAL | A_NO_RETVAL,
     S(rt_sigaction) = A_YES | A_LIBERAL,
     S(rt_sigprocmask) = A_YES | A_LIBERAL,
     S(rt_sigpending) = A_YES | A_LIBERAL,
     S(rt_sigaction) = A_YES | A_LIBERAL,
     S(rt_sigprocmask) = A_YES | A_LIBERAL,
     S(rt_sigpending) = A_YES | A_LIBERAL,
@@ -770,7 +777,7 @@ check_timeout(void)
       ms = (utime + stime) * 1000 / ticks_per_sec;
       if (verbose > 1)
        fprintf(stderr, "[time check: %d msec]\n", ms);
       ms = (utime + stime) * 1000 / ticks_per_sec;
       if (verbose > 1)
        fprintf(stderr, "[time check: %d msec]\n", ms);
-      if (ms > timeout)
+      if (ms > timeout && ms > extra_timeout)
        err("TO: Time limit exceeded");
     }
 }
        err("TO: Time limit exceeded");
     }
 }
@@ -823,23 +830,27 @@ sample_mem_peak(void)
 static void
 boxkeeper(void)
 {
 static void
 boxkeeper(void)
 {
-  int syscall_count = 0;
+  int syscall_count = (filter_syscalls ? 0 : 1);
   struct sigaction sa;
 
   is_ptraced = 1;
   struct sigaction sa;
 
   is_ptraced = 1;
+
   bzero(&sa, sizeof(sa));
   sa.sa_handler = signal_int;
   sigaction(SIGINT, &sa, NULL);
   bzero(&sa, sizeof(sa));
   sa.sa_handler = signal_int;
   sigaction(SIGINT, &sa, NULL);
+
   gettimeofday(&start_time, NULL);
   ticks_per_sec = sysconf(_SC_CLK_TCK);
   if (ticks_per_sec <= 0)
     die("Invalid ticks_per_sec!");
   gettimeofday(&start_time, NULL);
   ticks_per_sec = sysconf(_SC_CLK_TCK);
   if (ticks_per_sec <= 0)
     die("Invalid ticks_per_sec!");
+
   if (timeout || wall_timeout)
     {
       sa.sa_handler = signal_alarm;
       sigaction(SIGALRM, &sa, NULL);
       alarm(1);
     }
   if (timeout || wall_timeout)
     {
       sa.sa_handler = signal_alarm;
       sigaction(SIGALRM, &sa, NULL);
       alarm(1);
     }
+
   for(;;)
     {
       struct rusage rus;
   for(;;)
     {
       struct rusage rus;
@@ -900,28 +911,38 @@ boxkeeper(void)
          int sig = WSTOPSIG(stat);
          if (sig == SIGTRAP)
            {
          int sig = WSTOPSIG(stat);
          if (sig == SIGTRAP)
            {
+             if (verbose > 2)
+               msg("[ptrace status %08x] ", stat);
+             static int stop_count;
+             if (!stop_count++)                /* Traceme request */
+               msg(">> Traceme request caught\n");
+             else
+               err("SG: Breakpoint");
+             ptrace(PTRACE_SYSCALL, box_pid, 0, 0);
+           }
+         else if (sig == (SIGTRAP | 0x80))
+           {
+             if (verbose > 2)
+               msg("[ptrace status %08x] ", stat);
              struct user u;
              struct user u;
-             static int stop_count = -1;
+             static unsigned int sys_tick, last_sys, last_act;
              if (ptrace(PTRACE_GETREGS, box_pid, NULL, &u) < 0)
                die("ptrace(PTRACE_GETREGS): %m");
              if (ptrace(PTRACE_GETREGS, box_pid, NULL, &u) < 0)
                die("ptrace(PTRACE_GETREGS): %m");
-             if (u.regs.orig_eax < 0)          /* Process issued a breakpoint instruction */
-               err("SG: Breakpoint");
-             stop_count++;
-             if (!stop_count)                  /* Traceme request */
-               msg(">> Traceme request caught\n");
-             else if (stop_count & 1)          /* Syscall entry */
+             unsigned int sys = u.regs.orig_eax;
+             if (++sys_tick & 1)               /* Syscall entry */
                {
                  char namebuf[32];
                  int act;
                {
                  char namebuf[32];
                  int act;
-                 msg(">> Syscall %-12s (%08lx,%08lx,%08lx) ", syscall_name(u.regs.orig_eax, namebuf), u.regs.ebx, u.regs.ecx, u.regs.edx);
+                 msg(">> Syscall %-12s (%08lx,%08lx,%08lx) ", syscall_name(sys, namebuf), u.regs.ebx, u.regs.ecx, u.regs.edx);
                  if (!exec_seen)
                    {
                      msg("[master] ");
                  if (!exec_seen)
                    {
                      msg("[master] ");
-                     if (u.regs.orig_eax == __NR_execve)
+                     if (sys == __NR_execve)
                        exec_seen = 1;
                    }
                  else if ((act = valid_syscall(&u)) >= 0)
                    {
                        exec_seen = 1;
                    }
                  else if ((act = valid_syscall(&u)) >= 0)
                    {
+                     last_act = act;
                      syscall_count++;
                      if (act & A_SAMPLE_MEM)
                        sample_mem_peak();
                      syscall_count++;
                      if (act & A_SAMPLE_MEM)
                        sample_mem_peak();
@@ -933,23 +954,41 @@ boxkeeper(void)
                       * so we have to change it to something harmless (e.g., an undefined
                       * syscall) and make the program continue.
                       */
                       * so we have to change it to something harmless (e.g., an undefined
                       * syscall) and make the program continue.
                       */
-                     unsigned int sys = u.regs.orig_eax;
                      u.regs.orig_eax = 0xffffffff;
                      if (ptrace(PTRACE_SETREGS, box_pid, NULL, &u) < 0)
                        die("ptrace(PTRACE_SETREGS): %m");
                      err("FO: Forbidden syscall %s", syscall_name(sys, namebuf));
                    }
                      u.regs.orig_eax = 0xffffffff;
                      if (ptrace(PTRACE_SETREGS, box_pid, NULL, &u) < 0)
                        die("ptrace(PTRACE_SETREGS): %m");
                      err("FO: Forbidden syscall %s", syscall_name(sys, namebuf));
                    }
+                 last_sys = sys;
                }
              else                                      /* Syscall return */
                }
              else                                      /* Syscall return */
-               msg("= %ld\n", u.regs.eax);
+               {
+                 if (sys == 0xffffffff)
+                   {
+                     /* Some syscalls (sigreturn et al.) do not return a value */
+                     if (!(last_act & A_NO_RETVAL))
+                       err("XX: Syscall does not return, but it should");
+                   }
+                 else
+                   {
+                     if (sys != last_sys)
+                       err("XX: Mismatched syscall entry/exit");
+                   }
+                 if (last_act & A_NO_RETVAL)
+                   msg("= ?\n");
+                 else
+                   msg("= %ld\n", u.regs.eax);
+               }
              ptrace(PTRACE_SYSCALL, box_pid, 0, 0);
            }
          else if (sig == SIGSTOP)
            {
              msg(">> SIGSTOP\n");
              ptrace(PTRACE_SYSCALL, box_pid, 0, 0);
            }
          else if (sig == SIGSTOP)
            {
              msg(">> SIGSTOP\n");
+             if (ptrace(PTRACE_SETOPTIONS, box_pid, NULL, (void *) PTRACE_O_TRACESYSGOOD) < 0)
+               die("ptrace(PTRACE_SETOPTIONS): %m");
              ptrace(PTRACE_SYSCALL, box_pid, 0, 0);
            }
              ptrace(PTRACE_SYSCALL, box_pid, 0, 0);
            }
-         else if (sig != SIGSTOP && sig != SIGXCPU && sig != SIGXFSZ)
+         else if (sig != SIGXCPU && sig != SIGXFSZ)
            {
              msg(">> Signal %d\n", sig);
              sample_mem_peak();                        /* Signal might be fatal, so update mem-peak */
            {
              msg(">> Signal %d\n", sig);
              sample_mem_peak();                        /* Signal might be fatal, so update mem-peak */
@@ -997,15 +1036,22 @@ box_inside(int argc, char **argv)
   else
     dup2(1, 2);
   setpgrp();
   else
     dup2(1, 2);
   setpgrp();
+
   if (memory_limit)
     {
       rl.rlim_cur = rl.rlim_max = memory_limit * 1024;
       if (setrlimit(RLIMIT_AS, &rl) < 0)
   if (memory_limit)
     {
       rl.rlim_cur = rl.rlim_max = memory_limit * 1024;
       if (setrlimit(RLIMIT_AS, &rl) < 0)
-       die("setrlimit: %m");
+       die("setrlimit(RLIMIT_AS): %m");
     }
     }
+
+  rl.rlim_cur = rl.rlim_max = (stack_limit ? (rlim_t)stack_limit * 1024 : RLIM_INFINITY);
+  if (setrlimit(RLIMIT_STACK, &rl) < 0)
+    die("setrlimit(RLIMIT_STACK): %m");
+
   rl.rlim_cur = rl.rlim_max = 64;
   if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
   rl.rlim_cur = rl.rlim_max = 64;
   if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
-    die("setrlimit: %m");
+    die("setrlimit(RLIMIT_NOFILE): %m");
+
   char **env = setup_environment();
   if (filter_syscalls)
     {
   char **env = setup_environment();
   if (filter_syscalls)
     {
@@ -1033,6 +1079,7 @@ Options:\n\
 -E <var>=<val>\tSet the environment variable <var> to <val>; unset it if <var> is empty\n\
 -f\t\tFilter system calls (-ff=very restricted)\n\
 -i <file>\tRedirect stdin from <file>\n\
 -E <var>=<val>\tSet the environment variable <var> to <val>; unset it if <var> is empty\n\
 -f\t\tFilter system calls (-ff=very restricted)\n\
 -i <file>\tRedirect stdin from <file>\n\
+-k <size>\tLimit stack size to <size> KB (default: 0=unlimited)\n\
 -m <size>\tLimit address space to <size> KB\n\
 -M <file>\tOutput process information to <file> (name:value)\n\
 -o <file>\tRedirect stdout to <file>\n\
 -m <size>\tLimit address space to <size> KB\n\
 -M <file>\tOutput process information to <file> (name:value)\n\
 -o <file>\tRedirect stdout to <file>\n\
@@ -1045,6 +1092,8 @@ Options:\n\
 -T\t\tAllow syscalls for measuring run time\n\
 -v\t\tBe verbose (use multiple times for even more verbosity)\n\
 -w <time>\tSet wall clock time limit (seconds, fractions allowed)\n\
 -T\t\tAllow syscalls for measuring run time\n\
 -v\t\tBe verbose (use multiple times for even more verbosity)\n\
 -w <time>\tSet wall clock time limit (seconds, fractions allowed)\n\
+-x <time>\tSet extra timeout, before which a timing-out program is not yet killed,\n\
+\t\tso that its real execution time is reported (seconds, fractions allowed)\n\
 ");
   exit(2);
 }
 ");
   exit(2);
 }
@@ -1055,7 +1104,7 @@ main(int argc, char **argv)
   int c;
   uid_t uid;
 
   int c;
   uid_t uid;
 
-  while ((c = getopt(argc, argv, "a:c:eE:fi:m:M:o:p:r:s:t:Tvw:")) >= 0)
+  while ((c = getopt(argc, argv, "a:c:eE:fi:k:m:M:o:p:r:s:t:Tvw:x:")) >= 0)
     switch (c)
       {
       case 'a':
     switch (c)
       {
       case 'a':
@@ -1074,6 +1123,9 @@ main(int argc, char **argv)
       case 'f':
        filter_syscalls++;
        break;
       case 'f':
        filter_syscalls++;
        break;
+      case 'k':
+       stack_limit = atol(optarg);
+       break;
       case 'i':
        redir_stdin = optarg;
        break;
       case 'i':
        redir_stdin = optarg;
        break;
@@ -1109,6 +1161,9 @@ main(int argc, char **argv)
       case 'w':
         wall_timeout = 1000*atof(optarg);
        break;
       case 'w':
         wall_timeout = 1000*atof(optarg);
        break;
+      case 'x':
+       extra_timeout = 1000*atof(optarg);
+       break;
       default:
        usage();
       }
       default:
        usage();
       }