]> mj.ucw.cz Git - moe.git/blobdiff - isolate/isolate.c
Isolate: Directory rules can have flags
[moe.git] / isolate / isolate.c
index ce609bf39f0fc39f0d7d59954eb9dd24be648974..ea14aa9998ba4f6e79dd6a838104ad92b558af7b 100644 (file)
@@ -232,6 +232,8 @@ msg(char *msg, ...)
   va_end(args);
 }
 
   va_end(args);
 }
 
+/*** Utility functions ***/
+
 static void *
 xmalloc(size_t size)
 {
 static void *
 xmalloc(size_t size)
 {
@@ -241,6 +243,21 @@ xmalloc(size_t size)
   return p;
 }
 
   return p;
 }
 
+static char *
+xstrdup(char *str)
+{
+  char *p = strdup(str);
+  if (!p)
+    die("Out of memory");
+  return p;
+}
+
+static int dir_exists(char *path)
+{
+  struct stat st;
+  return (stat(path, &st) >= 0 && S_ISDIR(st.st_mode));
+}
+
 /*** Environment rules ***/
 
 struct env_rule {
 /*** Environment rules ***/
 
 struct env_rule {
@@ -376,6 +393,180 @@ setup_environment(void)
   return env;
 }
 
   return env;
 }
 
+/*** Mount rules ***/
+
+struct dir_rule {
+  char *inside;                        // A relative path
+  char *outside;               // This can be an absolute path or a relative path starting with "./"
+  unsigned int flags;          // DIR_FLAG_xxx
+  struct dir_rule *next;
+};
+
+enum dir_rule_flags {
+  DIR_FLAG_RW = 1,
+  DIR_FLAG_NOEXEC = 2,
+  DIR_FLAG_FS = 4,
+  DIR_FLAG_MAYBE = 8,
+};
+
+static struct dir_rule *first_dir_rule;
+static struct dir_rule **last_dir_rule = &first_dir_rule;
+
+static int add_dir_rule(char *in, char *out, unsigned int flags)
+{
+  // Make sure that "in" is relative
+  while (in[0] == '/')
+    in++;
+  if (!*in)
+    return 0;
+
+  // Check "out"
+  if (flags & DIR_FLAG_FS)
+    {
+      if (!out || out[0] == '/')
+       return 0;
+    }
+  else
+    {
+      if (out && out[0] != '/' && strncmp(out, "./", 2))
+       return 0;
+    }
+
+  // Override an existing rule
+  struct dir_rule *r;
+  for (r = first_dir_rule; r; r=r->next)
+    if (!strcmp(r->inside, in))
+      break;
+
+  // Add a new rule
+  if (!r)
+    {
+      struct dir_rule *r = xmalloc(sizeof(*r));
+      r->inside = in;
+      *last_dir_rule = r;
+      last_dir_rule = &r->next;
+      r->next = NULL;
+    }
+  r->outside = out;
+  r->flags = flags;
+  return 1;
+}
+
+static unsigned int parse_dir_option(char *opt)
+{
+  if (!strcmp(opt, "rw"))
+    return DIR_FLAG_RW;
+  if (!strcmp(opt, "noexec"))
+    return DIR_FLAG_NOEXEC;
+  if (!strcmp(opt, "fs"))
+    return DIR_FLAG_FS;
+  if (!strcmp(opt, "maybe"))
+    return DIR_FLAG_MAYBE;
+  die("Unknown directory option %s", opt);
+}
+
+static int set_dir_action(char *arg)
+{
+  arg = xstrdup(arg);
+
+  char *colon = strchr(arg, ':');
+  unsigned int flags = 0;
+  while (colon)
+    {
+      char *opt = colon + 1;
+      char *next = strchr(opt, ':');
+      if (next)
+       *next = 0;
+      flags |= parse_dir_option(opt);
+      colon = next;
+    }
+
+  char *eq = strchr(arg, '=');
+  if (eq)
+    {
+      *eq++ = 0;
+      return add_dir_rule(arg, (*eq ? eq : NULL), flags);
+    }
+  else
+    {
+      char *out = xmalloc(1 + strlen(arg) + 1);
+      sprintf(out, "/%s", arg);
+      return add_dir_rule(arg, out, flags);
+    }
+}
+
+static void init_dir_rules(void)
+{
+  set_dir_action("box=./box:rw");
+  set_dir_action("bin");
+  set_dir_action("dev");
+  set_dir_action("lib");
+  set_dir_action("lib64:maybe");
+  set_dir_action("proc=proc:fs");
+  set_dir_action("usr");
+}
+
+static void make_dir(char *path)
+{
+  char *sep = path;
+  for (;;)
+    {
+      sep = strchr(sep, '/');
+      if (sep)
+       *sep = 0;
+
+      if (!dir_exists(path) && mkdir(path, 0777) < 0)
+       die("Cannot create directory %s: %m\n", path);
+
+      if (!sep)
+       return;
+      *sep++ = '/';
+    }
+}
+
+static void apply_dir_rules(void)
+{
+  for (struct dir_rule *r = first_dir_rule; r; r=r->next)
+    {
+      char *in = r->inside;
+      char *out = r->outside;
+      if (!out)
+       {
+         msg("Not binding anything on %s\n", r->inside);
+         continue;
+       }
+
+      if ((r->flags & DIR_FLAG_MAYBE) && !dir_exists(out))
+       {
+         msg("Not binding %s on %s (does not exist)\n", out, r->inside);
+         continue;
+       }
+
+      char root_in[1024];
+      snprintf(root_in, sizeof(root_in), "root/%s", in);
+      make_dir(root_in);
+
+      unsigned long mount_flags = 0;
+      if (!(r->flags & DIR_FLAG_RW))
+       mount_flags |= MS_RDONLY;
+      if (r->flags & DIR_FLAG_NOEXEC)
+       mount_flags |= MS_NOEXEC;
+
+      if (r->flags & DIR_FLAG_FS)
+       {
+         msg("Mounting %s on %s\n", out, in);
+         if (mount("none", root_in, out, mount_flags, "") < 0)
+           die("Cannot mount %s on %s: %m", out, in);
+       }
+      else
+       {
+         msg("Binding %s on %s\n", out, in);
+         if (mount(out, root_in, "none", MS_BIND | MS_NOSUID | MS_NODEV | mount_flags, "") < 0)
+           die("Cannot bind %s on %s: %m", out, in);
+       }
+    }
+}
+
 /*** Control groups ***/
 
 static char cg_path[256];
 /*** Control groups ***/
 
 static char cg_path[256];
