Skip to content
Business logic Audit finding Intermediate

Max allocations can be bypassed with multiple addresses because of guaranteed allocations

The TokenSale contract's calculateMaxAllocation function returns a default maxAllocation for users with zero tier allocation, allowing anyone to deposit up to that amount without staking. An attacker could use multiple addresses to claim all tokens without staking, bypassing the intended restriction that only ION holders can participate.

GatewayGuardians Follow with Silvermist, ZdravkoHr. and 1 other

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-03-zap-protocol-judging/issues/152

Found by

GatewayGuardians, Silvermist, ZdravkoHr., s1ce

Summary

TokenSale._processPrivate() ensures that a user cannot deposit more than their allocation amount. However, each address can deposit up to at least maxAllocations. This can be leveraged by a malicious user by using different addresses to claim all tokens without even staking.

Vulnerability Detail

The idea of the protocol is to give everyone the right to have at least maxAlocations allocations. By completing missions, users level up and unlock new tiers. This process will be increasing their allocations. The problem is that when a user has no allocations, they have still a granted amount of maxAllocations.

TokenSale.calculateMaxAllocation returns $max(maxTierAlloc(), maxAllocation)$

For a user with no allocations, _maxTierAlloc() will return 0. The final result will be that this user have maxAllocation allocations (because maxAllocation > 0).

Solidity
        if (userTier == 0 && giftedTierAllc == 0) {
            return 0;
        }

Multiple Ethereum accounts can be used by the same party to take control over the IDO and all its allocations, on top of that without even staking.

NOTE: setting maxAllocation = 0 is not a solution in this case because the protocol wants to still give some allocations to their users.

Impact

Buying all allocations without staking. This also violates a key property that only ION holders can deposit.

Code Snippet

Solidity
    function calculateMaxAllocation(address _sender) public returns (uint256) {
        uint256 userMaxAllc = _maxTierAllc(_sender);

        if (userMaxAllc > maxAllocation) {
            return userMaxAllc;
        } else {
            return maxAllocation;
        }
    }

Tool used

Manual Review

Recommendation

A possible solution may be to modify calculateMaxAllocation in the following way:

Diff
    function calculateMaxAllocation(address _sender) public returns (uint256) {
        uint256 userMaxAllc = _maxTierAllc(_sender);
+       if (userMaxAllc == 0) return 0;

         if (userMaxAllc > maxAllocation) {
            return userMaxAllc;
        } else {
            return maxAllocation;
        }
    }

Discussion

ZdravkoHr

Escalate

@Hash01011122, why was this issue excluded? It shows how users can bypass a core restriction and on top of that - doing it without staking.

sherlock-admin2

Escalate

@Hash01011122, why was this issue excluded? It shows how users can bypass a core restriction and on top of that - doing it without staking.

You've created a valid escalation!

To remove the escalation from consideration: Delete your comment.

You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.

Hash01011122

The function is operating as designed. If you believe the issue warrants further investigation, please submit a Proof of Concept (PoC).

sherlock-admin4

The protocol team fixed this issue in the following PRs/commits: https://github.com/Lithium-Ventures/zap-contracts-labs/pull/1

ZdravkoHr

@Hash01011122 basically everyone, regardless of the staked amount, can deposit up to maxAllocations

Hash01011122

If you don't provide a valid PoC for this issue within 48 hours, I will consider this as invalid

ZdravkoHr

@Hash01011122, here is my PoC. You have to do two things before running it:

  1. Add this MockStaking.sol file in the same folder as the PoC.
Solidity
contract MockStaking {
    function getUserState(address) public returns(uint256, uint256, uint256, uint256) {
        return (0, 0, 0, 0);
    }
}
  1. Add this function to TokenSale.sol.
Solidity
    function setUsdc(address _new) public {
        usdc = IERC20D(_new);
    }

The main file TokenSale.t.sol demonstrates how a user that hasn't staked in the staking contract, can use different accounts to deposit as many assets as they want to.

Solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.11;

