summaryrefslogtreecommitdiff
path: root/src/agent/sud.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 11:50:30 -0300
committerGabriel Schneider <[email protected]>2026-11-10 12:09:34 -0300
commit2be93397533432020413d6e0bb5021e2f565ef9c (patch)
treeb7212066211e857d3646a6eea5948da9c967b6d5 /src/agent/sud.zig
parentce7c0abf14e7ab056d5479adc3f05df9976e94e4 (diff)
downloadcomputeranim-2be93397533432020413d6e0bb5021e2f565ef9c.tar.gz
computeranim-2be93397533432020413d6e0bb5021e2f565ef9c.zip
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 <[email protected]>
Diffstat (limited to 'src/agent/sud.zig')
-rw-r--r--src/agent/sud.zig120
1 files changed, 86 insertions, 34 deletions
diff --git a/src/agent/sud.zig b/src/agent/sud.zig
index e556df3..a32735d 100644
--- a/src/agent/sud.zig
+++ b/src/agent/sud.zig
@@ -10,11 +10,17 @@
//! `block`, through the gate (signal.zig), the range of code the kernel
//! 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 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.
const std = @import("std");
const linux = std.os.linux;
const zt = @import("ztrace");
const agent = @import("agent.zig");
+const hook = @import("hook.zig");
const signal = @import("signal.zig");
+const CLONE = linux.CLONE;
pub const allow: u8 = 0;
pub const block: u8 = 1;
@@ -23,8 +29,10 @@ pub const block: u8 = 1;
pub threadlocal var selector: u8 = allow;
/// What the target's code runs with: `block` once syscalls are traced.
pub var resting: u8 = allow;
-/// 1 + the syscall this thread is making natively, under a single step.
-threadlocal var passing: u64 = 0;
+/// This thread has its alternate stack.
+threadlocal var stacked = false;
+/// The alternate stack the target believes it has: ours stays.
+threadlocal var pretended: linux.stack_t = .{ .sp = undefined, .flags = linux.SS.DISABLE, .size = 0 };
/// Signals the target may neither block nor take over: the kernel kills a
/// process whose forced signal is blocked.
@@ -36,8 +44,6 @@ fn nr(comptime name: linux.SYS) u64 {
pub fn enable() void {
signal.install(.SYS, onSyscall);
- // Syscalls let through natively are stepped over: hook.zig's handler calls `stepped`.
- @import("hook.zig").listen();
resting = block;
arm();
}
@@ -46,6 +52,13 @@ pub fn enable() void {
/// clone starts without it.
pub fn arm() void {
if (resting == allow) return;
+ if (!stacked) {
+ // shortcut: never unmapped, a thread's exit leaves it behind (256 KiB of address space, the pages it touched); free it from the thread's last syscall if a target makes threads by the thousand.
+ const size = 1 << 18;
+ const stack = linux.mmap(null, size, .{ .READ = true, .WRITE = true }, .{ .TYPE = .PRIVATE, .ANONYMOUS = true }, -1, 0);
+ if (linux.errno(stack) == .SUCCESS) _ = linux.sigaltstack(&.{ .sp = @ptrFromInt(stack), .flags = 0, .size = size }, null);
+ stacked = true;
+ }
const PR_SET_SYSCALL_USER_DISPATCH = 59;
const PR_SYS_DISPATCH_ON = 1;
const gate = signal.gate();
@@ -58,64 +71,103 @@ fn onSyscall(_: i32, _: *const linux.siginfo_t, uc: *signal.Ucontext) callconv(.
var a = [6]u64{ uc.rdi, uc.rsi, uc.rdx, uc.r10, uc.r8, uc.r9 };
if (number == nr(.rt_sigreturn)) {
// The target leaves a handler of its own: its frame is where its
- // stack pointer is, and ours, pushed below, goes with it.
+ // stack pointer is, and ours goes with it.
selector = block;
signal.ztrace_gate_sigreturn(uc.rsp);
}
agent.emit(.syscall_enter, @truncate(number), a, argument(number, a));
- if (number == nr(.clone3) or number == nr(.vfork) or (number == nr(.clone) and a[0] & linux.CLONE.VM != 0)) {
- // The child starts on another stack, or shares this one: not from
- // inside a handler. Back onto the syscall instruction with dispatch
- // off and one step to go; `stepped` turns it on again.
- passing = number + 1;
- uc.rip -= 2;
- uc.eflags |= signal.trap_flag;
- return;
- }
+
+ // What cannot be done as asked.
+ var answer: ?u64 = null;
var act: linux.k_sigaction = undefined;
- var faked = false;
if (number == nr(.rt_sigaction) and a[1] != 0) {
- // shortcut: the target is told its handler for one of our signals is installed, and it is not; chain to it when a target needs SIGTRAP or SIGSYS itself.
- faked = a[0] == @intFromEnum(linux.SIG.SYS) or a[0] == @intFromEnum(linux.SIG.TRAP);
+ // shortcut: the target is told its handler for one of our signals is installed, and it is not; chain to it when a target needs SIGSYS, or SIGTRAP beside hooks.
+ if (a[0] == @intFromEnum(linux.SIG.SYS) or (a[0] == @intFromEnum(linux.SIG.TRAP) and hook.active())) answer = 0;
act = @as(*const linux.k_sigaction, @ptrFromInt(a[1])).*;
act.mask[0] &= ~ours;
a[1] = @intFromPtr(&act);
+ } else if (number == nr(.sigaltstack)) {
+ if (a[1] != 0) @as(*linux.stack_t, @ptrFromInt(a[1])).* = pretended;
+ if (a[0] != 0) pretended = @as(*const linux.stack_t, @ptrFromInt(a[0])).*;
+ answer = 0;
}
+
selector = block;
- const result = if (faked) 0 else signal.ztrace_gate_syscall(number, a[0], a[1], a[2], a[3], a[4], a[5]);
+ const result = answer orelse if (cloning(number, a)) |child| result: {
+ var born: Born = .{ .uc = uc, .flags = child.flags, .stack = child.stack };
+ const pid = signal.ztrace_gate_clone(number, a[0], a[1], a[2], a[3], a[4], a[5], &born, &Born.start);
+ // A thread reads this frame as it starts: until it has, the frame stays.
+ if (@as(i64, @bitCast(pid)) > 0 and child.flags & (CLONE.VM | CLONE.VFORK) == CLONE.VM) {
+ while (born.read.load(.acquire) == 0) std.atomic.spinLoopHint();
+ }
+ break :result pid;
+ } else signal.ztrace_gate_syscall(number, a[0], a[1], a[2], a[3], a[4], a[5]);
selector = allow;
+
if (number == nr(.rt_sigprocmask)) {
// The mask it set is this handler's; the target's is the one the frame restores.
_ = linux.sigprocmask(linux.SIG.BLOCK, null, @ptrCast(&uc.sigmask));
uc.sigmask &= ~ours;
}
- // A forked child.
- if (result == 0 and (number == nr(.fork) or number == nr(.clone))) agent.forked();
+ // A forked child: a copy of everything, this frame included, which it returns through.
+ 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;
selector = block;
}
-/// A single step ended (hook.zig's SIGTRAP handler): if it was over a syscall
-/// let through, here is its exit, in the parent and in a new thread both.
-pub fn stepped(uc: *const signal.Ucontext) void {
- if (resting == allow) return;
- agent.threadStart();
- if (passing != 0) {
- // shortcut: the step may end one instruction after the syscall, which could have changed rax; none of libc's do.
- agent.emit(.syscall_exit, @truncate(passing - 1), .{ uc.rax, 0, 0, 0, 0, 0 }, "");
- passing = 0;
- }
- selector = block;
+/// A clone whose child cannot return through this handler's frames: it
+/// starts on a stack of its own, or shares its parent's (vfork) and may not
+/// touch them. Null for the rest: a syscall like any other, a fork included.
+fn cloning(number: u64, a: [6]u64) ?struct { flags: u64, stack: bool } {
+ const flags, const stack = switch (number) {
+ nr(.vfork) => .{ CLONE.VM | CLONE.VFORK, false },
+ nr(.clone) => .{ a[0], a[1] != 0 },
+ nr(.clone3) => .{ @as(*align(1) const [6]u64, @ptrFromInt(a[0]))[0], @as(*align(1) const [6]u64, @ptrFromInt(a[0]))[5] != 0 },
+ else => return null,
+ };
+ return if (stack or flags & CLONE.VM != 0) .{ .flags = flags, .stack = stack } else null;
}
+/// What the gate hands a clone's child: where the target was, to continue there.
+const Born = struct {
+ uc: *const signal.Ucontext,
+ flags: u64,
+ stack: bool,
+ /// The child has what it needs of `uc`.
+ read: std.atomic.Value(u32) = .init(0),
+
+ /// The child's first code, on its own stack, or below its parent's
+ /// frames on the parent's. `regs` is what the gate then loads, in its
+ /// order, the instruction pointer and the stack pointer last; `sp` is
+ /// the stack pointer the kernel started the child with.
+ fn start(born: *Born, regs: *[14]u64, sp: u64) callconv(.c) void {
+ const uc = born.uc;
+ const flags = born.flags;
+ regs.* = .{ uc.r8, uc.r9, uc.r10, uc.r12, uc.r13, uc.r14, uc.r15, uc.rdi, uc.rsi, uc.rbp, uc.rbx, uc.rdx, uc.rip, if (born.stack) sp else uc.rsp };
+ born.read.store(1, .release);
+ if (flags & CLONE.VM == 0) {
+ // A process of its own, without our handlers if it asked to start with none.
+ if (flags & CLONE.CLEAR_SIGHAND != 0) zt.client.tid = 0 else agent.forked();
+ } else if (flags & CLONE.VFORK != 0) {
+ // On loan from its parent until it execs: nothing to set up, and
+ // the signal stack is the parent's, which is on it.
+ _ = linux.sigaltstack(&.{ .sp = undefined, .flags = linux.SS.DISABLE, .size = 0 }, null);
+ } else if (flags & CLONE.SETTLS != 0) {
+ agent.threadStart();
+ selector = block;
+ }
+ // Else a thread with its parent's thread-local storage: nothing of ours can tell the two apart, and it runs unseen.
+ }
+};
+
/// What a syscall's pointer argument points at, for the event's payload.
// shortcut: read without checking; a pointer the kernel would refuse with EFAULT faults here instead.
fn argument(number: u64, a: [6]u64) []const u8 {
return switch (number) {
nr(.open), nr(.stat), nr(.lstat), nr(.access), nr(.execve), nr(.chdir), nr(.unlink), nr(.readlink), nr(.mkdir), nr(.rmdir), nr(.truncate), nr(.chmod) => string(a[0]),
nr(.openat), nr(.newfstatat), nr(.unlinkat), nr(.readlinkat), nr(.faccessat), nr(.faccessat2), nr(.statx), nr(.mkdirat), nr(.execveat) => string(a[1]),
- nr(.write), nr(.pwrite64), nr(.sendto) => if (a[1] == 0) "" else agent.text(@as([*]const u8, @ptrFromInt(a[1]))[0..a[2]]),
+ nr(.write), nr(.pwrite64), nr(.sendto) => if (a[1] == 0) "" else agent.text(@as([*]const u8, @ptrFromInt(a[1]))[0..a[2]], agent.capture),
else => "",
};
}
@@ -124,6 +176,6 @@ fn string(addr: u64) []const u8 {
if (addr == 0) return "";
const s: [*]const u8 = @ptrFromInt(addr);
var len: usize = 0;
- while (len < agent.max_payload and s[len] != 0) len += 1;
- return agent.text(s[0..len]);
+ while (len < agent.capture and s[len] != 0) len += 1;
+ return agent.text(s[0..len], agent.capture);
}