Skip to content
Business logic Audit finding Intermediate

Edge case breaks APR cap calculation and leads to excessive fee extraction from the pool

The fee vault's update_rate function uses integer division that can round the target growth rate down to 1.0 when the product of 100,000, target APR, and elapsed time is less than seconds per year. This causes the protocol to take 100% of any interest growth as fees, even when the growth is below the configured APR cap.

Sparrow Follow

Originally disclosed on Code4rena

Submitted by Sparrow

In the fee vault’s update_rate function, there’s an edge case that affects the calculation of target growth rate when the product of 100_000 * target_apr * time_elapsed is less than SECONDS_PER_YEAR (31,536,000). Due to integer division, this calculation results in 0, making the target growth rate exactly equal to SCALAR_12 (1.0).

This issue occurs under several conditions:

  • When time_elapsed is small (frequent updates)
  • When target_apr is low
  • Or when both conditions combine

When this happens, the target growth rate becomes 1.0 (no growth), which effectively means the protocol will take 100% of any growth as fees, contrary to the expected behavior of only taking growth above the APR cap.

Impact

The protocol wouldn’t function as intended, considering we now have the vault extracting 100% of the interest as fees, despite having a non-zero APR cap configured which directly contradicts the intended economic model and also means leak of value for the depositors since more funds are extracted from the pool (total_b_tokens).

Also this tends more to active reserves which would have frequent interaction with get_reserve_vault_updated() (which happens during deposits, withdrawals, or position queries) will trigger an update with small time intervals, causing the protocol to take all interest.

Proof of Concept

In fee-vault/src/reserve_vault.rs, the problematic calculation occurs in the update_rate method:

reserve_vault.rs#update_rate()

// Target growth rate scaled in 12 decimals =
// SCALAR_12 * (target_apr / SCALAR_7) * (time_elapsed / SECONDS_PER_YEAR) + SCALAR_12
let target_growth_rate =
    100_000 * target_apr * (time_elapsed as i128) / SECONDS_PER_YEAR + SCALAR_12;

let target_b_rate = self
    .b_rate
    .fixed_mul_ceil(target_growth_rate, SCALAR_12)
    .unwrap();

// If the target APR wasn't reached, no fees are accrued
if target_b_rate >= new_rate {
    0
} else {
    // calculate fees
}

For example, with a 1% APR cap (target_apr = 0_0100000) and a X-second interval between updates, we get:

100_000 * target_apr * X = 100_000Xtarget_apr... here `100_000Xtarget_apr` < `SECONDS_PER_YEAR`
100_000Xtarget_apr / 31,536,000 = 0 (integer division)
target_growth_rate = 0 + SCALAR_12 = SCALAR_12 (1.0)

This means the target growth rate becomes exactly 1.0 (no growth), and when calculating target_b_rate:

target_b_rate = self.b_rate * SCALAR_12 / SCALAR_12 = self.b_rate

Therefore, target_b_rate equals the old rate with no allowed growth.

The consequence is seen in the fee calculation. Since target_b_rate = self.b_rate, whenever new_rate > self.b_rate (i.e., any positive growth), the protocol will take 100% of the growth as fees, contrary to the expected behavior of only taking growth above the APR cap:

src/reserve_vault.rs#L96-L104:

            // If the target APR wasn't reached, no fees are accrued
            if target_b_rate >= new_rate {
                0
            } else {
                self.total_b_tokens
                    .fixed_mul_floor(new_rate - target_b_rate, new_rate)
                    .unwrap()
            }

Would also be key to note that any function that calls get_reserve_vault_updated() which in turn calls update_rate() will trigger this issue under the right conditions.

Coded POC

