Skip to content
Business logic Audit finding Intermediate

Rewards aren’t updated before user’s balance change in Gauge’s withdrawToken

The withdrawToken function in the Gauge contract does not update rewards before changing a user's balance, unlike the withdraw wrapper. If a user calls withdrawToken directly, their accumulated rewards can be lost due to incorrect reward accounting.

hyh Follow

Originally disclosed on Code4rena

Submitted by hyh, also found by codexploder

_updateRewardForAllTokens is called in withdraw() only, while both withdraw() and withdrawToken() are public and can be called directly. Reward update is needed on user’s balance change, its absence leads to reward data rewriting and rewards losing impact for a user.

Per discussion with project withdrawToken() will not be exposed in UI, so setting the severity to be medium as user’s direct usage of the vulnerable function is required here, while the impact is losing of a part of accumulated rewards.

Proof of Concept

withdraw() is a wrapper for withdrawToken(), which changes user’s balance. withdrawToken() can be called directly, and in this case there will be no rewards update:

Gauge.sol#L548-L561

    function withdraw(uint amount) public {
        _updateRewardForAllTokens();

        uint tokenId = 0;
        if (amount == balanceOf[msg.sender]) {
            tokenId = tokenIds[msg.sender];
        }
        withdrawToken(amount, tokenId);
    }

    function withdrawToken(uint amount, uint tokenId) public lock {
        totalSupply -= amount;
        balanceOf[msg.sender] -= amount;
        _safeTransfer(stake, msg.sender, amount);

This way if a user call withdrawToken(), the associated rewards can be lost due to incomplete reward balance accounting.

References

A showcase of reward update issue: https://github.com/belbix/solidly/issues/1

It’s replicated live with users losing accumulated rewards: https://github.com/solidlyexchange/solidly/issues/55

Recommended Mitigation Steps

Consider moving rewards update to withdrawToken(), since it is called by withdraw() anyway:

    function withdraw(uint amount) public {
-       _updateRewardForAllTokens();

        uint tokenId = 0;
        if (amount == balanceOf[msg.sender]) {
            tokenId = tokenIds[msg.sender];
        }
        withdrawToken(amount, tokenId);
    }

    function withdrawToken(uint amount, uint tokenId) public lock {
+    	_updateRewardForAllTokens();

        totalSupply -= amount;
        balanceOf[msg.sender] -= amount;
        _safeTransfer(stake, msg.sender, amount);

Or, if the intent is to keep withdrawToken() as is, consider making it private, so no direct usage be possible:

-    function withdrawToken(uint amount, uint tokenId) public lock {
+    function withdrawToken(uint amount, uint tokenId) private lock {

        totalSupply -= amount;
        balanceOf[msg.sender] -= amount;
        _safeTransfer(stake, msg.sender, amount);

pooltypes (Velodrome) disputed

Alex the Entreprenerd (judge) commented:

I’m extremely conflicted about this finding as it tries to imply that not calling _updateRewardForAllTokens is dangerous, yet no effort is made in showing why.

That said, _updateRewardForAllTokens seems to be reliant on the supplyCheckpoints which would be changed via _writeSupplyCheckpoint. Leading me to agree that the reward math will be wrong.


The original report as published on Code4rena, by hyh. 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 reward update logic was placed only in the withdraw wrapper instead of in the withdrawToken function that actually changes the balance, allowing direct calls to bypass it.

Impact

A user who calls withdrawToken directly could lose a part of their accumulated rewards, but the project stated that withdrawToken will not be exposed in the UI, so direct usage is required.

What the attacker needed

User calls withdrawToken directly

The fix

The reward update should be moved into withdrawToken, or withdrawToken should be made private so it cannot be called directly.

How it was found

  1. A user calls withdrawToken directly instead of withdraw.
  2. The balance is reduced without updating rewards first.
  3. The reward accounting becomes incorrect and part of the user's accumulated rewards is lost.

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.

In a smart contract, some functions are meant to be called only through a wrapper that does extra bookkeeping. Here, the wrapper updates rewards, but the inner function can be called directly, skipping that update. So a user who calls the inner function directly may lose some of the rewards they have earned.

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 hyh

View all

137 more disclosed reports, best first.

More from Velodrome Finance 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.