]> mj.ucw.cz Git - eval.git/blob - src/box.c
No longer force gcc version.
[eval.git] / src / box.c
1 /*
2  *      A Simple Sandbox for MO-Eval
3  *
4  *      (c) 2001--2008 Martin Mares <mj@ucw.cz>
5  */
6
7 #define _LARGEFILE64_SOURCE
8 #define _GNU_SOURCE
9
10 #include <errno.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdarg.h>
16 #include <unistd.h>
17 #include <getopt.h>
18 #include <time.h>
19 #include <sys/wait.h>
20 #include <sys/user.h>
21 #include <sys/time.h>
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
28 #define NONRET __attribute__((noreturn))
29 #define UNUSED __attribute__((unused))
30 #define ARRAY_SIZE(a) (int)(sizeof(a)/sizeof(a[0]))
31
32 static int filter_syscalls;             /* 0=off, 1=liberal, 2=totalitarian */
33 static int timeout;                     /* milliseconds */
34 static int wall_timeout;
35 static int pass_environ;
36 static int file_access;
37 static int verbose;
38 static int memory_limit;
39 static char *redir_stdin, *redir_stdout;
40 static char *set_cwd;
41
42 static pid_t box_pid;
43 static int is_ptraced;
44 static volatile int timer_tick;
45 static struct timeval start_time;
46 static int ticks_per_sec;
47 static int exec_seen;
48
49 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0
50 /* glibc 2.1 or newer -> has lseek64 */
51 #define long_seek(f,o,w) lseek64(f,o,w)
52 #else
53 /* Touching clandestine places in glibc */
54 extern loff_t llseek(int fd, loff_t pos, int whence);
55 #define long_seek(f,o,w) llseek(f,o,w)
56 #endif
57
58 static void NONRET
59 box_exit(void)
60 {
61   if (box_pid > 0)
62     {
63       if (is_ptraced)
64         ptrace(PTRACE_KILL, box_pid);
65       kill(-box_pid, SIGKILL);
66       kill(box_pid, SIGKILL);
67     }
68   exit(1);
69 }
70
71 static void NONRET __attribute__((format(printf,1,2)))
72 die(char *msg, ...)
73 {
74   va_list args;
75   va_start(args, msg);
76   vfprintf(stderr, msg, args);
77   fputc('\n', stderr);
78   box_exit();
79 }
80
81 static void __attribute__((format(printf,1,2)))
82 msg(char *msg, ...)
83 {
84   va_list args;
85   va_start(args, msg);
86   if (verbose)
87     {
88       vfprintf(stderr, msg, args);
89       fflush(stderr);
90     }
91   va_end(args);
92 }
93
94 static void *
95 xmalloc(size_t size)
96 {
97   void *p = malloc(size);
98   if (!p)
99     die("Out of memory");
100   return p;
101 }
102
103 static const char * const syscall_names[] = {
104 #include "syscall-table.h"
105 };
106 #define NUM_SYSCALLS ARRAY_SIZE(syscall_names)
107 #define NUM_ACTIONS (NUM_SYSCALLS+64)
108
109 enum action {
110   A_DEFAULT,            // Use the default action
111   A_NO,         // Always forbid
112   A_YES,                // Always permit
113   A_FILENAME,           // Permit if arg1 is a known filename
114   A_LIBERAL = 128,      // Valid only in liberal mode
115 };
116
117 static unsigned char syscall_action[NUM_ACTIONS] = {
118 #define S(x) [__NR_##x]
119
120     // Syscalls permitted for specific file names
121     S(open) = A_FILENAME,
122     S(creat) = A_FILENAME,
123     S(unlink) = A_FILENAME,
124     S(oldstat) = A_FILENAME,
125     S(access) = A_FILENAME,                     
126     S(oldlstat) = A_FILENAME,                   
127     S(truncate) = A_FILENAME,
128     S(stat) = A_FILENAME,
129     S(lstat) = A_FILENAME,
130     S(truncate64) = A_FILENAME,
131     S(stat64) = A_FILENAME,
132     S(lstat64) = A_FILENAME,
133     S(readlink) = A_FILENAME,
134
135     // Syscalls permitted always
136     S(exit) = A_YES,
137     S(read) = A_YES,
138     S(write) = A_YES,
139     S(close) = A_YES,
140     S(lseek) = A_YES,
141     S(getpid) = A_YES,
142     S(getuid) = A_YES,
143     S(oldfstat) = A_YES,
144     S(dup) = A_YES,
145     S(brk) = A_YES,
146     S(getgid) = A_YES,
147     S(geteuid) = A_YES,
148     S(getegid) = A_YES,
149     S(dup2) = A_YES,
150     S(ftruncate) = A_YES,
151     S(fstat) = A_YES,
152     S(personality) = A_YES,
153     S(_llseek) = A_YES,
154     S(readv) = A_YES,
155     S(writev) = A_YES,
156     S(getresuid) = A_YES,
157 #ifdef __NR_pread64
158     S(pread64) = A_YES,
159     S(pwrite64) = A_YES,
160 #else
161     S(pread) = A_YES,
162     S(pwrite) = A_YES,
163 #endif
164     S(ftruncate64) = A_YES,
165     S(fstat64) = A_YES,
166     S(fcntl) = A_YES,
167     S(fcntl64) = A_YES,
168     S(mmap) = A_YES,
169     S(munmap) = A_YES,
170     S(ioctl) = A_YES,
171     S(uname) = A_YES,
172     S(gettid) = A_YES,
173     S(set_thread_area) = A_YES,
174     S(get_thread_area) = A_YES,
175     S(exit_group) = A_YES,
176
177     // Syscalls permitted only in liberal mode
178     S(time) = A_YES | A_LIBERAL,
179     S(alarm) = A_YES | A_LIBERAL,
180     S(pause) = A_YES | A_LIBERAL,
181     S(signal) = A_YES | A_LIBERAL,
182     S(fchmod) = A_YES | A_LIBERAL,
183     S(sigaction) = A_YES | A_LIBERAL,
184     S(sgetmask) = A_YES | A_LIBERAL,
185     S(ssetmask) = A_YES | A_LIBERAL,
186     S(sigsuspend) = A_YES | A_LIBERAL,
187     S(sigpending) = A_YES | A_LIBERAL,
188     S(getrlimit) = A_YES | A_LIBERAL,
189     S(getrusage) = A_YES | A_LIBERAL,
190     S(ugetrlimit) = A_YES | A_LIBERAL,
191     S(gettimeofday) = A_YES | A_LIBERAL,
192     S(select) = A_YES | A_LIBERAL,
193     S(readdir) = A_YES | A_LIBERAL,
194     S(setitimer) = A_YES | A_LIBERAL,
195     S(getitimer) = A_YES | A_LIBERAL,
196     S(sigreturn) = A_YES | A_LIBERAL,
197     S(mprotect) = A_YES | A_LIBERAL,
198     S(sigprocmask) = A_YES | A_LIBERAL,
199     S(getdents) = A_YES | A_LIBERAL,
200     S(getdents64) = A_YES | A_LIBERAL,
201     S(_newselect) = A_YES | A_LIBERAL,
202     S(fdatasync) = A_YES | A_LIBERAL,
203     S(mremap) = A_YES | A_LIBERAL,
204     S(poll) = A_YES | A_LIBERAL,
205     S(getcwd) = A_YES | A_LIBERAL,
206     S(nanosleep) = A_YES | A_LIBERAL,
207     S(rt_sigreturn) = A_YES | A_LIBERAL,
208     S(rt_sigaction) = A_YES | A_LIBERAL,
209     S(rt_sigprocmask) = A_YES | A_LIBERAL,
210     S(rt_sigpending) = A_YES | A_LIBERAL,
211     S(rt_sigtimedwait) = A_YES | A_LIBERAL,
212     S(rt_sigqueueinfo) = A_YES | A_LIBERAL,
213     S(rt_sigsuspend) = A_YES | A_LIBERAL,
214     S(mmap2) = A_YES | A_LIBERAL,
215     S(_sysctl) = A_YES | A_LIBERAL,
216 #undef S
217 };
218
219 static const char *
220 syscall_name(unsigned int id, char *buf)
221 {
222   if (id < NUM_SYSCALLS && syscall_names[id])
223     return syscall_names[id];
224   else
225     {
226       sprintf(buf, "#%d", id);
227       return buf;
228     }
229 }
230
231 static int
232 syscall_by_name(char *name)
233 {
234   for (unsigned int i=0; i<NUM_SYSCALLS; i++)
235     if (syscall_names[i] && !strcmp(syscall_names[i], name))
236       return i;
237   if (name[0] == '#')
238     name++;
239   if (!*name)
240     return -1;
241   char *ep;
242   unsigned long l = strtoul(name, &ep, 0);
243   if (*ep)
244     return -1;
245   if (l >= NUM_ACTIONS)
246     return NUM_ACTIONS;
247   return l;
248 }
249
250 static int
251 set_syscall_action(char *a)
252 {
253   char *sep = strchr(a, '=');
254   enum action act = A_YES;
255   if (sep)
256     {
257       *sep++ = 0;
258       if (!strcmp(sep, "yes"))
259         act = A_YES;
260       else if (!strcmp(sep, "no"))
261         act = A_NO;
262       else if (!strcmp(sep, "file"))
263         act = A_FILENAME;
264       else
265         return 0;
266     }
267
268   int sys = syscall_by_name(a);
269   if (sys < 0)
270     die("Unknown syscall `%s'", a);
271   if (sys >= NUM_ACTIONS)
272     die("Syscall `%s' out of range", a);
273   syscall_action[sys] = act;
274   return 1;
275 }
276
277 struct path_rule {
278   char *path;
279   enum action action;
280   struct path_rule *next;
281 };
282
283 static struct path_rule default_path_rules[] = {
284   { "/etc/", A_YES },
285   { "/lib/", A_YES },
286   { "/usr/lib/", A_YES },
287   { "/opt/lib/", A_YES },
288   { "/usr/share/zoneinfo/", A_YES },
289   { "/usr/share/locale/", A_YES },
290   { "/dev/null", A_YES },
291   { "/dev/zero", A_YES },
292   { "/proc/meminfo", A_YES },
293   { "/proc/self/stat", A_YES },
294   { "/proc/self/exe", A_YES },                  // Needed by FPC 2.0.x runtime
295 };
296
297 static struct path_rule *user_path_rules;
298 static struct path_rule **last_path_rule = &user_path_rules;
299
300 static int
301 set_path_action(char *a)
302 {
303   char *sep = strchr(a, '=');
304   enum action act = A_YES;
305   if (sep)
306     {
307       *sep++ = 0;
308       if (!strcmp(sep, "yes"))
309         act = A_YES;
310       else if (!strcmp(sep, "no"))
311         act = A_NO;
312       else
313         return 0;
314     }
315
316   struct path_rule *r = xmalloc(sizeof(*r) + strlen(a));
317   r->path = (char *)(r+1);
318   strcpy(r->path, a);
319   r->action = act;
320   r->next = NULL;
321   *last_path_rule = r;
322   last_path_rule = &r->next;
323   return 1;
324 }
325
326 static enum action
327 match_path_rule(struct path_rule *r, char *path)
328 {
329   char *rr = r->path;
330   while (*rr)
331     if (*rr++ != *path++)
332       {
333         if (rr[-1] == '/' && !path[-1])
334           break;
335         return A_DEFAULT;
336       }
337   if (rr > r->path && rr[-1] != '/' && *path)
338     return A_DEFAULT;
339   return r->action;
340 }
341
342 static void
343 valid_filename(unsigned long addr)
344 {
345   char namebuf[4096], *p, *end;
346   static int mem_fd;
347
348   if (!file_access)
349     die("File access forbidden");
350   if (file_access >= 9)
351     return;
352
353   if (!mem_fd)
354     {
355       sprintf(namebuf, "/proc/%d/mem", (int) box_pid);
356       mem_fd = open(namebuf, O_RDONLY);
357       if (mem_fd < 0)
358         die("open(%s): %m", namebuf);
359     }
360   p = end = namebuf;
361   do
362     {
363       if (p >= end)
364         {
365           int remains = PAGE_SIZE - (addr & (PAGE_SIZE-1));
366           int l = namebuf + sizeof(namebuf) - end;
367           if (l > remains)
368             l = remains;
369           if (!l)
370             die("Access to file with name too long");
371           if (long_seek(mem_fd, addr, SEEK_SET) < 0)
372             die("long_seek(mem): %m");
373           remains = read(mem_fd, end, l);
374           if (remains < 0)
375             die("read(mem): %m");
376           if (!remains)
377             die("Access to file with name out of memory");
378           end += l;
379           addr += l;
380         }
381     }
382   while (*p++);
383
384   msg("[%s] ", namebuf);
385   if (file_access >= 3)
386     return;
387
388   // Everything in current directory is permitted
389   if (!strchr(namebuf, '/') && strcmp(namebuf, ".."))
390     return;
391
392   // ".." anywhere in the path is forbidden
393   enum action act = A_DEFAULT;
394   if (strstr(namebuf, ".."))
395     act = A_NO;
396
397   // Scan user rules
398   for (struct path_rule *r = user_path_rules; r && !act; r=r->next)
399     act = match_path_rule(r, namebuf);
400
401   // Scan built-in rules
402   if (file_access >= 2)
403     for (int i=0; i<ARRAY_SIZE(default_path_rules) && !act; i++)
404       act = match_path_rule(&default_path_rules[i], namebuf);
405
406   if (act != A_YES)
407     die("Forbidden access to file `%s'", namebuf);
408 }
409
410 static int
411 valid_syscall(struct user *u)
412 {
413   unsigned int sys = u->regs.orig_eax;
414   enum action act = (sys < NUM_ACTIONS) ? syscall_action[sys] : A_DEFAULT;
415
416   if (act & A_LIBERAL)
417     {
418       if (filter_syscalls == 1)
419         act &= ~A_LIBERAL;
420       else
421         act = A_DEFAULT;
422     }
423   switch (act)
424     {
425     case A_YES:
426       return 1;
427     case A_NO:
428       return 0;
429     case A_FILENAME:
430       valid_filename(u->regs.ebx);
431       return 1;
432     default: ;
433     }
434
435   switch (sys)
436     {
437     case __NR_kill:
438       if (u->regs.ebx == box_pid)
439         die("Committed suicide by signal %d", (int)u->regs.ecx);
440       return 0;
441     case __NR_tgkill:
442       if (u->regs.ebx == box_pid && u->regs.ecx == box_pid)
443         die("Committed suicide by signal %d", (int)u->regs.edx);
444       return 0;
445     default:
446       return 0;
447     }
448 }
449
450 static void
451 signal_alarm(int unused UNUSED)
452 {
453   /* Time limit checks are synchronous, so we only schedule them there. */
454   timer_tick = 1;
455   alarm(1);
456 }
457
458 static void
459 signal_int(int unused UNUSED)
460 {
461   /* Interrupts are fatal, so no synchronization requirements. */
462   die("Interrupted");
463 }
464
465 static void
466 check_timeout(void)
467 {
468   if (wall_timeout)
469     {
470       struct timeval now, wall;
471       int wall_ms;
472       gettimeofday(&now, NULL);
473       timersub(&now, &start_time, &wall);
474       wall_ms = wall.tv_sec*1000 + wall.tv_usec/1000;
475       if (wall_ms > wall_timeout)
476         die("Time limit exceeded (wall clock)");
477       if (verbose > 1)
478         fprintf(stderr, "[wall time check: %d msec]\n", wall_ms);
479     }
480   if (timeout)
481     {
482       char buf[4096], *x;
483       int c, utime, stime, ms;
484       static int proc_status_fd;
485       if (!proc_status_fd)
486         {
487           sprintf(buf, "/proc/%d/stat", (int) box_pid);
488           proc_status_fd = open(buf, O_RDONLY);
489           if (proc_status_fd < 0)
490             die("open(%s): %m", buf);
491         }
492       lseek(proc_status_fd, 0, SEEK_SET);
493       if ((c = read(proc_status_fd, buf, sizeof(buf)-1)) < 0)
494         die("read on /proc/$pid/stat: %m");
495       if (c >= (int) sizeof(buf) - 1)
496         die("/proc/$pid/stat too long");
497       buf[c] = 0;
498       x = buf;
499       while (*x && *x != ' ')
500         x++;
501       while (*x == ' ')
502         x++;
503       if (*x++ != '(')
504         die("proc syntax error 1");
505       while (*x && (*x != ')' || x[1] != ' '))
506         x++;
507       while (*x == ')' || *x == ' ')
508         x++;
509       if (sscanf(x, "%*c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %d %d", &utime, &stime) != 2)
510         die("proc syntax error 2");
511       ms = (utime + stime) * 1000 / ticks_per_sec;
512       if (verbose > 1)
513         fprintf(stderr, "[time check: %d msec]\n", ms);
514       if (ms > timeout)
515         die("Time limit exceeded");
516     }
517 }
518
519 static void
520 boxkeeper(void)
521 {
522   int syscall_count = 0;
523   struct sigaction sa;
524
525   is_ptraced = 1;
526   bzero(&sa, sizeof(sa));
527   sa.sa_handler = signal_int;
528   sigaction(SIGINT, &sa, NULL);
529   gettimeofday(&start_time, NULL);
530   ticks_per_sec = sysconf(_SC_CLK_TCK);
531   if (ticks_per_sec <= 0)
532     die("Invalid ticks_per_sec!");
533   if (timeout || wall_timeout)
534     {
535       sa.sa_handler = signal_alarm;
536       sigaction(SIGALRM, &sa, NULL);
537       alarm(1);
538     }
539   for(;;)
540     {
541       struct rusage rus;
542       int stat;
543       pid_t p;
544       if (timer_tick)
545         {
546           check_timeout();
547           timer_tick = 0;
548         }
549       p = wait4(box_pid, &stat, WUNTRACED, &rus);
550       if (p < 0)
551         {
552           if (errno == EINTR)
553             continue;
554           die("wait4: %m");
555         }
556       if (p != box_pid)
557         die("wait4: unknown pid %d exited!", p);
558       if (WIFEXITED(stat))
559         {
560           struct timeval total, now, wall;
561           int total_ms, wall_ms;
562           box_pid = 0;
563           if (WEXITSTATUS(stat))
564             die("Exited with error status %d", WEXITSTATUS(stat));
565           timeradd(&rus.ru_utime, &rus.ru_stime, &total);
566           total_ms = total.tv_sec*1000 + total.tv_usec/1000;
567           gettimeofday(&now, NULL);
568           timersub(&now, &start_time, &wall);
569           wall_ms = wall.tv_sec*1000 + wall.tv_usec/1000;
570           if (timeout && total_ms > timeout)
571             die("Time limit exceeded");
572           if (wall_timeout && wall_ms > wall_timeout)
573             die("Time limit exceeded (wall clock)");
574           fprintf(stderr, "OK (%d.%03d sec real, %d.%03d sec wall, %d syscalls)\n",
575               (int) total.tv_sec, (int) total.tv_usec/1000,
576               (int) wall.tv_sec, (int) wall.tv_usec/1000,
577               syscall_count);
578           exit(0);
579         }
580       if (WIFSIGNALED(stat))
581         {
582           box_pid = 0;
583           die("Caught fatal signal %d%s", WTERMSIG(stat), (syscall_count ? "" : " during startup"));
584         }
585       if (WIFSTOPPED(stat))
586         {
587           int sig = WSTOPSIG(stat);
588           if (sig == SIGTRAP)
589             {
590               struct user u;
591               static int stop_count = -1;
592               if (ptrace(PTRACE_GETREGS, box_pid, NULL, &u) < 0)
593                 die("ptrace(PTRACE_GETREGS): %m");
594               stop_count++;
595               if (!stop_count)                  /* Traceme request */
596                 msg(">> Traceme request caught\n");
597               else if (stop_count & 1)          /* Syscall entry */
598                 {
599                   char namebuf[32];
600                   msg(">> Syscall %-12s (%08lx,%08lx,%08lx) ", syscall_name(u.regs.orig_eax, namebuf), u.regs.ebx, u.regs.ecx, u.regs.edx);
601                   if (!exec_seen)
602                     {
603                       msg("[master] ");
604                       if (u.regs.orig_eax == __NR_execve)
605                         exec_seen = 1;
606                     }
607                   else if (valid_syscall(&u))
608                     syscall_count++;
609                   else
610                     {
611                       /*
612                        * Unfortunately, PTRACE_KILL kills _after_ the syscall completes,
613                        * so we have to change it to something harmless (e.g., an undefined
614                        * syscall) and make the program continue.
615                        */
616                       unsigned int sys = u.regs.orig_eax;
617                       u.regs.orig_eax = 0xffffffff;
618                       if (ptrace(PTRACE_SETREGS, box_pid, NULL, &u) < 0)
619                         die("ptrace(PTRACE_SETREGS): %m");
620                       die("Forbidden syscall %s", syscall_name(sys, namebuf));
621                     }
622                 }
623               else                                      /* Syscall return */
624                 msg("= %ld\n", u.regs.eax);
625               ptrace(PTRACE_SYSCALL, box_pid, 0, 0);
626             }
627           else if (sig != SIGSTOP && sig != SIGXCPU && sig != SIGXFSZ)
628             {
629               msg(">> Signal %d\n", sig);
630               ptrace(PTRACE_SYSCALL, box_pid, 0, sig);
631             }
632           else
633             die("Received signal %d", sig);
634         }
635       else
636         die("wait4: unknown status %x, giving up!", stat);
637     }
638 }
639
640 static void
641 box_inside(int argc, char **argv)
642 {
643   struct rlimit rl;
644   char *args[argc+1];
645   char *env[] = { "LIBC_FATAL_STDERR_=1", NULL };
646
647   memcpy(args, argv, argc * sizeof(char *));
648   args[argc] = NULL;
649   if (set_cwd && chdir(set_cwd))
650     die("chdir: %m");
651   if (redir_stdin)
652     {
653       close(0);
654       if (open(redir_stdin, O_RDONLY) != 0)
655         die("open(\"%s\"): %m", redir_stdin);
656     }
657   if (redir_stdout)
658     {
659       close(1);
660       if (open(redir_stdout, O_WRONLY | O_CREAT | O_TRUNC, 0666) != 1)
661         die("open(\"%s\"): %m", redir_stdout);
662     }
663   dup2(1, 2);
664   setpgrp();
665   if (memory_limit)
666     {
667       rl.rlim_cur = rl.rlim_max = memory_limit * 1024;
668       if (setrlimit(RLIMIT_AS, &rl) < 0)
669         die("setrlimit: %m");
670     }
671   rl.rlim_cur = rl.rlim_max = 64;
672   if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
673     die("setrlimit: %m");
674   if (filter_syscalls)
675     {
676       if (ptrace(PTRACE_TRACEME) < 0)
677         die("ptrace(PTRACE_TRACEME): %m");
678       /* Trick: Make sure that we are stopped until the boxkeeper wakes up. */
679       signal(SIGCHLD, SIG_IGN);
680       raise(SIGCHLD);
681     }
682   execve(args[0], args, (pass_environ ? environ : env));
683   die("execve(\"%s\"): %m", args[0]);
684 }
685
686 static void
687 usage(void)
688 {
689   fprintf(stderr, "Invalid arguments!\n");
690   printf("\
691 Usage: box [<options>] -- <command> <arguments>\n\
692 \n\
693 Options:\n\
694 -a <level>\tSet file access level (0=none, 1=cwd, 2=/etc,/lib,..., 3=whole fs, 9=no checks; needs -f)\n\
695 -c <dir>\tChange directory to <dir> first\n\
696 -e\t\tPass full environment of parent process\n\
697 -f\t\tFilter system calls (-ff=very restricted)\n\
698 -i <file>\tRedirect stdin from <file>\n\
699 -m <size>\tLimit address space to <size> KB\n\
700 -o <file>\tRedirect stdout to <file>\n\
701 -p <path>\tPermit access to the specified path (or subtree if it ends with a `/')\n\
702 -p <path>=<act>\tDefine action for the specified path (<act>=yes/no)\n\
703 -s <sys>\tPermit the specified syscall (be careful)\n\
704 -s <sys>=<act>\tDefine action for the specified syscall (<act>=yes/no/file)\n\
705 -t <time>\tSet run time limit (seconds, fractions allowed)\n\
706 -T\t\tAllow syscalls for measuring run time\n\
707 -v\t\tBe verbose\n\
708 -w <time>\tSet wall clock time limit (seconds, fractions allowed)\n\
709 ");
710   exit(1);
711 }
712
713 int
714 main(int argc, char **argv)
715 {
716   int c;
717   uid_t uid;
718
719   while ((c = getopt(argc, argv, "a:c:efi:m:o:p:s:t:Tvw:")) >= 0)
720     switch (c)
721       {
722       case 'a':
723         file_access = atol(optarg);
724         break;
725       case 'c':
726         set_cwd = optarg;
727         break;
728       case 'e':
729         pass_environ = 1;
730         break;
731       case 'f':
732         filter_syscalls++;
733         break;
734       case 'i':
735         redir_stdin = optarg;
736         break;
737       case 'm':
738         memory_limit = atol(optarg);
739         break;
740       case 'o':
741         redir_stdout = optarg;
742         break;
743       case 'p':
744         if (!set_path_action(optarg))
745           usage();
746         break;
747       case 's':
748         if (!set_syscall_action(optarg))
749           usage();
750         break;
751       case 't':
752         timeout = 1000*atof(optarg);
753         break;
754       case 'T':
755         syscall_action[__NR_times] = A_YES;
756         break;
757       case 'v':
758         verbose++;
759         break;
760       case 'w':
761         wall_timeout = 1000*atof(optarg);
762         break;
763       default:
764         usage();
765       }
766   if (optind >= argc)
767     usage();
768
769   uid = geteuid();
770   if (setreuid(uid, uid) < 0)
771     die("setreuid: %m");
772   box_pid = fork();
773   if (box_pid < 0)
774     die("fork: %m");
775   if (!box_pid)
776     box_inside(argc-optind, argv+optind);
777   else
778     boxkeeper();
779   die("Internal error: fell over edge of the world");
780 }