Users are able to front-run bad debt settlements to avoid insurance costs
A user can front-run the public call to settle bad debt by withdrawing their insurance fund shares first, avoiding the cost of the debt being seized from the fund. This lets them keep their share of insurance payments while shifting the loss to other users.
Originally disclosed on Code4rena
Submitted by kirk-baird, also found by itsmeSTYJ
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/InsuranceFund.sol#L71-L75 https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/InsuranceFund.sol#L62-L69
A user is able to front-run the call to seizeBadDebt() in InsuranceFund.sol to avoid paying the insurance costs.
seizeBadDebt() is called by MarginAccount.settleBadDebt() which is a public function. When this functions is called the transaction will appear in the mem pool. A user may then call InsuranceFund.withdraw() to withdraw all of their shares. If they do this with a higher gas fee it will likely be processed before the settleBadDebt() transaction. In this way they will avoid incurring any cost from the assets being seized.
The impact is that users may gain their share of the insurance funding payments with minimal risk (minimal as there is a change the front-run will not succeed) of having to repay these costs.
Proof of Concept
function withdraw(uint _shares) external {
settlePendingObligation();
require(pendingObligation == 0, "IF.withdraw.pending_obligations");
uint amount = balance() * _shares / totalSupply();
_burn(msg.sender, _shares);
vusd.safeTransfer(msg.sender, amount);
emit FundsWithdrawn(msg.sender, amount, block.timestamp);
}
function seizeBadDebt(uint amount) external onlyMarginAccount {
pendingObligation += amount;
emit BadDebtAccumulated(amount, block.timestamp);
settlePendingObligation();
}
Recommended Mitigation Steps
Consider making the withdrawals a two step process. The first step requests a withdrawal and marks the time. The second request processes the withdrawal but requires a period of time to elapse since the first step.
To avoid having users constantly having pending withdrawal, each withdrawal should have an expiry time and also a recharge time. The if the second step is not called within expiry amount of time it should be considered invalid. The first step must not be able to be called until recharge time has passed.
Another solution involves a design change where the insurance fund is slowly filled up over time without external deposits. However, this has the disadvantage that bad debts received early in the protocols life time may not have sufficient insurance capital to cover them.
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 withdrawal function allows immediate withdrawal without any delay or lock-up, so users can exit before a pending bad debt settlement is processed.
Impact
Users could gain their share of insurance funding payments with minimal risk of having to repay costs, as the front-run may not always succeed.
What the attacker needed
User holds insurance fund shares; bad debt settlement is pending; user can pay higher gas fee
The fix
The withdrawal should be made a two-step process with a time delay between request and completion, and each request should have an expiry and recharge time to prevent constant pending withdrawals.
How it was found
- A user holds shares in the insurance fund.
- A public call to settle bad debt is made and appears in the mem pool.
- The user calls withdraw with a higher gas fee to front-run the settlement.
- The withdrawal is processed first, so the user avoids the cost of the seized assets.
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.
When bad debt occurs, the insurance fund takes a hit, and everyone with shares in the fund pays a little. But a user can watch for the bad debt settlement transaction and quickly withdraw their shares before it goes through, so they avoid paying their part. This is unfair to the remaining users who then bear the whole cost.
What is Race condition?
Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.