#define UNUSED __attribute__((unused))
#define ARRAY_SIZE(a) (int)(sizeof(a)/sizeof(a[0]))
-// FIXME: Make configurable, probably in compile time
-#define BOX_DIR "/tmp/box"
-#define BOX_UID 60000
-#define BOX_GID 60000
+#define BOX_DIR CONFIG_ISOLATE_BOX_DIR
+#define BOX_UID CONFIG_ISOLATE_BOX_UID
+#define BOX_GID CONFIG_ISOLATE_BOX_GID
static int timeout; /* milliseconds */
static int wall_timeout;
if (mount("none", "root", "tmpfs", 0, "mode=755") < 0)
die("Cannot mount root ramdisk: %m");
- // FIXME: Make the list of bind-mounts configurable
- // FIXME: Virtual /dev?
- // FIXME: Read-only mounts?
-
static const char * const dirs[] = { "box", "/bin", "/lib", "/usr", "/dev" };
for (int i=0; i < ARRAY_SIZE(dirs); i++)
{
const char *d = dirs[i];
- char buf[1024]; // FIXME
- sprintf(buf, "root/%s", (d[0] == '/' ? d+1 : d));
+ 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);
die("Cannot change current directory: %m");
}
-static int
-box_inside(void *arg)
+static void
+setup_credentials(void)
{
- write_errors_to_fd = error_pipes[1];
- close(error_pipes[0]);
-
- char **argv = arg;
- int argc = 0;
- while (argv[argc])
- argc++;
-
- struct rlimit rl;
- char *args[argc+1];
-
- memcpy(args, argv, argc * sizeof(char *));
- args[argc] = NULL;
-
- cg_enter();
- setup_root();
-
if (setresgid(BOX_GID, BOX_GID, BOX_GID) < 0)
die("setresgid: %m");
if (setgroups(0, NULL) < 0)
die("setgroups: %m");
if (setresuid(BOX_UID, BOX_UID, BOX_UID) < 0)
die("setresuid: %m");
+ setpgrp();
+}
+static void
+setup_fds(void)
+{
if (redir_stdin)
{
close(0);
}
else
dup2(1, 2);
- setpgrp();
+}
- if (memory_limit)
- {
- rl.rlim_cur = rl.rlim_max = memory_limit * 1024;
- if (setrlimit(RLIMIT_AS, &rl) < 0)
- die("setrlimit(RLIMIT_AS): %m");
- }
+static void
+setup_rlim(const char *res_name, int res, rlim_t limit)
+{
+ struct rlimit rl = { .rlim_cur = limit, .rlim_max = limit };
+ if (setrlimit(res, &rl) < 0)
+ die("setrlimit(%s, %jd)", res_name, (intmax_t) limit);
+}
- 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");
+static void
+setup_rlimits(void)
+{
+#define RLIM(res, val) setup_rlim("RLIMIT_" #res, RLIMIT_##res, val)
- rl.rlim_cur = rl.rlim_max = 64;
- if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
- die("setrlimit(RLIMIT_NOFILE): %m");
+ if (memory_limit)
+ RLIM(AS, memory_limit * 1024);
+
+ RLIM(STACK, (stack_limit ? (rlim_t)stack_limit * 1024 : RLIM_INFINITY));
+ RLIM(NOFILE, 64);
+ RLIM(MEMLOCK, 0);
if (max_processes)
- {
- rl.rlim_cur = rl.rlim_max = max_processes;
- if (setrlimit(RLIMIT_NPROC, &rl) < 0)
- die("setrlimit(RLIMIT_NPROC): %m");
- }
+ RLIM(NPROC, max_processes);
- rl.rlim_cur = rl.rlim_max = 0;
- if (setrlimit(RLIMIT_MEMLOCK, &rl) < 0)
- die("setrlimit(RLIMIT_MEMLOCK): %m");
+#undef RLIM
+}
+static int
+box_inside(void *arg)
+{
+ char **args = arg;
+ write_errors_to_fd = error_pipes[1];
+ close(error_pipes[0]);
+
+ cg_enter();
+ setup_root();
+ setup_credentials();
+ setup_fds();
+ setup_rlimits();
char **env = setup_environment();
+
execve(args[0], args, env);
die("execve(\"%s\"): %m", args[0]);
}
+/*** Commands ***/
+
static void
init(void)
{
printf("Sandbox credentials: uid=%u gid=%u\n", BOX_UID, BOX_GID);
}
+/*** Options ***/
+
static void
usage(void)
{