BBCommon::_accrue wrong value is used to prevent overflow
The _accrue function clamps the accrued value using totalBorrowCap instead of the current totalBorrow.elastic, so if totalBorrow.elastic already exceeds totalBorrowCap, the clamp does not prevent an overflow. This can cause _accrue to revert, blocking most functions of a BigBang market.
Source: https://github.com/sherlock-audit/2024-02-tapioca-judging/issues/41
Found by
cergyk, duc
Summary
A mechanism is used in _accrue, to prevent overflow in order to avoid Dos on multiple entrypoints of a BigBang market (many external functions call on _accrue before executing their logic). However the wrong value is used to prevent an overflow, and even though it could be prevented the first time, it should overflow the second one it is called
Vulnerability Detail
We can see that the value to be accrued is clamped to type(uint128).max - totalBorrowCap
Which works the first time to avoid an overflow since totalBorrow.elastic should be less than totalBorrowCap. However if totalBorrow.elastic is already bigger than totalBorrowCap (due to a previous accrual), this clamping does not prevent overflow.
Impact
_accrue can still revert due to an overflow blocking most of the functions of a BigBang market.
Code Snippet
Tool used
Manual Review
Recommendation
Clamp accrued value to type(uint128).max - totalBorrow.elastic instead
Discussion
cryptotechmaker
Fixed by https://github.com/Tapioca-DAO/Tapioca-bar/pull/351
sherlock-admin4
The protocol team fixed this issue in PR/commit https://github.com/Tapioca-DAO/Tapioca-bar/pull/351.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The accrued value is clamped to type(uint128).max - totalBorrowCap instead of type(uint128).max - totalBorrow.elastic, so the clamp fails when totalBorrow.elastic already exceeds totalBorrowCap.
Impact
_accrue can still revert due to an overflow, blocking most functions of a BigBang market.
What the attacker needed
TotalBorrow.elastic exceeds totalBorrowCap
The fix
The accrued value should be clamped to type(uint128).max - totalBorrow.elastic instead; the protocol team fixed this issue in a pull request.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
In a smart contract, numbers have fixed maximum sizes. If a calculation produces a number too large for its storage type, the transaction fails with an overflow error. Here, a safety check used the wrong limit, so after one large accrual the next accrual could still overflow and make many functions unusable.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.