From f044a31bc569effb80f2521bb8e7c92930758d1f Mon Sep 17 00:00:00 2001 From: Mike MacCana Date: Tue, 10 Nov 2026 16:38:52 +0000 Subject: [PATCH] lending: make the share-inflation test run its attack inflating_shares_through_own_borrow_does_not_pay borrowed from a reserve with the default 10% reserve factor. The fee rounds up, so the program took the one minor unit of interest as its fee and suppliers gained nothing: after the borrow the reserve held 1,001 units against 1,001 shares, and after the ratchet loop it still did. The test passed without the share price ever moving. The borrow reserve now has a zero reserve factor, and the test asserts that the share price rises after the borrow and again after the round trips (1,002 then 1,066 units in anchor-v1). With the 10% factor restored, the new assertions fail. The attacker still ends with less than they started with, and the victim still gets back at least 99.9% of the deposit. Applied to anchor-v1, anchor and quasar. Claude-Session: https://claude.ai/code/session_01GZHin6BQpAqf7j2ucSZWkx --- finance/lending/anchor-v1/CHANGELOG.md | 11 ++++++ .../lending/tests/test_deposit_redeem.rs | 23 +++++++++-- finance/lending/anchor/CHANGELOG.md | 11 ++++++ .../lending/tests/test_deposit_redeem.rs | 23 +++++++++-- finance/lending/quasar/CHANGELOG.md | 11 ++++++ finance/lending/quasar/src/tests.rs | 38 +++++++++++++++---- 6 files changed, 103 insertions(+), 14 deletions(-) diff --git a/finance/lending/anchor-v1/CHANGELOG.md b/finance/lending/anchor-v1/CHANGELOG.md index 6e55563b..2f47f8b8 100644 --- a/finance/lending/anchor-v1/CHANGELOG.md +++ b/finance/lending/anchor-v1/CHANGELOG.md @@ -1,5 +1,16 @@ # Changelog +## Unreleased (2026-11-10) + +`inflating_shares_through_own_borrow_does_not_pay` now runs its attack. The +borrow reserve used the default 10% reserve factor, which rounds its fee up, so +the program took the attacker's one minor unit of interest as its fee: the share +price never rose, the ratchet loop never moved it, and the test's assertions +held without the attack happening. The borrow reserve now has a zero reserve +factor, the attacker's best case, and the test asserts that the share price +rises after the borrow and again after the round trips. With a 10% reserve +factor the new assertion fails. + ## Unreleased (2026-10-07) Round interest against the borrower. Every debt is `borrowed_principal` diff --git a/finance/lending/anchor-v1/programs/lending/tests/test_deposit_redeem.rs b/finance/lending/anchor-v1/programs/lending/tests/test_deposit_redeem.rs index 7b52d25a..72501c64 100644 --- a/finance/lending/anchor-v1/programs/lending/tests/test_deposit_redeem.rs +++ b/finance/lending/anchor-v1/programs/lending/tests/test_deposit_redeem.rs @@ -2,6 +2,7 @@ mod common; use common::{default_config, Env}; use lending::constants::MINIMUM_SHARES; +use lending::state::ReserveConfig; use solana_kite::mint_tokens_to_token_account; /// The first deposit mints shares one-for-one, less the `MINIMUM_SHARES` @@ -133,8 +134,13 @@ fn sole_supplier_leaves_the_minimum_behind() { /// victim is not robbed outright; instead a deposit just under two shares' /// worth mints one, and the attacker's share redeems half the pool. /// -/// The rate curve is the default one. Nothing about the attack needs the -/// market owner's cooperation or bad debt: the attacker repays what they owe. +/// The rate curve is the default one, with the reserve factor at zero. A +/// nonzero reserve factor rounds its fee up, so the program would take the one +/// unit of interest as its fee and the share price would not move; zero is the +/// attacker's best case. The test asserts that the share price rises after the +/// borrow and again after the round trips, so the attack does run. Nothing about +/// the attack needs the market owner's cooperation or bad debt: the attacker +/// repays what they owe. /// /// `MINIMUM_SHARES` counts as shares nobody holds in every share conversion, /// so the attacker's one share is 1 of 1_001 and whatever the rounding leaves @@ -142,7 +148,11 @@ fn sole_supplier_leaves_the_minimum_behind() { #[test] fn inflating_shares_through_own_borrow_does_not_pay() { let mut env = Env::new(); - let usdc = env.add_empty_reserve(6, common::dollars(1), default_config()); + let no_reserve_factor = ReserveConfig { + reserve_factor_bps: 0, + ..default_config() + }; + let usdc = env.add_empty_reserve(6, common::dollars(1), no_reserve_factor); let collateral = env.add_reserve(6, common::dollars(1), default_config()); let attacker = env.create_user(); @@ -169,6 +179,10 @@ fn inflating_shares_through_own_borrow_does_not_pay() { env.warp_seconds(1); env.refresh_reserve_only(&attacker, &usdc); assert_eq!(env.reserve(&usdc).current_borrowed_amount().unwrap(), 2); + // The extra unit of interest went to suppliers, so a share is now worth + // more than one minor unit. + let after_borrow = env.reserve(&usdc).total_liquidity().unwrap(); + assert!(after_borrow > env.reserve(&usdc).total_shares().unwrap()); // Ratchet until one share is worth more than half the victim's deposit. let victim_deposit: u64 = 1_000_000_000; @@ -186,6 +200,9 @@ fn inflating_shares_through_own_borrow_does_not_pay() { env.try_redeem(&attacker, &usdc, 1).unwrap(); } + // The round trips raised the share price further. + assert!(env.reserve(&usdc).total_liquidity().unwrap() > after_borrow); + let victim = env.create_user(); let victim_liquidity = env.fund(&victim, usdc.mint, victim_deposit); let victim_share = env.supply(&victim, &usdc, victim_deposit); diff --git a/finance/lending/anchor/CHANGELOG.md b/finance/lending/anchor/CHANGELOG.md index 83c233b1..d30970cc 100644 --- a/finance/lending/anchor/CHANGELOG.md +++ b/finance/lending/anchor/CHANGELOG.md @@ -1,5 +1,16 @@ # Changelog +## Unreleased (2026-11-10) + +`inflating_shares_through_own_borrow_does_not_pay` now runs its attack. The +borrow reserve used the default 10% reserve factor, which rounds its fee up, so +the program took the attacker's one minor unit of interest as its fee: the share +price never rose, the ratchet loop never moved it, and the test's assertions +held without the attack happening. The borrow reserve now has a zero reserve +factor, the attacker's best case, and the test asserts that the share price +rises after the borrow and again after the round trips. With a 10% reserve +factor the new assertion fails. + ## Unreleased (2026-10-07) Round interest against the borrower. Every debt is `borrowed_principal` diff --git a/finance/lending/anchor/programs/lending/tests/test_deposit_redeem.rs b/finance/lending/anchor/programs/lending/tests/test_deposit_redeem.rs index 51573aa5..97c5343c 100644 --- a/finance/lending/anchor/programs/lending/tests/test_deposit_redeem.rs +++ b/finance/lending/anchor/programs/lending/tests/test_deposit_redeem.rs @@ -3,6 +3,7 @@ mod common; use common::{default_config, Env}; use lending::constants::MINIMUM_SHARES; use lending::errors::LendingError; +use lending::state::ReserveConfig; use solana_kite::mint_tokens_to_token_account; /// The first deposit mints shares one-for-one, less the `MINIMUM_SHARES` @@ -131,8 +132,13 @@ fn sole_supplier_leaves_the_minimum_behind() { /// victim is not robbed outright; instead a deposit just under two shares' /// worth mints one, and the attacker's share redeems half the pool. /// -/// The rate curve is the default one. Nothing about the attack needs the -/// market owner's cooperation or bad debt: the attacker repays what they owe. +/// The rate curve is the default one, with the reserve factor at zero. A +/// nonzero reserve factor rounds its fee up, so the program would take the one +/// unit of interest as its fee and the share price would not move; zero is the +/// attacker's best case. The test asserts that the share price rises after the +/// borrow and again after the round trips, so the attack does run. Nothing about +/// the attack needs the market owner's cooperation or bad debt: the attacker +/// repays what they owe. /// /// `MINIMUM_SHARES` counts as shares nobody holds in every share conversion, /// so the attacker's one share is 1 of 1_001 and whatever the rounding leaves @@ -140,7 +146,11 @@ fn sole_supplier_leaves_the_minimum_behind() { #[test] fn inflating_shares_through_own_borrow_does_not_pay() { let mut env = Env::new(); - let usdc = env.add_empty_reserve(6, common::dollars(1), default_config()); + let no_reserve_factor = ReserveConfig { + reserve_factor_bps: 0, + ..default_config() + }; + let usdc = env.add_empty_reserve(6, common::dollars(1), no_reserve_factor); let collateral = env.add_reserve(6, common::dollars(1), default_config()); let attacker = env.create_user(); @@ -167,6 +177,10 @@ fn inflating_shares_through_own_borrow_does_not_pay() { env.warp_seconds(1); env.refresh_reserve_only(&attacker, &usdc); assert_eq!(env.reserve(&usdc).current_borrowed_amount().unwrap(), 2); + // The extra unit of interest went to suppliers, so a share is now worth + // more than one minor unit. + let after_borrow = env.reserve(&usdc).total_liquidity().unwrap(); + assert!(after_borrow > env.reserve(&usdc).total_shares().unwrap()); // Ratchet until one share is worth more than half the victim's deposit. let victim_deposit: u64 = 1_000_000_000; @@ -184,6 +198,9 @@ fn inflating_shares_through_own_borrow_does_not_pay() { env.try_redeem(&attacker, &usdc, 1).unwrap(); } + // The round trips raised the share price further. + assert!(env.reserve(&usdc).total_liquidity().unwrap() > after_borrow); + let victim = env.create_user(); let victim_liquidity = env.fund(&victim, usdc.mint, victim_deposit); let victim_share = env.supply(&victim, &usdc, victim_deposit); diff --git a/finance/lending/quasar/CHANGELOG.md b/finance/lending/quasar/CHANGELOG.md index bb13909f..eb30441a 100644 --- a/finance/lending/quasar/CHANGELOG.md +++ b/finance/lending/quasar/CHANGELOG.md @@ -1,5 +1,16 @@ # Changelog +## Unreleased (2026-11-10) + +`inflating_shares_through_own_borrow_does_not_pay` now runs its attack. The +borrow reserve used the default 10% reserve factor, which rounds its fee up, so +the program took the attacker's one minor unit of interest as its fee: the share +price never rose, the ratchet loop never moved it, and the test's assertions +held without the attack happening. The borrow reserve now has a zero reserve +factor, the attacker's best case, and the test asserts that the share price +rises after the borrow and again after the round trips. With a 10% reserve +factor the new assertion fails. + ## [Unreleased] (2026-10-07) ### Changed diff --git a/finance/lending/quasar/src/tests.rs b/finance/lending/quasar/src/tests.rs index 91a5303d..dbcd7557 100644 --- a/finance/lending/quasar/src/tests.rs +++ b/finance/lending/quasar/src/tests.rs @@ -1662,16 +1662,17 @@ mod clock_warp { vault: Pubkey, share: Pubkey, price: Pubkey, + reserve_factor_bps: u16, ) { // 75% LTV, 80% liquidation threshold, 5% bonus, 50% close factor, - // 10% reserve factor, kink 80%, 2% / 20% / 150% APR curve, and a - // 1% confidence limit on the price feed. + // the given reserve factor, kink 80%, 2% / 20% / 150% APR curve, + // and a 1% confidence limit on the price feed. let config: [u16; 10] = [ 7_500, 8_000, 500, 5_000, - 1_000, + reserve_factor_bps, 8_000, 200, 2_000, @@ -1705,8 +1706,17 @@ mod clock_warp { } /// Create the market and both reserves with no deposits, for tests of - /// the first deposit itself. + /// the first deposit itself. Both reserves keep a 10% reserve factor. fn setup_empty_markets(&mut self) { + self.setup_empty_markets_with_borrow_reserve_factor(1_000); + } + + /// As `setup_empty_markets`, with the borrow reserve keeping + /// `borrow_reserve_factor_bps` of interest as fees. + fn setup_empty_markets_with_borrow_reserve_factor( + &mut self, + borrow_reserve_factor_bps: u16, + ) { self.init_market(); self.set_price(COLLATERAL_MINT, self.collateral_price, dollars(1)); self.set_price(BORROW_MINT, self.borrow_price, dollars(1)); @@ -1716,6 +1726,7 @@ mod clock_warp { self.collateral_vault, self.collateral_share_mint, self.collateral_price, + 1_000, ); self.initialize_reserve( BORROW_MINT, @@ -1723,6 +1734,7 @@ mod clock_warp { self.borrow_vault, self.borrow_share_mint, self.borrow_price, + borrow_reserve_factor_bps, ); } @@ -2426,9 +2438,13 @@ mod clock_warp { /// instead a deposit just under two shares' worth mints one, and the /// attacker's share redeems half the pool. /// - /// The rate curve is the harness's usual one. Nothing about the attack - /// needs the market owner's cooperation or bad debt: the attacker repays - /// what they owe. + /// The rate curve is the harness's usual one, with the borrow reserve's + /// reserve factor at zero. A nonzero reserve factor rounds its fee up, so + /// the program would take the one unit of interest as its fee and the share + /// price would not move; zero is the attacker's best case. The test asserts + /// that the share price rises after the borrow and again after the round + /// trips, so the attack does run. Nothing about the attack needs the market + /// owner's cooperation or bad debt: the attacker repays what they owe. /// /// `MINIMUM_SHARES` counts as shares nobody holds in every share /// conversion, so the attacker's one share is 1 of 1_001 and whatever the @@ -2437,7 +2453,7 @@ mod clock_warp { #[test] fn inflating_shares_through_own_borrow_does_not_pay() { let mut world = World::new(); - world.setup_empty_markets(); + world.setup_empty_markets_with_borrow_reserve_factor(0); world.open_collateral_reserve(); let budget = world.tokens(ATTACKER_BORROW); @@ -2486,6 +2502,10 @@ mod clock_warp { ) .unwrap(); assert_eq!(debt, 2); + // The extra unit of interest went to suppliers, so a share is now worth + // more than one minor unit. + let (after_borrow, shares) = world.borrow_reserve_totals(); + assert!(after_borrow > shares); // Ratchet until one share is worth more than half the victim's deposit. let victim_deposit = world.tokens(VICTIM_BORROW); @@ -2508,6 +2528,8 @@ mod clock_warp { .redeem(ATTACKER, ATTACKER_BORROW, ATTACKER_BORROW_SHARE, 1) .assert_success(); } + // The round trips raised the share price further. + assert!(world.borrow_reserve_totals().0 > after_borrow); world .supply(VICTIM, VICTIM_BORROW, VICTIM_BORROW_SHARE, victim_deposit)