Vault rebalancing can be exploited if two vaults rebalance into the same vault
The rebalance function in a vault contract could be re-entered via a malicious ERC20 token during a swap, allowing an attacker to manipulate vault value calculations and steal funds. The attacker could siphon up to half the total rebalance amount.
*Submitted by 0x52, also found by ayeslick*
User funds stolen.
### Proof of Concept
Swap data is completely arbitrary and can be used to swap though malicious ERC20 tokens allowing control transfer. This control transfer would allow the attacker to call rebalance on a second vault and exploit both as long as both vaults rebalance into the same vault.
Assumptions:
Vault A and C both rebalance into vault B (i.e. value is transferred from vault A and C to vault B)
Vault A and C are both eligible for rebalances
Vault A -
Value: $100
Flashloan value: 50
Vault B -
Value: $100
Vault C -
Value: $100
Flashloan value: 50
1. User calls rebalance on vault A to trigger it rebalancing to vault B, storing vault B’s value as $100
2. During the swap control is transferred due to use of malicious ERC20 specified in swap data
3. Malicious token calls rebalance on vault C to trigger a rebalancing to vault B, storing vault B’s value as $100 because Vault B’s value hasn’t been modified yet.
4. Swap data in vault C rebalance swaps flashloan C to $50 worth of asset B
5. Vault C rebalance deposits swap funds into vault B
6. Vault C rebalance withdraws from vault C to pay back flashloan C
7. Vault C rebalance validates that the value of B = $150 ( 100 + 50 ) and finishes, resuming Vault A rebalance
8. Vault A rebalance finishes its swap, siphoning off the swapped funds to attacker through the malicious pool
9. Vault A rebalance doesn’t deposit any funds but the value of vault B has already been increased by rebalance C
10. Vault A rebalance withdraws from vault A to pay back vault flashloan A
11. Vault A rebalance validates that the value of B = $150 ( 100 + 50 )
The attacker has now stolen funds, up to half the value of the total rebalance amount.
### Recommended Mitigation Steps
Add nonReentrant modifier to MIMOAutomatedRebalnce.sol#rebalance.
**[RayXpub (Mimo) disagreed with severity and commented](https://github.com/code-423n4/2022-08-mimo-findings/issues/39#issuecomment-1210505384):**
> The attack described seems to be missing some elements mainly on item 9. Rebalance can’t choose to just not deposit as it calls `depositAndBorrow()` with a `mintAmount` computed onchain through the `_getAmounts()` function so it will have to mint some amount. This might be possible if the additional minting requirement by the second rebalance repayment can happen within the limits of vault B MCR. This seems to be an edge case and a complex attack, as it would require 2 vaults under trigger ratio, a malicious pool with enough liquidity and a user set mcr buffer high enough to not require additional deposit on second rebalance.
>
>
>
> Given the complexity and the low probability of this attack we think it should be downgraded to medium risk. We do plan on applying the recommendation of adding nonReentrant modifier.
**[gzeoneth (judge) decreased severity to Medium](https://github.com/code-423n4/2022-08-mimo-findings/issues/39)**
**horsefacts (warden) reviewed mitigation:**
> **Status:** �
Resolved
> **Finding:** Wardens identified that `MIMOAutoRebalance#rebalance` was vulnerable to reentrancy by swapping through a malicious token that transfers control to the caller.
> **What changed:** The Mimo team added a [reentrancy guard](https://github.com/mimo-capital/2022-08-mimo/blob/5186ef4be23f9dda81c8474096edb1f0594d70c3/contracts/actions/automated/MIMOAutoRebalance.sol#L59) to the `rebalance` function. An [integration test](https://github.com/mimo-capital/2022-08-mimo/blob/5186ef4be23f9dda81c8474096edb1f0594d70c3/test/02_integration/automated/MIMOAutoActionSecurity.test.ts#L137) demonstrates that the function is protected against reentrancy.
> **Why it works:** Since `rebalance` is protected by a reentrancy guard, attempts to reenter `rebalance` will now revert.
---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 rebalance function lacked a reentrancy guard, allowing an attacker-controlled token to re-enter the function during a swap and manipulate vault value checks.
What the attacker needed
Two vaults rebalance into the same vault; Both vaults eligible for rebalance; Attacker controls a malicious ERC20 token; Malicious pool with enough liquidity
How it was found
- User calls rebalance on vault A to trigger it rebalancing to vault B, storing vault B’s value as $100
- During the swap control is transferred due to use of malicious ERC20 specified in swap data
- Malicious token calls rebalance on vault C to trigger a rebalancing to vault B, storing vault B’s value as $100 because Vault B’s value hasn’t been modified yet.
- Swap data in vault C rebalance swaps flashloan C to $50 worth of asset B
- Vault C rebalance deposits swap funds into vault B
- Vault C rebalance withdraws from vault C to pay back flashloan C
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 smart contract that holds and manages funds. The rebalance function moves funds between vaults to keep them balanced. Because it calls an external token contract during a swap, an attacker can make that token call back into the rebalance function before the first call finishes, tricking the vault into miscalculating its value and letting the attacker take funds.
What is Race condition?
Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.