Skip to content

kemu: fix security and correctness issues found in audit - #3

Merged
losfair merged 2 commits into
mainfrom
claude/magical-keller-9kishf
Oct 25, 2026
Merged

losfair merged 2 commits into
mainfrom
claude/magical-keller-9kishf

Conversation

@losfair

@losfair losfair commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner

An audit of the kernel glue, syscall ABI layer, shared translation cache, JIT driver and Rust core turned up one critical and several high/medium issues. This fixes them, each with a regression test.

Critical / high

  • seccomp/audit report the wrong architecture. The syscall_get_arch() hook was in the CONFIG_X86_32 branch of asm/syscall.h, which is never compiled on x86_64, so kemu tasks reported AUDIT_ARCH_X86_64 with arm64 syscall numbers. x86_64 seccomp blocklists (e.g. Docker's unshare → arm64 nr 97 reads as getrlimit) were bypassed and guest arm64 filters mis-fired. Moved to the x86_64 branch; also bypass the seccomp bitmap cache for kemu tasks (bitmaps are built from the x86 tables).
  • Shared-cache poisoning across processes. Flag look-ahead re-read exit-target words from guest memory while only the first read was recorded as a dependency. A process rewriting its own file-backed text mid-translation could cache a block whose dependency records disagreed with the emitted code; another process reusing it would run the wrong code. Fetches are now memoized within a translation.
  • Kernel memory leaks to the guest. The fork-time lookup table (vmalloc, half-written) and code-cache chunks (whole chunk mapped, only chunk_used written) exposed stale kernel bytes. Both are now zeroed.
  • Strict-mode seccomp checked the x86 mode1 list against arm64 numbers; added a kemu allow-list (read/write/exit/rt_sigreturn).
  • semctl buffer overflow. IPC_STAT/SET used the x86_64 semid64_ds (104 B) for an arm64 guest buffer (88 B), overflowing it and landing ctime in sem_nsems. Marshalled via new ipc/sem.c helpers; a negative semid is rejected first, as natively.
  • Retired code-chunk use-after-free. kemu_materialize() parsed a chunk a guest-steered RIP pointed into without checking it was retired, racing kemu_reclaim()'s vfree(). A RIP in a chunk retired at/before the thread's epoch is now treated as unrecoverable, and chunk free/publish use release/acquire against that lockless lookup.

Medium

  • rseq is passed through to the host, which compares the host (code-cache) PC against guest rseq_cs ranges; return -ENOSYS until it's implemented (README already lists it as not done).
  • Core-dump NULL deref: a thread group with a user-worker thread (p->kemu == NULL) crashed in the regset getters; every getter/setter is now guarded.
  • Freed-page read in kemu_svc() after an arm64→arm64 exec; the exec is now detected by mm change before touching the state page.
  • Unbounded atomic retry in the interpreter could spin in the kernel under contention; bounded with instruction restart.
  • x0 clobbered on syscall rollback (seccomp RET_TRAP/KILL, syscall-user-dispatch): arm64 restores orig_x0, which SIGSYS brokers (Chromium, Firefox) rely on. syscall_rollback() flags the rollback, so a skipped syscall whose return value equals its number (RET_ERRNO|0, a user-notification reply) still returns that value.
  • openat2 didn't force O_LARGEFILE on 64-bit or emit an audit record (now logged with the guest's flags).

(The loop-exit register-recovery bug found by the audit was fixed independently on main in dbf2f22.)

Tests

  • kemu_core regressions, each verified to fail without its fix: shared-cache fetch memoization, bounded contended atomic.
  • tests/kernel/abi.c: semctl IPC_STAT buffer/sem_nsems and negative ids, openat2 >2 GiB write, a seccomp AUDIT_ARCH_AARCH64 filter match, strict-mode allow-list, and x0 after RET_ERRNO|0 and RET_TRAP.

Validation

  • kemu_core unit tests and the full jit-test matrix pass; kemu-user smoke runs.
  • The patched v6.18 tree builds bzImage, and the 14-patch series applies cleanly to a pristine tree.
  • Booted under QEMU TCG (no KVM here): abi (all checks), hello, mtstress and intbench pass. With the rollback and semctl fixes reverted, seccomp-x0 and both semctl-negative-* checks fail. tbi's tagged-fault fails under TCG on unmodified main too (and -cpu max enables LA57), so it's left to the KVM run in CI.

The patch series gained two files (0013-kemu-seccomp.patch, 0014-kemu-ipc.patch); scripts/export-patches.sh and scripts/gen-syscalls.py were updated so regeneration reproduces the tree.

🤖 Generated with Claude Code

https://claude.ai/code/session_01CFPDNz2MRgp7ebuCpmet1E

@losfair
losfair force-pushed the claude/magical-keller-9kishf branch from be293f6 to 723511f Compare October 25, 2026 07:44
Addresses findings from a security/correctness audit of the kernel glue,
syscall ABI layer, shared translation cache, JIT driver and Rust core.

