`XChainController::sendFundsToVault` can be griefed and leave `XChainController` in a bad state
The sendFundsToVault function lacks a check that a chain has already been processed, allowing any user to call it repeatedly and reset the vault's state prematurely. This leaves XChainController out of sync with vaults that still need funds, blocking rebalancing until an admin intervenes.
Source: https://github.com/sherlock-audit/2023-01-derby-judging/issues/271
Found by
Ch_301, cergyk, immeas, nobody2018, saidam017
Summary
A user can grief the send funds to vault state transition during by calling sendFundsToVault multiple times with the same vault.
Vulnerability Detail
During rebalancing, some vaults might need funds sent to them. They will be in state WaitingForFunds. To transition from here any user can trigger XChainController to send them funds by calling sendFundsToVault.
This is trigger per chain and will transfer funds from XChainController to the respective vaults on each chain.
At the end, when the vaults on each chain are processed and either have gotten funds sent to them or didn't need to sendFundsToVaults will trigger the state for this vaultNumber to be reset.
However, when transferring funds, there's never any check that this chain has not already been processed. So any user could simply call this function for a vault that either has no funds to transfer or where there's enough funds in XChainController and trigger the state reset for the vault.
PoC in xChaincontroller.test.ts, run after 4.5) Trigger vaults to transfer funds to xChainController:
it('5) Grief xChainController send funds to vaults', async function () {
await xChainController.sendFundsToVault(vaultNumber, slippage, 10000, 0, { value: 0, });
await xChainController.sendFundsToVault(vaultNumber, slippage, 10000, 0, { value: 0, });
await xChainController.sendFundsToVault(vaultNumber, slippage, 10000, 0, { value: 0, });
await xChainController.sendFundsToVault(vaultNumber, slippage, 10000, 0, { value: 0, });
expect(await xChainController.getFundsReceivedState(vaultNumber)).to.be.equal(0);
expect(await vault3.state()).to.be.equal(3);
// can't trigger state change anymore
await expect(xChainController.sendFundsToVault(vaultNumber, slippage, 1000, relayerFee, {value: parseEther('0.1'),})).to.be.revertedWith('Not all funds received');
});
Impact
XChainController ends up out of sync with the vault(s) that were supposed to receive funds.
guardian can resolve this by resetting the states using admin functions but these functions can still be frontrun by a malicious user.
Until this is resolved the rebalancing of the impacted vaults cannot continue.
Code Snippet
Tool used
Manual Review, hardhat
Recommendation
I recommend the protocol either keeps track of which vaults have been sent funds in XChainController.
or changes so a vault can only receive funds when waiting for them:
diff --git a/derby-yield-optimiser/contracts/MainVault.sol b/derby-yield-optimiser/contracts/MainVault.sol
index 8739e24..d475ee6 100644
--- a/derby-yield-optimiser/contracts/MainVault.sol
+++ b/derby-yield-optimiser/contracts/MainVault.sol
@@ -328,7 +328,7 @@ contract MainVault is Vault, VaultToken {
/// @notice Step 5 end; Push funds from xChainController to vaults
/// @notice Receiving feedback from xController when funds are received, so the vault can rebalance
function receiveFunds() external onlyXProvider {
- if (state != State.WaitingForFunds) return;
+ require(state == State.WaitingForFunds,stateError);
settleReservedFunds();
}
Discussion
sjoerdsommen
Duplicate with #163
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
XChainController becomes out of sync with vaults that were supposed to receive funds, and rebalancing of those vaults cannot continue until an admin resets the states, though the admin action can still be frontrun.
What the attacker needed
Any user can call sendFundsToVault; vault is in WaitingForFunds state
The fix
The protocol should track which vaults have been sent funds, or require that a vault is in the WaitingForFunds state before it can receive funds.
How it was found
- Trigger vaults to transfer funds to XChainController so they are in WaitingForFunds state.
- Call sendFundsToVault multiple times with the same vault number.
- Observe that the funds received state is reset to 0 and the vault state changes.
- Further calls to sendFundsToVault revert with 'Not all funds received'.
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 smart contract coordinates moving funds between vaults on different blockchains. It has a step that sends money to each vault and then resets a status flag. Because anyone can call that step and there is no guard against calling it twice, a user can reset the flag before the real transfers finish, so the system thinks the job is done when it is not.
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.