nish 0.41.0 → 0.44.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.
Files changed (47) hide show
  1. package/README.md +16 -4
  2. package/bin/nish-skills.js +233 -0
  3. package/docs/AI.md +282 -126
  4. package/llms.txt +4 -0
  5. package/package.json +14 -11
  6. package/runtime/core.d.ts +16 -0
  7. package/runtime/core.mjs +5 -0
  8. package/runtime/nish.d.ts +204 -33
  9. package/runtime/nish.h +19 -3
  10. package/runtime/nish.mjs +5 -1
  11. package/runtime/runtime-cross.c +15 -0
  12. package/runtime/runtime-net.c +876 -10
  13. package/runtime/runtime-region.c +21 -0
  14. package/runtime/runtime-sandbox.c +112 -15
  15. package/runtime/runtime-text.c +11 -0
  16. package/runtime/runtime-uring.c +533 -77
  17. package/runtime/runtime-wasm.c +3 -0
  18. package/runtime/runtime.c +474 -5
  19. package/runtime/sqlite/README.md +16 -3
  20. package/runtime/sqlite/nish-sqlite.c +940 -29
  21. package/scripts/build.sh +125 -29
  22. package/scripts/link-skills.mjs +1039 -0
  23. package/scripts/postinstall.mjs +43 -0
  24. package/skills/writing-nish/SKILL.md +144 -0
  25. package/std/README.md +3 -3
  26. package/std/crypto/base64url.ts +102 -1
  27. package/std/crypto/scram.ts +142 -0
  28. package/std/crypto/sha1.ts +99 -41
  29. package/std/host/ring.ts +1 -1
  30. package/std/host/text.ts +1 -1
  31. package/std/net/fetch.ts +9 -1
  32. package/std/net/handler.ts +79 -15
  33. package/std/net/http-server.ts +2 -3
  34. package/std/net/http1-server.ts +294 -51
  35. package/std/net/http3-server.ts +26 -15
  36. package/std/net/http3.ts +2 -2
  37. package/std/net/oidc.ts +1107 -0
  38. package/std/net/postgres-async.ts +64 -33
  39. package/std/net/postgres.ts +65 -76
  40. package/std/net/quic-conn-ack.ts +57 -7
  41. package/std/net/quic-frame.ts +104 -2
  42. package/std/net/quic-listener.ts +9 -2
  43. package/std/net/quic.ts +16 -1
  44. package/std/net/serve.ts +10 -8
  45. package/std/net/websocket.ts +188 -49
  46. package/std/sqlite.ts +325 -0
  47. package/std/text.ts +80 -11
@@ -89,6 +89,14 @@
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
+ #elif defined NISH_ARENA_RANGE && !defined NISH_ASAN
96
+ /* runtime.c's: put a compact-range chunk on its class's free list, or free
97
+ one malloc made. */
98
+ void nish_range_drop(void *c);
99
+ #endif
92
100
 
93
101
  #define NISH_REGION_SLOT_BITS 24
94
102
  #define NISH_REGION_SLOTS ((uint64_t)1 << NISH_REGION_SLOT_BITS)
@@ -380,7 +388,13 @@ void nish_region_adopt(uint64_t r) {
380
388
  took on entry, 0 in an empty region, keeps that chunk for reuse
381
389
  (`nish_reset_arena`), but nothing will bump it again once adopted. */
382
390
  nish_chunk_link *next = head->next;
391
+ #ifdef NISH_ARENA_DEBUG
392
+ if (!(head->cap & NISH_CHUNK_CARVED)) nish_debug_drop(head);
393
+ #elif defined NISH_ARENA_RANGE && !defined NISH_ASAN
394
+ if (!(head->cap & NISH_CHUNK_CARVED)) nish_range_drop(head);
395
+ #else
383
396
  if (!(head->cap & NISH_CHUNK_CARVED)) free(head);
397
+ #endif
384
398
  head = next;
385
399
  uint64_t cap = head ? head->cap & ~(uint64_t)NISH_CHUNK_CARVED : 0;
386
400
  s->arena = head ? (struct nish_arena){ (char *)(head + 1), cap, cap, (void *)head } : (struct nish_arena){ 0 };
@@ -389,6 +403,13 @@ void nish_region_adopt(uint64_t r) {
389
403
  nish_chunk_link *last = head;
390
404
  while (last->next) last = last->next;
391
405
  struct nish_arena caller = nish_arena;
406
+ #ifdef NISH_ARENA_RANGE
407
+ /* A compact class's 4-byte bump may leave `off` 4 past an 8-byte
408
+ boundary (runtime.c's `nish_alloc_struct`): the carved chunk's header,
409
+ and every block bumped from it, start at the next one. */
410
+ caller.off = (caller.off + 7) & ~(uint64_t)7;
411
+ if (caller.off > caller.cap) caller.off = caller.cap;
412
+ #endif
392
413
  uint64_t left = caller.buf ? caller.cap - caller.off : 0;
393
414
  last->next = caller.chunks;
394
415
  nish_arena = s->arena;
@@ -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. the seccomp filter of the note's section 4, a denylist;
47
- * 6. set the flag `nish_sandbox_check` reads at the top of `main`, so a C
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 allocate or
65
- * touch a `FILE` before libc has finished its own start-up. */
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 6 and read by `nish_sandbox_check`. */
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 lives. */
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 5: a denylist (open question 4), each row of the note's section 4. */
432
- static void nish_sb_seccomp(int32_t caps) {
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[96];
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
- if ((caps & (CAP_BIT_NET | CAP_BIT_SPAWN)) != (CAP_BIT_NET | CAP_BIT_SPAWN))
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
 
@@ -70,11 +70,22 @@ extern NISH_TEXT_TLS struct nish_arena nish_arena;
70
70
  void *nish_arena_grow(uint64_t size);
71
71
  static inline void *nish_text_alloc(uint64_t size) {
72
72
  size = (size + 7) & ~(uint64_t)7;
73
+ #ifdef NISH_ARENA_RANGE
74
+ /* runtime.c's `nish_alloc_struct` gives the reason: `off` may be 4 bytes
75
+ past an 8-byte boundary once a compact class has been bumped. */
76
+ uint64_t at = (nish_arena.off + 7) & ~(uint64_t)7;
77
+ if (at + size <= nish_arena.cap) {
78
+ void *p = nish_arena.buf + at;
79
+ nish_arena.off = at + size;
80
+ return p;
81
+ }
82
+ #else
73
83
  if (nish_arena.off + size <= nish_arena.cap) {
74
84
  void *p = nish_arena.buf + nish_arena.off;
75
85
  nish_arena.off += size;
76
86
  return p;
77
87
  }
88
+ #endif
78
89
  return nish_arena_grow(size);
79
90
  }
80
91
  #else