]> mj.ucw.cz Git - eval.git/blobdiff - box/box.c
Box: Allow /proc/self/maps
[eval.git] / box / box.c
index 4a096e49a9a3039f1e44e7471f5915c73490fe8f..71ec68c75cd9767d690c429de13379e1a1b8f360 100644 (file)
--- a/box/box.c
+++ b/box/box.c
@@ -7,12 +7,15 @@
 #define _LARGEFILE64_SOURCE
 #define _GNU_SOURCE
 
+#include "autoconf.h"
+
 #include <errno.h>
 #include <stdio.h>
 #include <fcntl.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
+#include <stdint.h>
 #include <unistd.h>
 #include <getopt.h>
 #include <time.h>
 #include <sys/ptrace.h>
 #include <sys/signal.h>
 #include <sys/sysinfo.h>
-#include <sys/syscall.h>
 #include <sys/resource.h>
+#include <sys/utsname.h>
 #include <linux/ptrace.h>
 
+#if defined(CONFIG_BOX_KERNEL_AMD64) && !defined(CONFIG_BOX_USER_AMD64)
+#include <asm/unistd_32.h>
+#define NATIVE_NR_execve 59            /* 64-bit execve */
+#else
+#include <asm/unistd.h>
+#define NATIVE_NR_execve __NR_execve
+#endif
+
 #define NONRET __attribute__((noreturn))
 #define UNUSED __attribute__((unused))
 #define ARRAY_SIZE(a) (int)(sizeof(a)/sizeof(a[0]))
