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.
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.
fee is set to 50 BPS (i.e. 0.50%)
A whale deposits 200 tokens
A minnow deposits 0.01 tokens
A BathBuddy contract is set up for the BathToken contract.
The whale withdraws their funds
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.
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 :)
@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
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. ”
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
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
Set the fee to 50 basis points (0.50%).
A whale deposits 200 tokens.
A minnow deposits 0.01 tokens.
A BathBuddy contract is set up for the BathToken contract.
The whale withdraws their funds.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
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.