Skip to content

Conversation

@alxiord
Copy link

@alxiord alxiord commented Jun 30, 2020

Reason for This PR

Fixes #1977

Description of Changes

  • Changed Content-Length from i32 to u32.
  • Added a bunch of fixes for unchecked unsigned arithmetic in connection.rs.
  • Added more unit tests too that exercise failure cases for over/underflows in math operations.
  • Replaced some unwraps with error propagation, introducing new error variants.
  • This functionality can be added in 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]

  • All commits in this PR are signed (git commit -s).
  • The reason for this PR is clearly provided (issue no. or explanation).
  • The description of changes is clear and encompassing.
  • Any required documentation changes (code and docs) are included in this PR.
  • Any newly added unsafe code is properly documented.
  • Any API changes are reflected in firecracker/swagger.yaml.
  • Any user-facing changes are mentioned in CHANGELOG.md.

@alxiord alxiord self-assigned this Jun 30, 2020
.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`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe

Suggested change
// 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`

?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

ioanachirca
ioanachirca previously approved these changes Jul 1, 2020
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
// Safe to substract directly as the `if` condition prevents underflow.
// Safe to subtract directly as the `if` condition prevents underflow.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@alxiord alxiord added the Status: Awaiting review Indicates that a pull request is ready to be reviewed label Jul 2, 2020
@lauralt lauralt self-requested a review July 6, 2020 07:01
Comment on lines 137 to 140
let result = bytes_read
.checked_add(self.read_cursor)
.ok_or(ConnectionError::ParseError(RequestError::Overflow))?;
Ok(result)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

start: &mut usize,
end: usize,
) -> Result<bool, ConnectionError> {
if end < *start || end > self.buffer.len() {
Copy link

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?

Copy link
Author

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


// 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.
Copy link

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 :-?.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

}
}
Err(e) => match e {
RequestError::BodyWithoutPendingRequest => build_response(
Copy link

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.

Copy link
Author

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

@lauralt lauralt added Status: Author and removed Status: Awaiting review Indicates that a pull request is ready to be reviewed labels Jul 22, 2020
@alxiord alxiord force-pushed the uhttp_unsigned branch 3 times, most recently from 124e84b to 5a27123 Compare July 22, 2020 15:24
...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>
@ioanachirca ioanachirca merged commit 96e0c1b into firecracker-microvm:master Jul 23, 2020
@alxiord alxiord deleted the uhttp_unsigned branch July 24, 2020 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

micro-http: validate that the content-length is valid

3 participants