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

[NCC-E005955-XVE] zebra-chain: Inconsistent error management in Add and Sub for Height #6279

Closed
Tracked by #6277
mpguerra opened this issue Mar 9, 2023 · 4 comments · Fixed by #6330
Closed
Tracked by #6277
Assignees
Labels
C-audit Category: Issues arising from audit findings

Comments

@mpguerra
Copy link
Contributor

mpguerra commented Mar 9, 2023

Motivation

We want to track all of the findings from the zebra audit.

Details

Some arithmetic operations for Height are implemented in zebra-chain/src/block/height.rs for both Height and i32

impl Add<Height> for Height {
type Output = Option<Height>;
fn add(self, rhs: Height) -> Option<Height> {
// We know that both values are positive integers. Therefore, the result is
// positive, and we can skip the conversions. The checked_add is required,
// because the result may overflow.
let height = self.0.checked_add(rhs.0)?;
let height = Height(height);
if height <= Height::MAX && height >= Height::MIN {
Some(height)
} else {
None
}
}
}
impl Sub<Height> for Height {
type Output = i32;
/// Panics if the inputs or result are outside the valid i32 range.
fn sub(self, rhs: Height) -> i32 {
// We construct heights from integers without any checks,
// so the inputs or result could be out of range.
let lhs = i32::try_from(self.0)
.expect("out of range input `self`: inputs should be valid Heights");
let rhs =
i32::try_from(rhs.0).expect("out of range input `rhs`: inputs should be valid Heights");
lhs.checked_sub(rhs)
.expect("out of range result: valid input heights should yield a valid result")
}
}

The Add function handles overflow with an Option , but Sub will panic. The Sub function later in the file for i32 subtraction returns an Option as well, making the panic behavior an outlier. Panics should be used as a last resort, when there is no possibility of recovery. Otherwise, an attacker may attempt to intentionally trigger a panic as part of a denial-of-service attack. Additionally, the Sub function does not enforce the same constraint checks shown on Line 73 above, unlike the other functions in the same file.

@mpguerra mpguerra added this to Zebra Mar 9, 2023
@github-project-automation github-project-automation bot moved this to 🆕 New in Zebra Mar 9, 2023
@mpguerra mpguerra added C-audit Category: Issues arising from audit findings S-needs-triage Status: A bug report needs triage P-Medium ⚡ labels Mar 9, 2023
@mpguerra
Copy link
Contributor Author

@mpguerra mpguerra changed the title zebra-chain: Inconsistent error management in Add and Sub for Height [NCC-E005955-XVE] zebra-chain: Inconsistent error management in Add and Sub for Height Mar 16, 2023
@mpguerra mpguerra removed the S-needs-triage Status: A bug report needs triage label Mar 16, 2023
@teor2345
Copy link
Contributor

teor2345 commented Mar 21, 2023

The Add function handles overflow with an Option , but Sub will panic. The Sub function later in the file for i32 subtraction returns an Option as well, making the panic behavior an outlier.

We don't think this panic is possible with valid Heights, due to the range of the data types involved:
#6330 (comment)

However, since some code (particularly RPC code) might construct invalid heights, we're going to do some fixes anyway.

Additionally, the Sub function does not enforce the same constraint checks shown on Line 73 above, unlike the other functions in the same file.

The output type is not a Height, it's the difference between two heights. So the height range limits do not apply. But we'll make some fixes to make this clearer.

@mergify mergify bot closed this as completed in #6330 Mar 29, 2023
@github-project-automation github-project-automation bot moved this from 🆕 New to ✅ Done in Zebra Mar 29, 2023
@mpguerra
Copy link
Contributor Author

Do we need to review the estimate on this one? Seems like it ended up being bigger than a "size 2" issue. I don't necessarily want us to actually change the estimate retroactively but it might be interesting to discuss during the retro :)

@teor2345
Copy link
Contributor

This refactor has already helped me discover a potential bug in my progress bar code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-audit Category: Issues arising from audit findings
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants