Missing update_rz_emis_data calls in draw and donate functions lead to incorrect emissions distribution
The execute_draw and execute_donate functions modify the non_queued_tokens value without first updating the reward zone emissions data, causing the emissions index to be calculated with incorrect token amounts. This leads to unfair distribution of emissions: pools that have tokens drawn earn fewer emissions, while pools that receive donations earn more than they should.
0x007 Follow with 0xAlix2, adamIdarrha and 6 others
Originally disclosed on Code4rena
Submitted by 0x007, also found by 0xAlix2, adamIdarrha, carrotsmuggler, Kirkeelee, oakcobalt, rapid, Testerbot and Tigerfrake
Finding description
The non_queued_tokens value is a critical component in the calculation of reward zone emissions index. While most functions that modify this value (such as deposits and withdrawals) correctly call update_rz_emis_data before making changes, two important functions - execute_draw and execute_donate in the fund_management.rs - fail to update the emissions data before modifying non_queued_tokens.
This oversight creates an inconsistency in the protocol’s emissions accounting system, leading to unfair distribution of rewards.
Impact
This vulnerability affects the fair distribution of emissions across pools:
draw: Pools that have tokens drawn from them will unfairly earn fewer emissions than they deserve. This occurs because the emissions calculation would use the new (lower) token amount rather than the amount that was actually present during thedistributeperiod.donate: Pools that receive donations will unfairly earn more emissions than they should. This occurs because the emissions index update would use the new (higher) token amount rather than the amount at the time of the lastdistribute. This would also cause the total tokens of all pools to be less than balance.
Proof of Concept
Consider this scenario with two pools:
Initial state:
- PoolA: 100 tokens, 100 shares, 0 q4w
- PoolB: 100 tokens, 100 shares, 0 q4w
- Total
non_queued_tokens: 200 - New emissions to distribute: 10
- Emissions rate increase: 0.05 per token (10/200)
Normal case:
- Both pools should receive 5 emissions each (
0.05 * 100)
Exploited case:
- PoolB receives a donation of 50 tokens (increasing its tokens to 150)
- Since
donatedoesn’t callupdate_rz_emis_data, the emissions calculation uses 150 tokens instead of 100 - When
gulp_emissionsis called, PoolB receives 7.5 emissions (0.05 * 150) instead of the fair 5 emissions - This results in 2.5 emissions being unfairly distributed to PoolB
This test is modified from test_gulp_emissions
#[test]
fn test_gulp_emissions_after_donate() {
let e = Env::default();
e.cost_estimate().budget().reset_unlimited();
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,
});
let backstop = create_backstop(&e);
let emitter_distro_time = 1713139200 - 10;
let blnd_token_client = create_blnd_token(&e, &backstop, &Address::generate(&e)).1;
create_emitter(
&e,
&backstop,
&Address::generate(&e),
&Address::generate(&e),
emitter_distro_time,
);
let pool_1 = Address::generate(&e);
let pool_2 = Address::generate(&e);
let pool_3 = Address::generate(&e);
let reward_zone: Vec<Address> = vec![&e, pool_1.clone(), pool_2.clone(), pool_3.clone()];
// setup pool 1 to have ongoing emissions
let pool_1_emissions_data = BackstopEmissionData {
expiration: 1713139200 + 1000,
eps: 0_10000000000000,
index: 8877660000000,
last_time: 1713139200 - 12345,
};
// setup pool 2 to have expired emissions
let pool_2_emissions_data = BackstopEmissionData {
expiration: 1713139200 - 12345,
eps: 0_05000000000000,
index: 4532340000000,
last_time: 1713139200 - 12345,
};
// setup pool 3 to have no emissions
e.as_contract(&backstop, || {
storage::set_last_distribution_time(&e, &(emitter_distro_time - 7 * 24 * 60 * 60));
storage::set_reward_zone(&e, &reward_zone);
storage::set_backstop_emis_data(&e, &pool_1, &pool_1_emissions_data);
storage::set_rz_emis_data(
&e,
&pool_1,
&RzEmissionData {
index: 0,
accrued: 0,
},
);
storage::set_rz_emis_data(
&e,
&pool_2,
&RzEmissionData {
index: 0,
accrued: 0,
},
);
storage::set_rz_emis_data(
&e,
&pool_3,
&RzEmissionData {
index: 0,
accrued: 0,
},
);
storage::set_backstop_emis_data(&e, &pool_2, &pool_2_emissions_data);
storage::set_pool_balance(
&e,
&pool_1,
&PoolBalance {
tokens: 300_000_0000000,
shares: 200_000_0000000,
q4w: 0,
},
);
storage::set_pool_balance(
&e,
&pool_2,
&PoolBalance {
tokens: 200_000_0000000,
shares: 150_000_0000000,
q4w: 0,
},
);
storage::set_pool_balance(
&e,
&pool_3,
&PoolBalance {
tokens: 500_000_0000000,
shares: 600_000_0000000,
q4w: 0,
},
);
// diff 1, I don't see the point
// blnd_token_client.approve(&backstop, &pool_1, &100_123_0000000, &e.ledger().sequence());
distribute(&e);
// diff 2: donate
let user = Address::generate(&e);
let (backstop_token, backstop_token_client) = create_token(&e, &user);
storage::set_backstop_token(&e, &backstop_token);
e.mock_all_auths();
backstop_token_client.mint(&user, &300_000_0000000);
backstop_token_client.approve(&user, &backstop, &300_000_0000000, &e.ledger().sequence());
// this can be triggered from auction
execute_donate(&e, &user, &pool_1, 300_000_0000000);
gulp_emissions(&e, &pool_1);
gulp_emissions(&e, &pool_2);
gulp_emissions(&e, &pool_3);
assert_eq!(storage::get_last_distribution_time(&e), emitter_distro_time);
assert_eq!(
storage::get_pool_balance(&e, &pool_1).tokens,
// diff 3: pool balance has doubled
300_000_0000000 * 2
);
assert_eq!(
storage::get_pool_balance(&e, &pool_2).tokens,
200_000_0000000
);
assert_eq!(
storage::get_pool_balance(&e, &pool_3).tokens,
500_000_0000000
);
assert_eq!(
blnd_token_client.allowance(&backstop, &pool_1),
// diff 4: allowance should double 544320000000=154_555_0000000-100_123_0000000
// 154_555_0000000
544320000000 * 2
);
assert_eq!(
blnd_token_client.allowance(&backstop, &pool_2),
36_288_0000000
);
assert_eq!(
blnd_token_client.allowance(&backstop, &pool_3),
90_720_0000000
);
// validate backstop emissions
let new_pool_1_data = storage::get_backstop_emis_data(&e, &pool_1).unwrap_optimized();
// diff 5: eps approximately doubled from 0_21016534391534 to 0_42016534391534
assert_eq!(new_pool_1_data.eps, 0_42016534391534);
assert_eq!(new_pool_1_data.expiration, 1713139200 + 7 * 24 * 60 * 60);
assert_eq!(new_pool_1_data.index, 9494910000000);
assert_eq!(new_pool_1_data.last_time, 1713139200);
let new_pool_2_data = storage::get_backstop_emis_data(&e, &pool_2).unwrap_optimized();
assert_eq!(new_pool_2_data.eps, 0_14000000000000);
assert_eq!(new_pool_2_data.expiration, 1713139200 + 7 * 24 * 60 * 60);
assert_eq!(new_pool_2_data.index, 4532340000000);
assert_eq!(new_pool_2_data.last_time, 1713139200);
let new_pool_3_data = storage::get_backstop_emis_data(&e, &pool_3).unwrap_optimized();
assert_eq!(new_pool_3_data.eps, 0_35000000000000);
assert_eq!(new_pool_3_data.expiration, 1713139200 + 7 * 24 * 60 * 60);
assert_eq!(new_pool_3_data.index, 0);
assert_eq!(new_pool_3_data.last_time, 1713139200);
});
}
Recommended mitigation steps
Add calls to update_rz_emis_data at the beginning of both the execute_draw and execute_donate functions to ensure emissions calculations use the correct token amounts.
markus_pl10 (Script3) confirmed
Validated as a finding.
The impact of this is small, and is unlikely to impact actual claim ability in the long run. Backstop emissions data for pools is updated daily, if not more than daily, for all active Blend pools, and user’s don’t really have control over when
donateanddraware called, so attempting to time long emission gaps up to exploit this is not possible.However, this does result in an increased emissions output for the entire pool backstop, which does result in slightly higher emissions for users that expected. So the angle is there that if timing is lucky for a long period of time and lots of tokens are added through
donate, we could run into an issue where some users are unable to claim.Given this is possible and no user funds are at risk, a medium seems appropriate.
Agree that medium is more appropriate given the scale of funds lost.
Fixed; backstop emissions math was reverted to how it works in v1, alongside some optimizations.
- Thus, no
rz_emis_indexis tracked, and rather emissions are distributed duringdistributebased on the current state of the backstops.- Pools can still call
distributeimmediately after adonate, but given pool token balances>>donate amounts, this effect is small and therefore accepted, as it does not result in any way to inflate overall emissions.
Commit 77373e3 to remove
rz indexfrom backstop emissions.
Status: Mitigation confirmed. Full details in reports from 0x007, Testerbot, 0xAlix2 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 functions execute_draw and execute_donate modify non_queued_tokens without calling update_rz_emis_data beforehand, so the emissions index is updated using the post-change token amount rather than the amount at the last distribution.
Impact
Pools that have tokens drawn may earn fewer emissions, and pools that receive donations may earn more emissions than they should, leading to unfair distribution; the impact is small and unlikely to affect claim ability in the long run, but could cause some users to be unable to claim if timing is lucky over a long period with large donations.
What the attacker needed
A pool in the reward zone; ability to call donate or draw; timing of emissions distribution
The fix
The issue was fixed by reverting backstop emissions math to how it works in v1, removing the rz_emis_index and distributing emissions during distribute based on the current state of the backstops.
How it was found
- Set up two pools with equal token balances and shares.
- Call distribute to establish the emissions rate.
- Donate tokens to one pool via execute_donate, which increases its token balance without updating emissions data.
- Call gulp_emissions for both pools.
- Observe that the pool receiving the donation earns more emissions than it should, while the other pool earns less.
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.
In this system, rewards (emissions) are shared among pools based on how many tokens each pool holds. The code is supposed to record the token balance before it changes, so the reward calculation uses the correct amount. Two functions that change the balance (draw and donate) skip that recording step, so the reward calculation uses the new balance instead of the old one, giving some pools too many or too few rewards.
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.