aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-kernel / src / pe.rs
| 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 | //! Bounds-checked parser for loaded PE32+ AArch64 section tables. |
| 17 | use crate::mem::map_plan::PeSection; |
| 18 | use alloc::vec::Vec; |
| 19 | |
| 20 | const PAGE: u64 = 4096; |
| 21 | const IMAGE_FILE_MACHINE_ARM64: u16 = 0xaa64; |
| 22 | const PE32_PLUS: u16 = 0x20b; |
| 23 | const EXECUTE: u32 = 0x2000_0000; |
| 24 | const WRITE: u32 = 0x8000_0000; |
| 25 | |
| 26 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 27 | pub enum PeError { |
| 28 | Truncated, |
| 29 | BadDosMagic, |
| 30 | BadPeSignature, |
| 31 | WrongMachine, |
| 32 | WrongOptionalMagic, |
| 33 | Overflow, |
| 34 | Overlap, |
| 35 | WritableExecutable, |
| 36 | } |
| 37 | |
| 38 | fn u16at(b: &[u8], n: usize) -> Result<u16, PeError> { |
| 39 | let s = b |
| 40 | .get(n..n.checked_add(2).ok_or(PeError::Overflow)?) |
| 41 | .ok_or(PeError::Truncated)?; |
| 42 | Ok(u16::from_le_bytes([s[0], s[1]])) |
| 43 | } |
| 44 | fn u32at(b: &[u8], n: usize) -> Result<u32, PeError> { |
| 45 | let s = b |
| 46 | .get(n..n.checked_add(4).ok_or(PeError::Overflow)?) |
| 47 | .ok_or(PeError::Truncated)?; |
| 48 | Ok(u32::from_le_bytes([s[0], s[1], s[2], s[3]])) |
| 49 | } |
| 50 | |
| 51 | /// Parse loaded image sections. Returned virtual addresses are absolute and sizes |
| 52 | /// cover both virtual and raw contents, rounded to 4 KiB. |
| 53 | pub fn parse_pe_sections(image: &[u8], image_base: u64) -> Result<Vec<PeSection>, PeError> { |
| 54 | if u16at(image, 0)? != 0x5a4d { |
| 55 | return Err(PeError::BadDosMagic); |
| 56 | } |
| 57 | let pe = usize::try_from(u32at(image, 0x3c)?).map_err(|_| PeError::Overflow)?; |
| 58 | if image |
| 59 | .get(pe..pe.checked_add(4).ok_or(PeError::Overflow)?) |
| 60 | .ok_or(PeError::Truncated)? |
| 61 | != b"PE\0\0" |
| 62 | { |
| 63 | return Err(PeError::BadPeSignature); |
| 64 | } |
| 65 | let coff = pe.checked_add(4).ok_or(PeError::Overflow)?; |
| 66 | if u16at(image, coff)? != IMAGE_FILE_MACHINE_ARM64 { |
| 67 | return Err(PeError::WrongMachine); |
| 68 | } |
| 69 | let count = usize::from(u16at(image, coff + 2)?); |
| 70 | let optional_size = usize::from(u16at(image, coff + 16)?); |
| 71 | let optional = coff.checked_add(20).ok_or(PeError::Overflow)?; |
| 72 | let optional_end = optional |
| 73 | .checked_add(optional_size) |
| 74 | .ok_or(PeError::Overflow)?; |
| 75 | if image |
| 76 | .get(optional..optional_end) |
| 77 | .ok_or(PeError::Truncated)? |
| 78 | .len() |
| 79 | < 2 |
| 80 | { |
| 81 | return Err(PeError::Truncated); |
| 82 | } |
| 83 | if u16at(image, optional)? != PE32_PLUS { |
| 84 | return Err(PeError::WrongOptionalMagic); |
| 85 | } |
| 86 | let table = optional_end; |
| 87 | let table_len = count.checked_mul(40).ok_or(PeError::Overflow)?; |
| 88 | image |
| 89 | .get(table..table.checked_add(table_len).ok_or(PeError::Overflow)?) |
| 90 | .ok_or(PeError::Truncated)?; |
| 91 | let mut sections = Vec::with_capacity(count); |
| 92 | for i in 0..count { |
| 93 | let h = table + i * 40; |
| 94 | let virtual_size = u64::from(u32at(image, h + 8)?); |
| 95 | let rva = u64::from(u32at(image, h + 12)?); |
| 96 | let raw_size = u64::from(u32at(image, h + 16)?); |
| 97 | let bytes = virtual_size.max(raw_size); |
| 98 | let start = image_base.checked_add(rva).ok_or(PeError::Overflow)?; |
| 99 | let characteristics = u32at(image, h + 36)?; |
| 100 | if characteristics & WRITE != 0 && characteristics & EXECUTE != 0 { |
| 101 | return Err(PeError::WritableExecutable); |
| 102 | } |
| 103 | // Page-round both ends: a section starting mid-page still owns that |
| 104 | // page, and two sections sharing a page must surface as Overlap |
| 105 | // because one page can't carry two different permissions. |
| 106 | let va = start & !(PAGE - 1); |
| 107 | let end = start |
| 108 | .checked_add(bytes) |
| 109 | .and_then(|e| e.checked_add(PAGE - 1)) |
| 110 | .ok_or(PeError::Overflow)? |
| 111 | & !(PAGE - 1); |
| 112 | let size = end - va; |
| 113 | for prior in §ions { |
| 114 | let prior: &PeSection = prior; |
| 115 | let prior_end = prior.va.checked_add(prior.size).ok_or(PeError::Overflow)?; |
| 116 | if size != 0 && prior.size != 0 && va < prior_end && prior.va < end { |
| 117 | return Err(PeError::Overlap); |
| 118 | } |
| 119 | } |
| 120 | sections.push(PeSection { |
| 121 | va, |
| 122 | size, |
| 123 | characteristics, |
| 124 | }); |
| 125 | } |
| 126 | Ok(sections) |
| 127 | } |
| 128 | |
| 129 | #[cfg(test)] |
| 130 | mod tests { |
| 131 | use super::*; |
| 132 | fn image(n: usize) -> Vec<u8> { |
| 133 | let mut b = vec![0; n.max(0x100 + 24 + 240 + 80)]; |
| 134 | b[0..2].copy_from_slice(b"MZ"); |
| 135 | b[0x3c..0x40].copy_from_slice(&0x80u32.to_le_bytes()); |
| 136 | b[0x80..0x84].copy_from_slice(b"PE\0\0"); |
| 137 | b[0x84..0x86].copy_from_slice(&IMAGE_FILE_MACHINE_ARM64.to_le_bytes()); |
| 138 | b[0x86..0x88].copy_from_slice(&2u16.to_le_bytes()); |
| 139 | b[0x94..0x96].copy_from_slice(&240u16.to_le_bytes()); |
| 140 | b[0x98..0x9a].copy_from_slice(&PE32_PLUS.to_le_bytes()); |
| 141 | let t = 0x80 + 24 + 240; |
| 142 | for (i, (rva, vs, raw, c)) in [ |
| 143 | (0x1000u32, 0x1000u32, 0x800u32, 0x6000_0020u32), |
| 144 | (0x2000, 0x800, 0x1000, 0xc000_0040), |
| 145 | ] |
| 146 | .iter() |
| 147 | .enumerate() |
| 148 | { |
| 149 | let h = t + i * 40; |
| 150 | b[h + 8..h + 12].copy_from_slice(&vs.to_le_bytes()); |
| 151 | b[h + 12..h + 16].copy_from_slice(&rva.to_le_bytes()); |
| 152 | b[h + 16..h + 20].copy_from_slice(&raw.to_le_bytes()); |
| 153 | b[h + 36..h + 40].copy_from_slice(&c.to_le_bytes()); |
| 154 | } |
| 155 | b |
| 156 | } |
| 157 | #[test] |
| 158 | fn valid_sections_use_base_and_page_rounded_max_size() { |
| 159 | let s = parse_pe_sections(&image(0), 0x400000).unwrap(); |
| 160 | assert_eq!( |
| 161 | s[0], |
| 162 | PeSection { |
| 163 | va: 0x401000, |
| 164 | size: 4096, |
| 165 | characteristics: 0x6000_0020 |
| 166 | } |
| 167 | ); |
| 168 | assert_eq!(s[1].size, 4096); |
| 169 | } |
| 170 | #[test] |
| 171 | fn rejects_truncation_and_invalid_headers() { |
| 172 | let mut b = image(0); |
| 173 | assert_eq!(parse_pe_sections(&b[..10], 0), Err(PeError::Truncated)); |
| 174 | assert_eq!(parse_pe_sections(&b[..0x90], 0), Err(PeError::Truncated)); |
| 175 | let table_end = 0x80 + 24 + 240 + 40; |
| 176 | assert_eq!( |
| 177 | parse_pe_sections(&b[..table_end], 0), |
| 178 | Err(PeError::Truncated) |
| 179 | ); |
| 180 | b[0] = 0; |
| 181 | assert_eq!(parse_pe_sections(&b, 0), Err(PeError::BadDosMagic)); |
| 182 | let mut b = image(0); |
| 183 | b[0x80] = 0; |
| 184 | assert_eq!(parse_pe_sections(&b, 0), Err(PeError::BadPeSignature)); |
| 185 | let mut b = image(0); |
| 186 | b[0x84] = 0; |
| 187 | assert_eq!(parse_pe_sections(&b, 0), Err(PeError::WrongMachine)); |
| 188 | let mut b = image(0); |
| 189 | b[0x98] = 0; |
| 190 | assert_eq!(parse_pe_sections(&b, 0), Err(PeError::WrongOptionalMagic)); |
| 191 | } |
| 192 | #[test] |
| 193 | fn rejects_wx_and_overlapping_ranges() { |
| 194 | let mut b = image(0); |
| 195 | let t = 0x80 + 24 + 240; |
| 196 | b[t + 36..t + 40].copy_from_slice(&0xa000_0000u32.to_le_bytes()); |
| 197 | assert_eq!(parse_pe_sections(&b, 0), Err(PeError::WritableExecutable)); |
| 198 | let mut b = image(0); |
| 199 | b[t + 40 + 12..t + 40 + 16].copy_from_slice(&0x1800u32.to_le_bytes()); |
| 200 | assert_eq!(parse_pe_sections(&b, 0), Err(PeError::Overlap)); |
| 201 | } |
| 202 | #[test] |
| 203 | fn unaligned_sections_are_page_rounded_and_shared_pages_overlap() { |
| 204 | // One section at RVA 0x1001, 1 byte: must cover the whole page 0x1000..0x2000. |
| 205 | let mut b = image(0x1000); |
| 206 | let t = 0x80 + 24 + 240; |
| 207 | b[0x86..0x88].copy_from_slice(&1u16.to_le_bytes()); |
| 208 | b[t + 8..t + 12].copy_from_slice(&1u32.to_le_bytes()); |
| 209 | b[t + 12..t + 16].copy_from_slice(&0x1001u32.to_le_bytes()); |
| 210 | b[t + 16..t + 20].copy_from_slice(&0u32.to_le_bytes()); |
| 211 | b[t + 36..t + 40].copy_from_slice(&0x4000_0040u32.to_le_bytes()); |
| 212 | let s = parse_pe_sections(&b, 0x4000_0000).unwrap(); |
| 213 | assert_eq!((s[0].va, s[0].size), (0x4000_1000, 0x1000)); |
| 214 | // A second section in the same page (RVA 0x1800) is a shared page: Overlap. |
| 215 | b[0x86..0x88].copy_from_slice(&2u16.to_le_bytes()); |
| 216 | let t2 = t + 40; |
| 217 | b[t2 + 8..t2 + 12].copy_from_slice(&0x100u32.to_le_bytes()); |
| 218 | b[t2 + 12..t2 + 16].copy_from_slice(&0x1800u32.to_le_bytes()); |
| 219 | b[t2 + 16..t2 + 20].copy_from_slice(&0u32.to_le_bytes()); |
| 220 | b[t2 + 36..t2 + 40].copy_from_slice(&0x6000_0020u32.to_le_bytes()); |
| 221 | assert_eq!(parse_pe_sections(&b, 0x4000_0000), Err(PeError::Overlap)); |
| 222 | } |
| 223 | } |