Santa's List

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

Bug in buyPresent() allows a malicious actor to steal(from any address wih SantaToken) and burns the victim's tokens and mints NFT to caller

Root + Impact

Allows for direct theft where no privileges required, no approvals required for attacker to profits from victim's loses assets.

Description

The buyPresent() function NatSpec specifies as follows `https://github.com/CodeHawks-Contests/ai-santas-list/blob/f8b243940c9191981c11a94083173462d64412bb/src/SantasList.sol#L169`.
The issue stems from the fact that the user input prarameter meant to be the presentReceiver is where deductions are made for the purchase of the present `https://github.com/CodeHawks-Contests/ai-santas-list/blob/f8b243940c9191981c11a94083173462d64412bb/src/SantasList.sol#L173 `, instead from the actually caller as specified by NatSpec. Furthermore, with the function calling `https://github.com/CodeHawks-Contests/ai-santas-list/blob/f8b243940c9191981c11a94083173462d64412bb/src/SantasList.sol#L174`, the nft is minted to the caller address not the presentReceiver as intended. A malicious actor would simply use any address with SantaTokens to mint themselves NFTs, exploiting this as much as they want.
https://github.com/CodeHawks-Contests/ai-santas-list/blob/f8b243940c9191981c11a94083173462d64412bb/src/SantasList.sol#L180`which uses _safemint() `https://github.com/CodeHawks-Contests/ai-santas-list/blob/f8b243940c9191981c11a94083173462d64412bb/src/SantasList.sol#L181

Risk

This basically makes legit owner of SantaToken to lose their tokens due to malicious actor taking advantage of the bug.

Likelihood:

it is high as all it takes is finding addresses with SantaTokens.

Impact:

SantaToken Can Be Burned Without Consent.
Anyone Can Force Token Burns On Rich Users whilst the attacker doesn't even need to care about NFTs.
The attacker basically uses other users token to prit nft as well.

Proof of Concept

The following test show how a malicious actor could in fact use stolen SantaToken to mint themselves nfts.
```solidity
SantasList.Status goodguy = SantasList.Status.EXTRA_NICE;
vm.startPrank(santaOwner);
santaList.checkList(address(User1), goodguy);
santaList.checkTwice(address(User1), goodguy);
vm.stopPrank();
vm.startPrank(santaOwner);
santaList.checkList(address(User2), goodguy);
santaList.checkTwice(address(User2), goodguy);
vm.stopPrank();
vm.warp(1703480381 + 30 days);
//user2 collects their presents
vm.startPrank(address(User1));
santaList.collectPresent();
vm.stopPrank();
//user2 collects their presents
vm.startPrank(address(User2));
santaList.collectPresent();
vm.stopPrank();
//new balance of nfts malicious actor has before thefy
uint256 beforeSteal = santaList.balanceOf(address(maliciousActor));
//malicious actor calls buyPresent with their addresses to mint nfts to themselves
vm.startPrank(maliciousActor);
santaList.buyPresent(User1);
santaList.buyPresent(User2);
vm.stopPrank();
//new balance of nfts malicious actor has after thefy
uint256 stolen = santaList.balanceOf(address(maliciousActor));
assertGt(stolen, beforeSteal);
├─ [975] SantasList::balanceOf(maliciousActor: [0x195Ef46F233F37FF15b37c022c293753Dc04A8C3]) [staticcall]
│ └─ ← [Return] 2
└─ ← [Stop]
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 24.53ms (2.26ms CPU time)

Recommended Mitigation

In the buyPresent() function SantaToken ha to be taken from the caller as specified in NatSpec, and use _safeMint() directly in the function.
i_santaToken.burn(presentReceiver); - remove this code
_mintAndIncrement(); - remove this code
i_santaToken.burn(msg.sender); + add this code
_safeMint(presentReceiver, s_tokenCounter++); + add this code
Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge about 4 hours 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!