maxWithdraw() and maxRedeem() doesn’t return correct value which can make other contracts fail while working with protocol
The maxWithdraw() and maxRedeem() functions in TokenggAVAX return a maximum amount that can exceed the contract's totalReleasedAssets, because they use totalAssets() which includes current cycle rewards. An attacker or user could attempt to withdraw the returned amount, causing a revert due to underflow in beforeWithdraw(), breaking integrations and user experience.
Originally disclosed on Code4rena
Submitted by unforgiven
https://github.com/code-423n4/2022-12-gogopool/blob/aec9928d8bdce8a5a4efe45f54c39d4fc7313731/contracts/contract/tokens/TokenggAVAX.sol#L215-L223 https://github.com/code-423n4/2022-12-gogopool/blob/aec9928d8bdce8a5a4efe45f54c39d4fc7313731/contracts/contract/tokens/TokenggAVAX.sol#L205-L213
Functions maxWithdraw() and maxRedeem() returns max amount of assets or shares owner would be able to withdraw taking into account liquidity in the TokenggAVAX contract, but logics don’t consider that when user withdraws the withdrawal amounts subtracted from totalReleasedAssets (in beforeWithdraw() function) so the maximum amounts that can user withdraws should always be lower than totalReleasedAssets (which shows all the deposits and withdraws) but because functions maxWithdraw() and maxRedeem() uses totalAssets() to calculate available AVAX which includes deposits and current cycle rewards so those functions would return wrong value (whenever the return value is bigger than totalReleaseAssets then it would be wrong).
Proof of Concept
This is beforeWithdraw() code:
function beforeWithdraw(
uint256 amount,
uint256 /* shares */
) internal override {
totalReleasedAssets -= amount;
}
This is beforeWithdraw() code which is called whenever users withdraws their funds and as you can see the amount of withdrawal assets subtracted from totalReleaseAssets so withdrawal amounts can never be bigger than totalReleaseAssets. This is maxWithdraw() code:
function maxWithdraw(address _owner) public view override returns (uint256) {
if (getBool(keccak256(abi.encodePacked("contract.paused", "TokenggAVAX")))) {
return 0;
}
uint256 assets = convertToAssets(balanceOf[_owner]);
uint256 avail = totalAssets() - stakingTotalAssets;
return assets > avail ? avail : assets;
}
As you can see to calculate available AVAX in the contract address code uses totalAssets() - stakingTotalAssets and totalAssets() shows deposits + current cycle rewards so totalAssets() is bigger than totalReleaseAssets and the value of the totalAssets() - stakingTotalAssets can be bigger than totalReleaseAssets and if code returns avail as answer then the return value would be wrong.
Imagine this scenario:
totalReleaseAssetsis10000AVAX.stakingTotalAssetsis1000AVAX.- current cycle rewards is
4000AVAX andblock.timestampis currently in the middle of the cycle so current rewards is2000AVAX. totalAssets()istotalReleaseAssets + current rewards = 10000 + 2000 = 12000.- contract balance is
10000 + 4000 - 1000 = 13000AVAX. - user1 has 90% contract shares and calls
maxWithdraw()and code would calculate user assets as10800AVAX and available AVAX in contract astotalAssets() - stakingTotalAssets = 12000 - 1000 = 11000and code would return10800as answer. - now if user1 withdraws
10800AVAX code would revert in the functionbeforeWithdraw()because code would try to executetotalReleaseAssets = totalReleaseAssets - amount = 10000 - 10800and it would revert because of the underflow. so in reality user1 couldn’t withdraw10800AVAX which was the return value of themaxWithdraw()for user1.
The root cause of the bug is that the withdrawal amount is subtracted from totalReleaseAssets and so max withdrawal can never be totalReleaseAssets and function maxWithdraw() should never return value bigger than totalReleaseAssets. (the bug in function maxRedeem() is similar)
This bug would cause other contract or front end calls to fail, for example if the logic is something like this:
amount = maxWithdraw(user);
TokenggAVAX.withdrawAVAX(amount);
According the function definitions this code should work bug because of the the issue there are situations that this would revert and other contracts and UI can’t work properly with the protocol.
Tools Used
VIM
Recommended Mitigation Steps
Consider totalReleaseAssets in max withdrawal amount too.
emersoncloud (GoGoPool) confirmed
Alex the Entreprenerd (judge) commented:
The Warden has shown an inconsistency between the view functions and the actual behaviour of
TokenggAVAX. This breaks ERC4626, as well as offering subpar experience for end-users.For this reason I agree with Medium Severity.
emersoncloud (GoGoPool) commented:
Acknowledged.
I’ve added some tests to explore this more, but it’s a known issue with how we’ve implemented the streaming of rewards in our 4626. Some more context here https://github.com/fei-protocol/ERC4626/issues/24.
It’s most prevalent with low liquidity in ggAVAX.
We’re not going to fix in the first iteration of protocol.
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
Impact
This bug could cause other contracts or front-end calls that rely on maxWithdraw() to fail, as withdrawing the returned amount may revert, breaking integrations and user experience.
What the attacker needed
User has shares in TokenggAVAX; Contract has current cycle rewards; Low liquidity in ggAVAX
The fix
The maxWithdraw() and maxRedeem() functions should consider totalReleaseAssets when calculating the maximum withdrawal amount.
How it was found
- Set totalReleaseAssets to 10000 AVAX.
- Set stakingTotalAssets to 1000 AVAX.
- Set current cycle rewards to 4000 AVAX with block.timestamp in the middle of the cycle, so current rewards are 2000 AVAX.
- totalAssets() becomes totalReleaseAssets + current rewards = 12000 AVAX.
- Contract balance is 10000 + 4000 - 1000 = 13000 AVAX.
- User1 with 90% of shares calls maxWithdraw(), which returns 10800 AVAX, but withdrawing that amount reverts in beforeWithdraw() due to underflow.
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.
This report is about a smart contract that lets people deposit and withdraw a cryptocurrency called AVAX. The contract has functions that tell you the maximum you can withdraw, but they sometimes give a number that is too high. If you try to withdraw that amount, the transaction fails because the contract tries to subtract more than it has recorded, causing an error.
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.