Submitted by xiaoming90, also found by 0x52, PP1004, shenwilly, and sashiketh_
BathBuddy is a Vesting Wallet that payout withdrawers any bonusTokens they may have accrued while staking in the Bath Token (e.g. network incentives/governance tokens).
BathBuddy Vesting Wallet releases a user their relative share of the pools total vested bonus token during the withdraw call on BathToken.sol. This vesting occurs linearly over Unix time.
It was observed that an attacker could steal almost all the bonusTokens in the BathBuddy Vesting Wallet.
The root cause of this issue is that the amount of bonusTokens that a user is entitled to is based on their relative share of the pools total vested bonus token at the point of the withdraw call. It is calculated based on the users spot share in the pool. 
Thus, it is possible for an attacker to deposit large amount of tokens into a BathToken Pool to gain significant share of the pool (e.g. 95%), and then withdraw the all the shares immediately. The withdraw call will trigger the BathToken.distributeBonusTokenRewards, and since attacker holds overwhelming amount of share in the pool, they will receive almost all the bonusToken in the BathBuddy Vesting wallet, leaving behind dust amount of bonusToken in the wallet. This could be perform in an atomic transaction and attacker can leverage on flash-loan to fund this attack.
The following shows an example of this issue:
The following code shows that the amount of bonusTokens a user is entitled is based on the users current share in the pool - amount = releasable * sharesWithdrawn/initialTotalSupply.
https://github.com/code-423n4/2022-05-rubicon/blob/8c312a63a91193c6a192a9aab44ff980fbfd7741/contracts/peripheral_contracts/BathBuddy.sol#L87
Following is the test output that demonstrates the above scenario:
Attacker Charles deposited 8500 WETH to the pool and withdraw them immediately at the final hour, and obtained almost all of the bonusTokens (997 DAI). When Alice withdraw from the pool, she only received 0.7 DAI as bonusTokens.
Script can be found https://gist.github.com/xiaoming9090/2252f6b6f7e62fca20ecfbaac6f754f5
Note: Due to some unknown issue with the testing environment, please create a new BathBuddy.released2 functions to fetch the amount of token already released.
Loss of Fund for the users. BathToken LPs not able to receive the accured bonusToken that they are entitled to.
Update the reward mechanism to ensure that the bonusTokens are distribute fairly and rewards of each user are accured correctly.
In the above example, since Alice holds 50% of the shares in the pool throughout the majority of the reward period, she should be entitled to close to 50% to the rewards/bonus. Anyone who joins the pool at the last hour of the reward period should only be entitled to dust amount of bonusToken.
Additionally, spot (or current) share of the pool should not be used to determine the amount of bonusToken a user is entitled to as it is vulnerable to pool/share manipulation or flash-loan attack. Checkpointing mechanism should be implemented so that at the minimum, the users amount of share in the previous block is used for determining the rewards. This make flash-loan attack infeasible as such attack has to happen within the same block/transaction.
For distributing bonus/rewards, I would suggest checking out a widely referenced Synthetixs Reward Contract as I think that it would be more relevant than OZs Vesting Wallet for this particular purpose.
bghughes (Rubicon) confirmed
HickupHH3 (judge) commented:
