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 --- targets/agent_sys.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'targets/agent_sys.c') diff --git a/targets/agent_sys.c b/targets/agent_sys.c index 12a6ada..85b460a 100644 --- a/targets/agent_sys.c +++ b/targets/agent_sys.c @@ -7,6 +7,10 @@ #include #include #include +#include + +// MXCSR's flush-to-zero and denormals-are-zero: what -ffast-math turns on. +enum { flush_to_zero = 0x8040 }; static int fd; @@ -19,8 +23,9 @@ static void on_signal(int signo) { say("signal\n"); } +// A thread starts with the floating-point modes of the thread that made it. static void *thread(void *arg) { - say(arg); + say((_mm_getcsr() & flush_to_zero) == flush_to_zero ? arg : "thread without its maker's modes\n"); return NULL; } @@ -41,6 +46,7 @@ int main(void) { raise(SIGUSR1); pthread_t t; + _mm_setcsr(_mm_getcsr() | flush_to_zero); pthread_create(&t, NULL, thread, "thread\n"); pthread_join(t, NULL); -- cgit v1.3