possible precision loss in D3VaultLiquidation.finishLiquidation() function when calculating realDebt because of division before multiplication
The finishLiquidation() function calculates realDebt by dividing before multiplying, causing precision loss due to Solidity's truncation. This leads to incorrect updates of totalBorrows, potentially affecting the protocol's accounting.
0xdice91 Follow with BugBusters, BugHunter101 and 6 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-06-dodo-judging/issues/45
Found by
0xdice91, BugBusters, BugHunter101, Kalyan-Singh, MohammedRizwan, Oxhunter526, PRAISE, amaechieth, kutugu
Summary
finishLiquidation() divides before multiplying when calculating realDebt.
Vulnerability Detail
uint256 realDebt = borrows.div(record.interestIndex == 0 ? 1e18 : record.interestIndex).mul(info.borrowIndex);
There will be precision loss when calculating the realDebt because solidity truncates values when dividing and dividing before multiplying causes precision loss.
Values that suffered from precision loss will be updated here
info.totalBorrows = info.totalBorrows - realDebt;
Impact
Values that suffered from precision loss will be updated here
info.totalBorrows = info.totalBorrows - realDebt;
Code Snippet
Tool used
Manual Review
Recommendation
don't divide before multiplying
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 code performs division before multiplication, causing truncation and precision loss.
Impact
The precision loss could lead to incorrect totalBorrows values, potentially affecting the protocol's financial accounting.
What the attacker needed
None
The fix
The code should be fixed by multiplying before dividing to avoid precision loss.
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 does maths with whole numbers, dividing first can lose small remainders. Here, the code divides before multiplying, so the final debt amount is slightly too small. That wrong number is then used to update the total borrowed amount, which could cause accounting errors.