borrow amount recorded in AssetInfo and BorrowRecord unmatched due to the precision loss
When a token with few decimals is borrowed, the vault's stored total borrows loses precision compared with the per-borrower records, so the aggregate becomes smaller than the sum of individual debts. The final repayment or liquidation then underflows and is blocked.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-06-dodo-judging/issues/248
Found by
Protocol Team
If the token has small decimals, like WBTC, which has 8 decimals, since borrowIndex is larger than 1e18, during accrueInterest, assetInfo[token].totalBorrows will loss more precision than assetInfo[token].borrowIndex. Eventually the assetInfo[token].totalBorrows will be smaller than the total sum of BorrowRecord.amount.
Vulnerability Detail
POC
/*
Copyright 2023 DODO ZOO.
SPDX-License-Identifier: Apache-2.0
*/
pragma solidity 0.8.16;
import "forge-std/Test.sol";
import "../../TestContext.t.sol";
contract POCTest is TestContext {
D3UserQuota public d3UserQutoa;
MockERC20 public wbtc;
address public wbtcAddr;
address public dodoAddr;
function setUp() public {
contextBasic();
wbtc = token1;
dodo = token3;
wbtcAddr = address(token1);
dodoAddr = address(token3);
wbtc.mint(user1, 1000e8);
vm.prank(user1);
wbtc.approve(address(dodoApprove), type(uint256).max);
wbtc.mint(poolCreator, 1000e8);
vm.prank(poolCreator);
wbtc.approve(address(dodoApprove), type(uint256).max);
dodo.mint(poolCreator, 1000000 ether);
vm.prank(poolCreator);
dodo.approve(address(dodoApprove), type(uint256).max);
d3UserQutoa = new D3UserQuota(address(token4), address(d3Vault));
vm.prank(vaultOwner);
d3Vault.setNewD3UserQuota(address(d3UserQutoa));
vm.prank(vaultOwner);
d3Vault.addLiquidator(address(this));
vm.prank(vaultOwner);
d3Vault.addLiquidator(liquidator);
}
function testPOC() public {
vm.prank(user1);
d3Proxy.userDeposit(user1, wbtcAddr, 100e8, 90e8);
// make dodo price high
token3ChainLinkOracle.feedData(1e9 * 1e18);
// pool1 deposit high price dodo, and borrow wbtc
vm.prank(poolCreator);
d3Proxy.makerDeposit(address(d3MM), dodoAddr, 1000 ether);
vm.prank(poolCreator);
d3MM.borrow(wbtcAddr, 10e8);
// make dodo price low
token3ChainLinkOracle.feedData(1 * 1e18);
vm.warp(3600 * 2);
d3Vault.accrueInterests(); // This is the key step. The test will pass if this line is comment out.
vm.warp(3600 * 3);
wbtc.burn(address(d3MM), 9e8);
d3MM.updateReserve(wbtcAddr);
d3Vault.startLiquidation(address(d3MM));
liquidateSwap(address(d3MM), dodoAddr, wbtcAddr, 1000 ether);
d3Vault.finishLiquidation(address(d3MM));
}
function testPOC2() public {
vm.prank(user1);
d3Proxy.userDeposit(user1, wbtcAddr, 100e8, 90e8);
// make dodo price high
token3ChainLinkOracle.feedData(1e9 * 1e18);
// pool1 deposit high price dodo, and borrow wbtc
vm.prank(poolCreator);
d3Proxy.makerDeposit(address(d3MM), dodoAddr, 1000 ether);
vm.prank(poolCreator);
d3MM.borrow(wbtcAddr, 10e8);
uint256 i = 1;
while (i < 365 * 2) {
vm.warp(3600 * 12 * i);
console.log("d3Vault.accrueInterests()...");
d3Vault.accrueInterest(wbtcAddr);
i++;
}
vm.prank(poolCreator);
d3Proxy.makerDeposit(address(d3MM), wbtcAddr, 1e8);
vm.prank(poolCreator);
d3MM.repayAll(wbtcAddr);
}
}
Impact
The last pool's repay() / finishLiquidation() will be blocked, since the record.amount will be larger than the info.borrowAmount, which will cause a underflow in info.totalBorrows = info.totalBorrows - realDebt;
Tool Used
Manual Review
Recommandation
Check if info.totalBorrows is smaller than the debt. If info.totalBorrows is smaller than the debt, then make info.totalBorrows to 0.
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 interest accrual updates the aggregate totalBorrows and the per-record amounts with different rounding, so the aggregate drifts below the sum of records for low-decimal tokens.
Impact
The last pool's repay or finishLiquidation is blocked by an underflow, so the debt cannot be cleared through the normal path.
What the attacker needed
A lending pool using a low-decimal token; interest accrual over time; a borrower repaying or a liquidation finishing
The fix
The recommendation is to check whether the aggregate totalBorrows is smaller than the debt and, if so, set it to zero before subtracting.
How it was found
- Deposit collateral and borrow a low-decimal token such as WBTC.
- Let time pass and call accrueInterests so interest is added.
- Repay the full debt or finish liquidation, which subtracts the recorded debt from the aggregate total.
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.
A lending pool keeps two running totals: one for the whole pool and one for each borrower. Because of how numbers are rounded when interest is added, the pool-wide total can end up slightly lower than the sum of the individual debts. When the last borrower tries to repay, the code subtracts a bigger number from a smaller one, which is not allowed, so the repayment fails.