-
Notifications
You must be signed in to change notification settings - Fork 224
Add bounds checking for apob_read #2484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1269,6 +1269,10 @@ impl ServerImpl { | |
| fn apob_read(&mut self, sequence: u64, offset: u64, size: u64) { | ||
| use drv_hf_api::ApobReadError; | ||
| use host_sp_messages::ApobReadResult; | ||
|
|
||
| // NOTE: This ONLY checks we are not out of bounds for `usize`, | ||
| // we will check if the requested size can be honored later in | ||
| // the `fill` closure. | ||
| let Ok(size) = usize::try_from(size) else { | ||
| self.tx_buf.encode_response( | ||
| sequence, | ||
|
|
@@ -1285,33 +1289,37 @@ impl ServerImpl { | |
| ); | ||
| return; | ||
| }; | ||
|
|
||
| // closure for performing the actual apob read | ||
| let fill = |buf: &mut [u8]| { | ||
| // Did the user pass in a reasonable size? | ||
| let Some(rbuf) = buf.get_mut(..size) else { | ||
| return Err(SpToHost::ApobRead(ApobReadResult::InvalidSize)); | ||
| }; | ||
|
|
||
| // If successful: return the number of bytes read | ||
| let err = match self.hf.apob_read(offset, rbuf) { | ||
| Ok(n) => return Ok(n), | ||
| Err(e) => e, | ||
| }; | ||
|
|
||
| // Something went wrong, ringbuf the error, and convert | ||
| // the error types | ||
| ringbuf_entry!(Trace::ApobReadError { offset, err }); | ||
| let read_err = match err { | ||
| ApobReadError::NotImplemented => ApobReadResult::NotImplemented, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may not know the answer to this either, since you just moved this code, but...why are these types distinct?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use drv_hf_api::ApobReadError;
use host_sp_messages::ApobReadResult;I think they are just different types from different sources, IPC vs IPCC types? I think there are probably just two different wire schemas we're marshalling between here, not 100% certain though. |
||
| ApobReadError::InvalidState => ApobReadResult::InvalidState, | ||
| ApobReadError::NoValidApob => ApobReadResult::NoValidApob, | ||
| ApobReadError::InvalidOffset => ApobReadResult::InvalidOffset, | ||
| ApobReadError::InvalidSize => ApobReadResult::InvalidSize, | ||
| ApobReadError::ReadFailed => ApobReadResult::ReadFailed, | ||
| }; | ||
| Err(SpToHost::ApobRead(read_err)) | ||
| }; | ||
| self.tx_buf.try_encode_response( | ||
| sequence, | ||
| &SpToHost::ApobRead(ApobReadResult::Ok), | ||
| |buf| match self.hf.apob_read(offset, &mut buf[..size]) { | ||
| Ok(n) => Ok(n), | ||
| Err(err) => { | ||
| ringbuf_entry!(Trace::ApobReadError { offset, err }); | ||
| Err(SpToHost::ApobRead(match err { | ||
| ApobReadError::NotImplemented => { | ||
| ApobReadResult::NotImplemented | ||
| } | ||
| ApobReadError::InvalidState => { | ||
| ApobReadResult::InvalidState | ||
| } | ||
| ApobReadError::NoValidApob => { | ||
| ApobReadResult::NoValidApob | ||
| } | ||
| ApobReadError::InvalidOffset => { | ||
| ApobReadResult::InvalidOffset | ||
| } | ||
| ApobReadError::InvalidSize => { | ||
| ApobReadResult::InvalidSize | ||
| } | ||
| ApobReadError::ReadFailed => ApobReadResult::ReadFailed, | ||
| })) | ||
| } | ||
| }, | ||
| fill, | ||
| ); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you prefer let/else to ok_or here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tbh I have a slight/weak preference to let-else here, but some of that is baseless preference, and some of that is habit because the
.into()of?can sometimes codegen slightly larger. That being said, I probably wouldn't refactorok_orif I saw it.