From 8b28a0a5101a62fd55c2d55433e0fa899d4623f9 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 12:52:12 -0300 Subject: agent: its handlers act with the thread's protection keys and float modes, not a handler's The kernel starts a signal handler with default PKRU (key 0 only) and default MXCSR and x87 control word, the thread's own saved in the frame. The SIGSYS handler makes the thread's syscalls and clones its threads from there, so under --syscalls: - a pkey_alloc's new rights were undone when the handler returned; - a thread was born with no right to keyed memory (V8's compiler threads fault reading the code space: d8 died of SEGV_PKUERR, and the 35 seconds were its core dump, not a spin); - a buffer in keyed memory was EFAULT to the kernel, and a fault to the agent reading it for the event; - a thread started without its maker's float modes (flush-to-zero). Both handlers now take the thread's PKRU and control words from the frame first (signal.adopt), and the SIGSYS one writes PKRU back to the frame after the syscall. Watched memory is read, and hooked code patched, with every key open, so a key the thread itself cannot use hides nothing. test-agent: agent_pkey (a keyed page read, given to syscalls, watched, read by a later thread; skips itself without keys), and agent_sys's thread checks its MXCSR. README: --alloc and programs with their own malloc; the core dump. Co-Authored-By: Claude Fable 5.2 --- src/agent/signal.zig | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) (limited to 'src/agent/signal.zig') diff --git a/src/agent/signal.zig b/src/agent/signal.zig index 66baf2d..9a14222 100644 --- a/src/agent/signal.zig +++ b/src/agent/signal.zig @@ -51,6 +51,103 @@ pub const Ucontext = extern struct { } }; +/// What a handler has to take back before it acts for the thread it +/// interrupted. The kernel starts every handler with defaults (rights to +/// memory behind protection keys: key 0 only; floating-point modes: the +/// initial ones) and keeps the thread's own in the frame until the handler +/// returns. A syscall made for the thread, a thread started for it, its +/// memory read for it: all would happen as somebody else. +pub fn adopt(uc: *const Ucontext) void { + if (uc.fpstate == 0) return; + const area: [*]const u8 = @ptrFromInt(uc.fpstate); + // FXSAVE: the x87 control word first, MXCSR at 24. + asm volatile ( + \\ fldcw (%[area]) + \\ ldmxcsr 24(%[area]) + : + : [area] "r" (area), + ); + if (keys.inFrame(uc)) |in| keys.set(if (in.present.* & keys.component != 0) in.pkru.* else 0); +} + +/// Memory protection keys: a thread's rights to protected memory are a +/// register, PKRU, two bits a key. +pub const keys = struct { + /// Where XSAVE keeps PKRU; 0 when the CPU or the kernel has no protection keys. + var offset: u32 = 0; + /// PKRU's bit among XSAVE's state components. + const component: u64 = 1 << 9; + + /// Once, before any handler is installed. + pub fn detect() void { + // Leaf 7: the kernel has turned the keys on (OSPKE). Leaf 0xD, component 9: where PKRU is saved. + if (cpuid(7, 0)[2] & (1 << 4) != 0) offset = cpuid(0xd, 9)[1]; + } + + /// eax, ebx, ecx, edx. + fn cpuid(leaf: u32, subleaf: u32) [4]u32 { + var eax: u32 = undefined; + var ebx: u32 = undefined; + var ecx: u32 = undefined; + var edx: u32 = undefined; + asm volatile ("cpuid" + : [a] "={eax}" (eax), + [b] "={ebx}" (ebx), + [c] "={ecx}" (ecx), + [d] "={edx}" (edx), + : [leaf] "{eax}" (leaf), + [subleaf] "{ecx}" (subleaf), + ); + return .{ eax, ebx, ecx, edx }; + } + + pub fn get() u32 { + return asm volatile ("rdpkru" + : [pkru] "={eax}" (-> u32), + : [zero] "{ecx}" (@as(u32, 0)), + : .{ .edx = true }); + } + + pub fn set(pkru: u32) void { + asm volatile ("wrpkru" + : + : [pkru] "{eax}" (pkru), + [c] "{ecx}" (@as(u32, 0)), + [d] "{edx}" (@as(u32, 0)), + ); + } + + /// Every key's memory readable and writable to this thread, for the + /// agent's own look at what is watched or hooked. Returns what to `set` + /// back; null without protection keys. + pub fn open() ?u32 { + if (offset == 0) return null; + const was = get(); + set(0); + return was; + } + + /// The interrupted thread's PKRU in its signal frame, and the header + /// word that says whether it is there at all (absent, it is 0). + fn inFrame(uc: *const Ucontext) ?struct { pkru: *align(1) u32, present: *align(1) u64 } { + if (offset == 0 or uc.fpstate == 0) return null; + const area: [*]u8 = @ptrFromInt(uc.fpstate); + // After FXSAVE's 512 bytes comes XSAVE's header, when the kernel says so at 464. + const extended: *align(1) const extern struct { magic: u32, size: u32, features: u64, state_size: u32 } = @ptrCast(area + 464); + if (extended.magic != 0x46505853 or extended.features & component == 0 or extended.state_size < offset + 4) return null; + return .{ .pkru = @ptrCast(area + offset), .present = @ptrCast(area + 512) }; + } + + /// The rights as they are now are the thread's from the handler's + /// return on: a pkey_alloc made in the handler changed them here, and + /// the frame would put the old ones back. + pub fn keep(uc: *const Ucontext) void { + const in = inFrame(uc) orelse return; + in.pkru.* = get(); + in.present.* |= component; + } +}; + /// The trap flag: one instruction, then SIGTRAP. pub const trap_flag = 0x100; -- cgit v1.3