@@ -456,8 +647,7 @@ cg_init(void)
   if (!cg_enable)
     return;
 
   if (!cg_enable)
     return;
 
-  struct stat st;
-  if (stat(cg_root, &st) < 0 || !S_ISDIR(st.st_mode))
+  if (!dir_exists(cg_root))
     die("Control group filesystem at %s not mounted", cg_root);
 
   snprintf(cg_path, sizeof(cg_path), "%s/box-%d", cg_root, BOX_UID);
     die("Control group filesystem at %s not mounted", cg_root);
 
   snprintf(cg_path, sizeof(cg_path), "%s/box-%d", cg_root, BOX_UID);
@@ -767,23 +957,7 @@ setup_root(void)
   if (mount("none", "root", "tmpfs", 0, "mode=755") < 0)
     die("Cannot mount root ramdisk: %m");
 
   if (mount("none", "root", "tmpfs", 0, "mode=755") < 0)
     die("Cannot mount root ramdisk: %m");
 
-  static const char * const dirs[] = { "box", "/bin", "/lib", "/lib64", "/usr", "/dev" };
-  for (int i=0; i < ARRAY_SIZE(dirs); i++)
-    {
-      const char *d = dirs[i];
-      char buf[1024];
-      snprintf(buf, sizeof(buf), "root/%s", (d[0] == '/' ? d+1 : d));
-      msg("Binding %s on %s\n", d, buf);
-      if (mkdir(buf, 0755) < 0)
-       die("mkdir(%s): %m", buf);
-      if (mount(d, buf, "none", MS_BIND | MS_NOSUID | MS_NODEV, "") < 0)
-       die("Cannot bind %s on %s: %m", d, buf);
-    }
-
-  if (mkdir("root/proc", 0755) < 0)
-    die("Cannot create proc: %m");
-  if (mount("none", "root/proc", "proc", 0, "") < 0)
-    die("Cannot mount proc: %m");
+  apply_dir_rules();
 
   if (chroot("root") < 0)
     die("Chroot failed: %m");
 
   if (chroot("root") < 0)
     die("Chroot failed: %m");
