interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / crates / aienos-artifact / src / format.rs

350 lines · 13323 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
16use core::convert::TryFrom;
17
18use crate::capability::{
19 CapabilityRequest, CapabilityTable, CAPABILITY_RECORD_SIZE, MAX_CAPABILITIES,
20};
21use crate::error::ArtifactError;
22use crate::resource::{ResourceEnvelope, PAGE_SIZE, RESOURCE_ENVELOPE_SIZE};
23use crate::signature::SignatureBlock;
24
25pub const ARTIFACT_MAGIC: &[u8; 8] = b"AIENART\0";
26pub const FORMAT_VERSION: u16 = 0;
27pub const HEADER_SIZE: usize = 128;
28pub const SECTION_TABLE_OFFSET: usize = HEADER_SIZE;
29pub const SECTION_COUNT: usize = 2;
30pub const SECTION_RECORD_SIZE: usize = 32;
31pub const SECTION_TABLE_SIZE: usize = SECTION_COUNT * SECTION_RECORD_SIZE;
32pub const CAPABILITY_TABLE_OFFSET: usize = HEADER_SIZE + SECTION_TABLE_SIZE;
33pub const SIGNATURE_BLOCK_SIZE: usize = 100;
34pub const SIGNATURE_ALGORITHM_ED25519: u16 = 1;
35pub const MAX_ARTIFACT_SIZE: usize = 16 * 1024 * 1024;
36pub const MAX_PAYLOAD_SIZE: usize = 8 * 1024 * 1024;
37pub const TARGET_AARCH64_LE: u16 = 1;
38pub const ABI_V1: u16 = 1;
39
40pub const SECTION_KIND_CODE: u16 = 1;
41pub const SECTION_KIND_DATA: u16 = 2;
42pub const PERMISSIONS_RX: u16 = 0b101;
43pub const PERMISSIONS_RW: u16 = 0b011;
44
45#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
46pub struct Section {
47 pub kind: u16,
48 pub permissions: u16,
49 pub payload_relative_offset: u32,
50 pub file_length: u32,
51 pub memory_length: u32,
52 pub alignment: u32,
53}
54
55#[derive(Clone, Copy, Debug, Eq, PartialEq)]
56pub struct Artifact<'a> {
57 pub target_arch: u16,
58 pub abi_version: u16,
59 pub entry_offset: u32,
60 pub sections: [Section; SECTION_COUNT],
61 pub capabilities: CapabilityTable,
62 pub resources: ResourceEnvelope,
63 pub capability_request_bytes: &'a [u8],
64 pub resource_envelope_bytes: &'a [u8],
65 pub payload: &'a [u8],
66 pub unsigned_bytes: &'a [u8],
67 pub signer_fingerprint: &'a [u8; 32],
68 pub signature: &'a [u8; 64],
69 pub signature_algorithm: u16,
70}
71
72impl<'a> Artifact<'a> {
73 pub fn parse(bytes: &'a [u8]) -> Result<Self, ArtifactError> {
74 if bytes.len() < HEADER_SIZE {
75 return Err(ArtifactError::Truncated);
76 }
77 if bytes.len() > MAX_ARTIFACT_SIZE {
78 return Err(ArtifactError::ResourceLimit);
79 }
80 if get(bytes, 0, 8)? != ARTIFACT_MAGIC {
81 return Err(ArtifactError::BadMagic);
82 }
83 if read_u16(bytes, 8)? != FORMAT_VERSION || read_u16(bytes, 10)? as usize != HEADER_SIZE {
84 return Err(ArtifactError::UnsupportedVersion);
85 }
86
87 let target_arch = read_u16(bytes, 12)?;
88 if target_arch != TARGET_AARCH64_LE {
89 return Err(ArtifactError::WrongTarget);
90 }
91 let abi_version = read_u16(bytes, 14)?;
92 if abi_version != ABI_V1 {
93 return Err(ArtifactError::WrongAbi);
94 }
95 if read_u32(bytes, 16)? != 0 {
96 return Err(ArtifactError::UnsupportedFlags);
97 }
98
99 let total_length = usize_from_u32(read_u32(bytes, 20)?)?;
100 if total_length != bytes.len() {
101 return Err(ArtifactError::WrongLength);
102 }
103 if read_u32(bytes, 24)? as usize != SECTION_TABLE_OFFSET
104 || read_u16(bytes, 28)? as usize != SECTION_COUNT
105 || read_u16(bytes, 30)? as usize != SECTION_RECORD_SIZE
106 || read_u16(bytes, 32)? != 0
107 || read_u16(bytes, 34)? != 0
108 {
109 return Err(ArtifactError::UnsupportedVersion);
110 }
111 let entry_offset = read_u32(bytes, 36)?;
112 if read_u32(bytes, 40)? as usize != CAPABILITY_TABLE_OFFSET
113 || read_u16(bytes, 46)? as usize != CAPABILITY_RECORD_SIZE
114 || read_u16(bytes, 52)? as usize != RESOURCE_ENVELOPE_SIZE
115 || read_u16(bytes, 54)? != 0
116 {
117 return Err(ArtifactError::UnsupportedVersion);
118 }
119 if !all_zero(get(bytes, 72, 56)?) {
120 return Err(ArtifactError::ReservedNonZero);
121 }
122
123 let capability_count = read_u16(bytes, 44)? as usize;
124 if capability_count > MAX_CAPABILITIES {
125 return Err(ArtifactError::ResourceLimit);
126 }
127 let capability_bytes_len = capability_count
128 .checked_mul(CAPABILITY_RECORD_SIZE)
129 .ok_or(ArtifactError::LengthOverflow)?;
130 let capability_end = CAPABILITY_TABLE_OFFSET
131 .checked_add(capability_bytes_len)
132 .ok_or(ArtifactError::LengthOverflow)?;
133 let resource_offset = usize_from_u32(read_u32(bytes, 48)?)?;
134 if resource_offset != capability_end {
135 return Err(ArtifactError::SectionOverlap);
136 }
137 let resource_end = resource_offset
138 .checked_add(RESOURCE_ENVELOPE_SIZE)
139 .ok_or(ArtifactError::LengthOverflow)?;
140 let expected_payload_offset = align_up_16(resource_end)?;
141 let payload_offset = usize_from_u32(read_u32(bytes, 56)?)?;
142 if payload_offset != expected_payload_offset {
143 return Err(ArtifactError::SectionOverlap);
144 }
145 if !all_zero(get(bytes, resource_end, payload_offset - resource_end)?) {
146 return Err(ArtifactError::ReservedNonZero);
147 }
148
149 let payload_length = usize_from_u32(read_u32(bytes, 60)?)?;
150 if payload_length > MAX_PAYLOAD_SIZE {
151 return Err(ArtifactError::ResourceLimit);
152 }
153 let signature_offset = usize_from_u32(read_u32(bytes, 64)?)?;
154 let signature_length = usize::from(read_u16(bytes, 68)?);
155 let signature_algorithm = read_u16(bytes, 70)?;
156 if signature_length != SIGNATURE_BLOCK_SIZE
157 || signature_algorithm != SIGNATURE_ALGORITHM_ED25519
158 {
159 return Err(ArtifactError::BadSignatureFormat);
160 }
161 let expected_signature_offset = payload_offset
162 .checked_add(payload_length)
163 .ok_or(ArtifactError::LengthOverflow)?;
164 if signature_offset != expected_signature_offset {
165 return Err(ArtifactError::SectionOverlap);
166 }
167 let expected_total = signature_offset
168 .checked_add(SIGNATURE_BLOCK_SIZE)
169 .ok_or(ArtifactError::LengthOverflow)?;
170 if expected_total != total_length {
171 return Err(ArtifactError::WrongLength);
172 }
173
174 let mut sections = [Section::default(); SECTION_COUNT];
175 let section_table_end = SECTION_TABLE_OFFSET
176 .checked_add(SECTION_TABLE_SIZE)
177 .ok_or(ArtifactError::LengthOverflow)?;
178 if section_table_end != CAPABILITY_TABLE_OFFSET {
179 return Err(ArtifactError::SectionOverlap);
180 }
181 for (index, section) in sections.iter_mut().enumerate() {
182 let offset = SECTION_TABLE_OFFSET
183 .checked_add(
184 index
185 .checked_mul(SECTION_RECORD_SIZE)
186 .ok_or(ArtifactError::LengthOverflow)?,
187 )
188 .ok_or(ArtifactError::LengthOverflow)?;
189 *section = decode_section(get(bytes, offset, SECTION_RECORD_SIZE)?)?;
190 }
191 validate_sections(&sections, payload_length)?;
192
193 let mut capabilities = CapabilityTable::default();
194 let mut previous: Option<(u16, u32)> = None;
195 for index in 0..capability_count {
196 let offset = CAPABILITY_TABLE_OFFSET
197 .checked_add(
198 index
199 .checked_mul(CAPABILITY_RECORD_SIZE)
200 .ok_or(ArtifactError::LengthOverflow)?,
201 )
202 .ok_or(ArtifactError::LengthOverflow)?;
203 let request = CapabilityRequest::decode(get(bytes, offset, CAPABILITY_RECORD_SIZE)?)?;
204 let key = (request.resource_kind, request.resource_id);
205 if previous.is_some_and(|p| p >= key) {
206 return Err(ArtifactError::MalformedCapability);
207 }
208 previous = Some(key);
209 capabilities.push(request)?;
210 }
211
212 let resource_envelope_bytes = get(bytes, resource_offset, RESOURCE_ENVELOPE_SIZE)?;
213 let resources = ResourceEnvelope::decode(resource_envelope_bytes, capability_count)?;
214 if sections[0].memory_length > resources.code_capacity_bytes()?
215 || sections[1].memory_length > resources.data_capacity_bytes()?
216 {
217 return Err(ArtifactError::ResourceLimit);
218 }
219 if entry_offset & 3 != 0 {
220 return Err(ArtifactError::BadEntryPoint);
221 }
222 let entry_end = entry_offset
223 .checked_add(4)
224 .ok_or(ArtifactError::LengthOverflow)?;
225 if entry_end > sections[0].file_length {
226 return Err(ArtifactError::BadEntryPoint);
227 }
228
229 let payload_end = payload_offset
230 .checked_add(payload_length)
231 .ok_or(ArtifactError::LengthOverflow)?;
232 let payload = get(bytes, payload_offset, payload_length)?;
233 let signature_block =
234 SignatureBlock::decode(get(bytes, signature_offset, SIGNATURE_BLOCK_SIZE)?)?;
235 if signature_block.algorithm != signature_algorithm {
236 return Err(ArtifactError::BadSignatureFormat);
237 }
238 if payload_end != signature_offset {
239 return Err(ArtifactError::SectionOverlap);
240 }
241
242 Ok(Self {
243 target_arch,
244 abi_version,
245 entry_offset,
246 sections,
247 capabilities,
248 resources,
249 capability_request_bytes: get(bytes, CAPABILITY_TABLE_OFFSET, capability_bytes_len)?,
250 resource_envelope_bytes,
251 payload,
252 unsigned_bytes: get(bytes, 0, signature_offset)?,
253 signer_fingerprint: signature_block.signer_fingerprint,
254 signature: signature_block.signature,
255 signature_algorithm,
256 })
257 }
258
259 pub fn code_bytes(&self) -> &[u8] {
260 &self.payload[..self.sections[0].file_length as usize]
261 }
262
263 pub fn data_bytes(&self) -> &[u8] {
264 let start = self.sections[1].payload_relative_offset as usize;
265 let end = start + self.sections[1].file_length as usize;
266 &self.payload[start..end]
267 }
268}
269
270fn decode_section(bytes: &[u8]) -> Result<Section, ArtifactError> {
271 if bytes.len() != SECTION_RECORD_SIZE {
272 return Err(ArtifactError::Truncated);
273 }
274 if read_u32(bytes, 4)? != 0 || !all_zero(get(bytes, 24, 8)?) {
275 return Err(ArtifactError::ReservedNonZero);
276 }
277 Ok(Section {
278 kind: read_u16(bytes, 0)?,
279 permissions: read_u16(bytes, 2)?,
280 payload_relative_offset: read_u32(bytes, 8)?,
281 file_length: read_u32(bytes, 12)?,
282 memory_length: read_u32(bytes, 16)?,
283 alignment: read_u32(bytes, 20)?,
284 })
285}
286
287fn validate_sections(
288 sections: &[Section; SECTION_COUNT],
289 payload_length: usize,
290) -> Result<(), ArtifactError> {
291 let code = sections[0];
292 let data = sections[1];
293 if code.kind != SECTION_KIND_CODE
294 || code.permissions != PERMISSIONS_RX
295 || code.payload_relative_offset != 0
296 || code.file_length == 0
297 || code.memory_length != code.file_length
298 || code.alignment != PAGE_SIZE
299 {
300 return Err(ArtifactError::BadSection);
301 }
302 if data.kind != SECTION_KIND_DATA
303 || data.permissions != PERMISSIONS_RW
304 || data.payload_relative_offset != code.file_length
305 || data.file_length == 0
306 || data.memory_length < data.file_length
307 || data.alignment != PAGE_SIZE
308 {
309 return Err(ArtifactError::BadSection);
310 }
311 let end = u64::from(code.file_length)
312 .checked_add(u64::from(data.file_length))
313 .ok_or(ArtifactError::LengthOverflow)?;
314 if end != payload_length as u64 {
315 return Err(ArtifactError::SectionOverlap);
316 }
317 Ok(())
318}
319
320fn align_up_16(value: usize) -> Result<usize, ArtifactError> {
321 value
322 .checked_add(15)
323 .map(|v| v & !15)
324 .ok_or(ArtifactError::LengthOverflow)
325}
326
327fn usize_from_u32(value: u32) -> Result<usize, ArtifactError> {
328 usize::try_from(value).map_err(|_| ArtifactError::LengthOverflow)
329}
330
331fn all_zero(bytes: &[u8]) -> bool {
332 bytes.iter().all(|byte| *byte == 0)
333}
334
335fn get(bytes: &[u8], offset: usize, length: usize) -> Result<&[u8], ArtifactError> {
336 let end = offset
337 .checked_add(length)
338 .ok_or(ArtifactError::LengthOverflow)?;
339 bytes.get(offset..end).ok_or(ArtifactError::Truncated)
340}
341
342fn read_u16(bytes: &[u8], offset: usize) -> Result<u16, ArtifactError> {
343 let value = get(bytes, offset, 2)?;
344 Ok(u16::from_le_bytes([value[0], value[1]]))
345}
346
347fn read_u32(bytes: &[u8], offset: usize) -> Result<u32, ArtifactError> {
348 let value = get(bytes, offset, 4)?;
349 Ok(u32::from_le_bytes([value[0], value[1], value[2], value[3]]))
350}