-
Notifications
You must be signed in to change notification settings - Fork 2.1k
micro-http: unsigned Content-Length + several checked arithmetic operations #1985
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
micro-http: unsigned Content-Length + several checked arithmetic operations #1985
Conversation
src/micro_http/src/connection.rs
Outdated
| .ok_or(ConnectionError::ParseError(RequestError::Overflow))?; | ||
|
|
||
| // Get the line slice and parse it. | ||
| // The slice access is safe becase `line_end_index` is a sum of `line_end_index` |
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.
maybe
| // The slice access is safe becase `line_end_index` is a sum of `line_end_index` | |
| // The slice access is safe because `line_end_index` is a sum of `line_start_index` |
?
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.
Fixed
src/micro_http/src/connection.rs
Outdated
| self.body_vec | ||
| .extend_from_slice(&self.buffer[*line_start_index..end_cursor]); | ||
| self.body_bytes_to_be_read -= end_cursor as i32 - *line_start_index as i32; | ||
| // Safe to substract directly as the `if` condition prevents underflow. |
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.
nit:
| // Safe to substract directly as the `if` condition prevents underflow. | |
| // Safe to subtract directly as the `if` condition prevents underflow. |
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.
Fixed
src/micro_http/src/connection.rs
Outdated
| let result = bytes_read | ||
| .checked_add(self.read_cursor) | ||
| .ok_or(ConnectionError::ParseError(RequestError::Overflow))?; | ||
| Ok(result) |
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.
| let result = bytes_read | |
| .checked_add(self.read_cursor) | |
| .ok_or(ConnectionError::ParseError(RequestError::Overflow))?; | |
| Ok(result) | |
| bytes_read | |
| .checked_add(self.read_cursor) | |
| .ok_or(ConnectionError::ParseError(RequestError::Overflow)) |
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.
Done
src/micro_http/src/connection.rs
Outdated
| start: &mut usize, | ||
| end: usize, | ||
| ) -> Result<bool, ConnectionError> { | ||
| if end < *start || end > self.buffer.len() { |
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.
For end < *start should we return Underflow instead?
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.
That's right, good catch! Other functions where end might be before start return Underflow indeed
src/micro_http/src/connection.rs
Outdated
|
|
||
| // Get the line slice and parse it. | ||
| // The slice access is safe becase `line_end_index` is a sum of `line_end_index` | ||
| // and something else. |
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.
Might be worth adding that it is also safe because find assures us that line_end_index is < end_cursor or something like that :-?.
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.
Updated
src/mmds/src/lib.rs
Outdated
| } | ||
| } | ||
| Err(e) => match e { | ||
| RequestError::BodyWithoutPendingRequest => build_response( |
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.
The changes in this file will need a small rebase and after that, we should test in test_parse_request_bytes_error() also the newly added error types.
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.
After rebasing and looking more carefully I realized that the overflow checks I'd added were superfluous, so I documented the safety of the operations instead
124e84b to
5a27123
Compare
...and a bunch of fixes for unchecked unsigned arithmetic in connection.rs. Added more unit tests too that exercise failure cases for over/underflows in mathematic operations. Fixes firecracker-microvm#1977 Signed-off-by: Alexandra Iordache <aghecen@amazon.com>
Reason for This PR
Fixes #1977
Description of Changes
Content-Lengthfromi32tou32.connection.rs.unwraps with error propagation, introducing new error variants.rust-vmm.License Acceptance
By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache 2.0 license.
PR Checklist
[Author TODO: Meet these criteria.][Reviewer TODO: Verify that these criteria are met. Request changes if not]git commit -s).unsafecode is properly documented.firecracker/swagger.yaml.CHANGELOG.md.