Skip to content
Smart contracts Audit finding Intermediate

Unsafe casting from int128 can cause wrong accounting of locked amounts

The VotingEscrow contract casts a user-supplied amount to int128 without checking bounds, so a value above the int128 maximum truncates to zero. An attacker could lock a huge amount, have the tokens transferred, but the contract records zero locked amount, leaving the tokens stuck.

CertoraInc Follow

Originally disclosed on Code4rena

Submitted by CertoraInc, also found by 0x1f8b, carlitox477, cRat1st0s, DecorativePineapple, joestakey, ladboy233, reassor, and rvierdiiev

https://github.com/code-423n4/2022-08-fiatdao/blob/fece3bdb79ccacb501099c24b60312cd0b2e4bb2/contracts/VotingEscrow.sol#L418

The unsafe casting to int128 variable can cause its value to be different from the correct value. For example in the createLock function, the addition to the locked amount variable is done by locked_.amount += int128(int256(_value)). In that case, if _value is greater than type(int128).max which is 2**127 - 1, then the accounting will be wrong and the amount that will be added to locked_.amount will be less than the amount of token that will be transferred from the user. Then the user won’t be able to withdraw the tokens that he transferred, and they’ll be stuck in the contract forever.

Proof of Concept

  1. Alice tries to lock 2**128 tokens. She calls createLock(2**128, unlockTime) with the time she wants to lock for.
  2. The addition of the given value is done by locked_.amount += int128(int256(_value)), which actually does nothing to the locked_.amount variable and it remains 0. That’s because when casting int128(int256(2**128)) truncates to 0, and that leaves the locked amount unchanged but the tokens are transferred.

Tools Used

Manual auditing - VS Code and me :)

Recommended Mitigation Steps

Make sure that the values fit in the variables you are trying to assign them to when casting variables to smaller types.

lacoop6tu (FIAT DAO) acknowledged and commented:

This is true but doesn’t apply in our case, we use a BalV2 Pool Token which would never reach those values in terms of existing supply.

Justin Goro (judge) decreased severity and commented:

The use of Balancer tokens doesn’t preclude numbers above 128bit. In the BalancerV2 source code, all amounts are in uint256. However, the widespread practice of standard Ethereum tokens makes the likelihood of even encountering a token balance above 128 bits is negligible and Balancer does scale down big tokens if the other tokens in the pool are lower when minting.

Marking this as high risk is simply not realistic. This and its duplicates will be downgraded to medium risk (2) as it’s a type of technicality that will have no bite in reality.

IllIllI (warden) reviewed mitigation:

The sponsor acknowledges that overflow is technically possible, but considers this as unlikely to happen in practice.

In the final PR, the sponsor added a code comment saying that the contract does not support tokens where maxSupply>2^128-10^[decimals].


The original report as published on Code4rena, by CertoraInc. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 casts a user-supplied amount to int128 without validating that it fits, causing truncation.

Impact

Tokens transferred by the user could become permanently stuck in the contract because the recorded locked amount is wrong; however, the sponsor argues this is unlikely in practice due to token supply limits.

What the attacker needed

A token with supply above 2^127; ability to call createLock

The fix

The contract should validate that the amount fits within int128 before casting, or use a larger integer type; the sponsor added a comment that the contract does not support tokens with very large max supply.

How it was found

  1. Alice calls createLock with an amount of 2**128 tokens and a chosen unlock time.
  2. The contract adds the casted amount to locked_.amount, but the cast truncates to 0, so locked_.amount stays 0.
  3. The tokens are transferred from Alice, but the contract records no locked amount, so she cannot withdraw them.

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 stores locked token amounts in a small integer type that cannot hold very large numbers. If someone tries to lock an amount too big for that type, the number wraps around to a smaller value (or zero), so the contract thinks nothing was locked even though the tokens were taken. The tokens then cannot be withdrawn.

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 CertoraInc

View all

16 more disclosed reports, best first.

More from FIAT DAO veFDT contest

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.