]> mj.ucw.cz Git - moe.git/blobdiff - box/box.c
Box: Carefully check that 64-bit syscalls are what they seem to be
[moe.git] / box / box.c
index 323e07e77e9d270dc85d43f0092a346deba41ed1..65734db9b3c7a2ce6d1db0eae071c9c248dd6b7e 100644 (file)
--- a/box/box.c
+++ b/box/box.c
@@ -1,7 +1,7 @@
 /*
 /*
- *     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 _LARGEFILE64_SOURCE
@@ -13,6 +13,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
+#include <stdint.h>
 #include <unistd.h>
 #include <getopt.h>
 #include <time.h>
 #include <unistd.h>
 #include <getopt.h>
 #include <time.h>
@@ -24,6 +25,7 @@
 #include <sys/sysinfo.h>
 #include <sys/syscall.h>
 #include <sys/resource.h>
 #include <sys/sysinfo.h>
 #include <sys/syscall.h>
 #include <sys/resource.h>
+#include <linux/ptrace.h>
 
 #define NONRET __attribute__((noreturn))
 #define UNUSED __attribute__((unused))
 
 #define NONRET __attribute__((noreturn))
 #define UNUSED __attribute__((unused))
@@ -37,6 +39,7 @@ static int pass_environ;
 static int file_access;
 static int verbose;
 static int memory_limit;
 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 char *redir_stdin, *redir_stdout, *redir_stderr;
 static char *set_cwd;
 
@@ -120,10 +123,12 @@ box_exit(int rc)
       meta_printf("killed:1\n");
 
       struct rusage rus;
       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)
       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);
     }
       else
        final_stats(&rus);
     }
@@ -227,16 +232,18 @@ static unsigned char syscall_action[NUM_ACTIONS] = {
     S(open) = A_FILENAME,
     S(creat) = A_FILENAME,
     S(unlink) = A_FILENAME,
     S(open) = A_FILENAME,
     S(creat) = A_FILENAME,
     S(unlink) = A_FILENAME,
-    S(oldstat) = A_FILENAME,
     S(access) = A_FILENAME,                    
     S(access) = A_FILENAME,                    
-    S(oldlstat) = A_FILENAME,                  
     S(truncate) = A_FILENAME,
     S(stat) = A_FILENAME,
     S(lstat) = A_FILENAME,
     S(truncate) = A_FILENAME,
     S(stat) = A_FILENAME,
     S(lstat) = A_FILENAME,
+    S(readlink) = A_FILENAME,
+#ifndef CONFIG_BOX_AMD64
+    S(oldstat) = A_FILENAME,
+    S(oldlstat) = A_FILENAME,
     S(truncate64) = A_FILENAME,
     S(stat64) = A_FILENAME,
     S(lstat64) = 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 | A_SAMPLE_MEM,
 
     // Syscalls permitted always
     S(exit) = A_YES | A_SAMPLE_MEM,
@@ -246,7 +253,6 @@ static unsigned char syscall_action[NUM_ACTIONS] = {
     S(lseek) = A_YES,
     S(getpid) = A_YES,
     S(getuid) = 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(dup) = A_YES,
     S(brk) = A_YES,
     S(getgid) = A_YES,
@@ -256,7 +262,6 @@ static unsigned char syscall_action[NUM_ACTIONS] = {
     S(ftruncate) = A_YES,
     S(fstat) = A_YES,
     S(personality) = 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(readv) = A_YES,
     S(writev) = A_YES,
     S(getresuid) = A_YES,
@@ -267,12 +272,8 @@ static unsigned char syscall_action[NUM_ACTIONS] = {
     S(pread) = A_YES,
     S(pwrite) = A_YES,
 #endif
     S(pread) = A_YES,
     S(pwrite) = A_YES,
 #endif
-    S(ftruncate64) = A_YES,
-    S(fstat64) = A_YES,
     S(fcntl) = A_YES,
     S(fcntl) = A_YES,
-    S(fcntl64) = A_YES,
     S(mmap) = A_YES,
     S(mmap) = A_YES,
-    S(mmap2) = A_YES,
     S(munmap) = A_YES,
     S(ioctl) = A_YES,
     S(uname) = A_YES,
     S(munmap) = A_YES,
     S(ioctl) = A_YES,
     S(uname) = A_YES,
@@ -281,32 +282,29 @@ static unsigned char syscall_action[NUM_ACTIONS] = {
     S(get_thread_area) = A_YES,
     S(set_tid_address) = A_YES,
     S(exit_group) = A_YES | A_SAMPLE_MEM,
     S(get_thread_area) = A_YES,
     S(set_tid_address) = A_YES,
     S(exit_group) = A_YES | A_SAMPLE_MEM,
+#ifndef CONFIG_BOX_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,
 
     // 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(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(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(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(setitimer) = A_YES | A_LIBERAL,
     S(getitimer) = A_YES | A_LIBERAL,
-    S(sigreturn) = A_YES | A_LIBERAL | A_NO_RETVAL,
     S(mprotect) = 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(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(fdatasync) = A_YES | A_LIBERAL,
     S(mremap) = A_YES | A_LIBERAL,
     S(poll) = A_YES | A_LIBERAL,
@@ -320,6 +318,20 @@ static unsigned char syscall_action[NUM_ACTIONS] = {
     S(rt_sigqueueinfo) = A_YES | A_LIBERAL,
     S(rt_sigsuspend) = A_YES | A_LIBERAL,
     S(_sysctl) = A_YES | A_LIBERAL,
     S(rt_sigqueueinfo) = A_YES | A_LIBERAL,
     S(rt_sigsuspend) = A_YES | A_LIBERAL,
     S(_sysctl) = A_YES | A_LIBERAL,
+#ifndef CONFIG_BOX_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
 };
 
 #undef S
 };
 
@@ -583,10 +595,128 @@ setup_environment(void)
   return env;
 }
 
   return env;
 }
 
+/*** Low-level parsing of syscalls ***/
+
+#ifdef CONFIG_BOX_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_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;
+
+  /*
+   *  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;
+
+  switch (a->user.regs.cs)
+    {
+    case 0x23:
+      err("FO: Forbidden 32-bit mode syscall");
+    case 0x33:
+      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)
+    {
+    case 0x050f:
+      break;
+    case 0x80cd:
+      err("FO: Forbidden 32-bit syscall in 64-bit mode");
+    default:
+      err("FO: Unknown syscall instruction %04x", instr);
+    }
+}
+
+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");
+}
+
+#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");
+}
+
+#endif
+
 /*** Syscall checks ***/
 
 static void
 /*** Syscall checks ***/
 
 static void
