1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
//! 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 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.
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;
// 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)) {
// 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;
signal.keys.keep(uc);
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);
}
|