Repository navigation
kemu: fix security and correctness issues found in audit - #3
Merged
Merged
Conversation
losfair
force-pushed
the
claude/magical-keller-9kishf
branch
from
October 25, 2026 07:44
be293f6 to
723511f
Compare
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
force-pushed
the
claude/magical-keller-9kishf
branch
from
October 25, 2026 09:12
f5b81ba to
bdd76b4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
syscall_get_arch()hook was in theCONFIG_X86_32branch ofasm/syscall.h, which is never compiled on x86_64, so kemu tasks reportedAUDIT_ARCH_X86_64with arm64 syscall numbers. x86_64 seccomp blocklists (e.g. Docker'sunshare→ arm64 nr 97 reads asgetrlimit) 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).vmalloc, half-written) and code-cache chunks (whole chunk mapped, onlychunk_usedwritten) exposed stale kernel bytes. Both are now zeroed.semctlbuffer overflow. IPC_STAT/SET used the x86_64semid64_ds(104 B) for an arm64 guest buffer (88 B), overflowing it and landing ctime insem_nsems. Marshalled via newipc/sem.chelpers; a negative semid is rejected first, as natively.kemu_materialize()parsed a chunk a guest-steered RIP pointed into without checking it was retired, racingkemu_reclaim()'svfree(). 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_csranges; return-ENOSYSuntil it's implemented (README already lists it as not done).p->kemu == NULL) crashed in the regset getters; every getter/setter is now guarded.kemu_svc()after an arm64→arm64 exec; the exec is now detected by mm change before touching the state page.x0clobbered on syscall rollback (seccomp RET_TRAP/KILL, syscall-user-dispatch): arm64 restoresorig_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.O_LARGEFILEon 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_coreregressions, each verified to fail without its fix: shared-cache fetch memoization, bounded contended atomic.tests/kernel/abi.c: semctl IPC_STAT buffer/sem_nsemsand negative ids, openat2 >2 GiB write, a seccompAUDIT_ARCH_AARCH64filter match, strict-mode allow-list, andx0after RET_ERRNO|0 and RET_TRAP.Validation
kemu_coreunit tests and the fulljit-testmatrix pass;kemu-usersmoke runs.bzImage, and the 14-patch series applies cleanly to a pristine tree.abi(all checks),hello,mtstressandintbenchpass. With the rollback and semctl fixes reverted,seccomp-x0and bothsemctl-negative-*checks fail.tbi'stagged-faultfails under TCG on unmodified main too (and-cpu maxenables 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.shandscripts/gen-syscalls.pywere updated so regeneration reproduces the tree.🤖 Generated with Claude Code
https://claude.ai/code/session_01CFPDNz2MRgp7ebuCpmet1E