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.
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
- Report ID: #35815
- Report Type: Smart Contract
- Report severity: Medium
- Target: https://github.com/Swaylend/swaylend-monorepo/blob/develop/contracts/market/src/main.sw
- Impacts:
- Protocol insolvency
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); } ```
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.