import "forge-std/Test.sol";
import "../contracts/Admin.sol";
import "../contracts/USDC.sol";
import "../contracts/interfaces/ITokenSale.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "forge-std/console.sol";
import {TokenSale} from "../contracts/TokenSale.sol";
import {MockStaking} from "./MockStaking.sol";


contract TokenSaleTest is Test {
    using stdStorage for StdStorage;
     address public saleContract;
     Admin public admin;
     USDCWithSixDecimal public mockUSDC;
     MockStaking public stakingContract;
     TokenSale public saleInstance;

     

    function setUp() public {
        saleContract = address(new TokenSale());
        mockUSDC = new USDCWithSixDecimal("MockUSDC", "USDC");
        _config();
    }

    function _config() internal {
       stakingContract = new MockStaking();
       admin = new Admin();
       admin.initialize(address(this));
       admin.setStakingContract(address(stakingContract));
       admin.addOperator(address(this));
       admin.setMasterContract(saleContract);
       admin.setWallet(address(this));

    
       ITokenSale.Params memory params = ITokenSale.Params({
        totalSupply: 1000e18,
        privateStart: uint32(block.timestamp),
        privateTokenPrice: 20e18,
        privateEnd: uint32(block.timestamp + 1 weeks)
       });

       admin.createPoolNew(params, 15000e18, 1000, false, 0);
       
       assertTrue(admin.tokenSalesM(0x5B0091f49210e7B2A57B03dfE1AB9D08289d9294));
       saleInstance = TokenSale(payable(0x5B0091f49210e7B2A57B03dfE1AB9D08289d9294));

       // A setUsdc function is added to the TokenSale contract for test purposes
       saleInstance.setUsdc(address(mockUSDC));
    }

    function testDeposit() public {
        uint160 addressCount = 20;
        // A single user, WITHOUT EVER DEPOSITING TO THE STAKING CONTRACT, can deposit as much as they want using different addresses.
            for (uint160 i = 1; i < addressCount; i++) {
            address currentAddress = address(i);
            mockUSDC.mint(currentAddress, type(uint256).max / addressCount);

            vm.startPrank(currentAddress);
            mockUSDC.approve(address(saleInstance), type(uint256).max);
            // Just 100 because of decimals mess
            saleInstance.deposit(100);
            vm.stopPrank();

            (uint128 totalPrivateSold, ) = saleInstance.getState();
            console.log(totalPrivateSold);
        }
        
    }

}

s1ce

@Evert0x @Hash01011122

I am not sure if this issue is valid which is why I didn't escalate my own, but mine (#196) is a duplicate of this

My issue #172 that has been a marked a dup of this one is actually a dup of #158 instead

Evert0x

@Hash01011122 what's your opinion about the poc?

Hash01011122

@ZdravkoHr your PoC isn't working, @Evert0x this issue can be invalidated. Also their is a check in TokenSale.sol:_maxTierAllc whether if userTier or giftedTierAllc of user is zero then allocation will be zero. @ZdravkoHr do you want to add anything?? Correct me if I am wrong.

ZdravkoHr

The 0 will be used in calculateMaxAllocation and maxAllocations will be returned. Which part of the PoC is not working?

Hash01011122

The deposit function in your PoC isn't working, if you still feel it's valid can you please provide the exact lines of code which justifies the logic you are trying to portray with proper mitigation for it.

ZdravkoHr

Have you added the MockStaking contract and the setUsdc function?

detectiveking123

@Evert0x I believe this one and it's duplicates are actually the same issue family as #158 (they point out the same root cause issue)

You have invalidated issue family #158, but in the documentation in TokenSale.sol there are the following two lines:

1. Private round. Only ion token holders can participate in this round.

A single investor can purchase up to their maximum allowed investment defined by the tier.

Clearly, based on this documentation (which I strongly believe is incorrect and should not be used as a source of truth, but my personal opinions are besides the point), both issues should be valid.

Hash01011122

@ZdravkoHr I've added the setUsdc function and MockStaking contract, and also deleted the entire file and environment. I reinstalled everything from scratch to rerun your PoC, but the results remain unchanged.

Hash01011122

