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 / ipc.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 | //! Typed kernel-owned message channels, gated by capability handles. |
| 17 | //! |
| 18 | //! The channel ring, the object bytes and every principal's capability table |
| 19 | //! are kernel-owned. A task can only touch a channel or object through a |
| 20 | //! handle in its own table with the required right, so there is no global |
| 21 | //! lookup that bypasses caller authority. Delegation uses |
| 22 | //! [`crate::caps::CapTable::derive_into`], which links the child to its parent |
| 23 | //! for tree revocation: attenuating a capability and later revoking the |
| 24 | //! ancestor invalidates every derived copy. |
| 25 | |
| 26 | use crate::caps::{CapError, CapTable, Handle, Rights}; |
| 27 | use core::sync::atomic::{AtomicU32, Ordering}; |
| 28 | |
| 29 | pub use crate::abi::{ChannelId, MemoryRegion, Message, ObjectId, TaskId}; |
| 30 | |
| 31 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 32 | pub enum ChannelError { |
| 33 | Full, |
| 34 | Empty, |
| 35 | } |
| 36 | |
| 37 | /// Bounded FIFO ring of fixed-size messages. Deterministic: send appends at |
| 38 | /// `tail`, receive pops from `head`, no allocation, no dropping. |
| 39 | pub struct Channel<const N: usize> { |
| 40 | ring: [Message; N], |
| 41 | head: usize, |
| 42 | len: usize, |
| 43 | } |
| 44 | |
| 45 | impl<const N: usize> Channel<N> { |
| 46 | pub const fn new() -> Self { |
| 47 | Self { |
| 48 | ring: [Message::NEW; N], |
| 49 | head: 0, |
| 50 | len: 0, |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | pub fn send(&mut self, message: Message) -> Result<(), ChannelError> { |
| 55 | if self.len == N { |
| 56 | return Err(ChannelError::Full); |
| 57 | } |
| 58 | let tail = (self.head + self.len) % N; |
| 59 | self.ring[tail] = message; |
| 60 | self.len += 1; |
| 61 | Ok(()) |
| 62 | } |
| 63 | |
| 64 | pub fn receive(&mut self) -> Result<Message, ChannelError> { |
| 65 | if self.len == 0 { |
| 66 | return Err(ChannelError::Empty); |
| 67 | } |
| 68 | let message = self.ring[self.head]; |
| 69 | self.head = (self.head + 1) % N; |
| 70 | self.len -= 1; |
| 71 | Ok(message) |
| 72 | } |
| 73 | |
| 74 | pub fn len(&self) -> usize { |
| 75 | self.len |
| 76 | } |
| 77 | |
| 78 | pub fn is_empty(&self) -> bool { |
| 79 | self.len == 0 |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | impl<const N: usize> Default for Channel<N> { |
| 84 | fn default() -> Self { |
| 85 | Self::new() |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 90 | pub enum IpcError { |
| 91 | ChannelFull, |
| 92 | ChannelEmpty, |
| 93 | MissingRights, |
| 94 | InvalidHandle, |
| 95 | WrongResource, |
| 96 | DelegationFailed, |
| 97 | } |
| 98 | |
| 99 | impl From<CapError> for IpcError { |
| 100 | fn from(error: CapError) -> Self { |
| 101 | match error { |
| 102 | CapError::Full => IpcError::DelegationFailed, |
| 103 | CapError::InvalidHandle => IpcError::InvalidHandle, |
| 104 | CapError::MissingRights => IpcError::MissingRights, |
| 105 | CapError::RightsEscalation => IpcError::MissingRights, |
| 106 | CapError::DuplicateTable => IpcError::InvalidHandle, |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /// Capability resource IDs. A channel capability carries the channel ID as its |
| 112 | /// resource; an object capability carries [`OBJECT_RESOURCE`]. |
| 113 | pub const CHANNEL_RESOURCE: u32 = 0x4950_4348; |
| 114 | pub const OBJECT_RESOURCE: u32 = 0x4950_4F42; |
| 115 | |
| 116 | pub const PRINCIPAL_A: u32 = 0x4500_0041; |
| 117 | pub const PRINCIPAL_B: u32 = 0x4500_0042; |
| 118 | |
| 119 | pub const CHANNEL: ChannelId = ChannelId(0); |
| 120 | pub const CHANNEL_CAPACITY: usize = 8; |
| 121 | pub const TABLE_CAPACITY: usize = 8; |
| 122 | |
| 123 | /// Kernel-owned object bytes reachable only through object capabilities. |
| 124 | pub const OBJECT_SLOTS: usize = 4; |
| 125 | |
| 126 | // Kernel-owned IPC state. Touched only from EL1 dispatchers. |
| 127 | static mut CHANNELS: [Channel<CHANNEL_CAPACITY>; 2] = [Channel::new(), Channel::new()]; |
| 128 | static mut OBJECT: [u64; OBJECT_SLOTS] = [0; OBJECT_SLOTS]; |
| 129 | static mut PRINCIPAL_TABLES: [CapTable<TABLE_CAPACITY>; 2] = |
| 130 | [CapTable::new(PRINCIPAL_A), CapTable::new(PRINCIPAL_B)]; |
| 131 | static ACTIVE_PRINCIPAL: AtomicU32 = AtomicU32::new(0); |
| 132 | |
| 133 | fn principal_index(id: u32) -> Option<usize> { |
| 134 | match id { |
| 135 | PRINCIPAL_A => Some(0), |
| 136 | PRINCIPAL_B => Some(1), |
| 137 | _ => None, |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /// Reset every kernel-owned IPC structure and provision the two principals: |
| 142 | /// A holds the channel send right plus a full-rights object capability; |
| 143 | /// B holds only the channel receive right. Returns A's channel and object |
| 144 | /// handles and B's channel handle. |
| 145 | /// |
| 146 | /// # Safety |
| 147 | /// Caller must have exclusive access to the kernel IPC state (EL1, single CPU). |
| 148 | pub unsafe fn reset() -> (Handle, Handle, Handle) { |
| 149 | unsafe { CHANNELS[CHANNEL.0 as usize] = Channel::new() }; |
| 150 | unsafe { |
| 151 | OBJECT = [0xc0ff_ee01, 0x0bad_c0de, 0x1234_5678, 0x9abc_def0]; |
| 152 | PRINCIPAL_TABLES = [CapTable::new(PRINCIPAL_A), CapTable::new(PRINCIPAL_B)]; |
| 153 | } |
| 154 | ACTIVE_PRINCIPAL.store(PRINCIPAL_A, Ordering::Release); |
| 155 | let send = unsafe { principal_table_at(0) } |
| 156 | .insert(CHANNEL_RESOURCE, Rights::WRITE) |
| 157 | .expect("principal A send capability"); |
| 158 | let object = unsafe { principal_table_at(0) } |
| 159 | .insert( |
| 160 | OBJECT_RESOURCE, |
| 161 | Rights::READ | Rights::WRITE | Rights::DERIVE | Rights::REVOKE, |
| 162 | ) |
| 163 | .expect("principal A object capability"); |
| 164 | let recv = unsafe { principal_table_at(1) } |
| 165 | .insert(CHANNEL_RESOURCE, Rights::READ) |
| 166 | .expect("principal B receive capability"); |
| 167 | (send, object, recv) |
| 168 | } |
| 169 | |
| 170 | /// Record the principal about to run at EL0; syscall dispatch resolves |
| 171 | /// handles against that principal's table. |
| 172 | pub fn set_active_principal(id: TaskId) { |
| 173 | ACTIVE_PRINCIPAL.store(id.0, Ordering::Release); |
| 174 | } |
| 175 | |
| 176 | /// # Safety |
| 177 | /// The active principal's table must not be aliased elsewhere. |
| 178 | pub unsafe fn with_active_table<R>( |
| 179 | f: impl FnOnce(&mut CapTable<TABLE_CAPACITY>) -> Result<R, IpcError>, |
| 180 | ) -> Result<R, IpcError> { |
| 181 | let index = |
| 182 | principal_index(ACTIVE_PRINCIPAL.load(Ordering::Acquire)).ok_or(IpcError::InvalidHandle)?; |
| 183 | let table = unsafe { (&raw mut PRINCIPAL_TABLES[index]).as_mut().unwrap() }; |
| 184 | f(table) |
| 185 | } |
| 186 | |
| 187 | fn channel_table() -> &'static mut Channel<CHANNEL_CAPACITY> { |
| 188 | // SAFETY: EL1-only, single CPU, bounded channel set. |
| 189 | unsafe { (&raw mut CHANNELS[CHANNEL.0 as usize]).as_mut().unwrap() } |
| 190 | } |
| 191 | |
| 192 | fn gated( |
| 193 | table: &CapTable<TABLE_CAPACITY>, |
| 194 | handle: Handle, |
| 195 | right: Rights, |
| 196 | resource: u32, |
| 197 | ) -> Result<(), IpcError> { |
| 198 | match table.lookup(handle, right) { |
| 199 | Ok(found) if found == resource => Ok(()), |
| 200 | Ok(_) => Err(IpcError::WrongResource), |
| 201 | Err(CapError::MissingRights) => Err(IpcError::MissingRights), |
| 202 | Err(_) => Err(IpcError::InvalidHandle), |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /// Send a typed message on a channel the caller holds with WRITE right. |
| 207 | pub fn channel_send( |
| 208 | table: &mut CapTable<TABLE_CAPACITY>, |
| 209 | handle: Handle, |
| 210 | message: Message, |
| 211 | ) -> Result<(), IpcError> { |
| 212 | gated(table, handle, Rights::WRITE, CHANNEL_RESOURCE)?; |
| 213 | channel_table() |
| 214 | .send(message) |
| 215 | .map_err(|_| IpcError::ChannelFull) |
| 216 | } |
| 217 | |
| 218 | /// Receive a typed message from a channel the caller holds with READ right. |
| 219 | pub fn channel_receive( |
| 220 | table: &mut CapTable<TABLE_CAPACITY>, |
| 221 | handle: Handle, |
| 222 | ) -> Result<Message, IpcError> { |
| 223 | gated(table, handle, Rights::READ, CHANNEL_RESOURCE)?; |
| 224 | channel_table() |
| 225 | .receive() |
| 226 | .map_err(|_| IpcError::ChannelEmpty) |
| 227 | } |
| 228 | |
| 229 | /// Delegate an object capability into another principal's table with |
| 230 | /// attenuated rights. Requires DERIVE on the source; escalation is rejected by |
| 231 | /// the capability table. The child keeps a parent link for tree revocation. |
| 232 | pub fn delegate_object( |
| 233 | from: &mut CapTable<TABLE_CAPACITY>, |
| 234 | handle: Handle, |
| 235 | to: &mut CapTable<TABLE_CAPACITY>, |
| 236 | rights: Rights, |
| 237 | ) -> Result<Handle, IpcError> { |
| 238 | gated(from, handle, Rights::READ, OBJECT_RESOURCE)?; |
| 239 | if !rights.is_subset_of(Rights::READ | Rights::WRITE) { |
| 240 | return Err(IpcError::MissingRights); |
| 241 | } |
| 242 | CapTable::derive_into(from, handle, to, rights).map_err(IpcError::from) |
| 243 | } |
| 244 | |
| 245 | /// Read one word of a kernel object through a READ capability. |
| 246 | pub fn object_read( |
| 247 | table: &CapTable<TABLE_CAPACITY>, |
| 248 | handle: Handle, |
| 249 | offset: usize, |
| 250 | ) -> Result<u64, IpcError> { |
| 251 | gated(table, handle, Rights::READ, OBJECT_RESOURCE)?; |
| 252 | if offset >= OBJECT_SLOTS { |
| 253 | return Err(IpcError::WrongResource); |
| 254 | } |
| 255 | // SAFETY: EL1-only state; bounds checked above. |
| 256 | Ok(unsafe { OBJECT[offset] }) |
| 257 | } |
| 258 | |
| 259 | /// Write one word of a kernel object through a WRITE capability. |
| 260 | pub fn object_write( |
| 261 | table: &mut CapTable<TABLE_CAPACITY>, |
| 262 | handle: Handle, |
| 263 | offset: usize, |
| 264 | value: u64, |
| 265 | ) -> Result<(), IpcError> { |
| 266 | gated(table, handle, Rights::WRITE, OBJECT_RESOURCE)?; |
| 267 | if offset >= OBJECT_SLOTS { |
| 268 | return Err(IpcError::WrongResource); |
| 269 | } |
| 270 | // SAFETY: EL1-only state; bounds checked above. |
| 271 | unsafe { OBJECT[offset] = value }; |
| 272 | Ok(()) |
| 273 | } |
| 274 | |
| 275 | /// Revoke an ancestor object capability; every derived child in the supplied |
| 276 | /// table set becomes invalid. |
| 277 | /// |
| 278 | /// # Safety |
| 279 | /// Exclusive access to the kernel IPC state (EL1, single CPU). |
| 280 | pub unsafe fn revoke_object_ancestor(owner: u32, handle: Handle) -> Result<(), IpcError> { |
| 281 | let mut tables = [ |
| 282 | unsafe { (&raw mut PRINCIPAL_TABLES[0]).as_mut().unwrap() }, |
| 283 | unsafe { (&raw mut PRINCIPAL_TABLES[1]).as_mut().unwrap() }, |
| 284 | ]; |
| 285 | CapTable::<TABLE_CAPACITY>::revoke(owner, handle, &mut tables).map_err(IpcError::from) |
| 286 | } |
| 287 | |
| 288 | /// Look a raw handle up in the active principal's table for `right`, bounded |
| 289 | /// to `resource`. The EL0 entry point for every object/channel operation. |
| 290 | /// |
| 291 | /// # Safety |
| 292 | /// Exclusive access to the kernel IPC state (EL1, single CPU). |
| 293 | pub unsafe fn active_lookup(handle: Handle, right: Rights, resource: u32) -> Result<(), IpcError> { |
| 294 | let index = |
| 295 | principal_index(ACTIVE_PRINCIPAL.load(Ordering::Acquire)).ok_or(IpcError::InvalidHandle)?; |
| 296 | let table = unsafe { (&raw mut PRINCIPAL_TABLES[index]).as_mut().unwrap() }; |
| 297 | gated(table, handle, right, resource) |
| 298 | } |
| 299 | |
| 300 | /// Resolve the table for a principal index without an active-principal check; |
| 301 | /// callers gate authority themselves. |
| 302 | /// |
| 303 | /// # Safety |
| 304 | /// Exclusive access to the kernel IPC state; indices must be distinct when |
| 305 | /// two tables are held at once. |
| 306 | unsafe fn principal_table_at(index: usize) -> &'static mut CapTable<TABLE_CAPACITY> { |
| 307 | unsafe { (&raw mut PRINCIPAL_TABLES[index]).as_mut().unwrap() } |
| 308 | } |
| 309 | |
| 310 | fn active_index() -> Result<usize, IpcError> { |
| 311 | principal_index(ACTIVE_PRINCIPAL.load(Ordering::Acquire)).ok_or(IpcError::InvalidHandle) |
| 312 | } |
| 313 | |
| 314 | /// Send on a channel from the active principal's table. |
| 315 | /// |
| 316 | /// # Safety |
| 317 | /// Exclusive access to the kernel IPC state (EL1, single CPU). |
| 318 | pub unsafe fn channel_send_active(handle: Handle, message: Message) -> Result<(), IpcError> { |
| 319 | let index = active_index()?; |
| 320 | let table = unsafe { principal_table_at(index) }; |
| 321 | channel_send(table, handle, message) |
| 322 | } |
| 323 | |
| 324 | /// Receive from a channel on the active principal's table. |
| 325 | /// |
| 326 | /// # Safety |
| 327 | /// Exclusive access to the kernel IPC state (EL1, single CPU). |
| 328 | pub unsafe fn channel_receive_active(handle: Handle) -> Result<Message, IpcError> { |
| 329 | let index = active_index()?; |
| 330 | let table = unsafe { principal_table_at(index) }; |
| 331 | channel_receive(table, handle) |
| 332 | } |
| 333 | |
| 334 | /// Delegate from the active principal's table into another principal's table. |
| 335 | /// The source is always the active principal, so a task can never delegate a |
| 336 | /// capability it does not itself hold. |
| 337 | /// |
| 338 | /// # Safety |
| 339 | /// Exclusive access to the kernel IPC state (EL1, single CPU). |
| 340 | pub unsafe fn delegate_from_active( |
| 341 | handle: Handle, |
| 342 | to_index: u32, |
| 343 | rights: Rights, |
| 344 | ) -> Result<Handle, IpcError> { |
| 345 | let from_index = active_index()?; |
| 346 | let to_index = principal_index(match to_index { |
| 347 | 0 => PRINCIPAL_A, |
| 348 | 1 => PRINCIPAL_B, |
| 349 | _ => return Err(IpcError::InvalidHandle), |
| 350 | }) |
| 351 | .ok_or(IpcError::InvalidHandle)?; |
| 352 | if from_index == to_index { |
| 353 | return Err(IpcError::InvalidHandle); |
| 354 | } |
| 355 | let from = unsafe { principal_table_at(from_index) }; |
| 356 | let to = unsafe { principal_table_at(to_index) }; |
| 357 | delegate_object(from, handle, to, rights) |
| 358 | } |
| 359 | |
| 360 | /// Read a kernel object word from the active principal's table. |
| 361 | /// |
| 362 | /// # Safety |
| 363 | /// Exclusive access to the kernel IPC state (EL1, single CPU). |
| 364 | pub unsafe fn object_read_active(handle: Handle, offset: usize) -> Result<u64, IpcError> { |
| 365 | let index = active_index()?; |
| 366 | let table = unsafe { principal_table_at(index) }; |
| 367 | object_read(table, handle, offset) |
| 368 | } |
| 369 | |
| 370 | /// Write a kernel object word from the active principal's table. |
| 371 | /// |
| 372 | /// # Safety |
| 373 | /// Exclusive access to the kernel IPC state (EL1, single CPU). |
| 374 | pub unsafe fn object_write_active( |
| 375 | handle: Handle, |
| 376 | offset: usize, |
| 377 | value: u64, |
| 378 | ) -> Result<(), IpcError> { |
| 379 | let index = active_index()?; |
| 380 | let table = unsafe { principal_table_at(index) }; |
| 381 | object_write(table, handle, offset, value) |
| 382 | } |
| 383 | |
| 384 | /// Access the active principal's table mutably. |
| 385 | /// |
| 386 | /// # Safety |
| 387 | /// Exclusive access to the kernel IPC state (EL1, single CPU). |
| 388 | pub unsafe fn active_table() -> &'static mut CapTable<TABLE_CAPACITY> { |
| 389 | let index = |
| 390 | principal_index(ACTIVE_PRINCIPAL.load(Ordering::Acquire)).expect("active principal"); |
| 391 | unsafe { (&raw mut PRINCIPAL_TABLES[index]).as_mut().unwrap() } |
| 392 | } |
| 393 | |
| 394 | #[cfg(test)] |
| 395 | mod tests { |
| 396 | use super::*; |
| 397 | |
| 398 | static TEST_SERIAL: AtomicU32 = AtomicU32::new(0); |
| 399 | |
| 400 | /// Tests share the kernel-owned IPC statics; serialize them. The guard |
| 401 | /// releases on drop so a panicking test cannot wedge the rest. |
| 402 | struct Serial; |
| 403 | impl Drop for Serial { |
| 404 | fn drop(&mut self) { |
| 405 | TEST_SERIAL.store(0, Ordering::Release); |
| 406 | } |
| 407 | } |
| 408 | fn serial() -> Serial { |
| 409 | while TEST_SERIAL |
| 410 | .compare_exchange(0, 1, Ordering::AcqRel, Ordering::Acquire) |
| 411 | .is_err() |
| 412 | { |
| 413 | core::hint::spin_loop(); |
| 414 | } |
| 415 | Serial |
| 416 | } |
| 417 | |
| 418 | fn message(kind: u32) -> Message { |
| 419 | Message::new( |
| 420 | kind, |
| 421 | [kind as u64, 0x22, 0x33], |
| 422 | MemoryRegion::new(0x1000, 2), |
| 423 | ObjectId(0x0a), |
| 424 | ) |
| 425 | } |
| 426 | |
| 427 | #[test] |
| 428 | fn message_abi_is_fixed_size_and_unaligned_free() { |
| 429 | let _serial = serial(); |
| 430 | assert_eq!(core::mem::size_of::<Message>(), 56); |
| 431 | assert_eq!(core::mem::align_of::<Message>(), 8); |
| 432 | assert_eq!(core::mem::size_of::<MemoryRegion>(), 16); |
| 433 | assert_eq!(core::mem::size_of::<TaskId>(), 4); |
| 434 | assert_eq!(core::mem::size_of::<ChannelId>(), 4); |
| 435 | assert_eq!(core::mem::size_of::<ObjectId>(), 8); |
| 436 | } |
| 437 | |
| 438 | #[test] |
| 439 | fn channel_is_fifo_and_bounded() { |
| 440 | let _serial = serial(); |
| 441 | let mut channel: Channel<2> = Channel::new(); |
| 442 | assert!(channel.is_empty()); |
| 443 | channel.send(message(1)).unwrap(); |
| 444 | channel.send(message(2)).unwrap(); |
| 445 | assert_eq!(channel.send(message(3)), Err(ChannelError::Full)); |
| 446 | assert_eq!(channel.receive().unwrap(), message(1)); |
| 447 | assert_eq!(channel.receive().unwrap(), message(2)); |
| 448 | assert_eq!(channel.receive(), Err(ChannelError::Empty)); |
| 449 | // Wrap deterministically after drain. |
| 450 | channel.send(message(4)).unwrap(); |
| 451 | assert_eq!(channel.receive().unwrap(), message(4)); |
| 452 | } |
| 453 | |
| 454 | #[test] |
| 455 | fn send_requires_write_and_receive_requires_read() { |
| 456 | let _serial = serial(); |
| 457 | let (a_send, _a_object, b_recv) = unsafe { reset() }; |
| 458 | let a = unsafe { active_table() }; |
| 459 | // A's send handle lacks READ, so receive must be denied even for A. |
| 460 | assert_eq!(channel_receive(a, a_send), Err(IpcError::MissingRights)); |
| 461 | // B's receive handle lacks WRITE, so send must be denied. |
| 462 | set_active_principal(TaskId(PRINCIPAL_B)); |
| 463 | let b = unsafe { active_table() }; |
| 464 | assert_eq!( |
| 465 | channel_send(b, b_recv, message(9)), |
| 466 | Err(IpcError::MissingRights) |
| 467 | ); |
| 468 | assert!(channel_table().is_empty()); |
| 469 | } |
| 470 | |
| 471 | #[test] |
| 472 | fn stale_and_forged_handles_are_invalid() { |
| 473 | let _serial = serial(); |
| 474 | let (a_send, _a_object, _b_recv) = unsafe { reset() }; |
| 475 | let mut forged = a_send; |
| 476 | forged.generation = forged.generation.wrapping_add(1); |
| 477 | let a = unsafe { active_table() }; |
| 478 | assert_eq!( |
| 479 | channel_send(a, forged, message(1)), |
| 480 | Err(IpcError::InvalidHandle) |
| 481 | ); |
| 482 | // Tombstoned index with an old generation is equally invalid. |
| 483 | let mut stale = a_send; |
| 484 | stale.index = TABLE_CAPACITY as u32 + 1; |
| 485 | assert_eq!( |
| 486 | channel_send(a, stale, message(1)), |
| 487 | Err(IpcError::InvalidHandle) |
| 488 | ); |
| 489 | } |
| 490 | |
| 491 | #[test] |
| 492 | fn delegation_without_derive_right_is_denied() { |
| 493 | let _serial = serial(); |
| 494 | let (_a_send, _a_object, _b_recv) = unsafe { reset() }; |
| 495 | let mut b = CapTable::<TABLE_CAPACITY>::new(PRINCIPAL_B); |
| 496 | // Strip DERIVE from A's object capability first: derive a READ-only |
| 497 | // child into a scratch table, revoke the original... simpler: build a |
| 498 | // source without DERIVE directly. |
| 499 | let a = unsafe { active_table() }; |
| 500 | let plain = a.insert(OBJECT_RESOURCE, Rights::READ).unwrap(); |
| 501 | assert_eq!( |
| 502 | delegate_object(a, plain, &mut b, Rights::READ), |
| 503 | Err(IpcError::MissingRights) |
| 504 | ); |
| 505 | } |
| 506 | |
| 507 | #[test] |
| 508 | fn rights_escalation_is_rejected() { |
| 509 | let _serial = serial(); |
| 510 | let (_a_send, a_object, _b_recv) = unsafe { reset() }; |
| 511 | let a = unsafe { active_table() }; |
| 512 | let mut b = CapTable::<TABLE_CAPACITY>::new(PRINCIPAL_B); |
| 513 | assert_eq!( |
| 514 | delegate_object(a, a_object, &mut b, Rights::READ | Rights::MAP), |
| 515 | Err(IpcError::MissingRights), |
| 516 | "delegating a right the source does not hold is escalation" |
| 517 | ); |
| 518 | // READ|WRITE is a legal subset of the source and must succeed. |
| 519 | assert!(delegate_object(a, a_object, &mut b, Rights::READ | Rights::WRITE).is_ok()); |
| 520 | } |
| 521 | |
| 522 | #[test] |
| 523 | fn receiver_table_full_rolls_back_cleanly() { |
| 524 | let _serial = serial(); |
| 525 | let (_a_send, a_object, _b_recv) = unsafe { reset() }; |
| 526 | let a = unsafe { active_table() }; |
| 527 | let mut tiny = CapTable::<1>::new(PRINCIPAL_B); |
| 528 | tiny.insert(CHANNEL_RESOURCE, Rights::READ).unwrap(); |
| 529 | assert_eq!( |
| 530 | CapTable::derive_into(a, a_object, &mut tiny, Rights::READ), |
| 531 | Err(CapError::Full) |
| 532 | ); |
| 533 | // The source must be untouched by the failed derivation: it can still |
| 534 | // derive into a table with room. |
| 535 | let mut fresh = CapTable::<TABLE_CAPACITY>::new(0x515); |
| 536 | assert!(CapTable::derive_into(a, a_object, &mut fresh, Rights::READ).is_ok()); |
| 537 | } |
| 538 | |
| 539 | #[test] |
| 540 | fn attenuated_child_cannot_write_or_escalate() { |
| 541 | let _serial = serial(); |
| 542 | let (_a_send, a_object, _b_recv) = unsafe { reset() }; |
| 543 | let a = unsafe { active_table() }; |
| 544 | let mut b = CapTable::<TABLE_CAPACITY>::new(PRINCIPAL_B); |
| 545 | let child = CapTable::derive_into(a, a_object, &mut b, Rights::READ).unwrap(); |
| 546 | assert_eq!(object_read(&b, child, 0), Ok(OBJECT_INITIALIZER[0])); |
| 547 | assert_eq!( |
| 548 | object_write(&mut b, child, 0, 0xdead), |
| 549 | Err(IpcError::MissingRights) |
| 550 | ); |
| 551 | // The child cannot re-delegate either: it lacks DERIVE. |
| 552 | let mut c = CapTable::<TABLE_CAPACITY>::new(0x515); |
| 553 | assert_eq!( |
| 554 | CapTable::derive_into(&mut b, child, &mut c, Rights::READ), |
| 555 | Err(CapError::MissingRights) |
| 556 | ); |
| 557 | } |
| 558 | |
| 559 | #[test] |
| 560 | fn ancestor_revocation_reaches_the_derived_child() { |
| 561 | let _serial = serial(); |
| 562 | let (_a_send, a_object, _b_recv) = unsafe { reset() }; |
| 563 | let a = unsafe { active_table() }; |
| 564 | set_active_principal(TaskId(PRINCIPAL_B)); |
| 565 | let b = unsafe { active_table() }; |
| 566 | let child = CapTable::derive_into(a, a_object, b, Rights::READ).unwrap(); |
| 567 | assert!(object_read(b, child, 1).is_ok()); |
| 568 | unsafe { revoke_object_ancestor(PRINCIPAL_A, a_object).unwrap() }; |
| 569 | assert_eq!( |
| 570 | object_read(b, child, 1), |
| 571 | Err(IpcError::InvalidHandle), |
| 572 | "revoking the ancestor invalidates the derived handle" |
| 573 | ); |
| 574 | // And the owner's own capability is gone too. |
| 575 | assert_eq!(object_read(a, a_object, 1), Err(IpcError::InvalidHandle)); |
| 576 | } |
| 577 | |
| 578 | #[test] |
| 579 | fn end_to_end_send_delegate_receive_read() { |
| 580 | let _serial = serial(); |
| 581 | let (a_send, a_object, b_recv) = unsafe { reset() }; |
| 582 | let a = unsafe { active_table() }; |
| 583 | channel_send(a, a_send, message(7)).unwrap(); |
| 584 | set_active_principal(TaskId(PRINCIPAL_B)); |
| 585 | let b = unsafe { active_table() }; |
| 586 | let child = CapTable::derive_into(a, a_object, b, Rights::READ).unwrap(); |
| 587 | let received = channel_receive(b, b_recv).unwrap(); |
| 588 | assert_eq!(received, message(7)); |
| 589 | assert_eq!(object_read(b, child, 0), Ok(OBJECT_INITIALIZER[0])); |
| 590 | assert_eq!(object_write(b, child, 0, 1), Err(IpcError::MissingRights)); |
| 591 | } |
| 592 | |
| 593 | const OBJECT_INITIALIZER: [u64; OBJECT_SLOTS] = |
| 594 | [0xc0ff_ee01, 0x0bad_c0de, 0x1234_5678, 0x9abc_def0]; |
| 595 | } |