@amritk/nish-x86_64-linux 0.41.0 → 0.42.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/nish +0 -0
- package/package.json +2 -2
- package/runtime/nish.d.ts +84 -14
- package/runtime/nish.h +19 -3
- package/runtime/nish.mjs +2 -0
- package/runtime/runtime-cross.c +6 -0
- package/runtime/runtime-net.c +837 -9
- package/runtime/runtime-region.c +8 -0
- package/runtime/runtime-sandbox.c +112 -15
- package/runtime/runtime-uring.c +454 -56
- package/runtime/runtime.c +148 -2
- package/scripts/build.sh +14 -0
- package/std/net/fetch.ts +9 -1
- package/std/net/handler.ts +78 -14
- package/std/net/http1-server.ts +13 -0
- package/std/net/oidc.ts +1107 -0
package/runtime/runtime-region.c
CHANGED
|
@@ -89,6 +89,10 @@
|
|
|
89
89
|
/* runtime.c's: the size of the next chunk `nish_arena_grow` makes for the
|
|
90
90
|
arena in `nish_arena`, or 0 for a thread's own, which takes 64 KiB. */
|
|
91
91
|
extern NISH_TLS uint64_t nish_region_chunk;
|
|
92
|
+
#ifdef NISH_ARENA_DEBUG
|
|
93
|
+
/* runtime.c's: retire a debug arena chunk, or free one C code malloc'd. */
|
|
94
|
+
void nish_debug_drop(void *c);
|
|
95
|
+
#endif
|
|
92
96
|
|
|
93
97
|
#define NISH_REGION_SLOT_BITS 24
|
|
94
98
|
#define NISH_REGION_SLOTS ((uint64_t)1 << NISH_REGION_SLOT_BITS)
|
|
@@ -380,7 +384,11 @@ void nish_region_adopt(uint64_t r) {
|
|
|
380
384
|
took on entry, 0 in an empty region, keeps that chunk for reuse
|
|
381
385
|
(`nish_reset_arena`), but nothing will bump it again once adopted. */
|
|
382
386
|
nish_chunk_link *next = head->next;
|
|
387
|
+
#ifdef NISH_ARENA_DEBUG
|
|
388
|
+
if (!(head->cap & NISH_CHUNK_CARVED)) nish_debug_drop(head);
|
|
389
|
+
#else
|
|
383
390
|
if (!(head->cap & NISH_CHUNK_CARVED)) free(head);
|
|
391
|
+
#endif
|
|
384
392
|
head = next;
|
|
385
393
|
uint64_t cap = head ? head->cap & ~(uint64_t)NISH_CHUNK_CARVED : 0;
|
|
386
394
|
s->arena = head ? (struct nish_arena){ (char *)(head + 1), cap, cap, (void *)head } : (struct nish_arena){ 0 };
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* before any constructor in the executable, an `ffi` object or a shared
|
|
13
13
|
* library:
|
|
14
14
|
*
|
|
15
|
-
* nish_sandbox(caps, mode, read_dirs, write_dirs, exec_paths)
|
|
15
|
+
* nish_sandbox(caps, mode, read_dirs, write_dirs, exec_paths, net_rings)
|
|
16
16
|
*
|
|
17
17
|
* `caps` is `main`'s capability mask (src/capabilities.ts numbers the bits),
|
|
18
18
|
* `mode` is 1 for `--sandbox` and 2 for `--sandbox=best-effort`. The two
|
|
@@ -31,6 +31,10 @@
|
|
|
31
31
|
* child's whole subtree to the one exec ("One exec per spawn" below). A
|
|
32
32
|
* program granted `process.spawn=exec:<program>` is written `///<path>`, and
|
|
33
33
|
* its identity is recorded too: its child is spared the one exec.
|
|
34
|
+
* `net_rings` is how many restricted io_uring rings to make before the
|
|
35
|
+
* filter loads (WP63 S3): the root package.json's `nish.netRings`, 1 to 64,
|
|
36
|
+
* when `main` reaches `tcpListenFlags` with `NET_LISTEN_RING` and `net` is
|
|
37
|
+
* granted, and 0 otherwise.
|
|
34
38
|
*
|
|
35
39
|
* What it does, in the order of the note's section 3, each step before the
|
|
36
40
|
* next because each needs the one before it:
|
|
@@ -43,8 +47,14 @@
|
|
|
43
47
|
* 4. Landlock: handle every filesystem right the running ABI knows, then
|
|
44
48
|
* grant back beneath `/` exactly what the mask grants whole, and beneath
|
|
45
49
|
* each scope directory what its capability grants;
|
|
46
|
-
* 5.
|
|
47
|
-
*
|
|
50
|
+
* 5. unless `net` and `process.spawn` are both granted, make the
|
|
51
|
+
* `net_rings` rings runtime-uring.c's set will take, each restricted to
|
|
52
|
+
* a listener's opcodes and flags before it is enabled, or tell the ring
|
|
53
|
+
* layer it may make none (docs/security/net-uring.md);
|
|
54
|
+
* 6. the seccomp filter of the note's section 4, a denylist, which then
|
|
55
|
+
* kills io_uring_setup and io_uring_register unless both are granted,
|
|
56
|
+
* and io_uring_enter on any descriptor but step 5's rings;
|
|
57
|
+
* 7. set the flag `nish_sandbox_check` reads at the top of `main`, so a C
|
|
48
58
|
* library that never ran `.preinit_array` (musl) refuses to run rather
|
|
49
59
|
* than running unconfined.
|
|
50
60
|
*
|
|
@@ -61,8 +71,10 @@
|
|
|
61
71
|
* `-DNISH_SANDBOX_SIMULATE_ABI=<n>`, makes the ABI probe answer `n` in a
|
|
62
72
|
* test-only build of this file; scripts/build.sh never passes it.
|
|
63
73
|
*
|
|
64
|
-
* Messages go through write(2) rather than stdio: nothing here may
|
|
65
|
-
*
|
|
74
|
+
* Messages go through write(2) rather than stdio: nothing here may touch a
|
|
75
|
+
* `FILE` before libc has finished its own start-up. Step 5 is the one
|
|
76
|
+
* allocation: a ring's slot tables are calloc'd, which glibc serves in
|
|
77
|
+
* `.preinit_array`, and musl, the C library that might not, never runs it. */
|
|
66
78
|
#define _GNU_SOURCE
|
|
67
79
|
#include "nish.h"
|
|
68
80
|
|
|
@@ -168,6 +180,9 @@ static const uint16_t nish_sb_net[] = {41, 53, 42, 49, 50, 43, 288}; /* socket .
|
|
|
168
180
|
#define NISH_SB_EXECVEAT 322
|
|
169
181
|
static const uint16_t nish_sb_spawn[] = {NISH_SB_EXECVE, NISH_SB_EXECVEAT, 57, 58}; /* execve, execveat, fork, vfork */
|
|
170
182
|
#define NISH_SB_CLONE 56
|
|
183
|
+
#define NISH_SB_CLOSE 3
|
|
184
|
+
#define NISH_SB_DUP2 33
|
|
185
|
+
#define NISH_SB_DUP3 292
|
|
171
186
|
static const uint16_t nish_sb_meta[] = {
|
|
172
187
|
90, 91, 268, 452, /* chmod, fchmod, fchmodat, fchmodat2 */
|
|
173
188
|
92, 93, 94, 260, /* chown, fchown, lchown, fchownat */
|
|
@@ -183,6 +198,8 @@ static const uint16_t nish_sb_net[] = {198, 199, 203, 200, 201, 202, 242};
|
|
|
183
198
|
#define NISH_SB_EXECVEAT 281
|
|
184
199
|
static const uint16_t nish_sb_spawn[] = {NISH_SB_EXECVE, NISH_SB_EXECVEAT};
|
|
185
200
|
#define NISH_SB_CLONE 220
|
|
201
|
+
#define NISH_SB_CLOSE 57
|
|
202
|
+
#define NISH_SB_DUP3 24
|
|
186
203
|
static const uint16_t nish_sb_meta[] = {
|
|
187
204
|
52, 53, 452, /* fchmod, fchmodat, fchmodat2 */
|
|
188
205
|
55, 54, /* fchown, fchownat */
|
|
@@ -192,9 +209,18 @@ static const uint16_t nish_sb_meta[] = {
|
|
|
192
209
|
};
|
|
193
210
|
#endif
|
|
194
211
|
static const uint16_t nish_sb_uring[] = {425, 426, 427}; /* io_uring_setup, _enter, _register */
|
|
212
|
+
/* With `net` and without `process.spawn` only the rings step 5 made before
|
|
213
|
+
* the filter are left: no ring may be made or registered (WP63 S3), and
|
|
214
|
+
* `io_uring_enter` is allowed on those rings' descriptors alone. */
|
|
215
|
+
static const uint16_t nish_sb_uring_make[] = {425, 427}; /* io_uring_setup, _register */
|
|
216
|
+
#define NISH_SB_ENTER 426
|
|
195
217
|
#define NISH_SB_CLONE3 435
|
|
218
|
+
/* `close_range`, which would free the premade rings' numbers in bulk
|
|
219
|
+
* (NURING-36); `close`, `dup2` and `dup3` are above, and AArch64 has no
|
|
220
|
+
* `dup2`. */
|
|
221
|
+
static const uint16_t nish_sb_close_range[] = {436};
|
|
196
222
|
|
|
197
|
-
/* Set by step
|
|
223
|
+
/* Set by step 7 and read by `nish_sandbox_check`. */
|
|
198
224
|
static int nish_sb_applied = 0;
|
|
199
225
|
|
|
200
226
|
/* The errno a failure prints, as the line's parenthesized tail: its name, or
|
|
@@ -403,13 +429,47 @@ static int nish_sb_rows(struct sock_filter *prog, int at, const uint16_t *nrs, i
|
|
|
403
429
|
|
|
404
430
|
#define NISH_SB_ROWS(prog, at, nrs, action) nish_sb_rows(prog, at, nrs, (int)(sizeof nrs / sizeof nrs[0]), action)
|
|
405
431
|
|
|
406
|
-
/* The low word of `clone`'s flags, where CLONE_THREAD
|
|
432
|
+
/* The low word of the first argument: `clone`'s flags, where CLONE_THREAD
|
|
433
|
+
* lives, `io_uring_enter`'s and `close`'s descriptor and `dup3`'s source;
|
|
434
|
+
* and of the second: `dup2`'s and `dup3`'s target. */
|
|
407
435
|
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
|
408
436
|
#define NISH_SB_ARG0_LOW (offsetof(struct seccomp_data, args) + 4)
|
|
437
|
+
#define NISH_SB_ARG1_LOW (offsetof(struct seccomp_data, args) + 12)
|
|
409
438
|
#else
|
|
410
439
|
#define NISH_SB_ARG0_LOW offsetof(struct seccomp_data, args)
|
|
440
|
+
#define NISH_SB_ARG1_LOW (offsetof(struct seccomp_data, args) + 8)
|
|
411
441
|
#endif
|
|
412
442
|
|
|
443
|
+
/* Writes one instruction at `at` and answers the next index: the filter's
|
|
444
|
+
* argument blocks are built from it rather than from inline initialisers,
|
|
445
|
+
* which -Oz would otherwise expand once per instruction. */
|
|
446
|
+
static int nish_sb_put(struct sock_filter *prog, int at, uint16_t code, uint32_t k, int jt, int jf) {
|
|
447
|
+
prog[at] = (struct sock_filter){code, (uint8_t)jt, (uint8_t)jf, k};
|
|
448
|
+
return at + 1;
|
|
449
|
+
}
|
|
450
|
+
#define NISH_SB_LD(arg) BPF_LD | BPF_W | BPF_ABS, (arg)
|
|
451
|
+
#define NISH_SB_JEQ BPF_JMP | BPF_JEQ | BPF_K
|
|
452
|
+
#define NISH_SB_RET BPF_RET | BPF_K
|
|
453
|
+
|
|
454
|
+
/* Appends a block that answers `hit` when call `nr`'s argument word at `arg`
|
|
455
|
+
* is one of the `n` numbers in `fds`, and `miss` when it is none, or when
|
|
456
|
+
* `from` is not -1 and the first argument is `from`. Another call skips the
|
|
457
|
+
* block with its number still loaded; this one returns on both paths, so the
|
|
458
|
+
* argument loaded over the number is never compared with a later row. */
|
|
459
|
+
static int nish_sb_fd_row(struct sock_filter *prog, int at, uint32_t nr, uint32_t arg, const int32_t *fds, int n,
|
|
460
|
+
uint32_t hit, uint32_t miss, int32_t from) {
|
|
461
|
+
int pre = from < 0 ? 0 : 2;
|
|
462
|
+
at = nish_sb_put(prog, at, NISH_SB_JEQ, nr, 0, n + 3 + pre);
|
|
463
|
+
if (pre) {
|
|
464
|
+
at = nish_sb_put(prog, at, NISH_SB_LD(NISH_SB_ARG0_LOW), 0, 0);
|
|
465
|
+
at = nish_sb_put(prog, at, NISH_SB_JEQ, (uint32_t)from, n + 1, 0);
|
|
466
|
+
}
|
|
467
|
+
at = nish_sb_put(prog, at, NISH_SB_LD(arg), 0, 0);
|
|
468
|
+
for (int i = 0; i < n; i++) at = nish_sb_put(prog, at, NISH_SB_JEQ, (uint32_t)fds[i], n - i, 0);
|
|
469
|
+
at = nish_sb_put(prog, at, NISH_SB_RET, miss, 0, 0);
|
|
470
|
+
return nish_sb_put(prog, at, NISH_SB_RET, hit, 0, 0);
|
|
471
|
+
}
|
|
472
|
+
|
|
413
473
|
/* How every filter here starts, leaving the syscall number loaded. The arch
|
|
414
474
|
* check comes first and kills on a mismatch, so a 32-bit entry cannot step
|
|
415
475
|
* around the 64-bit numbers; on x86-64 the next instruction kills any number
|
|
@@ -428,20 +488,47 @@ static int nish_sb_rows(struct sock_filter *prog, int at, const uint16_t *nrs, i
|
|
|
428
488
|
BPF_STMT(BPF_RET | BPF_K, KILL), \
|
|
429
489
|
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)) NISH_SB_X32
|
|
430
490
|
|
|
431
|
-
/* Step
|
|
432
|
-
|
|
491
|
+
/* Step 6: a denylist (open question 4), each row of the note's section 4.
|
|
492
|
+
* `rings` holds the descriptors of the `nrings` rings step 5 made, then the
|
|
493
|
+
* keeper's, the ring the runtime drops a freed one with. */
|
|
494
|
+
static void nish_sb_seccomp(int32_t caps, const int32_t *rings, int nrings) {
|
|
433
495
|
uint32_t action = SECCOMP_RET_KILL_PROCESS;
|
|
434
496
|
if (syscall(SYS_seccomp, SECCOMP_GET_ACTION_AVAIL, 0, &action) != 0)
|
|
435
497
|
nish_sb_fail("this kernel has no seccomp filter that kills the process");
|
|
436
498
|
static const struct sock_filter head[] = {NISH_SB_HEAD};
|
|
437
|
-
struct sock_filter prog[
|
|
499
|
+
struct sock_filter prog[512];
|
|
438
500
|
int at = sizeof head / sizeof head[0];
|
|
439
501
|
memcpy(prog, head, sizeof head);
|
|
440
502
|
at = NISH_SB_ROWS(prog, at, nish_sb_always, KILL);
|
|
441
503
|
if (!(caps & CAP_BIT_NET)) at = NISH_SB_ROWS(prog, at, nish_sb_net, KILL);
|
|
442
|
-
/* io_uring opens, connects and accepts sockets without those calls.
|
|
443
|
-
|
|
504
|
+
/* io_uring opens, connects and accepts sockets without those calls. With
|
|
505
|
+
* `net`, a ring made before the filter is restricted to accept, recv,
|
|
506
|
+
* send, close, cancel and nop, so only entering one stays allowed: by its
|
|
507
|
+
* descriptor, the kernel's `unsigned int`, so a ring made elsewhere and
|
|
508
|
+
* received over a socket is killed too. With no ring made, entering is
|
|
509
|
+
* killed outright. */
|
|
510
|
+
if (!(caps & CAP_BIT_NET) || (!(caps & CAP_BIT_SPAWN) && nrings <= 0))
|
|
444
511
|
at = NISH_SB_ROWS(prog, at, nish_sb_uring, KILL);
|
|
512
|
+
else if (!(caps & CAP_BIT_SPAWN)) {
|
|
513
|
+
at = NISH_SB_ROWS(prog, at, nish_sb_uring_make, KILL);
|
|
514
|
+
at = nish_sb_fd_row(prog, at, NISH_SB_ENTER, NISH_SB_ARG0_LOW, rings, nrings, SECCOMP_RET_ALLOW, KILL, -1);
|
|
515
|
+
/* A filter by number holds only while each number stays the ring it was
|
|
516
|
+
* (NURING-36): a freed one would take the next descriptor made, which may
|
|
517
|
+
* be a ring from elsewhere. So `close` and `dup2` kill on a ring's or the
|
|
518
|
+
* keeper's number, and `dup3` onto one kills unless it is from the
|
|
519
|
+
* keeper, which is how the runtime drops a ring and leaves a ring that
|
|
520
|
+
* takes only NOP in its place. `close_range` answers ENOSYS, as before
|
|
521
|
+
* 5.9, so no range close passes a ring's number by the `close` row. `dup`,
|
|
522
|
+
* `F_DUPFD`, `recvmsg` and `pidfd_getfd` take a free number only, and
|
|
523
|
+
* exec is killed without `process.spawn`. */
|
|
524
|
+
int nfds = nrings + 1;
|
|
525
|
+
at = nish_sb_fd_row(prog, at, NISH_SB_CLOSE, NISH_SB_ARG0_LOW, rings, nfds, KILL, SECCOMP_RET_ALLOW, -1);
|
|
526
|
+
#ifdef NISH_SB_DUP2
|
|
527
|
+
at = nish_sb_fd_row(prog, at, NISH_SB_DUP2, NISH_SB_ARG1_LOW, rings, nfds, KILL, SECCOMP_RET_ALLOW, -1);
|
|
528
|
+
#endif
|
|
529
|
+
at = nish_sb_fd_row(prog, at, NISH_SB_DUP3, NISH_SB_ARG1_LOW, rings, nfds, KILL, SECCOMP_RET_ALLOW, rings[nrings]);
|
|
530
|
+
at = NISH_SB_ROWS(prog, at, nish_sb_close_range, ENOSYS_RET);
|
|
531
|
+
}
|
|
445
532
|
if (!(caps & CAP_BIT_FS_WRITE)) at = NISH_SB_ROWS(prog, at, nish_sb_meta, EPERM_RET);
|
|
446
533
|
if (!(caps & CAP_BIT_SPAWN)) {
|
|
447
534
|
at = NISH_SB_ROWS(prog, at, nish_sb_spawn, KILL);
|
|
@@ -822,15 +909,23 @@ static void nish_sb_keep_marks(const char *paths) {
|
|
|
822
909
|
}
|
|
823
910
|
}
|
|
824
911
|
|
|
825
|
-
void nish_sandbox(int32_t caps, int32_t mode, const char *read_dirs, const char *write_dirs, const char *exec_paths
|
|
912
|
+
void nish_sandbox(int32_t caps, int32_t mode, const char *read_dirs, const char *write_dirs, const char *exec_paths,
|
|
913
|
+
int32_t net_rings) {
|
|
826
914
|
nish_cpu_prime();
|
|
827
915
|
nish_sb_keep_marks(exec_paths);
|
|
828
916
|
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) nish_sb_fail("cannot set no_new_privs");
|
|
829
917
|
nish_sb_drop_capabilities();
|
|
830
918
|
nish_sb_landlock(caps, mode, read_dirs, write_dirs, exec_paths);
|
|
919
|
+
/* The filter kills io_uring_setup unless `net` and `process.spawn` are
|
|
920
|
+
* both granted, so the rings a ring listener takes are made now, or the
|
|
921
|
+
* ring layer is told it may make none (WP63 S3). */
|
|
922
|
+
int32_t rings[65];
|
|
923
|
+
int32_t nrings = 0;
|
|
924
|
+
if ((caps & (CAP_BIT_NET | CAP_BIT_SPAWN)) != (CAP_BIT_NET | CAP_BIT_SPAWN))
|
|
925
|
+
nrings = nish_net_ring_premake(caps & CAP_BIT_NET ? net_rings : 0, rings);
|
|
831
926
|
/* Metadata writes stay EPERM inside a scoped write directory (open
|
|
832
927
|
* question 1): Landlock has no right for chmod, chown or xattrs. */
|
|
833
|
-
nish_sb_seccomp(write_dirs ? caps & ~CAP_BIT_FS_WRITE : caps);
|
|
928
|
+
nish_sb_seccomp(write_dirs ? caps & ~CAP_BIT_FS_WRITE : caps, rings, nrings);
|
|
834
929
|
__atomic_store_n(&nish_sb_applied, 1, __ATOMIC_RELEASE);
|
|
835
930
|
}
|
|
836
931
|
|
|
@@ -852,12 +947,14 @@ static void nish_sb_none(void) {
|
|
|
852
947
|
_exit(126);
|
|
853
948
|
}
|
|
854
949
|
|
|
855
|
-
void nish_sandbox(int32_t caps, int32_t mode, const char *read_dirs, const char *write_dirs, const char *exec_paths
|
|
950
|
+
void nish_sandbox(int32_t caps, int32_t mode, const char *read_dirs, const char *write_dirs, const char *exec_paths,
|
|
951
|
+
int32_t net_rings) {
|
|
856
952
|
(void)caps;
|
|
857
953
|
(void)mode;
|
|
858
954
|
(void)read_dirs;
|
|
859
955
|
(void)write_dirs;
|
|
860
956
|
(void)exec_paths;
|
|
957
|
+
(void)net_rings;
|
|
861
958
|
nish_sb_none();
|
|
862
959
|
}
|
|
863
960
|
|