//! Bubblewrap-based filesystem sandboxing for Linux. //! //! This module mirrors the semantics used by the macOS Seatbelt sandbox: //! - the filesystem is read-only by default, //! - explicit writable roots are layered on top, and //! - sensitive subpaths such as `.git`, `.agents`, and `.codex` remain //! read-only even when their parent root is writable. //! //! Restricted execution also hides WSLg's duplicate distro root so it cannot //! expose filesystem contents outside the policy's path masks. Explicit grants and //! command paths using that alias are rejected before constructing mounts. //! //! The overall Linux sandbox is composed of: //! - seccomp + `PR_SET_NO_NEW_PRIVS` applied in-process, and //! - bubblewrap used to construct the filesystem view before exec. use std::collections::BTreeMap; use std::collections::BTreeSet; use std::collections::HashSet; use std::fs; use std::fs::File; use std::fs::Metadata; use std::io; use std::os::fd::AsRawFd; use std::os::unix::fs::MetadataExt; use std::path::Path; use std::path::PathBuf; use std::sync::Mutex; use crate::linux_run_main::synthetic_mount_registry_root; use codex_protocol::error::CodexErr; use codex_protocol::error::Result; use codex_protocol::permissions::FileSystemPolicyProjection; use codex_protocol::permissions::is_protected_metadata_name; use codex_protocol::protocol::FileSystemAccessMode; use codex_protocol::protocol::FileSystemPath; use codex_protocol::protocol::FileSystemSandboxPolicy; use codex_protocol::protocol::FileSystemSpecialPath; use codex_protocol::protocol::WritableRoot; use codex_utils_absolute_path::AbsolutePathBuf; use ignore::WalkState; use ignore::overrides::OverrideBuilder; /// Linux "platform defaults" that keep common system binaries and dynamic /// libraries readable when a split filesystem policy requests `:minimal`. /// /// These are intentionally system-level paths only (plus Nix store roots) so /// `include_platform_defaults` does not silently widen access to user data. pub(super) const LINUX_PLATFORM_DEFAULT_READ_ROOTS: &[&str] = &[ "/bin", "/sbin", "/usr", "/etc", "/lib", "/lib64", "/nix/store", "/run/current-system/sw", ]; const MAX_UNREADABLE_GLOB_MATCHES: usize = 8192; pub(crate) const WSL_INTEROP_DIR: &str = "/run/WSL"; pub(crate) const WSLG_DISTRO_ROOT: &str = "/mnt/wslg/distro"; /// Options that control how bubblewrap is invoked. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) struct BwrapOptions { /// Whether to mount a fresh `/proc`; disabled by `--no-proc` or mount fallback. pub mount_proc: bool, /// Explicitly reuse the caller's PID namespace instead of creating one. pub inherit_pid_namespace: bool, /// How networking should be configured inside the bubblewrap sandbox. pub network_mode: BwrapNetworkMode, /// Hide the WSL Windows interop socket from commands with restricted filesystem access. pub mask_wsl_interop: bool, /// Whether the host exposes WSLg's duplicate distro root, which must be hidden /// when constructing a restricted filesystem view. pub mask_wslg_distro: bool, /// Optional maximum depth for expanding unreadable glob patterns. /// /// Keep this uncapped by default so existing nested deny-read matches are /// masked before the sandboxed command starts. pub glob_scan_max_depth: Option, } impl Default for BwrapOptions { fn default() -> Self { Self { mount_proc: true, inherit_pid_namespace: false, network_mode: BwrapNetworkMode::FullAccess, mask_wsl_interop: false, mask_wslg_distro: false, glob_scan_max_depth: None, } } } /// Network policy modes for bubblewrap. #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub(crate) enum BwrapNetworkMode { /// Keep access to the host network namespace. #[default] FullAccess, /// Remove access to the host network namespace. Isolated, /// Intended proxy-only mode. /// /// Bubblewrap enforces this by unsharing the network namespace. The /// proxy-routing bridge is established by the helper process after startup. ProxyOnly, /// The transparent TCP setup already isolated this command's network namespace. PreparedProxy, } impl BwrapNetworkMode { fn should_unshare_network(self) -> bool { matches!(self, Self::Isolated | Self::ProxyOnly) } } #[derive(Debug)] pub(crate) struct BwrapArgs { pub args: Vec, pub preserved_files: Vec, pub synthetic_mount_targets: Vec, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] struct FileIdentity { dev: u64, ino: u64, } impl FileIdentity { fn from_metadata(metadata: &Metadata) -> Self { Self { dev: metadata.dev(), ino: metadata.ino(), } } } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum SyntheticMountTargetKind { EmptyFile, EmptyDirectory, } #[derive(Clone, Debug, PartialEq, Eq)] pub(crate) struct SyntheticMountTarget { path: PathBuf, kind: SyntheticMountTargetKind, // If an empty metadata path was already present, remember its inode so // cleanup does not delete a real pre-existing file or directory. pre_existing_path: Option, } impl SyntheticMountTarget { pub(crate) fn missing(path: &Path) -> Self { Self { path: path.to_path_buf(), kind: SyntheticMountTargetKind::EmptyFile, pre_existing_path: None, } } pub(crate) fn missing_empty_directory(path: &Path) -> Self { Self { path: path.to_path_buf(), kind: SyntheticMountTargetKind::EmptyDirectory, pre_existing_path: None, } } pub(crate) fn existing_empty_file(path: &Path, metadata: &Metadata) -> Self { Self { path: path.to_path_buf(), kind: SyntheticMountTargetKind::EmptyFile, pre_existing_path: Some(FileIdentity::from_metadata(metadata)), } } fn existing_empty_directory(path: &Path, metadata: &Metadata) -> Self { Self { path: path.to_path_buf(), kind: SyntheticMountTargetKind::EmptyDirectory, pre_existing_path: Some(FileIdentity::from_metadata(metadata)), } } pub(crate) fn preserves_pre_existing_path(&self) -> bool { self.pre_existing_path.is_some() } pub(crate) fn path(&self) -> &Path { &self.path } pub(crate) fn kind(&self) -> SyntheticMountTargetKind { self.kind } pub(crate) fn should_remove_after_bwrap(&self, metadata: &Metadata) -> bool { match self.kind { SyntheticMountTargetKind::EmptyFile => { if !metadata.file_type().is_file() || metadata.len() != 0 { return false; } } SyntheticMountTargetKind::EmptyDirectory => { if !metadata.file_type().is_dir() { return false; } } } match self.pre_existing_path { Some(pre_existing_path) => pre_existing_path != FileIdentity::from_metadata(metadata), None => true, } } } /// Wrap a command with bubblewrap so the filesystem is read-only by default, /// with explicit writable roots and read-only subpaths layered afterward. /// /// When the policy grants full disk write access and full network access, this /// returns `command` unchanged so we avoid unnecessary sandboxing overhead. /// If network isolation is requested, we still wrap with bubblewrap so network /// namespace restrictions apply while preserving full filesystem access. pub(crate) fn create_bwrap_command_args( command: Vec, file_system_sandbox_policy: &FileSystemSandboxPolicy, sandbox_policy_cwd: &Path, command_cwd: &Path, options: BwrapOptions, ) -> Result { // Full disk write normally skips bwrap, but unreadable glob patterns still // need concrete bwrap masks for the matches expanded below. if file_system_sandbox_policy.has_full_disk_write_access() && file_system_sandbox_policy .get_unreadable_globs_with_cwd(sandbox_policy_cwd) .is_empty() { return if options.network_mode == BwrapNetworkMode::FullAccess { Ok(BwrapArgs { args: command, preserved_files: Vec::new(), synthetic_mount_targets: Vec::new(), }) } else { Ok(create_bwrap_flags_full_filesystem(command, options)) }; } create_bwrap_flags( command, file_system_sandbox_policy, sandbox_policy_cwd, command_cwd, options, ) } fn create_bwrap_flags_full_filesystem(command: Vec, options: BwrapOptions) -> BwrapArgs { let mut args = vec![ "--new-session".to_string(), "--die-with-parent".to_string(), "--bind".to_string(), "/".to_string(), "/".to_string(), // Preserve nodev on the root bind while exposing only standard devices. "--dev".to_string(), "/dev".to_string(), // Restore shared memory without exposing other host devices. "--bind-try".to_string(), "/dev/shm".to_string(), "/dev/shm".to_string(), // Always enter a fresh user namespace so root inside a container does // not need ambient CAP_SYS_ADMIN to create the remaining namespaces. "--unshare-user".to_string(), "--unshare-ipc".to_string(), ]; if !options.inherit_pid_namespace { args.push("--unshare-pid".to_string()); } if options.network_mode.should_unshare_network() { args.push("--unshare-net".to_string()); } if options.mount_proc { args.push("--proc".to_string()); args.push("/proc".to_string()); } args.push("--cap-drop".to_string()); args.push("ALL".to_string()); args.push("--".to_string()); args.extend(command); BwrapArgs { args, preserved_files: Vec::new(), synthetic_mount_targets: Vec::new(), } } /// Build the bubblewrap flags (everything after `argv[0]`). fn create_bwrap_flags( command: Vec, file_system_sandbox_policy: &FileSystemSandboxPolicy, sandbox_policy_cwd: &Path, command_cwd: &Path, options: BwrapOptions, ) -> Result { if options.mask_wslg_distro { let granted_paths = file_system_sandbox_policy.entries.iter().flat_map(|entry| { FileSystemSandboxPolicy::restricted(vec![entry.clone()]) .get_readable_roots_with_cwd(sandbox_policy_cwd) }); let executable = command .first() .filter(|executable| executable.contains('/')) .map(|executable| AbsolutePathBuf::resolve_path_against_base(executable, command_cwd)); for path in granted_paths .map(AbsolutePathBuf::into_path_buf) .chain([sandbox_policy_cwd.to_path_buf(), command_cwd.to_path_buf()]) .chain(executable.map(AbsolutePathBuf::into_path_buf)) { crate::wslg::ensure_supported_path(&path)?; } } let BwrapArgs { args: filesystem_args, preserved_files, synthetic_mount_targets, } = create_filesystem_args(file_system_sandbox_policy, sandbox_policy_cwd, options)?; let normalized_command_cwd = normalize_command_cwd_for_bwrap(command_cwd); let mut args = Vec::new(); args.push("--new-session".to_string()); args.push("--die-with-parent".to_string()); args.extend(filesystem_args); if options.mask_wsl_interop { // WSL's Windows-process bridge uses Unix sockets under /run/WSL. // Mask them after all filesystem binds so network access cannot bypass // the Linux filesystem policy by launching an unrestricted Windows process. args.push("--tmpfs".to_string()); args.push(WSL_INTEROP_DIR.to_string()); } if (options.mask_wsl_interop || options.mask_wslg_distro) && !options.mount_proc { // Without a fresh procfs, the root bind retains the host procfs. // Hide it so host process roots cannot restore the masked WSL views. args.push("--tmpfs".to_string()); args.push("/proc".to_string()); } // Request a user namespace explicitly rather than relying on bubblewrap's // auto-enable behavior, which is skipped when the caller runs as uid 0. // This also blocks host procfs root/cwd/fd links through ptrace permission // checks, including when a container requires retaining the host procfs. args.push("--unshare-user".to_string()); if !options.inherit_pid_namespace { args.push("--unshare-pid".to_string()); } args.push("--unshare-ipc".to_string()); if options.network_mode.should_unshare_network() { args.push("--unshare-net".to_string()); } // Mount a fresh /proc unless the caller explicitly disables it. if options.mount_proc { args.push("--proc".to_string()); args.push("/proc".to_string()); } if normalized_command_cwd.as_path() != command_cwd { // Bubblewrap otherwise inherits the helper's logical cwd, which can be // a symlink alias that disappears once the sandbox only mounts // canonical roots. Enter the canonical command cwd explicitly so // relative paths stay aligned with the mounted filesystem view. args.push("--chdir".to_string()); args.push(path_to_string(normalized_command_cwd.as_path())); } args.push("--cap-drop".to_string()); args.push("ALL".to_string()); args.push("--".to_string()); args.extend(command); Ok(BwrapArgs { args, preserved_files, synthetic_mount_targets, }) } /// Build the bubblewrap filesystem mounts for a given filesystem policy. /// /// The mount order is important: /// 1. Full-read policies, and restricted policies that explicitly read `/`, /// use `--ro-bind / /`; other restricted-read policies start from /// `--tmpfs /` and layer scoped `--ro-bind` mounts. /// 2. `--dev /dev` mounts a minimal writable `/dev` with standard device nodes /// (including `/dev/urandom`) even under a read-only root. /// 3. Unreadable ancestors of writable roots are masked before their child /// mounts are rebound so nested writable carveouts can be reopened safely. /// 4. `--bind ` re-enables writes for allowed roots, including /// writable subpaths under `/dev` (for example, `/dev/shm`). Binding `/` /// recreates the minimal `/dev` before applying any carveouts below it. /// 5. `--ro-bind ` re-applies read-only protections under /// those writable roots so protected subpaths win. /// 6. Nested unreadable carveouts under a writable root are masked after that /// root is bound, and unrelated unreadable roots are masked afterward. fn create_filesystem_args( file_system_sandbox_policy: &FileSystemSandboxPolicy, cwd: &Path, options: BwrapOptions, ) -> Result { let daemon_directory = codex_uds::prepare_shared_daemon_socket_directory()?; let daemon_directories = crate::daemon_mounts::daemon_socket_mask_paths( &daemon_directory, options .mask_wslg_distro .then_some(Path::new(WSLG_DISTRO_ROOT)), )?; let resolved = file_system_sandbox_policy .resolve_for_enforcement(cwd, FileSystemPolicyProjection::LinuxMounts)?; // Bubblewrap requires bind mount targets to exist. Skip missing writable // roots so mixed-platform configs can keep harmless paths for other // environments without breaking Linux command startup. let mut writable_roots = resolved.writable_roots; writable_roots.retain(|writable_root| writable_root.root.as_path().exists()); if writable_roots.is_empty() && resolved.has_full_disk_write_access && !resolved.unreadable_globs.is_empty() { writable_roots.push(WritableRoot { root: AbsolutePathBuf::from_absolute_path("/")?, read_only_subpaths: Vec::new(), protected_metadata_names: Vec::new(), }); } let missing_auto_metadata_read_only_project_root_subpaths: HashSet = file_system_sandbox_policy .entries .iter() .filter(|entry| entry.access == FileSystemAccessMode::Read) .filter_map(|entry| { let subpath = match &entry.path { FileSystemPath::Special { value: FileSystemSpecialPath::ProjectRoots { subpath: Some(subpath), }, } => subpath, _ => return None, }; // Automatic repo-metadata read masks are skipped here so the // metadata handling below can apply the root-scoped // protection consistently for `.git`, `.agents`, and `.codex`. // User-authored `read` rules for other subpaths and `none` // rules should keep their normal bwrap behavior, which can mask // the first missing component to prevent creation under writable // roots. let project_subpath = Path::new(subpath); if project_subpath != Path::new(".git") && project_subpath != Path::new(".agents") && project_subpath != Path::new(".codex") { return None; } let resolved = AbsolutePathBuf::resolve_path_against_base(subpath, cwd); (!resolved.as_path().exists()).then(|| resolved.into_path_buf()) }) .collect(); let mut unreadable_roots = resolved .unreadable_roots .into_iter() .map(AbsolutePathBuf::into_path_buf) .collect::>(); // Bubblewrap can only mask concrete paths. Expand unreadable glob patterns // to the existing matches we can see before constructing the mount overlay; // core tool helpers still evaluate the original patterns directly at read time. unreadable_roots.extend( expand_unreadable_globs( &resolved.unreadable_globs, cwd, options.glob_scan_max_depth.or(resolved.glob_scan_max_depth), )? .into_iter() .map(AbsolutePathBuf::into_path_buf), ); unreadable_roots.sort(); unreadable_roots.dedup(); let mut args = if resolved.has_full_disk_read_access { // Read-only root, then mount a minimal device tree. // In bubblewrap (`bubblewrap.c`, `SETUP_MOUNT_DEV`), `--dev /dev` // creates the standard minimal nodes: null, zero, full, random, // urandom, and tty. `/dev` must be mounted before writable roots so // explicit `/dev/*` writable binds remain visible. vec![ "--ro-bind".to_string(), "/".to_string(), "/".to_string(), "--dev".to_string(), "/dev".to_string(), ] } else { // Start from an empty filesystem and add only the approved readable // roots plus a minimal `/dev`. let mut args = vec![ "--tmpfs".to_string(), "/".to_string(), "--dev".to_string(), "/dev".to_string(), ]; let mut readable_roots: BTreeSet = resolved .readable_roots .into_iter() .map(PathBuf::from) .collect(); if resolved.include_platform_defaults { readable_roots.extend( LINUX_PLATFORM_DEFAULT_READ_ROOTS .iter() .map(|path| PathBuf::from(*path)) .filter(|path| path.exists()), ); if options.inherit_pid_namespace { // Reuse the caller's procfs, including its submount masks. Bind it // with the other read roots so explicit deny rules still win. readable_roots.insert(PathBuf::from("/proc")); } } // A restricted policy can still explicitly request `/`, which is // the broad read baseline. Explicit unreadable carveouts are // re-applied later. if readable_roots.iter().any(|root| root == Path::new("/")) { args = vec![ "--ro-bind".to_string(), "/".to_string(), "/".to_string(), "--dev".to_string(), "/dev".to_string(), ]; } else { for root in readable_roots { if !root.exists() { continue; } // Writable roots are rebound by real target below; mirror that // for their restricted-read bootstrap mount. Plain read-only // roots must stay logical because callers may execute those // paths inside bwrap, such as Bazel runfiles helper binaries. let mount_root = if writable_roots .iter() .any(|writable_root| root.starts_with(writable_root.root.as_path())) { canonical_target_if_symlinked_path(&root).unwrap_or(root) } else { root }; args.push("--ro-bind".to_string()); args.push(path_to_string(&mount_root)); args.push(path_to_string(&mount_root)); append_daemon_socket_masks(&mut args, &mount_root, &daemon_directories)?; } } args }; if args[0] == "--ro-bind" { append_daemon_socket_masks(&mut args, Path::new("/"), &daemon_directories)?; } let mut bwrap_args = BwrapArgs { args, preserved_files: Vec::new(), synthetic_mount_targets: Vec::new(), }; let mut allowed_write_paths = Vec::with_capacity(writable_roots.len()); for writable_root in &writable_roots { let root = writable_root.root.as_path(); allowed_write_paths.push(root.to_path_buf()); if let Some(target) = canonical_target_if_symlinked_path(root) { allowed_write_paths.push(target); } } let unreadable_paths: HashSet = unreadable_roots.iter().cloned().collect(); let mut sorted_writable_roots = writable_roots; sorted_writable_roots.sort_by_key(|writable_root| path_depth(writable_root.root.as_path())); let binds_file_system_root = sorted_writable_roots .iter() .any(|writable_root| writable_root.root.as_path() == Path::new("/")); // Mask only the unreadable ancestors that sit outside every writable root. // Unreadable paths nested under a broader writable root are applied after // that broader root is bound, then reopened by any deeper writable child. let mut unreadable_ancestors_of_writable_roots: Vec = unreadable_roots .iter() .filter(|path| { let unreadable_root = path.as_path(); !allowed_write_paths .iter() .any(|root| unreadable_root.starts_with(root)) && allowed_write_paths .iter() .any(|root| root.starts_with(unreadable_root)) }) .cloned() .collect(); unreadable_ancestors_of_writable_roots.sort_by_key(|path| path_depth(path)); for unreadable_root in &unreadable_ancestors_of_writable_roots { append_unreadable_root_args(&mut bwrap_args, unreadable_root, &allowed_write_paths)?; } for writable_root in &sorted_writable_roots { let root = writable_root.root.as_path(); let symlink_target = canonical_target_if_symlinked_path(root); // If a denied ancestor was already masked, recreate any missing mount // target parents before binding the narrower writable descendant. if let Some(masking_root) = unreadable_roots .iter() .map(PathBuf::as_path) .filter(|unreadable_root| root.starts_with(unreadable_root)) .max_by_key(|unreadable_root| path_depth(unreadable_root)) { append_mount_target_parent_dir_args(&mut bwrap_args.args, root, masking_root); } let mount_root = symlink_target.as_deref().unwrap_or(root); // Rebinding a root alias would also undo masks already applied to `/`. // The physical root is already writable; only the alias's carveouts remain. let redundant_root_alias = binds_file_system_root && root != Path::new("/") && mount_root == Path::new("/"); if !redundant_root_alias { bwrap_args.args.push("--bind".to_string()); bwrap_args.args.push(path_to_string(mount_root)); bwrap_args.args.push(path_to_string(mount_root)); if mount_root == Path::new("/") { // The root bind shadows the earlier device tree and applies nodev. // Recreate only standard devices before applying explicit deny masks. bwrap_args.args.extend([ "--dev".to_string(), "/dev".to_string(), "--bind-try".to_string(), "/dev/shm".to_string(), "/dev/shm".to_string(), ]); } append_daemon_socket_masks(&mut bwrap_args.args, mount_root, &daemon_directories)?; } let mut read_only_subpaths: Vec = writable_root .read_only_subpaths .iter() .map(|path| path.as_path().to_path_buf()) .filter(|path| !unreadable_paths.contains(path)) .filter(|path| !missing_auto_metadata_read_only_project_root_subpaths.contains(path)) .collect(); // Remember which mounts belong to a deeper writable root without dropping // their paths: every path must still be remapped and validated below. let mut deferred_read_only_subpaths: Vec = read_only_subpaths .iter() .filter(|path| { sorted_writable_roots.iter().any(|nested_root| { let nested_path = nested_root.root.as_path(); nested_path != root && nested_path.starts_with(root) && path.starts_with(nested_path) && nested_root .read_only_subpaths .iter() .any(|nested_subpath| nested_subpath.as_path() == path.as_path()) }) }) .cloned() .collect(); let protected_metadata_names = &writable_root.protected_metadata_names; append_metadata_path_masks_for_writable_root( &mut read_only_subpaths, root, protected_metadata_names, ); if let Some(target) = &symlink_target { read_only_subpaths = remap_paths_for_symlink_target(read_only_subpaths, root, target); deferred_read_only_subpaths = remap_paths_for_symlink_target(deferred_read_only_subpaths, root, target); } read_only_subpaths.sort_by_key(|path| path_depth(path)); for subpath in read_only_subpaths { if let Some(symlink) = first_writable_symlink_component_in_path(&subpath, &allowed_write_paths) { /* * A read-only carveout under a writable symlink cannot be made reliable * with bwrap path arguments. Binding the symlink's current target would * only protect a startup-time snapshot; the sandboxed process could * replace the writable symlink before it reads through the logical path. */ return Err(CodexErr::Fatal(format!( "cannot enforce sandbox read-only path {} because it crosses writable symlink {}", subpath.display(), symlink.display() ))); } // Defer only the mount, after the same validation as an immediate mount. if deferred_read_only_subpaths.contains(&subpath) { continue; } append_read_only_subpath_args( &mut bwrap_args, &subpath, &allowed_write_paths, &daemon_directories, )?; } // Protect the registry only where a writable bind exposes it. Apply // this before deny masks so a denied parent stays hidden, rather than // trying to recreate the registry beneath an already read-only tmpfs. let registry_root = synthetic_mount_registry_root(); let registry_mount = if registry_root.starts_with(mount_root) { Some(registry_root.as_path()) } else if mount_root.starts_with(®istry_root) { Some(mount_root) } else { None }; if let Some(registry_mount) = registry_mount { fs::create_dir_all(®istry_root)?; bwrap_args.args.push("--ro-bind".to_string()); bwrap_args.args.push(path_to_string(registry_mount)); bwrap_args.args.push(path_to_string(registry_mount)); append_daemon_socket_masks(&mut bwrap_args.args, registry_mount, &daemon_directories)?; } let mut nested_unreadable_roots: Vec = unreadable_roots .iter() .filter(|path| path.starts_with(root)) .cloned() .collect(); if let Some(target) = &symlink_target { nested_unreadable_roots = remap_paths_for_symlink_target(nested_unreadable_roots, root, target); } nested_unreadable_roots.sort_by_key(|path| path_depth(path)); for unreadable_root in nested_unreadable_roots { append_unreadable_root_args(&mut bwrap_args, &unreadable_root, &allowed_write_paths)?; } } let mut rootless_unreadable_roots: Vec = unreadable_roots .iter() .filter(|path| { let unreadable_root = path.as_path(); !allowed_write_paths .iter() .any(|root| unreadable_root.starts_with(root) || root.starts_with(unreadable_root)) }) .cloned() .collect(); rootless_unreadable_roots.sort_by_key(|path| path_depth(path)); for unreadable_root in rootless_unreadable_roots { append_unreadable_root_args(&mut bwrap_args, &unreadable_root, &allowed_write_paths)?; } if options.mask_wslg_distro && !unreadable_roots.iter().any(|denied| { let distro = Path::new(WSLG_DISTRO_ROOT); distro.starts_with(denied) && !allowed_write_paths.iter().any(|root| { root.starts_with(denied) && (distro.starts_with(root) || root.starts_with(distro)) }) }) { // Use the expanded masks, including symlink targets from deny globs. // An already hidden ancestor cannot accommodate a new child mount. // Otherwise apply this after every grant to exclude the duplicate view. bwrap_args.args.extend([ "--perms".to_string(), "000".to_string(), "--tmpfs".to_string(), WSLG_DISTRO_ROOT.to_string(), "--remount-ro".to_string(), WSLG_DISTRO_ROOT.to_string(), ]); } Ok(bwrap_args) } fn append_metadata_path_masks_for_writable_root( read_only_subpaths: &mut Vec, root: &Path, protected_metadata_names: &[String], ) { for name in protected_metadata_names { let path = root.join(name); if !read_only_subpaths.iter().any(|subpath| subpath == &path) { read_only_subpaths.push(path); } } } // Mask immediately after every bind that exposes the directory, before policy // deny masks can hide its parent. Use the mount destination for read-root aliases. fn append_daemon_socket_masks( args: &mut Vec, mount_root: &Path, daemon_directories: &BTreeSet, ) -> Result<()> { let source = fs::canonicalize(mount_root)?; for daemon_directory in daemon_directories { if source.starts_with(daemon_directory) { return Err(CodexErr::Fatal( "app-server socket directory cannot be a sandbox mount root".to_string(), )); } if let Ok(relative) = daemon_directory.strip_prefix(&source) { let destination = mount_root.join(relative); // A lossy conversion could mask a different path and expose the socket. let destination = destination .to_str() .ok_or_else(|| { CodexErr::Fatal("app-server socket mask path must be valid UTF-8".to_string()) })? .to_string(); args.extend([ "--perms".to_string(), "000".to_string(), "--tmpfs".to_string(), destination.clone(), "--remount-ro".to_string(), destination, ]); } } Ok(()) } /// Snapshot deny-glob matches and their canonical targets for bubblewrap masks. /// Glob overrides are relative to each pattern's static search root. pub fn expand_unreadable_globs( patterns: &[String], cwd: &Path, max_depth: Option, ) -> Result> { if patterns.is_empty() || max_depth == Some(0) { return Ok(Vec::new()); } // Group each pattern by the static path prefix before its first glob // metacharacter. That keeps scans narrow, avoids searching from `/`, and // lets one walk handle all patterns under the same root. let mut patterns_by_search_root: BTreeMap> = BTreeMap::new(); for pattern in patterns { let Some((search_root, glob)) = split_glob_pattern(pattern, cwd) else { return Err(CodexErr::Fatal(format!( "unreadable glob `{pattern}` cannot be safely expanded; use a pattern with a non-root directory prefix" ))); }; // Follow symlink roots, but never discard a deny because inspecting its // search root failed. Only absent or non-directory roots have no matches. match fs::metadata(search_root.as_path()) { Ok(metadata) if metadata.is_dir() => { patterns_by_search_root .entry(search_root) .or_default() .push(glob); } Ok(_) => {} Err(err) if matches!( err.kind(), io::ErrorKind::NotFound | io::ErrorKind::NotADirectory ) => {} Err(err) => { return Err(CodexErr::Fatal(format!( "failed to inspect unreadable glob search root {}: {err}", search_root.display() ))); } } } // Record both the logical match and any canonical symlink target. The bwrap // overlay needs the resolved target to prevent a readable symlink path from // bypassing an unreadable glob match. let mut expanded_paths = BTreeSet::new(); for (search_root, globs) in patterns_by_search_root { let paths = glob_files(search_root.as_path(), &globs, max_depth)?; for path in paths { if let Some(target) = canonical_target_if_symlinked_path(path.as_path()) { expanded_paths.insert(AbsolutePathBuf::from_absolute_path_checked(target)?); } expanded_paths.insert(path); if expanded_paths.len() > MAX_UNREADABLE_GLOB_MATCHES { return Err(CodexErr::Fatal(format!( "unreadable glob expansion for {} matched more than {MAX_UNREADABLE_GLOB_MATCHES} paths", search_root.display() ))); } } } Ok(expanded_paths.into_iter().collect()) } fn split_glob_pattern(pattern: &str, cwd: &Path) -> Option<(AbsolutePathBuf, String)> { // Resolve relative patterns once, then split at the first glob // metacharacter. The prefix becomes the search root and the suffix stays as // the file glob. Root-level glob scans are intentionally skipped because // they are too broad for startup-time sandbox construction. let absolute_pattern = AbsolutePathBuf::resolve_path_against_base(pattern, cwd); let pattern = absolute_pattern.to_string_lossy(); let first_glob_index = pattern .char_indices() .find_map(|(index, ch)| matches!(ch, '*' | '?' | '[' | ']').then_some(index))?; let static_prefix = &pattern[..first_glob_index]; if static_prefix.is_empty() || static_prefix == "/" { return None; } let search_root_end = if static_prefix.ends_with('/') { static_prefix.len() - 1 } else { static_prefix.rfind('/').unwrap_or(0) }; let search_root = if search_root_end == 0 { PathBuf::from("/") } else { PathBuf::from(&pattern[..search_root_end]) }; let search_root = AbsolutePathBuf::from_absolute_path_checked(search_root).ok()?; let glob = pattern[search_root_end + 1..].to_string(); (!glob.is_empty()).then_some((search_root, glob)) } fn glob_files( search_root: &Path, globs: &[String], max_depth: Option, ) -> Result> { let mut builder = OverrideBuilder::new(search_root); builder.allow_unclosed_class(/*yes*/ true); for glob in globs { // Anchor to the scan root; this also keeps leading `#` and `!` literal. let mut glob = format!("/{glob}"); if glob.ends_with(' ') && !glob.ends_with("\\ ") { glob.insert(glob.len() - 1, '\\'); } builder.add(&glob).map_err(|err| { CodexErr::Fatal(format!( "unreadable glob pattern is invalid for {}: {err}", search_root.display() )) })?; } let overrides = builder.build().map_err(|err| { CodexErr::Fatal(format!( "unreadable glob matcher failed for {}: {err}", search_root.display() )) })?; let paths = Mutex::new(Ok(Vec::new())); let threads = std::thread::available_parallelism() .map_or(1, usize::from) .min(12); // Include hidden and ignored files, and follow explicit symlink roots. // Skip child symlinks and only return regular files. let mut walker = ignore::WalkBuilder::new(search_root); walker .standard_filters(/*yes*/ false) .follow_links(/*yes*/ false) .max_depth(max_depth) .overrides(overrides) .threads(threads); let visit = |entry: std::result::Result| { let path = match entry { Ok(entry) => { if !entry.file_type().is_some_and(|kind| kind.is_file()) { return WalkState::Continue; } AbsolutePathBuf::from_absolute_path_checked(entry.into_path()) .map_err(CodexErr::from) } Err(err) => Err(CodexErr::Fatal(format!( "failed to enumerate unreadable glob paths under {}: {err}", search_root.display() ))), }; let Ok(mut paths) = paths.lock() else { return WalkState::Quit; }; match path { Ok(path) => { if let Ok(paths) = &mut *paths { paths.push(path); } } Err(err) => { *paths = Err(err); return WalkState::Quit; } } WalkState::Continue }; // Like rg, use the serial walk on one CPU: depth-boundary errors differ. if threads == 1 { for entry in walker.build() { if visit(entry) == WalkState::Quit { break; } } } else { walker.build_parallel().run(|| Box::new(&visit)); } paths .into_inner() .map_err(|_| CodexErr::Fatal("unreadable glob scan result mutex poisoned".to_string()))? } fn path_to_string(path: &Path) -> String { path.to_string_lossy().to_string() } fn path_depth(path: &Path) -> usize { path.components().count() } fn canonical_target_if_symlinked_path(path: &Path) -> Option { // Return the fully resolved target only when some path component is a // symlink. Callers use this to bind/mask the real filesystem location while // leaving ordinary paths in their logical form. let mut current = PathBuf::new(); for component in path.components() { use std::path::Component; match component { Component::RootDir => { current.push(Path::new("/")); continue; } Component::CurDir => continue, Component::ParentDir => { current.pop(); continue; } Component::Normal(part) => current.push(part), Component::Prefix(_) => continue, } let metadata = match fs::symlink_metadata(¤t) { Ok(metadata) => metadata, Err(_) => return None, }; if metadata.file_type().is_symlink() { let target = fs::canonicalize(path).ok()?; if target.as_path() == path { return None; } return Some(target); } } None } fn remap_paths_for_symlink_target(paths: Vec, root: &Path, target: &Path) -> Vec { paths .into_iter() .map(|path| { if let Ok(relative) = path.strip_prefix(root) { target.join(relative) } else { path } }) .collect() } fn normalize_command_cwd_for_bwrap(command_cwd: &Path) -> PathBuf { command_cwd .canonicalize() .unwrap_or_else(|_| command_cwd.to_path_buf()) } fn append_mount_target_parent_dir_args(args: &mut Vec, mount_target: &Path, anchor: &Path) { let mount_target_dir = if mount_target.is_dir() { mount_target } else if let Some(parent) = mount_target.parent() { parent } else { return; }; let mut mount_target_dirs: Vec = mount_target_dir .ancestors() .take_while(|path| *path != anchor) .map(Path::to_path_buf) .collect(); mount_target_dirs.reverse(); for mount_target_dir in mount_target_dirs { args.push("--dir".to_string()); args.push(path_to_string(&mount_target_dir)); } } /// Append mounts for a read-only path after writable-symlink validation. fn append_read_only_subpath_args( bwrap_args: &mut BwrapArgs, subpath: &Path, allowed_write_paths: &[PathBuf], daemon_directories: &BTreeSet, ) -> Result<()> { if let Some(metadata) = transient_empty_metadata_path(subpath) && is_within_allowed_write_paths(subpath, allowed_write_paths) { // Another concurrent bwrap setup can leave an empty mount target at // a missing metadata path. Treat it like the missing case instead of // binding that transient host path as the stable source. match metadata { EmptyProtectedMetadataPath::File(metadata) => { append_existing_empty_file_bind_data_args(bwrap_args, subpath, &metadata)?; } EmptyProtectedMetadataPath::Directory(metadata) => { append_existing_empty_directory_args(bwrap_args, subpath, &metadata); } } return Ok(()); } if !subpath.exists() { if let Some(first_missing_component) = find_first_non_existent_component(subpath) && is_within_allowed_write_paths(&first_missing_component, allowed_write_paths) { append_missing_read_only_subpath_args(bwrap_args, &first_missing_component)?; } return Ok(()); } if is_within_allowed_write_paths(subpath, allowed_write_paths) { bwrap_args.args.push("--ro-bind".to_string()); bwrap_args.args.push(path_to_string(subpath)); bwrap_args.args.push(path_to_string(subpath)); append_daemon_socket_masks(&mut bwrap_args.args, subpath, daemon_directories)?; } Ok(()) } fn append_empty_file_bind_data_args(bwrap_args: &mut BwrapArgs, path: &Path) -> Result<()> { // Bubblewrap consumes and closes the descriptor for each bind-data mount. let null_file = File::open("/dev/null")?; let null_fd = null_file.as_raw_fd().to_string(); bwrap_args.preserved_files.push(null_file); bwrap_args.args.push("--ro-bind-data".to_string()); bwrap_args.args.push(null_fd); bwrap_args.args.push(path_to_string(path)); Ok(()) } fn append_empty_directory_args(bwrap_args: &mut BwrapArgs, path: &Path) { bwrap_args.args.push("--perms".to_string()); bwrap_args.args.push("555".to_string()); bwrap_args.args.push("--tmpfs".to_string()); bwrap_args.args.push(path_to_string(path)); bwrap_args.args.push("--remount-ro".to_string()); bwrap_args.args.push(path_to_string(path)); } fn append_missing_read_only_subpath_args(bwrap_args: &mut BwrapArgs, path: &Path) -> Result<()> { if path.file_name().is_some_and(is_protected_metadata_name) { append_empty_directory_args(bwrap_args, path); bwrap_args .synthetic_mount_targets .push(SyntheticMountTarget::missing_empty_directory(path)); return Ok(()); } append_missing_empty_file_bind_data_args(bwrap_args, path) } fn append_missing_empty_file_bind_data_args(bwrap_args: &mut BwrapArgs, path: &Path) -> Result<()> { append_empty_file_bind_data_args(bwrap_args, path)?; bwrap_args .synthetic_mount_targets .push(SyntheticMountTarget::missing(path)); Ok(()) } fn append_existing_empty_file_bind_data_args( bwrap_args: &mut BwrapArgs, path: &Path, metadata: &Metadata, ) -> Result<()> { append_empty_file_bind_data_args(bwrap_args, path)?; bwrap_args .synthetic_mount_targets .push(SyntheticMountTarget::existing_empty_file(path, metadata)); Ok(()) } fn append_existing_empty_directory_args( bwrap_args: &mut BwrapArgs, path: &Path, metadata: &Metadata, ) { append_empty_directory_args(bwrap_args, path); bwrap_args .synthetic_mount_targets .push(SyntheticMountTarget::existing_empty_directory( path, metadata, )); } fn append_unreadable_root_args( bwrap_args: &mut BwrapArgs, unreadable_root: &Path, allowed_write_paths: &[PathBuf], ) -> Result<()> { if let Some(symlink) = first_writable_symlink_component_in_path(unreadable_root, allowed_write_paths) { /* * Deny-read masks must fail closed when the protected path crosses a * symlink that remains writable to the sandboxed process. Resolving and * masking the symlink's current target is a TOCTTOU snapshot: bwrap would * protect the old target while the logical path could later point * somewhere else. */ return Err(CodexErr::Fatal(format!( "cannot enforce sandbox deny-read path {} because it crosses writable symlink {}", unreadable_root.display(), symlink.display() ))); } if !unreadable_root.exists() { if let Some(first_missing_component) = find_first_non_existent_component(unreadable_root) && is_within_allowed_write_paths(&first_missing_component, allowed_write_paths) { append_missing_empty_file_bind_data_args(bwrap_args, &first_missing_component)?; } return Ok(()); } append_existing_unreadable_path_args(bwrap_args, unreadable_root, allowed_write_paths) } fn append_existing_unreadable_path_args( bwrap_args: &mut BwrapArgs, unreadable_root: &Path, allowed_write_paths: &[PathBuf], ) -> Result<()> { if unreadable_root.is_dir() { let mut writable_descendants: Vec<&Path> = allowed_write_paths .iter() .map(PathBuf::as_path) .filter(|path| *path != unreadable_root && path.starts_with(unreadable_root)) .collect(); bwrap_args.args.push("--perms".to_string()); // Execute-only perms let the process traverse into explicitly // re-opened writable descendants while still hiding the denied // directory contents. Plain denied directories with no writable child // mounts stay at `000`. bwrap_args.args.push(if writable_descendants.is_empty() { "000".to_string() } else { "111".to_string() }); bwrap_args.args.push("--tmpfs".to_string()); bwrap_args.args.push(path_to_string(unreadable_root)); // Recreate any writable descendants inside the tmpfs before remounting // the denied parent read-only. Otherwise bubblewrap cannot mkdir the // nested mount targets after the parent has been frozen. writable_descendants.sort_by_key(|path| path_depth(path)); for writable_descendant in writable_descendants { append_mount_target_parent_dir_args( &mut bwrap_args.args, writable_descendant, unreadable_root, ); } bwrap_args.args.push("--remount-ro".to_string()); bwrap_args.args.push(path_to_string(unreadable_root)); return Ok(()); } bwrap_args.args.push("--perms".to_string()); bwrap_args.args.push("000".to_string()); append_empty_file_bind_data_args(bwrap_args, unreadable_root) } /// Returns true when `path` is under any allowed writable root. fn is_within_allowed_write_paths(path: &Path, allowed_write_paths: &[PathBuf]) -> bool { allowed_write_paths .iter() .any(|root| path.starts_with(root)) } enum EmptyProtectedMetadataPath { File(Metadata), Directory(Metadata), } fn transient_empty_metadata_path(path: &Path) -> Option { if !path.file_name().is_some_and(is_protected_metadata_name) { return None; } let metadata = fs::symlink_metadata(path).ok()?; if metadata.file_type().is_file() && metadata.len() == 0 { return Some(EmptyProtectedMetadataPath::File(metadata)); } if metadata.file_type().is_dir() && directory_is_empty(path) { return Some(EmptyProtectedMetadataPath::Directory(metadata)); } None } fn directory_is_empty(path: &Path) -> bool { let Ok(mut entries) = fs::read_dir(path) else { return false; }; entries.next().is_none() } fn first_writable_symlink_component_in_path( target_path: &Path, allowed_write_paths: &[PathBuf], ) -> Option { /* * Walk the logical path and report the first symlink component that lives * under a writable root. These symlinks are mutable from inside the sandbox, * so any mount or mask based on their resolved target would be racing a path * the sandboxed process can change. */ let mut current = PathBuf::new(); for component in target_path.components() { use std::path::Component; match component { Component::RootDir => { current.push(Path::new("/")); continue; } Component::CurDir => continue, Component::ParentDir => { current.pop(); continue; } Component::Normal(part) => current.push(part), Component::Prefix(_) => continue, } let metadata = match std::fs::symlink_metadata(¤t) { Ok(metadata) => metadata, Err(_) => break, }; if metadata.file_type().is_symlink() && is_within_allowed_write_paths(¤t, allowed_write_paths) { return Some(current); } } None } /// Find the first missing path component while walking `target_path`. /// /// Mounting `/dev/null` on the first missing component prevents the sandboxed /// process from creating the protected path hierarchy. fn find_first_non_existent_component(target_path: &Path) -> Option { let mut current = PathBuf::new(); for component in target_path.components() { use std::path::Component; match component { Component::RootDir => { current.push(Path::new("/")); continue; } Component::CurDir => continue, Component::ParentDir => { current.pop(); continue; } Component::Normal(part) => current.push(part), Component::Prefix(_) => continue, } if !current.exists() { return Some(current); } } None } #[cfg(test)] #[path = "bwrap_wslg_tests.rs"] mod wslg_tests; #[cfg(test)] #[path = "bwrap_tests.rs"] mod tests;