about summary refs log tree commit diff
path: root/pkgs/build-support/build-sandbox/src/setup.c
blob: a23983c8fd9fab5fe8767b45e2dbafc9b13976f4 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
#define _GNU_SOURCE

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

#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <malloc.h>
#include <sched.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "params.h"
#include "nix-query.h"
#include "path-cache.h"

static path_cache cached_paths = NULL;

static bool write_proc(int proc_pid_fd, const char *fname, const char *buf,
                       size_t buflen, bool ignore_errors)
{
    int fd;

    if ((fd = openat(proc_pid_fd, fname, O_WRONLY)) == -1) {
        fprintf(stderr, "open %s: %s\n", fname, strerror(errno));
        return false;
    }

    if (write(fd, buf, buflen) == -1) {
        if (!ignore_errors)
            fprintf(stderr, "write %s: %s\n", fname, strerror(errno));
        close(fd);
        return ignore_errors;
    }

    close(fd);
    return true;
}

#define WRITE_IDMAP(file, value) \
    buflen = snprintf(buf, 100, "%1$lu %1$lu 1", (unsigned long)value); \
    if (buflen >= 100) { \
        fputs("Unable to write buffer for " file ".\n", stderr); \
        close(proc_pid_fd); \
        return false; \
    } else if (buflen < 0) { \
        perror("snprintf " file " buffer"); \
        close(proc_pid_fd); \
        return false; \
    } \
    if (!write_proc(proc_pid_fd, file, buf, buflen, false)) { \
        close(proc_pid_fd); \
        return false; \
    }

bool write_maps(pid_t parent_pid)
{
    int proc_pid_fd;
    size_t buflen;
    char buf[100];

    buflen = snprintf(buf, 100, "/proc/%lu", (unsigned long)parent_pid);
    if (buflen >= 100) {
        fputs("Unable to write buffer for child pid proc path.\n", stderr);
        return false;
    } else if (buflen < 0) {
        perror("snprintf child pid proc path");
        return false;
    }

    if ((proc_pid_fd = open(buf, O_RDONLY | O_DIRECTORY)) == -1) {
        fprintf(stderr, "open %s: %s\n", buf, strerror(errno));
        return false;
    }

    WRITE_IDMAP("uid_map", geteuid());

    // Kernels prior to Linux 3.19 which do not impose setgroups()
    // restrictions won't have this file, so ignore failure.
    write_proc(proc_pid_fd, "setgroups", "deny", 4, true);

    WRITE_IDMAP("gid_map", getegid());

    return true;
}

static bool makedirs(const char *path, bool do_cache)
{
    char *tmp, *segment;

    if (*path != '/') {
        fprintf(stderr, "fatal: Path '%s' is not absolute.\n", path);
        return false;
    }

    if ((tmp = strdup(path)) == NULL) {
        fprintf(stderr, "strdup of %s: %s\n", path, strerror(errno));
        return false;
    }

    segment = dirname(tmp);

    if (!(segment[0] == '/' && segment[1] == '\0')) {
        if (!makedirs(segment, do_cache)) {
            free(tmp);
            return false;
        }
    }

    if (!do_cache || cache_path(cached_paths, path))
        (void)mkdir(path, 0755);
    free(tmp);
    return true;
}

static char *get_mount_target(const char *path)
{
    size_t pathlen = strlen(path), rootdir_len = strlen(FS_ROOT_DIR);
    char *target;

    if ((target = malloc(rootdir_len + pathlen + 1)) == NULL) {
        perror("malloc mount target");
        return NULL;
    }

    memcpy(target, FS_ROOT_DIR, rootdir_len);
    memcpy(target + rootdir_len, path, pathlen + 1);
    return target;
}

static bool is_regular_file(const char *path)
{
    struct stat st;
    stat(path, &st);
    return S_ISREG(st.st_mode);
}

static bool bind_file(const char *path)
{
    char *target, *tmp;

    if (access(path, R_OK) == -1)
        // Skip missing mount source
        return true;

    if ((target = get_mount_target(path)) == NULL)
        return false;

    if ((tmp = strdup(target)) == NULL) {
        perror("strdup bind file target path");
        free(target);
        return false;
    }

    if (!makedirs(dirname(tmp), true)) {
        free(target);
        free(tmp);
        return false;
    }

    free(tmp);

    if (!cache_path(cached_paths, path)) {
        free(target);
        return true;
    }

    if (creat(target, 0666) == -1) {
        fprintf(stderr, "unable to create %s: %s\n", target, strerror(errno));
        free(target);
        return false;
    }

    if (mount(path, target, "", MS_BIND, NULL) == -1) {
        fprintf(stderr, "mount file %s to %s: %s\n",
                path, target, strerror(errno));
        free(target);
        return false;
    }

    free(target);
    return true;
}

