Skip to content
Business logic Audit finding Intermediate

Vault is Not Compatible with Fee Tokens and Vaults with Such Tokens Could Be Exploited

The Cally contract records the full amount of an ERC20 token transferred into a vault, but fee-on-transfer tokens deliver less than that amount, so the contract's balance is lower than the sum of recorded vault amounts. An attacker can create a vault with the same fee token and withdraw the recorded amount, making the contract pay the transfer fee and potentially causing other users' withdrawals to fail.

0x1337 Follow

Originally disclosed on Code4rena

Submitted by 0x1337, also found by 0x52, 0xDjango, 0xsanson, berndartmueller, BondiPestControl, BowTiedWardens, cccz, dipp, GimelSec, hake, hickuphh3, horsefacts, hubble, IllIllI, MaratCerby, MiloTruck, minhquanym, PPrieditis, reassor, shenwilly, smilingheretic, TrungOre, VAD37, and WatchPug_

https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L198-L200

https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L294-L296

https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L343-L345

Impact

Some ERC20 tokens charge a transaction fee for every transfer (used to encourage staking, add to liquidity pool, pay a fee to contract owner, etc.). If any such token is used in the createVault() function, either the token cannot be withdrawn from the contract (due to insufficient token balance), or it could be exploited by other such token holders and the Cally contract would lose economic value and some users would be unable to withdraw the underlying asset.

Proof of Concept

Plenty of ERC20 tokens charge a fee for every transfer (e.g. Safemoon and its forks), in which the amount of token received is less than the amount being sent. When a fee token is used as the token in the createVault() function, the amount received by the contract would be less than the amount being sent. To be more precise, the increase in the cally contract token balance would be less than vault.tokenIdOrAmount for such ERC20 token because of the fee.

        vault.tokenType == TokenType.ERC721
            ? ERC721(vault.token).transferFrom(msg.sender, address(this), vault.tokenIdOrAmount)
            : ERC20(vault.token).safeTransferFrom(msg.sender, address(this), vault.tokenIdOrAmount);

The implication is that both the exercise() function and the withdraw() function are guaranteed to revert if there’s no other vault in the contract that contains the same fee tokens, due to insufficient token balance in the Cally contract.

When an attacker observes that a vault is being created that contains such fee tokens, the attacker could create a new vault himself that contains the same token, and then withdraw the same amount. Essentially the Cally contract would be paying the transfer fee for the attacker because of how the token amount is recorded. This causes loss of user fund and loss of value from the Cally contract. It would make economic sense for the attacker when the fee charged by the token accrue to the attacker. The attacker would essentially use the Cally contract as a conduit to generate fee income.

Recommended Mitigation Steps

Recommend disallowing fee tokens from being used in the vault. This can be done by adding a require() statement to check that the amount increase of the token balance in the Cally contract is equal to the amount being sent by the caller of the createVault() function.

outdoteth (Cally) confirmed and commented:

reference issue: https://github.com/code-423n4/2022-05-cally-findings/issues/39

HardlyDifficult (judge) decreased severity to Medium and commented:

This is a good description of the potential issue when a fee on transfer token is used.

Lowing to 2 (Medium). See https://github.com/code-423n4/org/issues/3 for some discussion on how to consider the severity for these types of issues.

The attack described does leak value, but the vault could be recovered by transferring in the delta balance so that the contract has more than enough funds in order to exercise or withdraw. That plus these types of tokens are relatively rare is why I don’t think this warrants a High severity.


The original report as published on Code4rena, by 0x1337. 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 credits the vault with the requested transfer amount without checking the actual increase in its token balance, so fee-on-transfer tokens create a shortfall.

Impact

The contract could lose value and some users may be unable to withdraw their assets, though the judge noted the vault could be recovered by transferring in the missing balance and such tokens are relatively rare.

What the attacker needed

A fee-on-transfer ERC20 token is used; attacker can create vaults with that token

The fix

The recommendation is to disallow fee tokens by requiring that the increase in the contract's token balance equals the amount sent by the caller.

How it was found

  1. Create a vault using a fee-on-transfer ERC20 token, so the contract receives less than the recorded amount.
  2. Observe that the contract's token balance is now lower than the sum of vault amounts for that token.
  3. Create another vault with the same fee token and withdraw the same recorded amount, making the contract cover the transfer fee.

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.

Some tokens take a small cut every time they are moved, so the receiver gets less than the sender sent. This contract assumes it receives the full amount and writes that full amount into its records. That mismatch lets someone else take out more than they put in, and can leave the contract short of tokens for honest users.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by 0x1337

View all

4 more disclosed reports, best first.

More from Cally contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.