Bond max-buyer might end up buying the max buy of the next epoch
The MuteBond.deposit() function lets a user set max_buy to true, which ignores the supplied value and buys the maximum amount available in the current epoch. If a new epoch starts before the transaction is included, the user may buy the entire next epoch's maximum instead of the intended remaining amount.
0xA5DF Follow with hansfriese and HollaDieWaldfee
Originally disclosed on Code4rena
Submitted by 0xA5DF, also found by hansfriese and HollaDieWaldfee
The MuteBond.deposit() function allows users to specify the amount of value they want to purchase bonds for or to set max_buy to true. If max_buy is set to true the amount specified in the value parameter is ignored and instead the maximum amount available for purchase in the current epoch is used. This can lead to a scenario where a user intends to purchase the remaining amount of current epoch, but till the tx is included in the blockchain a new epoch starts (either by an innocent user or by an attacker) and the user ends up buying the entire amount of the next epoch.
Impact
A. The user ends up buying a much higher amount than intended B. The user ends up buying it for a lower price than intended (i.e. less payout for the buyer)
Proof of Concept
The PoC below shows how maxPurchaseAmount() increases when a new era starts.
File: test/bonds.ts
it('Max buy PoC', async function () {
// buy 99% of amount available for purchase in current epoch
let maxValue = await bondContract.maxPurchaseAmount();
let depositValue = maxValue.mul(99).div(100);
await bondContract.connect(buyer1).deposit(depositValue, buyer1.address, false);
// The amount available when the victim sends out the tx
var expectedDeposit = await bondContract.maxPurchaseAmount()
await bondContract.connect(buyer1).deposit('0', buyer1.address, true);
// The amount available when the victims's tx is included in the blockchain
var actualDeposit = await bondContract.maxPurchaseAmount();
// expected deposit = 1 wad
// actual deposit = 100 wad
console.log({expectedDeposit, actualDeposit});
})
The following snippet shows that when a user sets max_buy to true the value used is the maxPurchaseAmount()
if(max_buy == true){
value = maxPurchaseAmount();
payout = maxDeposit();
} else {
Recommended Mitigation Steps
Require the user to specify the epoch number when doing a ‘max buy’, and revert if it doesn’t match the current epoch (it might be a good idea to refactor the code to 2 external functions for normal buy and max buy, where they both share an internal function to make the actual deposit).
Side note: this is similar to another bug I’ve reported regarding getting a lower price than expected, however the root cause, impact, and mitigation are different and therefore I’ve reported this separately.
mattt21 (Mute Switch) confirmed
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 max_buy option reads the current epoch's maximum at execution time rather than binding the purchase to the epoch the user saw when submitting the transaction.
Impact
The user could buy a much higher amount than intended and at a lower price than intended, resulting in less payout for the buyer.
What the attacker needed
User chooses max_buy option; new epoch starts before tx inclusion
The fix
The user should be required to specify the epoch number for a max buy, and the transaction should revert if it does not match the current epoch.
How it was found
- Buy 99% of the amount available for purchase in the current epoch.
- Record the expected maximum purchase amount for the victim's transaction.
- Send a max_buy deposit transaction.
- A new epoch starts before the transaction is included, so the actual maximum purchase amount is much larger.
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 bond is a way to buy tokens at a set price. The contract lets you say 'buy as much as possible right now' instead of giving an exact amount. If the 'right now' changes between when you send the transaction and when it is processed, you can end up buying far more than you planned.
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.