Skip to content

Commit e82833e

Browse files
committed
dbg: read/write addresses check offset calculation
- use checked_sub to avoid issues from addresses that could cause underflow Signed-off-by: Doru Blânzeanu <[email protected]>
1 parent 8d64112 commit e82833e

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/hyperlight_host/src/hypervisor/gdb/hyp_debug.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,14 @@ pub trait GuestMemoryDebug: GuestVcpuDebug {
164164
data.len(),
165165
(PAGE_SIZE - (gpa & (PAGE_SIZE - 1))).try_into().unwrap(),
166166
);
167-
let offset = gpa as usize - SandboxMemoryLayout::BASE_ADDRESS;
167+
let offset = (gpa as usize)
168+
.checked_sub(SandboxMemoryLayout::BASE_ADDRESS)
169+
.ok_or_else(|| {
170+
log::warn!(
171+
"gva=0x{:#X} causes subtract with underflow: \"gpa - BASE_ADDRESS={:#X}-{:#X}\"",
172+
gva, gpa, SandboxMemoryLayout::BASE_ADDRESS);
173+
HyperlightError::TranslateGuestAddress(gva)
174+
})?;
168175

169176
dbg_mem_access_fn
170177
.try_lock()
@@ -220,7 +227,14 @@ pub trait GuestMemoryDebug: GuestVcpuDebug {
220227
data.len(),
221228
(PAGE_SIZE - (gpa & (PAGE_SIZE - 1))).try_into().unwrap(),
222229
);
223-
let offset = gpa as usize - SandboxMemoryLayout::BASE_ADDRESS;
230+
let offset = (gpa as usize)
231+
.checked_sub(SandboxMemoryLayout::BASE_ADDRESS)
232+
.ok_or_else(|| {
233+
log::warn!(
234+
"gva=0x{:#X} causes subtract with underflow: \"gpa - BASE_ADDRESS={:#X}-{:#X}\"",
235+
gva, gpa, SandboxMemoryLayout::BASE_ADDRESS);
236+
HyperlightError::TranslateGuestAddress(gva)
237+
})?;
224238

225239
dbg_mem_access_fn
226240
.try_lock()

0 commit comments

Comments
 (0)