diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 12:52:12 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 13:00:24 -0300 |
| commit | 8b28a0a5101a62fd55c2d55433e0fa899d4623f9 (patch) | |
| tree | de233408fc45f93e19bbcf637ed66242d569fc56 /src/agent/sud.zig | |
| parent | 36d7f642e71207d04f5da80530da552e06447337 (diff) | |
| download | computeranim-8b28a0a5101a62fd55c2d55433e0fa899d4623f9.tar.gz computeranim-8b28a0a5101a62fd55c2d55433e0fa899d4623f9.zip | |
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 <[email protected]>
Diffstat (limited to 'src/agent/sud.zig')
| -rw-r--r-- | src/agent/sud.zig | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/src/agent/sud.zig b/src/agent/sud.zig index 5d2c6ad..41d7cb6 100644 --- a/src/agent/sud.zig +++ b/src/agent/sud.zig @@ -11,6 +11,14 @@ //! always lets through: a signal that interrupts it runs the target's handler //! as the target's code, its syscalls seen like any other. //! +//! The kernel starts a handler with default rights to memory behind +//! protection keys and default floating-point modes, the thread's own put +//! away in the frame. The handler takes the thread's first (`signal.adopt`): +//! otherwise a buffer in protected memory is EFAULT to the syscall, a +//! pkey_alloc's new rights are undone by the handler's return, and a thread +//! cloned here is born without its maker's rights (V8's compiler threads +//! then fault reading the code space). +//! //! The handler runs on an alternate stack of the agent's, one a thread: the //! target's stack may have no room for a signal frame (a thread made with a //! few kilobytes), and a vfork child takes its parent's stack over. @@ -67,6 +75,9 @@ pub fn arm() void { fn onSyscall(_: i32, _: *const linux.siginfo_t, uc: *signal.Ucontext) callconv(.c) void { selector = allow; + // The syscall is the thread's: made with its rights to protected memory, + // and a thread it starts gets its floating-point modes, not a handler's. + signal.adopt(uc); const number = uc.rax; var a = [6]u64{ uc.rdi, uc.rsi, uc.rdx, uc.r10, uc.r8, uc.r9 }; if (number == nr(.rt_sigreturn)) { @@ -113,6 +124,7 @@ fn onSyscall(_: i32, _: *const linux.siginfo_t, uc: *signal.Ucontext) callconv(. if (result == 0 and (number == nr(.fork) or number == nr(.clone) or number == nr(.clone3))) agent.forked(); agent.emit(.syscall_exit, @truncate(number), .{ result, 0, 0, 0, 0, 0 }, ""); uc.rax = result; + signal.keys.keep(uc); selector = block; } |
