Skip to content

Commit 9b1c9b5

Browse files
sjenningTaylorMutch
authored andcommitted
feat(policy): accept numeric UIDs in sandbox process identity validation
Allow run_as_user and run_as_group to be either the literal 'sandbox' or a numeric UID/GID within [1000, 2_000_000_000]. This removes the hard dependency on a baked-in 'sandbox' user in container images, enabling compute drivers to inject resolved UIDs at sandbox creation. Phase 1 of #1959. Signed-off-by: Seth Jennings <[email protected]>
1 parent 7e0cce4 commit 9b1c9b5

1 file changed

Lines changed: 207 additions & 5 deletions

File tree

  • crates/openshell-policy/src

‎crates/openshell-policy/src/lib.rs‎

Lines changed: 207 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,41 @@ fn from_proto(policy: &SandboxPolicy) -> PolicyFile {
552552
}
553553
}
554554

555+
// ---------------------------------------------------------------------------
556+
// Sandbox UID/GID constants
557+
// ---------------------------------------------------------------------------
558+
559+
/// Minimum accepted UID for sandbox process identity.
560+
/// UIDs below this are reserved for system users and are rejected.
561+
pub const MIN_SANDBOX_UID: u32 = 1000;
562+
563+
/// Maximum accepted UID for sandbox process identity.
564+
/// UIDs above this exceed typical OS limits and are rejected.
565+
pub const MAX_SANDBOX_UID: u32 = 2_000_000_000;
566+
567+
/// The literal string value accepted as a valid sandbox user/group name.
568+
const SANDBOX_NAME: &str = "sandbox";
569+
570+
/// Validate whether a process identity field value is acceptable.
571+
///
572+
/// Accepts either the literal `"sandbox"` or a numeric UID/GID parsed as
573+
/// `u32` within the range `[MIN_SANDBOX_UID, MAX_SANDBOX_UID]`.
574+
///
575+
/// Rejects:
576+
/// - The empty string (callers should use `ensure_sandbox_process_identity`
577+
/// to fill defaults before validation)
578+
/// - UID 0 or values below `MIN_SANDBOX_UID`
579+
/// - Values above `MAX_SANDBOX_UID`
580+
/// - Non-numeric strings other than `"sandbox"` (e.g. `"root"`, `"nobody"`)
581+
pub fn is_valid_sandbox_identity(value: &str) -> bool {
582+
if value == SANDBOX_NAME {
583+
return true;
584+
}
585+
value.parse::<u32>().is_ok_and(|uid| {
586+
(MIN_SANDBOX_UID..=MAX_SANDBOX_UID).contains(&uid)
587+
})
588+
}
589+
555590
// ---------------------------------------------------------------------------
556591
// Public API
557592
// ---------------------------------------------------------------------------
@@ -725,7 +760,10 @@ impl fmt::Display for PolicyViolation {
725760
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
726761
match self {
727762
Self::InvalidProcessIdentity { field, value } => {
728-
write!(f, "{field} must be 'sandbox', got '{value}'")
763+
write!(
764+
f,
765+
"{field} must be 'sandbox' or a numeric UID/GID in range [{MIN_SANDBOX_UID}, {MAX_SANDBOX_UID}], got '{value}'"
766+
)
729767
}
730768
Self::PathTraversal { path } => {
731769
write!(f, "path contains '..' traversal component: {path}")
@@ -803,17 +841,18 @@ pub fn validate_sandbox_policy(
803841
) -> std::result::Result<(), Vec<PolicyViolation>> {
804842
let mut violations = Vec::new();
805843

806-
// Check process identity — must be "sandbox".
844+
// Check process identity — must be "sandbox" or a numeric UID/GID
845+
// within the acceptable sandbox range.
807846
// `ensure_sandbox_process_identity` should be called before this to
808-
// fill in defaults; anything other than "sandbox" is rejected.
847+
// fill in defaults; any invalid value is rejected.
809848
if let Some(ref process) = policy.process {
810-
if process.run_as_user != "sandbox" {
849+
if !is_valid_sandbox_identity(&process.run_as_user) {
811850
violations.push(PolicyViolation::InvalidProcessIdentity {
812851
field: "run_as_user",
813852
value: process.run_as_user.clone(),
814853
});
815854
}
816-
if process.run_as_group != "sandbox" {
855+
if !is_valid_sandbox_identity(&process.run_as_group) {
817856
violations.push(PolicyViolation::InvalidProcessIdentity {
818857
field: "run_as_group",
819858
value: process.run_as_group.clone(),
@@ -1637,6 +1676,169 @@ network_policies:
16371676
assert!(s.contains("sandbox"));
16381677
}
16391678

1679+
// ---- is_valid_sandbox_identity tests ----
1680+
1681+
#[test]
1682+
fn valid_identity_accepts_sandbox() {
1683+
assert!(is_valid_sandbox_identity("sandbox"));
1684+
}
1685+
1686+
#[test]
1687+
fn valid_identity_accepts_numeric_uid_in_range() {
1688+
assert!(is_valid_sandbox_identity("1000"));
1689+
assert!(is_valid_sandbox_identity("50000"));
1690+
assert!(is_valid_sandbox_identity("1000660000"));
1691+
}
1692+
1693+
#[test]
1694+
fn valid_identity_accepts_boundary_uids() {
1695+
assert!(is_valid_sandbox_identity(&MIN_SANDBOX_UID.to_string()));
1696+
assert!(is_valid_sandbox_identity(&MAX_SANDBOX_UID.to_string()));
1697+
}
1698+
1699+
#[test]
1700+
fn valid_identity_rejects_zero() {
1701+
assert!(!is_valid_sandbox_identity("0"));
1702+
}
1703+
1704+
#[test]
1705+
fn valid_identity_rejects_system_uids_below_min() {
1706+
assert!(!is_valid_sandbox_identity("999"));
1707+
assert!(!is_valid_sandbox_identity("100"));
1708+
assert!(!is_valid_sandbox_identity("1"));
1709+
}
1710+
1711+
#[test]
1712+
fn valid_identity_rejects_uid_above_max() {
1713+
assert!(!is_valid_sandbox_identity(&MAX_SANDBOX_UID.saturating_add(1).to_string()));
1714+
}
1715+
1716+
#[test]
1717+
fn valid_identity_rejects_non_numeric_names() {
1718+
assert!(!is_valid_sandbox_identity("root"));
1719+
assert!(!is_valid_sandbox_identity("nobody"));
1720+
assert!(!is_valid_sandbox_identity("user"));
1721+
}
1722+
1723+
#[test]
1724+
fn valid_identity_rejects_empty_string() {
1725+
assert!(!is_valid_sandbox_identity(""));
1726+
}
1727+
1728+
// ---- Policy validation with numeric UIDs ----
1729+
1730+
#[test]
1731+
fn validate_accepts_numeric_uid_in_range() {
1732+
let policy = SandboxPolicy {
1733+
version: 1,
1734+
process: Some(ProcessPolicy {
1735+
run_as_user: "1000".into(),
1736+
run_as_group: "5000".into(),
1737+
}),
1738+
filesystem: None,
1739+
landlock: None,
1740+
network_policies: HashMap::new(),
1741+
};
1742+
assert!(validate_sandbox_policy(&policy).is_ok());
1743+
}
1744+
1745+
#[test]
1746+
fn validate_accepts_boundary_uids() {
1747+
let policy = SandboxPolicy {
1748+
version: 1,
1749+
process: Some(ProcessPolicy {
1750+
run_as_user: MIN_SANDBOX_UID.to_string(),
1751+
run_as_group: MAX_SANDBOX_UID.to_string(),
1752+
}),
1753+
filesystem: None,
1754+
landlock: None,
1755+
network_policies: HashMap::new(),
1756+
};
1757+
assert!(validate_sandbox_policy(&policy).is_ok());
1758+
}
1759+
1760+
#[test]
1761+
fn validate_rejects_uid_out_of_range_low() {
1762+
let mut policy = restrictive_default_policy();
1763+
policy.process = Some(ProcessPolicy {
1764+
run_as_user: "500".into(),
1765+
run_as_group: "sandbox".into(),
1766+
});
1767+
let violations = validate_sandbox_policy(&policy).unwrap_err();
1768+
assert!(violations.iter().any(|v| matches!(
1769+
v,
1770+
PolicyViolation::InvalidProcessIdentity { field: "run_as_user", .. }
1771+
)));
1772+
}
1773+
1774+
#[test]
1775+
fn validate_rejects_uid_out_of_range_high() {
1776+
let mut policy = restrictive_default_policy();
1777+
policy.process = Some(ProcessPolicy {
1778+
run_as_user: (MAX_SANDBOX_UID + 1).to_string(),
1779+
run_as_group: "sandbox".into(),
1780+
});
1781+
let violations = validate_sandbox_policy(&policy).unwrap_err();
1782+
assert!(violations.iter().any(|v| matches!(
1783+
v,
1784+
PolicyViolation::InvalidProcessIdentity { field: "run_as_user", .. }
1785+
)));
1786+
}
1787+
1788+
#[test]
1789+
fn validate_rejects_root_string() {
1790+
let mut policy = restrictive_default_policy();
1791+
policy.process = Some(ProcessPolicy {
1792+
run_as_user: "root".into(),
1793+
run_as_group: "sandbox".into(),
1794+
});
1795+
let violations = validate_sandbox_policy(&policy).unwrap_err();
1796+
assert!(violations.iter().any(|v| matches!(
1797+
v,
1798+
PolicyViolation::InvalidProcessIdentity { field: "run_as_user", .. }
1799+
)));
1800+
}
1801+
1802+
#[test]
1803+
fn validate_rejects_nobody_string() {
1804+
let mut policy = restrictive_default_policy();
1805+
policy.process = Some(ProcessPolicy {
1806+
run_as_user: "nobody".into(),
1807+
run_as_group: "nogroup".into(),
1808+
});
1809+
let violations = validate_sandbox_policy(&policy).unwrap_err();
1810+
assert_eq!(violations.len(), 2);
1811+
}
1812+
1813+
#[test]
1814+
fn validate_accepts_mixed_sandbox_name_and_uid() {
1815+
// run_as_user as "sandbox" name, run_as_group as numeric UID
1816+
let policy = SandboxPolicy {
1817+
version: 1,
1818+
process: Some(ProcessPolicy {
1819+
run_as_user: "sandbox".into(),
1820+
run_as_group: "1000".into(),
1821+
}),
1822+
filesystem: None,
1823+
landlock: None,
1824+
network_policies: HashMap::new(),
1825+
};
1826+
assert!(validate_sandbox_policy(&policy).is_ok());
1827+
}
1828+
1829+
#[test]
1830+
fn policy_violation_display_includes_range() {
1831+
let v = PolicyViolation::InvalidProcessIdentity {
1832+
field: "run_as_user",
1833+
value: "root".into(),
1834+
};
1835+
let s = format!("{v}");
1836+
assert!(s.contains("sandbox"));
1837+
assert!(s.contains(&MIN_SANDBOX_UID.to_string()));
1838+
assert!(s.contains(&MAX_SANDBOX_UID.to_string()));
1839+
assert!(s.contains("root"));
1840+
}
1841+
16401842
// ---- Multi-port and host wildcard tests ----
16411843

16421844
#[test]

0 commit comments

Comments
 (0)