Critical / high:

- seccomp/audit arch: the syscall_get_arch() hook was placed in the
  CONFIG_X86_32 branch of asm/syscall.h, which is never compiled on
  x86_64, so kemu tasks reported AUDIT_ARCH_X86_64 with arm64 syscall
  numbers. x86_64 seccomp blocklists (e.g. Docker's) were bypassed and
  guest-installed arm64 filters mis-fired. Move the hook to the x86_64
  branch and bypass the seccomp bitmap cache for kemu tasks (the bitmaps
  are built from the x86 tables).

- shared translation cache poisoning: the translator recorded only the
  first read of each guest word, but flag look-ahead re-read exit-target
  words from guest memory. A process rewriting its own file-backed text
  mid-translation could produce a block whose dependency records (which
  the cache re-validates) disagreed with the emitted code, and another
  process reusing the entry would run it. Memoize fetches within a
  translation so no decision depends on a re-read.

- uninitialized kernel memory mapped to the guest: the fork-time lookup
  table (vmalloc, only half written) and the code-cache chunks (whole
  chunk mapped, only chunk_used bytes written) leaked stale kernel bytes.
  Zero both.

- SECCOMP_MODE_STRICT checked the x86 mode1 syscall list against arm64
  numbers; add a kemu allow-list (read/write/exit/rt_sigreturn).

- semctl IPC_STAT/SET used the x86_64 semid64_ds (104 bytes) for a guest
  buffer laid out as arm64's (88), overflowing the caller's buffer and
  landing ctime in sem_nsems. Marshal the struct via new kernel helpers.

- retired code-chunk use-after-free: kemu_materialize() parsed a chunk a
  guest-steered RIP pointed into without checking it was retired, racing
  kemu_reclaim()'s vfree(). Treat a RIP in a chunk retired at or before
  the thread's epoch as unrecoverable.

Medium:

- rseq passed through to the host, which compares the host (code-cache)
  PC against guest rseq_cs ranges; return -ENOSYS until implemented.
- core dumps of a thread group with a user-worker thread (p->kemu == NULL)
  hit a NULL deref in the regset getters; guard every getter/setter.
- kemu_svc() read the old mm's freed state page after an arm64->arm64
  exec; detect the exec by mm change before touching it.
- remapped self-loop blocks recovered wrong registers at exit
  trampolines (the last REMAP point said "remapped"); emit a canonical
  REMAP point before the trampolines.
- unbounded LSE-atomic retry loop in the interpreter could spin in the
  kernel under contention; bound it and restart the instruction.
- syscall rollback (seccomp RET_TRAP/KILL, SUD) wrote the return value to
  guest x0 where arm64 restores orig_x0; preserve it so SIGSYS brokers
  see the right arg0.
- openat2 didn't force O_LARGEFILE on 64-bit or emit an audit record.

Tests:

- kemu_core regressions (fail without the fix): shared-cache fetch
  memoization, canonical remap recovery at loop exits, bounded contended
  atomic.
- tests/kernel/abi.c: semctl IPC_STAT buffer/nsems, openat2 >2 GiB write,
  seccomp AUDIT_ARCH_AARCH64 filter match, strict-mode allow-list.

Verified: kemu_core unit tests (29) and jit-test matrix pass; the patched
v6.18 tree builds bzImage and the series applies cleanly to a pristine
tree. In-kernel ABI tests run under CI (KVM).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CFPDNz2MRgp7ebuCpmet1E
- syscall: flag seccomp/syscall-user-dispatch rollbacks from
  syscall_rollback() (patch 0006) instead of inferring them from
  ax == orig_ax. A skipped syscall whose return value happens to equal
  its number (RET_ERRNO|0 on io_setup, a user-notification reply of 64
  to write) had its result replaced by the original x0.
- semctl: reject a negative semid with EINVAL first, as ksys_semctl()
  does; the stat/set helpers masked INT_MIN into index 0 and IPC_SET
  returned EFAULT for a bad buffer.
- openat2: audit the guest's (arm64) open flags, before translation.
- jit: clear a freed chunk's pointers before marking it live again, with
  release/acquire against the lockless kemu_materialize() lookup.
- tests/kernel/abi: seccomp-strict child exits with exit (strict mode
  does not allow exit_group, so the test could never pass); new
  seccomp-x0 and semctl-negative-* checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CFPDNz2MRgp7ebuCpmet1E
@losfair
losfair force-pushed the claude/magical-keller-9kishf branch from f5b81ba to bdd76b4 Compare October 25, 2026 09:12
@losfair
losfair merged commit 3dbb1a0 into main Oct 25, 2026
2 checks passed
@losfair
losfair deleted the claude/magical-keller-9kishf branch October 25, 2026 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants