fix edgecase in nocopy section

do not try to decrypt data for sections that are nocopy,
this can lead to spurious decryption errors
This commit is contained in:
bunnie 2024-06-06 22:19:42 +08:00
parent 529cdcc67d
commit 9f43e1df0a
4 changed files with 41 additions and 15 deletions

View file

@ -24,6 +24,7 @@ pub const MINIELF_FLG_EHF: u8 = 8;
pub const MINIELF_FLG_EHH: u8 = 0x10;
#[repr(C)]
#[derive(Debug)]
pub struct MiniElfSection {
// Virtual address of this section
pub virt: u32,
@ -490,8 +491,12 @@ impl MiniElf {
);
#[cfg(feature = "swap")]
if let Some(swap) = allocator.swap_hal.as_mut() {
let dump_disk = swap.decrypt_src_page_at(dump_pa_src & !(PAGE_SIZE - 1));
dump_slice(&dump_disk[dump_pa_src & (PAGE_SIZE - 1)..], " Src [:20] ");
if !section.no_copy() {
let dump_disk = swap.decrypt_src_page_at(dump_pa_src & !(PAGE_SIZE - 1));
dump_slice(&dump_disk[dump_pa_src & (PAGE_SIZE - 1)..], " Src [:20] ");
} else {
println!(" -- nocopy --");
};
let dump_swap = swap.decrypt_swap_from(
dest_offset & !(PAGE_SIZE - 1),
(section.virt as usize) & !(PAGE_SIZE - 1),
@ -501,12 +506,17 @@ impl MiniElf {
&dump_swap[(section.virt as usize) & (PAGE_SIZE - 1)..],
" Dst [:20] ",
);
let dump_disk = swap
.decrypt_src_page_at((dump_pa_src + section.len() - 20) & !(PAGE_SIZE - 1));
dump_slice(
&dump_disk[(dump_pa_src + section.len() - 20) & (PAGE_SIZE - 1)..],
" Src [-20:] ",
);
if !section.no_copy() {
let dump_disk = swap.decrypt_src_page_at(
(dump_pa_src + section.len() - 20) & !(PAGE_SIZE - 1),
);
dump_slice(
&dump_disk[(dump_pa_src + section.len() - 20) & (PAGE_SIZE - 1)..],
" Src [-20:] ",
);
} else {
println!(" -- nocopy --");
}
// recompute the end section mapping, because PA/VA mappings don't have to be
// linear (in fact they go in the opposite direction)
if let Some(pa_dst_end) = dump_pa_end_dst {

View file

@ -155,8 +155,10 @@ pub fn phase_1(cfg: &mut BootConfig) {
// smashed if the stack overflows! It should be all 0's if the stack did not overrun.
#[cfg(feature = "swap")]
if SDBG && VDBG {
for (_i, _r) in
cfg.runtime_page_tracker[cfg.runtime_page_tracker.len() - 1024..].chunks(32).enumerate()
for (_i, _r) in cfg.runtime_page_tracker
[cfg.runtime_page_tracker.len() - 1024.min(cfg.runtime_page_tracker.len())..]
.chunks(32)
.enumerate()
{
println!(" rpt {:08x}: {:02x?}", cfg.runtime_page_tracker.len() - 1024 + _i * 32, _r);
}
@ -687,7 +689,10 @@ fn copy_processes(cfg: &mut BootConfig) {
let mut last_copy_vaddr = 0;
for section in inis.sections.iter() {
for (index, section) in inis.sections.iter().enumerate() {
if SDBG {
println!("Section {}: {:x?}", index, section);
}
let mut dst_page_vaddr = section.virt as usize;
let mut bytes_to_copy = section.len();
@ -734,8 +739,9 @@ fn copy_processes(cfg: &mut BootConfig) {
let src_swap_img_page = src_swap_img_addr & !(PAGE_SIZE - 1);
let src_swap_img_offset = src_swap_img_addr & (PAGE_SIZE - 1);
// it's almost free to check, so we check at every loop start
if cfg.swap_hal.as_ref().expect("swap HAL uninit").decrypt_page_addr()
!= src_swap_img_page
if (cfg.swap_hal.as_ref().expect("swap HAL uninit").decrypt_page_addr()
!= src_swap_img_page)
&& !section.no_copy()
{
cfg.swap_hal
.as_mut()

View file

@ -35,9 +35,8 @@ pub fn early_init() {
// don't futz with it" is actually the answer that goes to production.
use utralib::utra::sysctrl;
use crate::platform::cramium;
unsafe {
// this is MANDATORY for any chip stapbility in real silicon, as the initial
// this is MANDATORY for any chip stability in real silicon, as the initial
// clocks are too unstable to do anything otherwise. However, for the simulation
// environment, this can (should?) be dropped
let daric_cgu = sysctrl::HW_SYSCTRL_BASE as *mut u32;
@ -644,6 +643,7 @@ pub fn early_init() {
// safety: this is safe because we are in machine mode, and vaddr/paddr always pairs up
let mut usb = unsafe {
cramium_hal::usb::driver::CorigineUsb::new(
0, // is dummy in no-std
0, // is dummy in no-std
cramium_hal::usb::driver::CRG_UDC_MEMBASE,
csr,

View file

@ -368,6 +368,16 @@ impl SwapHal {
(self.image_mac_start + (offset / 4096) * size_of::<Tag>()) as u32
);
};
if crate::VVDBG {
println!(
"Decrypting nonce {:x?}, aad {:x?}, tag {:x?}, data{:x?}..{:x?}",
nonce,
&aad[..self.aad_len],
&tag,
&self.buf.data[..16],
&self.buf.data[self.buf.data.len() - 16..]
);
}
match self.src_cipher.decrypt_in_place_detached(
Nonce::from_slice(&nonce),
&aad[..self.aad_len],