@@ -537,6 +537,41 @@ fn from_proto(policy: &SandboxPolicy) -> PolicyFile {
537537 }
538538}
539539
540+ // ---------------------------------------------------------------------------
541+ // Sandbox UID/GID constants
542+ // ---------------------------------------------------------------------------
543+
544+ /// Minimum accepted UID for sandbox process identity.
545+ /// UIDs below this are reserved for system users and are rejected.
546+ pub const MIN_SANDBOX_UID : u32 = 1000 ;
547+
548+ /// Maximum accepted UID for sandbox process identity.
549+ /// UIDs above this exceed typical OS limits and are rejected.
550+ pub const MAX_SANDBOX_UID : u32 = 2_000_000_000 ;
551+
552+ /// The literal string value accepted as a valid sandbox user/group name.
553+ const SANDBOX_NAME : & str = "sandbox" ;
554+
555+ /// Validate whether a process identity field value is acceptable.
556+ ///
557+ /// Accepts either the literal `"sandbox"` or a numeric UID/GID parsed as
558+ /// `u32` within the range `[MIN_SANDBOX_UID, MAX_SANDBOX_UID]`.
559+ ///
560+ /// Rejects:
561+ /// - The empty string (callers should use `ensure_sandbox_process_identity`
562+ /// to fill defaults before validation)
563+ /// - UID 0 or values below `MIN_SANDBOX_UID`
564+ /// - Values above `MAX_SANDBOX_UID`
565+ /// - Non-numeric strings other than `"sandbox"` (e.g. `"root"`, `"nobody"`)
566+ pub fn is_valid_sandbox_identity ( value : & str ) -> bool {
567+ if value == SANDBOX_NAME {
568+ return true ;
569+ }
570+ value. parse :: < u32 > ( ) . is_ok_and ( |uid| {
571+ ( MIN_SANDBOX_UID ..=MAX_SANDBOX_UID ) . contains ( & uid)
572+ } )
573+ }
574+
540575// ---------------------------------------------------------------------------
541576// Public API
542577// ---------------------------------------------------------------------------
@@ -700,7 +735,10 @@ impl fmt::Display for PolicyViolation {
700735 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
701736 match self {
702737 Self :: InvalidProcessIdentity { field, value } => {
703- write ! ( f, "{field} must be 'sandbox', got '{value}'" )
738+ write ! (
739+ f,
740+ "{field} must be 'sandbox' or a numeric UID/GID in range [{MIN_SANDBOX_UID}, {MAX_SANDBOX_UID}], got '{value}'"
741+ )
704742 }
705743 Self :: PathTraversal { path } => {
706744 write ! ( f, "path contains '..' traversal component: {path}" )
@@ -753,17 +791,18 @@ pub fn validate_sandbox_policy(
753791) -> std:: result:: Result < ( ) , Vec < PolicyViolation > > {
754792 let mut violations = Vec :: new ( ) ;
755793
756- // Check process identity — must be "sandbox".
794+ // Check process identity — must be "sandbox" or a numeric UID/GID
795+ // within the acceptable sandbox range.
757796 // `ensure_sandbox_process_identity` should be called before this to
758- // fill in defaults; anything other than "sandbox" is rejected.
797+ // fill in defaults; any invalid value is rejected.
759798 if let Some ( ref process) = policy. process {
760- if process. run_as_user != "sandbox" {
799+ if ! is_valid_sandbox_identity ( & process. run_as_user ) {
761800 violations. push ( PolicyViolation :: InvalidProcessIdentity {
762801 field : "run_as_user" ,
763802 value : process. run_as_user . clone ( ) ,
764803 } ) ;
765804 }
766- if process. run_as_group != "sandbox" {
805+ if ! is_valid_sandbox_identity ( & process. run_as_group ) {
767806 violations. push ( PolicyViolation :: InvalidProcessIdentity {
768807 field : "run_as_group" ,
769808 value : process. run_as_group . clone ( ) ,
@@ -1402,6 +1441,169 @@ network_policies:
14021441 assert ! ( s. contains( "sandbox" ) ) ;
14031442 }
14041443
1444+ // ---- is_valid_sandbox_identity tests ----
1445+
1446+ #[ test]
1447+ fn valid_identity_accepts_sandbox ( ) {
1448+ assert ! ( is_valid_sandbox_identity( "sandbox" ) ) ;
1449+ }
1450+
1451+ #[ test]
1452+ fn valid_identity_accepts_numeric_uid_in_range ( ) {
1453+ assert ! ( is_valid_sandbox_identity( "1000" ) ) ;
1454+ assert ! ( is_valid_sandbox_identity( "50000" ) ) ;
1455+ assert ! ( is_valid_sandbox_identity( "1000660000" ) ) ;
1456+ }
1457+
1458+ #[ test]
1459+ fn valid_identity_accepts_boundary_uids ( ) {
1460+ assert ! ( is_valid_sandbox_identity( & MIN_SANDBOX_UID . to_string( ) ) ) ;
1461+ assert ! ( is_valid_sandbox_identity( & MAX_SANDBOX_UID . to_string( ) ) ) ;
1462+ }
1463+
1464+ #[ test]
1465+ fn valid_identity_rejects_zero ( ) {
1466+ assert ! ( !is_valid_sandbox_identity( "0" ) ) ;
1467+ }
1468+
1469+ #[ test]
1470+ fn valid_identity_rejects_system_uids_below_min ( ) {
1471+ assert ! ( !is_valid_sandbox_identity( "999" ) ) ;
1472+ assert ! ( !is_valid_sandbox_identity( "100" ) ) ;
1473+ assert ! ( !is_valid_sandbox_identity( "1" ) ) ;
1474+ }
1475+
1476+ #[ test]
1477+ fn valid_identity_rejects_uid_above_max ( ) {
1478+ assert ! ( !is_valid_sandbox_identity( & MAX_SANDBOX_UID . saturating_add( 1 ) . to_string( ) ) ) ;
1479+ }
1480+
1481+ #[ test]
1482+ fn valid_identity_rejects_non_numeric_names ( ) {
1483+ assert ! ( !is_valid_sandbox_identity( "root" ) ) ;
1484+ assert ! ( !is_valid_sandbox_identity( "nobody" ) ) ;
1485+ assert ! ( !is_valid_sandbox_identity( "user" ) ) ;
1486+ }
1487+
1488+ #[ test]
1489+ fn valid_identity_rejects_empty_string ( ) {
1490+ assert ! ( !is_valid_sandbox_identity( "" ) ) ;
1491+ }
1492+
1493+ // ---- Policy validation with numeric UIDs ----
1494+
1495+ #[ test]
1496+ fn validate_accepts_numeric_uid_in_range ( ) {
1497+ let policy = SandboxPolicy {
1498+ version : 1 ,
1499+ process : Some ( ProcessPolicy {
1500+ run_as_user : "1000" . into ( ) ,
1501+ run_as_group : "5000" . into ( ) ,
1502+ } ) ,
1503+ filesystem : None ,
1504+ landlock : None ,
1505+ network_policies : HashMap :: new ( ) ,
1506+ } ;
1507+ assert ! ( validate_sandbox_policy( & policy) . is_ok( ) ) ;
1508+ }
1509+
1510+ #[ test]
1511+ fn validate_accepts_boundary_uids ( ) {
1512+ let policy = SandboxPolicy {
1513+ version : 1 ,
1514+ process : Some ( ProcessPolicy {
1515+ run_as_user : MIN_SANDBOX_UID . to_string ( ) ,
1516+ run_as_group : MAX_SANDBOX_UID . to_string ( ) ,
1517+ } ) ,
1518+ filesystem : None ,
1519+ landlock : None ,
1520+ network_policies : HashMap :: new ( ) ,
1521+ } ;
1522+ assert ! ( validate_sandbox_policy( & policy) . is_ok( ) ) ;
1523+ }
1524+
1525+ #[ test]
1526+ fn validate_rejects_uid_out_of_range_low ( ) {
1527+ let mut policy = restrictive_default_policy ( ) ;
1528+ policy. process = Some ( ProcessPolicy {
1529+ run_as_user : "500" . into ( ) ,
1530+ run_as_group : "sandbox" . into ( ) ,
1531+ } ) ;
1532+ let violations = validate_sandbox_policy ( & policy) . unwrap_err ( ) ;
1533+ assert ! ( violations. iter( ) . any( |v| matches!(
1534+ v,
1535+ PolicyViolation :: InvalidProcessIdentity { field: "run_as_user" , .. }
1536+ ) ) ) ;
1537+ }
1538+
1539+ #[ test]
1540+ fn validate_rejects_uid_out_of_range_high ( ) {
1541+ let mut policy = restrictive_default_policy ( ) ;
1542+ policy. process = Some ( ProcessPolicy {
1543+ run_as_user : ( MAX_SANDBOX_UID + 1 ) . to_string ( ) ,
1544+ run_as_group : "sandbox" . into ( ) ,
1545+ } ) ;
1546+ let violations = validate_sandbox_policy ( & policy) . unwrap_err ( ) ;
1547+ assert ! ( violations. iter( ) . any( |v| matches!(
1548+ v,
1549+ PolicyViolation :: InvalidProcessIdentity { field: "run_as_user" , .. }
1550+ ) ) ) ;
1551+ }
1552+
1553+ #[ test]
1554+ fn validate_rejects_root_string ( ) {
1555+ let mut policy = restrictive_default_policy ( ) ;
1556+ policy. process = Some ( ProcessPolicy {
1557+ run_as_user : "root" . into ( ) ,
1558+ run_as_group : "sandbox" . into ( ) ,
1559+ } ) ;
1560+ let violations = validate_sandbox_policy ( & policy) . unwrap_err ( ) ;
1561+ assert ! ( violations. iter( ) . any( |v| matches!(
1562+ v,
1563+ PolicyViolation :: InvalidProcessIdentity { field: "run_as_user" , .. }
1564+ ) ) ) ;
1565+ }
1566+
1567+ #[ test]
1568+ fn validate_rejects_nobody_string ( ) {
1569+ let mut policy = restrictive_default_policy ( ) ;
1570+ policy. process = Some ( ProcessPolicy {
1571+ run_as_user : "nobody" . into ( ) ,
1572+ run_as_group : "nogroup" . into ( ) ,
1573+ } ) ;
1574+ let violations = validate_sandbox_policy ( & policy) . unwrap_err ( ) ;
1575+ assert_eq ! ( violations. len( ) , 2 ) ;
1576+ }
1577+
1578+ #[ test]
1579+ fn validate_accepts_mixed_sandbox_name_and_uid ( ) {
1580+ // run_as_user as "sandbox" name, run_as_group as numeric UID
1581+ let policy = SandboxPolicy {
1582+ version : 1 ,
1583+ process : Some ( ProcessPolicy {
1584+ run_as_user : "sandbox" . into ( ) ,
1585+ run_as_group : "1000" . into ( ) ,
1586+ } ) ,
1587+ filesystem : None ,
1588+ landlock : None ,
1589+ network_policies : HashMap :: new ( ) ,
1590+ } ;
1591+ assert ! ( validate_sandbox_policy( & policy) . is_ok( ) ) ;
1592+ }
1593+
1594+ #[ test]
1595+ fn policy_violation_display_includes_range ( ) {
1596+ let v = PolicyViolation :: InvalidProcessIdentity {
1597+ field : "run_as_user" ,
1598+ value : "root" . into ( ) ,
1599+ } ;
1600+ let s = format ! ( "{v}" ) ;
1601+ assert ! ( s. contains( "sandbox" ) ) ;
1602+ assert ! ( s. contains( & MIN_SANDBOX_UID . to_string( ) ) ) ;
1603+ assert ! ( s. contains( & MAX_SANDBOX_UID . to_string( ) ) ) ;
1604+ assert ! ( s. contains( "root" ) ) ;
1605+ }
1606+
14051607 // ---- Multi-port and host wildcard tests ----
14061608
14071609 #[ test]
0 commit comments