Skip to content
Business logic Audit finding Intermediate

No slippage protection for Market functions

The Market contract's buy and sell functions lack slippage protection, so the price can change between when a user submits a transaction and when it executes. An attacker can sandwich the user's transaction to profit at the user's expense.

rvierdiiev Follow with 0xMango, d3e4 and 48 others

Originally disclosed on Code4rena

Submitted by rvierdiiev, also found by 0xMango, d3e4, turvy_fuzz, Udsen, pontifex, jnforja, Madalad, peritoflores, inzinko, deepkin, DarkTower, peanuts, pep7siup, 0xarno, vangrim, aslanbek, mojito_auditor (1, 2), mahyar, nmirchev8, Yanchuan, PENGUN, 0x175, KupiaSec, jasonxiale, Pheonix, rice_cooker, osmanozdemir1, neocrao, twcctop, T1MOH, ast3ros, Kose, bart1e, tnquanghuy0512, openwide, t0x1c (1, 2, 3), zhaojie, Tricko, 0x3b, Bauchibred, chaduke (1, 2), HChang26, RaoulSchaffranek, max10afternoon, ustas, developerjordy, Giorgio, bin2chen, SpicyMeatball, 0xpiken, glcanvas, ElCid, rouhsamad, and zhaojohnson

Proof of Concept

Price for shares inside Market is calculated using bonding curve. Currently, LinearBondingCurve is supported. This bonging curve increases each next shares with fixed amount and also uses 10% / log2(shareIndex) to calculate fee for the share.

In order to calculate price to buy shares getBuyPrice is used and to calculate price to sell shares getSellPrice is used.

https://github.com/code-423n4/2023-11-canto/blob/main/1155tech-contracts/src/Market.sol#L132-L145

    function getBuyPrice(uint256 _id, uint256 _amount) public view returns (uint256 price, uint256 fee) {
        // If id does not exist, this will return address(0), causing a revert in the next line
        address bondingCurve = shareData[_id].bondingCurve;
        (price, fee) = IBondingCurve(bondingCurve).getPriceAndFee(shareData[_id].tokenCount + 1, _amount);
    }

    function getSellPrice(uint256 _id, uint256 _amount) public view returns (uint256 price, uint256 fee) {
        // If id does not exist, this will return address(0), causing a revert in the next line
        address bondingCurve = shareData[_id].bondingCurve;
        (price, fee) = IBondingCurve(bondingCurve).getPriceAndFee(shareData[_id].tokenCount - _amount + 1, _amount);
    }

Both functions use IBondingCurve(bondingCurve).getPriceAndFee, but getBuyPrice provides current token index as start index for curve, while getSellPrice provides current token index - amount to sell as start index for curve.

In the case when someone wants to buy shares, the price depends on current circulation supply. If this supply is increased right after a user’s buy tx, then they will pay more for the shares. If someone sells shares right before the buy tx, then a user will pay a lesser amount (which is good of course).

The same we can say about sell function. If someone sells shares right before a user’s sell execution, then the user receives a smaller amount. If someone buys shares, then the user gets a bigger amount.

As the price at the moment when user initiates buy or sell function can be changed before execution (with frontrunning or just innocent), this means that slippage protection should be introduced, so a user can be sure that they will not pay more than expected or receive less than expected.

When a user expects to buy shares for 5 USD, then the attacker can sandwich a user’s tx shares in order to make profit.

  • Attacker first frontruns and buys shares that cost 5 USD.
  • Then, let a user’s buy tx to be executed so a user buys shares more expensive (10 USD).
  • Then the attacker backruns with sell shares that cost now 10 USD and earn on this (5 USD).

The same thing can be done with sell sandwiching. When a user expects to sell their share for 20 USD:

  • Attacker can sell own share (and get 20 USD).
  • Then, let a user sell their share cheaper (and get 15 USD).
  • Then the attacker buys share cheaper (for 10 USD) and earn on this (10 USD).

While buy sandwiching needs a user to give the full approval to Market as this price is then sent from user, sell sandwiching doesn’t need that because it only sends tokens to the victim.

Impact

User can lose funds.

Tools Used

VsCode

Recommended Mitigation Steps

Make a user provide slippage to buy, sell, burnNFT and mintNFT functions.

Assessed type

Error

OpenCoreCH (Canto) confirmed

0xTheC0der (judge) decreased severity to Medium and commented:

On the one hand, according to our guidelines:

Unless there is something uniquely novel created by combining vectors, most submissions regarding vulnerabilities that are inherent to a particular system or the Ethereum network as a whole should be considered QA. Examples of such vulnerabilities include front running, sandwich attacks, and MEV.

But on the other hand, a slippage percentage parameter or expected amount has become state-of-the-art which also makes this a shortcoming of the protocol.

Consequently, it seems appropriate to move forward with Medium severity.

OpenCoreCH (Canto) commented:

Introduced params.


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

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 buy and sell functions do not accept a slippage parameter or expected price, so users cannot limit how much the execution price differs from the price they saw.

Impact

A user could lose funds due to price changes before their transaction executes, though the judge noted that sandwich attacks are generally considered QA unless slippage protection is a protocol shortcoming.

What the attacker needed

User submits a buy or sell transaction; Attacker can frontrun and backrun

The fix

The protocol introduced parameters to allow users to specify slippage protection.

How it was found

  1. Attacker frontruns and buys shares that cost 5 USD.
  2. The user's buy transaction executes, buying shares at a higher price (10 USD).
  3. The attacker backruns by selling the shares at the now higher price and earns a profit.

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 trade on a blockchain, the price can move between the moment you click 'buy' and the moment your transaction is processed. Without a limit on how much the price can move, someone else can trade just before and after you to make money at your expense. This is called a sandwich attack, and the fix is to let users set a maximum acceptable price change.

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 rvierdiiev

View all

72 more disclosed reports, best first.

More from Canto

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.