nish 0.28.0 → 0.29.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.
@@ -358,19 +358,23 @@ _Bool nish_mkdir(const nish_str *path) {
358
358
  value rather than through a child that has already run.
359
359
 
360
360
  `out` and `err` are paths for the child's stdout and stderr, or NULL to let
361
- it inherit this process's; `dup`, when not NULL, is three descriptors the
362
- child takes as its stdin, stdout and stderr instead (`spawnPiped`'s pipes). Every spawn builtin starts its child here, so the
363
- argv vector and the redirects are written once; it is `static`, so a program
361
+ it inherit this process's; `dup`, when not NULL, is `n` descriptors the
362
+ child takes as `base`, `base + 1`, ...: its stdin, stdout and stderr for
363
+ `spawnPiped`'s pipes, and 3 onward for `spawnWithFds`. Every spawn builtin
364
+ starts its child here, so the argv vector and the redirects are written once; it is `static`, so a program
364
365
  that spawns nothing loses it to `--gc-sections`. The answer is the child's
365
366
  pid, or a negative errno: -EINVAL for an empty argv or an argument holding a
366
367
  NUL, and `posix_spawnp`'s own error otherwise (-ENOENT for a command that is
367
368
  not on PATH, on glibc). */
368
- static int32_t nish_spawn_start(const nish_array *argv, const nish_str *out, const nish_str *err, const int *dup) {
369
+ static int32_t nish_spawn_start(const nish_array *argv, const nish_str *out, const nish_str *err, const int *dup,
370
+ int base, int n) {
369
371
  #ifdef __wasi__
370
372
  (void)argv;
371
373
  (void)out;
372
374
  (void)err;
373
375
  (void)dup;
376
+ (void)base;
377
+ (void)n;
374
378
  return -ENOSYS;
375
379
  #else
376
380
  if (!argv->len) return -EINVAL; /* argv[0] would read past an empty array */
@@ -395,7 +399,7 @@ static int32_t nish_spawn_start(const nish_array *argv, const nish_str *out, con
395
399
  if (failed) return -failed;
396
400
  /* A `dup2` action that could not be recorded would start the child on this
397
401
  process's own stream in its place, so it fails the spawn. */
398
- for (int k = 0; dup && k < 3 && !failed; k++) failed = posix_spawn_file_actions_adddup2(&fa, dup[k], k);
402
+ for (int k = 0; dup && k < n && !failed; k++) failed = posix_spawn_file_actions_adddup2(&fa, dup[k], base + k);
399
403
  if (failed) {
400
404
  posix_spawn_file_actions_destroy(&fa);
401
405
  return -failed;
@@ -425,7 +429,7 @@ static int32_t nish_exit_code(int status) {
425
429
  /* The two blocking builtins: start, then wait for that one child. A failed
426
430
  start is -1, the answer they have always given. */
427
431
  static int32_t nish_spawn_impl(const nish_array *argv, const nish_str *out, const nish_str *err) {
428
- int32_t pid = nish_spawn_start(argv, out, err, 0);
432
+ int32_t pid = nish_spawn_start(argv, out, err, 0, 0, 0);
429
433
  if (pid < 0) return -1;
430
434
  #ifdef __wasi__
431
435
  return -1;
@@ -456,7 +460,54 @@ int32_t nish_spawn_to(const nish_array *argv, const nish_str *out, const nish_st
456
460
  /* `spawn(argv)`: the child's pid, or a negative errno, without waiting. The
457
461
  child is this process's until a `waitPid` reaps it; one never reaped stays
458
462
  a zombie until the program exits, as it would in C. */
459
- int32_t nish_spawn_child(const nish_array *argv) { return nish_spawn_start(argv, 0, 0, 0); }
463
+ int32_t nish_spawn_child(const nish_array *argv) { return nish_spawn_start(argv, 0, 0, 0, 0, 0); }
464
+
465
+ /* The most descriptors `spawnWithFds` hands on in one call. */
466
+ #define NISH_SPAWN_FDS 64
467
+
468
+ /* `spawnWithFds(argv, fds)`: `spawn`, with the child holding `fds[i]` as
469
+ descriptor `3 + i`, close-on-exec cleared there, and its pid or a negative
470
+ errno back. More than NISH_SPAWN_FDS descriptors, or a negative one, is
471
+ -EINVAL before anything is opened or started. Each source is first
472
+ duplicated, close-on-exec, to a scratch descriptor at or above `3 + n`, and
473
+ the child's `dup2` actions read only the scratch ones: a source that is
474
+ already its own target (`fds[0] == 3`) is then a `dup2` between two
475
+ different descriptors, which clears close-on-exec on every libc where a
476
+ `dup2` onto itself leaves it set, and a source that is an earlier entry's
477
+ target (`fds = [4, 3]`) is not overwritten before it is read. A source that
478
+ is not open is the `F_DUPFD_CLOEXEC`'s -EBADF. The scratch descriptors are
479
+ closed on every path out, so this process holds what it held before; every
480
+ descriptor of its own stays as it was, so the child also inherits whatever
481
+ this process holds without close-on-exec, which nothing the runtime opens
482
+ is (docs/security/runtime.md, RT-17, and RT-40 for what this call hands
483
+ on). The child shares each descriptor's open file: its offset and its
484
+ status flags, `O_NONBLOCK` among them. */
485
+ int32_t nish_spawn_with_fds(const nish_array *argv, const nish_array *fds) {
486
+ #ifdef __wasi__
487
+ (void)argv;
488
+ (void)fds;
489
+ return -ENOSYS;
490
+ #else
491
+ const int32_t *src = (const int32_t *)fds->data;
492
+ int n = (int)fds->len;
493
+ if (fds->len > NISH_SPAWN_FDS) return -EINVAL;
494
+ for (int k = 0; k < n; k++)
495
+ if (src[k] < 0) return -EINVAL;
496
+ int scratch[NISH_SPAWN_FDS];
497
+ int32_t rc = 0;
498
+ int made = 0;
499
+ for (; made < n; made++) {
500
+ scratch[made] = fcntl(src[made], F_DUPFD_CLOEXEC, 3 + n);
501
+ if (scratch[made] < 0) {
502
+ rc = -errno;
503
+ break;
504
+ }
505
+ }
506
+ if (!rc) rc = nish_spawn_start(argv, 0, 0, scratch, 3, n);
507
+ while (made-- > 0) close(scratch[made]);
508
+ return rc;
509
+ #endif
510
+ }
460
511
 
461
512
  /* `spawnPiped(argv, stdin, stdout, stderr)`: run the child as `nish_spawn`
462
513
  does, with `stdin`'s bytes on its standard input and its standard output
@@ -525,7 +576,7 @@ int32_t nish_spawn_piped(const nish_array *argv, const nish_array *in, nish_arra
525
576
  int32_t rc = -1;
526
577
  if (nish_pipe(p) || nish_pipe(p + 2) || nish_pipe(p + 4)) goto done;
527
578
  int child[3] = { p[0], p[3], p[5] };
528
- int32_t pid = nish_spawn_start(argv, 0, 0, child);
579
+ int32_t pid = nish_spawn_start(argv, 0, 0, child, 0, 3);
529
580
  /* The child holds its ends now; this process keeps only its own. */
530
581
  for (int k = 0; k < 3; k++) {
531
582
  close(child[k]);
@@ -41,6 +41,7 @@
41
41
 
42
42
  #if defined(NISH_THREADS) && !defined(__wasi__) && !defined(__wasm__)
43
43
  #define NISH_PAR_REAL 1
44
+ #include <errno.h>
44
45
  #include <pthread.h>
45
46
  #include <sched.h>
46
47
  #ifdef __GLIBC__
@@ -360,6 +361,51 @@ static void *nish_job_thread(void *p) {
360
361
  return 0;
361
362
  }
362
363
 
364
+ /* The end of the program when a thread with the stack a scope asked for
365
+ * cannot be started: running the task on a smaller one would trade the panic
366
+ * for an overflow nobody can read. `limit` is pthread's EAGAIN, the system out
367
+ * of threads or of memory for the stack; anything else is the size refused. */
368
+ __attribute__((noinline, cold)) static void nish_stack_fail(int limit) {
369
+ static const char size[] = "nish: cannot start a thread with the stack scopeWithStack asked for\n";
370
+ static const char room[] =
371
+ "nish: cannot start a thread for scopeWithStack: out of threads or of memory for its stack\n";
372
+ nish_panic_claim();
373
+ if (limit)
374
+ (void)!write(2, room, sizeof room - 1);
375
+ else
376
+ (void)!write(2, size, sizeof size - 1);
377
+ nish_fatal_exit();
378
+ }
379
+
380
+ /* Runs every job of the list `jobs` on a thread of its own with a stack of
381
+ * `stack` bytes, as the unpooled path does, and returns once every one has
382
+ * finished; the calling thread runs none of them, since its own stack is the
383
+ * one that was too small. The size is rounded up to the page and to the
384
+ * platform's minimum, a page is added for what the C library keeps at the top
385
+ * of a thread's stack, and a guard page sits below it, so a task that
386
+ * overflows faults. A job runs at depth 1, as on a worker, so anything it
387
+ * divides runs inline. Out of line and cold, so `nish_scope_join` keeps the
388
+ * body it had before the option, plus one load and a branch. */
389
+ __attribute__((noinline, cold)) static void nish_jobs_run_sized(nish_job *jobs, int64_t stack) {
390
+ long sys = sysconf(_SC_PAGESIZE);
391
+ size_t page = sys > 0 ? (size_t)sys : 4096;
392
+ size_t size = ((size_t)stack + page - 1) / page * page;
393
+ /* sysconf, not PTHREAD_STACK_MIN: a strict -std=c11 build hides the macro. */
394
+ long min = sysconf(_SC_THREAD_STACK_MIN);
395
+ if (min > 0 && size < (size_t)min) size = (size_t)min;
396
+ pthread_attr_t attr;
397
+ int rc = pthread_attr_init(&attr);
398
+ if (rc != 0) nish_stack_fail(rc == EAGAIN || rc == ENOMEM);
399
+ if (pthread_attr_setguardsize(&attr, page) != 0 || pthread_attr_setstacksize(&attr, size + page) != 0)
400
+ nish_stack_fail(0);
401
+ for (nish_job *j = jobs; j; j = j->next) {
402
+ rc = pthread_create(&j->th, &attr, nish_job_thread, j);
403
+ if (rc != 0) nish_stack_fail(rc == EAGAIN);
404
+ }
405
+ pthread_attr_destroy(&attr);
406
+ for (nish_job *j = jobs; j; j = j->next) pthread_join(j->th, 0);
407
+ }
408
+
363
409
  /* Runs `lead` on this thread and the list `rest` elsewhere, oldest first, and
364
410
  * returns once every one of them has finished. The caller runs `lead` itself,
365
411
  * so N jobs keep N-1 other threads busy and this one is not idle, and then
@@ -608,11 +654,20 @@ void nish_scope_join(void *scope) {
608
654
  }
609
655
  if (!mine) return;
610
656
  #ifdef NISH_PAR_REAL
611
- /* The first task on this thread and the rest on the pool, in the order they
612
- * were spawned, which is the order a task no worker is free for runs in here,
613
- * and the order every task runs in when the scope is joined inside another
614
- * job (`nish_jobs_run`). */
615
- nish_jobs_run(&mine->job, mine->job.next);
657
+ /* The stack `scopeWithStack` asked for, `ThreadScope`'s first and only field
658
+ * (std/threads.ts), or 0 for `scope()`. A scope with one runs every task on
659
+ * a thread of its own that has it; the checker refuses a scope inside a
660
+ * task, so this thread is never one of those. */
661
+ int32_t stack = *(const int32_t *)scope;
662
+ if (__builtin_expect(stack > 0, 0)) {
663
+ nish_jobs_run_sized(&mine->job, stack);
664
+ } else {
665
+ /* The first task on this thread and the rest on the pool, in the order
666
+ * they were spawned, which is the order a task no worker is free for runs
667
+ * in here, and the order every task runs in when the scope is joined
668
+ * inside another job (`nish_jobs_run`). */
669
+ nish_jobs_run(&mine->job, mine->job.next);
670
+ }
616
671
  #else
617
672
  for (nish_task *t = mine; t; t = t->next) t->run(t->payload);
618
673
  #endif
@@ -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)
15
+ * nish_sandbox(caps, mode, read_dirs, write_dirs, exec_paths)
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
@@ -20,6 +20,9 @@
20
20
  * scopes, each a run of NUL-terminated absolute paths ended by an empty one,
21
21
  * or null when the capability is granted whole or not at all; a list moves
22
22
  * its capability's rights from the rule on `/` to one rule per directory.
23
+ * `exec_paths` is the programs a dependency's `process.spawn=<program>`
24
+ * grants name (WP40 §2a), and their interpreters, in the same shape: with
25
+ * it, `EXECUTE` leaves the rule on `/` and each program gets a rule of its own.
23
26
  *
24
27
  * What it does, in the order of the note's section 3, each step before the
25
28
  * next because each needs the one before it:
@@ -169,27 +172,28 @@ static const uint16_t nish_sb_uring[] = {425, 426, 427}; /* io_uring_setup, _ent
169
172
  /* Set by step 6 and read by `nish_sandbox_check`. */
170
173
  static int nish_sb_applied = 0;
171
174
 
172
- /* The errno names a failure prints; a number for any other. */
175
+ /* The errno a failure prints, as the line's parenthesized tail: its name, or
176
+ * its number for any other. */
173
177
  static const char *nish_sb_errno_name(int e, char *buf) {
174
178
  switch (e) {
175
- case EPERM: return "EPERM";
176
- case ENOSYS: return "ENOSYS";
177
- case EOPNOTSUPP: return "EOPNOTSUPP";
178
- case EINVAL: return "EINVAL";
179
- case EACCES: return "EACCES";
180
- case ENOENT: return "ENOENT";
181
- case EFAULT: return "EFAULT";
182
- case E2BIG: return "E2BIG";
183
- case ENOMEM: return "ENOMEM";
184
- case EBUSY: return "EBUSY";
179
+ case EPERM: return " (EPERM)";
180
+ case ENOSYS: return " (ENOSYS)";
181
+ case EOPNOTSUPP: return " (EOPNOTSUPP)";
182
+ case EINVAL: return " (EINVAL)";
183
+ case EACCES: return " (EACCES)";
184
+ case ENOENT: return " (ENOENT)";
185
+ case EFAULT: return " (EFAULT)";
186
+ case E2BIG: return " (E2BIG)";
187
+ case ENOMEM: return " (ENOMEM)";
188
+ case EBUSY: return " (EBUSY)";
185
189
  }
186
190
  char digits[12];
187
191
  int n = 0;
188
192
  unsigned u = (unsigned)e;
189
193
  do digits[n++] = (char)('0' + u % 10); while ((u /= 10) != 0 && n < 11);
190
- memcpy(buf, "errno ", 6);
191
- for (int i = 0; i < n; i++) buf[6 + i] = digits[n - 1 - i];
192
- buf[6 + n] = 0;
194
+ memcpy(buf, " (errno ", 8);
195
+ for (int i = 0; i < n; i++) buf[8 + i] = digits[n - 1 - i];
196
+ memcpy(buf + 8 + n, ")", 2);
193
197
  return buf;
194
198
  }
195
199
 
@@ -205,12 +209,7 @@ static void nish_sb_say(const char *a, const char *b, const char *c) {
205
209
  * named after `what`. */
206
210
  static void nish_sb_fail_at(const char *what, const char *path) {
207
211
  char buf[24];
208
- char tail[40] = " (";
209
- const char *name = nish_sb_errno_name(errno, buf);
210
- size_t len = strlen(name);
211
- memcpy(tail + 2, name, len);
212
- memcpy(tail + 2 + len, ")", 2);
213
- nish_sb_say(what, path, tail);
212
+ nish_sb_say(what, path, nish_sb_errno_name(errno, buf));
214
213
  _exit(126);
215
214
  }
216
215
 
@@ -279,15 +278,19 @@ static size_t nish_sb_name(char *line, size_t at, const char *name) {
279
278
  return at + len;
280
279
  }
281
280
 
282
- /* One path_beneath rule granting `rights` beneath `path`. A rule granting
281
+ /* One path_beneath rule granting `rights` beneath `path`, opened with
282
+ * `oflag` added: O_DIRECTORY for a scope directory, which refuses anything
283
+ * else, and 0 for a program the exec allowlist names, which O_PATH opens as
284
+ * a file. The failure lines name a directory either way, a program's too,
285
+ * because a second wording costs this unit's budget more than it says. A rule granting
283
286
  * nothing is refused (ENOMSG), and nothing is what it would add, so it is
284
287
  * left out. A scope directory gone since the build refuses strict; under
285
288
  * best-effort it is named and its rule left out, which withholds the right
286
289
  * there too, so the program loses the directory rather than gaining more. */
287
- static void nish_sb_rule(int ruleset, const char *path, uint64_t rights, int32_t mode) {
290
+ static void nish_sb_rule(int ruleset, const char *path, uint64_t rights, int32_t mode, int oflag) {
288
291
  struct nish_ll_path_beneath rule = {rights, -1};
289
292
  if (!rights) return;
290
- rule.parent_fd = open(path, O_PATH | O_CLOEXEC | O_DIRECTORY);
293
+ rule.parent_fd = open(path, O_PATH | O_CLOEXEC | oflag);
291
294
  if (rule.parent_fd < 0) {
292
295
  if (mode != 2) nish_sb_fail_at("cannot open the directory ", path);
293
296
  nish_sb_say("the scope directory ", path, " cannot be opened, so the program gets no access beneath it");
@@ -297,9 +300,10 @@ static void nish_sb_rule(int ruleset, const char *path, uint64_t rights, int32_t
297
300
  close(rule.parent_fd);
298
301
  }
299
302
 
300
- /* One rule per directory of a scope list, each granting `rights`. */
301
- static void nish_sb_scope(int ruleset, const char *dirs, uint64_t rights, int32_t mode) {
302
- for (; dirs && *dirs; dirs += strlen(dirs) + 1) nish_sb_rule(ruleset, dirs, rights, mode);
303
+ /* One rule per path of a scope list, each granting `rights` and opened with
304
+ * `oflag` as `nish_sb_rule` says. */
305
+ static void nish_sb_scope(int ruleset, const char *dirs, uint64_t rights, int32_t mode, int oflag) {
306
+ for (; dirs && *dirs; dirs += strlen(dirs) + 1) nish_sb_rule(ruleset, dirs, rights, mode, oflag);
303
307
  }
304
308
 
305
309
  /* Step 4. Every right the ABI knows is handled, and one path_beneath rule on
@@ -307,7 +311,7 @@ static void nish_sb_scope(int ruleset, const char *dirs, uint64_t rights, int32_
307
311
  * everywhere or nowhere; a scoped capability's rights are granted beneath its
308
312
  * directories alone. Returns with nothing done when there is no Landlock and
309
313
  * the mode is best-effort. */
310
- static void nish_sb_landlock(int32_t caps, int32_t mode, const char *read_dirs, const char *write_dirs) {
314
+ static void nish_sb_landlock(int32_t caps, int32_t mode, const char *read_dirs, const char *write_dirs, const char *exec_paths) {
311
315
  int abi = nish_sb_landlock_abi();
312
316
  if (abi < 0) {
313
317
  /* EPERM is a container runtime's own seccomp profile refusing the call;
@@ -331,6 +335,9 @@ static void nish_sb_landlock(int32_t caps, int32_t mode, const char *read_dirs,
331
335
  if (read_dirs) whole &= ~CAP_BIT_FS_READ;
332
336
  if (write_dirs) whole &= ~CAP_BIT_FS_WRITE;
333
337
  uint64_t granted = nish_sb_granted(whole);
338
+ /* An exec allowlist (a dependency's process.spawn grant, WP40 §2a) keeps
339
+ * EXECUTE off `/` and grants it on each listed file alone. */
340
+ if (exec_paths) granted &= ~LL_EXECUTE;
334
341
  uint64_t lost = ~granted & ~known & (LL_TRUNCATE | LL_IOCTL_DEV);
335
342
  int unscoped = abi < 6;
336
343
  /* Strict runs without IOCTL_DEV and without the ABI 6 scopes: a terminal
@@ -352,9 +359,10 @@ static void nish_sb_landlock(int32_t caps, int32_t mode, const char *read_dirs,
352
359
  struct nish_ll_ruleset attr = {known, 0, abi >= 6 ? LL_SCOPE_ALL : 0};
353
360
  int ruleset = (int)syscall(LL_CREATE, &attr, sizeof attr, 0);
354
361
  if (ruleset < 0) nish_sb_fail("cannot create the Landlock ruleset");
355
- nish_sb_rule(ruleset, "/", granted & known, 1);
356
- if (read_dirs && (caps & CAP_BIT_FS_READ)) nish_sb_scope(ruleset, read_dirs, nish_sb_granted(CAP_BIT_FS_READ) & known, mode);
357
- if (write_dirs && (caps & CAP_BIT_FS_WRITE)) nish_sb_scope(ruleset, write_dirs, nish_sb_granted(CAP_BIT_FS_WRITE) & known, mode);
362
+ nish_sb_rule(ruleset, "/", granted & known, 1, O_DIRECTORY);
363
+ if (read_dirs && (caps & CAP_BIT_FS_READ)) nish_sb_scope(ruleset, read_dirs, nish_sb_granted(CAP_BIT_FS_READ) & known, mode, O_DIRECTORY);
364
+ if (write_dirs && (caps & CAP_BIT_FS_WRITE)) nish_sb_scope(ruleset, write_dirs, nish_sb_granted(CAP_BIT_FS_WRITE) & known, mode, O_DIRECTORY);
365
+ if (caps & CAP_BIT_SPAWN) nish_sb_scope(ruleset, exec_paths, LL_EXECUTE, mode, 0);
358
366
  if (syscall(LL_RESTRICT, ruleset, 0) != 0) nish_sb_fail("cannot apply the Landlock ruleset");
359
367
  close(ruleset);
360
368
  }
@@ -428,11 +436,11 @@ static void nish_sb_seccomp(int32_t caps) {
428
436
  if (loaded != 0) nish_sb_fail("cannot load the seccomp filter");
429
437
  }
430
438
 
431
- void nish_sandbox(int32_t caps, int32_t mode, const char *read_dirs, const char *write_dirs) {
439
+ void nish_sandbox(int32_t caps, int32_t mode, const char *read_dirs, const char *write_dirs, const char *exec_paths) {
432
440
  nish_cpu_prime();
433
441
  if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) nish_sb_fail("cannot set no_new_privs");
434
442
  nish_sb_drop_capabilities();
435
- nish_sb_landlock(caps, mode, read_dirs, write_dirs);
443
+ nish_sb_landlock(caps, mode, read_dirs, write_dirs, exec_paths);
436
444
  /* Metadata writes stay EPERM inside a scoped write directory (open
437
445
  * question 1): Landlock has no right for chmod, chown or xattrs. */
438
446
  nish_sb_seccomp(write_dirs ? caps & ~CAP_BIT_FS_WRITE : caps);
@@ -457,11 +465,12 @@ static void nish_sb_none(void) {
457
465
  _exit(126);
458
466
  }
459
467
 
460
- void nish_sandbox(int32_t caps, int32_t mode, const char *read_dirs, const char *write_dirs) {
468
+ void nish_sandbox(int32_t caps, int32_t mode, const char *read_dirs, const char *write_dirs, const char *exec_paths) {
461
469
  (void)caps;
462
470
  (void)mode;
463
471
  (void)read_dirs;
464
472
  (void)write_dirs;
473
+ (void)exec_paths;
465
474
  nish_sb_none();
466
475
  }
467
476