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/hook.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/hook.zig')
| -rw-r--r-- | src/agent/hook.zig | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/src/agent/hook.zig b/src/agent/hook.zig index fa44cc4..88e187c 100644 --- a/src/agent/hook.zig +++ b/src/agent/hook.zig @@ -61,15 +61,24 @@ pub fn add(addr: usize) void { const code: *[4]u8 = @ptrFromInt(addr); hooks[count] = .{ .addr = addr, .first = code.* }; count += 1; - code[0] = 0xcc; + poke(addr, 0xcc); +} + +/// One byte of code changed, whatever protection key the code is behind (a JIT's is). +fn poke(addr: usize, byte: u8) void { + const rights = signal.keys.open(); + @as(*u8, @ptrFromInt(addr)).* = byte; + if (rights) |was| signal.keys.set(was); } fn onTrap(_: i32, info: *const linux.siginfo_t, uc: *signal.Ucontext) callconv(.c) void { + // As the thread, not with a handler's defaults: its stack and what is watched are read from here. + signal.adopt(uc); const TRAP_TRACE = 2; if (info.code == TRAP_TRACE) { // The step over a hooked function's first instruction ended. Whose, // nobody wrote down: all the breakpoints go back in. - for (hooks[0..count]) |h| @as(*u8, @ptrFromInt(h.addr)).* = 0xcc; + for (hooks[0..count]) |h| poke(h.addr, 0xcc); uc.eflags &= ~@as(u64, signal.trap_flag); return; } @@ -99,7 +108,7 @@ fn onTrap(_: i32, info: *const linux.siginfo_t, uc: *signal.Ucontext) callconv(. uc.rip += 4; } else { // shortcut: while the byte is back another thread can enter unseen; a relocated copy of the instruction would close that. - @as(*u8, @ptrFromInt(hook.addr)).* = hook.first[0]; + poke(hook.addr, hook.first[0]); uc.eflags |= signal.trap_flag; } } |