bool bind_mount(const char *path, bool restricted, bool resolve)
{
    int mflags = MS_BIND | MS_REC;
    char src[PATH_MAX], *target;

    if (restricted)
        mflags |= MS_NOSUID | MS_NODEV | MS_NOATIME;

    if (resolve ? realpath(path, src) == NULL : access(path, F_OK) == -1)
        // Skip missing mount source
        return true;

    if (is_regular_file(resolve ? src : path))
        return bind_file(resolve ? src : path);

    if ((target = get_mount_target(resolve ? src : path)) == NULL)
        return false;

    if (!makedirs(target, false)) {
        free(target);
        return false;
    }

    if (!cache_path(cached_paths, resolve ? src : path)) {
        free(target);
        return true;
    }

    if (mount(resolve ? src : path, target, "", mflags, NULL) == -1) {
        fprintf(stderr, "mount %s to %s: %s\n",
                resolve ? src : path, target, strerror(errno));
        free(target);
        return false;
    }

    free(target);
    return true;
}

struct envar_offset {
    int start;
    int length;
    int var_start;
    int var_length;
    struct envar_offset *next;
};

static struct envar_offset *alloc_offset(void)
{
    struct envar_offset *new_offset;
    new_offset = malloc(sizeof(struct envar_offset));

    if (new_offset == NULL) {
        perror("malloc envar_offset");
        return NULL;
    }

    new_offset->next = NULL;
    return new_offset;
}

static struct envar_offset *push_offset(struct envar_offset *current,
                                        struct envar_offset **base)
{
    if (current == NULL) {
        if ((current = alloc_offset()) != NULL)
            *base = current;
        return current;
    }

    return current->next = alloc_offset();
}

static void free_offsets(struct envar_offset *base)
{
    struct envar_offset *next;
    if (base == NULL)
        return;
    next = base->next;
    free(base);
    if (next != NULL)
        free_offsets(next);
}

#define MK_XDG_EXPAND(varname, fallback) \
    if (strcmp(xdg_var, varname) == 0) { \
        result = malloc(homelen + sizeof fallback); \
        if (result == NULL) { \
            perror("malloc " varname); \
            return NULL; \
        } \
        memcpy(result, home, homelen); \
        memcpy(result + homelen, fallback, sizeof fallback); \
        return result; \
    }

static char *expand_xdg_fallback(const char *xdg_var)
{
    static char *home = NULL;
    static size_t homelen;
    char *result;

    if (home == NULL) {
        if ((home = getenv("HOME")) == NULL) {
            fputs("Unable find $HOME.\n", stderr);
            return NULL;
        }
        homelen = strlen(home);
    }

    MK_XDG_EXPAND("XDG_DATA_HOME", "/.local/share");
    MK_XDG_EXPAND("XDG_CONFIG_HOME", "/.config");
    MK_XDG_EXPAND("XDG_CACHE_HOME", "/.cache");

    return NULL;
}

static char *get_offset_var(struct envar_offset *offset, const char *haystack)
{
    char *tmp, *result;

    tmp = strndup(haystack + offset->var_start, offset->var_length);

    if (tmp == NULL) {
        perror("strndup");
        return NULL;
    }

    result = getenv(tmp);
    if (result == NULL) {
        if ((result = expand_xdg_fallback(tmp)) == NULL) {
            fprintf(stderr, "Unable find variable %s in %s\n", tmp, haystack);
            free(tmp);
            return NULL;
        }
        free(tmp);
        return result;
    }
    free(tmp);
    return strdup(result);
}

static char *replace_env_offset_free(const char *path,
                                     struct envar_offset *offset)
{
    struct envar_offset *tmp_offset;
    size_t buflen, pathlen, varlen, tmplen;
    int inpos = 0, outpos = 0;
    char *buf, *curvar;

    buflen = pathlen = strlen(path);

