|
128 | 128 | //! - **Process identity.** Every request carries the same placeholder |
129 | 129 | //! `principal.user` and `principal.group`, since the sandbox's run-as |
130 | 130 | //! identity is not yet available to policies, so a policy that reads |
131 | | -//! either is rejected rather than evaluated against a constant. |
| 131 | +//! either is rejected rather than evaluated against a constant. Every |
| 132 | +//! request is also evaluated as the same placeholder process, so a policy |
| 133 | +//! that tests which process the principal is, with a scope other than |
| 134 | +//! `principal` or `principal is Sandbox::Process`, or with `==`, `!=`, |
| 135 | +//! `in`, a `contains` method, or `is` on it in its conditions, is rejected |
| 136 | +//! too: it could never match a real process. |
132 | 137 | //! |
133 | 138 | //! Before any of that, the policy set is validated against the schema in |
134 | 139 | //! strict mode, so a typo such as `context.binray_path` is a load error |
@@ -813,6 +818,7 @@ pub fn analyze(schema: &Schema, policies: &PolicySet) -> Result<PolicyAnalysis, |
813 | 818 | )?; |
814 | 819 | check_delimited_patterns(conditions, &policy_id)?; |
815 | 820 | check_principal_identity(conditions, &policy_id)?; |
| 821 | + check_principal_constraints(policy, conditions, &policy_id)?; |
816 | 822 | let scope = ActionScope::of(policy); |
817 | 823 | check_has_tests(conditions, policy, &policy_id, &scope, schema, &declared)?; |
818 | 824 | let touches_filesystem = (scope.includes(actions::READ_FILE) |
@@ -1443,6 +1449,67 @@ fn check_principal_identity(expr: &Value, policy_id: &str) -> Result<(), CedarEn |
1443 | 1449 | } |
1444 | 1450 | } |
1445 | 1451 |
|
| 1452 | +/// Rejects a test of which process the principal is, in the scope or within |
| 1453 | +/// `conditions`; see the module docs. |
| 1454 | +/// |
| 1455 | +/// The scope may only be `principal` or `principal is Sandbox::Process`. In |
| 1456 | +/// `conditions`, an `==`, `!=`, `in`, or `contains`-family operand, or the |
| 1457 | +/// receiver of an `is` other than a bare `is Sandbox::Process`, counts when |
| 1458 | +/// it can evaluate to a `Process`, as for [`check_principal_identity`]. |
| 1459 | +/// |
| 1460 | +/// # Errors |
| 1461 | +/// |
| 1462 | +/// Returns [`CedarEngineError::UnsupportedPolicy`] for the first such test. |
| 1463 | +fn check_principal_constraints( |
| 1464 | + policy: &Policy, |
| 1465 | + conditions: &Value, |
| 1466 | + policy_id: &str, |
| 1467 | +) -> Result<(), CedarEngineError> { |
| 1468 | + let unconstrained = match policy.principal_constraint() { |
| 1469 | + PrincipalConstraint::Any => true, |
| 1470 | + PrincipalConstraint::Is(entity_type) => entity_type.to_string() == entity_types::PROCESS, |
| 1471 | + _ => false, |
| 1472 | + }; |
| 1473 | + if unconstrained && !tests_principal(conditions) { |
| 1474 | + return Ok(()); |
| 1475 | + } |
| 1476 | + Err(CedarEngineError::UnsupportedPolicy { |
| 1477 | + policy_id: policy_id.to_string(), |
| 1478 | + reason: "it tests which process the principal is, but every request is evaluated \ |
| 1479 | + with the same placeholder process, so the test cannot tell processes apart; \ |
| 1480 | + use `principal` or `principal is Sandbox::Process`" |
| 1481 | + .to_string(), |
| 1482 | + }) |
| 1483 | +} |
| 1484 | + |
| 1485 | +/// Returns whether `expr` compares a value that may be a `Process` with |
| 1486 | +/// `==`, `!=`, `in`, or a `contains` method, or tests its type other than |
| 1487 | +/// as a bare `is Sandbox::Process`. |
| 1488 | +fn tests_principal(expr: &Value) -> bool { |
| 1489 | + match expr { |
| 1490 | + Value::Object(fields) => fields.iter().any(|(name, value)| { |
| 1491 | + let compares = match name.as_str() { |
| 1492 | + "==" | "!=" | "in" | "contains" | "containsAll" | "containsAny" => { |
| 1493 | + ["left", "right"] |
| 1494 | + .iter() |
| 1495 | + .any(|operand| value.get(operand).is_some_and(may_be_process)) |
| 1496 | + } |
| 1497 | + "is" => { |
| 1498 | + value.get("in").is_some_and(may_be_process) |
| 1499 | + || value.get("left").is_some_and(may_be_process) |
| 1500 | + && (value.get("in").is_some() |
| 1501 | + || value.get("entity_type").and_then(Value::as_str) |
| 1502 | + != Some(entity_types::PROCESS)) |
| 1503 | + } |
| 1504 | + _ => false, |
| 1505 | + }; |
| 1506 | + compares || tests_principal(value) |
| 1507 | + }), |
| 1508 | + Value::Array(values) => values.iter().any(tests_principal), |
| 1509 | + _ => false, |
| 1510 | + } |
| 1511 | +} |
| 1512 | + |
1446 | 1513 | /// Returns whether `expr` contains `principal` or a `Process` entity |
1447 | 1514 | /// literal, so it may evaluate to a `Process`. |
1448 | 1515 | fn may_be_process(expr: &Value) -> bool { |
|
0 commit comments