Skip to content
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

Handle padded_len_* overflows gracefully #719

Merged
merged 8 commits into from
Apr 15, 2024

Conversation

Dentosal
Copy link
Member

@Dentosal Dentosal commented Apr 10, 2024

There was a bug in padded_len_word and padded_len_usize functions that would overflow on inputs close to max value of the type. While inputs around the max value were never sensible, at least the LDC opcode could be used to trigger the overflow.

Checklist

  • Breaking changes are clearly marked as such in the PR description and changelog
  • New behavior is reflected in tests

Before requesting review

  • I have reviewed the code myself

@Dentosal Dentosal added bug Something isn't working breaking A breaking api change fuel-vm Related to the `fuel-vm` crate. fuel-tx Related to the `fuel-tx` crate. labels Apr 10, 2024
@Dentosal Dentosal self-assigned this Apr 10, 2024
@Dentosal Dentosal marked this pull request as ready for review April 10, 2024 16:55
@Dentosal Dentosal requested a review from a team April 10, 2024 16:55
@@ -733,8 +733,8 @@ fn script_input_coin_data_offset() {
.inputs_predicate_offset_at(offset)
.expect("Failed to fetch offset");

assert_ne!(bytes::padded_len(&predicate), predicate.len());
assert_eq!(bytes::padded_len(&predicate), len);
assert_ne!(bytes::padded_len(&predicate), Some(predicate.len()));
Copy link
Member

Choose a reason for hiding this comment

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

Why even include the ne case? There are many things that it's not equal to :)

Copy link
Member Author

Choose a reason for hiding this comment

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

No idea. It was there already, and I just kept the old tests as-is.

@@ -826,8 +826,8 @@ fn upgrade_input_coin_data_offset() {
.inputs_predicate_offset_at(offset)
.expect("Failed to fetch offset");

assert_ne!(bytes::padded_len(&predicate), predicate.len());
assert_eq!(bytes::padded_len(&predicate), len);
assert_ne!(bytes::padded_len(&predicate), Some(predicate.len()));
Copy link
Member

Choose a reason for hiding this comment

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

Same question.

Copy link
Member Author

Choose a reason for hiding this comment

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

Same answer.

.zip(
input
.predicate_len()
.map(|l| bytes::padded_len_usize(l).unwrap_or(usize::MAX)),
Copy link
Member

Choose a reason for hiding this comment

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

You can use and_then instead of map here to keep an Option and not need the unwrap_or.

The usize::MAX feels arbitrary to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

Using and_then here would mean combining together (a) input has no predicate_len and (b) that the padding overflows. These are fundamentally different things. The idea behind using usize::MAX is that the value was already quite close to it, and impossibly large. Any actual usage of the return value will correctly cause MemoryOverflow or similar panic. The alternative would be to return Option<Result<usize, PanicReason>> , and that feels a bit excessive. Alternatively we could just Rust-panic on such sizes, but I'm not confident that we actually verify the sizes beforehands.

.data_offset()
.map(|o| o + bytes::padded_len(data))
InputRepr::Message.data_offset().map(|o| {
o.saturating_add(bytes::padded_len(data).unwrap_or(usize::MAX))
Copy link
Member

Choose a reason for hiding this comment

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

The usize::Max kinda makes more sense here with the saturating_add, but I haven a hard time with None for len being equivalent to usize::Max.

You could do a:

bytes::padded_len(data).map(|len| InputRepr::Message.data_offset().map(|o| o.saturating_add(len))

Not really an improvement on readability though :/ So up to you.

.map(|o| o + bytes::padded_len(predicate)),
| Input::MessageDataPredicate(MessageDataPredicate { predicate, .. }) => {
self.predicate_offset().map(|o| {
o.saturating_add(bytes::padded_len(predicate).unwrap_or(usize::MAX))
Copy link
Member

Choose a reason for hiding this comment

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

same here

@@ -233,7 +233,9 @@ mod field {
return body.script_data_offset;
}

self.script_offset() + bytes::padded_len(self.body.script.as_slice())
self.script_offset().saturating_add(
bytes::padded_len(self.body.script.as_slice()).unwrap_or(usize::MAX),
Copy link
Member

Choose a reason for hiding this comment

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

same here

self.script_data_offset()
+ bytes::padded_len(self.body.script_data.as_slice())
self.script_data_offset().saturating_add(
bytes::padded_len(self.body.script_data.as_slice()).unwrap_or(usize::MAX),
Copy link
Member

Choose a reason for hiding this comment

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

and here

}

#[test]
fn padded_len_value() {
Copy link
Member

Choose a reason for hiding this comment

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

Can we name this something like padded_len_usize__gives_values_in_increments_of_8 or something like that. To say what the tested behavior is.

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense. I'll rename it.

Copy link
Member Author

Choose a reason for hiding this comment

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

assert_eq!(WORD_SIZE * 1, padded_len(&[0; WORD_SIZE]));
assert_eq!(WORD_SIZE * 2, padded_len(&[0; WORD_SIZE + 1]));
assert_eq!(WORD_SIZE * 2, padded_len(&[0; WORD_SIZE * 2]));
assert_eq!(Some(WORD_SIZE * 0), padded_len(&[]));
Copy link
Member

Choose a reason for hiding this comment

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

This test could use a clearer name of what the expected behavior is.

Copy link
Member Author

Choose a reason for hiding this comment

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

Comment on lines +595 to +596
.map(|len| len as Word)
.unwrap_or(Word::MAX);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we also want to return PanicReason::MemoryOverflow?

Copy link
Member Author

Choose a reason for hiding this comment

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

This Word::MAX is guaranteed to hit the check just below, and return PanicReason::ContracMaxSize. Unless we set contract_max_size == Word::MAX which shouldn't happen, and even then the grow_stack below will catch it.

xgreenx
xgreenx previously approved these changes Apr 15, 2024
@Dentosal Dentosal added this pull request to the merge queue Apr 15, 2024
Merged via the queue into master with commit f94aa10 Apr 15, 2024
38 checks passed
@Dentosal Dentosal deleted the dento/ldc-padding-overflow-fix branch April 15, 2024 15:11
@xgreenx xgreenx mentioned this pull request Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking A breaking api change bug Something isn't working fuel-tx Related to the `fuel-tx` crate. fuel-vm Related to the `fuel-vm` crate.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants