@amritk/nish-aarch64-linux 0.39.0 → 0.40.1
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/INSTALL.md +74 -129
- package/bin/nish +0 -0
- package/docs/AI.md +112 -33
- package/package.json +1 -1
- package/runtime/nish.d.ts +21 -17
- package/runtime/nish.h +10 -0
- package/runtime/page-frame.d.ts +123 -0
- package/runtime/page-frame.mjs +736 -0
- package/runtime/page-game.d.ts +132 -0
- package/runtime/page-game.mjs +738 -0
- package/runtime/runtime-aes.c +85 -8
- package/runtime/runtime-bytes.c +88 -4
- package/runtime/runtime-net.c +32 -0
- package/runtime/runtime-sandbox.c +55 -28
- package/runtime/runtime-wasm.c +422 -0
- package/runtime/vite.mjs +1 -1
- package/scripts/build.sh +15 -0
- package/std/README.md +73 -87
- package/std/array.ts +36 -3
- package/std/collections.ts +51 -0
- package/std/crypto/hmac.ts +37 -10
- package/std/crypto/p256.ts +1388 -2664
- package/std/crypto/pki.ts +116 -4
- package/std/crypto/sha256.ts +34 -6
- package/std/crypto/x509.ts +7 -0
- package/std/host/game.ts +273 -0
- package/std/input.ts +56 -0
- package/std/json-tokenizer.ts +325 -112
- package/std/json-typed.ts +43 -8
- package/std/json-value.ts +144 -3
- package/std/json.ts +63 -8
- package/std/net/fetch.ts +22 -4
- package/std/net/http-server.ts +3382 -0
- package/std/net/http1-client-codec.ts +6 -2
- package/std/net/http1-codec.ts +137 -12
- package/std/net/http1-server.ts +83 -29
- package/std/net/http2.ts +47 -14
- package/std/net/http3-server.ts +29 -3
- package/std/net/http3.ts +180 -7
- package/std/net/quic-recovery.ts +240 -9
- package/std/net/quic-stream.ts +21 -1
- package/std/net/quic.ts +98 -4
- package/std/net/router.ts +378 -31
- package/std/net/serve.ts +873 -65
- package/std/net/tls/verify.ts +19 -1
- package/std/net/url.ts +83 -35
- package/std/net/websocket.ts +74 -1
- package/std/net/webtransport.ts +7 -1
- package/std/raster.ts +837 -0
- package/std/release.ts +593 -0
- package/std/synth.ts +143 -0
- package/std/threads.ts +298 -0
- package/std/yaml-json.ts +368 -28
- package/std/yaml.ts +122 -27
package/runtime/runtime-aes.c
CHANGED
|
@@ -56,9 +56,12 @@
|
|
|
56
56
|
* state on the stack on purpose. The two stack buffers they do have, the
|
|
57
57
|
* keystream of a partial last block and a zero-padded last block for GHASH, are
|
|
58
58
|
* zeroed before return by volatile stores, the mechanism `nish:secret`'s wipe
|
|
59
|
-
* uses, which tests/run.js holds against `clang -O2`.
|
|
60
|
-
*
|
|
61
|
-
*
|
|
59
|
+
* uses, which tests/run.js holds against `clang -O2`. Every kernel ends with
|
|
60
|
+
* `nish_aes_clear`, which zeroes the vector registers and the caller-saved
|
|
61
|
+
* general ones, and no kernel spills a vector register: tests/run.js reads both
|
|
62
|
+
* in the `-O2` assembly. What that cannot reach -- the `debug` profile's `-O0`
|
|
63
|
+
* stack, and a signal frame taken mid-kernel -- is in
|
|
64
|
+
* docs/security/crypto-aead.md, AEAD-4.
|
|
62
65
|
*
|
|
63
66
|
* The algorithms are FIPS 197 (the key expansion, with SubWord taken from the
|
|
64
67
|
* AES instructions) and SP 800-38D (GCTR and GHASH); the GHASH multiply is the
|
|
@@ -141,6 +144,64 @@ NISH_AES_INLINE void nish_aes_wipe16(void *p) {
|
|
|
141
144
|
v[1] = 0;
|
|
142
145
|
}
|
|
143
146
|
|
|
147
|
+
/* Zeroes, as a kernel's last act, every register a kernel may leave a round
|
|
148
|
+
key, H, a counter block, keystream or GHASH state in: the vector registers a
|
|
149
|
+
call may clobber and the general ones (SubWord's key words and a partial
|
|
150
|
+
block's keystream bytes pass through those). Each kernel ends with it, so
|
|
151
|
+
what it computed leaves with the call rather than staying until later code
|
|
152
|
+
overwrites it (docs/security/crypto-aead.md, AEAD-4). The registers are named
|
|
153
|
+
by number, not by the kernel's own use, so a change of register allocation
|
|
154
|
+
cannot open a gap; the clobber list tells the compiler each is gone, and the
|
|
155
|
+
`memory` clobber keeps every store of the kernel before it. The callee-saved
|
|
156
|
+
ones are left: the epilogue restores the caller's values into them, and a
|
|
157
|
+
kernel that used them would show it in tests/run.js's reading. On x86-64
|
|
158
|
+
that is all sixteen XMM registers, or with AVX in the target (`--cpu
|
|
159
|
+
x86-64-v3`) the whole of every YMM register, which `vzeroall` clears; on
|
|
160
|
+
AArch64 v0 to v7 and v16 to v31, since AAPCS64 has the callee keep v8 to v15's
|
|
161
|
+
low halves. Every instruction is a zeroing idiom: no input, so no secret
|
|
162
|
+
reaches it, and no flag a branch reads after it. It is one function the
|
|
163
|
+
kernels jump to as their last act (a tail call at `-O2`, after their
|
|
164
|
+
epilogue), not inlined into each: the same clear in a quarter of the bytes. */
|
|
165
|
+
static __attribute__((noinline)) NISH_AES_TARGET void nish_aes_clear(void) {
|
|
166
|
+
#if defined(NISH_AES_X86)
|
|
167
|
+
#if defined(__AVX__)
|
|
168
|
+
#define NISH_AES_CLEAR_VEC "vzeroall\n\t"
|
|
169
|
+
#else
|
|
170
|
+
#define NISH_AES_CLEAR_VEC \
|
|
171
|
+
"pxor %%xmm0, %%xmm0\n\tpxor %%xmm1, %%xmm1\n\tpxor %%xmm2, %%xmm2\n\tpxor %%xmm3, %%xmm3\n\t" \
|
|
172
|
+
"pxor %%xmm4, %%xmm4\n\tpxor %%xmm5, %%xmm5\n\tpxor %%xmm6, %%xmm6\n\tpxor %%xmm7, %%xmm7\n\t" \
|
|
173
|
+
"pxor %%xmm8, %%xmm8\n\tpxor %%xmm9, %%xmm9\n\tpxor %%xmm10, %%xmm10\n\tpxor %%xmm11, %%xmm11\n\t" \
|
|
174
|
+
"pxor %%xmm12, %%xmm12\n\tpxor %%xmm13, %%xmm13\n\tpxor %%xmm14, %%xmm14\n\tpxor %%xmm15, %%xmm15\n\t"
|
|
175
|
+
#endif
|
|
176
|
+
__asm__ volatile(NISH_AES_CLEAR_VEC
|
|
177
|
+
"xorl %%eax, %%eax\n\txorl %%ecx, %%ecx\n\txorl %%edx, %%edx\n\txorl %%esi, %%esi\n\t"
|
|
178
|
+
"xorl %%edi, %%edi\n\txorl %%r8d, %%r8d\n\txorl %%r9d, %%r9d\n\txorl %%r10d, %%r10d\n\t"
|
|
179
|
+
"xorl %%r11d, %%r11d"
|
|
180
|
+
:
|
|
181
|
+
:
|
|
182
|
+
: "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7", "xmm8", "xmm9", "xmm10", "xmm11",
|
|
183
|
+
"xmm12", "xmm13", "xmm14", "xmm15", "rax", "rcx", "rdx", "rsi", "rdi", "r8", "r9", "r10", "r11",
|
|
184
|
+
"cc", "memory");
|
|
185
|
+
#undef NISH_AES_CLEAR_VEC
|
|
186
|
+
#else
|
|
187
|
+
__asm__ volatile(
|
|
188
|
+
"movi v0.16b, #0\n\tmovi v1.16b, #0\n\tmovi v2.16b, #0\n\tmovi v3.16b, #0\n\t"
|
|
189
|
+
"movi v4.16b, #0\n\tmovi v5.16b, #0\n\tmovi v6.16b, #0\n\tmovi v7.16b, #0\n\t"
|
|
190
|
+
"movi v16.16b, #0\n\tmovi v17.16b, #0\n\tmovi v18.16b, #0\n\tmovi v19.16b, #0\n\t"
|
|
191
|
+
"movi v20.16b, #0\n\tmovi v21.16b, #0\n\tmovi v22.16b, #0\n\tmovi v23.16b, #0\n\t"
|
|
192
|
+
"movi v24.16b, #0\n\tmovi v25.16b, #0\n\tmovi v26.16b, #0\n\tmovi v27.16b, #0\n\t"
|
|
193
|
+
"movi v28.16b, #0\n\tmovi v29.16b, #0\n\tmovi v30.16b, #0\n\tmovi v31.16b, #0\n\t"
|
|
194
|
+
"mov x0, xzr\n\tmov x1, xzr\n\tmov x2, xzr\n\tmov x3, xzr\n\tmov x4, xzr\n\tmov x5, xzr\n\t"
|
|
195
|
+
"mov x6, xzr\n\tmov x7, xzr\n\tmov x8, xzr\n\tmov x9, xzr\n\tmov x10, xzr\n\tmov x11, xzr\n\t"
|
|
196
|
+
"mov x12, xzr\n\tmov x13, xzr\n\tmov x14, xzr\n\tmov x15, xzr\n\tmov x16, xzr\n\tmov x17, xzr"
|
|
197
|
+
:
|
|
198
|
+
:
|
|
199
|
+
: "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", "v24",
|
|
200
|
+
"v25", "v26", "v27", "v28", "v29", "v30", "v31", "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9",
|
|
201
|
+
"x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", "memory");
|
|
202
|
+
#endif
|
|
203
|
+
}
|
|
204
|
+
|
|
144
205
|
/* The block read as a big-endian 128-bit number, as two lanes low then high:
|
|
145
206
|
the order GHASH's multiply works in. Its own inverse. */
|
|
146
207
|
NISH_AES_INLINE nish_aes_u64x2 nish_aes_reflect(nish_aes_u8x16 b) {
|
|
@@ -326,10 +387,12 @@ NISH_AES_TARGET void nish_aes_schedule(const nish_array *key, int32_t rounds, ni
|
|
|
326
387
|
nish_aes_store(rk->data + NISH_AES_HMID + 16 * i, (nish_aes_u8x16)nish_aes_mid(p));
|
|
327
388
|
p = nish_aes_mul(p, h);
|
|
328
389
|
}
|
|
390
|
+
nish_aes_clear();
|
|
329
391
|
}
|
|
330
392
|
|
|
331
393
|
NISH_AES_TARGET void nish_aes_block(const nish_array *rk, int32_t rounds, const nish_array *src, nish_array *dst) {
|
|
332
394
|
nish_aes_store(dst->data, nish_aes_encrypt(rk->data, rounds, nish_aes_load(src->data)));
|
|
395
|
+
nish_aes_clear();
|
|
333
396
|
}
|
|
334
397
|
|
|
335
398
|
NISH_AES_TARGET void nish_aes_ctr(const nish_array *rk, int32_t rounds, const nish_array *j0, const nish_array *src,
|
|
@@ -364,6 +427,7 @@ NISH_AES_TARGET void nish_aes_ctr(const nish_array *rk, int32_t rounds, const ni
|
|
|
364
427
|
for (uint32_t i = 0; i < rest; i++) out[at + i] = (char)(in[at + i] ^ ks[i]);
|
|
365
428
|
nish_aes_wipe16(ks);
|
|
366
429
|
}
|
|
430
|
+
nish_aes_clear();
|
|
367
431
|
}
|
|
368
432
|
|
|
369
433
|
NISH_AES_TARGET void nish_aes_ghash(nish_array *y, uint64_t hHi, uint64_t hLo, const nish_array *rk,
|
|
@@ -381,17 +445,29 @@ NISH_AES_TARGET void nish_aes_ghash(nish_array *y, uint64_t hHi, uint64_t hLo, c
|
|
|
381
445
|
blocks of the serial recurrence, and its products add before the one
|
|
382
446
|
reduction they share. */
|
|
383
447
|
if (blocks >= 4) {
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
448
|
+
/* The four middle operands packed two to a register, H's and H^2's in
|
|
449
|
+
`hm[0]`, H^3's and H^4's in `hm[1]`: a middle product reads one lane of
|
|
450
|
+
each side, and a block's own middle operand holds the same word in both,
|
|
451
|
+
so the low lane's product or the high lane's picks the power. Six
|
|
452
|
+
registers rather than eight leave the loop enough to hold every power
|
|
453
|
+
without a spill, which no wipe could reach, on every target
|
|
454
|
+
tests/run.js reads ("the AES kernels clear ..."). */
|
|
455
|
+
nish_aes_u64x2 hp[4], hm[2];
|
|
456
|
+
for (int32_t i = 0; i < 4; i++) hp[i] = (nish_aes_u64x2)nish_aes_load(k + NISH_AES_HPOW + 16 * i);
|
|
457
|
+
for (int32_t i = 0; i < 2; i++) {
|
|
458
|
+
hm[i] = __builtin_shufflevector((nish_aes_u64x2)nish_aes_load(k + NISH_AES_HMID + 32 * i),
|
|
459
|
+
(nish_aes_u64x2)nish_aes_load(k + NISH_AES_HMID + 32 * i + 16), 0, 2);
|
|
388
460
|
}
|
|
389
461
|
for (; n + 4 <= blocks; n += 4) {
|
|
390
462
|
nish_aes_u64x2 sum[3] = {{0, 0}, {0, 0}, {0, 0}};
|
|
391
463
|
for (uint32_t j = 0; j < 4; j++) {
|
|
392
464
|
nish_aes_u64x2 x = nish_aes_reflect(nish_aes_load(in + 16 * (uint64_t)(n + j)));
|
|
393
465
|
if (j == 0) x ^= acc;
|
|
394
|
-
|
|
466
|
+
const uint32_t power = 3 - j;
|
|
467
|
+
const nish_aes_u64x2 xm = nish_aes_mid(x);
|
|
468
|
+
sum[0] ^= nish_aes_clmul_lo(x, hp[power]);
|
|
469
|
+
sum[1] ^= nish_aes_clmul_hi(x, hp[power]);
|
|
470
|
+
sum[2] ^= (power & 1) != 0 ? nish_aes_clmul_hi(xm, hm[power >> 1]) : nish_aes_clmul_lo(xm, hm[power >> 1]);
|
|
395
471
|
}
|
|
396
472
|
acc = nish_aes_mul_finish(sum);
|
|
397
473
|
}
|
|
@@ -407,6 +483,7 @@ NISH_AES_TARGET void nish_aes_ghash(nish_array *y, uint64_t hHi, uint64_t hLo, c
|
|
|
407
483
|
nish_aes_wipe16(pad);
|
|
408
484
|
}
|
|
409
485
|
nish_aes_store(y->data, (nish_aes_u8x16)__builtin_shufflevector(acc, acc, 1, 0));
|
|
486
|
+
nish_aes_clear();
|
|
410
487
|
}
|
|
411
488
|
|
|
412
489
|
#if defined(NISH_AES_X86)
|
package/runtime/runtime-bytes.c
CHANGED
|
@@ -176,6 +176,55 @@ static inline int64_t nish_run_resume(int cls, const uint8_t *p, int64_t i, int6
|
|
|
176
176
|
}
|
|
177
177
|
#endif
|
|
178
178
|
|
|
179
|
+
#if defined(NISH_SIMD_SSE2)
|
|
180
|
+
/* The x86 paths check a text block's UTF-8 a block at a time rather than a
|
|
181
|
+
sequence at a time, once its first stop is a high byte. A block starts on a
|
|
182
|
+
sequence boundary, so with each byte `v` beside the three before it in the
|
|
183
|
+
block (`p1`, `p2`, `p3`, zero before the block's first byte), the block's
|
|
184
|
+
bytes are well formed exactly where none of these holds:
|
|
185
|
+
|
|
186
|
+
- a byte is a continuation (0x80 to 0xbf) where no lead of the three before
|
|
187
|
+
it reaches it, or is not one where one does: a sequence cut short, a
|
|
188
|
+
stray continuation or one too many (a lead at 0xc0 reaches one byte on,
|
|
189
|
+
at 0xe0 two, at 0xf0 three);
|
|
190
|
+
- the byte is a lead no sequence has: 0xc0 or 0xc1, which only spell an
|
|
191
|
+
overlong two-byte form, or 0xf5 and up, past U+10FFFF;
|
|
192
|
+
- it is the second byte of a sequence outside the range its lead narrows it
|
|
193
|
+
to, the same four `nish_run_utf8` narrows: below 0xa0 after 0xe0 or 0x90
|
|
194
|
+
after 0xf0 (an overlong form), above 0x9f after 0xed (a surrogate) or
|
|
195
|
+
0x8f after 0xf4 (past U+10FFFF).
|
|
196
|
+
|
|
197
|
+
Each holds at a byte no later than the first stop that follows the bad
|
|
198
|
+
sequence's lead, so a block whose first ASCII stop has nothing wrong at or
|
|
199
|
+
before it is well formed up to it. This is compare and shift work alone, in
|
|
200
|
+
the spirit of Keiser and Lemire's lookup validator ("Validating UTF-8 In
|
|
201
|
+
Less Than One Instruction Per Byte", 2021) but without its table lookups,
|
|
202
|
+
which SSE2 has no byte shuffle for, and written here rather than taken from
|
|
203
|
+
any implementation of it. */
|
|
204
|
+
#define NISH_RUN_BAD(v, p1, p2, p3) \
|
|
205
|
+
(((((p1) >= 0xc0) | ((p2) >= 0xe0) | ((p3) >= 0xf0)) != (((v) & 0xc0) == 0x80)) | (((v) & 0xfe) == 0xc0) | \
|
|
206
|
+
((v) >= 0xf5) | (((p1) == 0xe0) & ((v) < 0xa0)) | (((p1) == 0xf0) & ((v) < 0x90)) | \
|
|
207
|
+
(((p1) == 0xed) & ((v) > 0x9f)) | (((p1) == 0xf4) & ((v) > 0x8f)))
|
|
208
|
+
|
|
209
|
+
typedef uint8_t nish_u8x16 __attribute__((vector_size(16)));
|
|
210
|
+
|
|
211
|
+
/* How many of the bytes before `e` start a sequence that `e` cuts off, in a
|
|
212
|
+
block whose UTF-8 is otherwise well formed: the block after starts at its
|
|
213
|
+
lead. */
|
|
214
|
+
static inline int64_t nish_run_cut(const uint8_t *p, int64_t e) {
|
|
215
|
+
if (p[e - 1] >= 0xc0) return 1;
|
|
216
|
+
if (p[e - 2] >= 0xe0) return 2;
|
|
217
|
+
return p[e - 3] >= 0xf0 ? 3 : 0;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/* The bytes of a 16-byte block that break its UTF-8, as a movemask. */
|
|
221
|
+
static inline unsigned nish_run_bad_sse2(__m128i x) {
|
|
222
|
+
nish_u8x16 v = (nish_u8x16)x, p1 = (nish_u8x16)_mm_slli_si128(x, 1), p2 = (nish_u8x16)_mm_slli_si128(x, 2),
|
|
223
|
+
p3 = (nish_u8x16)_mm_slli_si128(x, 3);
|
|
224
|
+
return (unsigned)_mm_movemask_epi8((__m128i)NISH_RUN_BAD(v, p1, p2, p3));
|
|
225
|
+
}
|
|
226
|
+
#endif
|
|
227
|
+
|
|
179
228
|
/* Each vector path computes a mask of the block's stops -- the bytes outside
|
|
180
229
|
the class, where for text every byte with its top bit set counts as a stop
|
|
181
230
|
-- and either moves on a whole block or jumps to the first stop. */
|
|
@@ -209,21 +258,43 @@ static int64_t nish_plain_base(int cls, const uint8_t *p, int64_t i, int64_t to)
|
|
|
209
258
|
return nish_plain_scalar(cls, p, i, to);
|
|
210
259
|
}
|
|
211
260
|
|
|
212
|
-
|
|
261
|
+
/* Sixteen bytes a block. With `yield` set, a block with no stop at all ends
|
|
262
|
+
the walk with `~i`, its start complemented, so the AVX2 path can take back
|
|
263
|
+
a run that has gone back to ASCII; a run's end is never negative. */
|
|
264
|
+
__attribute__((noinline)) static int64_t nish_run_sse2(int cls, const uint8_t *p, int64_t from, int64_t to, int yield) {
|
|
213
265
|
int64_t i = from;
|
|
214
266
|
while (to - i >= 16) {
|
|
215
|
-
|
|
267
|
+
__m128i v = _mm_loadu_si128((const __m128i *)(p + i));
|
|
268
|
+
unsigned m = nish_run_stops_sse2(cls, v);
|
|
216
269
|
if (m == 0) {
|
|
270
|
+
if (yield) return ~i;
|
|
217
271
|
i += 16;
|
|
218
272
|
continue;
|
|
219
273
|
}
|
|
220
274
|
int64_t at = i + __builtin_ctz(m);
|
|
275
|
+
/* A text block whose first stop is a high byte: its UTF-8 checked whole,
|
|
276
|
+
up to its first ASCII stop or, with none, to its end less a sequence
|
|
277
|
+
the end cuts off. Anything wrong goes to the step below, which finds
|
|
278
|
+
the exact byte. A high byte is never an ASCII stop, so those are the
|
|
279
|
+
stops less the bytes whose sign bit is set. */
|
|
280
|
+
if (cls == NISH_RUN_TEXT && p[at] >= 0x80) {
|
|
281
|
+
unsigned s = m & ~(unsigned)_mm_movemask_epi8(v), bad = nish_run_bad_sse2(v);
|
|
282
|
+
if (s != 0 && (bad & (s ^ (s - 1))) == 0) return i + __builtin_ctz(s);
|
|
283
|
+
if (s == 0 && bad == 0) {
|
|
284
|
+
i += 16 - nish_run_cut(p, i + 16);
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
221
288
|
i = nish_run_resume(cls, p, at, to);
|
|
222
289
|
if (i == at) return at;
|
|
223
290
|
}
|
|
224
291
|
return nish_run_scalar(cls, p, i, to);
|
|
225
292
|
}
|
|
226
293
|
|
|
294
|
+
static int64_t nish_run_base(int cls, const uint8_t *p, int64_t from, int64_t to) {
|
|
295
|
+
return nish_run_sse2(cls, p, from, to, 0);
|
|
296
|
+
}
|
|
297
|
+
|
|
227
298
|
__attribute__((target("avx2"))) static inline unsigned nish_run_stops_avx2(int cls, __m256i v) {
|
|
228
299
|
if (cls == NISH_RUN_BLANK) {
|
|
229
300
|
__m256i in = _mm256_or_si256(
|
|
@@ -242,6 +313,19 @@ __attribute__((target("avx2"))) static inline unsigned nish_run_stops_avx2(int c
|
|
|
242
313
|
return (unsigned)_mm256_movemask_epi8(_mm256_or_si256(stop, v));
|
|
243
314
|
}
|
|
244
315
|
|
|
316
|
+
__attribute__((target("avx2"))) static int64_t nish_run_avx2(int cls, const uint8_t *p, int64_t from, int64_t to);
|
|
317
|
+
|
|
318
|
+
/* Past UTF-8 the base path's block check takes the run, text that has one
|
|
319
|
+
multi-byte sequence tending to have more, until a block of it is ASCII
|
|
320
|
+
again and the AVX2 path takes it back. Out of line and reached by tail
|
|
321
|
+
calls both ways, so the ASCII loop saves no registers and the stack does
|
|
322
|
+
not grow with the number of sequences. */
|
|
323
|
+
__attribute__((noinline, target("avx2"))) static int64_t nish_run_avx2_utf8(int cls, const uint8_t *p, int64_t from, int64_t to) {
|
|
324
|
+
int64_t i = nish_run_sse2(cls, p, from, to, 1);
|
|
325
|
+
if (i >= 0) return i;
|
|
326
|
+
__attribute__((musttail)) return nish_run_avx2(cls, p, ~i, to);
|
|
327
|
+
}
|
|
328
|
+
|
|
245
329
|
/* Thirty-two bytes a block, then the base path for what is left. */
|
|
246
330
|
__attribute__((target("avx2"))) static int64_t nish_run_avx2(int cls, const uint8_t *p, int64_t from, int64_t to) {
|
|
247
331
|
int64_t i = from;
|
|
@@ -252,8 +336,8 @@ __attribute__((target("avx2"))) static int64_t nish_run_avx2(int cls, const uint
|
|
|
252
336
|
continue;
|
|
253
337
|
}
|
|
254
338
|
int64_t at = i + __builtin_ctz(m);
|
|
255
|
-
|
|
256
|
-
|
|
339
|
+
if (cls != NISH_RUN_TEXT || p[at] < 0x80) return at;
|
|
340
|
+
__attribute__((musttail)) return nish_run_avx2_utf8(cls, p, at, to);
|
|
257
341
|
}
|
|
258
342
|
return nish_run_base(cls, p, i, to);
|
|
259
343
|
}
|
package/runtime/runtime-net.c
CHANGED
|
@@ -68,6 +68,7 @@ typedef int nish_net_unused;
|
|
|
68
68
|
#include <stdint.h>
|
|
69
69
|
#include <stddef.h>
|
|
70
70
|
#include <string.h>
|
|
71
|
+
#include <sys/resource.h>
|
|
71
72
|
#include <sys/socket.h>
|
|
72
73
|
#include <sys/stat.h>
|
|
73
74
|
#include <sys/un.h>
|
|
@@ -88,6 +89,7 @@ typedef int nish_net_unused;
|
|
|
88
89
|
#define SO_ATTACH_REUSEPORT_CBPF 51
|
|
89
90
|
#endif
|
|
90
91
|
#else
|
|
92
|
+
#include <limits.h>
|
|
91
93
|
#include <sys/event.h>
|
|
92
94
|
#include <sys/time.h>
|
|
93
95
|
#endif
|
|
@@ -178,7 +180,35 @@ static int nish_net_setup(int fd) {
|
|
|
178
180
|
#define NISH_NOSIGNAL MSG_NOSIGNAL
|
|
179
181
|
#endif
|
|
180
182
|
|
|
183
|
+
/* Raises the soft `RLIMIT_NOFILE` to the hard limit, once per process, as Go
|
|
184
|
+
and Node do at start-up: a server is otherwise held to the soft limit it
|
|
185
|
+
inherits, often 1024 (256 on macOS), though the hard limit is usually far
|
|
186
|
+
higher. Every call here that makes a descriptor out of nothing calls it
|
|
187
|
+
first, so a program that never touches `nish:net` keeps the limit it was
|
|
188
|
+
given. Darwin's `setrlimit` refuses a soft limit above `OPEN_MAX`, and an
|
|
189
|
+
unlimited hard limit is the usual case there, so the raise stops at
|
|
190
|
+
`OPEN_MAX`. A failure is ignored: the program keeps the limit it had, and
|
|
191
|
+
the call that wanted a descriptor answers -24 when it meets it. The flag is
|
|
192
|
+
claimed atomically because `scope()` tasks can make their first sockets at
|
|
193
|
+
the same moment; a task that loses the claim goes on before the raise lands,
|
|
194
|
+
and holds no more descriptors than it could before. Children `spawn` starts
|
|
195
|
+
inherit the raised soft limit (docs/security/runtime.md, RT-54). */
|
|
196
|
+
static void nish_net_raise_nofile(void) {
|
|
197
|
+
static int raised;
|
|
198
|
+
if (__atomic_exchange_n(&raised, 1, __ATOMIC_RELAXED)) return;
|
|
199
|
+
struct rlimit r;
|
|
200
|
+
if (getrlimit(RLIMIT_NOFILE, &r) != 0) return;
|
|
201
|
+
rlim_t want = r.rlim_max;
|
|
202
|
+
#if !defined(__linux__)
|
|
203
|
+
if (want > (rlim_t)OPEN_MAX) want = (rlim_t)OPEN_MAX;
|
|
204
|
+
#endif
|
|
205
|
+
if (r.rlim_cur >= want) return;
|
|
206
|
+
r.rlim_cur = want;
|
|
207
|
+
setrlimit(RLIMIT_NOFILE, &r);
|
|
208
|
+
}
|
|
209
|
+
|
|
181
210
|
static int nish_net_socket(int family, int type) {
|
|
211
|
+
nish_net_raise_nofile();
|
|
182
212
|
#if defined(__linux__)
|
|
183
213
|
return socket(family, type | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
|
|
184
214
|
#else
|
|
@@ -686,6 +716,7 @@ int32_t nish_udp_recv_from(int32_t fd, nish_array *buf, int64_t off, int64_t len
|
|
|
686
716
|
the `fcntl` after `kqueue()` on Darwin, where a child another thread spawns
|
|
687
717
|
in between can inherit it, as `nish_net_setup`'s sockets can. */
|
|
688
718
|
int32_t nish_poll_create(void) {
|
|
719
|
+
nish_net_raise_nofile();
|
|
689
720
|
#if defined(__linux__)
|
|
690
721
|
int fd = epoll_create1(EPOLL_CLOEXEC);
|
|
691
722
|
#else
|
|
@@ -969,6 +1000,7 @@ int32_t nish_unix_accept(int32_t fd) {
|
|
|
969
1000
|
int32_t nish_unix_socket_pair(int32_t kind, nish_array *fds) {
|
|
970
1001
|
if ((kind != 1 && kind != 2) || fds->len < 2) return -22;
|
|
971
1002
|
int sv[2];
|
|
1003
|
+
nish_net_raise_nofile();
|
|
972
1004
|
#if defined(__linux__)
|
|
973
1005
|
if (socketpair(AF_UNIX, (kind == 1 ? SOCK_STREAM : SOCK_SEQPACKET) | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, sv) != 0)
|
|
974
1006
|
return nish_net_fail();
|
|
@@ -28,7 +28,9 @@
|
|
|
28
28
|
* identity is recorded before step 2, and `nish_spawn_sandboxed`, which
|
|
29
29
|
* runtime-os.c's spawn calls once it is set, refuses a target that is that
|
|
30
30
|
* file, has the child execute a descriptor of any other, and holds that
|
|
31
|
-
* child's whole subtree to the one exec ("One exec per spawn" below).
|
|
31
|
+
* child's whole subtree to the one exec ("One exec per spawn" below). A
|
|
32
|
+
* program granted `process.spawn=exec:<program>` is written `///<path>`, and
|
|
33
|
+
* its identity is recorded too: its child is spared the one exec.
|
|
32
34
|
*
|
|
33
35
|
* What it does, in the order of the note's section 3, each step before the
|
|
34
36
|
* next because each needs the one before it:
|
|
@@ -475,14 +477,21 @@ static void nish_sb_seccomp(int32_t caps) {
|
|
|
475
477
|
* which names the same file. Each such file's identity is kept here when the
|
|
476
478
|
* sandbox is applied, and `nish_sb_spawn_open` refuses a spawn target that is
|
|
477
479
|
* one of them, by identity, so a symlink, a hardlink or any second name is
|
|
478
|
-
* refused as well.
|
|
479
|
-
*
|
|
480
|
-
|
|
480
|
+
* refused as well.
|
|
481
|
+
*
|
|
482
|
+
* A program granted `process.spawn=exec:<program>` is written with a third
|
|
483
|
+
* leading `/` (`///usr/bin/git`), which names the same file too, and its
|
|
484
|
+
* identity is kept in the same table: such a target is started without the
|
|
485
|
+
* one-exec filter ("One exec per spawn" below), so its subtree may execute
|
|
486
|
+
* whatever the allowlist holds. A file kept both ways is refused. More marked
|
|
487
|
+
* entries than the table holds refuse every spawn rather than some. */
|
|
488
|
+
#define NISH_SB_MARKED 16
|
|
481
489
|
static struct {
|
|
482
490
|
dev_t dev;
|
|
483
491
|
ino_t ino;
|
|
484
|
-
|
|
485
|
-
|
|
492
|
+
int exec; /* 1 for an `exec:` grant, 0 for a kept interpreter */
|
|
493
|
+
} nish_sb_marked[NISH_SB_MARKED];
|
|
494
|
+
static int nish_sb_nmarked;
|
|
486
495
|
|
|
487
496
|
/* The file `posix_spawnp` would execute for `file`: `file` itself when it
|
|
488
497
|
* holds a `/`, else the first executable regular file of that name in a PATH
|
|
@@ -491,8 +500,9 @@ static int nish_sb_nrefused;
|
|
|
491
500
|
* gets its /proc/self/fd name, so the child executes the very file checked: a
|
|
492
501
|
* name swapped between this check and the exec reaches nothing, and a script's
|
|
493
502
|
* interpreter can open the script by that name. Returns the descriptor, or
|
|
494
|
-
* -EACCES for a kept interpreter, or -ENOENT when there is no such file
|
|
495
|
-
|
|
503
|
+
* -EACCES for a kept interpreter, or -ENOENT when there is no such file;
|
|
504
|
+
* `*unheld` is set when the file is an `exec:` grant's program. */
|
|
505
|
+
static int nish_sb_spawn_open(const char *file, int low, char *proc, int *unheld) {
|
|
496
506
|
const char *dir = strchr(file, '/') ? "" : getenv("PATH");
|
|
497
507
|
char buf[PATH_MAX];
|
|
498
508
|
struct stat st;
|
|
@@ -510,9 +520,13 @@ static int nish_sb_spawn_open(const char *file, int low, char *proc) {
|
|
|
510
520
|
}
|
|
511
521
|
if (!len || path == buf) fd = open(path, O_PATH | O_CLOEXEC);
|
|
512
522
|
if (fd >= 0 && !fstat(fd, &st) && S_ISREG(st.st_mode) && (st.st_mode & 0111)) {
|
|
513
|
-
int k =
|
|
514
|
-
while (k > 0
|
|
515
|
-
|
|
523
|
+
int k = nish_sb_nmarked > NISH_SB_MARKED ? -1 : nish_sb_nmarked, held = k < 0;
|
|
524
|
+
while (k-- > 0)
|
|
525
|
+
if (nish_sb_marked[k].dev == st.st_dev && nish_sb_marked[k].ino == st.st_ino) {
|
|
526
|
+
if (nish_sb_marked[k].exec) *unheld = 1;
|
|
527
|
+
else held = 1;
|
|
528
|
+
}
|
|
529
|
+
int moved = held ? -EACCES : fcntl(fd, F_DUPFD, low);
|
|
516
530
|
if (moved == -1) moved = -errno;
|
|
517
531
|
close(fd);
|
|
518
532
|
if (moved >= 0) snprintf(proc, 32, "/proc/self/fd/%d", moved);
|
|
@@ -585,6 +599,7 @@ struct nish_sb_job {
|
|
|
585
599
|
const posix_spawn_file_actions_t *actions;
|
|
586
600
|
char **argv;
|
|
587
601
|
char proc[32];
|
|
602
|
+
int unheld; /* the target is an `exec:` grant's program: no filter */
|
|
588
603
|
int pipe[2]; /* the spawner's two answers: its listener, then its child */
|
|
589
604
|
sigset_t mask; /* the calling thread's, which the child starts with */
|
|
590
605
|
int32_t result;
|
|
@@ -600,6 +615,22 @@ static int nish_sb_lift(int fd, int low) {
|
|
|
600
615
|
return moved;
|
|
601
616
|
}
|
|
602
617
|
|
|
618
|
+
/* `posix_spawn` of the checked target, its child started with the caller's
|
|
619
|
+
* mask: the pid, or a negative errno. This thread blocks every signal, and a
|
|
620
|
+
* child starts with its parent's mask unless it is given one. */
|
|
621
|
+
static int32_t nish_sb_start(struct nish_sb_job *job) {
|
|
622
|
+
pid_t pid;
|
|
623
|
+
posix_spawnattr_t attr;
|
|
624
|
+
int failed = posix_spawnattr_init(&attr);
|
|
625
|
+
if (!failed) {
|
|
626
|
+
posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK);
|
|
627
|
+
posix_spawnattr_setsigmask(&attr, &job->mask);
|
|
628
|
+
failed = posix_spawn(&pid, job->proc, job->actions, &attr, job->argv, environ);
|
|
629
|
+
posix_spawnattr_destroy(&attr);
|
|
630
|
+
}
|
|
631
|
+
return failed ? -failed : (int32_t)pid;
|
|
632
|
+
}
|
|
633
|
+
|
|
603
634
|
static void nish_sb_refuse_spawn(int e) {
|
|
604
635
|
char buf[24];
|
|
605
636
|
nish_sb_say("cannot hold a spawned program to one exec, so the spawn is refused", 0, nish_sb_errno_name(e, buf));
|
|
@@ -630,18 +661,7 @@ static void *nish_sb_spawner(void *arg) {
|
|
|
630
661
|
if (said < 0) said = -errno;
|
|
631
662
|
(void)!write(job->pipe[1], &said, sizeof said);
|
|
632
663
|
if (said < 0) return 0;
|
|
633
|
-
|
|
634
|
-
/* This thread blocks every signal, and a child starts with its parent's
|
|
635
|
-
* mask unless it is given one: the caller's. */
|
|
636
|
-
posix_spawnattr_t attr;
|
|
637
|
-
int failed = posix_spawnattr_init(&attr);
|
|
638
|
-
if (!failed) {
|
|
639
|
-
posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK);
|
|
640
|
-
posix_spawnattr_setsigmask(&attr, &job->mask);
|
|
641
|
-
failed = posix_spawn(&pid, job->proc, job->actions, &attr, job->argv, environ);
|
|
642
|
-
posix_spawnattr_destroy(&attr);
|
|
643
|
-
}
|
|
644
|
-
said = failed ? -failed : (int32_t)pid;
|
|
664
|
+
said = nish_sb_start(job);
|
|
645
665
|
(void)!write(job->pipe[1], &said, sizeof said);
|
|
646
666
|
return 0;
|
|
647
667
|
}
|
|
@@ -699,11 +719,17 @@ static void *nish_sb_supervisor(void *arg) {
|
|
|
699
719
|
nish_sb_refuse_spawn(EMFILE);
|
|
700
720
|
return 0;
|
|
701
721
|
}
|
|
702
|
-
int fd = nish_sb_spawn_open(job->file, job->low, job->proc);
|
|
722
|
+
int fd = nish_sb_spawn_open(job->file, job->low, job->proc, &job->unheld);
|
|
703
723
|
if (fd < 0) {
|
|
704
724
|
job->result = fd;
|
|
705
725
|
return 0;
|
|
706
726
|
}
|
|
727
|
+
/* An `exec:` grant's program keeps its subtree's reach of the allowlist:
|
|
728
|
+
* it starts from this private table like any other, without the filter. */
|
|
729
|
+
if (job->unheld) {
|
|
730
|
+
job->result = nish_sb_start(job);
|
|
731
|
+
return 0;
|
|
732
|
+
}
|
|
707
733
|
int failed = pthread_create(&spawner, 0, nish_sb_spawner, job);
|
|
708
734
|
if (failed) {
|
|
709
735
|
nish_sb_refuse_spawn(failed);
|
|
@@ -783,21 +809,22 @@ static int32_t nish_sb_spawn_once(const char *file, const int *dup, int base, in
|
|
|
783
809
|
return job.result;
|
|
784
810
|
}
|
|
785
811
|
|
|
786
|
-
static void
|
|
812
|
+
static void nish_sb_keep_marks(const char *paths) {
|
|
787
813
|
struct stat st;
|
|
788
814
|
if (paths) nish_spawn_sandboxed = nish_sb_spawn_once;
|
|
789
815
|
for (; paths && *paths; paths += strlen(paths) + 1) {
|
|
790
816
|
/* An entry gone since the build has no identity to keep, and Landlock
|
|
791
817
|
* grants it nothing to execute either. */
|
|
792
818
|
if (paths[1] != '/' || stat(paths, &st)) continue;
|
|
793
|
-
int n =
|
|
794
|
-
if (n <
|
|
819
|
+
int n = nish_sb_nmarked++;
|
|
820
|
+
if (n < NISH_SB_MARKED)
|
|
821
|
+
nish_sb_marked[n].dev = st.st_dev, nish_sb_marked[n].ino = st.st_ino, nish_sb_marked[n].exec = paths[2] == '/';
|
|
795
822
|
}
|
|
796
823
|
}
|
|
797
824
|
|
|
798
825
|
void nish_sandbox(int32_t caps, int32_t mode, const char *read_dirs, const char *write_dirs, const char *exec_paths) {
|
|
799
826
|
nish_cpu_prime();
|
|
800
|
-
|
|
827
|
+
nish_sb_keep_marks(exec_paths);
|
|
801
828
|
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) nish_sb_fail("cannot set no_new_privs");
|
|
802
829
|
nish_sb_drop_capabilities();
|
|
803
830
|
nish_sb_landlock(caps, mode, read_dirs, write_dirs, exec_paths);
|