sherlock-audit / 2023-05-ironbank-judging

2 stars 2 forks source link

josephdara - Precision Loss causing loss of funds to the liquidator #488

Closed sherlock-admin closed 1 year ago

sherlock-admin commented 1 year ago

josephdara

high

Precision Loss causing loss of funds to the liquidator

Summary

The contract is vulnerable to precision loss in _getLiquidationSeizeAmount which calculates teh amount that would be sent to the liquidator. Which is introduced by division before multiplication.

Vulnerability Detail

In the function below,

    function _getLiquidationSeizeAmount(
        address marketBorrow,
        address marketCollateral,
        DataTypes.Market storage mCollateral,
        uint256 repayAmount
    ) internal view returns (uint256) {
        uint256 borrowMarketPrice = PriceOracleInterface(priceOracle).getPrice(marketBorrow);
        uint256 collateralMarketPrice = PriceOracleInterface(priceOracle).getPrice(marketCollateral);
        require(borrowMarketPrice > 0 && collateralMarketPrice > 0, "invalid price");

        // collateral amount = repayAmount * liquidationBonus * borrowMarketPrice / collateralMarketPrice
        // IBToken amount = collateral amount / exchangeRate
        //   = repayAmount * (liquidationBonus * borrowMarketPrice) / (collateralMarketPrice * exchangeRate)
        uint256 numerator = (mCollateral.config.liquidationBonus * borrowMarketPrice) / FACTOR_SCALE;
        uint256 denominator = (_getExchangeRate(mCollateral) * collateralMarketPrice) / 1e18;

        return (repayAmount * numerator) / denominator;
    }

Impact

This can cause operators to receive zero bonus and even loose their funds to the contract because the function is called in liquidate

Code Snippet

https://github.com/sherlock-audit/2023-05-ironbank/blob/main/ib-v2/src/protocol/pool/IronBank.sol#L821-L838

https://github.com/sherlock-audit/2023-05-ironbank/blob/main/ib-v2/src/protocol/pool/IronBank.sol#L505-L507

Tool used

Manual Review

Recommendation

All multiplications should happen before division