From 3f62397fbd78b4f4a1397b70e0f9cbbf1d26ce0f Mon Sep 17 00:00:00 2001 From: Diego Chagas Date: Thu, 5 Nov 2026 16:24:42 -0300 Subject: [PATCH 1/6] memory: a sandbox without a cgroup mount no longer reads as 0 bytes free In the Flatpak sandbox /sys/fs/cgroup is absent, so the detector found a membership path it could not read and failed closed: available RAM 0, and every PSD refused with a 0 byte hard limit. With no cgroup mount in mountinfo and no /sys/fs/cgroup, no limit is observable; use host memory. --- crates/raster/src/memory/system.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/crates/raster/src/memory/system.rs b/crates/raster/src/memory/system.rs index ac2e4b772b..4779879030 100644 --- a/crates/raster/src/memory/system.rs +++ b/crates/raster/src/memory/system.rs @@ -206,6 +206,13 @@ pub(super) mod linux { MembershipParse::Malformed => return CgroupLimits::Unreadable, }; let mountinfo = std::fs::read_to_string(proc_root.join("self/mountinfo")).ok(); + // A sandbox with no cgroup filesystem mounted and no `/sys/fs/cgroup` at all (Flatpak) + // shows the process its membership path but no interface files, so no limit is + // observable. Failing closed there reported 0 bytes free and refused every PSD on a + // machine with tens of GB. A present directory still takes the guarded path below. + if !sys_root.exists() && mountinfo.as_deref().is_some_and(|info| !has_cgroup_mount(info)) { + return CgroupLimits::NotConstrained; + } if let Some(path) = membership.v1_memory { let Some(dir) = resolve_dir(sys_root, mountinfo.as_deref(), path, "cgroup", Some("memory")) else { @@ -336,6 +343,11 @@ pub(super) mod linux { best.map(|(_, _, dir)| dir) } + /// Whether any cgroup (v1 or v2) filesystem is mounted in this mount namespace. + fn has_cgroup_mount(info: &str) -> bool { + parse_mountinfo(info).iter().any(|m| m.fstype == "cgroup" || m.fstype == "cgroup2") + } + /// Trims trailing slashes so `Path` equality holds while walking ancestors, keeping `/`. fn normalize_root(point: &str) -> PathBuf { let trimmed = point.trim_end_matches('/'); @@ -933,6 +945,14 @@ mod tests { assert_eq!(linux::cgroup_limits(&proc, &sys), linux::CgroupLimits::Unreadable, "without metadata the absence of a constraint cannot be established"); std::fs::remove_dir_all(proc.parent().unwrap()).unwrap(); + // No cgroup filesystem mounted (Flatpak): the limit is invisible, host memory applies. + let (proc, _) = fixture("no-cgroup-mount"); + let sys = proc.parent().unwrap().join("absent-sys"); + write(&proc.join("self/cgroup"), "0::/app.slice/app-flatpak.scope\n"); + write(&proc.join("self/mountinfo"), "25 20 0:23 / /proc rw - proc proc rw\n"); + assert_eq!(linux::cgroup_limits(&proc, &sys), linux::CgroupLimits::NotConstrained); + std::fs::remove_dir_all(proc.parent().unwrap()).unwrap(); + // Available but not delegated anywhere on the path: no cgroup limit applies. let (proc, sys) = fixture("v2-not-enabled"); write(&proc.join("self/cgroup"), "0::/app\n"); From 5734b4405246c329bc9ad3776b1421ac8d08f9ca Mon Sep 17 00:00:00 2001 From: Diego Chagas Date: Thu, 5 Nov 2026 16:34:59 -0300 Subject: [PATCH 2/6] memory: only a confirmed-missing /sys/fs/cgroup takes the sandbox fallback Review feedback: a failed stat for another reason (permissions) stays fail-closed, and a test covers a cgroup2 mount at an alternate path. --- crates/raster/src/memory/system.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/raster/src/memory/system.rs b/crates/raster/src/memory/system.rs index 4779879030..a82b4f0f13 100644 --- a/crates/raster/src/memory/system.rs +++ b/crates/raster/src/memory/system.rs @@ -210,7 +210,10 @@ pub(super) mod linux { // shows the process its membership path but no interface files, so no limit is // observable. Failing closed there reported 0 bytes free and refused every PSD on a // machine with tens of GB. A present directory still takes the guarded path below. - if !sys_root.exists() && mountinfo.as_deref().is_some_and(|info| !has_cgroup_mount(info)) { + // Only a confirmed NotFound counts as absent: a stat that fails for another reason + // (permissions) leaves the directory possibly present, so that case stays fail-closed. + let sys_root_absent = matches!(std::fs::metadata(sys_root), Err(e) if e.kind() == std::io::ErrorKind::NotFound); + if sys_root_absent && mountinfo.as_deref().is_some_and(|info| !has_cgroup_mount(info)) { return CgroupLimits::NotConstrained; } @@ -953,6 +956,17 @@ mod tests { assert_eq!(linux::cgroup_limits(&proc, &sys), linux::CgroupLimits::NotConstrained); std::fs::remove_dir_all(proc.parent().unwrap()).unwrap(); + // A cgroup2 mount at an alternate path must be honoured while /sys/fs/cgroup is absent. + let (proc, _) = fixture("alternate-cgroup-mount"); + let sys = proc.parent().unwrap().join("absent-sys"); + let alt = proc.parent().unwrap().join("alt-cgroup"); + write(&proc.join("self/cgroup"), "0::/app\n"); + write(&proc.join("self/mountinfo"), &format!("30 20 0:26 / {} rw - cgroup2 cgroup2 rw\n", alt.display())); + write(&alt.join("app/memory.max"), "1073741824\n"); + write(&alt.join("app/memory.current"), "0\n"); + assert_eq!(linux::cgroup_limits(&proc, &sys), linux::CgroupLimits::Limited { ram: Some(1 << 30), swap: None }); + std::fs::remove_dir_all(proc.parent().unwrap()).unwrap(); + // Available but not delegated anywhere on the path: no cgroup limit applies. let (proc, sys) = fixture("v2-not-enabled"); write(&proc.join("self/cgroup"), "0::/app\n"); From 2504120c338804fd49a466a5a2d12c8f407b848c Mon Sep 17 00:00:00 2001 From: Diego Chagas Date: Fri, 6 Nov 2026 06:36:35 -0300 Subject: [PATCH 3/6] memory: an absent /sys/fs/cgroup counts only when mountinfo lists real mounts An empty or garbled mountinfo parses to no mounts, which proved nothing but let the sandbox shortcut report NotConstrained. Now at least one mount must parse, none a cgroup filesystem. Tests: missing/empty/garbage mountinfo, a non-NotFound stat error, and v1/v2 alternate mounts with unreadable limits all stay Unreadable. Co-Authored-By: Claude Sonnet 5.5 --- crates/raster/src/memory/system.rs | 54 +++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/crates/raster/src/memory/system.rs b/crates/raster/src/memory/system.rs index a82b4f0f13..22289d77e1 100644 --- a/crates/raster/src/memory/system.rs +++ b/crates/raster/src/memory/system.rs @@ -213,7 +213,7 @@ pub(super) mod linux { // Only a confirmed NotFound counts as absent: a stat that fails for another reason // (permissions) leaves the directory possibly present, so that case stays fail-closed. let sys_root_absent = matches!(std::fs::metadata(sys_root), Err(e) if e.kind() == std::io::ErrorKind::NotFound); - if sys_root_absent && mountinfo.as_deref().is_some_and(|info| !has_cgroup_mount(info)) { + if sys_root_absent && mountinfo.as_deref().is_some_and(lists_mounts_without_cgroup) { return CgroupLimits::NotConstrained; } @@ -351,6 +351,13 @@ pub(super) mod linux { parse_mountinfo(info).iter().any(|m| m.fstype == "cgroup" || m.fstype == "cgroup2") } + /// `mountinfo` parsed to at least one mount, none of them a cgroup filesystem. An empty or + /// garbled file parses to no mounts at all, which proves nothing about cgroups; a live kernel + /// always lists the process's own root, so only a real list can show a cgroup mount is absent. + fn lists_mounts_without_cgroup(info: &str) -> bool { + !parse_mountinfo(info).is_empty() && !has_cgroup_mount(info) + } + /// Trims trailing slashes so `Path` equality holds while walking ancestors, keeping `/`. fn normalize_root(point: &str) -> PathBuf { let trimmed = point.trim_end_matches('/'); @@ -1187,6 +1194,51 @@ mod tests { std::fs::remove_dir_all(proc.parent().unwrap()).unwrap(); } + /// A missing `/sys/fs/cgroup` only means "no limit visible" when `mountinfo` was read, parsed + /// to real mounts and lists no cgroup filesystem; every other combination stays fail-closed. + #[cfg(target_os = "linux")] + #[test] + fn linux_absent_cgroup_root_needs_proof_before_it_means_unconstrained() { + let cases: [(&str, Option<&str>); 3] = + [("mountinfo-missing", None), ("mountinfo-empty", Some("")), ("mountinfo-garbage", Some("not a mountinfo line\n\n- - -\n"))]; + for (name, mountinfo) in cases { + let (proc, _) = fixture(name); + let sys = proc.parent().unwrap().join("absent-sys"); + write(&proc.join("self/cgroup"), "0::/app\n"); + if let Some(text) = mountinfo { + write(&proc.join("self/mountinfo"), text); + } + assert_eq!(linux::cgroup_limits(&proc, &sys), linux::CgroupLimits::Unreadable, "{name}"); + std::fs::remove_dir_all(proc.parent().unwrap()).unwrap(); + } + + // A stat error that is not NotFound (a path component that is a file) may hide a real + // directory, so it is not "absent" either. + let (proc, _) = fixture("sys-root-not-a-directory"); + write(&proc.join("self/cgroup"), "0::/app\n"); + write(&proc.join("self/mountinfo"), "25 20 0:23 / /proc rw - proc proc rw\n"); + let sys = proc.join("self/cgroup/fs/cgroup"); + assert_eq!(linux::cgroup_limits(&proc, &sys), linux::CgroupLimits::Unreadable); + std::fs::remove_dir_all(proc.parent().unwrap()).unwrap(); + } + + /// A cgroup mount listed at an alternate path means a limit may exist: v1 and v2 alike, and + /// when its files cannot be read the answer is fail-closed, not "unconstrained". + #[cfg(target_os = "linux")] + #[test] + fn linux_alternate_cgroup_mounts_with_unreadable_limits_fail_closed() { + for (name, membership, fstype, options) in [("alt-v1", "4:memory:/app\n", "cgroup", "rw,memory"), ("alt-v2", "0::/app\n", "cgroup2", "rw")] { + let (proc, _) = fixture(name); + let sys = proc.parent().unwrap().join("absent-sys"); + let alt = proc.parent().unwrap().join("alt-cgroup"); + write(&proc.join("self/cgroup"), membership); + write(&proc.join("self/mountinfo"), &format!("30 20 0:26 / {} rw - {fstype} {fstype} {options}\n", alt.display())); + std::fs::create_dir_all(alt.join("app")).unwrap(); + assert_eq!(linux::cgroup_limits(&proc, &sys), linux::CgroupLimits::Unreadable, "{name}"); + std::fs::remove_dir_all(proc.parent().unwrap()).unwrap(); + } + } + #[cfg(target_os = "linux")] #[test] fn linux_snapshot_combines_host_meminfo_and_cgroup_room() { From ce73b335c9db20e07599aa28456ab1696bab4c92 Mon Sep 17 00:00:00 2001 From: Diego Chagas Date: Sat, 7 Nov 2026 07:17:08 -0300 Subject: [PATCH 4/6] memory: parse mountinfo once in the absent-cgroup-root check Co-Authored-By: Claude Sonnet 5.5 --- crates/raster/src/memory/system.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/raster/src/memory/system.rs b/crates/raster/src/memory/system.rs index 22289d77e1..7829d4cef0 100644 --- a/crates/raster/src/memory/system.rs +++ b/crates/raster/src/memory/system.rs @@ -348,14 +348,19 @@ pub(super) mod linux { /// Whether any cgroup (v1 or v2) filesystem is mounted in this mount namespace. fn has_cgroup_mount(info: &str) -> bool { - parse_mountinfo(info).iter().any(|m| m.fstype == "cgroup" || m.fstype == "cgroup2") + parse_mountinfo(info).iter().any(is_cgroup_mount) + } + + fn is_cgroup_mount(mount: &Mount) -> bool { + mount.fstype == "cgroup" || mount.fstype == "cgroup2" } /// `mountinfo` parsed to at least one mount, none of them a cgroup filesystem. An empty or /// garbled file parses to no mounts at all, which proves nothing about cgroups; a live kernel /// always lists the process's own root, so only a real list can show a cgroup mount is absent. fn lists_mounts_without_cgroup(info: &str) -> bool { - !parse_mountinfo(info).is_empty() && !has_cgroup_mount(info) + let mounts = parse_mountinfo(info); + !mounts.is_empty() && !mounts.iter().any(is_cgroup_mount) } /// Trims trailing slashes so `Path` equality holds while walking ancestors, keeping `/`. From 43b200743ed312be5910d3e551f76d5a750cc516 Mon Sep 17 00:00:00 2001 From: Diego Chagas Date: Sat, 7 Nov 2026 07:18:30 -0300 Subject: [PATCH 5/6] memory: drop the helper the single parse made unused Co-Authored-By: Claude Sonnet 5.5 --- crates/raster/src/memory/system.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crates/raster/src/memory/system.rs b/crates/raster/src/memory/system.rs index 7829d4cef0..3d09de25b6 100644 --- a/crates/raster/src/memory/system.rs +++ b/crates/raster/src/memory/system.rs @@ -346,11 +346,7 @@ pub(super) mod linux { best.map(|(_, _, dir)| dir) } - /// Whether any cgroup (v1 or v2) filesystem is mounted in this mount namespace. - fn has_cgroup_mount(info: &str) -> bool { - parse_mountinfo(info).iter().any(is_cgroup_mount) - } - + /// A cgroup (v1 or v2) filesystem. fn is_cgroup_mount(mount: &Mount) -> bool { mount.fstype == "cgroup" || mount.fstype == "cgroup2" } From 5e9eefc3b03e9a5d228b52abc7c52f6bc5b61434 Mon Sep 17 00:00:00 2001 From: Diego Chagas Date: Sat, 7 Nov 2026 07:25:07 -0300 Subject: [PATCH 6/6] memory: a skipped mountinfo line no longer proves the cgroup mount absent Review feedback: parse_mountinfo drops malformed lines, so a valid root mount plus a truncated cgroup line counted as 'no cgroup mount'. Every non-empty line must now parse; otherwise the case stays fail-closed. --- crates/raster/src/memory/system.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/raster/src/memory/system.rs b/crates/raster/src/memory/system.rs index 3d09de25b6..998720b8b5 100644 --- a/crates/raster/src/memory/system.rs +++ b/crates/raster/src/memory/system.rs @@ -354,9 +354,11 @@ pub(super) mod linux { /// `mountinfo` parsed to at least one mount, none of them a cgroup filesystem. An empty or /// garbled file parses to no mounts at all, which proves nothing about cgroups; a live kernel /// always lists the process's own root, so only a real list can show a cgroup mount is absent. + /// Every non-empty line must parse: a skipped truncated line could be the cgroup mount. fn lists_mounts_without_cgroup(info: &str) -> bool { let mounts = parse_mountinfo(info); - !mounts.is_empty() && !mounts.iter().any(is_cgroup_mount) + let lines = info.lines().filter(|l| !l.trim().is_empty()).count(); + !mounts.is_empty() && mounts.len() == lines && !mounts.iter().any(is_cgroup_mount) } /// Trims trailing slashes so `Path` equality holds while walking ancestors, keeping `/`. @@ -964,6 +966,14 @@ mod tests { assert_eq!(linux::cgroup_limits(&proc, &sys), linux::CgroupLimits::NotConstrained); std::fs::remove_dir_all(proc.parent().unwrap()).unwrap(); + // A truncated mount line may be the cgroup mount: with /sys/fs/cgroup absent, fail closed. + let (proc, _) = fixture("truncated-mountinfo-line"); + let sys = proc.parent().unwrap().join("absent-sys"); + write(&proc.join("self/cgroup"), "0::/app\n"); + write(&proc.join("self/mountinfo"), "25 20 0:23 / /proc rw - proc proc rw\n30 20 0:26 / /sys/fs/cgroup rw - cgroup2\n"); + assert_eq!(linux::cgroup_limits(&proc, &sys), linux::CgroupLimits::Unreadable); + std::fs::remove_dir_all(proc.parent().unwrap()).unwrap(); + // A cgroup2 mount at an alternate path must be honoured while /sys/fs/cgroup is absent. let (proc, _) = fixture("alternate-cgroup-mount"); let sys = proc.parent().unwrap().join("absent-sys");