Skip to content
Business logic Audit finding Intermediate

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.

unforgiven Follow

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:

  1. totalReleaseAssets is 10000 AVAX.
  2. stakingTotalAssets is 1000 AVAX.
  3. current cycle rewards is 4000 AVAX and block.timestamp is currently in the middle of the cycle so current rewards is 2000 AVAX.
  4. totalAssets() is totalReleaseAssets + current rewards = 10000 + 2000 = 12000.
  5. contract balance is 10000 + 4000 - 1000 = 13000 AVAX.
  6. user1 has 90% contract shares and calls maxWithdraw() and code would calculate user assets as 10800 AVAX and available AVAX in contract as totalAssets() - stakingTotalAssets = 12000 - 1000 = 11000 and code would return 10800 as answer.
  7. now if user1 withdraws 10800 AVAX code would revert in the function beforeWithdraw() because code would try to execute totalReleaseAssets = totalReleaseAssets - amount = 10000 - 10800 and it would revert because of the underflow. so in reality user1 couldn’t withdraw 10800 AVAX which was the return value of the maxWithdraw() 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.


The original report as published on Code4rena, by unforgiven. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

  1. Set totalReleaseAssets to 10000 AVAX.
  2. Set stakingTotalAssets to 1000 AVAX.
  3. Set current cycle rewards to 4000 AVAX with block.timestamp in the middle of the cycle, so current rewards are 2000 AVAX.
  4. totalAssets() becomes totalReleaseAssets + current rewards = 12000 AVAX.
  5. Contract balance is 10000 + 4000 - 1000 = 13000 AVAX.
  6. 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.

More by unforgiven

View all

50 more disclosed reports, best first.

More from GoGoPool contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.