diff options
| -rw-r--r-- | README.md | 12 | ||||
| -rw-r--r-- | build/agent.zig | 9 | ||||
| -rw-r--r-- | src/agent/agent.zig | 10 | ||||
| -rw-r--r-- | src/agent/check.zig | 35 | ||||
| -rw-r--r-- | src/agent/hook.zig | 15 | ||||
| -rw-r--r-- | src/agent/signal.zig | 97 | ||||
| -rw-r--r-- | src/agent/sud.zig | 12 | ||||
| -rw-r--r-- | src/agent/watch.zig | 4 | ||||
| -rw-r--r-- | targets/agent_pkey.c | 58 | ||||
| -rw-r--r-- | targets/agent_sys.c | 8 |
10 files changed, 252 insertions, 8 deletions
@@ -322,7 +322,11 @@ protection makes the kernel refuse syscalls that write there, and soft-dirty bits still need the compare, a page at a time. **Syscalls** go through SIGSYS because the kernel then needs no second process: the cost is two signal frames, on a stack of the agent's so that it works on threads with -almost none. +almost none. A handler starts with the kernel's defaults, not the thread's +rights to memory behind protection keys (PKRU) nor its floating-point +modes; the agent's handlers take the thread's back first, so that a syscall +made for it, a thread cloned for it and a watched page read for it are as +the thread would have them (V8 keeps its code space behind a key). Measured (`zig build bench-agent -Doptimize=ReleaseFast`, an i7-11700, each loop 10^5 to 5 10^6 times; bytes are the recording's): @@ -350,7 +354,11 @@ Limits: a hooked function called with SIGTRAP blocked dies (not under do not unwind through a hooked call; a pointer the kernel would answer with EFAULT faults in the handler; syscalls made from the vDSO never enter the kernel and are not seen, as under ptrace; a target that needs SIGSYS or its -own alternate signal stack is told it has them. +own alternate signal stack is told it has them. `--alloc` sees calls that +reach libc's allocator by name: a program that brings its own `malloc` and +binds it inside itself (d8 does) shows none, and `--hook malloc` is the way +to see those. A target that dies under the agent dumps core like any other, +which for a V8 takes half a minute; `ulimit -c 0` if that is not wanted. ## The kernel's view diff --git a/build/agent.zig b/build/agent.zig index c1e1dcc..d02880a 100644 --- a/build/agent.zig +++ b/build/agent.zig @@ -82,6 +82,15 @@ pub fn add(ctx: Ctx) *Compile { exe.root_module.linkLibrary(libs[1]); t.expect("api", "", t.record(exe.getEmittedBin(), false, &.{}, &.{})); } + // Memory behind a protection key: syscalls into it, a thread made after + // it, a watch on it and a hook beside it (the target skips itself where + // there are no keys). + const pkey = ctx.cTarget("agent_pkey", &.{"agent_pkey.c"}); + pkey.root_module.addIncludePath(b.path("include")); + pkey.root_module.linkLibrary(libs[1]); + t.expect("pkey", "", t.record(pkey.getEmittedBin(), false, &.{ "--syscalls", "--hook", "reader" }, &.{})); + b.installArtifact(pkey); + const api_zig = ctx.zigTarget("agent_zig", "targets/agent_zig.zig"); api_zig.root_module.addImport("ztrace-agent", module); api_zig.use_llvm = true; diff --git a/src/agent/agent.zig b/src/agent/agent.zig index 2a32a05..0e23de9 100644 --- a/src/agent/agent.zig +++ b/src/agent/agent.zig @@ -45,6 +45,7 @@ pub fn init() void { if (state.load(.acquire) != 0 or state.cmpxchgStrong(0, 1, .acquire, .monotonic) != null) return; defer state.store(2, .release); const r = client.get() orelse return; + signal.keys.detect(); var buf: [1 << 16]u8 = undefined; const env = client.environ(&buf); cut = if (client.getenv(env, "ZTRACE_CUT")) |v| std.fmt.parseInt(u64, v, 0) catch 0 else 0; @@ -161,14 +162,17 @@ pub fn mark(label: []const u8, value: u64) void { /// Names `memory` (a `variable` event), records it, and keeps recording what /// changes in it: only the bytes that changed, before each event that ends a -/// moment. At most `max_region` bytes, which must stay mapped. Watched from -/// inside a hooked call, it is forgotten when that call returns. +/// moment. At most `max_region` bytes, which must stay mapped; a protection +/// key on it does not hide it. Watched from inside a hooked call, it is +/// forgotten when that call returns. pub fn watch(memory: []const u8, name: []const u8) void { init(); if (ring == null or tooLarge(memory, "watch")) return; const was = sud.selector; sud.selector = sud.allow; defer sud.selector = was; + const rights = signal.keys.open(); + defer if (rights) |r| signal.keys.set(r); const id = stringId(name); emit(.string, 0, .{ id, 0, 0, 0, 0, 0 }, text(name, max_payload)); emit(.variable, 0, .{ @intFromPtr(memory.ptr), memory.len, id, 0, 0, 0 }, ""); @@ -180,6 +184,8 @@ pub fn watch(memory: []const u8, name: []const u8) void { pub fn snapshot(memory: []const u8) void { init(); if (ring == null or tooLarge(memory, "snapshot")) return; + const rights = signal.keys.open(); + defer if (rights) |r| signal.keys.set(r); write(@intFromPtr(memory.ptr), memory); } diff --git a/src/agent/check.zig b/src/agent/check.zig index 8657665..348f497 100644 --- a/src/agent/check.zig +++ b/src/agent/check.zig @@ -89,6 +89,8 @@ pub fn main(init: std.process.Init) !void { try annotated(r); } else if (std.mem.eql(u8, scenario, "syscalls")) { try syscalls(r); + } else if (std.mem.eql(u8, scenario, "pkey")) { + try protected(r); } else return error.NoSuchScenario; } @@ -207,6 +209,39 @@ fn annotated(r: Recording) !void { try sorted(r, "numbers"); } +/// agent_pkey: syscalls into memory behind a protection key did what they +/// do natively, from the thread that made the key and from one made after; +/// the page was seen while its thread could not read it. +fn protected(r: Recording) !void { + const SYS = std.os.linux.SYS; + var writers: std.AutoArrayHashMapUnmanaged(u32, void) = .empty; + var reads: std.ArrayList(i64) = .empty; + var key: i64 = -1; + var writing = false; + for (r.items) |item| switch (item.ev.kind) { + .syscall_enter => writing = item.ev.aux == @intFromEnum(SYS.write) and std.mem.eql(u8, item.payload, "protected\n"), + .syscall_exit => if (item.ev.aux == @intFromEnum(SYS.pkey_alloc)) { + key = @bitCast(item.ev.a[0]); + } else if (item.ev.aux == @intFromEnum(SYS.read) and key > 0) { + try reads.append(r.arena, @bitCast(item.ev.a[0])); + } else if (writing) { + try expectEqual(@as(u64, 10), item.ev.a[0]); + try writers.put(r.arena, item.ev.tid, {}); + writing = false; + }, + else => {}, + }; + // No protection keys on this machine: nothing to hold the agent to. + if (key < 0) return; + try expectEqual(@as(usize, 2), writers.count()); + try std.testing.expectEqualSlices(i64, &.{ 16, -@as(i64, @intFromEnum(std.os.linux.E.FAULT)) }, reads.items); + const page = try r.variable("page"); + var text: [10]u8 = undefined; + try (try r.trace.moment(r.trace.len - 1)).read(page.ev.a[0], &text, null); + try expectEqualStrings("protected\n", &text); + try expect(r.count(.mark) == 1 and r.count(.call) == 1); +} + /// agent_sys: what it wrote, from which thread, and every syscall entered /// is left, innermost first. fn syscalls(r: Recording) !void { diff --git a/src/agent/hook.zig b/src/agent/hook.zig index fa44cc4..88e187c 100644 --- a/src/agent/hook.zig +++ b/src/agent/hook.zig @@ -61,15 +61,24 @@ pub fn add(addr: usize) void { const code: *[4]u8 = @ptrFromInt(addr); hooks[count] = .{ .addr = addr, .first = code.* }; count += 1; - code[0] = 0xcc; + poke(addr, 0xcc); +} + +/// One byte of code changed, whatever protection key the code is behind (a JIT's is). +fn poke(addr: usize, byte: u8) void { + const rights = signal.keys.open(); + @as(*u8, @ptrFromInt(addr)).* = byte; + if (rights) |was| signal.keys.set(was); } fn onTrap(_: i32, info: *const linux.siginfo_t, uc: *signal.Ucontext) callconv(.c) void { + // As the thread, not with a handler's defaults: its stack and what is watched are read from here. + signal.adopt(uc); const TRAP_TRACE = 2; if (info.code == TRAP_TRACE) { // The step over a hooked function's first instruction ended. Whose, // nobody wrote down: all the breakpoints go back in. - for (hooks[0..count]) |h| @as(*u8, @ptrFromInt(h.addr)).* = 0xcc; + for (hooks[0..count]) |h| poke(h.addr, 0xcc); uc.eflags &= ~@as(u64, signal.trap_flag); return; } @@ -99,7 +108,7 @@ fn onTrap(_: i32, info: *const linux.siginfo_t, uc: *signal.Ucontext) callconv(. uc.rip += 4; } else { // shortcut: while the byte is back another thread can enter unseen; a relocated copy of the instruction would close that. - @as(*u8, @ptrFromInt(hook.addr)).* = hook.first[0]; + poke(hook.addr, hook.first[0]); uc.eflags |= signal.trap_flag; } } diff --git a/src/agent/signal.zig b/src/agent/signal.zig index 66baf2d..9a14222 100644 --- a/src/agent/signal.zig +++ b/src/agent/signal.zig @@ -51,6 +51,103 @@ pub const Ucontext = extern struct { } }; +/// 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; diff --git a/src/agent/sud.zig b/src/agent/sud.zig index 5d2c6ad..41d7cb6 100644 --- a/src/agent/sud.zig +++ b/src/agent/sud.zig @@ -11,6 +11,14 @@ //! 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. @@ -67,6 +75,9 @@ pub fn arm() void { 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)) { @@ -113,6 +124,7 @@ fn onSyscall(_: i32, _: *const linux.siginfo_t, uc: *signal.Ucontext) callconv(. 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; } diff --git a/src/agent/watch.zig b/src/agent/watch.zig index 356dac8..fb46b09 100644 --- a/src/agent/watch.zig +++ b/src/agent/watch.zig @@ -7,6 +7,7 @@ const std = @import("std"); const linux = std.os.linux; const agent = @import("agent.zig"); +const signal = @import("signal.zig"); const Region = struct { addr: usize, @@ -65,6 +66,9 @@ pub fn diff() void { // Busy: another thread is at it, or this one is, below this frame. if (busy.swap(true, .acquire)) return; defer busy.store(false, .release); + // Whatever key the memory is behind, and whether this thread may read it now. + const rights = signal.keys.open(); + defer if (rights) |was| signal.keys.set(was); for (table[0..count]) |r| { var changes: Changes = .{ .old = r.shadow[0..r.len], .new = @as([*]const u8, @ptrFromInt(r.addr))[0..r.len] }; while (changes.next()) |run| agent.write(r.addr + run.at, run.bytes); diff --git a/targets/agent_pkey.c b/targets/agent_pkey.c new file mode 100644 index 0000000..d915de2 --- /dev/null +++ b/targets/agent_pkey.c @@ -0,0 +1,58 @@ +// Memory behind a protection key, used the ways a JIT uses its code space: +// read as soon as the key is made, handed to syscalls, watched, and read by +// a thread made later. Each step dies or fails if the tracer acts with other +// rights to that memory than the thread has. Where the CPU or the kernel +// has no protection keys it says so and exits 0. +#define _GNU_SOURCE +#include <fcntl.h> +#include <pthread.h> +#include <stdio.h> +#include <string.h> +#include <sys/mman.h> +#include <unistd.h> +#include <ztrace.h> + +static char *page; +static int out; + +// Made after the key: it has its maker's rights, to read and not to write. +__attribute__((noinline)) static void *reader(void *arg) { + (void)arg; + if (page[0] != 'p') return (void *)1; + return (void *)(long)(write(out, page, 10) != 10); +} + +int main(void) { + int key = pkey_alloc(0, PKEY_DISABLE_WRITE); + if (key < 0) { + puts("no protection keys"); + return 0; + } + page = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (pkey_mprotect(page, 4096, PROT_READ | PROT_WRITE, key)) return 1; + // The key came with the right to read. + if (page[0] != 0) return 2; + ztrace_watch(page, 64, "page"); + out = open("/dev/null", O_WRONLY); + int zero = open("/dev/zero", O_RDONLY); + + // With the right to write, the kernel may write there for us too... + pkey_set(key, 0); + if (read(zero, page, 16) != 16) return 3; + memcpy(page, "protected\n", 10); + // ... and without it, it may not. + pkey_set(key, PKEY_DISABLE_WRITE); + if (read(zero, page, 16) != -1) return 4; + if (write(out, page, 10) != 10) return 5; + + // With no right at all the thread still runs, and the tracer still sees the page. + pkey_set(key, PKEY_DISABLE_ACCESS); + ztrace_mark("hidden", 0); + pkey_set(key, PKEY_DISABLE_WRITE); + + pthread_t thread; + void *failed; + if (pthread_create(&thread, NULL, reader, NULL) || pthread_join(thread, &failed) || failed) return 6; + puts("protected"); + return 0; +} diff --git a/targets/agent_sys.c b/targets/agent_sys.c index 12a6ada..85b460a 100644 --- a/targets/agent_sys.c +++ b/targets/agent_sys.c @@ -7,6 +7,10 @@ #include <string.h> #include <sys/wait.h> #include <unistd.h> +#include <xmmintrin.h> + +// MXCSR's flush-to-zero and denormals-are-zero: what -ffast-math turns on. +enum { flush_to_zero = 0x8040 }; static int fd; @@ -19,8 +23,9 @@ static void on_signal(int signo) { say("signal\n"); } +// A thread starts with the floating-point modes of the thread that made it. static void *thread(void *arg) { - say(arg); + say((_mm_getcsr() & flush_to_zero) == flush_to_zero ? arg : "thread without its maker's modes\n"); return NULL; } @@ -41,6 +46,7 @@ int main(void) { raise(SIGUSR1); pthread_t t; + _mm_setcsr(_mm_getcsr() | flush_to_zero); pthread_create(&t, NULL, thread, "thread\n"); pthread_join(t, NULL); |
