Fee-vault can be made insolvent in case of defaults
The fee vault caches the pool's b_rate and skips updating it if the timestamp matches, so within a single block a default that lowers the pool's b_rate leaves the vault using an outdated higher rate. An attacker can bundle a deposit, a liquidation causing default, and a withdrawal in one block to make the vault burn fewer b_tokens than the pool actually burns, creating a deficit that makes the vault insolvent for the last withdrawer.
carrotsmuggler Follow with 0x007, Tigerfrake and 1 other
Originally disclosed on Code4rena
Submitted by carrotsmuggler, also found by 0x007, Tigerfrake, and YouCrossTheLineAlfie
Finding description and impact
In case of a loan default, the backstop funds are insufficient to cover the liability. In this case, the hit is taken by the collateral providers, and the b_rate drops.
pub fn default_liabilities(&mut self, e: &Env, reserve: &mut Reserve, amount: i128) {
self.remove_liabilities(e, reserve, amount);
// determine amount of funds in underlying that have defaulted
// and deduct them from the b_rate
let default_amount = reserve.to_asset_from_d_token(e, amount);
let b_rate_loss = default_amount.fixed_div_ceil(&e, &reserve.data.b_supply, &SCALAR_12);
reserve.data.b_rate -= b_rate_loss;
In this scenario, the b_rate of the token is reduced. This creates a situation in the fee vault which can lead to insolvency.
The fee-vault, during deposits and withdrawals, calls update_rate to make sure it has the latest b_rate from the pool.
let now = e.ledger().timestamp();
if now == self.last_update_timestamp {
return;
}
let new_rate = pool::reserve_b_rate(e, &self.address);
if new_rate == self.b_rate {
self.last_update_timestamp = now;
return;
}
However, as can be seen above, there is an escape condition where the b_rate is not updated if the timestamp is the same. This can trigger when there are two transactions that are executed in the same block. In that case, the b_rate is not updated and the older value is used.
This is not an issue in general operations, because b_rate cannot change within the same block. However, in the case of a default, the b_rate can change within the same block. So the fee_vault can be made to use an outdated b_rate.
Imagine a situation where 3 transactions are bundled together in the same block:
- tx 1 - a simple deposit to the fee vault
- tx 2 - a liquidation causing a default
- tx 3 - a withdrawal from the fee vault
Tx 1 updates the b_rate of the vault to the one in the pool. Tx 2 drops the b_rate in the pool, but the one in the vault remains the same. Tx 3 uses the outdated higher b_rate to calculate withdrawal amounts, since tx1 was in the same block so now == self.last_update_timestamp check passes. Since a higher b_rate is used, the fee vault ends up burning more b_tokens than it expects, leading to insolvency.
Lets assume the b_rate drops from 1.1 to 1.05. In tx 3, the vault still uses 1.1, but the pool uses 1.05. Say a user wants to withdraw 1000 amount from the vault. The pool::withdraw(&e, &reserve, &user, amount); call will burn 1000/1.05=953 b_tokens from the vault. But in the vaults withdraw function, the b_tokens_amount will be calculated as 1000/1.1=910. So the vault.total_b_tokens will be reduced by 910, but the pool will burn 953 btokens from the vault. So no there is a deficit of 43 b_tokens in the vault, which leads to the insolvency for the user who exits last.
let b_tokens_amount = vault.underlying_to_b_tokens_up(amount); //@audit older b_rate used
let mut user_shares = storage::get_reserve_vault_shares(e, &vault.address, user);
let share_amount = vault.b_tokens_to_shares_up(b_tokens_amount);
require_positive(e, share_amount, FeeVaultError::InvalidBTokensBurnt);
if vault.total_shares < share_amount || vault.total_b_tokens < b_tokens_amount {
panic_with_error!(e, FeeVaultError::InsufficientReserves);
}
if share_amount > user_shares {
panic_with_error!(e, FeeVaultError::BalanceError);
}
vault.total_shares -= share_amount;
vault.total_b_tokens -= b_tokens_amount; //@audit reducing by incorrect amount
All three transactions above can be sent by the same person, bundled together, removing the MEV necessity.
Proof of Concept
Say the default reduces the b_rate from 1.1 to 1.05. During withdrawal, the pool has a b_rate of 1.05, but the vault has a b_rate of 1.1.
Thus, when withdrawing 1000, the pool will burn 1000/1.05=953 b_tokens, but the vault will burn 1000/1.1=910 b_tokens. This leads to a deficit of 43 b_tokens in the vault, which leads to insolvency for the user who exits last.
Recommended mitigation steps
Update the rate even if the timestamp matches.
markus_pl10 (Script3) confirmed
Validated this is a finding. The execution of this is extremely edge case, as A (the backstop needs to hit the doomsday scenario of running out of funds), and B, you have to win the bad debt auction to bundle the exploit pathway correctly in one contract call.
However, it doesn’t seem like there is a way to extract funds here. An attacker would need to already be using the fee vault, and use the exploit to avoid losing funds, at the expense of the last withdrawer.
Fixed to load
b_ratefrom chain every time, and update the internal vault every time.
Commit a63d9a0 to patch
b_rateloss sandwhich issue.
Status: Mitigation confirmed. Full details in reports from 0x007, oakcobalt, 0xAlix2 and Testerbot.
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
The fee vault's update_rate function returns early if the current timestamp equals the last update timestamp, so it does not refresh the b_rate within the same block even when a default has changed it.
Impact
The fee vault can become insolvent, causing the last user to withdraw to lose funds; the text notes this requires the backstop to run out of funds and the attacker to win a bad debt auction, and that no direct fund extraction is possible.
What the attacker needed
Backstop funds exhausted; attacker wins bad debt auction; attacker uses fee vault; transactions bundled in one block
The fix
The fee vault was fixed to load the b_rate from the chain every time and update its internal state accordingly.
How it was found
- Deposit into the fee vault to update its cached b_rate to the current pool rate.
- Trigger a liquidation that causes a default, lowering the pool's b_rate.
- Withdraw from the fee vault in the same block, using the outdated higher b_rate.
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 fee vault holds shares that represent a claim on a lending pool. The vault keeps a cached exchange rate (b_rate) to calculate how many pool tokens to burn when someone withdraws. If the pool's rate drops due to a loan default, but the vault still uses the old higher rate, it undercounts the tokens it should burn, leaving fewer tokens than needed for everyone else. This can make the vault insolvent, meaning the last person to withdraw cannot get their full share.
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.