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

fix(chain): Validate header versions when serializing blocks #6475

Merged
merged 4 commits into from
Apr 12, 2023

Conversation

arya2
Copy link
Contributor

@arya2 arya2 commented Apr 8, 2023

Motivation

This PR updates the ZcashSerialize implementation for block headers to apply to same constraints as ZcashDeserialize.

Closes #497.

Solution

  • Splits version constraint checks in zcash_deserialize to a check_version function
  • Calls check_version() from zcash_serialize

Related changes:

  • Removes LOW_31_BITS constant and masking of high bit when deserializing (a high bit causes an error anyways)

Review

Anyone can review.

Reviewer Checklist

  • Will the PR name make sense to users?
    • Does it need extra CHANGELOG info? (new features, breaking changes, large changes)
  • Are the PR labels correct?
  • Does the code do what the ticket and PR says?
    • Does it change concurrent code, unsafe code, or consensus rules?
  • How do you know it works? Does it have tests?

@arya2 arya2 added C-bug Category: This is a bug A-consensus Area: Consensus rule updates P-Medium ⚡ C-security Category: Security issues I-invalid-data Zebra relies on invalid or untrusted data, or sends invalid data C-audit Category: Issues arising from audit findings labels Apr 8, 2023
@arya2 arya2 requested a review from a team as a code owner April 8, 2023 02:43
@arya2 arya2 self-assigned this Apr 8, 2023
@arya2 arya2 requested review from oxarbitrage and removed request for a team April 8, 2023 02:43
Copy link
Contributor

@oxarbitrage oxarbitrage left a comment

Choose a reason for hiding this comment

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

It looks good, thank you for the fix.

wrong_checkpoint_hash_fail_test is failing. This seems to be because Zebra now panic in the serialization instead of an error in the verifier.

Can be fixed by changed the version to 5 at https://github.com/ZcashFoundation/zebra/blob/main/zebra-consensus/src/checkpoint/tests.rs#L507

Copy link
Contributor

@teor2345 teor2345 left a comment

Choose a reason for hiding this comment

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

Looks good, I just want to check we handle the serialization error correctly.

In some cases, we panic because we assume serialization errors are not possible. But we only do this when we serialize to a vector or memory. (If we're using the disk or network, we can encounter an IO error like a full disk or a broken connection.)

@mpguerra mpguerra mentioned this pull request Apr 11, 2023
36 tasks
@codecov
Copy link

codecov bot commented Apr 11, 2023

Codecov Report

Merging #6475 (6b4ec1b) into main (044ecb0) will decrease coverage by 0.17%.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6475      +/-   ##
==========================================
- Coverage   77.89%   77.72%   -0.17%     
==========================================
  Files         304      304              
  Lines       39678    39665      -13     
==========================================
- Hits        30908    30831      -77     
- Misses       8770     8834      +64     

@arya2
Copy link
Contributor Author

arya2 commented Apr 12, 2023

Can be fixed by changed the version to 5 at https://github.com/ZcashFoundation/zebra/blob/main/zebra-consensus/src/checkpoint/tests.rs#L507

Thank you!! Applied in 86ec19b.

In some cases, we panic because we assume serialization errors are not possible. But we only do this when we serialize to a vector or memory. (If we're using the disk or network, we can encounter an IO error like a full disk or a broken connection.)

It looks like Headers are only created in production via ZcashDeserialize and never modified, so the check_version() call in ZcashSerialize should only ever return an error in tests?

@arya2 arya2 requested review from oxarbitrage and teor2345 April 12, 2023 00:18
@teor2345
Copy link
Contributor

In some cases, we panic because we assume serialization errors are not possible. But we only do this when we serialize to a vector or memory. (If we're using the disk or network, we can encounter an IO error like a full disk or a broken connection.)

It looks like Headers are only created in production via ZcashDeserialize and never modified, so the check_version() call in ZcashSerialize should only ever return an error in tests?

That makes sense to me!

Copy link
Contributor

@teor2345 teor2345 left a comment

Choose a reason for hiding this comment

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

Thanks for the extra docs and tests!

mergify bot added a commit that referenced this pull request Apr 12, 2023
@mergify mergify bot merged commit 403c074 into main Apr 12, 2023
@mergify mergify bot deleted the check-header-version branch April 12, 2023 04:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-consensus Area: Consensus rule updates C-audit Category: Issues arising from audit findings C-bug Category: This is a bug C-security Category: Security issues I-invalid-data Zebra relies on invalid or untrusted data, or sends invalid data
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[NCC-E005955-M2F] zebra-chain: Check that header block version field is valid when serializing blocks
3 participants