From 074b0429d3d6bcfc02cb610cd6192dac79c0b4d2 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 14 Dec 2025 16:05:12 +0800 Subject: [PATCH] add timing dither to ticktimer add a little bit of timing dither to one of the core delay routines this hardens the OS against glitch attacks by making the timing of concurrent tasks less predictable --- services/xous-ticktimer/Cargo.toml | 3 +- services/xous-ticktimer/src/main.rs | 60 +++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/services/xous-ticktimer/Cargo.toml b/services/xous-ticktimer/Cargo.toml index 0fa7f0422..300656858 100644 --- a/services/xous-ticktimer/Cargo.toml +++ b/services/xous-ticktimer/Cargo.toml @@ -27,6 +27,7 @@ xous-semver = "0.1.5" utralib = { version = "0.1.26", optional = true, default-features = false } loader = { path = "../../loader", optional = true } bao1x-api = { path = "../../libs/bao1x-api", optional = true } +rand_chacha = { version = "0.3.1", optional = true } # TODO: fix this #[target.'cfg(target_arch = "arm")'.dependencies] @@ -43,7 +44,7 @@ renode = ["utralib/renode", "susres/renode", "watchdog"] board-dabao = [] board-baosec = [] -bao1x = ["utralib/bao1x", "utralib/std", "bao1x-api"] +bao1x = ["utralib/bao1x", "utralib/std", "bao1x-api", "rand_chacha"] hosted-baosec = [] susres = [] diff --git a/services/xous-ticktimer/src/main.rs b/services/xous-ticktimer/src/main.rs index a3222d202..4980a6eea 100644 --- a/services/xous-ticktimer/src/main.rs +++ b/services/xous-ticktimer/src/main.rs @@ -12,6 +12,11 @@ use log::{error, info}; mod platform; use platform::implementation::*; use platform::*; +#[cfg(feature = "bao1x")] +use rand_chacha::{ + ChaCha8Rng, + rand_core::{RngCore, SeedableRng}, +}; #[cfg(not(any(target_arch = "arm", feature = "bao1x")))] use susres::SuspendOrder; @@ -61,6 +66,26 @@ fn main() -> ! { susres::Susres::new(Some(SuspendOrder::Last), &xns, api::Opcode::SuspendResume as u32, sr_cid) .expect("couldn't create suspend/resume object"); + #[cfg(feature = "bao1x")] + let xns = xous_names::XousNames::new().unwrap(); + #[cfg(feature = "bao1x")] + let wakeup_cid = xns + .request_connection(bao1x_api::SERVER_NAME_BAO1X_HAL) + .expect("Couldn't connect to bao1x HAL server"); + #[cfg(feature = "bao1x")] + let mut csprng = { + let mut seed = [0u8; 32]; + for chunk in seed.chunks_mut(16) { + let random = xous::syscall::create_server_id().unwrap(); + for (dst, src) in chunk.chunks_mut(4).zip(random.to_array()) { + dst.copy_from_slice(&src.to_ne_bytes()); + } + } + ChaCha8Rng::from_seed(seed) + }; + #[cfg(feature = "bao1x")] + let mut rand_cache = csprng.next_u64(); + // A list of all sleep requests in the system, sorted by the time at which it // expires. That is, if a request comes in to sleep for 1000 ms, and the ticktimer // is currently at 900, the Request will be `1900`. @@ -133,6 +158,24 @@ fn main() -> ! { // but this definitely would need to be done for e.g. Memory messages). core::mem::forget(msg_opt.take()); + // add some small timing jitter every time a sleep call is made. This helps + // improve robustness against glitch attacks while running in OS mode. + #[cfg(feature = "bao1x")] + { + // Probably, a short delay is sufficient to randomize execution from glitches + const DELAY_LOG2: u32 = 2; + let i = (rand_cache as u32) & ((1 << DELAY_LOG2) - 1); + for _ in 0..(i * 2) { + bao1x_api::bollard!(4); + } + rand_cache >>= DELAY_LOG2; + // quite intentionally, this can terminate "early": doesn't always take the same + // number of iterations to trigger a re-seed. This contributes a + // bit to execution timing randomization without adding too much overhead. + if rand_cache == 0 { + rand_cache = csprng.next_u64(); + } + } // let timeout_queue = timeout_heap.entry(msg.sender.pid()).or_default(); ticktimer.recalculate_sleep( &mut sleep_heap, @@ -194,6 +237,23 @@ fn main() -> ! { ticktimer.suspend(); #[cfg(not(any(target_arch = "arm", feature = "bao1x")))] susres.suspend_until_resume(_token).expect("couldn't execute suspend/resume"); + #[cfg(feature = "bao1x")] + { + log::info!("waiting on good morning"); + // Do not use the HAL-Service API to avoid creating a circular dependency on the HAL crate + xous::send_message( + wakeup_cid, + xous::Message::new_blocking_scalar( + bao1x_api::HalOpcode::GoodNight as usize, + 0, + 0, + 0, + 0, + ), + ) + .unwrap(); + log::info!("good morning!"); + } ticktimer.resume(); }),