]> mj.ucw.cz Git - moe.git/blobdiff - box/box.c
Judge: Added function get_nl() for checking of an expected end of line.
[moe.git] / box / box.c
index 1a84e106aa2547b00cb2c8eba49376809cca9a89..ca2a292bbb3d2793e965b0ae1b5ea307e432c575 100644 (file)
--- a/box/box.c
+++ b/box/box.c
@@ -47,16 +47,11 @@ static int ticks_per_sec;
 static int exec_seen;
 static int partial_line;
 
 static int exec_seen;
 static int partial_line;
 
-#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0
-/* glibc 2.1 or newer -> has lseek64 */
-#define long_seek(f,o,w) lseek64(f,o,w)
-#else
-/* Touching clandestine places in glibc */
-extern loff_t llseek(int fd, loff_t pos, int whence);
-#define long_seek(f,o,w) llseek(f,o,w)
-#endif
+static int mem_peak_kb;
+static int total_ms, wall_ms;
 
 static void die(char *msg, ...) NONRET;
 
 static void die(char *msg, ...) NONRET;
+static void sample_mem_peak(void);
 
 /*** Meta-files ***/
 
 
 /*** Meta-files ***/
 
@@ -94,8 +89,6 @@ meta_printf(const char *fmt, ...)
   va_end(args);
 }
 
   va_end(args);
 }
 
-static int total_ms, wall_ms;
-
 static void
 final_stats(struct rusage *rus)
 {
 static void
 final_stats(struct rusage *rus)
 {
@@ -103,13 +96,12 @@ final_stats(struct rusage *rus)
   timeradd(&rus->ru_utime, &rus->ru_stime, &total);
   total_ms = total.tv_sec*1000 + total.tv_usec/1000;
   gettimeofday(&now, NULL);
   timeradd(&rus->ru_utime, &rus->ru_stime, &total);
   total_ms = total.tv_sec*1000 + total.tv_usec/1000;
   gettimeofday(&now, NULL);
-  // FIXME: We are not guaranteed to have start_time always initialized
   timersub(&now, &start_time, &wall);
   wall_ms = wall.tv_sec*1000 + wall.tv_usec/1000;
 
   timersub(&now, &start_time, &wall);
   wall_ms = wall.tv_sec*1000 + wall.tv_usec/1000;
 
-  meta_printf("time:%d.%03d\ntime_wall:%d.%03d\n",
-    total_ms/1000, total_ms%1000,
-    wall_ms/1000, wall_ms%1000);
+  meta_printf("time:%d.%03d\n", total_ms/1000, total_ms%1000);
+  meta_printf("time-wall:%d.%03d\n", wall_ms/1000, wall_ms%1000);
+  meta_printf("mem:%llu\n", (unsigned long long) mem_peak_kb * 1024);
 }
 
 /*** Messages and exits ***/
 }
 
 /*** Messages and exits ***/
