Skip to content
Business logic Audit finding Intermediate

`updateState()` should be called in `depositEth()` and `redeemEth()`

The depositEth and redeemEth functions in the LEther contract did not call updateState before changing the liquidity, so the interest rate used for pending interest calculations was based on an outdated utilisation ratio. This could cause borrowers to pay incorrect interest amounts, either overpaying or underpaying, leading to losses for lenders or borrowers.

Lambda Follow with Ruhum, bytehat and 5 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2022-08-sentiment-judging/tree/main/085-H

Found by

Lambda, Ruhum, bytehat, WATCHPUG, xiaoming90, pashov, cccz, GimelSec

Summary

Whenever the liquidity of a LToken changes, getRateFactor will be changed.

Therefore, updateState() must be called prior to the change to settle the pending interests.

Vulnerability Detail

Alice is a liquidity provider for LEther, Bob is a borrower.

  1. Alice added 1000 ETH;
  2. Bob borrowed 500 ETH; In which updateState() is called. The util in getBorrowRatePerSecond is 0.5 now.
  3. One year later (no one interacts with the asset for 1 year), Alice redeemed 500 ETH with redeemEth(), in which updateState() is not called. The util in getBorrowRatePerSecond is 1 now.
  4. Bob called repay(), in which updateState() is called to calculates the pending interest:

For Bob the borrower, the sum of principal and interest is:

$$ Borrow Rate Per Second = c3 \cdot (util \cdot c1 + util^{32} \cdot c1 + util^{64} \cdot c2) \div secsPerYear = 5545529241 $$

$$ rateFactor = Borrow Rate Per Second \cdot secsPerYear \div 1e18 = 175000000081490720 $$

$$ sum = borrow \cdot rateFactor \div 1e18 + borrow = 587 $$

But the actual sum is as below, due to updateState() is not called in redeemEth():

$$ Borrow Rate Per Second = c3 \cdot (util \cdot c1 + util^{32} \cdot c1 + util^{64} \cdot c2) \div secsPerYear = 55455292386 $$

$$ rateFactor = Borrow Rate Per Second \cdot secsPerYear \div 1e18 = 1750000000000000000 $$

$$ sum = borrow \cdot rateFactor \div 1e18 + borrow = 1375 $$

As a result, Bob the borrower is now paying 1375 instead of 587 for the interest, which is 2x the expected amount.

On the other hand, if another liquidity provider called depositEth() before Bob repays the loan, the actual interest can be lower than expected, which constitutes a loss of yields to Alice.

Impact

Incorrect amounts of interests will be paid by the borrowers, which can result in loss of yields to the lenders or overpaid interest for the borrowers.

Code Snippet

https://github.com/sentimentxyz/protocol/blob/4e45871e4540df0f189f6c89deb8d34f24930120/src/tokens/LToken.sol#L200-L227

Solidity
    function updateState() public {
        if (lastUpdated == block.timestamp) return;
        uint rateFactor = getRateFactor();
        uint interestAccrued = borrows.mulWadUp(rateFactor);
        borrows += interestAccrued;
        reserves += interestAccrued.mulWadUp(reserveFactor);
        lastUpdated = block.timestamp;
    }

    /* -------------------------------------------------------------------------- */
    /*                             INTERNAL FUNCTIONS                             */
    /* -------------------------------------------------------------------------- */

    /**
        @dev Rate Factor = Timestamp Delta * 1e18 (Scales timestamp delta to 18 decimals) * Interest Rate Per Block
            Timestamp Delta = Number of seconds since last update
    */
    function getRateFactor() internal view returns (uint) {
        return (block.timestamp == lastUpdated) ?
            0 :
            ((block.timestamp - lastUpdated)*1e18)
            .mulWadUp(
                rateModel.getBorrowRatePerSecond(
                    asset.balanceOf(address(this)),
                    borrows
                )
            );
    }

https://github.com/sentimentxyz/protocol/blob/4e45871e4540df0f189f6c89deb8d34f24930120/src/core/DefaultRateModel.sol#L51-L77

Solidity
function getBorrowRatePerSecond(
        uint liquidity,
        uint borrows
    )
        external
        view
        returns (uint)
    {
        uint util = _utilization(liquidity, borrows);
        return c3.mulDivDown(
            (
                util.mulWadDown(c1)
                + util.rpow(32, SCALE).mulWadDown(c1)
                + util.rpow(64, SCALE).mulWadDown(c2)
            ),
            secsPerYear
        );
    }

    function _utilization(uint liquidity, uint borrows)
        internal
        pure
        returns (uint)
    {
        uint totalAssets = liquidity + borrows;
        return (totalAssets == 0) ? 0 : borrows.divWadDown(totalAssets);
    }