    if ((buf = malloc(buflen + 1)) == NULL) {
        perror("malloc replace_env buffer");
        return NULL;
    }

    while (offset != NULL) {
        if ((curvar = get_offset_var(offset, path)) == NULL) {
            free(buf);
            free_offsets(offset);
            return NULL;
        }

        varlen = strlen(curvar);
        tmplen = varlen + (buflen - offset->length);

        if (tmplen > buflen) {
            if ((buf = realloc(buf, (buflen = tmplen) + 1)) == NULL) {
                perror("realloc replace_env buffer");
                free(buf);
                free(curvar);
                free_offsets(offset);
                return NULL;
            }
        }

        memcpy(buf + outpos, path + inpos, offset->start - inpos);
        outpos += offset->start - inpos;
        inpos = offset->start;

        memcpy(buf + outpos, curvar, varlen);
        outpos += varlen;
        inpos += offset->length;

        free(curvar);

        tmp_offset = offset;
        offset = offset->next;
        free(tmp_offset);
    }

    memcpy(buf + outpos, path + inpos, pathlen - inpos);
    *(buf + outpos + (pathlen - inpos)) = '\0';

    return buf;
}

static char *replace_env(const char *path)
{
    int i = 0, start = 0, var_start = 0;
    size_t pathlen;
    bool in_var = false, curly = false;
    struct envar_offset *base = NULL, *offset = NULL;

    pathlen = strlen(path);

    while (i < pathlen) {
        if (path[i] == '$' && !curly && !in_var) {
            if (i + 1 >= pathlen)
                break;

            start = i;

            if (path[i + 1] == '{') {
                curly = true;
                var_start = i + 2;
                ++i;
            } else {
                in_var = true;
                var_start = i + 1;
            }
        } else if (in_var) {
            if (!(path[i] >= 'a' && path[i] <= 'z') &&
                !(path[i] >= 'A' && path[i] <= 'Z') &&
                !(path[i] >= '0' && path[i] <= '9') &&
                path[i] != '_'
            ) {
                in_var = false;

                if ((offset = push_offset(offset, &base)) == NULL) {
                    free_offsets(base);
                    return NULL;
                }

                offset->start = start;
                offset->length = i - start;
                offset->var_start = var_start;
                offset->var_length = i - var_start;
                continue;
            }
        } else if (curly) {
            if (path[i] == '}') {
                curly = false;

                if ((offset = push_offset(offset, &base)) == NULL) {
                    free_offsets(base);
                    return NULL;
                }

                offset->start = start;
                offset->length = (i + 1) - offset->start;
                offset->var_start = var_start;
                offset->var_length = i - offset->var_start;
            }
        }

        ++i;
    }

    if (in_var) {
        if ((offset = push_offset(offset, &base)) == NULL) {
            free_offsets(base);
            return NULL;
        }

        offset->start = start;
        offset->length = i - start;
        offset->var_start = var_start;
        offset->var_length = i - var_start;
    }

    return replace_env_offset_free(path, base);
}

bool extra_mount(const char *path, bool is_required)
{
    char *expanded;

    if ((expanded = replace_env(path)) == NULL)
        return false;

    if (is_required && !makedirs(expanded, false))
        return false;

    if (!bind_mount(expanded, true, true)) {
        free(expanded);
        return false;
    }

    free(expanded);
    return true;
}

static bool setup_xauthority(void)
{
    char *xauth, *home;
    bool result;
    size_t homelen;

    if ((xauth = getenv("XAUTHORITY")) != NULL)
        return bind_file(xauth);

    if ((home = getenv("HOME")) == NULL) {
        fputs("Unable find $HOME.\n", stderr);
        return false;
    }

    homelen = strlen(home);

    if ((xauth = malloc(homelen + 13)) == NULL) {
        perror("malloc xauth file path");
        return false;
    }

    memcpy(xauth, home, homelen);
    memcpy(xauth + homelen, "/.Xauthority", 13);

    result = bind_file(xauth);
    free(xauth);
    return result;
}

static bool is_dir(const char *path)
{
    struct stat sb;
    if (stat(path, &sb) == -1) {
        fprintf(stderr, "stat %s: %s\n", path, strerror(errno));
        // Default to directory for mounting
        return true;
    }
    return S_ISDIR(sb.st_mode);
}

