summary refs log tree commit diff
path: root/pkgs/build-support/build-fhs-userenv/chrootenv/chrootenv.c
blob: e57949cc44cbef52d8e70519da9ff7c5be8b4db3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#define _GNU_SOURCE

#include <glib.h>
#include <glib/gstdio.h>

#include <errno.h>
#include <sched.h>
#include <unistd.h>

#define fail(s, err) g_error("%s: %s: %s", __func__, s, g_strerror(err))
#define fail_if(expr)                                                          \
  if (expr)                                                                    \
    fail(#expr, errno);

#include <ftw.h>

#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/syscall.h>

int min(int a, int b) {
  return a > b ? b : a;
}

const gchar *bind_blacklist[] = {"bin", "etc", "host", "usr", "lib", "lib64", "lib32", "sbin", NULL};

int pivot_root(const char *new_root, const char *put_old) {
  return syscall(SYS_pivot_root, new_root, put_old);
}

void mount_tmpfs(const gchar *target) {
  fail_if(mount("none", target, "tmpfs", 0, NULL));
}

void bind_mount(const gchar *source, const gchar *target) {
  fail_if(g_mkdir(target, 0755));
  fail_if(mount(source, target, "bind", MS_BIND | MS_REC, NULL));
}

void pivot_host(const gchar *host, const gchar *guest) {
  g_autofree gchar *point = g_build_filename(guest, "host", NULL);
  fail_if(g_mkdir(point, 0755));
  fail_if(pivot_root(guest, point));
}

void bind_mount_item(const gchar *host, const gchar *guest, const gchar *name) {
  g_autofree gchar *source = g_build_filename(host, name, NULL);
  g_autofree gchar *target = g_build_filename(guest, name, NULL);

  if (G_LIKELY(g_file_test(source, G_FILE_TEST_IS_DIR)))
    bind_mount(source, target);
}

void bind(const gchar *host, const gchar *guest) {
  mount_tmpfs(guest);

  g_autoptr(GError) err = NULL;
  g_autoptr(GDir) dir = g_dir_open(host, 0, &err);

  if (err != NULL)
    fail("g_dir_open", errno);

  const gchar *item;

  while ((item = g_dir_read_name(dir)))
    if (!g_strv_contains(bind_blacklist, item))
      bind_mount_item(host, guest, item);

  pivot_host(host, guest);
}

void spit(const char *path, char *fmt, ...) {
  va_list args;
  va_start(args, fmt);

  FILE *f = g_fopen(path, "w");

  if (f == NULL)
    fail("g_fopen", errno);

  g_vfprintf(f, fmt, args);
  fclose(f);
}

int nftw_remove(const char *path, const struct stat *sb, int type,
                struct FTW *ftw) {
  return remove(path);
}

int main(gint argc, gchar **argv) {
  const gchar *self = *argv++;

  if (argc < 2) {
    g_message("%s command [arguments...]", self);
    return 1;
  }

  g_autofree gchar *prefix =
      g_build_filename(g_get_tmp_dir(), "chrootenvXXXXXX", NULL);

  fail_if(!g_mkdtemp_full(prefix, 0755));

  pid_t cpid = fork();

  if (cpid < 0)
    fail("fork", errno);

  else if (cpid == 0) {
    uid_t uid = getuid();
    gid_t gid = getgid();

    if (unshare(CLONE_NEWNS | CLONE_NEWUSER) < 0) {
      int unshare_errno = errno;

      g_message("Requires Linux version >= 3.19 built with CONFIG_USER_NS");
      if (g_file_test("/proc/sys/kernel/unprivileged_userns_clone",
                      G_FILE_TEST_EXISTS))
        g_message("Run: sudo sysctl -w kernel.unprivileged_userns_clone=1");

      fail("unshare", unshare_errno);
    }

    spit("/proc/self/setgroups", "deny");
    spit("/proc/self/uid_map", "%d %d 1", uid, uid);
    spit("/proc/self/gid_map", "%d %d 1", gid, gid);

    bind("/", prefix);

    fail_if(chdir("/"));
    fail_if(execvp(*argv, argv));
  }

  else {
    int status;

    fail_if(waitpid(cpid, &status, 0) != cpid);
    // glibc 2.27 (and possibly other versions) can't handle
    // an nopenfd value larger than 2^19
    fail_if(nftw(prefix, nftw_remove, min(getdtablesize(), 1<<19),
                 FTW_DEPTH | FTW_MOUNT | FTW_PHYS));

    if (WIFEXITED(status))
      return WEXITSTATUS(status);

    else if (WIFSIGNALED(status))
      kill(getpid(), WTERMSIG(status));

    return 1;
  }
}