Malicious Users Can Drain The Assets Of Auto Compound Vault
The vault's withdrawal function rounds down the number of shares to burn, so a small withdrawal can require zero shares. An attacker could withdraw assets repeatedly without burning shares, draining the vault.
xiaoming90 Follow with pashov, adriro and 11 others
Originally disclosed on Code4rena
Submitted by xiaoming90, also found by pashov, adriro, poirots, unforgiven, bin2chen, PaludoX0, 0xSmartContract, ladboy233, Ruhum, cccz, koxuan, 8olidity, and rvierdiiev
Proof of Concept
Note: This issue affects both the AutoPxGmx and AutoPxGlp vaults. Since the root cause is the same, the PoC of AutoPxGlp vault is omitted for brevity.
The PirexERC4626.convertToShares function relies on the mulDivDown function in Line 164 when calculating the number of shares needed in exchange for a certain number of assets. Note that the computation is rounded down, therefore, if the result is less than 1 (e.g. 0.9), Solidity will round them down to zero. Thus, it is possible that this function will return zero.
File: PirexERC4626.sol
156: function convertToShares(uint256 assets)
157: public
158: view
159: virtual
160: returns (uint256)
161: {
162: uint256 supply = totalSupply; // Saves an extra SLOAD if totalSupply is non-zero.
163:
164: return supply == 0 ? assets : assets.mulDivDown(supply, totalAssets());
165: }
The AutoPxGmx.previewWithdraw function relies on the PirexERC4626.convertToShares function in Line 206. Thus, this function will also “round down”.
File: AutoPxGmx.sol
199: function previewWithdraw(uint256 assets)
200: public
201: view
202: override
203: returns (uint256)
204: {
205: // Calculate shares based on the specified assets' proportion of the pool
206: uint256 shares = convertToShares(assets);
207:
208: // Save 1 SLOAD
209: uint256 _totalSupply = totalSupply;
210:
211: // Factor in additional shares to fulfill withdrawal if user is not the last to withdraw
212: return
213: (_totalSupply == 0 || _totalSupply - shares == 0)
214: ? shares
215: : (shares * FEE_DENOMINATOR) /
216: (FEE_DENOMINATOR - withdrawalPenalty);
217: }
The AutoPxGmx.withdraw function relies on the AutoPxGmx.previewWithdraw function. In certain conditions, the AutoPxGmx.previewWithdraw function in Line 323 will return zero if the withdrawal amount causes the division within the PirexERC4626.convertToShares function to round down to zero (usually due to a small amount of withdrawal amount).
If the AutoPxGmx.previewWithdraw function in Line 323 returns zero, no shares will be burned at Line 332. Subsequently, in Line 336, the contract will transfer the assets to the users. As a result, the users receive the assets without burning any of their shares, effectively allowing them to receive assets for free.
File: AutoPxGmx.sol
315: function withdraw(
316: uint256 assets,
317: address receiver,
318: address owner
319: ) public override returns (uint256 shares) {
320: // Compound rewards and ensure they are properly accounted for prior to withdrawal calculation
321: compound(poolFee, 1, 0, true);
322:
323: shares = previewWithdraw(assets); // No need to check for rounding error, previewWithdraw rounds up.
324:
325: if (msg.sender != owner) {
326: uint256 allowed = allowance[owner][msg.sender]; // Saves gas for limited approvals.
327:
328: if (allowed != type(uint256).max)
329: allowance[owner][msg.sender] = allowed - shares;
330: }
331:
332: _burn(owner, shares);
333:
334: emit Withdraw(msg.sender, receiver, owner, assets, shares);
335:
336: asset.safeTransfer(receiver, assets);
337: }
Assume that the vault with the following state:
- Total Asset = 1000 WETH
- Total Supply = 10 shares
Assume that Alice wants to withdraw 99 WETH from the vault. Thus, she calls the AutoPxGmx.withdraw(99 WETH) function.
The PirexERC4626.convertToShares function will compute the number of shares that Alice needs to burn in exchange for 99 WETH.
assets.mulDivDown(supply, totalAssets())
99WETH.mulDivDown(10 shares, 1000WETH)
(99 * 10) / 1000
990 / 1000 = 0.99 = 0
However, since Solidity rounds 0.99 down to 0, Alice does not need to burn a single share. She will receive 99 WETH for free.
Impact
Malicious users can withdraw the assets from the vault for free, effectively allowing them to drain the assets of the vault.
Recommended Mitigation Steps
Ensure that at least 1 share is burned when the users withdraw their assets.
This can be mitigated by updating the previewWithdraw function to round up instead of round down when computing the number of shares to be burned.
function previewWithdraw(uint256 assets)
public
view
override
returns (uint256)
{
// Calculate shares based on the specified assets' proportion of the pool
- uint256 shares = convertToShares(assets);
+ uint256 shares = supply == 0 ? assets : assets.mulDivUp(supply, totalAssets());
// Save 1 SLOAD
uint256 _totalSupply = totalSupply;
// Factor in additional shares to fulfill withdrawal if user is not the last to withdraw
return
(_totalSupply == 0 || _totalSupply - shares == 0)
? shares
: (shares * FEE_DENOMINATOR) /
(FEE_DENOMINATOR - withdrawalPenalty);
}
kphed (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 share calculation for withdrawals uses a rounding-down division, so small withdrawals can round to zero shares.
Impact
Malicious users could withdraw assets from the vault for free, potentially draining the vault.
What the attacker needed
A vault with assets and shares; a small withdrawal amount
The fix
The previewWithdraw function should round up instead of down when computing the number of shares to burn, ensuring at least one share is burned.
How it was found
- Assume the vault has 1000 WETH total assets and 10 shares total supply.
- Alice calls withdraw with 99 WETH.
- The share calculation rounds 0.99 down to 0.
- No shares are burned, but 99 WETH is transferred to Alice.
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 assets and receive shares in return. When they withdraw, the vault calculates how many shares to burn. Because the calculation rounds down, a small withdrawal can round to zero shares, so the person gets assets back without giving up any shares.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.