Skip to content
Audit finding Intermediate

Market.present_value_borrow should be roundUp

The function that converts a borrower's principal into the current debt amount uses integer division that rounds down, so the debt is slightly understated. An attacker could repay slightly less than owed, and repeated rounding in the protocol's favour of the borrower could contribute to insolvency.

jasonxiale Follow

Originally disclosed on Immunefi

  • Disclosed after under an hour

#35815 [SC-Medium] `Market.present_value_borrow` should be roundUp

Submitted on Oct 9th 2024 at 16:11:41 UTC by @jasonxiale for IOP | Swaylend

Description

Brief/Intro

In order to guarantee the contract does not become insolvent, incoming assets should be rounded up, while outgoing assets should be rounded down.

So while a borrower pays debt, his payment should be rounded up. `Market.present_value_borrow` is used to calculate the amount of base token to pay for debt in `Market.withdraw_base` and other functions, but the function has a rounding error.

Vulnerability Details

As Market.present_value_borrow shows ```Rust 1124 pub fn present_value_borrow(base_borrow_index: u256, principal: u256) -> u256 { 1125 principal * base_borrow_index / BASE_INDEX_SCALE_15 <<<--- here should be roundUp 1126 } ```

Impact Details

the contract might be insolvent

References

Add any relevant links to documentation or code

Proof of Concept

Proof of Concept

The following Rust code is used to demonstrate that `principal * base_borrow_index / BASE_INDEX_SCALE_15` is rounded down

```bash rustc calc.rs ; .calc 1 1.000000004419746 ```

```Rust pub const BASE_INDEX_SCALE_15: u64 = 1_000_000_000_000_000; // 1e15

fn main() { let principal: u64 = 12345678; let base_borrow_index: u64 = 81000007; println!("{}", principal * base_borrow_index / BASE_INDEX_SCALE_15); println!("{}", principal as f64 * base_borrow_index as f64 / BASE_INDEX_SCALE_15 as f64); } ```

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

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

Impact

The contract could become insolvent because borrowers pay slightly less than they owe.

What the attacker needed

Borrower repays debt

The fix

The calculation should round up instead of down.

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.

When a smart contract calculates how much a borrower owes, it multiplies their original loan by an interest index and then divides. Integer division in programming throws away any remainder, so the result is a tiny bit smaller than the true value. Here the code should round that result up to protect the lender, but it rounds down, letting borrowers pay marginally less.

More by jasonxiale

View all

25 more disclosed reports, best first.

More from Swaylend

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.