Skip to content
Smart contracts Audit finding Intermediate

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.

Lambda Follow with Tomo, hickuphh3 and 3 others

Originally disclosed on Sherlock

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:

Solidity
_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.

The original report as published on Sherlock, by Lambda. 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 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.

More by Lambda

View all

64 more disclosed reports, best first.

More from Harpie

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.