Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/crates/aienos-kernel/src/admission.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / crates / aienos-kernel / src / admission.rs` 739 lines ยท 27437 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 //! Deterministic policy evaluation for authenticated native artifacts. 17 //! 18 //! This module decides what a verified artifact may receive. It does not 19 //! reserve resources, allocate pages, create handles, or execute candidate 20 //! code; those are loader responsibilities. 21 22 use aienos_artifact::canonical::{ 23 granted_capability_digest, policy_digest, requested_capability_digest, resource_envelope_digest, 24 }; 25 use aienos_artifact::capability::{ 26 CapabilityRequest, MAX_CAPABILITIES, RESOURCE_KIND_CHANNEL, RESOURCE_KIND_OBJECT, 27 }; 28 use aienos_artifact::error::ArtifactError; 29 use aienos_artifact::format::{ABI_V1, MAX_PAYLOAD_SIZE, TARGET_AARCH64_LE}; 30 use aienos_artifact::resource::{ 31 ResourceEnvelope, MAX_CODE_PAGES, MAX_CPU_TICKS, MAX_DATA_PAGES, MAX_ELAPSED_TICKS, 32 MAX_IPC_BYTES, MAX_IPC_MESSAGES, MAX_STACK_PAGES, MAX_SYSCALLS, PAGE_SIZE, 33 }; 34 use aienos_artifact::signature::{TrustTier, VerifiedArtifact}; 35 use aienos_crypto::sha256::Digest; 36 37 use crate::abi::{ResourceId, ResourceKind}; 38 use crate::caps::Rights; 39 40 pub const MAX_POLICY_RESOURCES: usize = 16; 41 pub const MAX_POLICY_SIGNERS: usize = 8; 42 pub const POLICY_HEADER_SIZE: usize = 64; 43 pub const POLICY_RECORD_SIZE: usize = 48; 44 pub const POLICY_SIGNER_SIZE: usize = 32; 45 const MAX_POLICY_BYTES: usize = POLICY_HEADER_SIZE 46 + MAX_POLICY_RESOURCES * POLICY_RECORD_SIZE 47 + MAX_POLICY_SIGNERS * POLICY_SIGNER_SIZE; 48 49 #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] 50 pub struct AdmissionLimits { 51 pub code_pages: u32, 52 pub data_pages: u32, 53 pub stack_pages: u32, 54 pub max_capabilities: u32, 55 pub ipc_messages: u32, 56 pub ipc_bytes: u32, 57 pub cpu_ticks: u64, 58 pub elapsed_ticks: u64, 59 pub syscall_count: u32, 60 } 61 62 impl AdmissionLimits { 63 fn validate(self) -> Result<(), ArtifactError> { 64 if self.code_pages > MAX_CODE_PAGES 65 || self.data_pages > MAX_DATA_PAGES 66 || self.stack_pages > MAX_STACK_PAGES 67 || self.max_capabilities > MAX_CAPABILITIES as u32 68 || self.ipc_messages > MAX_IPC_MESSAGES 69 || self.ipc_bytes > MAX_IPC_BYTES 70 || self.cpu_ticks > MAX_CPU_TICKS 71 || self.elapsed_ticks > MAX_ELAPSED_TICKS 72 || self.syscall_count > MAX_SYSCALLS 73 { 74 return Err(ArtifactError::ResourceLimit); 75 } 76 Ok(()) 77 } 78 79 fn encode(self, out: &mut [u8]) { 80 put_u32(out, 16, self.code_pages); 81 put_u32(out, 20, self.data_pages); 82 put_u32(out, 24, self.stack_pages); 83 put_u32(out, 28, self.max_capabilities); 84 put_u32(out, 32, self.ipc_messages); 85 put_u32(out, 36, self.ipc_bytes); 86 put_u64(out, 40, self.cpu_ticks); 87 put_u64(out, 48, self.elapsed_ticks); 88 put_u32(out, 56, self.syscall_count); 89 } 90 } 91 92 #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] 93 pub struct AvailableResources { 94 pub code_pages: u32, 95 pub data_pages: u32, 96 pub stack_pages: u32, 97 pub capability_slots: u32, 98 pub ipc_messages: u32, 99 pub ipc_bytes: u32, 100 pub cpu_ticks: u64, 101 pub elapsed_ticks: u64, 102 pub syscall_count: u32, 103 } 104 105 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 106 pub struct AdmissionPolicy { 107 limits: AdmissionLimits, 108 resource_rules: [CapabilityRequest; MAX_POLICY_RESOURCES], 109 resource_count: u8, 110 signer_fingerprints: [Digest; MAX_POLICY_SIGNERS], 111 signer_count: u8, 112 } 113 114 impl AdmissionPolicy { 115 pub fn new(limits: AdmissionLimits) -> Result { 116 limits.validate()?; 117 Ok(Self { 118 limits, 119 resource_rules: [CapabilityRequest::default(); MAX_POLICY_RESOURCES], 120 resource_count: 0, 121 signer_fingerprints: [[0; 32]; MAX_POLICY_SIGNERS], 122 signer_count: 0, 123 }) 124 } 125 126 pub fn add_resource_rule(&mut self, rule: CapabilityRequest) -> Result<(), ArtifactError> { 127 rule.validate()?; 128 let key = resource_key(&rule); 129 let count = usize::from(self.resource_count); 130 let mut insertion = count; 131 for index in 0..count { 132 let current = resource_key(&self.resource_rules[index]); 133 if current == key { 134 return Err(ArtifactError::MalformedCapability); 135 } 136 if current > key { 137 insertion = index; 138 break; 139 } 140 } 141 if count == MAX_POLICY_RESOURCES { 142 return Err(ArtifactError::ResourceLimit); 143 } 144 for index in (insertion..count).rev() { 145 self.resource_rules[index + 1] = self.resource_rules[index]; 146 } 147 self.resource_rules[insertion] = rule; 148 self.resource_count += 1; 149 Ok(()) 150 } 151 152 pub fn allow_signer(&mut self, fingerprint: Digest) -> Result<(), ArtifactError> { 153 let count = usize::from(self.signer_count); 154 let mut insertion = count; 155 for index in 0..count { 156 match self.signer_fingerprints[index].cmp(&fingerprint) { 157 core::cmp::Ordering::Equal => return Err(ArtifactError::UntrustedSigner), 158 core::cmp::Ordering::Greater => { 159 insertion = index; 160 break; 161 } 162 core::cmp::Ordering::Less => {} 163 } 164 } 165 if count == MAX_POLICY_SIGNERS { 166 return Err(ArtifactError::ResourceLimit); 167 } 168 for index in (insertion..count).rev() { 169 self.signer_fingerprints[index + 1] = self.signer_fingerprints[index]; 170 } 171 self.signer_fingerprints[insertion] = fingerprint; 172 self.signer_count += 1; 173 Ok(()) 174 } 175 176 pub fn canonical_bytes(&self) -> ([u8; MAX_POLICY_BYTES], usize) { 177 let mut bytes = [0u8; MAX_POLICY_BYTES]; 178 bytes[0..8].copy_from_slice(b"AIENPOL\0"); 179 put_u16(&mut bytes, 8, 0); 180 put_u16(&mut bytes, 10, POLICY_HEADER_SIZE as u16); 181 put_u16(&mut bytes, 12, POLICY_RECORD_SIZE as u16); 182 put_u16(&mut bytes, 14, u16::from(self.resource_count)); 183 self.limits.encode(&mut bytes); 184 put_u16(&mut bytes, 60, u16::from(self.signer_count)); 185 186 let mut offset = POLICY_HEADER_SIZE; 187 for rule in &self.resource_rules[..usize::from(self.resource_count)] { 188 bytes[offset..offset + POLICY_RECORD_SIZE].copy_from_slice(&rule.to_bytes()); 189 offset += POLICY_RECORD_SIZE; 190 } 191 for fingerprint in &self.signer_fingerprints[..usize::from(self.signer_count)] { 192 bytes[offset..offset + POLICY_SIGNER_SIZE].copy_from_slice(fingerprint); 193 offset += POLICY_SIGNER_SIZE; 194 } 195 (bytes, offset) 196 } 197 198 pub fn digest(&self) -> Digest { 199 let (bytes, length) = self.canonical_bytes(); 200 policy_digest(&bytes[..length]) 201 } 202 203 pub fn resource_rules(&self) -> &[CapabilityRequest] { 204 &self.resource_rules[..usize::from(self.resource_count)] 205 } 206 207 pub fn evaluate( 208 &self, 209 verified: &VerifiedArtifact<'_>, 210 available: AvailableResources, 211 ) -> Result { 212 let identified = verified.identified(); 213 let artifact = &identified.artifact; 214 if artifact.target_arch != TARGET_AARCH64_LE { 215 return Err(ArtifactError::WrongTarget); 216 } 217 if artifact.abi_version != ABI_V1 { 218 return Err(ArtifactError::WrongAbi); 219 } 220 if !self.contains_signer(verified.signer_fingerprint()) { 221 return Err(ArtifactError::UntrustedSigner); 222 } 223 validate_entry(artifact.entry_offset, artifact.sections[0].file_length)?; 224 if artifact.payload.len() > MAX_PAYLOAD_SIZE { 225 return Err(ArtifactError::ResourceLimit); 226 } 227 let requested = artifact.resources; 228 requested.validate(artifact.capabilities.len())?; 229 check_resource_limits(requested, self.limits)?; 230 check_available(requested, available)?; 231 232 let mut grants = [None; MAX_CAPABILITIES]; 233 let mut grant_count = 0usize; 234 let mut grant_bytes = [[0u8; POLICY_RECORD_SIZE]; MAX_CAPABILITIES]; 235 for request in artifact.capabilities.as_slice() { 236 let Some(rule) = self.find_rule(request) else { 237 continue; 238 }; 239 let Some(grant) = intersect_request(request, rule)? else { 240 continue; 241 }; 242 if !grant.is_attenuation_of(request) { 243 return Err(ArtifactError::RightsEscalation); 244 } 245 if !grant.is_attenuation_of(rule) { 246 return Err(ArtifactError::RightsEscalation); 247 } 248 let resource_kind = match grant.resource_kind { 249 RESOURCE_KIND_CHANNEL => ResourceKind::Channel, 250 RESOURCE_KIND_OBJECT => ResourceKind::Object, 251 _ => return Err(ArtifactError::MalformedCapability), 252 }; 253 let right_bits = 254 u8::try_from(grant.rights).map_err(|_| ArtifactError::RightsEscalation)?; 255 let rights = Rights::from_bits(right_bits).ok_or(ArtifactError::MalformedCapability)?; 256 grants[grant_count] = Some(GrantedCapability { 257 resource: ResourceId::new(resource_kind, grant.resource_id), 258 rights, 259 bounds: grant, 260 }); 261 grant_bytes[grant_count] = grant.to_bytes(); 262 grant_count += 1; 263 } 264 let mut canonical_grants = [0u8; MAX_CAPABILITIES * POLICY_RECORD_SIZE]; 265 for (index, record) in grant_bytes[..grant_count].iter().enumerate() { 266 let offset = index * POLICY_RECORD_SIZE; 267 canonical_grants[offset..offset + POLICY_RECORD_SIZE].copy_from_slice(record); 268 } 269 let policy_digest = self.digest(); 270 Ok(GrantedAdmission { 271 artifact_id: identified.artifact_id, 272 payload_digest: identified.payload_digest, 273 signer_fingerprint: *verified.signer_fingerprint(), 274 trust_tier: verified.trust_tier(), 275 requested_capability_digest: requested_capability_digest( 276 artifact.capability_request_bytes, 277 ), 278 granted_capability_digest: granted_capability_digest( 279 &canonical_grants[..grant_count * POLICY_RECORD_SIZE], 280 ), 281 resource_envelope_digest: resource_envelope_digest(artifact.resource_envelope_bytes), 282 policy_digest, 283 resources: requested, 284 entry_offset: artifact.entry_offset, 285 grants, 286 grant_count: grant_count as u8, 287 }) 288 } 289 290 fn contains_signer(&self, fingerprint: &Digest) -> bool { 291 self.signer_fingerprints[..usize::from(self.signer_count)] 292 .binary_search(fingerprint) 293 .is_ok() 294 } 295 296 fn find_rule(&self, request: &CapabilityRequest) -> Option<&CapabilityRequest> { 297 let key = resource_key(request); 298 self.resource_rules() 299 .binary_search_by_key(&key, resource_key) 300 .ok() 301 .map(|index| &self.resource_rules()[index]) 302 } 303 } 304 305 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 306 pub struct GrantedCapability { 307 pub resource: ResourceId, 308 pub rights: Rights, 309 /// Canonical bounds record retained for receipt and authority checks. 310 pub bounds: CapabilityRequest, 311 } 312 313 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 314 pub struct GrantedAdmission { 315 pub artifact_id: aienos_artifact::ArtifactId, 316 pub payload_digest: Digest, 317 pub signer_fingerprint: Digest, 318 pub trust_tier: TrustTier, 319 pub requested_capability_digest: Digest, 320 pub granted_capability_digest: Digest, 321 pub resource_envelope_digest: Digest, 322 pub policy_digest: Digest, 323 pub resources: ResourceEnvelope, 324 pub entry_offset: u32, 325 grants: [Option; MAX_CAPABILITIES], 326 grant_count: u8, 327 } 328 329 impl GrantedAdmission { 330 pub fn capabilities(&self) -> &[Option] { 331 &self.grants[..usize::from(self.grant_count)] 332 } 333 334 pub const fn capability_count(&self) -> usize { 335 self.grant_count as usize 336 } 337 } 338 339 fn validate_entry(entry: u32, code_length: u32) -> Result<(), ArtifactError> { 340 if entry & 3 != 0 || entry.checked_add(4).ok_or(ArtifactError::LengthOverflow)? > code_length { 341 return Err(ArtifactError::BadEntryPoint); 342 } 343 Ok(()) 344 } 345 346 fn check_resource_limits( 347 requested: ResourceEnvelope, 348 limits: AdmissionLimits, 349 ) -> Result<(), ArtifactError> { 350 if requested.code_pages > limits.code_pages 351 || requested.data_pages > limits.data_pages 352 || requested.stack_pages > limits.stack_pages 353 || u32::from(requested.max_capabilities) > limits.max_capabilities 354 || requested.ipc_messages > limits.ipc_messages 355 || requested.ipc_bytes > limits.ipc_bytes 356 || requested.cpu_ticks > limits.cpu_ticks 357 || requested.elapsed_ticks > limits.elapsed_ticks 358 || requested.syscall_count > limits.syscall_count 359 { 360 return Err(ArtifactError::ResourceLimit); 361 } 362 requested 363 .code_pages 364 .checked_mul(PAGE_SIZE) 365 .and_then(|_| requested.data_pages.checked_mul(PAGE_SIZE)) 366 .and_then(|_| requested.stack_pages.checked_mul(PAGE_SIZE)) 367 .ok_or(ArtifactError::LengthOverflow)?; 368 Ok(()) 369 } 370 371 fn check_available( 372 requested: ResourceEnvelope, 373 available: AvailableResources, 374 ) -> Result<(), ArtifactError> { 375 if requested.code_pages > available.code_pages 376 || requested.data_pages > available.data_pages 377 || requested.stack_pages > available.stack_pages 378 || u32::from(requested.max_capabilities) > available.capability_slots 379 || requested.ipc_messages > available.ipc_messages 380 || requested.ipc_bytes > available.ipc_bytes 381 || requested.cpu_ticks > available.cpu_ticks 382 || requested.elapsed_ticks > available.elapsed_ticks 383 || requested.syscall_count > available.syscall_count 384 { 385 return Err(ArtifactError::ResourceLimit); 386 } 387 Ok(()) 388 } 389 390 fn intersect_request( 391 request: &CapabilityRequest, 392 policy: &CapabilityRequest, 393 ) -> Result, ArtifactError> { 394 let rights = request.rights & policy.rights; 395 if rights == 0 { 396 return Ok(None); 397 } 398 let max_operations = request.max_operations.min(policy.max_operations); 399 let max_bytes = request.max_bytes.min(policy.max_bytes); 400 let (byte_offset, byte_length, bounded_max_bytes) = match request.bounds_kind { 401 1 => { 402 let request_end = request 403 .byte_offset 404 .checked_add(request.byte_length) 405 .ok_or(ArtifactError::LengthOverflow)?; 406 let policy_end = policy 407 .byte_offset 408 .checked_add(policy.byte_length) 409 .ok_or(ArtifactError::LengthOverflow)?; 410 let start = request.byte_offset.max(policy.byte_offset); 411 let end = request_end.min(policy_end); 412 if start >= end { 413 return Ok(None); 414 } 415 let length = end - start; 416 let bytes = max_bytes.min(length); 417 (start, length, bytes) 418 } 419 2 => (0, 0, max_bytes), 420 _ => return Err(ArtifactError::MalformedCapability), 421 }; 422 if max_operations == 0 || bounded_max_bytes == 0 { 423 return Ok(None); 424 } 425 let grant = CapabilityRequest { 426 resource_kind: request.resource_kind, 427 resource_id: request.resource_id, 428 rights, 429 bounds_kind: request.bounds_kind, 430 max_operations, 431 max_bytes: bounded_max_bytes, 432 byte_offset, 433 byte_length, 434 }; 435 grant.validate()?; 436 Ok(Some(grant)) 437 } 438 439 fn resource_key(request: &CapabilityRequest) -> (u16, u32) { 440 (request.resource_kind, request.resource_id) 441 } 442 443 fn put_u16(out: &mut [u8], offset: usize, value: u16) { 444 out[offset..offset + 2].copy_from_slice(&value.to_le_bytes()); 445 } 446 447 fn put_u32(out: &mut [u8], offset: usize, value: u32) { 448 out[offset..offset + 4].copy_from_slice(&value.to_le_bytes()); 449 } 450 451 fn put_u64(out: &mut [u8], offset: usize, value: u64) { 452 out[offset..offset + 8].copy_from_slice(&value.to_le_bytes()); 453 } 454 455 #[cfg(test)] 456 mod tests { 457 use std::vec::Vec; 458 459 use aienos_artifact::capability::{CapabilityRequest, RIGHT_READ, RIGHT_WRITE}; 460 use aienos_artifact::error::ArtifactError; 461 use aienos_artifact::signature::{ 462 ArtifactVerifier, ConfiguredArtifactVerifier, SignatureVerifier, TrustAnchorSet, TrustTier, 463 TrustedSigner, 464 }; 465 use aienos_crypto::sha256::{hash, Digest}; 466 467 use super::{ 468 AdmissionLimits, AdmissionPolicy, AvailableResources, POLICY_HEADER_SIZE, 469 POLICY_RECORD_SIZE, 470 }; 471 use crate::caps::{CapTable, Rights}; 472 473 const PUBLIC_KEY: [u8; 32] = [0x45; 32]; 474 475 struct TestAnchorSet; 476 477 impl TrustAnchorSet for TestAnchorSet { 478 fn find(&self, fingerprint: &Digest) -> Option { 479 (*fingerprint == hash(&PUBLIC_KEY)).then_some(TrustedSigner { 480 public_key: PUBLIC_KEY, 481 tier: TrustTier::Production, 482 }) 483 } 484 } 485 486 struct TestSignatureVerifier; 487 488 impl SignatureVerifier for TestSignatureVerifier { 489 fn verify(&self, _key: &[u8; 32], message: &[u8], _signature: &[u8; 64]) -> bool { 490 message.len() == aienos_artifact::signature::ARTIFACT_SIGNATURE_MESSAGE_SIZE 491 } 492 } 493 494 fn put_u16(bytes: &mut [u8], offset: usize, value: u16) { 495 bytes[offset..offset + 2].copy_from_slice(&value.to_le_bytes()); 496 } 497 498 fn put_u32(bytes: &mut [u8], offset: usize, value: u32) { 499 bytes[offset..offset + 4].copy_from_slice(&value.to_le_bytes()); 500 } 501 502 fn put_u64(bytes: &mut [u8], offset: usize, value: u64) { 503 bytes[offset..offset + 8].copy_from_slice(&value.to_le_bytes()); 504 } 505 506 fn signed_test_artifact(request: Option) -> Vec { 507 let cap_count = usize::from(request.is_some()); 508 let cap_bytes = cap_count * POLICY_RECORD_SIZE; 509 let resources_offset = 192 + cap_bytes; 510 let payload_offset = (resources_offset + 48 + 15) & !15; 511 let signature_offset = payload_offset + 8; 512 let total_length = signature_offset + 100; 513 let mut bytes = vec![0u8; total_length]; 514 bytes[0..8].copy_from_slice(b"AIENART\0"); 515 put_u16(&mut bytes, 8, 0); 516 put_u16(&mut bytes, 10, 128); 517 put_u16(&mut bytes, 12, 1); 518 put_u16(&mut bytes, 14, 1); 519 put_u32(&mut bytes, 20, total_length as u32); 520 put_u32(&mut bytes, 24, 128); 521 put_u16(&mut bytes, 28, 2); 522 put_u16(&mut bytes, 30, 32); 523 put_u16(&mut bytes, 32, 0); 524 put_u32(&mut bytes, 40, 192); 525 put_u16(&mut bytes, 44, cap_count as u16); 526 put_u16(&mut bytes, 46, 48); 527 put_u32(&mut bytes, 48, resources_offset as u32); 528 put_u16(&mut bytes, 52, 48); 529 put_u32(&mut bytes, 56, payload_offset as u32); 530 put_u32(&mut bytes, 60, 8); 531 put_u32(&mut bytes, 64, signature_offset as u32); 532 put_u16(&mut bytes, 68, 100); 533 put_u16(&mut bytes, 70, 1); 534 535 put_u16(&mut bytes, 128, 1); 536 put_u16(&mut bytes, 130, 5); 537 put_u32(&mut bytes, 140, 4); 538 put_u32(&mut bytes, 144, 4); 539 put_u32(&mut bytes, 148, 4096); 540 put_u16(&mut bytes, 160, 2); 541 put_u16(&mut bytes, 162, 3); 542 put_u32(&mut bytes, 168, 4); 543 put_u32(&mut bytes, 172, 4); 544 put_u32(&mut bytes, 176, 4096); 545 put_u32(&mut bytes, 180, 4096); 546 if let Some(request) = request { 547 bytes[192..240].copy_from_slice(&request.to_bytes()); 548 } 549 put_u32(&mut bytes, resources_offset, 1); 550 put_u32(&mut bytes, resources_offset + 4, 1); 551 put_u32(&mut bytes, resources_offset + 8, 1); 552 put_u16(&mut bytes, resources_offset + 12, cap_count as u16); 553 put_u64(&mut bytes, resources_offset + 24, 10); 554 put_u64(&mut bytes, resources_offset + 32, 20); 555 put_u32(&mut bytes, resources_offset + 40, 16); 556 bytes[payload_offset..payload_offset + 4].copy_from_slice(&[0xc0, 0x03, 0x5f, 0xd6]); 557 bytes[payload_offset + 4..payload_offset + 8].copy_from_slice(b"DATA"); 558 put_u16(&mut bytes, signature_offset, 1); 559 bytes[signature_offset + 4..signature_offset + 36].copy_from_slice(&hash(&PUBLIC_KEY)); 560 bytes 561 } 562 563 fn request(rights: u32) -> CapabilityRequest { 564 CapabilityRequest { 565 resource_kind: 3, 566 resource_id: 7, 567 rights, 568 bounds_kind: 1, 569 max_operations: 4, 570 max_bytes: 4, 571 byte_offset: 0, 572 byte_length: 4, 573 } 574 } 575 576 fn policy_with(rules: &[CapabilityRequest]) -> AdmissionPolicy { 577 let mut policy = AdmissionPolicy::new(AdmissionLimits { 578 code_pages: 1, 579 data_pages: 1, 580 stack_pages: 1, 581 max_capabilities: 1, 582 ipc_messages: 0, 583 ipc_bytes: 0, 584 cpu_ticks: 10, 585 elapsed_ticks: 20, 586 syscall_count: 16, 587 }) 588 .unwrap(); 589 for rule in rules { 590 policy.add_resource_rule(*rule).unwrap(); 591 } 592 policy.allow_signer(hash(&PUBLIC_KEY)).unwrap(); 593 policy 594 } 595 596 fn available() -> AvailableResources { 597 AvailableResources { 598 code_pages: 1, 599 data_pages: 1, 600 stack_pages: 1, 601 capability_slots: 1, 602 ipc_messages: 0, 603 ipc_bytes: 0, 604 cpu_ticks: 10, 605 elapsed_ticks: 20, 606 syscall_count: 16, 607 } 608 } 609 610 fn verified(bytes: &[u8]) -> aienos_artifact::signature::VerifiedArtifact<'_> { 611 ConfiguredArtifactVerifier::new(&TestAnchorSet, TestSignatureVerifier) 612 .verify(bytes) 613 .unwrap() 614 } 615 616 #[test] 617 fn read_is_granted_and_uses_the_existing_m3_rights_type() { 618 let bytes = signed_test_artifact(Some(request(RIGHT_READ))); 619 let candidate = verified(&bytes); 620 let policy = policy_with(&[request(RIGHT_READ)]); 621 let admission = policy.evaluate(&candidate, available()).unwrap(); 622 assert_eq!(admission.capability_count(), 1); 623 let granted = admission.capabilities()[0].unwrap(); 624 assert_eq!(granted.resource.id(), 7); 625 assert_eq!(granted.rights, Rights::READ); 626 assert!(granted.bounds.is_attenuation_of(&request(RIGHT_READ))); 627 628 let mut task_caps = CapTable::<2>::new(42); 629 let handle = task_caps 630 .insert(granted.resource.id(), granted.rights) 631 .unwrap(); 632 assert_eq!(task_caps.lookup(handle, Rights::READ), Ok(7)); 633 assert_eq!( 634 task_caps.lookup(handle, Rights::WRITE), 635 Err(crate::caps::CapError::MissingRights) 636 ); 637 } 638 639 #[test] 640 fn read_write_request_is_reduced_to_policy_read() { 641 let bytes = signed_test_artifact(Some(request(RIGHT_READ | RIGHT_WRITE))); 642 let candidate = verified(&bytes); 643 let policy = policy_with(&[request(RIGHT_READ)]); 644 let admission = policy.evaluate(&candidate, available()).unwrap(); 645 let granted = admission.capabilities()[0].unwrap(); 646 assert_eq!(granted.rights, Rights::READ); 647 assert!(granted 648 .bounds 649 .is_attenuation_of(&request(RIGHT_READ | RIGHT_WRITE))); 650 } 651 652 #[test] 653 fn unapproved_write_receives_no_capability() { 654 let bytes = signed_test_artifact(Some(request(RIGHT_WRITE))); 655 let candidate = verified(&bytes); 656 let policy = policy_with(&[]); 657 let admission = policy.evaluate(&candidate, available()).unwrap(); 658 assert_eq!(admission.capability_count(), 0); 659 assert!(admission.capabilities().is_empty()); 660 } 661 662 #[test] 663 fn policy_reduces_object_range_and_operation_byte_budgets() { 664 let bytes = signed_test_artifact(Some(request(RIGHT_READ))); 665 let candidate = verified(&bytes); 666 let narrow = CapabilityRequest { 667 max_operations: 2, 668 max_bytes: 2, 669 byte_offset: 1, 670 byte_length: 2, 671 ..request(RIGHT_READ) 672 }; 673 let policy = policy_with(&[narrow]); 674 let admission = policy.evaluate(&candidate, available()).unwrap(); 675 let granted = admission.capabilities()[0].unwrap(); 676 assert_eq!(granted.bounds.max_operations, 2); 677 assert_eq!(granted.bounds.max_bytes, 2); 678 assert_eq!(granted.bounds.byte_offset, 1); 679 assert_eq!(granted.bounds.byte_length, 2); 680 assert!(granted.bounds.is_attenuation_of(&request(RIGHT_READ))); 681 } 682 683 #[test] 684 fn policy_digest_is_canonical_and_binds_signer_allowlist() { 685 let read = request(RIGHT_READ); 686 let other_resource = CapabilityRequest { 687 resource_id: 9, 688 ..read 689 }; 690 let forward = policy_with(&[read, other_resource]); 691 let reverse = policy_with(&[other_resource, read]); 692 assert_eq!(forward.digest(), reverse.digest()); 693 let (canonical, length) = forward.canonical_bytes(); 694 assert_eq!(length, POLICY_HEADER_SIZE + 2 * POLICY_RECORD_SIZE + 32); 695 assert_eq!(&canonical[..8], b"AIENPOL\0"); 696 assert_eq!(u16::from_le_bytes([canonical[60], canonical[61]]), 1); 697 assert_eq!(&canonical[62..64], &[0, 0]); 698 let mut other_signer = AdmissionPolicy::new(forward.limits).unwrap(); 699 other_signer.add_resource_rule(read).unwrap(); 700 other_signer.add_resource_rule(other_resource).unwrap(); 701 other_signer.allow_signer([0x99; 32]).unwrap(); 702 assert_ne!(forward.digest(), other_signer.digest()); 703 } 704 705 #[test] 706 fn insufficient_resource_snapshot_rejects_before_authorization() { 707 let bytes = signed_test_artifact(None); 708 let candidate = verified(&bytes); 709 let policy = policy_with(&[]); 710 let mut insufficient = available(); 711 insufficient.stack_pages = 0; 712 assert_eq!( 713 policy.evaluate(&candidate, insufficient), 714 Err(ArtifactError::ResourceLimit) 715 ); 716 } 717 718 #[test] 719 fn verified_but_policy_untrusted_signer_is_rejected() { 720 let bytes = signed_test_artifact(None); 721 let candidate = verified(&bytes); 722 let policy = AdmissionPolicy::new(AdmissionLimits { 723 code_pages: 1, 724 data_pages: 1, 725 stack_pages: 1, 726 max_capabilities: 1, 727 ipc_messages: 0, 728 ipc_bytes: 0, 729 cpu_ticks: 10, 730 elapsed_ticks: 20, 731 syscall_count: 16, 732 }) 733 .unwrap(); 734 assert_eq!( 735 policy.evaluate(&candidate, available()), 736 Err(ArtifactError::UntrustedSigner) 737 ); 738 } 739 }