Skip to content

fix: insert critical overflow checks preventing heap corruption #3077

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

Merged
merged 30 commits into from
Jan 28, 2022

Conversation

crusso
Copy link
Contributor

@crusso crusso commented Jan 27, 2022

Fixes two classes of u32-undetected overflow bugs that could corrupt heap, stable variables, or both:

  • ic.rs: alloc_words wasn't checking validity of addresses, leading to possible (and observed) heap corruption.
  • compile.ml: buffer_size wasn't checking overflow of 32-bit size pre-computation.

The first is fixed using an overflow detecting add function of Rust.

The second is checked by using a local i64 in each type-directed size function. These functions are MV and return two i32s (for the data buffer and (vestigial) reference buffer sizes. I did not modify the functions to return 2 i64s because our MultiValue emulation only support i32 at the moment.

Fixes #3068.

@github-actions
Copy link
Contributor

github-actions bot commented Jan 27, 2022

Comparing from ecadc8d to 00c542c:
In terms of gas, 3 tests regressed and the mean change is +2.3%.
In terms of size, 1 tests regressed, 2 tests improved and the mean change is -0.1%.

@crusso crusso changed the title Claudio/oom query bugfix: fix critical overflow bugs leading to heap corruption Jan 28, 2022
@crusso crusso changed the title bugfix: fix critical overflow bugs leading to heap corruption fix: insert critical overflow bugs leading to heap corruption Jan 28, 2022
@crusso crusso changed the title fix: insert critical overflow bugs leading to heap corruption fix: insert critical overflow checks preventing heap corruption Jan 28, 2022
@crusso crusso marked this pull request as ready for review January 28, 2022 15:49
@crusso crusso requested review from nomeata and ggreif January 28, 2022 15:54
Copy link
Contributor

@ggreif ggreif left a comment

Choose a reason for hiding this comment

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

Looks fine! We can do the shift trick in another PR.

@ggreif
Copy link
Contributor

ggreif commented Jan 28, 2022 via email

@crusso crusso added the automerge-squash When ready, merge (using squash) label Jan 28, 2022
@mergify mergify bot merged commit 0657017 into master Jan 28, 2022
@mergify mergify bot deleted the claudio/oom-query branch January 28, 2022 20:24
@mergify mergify bot removed the automerge-squash When ready, merge (using squash) label Jan 28, 2022
mergify bot pushed a commit that referenced this pull request Jan 31, 2022
…sts (#3085)

#3077 added an expensive, but necessary overflow check to `alloc_words`, to prevent allocating addresses outside the 32-bit heap.

* This PR optimizes that check to avoid the conditional test by doing the page diff arithmetic in u64s, 
relying on `grow_memory` to trap when asked to grow beyond 2^16 pages.
* fixes test `run-drun/oom-update` to actually use an update (not query) method as advertised.

I wonder why `grow_memory` is marked `inline_never`? Seems like we'd save a function call on each allocation...
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.

Bug: Candid deserialization of large arrays fails with buffer overflow on read
2 participants