Fix daemon socket isolation checks for private tmp mounts (#46125)

## Why

Private `/tmp` bind mounts can leave hidden entries in `/proc/self/mountinfo`, causing the Linux sandbox to reject layouts that safely isolate daemon sockets. The proc-mount preflight also needs the main sandbox's WSL masks when checking aliases.

## What changed

- Identify the opened socket directory's mount using `fdinfo`, with a `statx` fallback, and follow its mount ancestry to distinguish hidden paths from exposed aliases.
- Accept safe private and stacked mounts while rejecting covered mounts, exposed aliases, and nested mounts. Retain conservative checks when no mount ID is available.
- Preserve WSL interop and WSLg masks in the proc-mount preflight.
- Heap-allocate large lifecycle futures in the TUI approval-gated MCP tool test to reduce Windows test-thread stack usage.

## Testing

Add mount-layout regression cases and a namespace integration test that verifies daemon sockets remain inaccessible under private `/tmp`, unrelated sockets remain reachable, and an exposed alias prevents startup. Add a preflight test for preserving WSL masks.

GitOrigin-RevId: 289b4446ba7776a7dd57ef5ff09e87bda19b069c
This commit is contained in:
open-matt
2026-09-17 05:23:55 +00:00
committed by copyberry
parent c5d079470e
commit 1bd1bfa7ca
6 changed files with 386 additions and 73 deletions
+108 -19
View File
@@ -3,8 +3,15 @@ use pretty_assertions::assert_eq;
use test_case::test_case;
// Most cases have no independently masked subtree.
fn check_mounts(directory: &Path, device: &str, mountinfo: &[u8]) -> io::Result<()> {
super::check_mounts(directory, device, mountinfo, /*masked_root*/ None)
fn check_mounts(
directory: &Path,
device: &str,
mount_id: Option<&str>,
mountinfo: &[u8],
) -> io::Result<()> {
super::check_mounts(
directory, device, mount_id, mountinfo, /*masked_root*/ None,
)
}
#[test_case("/tmp", "/host-tmp", false; "ancestor alias")]
@@ -18,37 +25,117 @@ fn check_mounts(directory: &Path, device: &str, mountinfo: &[u8]) -> io::Result<
fn rejects_only_mounts_that_compromise_the_directory(root: &str, destination: &str, allowed: bool) {
let mounts =
format!("1 0 0:1 / / rw - ext4 disk rw\n2 1 0:1 {root} {destination} rw - ext4 disk rw\n");
assert_eq!(
check_mounts(
Path::new("/tmp/codex-daemon-1000"),
"0:1",
mounts.as_bytes()
)
.is_ok(),
allowed
);
let visible_mount = if destination == "/tmp" { "2" } else { "1" };
for mount_id in [Some(visible_mount), None] {
assert_eq!(
check_mounts(
Path::new("/tmp/codex-daemon-1000"),
"0:1",
mount_id,
mounts.as_bytes()
)
.is_ok(),
allowed,
"mount_id: {mount_id:?}"
);
}
}
#[test]
fn rejects_alias_when_tmp_is_itself_a_bind_mount() {
let mounts = b"1 0 0:1 / / rw - ext4 disk rw\n2 1 0:1 /backing/tmp /tmp rw - ext4 disk rw\n";
assert!(check_mounts(Path::new("/tmp/codex-daemon-1000"), "0:1", mounts).is_err());
assert!(
check_mounts(
Path::new("/tmp/codex-daemon-1000"),
"0:1",
Some("2"),
mounts
)
.is_err()
);
// A hidden deeper mount must not override the actual /tmp backing location.
let hidden = [
mounts.as_slice(),
b"3 1 0:1 /tmp/codex-daemon-1000 /tmp/codex-daemon-1000 rw - ext4 disk rw\n",
]
.concat();
assert!(check_mounts(Path::new("/tmp/codex-daemon-1000"), "0:1", &hidden).is_err());
assert!(
check_mounts(
Path::new("/tmp/codex-daemon-1000"),
"0:1",
Some("2"),
&hidden
)
.is_err()
);
}
#[test]
fn accepts_private_tmp_filesystem_but_rejects_ambiguous_stacked_mounts() {
fn accepts_private_tmp_filesystem_and_resolves_stacked_mounts() {
let mounts = "1 0 0:1 / / rw - ext4 disk rw\n2 1 0:2 / /tmp rw - tmpfs tmpfs rw\n";
let directory = Path::new("/tmp/codex-daemon-1000");
assert!(check_mounts(directory, "0:2", mounts.as_bytes()).is_ok());
assert!(check_mounts(directory, "0:2", Some("2"), mounts.as_bytes()).is_ok());
assert!(check_mounts(directory, "0:2", /*mount_id*/ None, mounts.as_bytes()).is_ok());
// Missing or inconsistent precise IDs must not fall back to the otherwise
// acceptable conservative interpretation.
assert!(check_mounts(directory, "0:2", Some("missing"), mounts.as_bytes()).is_err());
assert!(check_mounts(directory, "0:2", Some("1"), mounts.as_bytes()).is_err());
assert!(check_mounts(directory, "0:3", /*mount_id*/ None, mounts.as_bytes()).is_err());
let stacked = format!("{mounts}3 2 0:2 /other /tmp rw - tmpfs tmpfs rw\n");
assert!(check_mounts(directory, "0:2", stacked.as_bytes()).is_err());
assert!(check_mounts(directory, "0:2", Some("3"), stacked.as_bytes()).is_ok());
assert!(check_mounts(directory, "0:2", /*mount_id*/ None, stacked.as_bytes()).is_err());
}
#[test]
fn rejects_open_mount_that_has_been_covered() {
let directory = Path::new("/tmp/codex-daemon-1000");
let mounts = "1 0 0:1 / / rw - ext4 disk rw\n\
2 1 0:1 /tmp/private-old/tmp /tmp rw - ext4 disk rw\n\
3 2 0:1 /tmp/private-new/tmp /tmp rw - ext4 disk rw\n";
assert!(check_mounts(directory, "0:1", Some("2"), mounts.as_bytes()).is_err());
assert!(check_mounts(directory, "0:1", Some("3"), mounts.as_bytes()).is_ok());
let exposed = format!(
"{mounts}4 1 0:1 /tmp/private-new/tmp/codex-daemon-1000 /outside rw - ext4 disk rw\n"
);
for mount_id in [Some("2"), Some("3"), None] {
assert!(check_mounts(directory, "0:1", mount_id, exposed.as_bytes()).is_err());
}
}
#[test]
fn rejects_mount_hidden_by_an_ancestor_overmount() {
let directory = Path::new("/tmp/private/codex-daemon-1000");
let mounts = "1 0 0:1 / / rw - ext4 disk rw\n\
2 1 0:2 / /tmp rw - tmpfs tmpfs rw\n\
3 2 0:3 / /tmp/private rw - tmpfs tmpfs rw\n";
assert!(check_mounts(directory, "0:3", Some("3"), mounts.as_bytes()).is_ok());
let covered = format!("{mounts}4 2 0:2 /other /tmp rw - tmpfs tmpfs rw\n");
assert!(check_mounts(directory, "0:3", Some("3"), covered.as_bytes()).is_err());
assert!(check_mounts(directory, "0:2", Some("4"), covered.as_bytes()).is_ok());
}
#[test_case("/tmp/systemd-private-service/tmp", "/"; "tmp on root filesystem")]
#[test_case("/systemd-private-service/tmp", "/tmp"; "tmp on separate filesystem")]
fn accepts_private_tmp_bind_but_rejects_exposed_aliases(root: &str, parent: &str) {
let directory = Path::new("/tmp/codex-daemon-1000");
let mounts =
format!("1 0 0:1 / {parent} rw - ext4 disk rw\n2 1 0:1 {root} /tmp rw - ext4 disk rw\n");
assert!(check_mounts(directory, "0:1", Some("2"), mounts.as_bytes()).is_ok());
// PrivateTmp remains ambiguous when neither fdinfo nor statx supplies an ID.
assert!(check_mounts(directory, "0:1", /*mount_id*/ None, mounts.as_bytes()).is_err());
// A real alias beneath /tmp is not hidden by the private /tmp mount.
for (alias_root, destination) in [
(root.to_owned(), "/tmp/exposed"),
(
format!("{root}/codex-daemon-1000/rpc.sock"),
"/tmp/alias.sock",
),
("/".to_owned(), "/host"),
] {
let exposed = format!("{mounts}3 2 0:1 {alias_root} {destination} rw - ext4 disk rw\n");
assert!(check_mounts(directory, "0:1", Some("2"), exposed.as_bytes()).is_err());
}
}
#[test]
@@ -56,8 +143,10 @@ fn masked_wslg_alias_does_not_allow_other_exposed_aliases() {
let mounts = "1 0 0:1 / / rw - ext4 disk rw\n2 1 0:1 / /mnt/wslg/distro rw - ext4 disk rw\n";
let directory = Path::new("/tmp/codex-daemon-1000");
let mask = Some(Path::new(crate::bwrap::WSLG_DISTRO_ROOT));
assert!(check_mounts(directory, "0:1", mounts.as_bytes()).is_err());
assert!(super::check_mounts(directory, "0:1", mounts.as_bytes(), mask).is_ok());
let exposed = format!("{mounts}3 1 0:1 /tmp /host-tmp rw - ext4 disk rw\n");
assert!(super::check_mounts(directory, "0:1", exposed.as_bytes(), mask).is_err());
for mount_id in [Some("1"), None] {
assert!(check_mounts(directory, "0:1", mount_id, mounts.as_bytes()).is_err());
assert!(super::check_mounts(directory, "0:1", mount_id, mounts.as_bytes(), mask).is_ok());
assert!(super::check_mounts(directory, "0:1", mount_id, exposed.as_bytes(), mask).is_err());
}
}