audit-fix: Rounding Logic Flaws in Division Operations #12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Rounding Logic Flaws in Division Operations
Description
The program uses floor-rounding division
checked_divacross nearly all arithmetic operations. This behavior can be unsafe in certain scenarios, below are two examples.During the borrow operation, the user debt shares will be updated. The computed
sharesmay be underestimated, and in edge cases (e.g.,amount == 1), the result will always be zero, causing the borrower to receive the borrowed token without taking any debt shares.Flashloan fees are also computed using floor rounding. This causes the protocol to charge less than the intended fee.
Impact
Users may gain unintended benefits, resulting in a loss to the protocol and LPs.
Recommendation
Review all division operations across the codebase and adjust rounding behavior according to economic intent. The basic principle is to round in a way that favors the protocol and is unfavorable to the user.
Fix
ceil_div()inmath.rs.ceil_div()for protocol-favored rounding.