Skip to content
Business logic Audit finding Intermediate

Users can deposit "0" ether to any round

The depositETHIntoMultipleRounds function allows a user to deposit zero ether into any round by including zero amounts alongside a normal deposit, because the zero amount passes the modulo check and does not affect the total expected value. This lets an attacker add zero-value entries to rounds, which can alter the winner selection by changing the indexes array.

0rpse Follow with 0xG0P1, 0xMAKEOUTHILL and 30 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-01-looksrare-judging/issues/18

Found by

0rpse, 0xG0P1, 0xMAKEOUTHILL, 0xrcontre360, 0xrice.cooker, Cosine, HSP, KingNFT, Krace, KupiaSec, LTDingZhen, Varun_05, asauditor, ast3ros, bughuntoor, cawfree, cocacola, dany.armstrong90, deepplus, dimulski, fibonacci, jasonxiale, mert_eren, mstpr-brainbot, nobody2018, petro1912, pontifex, s1ce, thank_you, unforgiven, vvv, zraxx, zzykxx

Summary

The main invariant to determine the winner is that the indexes must be in ascending order with no repetitions. Therefore, depositing "0" is strictly prohibited as it does not increase the index. However, there is a method by which a user can easily deposit "0" ether to any round without any extra costs than gas.

Vulnerability Detail

As stated in the summary, depositing "0" will not increment the entryIndex, leading to a potential issue with the indexes array. This, in turn, may result in an unfair winner selection due to how the upper bound is determined in the array. The relevant code snippet illustrating this behavior is found here.

Let's check the following code snippet in the depositETHIntoMultipleRounds function

Solidity
for (uint256 i; i < numberOfRounds; ++i) {
            uint256 roundId = _unsafeAdd(startingRoundId, i);
            Round storage round = rounds[roundId];
            uint256 roundValuePerEntry = round.valuePerEntry;
            if (roundValuePerEntry == 0) {
                (, , roundValuePerEntry) = _writeDataToRound({roundId: roundId, roundValue: 0});
            }

            _incrementUserDepositCount(roundId, round);

            // @review depositAmount can be "0"
            uint256 depositAmount = amounts[i];

            // @review 0 % ANY_NUMBER = 0
            if (depositAmount % roundValuePerEntry != 0) {
                revert InvalidValue();
            }
            uint256 entriesCount = _depositETH(round, roundId, roundValuePerEntry, depositAmount);
            expectedValue += depositAmount;

            entriesCounts[i] = entriesCount;
        }

        // @review will not fail as long as user deposits normally to 1 round
        // then he can deposit to any round with "0" amounts
        if (expectedValue != msg.value) {
            revert InvalidValue();
        }

as we can see in the above comments added by me starting with "review" it explains how its possible. As long as user deposits normally to 1 round then he can also deposit "0" amounts to any round because the expectedValue will be equal to msg.value.

Textual PoC: Assume Alice sends the tx with 1 ether as msg.value and "amounts" array as [1 ether, 0, 0]. first time the loop starts the 1 ether will be correctly evaluated in to the round. When the loop starts the 2nd and 3rd iterations it won't revert because the following code snippet will be "0" and adding 0 to expectedValue will not increment to expectedValue so the msg.value will be exactly same with the expectedValue.

Solidity
if (depositAmount % roundValuePerEntry != 0) {
                revert InvalidValue();
            }

Coded PoC (copy the test to Yolo.deposit.sol file and run the test):

Solidity
function test_deposit0ToRounds() external {
        vm.deal(user2, 1 ether);
        vm.deal(user3, 1 ether);

        // @dev first round starts normally
        vm.prank(user2);
        yolo.deposit{value: 1 ether}(1, _emptyDepositsCalldata());

        // @dev user3 will deposit 1 ether to the current round(1) and will deposit
        // 0,0 to round 2 and round3
        uint256[] memory amounts = new uint256[](3);
        amounts[0] = 1 ether;
        amounts[1] = 0;
        amounts[2] = 0;
        vm.prank(user3);
        yolo.depositETHIntoMultipleRounds{value: 1 ether}(amounts);

        // @dev check user3 indeed managed to deposit 0 ether to round2
        IYoloV2.Deposit[] memory deposits = _getDeposits(2);
        assertEq(deposits.length, 1);
        IYoloV2.Deposit memory deposit = deposits[0];
        assertEq(uint8(deposit.tokenType), uint8(IYoloV2.YoloV2__TokenType.ETH));
        assertEq(deposit.tokenAddress, address(0));
        assertEq(deposit.tokenId, 0);
        assertEq(deposit.tokenAmount, 0);
        assertEq(deposit.depositor, user3);
        assertFalse(deposit.withdrawn);
        assertEq(deposit.currentEntryIndex, 0);

        // @dev check user3 indeed managed to deposit 0 ether to round3
        deposits = _getDeposits(3);
        assertEq(deposits.length, 1);
        deposit = deposits[0];
        assertEq(uint8(deposit.tokenType), uint8(IYoloV2.YoloV2__TokenType.ETH));
        assertEq(deposit.tokenAddress, address(0));
        assertEq(deposit.tokenId, 0);
        assertEq(deposit.tokenAmount, 0);
        assertEq(deposit.depositor, user3);
        assertFalse(deposit.withdrawn);
        assertEq(deposit.currentEntryIndex, 0);
    }

Impact

High, since it will alter the games winner selection and it is very cheap to perform the attack.

Code Snippet

https://github.com/sherlock-audit/2024-01-looksrare/blob/7d76b96a58a6aee38f23bb38b8a5daa3bdc03f7c/contracts-yolo/contracts/YoloV2.sol#L312-L362

Tool used

Manual Review

Recommendation

Add the following check inside the depositETHIntoMultipleRounds function

Solidity
if (depositAmount == 0) {
     revert InvalidValue();
   }

Discussion

0xhiroshi

https://github.com/LooksRare/contracts-yolo/pull/176

sherlock-admin2

2 comment(s) were left on this issue during the judging contest.

takarez commented:

valid because {valid: but the impact is very low compared to the duplicated issue 002}, ?

takarez commented:

valid: high(1)

mstpr

LooksRare/contracts-yolo#176

Fix LGTM!

The original report as published on Sherlock, by 0rpse. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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

Likely the function depositETHIntoMultipleRounds does not validate that each deposit amount is greater than zero, allowing zero-value deposits that bypass the modulo check and do not affect the total expected value.

Impact

This could alter the game's winner selection and is very cheap to perform, as it only costs gas.

What the attacker needed

A small amount of ether for gas; ability to call depositETHIntoMultipleRounds

The fix

The function should be fixed by adding a check that reverts if depositAmount is zero.

How it was found

  1. Alice sends a transaction with 1 ether and an amounts array of [1 ether, 0, 0].
  2. The first iteration deposits 1 ether correctly into the first round.
  3. The second and third iterations process zero amounts, which pass the modulo check and add zero to expectedValue.
  4. The transaction succeeds because expectedValue equals msg.value, and zero-value deposits are recorded in the later rounds.

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.

In this game, players deposit ether into rounds, and each deposit should increase a counter that determines the winner. The code checks that the total deposited equals the ether sent, but it does not check that each individual deposit is greater than zero. So a player can send one normal deposit and also include zero-value deposits for other rounds, which creates entries that do not increase the counter and can skew the winner selection.

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

View all

23 more disclosed reports, best first.

More from LooksRare YOLO

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.