@@ -891,8 +1065,7 @@ init(void)
 static void
 cleanup(void)
 {
 static void
 cleanup(void)
 {
-  struct stat st;
-  if (stat("box", &st) < 0 || !S_ISDIR(st.st_mode))
+  if (!dir_exists("box"))
     die("Box directory not found, there isn't anything to clean up");
 
   msg("Deleting sandbox directory\n");
     die("Box directory not found, there isn't anything to clean up");
 
   msg("Deleting sandbox directory\n");
@@ -903,8 +1076,7 @@ cleanup(void)
 static void
 run(char **argv)
 {
 static void
 run(char **argv)
 {
-  struct stat st;
-  if (stat("box", &st) < 0 || !S_ISDIR(st.st_mode))
+  if (!dir_exists("box"))
     die("Box directory not found, did you run `isolate --init'?");
 
   char cmd[256];
     die("Box directory not found, did you run `isolate --init'?");
 
   char cmd[256];
@@ -954,6 +1126,10 @@ Options:\n\
 -c, --cg[=<parent>]\tPut process in a control group (optionally a sub-group of <parent>)\n\
     --cg-mem=<size>\tLimit memory usage of the control group to <size> KB\n\
     --cg-timing\t\tTime limits affects total run time of the control group\n\
 -c, --cg[=<parent>]\tPut process in a control group (optionally a sub-group of <parent>)\n\
     --cg-mem=<size>\tLimit memory usage of the control group to <size> KB\n\
     --cg-timing\t\tTime limits affects total run time of the control group\n\
+-d, --dir=<dir>\t\tMake a directory <dir> visible inside the sandbox\n\
+    --dir=<in>=<out>\tMake a directory <out> outside visible as <in> inside\n\
+    --dir=<in>=\t\tDelete a previously defined directory rule (even a default one)\n\
+    --dir=...:<opt>\tSpecify options for a rule: rw, noexec, fs, maybe\n\
 -E, --env=<var>\t\tInherit the environment variable <var> from the parent process\n\
 -E, --env=<var>=<val>\tSet the environment variable <var> to <val>; unset it if <var> is empty\n\
 -x, --extra-time=<time>\tSet extra timeout, before which a timing-out program is not yet killed,\n\
 -E, --env=<var>\t\tInherit the environment variable <var> from the parent process\n\
 -E, --env=<var>=<val>\tSet the environment variable <var> to <val>; unset it if <var> is empty\n\
 -x, --extra-time=<time>\tSet extra timeout, before which a timing-out program is not yet killed,\n\
@@ -988,13 +1164,14 @@ enum opt_code {
   OPT_CG_TIMING,
 };
 
   OPT_CG_TIMING,
 };
 
-static const char short_opts[] = "c::eE:i:k:m:M:o:p::r:t:vw:x:";
+static const char short_opts[] = "c::d:eE:i:k:m:M:o:p::r:t:vw:x:";
 
 static const struct option long_opts[] = {
   { "cg",              2, NULL, 'c' },
   { "cg-mem",          1, NULL, OPT_CG_MEM },
   { "cg-timing",       0, NULL, OPT_CG_TIMING },
   { "cleanup",         0, NULL, OPT_CLEANUP },
 
 static const struct option long_opts[] = {
   { "cg",              2, NULL, 'c' },
   { "cg-mem",          1, NULL, OPT_CG_MEM },
   { "cg-timing",       0, NULL, OPT_CG_TIMING },
   { "cleanup",         0, NULL, OPT_CLEANUP },
+  { "dir",             1, NULL, 'd' },
   { "env",             1, NULL, 'E' },
   { "extra-time",      1, NULL, 'x' },
   { "full-env",                0, NULL, 'e' },
   { "env",             1, NULL, 'E' },
   { "extra-time",      1, NULL, 'x' },
   { "full-env",                0, NULL, 'e' },
@@ -1020,6 +1197,8 @@ main(int argc, char **argv)
   int c;
   enum opt_code mode = 0;
 
   int c;
   enum opt_code mode = 0;
 
+  init_dir_rules();
+
   while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) >= 0)
     switch (c)
       {
   while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) >= 0)
     switch (c)
       {
@@ -1028,6 +1207,10 @@ main(int argc, char **argv)
          cg_root = optarg;
        cg_enable = 1;
        break;
          cg_root = optarg;
        cg_enable = 1;
        break;
+      case 'd':
+       if (!set_dir_action(optarg))
+         usage();
+       break;
       case 'e':
        pass_environ = 1;
        break;
       case 'e':
        pass_environ = 1;
        break;