+/*** 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
+