get_supply_ratio() compares raw shares to normal tokens without exchange price conversion, over-allocating yield to with-interest suppliers, leading to insolvency
The supply and borrow ratio functions compared with-interest amounts stored as raw shares against interest-free amounts stored in normal token units, without converting shares to tokens. As the exchange price drifted above its initial value, this mis-measurement over-allocated yield to with-interest suppliers, letting them withdraw more than the reserve's yield justified and creating an insolvency gap.
hackhack Follow with abdelhaq16, Auditor3 and 3 others
Originally disclosed on Code4rena
Submitted by hackhack, also found by abdelhaq16, Auditor3, Bigsam, edoscoba, and pwvux
programs/liquidity/src/state/token_reserve.rs#L96-L109 (get_supply_ratio/get_borrow_ratio)programs/liquidity/src/state/user_supply_position.rs#L192-L217 (with-interest stored as raw shares, interest-free stored as normal tokens)
get_supply_ratio() is intended to measure the split between with-interest and interest-free supply so the protocol can determine how much borrower interest should be routed to with-interest suppliers. However, the two sides are not stored in the same unit: with-interest supply is tracked in raw shares (amount * EXCHANGE_PRICES_PRECISION / supply_exchange_price), while interest-free supply is tracked in normal token units. Every other consumer of get_total_supply_with_interest() converts raw shares back to normal tokens before using the value — get_supply_with_interest_normal_ceil(), get_borrow_with_interest_normal(), and calc_utilization() all do this. get_supply_ratio() and get_borrow_ratio() are the only consumers that skip the conversion.
fn get_supply_ratio(&self) -> Result<u128> {
let supply_ratio: u128 = self.get_with_interest_vs_free_ratio(
self.get_total_supply_with_interest()?, // raw shares (not converted)
self.get_total_supply_interest_free()?, // normal tokens
)?;
Ok(supply_ratio)
}
At deployment (exchange_price = 1e12), raw shares and normal tokens are numerically equal, so the comparison is correct. The developer’s worked example in the comments (lines 395-409) validates the formula only at this 1:1 price point. As exchange_price grows above 1e12, each raw share represents more tokens than its face value, making the with-interest side appear smaller than it actually is.
This feeds into calculate_exchange_prices() where the ratio determines how yield is distributed to with-interest suppliers. When with-interest appears smaller than it is, the ratio_supply_yield calculation (lines 452-466) over-concentrates yield: supply_rate is set higher than correct, causing supply_exchange_price to grow faster than the actual yield generated by borrowers can sustain.
With-interest suppliers can withdraw more tokens than the reserve’s yield justifies. Every call to calculate_exchange_prices() — triggered by any operate, supply, borrow, or withdraw — compounds the error. The excess comes from the reserve’s token balance, creating a growing insolvency gap. Interest-free depositors or the last with-interest withdrawers absorb the loss. The same bug in get_borrow_ratio() has the opposite effect: with-interest borrows appear smaller, so less yield is routed to suppliers than borrowers actually generated. The magnitude scales with exchange price drift — at 36% drift with a 50/50 supply split, the supply_rate is ~15% too high.
Recommended Mitigation Steps
Convert raw shares to normal token amounts before passing to get_with_interest_vs_free_ratio(). The conversion functions already exist in the codebase — get_supply_with_interest_normal_ceil() and get_borrow_with_interest_normal(). Pass the exchange price into the ratio functions:
fn get_supply_ratio(&self, supply_exchange_price: u128) -> Result<u128> {
let with_interest_normal = self.get_total_supply_with_interest()?
.safe_mul(supply_exchange_price)?
.safe_div(EXCHANGE_PRICES_PRECISION)?;
let supply_ratio: u128 = self.get_with_interest_vs_free_ratio(
with_interest_normal,
self.get_total_supply_interest_free()?,
)?;
Ok(supply_ratio)
}
fn get_borrow_ratio(&self, borrow_exchange_price: u128) -> Result<u128> {
let with_interest_normal = self.get_total_borrow_with_interest()?
.safe_mul(borrow_exchange_price)?
.safe_div(EXCHANGE_PRICES_PRECISION)?;
let borrow_ratio: u128 = self.get_with_interest_vs_free_ratio(
with_interest_normal,
self.get_total_borrow_interest_free()?,
)?;
Ok(borrow_ratio)
}
The direct branch conditions in calculate_exchange_prices() must also be normalized, since they make the same raw-vs-normal comparison: self.total_supply_with_interest < self.total_supply_interest_free (line 423) and self.total_borrow_with_interest < self.total_borrow_interest_free (line 470). Fixing only the ratio helpers without fixing these branch conditions would leave the pricing logic partially incorrect.
View detailed Proof of Concept
Sponsor team notes
The Jupiter Lend team commented:
This was fixed in https://github.com/Instadapp/fluid-contracts-solana/commit/8358cdb3381918ad2f7b4442afe27b642535186a, https://github.com/Instadapp/fluid-contracts-solana/commit/b6821c479e49cd52d2a94c4145c64c26a5e95d8d (PR #137)
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
The ratio helpers passed raw share amounts (with-interest) and normal token amounts (interest-free) into the same comparison without converting shares to tokens using the exchange price.
Impact
With-interest suppliers could withdraw more tokens than the reserve's yield justified, and the error compounded on every pricing update, creating a growing insolvency gap absorbed by interest-free depositors or the last withdrawers; the same flaw in the borrow ratio routed less yield to suppliers than borrowers generated.
What the attacker needed
Exchange price drifts above its initial value; with-interest and interest-free supply both present
The fix
The team stated this was fixed by converting raw shares to normal token amounts before the ratio comparison and normalising the related branch conditions.
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 lending protocol, some deposits earn interest and are tracked in 'shares' whose value grows over time, while other deposits are tracked in plain token amounts. A function that decides how to split borrower interest between the two groups compared these two numbers directly, even though a share is worth more than one token once the exchange price rises. That made the interest-earning group look smaller than it was, so it received too much yield and could withdraw more than the protocol had actually earned.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.