-
Notifications
You must be signed in to change notification settings - Fork 5k
runtime transpiler cache: per-uid root, ownership check, mandatory payload hashes #35747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
2bcdc36
497d1c9
0d0da21
7d453d6
6a88f14
55e264d
75cbcd1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,7 +51,9 @@ | |
| /// Version 25: Every ModuleInfo record carries a trailing FetchParameters slot | ||
| /// so ImportEntry/ExportEntry/StarExportEntry moduleRequestType matches JSC's | ||
| /// after WebKit 90b2ecf79ae3 keyed m_loadedModules on (specifier, type). | ||
| const EXPECTED_VERSION: u32 = 25; | ||
| /// Version 26: section hashes are seeded with `input_hash` (not the fixed | ||
| /// `SEED`) and a stored hash of 0 no longer skips verification. | ||
| const EXPECTED_VERSION: u32 = 26; | ||
|
|
||
| /// Source files smaller than this are not written to / read from the on-disk | ||
| /// transpiler cache. Originally 50 KiB, which excluded almost every file in a | ||
|
|
@@ -336,11 +338,9 @@ | |
| ..Default::default() | ||
| }; | ||
|
|
||
| metadata.output_hash = hash(output_bytes); | ||
| metadata.sourcemap_hash = hash(sourcemap); | ||
| if !esm_record.is_empty() { | ||
| metadata.esm_record_hash = hash(esm_record); | ||
| } | ||
| metadata.output_hash = Wyhash::hash(input_hash, output_bytes); | ||
| metadata.sourcemap_hash = Wyhash::hash(input_hash, sourcemap); | ||
| metadata.esm_record_hash = Wyhash::hash(input_hash, esm_record); | ||
|
|
||
| let mut metadata_stream = bun_io::FixedBufferStream::new_mut(&mut metadata_buf[..]); | ||
| metadata.encode(&mut metadata_stream)?; | ||
|
|
@@ -432,6 +432,8 @@ | |
| return Err(crate::CrateError::MissingData); | ||
| } | ||
|
|
||
| let section_seed = self.metadata.input_hash; | ||
|
|
||
| debug_assert!( | ||
| matches!(&self.output_code, OutputCode::Utf8(b) if b.is_empty()), | ||
| "this should be the default value" | ||
|
|
@@ -474,7 +476,7 @@ | |
| return Err(crate::CrateError::MissingData); | ||
| } | ||
|
|
||
| if self.metadata.output_hash != 0 && hash(bytes) != self.metadata.output_hash { | ||
| if Wyhash::hash(section_seed, bytes) != self.metadata.output_hash { | ||
| return Err(crate::CrateError::InvalidHash); | ||
| } | ||
|
|
||
|
|
@@ -503,17 +505,14 @@ | |
| // errdefer latin1.deref() — BunString is `Copy`, so guard explicitly. | ||
| let errdefer = scopeguard::guard(latin1, |s| s.deref()); | ||
| let read_bytes = file.pread_all(bytes, self.metadata.output_byte_offset)?; | ||
|
|
||
| if self.metadata.output_hash != 0 { | ||
| if hash(latin1.latin1()) != self.metadata.output_hash { | ||
| return Err(crate::CrateError::InvalidHash); | ||
| } | ||
| } | ||
|
|
||
| if read_bytes as u64 != self.metadata.output_byte_length { | ||
| return Err(crate::CrateError::MissingData); | ||
| } | ||
|
|
||
| if Wyhash::hash(section_seed, latin1.latin1()) != self.metadata.output_hash { | ||
| return Err(crate::CrateError::InvalidHash); | ||
| } | ||
|
|
||
| scopeguard::ScopeGuard::into_inner(errdefer); | ||
| OutputCode::String(latin1) | ||
| } | ||
|
|
@@ -537,11 +536,9 @@ | |
| return Err(crate::CrateError::MissingData); | ||
| } | ||
|
|
||
| if self.metadata.output_hash != 0 { | ||
| let utf16_bytes: &[u8] = bytemuck::cast_slice(string.utf16()); | ||
| if hash(utf16_bytes) != self.metadata.output_hash { | ||
| return Err(crate::CrateError::InvalidHash); | ||
| } | ||
| let utf16_bytes: &[u8] = bytemuck::cast_slice(string.utf16()); | ||
| if Wyhash::hash(section_seed, utf16_bytes) != self.metadata.output_hash { | ||
| return Err(crate::CrateError::InvalidHash); | ||
| } | ||
|
|
||
| scopeguard::ScopeGuard::into_inner(errdefer); | ||
|
|
@@ -562,6 +559,9 @@ | |
| self.metadata.sourcemap_byte_length as usize, | ||
| self.metadata.sourcemap_byte_offset, | ||
| )?; | ||
| if Wyhash::hash(section_seed, &self.sourcemap) != self.metadata.sourcemap_hash { | ||
| return Err(crate::CrateError::InvalidHash); | ||
| } | ||
| } | ||
|
|
||
| if self.metadata.esm_record_byte_length > 0 { | ||
|
|
@@ -571,10 +571,8 @@ | |
| self.metadata.esm_record_byte_offset, | ||
| )?; | ||
|
|
||
| if self.metadata.esm_record_hash != 0 { | ||
| if hash(&esm_record) != self.metadata.esm_record_hash { | ||
| return Err(crate::CrateError::InvalidHash); | ||
| } | ||
| if Wyhash::hash(section_seed, &esm_record) != self.metadata.esm_record_hash { | ||
| return Err(crate::CrateError::InvalidHash); | ||
| } | ||
|
|
||
| self.esm_record = esm_record; | ||
|
|
@@ -613,6 +611,54 @@ | |
| Wyhash::hash(SEED, bytes) | ||
| } | ||
|
|
||
| #[cfg(unix)] | ||
| #[inline] | ||
| fn current_user_id() -> u32 { | ||
| bun_sys::c::getuid() as u32 | ||
| } | ||
|
|
||
| #[cfg(windows)] | ||
| #[inline] | ||
| fn current_user_id() -> u32 { | ||
| bun_sys::windows::user_unique_id() | ||
| } | ||
|
|
||
| #[cfg(unix)] | ||
| fn is_trusted_dir_stat(st: &sys::Stat) -> bool { | ||
| (st.st_mode & libc::S_IFMT) == libc::S_IFDIR | ||
| && st.st_uid == bun_sys::c::getuid() | ||
| && (st.st_mode & (libc::S_IWGRP | libc::S_IWOTH)) == 0 | ||
| } | ||
|
robobun marked this conversation as resolved.
|
||
|
|
||
| /// `true` when `path` lstat's as a directory owned by the current uid with no | ||
| /// group/other write bits, or does not exist. Any other result fails closed. | ||
|
robobun marked this conversation as resolved.
|
||
| #[cfg(unix)] | ||
| fn is_trusted_cache_root(path: &ZStr) -> bool { | ||
| match sys::lstat(path) { | ||
| Ok(st) => is_trusted_dir_stat(&st), | ||
| Err(e) if e.get_errno() == sys::E::ENOENT => true, | ||
| Err(_) => false, | ||
|
Comment on lines
+637
to
+641
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift Eliminate the cache-root TOCTOU window. Line 636 accepts a nonexistent root after Open or create the root with no-follow semantics. Validate ownership and mode with Also applies to: 799-809 🤖 Prompt for AI AgentsSource: Coding guidelines
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Partially addressed in 497d1c9. The write side now re-checks the directory it actually opened: The read side still opens Moving reads to a held directory fd plus
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The read-side condition remains. The default-root argument reduces exposure for normal configurations. It does not remove the condition for supported explicit paths. I agree that a held directory fd plus If this does not block the PR, would you like me to open a GitHub follow-up issue for the read-side ✏️ Learnings added
You are interacting with an AI system. |
||
| } | ||
| } | ||
|
|
||
| /// Same predicate as `is_trusted_cache_root`, on the directory we opened. | ||
| #[cfg(unix)] | ||
| fn is_trusted_opened_cache_dir(fd: Fd) -> bool { | ||
| sys::fstat(fd).is_ok_and(|st| is_trusted_dir_stat(&st)) | ||
| } | ||
|
|
||
| #[cfg(not(unix))] | ||
| #[inline(always)] | ||
| fn is_trusted_cache_root(_path: &ZStr) -> bool { | ||
| true | ||
| } | ||
|
|
||
| #[cfg(not(unix))] | ||
| #[inline(always)] | ||
| fn is_trusted_opened_cache_dir(_fd: Fd) -> bool { | ||
| true | ||
| } | ||
|
|
||
| /// Allocate `len` bytes and fill them via `pread_all` at `offset`, returning | ||
| /// `MissingData` on a short read. | ||
| /// | ||
|
|
@@ -702,8 +748,13 @@ | |
| // that `absBufZ` used. | ||
| let top = FileSystem::instance().top_level_dir; | ||
|
|
||
| let mut seg = [0u8; 4 + 10]; | ||
| seg[..4].copy_from_slice(b"@t@-"); | ||
| let n = bun_core::fmt::print_int(&mut seg[4..], current_user_id()); | ||
| let tcache_seg: &[u8] = &seg[..4 + n]; | ||
|
Check warning on line 754 in src/jsc/RuntimeTranspilerCache.rs
|
||
|
robobun marked this conversation as resolved.
|
||
|
|
||
| if let Some(dir) = env_var::XDG_CACHE_HOME.get() { | ||
| let parts: &[&[u8]] = &[dir, b"bun", b"@t@"]; | ||
| let parts: &[&[u8]] = &[dir, b"bun", tcache_seg]; | ||
| return path_handler::join_abs_string_buf_z::<platform::Loose>( | ||
| top, | ||
| &mut buf[..], | ||
|
|
@@ -717,7 +768,7 @@ | |
| // On a mac, default to ~/Library/Caches/bun/* | ||
| // This is different than ~/.bun/install/cache, and not configurable by the user. | ||
| if let Some(home) = env_var::HOME.get() { | ||
| let parts: &[&[u8]] = &[home, b"Library/", b"Caches/", b"bun", b"@t@"]; | ||
| let parts: &[&[u8]] = &[home, b"Library/", b"Caches/", b"bun", tcache_seg]; | ||
| return path_handler::join_abs_string_buf_z::<platform::Loose>( | ||
| top, | ||
| &mut buf[..], | ||
|
|
@@ -728,7 +779,7 @@ | |
| } | ||
|
|
||
| if let Some(dir) = env_var::HOME.get() { | ||
| let parts: &[&[u8]] = &[dir, b".bun", b"install", b"cache", b"@t@"]; | ||
| let parts: &[&[u8]] = &[dir, b".bun", b"install", b"cache", tcache_seg]; | ||
| return path_handler::join_abs_string_buf_z::<platform::Loose>( | ||
| top, | ||
| &mut buf[..], | ||
|
|
@@ -760,8 +811,17 @@ | |
| let path_len = match Self::RUNTIME_TRANSPILER_CACHE.with(|c| c.get()) { | ||
| Some(len) => len, | ||
| None => { | ||
| let len = Self::CACHE_DIR_BUF | ||
| .with_borrow_mut(|tl_buf| Self::really_get_cache_dir(tl_buf)); | ||
| let len = Self::CACHE_DIR_BUF.with_borrow_mut(|tl_buf| { | ||
| let len = Self::really_get_cache_dir(tl_buf); | ||
| if len > 0 && !is_trusted_cache_root(ZStr::from_buf(&tl_buf[..], len)) { | ||
| bun_core::scoped_log!( | ||
| cache, | ||
| "transpiler cache root failed ownership/mode check, disabling" | ||
| ); | ||
| return 0; | ||
| } | ||
| len | ||
| }); | ||
| if len == 0 { | ||
| IS_DISABLED.store(true, Ordering::Relaxed); | ||
| return Err(crate::CrateError::CacheDisabled); | ||
|
|
@@ -899,6 +959,11 @@ | |
| } | ||
| }); | ||
|
|
||
| if cache_dir_fd != Fd::cwd() && !is_trusted_opened_cache_dir(cache_dir_fd) { | ||
| IS_DISABLED.store(true, Ordering::Relaxed); | ||
| return Err(crate::CrateError::CacheDisabled); | ||
| } | ||
|
|
||
| Entry::save( | ||
| cache_dir_fd, | ||
| cache_file_path, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.