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-evidence/src/pe.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-evidence / src / pe.rs` 78 lines ยท 3002 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 //! Structural check that a file is an AArch64 PE32+ EFI application. 17 //! This verifies the artifact format only, not that the image boots. 18 19 fn u16_at(data: &[u8], at: usize) -> Option { 20 Some(u16::from_le_bytes(data.get(at..at + 2)?.try_into().ok()?)) 21 } 22 23 fn u32_at(data: &[u8], at: usize) -> Option { 24 Some(u32::from_le_bytes(data.get(at..at + 4)?.try_into().ok()?)) 25 } 26 27 const MACHINE_ARM64: u16 = 0xAA64; 28 const PE32_PLUS: u16 = 0x20B; 29 const SUBSYSTEM_EFI_APPLICATION: u16 = 10; 30 31 pub fn verify_efi_application(data: &[u8]) -> Result<(), String> { 32 if data.len() < 0x100 || &data[..2] != b"MZ" { 33 return Err("missing DOS image header".into()); 34 } 35 let pe = u32_at(data, 0x3C).ok_or("truncated DOS header")? as usize; 36 if data.get(pe..pe + 4) != Some(b"PE\0\0".as_slice()) { 37 return Err("missing PE header".into()); 38 } 39 let optional = pe + 4 + 20; 40 let machine = u16_at(data, pe + 4).ok_or("truncated COFF header")?; 41 let magic = u16_at(data, optional).ok_or("truncated optional header")?; 42 let subsystem = u16_at(data, optional + 68).ok_or("truncated optional header")?; 43 if (machine, magic, subsystem) != (MACHINE_ARM64, PE32_PLUS, SUBSYSTEM_EFI_APPLICATION) { 44 return Err(format!( 45 "unexpected machine/PE/subsystem: {machine:#x}/{magic:#x}/{subsystem}" 46 )); 47 } 48 Ok(()) 49 } 50 51 #[cfg(test)] 52 mod tests { 53 use super::*; 54 55 fn image(machine: u16, subsystem: u16) -> Vec { 56 let mut d = vec![0u8; 0x200]; 57 d[..2].copy_from_slice(b"MZ"); 58 d[0x3C..0x40].copy_from_slice(&0x80u32.to_le_bytes()); 59 d[0x80..0x84].copy_from_slice(b"PE\0\0"); 60 d[0x84..0x86].copy_from_slice(&machine.to_le_bytes()); 61 let optional = 0x80 + 24; 62 d[optional..optional + 2].copy_from_slice(&PE32_PLUS.to_le_bytes()); 63 d[optional + 68..optional + 70].copy_from_slice(&subsystem.to_le_bytes()); 64 d 65 } 66 67 #[test] 68 fn accepts_arm64_efi_application() { 69 assert!(verify_efi_application(&image(MACHINE_ARM64, 10)).is_ok()); 70 } 71 72 #[test] 73 fn rejects_wrong_machine_subsystem_and_garbage() { 74 assert!(verify_efi_application(&image(0x8664, 10)).is_err()); 75 assert!(verify_efi_application(&image(MACHINE_ARM64, 3)).is_err()); 76 assert!(verify_efi_application(b"not an image").is_err()); 77 } 78 }