static bool mount_requisites(struct query_state *qs, const char *path)
{
    const char *requisite;

    if (!query_requisites(qs, path)) {
        fprintf(stderr, "Unable to get requisites for %s.\n", path);
        return false;
    }

    while ((requisite = next_query_result(qs)) != NULL) {
        if (is_dir(requisite)) {
            if (!bind_mount(requisite, true, false))
                return false;
        } else {
            if (!bind_file(requisite))
                return false;
        }
    }

    return true;
}

bool mount_from_path_var(struct query_state *qs, const char *name)
{
    char *buf, *ptr, *value = getenv(name);

    if (value == NULL)
        return true;

    if ((buf = strdup(value)) == NULL) {
        fprintf(stderr, "strdup %s: %s\n", value, strerror(errno));
        return false;
    }

    ptr = strtok(buf, ":");

    while (ptr != NULL) {
        if (!mount_requisites(qs, ptr)) {
            free(buf);
            return false;
        }
        ptr = strtok(NULL, ":");
    }

    free(buf);
    return true;
}

static bool setup_static_etc(struct query_state *qs)
{
    char dest[PATH_MAX];
    ssize_t destlen;

    if ((destlen = readlink("/etc/static", dest, PATH_MAX)) == -1)
        return true;

    if (destlen >= PATH_MAX) {
        fputs("readlink of /etc/static larger than PATH_MAX.\n", stderr);
        return false;
    }

    dest[destlen] = '\0';
    return mount_requisites(qs, dest);
}

static bool setup_runtime_paths(void)
{
    struct query_state *qs;

    if ((qs = new_query()) == NULL) {
        fputs("Unable to allocate Nix query state.\n", stderr);
        return false;
    }

    if (!setup_static_etc(qs)) {
        free_query(qs);
        return false;
    }

    if (!mount_runtime_path_vars(qs)) {
        free_query(qs);
        return false;
    }

    free_query(qs);
    return true;
}

static bool setup_chroot(void)
{
    int mflags;

    mflags = MS_NOEXEC | MS_NOSUID | MS_NODEV | MS_NOATIME;

    if (mount("none", FS_ROOT_DIR, "tmpfs", mflags, NULL) == -1) {
        perror("mount rootfs");
        return false;
    }

    if (!bind_mount("/etc", true, false))
        return false;

    if (!bind_mount("/dev", false, false))
        return false;

    if (!bind_mount("/proc", false, false))
        return false;

    if (!bind_mount("/sys", false, false))
        return false;

    if (!bind_mount("/run", false, false))
        return false;

    if (!bind_mount("/var/run", false, false))
        return false;

    if (!bind_mount("/tmp", true, false))
        return false;

    if (!setup_runtime_paths())
        return false;

    if (!setup_app_paths())
        return false;

    if (!setup_xauthority())
        return false;

    if (chroot(FS_ROOT_DIR) == -1) {
        perror("chroot");
        return false;
    }

    if (chdir("/") == -1) {
        perror("chdir rootfs");
        return false;
    }

    return true;
}

bool setup_sandbox(void)
{
    int sync_pipe[2];
    char sync_status = '.';
    int child_status;
    pid_t pid, parent_pid;

    if (pipe(sync_pipe) == -1) {
        perror("pipe");
        return false;
    }

    parent_pid = getpid();

    switch (pid = fork()) {
        case -1:
            perror("fork");
            return false;
        case 0:
            close(sync_pipe[1]);
            if (read(sync_pipe[0], &sync_status, 1) == -1) {
                perror("read pipe from parent");
                _exit(1);
            } else if (sync_status == 'X')
                _exit(1);
            close(sync_pipe[0]);
            _exit(write_maps(parent_pid) ? 0 : 1);
        default:
            if (unshare(CLONE_NEWNS | CLONE_NEWUSER) == -1) {
                perror("unshare");
                if (write(sync_pipe[1], "X", 1) == -1)
                    perror("signal child exit");
                waitpid(pid, NULL, 0);
                return false;
            }

            close(sync_pipe[1]);
            waitpid(pid, &child_status, 0);
            if (WIFEXITED(child_status) && WEXITSTATUS(child_status) == 0)
                break;
            return false;
    }

    cached_paths = new_path_cache();

    if (!setup_chroot()) {
        free_path_cache(cached_paths);
        return false;
    }

    free_path_cache(cached_paths);
    return true;
}