# Rounding discrepancy between `MorphoLendingRouter::healthFactor` and `Morpho::repay` causes position migration failures

MorphoLendingRouter::healthFactor rounds borrow assets down while Morpho::repay rounds up, so flash-loaned migration amounts fall ~1 wei short and migratePosition reverts.

- Payout: Audit contest
- Severity: medium
- Program: Notional
- Found by: 0xRstStn
- Disclosed: 2025-06-27
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2025-06-notional-exponent-judging/issues/417
- Library page: https://bugbountylibrary.com/r/sherlock%3A2025-06-notional-exponent%3AM-7
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:dos vuln:precision-loss

**Target:** MorphoLendingRouter / AbstractLendingRouter migratePosition on Morpho Blue
**Root cause:** healthFactor computes borrowed = shares*totalBorrowAssets/totalBorrowShares (round down, no virtual offsets), while Morpho uses toAssetsUp with VIRTUAL_ASSETS/VIRTUAL_SHARES; the migration flash loan is sized from the smaller value but repay demands the larger.
**Steps:**
1. Call `migratePosition(onBehalf, vault, migrateFrom)` where migrateFrom is a MorphoLendingRouter.
2. healthFactor returns borrowAmount rounded down; `_enterPosition` flash-borrows exactly that amount.
3. In `onMorphoFlashLoan`, `_enterOrMigrate` calls `exitPosition(..., type(uint256).max, ...)`.
4. `_exitWithRepay` sets sharesToRepay = position.borrowShares and calls `MORPHO.repay(m, 0, sharesToRepay, onBehalf, repayData)`.
5. Morpho computes assetToRepay via `toAssetsUp` (rounded up, +1 wei).
6. `onMorphoRepay` does `ERC20(asset).safeTransferFrom(receiver, address(this), assetToRepay)` but router holds only the rounded-down flash loan; transfer reverts.
7. PoC: compare `(shares*totalBorrowAssets)/totalBorrowShares` vs `mulDivUp(shares, totalBorrowAssets+1, totalBorrowShares+1e6)` on eUSDE/USDE market 0x140fe...fd3c; 104 vs 105.
**How found:** Compared Notional's integer-division borrow conversion in healthFactor against Morpho Blue's toAssetsUp/_isHealthy rounding, noticing the migration path sizes a flash loan from the former but repays via the latter; confirmed with a fork test on a live interest-accruing market.
**Insight:** When a protocol wraps an external lending market, audit every share-to-asset conversion against the underlying's exact rounding direction and virtual offsets — any wrapper value used to size a flash loan or exact-repayment transfer must match the external protocol's round-up math or full-exit migrations revert by 1 wei.

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

**Foundry PoC comparing round-down vs round-up borrow amount**

```
function test_healthFactorReturnsIncorrectBorrowAmount() public pure {
        // 1. User will try to redeem 100e6 shares;
        uint256 shares = 100e6; 

        // Snapshoted values from Morpho market 0x140fe48783fe88d2a52b31705577d917628caaf74ff79865b39d4c2aa6c2fd3c (eUSDE, USDE)
        // https://app.morpho.org/ethereum/market/0x140fe48783fe88d2a52b31705577d917628caaf74ff79865b39d4c2aa6c2fd3c/eusde-usde
        // 07/16/2025
        uint256 totalBorrowAssets = 1683699089027334601033968;
        uint256 totalBorrowShares = 1607215221550593809991229062204;

        uint256 notionalResult = _calculationNotional(shares, totalBorrowAssets, totalBorrowShares); // Round down to 104
        uint256 morphoResult = _calculationMorpho(shares, totalBorrowAssets, totalBorrowShares); // Round up to 105

        console2.log("Protocol borrow amount calculated: ", notionalResult);
        console2.log("Needed assets to repay loan by Morpho calculation: ", morphoResult);

        assertLt(notionalResult, morphoResult);
    }
```

**On-chain fork test reading real Morpho market**

```
function test_healthFactorReturnsIncorrectBorrowAmountOnChain() public view {
        uint256 shares = 100e6; 

        Id id = Id.wrap(0x140fe48783fe88d2a52b31705577d917628caaf74ff79865b39d4c2aa6c2fd3c);
        (,, uint128 totalBorrowAssets, uint128 totalBorrowShares,,) = IMorphoStaticTyping(address(MORPHO)).market(id);

        uint256 notionalResult = _calculationNotional(shares, totalBorrowAssets, totalBorrowShares);
        uint256 morphoResult = _calculationMorpho(shares, totalBorrowAssets, totalBorrowShares);

        console2.log("Protocol borrow amount calculated: ", notionalResult);
        console2.log("Needed assets to repay loan by Morpho calculation: ", morphoResult);

        assertLt(notionalResult, morphoResult);
    }
```

**toAssetsUp and mulDivUp rounding helpers**

```
function toAssetsUp(uint256 shares, uint256 totalAssets, uint256 totalShares) internal pure returns (uint256) {
        return mulDivUp(shares, totalAssets + VIRTUAL_ASSETS, totalShares + VIRTUAL_SHARES);
    }

    /// @dev Returns (`x` * `y`) / `d` rounded up.
    function mulDivUp(uint256 x, uint256 y, uint256 d) internal pure returns (uint256) {
        return (x * y + (d - 1)) / d;
    }
```

