From 4e0b5100b8708f27c40d5d050722f255ac941c1f Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 28 Oct 2025 18:50:40 +0800 Subject: [PATCH] loader: detect dev key loader was previously just accepting a dev key image and not doing any countermeasures. now, kernel, detached-app, or swap not being properly signed activates countermeasuers. also select the swap key fallback for checking swap. this is "half" of the solution, the other half will have to be implemented in userspace - if we do it at all. i'm starting to think that the swap encryption is not necessary, it was introduced under the assumption that signature checking swap would be too slow, but it's not. so maybe this feature will get dropped. --- loader/src/main.rs | 29 ++++++++++++++++---- loader/src/platform/bao1x/swap.rs | 44 ++++++++++++++++++++++--------- 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/loader/src/main.rs b/loader/src/main.rs index 93cece1c0..7cc0d20ec 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -266,11 +266,21 @@ pub unsafe extern "C" fn rust_entry(signed_buffer: *const usize, signature: u32) false, None, ) { - Ok((k, tag)) => println!( - "*** Kernel signature check by key @ {}({}) OK ***", - k, - core::str::from_utf8(&tag).unwrap_or("invalid tag") - ), + Ok((k, tag)) => { + println!( + "*** Kernel signature check by key @ {}({}) OK ***", + k, + core::str::from_utf8(&tag).unwrap_or("invalid tag") + ); + // k is just a nominal slot number. If either match, assume we are dealing with a + // developer image. + if tag == *bao1x_api::pubkeys::KEYSLOT_INITIAL_TAGS[bao1x_api::pubkeys::DEVELOPER_KEY_SLOT] + || k == bao1x_api::pubkeys::DEVELOPER_KEY_SLOT + { + crate::println!("Developer key detected, ensuring secret are erased"); + bao1x_hal::sigcheck::erase_secrets(); + } + } Err(e) => { println!("Kernel failed signature check. Dying: {:?}", e); bao1x_hal::sigcheck::die_no_std(); @@ -295,6 +305,15 @@ pub unsafe extern "C" fn rust_entry(signed_buffer: *const usize, signature: u32) k, core::str::from_utf8(&tag).unwrap_or("invalid tag") ); + // k is just a nominal slot number. If either match, assume we are dealing with a + // developer image. + if tag + == *bao1x_api::pubkeys::KEYSLOT_INITIAL_TAGS[bao1x_api::pubkeys::DEVELOPER_KEY_SLOT] + || k == bao1x_api::pubkeys::DEVELOPER_KEY_SLOT + { + crate::println!("Developer key detected, ensuring secret are erased"); + bao1x_hal::sigcheck::erase_secrets(); + } true } Err(_e) => { diff --git a/loader/src/platform/bao1x/swap.rs b/loader/src/platform/bao1x/swap.rs index e6aa80ceb..79d6b92b4 100644 --- a/loader/src/platform/bao1x/swap.rs +++ b/loader/src/platform/bao1x/swap.rs @@ -5,7 +5,8 @@ use aes_gcm_siv::{AeadInPlace, Aes256GcmSiv, KeyInit, Nonce, Tag}; use bao1x_api::signatures::FunctionCode; use bao1x_api::udma::*; use bao1x_api::*; -use bao1x_hal::board::{APP_UART_IFRAM_ADDR, SPIM_FLASH_IFRAM_ADDR, SPIM_RAM_IFRAM_ADDR}; +use bao1x_hal::acram::SlotManager; +use bao1x_hal::board::{APP_UART_IFRAM_ADDR, SPIM_FLASH_IFRAM_ADDR, SPIM_RAM_IFRAM_ADDR, SWAP_KEY}; use bao1x_hal::ifram::IframRange; use bao1x_hal::iox::Iox; use bao1x_hal::sce; @@ -230,7 +231,7 @@ impl SwapHal { // check signature only if the swap key is all 0's if swap.key == [0u8; 32] { crate::println!("Fresh swap image found - checking signature before proceeding"); - if bao1x_hal::sigcheck::validate_image( + match bao1x_hal::sigcheck::validate_image( (bao1x_api::offsets::baosec::SWAP_HEADER_LEN - bao1x_api::signatures::SIGBLOCK_LEN) as *const u32, @@ -239,26 +240,43 @@ impl SwapHal { &[FunctionCode::Swap as u32, FunctionCode::UpdatedSwap as u32], false, Some(&mut hal.flash_spim), - ) - .is_err() - { - crate::println!("Fresh swap image did not pass public key validation"); - bao1x_hal::sigcheck::die_no_std(); + ) { + Ok((k, tag)) => { + println!( + "*** Swap signature check by key @ {}({}) OK ***", + k, + core::str::from_utf8(&tag).unwrap_or("invalid tag") + ); + // k is just a nominal slot number. If either match, assume we are dealing + // with a developer image. + if tag + == *bao1x_api::pubkeys::KEYSLOT_INITIAL_TAGS + [bao1x_api::pubkeys::DEVELOPER_KEY_SLOT] + || k == bao1x_api::pubkeys::DEVELOPER_KEY_SLOT + { + crate::println!("Developer key detected, ensuring secret are erased"); + bao1x_hal::sigcheck::erase_secrets(); + } + } + Err(e) => { + crate::println!( + "Fresh swap image did not pass public key validation: {:?}", + e + ); + bao1x_hal::sigcheck::die_no_std(); + } } - crate::println!("...passed!"); } } Err(_) => { - let swap_key = [1u8; 32]; // THIS IS FAKE replace it with the correct API call + let slot_mgr = SlotManager::new(); + let swap_key = slot_mgr.read(&SWAP_KEY).unwrap(); // replace the cipher with the new key - hal.src_cipher = Aes256GcmSiv::new((&swap_key).into()); + hal.src_cipher = Aes256GcmSiv::new((*swap_key).into()); if hal.decrypt_src_page_at(0).is_err() { crate::println!("Swap image failed cryptographic integrity checks!"); bao1x_hal::sigcheck::die_no_std(); } - todo!( - "Fall back to retrieving the derived key for re-encrypted swap, and attempting a trial decryption!" - ); } }