# Full Liquidation Won't Sweep the Whole Debts With Leaving Some, And Will Wrongly Set Borrowing as False

Liquidation logic treats debt/supply shares as asset amounts, so full liquidation leaves residual debt yet clears the borrowing flag, corrupting LTV and health factor.

- Payout: Audit contest
- Severity: high
- Program: New Scope
- Found by: 000000
- Disclosed: 2024-06-27
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2024-06-new-scope-judging/issues/107
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-06-new-scope%3AH-2
- Tags: area:web3 domain:web3 impact:integrity kind:contest platform:sherlock sev:high tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic vuln:liquidation vuln:precision-loss

**Target:** Lending pool liquidation logic (LiquidationLogic.sol)
**Root cause:** _calculateDebt and _calculateAvailableCollateralToLiquidate operate on raw debtShares/supplyShares without multiplying by borrow/liquidity index, so share values are later used as asset amounts in repayDebt and the userDebt==actualDebtToLiquidate check.
**Steps:**
1. Set up pool with equal-price oracles; alice supplies 60 tokenA, bob supplies 60 tokenB, alice borrows 45 tokenB
2. Warp 360 days so borrowIndex grows and position becomes unhealthy
3. Bob calls `liquidateSimple(tokenA, tokenB, pos, 100 ether)` intending full debt coverage
4. Observe debtShares drop from 45e18 to ~9.07e18 (residual debt) while `isBorrowing(tokenB)` flips to false
5. Apply fix converting shares to assets via getDebtBalance/getSupplyBalance; residual debt becomes 0
**How found:** Code-reading: noticed the IPoolSetters comment says debtToCover is in assets, then traced _calculateDebt and _calculateAvailableCollateralToLiquidate and saw no shares-to-assets conversion before values are passed to repayDebt and the borrowing-flag check.
**Exploitation:** Liquidator triggers full liquidation; because actualDebtToLiquidate is in shares not assets, repayDebt burns fewer shares than the true debt, leaving residual debtShares while the code sets borrowing=false, corrupting the user's debt accounting, LTV and health factor for the pool.
**Insight:** When auditing lending forks, verify every shares/asset boundary: grep for `.debtShares`/`.supplyShares` reads and confirm each is converted via index before being compared to user-supplied asset amounts or used in repay/burn calls.

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

**PoC test calling liquidateSimple for full debt**

```
function testLiquidationInvalidUnits() external {
    oracleA.updateAnswer(1e8);
    oracleB.updateAnswer(1e8);

    _mintAndApprove(alice, tokenA, mintAmountA, address(pool));
    _mintAndApprove(bob, tokenB, mintAmountB, address(pool));

    vm.startPrank(alice);
    pool.supplySimple(address(tokenA), alice, supplyAmountA, 0);
    vm.stopPrank();

    vm.startPrank(bob);
    pool.supplySimple(address(tokenB), bob, supplyAmountB, 0);
    vm.stopPrank();

    vm.startPrank(alice);
    pool.borrowSimple(address(tokenB), alice, borrowAmountB, 0);
    vm.stopPrank();

    // Advance time to make the position unhealthy
    vm.warp(block.timestamp + 360 days);
    oracleA.updateAnswer(1e8);
    oracleB.updateAnswer(1e8);

    // Print log of borrow rate before liquidation
    pool.forceUpdateReserve(address(tokenB));
    DataTypes.ReserveData memory reserveDataB = pool.getReserveData(address(tokenB));
    console.log("reserveDataB.borrowIndex before Liq.", reserveDataB.borrowIndex);

    DataTypes.PositionBalance memory positionBalance = pool.getBalanceRawByPositionId(address(tokenB), pos);
    console.log('debtShares Before Liq.', positionBalance.debtShares);

    DataTypes.UserConfigurationMap memory userConfig = pool.getUserConfiguration(alice, 0);
    console.log('TokenB isBorrowing Before Liq.', UserConfiguration.isBorrowing(userConfig, reserveDataB.id));

    vm.startPrank(bob);
    vm.expectEmit(true, true, true, false);
    emit PoolEventsLib.LiquidationCall(address(tokenA), address(tokenB), pos, 0, 0, bob);
    pool.liquidateSimple(address(tokenA), address(tokenB), pos, 100 ether); // @audit Tries to cover full debts
    vm.stopPrank();

    positionBalance = pool.getBalanceRawByPositionId(address(tokenB), pos);
    console.log('debtShares After Liq.', positionBalance.debtShares);

    userConfig = pool.getUserConfiguration(alice, 0);
    console.log('TokenB isBorrowing After Liq.', UserConfiguration.isBorrowing(userConfig, reserveDataB.id));
  }
```

**Oracle price setup diff for PoC**

```
function _setUpCorePool() internal {
    ... ...
    oracleA = new MockV3Aggregator(8, 1e8);
-   oracleB = new MockV3Aggregator(18, 2 * 1e8);
+   oracleB = new MockV3Aggregator(8, 1e8);
```

