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

Use checked math in frame-balances named_reserve #7365

Merged
merged 3 commits into from
Jan 29, 2025

Conversation

vgantchev
Copy link
Contributor

@vgantchev vgantchev commented Jan 28, 2025

This PR modifies named_reserve() in frame-balances to use checked math instead of defensive saturating math.

The use of saturating math relies on the assumption that the sum of the values will always fit in u128::MAX. However, there is nothing preventing the implementing pallet from passing a larger value which overflows. This can happen if the implementing pallet does not validate user input and instead relies on named_reserve() to return an error (this saves an additional read)

This is not a security concern, as the method will subsequently return an error thanks to <Self as ReservableCurrency<_>>::reserve(who, value)?;. However, the defensive_saturating_add will panic in --all-features, creating false positive crashes in fuzzing operations.

@vgantchev vgantchev requested a review from a team as a code owner January 28, 2025 15:25
@cla-bot-2021
Copy link

cla-bot-2021 bot commented Jan 28, 2025

User @vgantchev, please sign the CLA here.

@bkchr bkchr added the T2-pallets This PR/Issue is related to a particular pallet. label Jan 28, 2025
@bkchr
Copy link
Member

bkchr commented Jan 28, 2025

/cmd prdoc --audience runtime_dev --bump patch

@bkchr bkchr enabled auto-merge January 28, 2025 20:05
@bkchr bkchr added the A4-needs-backport Pull request must be backported to all maintained releases. label Jan 28, 2025
@ggwpez
Copy link
Member

ggwpez commented Jan 28, 2025

/cmd fmt

@bkchr bkchr added this pull request to the merge queue Jan 29, 2025
Merged via the queue into paritytech:master with commit f373af0 Jan 29, 2025
203 of 206 checks passed
github-actions bot pushed a commit that referenced this pull request Jan 29, 2025
This PR modifies `named_reserve()` in frame-balances to use checked math
instead of defensive saturating math.

The use of saturating math relies on the assumption that the sum of the
values will always fit in `u128::MAX`. However, there is nothing
preventing the implementing pallet from passing a larger value which
overflows. This can happen if the implementing pallet does not validate
user input and instead relies on `named_reserve()` to return an error
(this saves an additional read)

This is not a security concern, as the method will subsequently return
an error thanks to `<Self as ReservableCurrency<_>>::reserve(who,
value)?;`. However, the `defensive_saturating_add` will panic in
`--all-features`, creating false positive crashes in fuzzing operations.

---------

Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
(cherry picked from commit f373af0)
@paritytech-cmd-bot-polkadot-sdk

Successfully created backport PR for stable2407:

github-actions bot pushed a commit that referenced this pull request Jan 29, 2025
This PR modifies `named_reserve()` in frame-balances to use checked math
instead of defensive saturating math.

The use of saturating math relies on the assumption that the sum of the
values will always fit in `u128::MAX`. However, there is nothing
preventing the implementing pallet from passing a larger value which
overflows. This can happen if the implementing pallet does not validate
user input and instead relies on `named_reserve()` to return an error
(this saves an additional read)

This is not a security concern, as the method will subsequently return
an error thanks to `<Self as ReservableCurrency<_>>::reserve(who,
value)?;`. However, the `defensive_saturating_add` will panic in
`--all-features`, creating false positive crashes in fuzzing operations.

---------

Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
(cherry picked from commit f373af0)
@paritytech-cmd-bot-polkadot-sdk

Successfully created backport PR for stable2409:

github-actions bot pushed a commit that referenced this pull request Jan 29, 2025
This PR modifies `named_reserve()` in frame-balances to use checked math
instead of defensive saturating math.

The use of saturating math relies on the assumption that the sum of the
values will always fit in `u128::MAX`. However, there is nothing
preventing the implementing pallet from passing a larger value which
overflows. This can happen if the implementing pallet does not validate
user input and instead relies on `named_reserve()` to return an error
(this saves an additional read)

This is not a security concern, as the method will subsequently return
an error thanks to `<Self as ReservableCurrency<_>>::reserve(who,
value)?;`. However, the `defensive_saturating_add` will panic in
`--all-features`, creating false positive crashes in fuzzing operations.

---------

Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
(cherry picked from commit f373af0)
@paritytech-cmd-bot-polkadot-sdk

Successfully created backport PR for stable2412:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A4-needs-backport Pull request must be backported to all maintained releases. T2-pallets This PR/Issue is related to a particular pallet.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants