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/dbb31fb3d9ddf6f8 # 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#413 (code only) closed derived `dbb31fb3d9ddf6f8` #413 Imported from GitHub aien-dev/aienux#413 (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 `ee34c7d98b9d4fc10103441796c93b2b29413423` · Head `481155773792941da83cb2517ec7e28792d230b1` ### Fetch it A git bundle of the pull request's head, 12489 bytes. ``` curl -fsSL https://interplane.io/drakestapleton/aienux/patches/dbb31fb3d9ddf6f8.bundle -o dbb31fb3d9ddf6f8.bundle git fetch dbb31fb3d9ddf6f8.bundle refs/pull/413/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 413 481155773792941da83cb2517ec7e28792d230b1 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"aienux","number":413,"url":"https://github.com/aien-dev/aienux/pull/413","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:22Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/.crumb b/.crumb index 28e4e4f2..506d104b 100644 --- a/.crumb +++ b/.crumb @@ -33,27 +33,27 @@ "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "64cc87f8507bb580e1788d335c5a76039f3e3a016552595526718448ab5aa098", - "last_commit": "6ad9190", + "digest": "0f000acf8a40d0daecde441e94121543b8a45f88dc9e2dd53daf14473a9cf9f5", + "last_commit": "a8a1fd3", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { - "digest": "9adb69cc749bc97fa48e268f0397b900da83c84191ab6a9373358e8b9ab57922", - "last_commit": "f635add", + "digest": "8a783c03b1dab4e87a2c2df8933d490baf2fac25064667791f405046841d0d72", + "last_commit": "a8a1fd3", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:19cc8f21ac13e49018f60836bdaf7c4c23477cf558624048754f1e83c6d88cac", - "compiled_at": "2026-10-18T12:29:38Z", + "children_root": "sha256:18713944ca20331761857bd26927bc55b46639bf94479bf226b0c5e61dcfbb7c", + "compiled_at": "2026-10-18T15:08:53Z", "compiler": "crumb-compile/1", - "digest": "sha256:306082635f09fe7ba90bcf0ecc06b7cf4c24e5f13023479193bb80930d982081", - "evidence_files": 211, - "evidence_root": "sha256:3a8f56415109a697e4beb6048c85805c50c9c6c4089629057f4e8b3e76863981", - "files": 3501, - "generated_at_commit": "3d2500e11fb42496dd136e3bb3bf8ee98f94dcd7", - "source_tree": "sha256:0ca10e6b509c0b5b6a0e08ee6b6e32313a45d3910fd2f11d3ac364891c715a3a" + "digest": "sha256:434bc078c65533f86af017185e37cf9698f7abef0f9644ceca7d877f154a58cd", + "evidence_files": 212, + "evidence_root": "sha256:0d99722c814d3eb8b67bb630031b6c6547ffff8eb4969c356ecbb97c3904c42a", + "files": 3502, + "generated_at_commit": "efebd0257fc1feb4ff5037d10ed45be0d817979b", + "source_tree": "sha256:a05b583d54262715eff07cb2f5d03ceba783e6f8b4a6a1991faf71bfec81dc23" }, "owner_lane": "", "provenance": { diff --git a/evidence/imac_wx_2026-10-17/README.md b/evidence/imac_wx_2026-10-17/README.md new file mode 100644 index 00000000..a44d7318 --- /dev/null +++ b/evidence/imac_wx_2026-10-17/README.md @@ -0,0 +1,48 @@ +# iMac physical run: W^X (#409) + report opt-in (#410), 2026-10-17 + +The 2017 iMac (iMac18,3, Core i5-7500) started the combined image of PR #413 (W^X) and +PR #412 (network report opt-in, default off) from the SanDisk USB stick on the owner's +Option-key boot picker. The iMac has no serial port and the default image sends nothing, +so the only record is the screen. The owner sent a photo; the lines below are +transcribed from it. The photo itself is not stored here. + +| Field | Value | +|---|---| +| Image | `EFI/BOOT/BOOTX64.EFI` sha256 `ea12b2ab0a9b05a49e5a090ad10502b2cb4272747d2ce5e390cf6107a5add35f` | +| Source | local merge of #413 (9c8ce2b) and #412 (4031c13) + crumb recompile, commit e0c033a55039663f5e524fd7915635c5e7952817 (shown on screen as aienos_commit) | +| Same image in QEMU | `AIENOS_IMAC_KERNEL: PASS` (both CPU models, mutations 1 and 2 caught) | +| Photo sha256 | `4e9ac2fc2bf3a96067599ad644d45557dc72afb6283897600ca434687bdb437a` (kept by the operator, not in this repo) | +| Network | cable connected, link up 1000 Mb/s full duplex | + +## Lines (as shown) +``` +aienos_commit: e0c033a55039663f5e524fd7915635c5e7952817 +cpu: GenuineIntel Intel(R) Core(TM) i5-7500 CPU @ 3.40GHz +paging: root=0x2000 firmware_cr3=0x84000000 switched=yes top_gib=18 pages_2m_wb=8142 pages_2m_wc=17 pages_2m_uc=1055 pt_frames=22 fb=wc pat1=wc +guard_page: ok fault=contained (#PF cr2=0x7e5ae000 error=0x0, not present) +wx: ok nx=on wp=on text=rx ro=r data=rw-nx text_write=fault ro_write=fault heap_exec=fault stack_exec=fault +timebase: hpet period_fs=41666667 tsc_hz=3408333528 +wx_audit: ok wx_leaves=0 exec_pages=15 text_pages=15 pages_4k=1024 trampoline=retired +smp: cpus=4 started=3 failed=0 bsp_id=0 ap_ids=2,4,6 trampoline=0x1000 state=halted +net_tg3: link up 1000 Mb/s full duplex +storage: pci_functions=26 ahci_controllers=1 disks=1 nvme_controllers=1 nvme_disks=1 gpt_valid=1 writes=none +kernel: alive +summary: cores=4/4 timer=ok sata_disks=1 nvme_disks=1 gpt_valid=1 writes=none net=link-up +AIENOS-IMAC-KERNEL-0 DONE +power: holding the screen 60 s, then ACPI S5 +``` + +## What this shows +- #409 on real hardware: NX and CR0.WP on; writing code, writing read-only data, executing + the heap and executing the stack each fault and are contained; the audit finds zero + writable+executable leaves and exactly the code pages executable; all three secondary + cores start through the NXE/WP trampoline, which is then retired. +- #410 on real hardware: with the link up, no `report:` line appears; the default image + links no report code, so nothing was broadcast. + +## Limits +- One run, one machine. The photo was taken during the hold; the owner reported afterwards that + the iMac switched itself off after the 60 s hold (ACPI S5 still works with W^X on). +- The mutation (negative) builds ran in QEMU only, not on the iMac. +- W^X is not process isolation. VT-d DMA confinement (#411) is not done. +- The NVMe GPT is refused (no protective MBR), as in earlier runs; unrelated to this change. diff --git a/native/.crumb b/native/.crumb index dbbce44e..87a5cc50 100644 --- a/native/.crumb +++ b/native/.crumb @@ -50,7 +50,7 @@ "children": [ { "digest": "50aa2fbd6ca356b7703517bc7ecf439f7b6e0a6b935d0ca6ca4c6f7caf23ce34", - "last_commit": "4bd49ca", + "last_commit": "09e59a8", "name": "argus", "purpose": "Directory native/argus. Purpose not yet described by a human." }, @@ -62,62 +62,62 @@ }, { "digest": "750c519c466783ed6330a6a2c8fcb2153efdaadb9d65b38a6a85e8463d78b68d", - "last_commit": "4bd49ca", + "last_commit": "7db773a", "name": "capability", "purpose": "Directory native/capability. Purpose not yet described by a human." }, { "digest": "0a3e98a5f36f528eb7ae6e1611d466343068780b9029621286b292ec1c796950", - "last_commit": "4bd49ca", + "last_commit": "7db773a", "name": "crypto", "purpose": "Directory native/crypto. Purpose not yet described by a human." }, { "digest": "99bc55e79695290b65ae08a38e5731e3ed976d79cdb3b6c9bc21c706b1ddab22", - "last_commit": "4bd49ca", + "last_commit": "7db773a", "name": "disk", "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "7a81b1b4b47154e15b9682b72d7fa49f309454f093aeb019061b8148550920ae", - "last_commit": "3037967", + "digest": "a8cc94bc77c686aa01f4eefe8320f780f83a3f3d729f7700afa2ce4b8214ae17", + "last_commit": "a8a1fd3", "name": "kernel", "purpose": "Directory native/kernel. Purpose not yet described by a human." }, { "digest": "a805df7e3af4b8a2ef345223523b54e7e47565204ec8f8e598f6dcaf53b07419", - "last_commit": "4bd49ca", + "last_commit": "7db773a", "name": "m5", "purpose": "Directory native/m5. Purpose not yet described by a human." }, { "digest": "ff5c88f5e9bf86e7ff836d24c048dd2d4efc94f6f19a21a0caf72ae9c402deaf", - "last_commit": "4bd49ca", + "last_commit": "7db773a", "name": "net", "purpose": "Directory native/net. Purpose not yet described by a human." }, { "digest": "135fd1edbe2388764de7dc755611b483b0c61adab858718c390b7745d141635c", - "last_commit": "4bd49ca", + "last_commit": "7db773a", "name": "sig", "purpose": "Directory native/sig. Purpose not yet described by a human." }, { "digest": "f75197d76b7daaeee8223cf25353251824cd4497400a439724a3a424e014a229", - "last_commit": "4bd49ca", + "last_commit": "e5e8f08", "name": "store", "purpose": "Directory native/store. Purpose not yet described by a human." } ], - "children_root": "sha256:cec690f1b64aaba9f5f7ea41048a25c5b3cdf4834f672e977dc7eb47d34e4be2", - "compiled_at": "2026-10-17T01:49:45Z", + "children_root": "sha256:c353064aa230065244a57cdeea701ed107febd8ee123b3241d11f613e95dd6a5", + "compiled_at": "2026-10-17T16:57:40Z", "compiler": "crumb-compile/1", - "digest": "sha256:64cc87f8507bb580e1788d335c5a76039f3e3a016552595526718448ab5aa098", + "digest": "sha256:0f000acf8a40d0daecde441e94121543b8a45f88dc9e2dd53daf14473a9cf9f5", "evidence_files": 4, "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335", "files": 606, - "generated_at_commit": "30379678add587e388db4e11713d6bca8d159b08", - "source_tree": "sha256:1d9fe182bcaa94de3564ee02f9e8c63bddcea00fcea22feed112ffe6e01a1b78" + "generated_at_commit": "a8a1fd36cfaa273afd67987fa250da4a0d8eac7a", + "source_tree": "sha256:9a256d2ecbc153951a0c92be66c9aaf13b5c6f8756782ff515a6c5d36340c202" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/.crumb b/native/kernel/.crumb index 57d1b019..8382d376 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -46,43 +46,43 @@ "children": [ { "digest": "8f63182b423b274a3fa6dcf0a17811343f93bd1539e33479102ffa87935da1ca", - "last_commit": "4bd49ca", + "last_commit": "2529bc0", "name": "arch", "purpose": "Directory native/kernel/arch. Purpose not yet described by a human." }, { "digest": "cd3be26829bc7f947521b1e5df144df2504ef25a3763ec0356861b7d17d5d901", - "last_commit": "4bd49ca", + "last_commit": "6ad9190", "name": "artifact", "purpose": "Directory native/kernel/artifact. Purpose not yet described by a human." }, { "digest": "c9035b8f32a9539fe8d1f93ed968b3b56dd3ba4c2630b32b6c286b1dbd19d908", - "last_commit": "4bd49ca", + "last_commit": "6ad9190", "name": "core", "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, { "digest": "ebdd66c0966fba664f36dc167836b98d26ffae97f9502b4409e9c22da687b473", - "last_commit": "4bd49ca", + "last_commit": "9404c9e", "name": "dev", "purpose": "Directory native/kernel/dev. Purpose not yet described by a human." }, { "digest": "8198bb9a0fbd634281a251b012eca8874c97e469a45eec8b5a6bf06a7e828f9a", - "last_commit": "4bd49ca", + "last_commit": "0735543", "name": "include", "purpose": "Directory native/kernel/include. Purpose not yet described by a human." }, { "digest": "8252fab960e4a3bb686f2e864b0d9a0571f43381d5006de2ff6c134c132ef092", - "last_commit": "4bd49ca", + "last_commit": "2bc7146", "name": "mm", "purpose": "Directory native/kernel/mm. Purpose not yet described by a human." }, { "digest": "2a98333d826d94a7131119aaeb5a15d8bc543baa5531fa930e6f0afc1fccb83d", - "last_commit": "4bd49ca", + "last_commit": "15c19d4", "name": "svc", "purpose": "Directory native/kernel/svc. Purpose not yet described by a human." }, @@ -100,14 +100,14 @@ } ], "children_root": "sha256:658a040f493b6ff9dc4a775b2b73a0c89ded592a235231016dd8594094530e0f", - "compiled_at": "2026-10-17T01:49:45Z", + "compiled_at": "2026-10-17T16:57:40Z", "compiler": "crumb-compile/1", - "digest": "sha256:7a81b1b4b47154e15b9682b72d7fa49f309454f093aeb019061b8148550920ae", + "digest": "sha256:a8cc94bc77c686aa01f4eefe8320f780f83a3f3d729f7700afa2ce4b8214ae17", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 421, - "generated_at_commit": "30379678add587e388db4e11713d6bca8d159b08", - "source_tree": "sha256:cfa9d56bd0bae125be856e13a57758e007e620d1fc9501643728e676f53eaabe" + "generated_at_commit": "a8a1fd36cfaa273afd67987fa250da4a0d8eac7a", + "source_tree": "sha256:68d008efb26ce4822d30225eefbfcbd413f70411f65ffbdbc1a6763302627ef6" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/Makefile b/native/kernel/Makefile index 20a92ad3..d50c9eff 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -2310,6 +2310,13 @@ $(IMACKERN_EFI): $(IMACKERN_ELF) @[ "$$(od -An -tx1 -j68 -N2 $@ | tr -d ' ')" = "6486" ] || { echo "PE Machine is not x86_64 (0x8664)"; rm -f $@; exit 1; } @echo "built $@ (commit $(AIENOS_COMMIT))" imac-kernel: $(IMACKERN_EFI) +# Negative test builds for W^X (#409), QEMU only and never deployed: CK_X86_WX_MUTATE=1 leaves RAM +# executable, =2 leaves the kernel's code writable, and the kernel's W^X probes must then panic +# (scripts/qemu_ck_imac_kernel_test.sh). Build them into their own OUT directory. +CK_X86_WX_MUTATE ?= 0 +ifneq ($(CK_X86_WX_MUTATE),0) +IMACKERN_CFLAGS += -DCK_X86_WX_MUTATE=$(CK_X86_WX_MUTATE) +endif # Raspberry Pi 5 board image (see CK_PI5 above): target/native-kernel-pi5/BOOTAA64.EFI pi5-store: diff --git a/native/kernel/x86/kmain_x86.c b/native/kernel/x86/kmain_x86.c index e6f0b871..d785b646 100644 --- a/native/kernel/x86/kmain_x86.c +++ b/native/kernel/x86/kmain_x86.c @@ -7,7 +7,10 @@ * allocator over the memory map (mm/frames.c), own 4-level page tables that * identity map [0, top) with 2 MiB pages (RAM write-back, the rest uncached, * the GOP framebuffer write-combining through PAT entry 1) and a 4 KiB guard - * page under the kernel stack, CR3 switched and the stack moved. Then: the + * page under the kernel stack, CR3 switched and the stack moved. W^X (#409): + * EFER.NXE and CR0.WP on every core; only the kernel's code is executable + * (read-only), every other leaf is no-execute, read-only data is read-only, + * proven by probes and an audit of every leaf. Then: the * screen console (core/fbcon.c), heap (mm/heap.c), the guard page proven to * fault and the fault contained, the TSC measured against the HPET, the * local APIC (xAPIC) with its timer at 100 Hz for 20 ticks, the MADT CPU @@ -46,7 +49,20 @@ #define PTE_PWT 0x8ull #define PTE_PCD 0x10ull #define PTE_PS 0x80ull +#define PTE_NX (1ull << 63) #define PTE_ADDR 0x000ffffffffff000ull +#define MSR_EFER 0xc0000080u +#define EFER_NXE (1ull << 11) +#define CR0_WP (1ull << 16) + +/* CK_X86_WX_MUTATE (negative test builds only): 1 leaves RAM executable, + * 2 leaves the kernel's code writable. The W^X probes must then FAIL. */ +#ifndef CK_X86_WX_MUTATE +#define CK_X86_WX_MUTATE 0 +#endif + +/* Image regions (image_x86_64.ld), runtime addresses after self-relocation. */ +extern char __image_base[], __text_start[], __text_end[], __ro_start[], __ro_end[], __image_end[]; const char *ck_commit(void) { return AIENOS_COMMIT; } @@ -301,7 +317,7 @@ void ck_x86_trap(struct ck_x86_frame *f) /* ---- memory ---- */ struct mm { - uint64_t root, top, pages_wb, pages_wc, pages_uc, pt_frames, fb_lo, fb_hi; + uint64_t root, top, pages_wb, pages_wc, pages_uc, pt_frames, fb_lo, fb_hi, pages_4k; int fb_ok; }; static struct mm mm; @@ -323,6 +339,88 @@ static int efi_ram(uint32_t type) return type <= 10 && type != 0 && type != 8; } +static uint64_t *pds[MAP_TOP_MAX >> 30]; + +static int in(uint64_t p, const char *lo, const char *hi) +{ + return p >= (uint64_t)(uintptr_t)lo && p < (uint64_t)(uintptr_t)hi; +} + +/* The permission of one 4 KiB page inside a split 2 MiB page: the stack + * guard unmapped, code read-only executable, the PE header and read-only data + * read-only no-execute, the trampoline writable executable until + * wx_retire_trampoline(), everything else writable no-execute. */ +static uint64_t leaf4k(uint64_t p, uint64_t cache, uint64_t nx) +{ + if (p == (uint64_t)(uintptr_t)kstack) + return 0; + if (in(p, __text_start, __text_end)) + return p | PTE_P | cache | (CK_X86_WX_MUTATE == 2 ? PTE_W : 0); + if (in(p, __image_base, __text_start) || in(p, __ro_start, __ro_end)) + return p | PTE_P | cache | nx; + if (tramp_pa && p == tramp_pa) + return p | PTE_P | PTE_W | cache; + return p | PTE_P | PTE_W | cache | nx; +} + +static void split_2m(uint64_t a, uint64_t nx) +{ + uint64_t *e = &pds[a >> 30][(a >> 21) & 511]; + if (!(*e & PTE_PS)) + return; /* already split */ + uint64_t cache = *e & (PTE_PCD | PTE_PWT), *pt = table_new(); + for (uint64_t j = 0; j < 512; j++) + pt[j] = leaf4k(a | j << 12, cache, nx); + *e = (uint64_t)(uintptr_t)pt | PTE_P | PTE_W; + mm.pages_4k += 512; +} + +/* The 4 KiB PTE mapping `a`, or 0 when `a` sits in a 2 MiB page. */ +static uint64_t *pte4k(uint64_t a) +{ + uint64_t e = pds[a >> 30][(a >> 21) & 511]; + if (!(e & PTE_P) || (e & PTE_PS)) + return 0; + return (uint64_t *)(uintptr_t)(e & PTE_ADDR) + ((a >> 12) & 511); +} + +/* The secondary cores are up (or will never start): the trampoline page + * becomes read-only no-execute data. */ +static void wx_retire_trampoline(void) +{ + uint64_t *e = tramp_pa ? pte4k(tramp_pa) : 0; + if (!e) + return; + *e = (*e & ~PTE_W) | (CK_X86_WX_MUTATE == 1 ? 0 : PTE_NX); + __asm__ volatile("invlpg (%0)" ::"r"(tramp_pa) : "memory"); +} + +/* Every present leaf of the identity map: writable+executable ones, and + * executable 4 KiB units (must equal the code pages). */ +static void wx_audit(uint64_t *wx, uint64_t *exec4k) +{ + *wx = *exec4k = 0; + for (uint64_t g = 0; g < mm.top >> 30; g++) + for (uint64_t i = 0; i < 512; i++) { + uint64_t e = pds[g][i]; + if (!(e & PTE_P)) + continue; + if (e & PTE_PS) { + if (!(e & PTE_NX)) { + *exec4k += 512; + *wx += !!(e & PTE_W); + } + continue; + } + const uint64_t *pt = (const uint64_t *)(uintptr_t)(e & PTE_ADDR); + for (uint64_t j = 0; j < 512; j++) + if ((pt[j] & PTE_P) && !(pt[j] & PTE_NX)) { + (*exec4k)++; + *wx += !!(pt[j] & PTE_W); + } + } +} + static void mm_build(void) { const struct ck_handoff *h = hand; @@ -364,30 +462,29 @@ static void mm_build(void) /* Every 2 MiB page starts uncached (PCD|PWT, PAT index 3): device memory * (local APIC, I/O APIC, HPET, PCI BARs) whatever the MTRRs say. Pages * that hold RAM from the memory map become write-back (index 0), the - * framebuffer write-combining (PWT alone, index 1, set to WC below). The - * 2 MiB page holding the stack guard is split into 4 KiB pages. */ - static uint64_t *pds[MAP_TOP_MAX >> 30]; + * framebuffer write-combining (PWT alone, index 1, set to WC below). + * W^X (#409): every leaf is no-execute except the kernel's code (and the + * secondary-core trampoline while the cores start); the 2 MiB pages that + * hold the image or the trampoline are split into 4 KiB pages below. + * W^X separates code from data inside one privilege level; it is not + * process isolation. Learned or generated code must never run here. */ if (ck_rd_cr4() & (1ull << 12)) ck_panic("paging: firmware runs 5-level paging (CR4.LA57); this kernel builds 4-level tables"); + uint32_t xr[4]; + ck_cpuid(0x80000000, 0, xr); + if (xr[0] < 0x80000001) + ck_panic("paging: CPU has no extended CPUID leaf for NX"); + ck_cpuid(0x80000001, 0, xr); + if (!(xr[3] & (1u << 20))) + ck_panic("paging: CPU has no NX (execute disable)"); + uint64_t nx = CK_X86_WX_MUTATE == 1 ? 0 : PTE_NX; uint64_t *pml4 = table_new(), *pdpt = table_new(); pml4[0] = (uint64_t)(uintptr_t)pdpt | PTE_P | PTE_W; - uint64_t guard = (uint64_t)(uintptr_t)kstack; for (uint64_t g = 0; g < top >> 30; g++) { uint64_t *pd = pds[g] = table_new(); pdpt[g] = (uint64_t)(uintptr_t)pd | PTE_P | PTE_W; - for (uint64_t i = 0; i < 512; i++) { - uint64_t a = g << 30 | i << 21; - if (a <= guard && guard < a + (2ull << 20)) { - uint64_t *pt = table_new(); - for (uint64_t j = 0; j < 512; j++) { - uint64_t p = a | j << 12; - pt[j] = p == guard ? 0 : (p | PTE_P | PTE_W); - } - pd[i] = (uint64_t)(uintptr_t)pt | PTE_P | PTE_W; - continue; - } - pd[i] = a | PTE_P | PTE_W | PTE_PS | PTE_PCD | PTE_PWT; - } + for (uint64_t i = 0; i < 512; i++) + pd[i] = (g << 30 | i << 21) | PTE_P | PTE_W | PTE_PS | PTE_PCD | PTE_PWT | nx; } for (uint64_t off = 0; off + h->desc_size <= h->map_size; off += h->desc_size) { const struct ck_efi_desc *d = (const void *)(uintptr_t)(h->memory_map + off); @@ -405,6 +502,13 @@ static void mm_build(void) if (*e & PTE_PS) *e = (*e & ~PTE_PCD) | PTE_PWT; } + /* 4 KiB pages where the image and the trampoline live, keeping the + * 2 MiB page's cache type (PCD/PWT sit at the same bits in a 4 KiB PTE). */ + uint64_t img_lo = (uint64_t)(uintptr_t)__image_base, img_hi = (uint64_t)(uintptr_t)__image_end; + for (uint64_t a = img_lo & ~((2ull << 20) - 1); a < img_hi; a += 2ull << 20) + split_2m(a, nx); + if (tramp_pa) + split_2m(tramp_pa & ~((2ull << 20) - 1), nx); for (uint64_t g = 0; g < top >> 30; g++) for (uint64_t i = 0; i < 512; i++) { uint64_t e = pds[g][i]; @@ -426,7 +530,12 @@ static void mm_build(void) ck_panic("paging: CPU has no PAT"); __asm__ volatile("wbinvd" ::: "memory"); ck_wrmsr(0x277, 0x0007040600070106ull); + /* NX bits are reserved (a #PF on every access) until EFER.NXE is set. */ + ck_wrmsr(MSR_EFER, ck_rdmsr(MSR_EFER) | EFER_NXE); ck_wr_cr3(mm.root); + uint64_t cr0; + __asm__ volatile("mov %%cr0, %0" : "=r"(cr0)); + __asm__ volatile("mov %0, %%cr0" ::"r"(cr0 | CR0_WP) : "memory"); /* read-only binds the kernel too */ /* Toggling CR4.PGE drops global TLB entries the firmware's tables left. */ uint64_t cr4 = ck_rd_cr4(); if (cr4 & 0x80) { @@ -457,7 +566,7 @@ int ck_x86_map_mmio(uint64_t pa, uint64_t len) uint64_t *pd = sub_table(sub_table(pml4, (unsigned)(a >> 39) & 511), (unsigned)(a >> 30) & 511); uint64_t *e = &pd[(a >> 21) & 511]; if (!(*e & PTE_P)) { - *e = a | PTE_P | PTE_W | PTE_PS | PTE_PCD | PTE_PWT; + *e = a | PTE_P | PTE_W | PTE_PS | PTE_PCD | PTE_PWT | PTE_NX; mm.pages_uc++; } __asm__ volatile("invlpg (%0)" ::"r"(a) : "memory"); @@ -729,6 +838,68 @@ static void smp_start(const struct apic_report *ar, struct smp_report *sr) } } +/* ---- W^X probes (#409) ---- + * Each probe arms the #PF catcher (ck_x86_trap), touches one address and + * resumes at the label after the access. A store writes back the byte it + * read, so a mutated (writable) build changes nothing. An execute probe + * jumps to two bytes `jmp *%rax` with rax = the resume label, so code that + * does run comes straight back and the probe sees no fault. */ +static int probe_store(uint64_t a) +{ + probe.hit = 0; + probe.armed = 1; + __asm__ volatile("lea 1f(%%rip), %%rax\n\t" + "mov %%rax, %0\n\t" + "movb (%1), %%cl\n\t" + "movb %%cl, (%1)\n" + "1:" + : "=m"(probe.resume) + : "r"(a) + : "rax", "rcx", "memory"); + probe.armed = 0; + return probe.hit && probe.cr2 == a && (probe.err & 3) == 3; /* present + write */ +} + +static int probe_exec(uint8_t *buf) +{ + buf[0] = 0xff; /* jmp *%rax */ + buf[1] = 0xe0; + uint64_t a = (uint64_t)(uintptr_t)buf; + probe.hit = 0; + probe.armed = 1; + __asm__ volatile("lea 1f(%%rip), %%rax\n\t" + "mov %%rax, %0\n\t" + "jmp *%1\n" + "1:" + : "=m"(probe.resume) + : "r"(a) + : "rax", "memory"); + probe.armed = 0; + return probe.hit && probe.cr2 == a && (probe.err & 0x11) == 0x11; /* present + instruction fetch */ +} + +static void wx_probes(void) +{ + uint64_t efer = ck_rdmsr(MSR_EFER), cr0; + __asm__ volatile("mov %%cr0, %0" : "=r"(cr0)); + uint8_t *hb = ck_heap_alloc(&heap, 16); + volatile uint8_t sb[16]; + if (!hb) + ck_panic("wx: no heap for the execute probe"); + int tw = probe_store((uint64_t)(uintptr_t)__text_start); + int rw = probe_store((uint64_t)(uintptr_t)__ro_start); + int hx = probe_exec(hb); + int sx = probe_exec((uint8_t *)sb); + ck_heap_free(&heap, hb); + const char *f = "fault", *n = "ALLOWED"; + if (!(efer & EFER_NXE) || !(cr0 & CR0_WP) || !tw || !rw || !hx || !sx) + ck_panic("wx: FAIL nx=%s wp=%s text_write=%s ro_write=%s heap_exec=%s stack_exec=%s", + efer & EFER_NXE ? "on" : "off", cr0 & CR0_WP ? "on" : "off", tw ? f : n, rw ? f : n, + hx ? f : n, sx ? f : n); + ck_puts("wx: ok nx=on wp=on text=rx ro=r data=rw-nx text_write=fault ro_write=fault heap_exec=fault " + "stack_exec=fault\n"); +} + /* ---- the kernel ---- */ static void cpu_line(void) { @@ -814,6 +985,8 @@ static __attribute__((noreturn)) void kernel_main(void *arg) ck_panic("guard_page: FAIL hit=%d cr2=0x%llx error=0x%llx", probe.hit, (unsigned long long)probe.cr2, (unsigned long long)probe.err); + wx_probes(); + tsc_measure(); if (!tsc_hz) ck_panic("timebase: no HPET and no CPUID leaf 0x16 frequency"); @@ -835,6 +1008,14 @@ static __attribute__((noreturn)) void kernel_main(void *arg) struct smp_report sr = {0}; smp_start(&ar, &sr); + wx_retire_trampoline(); + uint64_t wx, exec4k, text4k = (uint64_t)(__text_end - __text_start) >> 12; + wx_audit(&wx, &exec4k); + if (wx || exec4k != text4k) + ck_panic("wx_audit: FAIL wx_leaves=%llu exec_pages=%llu text_pages=%llu", (unsigned long long)wx, + (unsigned long long)exec4k, (unsigned long long)text4k); + ck_printf("wx_audit: ok wx_leaves=0 exec_pages=%llu text_pages=%llu pages_4k=%llu trampoline=retired\n", + (unsigned long long)exec4k, (unsigned long long)text4k, (unsigned long long)mm.pages_4k); if (sr.why) { ck_printf("smp: cpus=%u started=0 (%s)\n", sr.listed, sr.why); } else { @@ -868,6 +1049,9 @@ void ck_x86_kernel_entry(struct ck_handoff *h) hand = h; tables_load(); ck_puts("\nreport_kind: kernel\n"); + if (CK_X86_WX_MUTATE) + ck_printf("wx: MUTATED TEST BUILD (CK_X86_WX_MUTATE=%u): W^X deliberately broken, never deploy\n", + (unsigned)CK_X86_WX_MUTATE); ck_printf("exit_boot_services: ok attempts=%u\n", h->exit_attempts); cpu_line(); ck_puts("descriptors: gdt=5 tss=yes ist1=df,nmi,mc idt=256\n"); diff --git a/native/kernel/x86/smp_x86.S b/native/kernel/x86/smp_x86.S index a49955ee..6e471b49 100644 --- a/native/kernel/x86/smp_x86.S +++ b/native/kernel/x86/smp_x86.S @@ -7,7 +7,7 @@ * INIT, SIPI, SIPI with the page number as the vector. A core then starts * here in real mode at CS = page >> 4, IP = 0, and walks: 16-bit -> 32-bit * protected mode (the trampoline's own flat GDT) -> PAE + the kernel's page - * tables + EFER.LME + paging -> 64-bit, then calls entry(arg) on its own + * tables + EFER.LME/NXE + paging and CR0.WP -> 64-bit, then calls entry(arg) on its own * stack (-mabi=ms: the argument in rcx, 32 bytes of shadow space). If entry * returns, the core halts with interrupts off. */ @@ -44,10 +44,10 @@ tramp_pm32: mov %eax, %cr3 mov $0xc0000080, %ecx /* EFER */ rdmsr - or $0x100, %eax /* LME */ + or $0x900, %eax /* LME + NXE: the kernel's tables carry NX bits */ wrmsr mov %cr0, %eax - or $0x80000000, %eax /* PG */ + or $0x80010000, %eax /* PG + WP: read-only pages bind this core too */ mov %eax, %cr0 ljmpl *(ck_x86_tramp_far64 - ck_x86_tramp_start)(%ebx) diff --git a/scripts/.crumb b/scripts/.crumb index 2bac4872..dabc9a31 100644 --- a/scripts/.crumb +++ b/scripts/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-17T01:30:44Z", + "compiled_at": "2026-10-17T16:57:40Z", "compiler": "crumb-compile/1", - "digest": "sha256:9adb69cc749bc97fa48e268f0397b900da83c84191ab6a9373358e8b9ab57922", + "digest": "sha256:8a783c03b1dab4e87a2c2df8933d490baf2fac25064667791f405046841d0d72", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 119, - "generated_at_commit": "44cf2dd5925747aebf105136d31c8db58d8851a4", - "source_tree": "sha256:8ae58db519604c153d7140c6898139a24d1664716d187229aa125e147d370af1" + "generated_at_commit": "a8a1fd36cfaa273afd67987fa250da4a0d8eac7a", + "source_tree": "sha256:6d5c7cd853cd2c7509a30b9f64b9df46f738c5f34d81bf29c2a44810871d4684" }, "owner_lane": "", "provenance": { diff --git a/scripts/qemu_ck_imac_kernel_test.sh b/scripts/qemu_ck_imac_kernel_test.sh index 701b4331..cf6fc82c 100755 --- a/scripts/qemu_ck_imac_kernel_test.sh +++ b/scripts/qemu_ck_imac_kernel_test.sh @@ -118,6 +118,8 @@ run() { # name, -cpu model, expected APIC mode chk "${name}: one-line summary" "${txt}" "^summary: cores=4/4 timer=ok sata_disks=2 nvme_disks=1 gpt_valid=2 writes=none net=none\$" [[ "$(sha256sum "${nvm}" | cut -d" " -f1)" == "${nvm_sum}" ]] && echo "PASS ${name}: NVMe disk unchanged" || { echo "FAIL ${name}: NVMe disk changed"; fails=$((fails + 1)); } [[ "$(sha256sum "${data}" | cut -d" " -f1)" == "${data_sum}" ]] && echo "PASS ${name}: data disk unchanged" || { echo "FAIL ${name}: data disk changed"; fails=$((fails + 1)); } + chk "${name}: W^X probes (#409)" "${txt}" '^wx: ok nx=on wp=on text=rx ro=r data=rw-nx text_write=fault ro_write=fault heap_exec=fault stack_exec=fault$' + chk "${name}: W^X audit, only code executable" "${txt}" '^wx_audit: ok wx_leaves=0 exec_pages=([1-9][0-9]*) text_pages=\1 pages_4k=[1-9][0-9]* trampoline=retired$' chk "${name}: kernel alive" "${txt}" '^kernel: alive$' chk "${name}: DONE" "${txt}" '^AIENOS-IMAC-KERNEL-0 DONE$' chk "${name}: ACPI S5 power off" "${txt}" '^power: off via ACPI S5 pm1a=0x[0-9a-f]+ ' @@ -131,5 +133,39 @@ run() { # name, -cpu model, expected APIC mode } run max max xapic run qemu64 qemu64 xapic +# Negative controls (#409): builds with W^X deliberately broken must make the kernel's own +# probes panic. The panic comes before the clock is measured, so the kernel halts instead +# of powering off: wait for the line, then stop QEMU. +mut() { # mode, expected panic pattern + local m="$1" mo="${work}/mut$1" mi="${work}/mut$1.img" log="${work}/mut$1.log" hit=0 + make -s -C native/kernel X86_CROSS="${cross}" OUT="${mo}" AIENOS_COMMIT="${commit}" CK_X86_WX_MUTATE="${m}" imac-kernel >/dev/null + truncate -s 64M "${mi}" + mformat -i "${mi}" -F :: + mmd -i "${mi}" ::/EFI ::/EFI/BOOT + mcopy -i "${mi}" "${mo}/IMACKERN.EFI" ::/EFI/BOOT/BOOTX64.EFI + cp "${vars_fd}" "${work}/vars.fd" + qemu-system-x86_64 -M q35 -accel tcg -cpu max -smp 1 -m 1024 \ + -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ + -drive if=pflash,format=raw,file="${work}/vars.fd" \ + -drive if=none,id=esp,format=raw,file="${mi}" -device ide-hd,drive=esp,bus=ide.0 \ + -display none -nic none -serial file:"${log}" -no-reboot & + qpid=$! + for _ in $(seq 1 120); do + if tr -d '\r' <"${log}" 2>/dev/null | grep -aqE -- "$2"; then hit=1; break; fi + kill -0 "${qpid}" 2>/dev/null || break + sleep 1 + done + kill "${qpid}" 2>/dev/null || true + wait "${qpid}" 2>/dev/null || true + qpid="" + if [[ ${hit} == 1 ]] && tr -d '\r' <"${log}" | grep -aq '^wx: MUTATED TEST BUILD'; then + echo "PASS mutation ${m}: broken W^X caught by the kernel's probes" + else + echo "FAIL mutation ${m}: no '$2' panic"; fails=$((fails + 1)) + tr -d '\r' <"${log}" | tail -20 + fi +} +mut 1 '^panic: wx: FAIL .*heap_exec=ALLOWED stack_exec=ALLOWED$' +mut 2 '^panic: wx: FAIL .*text_write=ALLOWED ' if [[ ${fails} == 0 ]]; then echo "${tag}: PASS"; exit 0; fi echo "${tag}: FAIL (${fails} checks)"; exit 1 ```