# Wrong calculation of supply/debt balance of a position, disrupting core system functionalities

PositionBalanceConfiguration returned share amounts instead of asset amounts, breaking withdraw, full repay, NFT position accounting, and vault totalAssets across the lending pool.

- 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/473
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-06-new-scope%3AH-11
- Tags: area:web3 domain:web3 impact:dos impact:integrity kind:contest platform:sherlock sev:high tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic vuln:precision-loss

**Target:** Lending pool position balance accounting (PositionBalanceConfiguration, SupplyLogic, BorrowLogic, NFTPositionManagerSetters, CuratedVault)
**Root cause:** getSupplyBalance/getDebtBalance compute `shares + (shares.rayMul(index) - shares.rayMul(lastIndex))` instead of `shares.rayMul(index)`, returning share units rather than asset units.
**Steps:**
1. Alice supplies 2000 ether tokenA and borrows 800 ether; warp 10 minutes so debt accrues.
2. Alice borrows 20 ether more, updating reserve.lastDebtLiquidityIndex.
3. Call `pool.forceUpdateReserve(tokenA)` then `pool.repaySimple(tokenA, UINT256_MAX, 0)`.
4. Observe debt before repay 819999977330884982376 but after repay still 929433690028148 (dust remains) because payback.assets is computed from wrong getDebtBalance.
5. Run `forge test --mt testRepayFailWithUint256MAX -vvv` to reproduce.
**How found:** Code review of PositionBalanceConfiguration comparing getSupplyBalance/getDebtBalance against ReserveSuppliesConfiguration equivalents; noticed the returned value is in share units, then wrote a Foundry PoC to confirm full-repay failure.
**Exploitation:** No direct theft; the miscalculation causes full-repay with type(uint256).max to leave residual debt, prevents setBorrowing(false), breaks withdraw balance checks, corrupts NFT position reward accounting and refunds (BalanceMisMatch revert), and makes CuratedVault.totalAssets report wrong asset totals.
**Insight:** When auditing share-based lending math, verify every balance getter returns assets (shares.rayMul(index)) not shares; a single wrong unit propagates into repay-max, withdraw, reward, and vault accounting paths.

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

**Foundry PoC test repay fails with uint256 max**

```
function testRepayFailWithUint256MAX() external {
    _mintAndApprove(alice, tokenA, 4000 ether, address(pool));

    // Set the reserve factor to 1000 bp (10%)
    poolFactory.setReserveFactor(10_000);

    // Alice supplies and borrows tokenA from the pool
    vm.startPrank(alice);
    pool.supplySimple(address(tokenA), alice, 2000 ether, 0);
    pool.borrowSimple(address(tokenA), alice, 800 ether, 0);

    vm.warp(block.timestamp + 10 minutes);

    assertGt(pool.getDebt(address(tokenA), alice, 0), 0);
    vm.stopPrank();

    // borrow again: this will update reserve.lastDebtLiquidtyIndex
    vm.startPrank(alice);
    pool.borrowSimple(address(tokenA), alice, 20 ether, 0);
    vm.stopPrank();

    pool.forceUpdateReserve(address(tokenA));

    console.log("Debt before repay: ", pool.getDebt(address(tokenA), alice, 0));
    vm.startPrank(alice);
    tokenA.approve(address(pool), UINT256_MAX);
    pool.repaySimple(address(tokenA), UINT256_MAX, 0);
    console.log("Debt after  repay: ", pool.getDebt(address(tokenA), alice, 0));

    console.log("Assert: Debt still exists after full-repay with UINT256_MAX");
    assertNotEq(pool.getDebt(address(tokenA), alice, 0), 0);
    vm.stopPrank();
  }
```

**Command to run the PoC test**

```
forge test --mt testRepayFailWithUint256MAX -vvv
```

