Skip to content
Business logic Audit finding Intermediate

ERC5095.mint function calculates slippage incorrectly

The ERC5095.mint function calculates slippage based on the asset amount rather than the shares amount, allowing users to receive fewer shares than expected. An attacker could exploit this to cause users to lose up to 2% of their funds.

pashov Follow with cccz, bin2chen and 4 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2022-10-illuminate-judging/issues/31

Found by

pashov, cccz, bin2chen, hansfriese, kenzo, hyh, rvierdiiev

Summary

ERC5095.mint function calculates slippage incorrectly. This leads to lost of funds for user.

Vulnerability Detail

ERC5095.mint function should take amount of shares that user wants to receive and then buy this amount. It uses hardcoded 1% slippage when trades base tokens for principal. But it takes 1% of calculated assets amount, not shares.

Solidity
    function mint(address r, uint256 s) external override returns (uint256) {
        if (block.timestamp > maturity) {
            revert Exception(
                21,
                block.timestamp,
                maturity,
                address(0),
                address(0)
            );
        }
        uint128 assets = Cast.u128(previewMint(s));
        Safe.transferFrom(
            IERC20(underlying),
            msg.sender,
            address(this),
            assets
        );
        // consider the hardcoded slippage limit, 4626 compliance requires no minimum param.
        uint128 returned = IMarketPlace(marketplace).sellUnderlying(
            underlying,
            maturity,
            assets,
            assets - (assets / 100)
        );
        _transfer(address(this), r, returned);
        return returned;
    }

This is how slippage is provided

Solidity
uint128 returned = IMarketPlace(marketplace).sellUnderlying(
            underlying,
            maturity,
            assets,
            assets - (assets / 100)
        );

But the problem is that assets it is amount of base tokens that user should pay for the shares he want to receive. Slippage should be calculated using shares amount user expect to get.

Example. User calls mint and provides amount 1000. That means that he wants to get 1000 principal tokens. While converting to assets, assets = 990. That means that user should pay 990 base tokens to get 1000 principal tokens. Then the sellUnderlying is send and slippage provided is 990*0.99=980.1. So when something happens with price it's possible that user will receive 980.1 principal tokens instead of 1000 which is 2% lost.

To fix this you should provide s - (s / 100) as slippage.

Impact

Lost of users funds.

Code Snippet

Provided above

Tool used

Manual Review

Recommendation

Use this.

Solidity
uint128 returned = IMarketPlace(marketplace).sellUnderlying(
            underlying,
            maturity,
            assets,
            s- (s / 100)
        );

The original report as published on Sherlock, by pashov. 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

The slippage limit is calculated from the asset amount (assets) instead of the shares amount (s), so the minimum returned shares is set too low.

Impact

Users could lose up to 2% of their funds due to incorrect slippage calculation.

What the attacker needed

User calls mint function; Price changes occur

The fix

The slippage should be calculated using the shares amount: s - (s / 100).

How it was found

  1. User calls mint with a desired share amount.
  2. The contract calculates the required asset amount.
  3. The contract calls sellUnderlying with a minimum output based on the asset amount minus 1%.
  4. Due to price changes, the user may receive fewer shares than expected, up to 2% less.

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.

When you mint shares, you specify how many shares you want. The contract calculates how many base tokens you need to pay and then buys the shares. The slippage protection (a limit on how much worse the trade can get) is set using the token amount instead of the share amount, so you might get fewer shares than you asked for. This means you could lose money.

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 pashov

View all

7 more disclosed reports, best first.

More from Illuminate Round 2

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.