-valid_filename(unsigned long addr)
+valid_filename(arg_t addr)
 {
   char namebuf[4096], *p, *end;
   static int mem_fd;
 {
   char namebuf[4096], *p, *end;
   static int mem_fd;
@@ -655,9 +785,9 @@ valid_filename(unsigned long addr)
 
 // Check syscall. If invalid, return -1, otherwise return the action mask.
 static int
 
 // 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;
+  unsigned int sys = a->sys;
   unsigned int act = (sys < NUM_ACTIONS) ? syscall_action[sys] : A_DEFAULT;
 
   if (act & A_LIBERAL)
   unsigned int act = (sys < NUM_ACTIONS) ? syscall_action[sys] : A_DEFAULT;
 
   if (act & A_LIBERAL)
@@ -673,7 +803,7 @@ valid_syscall(struct user *u)
     case A_NO:
       return -1;
     case A_FILENAME:
     case A_NO:
       return -1;
     case A_FILENAME:
-      valid_filename(u->regs.ebx);
+      valid_filename(a->arg1);
       return act;
     default: ;
     }
       return act;
     default: ;
     }
@@ -681,17 +811,17 @@ valid_syscall(struct user *u)
   switch (sys)
     {
     case __NR_kill:
   switch (sys)
     {
     case __NR_kill:
-      if (u->regs.ebx == box_pid)
+      if (a->arg1 == (arg_t) box_pid)
        {
        {
-         meta_printf("exitsig:%d\n", (int)u->regs.ecx);
-         err("SG: Committed suicide by signal %d", (int)u->regs.ecx);
+         meta_printf("exitsig:%d\n", (int) a->arg2);
+         err("SG: Committed suicide by signal %d", (int) a->arg2);
        }
       return -1;
     case __NR_tgkill:
        }
       return -1;
     case __NR_tgkill:
-      if (u->regs.ebx == box_pid && u->regs.ecx == box_pid)
+      if (a->arg1 == (arg_t) box_pid && a->arg2 == (arg_t) box_pid)
        {
        {
-         meta_printf("exitsig:%d\n", (int)u->regs.edx);
-         err("SG: Committed suicide by signal %d", (int)u->regs.edx);
+         meta_printf("exitsig:%d\n", (int) a->arg3);
+         err("SG: Committed suicide by signal %d", (int) a->arg3);
        }
       return -1;
     default:
        }
       return -1;
     default:
@@ -826,23 +956,27 @@ sample_mem_peak(void)
 static void
 boxkeeper(void)
 {
 static void
 boxkeeper(void)
 {
-  int syscall_count = 0;
+  int syscall_count = (filter_syscalls ? 0 : 1);
   struct sigaction sa;
 
   is_ptraced = 1;
   struct sigaction sa;
 
   is_ptraced = 1;
+
   bzero(&sa, sizeof(sa));
   sa.sa_handler = signal_int;
   sigaction(SIGINT, &sa, NULL);
   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!");
   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);
     }
   if (timeout || wall_timeout)
     {
       sa.sa_handler = signal_alarm;
       sigaction(SIGALRM, &sa, NULL);
       alarm(1);
     }
+
   for(;;)
     {
       struct rusage rus;
   for(;;)
     {
       struct rusage rus;
@@ -916,23 +1050,24 @@ boxkeeper(void)
            {
              if (verbose > 2)
                msg("[ptrace status %08x] ", stat);
            {
              if (verbose > 2)
                msg("[ptrace status %08x] ", stat);
-             struct user u;
-             static unsigned int sys_tick, last_sys, last_act;
-             if (ptrace(PTRACE_GETREGS, box_pid, NULL, &u) < 0)
-               die("ptrace(PTRACE_GETREGS): %m");
-             unsigned int sys = u.regs.orig_eax;
+             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];
                  int act;
              if (++sys_tick & 1)               /* Syscall entry */
                {
                  char namebuf[32];
                  int act;
-                 msg(">> Syscall %-12s (%08lx,%08lx,%08lx) ", syscall_name(sys, namebuf), u.regs.ebx, u.regs.ecx, u.regs.edx);
+
+                 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 (sys == __NR_execve)
                        exec_seen = 1;
                    }
                  if (!exec_seen)
                    {
                      msg("[master] ");
                      if (sys == __NR_execve)
                        exec_seen = 1;
                    }
-                 else if ((act = valid_syscall(&u)) >= 0)
+                 else if ((act = valid_syscall(&a)) >= 0)
                    {
                      last_act = act;
                      syscall_count++;
                    {
                      last_act = act;
                      syscall_count++;
@@ -946,16 +1081,15 @@ boxkeeper(void)
                       * so we have to change it to something harmless (e.g., an undefined
                       * syscall) and make the program continue.
                       */
                       * so we have to change it to something harmless (e.g., an undefined
                       * syscall) and make the program continue.
                       */
-                     u.regs.orig_eax = 0xffffffff;
-                     if (ptrace(PTRACE_SETREGS, box_pid, NULL, &u) < 0)
-                       die("ptrace(PTRACE_SETREGS): %m");
+                     set_syscall_nr(&a, ~(arg_t)0);
                      err("FO: Forbidden syscall %s", syscall_name(sys, namebuf));
                    }
                  last_sys = sys;
                }
              else                                      /* Syscall return */
                {
                      err("FO: Forbidden syscall %s", syscall_name(sys, namebuf));
                    }
                  last_sys = sys;
                }
              else                                      /* Syscall return */
                {
-                 if (sys == 0xffffffff)
+                 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))
                    {
                      /* Some syscalls (sigreturn et al.) do not return a value */
                      if (!(last_act & A_NO_RETVAL))
@@ -963,13 +1097,13 @@ boxkeeper(void)
                    }
                  else
                    {
                    }
                  else
                    {
-                     if (sys != last_sys)
+                     if (a.sys != last_sys)
                        err("XX: Mismatched syscall entry/exit");
                    }
                  if (last_act & A_NO_RETVAL)
                    msg("= ?\n");
                  else
                        err("XX: Mismatched syscall entry/exit");
                    }
                  if (last_act & A_NO_RETVAL)
                    msg("= ?\n");
                  else
-                   msg("= %ld\n", u.regs.eax);
+                   msg("= %jd\n", (intmax_t) a.result);
                }
              ptrace(PTRACE_SYSCALL, box_pid, 0, 0);
            }
                }
              ptrace(PTRACE_SYSCALL, box_pid, 0, 0);
            }
