Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/patches/a724193b34d2f82f # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers ## GitHub aien-dev/aienux#510 (code only) closed derived `a724193b34d2f82f` #510 Imported from GitHub aien-dev/aienux#510 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `feddde6255d2490f612756cf3723918e4c03e144` · Head `6f5e280d27fa042aad19200d8a2f69a79ed95c5b` ### Fetch it A git bundle of the pull request's head, 10869 bytes. ``` curl -fsSL https://interplane.io/drakestapleton/aienux/patches/a724193b34d2f82f.bundle -o a724193b34d2f82f.bundle git fetch a724193b34d2f82f.bundle refs/pull/510/head ``` Raw bundle · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/aienux 510 6f5e280d27fa042aad19200d8a2f69a79ed95c5b ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"aienux","number":510,"url":"https://github.com/aien-dev/aienux/pull/510","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:39Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/.crumb b/.crumb index f768bcb9..75e6a96a 100644 --- a/.crumb +++ b/.crumb @@ -33,8 +33,8 @@ "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "d3394f61904c7293fcef5b01a7eff4f5ae347dab0b145fc8e2541fc2d337e6c0", - "last_commit": "341069f9", + "digest": "cca8235751bf317e623ecc5e86d3fb818977b53027dc2b70ac2c5b7f4bd0392e", + "last_commit": "14b4a8b6", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, @@ -51,15 +51,15 @@ "purpose": "First-party host artifact preparation tools" } ], - "children_root": "sha256:b28771660a00c11840a897faf2a805856cd39ada780ca15915af56aedc7ab0a1", - "compiled_at": "2026-11-06T02:05:56Z", + "children_root": "sha256:1cbeab4c388d5065d6de1e065e38f6fe1d851936eca85b86525abd9910b433b3", + "compiled_at": "2026-11-06T02:27:56Z", "compiler": "crumb-compile/1", - "digest": "sha256:34ae2cdef08743d6b400de9cad056e5d66fc39f73f11f40768029738750e3b7d", + "digest": "sha256:974e836b8a4b525a3605edffc85920561815105925f42d6beda773cc79a4c2cf", "evidence_files": 274, "evidence_root": "sha256:af014a4ed8f6fe759fc5809807fb47578d3410811b522d9f65d749382b029802", "files": 3779, - "generated_at_commit": "341069f9f32fe7604c6b28db7fb1f5d375a6fe9d", - "source_tree": "sha256:1b09cfffcf50e0ee6413014e4c2c5762a2fa335cda95c5d18e51cbceb2ab495c" + "generated_at_commit": "14b4a8b63ec35e37fff7074c118a8a06b50bfaf3", + "source_tree": "sha256:91d14a3ce7a684a65121ba20c0d920788c94000b69228ea8fdb3733bea137458" }, "owner_lane": "", "provenance": { diff --git a/native/.crumb b/native/.crumb index db3d42e0..8a929f0f 100644 --- a/native/.crumb +++ b/native/.crumb @@ -79,8 +79,8 @@ "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "18a7c35d88ee9cf2e3b045e9b480d1cb50c094ab1fe8d8cbda470b88b66ced57", - "last_commit": "341069f9", + "digest": "ddf9545df0499aa0cb899809f9eca3bf532fbbdde2ed6c79608c3a331de158a0", + "last_commit": "14b4a8b6", "name": "kernel", "purpose": "Directory native/kernel. Purpose not yet described by a human." }, @@ -115,15 +115,15 @@ "purpose": "Directory native/store. Purpose not yet described by a human." } ], - "children_root": "sha256:cb0e6ce65fe94e3993f746af4b6db22e888d65daab2f81a6ee3ab6cb46d9a473", - "compiled_at": "2026-11-06T02:05:56Z", + "children_root": "sha256:7295118aaf78e62ce48a2d1158aa0222a0e0c12e2f50c6db42420548e65dde2f", + "compiled_at": "2026-11-06T02:27:56Z", "compiler": "crumb-compile/1", - "digest": "sha256:d3394f61904c7293fcef5b01a7eff4f5ae347dab0b145fc8e2541fc2d337e6c0", + "digest": "sha256:cca8235751bf317e623ecc5e86d3fb818977b53027dc2b70ac2c5b7f4bd0392e", "evidence_files": 24, "evidence_root": "sha256:ecc7ccf2e924c221a5bac1d91e23b13d8f3c8d78c2a893a882ed7ca5e88d855d", "files": 779, - "generated_at_commit": "341069f9f32fe7604c6b28db7fb1f5d375a6fe9d", - "source_tree": "sha256:fc442d67be8d66229f1a65d1fc96c41f36bceda4a130dd4110de39f09ceb5229" + "generated_at_commit": "14b4a8b63ec35e37fff7074c118a8a06b50bfaf3", + "source_tree": "sha256:45751e531133bee2084b11fd439448d333537df096b313aaceae69ede6d8fb1e" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/.crumb b/native/kernel/.crumb index c1bc855a..bd8623a6 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -57,8 +57,8 @@ "purpose": "Directory native/kernel/artifact. Purpose not yet described by a human." }, { - "digest": "82f5b707835d6baec04a4d7e87debf00054d9e16fd090292e0f121e56bf05b30", - "last_commit": "38d322ee", + "digest": "bcc16a4b2b8667677b3d5ca23e27dece3e62ae4f2b7aa5e3059eb699fe73ecdf", + "last_commit": "14b4a8b6", "name": "core", "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, @@ -87,8 +87,8 @@ "purpose": "Directory native/kernel/svc. Purpose not yet described by a human." }, { - "digest": "f973f76cc4e15d69d53f58665ce12638eb309d2f7bbf32351cbf44d93354e242", - "last_commit": "25784d18", + "digest": "08af7f33ca615c6516eb2e2d800c9e6cff204aeb90ca47e934e5a24060ab2865", + "last_commit": "14b4a8b6", "name": "tests", "purpose": "Directory native/kernel/tests. Purpose not yet described by a human." }, @@ -99,15 +99,15 @@ "purpose": "Hosted (Linux-built) checkers for the native kernel, run by `make -C native/kernel test`; nothin" } ], - "children_root": "sha256:a341474d105d0feef8eefadfbec2cd6412c5ac3002d8abf436559f6e0de2c3cf", - "compiled_at": "2026-11-06T02:05:56Z", + "children_root": "sha256:8ca5e34f0cd76e78a1531d7764f8c2e2f3d7179d2e24a537c6427604ee39a69a", + "compiled_at": "2026-11-06T02:27:56Z", "compiler": "crumb-compile/1", - "digest": "sha256:18a7c35d88ee9cf2e3b045e9b480d1cb50c094ab1fe8d8cbda470b88b66ced57", + "digest": "sha256:ddf9545df0499aa0cb899809f9eca3bf532fbbdde2ed6c79608c3a331de158a0", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 522, - "generated_at_commit": "341069f9f32fe7604c6b28db7fb1f5d375a6fe9d", - "source_tree": "sha256:d03de191c3736d90532feafdb3306ef71c32677f4882e1b3bccffacb11d64449" + "generated_at_commit": "14b4a8b63ec35e37fff7074c118a8a06b50bfaf3", + "source_tree": "sha256:397b9288bf678b8dd2c9e213cf52508a5d510c2d99e699da137b172eea9af15c" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/GATES.md b/native/kernel/GATES.md index f89baa63..e0b44283 100644 --- a/native/kernel/GATES.md +++ b/native/kernel/GATES.md @@ -883,6 +883,10 @@ scheduler; BROKEN_PIPE with no reader; a write after revoke refused with 0 bytes read succeeding afterwards; a closed handle STALE after its slot is reused; an unknown op and another ABI version NOT_SUPPORTED; cancel INTERRUPTED once with no byte moved. Host test `test_pipe` (the 20-case corpus in `tests/fixtures/pipe/` plus the scheduler transfers, also under ASan+UBSan); a mutant that skips the write-right check must fail it (`make pipe-mutant`). +`ck_pipe_dispatch` copies the caller's slice through a kernel bounce buffer (at most `CK_PIPE_BOUNCE` = 1024 bytes per call) and +never touches it with the pool lock held; a window ending above `CK_PIPE_USER_TOP` is INVALID_ARG (aienux#494). Host +`test_pipe` puts the slice on a page that faults on first touch and checks the ticket lock is free at the fault; the mutants +`make pipe-bounce-mutant` (slice copied under the lock) and `make pipe-top-mutant` (top not checked) must fail it. Limits: (1) the kernel's wait hook yields cooperatively to the scheduler inside one kernel thread; the QEMU image does not context-switch a blocked pipe op onto another core. Host `test_pipe` does, with pthreads: concurrent read/write, close while the peer is parked (including the window after the waiter is published and before `wait` runs), and create/close on a shared diff --git a/native/kernel/Makefile b/native/kernel/Makefile index 607a661f..386fb547 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -1581,6 +1581,25 @@ pipe-slice-mutant: $(OUT)/host/test_pipe_slice_mutant @if $< > $<.log 2>&1; then cat $<.log; echo "test_pipe mutant (user slice not checked): PASSED, test does not catch it"; exit 1; \ else grep -F "test_pipe: FAIL" $<.log && echo "test_pipe mutant (user slice not checked): FAILS as required"; fi +# TEST-ONLY mutants for #494. pipe-bounce-mutant: ck_pipe_dispatch hands the caller's slice to the ring copy, which +# touches it under the pool lock. pipe-top-mutant: a window ending above CK_PIPE_USER_TOP is accepted. test_pipe must +# FAIL against each. +$(OUT)/host/test_pipe_bounce_mutant: $(PIPE_DEPS) + @mkdir -p $(dir $@) + $(HOSTCC) $(PIPE_HCFLAGS) $(PIPE_DEF) -DCK_PIPE_MUTANT_NO_BOUNCE -o $@ $< $(PIPE_SRCS) + +pipe-bounce-mutant: $(OUT)/host/test_pipe_bounce_mutant + @if $< > $<.log 2>&1; then cat $<.log; echo "test_pipe mutant (slice copied under the lock): PASSED, test does not catch it"; exit 1; \ + else grep -F "test_pipe: FAIL" $<.log && echo "test_pipe mutant (slice copied under the lock): FAILS as required"; fi + +$(OUT)/host/test_pipe_top_mutant: $(PIPE_DEPS) + @mkdir -p $(dir $@) + $(HOSTCC) $(PIPE_HCFLAGS) $(PIPE_DEF) -DCK_PIPE_MUTANT_NO_USER_TOP -o $@ $< $(PIPE_SRCS) + +pipe-top-mutant: $(OUT)/host/test_pipe_top_mutant + @if $< > $<.log 2>&1; then cat $<.log; echo "test_pipe mutant (user-VA top not checked): PASSED, test does not catch it"; exit 1; \ + else grep -F "test_pipe: FAIL" $<.log && echo "test_pipe mutant (user-VA top not checked): FAILS as required"; fi + # N3 context assembly + approval binding (core/context.c, core/context_proposal.c, aienos#329): pure logic over # the argus SHA-256. Own rule (needs sha256.c). The mutant skips the foreign-binding check: test_context must # FAIL against it (red before green); "make test" fails if the test stops catching it. Host only (context.c @@ -1712,8 +1731,8 @@ smmu-l2-mutants: $(OUT)/host/test_smmu else echo "mutant flag refused under CK_HARDWARE_STAGING"; fi; \ if [ $$fail = 0 ]; then echo "CK_SMMU_L2_MUTANTS: PASS"; else echo "CK_SMMU_L2_MUTANTS: FAIL"; exit 1; fi -test: $(addprefix $(OUT)/host/,$(TESTS)) osc-receipt-mutants osc-receipt-release osc-code-flip-guard handoff-mutant acpi-dev-mutant iort-mutants smmu-l2-mutants osh-op-mutant pipe-mutant pipe-slice-mutant context-mutant agent-cmd-mutant allen-gate-mutant artifact-card-mutant artifact-tier-pi5 artifact-tier-mutant test-continuity-subject-restart gsp-fw-inspect-test gsp-cot-test gsp-bindata-test gsp-bootargs-test gsp-rpc-test gb10-gsp-free gb10-gsp-nvhdr-check gb10-gsp-mutants slot-test slot-free package-test slot-loader-check - @fail=0; for t in $(filter-out osc-receipt-mutants osc-receipt-release osc-code-flip-guard handoff-mutant acpi-dev-mutant iort-mutants smmu-l2-mutants osh-op-mutant pipe-mutant pipe-slice-mutant context-mutant agent-cmd-mutant allen-gate-mutant artifact-card-mutant artifact-tier-pi5 artifact-tier-mutant test-continuity-subject-restart gsp-fw-inspect-test gsp-cot-test gsp-bindata-test gsp-bootargs-test gsp-rpc-test gb10-gsp-free gb10-gsp-nvhdr-check gb10-gsp-mutants slot-test slot-free package-test slot-loader-check,$^); do $$t || fail=1; done; \ +test: $(addprefix $(OUT)/host/,$(TESTS)) osc-receipt-mutants osc-receipt-release osc-code-flip-guard handoff-mutant acpi-dev-mutant iort-mutants smmu-l2-mutants osh-op-mutant pipe-mutant pipe-slice-mutant pipe-bounce-mutant pipe-top-mutant context-mutant agent-cmd-mutant allen-gate-mutant artifact-card-mutant artifact-tier-pi5 artifact-tier-mutant test-continuity-subject-restart gsp-fw-inspect-test gsp-cot-test gsp-bindata-test gsp-bootargs-test gsp-rpc-test gb10-gsp-free gb10-gsp-nvhdr-check gb10-gsp-mutants slot-test slot-free package-test slot-loader-check + @fail=0; for t in $(filter-out osc-receipt-mutants osc-receipt-release osc-code-flip-guard handoff-mutant acpi-dev-mutant iort-mutants smmu-l2-mutants osh-op-mutant pipe-mutant pipe-slice-mutant pipe-bounce-mutant pipe-top-mutant context-mutant agent-cmd-mutant allen-gate-mutant artifact-card-mutant artifact-tier-pi5 artifact-tier-mutant test-continuity-subject-restart gsp-fw-inspect-test gsp-cot-test gsp-bindata-test gsp-bootargs-test gsp-rpc-test gb10-gsp-free gb10-gsp-nvhdr-check gb10-gsp-mutants slot-test slot-free package-test slot-loader-check,$^); do $$t || fail=1; done; \ if [ $$fail = 0 ]; then echo "CK_CORE_HOST: PASS"; else echo "CK_CORE_HOST: FAIL"; exit 1; fi sanitize: $(addprefix $(OUT)/host-san/,$(TESTS)) diff --git a/native/kernel/core/.crumb b/native/kernel/core/.crumb index 8bc2eb57..d4a52dcd 100644 --- a/native/kernel/core/.crumb +++ b/native/kernel/core/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-11-06T00:33:49Z", + "compiled_at": "2026-11-06T02:27:56Z", "compiler": "crumb-compile/1", - "digest": "sha256:82f5b707835d6baec04a4d7e87debf00054d9e16fd090292e0f121e56bf05b30", + "digest": "sha256:bcc16a4b2b8667677b3d5ca23e27dece3e62ae4f2b7aa5e3059eb699fe73ecdf", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 78, - "generated_at_commit": "6b91f3f0e54cd3323f6f1459460b4bf0c09e1c80", - "source_tree": "sha256:fb977c5f80f6fa476c9db2ee69c38b261cb91c481e4bee18c9f2a21dc7c0ee96" + "generated_at_commit": "14b4a8b63ec35e37fff7074c118a8a06b50bfaf3", + "source_tree": "sha256:6c451604c91dc1be1c996e7781a3e8a4a74b74d9afdb7e320dbd1d9cba1c05b9" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/core/pipe.c b/native/kernel/core/pipe.c index c8919d9d..9c23310f 100644 --- a/native/kernel/core/pipe.c +++ b/native/kernel/core/pipe.c @@ -548,18 +548,59 @@ static const uint8_t op_args[] = { [CK_PIPE_OP_CREATE] = 0, [CK_PIPE_OP_READ] = #define OP_MAX CK_PIPE_OP_DUP #ifndef CK_PIPE_MUTANT_SKIP_SLICE -/* 1 when [s, s+len) lies in [lo, hi). len 0 passes. Otherwise no s+len add: a wrapping length fails. */ +/* 1 when [s, s+len) lies in [lo, hi) and hi is at most the user-VA top. len 0 passes. Otherwise no s+len add: a + * wrapping length fails. */ static int slice_ok(const void *s, size_t len, uintptr_t lo, uintptr_t hi) { uintptr_t b = (uintptr_t)s; if (len == 0) return 1; +#ifndef CK_PIPE_MUTANT_NO_USER_TOP + if (hi > CK_PIPE_USER_TOP) + return 0; +#endif if (lo >= hi || b < lo || b >= hi) return 0; return len <= hi - b; } #endif +#ifndef CK_PIPE_MUTANT_NO_BOUNCE +/* Copy between the caller's slice and the kernel bounce buffer. Never called with the pool lock held. */ +static void slice_copy(uint8_t *d, const uint8_t *s, size_t n) +{ + for (size_t i = 0; i < n; i++) + d[i] = s[i]; +} +#endif + +/* READ / WRITE through the bounce buffer: the slice is only touched here, before ck_stream_write takes the lock or + * after ck_stream_read dropped it. At most CK_PIPE_BOUNCE bytes per call. */ +static unsigned slice_rw(struct ck_pipe_pool *p, struct ck_cap_table *caller, struct ck_handle h, unsigned op, + uint8_t *slice, size_t slice_len, unsigned flags, size_t *n) +{ +#ifdef CK_PIPE_MUTANT_NO_BOUNCE + /* TEST-ONLY (make pipe-bounce-mutant): the ring copy reads and writes the slice under the lock. test_pipe must + * fail. */ + return op == CK_PIPE_OP_READ ? ck_stream_read(p, caller, h, slice, slice_len, flags, n) + : ck_stream_write(p, caller, h, slice, slice_len, flags, n); +#else + uint8_t bounce[CK_PIPE_BOUNCE]; + size_t len = slice_len < CK_PIPE_BOUNCE ? slice_len : CK_PIPE_BOUNCE; + unsigned e; + if (op == CK_PIPE_OP_WRITE) { + slice_copy(bounce, slice, len); + e = ck_stream_write(p, caller, h, len ? bounce : 0, len, flags, n); + } else { + e = ck_stream_read(p, caller, h, len ? bounce : 0, len, flags, n); + if (e == OSH_E_OK && *n) + slice_copy(slice, bounce, *n); + } + snap_zero(bounce, len); /* pipe bytes do not stay on the kernel stack */ + return e; +#endif +} + unsigned ck_pipe_dispatch(struct ck_pipe_pool *p, struct ck_cap_table *caller, struct ck_cap_table *peer, const uint64_t *frame, size_t ncells, uint8_t *slice, size_t slice_len, uintptr_t user_lo, uintptr_t user_hi, struct ck_pipe_ret *ret) @@ -615,8 +656,7 @@ unsigned ck_pipe_dispatch(struct ck_pipe_pool *p, struct ck_cap_table *caller, s break; } #endif - e = op == CK_PIPE_OP_READ ? ck_stream_read(p, caller, h, slice, slice_len, (unsigned)frame[5], &n) - : ck_stream_write(p, caller, h, slice, slice_len, (unsigned)frame[5], &n); + e = slice_rw(p, caller, h, op, slice, slice_len, (unsigned)frame[5], &n); r.value = n; break; case CK_PIPE_OP_CLOSE: diff --git a/native/kernel/core/pipe.h b/native/kernel/core/pipe.h index 633fefec..187f2c7f 100644 --- a/native/kernel/core/pipe.h +++ b/native/kernel/core/pipe.h @@ -39,8 +39,9 @@ * A blocking call returns as soon as it moved at least one byte (partial I/O; the caller loops). The kernel's hook * yields to the scheduler; the host test simulates it. Without a wait hook a blocking call returns WOULD_BLOCK. * A blocking write copies the bytes it may commit into a kernel buffer before it parks (at most CK_PIPE_WRITE_SNAP - * of them) and the retry reads that buffer, not the caller memory. A write that does not park copies once, under - * the pool lock, straight into the ring. + * of them) and the retry reads that buffer, not the caller memory. A direct ck_stream_write that does not park + * copies once, under the pool lock, straight into the ring; ck_pipe_dispatch never hands it caller memory (see the + * user-window note below). * * Locking. One ticket lock covers the pool (every pipe, the ref counts, the ring, the stats, the live count). * core/smp.c is the kernel's SMP primitive and it synchronises with __atomic_*; there is no separate spinlock @@ -55,14 +56,19 @@ * ck_pipe_pool_init is single-threaded, before the pool is shared. * * User window. ck_pipe_dispatch rejects a read or a write whose slice lies outside [user_lo, user_hi) with - * INVALID_ARG, before anything reads or writes the slice. A length of 0 passes. Otherwise the base must sit in + * INVALID_ARG, before anything reads or writes the slice. A length of 0 passes. Otherwise user_hi must be at most + * CK_PIPE_USER_TOP (the window ends at or below the top of user VA: TTBR1 space starts at 0xFFFF000000000000 on + * ARM64; the x86 canonical lower half ends at 0x0000800000000000 with 4-level paging), the base must sit in * [user_lo, user_hi) and the length must be <= user_hi - base. The check never adds base and length, so a length * that would wrap the address space fails. ck_stream_read and ck_stream_write do not repeat it; a direct caller * passes a kernel buffer it already owns. - * The syscall layer still has two duties. It must assert, on each architecture, that user_hi is below the user-VA - * top: below TTBR1 (0xFFFF000000000000) on ARM64, and below the canonical upper half on x86. It must copy the - * slice in and out through a kernel bounce buffer outside the pool lock, so a user fault never runs while the - * ticket lock is held. + * Bounce. ck_pipe_dispatch never touches the caller's slice while the pool lock is held. A write copies at most + * CK_PIPE_BOUNCE bytes of the slice into a kernel stack buffer before it calls ck_stream_write; a read reads at + * most CK_PIPE_BOUNCE bytes into that buffer through ck_stream_read and copies them out to the slice after the + * lock is dropped. A longer slice moves a partial count (section 8.1 allows partial I/O; the caller loops). A + * fault on the slice therefore never runs inside the ticket lock. The copy is still a plain load/store loop: the + * syscall layer, once there is one, supplies fault recovery around it. A read whose copy-out faults has already + * consumed those bytes from the ring. * * Memory: a fixed pool of CK_PIPE_POOL_PIPES pipes with CK_PIPE_CAPACITY bytes each, all static; nothing grows. * Pure logic, no hardware: host-tested in tests/test_pipe.c (fixtures in tests/fixtures/pipe/, plus host threads @@ -109,6 +115,20 @@ enum ck_pipe_op { * secondary-core stack (core/smp.c). A longer blocking write returns a partial count and the caller loops. */ #define CK_PIPE_WRITE_SNAP 1024u _Static_assert(CK_PIPE_WRITE_SNAP >= 1u && CK_PIPE_WRITE_SNAP <= 4096u, "snapshot must fit a secondary stack"); +/* Bytes ck_pipe_dispatch moves per READ or WRITE call through its kernel bounce buffer. It sits on the stack next to + * a blocking write's snapshot, so the two together stay under 4 KiB (same secondary-stack budget as above). */ +#define CK_PIPE_BOUNCE 1024u +_Static_assert(CK_PIPE_BOUNCE >= 1u && CK_PIPE_BOUNCE + CK_PIPE_WRITE_SNAP <= 4096u, "bounce + snapshot must fit"); + +/* Top of user VA (exclusive): a dispatch window must end at or below it. The x86 value assumes 4-level paging; + * x86/kmain_x86.c panics if firmware runs 5-level paging (CR4.LA57). */ +#if defined(__aarch64__) +#define CK_PIPE_USER_TOP ((uintptr_t)0xFFFF000000000000ull) /* TTBR1 (kernel) space starts here */ +#elif defined(__x86_64__) +#define CK_PIPE_USER_TOP ((uintptr_t)0x0000800000000000ull) /* end of the canonical lower half, 4-level paging */ +#else +#error "pipe.h: no user-VA top for this architecture" +#endif /* Rights a create gives each end beyond its data right, and the only rights a dup may carry. */ #define CK_PIPE_CTL_RIGHTS (CK_R_DERIVE | CK_R_GRANT | CK_R_REVOKE) @@ -202,8 +222,9 @@ unsigned ck_pipe_release_table(struct ck_pipe_pool *p, struct ck_cap_table *t); * than 4 cells, bad magic or nonzero reserved -> INVALID_ARG; abi_version not CK_PIPE_ABI_VERSION or an unknown op * -> NOT_SUPPORTED; wrong cell count, a generation-0 handle or unknown flag bits -> INVALID_ARG. * Result: {error, value} as in section 8; PIPE_CREATE also fills value2 (the write end). value2 is UNVERIFIED. - * READ and WRITE then require slice[0, slice_len) inside [user_lo, user_hi), user_hi exclusive. A length of 0 - * passes. Any other slice is INVALID_ARG before the buffer is touched (see the user-window note above). + * READ and WRITE then require slice[0, slice_len) inside [user_lo, user_hi), user_hi exclusive and at most + * CK_PIPE_USER_TOP. A length of 0 passes. Any other slice is INVALID_ARG before the buffer is touched (see the + * user-window note above). At most CK_PIPE_BOUNCE bytes move per call, through a kernel bounce buffer. * user_lo and user_hi are the caller's window; kernel self-tests pass the bounds of their own buffer. */ struct ck_pipe_ret { uint32_t error; diff --git a/native/kernel/tests/.crumb b/native/kernel/tests/.crumb index 49e53f2c..ead28e17 100644 --- a/native/kernel/tests/.crumb +++ b/native/kernel/tests/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-11-06T00:50:27Z", + "compiled_at": "2026-11-06T02:27:56Z", "compiler": "crumb-compile/1", - "digest": "sha256:f973f76cc4e15d69d53f58665ce12638eb309d2f7bbf32351cbf44d93354e242", + "digest": "sha256:08af7f33ca615c6516eb2e2d800c9e6cff204aeb90ca47e934e5a24060ab2865", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 221, - "generated_at_commit": "47973bb401c35ff45002e8296a216a1558f663e0", - "source_tree": "sha256:f93908806410e11a1b945b1ebae3cdf0485f49f2d172809e16fb7dbde6d23bd4" + "generated_at_commit": "14b4a8b63ec35e37fff7074c118a8a06b50bfaf3", + "source_tree": "sha256:531ba0abdf3da403ab27765cc728612340cbbf0723160ce4a06c4812b9c65170" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/tests/test_pipe.c b/native/kernel/tests/test_pipe.c index 3edeabb0..5f783d0c 100644 --- a/native/kernel/tests/test_pipe.c +++ b/native/kernel/tests/test_pipe.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include "ck_test.h" @@ -1174,6 +1175,104 @@ static void test_slice_window(void) CHECK(sigaction(SIGBUS, &old_bus, 0) == 0); } +/* #494: ck_pipe_dispatch touches the slice only outside the pool lock. The slice sits on a page that faults on the + * first access; the handler records whether the ticket lock was held at that moment, then opens the page so the + * access retries. Before the bounce buffer, the ring copy faulted inside the lock. */ +static volatile sig_atomic_t bounce_faults, bounce_held; +static uint8_t *bounce_page; +static size_t bounce_pagesz; + +static void bounce_fault(int sig) +{ + (void)sig; + bounce_faults++; + if (__atomic_load_n(&pool.lock_ticket, __ATOMIC_RELAXED) != __atomic_load_n(&pool.lock_serving, __ATOMIC_RELAXED)) + bounce_held = 1; + if (mprotect(bounce_page, bounce_pagesz, PROT_READ | PROT_WRITE) != 0) + _Exit(2); +} + +static void bounce_arm(int prot) +{ + bounce_faults = 0; + bounce_held = 0; + CHECK(mprotect(bounce_page, bounce_pagesz, prot) == 0); +} + +static void test_slice_bounce(void) +{ + fresh_tables(); + ck_pipe_pool_init(&pool, 0); + long ps = sysconf(_SC_PAGESIZE); + CHECK(ps >= (long)CK_PIPE_BOUNCE + 8); + bounce_pagesz = (size_t)ps; + void *pg = mmap(0, bounce_pagesz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + CHECK(pg != MAP_FAILED); + if (pg == MAP_FAILED) + return; + bounce_page = pg; + struct sigaction sa, old_segv, old_bus; + memset(&sa, 0, sizeof sa); + sa.sa_handler = bounce_fault; + sigemptyset(&sa.sa_mask); + CHECK(sigaction(SIGSEGV, &sa, &old_segv) == 0); + CHECK(sigaction(SIGBUS, &sa, &old_bus) == 0); + + struct ck_pipe_ret ret; + uint64_t create[4] = { CK_PIPE_FRAME_MAGIC, CK_PIPE_ABI_VERSION, CK_PIPE_OP_CREATE, 0 }; + CHECK(ck_pipe_dispatch(&pool, &ta, &tb, create, 4, 0, 0, 0, 0, &ret) == OSH_E_OK); + uint64_t rd = ret.value, wr = ret.value2; + uintptr_t lo = (uintptr_t)bounce_page, hi = lo + bounce_pagesz; + + /* WRITE: the slice is read before the lock is taken. */ + memcpy(bounce_page, "bounce", 6); + bounce_arm(PROT_NONE); + CHECK(frame_rw(1, wr, bounce_page, 6, lo, hi, &ret) == OSH_E_OK && ret.value == 6); + CHECK(bounce_faults == 1 && bounce_held == 0); + CHECK(pool.pipes[0].len == 6); + + /* READ: the slice is written after the lock is dropped. */ + memset(bounce_page, 0, 16); + bounce_arm(PROT_READ); + CHECK(frame_rw(0, rd, bounce_page, 16, lo, hi, &ret) == OSH_E_OK && ret.value == 6); + CHECK(bounce_faults == 1 && bounce_held == 0); + CHECK(!memcmp(bounce_page, "bounce", 6) && pool.pipes[0].len == 0); + + /* A slice longer than the bounce buffer moves CK_PIPE_BOUNCE bytes; the caller loops for the rest. */ + bounce_arm(PROT_READ | PROT_WRITE); + for (size_t i = 0; i < CK_PIPE_BOUNCE + 7; i++) + bounce_page[i] = (uint8_t)(i * 7u + 1u); + CHECK(frame_rw(1, wr, bounce_page, CK_PIPE_BOUNCE + 7, lo, hi, &ret) == OSH_E_OK && ret.value == CK_PIPE_BOUNCE); + CHECK(frame_rw(1, wr, bounce_page + CK_PIPE_BOUNCE, 7, lo, hi, &ret) == OSH_E_OK && ret.value == 7); + CHECK(pool.pipes[0].len == CK_PIPE_BOUNCE + 7); + uint8_t back[CK_PIPE_BOUNCE + 7]; + CHECK(frame_rw(0, rd, back, sizeof back, (uintptr_t)back, (uintptr_t)back + sizeof back, &ret) == OSH_E_OK && + ret.value == CK_PIPE_BOUNCE); + CHECK(frame_rw(0, rd, back + CK_PIPE_BOUNCE, 7, (uintptr_t)back, (uintptr_t)back + sizeof back, &ret) == + OSH_E_OK && + ret.value == 7); + CHECK(!memcmp(back, bounce_page, sizeof back) && pool.pipes[0].len == 0); + + /* A window that ends above the user-VA top is refused before the slice is touched, for both directions. */ + bounce_arm(PROT_NONE); + CHECK(frame_rw(1, wr, bounce_page, 4, lo, CK_PIPE_USER_TOP + 1, &ret) == OSH_E_INVALID_ARG && ret.value == 0); + CHECK(frame_rw(0, rd, bounce_page, 4, lo, CK_PIPE_USER_TOP + 1, &ret) == OSH_E_INVALID_ARG && ret.value == 0); + CHECK(frame_rw(1, wr, bounce_page, 4, lo, UINTPTR_MAX, &ret) == OSH_E_INVALID_ARG && ret.value == 0); + CHECK(bounce_faults == 0 && pool.pipes[0].len == 0); + /* The top itself is a legal exclusive end. */ + bounce_arm(PROT_READ | PROT_WRITE); + CHECK(frame_rw(1, wr, bounce_page, 4, lo, CK_PIPE_USER_TOP, &ret) == OSH_E_OK && ret.value == 4); + + uint64_t cl[5] = { CK_PIPE_FRAME_MAGIC, CK_PIPE_ABI_VERSION, CK_PIPE_OP_CLOSE, 0, rd }; + CHECK(ck_pipe_dispatch(&pool, &ta, &tb, cl, 5, 0, 0, 0, 0, &ret) == OSH_E_OK); + cl[4] = wr; + CHECK(ck_pipe_dispatch(&pool, &ta, &tb, cl, 5, 0, 0, 0, 0, &ret) == OSH_E_OK); + CHECK(all_empty()); + CHECK(sigaction(SIGSEGV, &old_segv, 0) == 0); + CHECK(sigaction(SIGBUS, &old_bus, 0) == 0); + CHECK(munmap(bounce_page, bounce_pagesz) == 0); +} + int main(int argc, char **argv) { _Static_assert(CK_PIPE_CAPACITY >= 65536u, "section 8.5 minimum"); @@ -1185,6 +1284,7 @@ int main(int argc, char **argv) test_epoch_and_forgery(); test_dispatch(); test_slice_window(); + test_slice_bounce(); test_release_count(); test_toctou_snapshot(); CHECK(pthread_mutex_init(&host.mu, 0) == 0); ```