Skip to content
Business logic Audit finding Intermediate

ASDRouter didn’t call ASDUSDC.approve() to to grant permission for crocSwapAddress to spend their ASDUSDC

The ASDRouter contract failed to approve the crocSwapAddress to spend ASDUSDC tokens, so the swap step in lzCompose always fails. As a result, the router cannot complete the intended deposit and swap flow, and ASDUSDC is refunded to the receiver instead of being converted to NOTE and deposited for ASD.

0xpiken Follow with SpicyMeatball

Originally disclosed on Code4rena

Submitted by 0xpiken, also found by SpicyMeatball

The function ‘ASDRouter#lzCompose()’ never succeeds. The receiver can only receive ASDUSDC token when the call of ‘ASDRouter#lzCompose()’ is done.

Proof of Concept

‘ASDRouter#lzCompose()’ is used to deposit wrapped USDC token for ASD token:

  1. Once wrapped USDC is received, it will be deposited for ASDUSDC by calling ASDUSDC.deposit().
  2. Next ASDUSDC will swapped to NOTE by calling _swapOFTForNote().
  3. In the last, all NOTE will be deposited into ASD vault for ASD token by calling _depositNoteToASDVault().

However, in the second step, ASDUSDC.approve() was not invoked to authorize crocSwapAddress to spend their ASDUSDC. Consequently, crocSwapAddress#swap() cannot successfully swap ASDUSDC for NOTE. _swapOFTForNote() will fail and all ASDUSDC will be refunded to the designated receiver.

To demonstrate the aforementioned issue, the POC is divided into two parts:

  1. The allowance that crocSwapAddress is allowed to spend from ASDRouter is 0. Modify asdRouter.sol as below:
