mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
Check for overflow in IncreaseHeap syscall
`process_inner.mem_heap_size + delta` can wrap on RV32 if a client requests a `delta` close to `usize::MAX`. A wrapped value can land below `mem_heap_max` and pass the cap check, letting the call proceed with a bogus heap size. Use `checked_add` and return `Error::OutOfMemory` on overflow. Adds a hosted-mode behavioral test that sends `usize::MAX` as `delta` and asserts the syscall rejects it. The test doesn't cleanly distinguish patched from unpatched code on 64-bit hosted (both paths reject absurd deltas, just at different layers), but it locks in the expected behavior as regression coverage. Signed-off-by: tunnell <tunnell@users.noreply.github.com> Assisted-by: Claude Code (Opus 4.7)
This commit is contained in:
parent
a642095c55
commit
5e42a656cf
2 changed files with 38 additions and 2 deletions
|
|
@ -828,12 +828,16 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR
|
||||||
|
|
||||||
let start = {
|
let start = {
|
||||||
ArchProcess::with_inner_mut(|process_inner| {
|
ArchProcess::with_inner_mut(|process_inner| {
|
||||||
if process_inner.mem_heap_size + delta > process_inner.mem_heap_max {
|
let new_size = process_inner
|
||||||
|
.mem_heap_size
|
||||||
|
.checked_add(delta)
|
||||||
|
.ok_or(xous_kernel::Error::OutOfMemory)?;
|
||||||
|
if new_size > process_inner.mem_heap_max {
|
||||||
return Err(xous_kernel::Error::OutOfMemory);
|
return Err(xous_kernel::Error::OutOfMemory);
|
||||||
}
|
}
|
||||||
|
|
||||||
let start = process_inner.mem_heap_base + process_inner.mem_heap_size;
|
let start = process_inner.mem_heap_base + process_inner.mem_heap_size;
|
||||||
process_inner.mem_heap_size += delta;
|
process_inner.mem_heap_size = new_size;
|
||||||
Ok(start as *mut u8)
|
Ok(start as *mut u8)
|
||||||
})?
|
})?
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1210,3 +1210,35 @@ fn renormalizer() {
|
||||||
assert!(max_element < TEST_LEN as u32);
|
assert!(max_element < TEST_LEN as u32);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `IncreaseHeap` must reject a `delta` that would overflow
|
||||||
|
/// `mem_heap_size + delta`. On an RV32 target, a large `delta`
|
||||||
|
/// silently wraps past `mem_heap_size` in unchecked arithmetic,
|
||||||
|
/// which can land below `mem_heap_max` and bypass the cap check.
|
||||||
|
/// Hosted `usize` is 64-bit so the 32-bit wrap isn't directly
|
||||||
|
/// triggerable here, but the syscall must still reject an absurd
|
||||||
|
/// delta cleanly.
|
||||||
|
#[test]
|
||||||
|
fn increase_heap_rejects_absurd_delta() {
|
||||||
|
let main_thread = start_kernel(SERVER_SPEC);
|
||||||
|
|
||||||
|
let xous_client = xous_kernel::create_process_as_thread(xous_kernel::ProcessArgsAsThread::new(
|
||||||
|
"increase_heap_overflow",
|
||||||
|
move || {
|
||||||
|
let flags = xous_kernel::MemoryFlags::R | xous_kernel::MemoryFlags::W;
|
||||||
|
|
||||||
|
// A normal call should succeed.
|
||||||
|
let ok = xous_kernel::increase_heap(4096, flags);
|
||||||
|
assert!(ok.is_ok(), "small IncreaseHeap should succeed, got {:?}", ok);
|
||||||
|
|
||||||
|
// A pathological delta should be rejected, not silently accepted.
|
||||||
|
let err = xous_kernel::increase_heap(usize::MAX, flags);
|
||||||
|
assert!(err.is_err(), "usize::MAX delta must be rejected, got {:?}", err);
|
||||||
|
},
|
||||||
|
))
|
||||||
|
.expect("spawn client");
|
||||||
|
|
||||||
|
crate::wait_process_as_thread(xous_client).expect("join client");
|
||||||
|
shutdown_kernel();
|
||||||
|
main_thread.join().expect("join kernel");
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue