Skip to content
DoS Audit finding Intermediate

Permanent protocol brick via reserve ID exhaustion — drop reserve retires IDs without recycling, causing irreversible init reserve DoS after 64 cumulative markets

The protocol assigns each new market a unique ID from a counter that only increases, and dropping a market retires its ID without recycling it. After 64 cumulative markets, the counter reaches the hard limit and any new market creation permanently fails, even if many markets were dropped.

starrohan Follow with CarlosMB, eyad_404 and 4 others

Originally disclosed on Code4rena

Submitted by starrohan, also found by CarlosMB, eyad_404, kodkirmizi, Krace, thrasos, and wuji

  • contracts/kinetic-router/src/storage.rs #L355-L366
  • contracts/kinetic-router/src/reserve.rs #L471-L475
  • contracts/shared/src/utils.rs #L265-L270
  • contracts/shared/src/constants.rs #L42

K2’s reserve management uses a monotonically increasing NEXT_RESERVE_ID counter to assign unique IDs to each new market. The UserConfiguration bitmap (u128) allocates 2 bits per reserve (one for collateral, one for borrowing), creating a hard architectural ceiling of 64 reserves. When drop_reserve delists a market, the reserve’s data is cleaned up but its ID is permanently retired — the counter is never decremented and the ID slot is never recycled. Once the cumulative number of reserves ever created reaches 64 (regardless of how many were dropped), init_reserve will hard-panic, permanently bricking the protocol.

Vulnerability Details