...
import {ASDUSDC} from "./asdUSDC.sol";
+import {console} from "hardhat/console.sol";
...
    function _swapOFTForNote(address _oftAddress, uint _amount, uint _minAmountNote) internal returns (uint, bool) {
         ...
+        console.log("balance:", ASDUSDC(asdUSDC).balanceOf(address(this)));
+        console.log("allowance:", ASDUSDC(asdUSDC).allowance(address(this), crocSwapAddress));
        // swap is good to make, use call just in case revert occurs
        (bool successSwap, bytes memory data) = crocSwapAddress.call(abi.encodeWithSelector(ICrocSwapDex.swap.selector, baseToken, quoteToken, ambientPoolIdx, !isNoteBase, !isNoteBase, amountConverted, 0, 0, uintMinAmount, 0));
...

Run npx hardhat test. From the log we can see that the allowance is 0, while the ASDUSDC balance of ASDRouter is not:

      ASDRouter
        ✔ lzCompose: invalid payload
        ✔ lzCompose: invalid payload with gas
        ✔ lzCompose: not whitelisted
        ✔ lzCompose: not whitelisted with gas
        ✔ lzCompose: bad swap
        ✔ lzCompose: bad swap with gas
    balance: 100000000000000000000
    allowance: 0
        ✔ lzCompose: insufficient send fee
    balance: 100000000000000000000
    allowance: 0
        ✔ lzCompose: insufficient send fee with gas
    balance: 100000000000000000000
    allowance: 0
        ✔ lzCompose: successful deposit and send on canto

      ASDUSDC
        ✔ should set whitelist for correct contract addresses
        ✔ should deposit whitelisted usdc versions for asdUSDC
        ✔ should withdraw whitelisted usdc versions for asdUSDC
        ✔ should recover USDC tokens sent to the contract
  1. crocSwapAddress#swap() wouldn’t work if the caller didn’t approve crocSwapAddress to spend their token. Add fork configuration into hardhat.config.js:
...
    networks: {
+       hardhat: {
+           forking: {
+             url: "https://canto-rpc.ansybl.io/",
+             blockNumber: 8977374,
+           },
+           chainId: 7700,
+       },
...

Create test/DEX.js:

const { expect } = require("chai");
const { ethers } = require("hardhat");
const hre = require("hardhat");

describe("Dex", function () {
    const dexAddress = "0x9290C893ce949FE13EF3355660d07dE0FB793618";
    const usdcAddress = "0x80b5a32E4F032B2a058b4F29EC95EEfEEB87aDcd";
    const cNoteAddress = "0xEe602429Ef7eCe0a13e4FfE8dBC16e101049504C";
    const usdcWhaleAddress = "0xfAc5EBD2b1b830806189FCcD0255DC9B174decbc";
    let dex;
    let usdc;
    let cNote;
    this.beforeEach(async () => {
        dex  = await hre.ethers.getContractAt("ICrocSwapDex", dexAddress);
        usdc = await hre.ethers.getContractAt("IERC20", usdcAddress);
        cNote = await hre.ethers.getContractAt("CErc20Interface", cNoteAddress);
    });

    it("User can only call DEX.swap() for cNote after call USDC.approve(dex)", async () => {
        await hre.network.provider.request({
            method: "hardhat_impersonateAccount",
            params: [usdcWhaleAddress],
        });
        whale = await ethers.getSigner(usdcWhaleAddress);
        let usdcBalanceBeforeSwap = await usdc.balanceOf(usdcWhaleAddress);
        await usdc.connect(whale).approve(dexAddress, 0);
        expect(await usdc.allowance(usdcWhaleAddress,dexAddress)).to.be.equal(0);
        //@audit-info swap failed due to zero allowance
        expect(dex.connect(whale).swap(
            usdcAddress,
            cNoteAddress,
            36000,
            true,
            true,
            2000000,
            0,
            0,
            0,
            0
        )).to.be.reverted;
        //@audit-info user set the allowance for dex to 2000000
        await usdc.connect(whale).approve(dexAddress, 2000000);
        expect(await usdc.allowance(usdcWhaleAddress,dexAddress)).to.be.equal(2000000);
        swapTx = await dex.connect(whale).swap(
            usdcAddress,
            cNoteAddress,
            36000,
            true,
            true,
            2000000,
            0,
            ethers.parseEther("10000000000"),
            0,
            0
        );
        await swapTx.wait();
        let usdcBalanceAfterSwap = await usdc.balanceOf(usdcWhaleAddress);
        let usedUSDC = usdcBalanceBeforeSwap - usdcBalanceAfterSwap;
        //@audit-info swap succeeded and 2000000 of USDC was tranferred from user
        expect(usedUSDC).to.be.equal(2000000);
    });
});

Open one terminal to run npx hardhat node, then open another terminal to run npx hardhat test. It’s obvious that dex.swap() will fail if usdc.approve() is not called first.

Recommended Mitigation Steps

Set allowance to the maximum value from the beginning:

    constructor(address _noteAddress, uint32 _cantoLzEID, address _crocSwapAddress, address _crocImpactAddress, address _asdUSDCAddress) {
        noteAddress = _noteAddress;
        cantoLzEID = _cantoLzEID;
        crocSwapAddress = _crocSwapAddress;
        crocImpactAddress = _crocImpactAddress;
        asdUSDC = _asdUSDCAddress;
+       ASDUSDC(asdUSDC).approve(crocSwapAddress, type(uint).max);
    }

Assessed type

Context

dsudit01 (Canto) confirmed and commented:

PR here.

SpicyMeatball (warden) commented:

@3docSec - the sole purpose of the router contract is to exchange USDC to asD and send it to the source chain. The bug described in this report disrupts this functionality making the router unusable. Therefore, I believe a high severity is more appropriate.

3docSec (judge) commented:

A quick refresher on our severity categorization:

2 — Med: Assets not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.

3 — High: Assets can be stolen/lost/compromised directly (or indirectly if there is a valid attack path that does not have hand-wavy hypotheticals).

Should help clarify how this finding is a well-deserved Medium.


The original report as published on Code4rena, by 0xpiken. 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 ASDRouter contract did not call ASDUSDC.approve() to allow crocSwapAddress to spend its ASDUSDC before attempting the swap.

Impact

The router's core function is broken: users cannot convert their USDC to ASD via this path, and the transaction reverts, refunding ASDUSDC instead of completing the intended deposit.

What the attacker needed

ASDRouter deployed; User sends wrapped USDC to trigger lzCompose

The fix

The router should approve crocSwapAddress to spend ASDUSDC, for example by setting a maximum allowance in the constructor.

How it was found

  1. Receive wrapped USDC and deposit it for ASDUSDC by calling ASDUSDC.deposit().
  2. Attempt to swap ASDUSDC to NOTE by calling _swapOFTForNote().
  3. The swap fails because crocSwapAddress has zero allowance to spend ASDUSDC.
  4. ASDUSDC is refunded to the designated receiver instead of being deposited into the ASD vault.

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 one contract wants another contract to move its tokens, it must first give permission by calling approve. Here the router forgot to give that permission, so the swap always failed and the tokens were sent back instead of being used.

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 0xpiken

View all

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