@@ -1028,15 +1162,22 @@ box_inside(int argc, char **argv)
   else
     dup2(1, 2);
   setpgrp();
   else
     dup2(1, 2);
   setpgrp();
+
   if (memory_limit)
     {
       rl.rlim_cur = rl.rlim_max = memory_limit * 1024;
       if (setrlimit(RLIMIT_AS, &rl) < 0)
   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)
   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)
     {
   char **env = setup_environment();
   if (filter_syscalls)
     {
@@ -1064,6 +1205,7 @@ Options:\n\
 -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\
 -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\
 -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\
@@ -1088,7 +1230,7 @@ main(int argc, char **argv)
   int c;
   uid_t uid;
 
   int c;
   uid_t uid;
 
-  while ((c = getopt(argc, argv, "a:c:eE:fi:m:M:o:p:r:s:t:Tvw:x:")) >= 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':
     switch (c)
       {
       case 'a':
@@ -1107,6 +1249,9 @@ main(int argc, char **argv)
       case 'f':
        filter_syscalls++;
        break;
       case 'f':
        filter_syscalls++;
        break;
+      case 'k':
+       stack_limit = atol(optarg);
+       break;
       case 'i':
        redir_stdin = optarg;
        break;
       case 'i':
        redir_stdin = optarg;
        break;