staging 4.0.0-rc
This commit is contained in:
1 parent
ae54f74070
commit
fb02622b5b
30 files changed
+2346
-2671
No files matched your search
+435
@@ -0,0 +1,435 @@
|
||||
/*
|
||||
* gsandbox: the build sandbox (BUILD_DESIGN.txt section 6). Run by
|
||||
* gpkg/syspkg, not by hand; installed beside the libraries.
|
||||
*
|
||||
* gsandbox -X [-u USER] -p GPKG -d LAYER
|
||||
* gsandbox -R [-u USER] -L DIR [-l NAME]... -w WORK -i IN -o OUT
|
||||
*
|
||||
* Run as root, -u is required: gsandbox drops to USER (supplementary
|
||||
* groups, gid, uid) before anything else and refuses a USER whose uid
|
||||
* is 0. Run as anyone else, there is no -u: it acts as its caller.
|
||||
*
|
||||
* -X extracts GPKG's files/ tree into LAYER (created; it must not
|
||||
* exist), with the rules of an install (gl_extract_files).
|
||||
*
|
||||
* -R runs a build. In new user, mount, network, pid, ipc and uts
|
||||
* namespaces, with the caller mapped to uid/gid 0: an overlay whose
|
||||
* lower layers are DIR/NAME for each -l (the first on top) over a
|
||||
* skeleton made in WORK, with WORK/upper as its writable layer; /dev's
|
||||
* null, zero, full, random, urandom and tty bound in; a fresh /proc;
|
||||
* IN read-only at /.glacier/in and OUT writable at /.glacier/out; then
|
||||
* pivot_root into it, no new privileges, a clean environment, and
|
||||
* /bin/sh /.glacier/in/driver as pid 1. The network namespace has only
|
||||
* a loopback, down. Everything the build writes outside OUT lands in
|
||||
* WORK/upper, which the caller deletes.
|
||||
*
|
||||
* Exit: -X 0 or 1; -R the driver's status (128 + signal if killed), or
|
||||
* 125 if the sandbox could not be set up (having said why).
|
||||
*/
|
||||
|
||||
#define _GNU_SOURCE
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <getopt.h>
|
||||
#include <grp.h>
|
||||
#include <limits.h>
|
||||
#include <pwd.h>
|
||||
#include <sched.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mount.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "istoreutils.h"
|
||||
#include "log.h"
|
||||
#include "pkg.h"
|
||||
|
||||
#define SETUP_FAILED 125
|
||||
#define MAX_LAYERS 256
|
||||
|
||||
#define USAGE \
|
||||
"usage: gsandbox [-hv]\n" \
|
||||
" gsandbox -X [-u USER] -p GPKG -d LAYER\n" \
|
||||
" gsandbox -R [-u USER] -L DIR [-l NAME]... -w WORK -i IN -o OUT\n"
|
||||
|
||||
static const struct option longopts[] = {
|
||||
{ "help", no_argument, NULL, 'h' },
|
||||
{ "version", no_argument, NULL, 'v' },
|
||||
{ "extract", no_argument, NULL, 'X' },
|
||||
{ "run", no_argument, NULL, 'R' },
|
||||
{ "user", required_argument, NULL, 'u' },
|
||||
{ "package", required_argument, NULL, 'p' },
|
||||
{ "dest", required_argument, NULL, 'd' },
|
||||
{ "layers", required_argument, NULL, 'L' },
|
||||
{ "layer", required_argument, NULL, 'l' },
|
||||
{ "work", required_argument, NULL, 'w' },
|
||||
{ "in", required_argument, NULL, 'i' },
|
||||
{ "out", required_argument, NULL, 'o' },
|
||||
{ NULL, 0, NULL, 0 }
|
||||
};
|
||||
|
||||
static void
|
||||
usage(void)
|
||||
{
|
||||
fprintf(stderr, USAGE);
|
||||
}
|
||||
|
||||
static void
|
||||
help(void)
|
||||
{
|
||||
printf("gsandbox: Run Glacier package builds in a sandbox\n");
|
||||
printf(USAGE);
|
||||
printf("\n");
|
||||
printf("%-25s %s\n", "{-h --help}", "Show this message");
|
||||
printf("%-25s %s\n", "{-v --version}", "Show the current version");
|
||||
printf("%-25s %s\n", "{-X --extract}", "Extract a package into a build layer");
|
||||
printf("%-25s %s\n", "{-R --run}", "Run a build");
|
||||
printf("%-25s %s\n", "{-u --user} USER", "Build as USER (required as root)");
|
||||
printf("%-25s %s\n", "{-p --package} GPKG", "Package to extract");
|
||||
printf("%-25s %s\n", "{-d --dest} LAYER", "Layer directory to create");
|
||||
printf("%-25s %s\n", "{-L --layers} DIR", "Directory holding the layers");
|
||||
printf("%-25s %s\n", "{-l --layer} NAME", "Add a layer, first on top");
|
||||
printf("%-25s %s\n", "{-w --work} WORK", "Work directory");
|
||||
printf("%-25s %s\n", "{-i --in} IN", "Build input, read-only");
|
||||
printf("%-25s %s\n", "{-o --out} OUT", "Build output");
|
||||
printf("\n");
|
||||
printf("This program is free software.\n");
|
||||
printf("See the GNU GPL version 3 for details.\n");
|
||||
}
|
||||
|
||||
/* As root: become USER for good. Elsewhere: USER must not be given. */
|
||||
static int
|
||||
drop_privileges(const char *user)
|
||||
{
|
||||
if (geteuid() != 0) {
|
||||
if (user) {
|
||||
lg_printf(LG_ERR, "-u needs root.");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (!user) {
|
||||
lg_printf(LG_ERR, "Refusing to build as root: -u is required.");
|
||||
return -1;
|
||||
}
|
||||
struct passwd *pw = getpwnam(user);
|
||||
if (!pw) {
|
||||
lg_printf(LG_ERR, "No user named %s.", user);
|
||||
return -1;
|
||||
}
|
||||
if (pw->pw_uid == 0 || pw->pw_gid == 0) {
|
||||
lg_printf(LG_ERR, "Refusing to build as %s: its uid or gid is 0.", user);
|
||||
return -1;
|
||||
}
|
||||
if (setgroups(0, NULL) != 0 || setgid(pw->pw_gid) != 0 ||
|
||||
setuid(pw->pw_uid) != 0) {
|
||||
lg_printf(LG_ERR, "Cannot become %s: %s", user, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
if (setuid(0) == 0 || geteuid() == 0 || getegid() == 0) {
|
||||
lg_printf(LG_ERR, "Cannot drop root.");
|
||||
return -1;
|
||||
}
|
||||
/* Changing uid made the process non-dumpable, which leaves
|
||||
* /proc/self owned by root: it could not write its own uid_map.
|
||||
* Nothing of root's is left in it to protect. */
|
||||
if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) != 0) {
|
||||
lg_printf(LG_ERR, "Cannot drop root.");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
write_file(const char *path, const char *text)
|
||||
{
|
||||
int fd = open(path, O_WRONLY | O_CLOEXEC);
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
ssize_t n = write(fd, text, strlen(text));
|
||||
int rc = (n == (ssize_t)strlen(text)) ? 0 : -1;
|
||||
close(fd);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int
|
||||
make_file(const char *path, const char *text, mode_t mode)
|
||||
{
|
||||
int fd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, mode);
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
ssize_t n = write(fd, text, strlen(text));
|
||||
int rc = (n == (ssize_t)strlen(text) && fchmod(fd, mode) == 0) ? 0 : -1;
|
||||
close(fd);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* The lowest layer: the merged-/usr links gl_create_fs_tree makes, the
|
||||
* mount points, a root user and the build directory. */
|
||||
static int
|
||||
make_skeleton(const char *skel)
|
||||
{
|
||||
static const char *const dirs[] = { "", "usr", "usr/bin", "usr/lib",
|
||||
"etc", "dev", "proc", "tmp", "build", ".glacier", ".glacier/in",
|
||||
".glacier/out", NULL };
|
||||
static const char *const links[][2] = { { "bin", "usr/bin" },
|
||||
{ "sbin", "usr/bin" }, { "lib", "usr/lib" }, { "usr/sbin", "bin" } };
|
||||
char p[PATH_MAX];
|
||||
for (size_t i = 0; dirs[i]; i++) {
|
||||
snprintf(p, sizeof p, "%s/%s", skel, dirs[i]);
|
||||
if (mkdir(p, 0755) != 0 || chmod(p, 0755) != 0)
|
||||
return -1;
|
||||
}
|
||||
snprintf(p, sizeof p, "%s/tmp", skel);
|
||||
if (chmod(p, 01777) != 0)
|
||||
return -1;
|
||||
for (size_t i = 0; i < sizeof links / sizeof *links; i++) {
|
||||
snprintf(p, sizeof p, "%s/%s", skel, links[i][0]);
|
||||
if (symlink(links[i][1], p) != 0)
|
||||
return -1;
|
||||
}
|
||||
snprintf(p, sizeof p, "%s/etc/passwd", skel);
|
||||
if (make_file(p, "root:x:0:0:root:/tmp:/bin/sh\n", 0644) != 0)
|
||||
return -1;
|
||||
snprintf(p, sizeof p, "%s/etc/group", skel);
|
||||
return make_file(p, "root:x:0:\n", 0644);
|
||||
}
|
||||
|
||||
/* A path that can go into overlayfs's option string as is. */
|
||||
static bool
|
||||
plain_path(const char *s)
|
||||
{
|
||||
return *s == '/' && !strpbrk(s, ",:\\") && strlen(s) < 1024;
|
||||
}
|
||||
|
||||
static int
|
||||
bind_dir(const char *src, const char *dst, bool ro)
|
||||
{
|
||||
if (mount(src, dst, NULL, MS_BIND | MS_REC, NULL) != 0)
|
||||
return -1;
|
||||
unsigned long fl = MS_BIND | MS_REMOUNT | MS_NOSUID | MS_NODEV | (ro ? MS_RDONLY : 0);
|
||||
return mount(NULL, dst, NULL, fl, NULL);
|
||||
}
|
||||
|
||||
#define FAIL(...) do { lg_printf(LG_ERR, __VA_ARGS__); _exit(SETUP_FAILED); } while (0)
|
||||
|
||||
/* Pid 1 of the new namespaces: mount the root, enter it, run the driver. */
|
||||
static void
|
||||
child(const char *ldir, char **layers, size_t nlayers, const char *work,
|
||||
const char *in, const char *out)
|
||||
{
|
||||
char skel[PATH_MAX], upper[PATH_MAX], ovl[PATH_MAX], root[PATH_MAX];
|
||||
snprintf(skel, sizeof skel, "%s/skel", work);
|
||||
snprintf(upper, sizeof upper, "%s/upper", work);
|
||||
snprintf(ovl, sizeof ovl, "%s/ovl", work);
|
||||
snprintf(root, sizeof root, "%s/root", work);
|
||||
|
||||
if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL) != 0)
|
||||
FAIL("Cannot make the mounts private: %s", strerror(errno));
|
||||
|
||||
/* Layer names relative to DIR keep the option string short. */
|
||||
if (chdir(ldir) != 0)
|
||||
FAIL("Cannot enter %s: %s", ldir, strerror(errno));
|
||||
size_t cap = 3 * PATH_MAX + 64, used = 0;
|
||||
for (size_t i = 0; i < nlayers; i++)
|
||||
cap += strlen(layers[i]) + 1;
|
||||
char *opts = malloc(cap);
|
||||
if (!opts)
|
||||
FAIL("Out of memory.");
|
||||
used += (size_t)snprintf(opts, cap, "lowerdir=");
|
||||
for (size_t i = 0; i < nlayers; i++)
|
||||
used += (size_t)snprintf(opts + used, cap - used, "%s:", layers[i]);
|
||||
/* userxattr: inside a user namespace overlayfs can't set its
|
||||
* trusted.* xattrs, so without it a directory removed and made
|
||||
* again in the build root would show the layer's old contents
|
||||
* (no opaque marker). Kernels before 5.11 don't know the option. */
|
||||
used += (size_t)snprintf(opts + used, cap - used,
|
||||
"%s,upperdir=%s,workdir=%s", skel, upper, ovl);
|
||||
snprintf(opts + used, cap - used, ",userxattr");
|
||||
if (mount("overlay", root, "overlay", MS_NOSUID | MS_NODEV, opts) != 0) {
|
||||
opts[used] = '\0';
|
||||
if (errno != EINVAL ||
|
||||
mount("overlay", root, "overlay", MS_NOSUID | MS_NODEV, opts) != 0)
|
||||
FAIL("Cannot mount the build root: %s", strerror(errno));
|
||||
}
|
||||
free(opts);
|
||||
|
||||
static const char *const devs[] = { "null", "zero", "full", "random",
|
||||
"urandom", "tty", NULL };
|
||||
char p[PATH_MAX], h[PATH_MAX];
|
||||
for (size_t i = 0; devs[i]; i++) {
|
||||
snprintf(h, sizeof h, "/dev/%s", devs[i]);
|
||||
if (access(h, F_OK) != 0)
|
||||
continue;
|
||||
snprintf(p, sizeof p, "%s/dev/%s", root, devs[i]);
|
||||
int fd = open(p, O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0666);
|
||||
if (fd < 0)
|
||||
FAIL("Cannot create %s: %s", p, strerror(errno));
|
||||
close(fd);
|
||||
if (mount(h, p, NULL, MS_BIND, NULL) != 0)
|
||||
FAIL("Cannot bind %s: %s", h, strerror(errno));
|
||||
}
|
||||
snprintf(p, sizeof p, "%s/proc", root);
|
||||
if (mount("proc", p, "proc", MS_NOSUID | MS_NODEV | MS_NOEXEC, NULL) != 0)
|
||||
FAIL("Cannot mount /proc: %s", strerror(errno));
|
||||
snprintf(p, sizeof p, "%s/.glacier/in", root);
|
||||
if (bind_dir(in, p, true) != 0)
|
||||
FAIL("Cannot bind %s: %s", in, strerror(errno));
|
||||
snprintf(p, sizeof p, "%s/.glacier/out", root);
|
||||
if (bind_dir(out, p, false) != 0)
|
||||
FAIL("Cannot bind %s: %s", out, strerror(errno));
|
||||
|
||||
if (chdir(root) != 0 || syscall(SYS_pivot_root, ".", ".") != 0 ||
|
||||
umount2(".", MNT_DETACH) != 0 || chdir("/") != 0)
|
||||
FAIL("Cannot enter the build root: %s", strerror(errno));
|
||||
if (sethostname("glacier-build", 13) != 0 ||
|
||||
prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0)
|
||||
FAIL("Cannot set up the build: %s", strerror(errno));
|
||||
|
||||
for (int fd = 3; fd < 1024; fd++)
|
||||
close(fd);
|
||||
char *const argv[] = { "/bin/sh", "/.glacier/in/driver", NULL };
|
||||
char *const envp[] = { "PATH=/usr/bin:/bin", "HOME=/tmp", "LC_ALL=C",
|
||||
"TZ=UTC", "TERM=dumb", "FORCE_UNSAFE_CONFIGURE=1", NULL };
|
||||
execve(argv[0], argv, envp);
|
||||
FAIL("Cannot run the build driver: %s", strerror(errno));
|
||||
}
|
||||
|
||||
static int
|
||||
run(const char *ldir, char **layers, size_t nlayers, const char *work,
|
||||
const char *in, const char *out)
|
||||
{
|
||||
if (!plain_path(work) || !plain_path(ldir) || !plain_path(in) || !plain_path(out)) {
|
||||
lg_printf(LG_ERR, "Refusing a path that is not absolute or holds ',', ':' or '\\'.");
|
||||
return SETUP_FAILED;
|
||||
}
|
||||
for (size_t i = 0; i < nlayers; i++) {
|
||||
if (!gl_valid_pkg_ident(layers[i])) {
|
||||
lg_printf(LG_ERR, "Refusing layer name %s.", layers[i]);
|
||||
return SETUP_FAILED;
|
||||
}
|
||||
}
|
||||
char p[PATH_MAX];
|
||||
const char *subdirs[] = { "upper", "ovl", "root" };
|
||||
for (size_t i = 0; i < 3; i++) {
|
||||
snprintf(p, sizeof p, "%s/%s", work, subdirs[i]);
|
||||
if (mkdir(p, 0700) != 0) {
|
||||
lg_printf(LG_ERR, "Cannot create %s: %s", p, strerror(errno));
|
||||
return SETUP_FAILED;
|
||||
}
|
||||
}
|
||||
snprintf(p, sizeof p, "%s/skel", work);
|
||||
if (make_skeleton(p) != 0) {
|
||||
lg_printf(LG_ERR, "Cannot create %s: %s", p, strerror(errno));
|
||||
return SETUP_FAILED;
|
||||
}
|
||||
|
||||
uid_t uid = getuid();
|
||||
gid_t gid = getgid();
|
||||
if (unshare(CLONE_NEWUSER | CLONE_NEWNS | CLONE_NEWNET | CLONE_NEWPID |
|
||||
CLONE_NEWIPC | CLONE_NEWUTS) != 0) {
|
||||
lg_printf(LG_ERR, "Builds need unprivileged user namespaces.");
|
||||
return SETUP_FAILED;
|
||||
}
|
||||
char map[64];
|
||||
snprintf(map, sizeof map, "0 %u 1\n", (unsigned)uid);
|
||||
if (write_file("/proc/self/setgroups", "deny") != 0 ||
|
||||
write_file("/proc/self/uid_map", map) != 0) {
|
||||
lg_printf(LG_ERR, "Cannot map the build user: %s", strerror(errno));
|
||||
return SETUP_FAILED;
|
||||
}
|
||||
snprintf(map, sizeof map, "0 %u 1\n", (unsigned)gid);
|
||||
if (write_file("/proc/self/gid_map", map) != 0) {
|
||||
lg_printf(LG_ERR, "Cannot map the build group: %s", strerror(errno));
|
||||
return SETUP_FAILED;
|
||||
}
|
||||
|
||||
pid_t pid = fork();
|
||||
if (pid < 0) {
|
||||
lg_printf(LG_ERR, "Cannot start the build: %s", strerror(errno));
|
||||
return SETUP_FAILED;
|
||||
}
|
||||
if (pid == 0)
|
||||
child(ldir, layers, nlayers, work, in, out);
|
||||
|
||||
int status;
|
||||
while (waitpid(pid, &status, 0) < 0) {
|
||||
if (errno != EINTR)
|
||||
return SETUP_FAILED;
|
||||
}
|
||||
if (WIFSIGNALED(status))
|
||||
return 128 + WTERMSIG(status);
|
||||
return WEXITSTATUS(status);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
const char *user = NULL, *pkg = NULL, *dest = NULL, *ldir = NULL;
|
||||
const char *work = NULL, *in = NULL, *out = NULL;
|
||||
char *layers[MAX_LAYERS];
|
||||
size_t nlayers = 0;
|
||||
int mode = 0, c;
|
||||
|
||||
while ((c = getopt_long(argc, argv, "hvXRu:p:d:L:l:w:i:o:", longopts, NULL)) != -1) {
|
||||
switch (c) {
|
||||
case 'h':
|
||||
help();
|
||||
return 0;
|
||||
case 'v':
|
||||
printf("v4.0.0\n");
|
||||
return 0;
|
||||
case 'X':
|
||||
case 'R':
|
||||
if (mode && mode != c) {
|
||||
usage();
|
||||
return 1;
|
||||
}
|
||||
mode = c;
|
||||
break;
|
||||
case 'u': user = optarg; break;
|
||||
case 'p': pkg = optarg; break;
|
||||
case 'd': dest = optarg; break;
|
||||
case 'L': ldir = optarg; break;
|
||||
case 'l':
|
||||
if (nlayers == MAX_LAYERS) {
|
||||
lg_printf(LG_ERR, "At most %d layers.", MAX_LAYERS);
|
||||
return 1;
|
||||
}
|
||||
layers[nlayers++] = optarg;
|
||||
break;
|
||||
case 'w': work = optarg; break;
|
||||
case 'i': in = optarg; break;
|
||||
case 'o': out = optarg; break;
|
||||
default:
|
||||
usage();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if (optind != argc || (mode == 'X' && (!pkg || !dest || ldir || nlayers || work || in || out)) ||
|
||||
(mode == 'R' && (!ldir || !work || !in || !out || pkg || dest)) || !mode) {
|
||||
usage();
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (drop_privileges(user) != 0)
|
||||
return mode == 'R' ? SETUP_FAILED : 1;
|
||||
|
||||
if (mode == 'X') {
|
||||
gl_install_status_t st = gl_extract_files(pkg, dest);
|
||||
if (st != GL_INSTALL_OK) {
|
||||
lg_printf(LG_ERR, "Cannot extract %s into %s.", pkg, dest);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return run(ldir, layers, nlayers, work, in, out);
|
||||
}
|
||||
Reference in new issue
Block a user