2 * A Simple Sandbox for Moe
4 * (c) 2001--2010 Martin Mares <mj@ucw.cz>
7 #define _LARGEFILE64_SOURCE
22 #include <sys/ptrace.h>
23 #include <sys/signal.h>
24 #include <sys/sysinfo.h>
25 #include <sys/syscall.h>
26 #include <sys/resource.h>
27 #include <linux/ptrace.h>
29 #define NONRET __attribute__((noreturn))
30 #define UNUSED __attribute__((unused))
31 #define ARRAY_SIZE(a) (int)(sizeof(a)/sizeof(a[0]))
33 static int filter_syscalls; /* 0=off, 1=liberal, 2=totalitarian */
34 static int timeout; /* milliseconds */
35 static int wall_timeout;
36 static int extra_timeout;
37 static int pass_environ;
38 static int file_access;
40 static int memory_limit;
41 static int stack_limit;
42 static char *redir_stdin, *redir_stdout, *redir_stderr;
46 static int is_ptraced;
47 static volatile int timer_tick;
48 static struct timeval start_time;
49 static int ticks_per_sec;
51 static int partial_line;
53 static int mem_peak_kb;
54 static int total_ms, wall_ms;
56 static void die(char *msg, ...) NONRET;
57 static void sample_mem_peak(void);
61 static FILE *metafile;
64 meta_open(const char *name)
66 if (!strcmp(name, "-"))
71 metafile = fopen(name, "w");
73 die("Failed to open metafile '%s'",name);
79 if (metafile && metafile != stdout)
83 static void __attribute__((format(printf,1,2)))
84 meta_printf(const char *fmt, ...)
91 vfprintf(metafile, fmt, args);
96 final_stats(struct rusage *rus)
98 struct timeval total, now, wall;
99 timeradd(&rus->ru_utime, &rus->ru_stime, &total);
100 total_ms = total.tv_sec*1000 + total.tv_usec/1000;
101 gettimeofday(&now, NULL);
102 timersub(&now, &start_time, &wall);
103 wall_ms = wall.tv_sec*1000 + wall.tv_usec/1000;
105 meta_printf("time:%d.%03d\n", total_ms/1000, total_ms%1000);
106 meta_printf("time-wall:%d.%03d\n", wall_ms/1000, wall_ms%1000);
107 meta_printf("mem:%llu\n", (unsigned long long) mem_peak_kb * 1024);
110 /*** Messages and exits ***/
119 ptrace(PTRACE_KILL, box_pid);
120 kill(-box_pid, SIGKILL);
121 kill(box_pid, SIGKILL);
122 meta_printf("killed:1\n");
127 p = wait4(box_pid, &stat, 0, &rus);
128 while (p < 0 && errno == EINTR);
130 fprintf(stderr, "UGH: Lost track of the process (%m)\n");
146 /* Report an error of the sandbox itself */
147 static void NONRET __attribute__((format(printf,1,2)))
154 vsnprintf(buf, sizeof(buf), msg, args);
155 meta_printf("status:XX\nmessage:%s\n", buf);
161 /* Report an error of the program inside the sandbox */
162 static void NONRET __attribute__((format(printf,1,2)))
168 if (msg[0] && msg[1] && msg[2] == ':' && msg[3] == ' ')
170 meta_printf("status:%c%c\n", msg[0], msg[1]);
174 vsnprintf(buf, sizeof(buf), msg, args);
175 meta_printf("message:%s\n", buf);
181 /* Write a message, but only if in verbose mode */
182 static void __attribute__((format(printf,1,2)))
189 int len = strlen(msg);
191 partial_line = (msg[len-1] != '\n');
192 vfprintf(stderr, msg, args);
201 void *p = malloc(size);
203 die("Out of memory");
207 /*** Syscall rules ***/
209 static const char * const syscall_names[] = {
210 #include "box/syscall-table.h"
212 #define NUM_SYSCALLS ARRAY_SIZE(syscall_names)
213 #define NUM_ACTIONS (NUM_SYSCALLS+64)
216 A_DEFAULT, // Use the default action
217 A_NO, // Always forbid
218 A_YES, // Always permit
219 A_FILENAME, // Permit if arg1 is a known filename
221 A_NO_RETVAL = 32, // Does not return a value
222 A_SAMPLE_MEM = 64, // Sample memory usage before the syscall
223 A_LIBERAL = 128, // Valid only in liberal mode
224 // Must fit in a unsigned char
227 static unsigned char syscall_action[NUM_ACTIONS] = {
228 #define S(x) [__NR_##x]
230 // Syscalls permitted for specific file names
231 S(open) = A_FILENAME,
232 S(creat) = A_FILENAME,
233 S(unlink) = A_FILENAME,
234 S(access) = A_FILENAME,
235 S(truncate) = A_FILENAME,
236 S(stat) = A_FILENAME,
237 S(lstat) = A_FILENAME,
238 S(readlink) = A_FILENAME,
239 #ifndef CONFIG_BOX_AMD64
240 S(oldstat) = A_FILENAME,
241 S(oldlstat) = A_FILENAME,
242 S(truncate64) = A_FILENAME,
243 S(stat64) = A_FILENAME,
244 S(lstat64) = A_FILENAME,
247 // Syscalls permitted always
248 S(exit) = A_YES | A_SAMPLE_MEM,
261 S(ftruncate) = A_YES,
263 S(personality) = A_YES,
266 S(getresuid) = A_YES,
280 S(set_thread_area) = A_YES,
281 S(get_thread_area) = A_YES,
282 S(set_tid_address) = A_YES,
283 S(exit_group) = A_YES | A_SAMPLE_MEM,
284 #ifndef CONFIG_BOX_AMD64
286 S(ftruncate64) = A_YES,
293 // Syscalls permitted only in liberal mode
294 S(time) = A_YES | A_LIBERAL,
295 S(alarm) = A_YES | A_LIBERAL,
296 S(pause) = A_YES | A_LIBERAL,
297 S(fchmod) = A_YES | A_LIBERAL,
298 S(getrlimit) = A_YES | A_LIBERAL,
299 S(getrusage) = A_YES | A_LIBERAL,
300 S(gettimeofday) = A_YES | A_LIBERAL,
301 S(select) = A_YES | A_LIBERAL,
302 S(setitimer) = A_YES | A_LIBERAL,
303 S(getitimer) = A_YES | A_LIBERAL,
304 S(mprotect) = A_YES | A_LIBERAL,
305 S(getdents) = A_YES | A_LIBERAL,
306 S(getdents64) = A_YES | A_LIBERAL,
307 S(fdatasync) = A_YES | A_LIBERAL,
308 S(mremap) = A_YES | A_LIBERAL,
309 S(poll) = A_YES | A_LIBERAL,
310 S(getcwd) = A_YES | A_LIBERAL,
311 S(nanosleep) = A_YES | A_LIBERAL,
312 S(rt_sigreturn) = A_YES | A_LIBERAL | A_NO_RETVAL,
313 S(rt_sigaction) = A_YES | A_LIBERAL,
314 S(rt_sigprocmask) = A_YES | A_LIBERAL,
315 S(rt_sigpending) = A_YES | A_LIBERAL,
316 S(rt_sigtimedwait) = A_YES | A_LIBERAL,
317 S(rt_sigqueueinfo) = A_YES | A_LIBERAL,
318 S(rt_sigsuspend) = A_YES | A_LIBERAL,
319 S(_sysctl) = A_YES | A_LIBERAL,
320 #ifndef CONFIG_BOX_AMD64
321 S(sigaction) = A_YES | A_LIBERAL,
322 S(sgetmask) = A_YES | A_LIBERAL,
323 S(ssetmask) = A_YES | A_LIBERAL,
324 S(sigsuspend) = A_YES | A_LIBERAL,
325 S(sigpending) = A_YES | A_LIBERAL,
326 S(sigreturn) = A_YES | A_LIBERAL | A_NO_RETVAL,
327 S(sigprocmask) = A_YES | A_LIBERAL,
328 S(ugetrlimit) = A_YES | A_LIBERAL,
329 S(readdir) = A_YES | A_LIBERAL,
330 S(signal) = A_YES | A_LIBERAL,
331 S(_newselect) = A_YES | A_LIBERAL,
338 syscall_name(unsigned int id, char *buf)
340 if (id < NUM_SYSCALLS && syscall_names[id])
341 return syscall_names[id];
344 sprintf(buf, "#%d", id);
350 syscall_by_name(char *name)
352 for (unsigned int i=0; i<NUM_SYSCALLS; i++)
353 if (syscall_names[i] && !strcmp(syscall_names[i], name))
360 unsigned long l = strtoul(name, &ep, 0);
363 if (l >= NUM_ACTIONS)
369 set_syscall_action(char *a)
371 char *sep = strchr(a, '=');
372 enum action act = A_YES;
376 if (!strcmp(sep, "yes"))
378 else if (!strcmp(sep, "no"))
380 else if (!strcmp(sep, "file"))
386 int sys = syscall_by_name(a);
388 die("Unknown syscall `%s'", a);
389 if (sys >= NUM_ACTIONS)
390 die("Syscall `%s' out of range", a);
391 syscall_action[sys] = act;
400 struct path_rule *next;
403 static struct path_rule default_path_rules[] = {
406 { "/usr/lib/", A_YES },
407 { "/opt/lib/", A_YES },
408 { "/usr/share/zoneinfo/", A_YES },
409 { "/usr/share/locale/", A_YES },
410 { "/dev/null", A_YES },
411 { "/dev/zero", A_YES },
412 { "/proc/meminfo", A_YES },
413 { "/proc/self/stat", A_YES },
414 { "/proc/self/exe", A_YES }, // Needed by FPC 2.0.x runtime
417 static struct path_rule *user_path_rules;
418 static struct path_rule **last_path_rule = &user_path_rules;
421 set_path_action(char *a)
423 char *sep = strchr(a, '=');
424 enum action act = A_YES;
428 if (!strcmp(sep, "yes"))
430 else if (!strcmp(sep, "no"))
436 struct path_rule *r = xmalloc(sizeof(*r) + strlen(a) + 1);
437 r->path = (char *)(r+1);
442 last_path_rule = &r->next;
447 match_path_rule(struct path_rule *r, char *path)
451 if (*rr++ != *path++)
453 if (rr[-1] == '/' && !path[-1])
457 if (rr > r->path && rr[-1] != '/' && *path)
462 /*** Environment rules ***/
465 char *var; // Variable to match
466 char *val; // ""=clear, NULL=inherit
468 struct env_rule *next;
471 static struct env_rule *first_env_rule;
472 static struct env_rule **last_env_rule = &first_env_rule;
474 static struct env_rule default_env_rules[] = {
475 { "LIBC_FATAL_STDERR_", "1" }
479 set_env_action(char *a0)
481 struct env_rule *r = xmalloc(sizeof(*r) + strlen(a0) + 1);
482 char *a = (char *)(r+1);
485 char *sep = strchr(a, '=');
497 last_env_rule = &r->next;
503 match_env_var(char *env_entry, struct env_rule *r)
505 if (strncmp(env_entry, r->var, r->var_len))
507 return (env_entry[r->var_len] == '=');
511 apply_env_rule(char **env, int *env_sizep, struct env_rule *r)
513 // First remove the variable if already set
515 while (pos < *env_sizep && !match_env_var(env[pos], r))
517 if (pos < *env_sizep)
520 env[pos] = env[*env_sizep];
521 env[*env_sizep] = NULL;
524 // What is the new value?
530 new = xmalloc(r->var_len + 1 + strlen(r->val) + 1);
531 sprintf(new, "%s=%s", r->var, r->val);
536 while (environ[pos] && !match_env_var(environ[pos], r))
538 if (!(new = environ[pos]))
542 // Add it at the end of the array
543 env[(*env_sizep)++] = new;
544 env[*env_sizep] = NULL;
548 setup_environment(void)
550 // Link built-in rules with user rules
551 for (int i=ARRAY_SIZE(default_env_rules)-1; i >= 0; i--)
553 default_env_rules[i].next = first_env_rule;
554 first_env_rule = &default_env_rules[i];
557 // Scan the original environment
558 char **orig_env = environ;
560 while (orig_env[orig_size])
563 // For each rule, reserve one more slot and calculate length
565 for (struct env_rule *r = first_env_rule; r; r=r->next)
568 r->var_len = strlen(r->var);
571 // Create a new environment
572 char **env = xmalloc((orig_size + num_rules + 1) * sizeof(char *));
576 memcpy(env, environ, orig_size * sizeof(char *));
583 // Apply the rules one by one
584 for (struct env_rule *r = first_env_rule; r; r=r->next)
585 apply_env_rule(env, &size, r);
587 // Return the new env and pass some gossip
590 fprintf(stderr, "Passing environment:\n");
591 for (int i=0; env[i]; i++)
592 fprintf(stderr, "\t%s\n", env[i]);
597 /*** Syscall checks ***/
600 valid_filename(unsigned long addr)
602 char namebuf[4096], *p, *end;
606 err("FA: File access forbidden");
607 if (file_access >= 9)
612 sprintf(namebuf, "/proc/%d/mem", (int) box_pid);
613 mem_fd = open(namebuf, O_RDONLY);
615 die("open(%s): %m", namebuf);
622 int remains = PAGE_SIZE - (addr & (PAGE_SIZE-1));
623 int l = namebuf + sizeof(namebuf) - end;
627 err("FA: Access to file with name too long");
628 if (lseek64(mem_fd, addr, SEEK_SET) < 0)
629 die("lseek64(mem): %m");
630 remains = read(mem_fd, end, l);
632 die("read(mem): %m");
634 err("FA: Access to file with name out of memory");
641 msg("[%s] ", namebuf);
642 if (file_access >= 3)
645 // Everything in current directory is permitted
646 if (!strchr(namebuf, '/') && strcmp(namebuf, ".."))
649 // ".." anywhere in the path is forbidden
650 enum action act = A_DEFAULT;
651 if (strstr(namebuf, ".."))
655 for (struct path_rule *r = user_path_rules; r && !act; r=r->next)
656 act = match_path_rule(r, namebuf);
658 // Scan built-in rules
659 if (file_access >= 2)
660 for (int i=0; i<ARRAY_SIZE(default_path_rules) && !act; i++)
661 act = match_path_rule(&default_path_rules[i], namebuf);
664 err("FA: Forbidden access to file `%s'", namebuf);
667 // Check syscall. If invalid, return -1, otherwise return the action mask.
669 valid_syscall(struct user *u)
671 unsigned int sys = u->regs.orig_eax;
672 unsigned int act = (sys < NUM_ACTIONS) ? syscall_action[sys] : A_DEFAULT;
676 if (filter_syscalls != 1)
680 switch (act & A_ACTION_MASK)
687 valid_filename(u->regs.ebx);
695 if (u->regs.ebx == box_pid)
697 meta_printf("exitsig:%d\n", (int)u->regs.ecx);
698 err("SG: Committed suicide by signal %d", (int)u->regs.ecx);
702 if (u->regs.ebx == box_pid && u->regs.ecx == box_pid)
704 meta_printf("exitsig:%d\n", (int)u->regs.edx);
705 err("SG: Committed suicide by signal %d", (int)u->regs.edx);
714 signal_alarm(int unused UNUSED)
716 /* Time limit checks are synchronous, so we only schedule them there. */
722 signal_int(int unused UNUSED)
724 /* Interrupts are fatal, so no synchronization requirements. */
725 meta_printf("exitsig:%d\n", SIGINT);
726 err("SG: Interrupted");
729 #define PROC_BUF_SIZE 4096
731 read_proc_file(char *buf, char *name, int *fdp)
737 sprintf(buf, "/proc/%d/%s", (int) box_pid, name);
738 *fdp = open(buf, O_RDONLY);
740 die("open(%s): %m", buf);
742 lseek(*fdp, 0, SEEK_SET);
743 if ((c = read(*fdp, buf, PROC_BUF_SIZE-1)) < 0)
744 die("read on /proc/$pid/%s: %m", name);
745 if (c >= PROC_BUF_SIZE-1)
746 die("/proc/$pid/%s too long", name);
755 struct timeval now, wall;
757 gettimeofday(&now, NULL);
758 timersub(&now, &start_time, &wall);
759 wall_ms = wall.tv_sec*1000 + wall.tv_usec/1000;
760 if (wall_ms > wall_timeout)
761 err("TO: Time limit exceeded (wall clock)");
763 fprintf(stderr, "[wall time check: %d msec]\n", wall_ms);
767 char buf[PROC_BUF_SIZE], *x;
768 int utime, stime, ms;
769 static int proc_stat_fd;
770 read_proc_file(buf, "stat", &proc_stat_fd);
772 while (*x && *x != ' ')
777 die("proc stat syntax error 1");
778 while (*x && (*x != ')' || x[1] != ' '))
780 while (*x == ')' || *x == ' ')
782 if (sscanf(x, "%*c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %d %d", &utime, &stime) != 2)
783 die("proc stat syntax error 2");
784 ms = (utime + stime) * 1000 / ticks_per_sec;
786 fprintf(stderr, "[time check: %d msec]\n", ms);
787 if (ms > timeout && ms > extra_timeout)
788 err("TO: Time limit exceeded");
793 sample_mem_peak(void)
796 * We want to find out the peak memory usage of the process, which is
797 * maintained by the kernel, but unforunately it gets lost when the
798 * process exits (it is not reported in struct rusage). Therefore we
799 * have to sample it whenever we suspect that the process is about
802 char buf[PROC_BUF_SIZE], *x;
803 static int proc_status_fd;
804 read_proc_file(buf, "status", &proc_status_fd);
810 while (*x && *x != ':' && *x != '\n')
812 if (!*x || *x == '\n')
815 while (*x == ' ' || *x == '\t')
819 while (*x && *x != '\n')
825 if (!strcmp(key, "VmPeak"))
827 int peak = atoi(val);
828 if (peak > mem_peak_kb)
834 msg("[mem-peak: %u KB]\n", mem_peak_kb);
840 int syscall_count = (filter_syscalls ? 0 : 1);
845 bzero(&sa, sizeof(sa));
846 sa.sa_handler = signal_int;
847 sigaction(SIGINT, &sa, NULL);
849 gettimeofday(&start_time, NULL);
850 ticks_per_sec = sysconf(_SC_CLK_TCK);
851 if (ticks_per_sec <= 0)
852 die("Invalid ticks_per_sec!");
854 if (timeout || wall_timeout)
856 sa.sa_handler = signal_alarm;
857 sigaction(SIGALRM, &sa, NULL);
871 p = wait4(box_pid, &stat, WUNTRACED, &rus);
879 die("wait4: unknown pid %d exited!", p);
884 if (WEXITSTATUS(stat))
888 meta_printf("exitcode:%d\n", WEXITSTATUS(stat));
889 err("RE: Exited with error status %d", WEXITSTATUS(stat));
893 // Internal error happened inside the child process and it has been already reported.
897 if (timeout && total_ms > timeout)
898 err("TO: Time limit exceeded");
899 if (wall_timeout && wall_ms > wall_timeout)
900 err("TO: Time limit exceeded (wall clock)");
902 fprintf(stderr, "OK (%d.%03d sec real, %d.%03d sec wall, %d MB, %d syscalls)\n",
903 total_ms/1000, total_ms%1000,
904 wall_ms/1000, wall_ms%1000,
905 (mem_peak_kb + 1023) / 1024,
909 if (WIFSIGNALED(stat))
912 meta_printf("exitsig:%d\n", WTERMSIG(stat));
914 err("SG: Caught fatal signal %d%s", WTERMSIG(stat), (syscall_count ? "" : " during startup"));
916 if (WIFSTOPPED(stat))
918 int sig = WSTOPSIG(stat);
922 msg("[ptrace status %08x] ", stat);
923 static int stop_count;
924 if (!stop_count++) /* Traceme request */
925 msg(">> Traceme request caught\n");
927 err("SG: Breakpoint");
928 ptrace(PTRACE_SYSCALL, box_pid, 0, 0);
930 else if (sig == (SIGTRAP | 0x80))
933 msg("[ptrace status %08x] ", stat);
935 static unsigned int sys_tick, last_sys, last_act;
936 if (ptrace(PTRACE_GETREGS, box_pid, NULL, &u) < 0)
937 die("ptrace(PTRACE_GETREGS): %m");
938 unsigned int sys = u.regs.orig_eax;
939 if (++sys_tick & 1) /* Syscall entry */
943 msg(">> Syscall %-12s (%08lx,%08lx,%08lx) ", syscall_name(sys, namebuf), u.regs.ebx, u.regs.ecx, u.regs.edx);
947 if (sys == __NR_execve)
950 else if ((act = valid_syscall(&u)) >= 0)
954 if (act & A_SAMPLE_MEM)
960 * Unfortunately, PTRACE_KILL kills _after_ the syscall completes,
961 * so we have to change it to something harmless (e.g., an undefined
962 * syscall) and make the program continue.
964 u.regs.orig_eax = 0xffffffff;
965 if (ptrace(PTRACE_SETREGS, box_pid, NULL, &u) < 0)
966 die("ptrace(PTRACE_SETREGS): %m");
967 err("FO: Forbidden syscall %s", syscall_name(sys, namebuf));
971 else /* Syscall return */
973 if (sys == 0xffffffff)
975 /* Some syscalls (sigreturn et al.) do not return a value */
976 if (!(last_act & A_NO_RETVAL))
977 err("XX: Syscall does not return, but it should");
982 err("XX: Mismatched syscall entry/exit");
984 if (last_act & A_NO_RETVAL)
987 msg("= %ld\n", u.regs.eax);
989 ptrace(PTRACE_SYSCALL, box_pid, 0, 0);
991 else if (sig == SIGSTOP)
994 if (ptrace(PTRACE_SETOPTIONS, box_pid, NULL, (void *) PTRACE_O_TRACESYSGOOD) < 0)
995 die("ptrace(PTRACE_SETOPTIONS): %m");
996 ptrace(PTRACE_SYSCALL, box_pid, 0, 0);
998 else if (sig != SIGXCPU && sig != SIGXFSZ)
1000 msg(">> Signal %d\n", sig);
1001 sample_mem_peak(); /* Signal might be fatal, so update mem-peak */
1002 ptrace(PTRACE_SYSCALL, box_pid, 0, sig);
1006 meta_printf("exitsig:%d", sig);
1007 err("SG: Received signal %d", sig);
1011 die("wait4: unknown status %x, giving up!", stat);
1016 box_inside(int argc, char **argv)
1021 memcpy(args, argv, argc * sizeof(char *));
1023 if (set_cwd && chdir(set_cwd))
1028 if (open(redir_stdin, O_RDONLY) != 0)
1029 die("open(\"%s\"): %m", redir_stdin);
1034 if (open(redir_stdout, O_WRONLY | O_CREAT | O_TRUNC, 0666) != 1)
1035 die("open(\"%s\"): %m", redir_stdout);
1040 if (open(redir_stderr, O_WRONLY | O_CREAT | O_TRUNC, 0666) != 2)
1041 die("open(\"%s\"): %m", redir_stderr);
1049 rl.rlim_cur = rl.rlim_max = memory_limit * 1024;
1050 if (setrlimit(RLIMIT_AS, &rl) < 0)
1051 die("setrlimit(RLIMIT_AS): %m");
1054 rl.rlim_cur = rl.rlim_max = (stack_limit ? (rlim_t)stack_limit * 1024 : RLIM_INFINITY);
1055 if (setrlimit(RLIMIT_STACK, &rl) < 0)
1056 die("setrlimit(RLIMIT_STACK): %m");
1058 rl.rlim_cur = rl.rlim_max = 64;
1059 if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
1060 die("setrlimit(RLIMIT_NOFILE): %m");
1062 char **env = setup_environment();
1063 if (filter_syscalls)
1065 if (ptrace(PTRACE_TRACEME) < 0)
1066 die("ptrace(PTRACE_TRACEME): %m");
1067 /* Trick: Make sure that we are stopped until the boxkeeper wakes up. */
1070 execve(args[0], args, env);
1071 die("execve(\"%s\"): %m", args[0]);
1077 fprintf(stderr, "Invalid arguments!\n");
1079 Usage: box [<options>] -- <command> <arguments>\n\
1082 -a <level>\tSet file access level (0=none, 1=cwd, 2=/etc,/lib,..., 3=whole fs, 9=no checks; needs -f)\n\
1083 -c <dir>\tChange directory to <dir> first\n\
1084 -e\t\tInherit full environment of the parent process\n\
1085 -E <var>\tInherit the environment variable <var> from the parent process\n\
1086 -E <var>=<val>\tSet the environment variable <var> to <val>; unset it if <var> is empty\n\
1087 -f\t\tFilter system calls (-ff=very restricted)\n\
1088 -i <file>\tRedirect stdin from <file>\n\
1089 -k <size>\tLimit stack size to <size> KB (default: 0=unlimited)\n\
1090 -m <size>\tLimit address space to <size> KB\n\
1091 -M <file>\tOutput process information to <file> (name:value)\n\
1092 -o <file>\tRedirect stdout to <file>\n\
1093 -p <path>\tPermit access to the specified path (or subtree if it ends with a `/')\n\
1094 -p <path>=<act>\tDefine action for the specified path (<act>=yes/no)\n\
1095 -r <file>\tRedirect stderr to <file>\n\
1096 -s <sys>\tPermit the specified syscall (be careful)\n\
1097 -s <sys>=<act>\tDefine action for the specified syscall (<act>=yes/no/file)\n\
1098 -t <time>\tSet run time limit (seconds, fractions allowed)\n\
1099 -T\t\tAllow syscalls for measuring run time\n\
1100 -v\t\tBe verbose (use multiple times for even more verbosity)\n\
1101 -w <time>\tSet wall clock time limit (seconds, fractions allowed)\n\
1102 -x <time>\tSet extra timeout, before which a timing-out program is not yet killed,\n\
1103 \t\tso that its real execution time is reported (seconds, fractions allowed)\n\
1109 main(int argc, char **argv)
1114 while ((c = getopt(argc, argv, "a:c:eE:fi:k:m:M:o:p:r:s:t:Tvw:x:")) >= 0)
1118 file_access = atol(optarg);
1127 if (!set_env_action(optarg))
1134 stack_limit = atol(optarg);
1137 redir_stdin = optarg;
1140 memory_limit = atol(optarg);
1146 redir_stdout = optarg;
1149 if (!set_path_action(optarg))
1153 redir_stderr = optarg;
1156 if (!set_syscall_action(optarg))
1160 timeout = 1000*atof(optarg);
1163 syscall_action[__NR_times] = A_YES;
1169 wall_timeout = 1000*atof(optarg);
1172 extra_timeout = 1000*atof(optarg);
1181 if (setreuid(uid, uid) < 0)
1182 die("setreuid: %m");
1187 box_inside(argc-optind, argv+optind);
1190 die("Internal error: fell over edge of the world");