Invalid utilization ratio check, blocking users from submitting a flash loan
The flash loan function checks the asset's utilization ratio immediately after adding the loan liability, before the user can execute actions and repay. This causes legitimate flash loans that would end below the maximum utilization to revert, blocking users from using the feature.
Submitted by 0xAlix2
Each asset in a pool has a utilization ratio, which refers to the percentage of the asset’s deposits that are currently being borrowed. A pool admin can set the max utilization ratio for each asset, which shouldn’t be bypassed; this is referred to as max_util.
The asset’s utilization ratio is checked whenever a borrow is made:
fn apply_borrow(
e: &Env,
actions: &mut Actions,
pool: &mut Pool,
user: &mut User,
request: &Request,
) -> i128 {
let mut reserve = pool.load_reserve(e, &request.address, true);
reserve.require_action_allowed(e, request.request_type);
let d_tokens_minted = reserve.to_d_token_up(e, request.amount);
user.add_liabilities(e, &mut reserve, d_tokens_minted);
@> reserve.require_utilization_below_max(e);
actions.add_for_pool_transfer(&reserve.asset, request.amount);
actions.do_check_health();
pool.cache_reserve(reserve);
d_tokens_minted
}
On the other hand, each pool allows users to submit a flash loan, which consists of taking a loan, doing some actions, and possibly paying it back in the same transaction.
However, when submitting a flash loan, and taking that loan (1@), the utilization ratio is checked immediately after it (2@).
https://github.com/code-423n4/2025-02-blend/blob/main/blend-contracts-v2/pool/src/pool/submit.rs#L89
pub fn execute_submit_with_flash_loan(
e: &Env,
from: &Address,
flash_loan: FlashLoan,
requests: Vec<Request>,
) -> Positions {
if from == &e.current_contract_address() {
panic_with_error!(e, &PoolError::BadRequest);
}
let mut pool = Pool::load(e);
let mut from_state = User::load(e, from);
let prev_positions_count = from_state.positions.effective_count();
// note: we add the flash loan liabilities before processing the other
// requests.
{
let mut reserve = pool.load_reserve(e, &flash_loan.asset, true);
let d_tokens_minted = reserve.to_d_token_up(e, flash_loan.amount);
1@> from_state.add_liabilities(e, &mut reserve, d_tokens_minted);
2@> reserve.require_utilization_below_max(e);
PoolEvents::flash_loan(
e,
flash_loan.asset.clone(),
from.clone(),
flash_loan.contract.clone(),
flash_loan.amount,
d_tokens_minted,
);
}
// ... snip ...
}
This is wrong, as it doesn’t give the user a chance to execute the flash loan, then repay it, and would revert. For example, A user submits a flash loan with 1k USDC. With that new debt, the utilization ratio exceeds the max utilization ratio. However, the user wants to take that loan, do some actions (we don’t really care), and wants to repay it after those actions. As a result, the end state after the transaction ends, the utilization ratio will be below the max; i.e., healthy.
With the current place of require_utilization_below_max, it blocks that user from executing that legit flashloan scenario.
Proof of Concept
Add the following test in blend-contracts-v2/pool/src/pool/submit.rs:
#[test]
#[should_panic(expected = "Error(Contract, #1207)")]
fn test_submit_with_flash_loan_wrong_max_util() {
let e = Env::default();
e.cost_estimate().budget().reset_unlimited();
e.mock_all_auths_allowing_non_root_auth();
e.ledger().set(LedgerInfo {
timestamp: 600,
protocol_version: 22,
sequence_number: 1234,
network_id: Default::default(),
base_reserve: 10,
min_temp_entry_ttl: 10,
min_persistent_entry_ttl: 10,
max_entry_ttl: 3110400,
});
let bombadil = Address::generate(&e);
let samwise = Address::generate(&e);
let pool = testutils::create_pool(&e);
let (oracle, oracle_client) = testutils::create_mock_oracle(&e);
let (flash_loan_receiver, _) = testutils::create_flashloan_receiver(&e);
let (underlying_0, underlying_0_client) = testutils::create_token_contract(&e, &bombadil);
let (mut reserve_config, mut reserve_data) = testutils::default_reserve_meta();
reserve_config.max_util = 9500000;
reserve_data.b_supply = 100_0000000;
reserve_data.d_supply = 50_0000000;
testutils::create_reserve(&e, &pool, &underlying_0, &reserve_config, &reserve_data);
let (underlying_1, underlying_1_client) = testutils::create_token_contract(&e, &bombadil);
let (reserve_config, reserve_data) = testutils::default_reserve_meta();
testutils::create_reserve(&e, &pool, &underlying_1, &reserve_config, &reserve_data);
oracle_client.set_data(
&bombadil,
&Asset::Other(Symbol::new(&e, "USD")),
&vec![
&e,
Asset::Stellar(underlying_0.clone()),
Asset::Stellar(underlying_1.clone()),
],
&7,
&300,
);
oracle_client.set_price_stable(&vec![&e, 1_0000000, 5_0000000]);
e.as_contract(&pool, || {
storage::set_pool_config(
&e,
&PoolConfig {
oracle,
min_collateral: 1_0000000,
bstop_rate: 0_1000000,
status: 0,
max_positions: 4,
},
);
underlying_1_client.mint(&samwise, &50_0000000);
underlying_1_client.approve(&samwise, &pool, &100_0000000, &10000);
underlying_0_client.mint(&samwise, &46_0000000);
underlying_0_client.approve(&samwise, &pool, &46_0000000, &10000);
// User takes a flash loan of 46_0000000, then supplies 50_0000000 of underlying_1, and finally repays the flash loan
execute_submit_with_flash_loan(
&e,
&samwise,
FlashLoan {
contract: flash_loan_receiver,
asset: underlying_0.clone(),
amount: 46_0000000,
},
vec![
&e,
Request {
request_type: RequestType::SupplyCollateral as u32,
address: underlying_1,
amount: 50_0000000,
},
Request {
request_type: RequestType::Repay as u32,
address: underlying_0.clone(),
amount: 46_0000000,
},
],
);
});
}
Recommended mitigation steps
pub fn execute_submit_with_flash_loan(
e: &Env,
from: &Address,
flash_loan: FlashLoan,
requests: Vec<Request>,
) -> Positions {
// ... snip ...
// note: we add the flash loan liabilities before processing the other
// requests.
{
let mut reserve = pool.load_reserve(e, &flash_loan.asset, true);
let d_tokens_minted = reserve.to_d_token_up(e, flash_loan.amount);
from_state.add_liabilities(e, &mut reserve, d_tokens_minted);
- reserve.require_utilization_below_max(e);
PoolEvents::flash_loan(
e,
flash_loan.asset.clone(),
from.clone(),
flash_loan.contract.clone(),
flash_loan.amount,
d_tokens_minted,
);
}
// ... snip ...
// store updated info to ledger
pool.store_cached_reserves(e);
from_state.store(e);
+ pool.load_reserve(e, &flash_loan.asset, false).require_utilization_below_max(e);
from_state.positions
}
markus_pl10 (Script3) confirmed
Validated this is an issue. It’s more of an implementation detail rather than an finding, as no funds or functionality is at risk.
Fixed to ensure flash loan is legal by checking flash loan under 100% util, then also check max util of asset during validation.
Commit f35271b to clean up utilization checks.
Status: Mitigation confirmed. Full details in reports from 0xAlix2, 0x007, Testerbot and oakcobalt.
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 utilization check is performed immediately after adding the flash loan liability, before the user's subsequent actions (including repayment) are processed.
Impact
Legitimate flash loans that would end with utilization below the maximum are blocked, preventing users from using the flash loan feature in such scenarios.
What the attacker needed
A pool with a reserve that has a max utilization ratio set; A user wanting to take a flash loan that temporarily…
The fix
The utilization check should be moved to after all requests are processed and the final state is stored, so that the end-of-transaction utilization is validated.
How it was found
- Set up a pool with a reserve that has a max utilization ratio.
- Call execute_submit_with_flash_loan with a flash loan amount that temporarily pushes utilization above the max.
- Include requests to supply collateral and repay the flash loan within the same transaction.
- Observe that the transaction reverts with error #1207 due to the early utilization check.
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 flash loan is a loan that must be borrowed and repaid within the same transaction. The pool enforces a maximum utilization ratio (the percentage of deposits that are borrowed). The code checks this limit too early, right after borrowing, so even if the user repays later in the same transaction, the check fails and the whole transaction is blocked.
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.