AutoPxGmx.maxWithdraw and AutoPxGlp.maxWithdraw functions calculate asset amount that is too big and cannot be withdrawn
The maxWithdraw function in AutoPxGmx and AutoPxGlp does not account for the withdrawal penalty, so it returns an amount that is too large to withdraw. Calling withdraw with that amount causes the transaction to revert because the penalty increases the shares to burn beyond the user's balance.
Submitted by aphak5010
Impact
The ERC-4626 Tokenized Vault Standard requires the maxWithdraw function to be implemented (https://ethereum.org/en/developers/docs/standards/tokens/erc-4626/#maxwithdraw).
This function is supposed to return “the maximum amount of underlying assets that can be withdrawn from the owner balance with a single withdraw call”.
The PirexERC4626 contract implements this function (https://github.com/code-423n4/2022-11-redactedcartel/blob/03b71a8d395c02324cb9fdaf92401357da5b19d1/src/vaults/PirexERC4626.sol#L225).
It is implemented correctly when PirexERC4626 is used on its own.
However in this project, the PirexERC4626 contract is not used on its own but inherited by AutoPxGmx (https://github.com/code-423n4/2022-11-redactedcartel/blob/03b71a8d395c02324cb9fdaf92401357da5b19d1/src/vaults/AutoPxGmx.sol#L14) and AutoPxGlp (https://github.com/code-423n4/2022-11-redactedcartel/blob/03b71a8d395c02324cb9fdaf92401357da5b19d1/src/vaults/AutoPxGlp.sol#L14).
AutoPxGmx and AutoPxGlp implement a withdrawalPenalty i.e. a fee that is paid when a user wants to withdraw assets from the vault.
AutoPxGmx and AutoPxGlp do not override the maxWithdraw function.
This causes the maxWithdraw function to return an amount of assets that is too big to be withdrawn.
So when maxWithdraw is called and with the returned amount withdraw is called, the call to withdraw will revert.
This can cause issues in any upstream components that rely on AutoPxGmx and AutoPxGlp to correctly implement the ERC4626 standard.
For example an upstream wrapper might only allow withdrawals with the maximum amount and determine this maximum amount by calling the maxWithdraw function. As this function returns a value that is too big, no withdrawals will be possible.
Proof of Concept
- The
maxWithdrawfunction in aAutoPxGmxcontract is called - Now the
withdrawfunction is called with the value that was returned by themaxWithdrawfunction (https://github.com/code-423n4/2022-11-redactedcartel/blob/03b71a8d395c02324cb9fdaf92401357da5b19d1/src/vaults/AutoPxGmx.sol#L315) - The
withdrawfunction in turn calls thepreviewWithdrawfunction (https://github.com/code-423n4/2022-11-redactedcartel/blob/03b71a8d395c02324cb9fdaf92401357da5b19d1/src/vaults/AutoPxGmx.sol#L199) - The
previewWithdrawfunction will increase the amount of shares to include thewithdrawalPenalty(https://github.com/code-423n4/2022-11-redactedcartel/blob/03b71a8d395c02324cb9fdaf92401357da5b19d1/src/vaults/AutoPxGmx.sol#L215-L216) which causes the amount of shares to burn to be too large and the call toburnwill revert (https://github.com/code-423n4/2022-11-redactedcartel/blob/03b71a8d395c02324cb9fdaf92401357da5b19d1/src/vaults/AutoPxGmx.sol#L332)
Tools Used
VS Code
Recommended Mitigation Steps
In the AutoPxGmx and AutoPxGlp function, implement the maxWithdraw function that overrides the function in PirexERC4626 and takes into account the withdrawalPenalty.
Potential fix:
function maxWithdraw(address owner) public view override returns (uint256) {
uint256 shares = balanceOf(owner);
// Calculate assets based on a user's % ownership of vault shares
uint256 assets = convertToAssets(shares);
uint256 _totalSupply = totalSupply;
// Calculate a penalty - zero if user is the last to withdraw
uint256 penalty = (_totalSupply == 0 || _totalSupply - shares == 0)
? 0
: assets.mulDivDown(withdrawalPenalty, FEE_DENOMINATOR);
// Redeemable amount is the post-penalty amount
return assets - penalty;
}
drahrealm (Redacted Cartel) confirmed
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 inherited maxWithdraw function does not account for the withdrawal penalty, so it overestimates the withdrawable amount.
Impact
Any upstream component that relies on maxWithdraw to determine the maximum withdrawal amount may be unable to process withdrawals, as the returned value is too large and causes withdraw to revert.
What the attacker needed
User has shares in the vault; Withdrawal penalty is non-zero
The fix
Override maxWithdraw in AutoPxGmx and AutoPxGlp to subtract the withdrawal penalty from the calculated assets.
How it was found
- Call maxWithdraw on an AutoPxGmx contract.
- Call withdraw with the value returned by maxWithdraw.
- The withdraw function calls previewWithdraw, which increases the shares to burn to include the withdrawal penalty.
- The burn call reverts because the share amount is too large.
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 vault lets people deposit tokens and later withdraw them. The vault has a fee for early withdrawal, but the function that tells you the maximum you can withdraw ignores that fee. So if you try to withdraw that maximum, the transaction fails because the vault tries to take more shares than you have.
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.