|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Compute launch input identity. Policy safety remains the admission owner's duty. |
| 5 | +
|
| 6 | +use crate::policy_identity::deterministic_policy_hash; |
| 7 | +use crate::proto::SandboxLaunchPolicy; |
| 8 | +use prost::Message; |
| 9 | + |
| 10 | +/// Check the complete bounded input before a driver changes retained resources. |
| 11 | +/// This does not authorize a policy or replace backend-specific admission. |
| 12 | +pub fn validate_launch_policy( |
| 13 | + input: &SandboxLaunchPolicy, |
| 14 | + generation: &str, |
| 15 | +) -> Result<(), &'static str> { |
| 16 | + if crate::sandbox_generation::SandboxGenerationId::parse(generation).is_err() |
| 17 | + || input.generation_id != generation |
| 18 | + || input.generation_id.is_empty() |
| 19 | + { |
| 20 | + return Err("launch policy generation does not match the compute launch"); |
| 21 | + } |
| 22 | + let policy = input |
| 23 | + .policy |
| 24 | + .as_ref() |
| 25 | + .ok_or("launch policy document is missing")?; |
| 26 | + if policy.encoded_len() > 262_144 { |
| 27 | + return Err("launch policy exceeds the policy size limit"); |
| 28 | + } |
| 29 | + if deterministic_policy_hash(policy) != input.policy_hash { |
| 30 | + return Err("launch policy hash does not match its document"); |
| 31 | + } |
| 32 | + Ok(()) |
| 33 | +} |
| 34 | + |
| 35 | +#[cfg(test)] |
| 36 | +mod tests { |
| 37 | + use super::*; |
| 38 | + |
| 39 | + #[test] |
| 40 | + fn launch_input_requires_matching_generation_document_hash_and_size() { |
| 41 | + let generation = uuid::Uuid::new_v4().to_string(); |
| 42 | + let policy = crate::proto::SandboxPolicy::default(); |
| 43 | + let valid = SandboxLaunchPolicy { |
| 44 | + generation_id: generation.clone(), |
| 45 | + policy_hash: deterministic_policy_hash(&policy), |
| 46 | + policy: Some(policy), |
| 47 | + }; |
| 48 | + assert!(validate_launch_policy(&valid, &generation).is_ok()); |
| 49 | + for mutation in 0..5 { |
| 50 | + let mut input = valid.clone(); |
| 51 | + match mutation { |
| 52 | + 0 => input.generation_id = uuid::Uuid::new_v4().to_string(), |
| 53 | + 1 => input.policy = None, |
| 54 | + 2 => input.policy_hash = "wrong".into(), |
| 55 | + 3 => { |
| 56 | + let policy = input.policy.as_mut().unwrap(); |
| 57 | + policy.filesystem = Some(crate::proto::FilesystemPolicy { |
| 58 | + read_write: vec!["/".repeat(262_145)], |
| 59 | + ..Default::default() |
| 60 | + }); |
| 61 | + input.policy_hash = deterministic_policy_hash(policy); |
| 62 | + } |
| 63 | + _ => input.generation_id.clear(), |
| 64 | + } |
| 65 | + assert!( |
| 66 | + validate_launch_policy(&input, &generation).is_err(), |
| 67 | + "mutation {mutation}" |
| 68 | + ); |
| 69 | + } |
| 70 | + let mut invalid = valid; |
| 71 | + invalid.generation_id = "invalid/generation".into(); |
| 72 | + assert!(validate_launch_policy(&invalid, "invalid/generation").is_err()); |
| 73 | + } |
| 74 | +} |
0 commit comments