//! Syscalls without ptrace: Syscall User Dispatch. The kernel turns every //! syscall the thread makes into SIGSYS; the handler runs here, in the //! target, emits `syscall_enter` (with the path or the bytes written, read //! straight from memory), makes the syscall itself, emits `syscall_exit` //! and returns the result. No context switch to a tracer: two signal frames //! a syscall. //! //! The agent's own syscalls pass because the selector byte says so while its //! code runs. The target's syscall itself is made with the selector back on //! `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; /// The kernel reads this at each of the thread's syscalls: `block` while the /// target's code runs, `allow` while the agent's does. pub threadlocal var selector: u8 = allow; /// What the target's code runs with: `block` once syscalls are traced. pub var resting: u8 = allow; /// 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. const ours = signal.bit(.SYS) | signal.bit(.TRAP); fn nr(comptime name: linux.SYS) u64 { return @intFromEnum(name); } pub fn enable() void { signal.install(.SYS, onSyscall); resting = block; arm(); } /// Dispatch on for the calling thread: the kernel keeps it per thread, and a /// 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(); _ = linux.prctl(PR_SET_SYSCALL_USER_DISPATCH, PR_SYS_DISPATCH_ON, gate[0], gate[1], @intFromPtr(&selector)); } fn onSyscall(_: i32, _: *const linux.siginfo_t, uc: *signal.Ucontext) callconv(.c) void { selector = allow; 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)) { // The target leaves a handler of its own: its frame is where its // 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)); // What cannot be done as asked. var answer: ?u64 = null; var act: linux.k_sigaction = undefined; 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 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 = 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: 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 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]], agent.capture), nr(.writev), nr(.pwritev), nr(.pwritev2) => gathered(a[1], a[2]), else => "", }; } /// A thread's vector of buffers copied end to end, until its event is emitted. threadlocal var gather: [2048]u8 = undefined; /// What writev's buffers hold, as far as `capture` goes. fn gathered(vec: u64, count: u64) []const u8 { if (vec == 0) return ""; const iov: [*]const std.posix.iovec_const = @ptrFromInt(vec); const max = @min(agent.capture, gather.len); var len: usize = 0; for (iov[0..@min(count, 1024)]) |v| { const n = @min(v.len, max - len); @memcpy(gather[len..][0..n], v.base[0..n]); len += n; if (len == max) break; } return agent.text(gather[0..len], max); } fn string(addr: u64) []const u8 { if (addr == 0) return ""; const s: [*]const u8 = @ptrFromInt(addr); var len: usize = 0; while (len < agent.capture and s[len] != 0) len += 1; return agent.text(s[0..len], agent.capture); }