Lack of Balance Validation
The pool's balance ratio invariant was not checked in the deposit and withdraw functions that use _reserveTokenSpecified, so a user could deposit or withdraw and push the ratio outside the allowed interval. This could break the pool's invariant.
Mirror Follow with prapandey031, d3e4 and 9 others
Originally disclosed on Code4rena
Submitted by Mirror, also found by prapandey031, d3e4, Udsen, ItsNio, pontifex (1, 2), ktg, markus_ether, Testerbot, mert_eren, T1MOH, oakcobalt, and skodi
Description
The pool’s ratio of y to x must be within the interval [MIN_M, MAX_M), which will be checked by the _checkBalances() function. External view functions will call the _swap(), _reserveTokenSpecified() or _lpTokenSpecified() functions to get the specified result. However, _checkBalances() is only used in the _swap() and _lpTokenSpecified() functions. There is no balance validation for depositGivenInputAmount() and withdrawGivenOutputAmount() functions, which use the _reserveTokenSpecified() function.
Impact
If are no other validations outside of these two functions, user deposits/withdrawls may break the invariant, i.e. the pool’s ratio of y to x is outside the interval [MIN_M, MAX_M).
Proof of Concept
Add the following code in the test/EvolvingProteusProperties.t.sol file to the EvolvingProteusProperties contract, and run forge test --mt RatioOutsideExpectedInterval:
function testDepositRatioOutsideExpectedInterval(uint256 x0, uint256 y0, uint256 s0, uint256 depositedAmount) public {
int128 MIN_M = 0x00000000000002af31dc461;
uint256 INT_MAX_SQRT = 0xb504f333f9de6484597d89b3754abe9f;
vm.assume(x0 >= MIN_BALANCE && x0 <= INT_MAX_SQRT);
vm.assume(y0 >= MIN_BALANCE && y0 <= INT_MAX_SQRT);
vm.assume(s0 >= MIN_BALANCE && s0 <= INT_MAX_SQRT);
vm.assume(depositedAmount >= MIN_OPERATING_AMOUNT && depositedAmount < INT_MAX_SQRT && depositedAmount >= 2 * uint256(FIXED_FEE));
vm.assume(y0/x0 <= MAX_BALANCE_AMOUNT_RATIO);
vm.assume(x0/y0 <= MAX_BALANCE_AMOUNT_RATIO);
vm.assume(int256(y0).divi(int256(x0) + int256(depositedAmount)) < MIN_M); // breaks the invariant
SpecifiedToken depositedToken = SpecifiedToken.X;
vm.expectRevert(); // There should be at least one case that call did not revert as expected
DUT.depositGivenInputAmount(
x0,
y0,
s0,
depositedAmount,
depositedToken
);
}
function testWithdrawRatioOutsideExpectedInterval(uint256 x0, uint256 y0, uint256 s0, uint256 withdrawnAmount) public {
int128 MIN_M = 0x00000000000002af31dc461;
uint256 INT_MAX_SQRT = 0xb504f333f9de6484597d89b3754abe9f;
vm.assume(x0 >= MIN_BALANCE && x0 <= INT_MAX_SQRT);
vm.assume(y0 >= MIN_BALANCE && y0 <= INT_MAX_SQRT);
vm.assume(s0 >= MIN_BALANCE && s0 <= INT_MAX_SQRT);
vm.assume(withdrawnAmount >= MIN_OPERATING_AMOUNT && withdrawnAmount < INT_MAX_SQRT && withdrawnAmount >= 2 * uint256(FIXED_FEE));
vm.assume(y0/x0 <= MAX_BALANCE_AMOUNT_RATIO);
vm.assume(x0/y0 <= MAX_BALANCE_AMOUNT_RATIO);
vm.assume(withdrawnAmount < y0); // no more than balance
vm.assume((int256(y0) - int256(withdrawnAmount)).divi(int256(x0)) < MIN_M); // breaks the invariant
SpecifiedToken withdrawnToken = SpecifiedToken.Y;
vm.expectRevert(); // There should be at least one case that call did not revert as expected
DUT.withdrawGivenOutputAmount(
x0,
y0,
s0,
withdrawnAmount,
withdrawnToken
);
}
Recommended Mitigation Steps
It’s recommended to add _checkBalances(xi + specifiedAmount, yi) after L579 and add _checkBalances(xi, yi + specifiedAmount) after L582.
Assessed type
Invalid Validation
viraj124 (Shell) commented via duplicate Issue #268:
This should be low/med at best IMO. We’re adding the balance check, but note _
getUtilityis an internal method and there are input checks of the reserve balances values passed, so this is an invalid argument.
viraj124 (Shell) confirmed and commented via duplicate Issue #268:
We checked this further with some other members of the team and agreed this is high severity. We’ve fixed this in a PR.
Dravee (judge) increased severity to High
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
Likely the _checkBalances() validation was omitted from the code paths for depositGivenInputAmount() and withdrawGivenOutputAmount(), which call _reserveTokenSpecified().
Impact
User deposits or withdrawals may break the invariant that the pool's ratio of y to x stays within [MIN_M, MAX_M), if no other validations exist outside these functions.
What the attacker needed
Ability to call deposit or withdraw functions
The fix
The project added the balance check and fixed this in a pull request.
How it was found
- Set up initial balances and a deposit amount such that the resulting ratio falls below the minimum.
- Call depositGivenInputAmount with those values and observe that it does not revert.
- Set up initial balances and a withdrawal amount such that the resulting ratio falls below the minimum.
- Call withdrawGivenOutputAmount with those values and observe that it does not revert.
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.
A liquidity pool keeps a ratio between two token balances within a safe range. The code checked this range in some actions but forgot to check it when depositing or withdrawing, so those actions could push the ratio out of range. That can break the pool's rules and potentially harm other users.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.