20,000 USDC
View results
Submission Details
Severity: medium

Potential Underflow in Withdraw Function

Summary

The withdraw function in the provided Solidity smart contract is susceptible to a potential underflow issue. If not properly validated, this could lead to incorrect balance calculations and allow users to withdraw more tokens than they originally staked.

Vulnerability Details

In the withdraw function, the contract directly decreases the user's balance (balances[msg.sender]) by the specified withdrawal amount (_amount) without verifying whether the user has sufficient balance to withdraw. Here's the vulnerable code snippet:

function withdraw(uint _amount) external {
updateFor(msg.sender);
balances[msg.sender] -= _amount;
require(TKN.transfer(msg.sender, _amount), "Transfer failed");
}

If a user tries to withdraw an amount greater than their staked balance, the subtraction operation balances[msg.sender] -= _amount will result in an underflow, causing the balance to wrap around to a very large value. This could potentially make the user's balance appear to be much larger than their actual staked amount.

Impact

The underflow vulnerability in the withdraw function could lead to users withdrawing more tokens than they initially staked or claiming an excessive amount of rewards. This could cause a loss of funds and disrupt the intended behavior of the staking mechanism.

Tools Used

Manual

Recommendations

To mitigate the potential underflow issue, a check should be added before the subtraction operation to ensure that the user's balance is sufficient for the withdrawal. Here's the updated withdraw function with the mitigation:

function withdraw(uint _amount) external {
updateFor(msg.sender);
require(balances[msg.sender] >= _amount, "Insufficient balance");
balances[msg.sender] -= _amount;
require(TKN.transfer(msg.sender, _amount), "Transfer failed");
}

By including the require statement to check the user's balance against the withdrawal amount, the contract ensures that the subtraction operation will not cause an underflow. If the user's balance is insufficient, the function will revert, preventing any potential issues and safeguarding the staking contract against unintended behavior.

Support

FAQs

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