A borrower might drain the vault by calling borrow() repeatedly with small borrow amount each time
The borrow function did not check whether the calculated debt share was zero, so a borrower could repeatedly borrow small amounts that rounded down to zero shares and never increase their debt. By calling borrow many times in one transaction, an attacker could drain the vault.
Source: https://github.com/sherlock-audit/2023-02-blueberry-judging/issues/45
Found by
chaduke
Summary
A borrower might drain the vault by calling borrow() repeatedly with small borrow amount that converts to a zero debt share each time.
Vulnerability Detail
This is possible because borrow() does not check whether the number of shares borrowed is equal to zero or not. Therefore, an attacker can take advantage of the rounding error and borrow funds for free. We show how a borrowed can drain the vault by calling borrow() repeatedly:
-
Suppose the for a particular token X, the total bank debt is 1000,000 and the total debt share is 100,000. That is each debt share has a 10 debt.
-
A malicious borrower Bob can call
borrow()(via SPELL) and borrow 9 each time, which will convert to9*100,000/1000,000 = 0debt shares.
https://github.com/sherlock-audit/2023-02-blueberry/blob/main/contracts/BlueBerryBank.sol#L709-L735
-
As a result, the borrower can steal 9 X tokens each time the
borrow()is called without increasing his debt shares. Bob can call this repeatedly in one transactionSteal()(within the gas limit) to borrow many tokens of X without increasing any debt shares. -
Call
Steal()many times, Bob will be able to drain the vault.
Impact
A malicious borrower can drain the the vault by calling borrow() repeatedly.
Code Snippet
See above
Tool used
VScode Manual Review
Recommendation
Borrow should revert when newShare == 0.
function borrow(address token, uint256 amount)
external
override
inExec
poke(token)
onlyWhitelistedToken(token)
{
if (!isBorrowAllowed()) revert BORROW_NOT_ALLOWED();
Bank storage bank = banks[token];
Position storage pos = positions[POSITION_ID];
uint256 totalShare = bank.totalShare;
uint256 totalDebt = bank.totalDebt;
uint256 share = totalShare == 0
? amount
: (amount * totalShare).divCeil(totalDebt);
+ if(share == 0) revert BorrowZeroShare();
bank.totalShare += share;
uint256 newShare = pos.debtShareOf[token] + share;
pos.debtShareOf[token] = newShare;
if (newShare > 0) {
pos.debtMap |= (1 << uint256(bank.index));
}
IERC20Upgradeable(token).safeTransfer(
msg.sender,
doBorrow(token, amount)
);
emit Borrow(POSITION_ID, msg.sender, token, amount, share);
}
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 borrow function did not revert when the calculated share amount was zero, allowing rounding to erase debt.
Impact
A malicious borrower could drain the vault by repeatedly calling borrow with small amounts.
What the attacker needed
A whitelisted token; ability to call borrow() via SPELL; sufficient gas for many calls
The fix
The borrow function should revert when the calculated share is zero.
How it was found
- Assume total bank debt is 1,000,000 and total debt share is 100,000, so each share is worth 10 debt.
- Call borrow() with a small amount, such as 9, which converts to 0 debt shares.
- Repeat the borrow call many times in one transaction to accumulate tokens without increasing debt shares.
- Repeat the whole process many times to drain the vault.
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 you borrow, the system records your debt as 'shares' rather than exact amounts. If the amount you borrow is very small, the maths can round the number of shares down to zero, so the system thinks you owe nothing. By doing this many times quickly, an attacker can take funds without ever increasing their recorded debt.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.