Unsafe casting of user amount from `uint256` to `uint128`
The contract stores a user's recovered token amount in a uint128 variable, but the amount is a uint256, so casting it can truncate the value. An attacker could cause the recorded amount to be less than the actual amount, leading to loss of funds.
Source: https://github.com/sherlock-audit/2022-09-harpie-judging/tree/main/018-M
Found by
Lambda, Tomo, hickuphh3, IllIllI, defsec, sirhashalot
Summary
The unsafe casting of the recovered amount from uint256 to uint128 means the users’ funds will be lost.
Vulnerability Detail
logIncomingERC20() has the recovered amount as type uint256, but amountStored is of type uint128. There is an unsafe casting when incrementing amountStored:
_erc20WithdrawalAllowances[_originalAddress][_erc20Address].amountStored += uint128(_amount);
It is thus possible for the amount recorded to be less than the actual amount recovered.
Impact
Loss of funds.
Proof of Concept
The user's balance is type(uint128).max = 2**128, but the incremented amount will be zero.
Recommendation
amountStored should be of type uint256. Alternatively, use OpenZeppelin’s SafeCast library when casting from uint256 to uint128.
Lead Senior Watson
Not sure, any tokens which would have a token supply over type(uint128).max but I guess it's best to be proactive. The proposed fix does create some issues. Instead of having less tokens transferred to the vault, the contract will revert and prevent the transfer entirely. Arguably more funds would be at risk, so you may as well use uint256 then or accept the risk and keep the slot packing.
Harpie Team
Decided to accept the risk of reverts on leastwood's comment on this issue since it's a lot of gas savings and there probably arent useful tokens w/ supply over (uint128).max. Used @openzeppelin/SafeCast. Fix here.
Lead Senior Watson
Confirmed fix.
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 contract uses a uint128 variable to store an amount that is calculated as uint256, and casts without checking for overflow.
Impact
Users could lose funds because the recorded amount may be less than the actual amount recovered.
What the attacker needed
Token supply over 2^128; user interacts with contract
The fix
The issue was fixed by using OpenZeppelin's SafeCast library, which reverts on overflow, though the team accepted the risk of reverts for very large token supplies.
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 smart contract keeps track of how many tokens each user can withdraw. It uses a smaller number type (uint128) to save space, but the actual amount can be larger. When the larger number is forced into the smaller type, it can wrap around to a tiny number, so the user's recorded balance becomes wrong and they lose funds.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.