summaryrefslogtreecommitdiff
path: root/src/agent
diff options
context:
space:
mode:
Diffstat (limited to 'src/agent')
-rw-r--r--src/agent/agent.zig10
-rw-r--r--src/agent/check.zig35
-rw-r--r--src/agent/hook.zig15
-rw-r--r--src/agent/signal.zig97
-rw-r--r--src/agent/sud.zig12
-rw-r--r--src/agent/watch.zig4
6 files changed, 168 insertions, 5 deletions
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);