#include "autoconf.h"
-// FIXME: prune
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <grp.h>
#include <sys/wait.h>
-#include <sys/user.h>
#include <sys/time.h>
-#include <sys/ptrace.h>
#include <sys/signal.h>
-#include <sys/sysinfo.h>
#include <sys/resource.h>
#include <sys/mount.h>
#include <sys/stat.h>
#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;
static gid_t orig_gid;
static pid_t box_pid;
-static volatile sig_atomic_t timer_tick;
-static struct timeval start_time;
-static int ticks_per_sec;
static int partial_line;
static char cleanup_cmd[256];
+static struct timeval start_time;
+static int ticks_per_sec;
static int total_ms, wall_ms;
+static volatile sig_atomic_t timer_tick;
+
+static int error_pipes[2];
+static int write_errors_to_fd;
+static int read_errors_from_fd;
static void die(char *msg, ...) NONRET;
static void cg_stats(void);
{
va_list args;
va_start(args, msg);
- flush_line();
char buf[1024];
- vsnprintf(buf, sizeof(buf), msg, args);
+ int n = vsnprintf(buf, sizeof(buf), msg, args);
+
+ if (write_errors_to_fd)
+ {
+ // We are inside the box, have to use error pipe for error reporting.
+ // We hope that the whole error message fits in PIPE_BUF bytes.
+ write(write_errors_to_fd, buf, n);
+ exit(2);
+ }
+
+ // Otherwise, we in the box keeper process, so we report errors normally
+ flush_line();
meta_printf("status:XX\nmessage:%s\n", buf);
fputs(buf, stderr);
fputc('\n', stderr);
if (buf[0])
die("Some tasks left in control group %s, failed to remove it", cg_path);
- // FIXME: Is this needed?
- // cg_write("memory.force_empty", "0\n");
-
if (rmdir(cg_path) < 0)
die("Cannot remove control group %s: %m", cg_path);
}
static void
box_keeper(void)
{
- struct sigaction sa;
+ read_errors_from_fd = error_pipes[0];
+ close(error_pipes[1]);
+ struct sigaction sa;
bzero(&sa, sizeof(sa));
sa.sa_handler = signal_int;
sigaction(SIGINT, &sa, NULL);
}
if (p != box_pid)
die("wait4: unknown pid %d exited!", p);
+ box_pid = 0;
+
+ // Check error pipe if there is an internal error passed from inside the box
+ char interr[1024];
+ int n = read(read_errors_from_fd, interr, sizeof(interr) - 1);
+ if (n > 0)
+ {
+ interr[n] = 0;
+ die("%s", interr);
+ }
+
if (WIFEXITED(stat))
{
- box_pid = 0;
final_stats(&rus);
if (WEXITSTATUS(stat))
{
- // FIXME: Recognize internal errors during setup
meta_printf("exitcode:%d\n", WEXITSTATUS(stat));
err("RE: Exited with error status %d", WEXITSTATUS(stat));
}
wall_ms/1000, wall_ms%1000);
box_exit(0);
}
- if (WIFSIGNALED(stat))
+ else if (WIFSIGNALED(stat))
{
- box_pid = 0;
meta_printf("exitsig:%d\n", WTERMSIG(stat));
final_stats(&rus);
err("SG: Caught fatal signal %d", WTERMSIG(stat));
}
- if (WIFSTOPPED(stat))
+ else if (WIFSTOPPED(stat))
{
- box_pid = 0;
meta_printf("exitsig:%d\n", WSTOPSIG(stat));
final_stats(&rus);
err("SG: Stopped by signal %d", WSTOPSIG(stat));
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)
{
- 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);
+}
+
+static void
+setup_rlimits(void)
+{
+#define RLIM(res, val) setup_rlim("RLIMIT_" #res, RLIMIT_##res, val)
- 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");
+ if (memory_limit)
+ RLIM(AS, memory_limit * 1024);
- rl.rlim_cur = rl.rlim_max = 64;
- if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
- die("setrlimit(RLIMIT_NOFILE): %m");
+ 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)
{
xsystem(cmd);
snprintf(cleanup_cmd, sizeof(cleanup_cmd), "chown -R %d.%d box", orig_uid, orig_gid);
+ if (pipe(error_pipes) < 0)
+ die("pipe: %m");
+ for (int i=0; i<2; i++)
+ if (fcntl(error_pipes[i], F_SETFD, fcntl(error_pipes[i], F_GETFD) | FD_CLOEXEC) < 0 ||
+ fcntl(error_pipes[i], F_SETFL, fcntl(error_pipes[i], F_GETFL) | O_NONBLOCK) < 0)
+ die("fcntl on pipe: %m");
+
box_pid = clone(
box_inside, // Function to execute as the body of the new process
argv, // Pass our stack
printf("Sandbox credentials: uid=%u gid=%u\n", BOX_UID, BOX_GID);
}
+/*** Options ***/
+
static void
usage(void)
{