Removal of pool from reward zone does not allow gulping emissions which were already distributed in the past
YouCrossTheLineAlfie Follow with 0xadrii, monrel and 1 other
Originally disclosed on Code4rena
Submitted by YouCrossTheLineAlfie, also found by 0xadrii, monrel, and rscodes
Finding description and impact
The BackstopContract::distribute is used to update the backstop with new emissions for all the reward zone pools.
Then, calling the BackstopContract::gulp_emissions with a specific pool address will gulp emissions in the ratio of 70% to the backstop and 30% to the pool.
A pool can be removed using the BackstopContract::remove_reward function if the threshold requirements are not met.
pub fn remove_from_reward_zone(e: &Env, to_remove: Address) {
let mut reward_zone = storage::get_reward_zone(e);
// ensure to_add has met the minimum backstop deposit threshold
// NOTE: "to_add" can only carry a pool balance if it is a deployed pool from the factory
let pool_data = load_pool_backstop_data(e, &to_remove);
if require_pool_above_threshold(&pool_data) { <<@ -- // Checks if a pool is above threshold and reverts
panic_with_error!(e, BackstopError::BadRequest);
} else {
remove_pool(e, &mut reward_zone, &to_remove);
storage::set_reward_zone(e, &reward_zone);
}
}
However, while removing the pool, the remove_pool sets the emission index to i128::MAX without allowing to claim the emissions via BackstopContract::gulp_emissions which were already distributed.
/// Remove a pool from the reward zone and set the backstop emissions index to i128::MAX
fn remove_pool(e: &Env, reward_zone: &mut Vec<Address>, to_remove: &Address) {
let to_remove_index = reward_zone.first_index_of(to_remove.clone());
match to_remove_index {
Some(idx) => {
// . . . Rest of the code . . .
// update backstop emissions for the pool before removing it from the reward zone
// set emission index to i128::MAX to prevent further emissions
let to_remove_emis_data = storage::get_rz_emis_data(e, &to_remove).unwrap_optimized();
set_rz_emissions(e, &to_remove, i128::MAX, to_remove_emis_data.accrued, false); <<@ -- // sets i128::MAX as the index
reward_zone.remove(idx);
}
None => panic_with_error!(e, BackstopError::InvalidRewardZoneEntry),
}
}
This would fail the BackstopContract::gulp_emissions call due to an overflow not allowing the rightful emissions to the pool and the backstop.
pub fn gulp_emissions(e: &Env, pool: &Address) -> (i128, i128) {
let pool_balance = storage::get_pool_balance(e, pool);
let new_emissions = update_rz_emis_data(e, pool, true); <<@ - // This call would revert
// . . . Rest of the code . . .
return (0, 0);
}
The new emissions variable would overflow here as gulp_index is valid amount but the emission_data.index is set as i128::MAX whose difference will get multiplied by SCALAR_14, hence overflowing the signed 128 bit integer.
pub fn update_rz_emis_data(e: &Env, pool: &Address, to_gulp: bool) -> i128 {
if let Some(emission_data) = storage::get_rz_emis_data(e, pool) {
let pool_balance = storage::get_pool_balance(e, pool);
let gulp_index = storage::get_rz_emission_index(e);
let mut accrued = emission_data.accrued;
if emission_data.index < gulp_index || to_gulp {
if pool_balance.non_queued_tokens() > 0 {
let new_emissions = pool_balance
.non_queued_tokens()
.fixed_mul_floor(gulp_index - emission_data.index, SCALAR_14) <<@ -- // Overflows here
.unwrap_optimized();
accrued += new_emissions;
return set_rz_emissions(e, pool, gulp_index, accrued, to_gulp);
} else {
return set_rz_emissions(e, pool, gulp_index, accrued, to_gulp);
}
}
}
return 0;
}
P.S: The distribute call was made prior to the pool being under threshold or simply some other pool got higher deposits than current.
Impact
- Loss of funds for the users and pool as the emissions were distributed before removal from reward zone, but the gulping of them is not allowed. There’s a possibility that the pool will never get added to the reward zone as pools are essentially competing with each other for a place in reward zone.
- Temporary funds stuck for the pool if there’s a chance it is added back to the reward zone.
Proof of Concept
The below test case was added inside blend-contracts-v2/backstop/src/emissions/manager.rs file:
#[test]
#[should_panic]
fn test_remove_from_rz_failing_gulp() {
let e = Env::default();
e.ledger().set(LedgerInfo {
timestamp: 1713139200,
protocol_version: 22,
sequence_number: 0,
network_id: Default::default(),
base_reserve: 10,
min_temp_entry_ttl: 10,
min_persistent_entry_ttl: 10,
max_entry_ttl: 3110400,
});
e.mock_all_auths();
let bombadil = Address::generate(&e);
let backstop_id = create_backstop(&e);
let to_remove = Address::generate(&e);
let (blnd_id, _) = create_blnd_token(&e, &backstop_id, &bombadil);
let (usdc_id, _) = create_usdc_token(&e, &backstop_id, &bombadil);
create_comet_lp_pool_with_tokens_per_share(
&e,
&backstop_id,
&bombadil,
&blnd_id,
5_0000000,
&usdc_id,
0_1000000,
);
let mut reward_zone: Vec<Address> = vec![
&e,
Address::generate(&e),
to_remove.clone(), // index 7
];
e.as_contract(&backstop_id, || {
storage::set_reward_zone(&e, &reward_zone);
storage::set_last_distribution_time(&e, &(1713139200 - 1 * 24 * 60 * 60));
storage::set_pool_balance(
&e,
&to_remove,
&PoolBalance {
shares: 90_000_0000000,
tokens: 100_001_0000000,
q4w: 1_000_0000000,
},
);
storage::set_pool_balance(
&e,
&to_remove,
&PoolBalance {
shares: 35_000_0000000,
tokens: 40_000_0000000,
q4w: 1_000_0000000,
},
);
storage::set_backstop_emis_data(
&e,
&to_remove,
&BackstopEmissionData {
eps: 0_10000000000000,
expiration: 1713139200 + 1000,
index: 0,
last_time: 1713139200 - 12345,
},
);
storage::set_rz_emis_data(&e, &to_remove, {
&RzEmissionData {
index: 1234 * SCALAR_7,
accrued: 0,
}
});
storage::set_rz_emission_index(&e, &(5678 * SCALAR_7));
remove_from_reward_zone(&e, to_remove.clone());
let actual_rz = storage::get_reward_zone(&e);
reward_zone.remove(1);
assert_eq!(actual_rz.len(), 1);
assert_eq!(actual_rz, reward_zone);
// gulp emissions after removal
let ( fi, si ) = gulp_emissions(&e, &to_remove.clone());
});
}
As we can infer, the function reverts as expected.
Recommended mitigation steps
It is recommended to gulp the emissions before removing the pool:
pub fn add_to_reward_zone(e: &Env, to_add: Address, to_remove: Option<Address>) {
// . . . Rest of the code . . .
if MAX_RZ_SIZE > reward_zone.len() {
// there is room in the reward zone. Add "to_add".
reward_zone.push_front(to_add.clone());
} else {
match to_remove {
None => panic_with_error!(e, BackstopError::RewardZoneFull),
Some(to_remove) => {
// Verify "to_add" has a higher backstop deposit that "to_remove"
if pool_data.tokens <= storage::get_pool_balance(e, &to_remove).tokens {
panic_with_error!(e, BackstopError::InvalidRewardZoneEntry);
}
+ gulp_emissions(e, &to_remove);
remove_pool(e, &mut reward_zone, &to_remove);
reward_zone.push_front(to_add.clone());
}
}
}
// . . . Rest of the code . . .
}
markus_pl10 (Script3) confirmed
Commit 6204dba to improve reward zone changes emissions impact.
Status: Mitigation confirmed. Full details in reports from Testerbot, oakcobalt, 0xAlix2 and 0x007.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A medium-severity business logic bug in Blend V2 Audit + Certora Formal Verification. Code4rena made it public on 13 August 2025. Found in the 2025-02-blend-v2-audit-certora-formal-verification audit contest (finding M-16), also found by 3 other wardens.
About Business logic
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
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.