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-artifact/src/capability.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-artifact / src / capability.rs` 206 lines ยท 7409 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 use crate::error::ArtifactError; 17 18 pub const CAPABILITY_RECORD_SIZE: usize = 48; 19 pub const MAX_CAPABILITIES: usize = 16; 20 21 pub const RESOURCE_KIND_CHANNEL: u16 = 2; 22 pub const RESOURCE_KIND_OBJECT: u16 = 3; 23 24 pub const RIGHT_READ: u32 = 1 << 0; 25 pub const RIGHT_WRITE: u32 = 1 << 1; 26 pub const RIGHT_MAP: u32 = 1 << 2; 27 pub const RIGHT_GRANT: u32 = 1 << 3; 28 pub const RIGHT_DERIVE: u32 = 1 << 4; 29 pub const RIGHT_REVOKE: u32 = 1 << 5; 30 pub const VALID_RIGHTS: u32 = 31 RIGHT_READ | RIGHT_WRITE | RIGHT_MAP | RIGHT_GRANT | RIGHT_DERIVE | RIGHT_REVOKE; 32 33 #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] 34 pub struct CapabilityRequest { 35 pub resource_kind: u16, 36 pub resource_id: u32, 37 pub rights: u32, 38 pub bounds_kind: u32, 39 pub max_operations: u32, 40 pub max_bytes: u64, 41 pub byte_offset: u64, 42 pub byte_length: u64, 43 } 44 45 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 46 pub struct CapabilityTable { 47 entries: [CapabilityRequest; MAX_CAPABILITIES], 48 len: u8, 49 } 50 51 impl Default for CapabilityTable { 52 fn default() -> Self { 53 Self { 54 entries: [CapabilityRequest::default(); MAX_CAPABILITIES], 55 len: 0, 56 } 57 } 58 } 59 60 impl CapabilityTable { 61 pub const fn len(&self) -> usize { 62 self.len as usize 63 } 64 65 pub const fn is_empty(&self) -> bool { 66 self.len == 0 67 } 68 69 pub fn as_slice(&self) -> &[CapabilityRequest] { 70 &self.entries[..self.len()] 71 } 72 73 pub(crate) fn push(&mut self, request: CapabilityRequest) -> Result<(), ArtifactError> { 74 let index = self.len(); 75 let slot = self 76 .entries 77 .get_mut(index) 78 .ok_or(ArtifactError::ResourceLimit)?; 79 *slot = request; 80 self.len += 1; 81 Ok(()) 82 } 83 } 84 85 impl CapabilityRequest { 86 /// Canonical 48-byte little-endian encoding for policy and grant records. 87 pub fn to_bytes(self) -> [u8; CAPABILITY_RECORD_SIZE] { 88 let mut bytes = [0u8; CAPABILITY_RECORD_SIZE]; 89 bytes[0..2].copy_from_slice(&self.resource_kind.to_le_bytes()); 90 bytes[4..8].copy_from_slice(&self.resource_id.to_le_bytes()); 91 bytes[8..12].copy_from_slice(&self.rights.to_le_bytes()); 92 bytes[12..16].copy_from_slice(&self.bounds_kind.to_le_bytes()); 93 bytes[16..20].copy_from_slice(&self.max_operations.to_le_bytes()); 94 bytes[24..32].copy_from_slice(&self.max_bytes.to_le_bytes()); 95 bytes[32..40].copy_from_slice(&self.byte_offset.to_le_bytes()); 96 bytes[40..48].copy_from_slice(&self.byte_length.to_le_bytes()); 97 bytes 98 } 99 100 pub fn decode(bytes: &[u8]) -> Result { 101 if bytes.len() != CAPABILITY_RECORD_SIZE { 102 return Err(if bytes.len() < CAPABILITY_RECORD_SIZE { 103 ArtifactError::Truncated 104 } else { 105 ArtifactError::WrongLength 106 }); 107 } 108 if read_u16(bytes, 2)? != 0 || read_u32(bytes, 20)? != 0 { 109 return Err(ArtifactError::ReservedNonZero); 110 } 111 112 let request = Self { 113 resource_kind: read_u16(bytes, 0)?, 114 resource_id: read_u32(bytes, 4)?, 115 rights: read_u32(bytes, 8)?, 116 bounds_kind: read_u32(bytes, 12)?, 117 max_operations: read_u32(bytes, 16)?, 118 max_bytes: read_u64(bytes, 24)?, 119 byte_offset: read_u64(bytes, 32)?, 120 byte_length: read_u64(bytes, 40)?, 121 }; 122 request.validate()?; 123 Ok(request) 124 } 125 126 pub fn validate(&self) -> Result<(), ArtifactError> { 127 if !matches!( 128 self.resource_kind, 129 RESOURCE_KIND_OBJECT | RESOURCE_KIND_CHANNEL 130 ) || self.resource_id == 0 131 || self.rights == 0 132 || self.rights & !VALID_RIGHTS != 0 133 || self.max_operations == 0 134 || self.max_bytes == 0 135 { 136 return Err(ArtifactError::MalformedCapability); 137 } 138 139 match (self.resource_kind, self.bounds_kind) { 140 (RESOURCE_KIND_OBJECT, 1) => { 141 if self.byte_offset.checked_add(self.byte_length).is_none() { 142 return Err(ArtifactError::LengthOverflow); 143 } 144 if self.byte_length == 0 || self.max_bytes > self.byte_length { 145 return Err(ArtifactError::MalformedCapability); 146 } 147 } 148 (RESOURCE_KIND_CHANNEL, 2) => { 149 if self.byte_offset != 0 || self.byte_length != 0 { 150 return Err(ArtifactError::MalformedCapability); 151 } 152 } 153 _ => return Err(ArtifactError::MalformedCapability), 154 } 155 Ok(()) 156 } 157 158 /// True only when `self` is no broader than the corresponding request. 159 pub fn is_attenuation_of(&self, requested: &Self) -> bool { 160 if self.validate().is_err() 161 || requested.validate().is_err() 162 || self.resource_kind != requested.resource_kind 163 || self.resource_id != requested.resource_id 164 || self.bounds_kind != requested.bounds_kind 165 || self.rights & !requested.rights != 0 166 || self.max_operations > requested.max_operations 167 || self.max_bytes > requested.max_bytes 168 { 169 return false; 170 } 171 match self.bounds_kind { 172 1 => { 173 let Some(self_end) = self.byte_offset.checked_add(self.byte_length) else { 174 return false; 175 }; 176 let Some(requested_end) = requested.byte_offset.checked_add(requested.byte_length) 177 else { 178 return false; 179 }; 180 self.byte_offset >= requested.byte_offset && self_end <= requested_end 181 } 182 2 => self.byte_offset == 0 && self.byte_length == 0, 183 _ => false, 184 } 185 } 186 } 187 188 fn read_u16(bytes: &[u8], offset: usize) -> Result { 189 let end = offset.checked_add(2).ok_or(ArtifactError::LengthOverflow)?; 190 let value = bytes.get(offset..end).ok_or(ArtifactError::Truncated)?; 191 Ok(u16::from_le_bytes([value[0], value[1]])) 192 } 193 194 fn read_u32(bytes: &[u8], offset: usize) -> Result { 195 let end = offset.checked_add(4).ok_or(ArtifactError::LengthOverflow)?; 196 let value = bytes.get(offset..end).ok_or(ArtifactError::Truncated)?; 197 Ok(u32::from_le_bytes([value[0], value[1], value[2], value[3]])) 198 } 199 200 fn read_u64(bytes: &[u8], offset: usize) -> Result { 201 let end = offset.checked_add(8).ok_or(ArtifactError::LengthOverflow)?; 202 let value = bytes.get(offset..end).ok_or(ArtifactError::Truncated)?; 203 Ok(u64::from_le_bytes([ 204 value[0], value[1], value[2], value[3], value[4], value[5], value[6], value[7], 205 ])) 206 }