Skip to content

Commit

Permalink
Prevent mailbox buffer overflow vulnerability
Browse files Browse the repository at this point in the history
  • Loading branch information
sree-revoori1 authored and jhand2 committed Apr 4, 2024
1 parent c92c83e commit 85c4b01
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
9 changes: 7 additions & 2 deletions runtime/src/mailbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ impl Mailbox {
}

/// Set the length of the current mailbox data in bytes
pub fn set_dlen(&mut self, len: u32) {
pub fn set_dlen(&mut self, len: u32) -> CaliptraResult<()> {
if len > memory_layout::MBOX_SIZE {
return Err(CaliptraError::RUNTIME_MAILBOX_INVALID_PARAMS);
}

let mbox = self.mbox.regs_mut();
mbox.dlen().write(|_| len);
Ok(())
}

/// Get the length of the current mailbox data in words
Expand Down Expand Up @@ -141,7 +146,7 @@ impl Mailbox {

/// Write a word-aligned `buf` to the mailbox
pub fn write_response(&mut self, buf: &[u8]) -> CaliptraResult<()> {
self.set_dlen(buf.len() as u32);
self.set_dlen(buf.len() as u32)?;
self.copy_bytes_to_mbox(buf);
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub extern "C" fn entry_point() -> ! {
if let Err(e) = caliptra_runtime::handle_mailbox_commands(&mut drivers) {
handle_fatal_error(e.into());
}
loop {}
caliptra_drivers::ExitCtrl::exit(0xff);
}

#[no_mangle]
Expand Down
5 changes: 3 additions & 2 deletions runtime/test-fw/src/mock_rt_test_interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ fn read_pcr_log(persistent_data: &PersistentDataAccessor, mbox: &mut Mailbox) {
(core::mem::size_of::<PcrLogEntry>() * pcr_entry_count)
.try_into()
.unwrap(),
);
)
.unwrap();
mbox.set_status(MboxStatusE::DataReady);
}

Expand All @@ -136,7 +137,7 @@ fn read_pcrs(mbox: &mut Mailbox) {
swap_word_bytes_inplace(&mut pcr_bytes);
mbox.copy_bytes_to_mbox(pcr.as_bytes()).unwrap();
}
mbox.set_dlen((48 * PCR_COUNT).try_into().unwrap());
mbox.set_dlen((48 * PCR_COUNT).try_into().unwrap()).unwrap();
mbox.set_status(MboxStatusE::DataReady);
}

Expand Down

0 comments on commit 85c4b01

Please sign in to comment.