increment_and_get_reserve_id only increments — it never decrements or recycles dropped IDs (storage.rs#L355-L366):

pub fn increment_and_get_reserve_id(env: &Env) -> u32 {
    let current = get_next_reserve_id(env);
    let next = current.checked_add(1).unwrap_or_else(|| {
        panic_with_error!(env, KineticRouterError::MathOverflow)
    });
    env.storage().persistent().set(&NEXT_RESERVE_ID, &next);
    current
}

drop_reserve removes all reserve data but explicitly retires the ID without making it available for reuse (reserve.rs#L471-L475):

// Reserve can be dropped but ID is permanently retired (never reused)
let reserve_id = _reserve_data.id;
storage::remove_reserve_data(&env, &asset);
storage::remove_reserve_from_list(&env, &asset)?;
storage::remove_reserve_address_by_id(&env, reserve_id);

The developers are explicitly aware this is the current behavior. calculation.rs#L128-L129 states:

// EFF-03: Must use next_reserve_id, NOT reserves_count, because reserve IDs are never
// reused after drop_reserve. Using count would skip higher IDs when gaps exist.

safe_reserve_id enforces the hard ceiling with an unrecoverable panic (utils.rs#L265-L270):

pub fn safe_reserve_id(env: &soroban_sdk::Env, id: u32) -> u8 {
    if id >= 64 {
        panic_with_error!(env, crate::KineticRouterError::MathOverflow);
    }
    id as u8
}

This function is called during init_reserve → first supply → set_using_as_collateral. Once NEXT_RESERVE_ID reaches 64, every future init_reserve will eventually trigger a hard panic when users try to interact with the new reserve.

The hard limit is defined in constants.rs#L42:

pub const MAX_RESERVES: u32 = 64;

There is no admin function to reset NEXT_RESERVE_ID, compact the bitmap, or recycle retired IDs. Once 64 reserves have cumulatively existed, new market creation is permanently bricked.

Impact

Permanent, irreversible Denial of Service on init_reserve:

  1. K2 is designed for RWA and AI agent markets — these are inherently experimental assets with high listing/delisting churn. RWA tokens expire, default, or get deprecated regularly.
  2. After enough list/delist cycles, the protocol permanently loses the ability to add new markets — even if only a handful of markets are actually active.
  3. The only fix requires deploying a V2 protocol and migrating all TVL (depositors, borrowers, liquidation infrastructure), creating significant liquidity risk during the migration window.
  4. There is no admin-level workaround — the NEXT_RESERVE_ID counter and UserConfiguration bitmap are architecturally coupled.

Scenario

  1. K2 DAO lists 20 experimental RWA markets over 2 years (IDs 0–19)
  2. 10 of those RWA tokens fail → admin calls drop_reserve on each (IDs 0–9 retired, counter stays at 20)
  3. 30 more markets listed over the next 3 years (IDs 20–49), 10 more dropped (counter stays at 50)
  4. 14 more markets listed (IDs 50–63) — counter reaches 64
  5. init_reserve permanently panics. Only ~44 markets are active, but no new markets can ever be added.

Recommended Mitigation

Implement a free-list to recycle retired IDs in drop_reserve:

const FREE_RESERVE_IDS: Symbol = symbol_short!("FREEIDS");

pub fn recycle_reserve_id(env: &Env, id: u32) {
    let mut free_ids: Vec<u32> = env.storage().persistent()
        .get(&FREE_RESERVE_IDS).unwrap_or(Vec::new(env));
    free_ids.push_back(id);
    env.storage().persistent().set(&FREE_RESERVE_IDS, &free_ids);
}

pub fn allocate_reserve_id(env: &Env) -> u32 {
    let mut free_ids: Vec<u32> = env.storage().persistent()
        .get(&FREE_RESERVE_IDS).unwrap_or(Vec::new(env));
    if let Some(recycled) = free_ids.pop_back() {
        env.storage().persistent().set(&FREE_RESERVE_IDS, &free_ids);
        return recycled;
    }
    increment_and_get_reserve_id(env)
}

Call recycle_reserve_id(env, reserve_id) inside drop_reserve, and replace increment_and_get_reserve_id with allocate_reserve_id in init_reserve.

Proof of Concept

Add the following test to tests/c4/src/lib.rs. It demonstrates that after 64 cumulative init_reserve calls (even if all reserves are dropped in between), the 65th call permanently fails.

#[test]
fn test_reserve_id_exhaustion_dos() {
    let env = Env::default();
    let setup = Setup::new(&env);

    // Phase 1: Create 32 reserves and drop all of them.
    // Each init_reserve increments NEXT_RESERVE_ID.
    // Each drop_reserve does NOT decrement it — the ID is permanently retired.
    let mut assets_to_drop: Vec<Address> = Vec::new();

    for _ in 0..32u32 {
        let asset = env.register_stellar_asset_contract_v2(setup.admin.clone());
        let params = default_init_reserve_params();
        setup.configurator.init_reserve(
            &setup.admin,
            &asset.address(),
            &setup.a_token_wasm,
            &setup.debt_token_wasm,
            &setup.interest_strategy,
            &setup.treasury,
            &params,
        );
        assets_to_drop.push(asset.address());
    }

    // Drop all 32 reserves — IDs 0..31 are permanently retired
    // NEXT_RESERVE_ID remains at 32
    for asset in &assets_to_drop {
        setup.configurator.drop_reserve(&setup.admin, asset);
    }

    // Phase 2: Create 32 more reserves (IDs 32..63)
    for _ in 0..32u32 {
        let asset = env.register_stellar_asset_contract_v2(setup.admin.clone());
        let params = default_init_reserve_params();
        setup.configurator.init_reserve(
            &setup.admin,
            &asset.address(),
            &setup.a_token_wasm,
            &setup.debt_token_wasm,
            &setup.interest_strategy,
            &setup.treasury,
            &params,
        );
    }

    // At this point: NEXT_RESERVE_ID == 64
    // Only 32 reserves are active, but the ID counter doesn't care about drops

    // Phase 3: Try to create the 65th cumulative reserve — PANICS
    let final_asset = env.register_stellar_asset_contract_v2(setup.admin.clone());
    let params = default_init_reserve_params();

    // This will panic with MathOverflow in safe_reserve_id(64)
    // when the first user tries to supply to this reserve:
    //   set_using_as_collateral(safe_reserve_id(64), true)
    //   -> safe_reserve_id: if id >= 64 { panic!(MathOverflow) }
    let result = std::panic::catch_unwind(|| {
        setup.configurator.init_reserve(
            &setup.admin,
            &final_asset.address(),
            &setup.a_token_wasm,
            &setup.debt_token_wasm,
            &setup.interest_strategy,
            &setup.treasury,
            &params,
        );
    });

    assert!(
        result.is_err(),
        "EXPLOIT CONFIRMED: init_reserve permanently bricked after 64 cumulative reserves"
    );
}

What the test proves

  1. Phase 1: 32 reserves are created (IDs 0–31) then all dropped. NEXT_RESERVE_ID = 32. Zero active reserves.
  2. Phase 2: 32 more reserves are created (IDs 32–63). NEXT_RESERVE_ID = 64. 32 active reserves.
  3. Phase 3: Attempting to create one more reserve panics in safe_reserve_id(64) because 64 >= MAX_RESERVES.
  4. Result: The protocol has only 32 active markets but can never add another one. init_reserve is permanently bricked. The only recovery path is deploying an entirely new protocol version and migrating all TVL.

Key code path that triggers the panic

init_reserve()
  → storage::increment_and_get_reserve_id()  // returns 64
  → ReserveData { id: 64, ... }
  → (later, on first supply to this reserve)
  → supply() → set_using_as_collateral()
  → safe_reserve_id(64)
  → if id >= 64 { panic_with_error!(MathOverflow) }  // PERMANENT BRICK

The original report as published on Code4rena, by starrohan. 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

Likely the reserve ID counter is never decremented or recycled when a reserve is dropped, so the cumulative count of reserves ever created eventually exceeds the hard architectural limit of 64.

Impact

After 64 cumulative reserves, init_reserve permanently panics, preventing any new markets from being added; the only recovery is deploying a new protocol version and migrating all funds.

What the attacker needed

Admin lists and drops markets repeatedly; cumulative reserve count reaches 64

The fix

Implement a free-list to recycle retired IDs in drop_reserve and use an allocate_reserve_id function that reuses recycled IDs before incrementing the counter.

How it was found

  1. Create 32 reserves, then drop all of them; the ID counter remains at 32.
  2. Create 32 more reserves, bringing the counter to 64.
  3. Attempt to create a 65th cumulative reserve, which panics in safe_reserve_id because the ID is 64 or higher.

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.

Each new market gets a number from a counter that only goes up. When a market is removed, its number is never given to a new market. After 64 markets have ever been created, the counter hits a fixed limit and the system refuses to create any more markets, even if most of the old ones were removed.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More from K2

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.