summaryrefslogtreecommitdiff
path: root/src/agent/signal.zig
blob: 9a14222b77d7981ba7e58204d0e7e2efac1d1d0e (plain) (blame)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//! What the agent's signal handlers share: the frame the kernel pushes on
//! x86-64, and handlers installed with the raw syscall, behind libc's back.
const std = @import("std");
const linux = std.os.linux;
const zt = @import("ztrace");

/// The kernel's `struct ucontext`: the interrupted thread's registers, which
/// a handler may change before they are restored.
pub const Ucontext = extern struct {
    flags: u64,
    link: ?*Ucontext,
    stack: extern struct { sp: usize, flags: i32, size: usize },
    r8: u64,
    r9: u64,
    r10: u64,
    r11: u64,
    r12: u64,
    r13: u64,
    r14: u64,
    r15: u64,
    rdi: u64,
    rsi: u64,
    rbp: u64,
    rbx: u64,
    rdx: u64,
    rax: u64,
    rcx: u64,
    rsp: u64,
    rip: u64,
    eflags: u64,
    cs: u16,
    gs: u16,
    fs: u16,
    ss: u16,
    err: u64,
    trapno: u64,
    oldmask: u64,
    cr2: u64,
    fpstate: usize,
    reserved: [8]u64,
    sigmask: u64,

    comptime {
        std.debug.assert(@offsetOf(Ucontext, "sigmask") == 296);
    }

    pub fn regs(uc: *const Ucontext) zt.Regs {
        var out: zt.Regs = .{ .orig_rax = uc.rax, .cs = uc.cs, .ss = uc.ss, .fs = uc.fs, .gs = uc.gs };
        inline for (zt.Regs.names) |name| @field(out, name) = @field(uc, name);
        return out;
    }
};

/// What a handler has to take back before it acts for the thread it
/// interrupted. The kernel starts every handler with defaults (rights to
/// memory behind protection keys: key 0 only; floating-point modes: the
/// initial ones) and keeps the thread's own in the frame until the handler
/// returns. A syscall made for the thread, a thread started for it, its
/// memory read for it: all would happen as somebody else.
pub fn adopt(uc: *const Ucontext) void {
    if (uc.fpstate == 0) return;
    const area: [*]const u8 = @ptrFromInt(uc.fpstate);
    // FXSAVE: the x87 control word first, MXCSR at 24.
    asm volatile (
        \\ fldcw (%[area])
        \\ ldmxcsr 24(%[area])
        :
        : [area] "r" (area),
    );
    if (keys.inFrame(uc)) |in| keys.set(if (in.present.* & keys.component != 0) in.pkru.* else 0);
}

/// Memory protection keys: a thread's rights to protected memory are a
/// register, PKRU, two bits a key.
pub const keys = struct {
    /// Where XSAVE keeps PKRU; 0 when the CPU or the kernel has no protection keys.
    var offset: u32 = 0;
    /// PKRU's bit among XSAVE's state components.
    const component: u64 = 1 << 9;

    /// Once, before any handler is installed.
    pub fn detect() void {
        // Leaf 7: the kernel has turned the keys on (OSPKE). Leaf 0xD, component 9: where PKRU is saved.
        if (cpuid(7, 0)[2] & (1 << 4) != 0) offset = cpuid(0xd, 9)[1];
    }

    /// eax, ebx, ecx, edx.
    fn cpuid(leaf: u32, subleaf: u32) [4]u32 {
        var eax: u32 = undefined;
        var ebx: u32 = undefined;
        var ecx: u32 = undefined;
        var edx: u32 = undefined;
        asm volatile ("cpuid"
            : [a] "={eax}" (eax),
              [b] "={ebx}" (ebx),
              [c] "={ecx}" (ecx),
              [d] "={edx}" (edx),
            : [leaf] "{eax}" (leaf),
              [subleaf] "{ecx}" (subleaf),
        );
        return .{ eax, ebx, ecx, edx };
    }

    pub fn get() u32 {
        return asm volatile ("rdpkru"
            : [pkru] "={eax}" (-> u32),
            : [zero] "{ecx}" (@as(u32, 0)),
            : .{ .edx = true });
    }

    pub fn set(pkru: u32) void {
        asm volatile ("wrpkru"
            :
            : [pkru] "{eax}" (pkru),
              [c] "{ecx}" (@as(u32, 0)),
              [d] "{edx}" (@as(u32, 0)),
        );
    }

    /// Every key's memory readable and writable to this thread, for the
    /// agent's own look at what is watched or hooked. Returns what to `set`
    /// back; null without protection keys.
    pub fn open() ?u32 {
        if (offset == 0) return null;
        const was = get();
        set(0);
        return was;
    }

    /// The interrupted thread's PKRU in its signal frame, and the header
    /// word that says whether it is there at all (absent, it is 0).
    fn inFrame(uc: *const Ucontext) ?struct { pkru: *align(1) u32, present: *align(1) u64 } {
        if (offset == 0 or uc.fpstate == 0) return null;
        const area: [*]u8 = @ptrFromInt(uc.fpstate);
        // After FXSAVE's 512 bytes comes XSAVE's header, when the kernel says so at 464.
        const extended: *align(1) const extern struct { magic: u32, size: u32, features: u64, state_size: u32 } = @ptrCast(area + 464);
        if (extended.magic != 0x46505853 or extended.features & component == 0 or extended.state_size < offset + 4) return null;
        return .{ .pkru = @ptrCast(area + offset), .present = @ptrCast(area + 512) };
    }

    /// The rights as they are now are the thread's from the handler's
    /// return on: a pkey_alloc made in the handler changed them here, and
    /// the frame would put the old ones back.
    pub fn keep(uc: *const Ucontext) void {
        const in = inFrame(uc) orelse return;
        in.pkru.* = get();
        in.present.* |= component;
    }
};

/// The trap flag: one instruction, then SIGTRAP.
pub const trap_flag = 0x100;

/// The bit of a signal in a kernel signal set.
pub fn bit(sig: linux.SIG) u64 {
    return @as(u64, 1) << @intCast(@intFromEnum(sig) - 1);
}

// The gate: the only syscall instructions the dispatcher (sud.zig) lets
// through whatever the selector says. One symbol after the other, so that
// the range between the first and the last is these and nothing else.
comptime {
    asm (
        \\ .pushsection .text.ztrace_gate, "ax", @progbits
        \\ .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:
        \\     movl $15, %eax
        \\     syscall
        \\ ztrace_gate_syscall:
        \\     movq %rdi, %rax
        \\     movq %rsi, %rdi
        \\     movq %rdx, %rsi
        \\     movq %rcx, %rdx
        \\     movq %r8, %r10
        \\     movq %r9, %r8
        \\     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
    );
}

/// rt_sigreturn from the signal frame whose `Ucontext` is at `uc`.
pub extern fn ztrace_gate_sigreturn(uc: u64) callconv(.c) noreturn;
/// The same for a handler that returns: its stack pointer is there already.
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.
pub fn gate() [2]usize {
    return .{ @intFromPtr(&ztrace_gate_sigreturn), @intFromPtr(&ztrace_gate_end) - @intFromPtr(&ztrace_gate_sigreturn) };
}

pub const Handler = *const fn (i32, *const linux.siginfo_t, *Ucontext) callconv(.c) void;

/// 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. 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 | linux.SA.ONSTACK,
        .restorer = @ptrCast(&ztrace_gate_restorer),
        .mask = linux.sigemptyset(),
    };
    _ = linux.syscall4(.rt_sigaction, @intFromEnum(sig), @intFromPtr(&act), 0, @sizeOf(linux.sigset_t));
}