1use std::path::{Path, PathBuf};
74use std::sync::{
75 atomic::{AtomicU64, Ordering},
76 Arc,
77};
78#[cfg(target_os = "linux")]
79use std::time::Duration;
80
81use crate::resources::HostResourceLimits;
82
83#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
86const DEFAULT_CGROUP2_MOUNT: &str = "/sys/fs/cgroup";
87
88#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
91const CPU_MAX_PERIOD_US: u64 = 100_000;
92
93pub const SCOPE_SWAP_MAX: &str = "0";
100
101#[derive(Debug, Clone, PartialEq, Eq)]
103pub(crate) struct ScopeLimit {
104 pub(crate) file: &'static str,
105 pub(crate) value: String,
106 pub(crate) absent_ok: bool,
111}
112
113#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
118pub(crate) fn scope_limits(limits: &HostResourceLimits) -> Vec<ScopeLimit> {
119 let required = |file, value| ScopeLimit {
120 file,
121 value,
122 absent_ok: false,
123 };
124 let mut writes = vec![
125 required("memory.max", limits.cgroup_memory_bytes.to_string()),
126 ScopeLimit {
127 file: "memory.swap.max",
128 value: SCOPE_SWAP_MAX.to_string(),
129 absent_ok: true,
130 },
131 required("pids.max", limits.cgroup_pids_max.to_string()),
132 ];
133 if let Some(percent) = limits.cgroup_cpu_percent {
134 writes.push(required(
135 "cpu.max",
136 format!(
137 "{} {CPU_MAX_PERIOD_US}",
138 u64::from(percent) * (CPU_MAX_PERIOD_US / 100)
139 ),
140 ));
141 }
142 writes
143}
144
145#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
149pub(crate) fn declared_scope_limits(
150 declared: &murmur_artifact::ResourceCapabilities,
151) -> Vec<ScopeLimit> {
152 let required = |file, value| ScopeLimit {
153 file,
154 value,
155 absent_ok: false,
156 };
157 let mut writes = Vec::new();
158 if let Some(bytes) = declared.cgroup_memory_bytes {
159 writes.push(required("memory.max", bytes.to_string()));
160 }
161 if let Some(pids) = declared.cgroup_pids_max {
162 writes.push(required("pids.max", pids.to_string()));
163 }
164 if let Some(percent) = declared.cgroup_cpu_percent {
165 writes.push(required(
166 "cpu.max",
167 format!(
168 "{} {CPU_MAX_PERIOD_US}",
169 u64::from(percent) * (CPU_MAX_PERIOD_US / 100)
170 ),
171 ));
172 }
173 writes
174}
175
176#[derive(Debug, Clone, PartialEq, Eq)]
178pub enum SwapControl {
179 Exposed,
182 Absent,
185 Unknown,
188}
189
190#[must_use]
197pub fn probe_swap_control() -> SwapControl {
198 #[cfg(not(target_os = "linux"))]
199 {
200 SwapControl::Unknown
201 }
202
203 #[cfg(target_os = "linux")]
204 {
205 let mount = std::fs::read_to_string("/proc/mounts")
206 .map(|contents| parse_cgroup2_mount(&contents))
207 .unwrap_or_else(|_| DEFAULT_CGROUP2_MOUNT.to_string());
208 let Some(own) = std::fs::read_to_string("/proc/self/cgroup")
209 .ok()
210 .and_then(|contents| parse_unified_cgroup_path(&contents))
211 else {
212 return SwapControl::Unknown;
213 };
214 let root = PathBuf::from(mount);
215 let mut dir = root.join(own.trim_start_matches('/'));
216 loop {
217 if dir.join("memory.max").exists() {
218 return if dir.join("memory.swap.max").exists() {
219 SwapControl::Exposed
220 } else {
221 SwapControl::Absent
222 };
223 }
224 if dir == root || !dir.pop() || !dir.starts_with(&root) {
225 return SwapControl::Unknown;
226 }
227 }
228 }
229}
230
231#[must_use]
233pub fn active_swap_areas() -> Option<usize> {
234 let contents = std::fs::read_to_string("/proc/swaps").ok()?;
235 Some(count_swap_areas(&contents))
236}
237
238fn count_swap_areas(contents: &str) -> usize {
240 contents
241 .lines()
242 .skip(1)
243 .filter(|line| !line.trim().is_empty())
244 .count()
245}
246
247#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
250const REQUIRED_CONTROLLERS: &[&str] = &["memory", "pids", "cpu"];
251
252#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
257const SUPERVISOR_LEAF: &str = "murmur-supervisor";
258
259#[derive(Debug)]
266pub(crate) struct CgroupScope {
267 path: PathBuf,
270 #[cfg_attr(not(target_os = "linux"), allow(dead_code))]
275 procs_fd: Option<std::os::fd::OwnedFd>,
276 occupancy: Arc<Occupancy>,
280 #[cfg(target_os = "linux")]
283 watcher: Option<ScopeWatcher>,
284}
285
286#[derive(Debug, Default)]
292struct Occupancy {
293 occupants: AtomicU64,
295 entries: AtomicU64,
298}
299
300impl Occupancy {
301 fn enter(self: &Arc<Self>) -> ScopeOccupancy {
303 let entries_before = self.entries.load(Ordering::SeqCst);
308 let others_inside = self.occupants.fetch_add(1, Ordering::SeqCst) > 0;
309 let sequence = self.entries.fetch_add(1, Ordering::SeqCst);
310 ScopeOccupancy {
311 occupancy: Arc::clone(self),
312 alone_at_entry: !others_inside && sequence == entries_before,
313 sequence,
314 }
315 }
316}
317
318#[derive(Debug)]
323pub(crate) struct ScopeOccupancy {
324 occupancy: Arc<Occupancy>,
325 alone_at_entry: bool,
326 sequence: u64,
327}
328
329impl ScopeOccupancy {
330 pub(crate) fn exclusive(&self) -> bool {
333 self.alone_at_entry && self.occupancy.entries.load(Ordering::SeqCst) == self.sequence + 1
334 }
335}
336
337impl Drop for ScopeOccupancy {
338 fn drop(&mut self) {
339 self.occupancy.occupants.fetch_sub(1, Ordering::SeqCst);
340 }
341}
342
343#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
348pub(crate) struct CgroupEventCounters {
349 pub(crate) oom_kill: u64,
351 pub(crate) pids_max: u64,
353}
354
355impl CgroupEventCounters {
356 pub(crate) fn attribution_since(&self, before: Self) -> Option<&'static str> {
368 if self.oom_kill > before.oom_kill {
369 return Some("cgroup_memory_bytes");
370 }
371 if self.pids_max > before.pids_max {
372 return Some("cgroup_pids_max");
373 }
374 None
375 }
376}
377
378#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize)]
386#[serde(rename_all = "kebab-case")]
387pub enum IoMaxStatus {
388 Enforced,
391 Unavailable,
395 NotRequired,
399 #[default]
402 NotProbed,
403}
404
405#[derive(Debug, Clone, PartialEq, Eq, Default, serde::Serialize)]
412pub struct IoMaxReport {
413 pub declared_bytes_per_sec: u64,
417 pub status: IoMaxStatus,
419 #[serde(skip_serializing_if = "Option::is_none")]
422 pub reason: Option<String>,
423}
424
425impl IoMaxReport {
426 #[cfg_attr(not(target_os = "linux"), allow(dead_code))]
428 fn enforced(declared_bytes_per_sec: u64) -> Self {
429 Self {
430 declared_bytes_per_sec,
431 status: IoMaxStatus::Enforced,
432 reason: None,
433 }
434 }
435
436 #[cfg_attr(not(target_os = "linux"), allow(dead_code))]
438 fn unavailable(declared_bytes_per_sec: u64, reason: impl Into<String>) -> Self {
439 Self {
440 declared_bytes_per_sec,
441 status: IoMaxStatus::Unavailable,
442 reason: Some(reason.into()),
443 }
444 }
445
446 #[must_use]
452 pub fn no_scope_required(declared_bytes_per_sec: u64) -> Self {
453 Self::not_required(declared_bytes_per_sec, NO_SCOPE_REASON)
454 }
455
456 fn not_required(declared_bytes_per_sec: u64, reason: impl Into<String>) -> Self {
460 Self {
461 declared_bytes_per_sec,
462 status: IoMaxStatus::NotRequired,
463 reason: Some(reason.into()),
464 }
465 }
466}
467
468#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize)]
474#[serde(rename_all = "kebab-case")]
475pub enum CgroupScopeStatus {
476 Delegated,
479 Unavailable,
482 NotRequired,
485 #[default]
487 NotProbed,
488}
489
490#[derive(Debug, Clone, PartialEq, Eq, Default, serde::Serialize)]
497pub struct CgroupScopeReport {
498 pub status: CgroupScopeStatus,
500 #[serde(skip_serializing_if = "Option::is_none")]
503 pub errno: Option<String>,
504 #[serde(skip_serializing_if = "Option::is_none")]
508 pub reason: Option<String>,
509}
510
511impl CgroupScopeReport {
512 fn delegated() -> Self {
514 Self {
515 status: CgroupScopeStatus::Delegated,
516 errno: None,
517 reason: None,
518 }
519 }
520
521 #[cfg_attr(not(target_os = "linux"), allow(dead_code))]
523 fn unavailable(error: &CgroupError) -> Self {
524 Self {
525 status: CgroupScopeStatus::Unavailable,
526 errno: error.errno.map(crate::network_namespace::errno_label),
527 reason: Some(error.message.clone()),
528 }
529 }
530
531 fn not_required(reason: &str) -> Self {
533 Self {
534 status: CgroupScopeStatus::NotRequired,
535 errno: None,
536 reason: Some(reason.to_string()),
537 }
538 }
539
540 #[must_use]
543 pub fn no_scope_required() -> Self {
544 Self::not_required(NO_CGROUP_SCOPE_REASON)
545 }
546
547 fn of_prepared(scope: Option<&Arc<CgroupScope>>, required: bool) -> Self {
550 match (scope, required) {
551 (Some(_), _) => Self::delegated(),
552 (None, false) => Self::no_scope_required(),
553 (None, true) => Self::not_required(NOT_LINUX_CGROUP_REASON),
554 }
555 }
556}
557
558#[must_use]
561pub fn render_cgroup_scope(report: &CgroupScopeReport) -> String {
562 let status = match report.status {
563 CgroupScopeStatus::Delegated => "delegated",
564 CgroupScopeStatus::Unavailable => "unavailable",
565 CgroupScopeStatus::NotRequired => "not-required",
566 CgroupScopeStatus::NotProbed => "not-probed",
567 };
568 match (&report.errno, &report.reason) {
569 (Some(errno), Some(reason)) => {
570 format!("cgroup scope: {status} ({errno}) \u{2014} {reason}")
571 }
572 (Some(errno), None) => format!("cgroup scope: {status} ({errno})"),
573 (None, Some(reason)) => format!("cgroup scope: {status} \u{2014} {reason}"),
574 (None, None) => format!("cgroup scope: {status}"),
575 }
576}
577
578#[derive(Debug, Clone, PartialEq, Eq)]
583pub(crate) struct CgroupError {
584 pub(crate) message: String,
586 pub(crate) errno: Option<i32>,
588}
589
590impl CgroupError {
591 #[cfg_attr(not(target_os = "linux"), allow(dead_code))]
593 fn io(message: String, error: &std::io::Error) -> Self {
594 Self {
595 message,
596 errno: error.raw_os_error(),
597 }
598 }
599}
600
601impl std::fmt::Display for CgroupError {
602 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
603 f.write_str(&self.message)
604 }
605}
606
607impl From<String> for CgroupError {
608 fn from(message: String) -> Self {
609 Self {
610 message,
611 errno: None,
612 }
613 }
614}
615
616impl From<CgroupError> for String {
617 fn from(error: CgroupError) -> Self {
618 error.message
619 }
620}
621
622const NO_CGROUP_SCOPE_REASON: &str = "this capsule declares no way to run a subprocess, so \
625 a launch creates no cgroup scope for it";
626const NOT_LINUX_CGROUP_REASON: &str =
627 "cgroups are a Linux mechanism and this host has none, so a launch creates no cgroup scope";
628
629const NO_SCOPE_REASON: &str = "this capsule declares no way to run a subprocess, so it is \
632 given no cgroup scope and there is no process tree for an I/O ceiling to apply to";
633const UNCONFINED_NO_CGROUP_SCOPE_REASON: &str = "this session runs unconfined and \
636 capabilities.resources declares no cgroup field, so a launch creates no cgroup scope for it";
637const UNCONFINED_NO_SCOPE_REASON: &str = "this session runs unconfined and \
638 capabilities.resources declares no cgroup field, so it is given no cgroup scope and no \
639 I/O ceiling applies";
640
641#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
643const UNCONFINED_IO_UNDECLARED_REASON: &str = "this session runs unconfined and \
644 capabilities.resources declares no cgroup_io_bytes_per_sec, so its cgroup scope writes no \
645 io.max";
646#[cfg_attr(target_os = "linux", allow(dead_code))]
647const NOT_LINUX_REASON: &str =
648 "cgroups are a Linux mechanism and this host has none, so no io.max ceiling can exist here";
649
650const IO_MAX_UNAVAILABLE_WARNING: &str = "the declared \
652capabilities.resources.cgroup_io_bytes_per_sec ceiling did not apply to this session's cgroup \
653scope, so this capsule's subprocess tree has no I/O bandwidth bound";
654
655pub(crate) fn io_max_warning(
664 report: &IoMaxReport,
665 cpu_capped: bool,
666) -> Option<(&'static str, String)> {
667 if report.status != IoMaxStatus::Unavailable {
668 return None;
669 }
670 let declared = report.declared_bytes_per_sec;
671 let reason = report.reason.as_deref().unwrap_or("no reason reported");
672 let enforced = if cpu_capped {
673 "memory.max, pids.max and cpu.max"
674 } else {
675 "memory.max and pids.max"
676 };
677 Some((
678 murmur_artifact::W_SEC_021,
679 format!(
680 "{IO_MAX_UNAVAILABLE_WARNING}; {enforced} are still enforced on the scope \
681 (declared {declared} bytes/s): {reason}"
682 ),
683 ))
684}
685
686pub(crate) fn warn_for_unenforced_io_max(
688 workdir: &crate::workdir_fs::WorkdirRoot,
689 report: &IoMaxReport,
690 cpu_capped: bool,
691) {
692 if let Some((code, message)) = io_max_warning(report, cpu_capped) {
693 let link = murmur_artifact::security_warning_link(code);
694 crate::runtime_err!("[capsule-runtime] warning[{code}]: {message} ({link})");
695 crate::agent::append_bootstrap_log(
696 workdir,
697 &format!("[capability-policy] warning[{code}]: {message} ({link})"),
698 );
699 }
700}
701
702#[derive(Debug)]
707pub(crate) struct PreparedScope {
708 pub(crate) scope: Option<Arc<CgroupScope>>,
710 pub(crate) io_max: IoMaxReport,
713 pub(crate) cgroup_scope: CgroupScopeReport,
715}
716
717pub(crate) fn prepare_scope(
732 required: bool,
733 limits: &HostResourceLimits,
734 session_id: &str,
735 workdir: &Path,
736) -> Result<PreparedScope, String> {
737 if !required {
738 return Ok(PreparedScope {
739 scope: None,
740 io_max: IoMaxReport::no_scope_required(limits.cgroup_io_bytes_per_sec),
741 cgroup_scope: CgroupScopeReport::of_prepared(None, false),
742 });
743 }
744 #[cfg(target_os = "linux")]
745 {
746 #[cfg(test)]
747 if FORCE_DELEGATION_FAILURE.with(std::cell::Cell::get) {
748 return Err("forced: delegation refused by the test seam".to_string());
749 }
750 let (scope, io_max) = CgroupScope::create(
751 &scope_limits(limits),
752 IoMaxWrite::Declared(limits.cgroup_io_bytes_per_sec),
753 session_id,
754 workdir,
755 )?;
756 let scope = Arc::new(scope);
757 Ok(PreparedScope {
758 cgroup_scope: CgroupScopeReport::of_prepared(Some(&scope), true),
759 scope: Some(scope),
760 io_max,
761 })
762 }
763 #[cfg(not(target_os = "linux"))]
764 {
765 let _ = (session_id, workdir);
766 Ok(PreparedScope {
767 scope: None,
768 io_max: IoMaxReport::not_required(limits.cgroup_io_bytes_per_sec, NOT_LINUX_REASON),
769 cgroup_scope: CgroupScopeReport::of_prepared(None, true),
770 })
771 }
772}
773
774pub(crate) fn prepare_policy_scope(
780 policy: &crate::types::CapabilityPolicy,
781 session_id: &str,
782 workdir: &Path,
783) -> Result<PreparedScope, String> {
784 let required = requires_process_bounding(policy);
785 if policy.containment_floor != murmur_artifact::ContainmentClass::Unconfined {
786 return prepare_scope(required, &policy.resources, session_id, workdir);
787 }
788 let reported_bytes_per_sec = policy.resources.cgroup_io_bytes_per_sec;
789 if !required {
790 let (io_max, cgroup_scope) = unrequired_scope_reports(policy);
791 return Ok(PreparedScope {
792 scope: None,
793 io_max,
794 cgroup_scope,
795 });
796 }
797 #[cfg(target_os = "linux")]
798 {
799 #[cfg(test)]
800 if FORCE_DELEGATION_FAILURE.with(std::cell::Cell::get) {
801 return Err("forced: delegation refused by the test seam".to_string());
802 }
803 let declared = &policy.declared_resources;
804 let io = match declared.cgroup_io_bytes_per_sec {
805 Some(bytes_per_sec) => IoMaxWrite::Declared(bytes_per_sec),
806 None => IoMaxWrite::Undeclared(reported_bytes_per_sec),
807 };
808 let (scope, io_max) =
809 CgroupScope::create(&declared_scope_limits(declared), io, session_id, workdir)?;
810 let scope = Arc::new(scope);
811 Ok(PreparedScope {
812 cgroup_scope: CgroupScopeReport::of_prepared(Some(&scope), true),
813 scope: Some(scope),
814 io_max,
815 })
816 }
817 #[cfg(not(target_os = "linux"))]
818 {
819 let _ = (session_id, workdir);
820 Ok(PreparedScope {
821 scope: None,
822 io_max: IoMaxReport::not_required(reported_bytes_per_sec, NOT_LINUX_REASON),
823 cgroup_scope: CgroupScopeReport::of_prepared(None, true),
824 })
825 }
826}
827
828fn unrequired_scope_reports(
832 policy: &crate::types::CapabilityPolicy,
833) -> (IoMaxReport, CgroupScopeReport) {
834 let reported_bytes_per_sec = policy.resources.cgroup_io_bytes_per_sec;
835 let has_route = crate::runtime::has_subprocess_route_at_staging(policy);
836 if policy.containment_floor == murmur_artifact::ContainmentClass::Unconfined && has_route {
837 return (
838 IoMaxReport::not_required(reported_bytes_per_sec, UNCONFINED_NO_SCOPE_REASON),
839 CgroupScopeReport::not_required(UNCONFINED_NO_CGROUP_SCOPE_REASON),
840 );
841 }
842 (
843 IoMaxReport::no_scope_required(reported_bytes_per_sec),
844 CgroupScopeReport::no_scope_required(),
845 )
846}
847
848#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
850#[derive(Debug, Clone, Copy)]
851enum IoMaxWrite {
852 Declared(u64),
854 Undeclared(u64),
857}
858
859#[derive(Debug, Clone, PartialEq, Eq)]
862pub struct ScopeProbe {
863 pub cgroup_scope: CgroupScopeReport,
865 pub io_max: IoMaxReport,
867}
868
869#[must_use]
891pub fn probe_scope(required: bool, limits: &HostResourceLimits, workdir: &Path) -> ScopeProbe {
892 let declared = limits.cgroup_io_bytes_per_sec;
893 if !required {
894 return ScopeProbe {
895 cgroup_scope: CgroupScopeReport::no_scope_required(),
896 io_max: IoMaxReport::no_scope_required(declared),
897 };
898 }
899 #[cfg(target_os = "linux")]
900 {
901 let base = match delegated_base() {
902 Ok(base) => base,
903 Err(error) => {
904 return ScopeProbe {
905 cgroup_scope: CgroupScopeReport::unavailable(&error),
906 io_max: IoMaxReport::unavailable(
907 declared,
908 format!("no delegated cgroup base to write io.max into: {error}"),
909 ),
910 }
911 }
912 };
913 let path = base.join(scope_dir_name("explain-scope"));
914 if let Err(error) = create_scope_dir(&path) {
915 return ScopeProbe {
916 cgroup_scope: CgroupScopeReport::unavailable(&scope_dir_refusal(&path, &error)),
917 io_max: IoMaxReport::unavailable(
918 declared,
919 format!(
920 "could not create a throwaway cgroup at {}: {error}",
921 path.display()
922 ),
923 ),
924 };
925 }
926 let io_max = write_io_max(&path, declared, workdir);
927 let _ = std::fs::remove_dir(&path);
930 ScopeProbe {
931 cgroup_scope: CgroupScopeReport::delegated(),
932 io_max,
933 }
934 }
935 #[cfg(not(target_os = "linux"))]
936 {
937 let _ = workdir;
938 ScopeProbe {
939 cgroup_scope: CgroupScopeReport::not_required(NOT_LINUX_CGROUP_REASON),
940 io_max: IoMaxReport::not_required(declared, NOT_LINUX_REASON),
941 }
942 }
943}
944
945#[cfg(target_os = "linux")]
948fn create_scope_dir(path: &Path) -> std::io::Result<()> {
949 match std::fs::create_dir(path) {
950 Err(error) if error.kind() != std::io::ErrorKind::AlreadyExists => Err(error),
951 _ => Ok(()),
952 }
953}
954
955#[cfg(target_os = "linux")]
957fn scope_dir_refusal(path: &Path, error: &std::io::Error) -> CgroupError {
958 CgroupError::io(
959 format!(
960 "could not create cgroup scope at {}: {error}",
961 path.display()
962 ),
963 error,
964 )
965}
966
967#[must_use]
972pub fn explain_scope_probe(policy: &crate::types::CapabilityPolicy, workdir: &Path) -> ScopeProbe {
973 let required = requires_process_bounding(policy);
974 if policy.containment_floor != murmur_artifact::ContainmentClass::Unconfined {
975 return probe_scope(required, &policy.resources, workdir);
976 }
977 if !required {
978 let (io_max, cgroup_scope) = unrequired_scope_reports(policy);
979 return ScopeProbe {
980 cgroup_scope,
981 io_max,
982 };
983 }
984 let declared_io = policy.declared_resources.cgroup_io_bytes_per_sec;
985 let mut probe = probe_scope(
986 true,
987 &HostResourceLimits {
988 cgroup_io_bytes_per_sec: declared_io
989 .unwrap_or(policy.resources.cgroup_io_bytes_per_sec),
990 ..policy.resources
991 },
992 workdir,
993 );
994 if cfg!(target_os = "linux") && declared_io.is_none() {
995 probe.io_max = IoMaxReport::not_required(
996 policy.resources.cgroup_io_bytes_per_sec,
997 UNCONFINED_IO_UNDECLARED_REASON,
998 );
999 }
1000 probe
1001}
1002
1003pub fn requires_process_bounding(policy: &crate::types::CapabilityPolicy) -> bool {
1013 let has_route = crate::runtime::has_subprocess_route_at_staging(policy);
1014 if policy.containment_floor == murmur_artifact::ContainmentClass::Unconfined {
1015 return has_route && crate::resources::declares_cgroup_limit(&policy.declared_resources);
1016 }
1017 has_route
1018}
1019
1020impl CgroupScope {
1021 pub(crate) fn path(&self) -> &Path {
1023 &self.path
1024 }
1025
1026 #[cfg_attr(not(target_os = "linux"), allow(dead_code))]
1034 #[allow(unsafe_code)]
1035 pub(crate) fn join_current_process(&self) -> std::io::Result<()> {
1036 use std::os::fd::AsRawFd;
1037
1038 let Some(fd) = self.procs_fd.as_ref() else {
1039 return Ok(());
1040 };
1041
1042 let pid = unsafe { libc::getpid() };
1044 let mut buf = [0u8; 24];
1045 let written = format_pid(pid, &mut buf);
1046
1047 let mut offset = 0;
1048 while offset < written {
1049 let rc = unsafe {
1052 libc::write(
1053 fd.as_raw_fd(),
1054 buf[offset..written].as_ptr().cast::<libc::c_void>(),
1055 written - offset,
1056 )
1057 };
1058 if rc < 0 {
1059 let error = std::io::Error::last_os_error();
1060 if error.kind() == std::io::ErrorKind::Interrupted {
1061 continue;
1062 }
1063 return Err(error);
1064 }
1065 offset += rc as usize;
1066 }
1067 Ok(())
1068 }
1069
1070 pub(crate) fn enter(&self) -> ScopeOccupancy {
1073 self.occupancy.enter()
1074 }
1075
1076 pub(crate) fn places_subprocesses(&self) -> bool {
1082 self.procs_fd.is_some()
1083 }
1084
1085 #[cfg_attr(not(target_os = "linux"), allow(dead_code))]
1090 pub(crate) fn event_counters(&self) -> CgroupEventCounters {
1091 CgroupEventCounters {
1092 oom_kill: read_event_counter(&self.path.join("memory.events"), "oom_kill"),
1093 pids_max: read_event_counter(&self.path.join("pids.events"), "max"),
1094 }
1095 }
1096}
1097
1098impl CgroupScope {
1099 pub(crate) fn kill_all(&self) -> std::io::Result<()> {
1110 if self.path.as_os_str().is_empty() {
1111 return Ok(());
1112 }
1113 #[cfg(target_os = "linux")]
1114 {
1115 #[cfg(test)]
1116 let forced = FORCE_KILL_FALLBACK.with(std::cell::Cell::get);
1117 #[cfg(not(test))]
1118 let forced = false;
1119 if !forced {
1120 match write_existing(&self.path.join("cgroup.kill"), "1") {
1121 Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
1122 written => return written,
1123 }
1124 }
1125 self.kill_by_freezing()
1126 }
1127 #[cfg(not(target_os = "linux"))]
1128 Ok(())
1129 }
1130
1131 pub(crate) fn wait_until_empty(&self, timeout: std::time::Duration) -> bool {
1136 let deadline = std::time::Instant::now() + timeout;
1137 loop {
1138 if self.member_pids().is_empty() {
1139 return true;
1140 }
1141 if std::time::Instant::now() >= deadline {
1142 return false;
1143 }
1144 std::thread::sleep(KILL_POLL_INTERVAL);
1145 }
1146 }
1147
1148 pub(crate) fn member_pids(&self) -> Vec<i32> {
1151 if self.path.as_os_str().is_empty() {
1152 return Vec::new();
1153 }
1154 std::fs::read_to_string(self.path.join("cgroup.procs"))
1155 .map(|procs| {
1156 procs
1157 .lines()
1158 .filter_map(|line| line.trim().parse().ok())
1159 .collect()
1160 })
1161 .unwrap_or_default()
1162 }
1163
1164 #[cfg(target_os = "linux")]
1170 #[allow(unsafe_code)]
1171 fn kill_by_freezing(&self) -> std::io::Result<()> {
1172 let freeze = self.path.join("cgroup.freeze");
1173 let deadline = std::time::Instant::now() + KILL_FALLBACK_DEADLINE;
1174 loop {
1175 let pids = self.member_pids();
1176 if pids.is_empty() {
1177 return Ok(());
1178 }
1179 let _ = write_existing(&freeze, "1");
1181 for pid in pids {
1182 unsafe { libc::kill(pid, libc::SIGKILL) };
1184 }
1185 let _ = write_existing(&freeze, "0");
1186 if std::time::Instant::now() >= deadline {
1187 return Err(std::io::Error::other(format!(
1188 "processes were still in {} after {} ms of SIGKILL",
1189 self.path.display(),
1190 KILL_FALLBACK_DEADLINE.as_millis()
1191 )));
1192 }
1193 std::thread::sleep(KILL_POLL_INTERVAL);
1194 }
1195 }
1196}
1197
1198const KILL_FALLBACK_DEADLINE: std::time::Duration = std::time::Duration::from_secs(2);
1200
1201const KILL_POLL_INTERVAL: std::time::Duration = std::time::Duration::from_millis(10);
1203
1204#[cfg(target_os = "linux")]
1207fn write_existing(path: &Path, value: &str) -> std::io::Result<()> {
1208 use std::io::Write as _;
1209 let file = std::fs::OpenOptions::new().write(true).open(path)?;
1210 (&file).write_all(value.as_bytes())
1211}
1212
1213#[cfg(all(test, target_os = "linux"))]
1216thread_local! {
1217 pub(crate) static FORCE_KILL_FALLBACK: std::cell::Cell<bool> =
1218 const { std::cell::Cell::new(false) };
1219}
1220
1221#[cfg(all(test, target_os = "linux"))]
1224thread_local! {
1225 pub(crate) static FORCE_DELEGATION_FAILURE: std::cell::Cell<bool> =
1226 const { std::cell::Cell::new(false) };
1227}
1228
1229impl Drop for CgroupScope {
1230 fn drop(&mut self) {
1238 if self.path.as_os_str().is_empty() {
1239 return;
1240 }
1241 if !self.member_pids().is_empty() {
1242 let _ = self.kill_all();
1243 self.wait_until_empty(KILL_FALLBACK_DEADLINE);
1244 }
1245 self.procs_fd = None;
1248 #[cfg(target_os = "linux")]
1249 {
1250 self.watcher = None;
1251 }
1252 if let Err(error) = std::fs::remove_dir(&self.path) {
1253 crate::runtime_err!(
1254 "[capsule-runtime] note: could not remove cgroup scope {} ({error}); \
1255 it will be reclaimed once its last task exits",
1256 self.path.display()
1257 );
1258 }
1259 }
1260}
1261
1262#[cfg(target_os = "linux")]
1267#[derive(Debug)]
1268struct ScopeWatcher {
1269 lifeline: std::sync::Mutex<Option<std::os::fd::OwnedFd>>,
1273 pid: libc::pid_t,
1274}
1275
1276#[cfg(target_os = "linux")]
1280#[derive(Clone, Copy)]
1281struct WatcherKill {
1282 kill: libc::c_int,
1283 freeze: libc::c_int,
1284 procs: libc::c_int,
1285}
1286
1287#[cfg(target_os = "linux")]
1288impl ScopeWatcher {
1289 #[allow(unsafe_code)]
1293 fn spawn(scope: &Path) -> std::io::Result<Self> {
1294 use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
1295
1296 #[cfg(test)]
1297 let forced = FORCE_KILL_FALLBACK.with(std::cell::Cell::get);
1298 #[cfg(not(test))]
1299 let forced = false;
1300 let kill = if forced {
1301 None
1302 } else {
1303 open_if_present(&scope.join("cgroup.kill"), libc::O_WRONLY)?
1304 };
1305 let (freeze, procs) = match kill {
1306 Some(_) => (None, None),
1307 None => (
1308 open_if_present(&scope.join("cgroup.freeze"), libc::O_WRONLY)?,
1309 Some(open_with(&scope.join("cgroup.procs"), libc::O_RDONLY)?),
1310 ),
1311 };
1312 let devnull = open_with(Path::new("/dev/null"), libc::O_RDWR)?;
1313
1314 let mut ends = [0 as libc::c_int; 2];
1315 if unsafe { libc::pipe2(ends.as_mut_ptr(), libc::O_CLOEXEC) } != 0 {
1317 return Err(std::io::Error::last_os_error());
1318 }
1319 let (read_end, write_end) =
1322 unsafe { (OwnedFd::from_raw_fd(ends[0]), OwnedFd::from_raw_fd(ends[1])) };
1323
1324 let raw = |fd: &Option<OwnedFd>| fd.as_ref().map_or(-1, AsRawFd::as_raw_fd);
1325 let plan = WatcherKill {
1326 kill: raw(&kill),
1327 freeze: raw(&freeze),
1328 procs: raw(&procs),
1329 };
1330 let mut keep = [-1 as libc::c_int; 7];
1334 let mut kept = 0;
1335 for fd in [
1336 0,
1337 1,
1338 2,
1339 read_end.as_raw_fd(),
1340 plan.kill,
1341 plan.freeze,
1342 plan.procs,
1343 ] {
1344 if fd >= 0 {
1345 keep[kept] = fd;
1346 kept += 1;
1347 }
1348 }
1349 keep[..kept].sort_unstable();
1350 let devnull_fd = devnull.as_raw_fd();
1351 let read_fd = read_end.as_raw_fd();
1352 let deadline_ns = KILL_FALLBACK_DEADLINE.as_nanos() as i64;
1353
1354 let pid = unsafe { libc::fork() };
1360 if pid < 0 {
1361 return Err(std::io::Error::last_os_error());
1362 }
1363 if pid == 0 {
1364 unsafe { watch(devnull_fd, &keep[..kept], read_fd, plan, deadline_ns) }
1366 }
1367 Ok(Self {
1368 lifeline: std::sync::Mutex::new(Some(write_end)),
1369 pid,
1370 })
1371 }
1372
1373 #[cfg(test)]
1375 fn cut_lifeline(&self) {
1376 self.lifeline
1377 .lock()
1378 .unwrap_or_else(std::sync::PoisonError::into_inner)
1379 .take();
1380 }
1381}
1382
1383#[cfg(target_os = "linux")]
1384impl Drop for ScopeWatcher {
1385 #[allow(unsafe_code)]
1388 fn drop(&mut self) {
1389 self.lifeline
1390 .get_mut()
1391 .unwrap_or_else(std::sync::PoisonError::into_inner)
1392 .take();
1393 let mut status = 0;
1394 loop {
1395 let waited = unsafe { libc::waitpid(self.pid, &mut status, 0) };
1397 if waited >= 0 || std::io::Error::last_os_error().raw_os_error() != Some(libc::EINTR) {
1398 return;
1399 }
1400 }
1401 }
1402}
1403
1404#[cfg(target_os = "linux")]
1406fn open_with(path: &Path, flags: libc::c_int) -> std::io::Result<std::os::fd::OwnedFd> {
1407 use std::os::unix::fs::OpenOptionsExt;
1408
1409 std::fs::OpenOptions::new()
1410 .read(flags & libc::O_ACCMODE != libc::O_WRONLY)
1411 .write(flags & libc::O_ACCMODE != libc::O_RDONLY)
1412 .custom_flags(libc::O_CLOEXEC)
1413 .open(path)
1414 .map(std::os::fd::OwnedFd::from)
1415}
1416
1417#[cfg(target_os = "linux")]
1419fn open_if_present(
1420 path: &Path,
1421 flags: libc::c_int,
1422) -> std::io::Result<Option<std::os::fd::OwnedFd>> {
1423 match open_with(path, flags) {
1424 Ok(fd) => Ok(Some(fd)),
1425 Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None),
1426 Err(error) => Err(error),
1427 }
1428}
1429
1430#[cfg(target_os = "linux")]
1439#[allow(unsafe_code)]
1440unsafe fn watch(
1441 devnull: libc::c_int,
1442 keep: &[libc::c_int],
1443 read_fd: libc::c_int,
1444 plan: WatcherKill,
1445 deadline_ns: i64,
1446) -> ! {
1447 unsafe {
1450 for target in 0..3 {
1452 libc::dup2(devnull, target);
1453 }
1454 let mut low: libc::c_uint = 0;
1455 for &fd in keep {
1456 let fd = fd as libc::c_uint;
1457 if fd > low {
1458 close_range(low, fd - 1);
1459 }
1460 low = fd + 1;
1461 }
1462 close_range(low, libc::c_uint::MAX);
1463
1464 libc::setpgid(0, 0);
1465 let mut ignore: libc::sigaction = std::mem::zeroed();
1466 ignore.sa_sigaction = libc::SIG_IGN;
1467 for signal in [libc::SIGINT, libc::SIGTERM, libc::SIGHUP, libc::SIGPIPE] {
1468 libc::sigaction(signal, &ignore, std::ptr::null_mut());
1469 }
1470
1471 let mut byte = [0u8; 1];
1472 loop {
1473 let read = libc::read(read_fd, byte.as_mut_ptr().cast(), 1);
1474 if read == 0 {
1475 break;
1476 }
1477 if read < 0 && *libc::__errno_location() != libc::EINTR {
1478 break;
1479 }
1480 }
1481
1482 kill_scope(plan, deadline_ns);
1483 libc::_exit(0)
1484 }
1485}
1486
1487#[cfg(target_os = "linux")]
1498#[allow(unsafe_code)]
1499pub(crate) unsafe fn close_range(low: libc::c_uint, high: libc::c_uint) {
1500 unsafe {
1502 if libc::syscall(libc::SYS_close_range, low, high, 0 as libc::c_uint) == 0 {
1503 return;
1504 }
1505 let mut limit: libc::rlimit = std::mem::zeroed();
1506 let ceiling = if libc::getrlimit(libc::RLIMIT_NOFILE, &mut limit) == 0 {
1507 limit.rlim_cur.min(libc::c_uint::MAX as libc::rlim_t) as libc::c_uint
1508 } else {
1509 1 << 20
1510 };
1511 let mut fd = low;
1512 while fd <= high && fd < ceiling {
1513 libc::close(fd as libc::c_int);
1514 fd += 1;
1515 }
1516 }
1517}
1518
1519#[cfg(target_os = "linux")]
1526#[allow(unsafe_code)]
1527unsafe fn kill_scope(plan: WatcherKill, deadline_ns: i64) {
1528 unsafe {
1530 if plan.kill >= 0 {
1531 write_control(plan.kill, b"1");
1532 return;
1533 }
1534 let start = monotonic_ns();
1535 loop {
1536 if plan.freeze >= 0 {
1537 write_control(plan.freeze, b"1");
1538 }
1539 let signalled = signal_listed(plan.procs);
1540 if plan.freeze >= 0 {
1541 write_control(plan.freeze, b"0");
1542 }
1543 if signalled == 0 || monotonic_ns() - start >= deadline_ns {
1544 return;
1545 }
1546 let pause = libc::timespec {
1547 tv_sec: 0,
1548 tv_nsec: KILL_POLL_INTERVAL.as_nanos() as libc::c_long,
1549 };
1550 libc::nanosleep(&pause, std::ptr::null_mut());
1551 }
1552 }
1553}
1554
1555#[cfg(target_os = "linux")]
1561#[allow(unsafe_code)]
1562unsafe fn write_control(fd: libc::c_int, value: &[u8]) {
1563 unsafe {
1565 libc::lseek(fd, 0, libc::SEEK_SET);
1566 libc::write(fd, value.as_ptr().cast(), value.len());
1567 }
1568}
1569
1570#[cfg(target_os = "linux")]
1577#[allow(unsafe_code)]
1578unsafe fn signal_listed(procs: libc::c_int) -> usize {
1579 unsafe {
1581 if procs < 0 || libc::lseek(procs, 0, libc::SEEK_SET) < 0 {
1582 return 0;
1583 }
1584 let mut buf = [0u8; 512];
1585 let mut pid: libc::pid_t = 0;
1586 let mut digits = false;
1587 let mut signalled = 0;
1588 loop {
1589 let read = libc::read(procs, buf.as_mut_ptr().cast(), buf.len());
1590 if read < 0 && *libc::__errno_location() == libc::EINTR {
1591 continue;
1592 }
1593 if read <= 0 {
1594 break;
1595 }
1596 for &byte in &buf[..read as usize] {
1597 if byte.is_ascii_digit() {
1598 pid = pid
1599 .saturating_mul(10)
1600 .saturating_add(libc::pid_t::from(byte - b'0'));
1601 digits = true;
1602 } else {
1603 if digits && pid > 0 {
1604 libc::kill(pid, libc::SIGKILL);
1605 signalled += 1;
1606 }
1607 pid = 0;
1608 digits = false;
1609 }
1610 }
1611 }
1612 if digits && pid > 0 {
1613 libc::kill(pid, libc::SIGKILL);
1614 signalled += 1;
1615 }
1616 signalled
1617 }
1618}
1619
1620#[cfg(target_os = "linux")]
1626#[allow(unsafe_code)]
1627unsafe fn monotonic_ns() -> i64 {
1628 unsafe {
1630 let mut now: libc::timespec = std::mem::zeroed();
1631 libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut now);
1632 now.tv_sec as i64 * 1_000_000_000 + now.tv_nsec as i64
1633 }
1634}
1635
1636#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
1641fn format_pid(pid: i32, buf: &mut [u8; 24]) -> usize {
1642 let mut digits = [0u8; 20];
1643 let mut count = 0;
1644 let mut value = pid.unsigned_abs() as u64;
1645 if value == 0 {
1646 digits[0] = b'0';
1647 count = 1;
1648 }
1649 while value > 0 {
1650 digits[count] = b'0' + (value % 10) as u8;
1651 value /= 10;
1652 count += 1;
1653 }
1654
1655 let mut written = 0;
1656 for index in (0..count).rev() {
1657 buf[written] = digits[index];
1658 written += 1;
1659 }
1660 buf[written] = b'\n';
1661 written + 1
1662}
1663
1664#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
1669fn read_event_counter(path: &Path, key: &str) -> u64 {
1670 let Ok(contents) = std::fs::read_to_string(path) else {
1671 return 0;
1672 };
1673 parse_event_counter(&contents, key)
1674}
1675
1676#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
1678fn parse_event_counter(contents: &str, key: &str) -> u64 {
1679 for line in contents.lines() {
1680 let mut parts = line.split_whitespace();
1681 if parts.next() == Some(key) {
1682 return parts
1683 .next()
1684 .and_then(|value| value.parse().ok())
1685 .unwrap_or(0);
1686 }
1687 }
1688 0
1689}
1690
1691#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
1703fn scope_dir_name(session_id: &str) -> String {
1704 static SERIAL: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
1705 let serial = SERIAL.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1706
1707 let sanitized: String = session_id
1708 .chars()
1709 .map(|c| {
1710 if c.is_ascii_alphanumeric() || c == '.' || c == '_' || c == '-' {
1711 c
1712 } else {
1713 '_'
1714 }
1715 })
1716 .collect();
1717 let pid = std::process::id();
1718 if sanitized.is_empty() {
1719 format!("murmur-{pid}-{serial}")
1720 } else {
1721 format!("murmur-{sanitized}-{pid}-{serial}")
1722 }
1723}
1724
1725#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
1731fn parse_unified_cgroup_path(contents: &str) -> Option<String> {
1732 for line in contents.lines() {
1733 let mut parts = line.splitn(3, ':');
1734 let (Some("0"), Some(""), Some(path)) = (parts.next(), parts.next(), parts.next()) else {
1735 continue;
1736 };
1737 if path.starts_with('/') {
1738 return Some(path.to_string());
1739 }
1740 }
1741 None
1742}
1743
1744#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
1747fn parse_cgroup2_mount(contents: &str) -> String {
1748 for line in contents.lines() {
1749 let mut parts = line.split_whitespace();
1750 let (Some(_source), Some(target), Some(fstype)) =
1751 (parts.next(), parts.next(), parts.next())
1752 else {
1753 continue;
1754 };
1755 if fstype == "cgroup2" {
1756 return target.to_string();
1757 }
1758 }
1759 DEFAULT_CGROUP2_MOUNT.to_string()
1760}
1761
1762#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
1764fn missing_controllers(subtree_control: &str, wanted: &[&str]) -> Vec<String> {
1765 let enabled: Vec<&str> = subtree_control.split_whitespace().collect();
1766 wanted
1767 .iter()
1768 .filter(|controller| !enabled.contains(*controller))
1769 .map(|controller| (*controller).to_string())
1770 .collect()
1771}
1772
1773#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
1776fn device_major_minor(dev: u64) -> (u64, u64) {
1777 let major = ((dev >> 8) & 0xfff) | ((dev >> 32) & !0xfff);
1778 let minor = (dev & 0xff) | ((dev >> 12) & !0xff);
1779 (major, minor)
1780}
1781
1782#[cfg(target_os = "linux")]
1783impl CgroupScope {
1784 fn create(
1791 limits: &[ScopeLimit],
1792 io: IoMaxWrite,
1793 session_id: &str,
1794 workdir: &Path,
1795 ) -> Result<(Self, IoMaxReport), String> {
1796 let base = delegated_base()?;
1797
1798 let path = base.join(scope_dir_name(session_id));
1799 create_scope_dir(&path).map_err(|error| scope_dir_refusal(&path, &error))?;
1800
1801 let scope = Self {
1802 path,
1803 procs_fd: None,
1804 occupancy: Arc::default(),
1805 watcher: None,
1806 };
1807
1808 for limit in limits {
1809 scope.write_limit(limit)?;
1810 }
1811 let io_max = match io {
1812 IoMaxWrite::Declared(bytes_per_sec) => scope.apply_io_max(bytes_per_sec, workdir),
1813 IoMaxWrite::Undeclared(bytes_per_sec) => {
1814 IoMaxReport::not_required(bytes_per_sec, UNCONFINED_IO_UNDECLARED_REASON)
1815 }
1816 };
1817
1818 let procs_fd = open_write_only(&scope.path.join("cgroup.procs")).map_err(|error| {
1821 format!(
1822 "could not open {}/cgroup.procs for subprocess placement: {error}",
1823 scope.path.display()
1824 )
1825 })?;
1826
1827 let watcher = ScopeWatcher::spawn(&scope.path).map_err(|error| {
1828 format!(
1829 "could not start the watcher that kills {} if the runtime dies: {error}",
1830 scope.path.display()
1831 )
1832 })?;
1833
1834 Ok((
1835 Self {
1836 path: scope.take_path(),
1837 procs_fd: Some(procs_fd),
1838 occupancy: Arc::default(),
1839 watcher: Some(watcher),
1840 },
1841 io_max,
1842 ))
1843 }
1844
1845 fn take_path(mut self) -> PathBuf {
1848 let path = std::mem::take(&mut self.path);
1849 path
1851 }
1852
1853 fn write_limit(&self, limit: &ScopeLimit) -> Result<(), String> {
1856 let path = self.path.join(limit.file);
1857 let value = &limit.value;
1858 match write_existing(&path, &format!("{value}\n")) {
1859 Err(error) if limit.absent_ok && error.kind() == std::io::ErrorKind::NotFound => Ok(()),
1860 written => written
1861 .map_err(|error| format!("could not write {value} to {}: {error}", path.display())),
1862 }
1863 }
1864
1865 fn apply_io_max(&self, bytes_per_sec: u64, workdir: &Path) -> IoMaxReport {
1877 write_io_max(&self.path, bytes_per_sec, workdir)
1878 }
1879}
1880
1881#[cfg(target_os = "linux")]
1891fn write_io_max(scope_path: &Path, bytes_per_sec: u64, workdir: &Path) -> IoMaxReport {
1892 let (major, minor) = match io_max_device(workdir) {
1893 Ok(device) => device,
1894 Err(reason) => return IoMaxReport::unavailable(bytes_per_sec, reason),
1895 };
1896 let value = format!("{major}:{minor} rbps={bytes_per_sec} wbps={bytes_per_sec}");
1897 match std::fs::write(scope_path.join("io.max"), format!("{value}\n")) {
1898 Ok(()) => IoMaxReport::enforced(bytes_per_sec),
1899 Err(error) => IoMaxReport::unavailable(
1905 bytes_per_sec,
1906 format!("writing `{value}` to the scope's io.max: {error}"),
1907 ),
1908 }
1909}
1910
1911#[cfg(target_os = "linux")]
1929fn io_max_device(path: &Path) -> Result<(u64, u64), String> {
1930 use std::os::linux::fs::MetadataExt;
1931
1932 let Some(existing) = nearest_existing_ancestor(path) else {
1933 return Err(format!(
1934 "could not stat {} or any ancestor of it to find its backing device",
1935 path.display()
1936 ));
1937 };
1938 let metadata = existing.metadata().map_err(|error| {
1939 format!(
1940 "could not stat {} to find its backing device: {error}",
1941 existing.display()
1942 )
1943 })?;
1944 let (major, minor) = device_major_minor(metadata.st_dev());
1945 let (major, minor) = if major == 0 {
1946 mount_source_device(existing)?
1947 } else {
1948 (major, minor)
1949 };
1950 Ok(whole_disk_device(major, minor))
1951}
1952
1953#[cfg(target_os = "linux")]
1959fn mount_source_device(path: &Path) -> Result<(u64, u64), String> {
1960 use std::os::unix::fs::{FileTypeExt, MetadataExt};
1961
1962 let resolved = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
1963 let mountinfo = std::fs::read_to_string("/proc/self/mountinfo").map_err(|error| {
1964 format!(
1965 "reading /proc/self/mountinfo to find the device behind {}: {error}",
1966 resolved.display()
1967 )
1968 })?;
1969 let Some(mount) = mount_covering(&mountinfo, &resolved) else {
1970 return Err(format!(
1971 "no mount in /proc/self/mountinfo covers {}, so its backing device is unknown",
1972 resolved.display()
1973 ));
1974 };
1975 match std::fs::metadata(&mount.source) {
1976 Ok(metadata) if metadata.file_type().is_block_device() => {
1977 Ok(device_major_minor(metadata.rdev()))
1978 }
1979 _ => Err(format!(
1980 "the filesystem mounted at {} is backed by `{}`, which is not a block device, so no \
1981 io.max ceiling can name one",
1982 mount.mount_point.display(),
1983 mount.source.display()
1984 )),
1985 }
1986}
1987
1988#[cfg(target_os = "linux")]
1996fn whole_disk_device(major: u64, minor: u64) -> (u64, u64) {
1997 let device = PathBuf::from(format!("/sys/dev/block/{major}:{minor}"));
1998 if !device.join("partition").exists() {
1999 return (major, minor);
2000 }
2001 std::fs::read_to_string(device.join("../dev"))
2002 .ok()
2003 .and_then(|contents| parse_dev_line(&contents))
2004 .unwrap_or((major, minor))
2005}
2006
2007#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
2009fn parse_dev_line(contents: &str) -> Option<(u64, u64)> {
2010 let (major, minor) = contents.trim().split_once(':')?;
2011 Some((major.parse().ok()?, minor.parse().ok()?))
2012}
2013
2014#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
2017#[derive(Debug, PartialEq, Eq)]
2018struct MountEntry {
2019 mount_point: PathBuf,
2021 source: PathBuf,
2024}
2025
2026#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
2031fn mount_covering(mountinfo: &str, path: &Path) -> Option<MountEntry> {
2032 let mut best: Option<(usize, MountEntry)> = None;
2033 for line in mountinfo.lines() {
2034 let Some(entry) = parse_mount_entry(line) else {
2035 continue;
2036 };
2037 if !path.starts_with(&entry.mount_point) {
2038 continue;
2039 }
2040 let depth = entry.mount_point.components().count();
2041 if best.as_ref().is_none_or(|(best, _)| depth >= *best) {
2042 best = Some((depth, entry));
2043 }
2044 }
2045 best.map(|(_, entry)| entry)
2046}
2047
2048#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
2053fn parse_mount_entry(line: &str) -> Option<MountEntry> {
2054 let (mounted, backing) = line.split_once(" - ")?;
2055 let mount_point = mounted.split_whitespace().nth(4)?;
2056 let source = backing.split_whitespace().nth(1)?;
2057 Some(MountEntry {
2058 mount_point: unescape_octal(mount_point),
2059 source: unescape_octal(source),
2060 })
2061}
2062
2063#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
2066fn unescape_octal(field: &str) -> PathBuf {
2067 use std::os::unix::ffi::OsStringExt;
2068
2069 let bytes = field.as_bytes();
2070 let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
2071 let mut index = 0;
2072 while index < bytes.len() {
2073 let escape = bytes[index] == b'\\'
2074 && index + 3 < bytes.len()
2075 && bytes[index + 1..index + 4]
2076 .iter()
2077 .all(|byte| (b'0'..=b'7').contains(byte));
2078 if escape {
2079 let digits = &field[index + 1..index + 4];
2080 out.push(u8::from_str_radix(digits, 8).expect("three octal digits"));
2081 index += 4;
2082 } else {
2083 out.push(bytes[index]);
2084 index += 1;
2085 }
2086 }
2087 PathBuf::from(std::ffi::OsString::from_vec(out))
2088}
2089
2090#[cfg(target_os = "linux")]
2096fn nearest_existing_ancestor(path: &Path) -> Option<&Path> {
2097 let mut candidate = Some(path);
2098 while let Some(current) = candidate {
2099 if current.exists() {
2100 return Some(current);
2101 }
2102 candidate = current.parent();
2103 }
2104 None
2105}
2106
2107#[cfg(all(test, target_os = "linux"))]
2108pub(crate) static DELEGATED_BASE_RESOLUTION_SPAWNS: std::sync::atomic::AtomicUsize =
2109 std::sync::atomic::AtomicUsize::new(0);
2110
2111pub(crate) fn resolve_delegated_base_in_background() {
2124 #[cfg(target_os = "linux")]
2125 {
2126 #[cfg(test)]
2127 DELEGATED_BASE_RESOLUTION_SPAWNS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
2128 let _ = std::thread::Builder::new()
2129 .name("murmur-cgroup-base".to_string())
2130 .spawn(|| {
2131 let _ = delegated_base();
2132 });
2133 }
2134}
2135
2136#[cfg(target_os = "linux")]
2150fn delegated_base() -> Result<PathBuf, CgroupError> {
2151 static BASE: std::sync::Mutex<Option<PathBuf>> = std::sync::Mutex::new(None);
2152
2153 let mut cached = BASE.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
2156 if let Some(base) = cached.as_ref() {
2157 return Ok(base.clone());
2158 }
2159
2160 move_self_into_own_systemd_scope();
2163
2164 let base = probe_delegation()?;
2165 enable_controllers(&base)?;
2166 *cached = Some(base.clone());
2167 Ok(base)
2168}
2169
2170#[cfg(target_os = "linux")]
2180fn move_self_into_own_systemd_scope() {
2181 static ATTEMPTED: std::sync::Once = std::sync::Once::new();
2182 ATTEMPTED.call_once(|| {
2183 let _ = request_transient_scope(&transient_scope_unit_name());
2184 });
2185}
2186
2187#[cfg(target_os = "linux")]
2194fn transient_scope_unit_name() -> String {
2195 static SERIAL: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
2196 let serial = SERIAL.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2197 format!("murmur-{}-{serial}.scope", std::process::id())
2198}
2199
2200#[cfg(target_os = "linux")]
2203fn request_transient_scope(unit: &str) -> Result<(), String> {
2204 let owned_unit = unit.to_string();
2207 std::thread::Builder::new()
2208 .name("murmur-cgroup-scope".to_string())
2209 .spawn(move || start_transient_scope(&owned_unit))
2210 .map_err(|error| format!("could not spawn the D-Bus thread: {error}"))?
2211 .join()
2212 .map_err(|_| "the D-Bus thread panicked".to_string())??;
2213
2214 await_scope_membership(unit)
2215}
2216
2217#[cfg(target_os = "linux")]
2233fn start_transient_scope(unit: &str) -> Result<(), String> {
2234 use zbus::Variant;
2235
2236 let properties: Vec<(&str, Variant<'_>)> = vec![
2237 (
2238 "Description",
2239 Variant::from("Murmur capsule subprocess scope"),
2240 ),
2241 ("PIDs", Variant::from(vec![std::process::id()])),
2242 ("Delegate", Variant::from(true)),
2243 ("CollectMode", Variant::from("inactive-or-failed")),
2244 ];
2245 let aux: Vec<(&str, Vec<(&str, Variant<'_>)>)> = Vec::new();
2247
2248 zbus::block_on(async {
2249 let connection = zbus::Connection::session()
2250 .await
2251 .map_err(|error| format!("no systemd session bus: {error}"))?;
2252 connection
2253 .call_method(
2254 Some("org.freedesktop.systemd1"),
2255 "/org/freedesktop/systemd1",
2256 Some("org.freedesktop.systemd1.Manager"),
2257 "StartTransientUnit",
2258 &(unit, "fail", properties, aux),
2261 )
2262 .await
2263 .map(|_| ())
2264 .map_err(|error| format!("systemd refused to start {unit}: {error}"))
2265 })
2266}
2267
2268#[cfg(target_os = "linux")]
2282fn await_scope_membership(unit: &str) -> Result<(), String> {
2283 let deadline = std::time::Instant::now() + Duration::from_secs(2);
2284 loop {
2285 let current = std::fs::read_to_string("/proc/self/cgroup").unwrap_or_default();
2286 if let Some(path) = parse_unified_cgroup_path(¤t) {
2287 if path.rsplit('/').next() == Some(unit) {
2288 return Ok(());
2289 }
2290 }
2291 if std::time::Instant::now() >= deadline {
2292 return Err(format!(
2293 "systemd accepted the request for {unit} but this process is not in it"
2294 ));
2295 }
2296 std::thread::sleep(Duration::from_millis(1));
2297 }
2298}
2299
2300#[cfg(target_os = "linux")]
2308fn probe_delegation() -> Result<PathBuf, CgroupError> {
2309 let self_cgroup = std::fs::read_to_string("/proc/self/cgroup").map_err(|error| {
2310 CgroupError::io(format!("could not read /proc/self/cgroup: {error}"), &error)
2311 })?;
2312 let relative = parse_unified_cgroup_path(&self_cgroup).ok_or_else(|| {
2313 "this host has no cgroup v2 unified hierarchy (no `0::` line in /proc/self/cgroup); \
2314 cgroup v1-only and hybrid hosts cannot be delegated a v2 scope"
2315 .to_string()
2316 })?;
2317
2318 let mount = std::fs::read_to_string("/proc/mounts")
2319 .map(|contents| parse_cgroup2_mount(&contents))
2320 .unwrap_or_else(|_| DEFAULT_CGROUP2_MOUNT.to_string());
2321
2322 let base = ascend_out_of_supervisor_leaves(
2323 PathBuf::from(mount).join(relative.trim_start_matches('/')),
2324 );
2325 if !base.is_dir() {
2326 return Err(format!(
2327 "delegated cgroup directory {} does not exist",
2328 base.display()
2329 )
2330 .into());
2331 }
2332
2333 let controllers =
2334 std::fs::read_to_string(base.join("cgroup.controllers")).map_err(|error| {
2335 CgroupError::io(
2336 format!(
2337 "could not read {}/cgroup.controllers: {error}",
2338 base.display()
2339 ),
2340 &error,
2341 )
2342 })?;
2343 let unavailable = missing_controllers(&controllers, REQUIRED_CONTROLLERS);
2344 if !unavailable.is_empty() {
2345 return Err(format!(
2346 "cgroup controllers [{}] are not delegated to {}; the unit `mur` runs under needs \
2347 `Delegate=yes` (or `Delegate=memory pids cpu io`)",
2348 unavailable.join(", "),
2349 base.display()
2350 )
2351 .into());
2352 }
2353
2354 let probe_dir = base.join(format!("murmur-delegation-probe-{}", std::process::id()));
2358 let _ = std::fs::remove_dir(&probe_dir);
2359 std::fs::create_dir(&probe_dir).map_err(|error| {
2360 CgroupError::io(
2361 format!(
2362 "cannot create a child cgroup under {} ({error}); the unit `mur` runs under needs \
2363 `Delegate=yes` for memory, pids, cpu and io",
2364 base.display()
2365 ),
2366 &error,
2367 )
2368 })?;
2369 let _ = std::fs::remove_dir(&probe_dir);
2370
2371 Ok(base)
2372}
2373
2374#[cfg(target_os = "linux")]
2381fn enable_controllers(base: &Path) -> Result<(), CgroupError> {
2382 let subtree_control = base.join("cgroup.subtree_control");
2383 let current = std::fs::read_to_string(&subtree_control).unwrap_or_default();
2384
2385 let available = std::fs::read_to_string(base.join("cgroup.controllers")).unwrap_or_default();
2388 let mut wanted: Vec<&str> = REQUIRED_CONTROLLERS.to_vec();
2389 if available.split_whitespace().any(|c| c == "io") {
2390 wanted.push("io");
2391 }
2392
2393 let missing = missing_controllers(¤t, &wanted);
2394 if missing.is_empty() {
2395 return Ok(());
2396 }
2397 let directive = missing
2398 .iter()
2399 .map(|controller| format!("+{controller}"))
2400 .collect::<Vec<_>>()
2401 .join(" ");
2402
2403 if std::fs::write(&subtree_control, format!("{directive}\n")).is_ok() {
2404 return Ok(());
2405 }
2406
2407 move_self_to_supervisor_leaf(base)?;
2408
2409 let deadline = std::time::Instant::now() + Duration::from_secs(2);
2420 let mut last_error;
2423 loop {
2424 let current = std::fs::read_to_string(&subtree_control).unwrap_or_default();
2425 if missing_controllers(¤t, &wanted).is_empty() {
2426 return Ok(());
2427 }
2428 match std::fs::write(&subtree_control, format!("{directive}\n")) {
2429 Ok(()) => return Ok(()),
2430 Err(error) => last_error = Some(error),
2431 }
2432 if std::time::Instant::now() >= deadline {
2433 break;
2434 }
2435 std::thread::sleep(Duration::from_millis(20));
2436 }
2437
2438 Err(CgroupError {
2439 errno: last_error.as_ref().and_then(std::io::Error::raw_os_error),
2440 message: format!(
2441 "could not enable controllers [{}] in {} ({}); the unit `mur` runs under needs \
2442 `Delegate=yes` for memory, pids, cpu and io",
2443 missing.join(", "),
2444 subtree_control.display(),
2445 last_error.map_or_else(|| "still occupied".to_string(), |error| error.to_string()),
2446 ),
2447 })
2448}
2449
2450#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
2461fn ascend_out_of_supervisor_leaves(mut base: PathBuf) -> PathBuf {
2462 while base.file_name().and_then(|name| name.to_str()) == Some(SUPERVISOR_LEAF) {
2463 base.pop();
2464 }
2465 base
2466}
2467
2468#[cfg(target_os = "linux")]
2470fn move_self_to_supervisor_leaf(base: &Path) -> Result<(), CgroupError> {
2471 let leaf = base.join(SUPERVISOR_LEAF);
2472 if let Err(error) = std::fs::create_dir(&leaf) {
2473 if error.kind() != std::io::ErrorKind::AlreadyExists {
2474 return Err(CgroupError::io(
2475 format!(
2476 "could not create {} to vacate the delegated cgroup: {error}",
2477 leaf.display()
2478 ),
2479 &error,
2480 ));
2481 }
2482 }
2483 std::fs::write(
2484 leaf.join("cgroup.procs"),
2485 format!("{}\n", std::process::id()),
2486 )
2487 .map_err(|error| {
2488 CgroupError::io(
2489 format!(
2490 "could not move this process into {} to vacate the delegated cgroup: {error}",
2491 leaf.display()
2492 ),
2493 &error,
2494 )
2495 })
2496}
2497
2498#[cfg(target_os = "linux")]
2504#[allow(unsafe_code)]
2505fn open_write_only(path: &Path) -> std::io::Result<std::os::fd::OwnedFd> {
2506 use std::os::unix::fs::OpenOptionsExt;
2507
2508 std::fs::OpenOptions::new()
2509 .write(true)
2510 .custom_flags(libc::O_CLOEXEC)
2511 .open(path)
2512 .map(std::os::fd::OwnedFd::from)
2513}
2514
2515pub fn cgroup_delegation_available() -> bool {
2530 #[cfg(not(target_os = "linux"))]
2531 {
2532 true
2533 }
2534
2535 #[cfg(target_os = "linux")]
2536 {
2537 delegated_base().is_ok()
2543 }
2544}
2545
2546#[must_use]
2555pub fn io_max_device_available(path: &Path) -> bool {
2556 #[cfg(not(target_os = "linux"))]
2557 {
2558 let _ = path;
2559 false
2560 }
2561
2562 #[cfg(target_os = "linux")]
2563 {
2564 io_max_device(path).is_ok()
2565 }
2566}
2567
2568pub fn skip_without_host_support(test_name: &str) -> bool {
2576 if crate::network_namespace::skip_without_egress_namespace(test_name) {
2577 return true;
2578 }
2579 if !cgroup_delegation_available() {
2580 crate::runtime_err!(
2581 "[SKIP-HOST] {test_name}: this host cannot delegate a cgroup v2 scope, so a capsule \
2582 that can spawn subprocesses refuses to launch with E-RUN-012 before anything \
2583 this test observes happens -- see {}",
2584 murmur_artifact::docs_reference_url!("resource-limits/#platform-behavior")
2585 );
2586 return true;
2587 }
2588 false
2589}
2590
2591#[cfg(test)]
2592mod tests {
2593 use super::*;
2594 use crate::types::CapabilityPolicy;
2595
2596 #[test]
2600 fn io_max_status_wire_names_are_kebab_case() {
2601 let spellings: Vec<String> = [
2602 IoMaxStatus::Enforced,
2603 IoMaxStatus::Unavailable,
2604 IoMaxStatus::NotRequired,
2605 IoMaxStatus::NotProbed,
2606 ]
2607 .into_iter()
2608 .map(|status| {
2609 serde_json::to_value(status)
2610 .unwrap()
2611 .as_str()
2612 .unwrap()
2613 .to_string()
2614 })
2615 .collect();
2616 assert_eq!(
2617 spellings,
2618 vec!["enforced", "unavailable", "not-required", "not-probed"]
2619 );
2620 }
2621
2622 #[test]
2625 fn an_unfilled_report_claims_nothing() {
2626 let report = IoMaxReport::default();
2627 assert_eq!(report.status, IoMaxStatus::NotProbed);
2628 assert_eq!(report.declared_bytes_per_sec, 0);
2629 assert_eq!(report.reason, None);
2630 assert_eq!(io_max_warning(&report, false), None);
2631 }
2632
2633 #[test]
2635 fn only_a_failed_write_omits_nothing_and_a_successful_one_explains_nothing() {
2636 let enforced = serde_json::to_value(IoMaxReport::enforced(1_000)).unwrap();
2637 assert_eq!(enforced["status"], "enforced");
2638 assert_eq!(enforced["declared_bytes_per_sec"], 1_000);
2639 assert!(
2640 enforced.get("reason").is_none(),
2641 "an applied ceiling has nothing to explain: {enforced}"
2642 );
2643
2644 let unavailable =
2645 serde_json::to_value(IoMaxReport::unavailable(1_000, "no io controller")).unwrap();
2646 assert_eq!(unavailable["status"], "unavailable");
2647 assert_eq!(unavailable["reason"], "no io controller");
2648 }
2649
2650 #[test]
2654 fn the_warning_fires_only_where_a_declared_ceiling_did_not_apply() {
2655 assert_eq!(io_max_warning(&IoMaxReport::enforced(4_096), true), None);
2656 assert_eq!(
2657 io_max_warning(&IoMaxReport::not_required(4_096, NO_SCOPE_REASON), true),
2658 None
2659 );
2660
2661 let unavailable = IoMaxReport::unavailable(4_096, "device 0:24 is not a block device");
2662 let (code, message) =
2663 io_max_warning(&unavailable, true).expect("an unapplied ceiling must be reported");
2664 assert_eq!(code, murmur_artifact::W_SEC_021);
2665 assert!(message.contains("4096 bytes/s"), "message was: {message}");
2666 assert!(
2667 message.contains("device 0:24 is not a block device"),
2668 "message was: {message}"
2669 );
2670 assert!(
2671 message.contains("memory.max, pids.max and cpu.max are still enforced on the scope"),
2672 "message was: {message}"
2673 );
2674 }
2675
2676 #[test]
2679 fn the_warning_names_cpu_max_only_when_a_cpu_cap_is_set() {
2680 let unavailable = IoMaxReport::unavailable(4_096, "no io controller");
2681 let (_, message) =
2682 io_max_warning(&unavailable, false).expect("an unapplied ceiling must be reported");
2683 assert!(
2684 message.contains("memory.max and pids.max are still enforced on the scope"),
2685 "message was: {message}"
2686 );
2687 assert!(!message.contains("cpu.max"), "message was: {message}");
2688 }
2689
2690 #[cfg(target_os = "linux")]
2693 #[test]
2694 fn the_device_is_resolved_from_the_nearest_existing_ancestor() {
2695 let temp = tempfile::tempdir().unwrap();
2696 let missing = temp
2697 .path()
2698 .join("workdir")
2699 .join("does")
2700 .join("not")
2701 .join("exist");
2702 assert_eq!(
2703 nearest_existing_ancestor(&missing),
2704 Some(temp.path()),
2705 "an ancestor of a tempdir always exists"
2706 );
2707 assert_eq!(
2708 io_max_device(&missing).ok(),
2709 io_max_device(temp.path()).ok(),
2710 "a path that does not exist yet must resolve to its filesystem's device"
2711 );
2712 assert!(!missing.exists(), "resolving a device must create nothing");
2713 }
2714
2715 #[test]
2718 fn the_covering_mount_is_the_deepest_one() {
2719 let mountinfo = "\
272025 30 0:29 / / rw,relatime shared:1 - btrfs /dev/nvme0n1p5 rw,subvol=/
272126 25 0:31 / /dev/shm rw,nosuid shared:2 - tmpfs tmpfs rw,inode64
272227 25 0:41 / /space rw,relatime shared:3 - btrfs /dev/nvme0n1p7 rw,subvol=/
2723";
2724 let covering = |path: &str| mount_covering(mountinfo, Path::new(path)).unwrap();
2725
2726 assert_eq!(
2727 covering("/space/project/workdir").source,
2728 PathBuf::from("/dev/nvme0n1p7")
2729 );
2730 assert_eq!(covering("/dev/shm/project").source, PathBuf::from("tmpfs"));
2731 assert_eq!(
2732 covering("/home/someone").source,
2733 PathBuf::from("/dev/nvme0n1p5"),
2734 "a path under no nested mount belongs to the root filesystem"
2735 );
2736 assert_eq!(
2737 covering("/spaceship").source,
2738 PathBuf::from("/dev/nvme0n1p5"),
2739 "mount points match whole path components, not string prefixes"
2740 );
2741 }
2742
2743 #[test]
2746 fn a_mount_over_another_at_the_same_point_wins() {
2747 let mountinfo = "\
274825 30 0:29 / / rw shared:1 - ext4 /dev/sda1 rw
274926 25 0:31 / /mnt rw shared:2 - ext4 /dev/sdb1 rw
275027 25 0:32 / /mnt rw shared:3 - ext4 /dev/sdc1 rw
2751";
2752 assert_eq!(
2753 mount_covering(mountinfo, Path::new("/mnt/data"))
2754 .unwrap()
2755 .source,
2756 PathBuf::from("/dev/sdc1")
2757 );
2758 }
2759
2760 #[test]
2763 fn mount_point_escapes_are_decoded() {
2764 let mountinfo = "\
276525 30 0:29 / /media/a\\040disk rw shared:1 - ext4 /dev/sdb1 rw
2766";
2767 let entry = mount_covering(mountinfo, Path::new("/media/a disk/project")).unwrap();
2768 assert_eq!(entry.mount_point, PathBuf::from("/media/a disk"));
2769 assert_eq!(entry.source, PathBuf::from("/dev/sdb1"));
2770 assert_eq!(unescape_octal("no\\134escape"), PathBuf::from("no\\escape"));
2771 assert_eq!(
2772 unescape_octal("\\04"),
2773 PathBuf::from("\\04"),
2774 "a truncated escape is not an escape"
2775 );
2776 }
2777
2778 #[test]
2780 fn sysfs_dev_attributes_are_read_as_major_minor() {
2781 assert_eq!(parse_dev_line("259:0\n"), Some((259, 0)));
2782 assert_eq!(parse_dev_line(""), None);
2783 assert_eq!(parse_dev_line("259"), None);
2784 }
2785
2786 #[cfg(target_os = "linux")]
2790 #[test]
2791 fn the_resolved_device_is_never_a_partition() {
2792 let Ok((major, minor)) = io_max_device(Path::new(".")) else {
2793 return;
2796 };
2797 let sysfs = PathBuf::from(format!("/sys/dev/block/{major}:{minor}"));
2798 assert!(
2799 !sysfs.join("partition").exists(),
2800 "io.max was given the partition {major}:{minor}, which the block layer refuses"
2801 );
2802 }
2803
2804 #[cfg(target_os = "linux")]
2807 #[test]
2808 fn probing_io_max_creates_nothing_that_survives_the_call() {
2809 if skip_without_host_support("probing_io_max_creates_nothing_that_survives_the_call") {
2810 return;
2811 }
2812 let temp = tempfile::tempdir().unwrap();
2813 let probe = probe_scope(
2814 true,
2815 &HostResourceLimits::default(),
2816 &temp.path().join("workdir"),
2817 );
2818 assert_eq!(probe.cgroup_scope, CgroupScopeReport::delegated());
2819 let report = probe.io_max;
2820 assert!(
2821 matches!(
2822 report.status,
2823 IoMaxStatus::Enforced | IoMaxStatus::Unavailable
2824 ),
2825 "a performed probe must report what it performed, got {report:?}"
2826 );
2827
2828 let leftovers: Vec<String> = std::fs::read_dir(delegated_base().expect("a delegated base"))
2832 .unwrap()
2833 .filter_map(|entry| Some(entry.ok()?.file_name().to_string_lossy().into_owned()))
2834 .filter(|name| name.contains("explain-scope"))
2835 .collect();
2836 assert!(
2837 leftovers.is_empty(),
2838 "the throwaway cgroup outlived the probe: {leftovers:?}"
2839 );
2840 }
2841
2842 #[test]
2845 fn a_probe_that_is_not_required_reports_what_a_launch_writes() {
2846 let limits = HostResourceLimits::default();
2847 let probe = probe_scope(false, &limits, Path::new("/nonexistent/workdir"));
2848 let prepared = prepare_scope(false, &limits, "s", Path::new("/nonexistent/workdir"))
2849 .expect("no scope is required");
2850 assert_eq!(probe.cgroup_scope, prepared.cgroup_scope);
2851 assert_eq!(probe.io_max, prepared.io_max);
2852 assert_eq!(probe.cgroup_scope.status, CgroupScopeStatus::NotRequired);
2853 assert_eq!(
2854 serde_json::to_value(&probe.cgroup_scope).unwrap(),
2855 serde_json::json!({"status": "not-required", "reason": NO_CGROUP_SCOPE_REASON})
2856 );
2857 }
2858
2859 #[test]
2863 fn the_cgroup_scope_report_serializes_each_status() {
2864 let error = CgroupError::io(
2865 "cannot create a child cgroup under /sys/fs/cgroup/x (Read-only file system (os \
2866 error 30))"
2867 .to_string(),
2868 &std::io::Error::from_raw_os_error(libc::EROFS),
2869 );
2870 assert_eq!(error.to_string(), error.message);
2871 assert_eq!(String::from(error.clone()), error.message);
2872 let unavailable = CgroupScopeReport::unavailable(&error);
2873 assert_eq!(
2874 serde_json::to_value(&unavailable).unwrap(),
2875 serde_json::json!({
2876 "status": "unavailable",
2877 "errno": "EROFS",
2878 "reason": error.message,
2879 })
2880 );
2881 assert_eq!(
2882 render_cgroup_scope(&unavailable),
2883 format!(
2884 "cgroup scope: unavailable (EROFS) \u{2014} {}",
2885 error.message
2886 )
2887 );
2888 let delegated = CgroupScopeReport::delegated();
2889 assert_eq!(
2890 serde_json::to_value(&delegated).unwrap(),
2891 serde_json::json!({"status": "delegated"})
2892 );
2893 assert_eq!(render_cgroup_scope(&delegated), "cgroup scope: delegated");
2894 assert_eq!(
2895 serde_json::to_value(CgroupScopeReport::default()).unwrap(),
2896 serde_json::json!({"status": "not-probed"})
2897 );
2898 let no_errno = CgroupScopeReport::unavailable(&CgroupError::from("no v2".to_string()));
2899 assert_eq!(no_errno.errno, None);
2900 }
2901
2902 #[cfg(target_os = "linux")]
2905 #[test]
2906 fn a_prepared_scope_reports_delegated() {
2907 if skip_without_host_support("a_prepared_scope_reports_delegated") {
2908 return;
2909 }
2910 let temp = tempfile::tempdir().unwrap();
2911 let prepared = prepare_scope(
2912 true,
2913 &HostResourceLimits::default(),
2914 "cgroup-scope-report",
2915 &temp.path().join("workdir"),
2916 )
2917 .expect("a delegated host creates the scope");
2918 assert!(prepared.scope.is_some());
2919 assert_eq!(prepared.cgroup_scope, CgroupScopeReport::delegated());
2920 }
2921
2922 #[test]
2923 fn scope_is_required_by_any_route_to_a_subprocess() {
2924 let shell = CapabilityPolicy {
2925 shell_allow: vec!["bash".to_string()],
2926 ..CapabilityPolicy::default()
2927 };
2928 let spawn = CapabilityPolicy {
2929 spawn_allow: vec!["child".to_string()],
2930 ..CapabilityPolicy::default()
2931 };
2932 let install_tool = CapabilityPolicy {
2933 install_tool: vec!["local/tool-*".to_string()],
2934 ..CapabilityPolicy::default()
2935 };
2936 let install_skill = CapabilityPolicy {
2937 install_skill: vec!["local/skill-*".to_string()],
2938 ..CapabilityPolicy::default()
2939 };
2940 let bare = CapabilityPolicy::default();
2941
2942 assert!(requires_process_bounding(&shell));
2943 assert!(requires_process_bounding(&spawn));
2944 assert!(
2945 !requires_process_bounding(&install_tool),
2946 "a pulled tool runs a program only through murmur:process, which obeys shell.allow"
2947 );
2948 assert!(
2949 !requires_process_bounding(&install_skill),
2950 "a pulled skill gains no grant and runs no subprocess"
2951 );
2952 for policy in [&shell, &spawn, &install_tool, &install_skill, &bare] {
2953 assert_eq!(
2954 requires_process_bounding(policy),
2955 crate::runtime::has_subprocess_route_at_staging(policy),
2956 "{policy:?}"
2957 );
2958 }
2959 assert!(
2960 !requires_process_bounding(&bare),
2961 "a WASM-only capsule has no process tree to bound and must not be blocked"
2962 );
2963 }
2964
2965 #[test]
2968 fn an_unconfined_session_requires_a_scope_only_for_a_declared_cgroup_field() {
2969 use murmur_artifact::{ContainmentClass, ResourceCapabilities};
2970 let shell = |containment_floor, declared_resources| crate::types::CapabilityPolicy {
2971 shell_allow: vec!["bash".to_string()],
2972 containment_floor,
2973 declared_resources,
2974 ..Default::default()
2975 };
2976 assert!(requires_process_bounding(&shell(
2977 ContainmentClass::Advisory,
2978 ResourceCapabilities::default()
2979 )));
2980 assert!(!requires_process_bounding(&shell(
2981 ContainmentClass::Unconfined,
2982 ResourceCapabilities::default()
2983 )));
2984 assert!(!requires_process_bounding(&shell(
2985 ContainmentClass::Unconfined,
2986 ResourceCapabilities {
2987 max_open_files: Some(256),
2988 workdir_max_bytes: Some(1 << 20),
2989 ..Default::default()
2990 }
2991 )));
2992 for declared in [
2993 ResourceCapabilities {
2994 cgroup_memory_bytes: Some(1 << 30),
2995 ..Default::default()
2996 },
2997 ResourceCapabilities {
2998 cgroup_pids_max: Some(64),
2999 ..Default::default()
3000 },
3001 ResourceCapabilities {
3002 cgroup_cpu_percent: Some(50),
3003 ..Default::default()
3004 },
3005 ResourceCapabilities {
3006 cgroup_io_bytes_per_sec: Some(1 << 20),
3007 ..Default::default()
3008 },
3009 ] {
3010 assert!(
3011 requires_process_bounding(&shell(ContainmentClass::Unconfined, declared)),
3012 "{declared:?}"
3013 );
3014 let bare = crate::types::CapabilityPolicy {
3015 containment_floor: ContainmentClass::Unconfined,
3016 declared_resources: declared,
3017 ..Default::default()
3018 };
3019 assert!(
3020 !requires_process_bounding(&bare),
3021 "no subprocess route, no scope"
3022 );
3023 }
3024
3025 let temp = tempfile::tempdir().unwrap();
3027 let prepared = prepare_policy_scope(
3028 &shell(
3029 ContainmentClass::Unconfined,
3030 ResourceCapabilities::default(),
3031 ),
3032 "ses_unconfined",
3033 temp.path(),
3034 )
3035 .expect("an unconfined session with no cgroup field is never refused");
3036 assert!(prepared.scope.is_none());
3037 assert_eq!(prepared.io_max.status, IoMaxStatus::NotRequired);
3038 assert_eq!(
3039 prepared.io_max.reason.as_deref(),
3040 Some(UNCONFINED_NO_SCOPE_REASON)
3041 );
3042 }
3043
3044 #[test]
3046 fn an_unconfined_scope_writes_only_the_declared_fields() {
3047 use murmur_artifact::ResourceCapabilities;
3048 assert!(declared_scope_limits(&ResourceCapabilities::default()).is_empty());
3049 let files = |declared: ResourceCapabilities| -> Vec<(&str, String)> {
3050 declared_scope_limits(&declared)
3051 .into_iter()
3052 .map(|limit| (limit.file, limit.value))
3053 .collect()
3054 };
3055 assert_eq!(
3056 files(ResourceCapabilities {
3057 cgroup_pids_max: Some(64),
3058 max_open_files: Some(256),
3059 ..Default::default()
3060 }),
3061 vec![("pids.max", "64".to_string())]
3062 );
3063 assert_eq!(
3064 files(ResourceCapabilities {
3065 cgroup_memory_bytes: Some(1024),
3066 cgroup_cpu_percent: Some(50),
3067 ..Default::default()
3068 }),
3069 vec![
3070 ("memory.max", "1024".to_string()),
3071 ("cpu.max", format!("50000 {CPU_MAX_PERIOD_US}")),
3072 ]
3073 );
3074 }
3075
3076 #[test]
3080 fn prepare_scope_returns_none_when_bounding_is_not_required() {
3081 let temp = tempfile::tempdir().unwrap();
3082 let scope = prepare_scope(
3083 false,
3084 &HostResourceLimits::default(),
3085 "ses_test",
3086 temp.path(),
3087 )
3088 .expect("a capsule with no subprocess capability must never be refused");
3089 assert!(scope.scope.is_none());
3090 assert_eq!(scope.io_max.status, IoMaxStatus::NotRequired);
3092 assert!(
3093 scope.io_max.reason.is_some(),
3094 "not-required must say which cause"
3095 );
3096 assert_eq!(io_max_warning(&scope.io_max, false), None);
3097 }
3098
3099 #[cfg(not(target_os = "linux"))]
3102 #[test]
3103 fn prepare_scope_is_never_an_error_off_linux() {
3104 let temp = tempfile::tempdir().unwrap();
3105 let scope = prepare_scope(
3106 true,
3107 &HostResourceLimits::default(),
3108 "ses_test",
3109 temp.path(),
3110 )
3111 .expect("a non-Linux host must not refuse to launch for a missing cgroup");
3112 assert!(scope.scope.is_none());
3113 assert_eq!(scope.io_max.status, IoMaxStatus::NotRequired);
3114 assert!(scope
3115 .io_max
3116 .reason
3117 .as_deref()
3118 .expect("not-required off Linux must say cgroups cannot exist here")
3119 .contains("Linux"));
3120 }
3121
3122 #[cfg(target_os = "linux")]
3127 #[test]
3128 #[allow(clippy::print_stdout)]
3129 fn a_required_scope_is_created_whatever_cgroup_this_process_inherited() {
3130 if skip_without_host_support(
3131 "a_required_scope_is_created_whatever_cgroup_this_process_inherited",
3132 ) {
3133 return;
3134 }
3135 let temp = tempfile::tempdir().unwrap();
3136 let prepared = prepare_scope(
3137 true,
3138 &HostResourceLimits::default(),
3139 "ses_selftest",
3140 temp.path(),
3141 )
3142 .expect("a Linux host with a systemd user session must be able to bound a subprocess tree");
3143 let io_max = prepared.io_max.clone();
3144 let scope = prepared
3145 .scope
3146 .expect("a required scope on Linux is never `None`");
3147
3148 for file in ["memory.max", "pids.max"] {
3149 let contents = std::fs::read_to_string(scope.path().join(file))
3150 .unwrap_or_else(|error| panic!("{file} unreadable in the scope: {error}"));
3151 assert!(!contents.trim().is_empty(), "{file} was left unset");
3152 assert_ne!(contents.trim(), "max", "{file} carries no ceiling");
3153 }
3154 let cpu_max = std::fs::read_to_string(scope.path().join("cpu.max"))
3156 .unwrap_or_else(|error| panic!("cpu.max unreadable in the scope: {error}"));
3157 assert_eq!(cpu_max.trim(), "max 100000");
3158 match std::fs::read_to_string(scope.path().join("memory.swap.max")) {
3159 Ok(contents) => {
3160 assert_eq!(
3161 contents.trim(),
3162 SCOPE_SWAP_MAX,
3163 "memory.swap.max was not bounded"
3164 );
3165 assert_eq!(contents.trim(), "0");
3166 println!(
3167 "memory.swap.max in the created scope reads {}",
3168 contents.trim()
3169 );
3170 }
3171 Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
3172 assert_eq!(probe_swap_control(), SwapControl::Absent);
3173 println!("this kernel exposes no memory.swap.max; swap is not bounded here");
3174 }
3175 Err(error) => panic!("memory.swap.max unreadable in the scope: {error}"),
3176 }
3177
3178 assert!(
3181 matches!(
3182 io_max.status,
3183 IoMaxStatus::Enforced | IoMaxStatus::Unavailable
3184 ),
3185 "a created scope must report a performed io.max write, got {io_max:?}"
3186 );
3187 assert_eq!(
3188 io_max.declared_bytes_per_sec,
3189 HostResourceLimits::default().cgroup_io_bytes_per_sec
3190 );
3191 if io_max.status == IoMaxStatus::Unavailable {
3192 assert!(io_max.reason.is_some(), "unavailable must carry a reason");
3193 assert!(io_max_warning(&io_max, false).is_some());
3194 } else {
3195 assert_eq!(io_max.reason, None);
3196 assert_eq!(io_max_warning(&io_max, false), None);
3197 }
3198 }
3199
3200 #[cfg(target_os = "linux")]
3202 #[test]
3203 fn a_declared_cpu_cap_is_written_to_the_scope() {
3204 if skip_without_host_support("a_declared_cpu_cap_is_written_to_the_scope") {
3205 return;
3206 }
3207 let temp = tempfile::tempdir().unwrap();
3208 let limits = HostResourceLimits {
3209 cgroup_cpu_percent: Some(50),
3210 ..HostResourceLimits::default()
3211 };
3212 let scope = prepare_scope(true, &limits, "ses_cpucap", temp.path())
3213 .expect(
3214 "a Linux host with a systemd user session must be able to bound a subprocess tree",
3215 )
3216 .scope
3217 .expect("a required scope on Linux is never `None`");
3218 let cpu_max = std::fs::read_to_string(scope.path().join("cpu.max"))
3219 .unwrap_or_else(|error| panic!("cpu.max unreadable in the scope: {error}"));
3220 assert_eq!(cpu_max.trim(), "50000 100000");
3221 }
3222
3223 #[cfg(target_os = "linux")]
3227 #[test]
3228 #[allow(clippy::print_stdout)]
3229 fn killing_a_scope_without_cgroup_kill_empties_it() {
3230 if skip_without_host_support("killing_a_scope_without_cgroup_kill_empties_it") {
3231 return;
3232 }
3233 let temp = tempfile::tempdir().unwrap();
3234 let scope = prepare_scope(
3235 true,
3236 &HostResourceLimits::default(),
3237 "ses_kill_fallback",
3238 temp.path(),
3239 )
3240 .expect("a delegated scope")
3241 .scope
3242 .expect("a required scope on Linux is never None");
3243 let enforcement = crate::sandbox::ShellEnforcement {
3244 cgroup_scope: Some(Arc::clone(&scope)),
3245 ..crate::sandbox::ShellEnforcement::environment_only().with_test_session_dirs()
3246 };
3247 let mut command = std::process::Command::new("bash");
3248 command
3249 .args([
3250 "-c",
3251 "setsid sleep 300 >/dev/null 2>&1 & sleep 300 >/dev/null 2>&1 & wait",
3252 ])
3253 .stdout(std::process::Stdio::null())
3254 .stderr(std::process::Stdio::null());
3255 crate::sandbox::attach_process_limits(&mut command, &enforcement);
3256 let mut child = command.spawn().expect("bash spawns into the scope");
3257
3258 let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
3259 while scope.member_pids().len() < 3 {
3260 assert!(
3261 std::time::Instant::now() < deadline,
3262 "the scope never held bash and both sleepers: {:?}",
3263 scope.member_pids()
3264 );
3265 std::thread::sleep(std::time::Duration::from_millis(20));
3266 }
3267 let members = scope.member_pids();
3268
3269 FORCE_KILL_FALLBACK.with(|flag| flag.set(true));
3270 let killed = scope.kill_all();
3271 FORCE_KILL_FALLBACK.with(|flag| flag.set(false));
3272 let emptied = scope.wait_until_empty(std::time::Duration::from_secs(2));
3273 let status = child.wait().expect("bash is reaped");
3274 println!(
3275 "members before: {members:?}; fallback kill: {killed:?}; empty: {emptied}; \
3276 cgroup.procs after: {:?}; bash: {status}",
3277 scope.member_pids()
3278 );
3279 killed.expect("the fallback kill succeeds");
3280 assert!(emptied, "the scope still holds {:?}", scope.member_pids());
3281 assert_eq!(
3282 std::os::unix::process::ExitStatusExt::signal(&status),
3283 Some(libc::SIGKILL)
3284 );
3285 }
3286
3287 #[cfg(target_os = "linux")]
3290 fn scope_with_sleepers(
3291 session: &str,
3292 fallback: bool,
3293 ) -> (Arc<CgroupScope>, std::process::Child) {
3294 let temp = tempfile::tempdir().unwrap();
3295 FORCE_KILL_FALLBACK.with(|flag| flag.set(fallback));
3296 let scope = prepare_scope(true, &HostResourceLimits::default(), session, temp.path());
3297 FORCE_KILL_FALLBACK.with(|flag| flag.set(false));
3298 let scope = scope
3299 .expect("a delegated scope")
3300 .scope
3301 .expect("a required scope on Linux is never None");
3302 let enforcement = crate::sandbox::ShellEnforcement {
3303 cgroup_scope: Some(Arc::clone(&scope)),
3304 ..crate::sandbox::ShellEnforcement::environment_only().with_test_session_dirs()
3305 };
3306 let mut command = std::process::Command::new("bash");
3307 command
3308 .args([
3309 "-c",
3310 "setsid sleep 300 >/dev/null 2>&1 & sleep 300 >/dev/null 2>&1 & wait",
3311 ])
3312 .stdout(std::process::Stdio::null())
3313 .stderr(std::process::Stdio::null());
3314 crate::sandbox::attach_process_limits(&mut command, &enforcement);
3315 let child = command.spawn().expect("bash spawns into the scope");
3316 let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
3317 while scope.member_pids().len() < 3 {
3318 assert!(
3319 std::time::Instant::now() < deadline,
3320 "the scope never held bash and both sleepers: {:?}",
3321 scope.member_pids()
3322 );
3323 std::thread::sleep(std::time::Duration::from_millis(20));
3324 }
3325 (scope, child)
3326 }
3327
3328 #[cfg(target_os = "linux")]
3329 fn watcher_of(scope: &CgroupScope) -> &ScopeWatcher {
3330 scope
3331 .watcher
3332 .as_ref()
3333 .expect("a scope on Linux has a watcher")
3334 }
3335
3336 #[cfg(target_os = "linux")]
3339 fn assert_a_cut_lifeline_empties_the_scope(test_name: &str, fallback: bool) {
3340 if skip_without_host_support(test_name) {
3341 return;
3342 }
3343 let (scope, mut child) = scope_with_sleepers(test_name, fallback);
3344 let members = scope.member_pids();
3345 watcher_of(&scope).cut_lifeline();
3346 assert!(
3347 scope.wait_until_empty(std::time::Duration::from_secs(5)),
3348 "the watcher left {:?} of {members:?} in the scope",
3349 scope.member_pids()
3350 );
3351 for pid in members {
3352 assert!(
3353 !Path::new(&format!("/proc/{pid}")).exists()
3354 || std::fs::read_to_string(format!("/proc/{pid}/stat"))
3355 .is_ok_and(|stat| stat.contains(") Z ")),
3356 "{pid} is still running"
3357 );
3358 }
3359 let status = child.wait().expect("bash is reaped");
3360 assert_eq!(
3361 std::os::unix::process::ExitStatusExt::signal(&status),
3362 Some(libc::SIGKILL)
3363 );
3364 }
3365
3366 #[cfg(target_os = "linux")]
3367 #[test]
3368 fn scope_watcher_kills_the_scope_through_cgroup_kill_when_the_lifeline_closes() {
3369 assert_a_cut_lifeline_empties_the_scope(
3370 "scope_watcher_kills_the_scope_through_cgroup_kill_when_the_lifeline_closes",
3371 false,
3372 );
3373 }
3374
3375 #[cfg(target_os = "linux")]
3376 #[test]
3377 fn scope_watcher_kills_the_scope_by_freezing_when_the_lifeline_closes() {
3378 assert_a_cut_lifeline_empties_the_scope(
3379 "scope_watcher_kills_the_scope_by_freezing_when_the_lifeline_closes",
3380 true,
3381 );
3382 }
3383
3384 #[cfg(target_os = "linux")]
3388 #[test]
3389 #[allow(unsafe_code)]
3390 fn scope_watcher_holds_only_its_own_descriptors_outside_the_scope() {
3391 if skip_without_host_support(
3392 "scope_watcher_holds_only_its_own_descriptors_outside_the_scope",
3393 ) {
3394 return;
3395 }
3396 let own = tempfile::tempfile().unwrap();
3398 let own_target = std::fs::read_link(format!(
3399 "/proc/self/fd/{}",
3400 std::os::fd::AsRawFd::as_raw_fd(&own)
3401 ))
3402 .unwrap();
3403 let (scope, mut child) = scope_with_sleepers("ses_watcher_fds", false);
3404 let pid = watcher_of(&scope).pid;
3405
3406 let fd_dir = format!("/proc/{pid}/fd");
3407 let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
3408 let targets = loop {
3409 let targets: Vec<(String, PathBuf)> = std::fs::read_dir(&fd_dir)
3410 .unwrap()
3411 .filter_map(Result::ok)
3412 .filter_map(|entry| {
3413 let target = std::fs::read_link(entry.path()).ok()?;
3414 Some((entry.file_name().to_string_lossy().into_owned(), target))
3415 })
3416 .collect();
3417 let settled = targets.iter().all(|(_, target)| {
3418 target == Path::new("/dev/null")
3419 || target.to_string_lossy().starts_with("pipe:")
3420 || target.starts_with(scope.path())
3421 });
3422 if settled || std::time::Instant::now() >= deadline {
3423 break targets;
3424 }
3425 std::thread::sleep(std::time::Duration::from_millis(20));
3426 };
3427 let mut numbers: Vec<u32> = targets
3428 .iter()
3429 .map(|(name, _)| name.parse().unwrap())
3430 .collect();
3431 numbers.sort_unstable();
3432 assert!(numbers.starts_with(&[0, 1, 2]), "{targets:?}");
3433 for (name, target) in &targets {
3434 assert_ne!(target, &own_target, "fd {name}");
3435 if ["0", "1", "2"].contains(&name.as_str()) {
3436 assert_eq!(target, Path::new("/dev/null"), "fd {name}");
3437 } else {
3438 assert!(
3439 target.to_string_lossy().starts_with("pipe:")
3440 || target.starts_with(scope.path()),
3441 "fd {name} -> {}",
3442 target.display()
3443 );
3444 }
3445 }
3446 let pipes = targets
3447 .iter()
3448 .filter(|(_, target)| target.to_string_lossy().starts_with("pipe:"))
3449 .count();
3450 assert_eq!(pipes, 1, "the lifeline is its only pipe: {targets:?}");
3451
3452 assert!(!scope.member_pids().contains(&pid));
3453 assert_eq!(
3455 unsafe { libc::getpgid(pid) },
3456 pid,
3457 "a process group of its own"
3458 );
3459
3460 scope.kill_all().unwrap();
3461 child.wait().unwrap();
3462 }
3463
3464 #[cfg(target_os = "linux")]
3466 #[test]
3467 #[allow(unsafe_code)]
3468 fn scope_watcher_is_reaped_when_the_scope_drops() {
3469 if skip_without_host_support("scope_watcher_is_reaped_when_the_scope_drops") {
3470 return;
3471 }
3472 let (scope, mut child) = scope_with_sleepers("ses_watcher_reaped", false);
3473 let pid = watcher_of(&scope).pid;
3474 let path = scope.path().to_path_buf();
3475 drop(Arc::try_unwrap(scope).expect("the test holds the only reference"));
3476 child.wait().unwrap();
3477 let mut status = 0;
3478 let waited = unsafe { libc::waitpid(pid, &mut status, libc::WNOHANG) };
3480 assert_eq!(waited, -1);
3481 assert_eq!(
3482 std::io::Error::last_os_error().raw_os_error(),
3483 Some(libc::ECHILD)
3484 );
3485 assert!(!path.exists(), "the scope directory is removed");
3486 }
3487
3488 #[test]
3491 fn the_scope_limits_bound_swap_at_zero() {
3492 let declared = HostResourceLimits {
3493 cgroup_memory_bytes: 268_435_456,
3494 cgroup_cpu_percent: Some(50),
3495 ..HostResourceLimits::default()
3496 };
3497 for limits in [HostResourceLimits::default(), declared] {
3498 let writes: Vec<(&str, String)> = scope_limits(&limits)
3499 .into_iter()
3500 .map(|limit| (limit.file, limit.value))
3501 .collect();
3502 assert!(
3503 writes.contains(&("memory.swap.max", "0".to_string())),
3504 "writes were {writes:?}"
3505 );
3506 assert!(writes.contains(&("memory.max", limits.cgroup_memory_bytes.to_string())));
3507 assert_eq!(
3508 &writes.iter().map(|(file, _)| *file).collect::<Vec<_>>()[..3],
3509 ["memory.max", "memory.swap.max", "pids.max"]
3510 );
3511 }
3512 let absent_ok: Vec<&str> = scope_limits(&HostResourceLimits::default())
3513 .into_iter()
3514 .filter(|limit| limit.absent_ok)
3515 .map(|limit| limit.file)
3516 .collect();
3517 assert_eq!(absent_ok, ["memory.swap.max"]);
3518 }
3519
3520 #[test]
3523 fn the_scope_limits_write_cpu_max_only_for_a_declared_cap() {
3524 let files: Vec<&str> = scope_limits(&HostResourceLimits::resolve(None))
3525 .into_iter()
3526 .map(|limit| limit.file)
3527 .collect();
3528 assert_eq!(files, ["memory.max", "memory.swap.max", "pids.max"]);
3529
3530 let capped = HostResourceLimits {
3531 cgroup_cpu_percent: Some(50),
3532 ..HostResourceLimits::default()
3533 };
3534 let writes = scope_limits(&capped);
3535 assert_eq!(writes.len(), 4);
3536 assert_eq!(writes[3].file, "cpu.max");
3537 assert_eq!(writes[3].value, "50000 100000");
3538 assert!(!writes[3].absent_ok);
3539 }
3540
3541 #[test]
3542 fn active_swap_areas_are_the_lines_below_the_header() {
3543 let header = "Filename\t\t\t\tType\t\tSize\t\tUsed\t\tPriority\n";
3544 assert_eq!(count_swap_areas(header), 0);
3545 assert_eq!(count_swap_areas(""), 0);
3546 assert_eq!(
3547 count_swap_areas(&format!(
3548 "{header}/swapfile file\t\t33554428\t\t0\t\t-2\n"
3549 )),
3550 1
3551 );
3552 }
3553
3554 #[cfg(target_os = "linux")]
3557 #[test]
3558 fn transient_scope_unit_names_are_murmur_prefixed_and_unique() {
3559 let first = transient_scope_unit_name();
3560 let second = transient_scope_unit_name();
3561 assert!(first.starts_with(&format!("murmur-{}-", std::process::id())));
3562 assert!(first.ends_with(".scope"));
3563 assert_ne!(first, second);
3564 }
3565
3566 #[test]
3567 fn unified_cgroup_path_is_read_from_the_zero_hierarchy_line() {
3568 let v2_only = "0::/user.slice/user-1000.slice/user@1000.service/app.slice/mur.scope\n";
3569 assert_eq!(
3570 parse_unified_cgroup_path(v2_only).as_deref(),
3571 Some("/user.slice/user-1000.slice/user@1000.service/app.slice/mur.scope")
3572 );
3573
3574 let hybrid = "12:pids:/user.slice\n11:memory:/user.slice\n0::/user.slice/session.scope\n";
3575 assert_eq!(
3576 parse_unified_cgroup_path(hybrid).as_deref(),
3577 Some("/user.slice/session.scope")
3578 );
3579
3580 let v1_only = "12:pids:/user.slice\n11:memory:/user.slice\n";
3581 assert_eq!(parse_unified_cgroup_path(v1_only), None);
3582 }
3583
3584 #[test]
3585 fn cgroup2_mount_falls_back_to_the_conventional_path() {
3586 let mounts = "cgroup2 /custom/cgroup cgroup2 rw,nsdelegate 0 0\n";
3587 assert_eq!(parse_cgroup2_mount(mounts), "/custom/cgroup");
3588 assert_eq!(
3589 parse_cgroup2_mount("proc /proc proc rw 0 0\n"),
3590 "/sys/fs/cgroup"
3591 );
3592 }
3593
3594 #[test]
3595 fn missing_controllers_reports_only_what_is_absent() {
3596 assert_eq!(
3597 missing_controllers("cpu memory", REQUIRED_CONTROLLERS),
3598 vec!["pids".to_string()]
3599 );
3600 assert!(missing_controllers("cpu io memory pids", REQUIRED_CONTROLLERS).is_empty());
3601 assert_eq!(
3602 missing_controllers("", REQUIRED_CONTROLLERS),
3603 vec!["memory".to_string(), "pids".to_string(), "cpu".to_string()]
3604 );
3605 }
3606
3607 #[test]
3608 fn event_counters_parse_the_named_key_only() {
3609 let memory_events = "low 0\nhigh 0\nmax 12\noom 3\noom_kill 2\n";
3610 assert_eq!(parse_event_counter(memory_events, "oom_kill"), 2);
3611 assert_eq!(parse_event_counter(memory_events, "max"), 12);
3612 assert_eq!(parse_event_counter("max 7\n", "max"), 7);
3613 assert_eq!(parse_event_counter("", "oom_kill"), 0);
3614 assert_eq!(parse_event_counter("oom_kill\n", "oom_kill"), 0);
3615 }
3616
3617 #[test]
3620 fn attribution_uses_the_delta_across_one_call() {
3621 let before = CgroupEventCounters {
3622 oom_kill: 1,
3623 pids_max: 4,
3624 };
3625 assert_eq!(before.attribution_since(before), None);
3626 assert_eq!(
3627 CgroupEventCounters {
3628 oom_kill: 2,
3629 pids_max: 4
3630 }
3631 .attribution_since(before),
3632 Some("cgroup_memory_bytes")
3633 );
3634 assert_eq!(
3635 CgroupEventCounters {
3636 oom_kill: 1,
3637 pids_max: 5
3638 }
3639 .attribution_since(before),
3640 Some("cgroup_pids_max")
3641 );
3642 }
3643
3644 fn scope_without_kernel() -> CgroupScope {
3647 CgroupScope {
3648 path: PathBuf::new(),
3649 procs_fd: None,
3650 occupancy: Arc::default(),
3651 #[cfg(target_os = "linux")]
3652 watcher: None,
3653 }
3654 }
3655
3656 #[test]
3657 fn occupancy_alone_is_exclusive() {
3658 let scope = scope_without_kernel();
3659 let call = scope.enter();
3660 assert!(call.exclusive());
3661 }
3662
3663 #[test]
3664 fn occupancy_entering_beside_another_is_not_exclusive() {
3665 let scope = scope_without_kernel();
3666 let first = scope.enter();
3667 let second = scope.enter();
3668 assert!(!second.exclusive(), "someone was already inside");
3669 drop(first);
3670 assert!(
3671 !second.exclusive(),
3672 "the sibling leaving does not clean the window it shared"
3673 );
3674 }
3675
3676 #[test]
3677 fn occupancy_someone_entering_during_the_window_makes_it_shared() {
3678 let scope = scope_without_kernel();
3679 let first = scope.enter();
3680 assert!(first.exclusive());
3681 drop(scope.enter());
3682 assert!(
3683 !first.exclusive(),
3684 "a call that entered and left inside this window could have moved the counters"
3685 );
3686 }
3687
3688 #[test]
3689 fn occupancy_drop_releases_the_scope() {
3690 let scope = scope_without_kernel();
3691 drop(scope.enter());
3692 let next = scope.enter();
3693 assert!(
3694 next.exclusive(),
3695 "an earlier call that has left does not share this window"
3696 );
3697 }
3698
3699 #[test]
3702 fn occupancy_guard_outlives_its_borrow_on_another_thread() {
3703 let scope = scope_without_kernel();
3704 let demoted = scope.enter();
3705 let held = std::thread::spawn(move || demoted);
3706 let demoted = held.join().unwrap();
3707 let foreground = scope.enter();
3708 assert!(!foreground.exclusive());
3709 assert!(!demoted.exclusive());
3710 }
3711
3712 #[test]
3713 fn scope_dir_name_is_sanitized_and_never_empty() {
3714 assert!(scope_dir_name("ses_abc123").starts_with("murmur-ses_abc123-"));
3715 assert!(scope_dir_name("../escape").starts_with("murmur-.._escape-"));
3716 assert!(!scope_dir_name("../escape").contains('/'));
3717 assert!(scope_dir_name("").starts_with("murmur-"));
3718 }
3719
3720 #[test]
3724 fn delegated_base_ascends_out_of_inherited_supervisor_leaves() {
3725 let base = PathBuf::from("/sys/fs/cgroup/user.slice/app.slice/mur.scope");
3726
3727 assert_eq!(ascend_out_of_supervisor_leaves(base.clone()), base);
3728 assert_eq!(
3729 ascend_out_of_supervisor_leaves(base.join(SUPERVISOR_LEAF)),
3730 base
3731 );
3732 assert_eq!(
3733 ascend_out_of_supervisor_leaves(base.join(SUPERVISOR_LEAF).join(SUPERVISOR_LEAF)),
3734 base,
3735 "a base already nested by an earlier build must still resolve to the real root"
3736 );
3737 }
3738
3739 #[cfg(target_os = "linux")]
3744 #[test]
3745 fn background_resolution_agrees_with_a_direct_one() {
3746 resolve_delegated_base_in_background();
3747 let first = delegated_base();
3748 let second = delegated_base();
3749 assert_eq!(first, second);
3750 }
3751
3752 #[test]
3756 fn scope_dir_name_is_unique_per_call_for_one_id() {
3757 let first = scope_dir_name("p");
3758 let second = scope_dir_name("p");
3759 assert_ne!(
3760 first, second,
3761 "same plan id must not reuse a scope directory"
3762 );
3763 }
3764
3765 #[test]
3766 fn pid_is_formatted_without_allocating() {
3767 let mut buf = [0u8; 24];
3768 let len = format_pid(4321, &mut buf);
3769 assert_eq!(&buf[..len], b"4321\n");
3770
3771 let len = format_pid(7, &mut buf);
3772 assert_eq!(&buf[..len], b"7\n");
3773 }
3774
3775 #[test]
3776 fn device_numbers_use_the_wide_glibc_encoding() {
3777 assert_eq!(device_major_minor(0x800), (8, 0));
3779 assert_eq!(device_major_minor((259 << 8) | 3), (259, 3));
3781 }
3782}