code-423n4 / 2023-05-maia-findings

20 stars 12 forks source link

The code uses arithmetic operations without explicitly checking for possible overflows or underflows #830

Closed code423n4 closed 1 year ago

code423n4 commented 1 year ago

Lines of code

https://github.com/code-423n4/2023-05-maia/blob/cfed0dfa3bebdac0993b1b42239b4944eb0b196c/src/erc-20/ERC20MultiVotes.sol#L43

Vulnerability details

Impact

The impact of the Integer Overflow/Underflow vulnerability can be summarized as follows:

Proof of Concept

The potential Integer Overflow/Underflow vulnerability can be found in the following line of code: LINK

return balanceOf[account] - userDelegatedVotes[account];

In this line, the subtraction operation (-) is performed between balanceOf[account] and userDelegatedVotes[account]. If balanceOf[account] is smaller than userDelegatedVotes[account], an underflow can occur, resulting in unexpected behavior and potential vulnerabilities.

Tools Used

Manual Review

Recommended Mitigation Steps

To fix the Integer Overflow/Underflow vulnerability in the code, we can add a check to ensure that balanceOf[account] is greater than or equal to userDelegatedVotes[account] before performing the subtraction operation. Here's an example of how we can modify the code to address this issue:

function freeVotes(address account) public view virtual returns (uint256) {
    uint256 accountBalance = balanceOf[account];
    uint256 delegatedVotes = userDelegatedVotes[account];

        if (accountBalance < delegatedVotes) {
            // Handle the error condition (e.g., revert, return a default value, etc.)
            revert("Insufficient account balance");
        }

        return accountBalance - delegatedVotes;
    }

By adding this check, the code ensures that the freeVotes function will only return a value if accountBalance is greater than or equal to delegatedVotes. If accountBalance is less than delegatedVotes, it will revert the transaction with an appropriate error message.

Assessed type

Under/Overflow

c4-judge commented 1 year ago

trust1995 marked the issue as unsatisfactory: Invalid