@@ -119,6 +111,7 @@ box_exit(int rc)
 {
   if (box_pid > 0)
     {
 {
   if (box_pid > 0)
     {
+      sample_mem_peak();
       if (is_ptraced)
        ptrace(PTRACE_KILL, box_pid);
       kill(-box_pid, SIGKILL);
       if (is_ptraced)
        ptrace(PTRACE_KILL, box_pid);
       kill(-box_pid, SIGKILL);
@@ -144,18 +137,42 @@ flush_line(void)
   partial_line = 0;
 }
 
   partial_line = 0;
 }
 
+/* Report an error of the sandbox itself */
 static void NONRET __attribute__((format(printf,1,2)))
 die(char *msg, ...)
 {
   va_list args;
   va_start(args, msg);
   flush_line();
 static void NONRET __attribute__((format(printf,1,2)))
 die(char *msg, ...)
 {
   va_list args;
   va_start(args, msg);
   flush_line();
-  vfprintf(stderr, msg, args);
+  char buf[1024];
+  vsnprintf(buf, sizeof(buf), msg, args);
+  meta_printf("status:XX\nmessage:%s\n", buf);
+  fputs(buf, stderr);
+  fputc('\n', stderr);
+  box_exit(2);
+}
+
+/* Report an error of the program inside the sandbox */
+static void NONRET __attribute__((format(printf,1,2)))
+err(char *msg, ...)
+{
+  va_list args;
+  va_start(args, msg);
+  flush_line();
+  if (msg[0] && msg[1] && msg[2] == ':' && msg[3] == ' ')
+    {
+      meta_printf("status:%c%c\n", msg[0], msg[1]);
+      msg += 4;
+    }
+  char buf[1024];
+  vsnprintf(buf, sizeof(buf), msg, args);
+  meta_printf("message:%s\n", buf);
+  fputs(buf, stderr);
   fputc('\n', stderr);
   box_exit(1);
   fputc('\n', stderr);
   box_exit(1);
-  // FIXME: exit(2) for errors of the box itself
 }
 
 }
 
+/* Write a message, but only if in verbose mode */
 static void __attribute__((format(printf,1,2)))
 msg(char *msg, ...)
 {
 static void __attribute__((format(printf,1,2)))
 msg(char *msg, ...)
 {
@@ -194,7 +211,10 @@ enum action {
   A_NO,                        // Always forbid
   A_YES,               // Always permit
   A_FILENAME,          // Permit if arg1 is a known filename
   A_NO,                        // Always forbid
   A_YES,               // Always permit
   A_FILENAME,          // Permit if arg1 is a known filename
+  A_ACTION_MASK = 15,
+  A_SAMPLE_MEM = 64,   // Sample memory usage before the syscall
   A_LIBERAL = 128,     // Valid only in liberal mode
   A_LIBERAL = 128,     // Valid only in liberal mode
+  // Must fit in a unsigned char
 };
 
 static unsigned char syscall_action[NUM_ACTIONS] = {
 };
 
 static unsigned char syscall_action[NUM_ACTIONS] = {
@@ -216,7 +236,7 @@ static unsigned char syscall_action[NUM_ACTIONS] = {
     S(readlink) = A_FILENAME,
 
     // Syscalls permitted always
     S(readlink) = A_FILENAME,
 
     // Syscalls permitted always
-    S(exit) = A_YES,
+    S(exit) = A_YES | A_SAMPLE_MEM,
     S(read) = A_YES,
     S(write) = A_YES,
     S(close) = A_YES,
     S(read) = A_YES,
     S(write) = A_YES,
     S(close) = A_YES,
@@ -249,13 +269,15 @@ static unsigned char syscall_action[NUM_ACTIONS] = {
     S(fcntl) = A_YES,
     S(fcntl64) = A_YES,
     S(mmap) = A_YES,
     S(fcntl) = A_YES,
     S(fcntl64) = A_YES,
     S(mmap) = A_YES,
+    S(mmap2) = A_YES,
     S(munmap) = A_YES,
     S(ioctl) = A_YES,
     S(uname) = A_YES,
     S(gettid) = A_YES,
     S(set_thread_area) = A_YES,
     S(get_thread_area) = A_YES,
     S(munmap) = A_YES,
     S(ioctl) = A_YES,
     S(uname) = A_YES,
     S(gettid) = A_YES,
     S(set_thread_area) = A_YES,
     S(get_thread_area) = A_YES,
-    S(exit_group) = A_YES,
+    S(set_tid_address) = A_YES,
+    S(exit_group) = A_YES | A_SAMPLE_MEM,
 
     // Syscalls permitted only in liberal mode
     S(time) = A_YES | A_LIBERAL,
 
     // Syscalls permitted only in liberal mode
     S(time) = A_YES | A_LIBERAL,
@@ -294,7 +316,6 @@ static unsigned char syscall_action[NUM_ACTIONS] = {
     S(rt_sigtimedwait) = A_YES | A_LIBERAL,
     S(rt_sigqueueinfo) = A_YES | A_LIBERAL,
     S(rt_sigsuspend) = A_YES | A_LIBERAL,
     S(rt_sigtimedwait) = A_YES | A_LIBERAL,
     S(rt_sigqueueinfo) = A_YES | A_LIBERAL,
     S(rt_sigsuspend) = A_YES | A_LIBERAL,
-    S(mmap2) = A_YES | A_LIBERAL,
     S(_sysctl) = A_YES | A_LIBERAL,
 #undef S
 };
     S(_sysctl) = A_YES | A_LIBERAL,
 #undef S
 };
@@ -568,7 +589,7 @@ valid_filename(unsigned long addr)
   static int mem_fd;
 
   if (!file_access)
   static int mem_fd;
 
   if (!file_access)
-    die("File access forbidden");
+    err("FA: File access forbidden");
   if (file_access >= 9)
     return;
 
   if (file_access >= 9)
     return;
 
@@ -589,14 +610,14 @@ valid_filename(unsigned long addr)
          if (l > remains)
            l = remains;
          if (!l)
          if (l > remains)
            l = remains;
          if (!l)
-           die("Access to file with name too long");
-         if (long_seek(mem_fd, addr, SEEK_SET) < 0)
-           die("long_seek(mem): %m");
+           err("FA: Access to file with name too long");
+         if (lseek64(mem_fd, addr, SEEK_SET) < 0)
+           die("lseek64(mem): %m");
          remains = read(mem_fd, end, l);
          if (remains < 0)
            die("read(mem): %m");
          if (!remains)
          remains = read(mem_fd, end, l);
          if (remains < 0)
            die("read(mem): %m");
          if (!remains)
-           die("Access to file with name out of memory");
+           err("FA: Access to file with name out of memory");
          end += l;
          addr += l;
        }
          end += l;
          addr += l;
        }
@@ -626,31 +647,31 @@ valid_filename(unsigned long addr)
       act = match_path_rule(&default_path_rules[i], namebuf);
 
   if (act != A_YES)
       act = match_path_rule(&default_path_rules[i], namebuf);
 
   if (act != A_YES)
-    die("Forbidden access to file `%s'", namebuf);
+    err("FA: Forbidden access to file `%s'", namebuf);
 }
 
 }
 
+// Check syscall. If invalid, return -1, otherwise return the action mask.
 static int
 valid_syscall(struct user *u)
 {
   unsigned int sys = u->regs.orig_eax;
 static int
 valid_syscall(struct user *u)
 {
   unsigned int sys = u->regs.orig_eax;
-  enum action act = (sys < NUM_ACTIONS) ? syscall_action[sys] : A_DEFAULT;
+  unsigned int act = (sys < NUM_ACTIONS) ? syscall_action[sys] : A_DEFAULT;
 
   if (act & A_LIBERAL)
     {
 
   if (act & A_LIBERAL)
     {
-      if (filter_syscalls == 1)
-        act &= ~A_LIBERAL;
-      else
+      if (filter_syscalls != 1)
         act = A_DEFAULT;
     }
         act = A_DEFAULT;
     }
-  switch (act)
+
+  switch (act & A_ACTION_MASK)
     {
     case A_YES:
     {
     case A_YES:
-      return 1;
+      return act;
     case A_NO:
     case A_NO:
-      return 0;
+      return -1;
     case A_FILENAME:
       valid_filename(u->regs.ebx);
     case A_FILENAME:
       valid_filename(u->regs.ebx);
-      return 1;
+      return act;
     default: ;
     }
 
     default: ;
     }
 
@@ -658,14 +679,20 @@ valid_syscall(struct user *u)
     {
     case __NR_kill:
       if (u->regs.ebx == box_pid)
     {
     case __NR_kill:
       if (u->regs.ebx == box_pid)
-       die("Committed suicide by signal %d", (int)u->regs.ecx);
-      return 0;
+       {
+         meta_printf("exitsig:%d\n", (int)u->regs.ecx);
+         err("SG: Committed suicide by signal %d", (int)u->regs.ecx);
+       }
+      return -1;
     case __NR_tgkill:
       if (u->regs.ebx == box_pid && u->regs.ecx == box_pid)
     case __NR_tgkill:
       if (u->regs.ebx == box_pid && u->regs.ecx == box_pid)
-       die("Committed suicide by signal %d", (int)u->regs.edx);
-      return 0;
+       {
+         meta_printf("exitsig:%d\n", (int)u->regs.edx);
+         err("SG: Committed suicide by signal %d", (int)u->regs.edx);
+       }
+      return -1;
     default:
     default:
-      return 0;
+      return -1;
     }
 }
 
     }
 }
 
@@ -681,7 +708,29 @@ static void
 signal_int(int unused UNUSED)
 {
   /* Interrupts are fatal, so no synchronization requirements. */
 signal_int(int unused UNUSED)
 {
   /* Interrupts are fatal, so no synchronization requirements. */
-  die("Interrupted");
+  meta_printf("exitsig:%d\n", SIGINT);
+  err("SG: Interrupted");
+}
+
+#define PROC_BUF_SIZE 4096
+static void
+read_proc_file(char *buf, char *name, int *fdp)
+{
+  int c;
+
+  if (!*fdp)
+    {
+      sprintf(buf, "/proc/%d/%s", (int) box_pid, name);
+      *fdp = open(buf, O_RDONLY);
+      if (*fdp < 0)
+       die("open(%s): %m", buf);
+    }
+  lseek(*fdp, 0, SEEK_SET);
+  if ((c = read(*fdp, buf, PROC_BUF_SIZE-1)) < 0)
+    die("read on /proc/$pid/%s: %m", name);
+  if (c >= PROC_BUF_SIZE-1)
+    die("/proc/$pid/%s too long", name);
+  buf[c] = 0;
 }
 
 static void
 }
 
 static void
@@ -695,49 +744,82 @@ check_timeout(void)
       timersub(&now, &start_time, &wall);
       wall_ms = wall.tv_sec*1000 + wall.tv_usec/1000;
       if (wall_ms > wall_timeout)
       timersub(&now, &start_time, &wall);
       wall_ms = wall.tv_sec*1000 + wall.tv_usec/1000;
       if (wall_ms > wall_timeout)
-        die("Time limit exceeded (wall clock)");
+        err("TO: Time limit exceeded (wall clock)");
       if (verbose > 1)
         fprintf(stderr, "[wall time check: %d msec]\n", wall_ms);
     }
   if (timeout)
     {
       if (verbose > 1)
         fprintf(stderr, "[wall time check: %d msec]\n", wall_ms);
     }
   if (timeout)
     {
-      char buf[4096], *x;
-      int c, utime, stime, ms;
-      static int proc_status_fd;
-      if (!proc_status_fd)
-       {
-         sprintf(buf, "/proc/%d/stat", (int) box_pid);
-         proc_status_fd = open(buf, O_RDONLY);
-         if (proc_status_fd < 0)
-           die("open(%s): %m", buf);
-       }
-      lseek(proc_status_fd, 0, SEEK_SET);
-      if ((c = read(proc_status_fd, buf, sizeof(buf)-1)) < 0)
-       die("read on /proc/$pid/stat: %m");
-      if (c >= (int) sizeof(buf) - 1)
-       die("/proc/$pid/stat too long");
-      buf[c] = 0;
+      char buf[PROC_BUF_SIZE], *x;
+      int utime, stime, ms;
+      static int proc_stat_fd;
+      read_proc_file(buf, "stat", &proc_stat_fd);
       x = buf;
       while (*x && *x != ' ')
        x++;
       while (*x == ' ')
        x++;
       if (*x++ != '(')
       x = buf;
       while (*x && *x != ' ')
        x++;
       while (*x == ' ')
        x++;
       if (*x++ != '(')
-       die("proc syntax error 1");
+       die("proc stat syntax error 1");
       while (*x && (*x != ')' || x[1] != ' '))
        x++;
       while (*x == ')' || *x == ' ')
        x++;
       if (sscanf(x, "%*c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %d %d", &utime, &stime) != 2)
       while (*x && (*x != ')' || x[1] != ' '))
        x++;
       while (*x == ')' || *x == ' ')
        x++;
       if (sscanf(x, "%*c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %d %d", &utime, &stime) != 2)
-       die("proc syntax error 2");
+       die("proc stat syntax error 2");
       ms = (utime + stime) * 1000 / ticks_per_sec;
       if (verbose > 1)
        fprintf(stderr, "[time check: %d msec]\n", ms);
       if (ms > timeout)
       ms = (utime + stime) * 1000 / ticks_per_sec;
       if (verbose > 1)
        fprintf(stderr, "[time check: %d msec]\n", ms);
       if (ms > timeout)
-       die("Time limit exceeded");
+       err("TO: Time limit exceeded");
     }
 }
 
     }
 }
 
