X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=src%2Fbox.c;h=6c120f821f36436ddd478eb5f16fb6ac9d4be094;hb=62f031249f096887b62544f8e4513fd225ac86fd;hp=fe1c22f83a7e81bd068a0a0dcab63666666671c2;hpb=51e23ac98d6eea877f5d1c71663e35151b3f2059;p=moe.git diff --git a/src/box.c b/src/box.c index fe1c22f..6c120f8 100644 --- a/src/box.c +++ b/src/box.c @@ -1,7 +1,7 @@ /* - * A Simple Testing Sandbox + * A Simple Sandbox for MO-Eval * - * (c) 2001--2007 Martin Mares + * (c) 2001--2008 Martin Mares */ #define _LARGEFILE64_SOURCE @@ -27,6 +27,7 @@ #define NONRET __attribute__((noreturn)) #define UNUSED __attribute__((unused)) +#define ARRAY_SIZE(a) (int)(sizeof(a)/sizeof(a[0])) static int filter_syscalls; /* 0=off, 1=liberal, 2=totalitarian */ static int timeout; /* milliseconds */ @@ -35,7 +36,6 @@ static int pass_environ; static int file_access; static int verbose; static int memory_limit; -static int allow_times; static char *redir_stdin, *redir_stdout; static char *set_cwd; @@ -91,21 +91,136 @@ msg(char *msg, ...) va_end(args); } -struct syscall { - const char *name; - int override; -}; +static void * +xmalloc(size_t size) +{ + void *p = malloc(size); + if (!p) + die("Out of memory"); + return p; +} -static struct syscall syscall_tab[] = { +static const char * const syscall_names[] = { #include "syscall-table.h" }; -#define NUM_SYSCALLS (sizeof(syscall_tab)/sizeof(syscall_tab[0])) +#define NUM_SYSCALLS ARRAY_SIZE(syscall_names) +#define NUM_ACTIONS (NUM_SYSCALLS+64) + +enum action { + A_DEFAULT, // Use the default action + A_NO, // Always forbid + A_YES, // Always permit + A_FILENAME, // Permit if arg1 is a known filename + A_LIBERAL = 128, // Valid only in liberal mode +}; + +static unsigned char syscall_action[NUM_ACTIONS] = { +#define S(x) [__NR_##x] + + // Syscalls permitted for specific file names + 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(truncate64) = A_FILENAME, + S(stat64) = A_FILENAME, + S(lstat64) = A_FILENAME, + S(readlink) = A_FILENAME, + + // Syscalls permitted always + S(exit) = A_YES, + 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(geteuid) = A_YES, + S(getegid) = A_YES, + S(dup2) = 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, +#ifdef __NR_pread64 + S(pread64) = A_YES, + S(pwrite64) = A_YES, +#else + 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(uname) = A_YES, + S(gettid) = A_YES, + S(set_thread_area) = A_YES, + S(get_thread_area) = A_YES, + S(exit_group) = A_YES, + + // 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_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, +#undef S +}; static const char * syscall_name(unsigned int id, char *buf) { - if (id < NUM_SYSCALLS && syscall_tab[id].name) - return syscall_tab[id].name; + if (id < NUM_SYSCALLS && syscall_names[id]) + return syscall_names[id]; else { sprintf(buf, "#%d", id); @@ -113,16 +228,116 @@ syscall_name(unsigned int id, char *buf) } } -#if 0 -static struct syscall * +static int syscall_by_name(char *name) { - for (unsigned int i=0; i= NUM_ACTIONS) + return NUM_ACTIONS; + return l; +} + +static int +set_syscall_action(char *a) +{ + char *sep = strchr(a, '='); + enum action act = A_YES; + if (sep) + { + *sep++ = 0; + if (!strcmp(sep, "yes")) + act = A_YES; + else if (!strcmp(sep, "no")) + act = A_NO; + else if (!strcmp(sep, "file")) + act = A_FILENAME; + else + return 0; + } + + int sys = syscall_by_name(a); + if (sys < 0) + die("Unknown syscall `%s'", a); + if (sys >= NUM_ACTIONS) + die("Syscall `%s' out of range", a); + syscall_action[sys] = act; + return 1; +} + +struct path_rule { + char *path; + enum action action; + struct path_rule *next; +}; + +static struct path_rule default_path_rules[] = { + { "/etc/", A_YES }, + { "/lib/", A_YES }, + { "/usr/lib/", A_YES }, + { "/opt/lib/", A_YES }, + { "/usr/share/zoneinfo/", A_YES }, + { "/usr/share/locale/", A_YES }, + { "/dev/null", A_YES }, + { "/dev/zero", A_YES }, + { "/proc/meminfo", A_YES }, + { "/proc/self/stat", A_YES }, + { "/proc/self/exe", A_YES }, // Needed by FPC 2.0.x runtime +}; + +static struct path_rule *user_path_rules; +static struct path_rule **last_path_rule = &user_path_rules; + +static int +set_path_action(char *a) +{ + char *sep = strchr(a, '='); + enum action act = A_YES; + if (sep) + { + *sep++ = 0; + if (!strcmp(sep, "yes")) + act = A_YES; + else if (!strcmp(sep, "no")) + act = A_NO; + else + return 0; + } + + struct path_rule *r = xmalloc(sizeof(*r) + strlen(a)); + r->path = (char *)(r+1); + strcpy(r->path, a); + r->action = act; + r->next = NULL; + *last_path_rule = r; + last_path_rule = &r->next; + return 1; +} + +static enum action +match_path_rule(struct path_rule *r, char *path) +{ + char *rr = r->path; + while (*rr) + if (*rr++ != *path++) + { + if (rr[-1] == '/' && !path[-1]) + break; + return A_DEFAULT; + } + if (rr > r->path && rr[-1] != '/' && *path) + return A_DEFAULT; + return r->action; } -#endif static void valid_filename(unsigned long addr) @@ -169,128 +384,56 @@ valid_filename(unsigned long addr) msg("[%s] ", namebuf); if (file_access >= 3) return; + + // Everything in current directory is permitted if (!strchr(namebuf, '/') && strcmp(namebuf, "..")) return; + + // ".." anywhere in the path is forbidden + enum action act = A_DEFAULT; + if (strstr(namebuf, "..")) + act = A_NO; + + // Scan user rules + for (struct path_rule *r = user_path_rules; r && !act; r=r->next) + act = match_path_rule(r, namebuf); + + // Scan built-in rules if (file_access >= 2) - { - if ((!strncmp(namebuf, "/etc/", 5) || - !strncmp(namebuf, "/lib/", 5) || - !strncmp(namebuf, "/usr/lib/", 9) || - !strncmp(namebuf, "/opt/lib/", 9)) - && !strstr(namebuf, "..")) - return; - if (!strcmp(namebuf, "/dev/null") || - !strcmp(namebuf, "/dev/zero") || - !strcmp(namebuf, "/proc/meminfo") || - !strcmp(namebuf, "/proc/self/stat") || - !strcmp(namebuf, "/proc/self/exe") || /* Needed by FPC 2.0.x runtime */ - !strncmp(namebuf, "/usr/share/zoneinfo/", 20)) - return; - } - die("Forbidden access to file `%s'", namebuf); + for (int i=0; iregs.orig_eax) + unsigned int sys = u->regs.orig_eax; + enum action act = (sys < NUM_ACTIONS) ? syscall_action[sys] : A_DEFAULT; + + if (act & A_LIBERAL) { - case __NR_open: - case __NR_creat: - case __NR_unlink: - case __NR_oldstat: - case __NR_access: - case __NR_oldlstat: - case __NR_truncate: - case __NR_stat: - case __NR_lstat: - case __NR_truncate64: - case __NR_stat64: - case __NR_lstat64: - case __NR_readlink: - valid_filename(u->regs.ebx); + if (filter_syscalls == 1) + act &= ~A_LIBERAL; + else + act = A_DEFAULT; + } + switch (act) + { + case A_YES: return 1; - case __NR_exit: - case __NR_read: - case __NR_write: - case __NR_close: - case __NR_lseek: - case __NR_getpid: - case __NR_getuid: - case __NR_oldfstat: - case __NR_dup: - case __NR_brk: - case __NR_getgid: - case __NR_geteuid: - case __NR_getegid: - case __NR_dup2: - case __NR_ftruncate: - case __NR_fstat: - case __NR_personality: - case __NR__llseek: - case __NR_readv: - case __NR_writev: - case __NR_getresuid: -#ifdef __NR_pread64 - case __NR_pread64: - case __NR_pwrite64: -#else - case __NR_pread: - case __NR_pwrite: -#endif - case __NR_ftruncate64: - case __NR_fstat64: - case __NR_fcntl: - case __NR_fcntl64: - case __NR_mmap: - case __NR_munmap: - case __NR_ioctl: - case __NR_uname: - case __NR_gettid: - case __NR_set_thread_area: - case __NR_get_thread_area: - case __NR_exit_group: + case A_NO: + return 0; + case A_FILENAME: + valid_filename(u->regs.ebx); return 1; - case __NR_time: - case __NR_alarm: - case __NR_pause: - case __NR_signal: - case __NR_fchmod: - case __NR_sigaction: - case __NR_sgetmask: - case __NR_ssetmask: - case __NR_sigsuspend: - case __NR_sigpending: - case __NR_getrlimit: - case __NR_getrusage: - case __NR_gettimeofday: - case __NR_select: - case __NR_readdir: - case __NR_setitimer: - case __NR_getitimer: - case __NR_sigreturn: - case __NR_mprotect: - case __NR_sigprocmask: - case __NR_getdents: - case __NR_getdents64: - case __NR__newselect: - case __NR_fdatasync: - case __NR_mremap: - case __NR_poll: - case __NR_getcwd: - case __NR_nanosleep: - case __NR_rt_sigreturn: - case __NR_rt_sigaction: - case __NR_rt_sigprocmask: - case __NR_rt_sigpending: - case __NR_rt_sigtimedwait: - case __NR_rt_sigqueueinfo: - case __NR_rt_sigsuspend: - case __NR_mmap2: - case __NR__sysctl: - return (filter_syscalls == 1); - case __NR_times: - return allow_times; + default: ; + } + + switch (sys) + { case __NR_kill: if (u->regs.ebx == box_pid) die("Committed suicide by signal %d", (int)u->regs.ecx); @@ -555,6 +698,10 @@ Options:\n\ -i \tRedirect stdin from \n\ -m \tLimit address space to KB\n\ -o \tRedirect stdout to \n\ +-p \tPermit access to the specified path (or subtree if it ends with a `/')\n\ +-p =\tDefine action for the specified path (=yes/no)\n\ +-s \tPermit the specified syscall (be careful)\n\ +-s =\tDefine action for the specified syscall (=yes/no/file)\n\ -t