From 2be93397533432020413d6e0bb5021e2f565ef9c Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 11:50:30 -0300 Subject: agent: syscalls on a stack of the agent's, clones made inside the handler; what a recording may hold Syscall User Dispatch now runs its handler on an alternate stack a thread (a thread's own stack may not hold a signal frame; node makes such threads), and makes clone, clone3 and vfork itself: the child leaves through the gate with the target's registers instead of being stepped into with the trap flag. Paths and written buffers are cut at 256 bytes (--capture), a watch or snapshot above 1 MiB is refused, and ztrace run ends a recording that is full (--max-size) with the target killed and the file closed. Co-Authored-By: Claude Fable 5.2 --- src/agent/signal.zig | 56 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) (limited to 'src/agent/signal.zig') diff --git a/src/agent/signal.zig b/src/agent/signal.zig index 99aead0..66baf2d 100644 --- a/src/agent/signal.zig +++ b/src/agent/signal.zig @@ -65,8 +65,8 @@ pub fn bit(sig: linux.SIG) u64 { comptime { asm ( \\ .pushsection .text.ztrace_gate, "ax", @progbits - \\ .globl ztrace_gate_sigreturn, ztrace_gate_restorer, ztrace_gate_syscall, ztrace_gate_end - \\ .hidden ztrace_gate_sigreturn, ztrace_gate_restorer, ztrace_gate_syscall, ztrace_gate_end + \\ .globl ztrace_gate_sigreturn, ztrace_gate_restorer, ztrace_gate_syscall, ztrace_gate_clone, ztrace_gate_end + \\ .hidden ztrace_gate_sigreturn, ztrace_gate_restorer, ztrace_gate_syscall, ztrace_gate_clone, ztrace_gate_end \\ ztrace_gate_sigreturn: \\ movq %rdi, %rsp \\ ztrace_gate_restorer: @@ -82,6 +82,46 @@ comptime { \\ movq 8(%rsp), %r9 \\ syscall \\ retq + \\ ztrace_gate_clone: + \\ pushq %rbx + \\ pushq %r12 + \\ movq 32(%rsp), %rbx + \\ movq 40(%rsp), %r12 + \\ movq %rdi, %rax + \\ movq %rsi, %rdi + \\ movq %rdx, %rsi + \\ movq %rcx, %rdx + \\ movq %r8, %r10 + \\ movq %r9, %r8 + \\ movq 24(%rsp), %r9 + \\ syscall + \\ testq %rax, %rax + \\ jz 1f + \\ popq %r12 + \\ popq %rbx + \\ retq + \\ 1: movq %rsp, %rdx + \\ andq $-16, %rsp + \\ subq $112, %rsp + \\ movq %rbx, %rdi + \\ movq %rsp, %rsi + \\ callq *%r12 + \\ popq %r8 + \\ popq %r9 + \\ popq %r10 + \\ popq %r12 + \\ popq %r13 + \\ popq %r14 + \\ popq %r15 + \\ popq %rdi + \\ popq %rsi + \\ popq %rbp + \\ popq %rbx + \\ popq %rdx + \\ popq %rcx + \\ popq %rsp + \\ xorl %eax, %eax + \\ jmpq *%rcx \\ ztrace_gate_end: \\ .popsection ); @@ -93,6 +133,14 @@ pub extern fn ztrace_gate_sigreturn(uc: u64) callconv(.c) noreturn; extern fn ztrace_gate_restorer() callconv(.c) void; /// Any syscall, by number. pub extern fn ztrace_gate_syscall(number: u64, a0: u64, a1: u64, a2: u64, a3: u64, a4: u64, a5: u64) callconv(.c) u64; +/// A clone-family syscall whose child cannot come back the way its parent +/// does (another stack, or its parent's own). The parent returns like +/// `ztrace_gate_syscall`. The child calls `start(context, regs, sp)` on the +/// stack it was given, then continues with the fourteen registers `start` +/// wrote (r8 r9 r10 r12 r13 r14 r15 rdi rsi rbp rbx rdx, then the +/// instruction pointer and the stack pointer) and 0 in rax: where the +/// target's own clone would have returned. +pub extern fn ztrace_gate_clone(number: u64, a0: u64, a1: u64, a2: u64, a3: u64, a4: u64, a5: u64, context: *anyopaque, start: *const anyopaque) callconv(.c) u64; extern const ztrace_gate_end: u8; /// Where the gate is: start and length. @@ -104,11 +152,11 @@ pub const Handler = *const fn (i32, *const linux.siginfo_t, *Ucontext) callconv( /// Null puts the default action back. The handler runs with no signal /// blocked, its own included: inside it the thread is still the target, with -/// the target's mask. +/// the target's mask. On the alternate stack, where the thread has one. pub fn install(sig: linux.SIG, handler: ?Handler) void { const act: linux.k_sigaction = .{ .handler = @ptrCast(handler), - .flags = linux.SA.SIGINFO | linux.SA.RESTORER | linux.SA.NODEFER, + .flags = linux.SA.SIGINFO | linux.SA.RESTORER | linux.SA.NODEFER | linux.SA.ONSTACK, .restorer = @ptrCast(&ztrace_gate_restorer), .mask = linux.sigemptyset(), }; -- cgit v1.3