Truly independent web browser
LibJS+LibSandbox: Allocate the JIT protection key before threads exist
Andreas Klinge4c2da702e64
diff --git a/Libraries/LibJS/Rust/src/jit/executable_memory.rs b/Libraries/LibJS/Rust/src/jit/executable_memory.rsindex 02a5e30c77f..21393f6e261 100644--- a/Libraries/LibJS/Rust/src/jit/executable_memory.rs+++ b/Libraries/LibJS/Rust/src/jit/executable_memory.rs@@ -8,10 +8,10 @@//!//! Code is carved out of large chunks that are mapped read+execute, and every write makes the affected pages writable//! (and not executable) for the duration of the copy: memory is never writable and executable at once. On Linux-//! x86-64 with memory protection keys, the pages are writable and executable, but a protection key keeps every thread-//! from writing them except the one writing code, while it writes. On macOS, chunks are MAP_JIT mappings whose write-//! protection is toggled per thread instead. On Windows, chunks are only reserved, and pages get committed when code is-//! first written to them.+//! x86-64 with memory protection keys, the pages are writable and executable, but a protection key, allocated before+//! the process has threads, keeps every thread from writing them except the one writing code, while it writes. On+//! macOS, chunks are MAP_JIT mappings whose write protection is toggled per thread instead. On Windows, chunks are only+//! reserved, and pages get committed when code is first written to them.//!//! Every VM has its own chunks, which only the VM's thread writes and runs code in, so flipping the protection of//! pages that hold other installed code is safe.@@ -68,21 +68,45 @@ mod protection_key {) -> libc::c_int;}+ static KEY: OnceLock<libc::c_int> = OnceLock::new();++ // NB: pkey_alloc() sets the rights to the key for the running thread only, and a new thread copies the rights of+ // the thread that creates it. Allocating the key while the library loads, before there is a second thread, is+ // what makes every thread start out unable to write with it.+ #[used]+ #[unsafe(link_section = ".init_array")]+ static ALLOCATE_KEY_ON_LOAD: extern "C" fn() = allocate_key;++ extern "C" fn allocate_key() {+ // NB: A thread that exists already would keep whatever rights it has to the key.+ if !process_has_one_thread() {+ return;+ }+ let key = pkey_alloc(0, PKEY_DISABLE_WRITE);+ if key >= 0 {+ let _ = KEY.set(key);+ }+ }++ fn process_has_one_thread() -> bool {+ // NB: A directory that cannot be read to its end may list any number of threads.+ std::fs::read_dir("/proc/self/task")+ .and_then(|threads| threads.collect::<Result<Vec<_>, _>>())+ .is_ok_and(|threads| threads.len() == 1)+ }+/// The memory protection key of chunks whose write access is toggled per thread with the key instead of with- /// mprotect(), or None if the CPU or kernel lacks protection keys. Threads may write the chunks only while they- /// write code; the pages themselves are writable and executable. One key serves every VM in the process.+ /// mprotect(), or None if the CPU or kernel lacks protection keys, or the process had threads when this library+ /// loaded. Threads may write the chunks only while they write code; the pages themselves are writable and+ /// executable. One key serves every VM in the process.pub fn key() -> Option<libc::c_int> {- static KEY: OnceLock<Option<libc::c_int>> = OnceLock::new();- *KEY.get_or_init(|| {- let key = pkey_alloc(0, PKEY_DISABLE_WRITE);- (key >= 0).then_some(key)- })+ KEY.get().copied()}/// Allows (or forbids again) the running thread to write memory with the key.pub fn set_writable(key: libc::c_int, writable: bool) {let rights = if writable { 0 } else { PKEY_DISABLE_WRITE };- // SAFETY: The key was allocated by key().+ // SAFETY: The key was allocated by allocate_key().let result = unsafe { pkey_set(key, rights) };assert_eq!(result, 0, "pkey_set() failed");}diff --git a/Libraries/LibSandbox/Seccomp.cpp b/Libraries/LibSandbox/Seccomp.cppindex a435f932f18..9443001bc9e 100644--- a/Libraries/LibSandbox/Seccomp.cpp+++ b/Libraries/LibSandbox/Seccomp.cpp@@ -1824,12 +1824,13 @@ void SeccompPolicy::allow_writable_executable_memory_mappings()// in its own register. Only pages with a key may be writable and executable.void SeccompPolicy::allow_memory_protection_keys(){-#if defined(__NR_pkey_alloc) && defined(__NR_pkey_free) && defined(__NR_pkey_mprotect)- SECCOMP_APPEND_ALLOW_SYSCALL(*this, pkey_alloc);- SECCOMP_APPEND_ALLOW_SYSCALL(*this, pkey_free);-- // NB: Key 0 is the default key, which every thread may write, and -1- // makes pkey_mprotect() behave like mprotect().+#ifdef __NR_pkey_mprotect+ // NB: LibJS allocates its key while it loads, before the sandbox starts.+ // With pkey_alloc() allowed, sandboxed code could make itself a key+ // that it may write. We deny key 0 and key -1 (0xFFFFFFFF). Key 0 is+ // the default key, which every thread may write, and -1 makes+ // pkey_mprotect() behave like mprotect(). Either value would let+ // sandboxed code make pages writable and executable without a key.append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_pkey_mprotect, 0, 5));append(SECCOMP_LOAD_ARGUMENT(3));append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, 2, 0));