#[cfg(test)]
mod apr_capped_tests {
    use super::*;
    use crate::{
        storage::FeeMode,
        testutils::{assert_approx_eq_rel, mockpool, register_fee_vault, EnvTestUtils},
    };
-    use soroban_sdk::{testutils::{Address as _, , Address};
+    use soroban_sdk::{testutils::{Address as _, LedgerInfo, Ledger}, Address};

### ..snip

fn test_update_rate_broken() {
        let e = Env::default();
        e.mock_all_auths();

        let init_b_rate = 1_000_000_000_000; // 1.0 with 12 decimals
        let bombadil = Address::generate(&e);

        let mock_client = &mockpool::register_mock_pool_with_b_rate(&e, init_b_rate);

        // Set up vault with APR capped mode and a 1% APR cap
        let vault_address = register_fee_vault(
            &e,
            Some((
                bombadil.clone(),
                mock_client.address.clone(),
                true, // APR capped mode
                0_0100000, // 1% APR cap (0.01 with 7 decimals)
            )),
        );

        e.as_contract(&vault_address, || {
            // PART 1: Demonstrate the bug with a small time interval
            let mut reserve_vault = ReserveVault {
                address: Address::generate(&e),
                total_b_tokens: 1000_0000000,
                last_update_timestamp: e.ledger().timestamp(),
                total_shares: 1000_0000000,
                b_rate: init_b_rate,
                accrued_fees: 0,
            };

            // Let's set a small time interval (30 seconds)
            let initial_timestamp = e.ledger().timestamp();
            let small_time_interval = 30; // 30 seconds

            // Update the timestamp to be 30 seconds later
            e.ledger().set(LedgerInfo {
                timestamp: initial_timestamp + small_time_interval,
                protocol_version: 22,
                sequence_number: 100,
                network_id: [0; 32],
                base_reserve: 10,
                min_temp_entry_ttl: 10,
                min_persistent_entry_ttl: 10,
                max_entry_ttl: 3110400,
            });

            // CRITICAL: Set new b_rate showing 0.005% growth (annualized to ~0.53% APR)
            // This is BELOW the 1% APR cap, so NO fees should be taken
            let new_b_rate = 1_000_050_000_000; // 1.00005 with 12 decimals (0.005% growth)
            mockpool::set_b_rate(&e, mock_client, new_b_rate);

            // Calculate what growth should be allowed by the APR cap
            // 1% APR for 30 seconds = 1% * (30 / 31536000) = 0.0000095... which is very small
            // This is approximately 0.00095% growth allowed
            // Our actual growth is 0.005%, which is well UNDER the allowed growth
            // Therefore NO fees should be taken

            // But due to integer division in the target_growth_rate calculation:
            // target_growth_rate = target_apr * time_elapsed / seconds_in_year + 1e12
            // = 100_000 * 30 / 31_536_000 + 1e12
            // = 0 (due to integer division) + 1e12 = 1e12
            // target_b_rate = old_b_rate, meaning no growth is allowed

            // Before the update
            let before_total_b_tokens = reserve_vault.total_b_tokens;

            // Perform the update
            reserve_vault.update_rate(&e);

            // After the update
            let after_total_b_tokens = reserve_vault.total_b_tokens;
            let fees_taken = before_total_b_tokens - after_total_b_tokens;

            // Calculate what the fee would be if ALL growth is taken
            // 0.005% growth on 1000_0000000 tokens = 0.05 * 1_0000000 = 0_0500000 tokens
            // With properly working APR cap, the fees should be ZERO (since growth < APR cap)
            // But due to the precision loss, ALL growth is taken as fees

            // Assert that growth is taken as fees (bug case)
            // This is wrong because growth is under the APR cap
            assert!(fees_taken > 0);
            assert!(reserve_vault.accrued_fees > 0);
        });
    }
test reserve_vault::apr_capped_tests::test_update_rate_broken ... ok

successes:

---- reserve_vault::apr_capped_tests::test_update_rate_broken stdout ----
Writing test snapshot file for test "reserve_vault::apr_capped_tests::test_update_rate_broken" to "test_snapshots/reserve_vault/apr_capped_tests/test_update_rate_broken.1.json".

successes:
    reserve_vault::apr_capped_tests::test_update_rate_broken

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 43 filtered out; finished in 0.02s

Recommended mitigation steps

Revamp the fee accrual logic to take into account lower fee rates and/or frequent updates, since we can’t add a minimum time threshold for updates to prevent excessive fee extraction from very frequent small updates cause this then breaks the contracts as multiple transactions would fail on the call to get_reserve_vault_updated()

markus_pl10 (Script3) confirmed

mootz12 (Script3) commented:

Validated this is a finding. This is separate from S-353.

The interest math is likely not precise enough to handle small APRs / target APRs / and the minimum 5s update period, causing admin fees to not be withheld properly.

Note that this does appear to work for typical higher interest assets (USDC/EURC/etc).

LSDan (judge) decreased severity to Medium and commented:

Agree that it is distinct from S-353, primarily due to impact and origin of the issue. This tracks as a valid medium to me.

mootz12 (Script3) commented:

https://github.com/script3/fee-vault/pull/5/commits/a63d9a0c04fd7165ad7f49344faa0d60e0f85177

This won’t be fixed. Unit tests were added to ensure the edge cases behaved as expected. Essentially, there always can be cases where, due to rounding, the admin gets 0 fees.

This gets more likely the less b_tokens that are being held by the vault, with low interest rate / low IR cases being able to scrape admin fees within a 5s interval at 1,000 b_tokens.

Thus, this isn’t an exploitable issue, given spamming this does not cause any fund loss, only less fees get taken from the users pool of funds. If a user wants to deny an admin fees, they will be paying significantly more in TX fees than the total rounding loss for the admin, which they would only be eligible for part of.


The original report as published on Code4rena, by Sparrow. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

Integer division in the target growth rate calculation truncates to zero when the product of 100,000, target APR, and time elapsed is less than seconds per year, making the target growth rate equal to 1.0.

Impact

The protocol extracts 100% of interest as fees instead of only growth above the APR cap, causing a leak of value for depositors; however, the maintainer notes this is not exploitable for fund loss and only results in less fees being taken, with rounding losses being small.

What the attacker needed

APR capped mode enabled; Low target APR or frequent updates; Small time interval between updates

The fix

The maintainer stated this won't be fixed; unit tests were added to ensure edge cases behave as expected, and they consider it not exploitable.

How it was found

  1. Set up a fee vault with APR capped mode and a low APR cap (e.g., 1%).
  2. Set a small time interval (e.g., 30 seconds) between updates.
  3. Set a new b_rate showing growth below the APR cap.
  4. Call update_rate and observe that fees are taken despite growth being under the cap.

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 is supposed to take only the interest above a set annual percentage rate (APR) cap as a fee. Due to a rounding mistake in the code, when updates happen frequently or the APR is low, the vault thinks no growth is allowed and takes all the interest as a fee instead of just the excess.

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.

More by Sparrow

View all

1 more disclosed report, best first.

More from Blend V2 Audit + Certora Formal Verification

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.