Skip to content
Smart contracts Audit finding Intermediate

Unrestricted claiming of fees due to missing balance updates in FeeSplitter

The FeeSplitter contract fails to update a user's fee accumulator when tokens are transferred, so a new account receiving tokens has a zero offset and can claim fees as if it had held the tokens from the start. An attacker could repeatedly transfer the same tokens to fresh accounts and claim fees each time, draining the contract.

EV_om Follow with lukejohn, Tychai0s and 48 others

Originally disclosed on Code4rena

Submitted by EV_om, also found by lukejohn, Tychai0s, visualbits, Josephdara_0xTiwa, kuprum, 0x0bserver, peritoflores, Tumelo_Crypto, anshujalan, Kose, nonseodion (1, 2), Aymen0909, grearlake, Kong, 0xPhantom (1, 2), jangle, rouhsamad, Soul22 (1, 2), btk (1, 2), 0xE1, Ryonen, Varun_05 (1, 2), 0xmystery, jacopod (1, 2, 3), wangxx2026 (1, 2), novodelta, ubermensch (1, 2), Draiakoo (1, 2), 0xprinc, santipu_, DarkTower, 0xc0ffEE (1, 2), petro_1912 (1, 2), almurhasan, Cosine, 0xNaN, zhaojie (1, 2), khramov, al88nsk, mahdirostami, Lalanda, iamandreiski (1, 2), jasonxiale (1, 2), UbiquitousComputing, pep7siup, osmanozdemir1, 0xLogos, salutemada, oreztker (1, 2), alexfilippov314, peanuts (1, 2), SovaSlava, aslanbek, SpicyMeatball, dimulski, BowTiedOriole, Bobface, Soliditors, dopeflamingo, nmirchev8, hals (1, 2), Krace, klau5, ke1caM, nuthan2x, cccz, pkqs90, AlexCzm (1, 2, 3), 0xMAKEOUTHILL, ptsanev, Kow, israeladelaja, rvierdiiev, kutugu (1, 2), DarkTower, peanuts, d3e4, 0x0bserver, Aymen0909, 0xE1, Soul22, and 0xStalin*

The FeeSplitter contract is designed to distribute fees among token holders. It employs an accumulator pattern to distribute rewards over time among users who do not sell or withdraw their tokens as ERC20s. This pattern works by maintaining an accumulator representing the cumulative total of the reward rate over time, and updating it for each user every time their balance changes.

However, the current implementation does not update the accumulator associated with each user during token transfers, deposits, or withdrawals. The onBalanceChange() function, which is responsible for updating the accumulator following changes in a user’s balance, is exclusively called from Curves._transferFees(), which is only called during buy and sell transactions.

This oversight can be easily exploited to drain the FeeSplitter contract of its fees. An attacker could repeatedly transfer the same tokens to new accounts and claim fees every time. This is possible because data.userFeeOffset[account] will be zero for every new account they transfer to, while the claimable rewards are calculated using the current balance returned by the Curves contract.

Since there is no limit to the amount of rewards that may accumulate in the FeeSplitter contract, this can be considered loss of matured yield and is hence classified as high severity.

Proof of Concept

Consider the following scenario:

  1. Alice buys any amount of curve tokens.

  2. Alice transfers her tokens to a new account, Account1, by calling transferCurvesToken().

  3. The onBalanceChange() function is not triggered during the transfer, so data.userFeeOffset[account] for Account1 is not updated.

  4. Account1 can now call FeeSplitter.claimFees() and will receive fees as if it had been holding the tokens since the beginning, as data.userFeeOffset(account) for this account is 0.

    https://github.com/code-423n4/2024-01-curves/blob/516aedb7b9a8d341d0d2666c23780d2bd8a9a600/contracts/FeeSplitter.sol#L63-L88

    function updateFeeCredit(address token, address account) internal {
        TokenData storage data = tokensData[token];
        uint256 balance = balanceOf(token, account);
        if (balance > 0) {
            uint256 owed = (data.cumulativeFeePerToken - data.userFeeOffset[account]) * balance;
            data.unclaimedFees[account] += owed / PRECISION;
            data.userFeeOffset[account] = data.cumulativeFeePerToken;
        }
    }
    
    ...
    
    function claimFees(address token) external {
        updateFeeCredit(token, msg.sender);
        uint256 claimable = getClaimableFees(token, msg.sender);
        if (claimable == 0) revert NoFeesToClaim();
        tokensData[token].unclaimedFees[msg.sender] = 0;
        payable(msg.sender).transfer(claimable);
        emit FeesClaimed(token, msg.sender, claimable);
    }
    
  5. Alice creates another new account, Account2, and transfers the tokens from Account1 to Account2.

  6. The onBalanceChange(token, account) function is not triggered during this transfer, so data.userFeeOffset[account] for Account2 is zero.

  7. Account2 can now claim fees.

  8. Alice can repeat this process, creating new accounts and transferring tokens between them, to drain the contract of its fees.

Recommended Mitigation Steps

To mitigate this issue, the onBalanceChange(token, account) function should be triggered during all token transfers, deposits, and withdrawals. For token transfers, it should be triggered for both accounts. This will ensure that userFeeOffset is accurately tracked, preventing users from claiming fees multiple times.

raymondfam (Lookout) commented:

The root cause is due to not calling updateFeeCredit() diligently.

andresaiello (Curves) confirmed


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

The onBalanceChange function, which updates the fee accumulator, is only called during buy and sell transactions, not during token transfers, deposits, or withdrawals.

Impact

An attacker could drain the FeeSplitter contract of its accumulated fees by repeatedly transferring tokens to new accounts and claiming rewards each time.

What the attacker needed

Ability to buy curve tokens; ability to create multiple accounts; ability to transfer tokens between accounts

The fix

The onBalanceChange function should be called during all token transfers, deposits, and withdrawals, for both accounts in a transfer, so that userFeeOffset is accurately tracked.

How it was found

  1. Alice buys curve tokens.
  2. Alice transfers her tokens to a new account, Account1.
  3. The onBalanceChange function is not triggered, so Account1's userFeeOffset remains zero.
  4. Account1 calls claimFees and receives fees as if it had held the tokens since the beginning.
  5. Alice transfers the tokens from Account1 to another new account, Account2.
  6. Account2 can also claim fees, and the process can be repeated to drain the contract.

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.

This is a smart contract that shares fees among token holders. It tracks how much each holder is owed using a running total. When tokens move to a new account, the contract forgets to update that account's starting point, so the new account can claim fees for time it never held the tokens. By moving tokens to new accounts over and over, someone can collect the same fees many times.

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 EV_om

View all

23 more disclosed reports, best first.

More from Curves

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.