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

Check for overflow in Script::bytes_to_asm_fmt() #658

Merged
merged 2 commits into from Sep 20, 2021

Conversation

Kixunil
Copy link
Collaborator

@Kixunil Kixunil commented Sep 19, 2021

This adds an overflow check in Script::bytes_to_asm_fmt() motivated by
electrs issue. While it was not tested yet, I'm very confident that
overflow is the cause of panic there and even if not it can cause panic
becuase the public function takes unvalidated byte array and reads
data_len from it.

The electrs issue: romanz/electrs#490

Strangely, this breaks a test case and I can't see why. I'm publishing in case someone wants to help.

Edit: One damn character. :D Should be OK now.

This adds an overflow check in `Script::bytes_to_asm_fmt()` motivated by
`electrs` issue. While it was not tested yet, I'm very confident that
overflow is the cause of panic there and even if not it can cause panic
becuase the public function takes unvalidated byte array and reads
`data_len` from it.

The `electrs` issue: romanz/electrs#490
@Kixunil Kixunil marked this pull request as ready for review September 19, 2021 11:34
This adds a test case for script formatting which caused overflow in the
past and a few others from the same "interesting" transaction. Note that
to trigger the bug one has to run the test on 32 bit architecture.
@Kixunil
Copy link
Collaborator Author

Kixunil commented Sep 19, 2021

We found the offending transaction and I added a test cases for its outputs and checked that old code causes them to fail because of overflow. Now I'm certain this was actually a bug and it was the bug that caused electrs issue.

Kixunil added a commit to Kixunil/rust-bitcoin that referenced this pull request Sep 19, 2021
This adds i686 to CI which can help catching pointer-size-related bugs
such as the one addressed by rust-bitcoin#658.
Kixunil added a commit to Kixunil/rust-bitcoin that referenced this pull request Sep 19, 2021
This adds i686 to CI which can help catching pointer-size-related bugs
such as the one addressed by rust-bitcoin#658.
Kixunil added a commit to Kixunil/rust-bitcoin that referenced this pull request Sep 19, 2021
This adds i686 to CI which can help catching pointer-size-related bugs
such as the one addressed by rust-bitcoin#658.
@Kixunil
Copy link
Collaborator Author

Kixunil commented Sep 19, 2021

Note that I noticed a few more overflows that I think are unlikely to happen in practice but intend to send a PR later.

Kixunil added a commit to Kixunil/rust-bitcoin that referenced this pull request Sep 19, 2021
This adds i686 to CI which can help catching pointer-size-related bugs
such as the one addressed by rust-bitcoin#658.
Copy link
Member

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

LGTM, is it possible to add a fuzz target for this function? It does seem like a good target for a fuzzer.

@Kixunil
Copy link
Collaborator Author

Kixunil commented Sep 19, 2021

Great idea! Though it'd be nice if this got fixed quickly, so not sure if worth dismissing a review.

Also forgot to ask if we can get a point release with this fix (and possibly others).

@TheBlueMatt
Copy link
Member

Yea, seems like a good thing to put in a new PR, doesn’t have to be here.

Copy link
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

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

Found one more: n + 5 can overflow here too!

if self.data.len() < n + 5 {

You are welcome to fix this in this PR, otherwise, I can make another PR for it.

ACK a0e1d2e

@Kixunil
Copy link
Collaborator Author

Kixunil commented Sep 20, 2021

Yes, that's one of them - each match harm has such. I was actually looking into converting iteration to iterator, systematically removing all overflows and merging repeated code via macro (not fn due to return statement), but then found the issue with 16-bits. I will continue assuming 16-bit is unsupported and 32-bit is. Also I tried to use no_panic originally and the compiler can't prove there's not panic. I'd like to analyze asm eventually.

Anyway, since this has two approvals and it'd be really nice to get it into electrs could this be merged and released in point version, please?

@sanket1729
Copy link
Member

sanket1729 commented Sep 20, 2021

We can certainly make a release with this PR, and without any taproot-related breaking changes that have been merged in master.
I think only @apoelstra @TheBlueMatt can push a release to crates.

@apoelstra
Copy link
Member

ACK 76cf74f

@apoelstra apoelstra merged commit 29549cc into rust-bitcoin:master Sep 20, 2021
apoelstra added a commit that referenced this pull request Sep 20, 2021
…m_fmt()

6c3434c bump version to 0.27.1 (Andrew Poelstra)
8a529e6 Added test for the overflow bug and few others (Martin Habovstiak)
78b152e Check for overflow in Script::bytes_to_asm_fmt() (Martin Habovstiak)

Pull request description:

  Backport of #658

ACKs for top commit:
  apoelstra:
    ACK 6c3434c
  sanket1729:
    ACK 6c3434c. Same as #658 which I ACKed

Tree-SHA512: ad9e02e2c748467b351039c3ab7f23b9902507cfa45d7d1084bfbaaad1ff7a1f22327b7311849f18a1a03dfd354a9424a74b125a2f412b9f6f678979a037df0a
apoelstra added a commit that referenced this pull request Sep 20, 2021
f2042bd Add i686 to tested architectures (Martin Habovstiak)

Pull request description:

  This adds i686 to CI which can help catching pointer-size-related bugs
  such as the one addressed by #658.

  Opening a new PR seemed more appropriate than adding it to #658 I can change it if you disagree.

ACKs for top commit:
  apoelstra:
    ACK f2042bd

Tree-SHA512: b772c8cbc5faa6fed25faed275eb903cd7226d8ff618ff57ce3e6735cc53b5c797380a519c670faf4fd5aa86ae01d5232e8c21e03a282599e7caa532b2176be8
@Kixunil Kixunil deleted the script-fmt-overflow-fix branch September 20, 2021 15:07
@dr-orlovsky dr-orlovsky added this to the 0.28.0 milestone Sep 25, 2021
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.

None yet

6 participants