@detectiveking123 Still verifying this issue's validity, whereas for issue #158, I will take confirmation from sponsors.

detectiveking123

@Hash01011122 They are the exact same root cause though, so they should be valid and duplicates of each other. What the sponsors say shouldn't matter; I've shown you excerpts from the documentation that prove validity.

Evert0x

Agree with @detectiveking123

For that reason I will reject the escalation and invalidate the issue

detectiveking123

@Evert0x I'm saying the issue should be valid. They're all valid issues, and duplicates of each other, because of the above excerpts from the documentation.

Evert0x

The language you quoted supports the invalidation of this issue.

A single investor can purchase up to their maximum allowed investment defined by the tier.

Indicating it's a maximum allowed investment personalized to the investor.

Besides that, they have clarified it in a discord message https://github.com/sherlock-audit/2024-03-zap-protocol-judging/issues/158#issuecomment-2047444809

I will reject the escalation and invalidate the issue

ZdravkoHr

@Evert0x, this issue shows how tokens can be obtained without any prior staking. I think the root cause here is the missing if statement from my recommendation

detectiveking123

@Evert0x You misunderstand. The tier based max allocation is defined by _maxTierAllc. The current code would allow them to purchase potentially more than that if maxAllocation is higher.

Furthermore, the language here is pretty clear:

1. Private round. Only ion token holders can participate in this round.

Clearly, this code allows non-ion token holders/stakers to participate.

detectiveking123

Also, to clarify my point, I am suggesting that it is not just this issue that is valid based on the documentation, but also the entire #158 family (they have the exact same root cause). This issue is a strict subset of #158 , although still valid.

My recommendation is to duplicate the two issue families and validate all of them.

Evert0x

Thank you for correcting me @detectiveking123. I indeed misunderstood this issue.

Still looking into the outcome for this issue. But I don't believe this is a duplicate of #158.

detectiveking123

@Evert0x This is actually a strict subset of #158 .

Both issues reference the exact code snippet and lead to the exact same impact, but have different opinions about what the correct fix is. In #158 , the fix that is suggested is to replace the max with a min, while in this issue adding an extra if statement is suggested.

In fact, #158 even states that: However, swapped return values allow a user to have 0 allocations and get themaxAllocation or to exceed the maximum allocations

Clearly all Watsons have identified the core issue but have different suggestions / opinions on how to fix it

Evert0x

I believe the following issues are a family #152, #158, #161, #172.

They all describe the following issue

get maximum allocation while having 0

Planning to make #152 the main report issue as the fix recommendation is correct

Thanks @detectiveking123

detectiveking123

@Evert0x No problem. Your proposed judgement makes sense to me.

Hash01011122

Thanks for bring this to light @detectiveking123, I agree that this should be considered a valid issue with dups #019, #158, #161, #172.

Evert0x

Result: Medium Has Duplicates

sherlock-admin3

Escalations have been resolved successfully!

Escalation status:

sherlock-admin2

The Lead Senior Watson signed off on the fix.

The original report as published on Sherlock, by GatewayGuardians. 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 calculateMaxAllocation function returns maxAllocation when the user's tier allocation is zero, instead of returning zero.

Impact

An attacker could use multiple addresses to buy all allocations without staking, violating the property that only ION holders can deposit.

What the attacker needed

Multiple Ethereum addresses; USDC tokens for each address

The fix

The protocol team fixed this issue in a pull request.

How it was found

  1. Deploy the TokenSale contract and configure it with a mock staking contract and USDC token.
  2. Create multiple Ethereum addresses and mint USDC to each.
  3. For each address, approve the TokenSale contract to spend USDC and call deposit with a small amount.
  4. Observe that each address can deposit up to maxAllocation without having staked anything.

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 a token sale, each user is supposed to have a maximum amount they can buy based on how much they have staked. The code mistakenly gives everyone a minimum allowance even if they have staked nothing, so someone can create many accounts and buy up all the tokens without staking anything.

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 GatewayGuardians

View all

1 more disclosed report, best first.

More from Zap Protocol

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.