Skip to content
Business logic Audit finding Intermediate

BathBuddy contract’s vestedAmount function includes fees leading to users being disproportionately rewarded after whale withdraws

The vestedAmount function in the BathBuddy contract incorrectly includes fees in its calculation, causing a double-counting of fees. This allows a small depositor to withdraw a disproportionately large reward after a large depositor withdraws, potentially draining fees from the contract.

sseefried Follow

Originally disclosed on Code4rena

Submitted by sseefried

https://github.com/code-423n4/2022-05-rubicon/blob/8c312a63a91193c6a192a9aab44ff980fbfd7741/contracts/peripheral_contracts/BathBuddy.sol#L103-L104

https://github.com/code-423n4/2022-05-rubicon/blob/8c312a63a91193c6a192a9aab44ff980fbfd7741/contracts/peripheral_contracts/BathBuddy.sol#L133

Impact

When a whale withdraws their tokens and receives rewards from the BathBuddy contract the fees they pay will erroneously become part of the calculation performed in function vestedAmount. This means that any subsequent withdrawer of funds may receive a disproportionate amount of tokens. The fees paid by a whale could still be much larger than the amount of tokens invested by a minnow.

Although similar to the issue “When BathToken contract is recipient of fees then users can make disproportionate returns after whales withdraw” it is not the same issue since fees are always accrued in the BathBuddy contract and this cannot be changed. Also, the calculations in are subtly different

However, the outcome is the same. A minnow can receive a disproportionate reward and drain much of the fees from the contract.

The intention of setting the pool as the recipient of the fees was to reward HODLers but, in fact, they will be incentivised to withdraw after a whale does.

Proof of Concept

Consider the following scenario.

  1. fee is set to 50 BPS (i.e. 0.50%)
  2. A whale deposits 200 tokens
  3. A minnow deposits 0.01 tokens
  4. A BathBuddy contract is set up for the BathToken contract.
  5. The whale withdraws their funds
  6. The minnow then withdraws their funds

After step 5, the function vestedAmount will return a value that includes the fees paid by the whale. This is because the BathBuddy contract is the recipient of all fees. They are not transferred anywhere.

Thus, when the minnow withdraws their funds releasable is much larger than the amount they otherwise would have expected. Further sharesWithdrawn is equal to initialTotalSupply in this particular scenario so mul(sharesWithdrawn).div(initialTotalSupply) evaluates to 1. This means that amount = releaseable.

A test has been written in the private fork that exhibits this behaviour.

Recommended Mitigation Steps

Keep a tally of the fees accrued in a separate variable and work out a fairer system for distributing rewards to HODLers.

csanuragjain (warden) commented:

The totalAllocation is set as IERC20(token).balanceOf(address(this)) + released(token)

In this case

a. IERC20(token).balanceOf(address(this)) includes the fees as warden mentioned

b. released(token) also includes the fees since _erc20Released[address(token)] is updated with amount and not amountWithdrawn BathBuddy.sol#L116

bghughes (Rubicon) disputed and commented:

I believe this is a non-issue and disagree with the assessment for one key reason: the fees accrued still adhere to the vesting schedule. You are correct that after a whale withdraws there are then more rewards to be withdrawn - that’s a feature, not a bug! Importantly, that fee accrual only adds to the pool stack, and although true that the minnow can withdraw right now for marginal gains (their share of the vested fee amount, fees vest as if vesting from the start) they have no reason to do so because they pay the same withdrawal fee.

What do you think of this situation @csanuragjain ? Would love another opinion :)

csanuragjain (warden) commented:

@bghughes I agree with your point but here fees is added twice while calculating the totalAllocation. totalAllocation is calculated as IERC20(token).balanceOf(address(this)) + released(token).

The problem here is both IERC20(token).balanceOf(address(this)) and released(token) are inclusive of the fees which makes double sum of fees

a. IERC20(token).balanceOf(address(this)) includes the fees as all fees are kept in contract itself b. released(token) also includes the fees since _erc20Released[address(token)] is updated with amount and not amountWithdrawn BathBuddy.sol#L116

Since totalAllocation represent a balance which is not even present in the contract (increased by fee amount twice), so contract wont have enough fund to send reward

Can you please suggest

HickupHH3 (judge) commented:

I agree with @csanuragjain: there seems to be a double increment of the fees.

  • _erc20Released[token] is incremented by amount, which consists of the fee

  • The fee is kept within the contract, not transferred out => balanceOf() consists of fee too

  • Calculated vested amount

    IERC20(token).balanceOf(address(this)) + released(token),
    

    therefore is a double fee accounting.

Seems like a high-severity bug that no other wardens caught o.O

HickupHH3 (judge) commented:

Anyway, regarding the main issue, because no funds are stolen or compromised per se, and can be treated as rewards / incentives, I’ll be downgrading it to medium as it relates to reward distribution that the remaining stakers aren’t entitled to yet (and thus isn’t considered to be “stolen” or “compromised”).

As leastwood puts it: “Protocol leaked value has a broad context but I think most judges can agree that it would pertain to rewards being paid out a lower rate than expected. Or, users can extract small amounts (up to debate on what is considered to be small) from the protocol under certain assumptions. ”

bghughes (Rubicon) confirmed and commented:

Great, thank you @HickupHH3


The original report as published on Code4rena, by sseefried. 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 total allocation is calculated as the contract's token balance plus already released tokens, but both include the fees, leading to double counting.

Impact

A small depositor could receive a disproportionately large reward and drain much of the fees from the contract, but the judge downgraded it to medium because no funds are stolen per se and it relates to reward distribution.

What the attacker needed

A whale deposits and withdraws; a minnow deposits; BathBuddy contract set up

The fix

Keep a tally of the fees accrued in a separate variable and work out a fairer system for distributing rewards to HODLers.

How it was found

  1. Set the fee to 50 basis points (0.50%).
  2. A whale deposits 200 tokens.
  3. A minnow deposits 0.01 tokens.
  4. A BathBuddy contract is set up for the BathToken contract.
  5. The whale withdraws their funds.
  6. The minnow then withdraws their funds.

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 holds rewards for people who keep their tokens in it. When someone takes their tokens out, they pay a fee that stays in the contract. The contract's reward calculation mistakenly counts those fees twice, so the next person to withdraw gets more than they should. This means a small depositor can take a large share of the fees left by a big depositor.

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 sseefried

View all

6 more disclosed reports, best first.

More from Rubicon v2

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.