diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 12:52:12 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 13:00:24 -0300 |
| commit | 8b28a0a5101a62fd55c2d55433e0fa899d4623f9 (patch) | |
| tree | de233408fc45f93e19bbcf637ed66242d569fc56 /targets/agent_pkey.c | |
| parent | 36d7f642e71207d04f5da80530da552e06447337 (diff) | |
| download | computeranim-8b28a0a5101a62fd55c2d55433e0fa899d4623f9.tar.gz computeranim-8b28a0a5101a62fd55c2d55433e0fa899d4623f9.zip | |
agent: its handlers act with the thread's protection keys and float modes, not a handler's
The kernel starts a signal handler with default PKRU (key 0 only) and
default MXCSR and x87 control word, the thread's own saved in the frame.
The SIGSYS handler makes the thread's syscalls and clones its threads
from there, so under --syscalls:
- a pkey_alloc's new rights were undone when the handler returned;
- a thread was born with no right to keyed memory (V8's compiler
threads fault reading the code space: d8 died of SEGV_PKUERR, and the
35 seconds were its core dump, not a spin);
- a buffer in keyed memory was EFAULT to the kernel, and a fault to the
agent reading it for the event;
- a thread started without its maker's float modes (flush-to-zero).
Both handlers now take the thread's PKRU and control words from the frame
first (signal.adopt), and the SIGSYS one writes PKRU back to the frame
after the syscall. Watched memory is read, and hooked code patched, with
every key open, so a key the thread itself cannot use hides nothing.
test-agent: agent_pkey (a keyed page read, given to syscalls, watched,
read by a later thread; skips itself without keys), and agent_sys's
thread checks its MXCSR. README: --alloc and programs with their own
malloc; the core dump.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'targets/agent_pkey.c')
| -rw-r--r-- | targets/agent_pkey.c | 58 |
1 files changed, 58 insertions, 0 deletions
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; +} |
