Repository navigation
kemu: seal private mappings and fix security audit findings - #11
Merged
Merged
Conversation
Address the critical and several high/medium audit findings in the core
per-mm region and task lifecycle:
- C-01: install [kemu-code]/[kemu-table]/[kemu-slots]/[vdso] and then seal
them with VM_SEALED, so the guest can no longer munmap/mprotect/mremap or
mmap-over and split a VMA. That split was what let fork's PTE copy and
chunk reclaim leave live Ring-3 PTEs on freed pages (a physical-page UAF).
Sealing is applied only after all four mappings exist, so the fail_unmap
teardown (do_munmap) still works if an install fails midway.
- H-01: refuse to enable kemu unless the host CPU provides every feature the
translator and glue emit unconditionally (x86-64-v3 + CX16/FSGSBASE +
AES-NI/PCLMULQDQ), so translated code or LOCK CMPXCHG16B can never #UD in
the kernel. Hand the optional features (AES/PCLMUL/SHA/RDRAND) to the Rust
core via cpu_feature_enabled() so clearcpuid= is honoured, and mask
HWCAP2_RNG without RDRAND.
- H-02a: strip host executability from guest VMAs. mm->kemu_guest is set in
SET_PERSONALITY (before any stack/ELF VMA exists), so vma_set_page_prot()
marks every guest mapping NX from birth -- no PTE is ever created
host-executable. Guest code is only read for translation, never run
natively; kemu's own region stays executable.
- M-03.3: clear a reused thread slot's return-stack pages.
- M-04: register kernel.kemu.{enabled,restrict}; account slot and
chunk/table allocations (__vmalloc(GFP_KERNEL_ACCOUNT)); gate the
translator on the init userns / CAP_SYS_ADMIN when restrict=1.
- M-02: an atomic64 unmap_seq, bumped before kemu_unmapped()'s own early
returns, lets a concurrent translation detect a mapping teardown even
before its block is indexed; don't early-return while a wide block exists.
- L-01: report the guest PC for /proc/<pid>/stat kstkeip.
- L-02: return -ENOMEM (not 0) when kemu_dup_mm() can't allocate the slot.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- M-02: discard a freshly translated block when unmap_seq advanced during translation (its instructions may be stale); the sequence is sampled under km->lock and re-checked after an smp_mb() so it can't miss an unmap whose invalidation already ran. Invalidate wide blocks; drop TBI hints for invalidated ranges. - M-03.1: recover a host #DE in translated code (a raced cross-thread divisor) by running the one guest instruction in the interpreter, which yields the architectural AArch64 result (UDIV/0 = 0, SDIV INT_MIN/-1 = INT_MIN) instead of a SIGFPE that also leaks a code-cache address. The divide-error IDT hook lives in the regenerated patch set. - M-03.2: READ_ONCE() the guest-writable state-page fields (pc, exit_info, svc pc) the kernel reads for control decisions. - M-04: drop the TBI hint set on flush so it can't grow without bound; account the block-index allocations. - Never skip a guest svc the interpreter retired. Since M-02 discards raced translations, the interpreter fallback often stops at an svc with pc already at svc + 4, expecting the interp_svc stub to perform it. An epoch change (a sibling invalidation, kemu.stress) or a pending signal on the way out redirected to dispatch instead, dropping the syscall with x0 still holding its first argument (glibc: "The futex facility returned an unexpected error code"). KEMU_TF_PENDING_SVC makes exit_to_user resume at interp_svc, and kemu_materialize() rewinds pc to restart the svc when a signal frame is built first, as for an interrupted svc stub. The helper and single-step paths use the same mechanism. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- M-01: wrap sigaltstack to require the arm64 minimum alternate-stack size (KEMU_MINSIGSTKSZ), rejecting 2048..5119-byte stacks the x86 entry point would accept and that would underflow on signal delivery. - M-05: wrap mmap (reject x86-only MAP_32BIT) and prctl (reject PR_SET_TSC / PR_GET_TSC and PR_SET_SYSCALL_USER_DISPATCH, which would hijack kemu's own host syscall instructions). - L-03: pin the cmpxchg16b target page (pin_user_pages_fast) and only mark it dirty when the exchange actually stored. - L-04: after a tracer writes the guest GPRs/FP regs, mark the state page authoritative (KEMU_TF_KSTATE) and redirect so exit-to-user rebuilds the flags/MXCSR image and translated code reloads the registers; carry a tracer-injected x0 into the result register so a skipped syscall (NT_ARM_SYSTEM_CALL = -1) returns it instead of -ENOSYS. - L-01: use %p rather than %px in the debug signal log. - M-04: account the transient signal-frame allocations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- H-01: the Rust core takes the optional host features (AES/PCLMUL/SHA from
the kernel instead of a raw CPUID, and RDRAND) via kemu_rs_set_host_features;
when the host has no RDRAND, ID_AA64ISAR0_EL1.RNDR is cleared and RNDR/RNDRRS
trap as UNDEFINED, keeping the ID register, HWCAP2_RNG and the instruction
consistent.
- L-05: DC ZVA honours TBI -- strip the address tag before the zeroing store
in tagged mode, like every other data access. Adds a regression test.
- H-03: forbid clippy::{unwrap_used,expect_used,panic} outside the test build
(a panic in the kernel core is a BUG()); make Slow::push bounds-checked;
replace the infallible FP mask .try_into().unwrap()s; and build kemu_rs.o
with -Coverflow-checks=no so two's-complement wrapping can't BUG() under
CONFIG_RUST_OVERFLOW_CHECKS=y.
- L-06: bound the kemu-user ELF loader with checked arithmetic and slice
gets, and reserve segment spans with MAP_FIXED_NOREPLACE.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Kernel-side hooks for the overlay changes, re-exported with scripts/export-patches.sh (now also exporting mm/madvise.c): - H-02a (0002): add mm_struct.kemu_guest, set early at exec, and strip host exec for guest VMAs in vma_set_page_prot(). - H-02b (0003): emulate_vsyscall() returns false for kemu tasks, so a forged return into the x86 vsyscall gate raises SIGSEGV instead of running an x86_64 syscall under AUDIT_ARCH_AARCH64 (seccomp confusion). - L-05 (0005): skip set_new_tls() for kemu CLONE_SETTLS so a tagged guest TPIDR_EL0 is accepted. - M-03.1 (0009): route a translated-code #DE through kemu_user_de(). - M-02 (0010): invalidate kemu translations on madvise(DONTNEED/FREE) and on FOLL_FORCE writes to executable mappings; strip host exec in vma_set_page_prot(). - L-01 (0011): hide the kemu region from numa_maps and skip it inside query_matching_vma() so a PROCMAP_QUERY COVERING_OR_NEXT scan keeps returning the real guest VMAs above it; report guest PC/SP in /proc stat. Verified: scripts/apply.sh applies the series to a pristine linux-6.18 with zero conflicts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kemu-sec: sigaltstack minimum size; prctl/mmap rejects; CLONE_SETTLS with a tagged TPIDR_EL0; DC ZVA through a tagged pointer; the kemu region hidden from /proc maps and from a PROCMAP_QUERY COVERING_OR_NEXT scan that still reaches the stack VMA; the sealed [vdso] rejecting mprotect/munmap; a ptrace syscall-skip that injects a return value; and a getpid svc run from freshly mapped pages while another thread churns mappings and a third keeps signalling, which must never be skipped. divrace: AArch64 division semantics (no trap) under multi-threaded flipping of a divisor pinned to a memory-resident guest register. vm-smoke.sh also toggles kernel.kemu.enabled. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CommaBot
force-pushed
the
fix/security-audit
branch
from
November 18, 2026 08:42
72082ef to
1b3ff6f
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.
Summary
Fix 15 security and correctness issues identified in a full-tree audit of
kemuagainst Linux v6.18:Critical & High
VM_SEALED): Install[kemu-code],[kemu-table],[kemu-slots], and the[vdso]sigreturn trampoline withVM_SEALEDonce all four mappings are in place. Previously, a guest could split theseVM_PFNMAPVMAs viamprotect/munmap/mmap(MAP_FIXED)so thatkemu_zap_region()onfork()orkemu_reclaim()on chunk retirement failed to zap split sub-VMAs, leaving live Ring-3 PTEs pointing to freed kernel physical pages.CX16,FSGSBASE,AES,PCLMULQDQ, and XCR0 AVX state inkemu_init()before enablingbinfmt_kemu(preventing Ring-0#UDintry_cmpxchg128or VEX-256 lowering on stripped CPUs/VMs). PassAES/PCLMULQDQ/SHA_NI/RDRANDavailability viacpu_feature_enabled()intokemu_coresoclearcpuid=is respected and an absentRDRANDconsistently clearsHWCAP2_RNG,ID_AA64ISAR0_EL1.RNDR, andRNDR/RNDRRS._PAGE_NXon guest VMAs & reject x86emulate_vsyscall(): Mark guestmm_structwithkemu_guestinkemu_set_personality()(before any stack or ELF VMA is mapped) sovma_set_page_prot()strips host executability from every non-kemu-region VMA from creation (perDESIGN.md §6.1). Returnfalsefor kemu tasks inemulate_vsyscall()so jumping to the x86 legacy vsyscall gate raisesSIGSEGVrather than running x86 syscall numbers underAUDIT_ARCH_AARCH64seccomp filters.kemu_corenon-test builds with#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic)], removeunwrap()and uncheckedSlow::pushindexing injit/t1/fp.rs, and buildkemu_rs.owith-Coverflow-checks=no.Medium & Low
sigaltstackminimum size: EnforceKEMU_MINSIGSTKSZ(5120 bytes) inkemu_sys_sigaltstack()so stacks between 2048 and 5119 bytes are rejected with-ENOMEMinstead of underflowing on arm64rt_sigframedelivery.svc: Hookmadvise(MADV_DONTNEED/MADV_FREE)andFOLL_FORCEwrites (__access_remote_vm) on executable VMAs; avoid skipping invalidation whenkm->wideblocks exist; trackatomic64_t unmap_seqbefore early returns inkemu_unmapped()to discard translations built concurrently withmunmap; trackKEMU_TF_PENDING_SVCso a guestsvcretired by the interpreter fallback (or single-step/helper) still entersinterp_svcacross epoch changes (kemu.stress, sibling invalidations) and rewindspcon signal materialization instead of being skipped.[kemu-slots]hardening: Recover raced cross-thread state-page divisors that raise#DEin translateddiv/idivviaexc_divide_error->kemu_user_de()single-step interpretation (producing architectural AArch640/INT_MINwithoutSIGFPEor leaking[kemu-code]addresses); clear reused slot return-stack pages; addREAD_ONCE()onkt->cpureads.tbi_pcsbounds & sysctls: Switch per-mmvzallocand XArray/IDA/sigframe allocations toGFP_KERNEL_ACCOUNT; clearkm->tbi_pcson flush and range invalidation; registerkernel.kemu.enabledandkernel.kemu.restrictsysctls.kemu-userfixes: Reject x86-onlyMAP_32BIT,PR_GET_TSC/PR_SET_TSC, andPR_SET_SYSCALL_USER_DISPATCH; hide private mappings inquery_matching_vma()(PROCMAP_QUERY) andnuma_mapswhile reporting guest PC/SP in/proc/<pid>/statand%pin debug signal logs; fixkemu_dup_mm()slot OOM return code (-ENOMEM); usepin_user_pages_fast()+ conditionalunpin_user_pages_dirty_lock()inkemu_cmpxchg128(); setKEMU_TF_KSTATEinkemu_gpr_set()/kemu_fpr_set()and propagatex0intoregs->axfor ptrace syscall-skip return injection; skip x86set_new_tls()forCLONE_SETTLSwith taggedTPIDR_EL0; untagRDXin TBIdc_zva(); hardentools/kemu-userELF bounds and useMAP_FIXED_NOREPLACE.Test Plan
cargo test(kemu_core+kemu-user),cargo clippy, andjit-testdifferential suites.scripts/apply.sh) andmake LLVM=1 bzImage(CONFIG_KEMU=y) against pristine Linux v6.18.tests/kernel/kemu-sec.candtests/kernel/divrace.c) andscripts/vm-smoke.sh(sysctltoggle).