VouchFaucet can be immediately drained by anyone
The claimTokens function in the VouchFaucet contract does not update the claimedTokens mapping, so the maxClaimable limit is never enforced. Any address can claim an arbitrary amount of any token, draining the contract's entire balance.
Source: https://github.com/sherlock-audit/2024-06-union-finance-update-2-judging/issues/33
The protocol has acknowledged this issue.
Found by
0xAadi, Bigsam, Bugvorus, Varun_05, cryptphi, korok, snapishere, trachev
Summary
The claimTokens function in the VouchFaucet contract fails to properly enforce the maxClaimable limit because it does not update the value in the claimedTokens mapping. This allows any address to claim an arbitrary amount of any token, potentially draining the entire token balance of the contract, in a single transaction or through multiple transactions.
Vulnerability Detail
Included below is the relevant code from the VouchFaucet followed by key insights:
/// @notice Token address to msg sender to claimed amount
mapping(address => mapping(address => uint256)) public claimedTokens;
/// @notice Token address to max claimable amount
mapping(address => uint256) public maxClaimable;
/// @notice Claim tokens from this contract
function claimTokens(address token, uint256 amount) external {
require(claimedTokens[token][msg.sender] <= maxClaimable[token], "amount>max");
IERC20(token).transfer(msg.sender, amount);
emit TokensClaimed(msg.sender, token, amount);
}
/// @notice Transfer ERC20 tokens
function transferERC20(address token, address to, uint256 amount) external onlyOwner {
IERC20(token).transfer(to, amount);
}
-
The claimedTokens mapping is never updated. It will always return 0 when looking up how much of any token has been claimed by any address.
-
The maxClaimable mapping will by default return 0 for any token the contract could ever receive. The contract owner can use the setMaxClaimable function but is only able to set the claimable amount for any token to 0 or greater.
-
The transferERC20 function is protected by the onlyOwner modifier signaling the desire to restrict access to this type of transfer.
-
Currently no matter what the admin does the require statement in claimTokens will always pass because it evaluates an expression that will always effectively be:
require(0 <= [uint256], "amount>max");This makes claimTokens effectively equivalent to an unrestricted version of transferERC20.
Proof of Concept
The proof of concept below imports and utilizes the protcols own TestWrapper for simplicity in setting up a realistic testing environment.
The test case demonstrates that despite the VouchFaucet containing mechanisms that clearly intend to disallow the faucet from being easily drained by a single address, such an outcome is possible with no effort.
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.16;
import {Test, console} from "forge-std/Test.sol";
import {TestWrapper} from "../TestWrapper.sol";
import {VouchFaucet} from "../../src/contracts/peripheral/VouchFaucet.sol";
import {IERC20} from "@openzeppelin/token/ERC20/IERC20.sol";
contract TestVouchFaucet is TestWrapper {
VouchFaucet public vouchFaucet;
uint256 public TRUST_AMOUNT = 10 * UNIT;
function setUp() public {
deployMocks();
vouchFaucet = new VouchFaucet(address(userManagerMock), TRUST_AMOUNT);
}
function testDrainVouchFaucet() public {
address bob = address(1234);
erc20Mock.mint(address(vouchFaucet), 3 * UNIT);
vouchFaucet.setMaxClaimable(address(erc20Mock), 1 * UNIT);
assertEq(vouchFaucet.maxClaimable(address(erc20Mock)), 1 * UNIT);
// Bob can claim any number of tokens despite maxClaimable set to 1 Unit
vm.prank(bob);
vouchFaucet.claimTokens(address(erc20Mock), 3 * UNIT);
assertEq(IERC20(erc20Mock).balanceOf(bob), 3 * UNIT);
}
}
Impact
Without the intended enforcement provided by the require statement the claimTokens function provides unrestricted external access to an ERC20 transfer function. This is clearly not intended as demonstrated by the presence of the onlyOwner on the similar transferERC20 function. The result is that any caller can immediately transfer out any amount of any token.
This oversight completely undermines the token distribution model of the faucet. If deployed without modification it would render the contract useless for the intended purpose due to its inability to securely hold any amount of any token.
Code Snippet
Tool used
Manual Review
Recommendation
The following correction ensures that any individual address can't claim more than the set claimable amount, maintaining the intended token distribution model of the faucet.
File: VouchFaucet.sol
function claimTokens(address token, uint256 amount) external {
- require(claimedTokens[token][msg.sender] <= maxClaimable[token], "amount>max");
+ uint256 newTotal = claimedTokens[token][msg.sender] + amount;
+ require(newTotal <= maxClaimable[token], "Exceeds max claimable amount");
+ claimedTokens[token][msg.sender] = newTotal;
IERC20(token).transfer(msg.sender, amount);
emit TokensClaimed(msg.sender, token, amount);
}
The following additional recommendations should be considered. The suggestions won't impact legitimate users, but raise the effort required for an malicious actor to disrupt the intended functioning of the contract.
-
Ensure the claimedTokens mapping is updated before the transfer to avoid reentrancy risk, OpenZeppelin ReentrancyGuard could also be considered.
-
Consider adding an admin adjustable global cap on total tokens that can be claimed across all addresses to help control faucet outflows.
-
Consider implementing a time-based cooldown mechanism to limit the frequency of claims per address.
Discussion
c-plus-plus-equals-c-plus-one
This highlights the exactly same problem as https://github.com/sherlock-audit/2024-06-union-finance-update-2-judging/issues/96 does. So these two issues should probably be merged as one single, duplicates of each other.
c-plus-plus-equals-c-plus-one
@WangSecurity is this one escalated too just like #96 is?
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
Impact
Any caller can immediately transfer out any amount of any token, rendering the contract useless for its intended purpose.
What the attacker needed
None
The fix
The claimTokens function should update the claimedTokens mapping before transferring tokens, and the require statement should check the new total against maxClaimable.
How it was found
- Deploy the VouchFaucet contract and mint tokens to it.
- Set a maxClaimable amount for a token.
- Call claimTokens with an amount larger than maxClaimable.
- Observe that the full amount is transferred despite the limit.
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.
A faucet is a contract that gives out free tokens, usually with a limit per user. Here, the limit check was broken because the contract never recorded how much each user had already claimed, so anyone could take everything.
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.