Native funds sent with pushVaultAmounts and sendFundsToVault can be lost
The XChainController contract's pushVaultAmounts and sendFundsToVault functions accept native funds to cover cross-chain fees but do not use or return them when the target chain is the same as the controller's chain. As a result, the caller's funds become permanently frozen in the contract balance.
Source: https://github.com/sherlock-audit/2023-01-derby-judging/issues/398
Found by
hyh
Summary
XChainController's pushVaultAmounts() and sendFundsToVault() do not always use the native funds provided to cover the fees.
In the cases when the funds aren't used they are not returned, ending up frozen on the contract balance as no balance utilization is now implemented.
Vulnerability Detail
Native funds attached to the pushVaultAmounts() and sendFundsToVault() calls are frozen with XChainController when not used.
Impact
Caller's funds meant to cover x-chain transfers are permanently frozen on the XChainController balance when getVaultChainIdOff(_vaultNumber, _chain).
Code Snippet
pushVaultAmounts() doesn't call sendXChainAmount() and use funds if getVaultChainIdOff(_vaultNumber, _chain):
function pushVaultAmounts(
uint256 _vaultNumber,
uint16 _chain
) external payable onlyWhenUnderlyingsReceived(_vaultNumber) {
address vault = getVaultAddress(_vaultNumber, _chain);
require(vault != address(0), "xChainController: not a valid vaultnumber");
int256 totalAllocation = getCurrentTotalAllocation(_vaultNumber);
uint256 totalWithdrawalRequests = getTotalWithdrawalRequests(_vaultNumber);
uint256 totalUnderlying = getTotalUnderlyingVault(_vaultNumber) - totalWithdrawalRequests;
uint256 totalSupply = getTotalSupply(_vaultNumber);
uint256 decimals = xProvider.getDecimals(vault);
uint256 newExchangeRate = (totalUnderlying * (10 ** decimals)) / totalSupply;
>> if (!getVaultChainIdOff(_vaultNumber, _chain)) {
int256 amountToChain = calcAmountToChain(
_vaultNumber,
_chain,
totalUnderlying,
totalAllocation
);
(int256 amountToDeposit, uint256 amountToWithdraw) = calcDepositWithdraw(
_vaultNumber,
_chain,
amountToChain
);
>> sendXChainAmount(_vaultNumber, _chain, amountToDeposit, amountToWithdraw, newExchangeRate);
}
}
function sendXChainAmount(
uint256 _vaultNumber,
uint32 _chainId,
int256 _amountDeposit,
uint256 _amountToWithdraw,
uint256 _exchangeRate
) internal {
address vault = getVaultAddress(_vaultNumber, _chainId);
bool receivingFunds;
uint256 amountToSend = 0;
if (_amountDeposit > 0 && _amountDeposit < minimumAmount) {
vaultStage[_vaultNumber].fundsReceived++;
} else if (_amountDeposit >= minimumAmount) {
receivingFunds = true;
setAmountToDeposit(_vaultNumber, _chainId, _amountDeposit);
vaultStage[_vaultNumber].fundsReceived++;
}
if (_amountToWithdraw > 0 && _amountToWithdraw < uint(minimumAmount)) {
vaultStage[_vaultNumber].fundsReceived++;
} else if (_amountToWithdraw >= uint(minimumAmount)) {
amountToSend = _amountToWithdraw;
}
>> xProvider.pushSetXChainAllocation{value: msg.value}(
vault,
_chainId,
amountToSend,
_exchangeRate,
receivingFunds
);
emit SendXChainAmount(vault, _chainId, amountToSend, _exchangeRate, receivingFunds);
}
sendFundsToVault() similarly do not use funds when getVaultChainIdOff(_vaultNumber, _chain):
function sendFundsToVault(
uint256 _vaultNumber,
uint256 _slippage,
uint32 _chain,
uint256 _relayerFee
) external payable onlyWhenFundsReceived(_vaultNumber) {
address vault = getVaultAddress(_vaultNumber, _chain);
require(vault != address(0), "xChainController: not a valid vaultnumber");
>> if (!getVaultChainIdOff(_vaultNumber, _chain)) {
uint256 amountToDeposit = getAmountToDeposit(_vaultNumber, _chain);
if (amountToDeposit > 0) {
address underlying = getUnderlyingAddress(_vaultNumber, _chain);
uint256 balance = IERC20(underlying).balanceOf(address(this));
if (amountToDeposit > balance) amountToDeposit = balance;
IERC20(underlying).safeIncreaseAllowance(address(xProvider), amountToDeposit);
>> xProvider.xTransferToVaults{value: msg.value}(
vault,
_chain,
amountToDeposit,
underlying,
_slippage,
_relayerFee
);
setAmountToDeposit(_vaultNumber, _chain, 0);
emit SentFundsToVault(vault, _chain, amountToDeposit, underlying);
}
}
vaultStage[_vaultNumber].fundsSent++;
if (vaultStage[_vaultNumber].fundsSent == chainIds.length) resetVaultStages(_vaultNumber);
}
Tool used
Manual Review
Recommendation
Consider returning the funds to the caller in both cases.
if (!getVaultChainIdOff(_vaultNumber, _chain)) {
...
} else {
if (msg.value > 0) {
send msg.value to the caller
}
}
Discussion
sjoerdsommen
payable eth amount to cover connext fees are stuck in the xchaincontroller when the funds are sent to the same chain as the xchaincontroller; correct but as we do the triggers ourselves we would never send any funds when doing the trigger to the homechain. Therefore not high but medium.
sjoerdsommen
https://github.com/derbyfinance/derby-yield-optimiser/pull/192
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 functions do not return unused native funds when the cross-chain transfer is skipped due to the target chain being the controller's own chain.
Impact
Callers' funds intended for cross-chain fees are permanently frozen in the contract when the target chain is the controller's chain; the project team considers this medium severity because they would not trigger such calls themselves.
What the attacker needed
Caller sends native funds with the call; target chain is the controller's chain
The fix
The contract should return any unused native funds to the caller when the cross-chain transfer is skipped.
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.
When you call a function that sends cryptocurrency, you can attach extra funds to pay for fees. Here, if the destination is the same chain as the contract, the function skips the cross-chain transfer but keeps the extra funds, and there is no way to get them back. So the money is stuck forever.
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.