Skip to content
Business logic Audit finding Intermediate

onBalanceChange causes previously unclaimed rewards to be cleared

The onBalanceChange function resets a user's fee offset without first crediting the fees they have already earned, so unclaimed rewards are wiped out. An attacker could buy the same token twice and lose the reward from the first purchase.

kutugu Follow with 0xMAKEOUTHILL, zxriptor and 48 others

Originally disclosed on Code4rena

Submitted by kutugu, also found by 0xMAKEOUTHILL, pkqs90 (1, 2), zxriptor, visualbits, d3e4, Tychai0s, peritoflores, 0x0bserver, kuprum, xiao, dimulski, McToady, SovaSlava, Aymen0909, anshujalan, Zach_166, rouhsamad, Kose, 0xAadi, nonseodion, nazirite, Soul22, TermoHash, ether_sky (1, 2, 3), whoismatthewmc1, 0xmystery (1, 2, 3), HChang26, FastChecker, jacopod, AgileJune, matejdb (1, 2), ubermensch, Mike_Bello90 (1, 2), btk, Varun_05, santipu_, 0xPhantom, DarkTower, codegpt, sl1, KingNFT, almurhasan, erosjohn, Cosine, zhaojie, Topmark, dd0x7e8, UbiquitousComputing, Lalanda, mahdirostami, XORs33r, PENGUN, jasonxiale, pep7siup, lukejohn (1, 2, 3), dopeflamingo, osmanozdemir1, spacelord47, 0xLogos, fishgang (1, 2), alexfilippov314, SpicyMeatball, DanielArmstrong, zhaojohnson, aslanbek, hals, BowTiedOriole, Bobface, wangxx2026, nmirchev8, L0s1, KupiaSec, Oxsadeeq, BugzyVonBuggernaut, klau5, ke1caM, Soliditors, ZanyBonzy, EV_om, 0x111, cccz, bronze_pickaxe, Stormreckson, twcctop, deepplus, Inference (1, 2), AlexCzm, alexbabits, Kow, rvierdiiev, AS, Krace, and haxatron (1, 2)

onBalanceChange does not help to claim the previous rewards, but directly resets userFeeOffset, causing the user’s unclaimed rewards to be cleared.

Proof of Concept

    function testRewardLostAfterTwoPurchases1() public {
        curves.buyCurvesTokenWithName(address(this), 1, "", "");

        address attacker = makeAddr("Attacker");
        vm.deal(attacker, 1 ether);
        vm.startPrank(attacker);
        curves.buyCurvesToken{value: curves.getBuyPriceAfterFee(address(this), 1)}(address(this), 1);
        console2.log(feeRedistributor.getClaimableFees(address(this), attacker));
        curves.buyCurvesToken{value: curves.getBuyPriceAfterFee(address(this), 1)}(address(this), 1);
        console2.log(feeRedistributor.getClaimableFees(address(this), attacker));
    }

    function testRewardLostAfterTwoPurchases2() public {
        curves.buyCurvesTokenWithName(address(this), 1, "", "");

        address attacker = makeAddr("Attacker");
        vm.deal(attacker, 1 ether);
        vm.startPrank(attacker);
        curves.buyCurvesToken{value: curves.getBuyPriceAfterFee(address(this), 1)}(address(this), 1);
        uint256 fee1 = feeRedistributor.getClaimableFees(address(this), attacker);
        console2.log(fee1);
        feeRedistributor.claimFees(address(this));
        curves.buyCurvesToken{value: curves.getBuyPriceAfterFee(address(this), 1)}(address(this), 1);
        console2.log(fee1 + feeRedistributor.getClaimableFees(address(this), attacker));
    }
forge test --match-test testRewardLostAfterTwoPurchases -vvv

[PASS] testRewardLostAfterTwoPurchases1() (gas: 1287980)
Logs:
  3125000000000
  16666666666666

[PASS] testRewardLostAfterTwoPurchases2() (gas: 1303337)
Logs:
  3125000000000
  19791666666666

The above POC shows that when a user purchases the same token twice, the first reward is cleared.

Tools Used

Foundry

Recommended Mitigation Steps

onBalanceChange should claim the previous rewards, and then resets userFeeOffset:

diff --git a/contracts/FeeSplitter.sol b/contracts/FeeSplitter.sol
index bb24f02..9751902 100644
--- a/contracts/FeeSplitter.sol
+++ b/contracts/FeeSplitter.sol
@@ -66,8 +66,8 @@ contract FeeSplitter is Security {
         if (balance > 0) {
             uint256 owed = (data.cumulativeFeePerToken - data.userFeeOffset[account]) * balance;
             data.unclaimedFees[account] += owed / PRECISION;
-            data.userFeeOffset[account] = data.cumulativeFeePerToken;
         }
+        data.userFeeOffset[account] = data.cumulativeFeePerToken;
     }

     function getClaimableFees(address token, address account) public view returns (uint256) {
@@ -94,8 +94,7 @@ contract FeeSplitter is Security {
     }

     function onBalanceChange(address token, address account) public onlyManager {
-        TokenData storage data = tokensData[token];
-        data.userFeeOffset[account] = data.cumulativeFeePerToken;
+        updateFeeCredit(token, account);
         if (balanceOf(token, account) > 0) userTokens[account].push(token);
     }

andresaiello (Curves) confirmed

alcueca (Judge) decreased severity to Medium and commented:

Loss of fees or rewards is Medium.


The original report as published on Code4rena, by kutugu. 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 onBalanceChange function overwrites the user's fee offset with the current cumulative fee per token without first calling updateFeeCredit to add the owed fees to unclaimedFees.

Impact

A user who buys the same token more than once loses the unclaimed fees earned before the later purchase; the judge rated loss of fees or rewards as Medium.

What the attacker needed

A user buys the same token more than once

The fix

The onBalanceChange function should call updateFeeCredit to claim previous rewards before resetting the user's fee offset.

How it was found

  1. Create a token and buy one unit of it.
  2. As an attacker, buy one unit of the same token.
  3. Buy one more unit of the same token.
  4. Observe that the claimable fees after the second purchase do not include the reward from the first purchase.

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 pays out a share of trading fees to token holders. When someone buys more of a token, the contract is supposed to add up the fees they have earned so far before starting a new tally. Instead it starts the new tally without adding the old fees, so the earlier earnings disappear.

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 kutugu

View all

8 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.

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.