/*
- * A Simple Sandbox for MO-Eval
+ * A Simple Sandbox for Moe
*
- * (c) 2001--2008 Martin Mares <mj@ucw.cz>
+ * (c) 2001--2010 Martin Mares <mj@ucw.cz>
*/
#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))
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 stack_limit;
static char *redir_stdin, *redir_stdout, *redir_stderr;
static char *set_cwd;
static int exec_seen;
static int partial_line;
+static int mem_peak_kb;
+static int total_ms, wall_ms;
+
static void die(char *msg, ...) NONRET;
+static void sample_mem_peak(void);
/*** Meta-files ***/
va_end(args);
}
-static int total_ms, wall_ms;
-
static void
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);
- // 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;
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 ***/
{
if (box_pid > 0)
{
+ sample_mem_peak();
if (is_ptraced)
ptrace(PTRACE_KILL, box_pid);
kill(-box_pid, SIGKILL);
kill(box_pid, SIGKILL);
+ meta_printf("killed:1\n");
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)
- fprintf(stderr, "UGH: Lost track of the process\n");
+ fprintf(stderr, "UGH: Lost track of the process (%m)\n");
else
final_stats(&rus);
}
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();
- 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();
- vfprintf(stderr, msg, args);
+ 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);
}
+/* Write a message, but only if in verbose mode */
static void __attribute__((format(printf,1,2)))
msg(char *msg, ...)
{
A_NO, // Always forbid
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
};
static unsigned char syscall_action[NUM_ACTIONS] = {
S(open) = A_FILENAME,
S(creat) = A_FILENAME,
S(unlink) = A_FILENAME,
- S(oldstat) = A_FILENAME,
S(access) = A_FILENAME,
- S(oldlstat) = A_FILENAME,
S(truncate) = A_FILENAME,
S(stat) = A_FILENAME,
S(lstat) = A_FILENAME,
+ S(readlink) = A_FILENAME,
+#ifndef CONFIG_BOX_USER_AMD64
+ S(oldstat) = A_FILENAME,
+ S(oldlstat) = A_FILENAME,
S(truncate64) = A_FILENAME,
S(stat64) = A_FILENAME,
S(lstat64) = A_FILENAME,
- S(readlink) = A_FILENAME,
+#endif
// 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(lseek) = A_YES,
S(getpid) = A_YES,
S(getuid) = A_YES,
- S(oldfstat) = A_YES,
S(dup) = A_YES,
S(brk) = A_YES,
S(getgid) = A_YES,
S(ftruncate) = A_YES,
S(fstat) = A_YES,
S(personality) = A_YES,
- S(_llseek) = A_YES,
S(readv) = A_YES,
S(writev) = A_YES,
S(getresuid) = A_YES,
S(pread) = A_YES,
S(pwrite) = A_YES,
#endif
- S(ftruncate64) = A_YES,
- S(fstat64) = A_YES,
S(fcntl) = A_YES,
- S(fcntl64) = A_YES,
S(mmap) = A_YES,
S(munmap) = A_YES,
S(ioctl) = 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,
+#ifndef CONFIG_BOX_USER_AMD64
+ S(oldfstat) = A_YES,
+ S(ftruncate64) = A_YES,
+ S(_llseek) = A_YES,
+ S(fstat64) = A_YES,
+ S(fcntl64) = A_YES,
+ S(mmap2) = A_YES,
+#endif
// Syscalls permitted only in liberal mode
S(time) = A_YES | A_LIBERAL,
S(alarm) = A_YES | A_LIBERAL,
S(pause) = A_YES | A_LIBERAL,
- S(signal) = A_YES | A_LIBERAL,
S(fchmod) = A_YES | A_LIBERAL,
- S(sigaction) = A_YES | A_LIBERAL,
- S(sgetmask) = A_YES | A_LIBERAL,
- S(ssetmask) = A_YES | A_LIBERAL,
- S(sigsuspend) = A_YES | A_LIBERAL,
- S(sigpending) = A_YES | A_LIBERAL,
S(getrlimit) = A_YES | A_LIBERAL,
S(getrusage) = A_YES | A_LIBERAL,
- S(ugetrlimit) = A_YES | A_LIBERAL,
S(gettimeofday) = A_YES | A_LIBERAL,
S(select) = 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(mprotect) = A_YES | A_LIBERAL,
- S(sigprocmask) = A_YES | A_LIBERAL,
S(getdents) = A_YES | A_LIBERAL,
S(getdents64) = A_YES | A_LIBERAL,
- S(_newselect) = A_YES | A_LIBERAL,
S(fdatasync) = A_YES | A_LIBERAL,
S(mremap) = 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_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,
+#ifndef CONFIG_BOX_USER_AMD64
+ S(sigaction) = A_YES | A_LIBERAL,
+ S(sgetmask) = A_YES | A_LIBERAL,
+ S(ssetmask) = A_YES | A_LIBERAL,
+ S(sigsuspend) = A_YES | A_LIBERAL,
+ S(sigpending) = A_YES | A_LIBERAL,
+ S(sigreturn) = A_YES | A_LIBERAL | A_NO_RETVAL,
+ S(sigprocmask) = A_YES | A_LIBERAL,
+ S(ugetrlimit) = A_YES | A_LIBERAL,
+ S(readdir) = A_YES | A_LIBERAL,
+ S(signal) = A_YES | A_LIBERAL,
+ S(_newselect) = A_YES | A_LIBERAL,
+#endif
+
#undef S
};
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("File access forbidden");
+ 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
{
if (l > remains)
l = remains;
if (!l)
- err("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);
+ err("FA: Access to file with name too long");
+ remains = read_user_mem(addr, end, l);
if (remains < 0)
die("read(mem): %m");
if (!remains)
- err("Access to file with name out of memory");
- end += l;
- addr += l;
+ err("FA: Access to file with name out of memory");
+ end += remains;
+ addr += remains;
}
}
while (*p++);
act = match_path_rule(&default_path_rules[i], namebuf);
if (act != A_YES)
- err("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)
+valid_syscall(struct syscall_args *a)
{
- unsigned int sys = u->regs.orig_eax;
- enum action act = (sys < NUM_ACTIONS) ? syscall_action[sys] : A_DEFAULT;
+ unsigned int sys = a->sys;
+ unsigned int act = (sys < NUM_ACTIONS) ? syscall_action[sys] : A_DEFAULT;
if (act & A_LIBERAL)
{
- if (filter_syscalls == 1)
- act &= ~A_LIBERAL;
- else
+ if (filter_syscalls != 1)
act = A_DEFAULT;
}
- switch (act)
+
+ switch (act & A_ACTION_MASK)
{
case A_YES:
- return 1;
+ return act;
case A_NO:
- return 0;
+ return -1;
case A_FILENAME:
- valid_filename(u->regs.ebx);
- return 1;
+ valid_filename(a->arg1);
+ return act;
default: ;
}
switch (sys)
{
case __NR_kill:
- if (u->regs.ebx == box_pid)
- err("Committed suicide by signal %d", (int)u->regs.ecx);
- return 0;
+ if (a->arg1 == (arg_t) box_pid)
+ {
+ 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)
- err("Committed suicide by signal %d", (int)u->regs.edx);
- return 0;
+ if (a->arg1 == (arg_t) box_pid && a->arg2 == (arg_t) box_pid)
+ {
+ meta_printf("exitsig:%d\n", (int) a->arg3);
+ err("SG: Committed suicide by signal %d", (int) a->arg3);
+ }
+ return -1;
default:
- return 0;
+ return -1;
}
}
signal_int(int unused UNUSED)
{
/* Interrupts are fatal, so no synchronization requirements. */
- err("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
timersub(&now, &start_time, &wall);
wall_ms = wall.tv_sec*1000 + wall.tv_usec/1000;
if (wall_ms > wall_timeout)
- err("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)
{
- 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++ != '(')
- 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)
- 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)
- err("Time limit exceeded");
+ if (ms > timeout && ms > extra_timeout)
+ 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)
{
- int syscall_count = 0;
+ int syscall_count = (filter_syscalls ? 0 : 1);
struct sigaction sa;
is_ptraced = 1;
+
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!");
+
if (timeout || wall_timeout)
{
sa.sa_handler = signal_alarm;
sigaction(SIGALRM, &sa, NULL);
alarm(1);
}
+
for(;;)
{
struct rusage rus;
{
box_pid = 0;
final_stats(&rus);
- // FIXME: If the process has exited before being ptraced, signal an internal error
if (WEXITSTATUS(stat))
- err("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)
- err("Time limit exceeded");
+ err("TO: Time limit exceeded");
if (wall_timeout && wall_ms > wall_timeout)
- err("Time limit exceeded (wall clock)");
+ err("TO: Time limit exceeded (wall clock)");
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,
+ (mem_peak_kb + 1023) / 1024,
syscall_count);
box_exit(0);
}
if (WIFSIGNALED(stat))
{
box_pid = 0;
- err("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))
{
int sig = WSTOPSIG(stat);
if (sig == SIGTRAP)
{
- struct user u;
- static int stop_count = -1;
- if (ptrace(PTRACE_GETREGS, box_pid, NULL, &u) < 0)
- die("ptrace(PTRACE_GETREGS): %m");
- stop_count++;
- if (!stop_count) /* Traceme request */
+ if (verbose > 2)
+ msg("[ptrace status %08x] ", stat);
+ static int stop_count;
+ if (!stop_count++) /* Traceme request */
msg(">> Traceme request caught\n");
- else if (stop_count & 1) /* Syscall entry */
+ 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 syscall_args a;
+ static unsigned int sys_tick, last_act;
+ static arg_t last_sys;
+ if (++sys_tick & 1) /* Syscall entry */
{
char namebuf[32];
- msg(">> Syscall %-12s (%08lx,%08lx,%08lx) ", syscall_name(u.regs.orig_eax, namebuf), u.regs.ebx, u.regs.ecx, u.regs.edx);
+ int act;
+
+ 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 (u.regs.orig_eax == __NR_execve)
+ if (sys == NATIVE_NR_execve)
exec_seen = 1;
}
- else if (valid_syscall(&u))
- syscall_count++;
+ else if ((act = valid_syscall(&a)) >= 0)
+ {
+ last_act = act;
+ syscall_count++;
+ if (act & A_SAMPLE_MEM)
+ sample_mem_peak();
+ }
else
{
/*
* 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("Forbidden syscall %s", syscall_name(sys, namebuf));
+ set_syscall_nr(&a, ~(arg_t)0);
+ err("FO: Forbidden syscall %s", syscall_name(sys, namebuf));
}
+ last_sys = sys;
}
else /* Syscall return */
- msg("= %ld\n", u.regs.eax);
+ {
+ 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))
+ err("XX: Syscall does not return, but it should");
+ }
+ else
+ {
+ if (a.sys != last_sys)
+ err("XX: Mismatched syscall entry/exit");
+ }
+ if (last_act & A_NO_RETVAL)
+ msg("= ?\n");
+ else
+ msg("= %jd\n", (intmax_t) a.result);
+ }
ptrace(PTRACE_SYSCALL, box_pid, 0, 0);
}
- else if (sig != SIGSTOP && sig != SIGXCPU && sig != SIGXFSZ)
+ 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);
+ }
+ else if (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
- err("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
dup2(1, 2);
setpgrp();
+
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)
- die("setrlimit: %m");
+ die("setrlimit(RLIMIT_NOFILE): %m");
+
+ char **env = setup_environment();
if (filter_syscalls)
{
if (ptrace(PTRACE_TRACEME) < 0)
die("ptrace(PTRACE_TRACEME): %m");
/* Trick: Make sure that we are stopped until the boxkeeper wakes up. */
- signal(SIGCHLD, SIG_IGN);
- raise(SIGCHLD);
+ raise(SIGSTOP);
}
- execve(args[0], args, setup_environment());
+ execve(args[0], args, env);
die("execve(\"%s\"): %m", args[0]);
}
-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\
-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(1);
+ exit(2);
}
int
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':
case 'f':
filter_syscalls++;
break;
+ case 'k':
+ stack_limit = atol(optarg);
+ break;
case 'i':
redir_stdin = optarg;
break;
case 'w':
wall_timeout = 1000*atof(optarg);
break;
+ case 'x':
+ extra_timeout = 1000*atof(optarg);
+ break;
default:
usage();
}
if (optind >= argc)
usage();
+ sanity_check();
uid = geteuid();
if (setreuid(uid, uid) < 0)
die("setreuid: %m");