@@ -236,7 +247,7 @@ static unsigned char syscall_action[NUM_ACTIONS] = {
     S(stat) = A_FILENAME,
     S(lstat) = A_FILENAME,
     S(readlink) = A_FILENAME,
-#ifndef CONFIG_BOX_AMD64
+#ifndef CONFIG_BOX_USER_AMD64
     S(oldstat) = A_FILENAME,
     S(oldlstat) = A_FILENAME,
     S(truncate64) = A_FILENAME,
@@ -281,7 +292,9 @@ static unsigned char syscall_action[NUM_ACTIONS] = {
     S(get_thread_area) = A_YES,
     S(set_tid_address) = A_YES,
     S(exit_group) = A_YES | A_SAMPLE_MEM,
-#ifndef CONFIG_BOX_AMD64
+#ifdef CONFIG_BOX_USER_AMD64
+    S(arch_prctl) = A_YES,
+#else
     S(oldfstat) = A_YES,
     S(ftruncate64) = A_YES,
     S(_llseek) = A_YES,
@@ -317,7 +330,7 @@ static unsigned char syscall_action[NUM_ACTIONS] = {
     S(rt_sigqueueinfo) = A_YES | A_LIBERAL,
     S(rt_sigsuspend) = A_YES | A_LIBERAL,
     S(_sysctl) = A_YES | A_LIBERAL,
-#ifndef CONFIG_BOX_AMD64
+#ifndef CONFIG_BOX_USER_AMD64
     S(sigaction) = A_YES | A_LIBERAL,
     S(sgetmask) = A_YES | A_LIBERAL,
     S(ssetmask) = A_YES | A_LIBERAL,
@@ -412,6 +425,7 @@ static struct path_rule default_path_rules[] = {
   { "/proc/meminfo", A_YES },
   { "/proc/self/stat", A_YES },
   { "/proc/self/exe", A_YES },                 // Needed by FPC 2.0.x runtime
+  { "/proc/self/maps", A_YES },                        // Needed by glibc when it reports arena corruption
 };
 
 static struct path_rule *user_path_rules;
@@ -594,26 +608,177 @@ setup_environment(void)
   return env;
 }
 
+/*** Low-level parsing of syscalls ***/
+
+#ifdef CONFIG_BOX_KERNEL_AMD64
+typedef uint64_t arg_t;
+#else
+typedef uint32_t arg_t;
+#endif
+
+struct syscall_args {
+  arg_t sys;
+  arg_t arg1, arg2, arg3;
+  arg_t result;
+  struct user user;
+};
+
+static int read_user_mem(arg_t addr, char *buf, int len)
+{
+  static int mem_fd;
+
+  if (!mem_fd)
+    {
+      char memname[64];
+      sprintf(memname, "/proc/%d/mem", (int) box_pid);
+      mem_fd = open(memname, O_RDONLY);
+      if (mem_fd < 0)
+       die("open(%s): %m", memname);
+    }
+  if (lseek64(mem_fd, addr, SEEK_SET) < 0)
+    die("lseek64(mem): %m");
+  return read(mem_fd, buf, len);
+}
+
+#ifdef CONFIG_BOX_KERNEL_AMD64
+
+static void
+get_syscall_args(struct syscall_args *a, int is_exit)
+{
+  if (ptrace(PTRACE_GETREGS, box_pid, NULL, &a->user) < 0)
+    die("ptrace(PTRACE_GETREGS): %m");
+  a->sys = a->user.regs.orig_rax;
+  a->result = a->user.regs.rax;
+
+  /*
+   *  CAVEAT: We have to check carefully that this is a real 64-bit syscall.
+   *  We test whether the process runs in 64-bit mode, but surprisingly this
+   *  is not enough: a 64-bit process can still issue the INT 0x80 instruction
+   *  which performs a 32-bit syscall. Currently, the only known way how to
+   *  detect this situation is to inspect the instruction code (the kernel
+   *  keeps a syscall type flag internally, but it is not accessible from
+   *  user space). Hopefully, there is no instruction whose suffix is the
+   *  code of the SYSCALL instruction. Sometimes, one would wish the
+   *  instruction codes to be unique even when read backwards :)
+   */
+
+  if (is_exit)
+    return;
+
+  int sys_type;
+  uint16_t instr;
+
+  switch (a->user.regs.cs)
+    {
+    case 0x23:
+      // 32-bit CPU mode => only 32-bit syscalls can be issued
+      sys_type = 32;
+      break;
+    case 0x33:
+      // 64-bit CPU mode
+      if (read_user_mem(a->user.regs.rip-2, (char *) &instr, 2) != 2)
+       err("FO: Cannot read syscall instruction");
+      switch (instr)
+       {
+       case 0x050f:
+         break;
+       case 0x80cd:
+         err("FO: Forbidden 32-bit syscall in 64-bit mode");
+       default:
+         err("XX: Unknown syscall instruction %04x", instr);
+       }
+      sys_type = 64;
+      break;
+    default:
+      err("XX: Unknown code segment %04jx", (intmax_t) a->user.regs.cs);
+    }
+
+#ifdef CONFIG_BOX_USER_AMD64
+  if (sys_type != 64)
+    err("FO: Forbidden %d-bit mode syscall", sys_type);
+#else
+  if (sys_type != (exec_seen ? 32 : 64))
+    err("FO: Forbidden %d-bit mode syscall", sys_type);
+#endif
+
+  if (sys_type == 32)
+    {
+      a->arg1 = a->user.regs.rbx;
+      a->arg2 = a->user.regs.rcx;
+      a->arg3 = a->user.regs.rdx;
+    }
+  else
+    {
+      a->arg1 = a->user.regs.rdi;
+      a->arg2 = a->user.regs.rsi;
+      a->arg3 = a->user.regs.rdx;
+    }
+}
+
+static void
+set_syscall_nr(struct syscall_args *a, arg_t sys)
+{
+  a->sys = sys;
+  a->user.regs.orig_rax = sys;
+  if (ptrace(PTRACE_SETREGS, box_pid, NULL, &a->user) < 0)
+    die("ptrace(PTRACE_SETREGS): %m");
+}
+
+static void
+sanity_check(void)
+{
+}
+
+#else
+
+static void
+get_syscall_args(struct syscall_args *a, int is_exit UNUSED)
+{
+  if (ptrace(PTRACE_GETREGS, box_pid, NULL, &a->user) < 0)
+    die("ptrace(PTRACE_GETREGS): %m");
+  a->sys = a->user.regs.orig_eax;
+  a->arg1 = a->user.regs.ebx;
+  a->arg2 = a->user.regs.ecx;
+  a->arg3 = a->user.regs.edx;
+  a->result = a->user.regs.eax;
+}
+
+static void
+set_syscall_nr(struct syscall_args *a, arg_t sys)
+{
+  a->sys = sys;
+  a->user.regs.orig_eax = sys;
+  if (ptrace(PTRACE_SETREGS, box_pid, NULL, &a->user) < 0)
+    die("ptrace(PTRACE_SETREGS): %m");
+}
+
+static void
+sanity_check(void)
+{
+#if !defined(CONFIG_BOX_ALLOW_INSECURE)
+  struct utsname uts;
+  if (uname(&uts) < 0)
+    die("uname() failed: %m");
+
+  if (!strcmp(uts.machine, "x86_64"))
+    die("Running 32-bit sandbox on 64-bit kernels is inherently unsafe. Please get a 64-bit version.");
+#endif
+}
+
+#endif
+
 /*** Syscall checks ***/
 
 static void
-valid_filename(unsigned long addr)
+valid_filename(arg_t addr)
 {
   char namebuf[4096], *p, *end;
-  static int mem_fd;
 
   if (!file_access)
     err("FA: File access forbidden");
   if (file_access >= 9)
     return;
 
-  if (!mem_fd)
-    {
-      sprintf(namebuf, "/proc/%d/mem", (int) box_pid);
-      mem_fd = open(namebuf, O_RDONLY);
-      if (mem_fd < 0)
-       die("open(%s): %m", namebuf);
-    }
   p = end = namebuf;
   do
     {
@@ -625,15 +790,13 @@ valid_filename(unsigned long addr)
            l = remains;
          if (!l)
            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);
+         remains = read_user_mem(addr, end, l);
          if (remains < 0)
            die("read(mem): %m");
          if (!remains)
            err("FA: Access to file with name out of memory");
-         end += l;
-         addr += l;
+         end += remains;
+         addr += remains;
        }
     }
   while (*p++);
@@ -666,9 +829,9 @@ valid_filename(unsigned long addr)
 
 // Check syscall. If invalid, return -1, otherwise return the action mask.
 static int
-valid_syscall(struct user *u)
+valid_syscall(struct syscall_args *a)
 {
-  unsigned int sys = u->regs.orig_eax;
+  unsigned int sys = a->sys;
   unsigned int act = (sys < NUM_ACTIONS) ? syscall_action[sys] : A_DEFAULT;
 
   if (act & A_LIBERAL)
@@ -684,7 +847,7 @@ valid_syscall(struct user *u)
     case A_NO:
       return -1;
     case A_FILENAME:
-      valid_filename(u->regs.ebx);
+      valid_filename(a->arg1);
       return act;
     default: ;
     }
@@ -692,17 +855,17 @@ valid_syscall(struct user *u)
   switch (sys)
     {
     case __NR_kill:
-      if (u->regs.ebx == box_pid)
+      if (a->arg1 == (arg_t) box_pid)
        {
-         meta_printf("exitsig:%d\n", (int)u->regs.ecx);
-         err("SG: Committed suicide by signal %d", (int)u->regs.ecx);
+         meta_printf("exitsig:%d\n", (int) a->arg2);
+         err("SG: Committed suicide by signal %d", (int) a->arg2);
        }
       return -1;
     case __NR_tgkill:
-      if (u->regs.ebx == box_pid && u->regs.ecx == box_pid)
+      if (a->arg1 == (arg_t) box_pid && a->arg2 == (arg_t) box_pid)
        {
-         meta_printf("exitsig:%d\n", (int)u->regs.edx);
-         err("SG: Committed suicide by signal %d", (int)u->regs.edx);
+         meta_printf("exitsig:%d\n", (int) a->arg3);
+         err("SG: Committed suicide by signal %d", (int) a->arg3);
        }
       return -1;
     default:
@@ -931,23 +1094,24 @@ boxkeeper(void)
            {
              if (verbose > 2)
                msg("[ptrace status %08x] ", stat);
-             struct user u;
-             static unsigned int sys_tick, last_sys, last_act;
-             if (ptrace(PTRACE_GETREGS, box_pid, NULL, &u) < 0)
-               die("ptrace(PTRACE_GETREGS): %m");
-             unsigned int sys = u.regs.orig_eax;
+             struct syscall_args a;
+             static unsigned int sys_tick, last_act;
+             static arg_t last_sys;
              if (++sys_tick & 1)               /* Syscall entry */
                {
                  char namebuf[32];
                  int act;
-                 msg(">> Syscall %-12s (%08lx,%08lx,%08lx) ", syscall_name(sys, namebuf), u.regs.ebx, u.regs.ecx, u.regs.edx);
+
+                 get_syscall_args(&a, 0);
+                 arg_t sys = a.sys;
+                 msg(">> Syscall %-12s (%08jx,%08jx,%08jx) ", syscall_name(sys, namebuf), (intmax_t) a.arg1, (intmax_t) a.arg2, (intmax_t) a.arg3);
                  if (!exec_seen)
                    {
                      msg("[master] ");
-                     if (sys == __NR_execve)
+                     if (sys == NATIVE_NR_execve)
                        exec_seen = 1;
                    }
-                 else if ((act = valid_syscall(&u)) >= 0)
+                 else if ((act = valid_syscall(&a)) >= 0)
                    {
                      last_act = act;
                      syscall_count++;
@@ -961,16 +1125,15 @@ boxkeeper(void)
                       * so we have to change it to something harmless (e.g., an undefined
                       * syscall) and make the program continue.
                       */
-                     u.regs.orig_eax = 0xffffffff;
-                     if (ptrace(PTRACE_SETREGS, box_pid, NULL, &u) < 0)
-                       die("ptrace(PTRACE_SETREGS): %m");
+                     set_syscall_nr(&a, ~(arg_t)0);
                      err("FO: Forbidden syscall %s", syscall_name(sys, namebuf));
                    }
                  last_sys = sys;
                }
              else                                      /* Syscall return */
                {
-                 if (sys == 0xffffffff)
+                 get_syscall_args(&a, 1);
+                 if (a.sys == ~(arg_t)0)
                    {
                      /* Some syscalls (sigreturn et al.) do not return a value */
                      if (!(last_act & A_NO_RETVAL))
@@ -978,13 +1141,13 @@ boxkeeper(void)
                    }
                  else
                    {
-                     if (sys != last_sys)
+                     if (a.sys != last_sys)
                        err("XX: Mismatched syscall entry/exit");
                    }
                  if (last_act & A_NO_RETVAL)
                    msg("= ?\n");
                  else
-                   msg("= %ld\n", u.regs.eax);
+                   msg("= %jd\n", (intmax_t) a.result);
                }
              ptrace(PTRACE_SYSCALL, box_pid, 0, 0);
            }
@@ -1177,6 +1340,7 @@ main(int argc, char **argv)
   if (optind >= argc)
     usage();
 
+  sanity_check();
   uid = geteuid();
   if (setreuid(uid, uid) < 0)
     die("setreuid: %m");