#define _LARGEFILE64_SOURCE
#define _GNU_SOURCE
+#include "autoconf.h"
+
#include <errno.h>
#include <stdio.h>
#include <fcntl.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]))
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,
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,
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,
{ "/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;
/*** Low-level parsing of syscalls ***/
-#ifdef CONFIG_BOX_AMD64
+#ifdef CONFIG_BOX_KERNEL_AMD64
typedef uint64_t arg_t;
#else
typedef uint32_t arg_t;
return read(mem_fd, buf, len);
}
-#ifdef CONFIG_BOX_AMD64
+#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->arg1 = a->user.regs.rdi;
- a->arg2 = a->user.regs.rsi;
- a->arg3 = a->user.regs.rdx;
a->result = a->user.regs.rax;
/*
if (is_exit)
return;
+ int sys_type;
+ uint16_t instr;
+
switch (a->user.regs.cs)
{
case 0x23:
- err("FO: Forbidden 32-bit mode syscall");
+ // 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);
}
- uint16_t instr;
- if (read_user_mem(a->user.regs.rip-2, (char *) &instr, 2) != 2)
- err("FO: Cannot read syscall instruction");
- switch (instr)
+#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)
{
- case 0x050f:
- break;
- case 0x80cd:
- err("FO: Forbidden 32-bit syscall in 64-bit mode");
- default:
- err("FO: Unknown syscall instruction %04x", instr);
+ 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;
}
}
die("ptrace(PTRACE_SETREGS): %m");
}
+static void
+sanity_check(void)
+{
+}
+
#else
static void
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 ***/
if (!exec_seen)
{
msg("[master] ");
- if (sys == __NR_execve)
+ if (sys == NATIVE_NR_execve)
exec_seen = 1;
}
else if ((act = valid_syscall(&a)) >= 0)
if (optind >= argc)
usage();
+ sanity_check();
uid = geteuid();
if (setreuid(uid, uid) < 0)
die("setreuid: %m");