+static void
+sample_mem_peak(void)
+{
+  /*
+   *  We want to find out the peak memory usage of the process, which is
+   *  maintained by the kernel, but unforunately it gets lost when the
+   *  process exits (it is not reported in struct rusage). Therefore we
+   *  have to sample it whenever we suspect that the process is about
+   *  to exit.
+   */
+  char buf[PROC_BUF_SIZE], *x;
+  static int proc_status_fd;
+  read_proc_file(buf, "status", &proc_status_fd);
+
+  x = buf;
+  while (*x)
+    {
+      char *key = x;
+      while (*x && *x != ':' && *x != '\n')
+       x++;
+      if (!*x || *x == '\n')
+       break;
+      *x++ = 0;
+      while (*x == ' ' || *x == '\t')
+       x++;
+
+      char *val = x;
+      while (*x && *x != '\n')
+       x++;
+      if (!*x)
+       break;
+      *x++ = 0;
+
+      if (!strcmp(key, "VmPeak"))
+       {
+         int peak = atoi(val);
+         if (peak > mem_peak_kb)
+           mem_peak_kb = peak;
+       }
+    }
+
+  if (verbose > 1)
+    msg("[mem-peak: %u KB]\n", mem_peak_kb);
+}
+
 static void
 boxkeeper(void)
 {
 static void
 boxkeeper(void)
 {
@@ -781,24 +863,37 @@ boxkeeper(void)
        {
          box_pid = 0;
          final_stats(&rus);
        {
          box_pid = 0;
          final_stats(&rus);
-         // FIXME: If the process has exited before being ptraced, signal an internal error
          if (WEXITSTATUS(stat))
          if (WEXITSTATUS(stat))
-           die("Exited with error status %d", WEXITSTATUS(stat));
+           {
+             if (syscall_count)
+               {
+                 meta_printf("exitcode:%d\n", WEXITSTATUS(stat));
+                 err("RE: Exited with error status %d", WEXITSTATUS(stat));
+               }
+             else
+               {
+                 // Internal error happened inside the child process and it has been already reported.
+                 box_exit(2);
+               }
+           }
          if (timeout && total_ms > timeout)
          if (timeout && total_ms > timeout)
-           die("Time limit exceeded");
+           err("TO: Time limit exceeded");
          if (wall_timeout && wall_ms > wall_timeout)
          if (wall_timeout && wall_ms > wall_timeout)
-           die("Time limit exceeded (wall clock)");
+           err("TO: Time limit exceeded (wall clock)");
          flush_line();
          flush_line();
-         fprintf(stderr, "OK (%d.%03d sec real, %d.%03d sec wall, %d syscalls)\n",
+         fprintf(stderr, "OK (%d.%03d sec real, %d.%03d sec wall, %d MB, %d syscalls)\n",
              total_ms/1000, total_ms%1000,
              wall_ms/1000, wall_ms%1000,
              total_ms/1000, total_ms%1000,
              wall_ms/1000, wall_ms%1000,
+             (mem_peak_kb + 1023) / 1024,
              syscall_count);
          box_exit(0);
        }
       if (WIFSIGNALED(stat))
        {
          box_pid = 0;
              syscall_count);
          box_exit(0);
        }
       if (WIFSIGNALED(stat))
        {
          box_pid = 0;
-         die("Caught fatal signal %d%s", WTERMSIG(stat), (syscall_count ? "" : " during startup"));
+         meta_printf("exitsig:%d\n", WTERMSIG(stat));
+         final_stats(&rus);
+         err("SG: Caught fatal signal %d%s", WTERMSIG(stat), (syscall_count ? "" : " during startup"));
        }
       if (WIFSTOPPED(stat))
        {
        }
       if (WIFSTOPPED(stat))
        {
@@ -809,12 +904,15 @@ boxkeeper(void)
              static int stop_count = -1;
              if (ptrace(PTRACE_GETREGS, box_pid, NULL, &u) < 0)
                die("ptrace(PTRACE_GETREGS): %m");
              static int stop_count = -1;
              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 */
                {
                  char namebuf[32];
              stop_count++;
              if (!stop_count)                  /* Traceme request */
                msg(">> Traceme request caught\n");
              else if (stop_count & 1)          /* Syscall entry */
                {
                  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);
                  if (!exec_seen)
                    {
                  msg(">> Syscall %-12s (%08lx,%08lx,%08lx) ", syscall_name(u.regs.orig_eax, namebuf), u.regs.ebx, u.regs.ecx, u.regs.edx);
                  if (!exec_seen)
                    {
@@ -822,8 +920,12 @@ boxkeeper(void)
                      if (u.regs.orig_eax == __NR_execve)
                        exec_seen = 1;
                    }
                      if (u.regs.orig_eax == __NR_execve)
                        exec_seen = 1;
                    }
-                 else if (valid_syscall(&u))
-                   syscall_count++;
+                 else if ((act = valid_syscall(&u)) >= 0)
+                   {
+                     syscall_count++;
+                     if (act & A_SAMPLE_MEM)
+                       sample_mem_peak();
+                   }
                  else
                    {
                      /*
                  else
                    {
                      /*
@@ -835,7 +937,7 @@ boxkeeper(void)
                      u.regs.orig_eax = 0xffffffff;
                      if (ptrace(PTRACE_SETREGS, box_pid, NULL, &u) < 0)
                        die("ptrace(PTRACE_SETREGS): %m");
                      u.regs.orig_eax = 0xffffffff;
                      if (ptrace(PTRACE_SETREGS, box_pid, NULL, &u) < 0)
                        die("ptrace(PTRACE_SETREGS): %m");
-                     die("Forbidden syscall %s", syscall_name(sys, namebuf));
+                     err("FO: Forbidden syscall %s", syscall_name(sys, namebuf));
                    }
                }
              else                                      /* Syscall return */
                    }
                }
              else                                      /* Syscall return */
@@ -845,10 +947,14 @@ boxkeeper(void)
          else if (sig != SIGSTOP && sig != SIGXCPU && sig != SIGXFSZ)
            {
              msg(">> Signal %d\n", sig);
          else if (sig != SIGSTOP && sig != SIGXCPU && sig != SIGXFSZ)
            {
              msg(">> Signal %d\n", sig);
+             sample_mem_peak();                        /* Signal might be fatal, so update mem-peak */
              ptrace(PTRACE_SYSCALL, box_pid, 0, sig);
            }
          else
              ptrace(PTRACE_SYSCALL, box_pid, 0, sig);
            }
          else
-           die("Received signal %d", sig);
+           {
+             meta_printf("exitsig:%d", sig);
+             err("SG: Received signal %d", sig);
+           }
        }
       else
        die("wait4: unknown status %x, giving up!", stat);
        }
       else
        die("wait4: unknown status %x, giving up!", stat);
@@ -935,7 +1041,7 @@ Options:\n\
 -v\t\tBe verbose (use multiple times for even more verbosity)\n\
 -w <time>\tSet wall clock time limit (seconds, fractions allowed)\n\
 ");
 -v\t\tBe verbose (use multiple times for even more verbosity)\n\
 -w <time>\tSet wall clock time limit (seconds, fractions allowed)\n\
 ");
-  exit(1);
+  exit(2);
 }
 
 int
 }
 
 int