updatestate must be called everytime balance of asset is changed. https://github.com/sentimentxyz/protocol/blob/4e45871e4540df0f189f6c89deb8d34f24930120/src/tokens/LEther.sol#L26-L53

Solidity
    /**
        @notice Wraps Eth sent by the user and deposits into the LP
            Transfers shares to the user denoting the amount of Eth deposited
        @dev Emits Deposit(caller, owner, assets, shares)
    */
    function depositEth() external payable {
        uint assets = msg.value;
        uint shares = previewDeposit(assets);
        require(shares != 0, "ZERO_SHARES");
        IWETH(address(asset)).deposit{value: assets}();
        _mint(msg.sender, shares);
        emit Deposit(msg.sender, msg.sender, assets, shares);
    }

    /**
        @notice Unwraps Eth and transfers it to the caller
            Amount of Eth transferred will be the total underlying assets that
            are represented by the shares
        @dev Emits Withdraw(caller, receiver, owner, assets, shares);
        @param shares Amount of shares to redeem
    */
    function redeemEth(uint shares) external {
        uint assets = previewRedeem(shares);
        _burn(msg.sender, shares);
        emit Withdraw(msg.sender, msg.sender, msg.sender, assets, shares);
        IWETH(address(asset)).withdraw(assets);
        msg.sender.safeTransferEth(assets);
    }

Tool used

Manual Review

Recommendation

beforeDeposit() should be called in depositEth() and redeemEth():

Solidity
    /**
        @notice Wraps Eth sent by the user and deposits into the LP
            Transfers shares to the user denoting the amount of Eth deposited
        @dev Emits Deposit(caller, owner, assets, shares)
    */
    function depositEth() external payable {
        uint assets = msg.value;
        uint shares = previewDeposit(assets);
        require(shares != 0, "ZERO_SHARES");
        beforeDeposit(assets, shares);
        IWETH(address(asset)).deposit{value: assets}();
        _mint(msg.sender, shares);
        emit Deposit(msg.sender, msg.sender, assets, shares);
    }

    /**
        @notice Unwraps Eth and transfers it to the caller
            Amount of Eth transferred will be the total underlying assets that
            are represented by the shares
        @dev Emits Withdraw(caller, receiver, owner, assets, shares);
        @param shares Amount of shares to redeem
    */
    function redeemEth(uint shares) external {
        uint assets = previewRedeem(shares);
        beforeWithdraw(assets, shares);
        _burn(msg.sender, shares);
        emit Withdraw(msg.sender, msg.sender, msg.sender, assets, shares);
        IWETH(address(asset)).withdraw(assets);
        msg.sender.safeTransferEth(assets);
    }

Sentiment Team

Fixed as recommended. PR here.

Lead Senior Watson

Confirmed fix.

The original report as published on Sherlock, by Lambda. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the depositEth and redeemEth functions did not call updateState (or beforeDeposit/beforeWithdraw) before changing the asset balance, so pending interest was calculated using a stale utilisation ratio.

Impact

Borrowers could pay incorrect interest amounts, leading to overpayment by borrowers or loss of yields for lenders.

What the attacker needed

A lending pool with ETH deposits and borrows; No interactions for a period of time; A user calls depositEth or redeemEth

The fix

The issue was fixed by calling beforeDeposit and beforeWithdraw in depositEth and redeemEth respectively, as recommended.

How it was found

  1. Alice deposits 1000 ETH via depositEth.
  2. Bob borrows 500 ETH, which calls updateState and sets utilisation to 0.5.
  3. One year passes with no interactions.
  4. Alice redeems 500 ETH via redeemEth, which does not call updateState, changing utilisation to 1.
  5. Bob repays the loan, which calls updateState and calculates interest using the new utilisation, resulting in overpayment.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

In a lending system, interest builds up over time based on how much of the pool is borrowed. If someone adds or removes funds without first updating the interest calculation, the rate used for past time can be wrong. Here, two functions that add or remove ETH skipped that update, so borrowers could be charged too much or too little interest.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by Lambda

View all

64 more disclosed reports, best first.

More from Sentiment Update #3

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.