When a D3MM pool repays all of the borrowed funds to vault using `D3Funding.sol repayAll`, an attacker can steal double the amount of those funds from vault
The vault's internal balance was decreased instead of increased when a pool repaid all borrowed funds, creating a mismatch between recorded and actual token balances. An attacker could deposit the difference and withdraw it, stealing double the repaid amount from the vault.
0x4db5362c Follow with 0xG0P1, 0xkaden and 7 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-06-dodo-judging/issues/217
Found by
0x4db5362c, 0xG0P1, 0xkaden, HALITUS, Proxy, Sulpiride, dirk_y, osmanozdemir1, seeques, skyge
Summary
When a D3MM pool repays all of the borrowed funds to vault using D3Funding.sol repayAll, an attacker can steal double the amount of those funds from vault. This is because the balance of vault is not updated correctly in D3VaultFunding.sol _poolRepayAll.
amount should be added in info.balance instead of being subtracted.
function _poolRepayAll(address pool, address token) internal {
.
.
info.totalBorrows = info.totalBorrows - amount;
info.balance = info.balance - amount; // amount should be added here
.
.
}
Vulnerability Detail
A D3MM pool can repay all of the borrowed funds from vault using the function D3Funding.sol repayAll which further calls D3VaultFunding.sol poolRepayAll and eventually D3VaultFunding.sol _poolRepayAll.
function repayAll(address token) external onlyOwner nonReentrant poolOngoing {
ID3Vault(state._D3_VAULT_).poolRepayAll(token);
_updateReserve(token);
require(checkSafe(), Errors.NOT_SAFE);
}
The vault keeps a record of borrowed funds and its current token balance.
_poolRepayAll() is supposed to:
- Decrease the borrowed funds by the repaid amount
- Increase the token balance by the same amount #vulnerability
- Transfer the borrowed funds from pool to vault
However, _poolRepayAll() is decreasing the token balance instead.
function _poolRepayAll(address pool, address token) internal {
.
.
.
.
info.totalBorrows = info.totalBorrows - amount;
info.balance = info.balance - amount; // amount should be added here
IERC20(token).safeTransferFrom(pool, address(this), amount);
emit PoolRepay(pool, token, amount, interests);
}
Let's say a vault has 100,000 USDC A pool borrows 20,000 USDC from vault
When the pool calls poolRepayAll(), the asset info in vault will change as follows:
totalBorrows => 20,000 - 20,000 => 0// info.totalBorrows - amountbalance => 100,000 - 20,000 => 80,000// info.balance - amounttokens owned by vault => 100,000 + 20,000 => 120,000 USDC// 20,000 USDC is transferred from pool to vault (repayment)- The difference of recorded balance (80,000) and actual balance (120,000) is
40,000 USDC
An attacker waits for the poolRepayAll() function call by a pool.
When poolRepayAll() is executed, the attacker calls D3VaultFunding.sol userDeposit(), which deposits 40,000 USDC in vault on behalf of the attacker.
After this, the attacker withdraws the deposited amount using D3VaultFunding.sol userWithdraw() and thus gains 40,000 USDC.
function userDeposit(address user, address token) external nonReentrant allowedToken(token) {
.
.
.
AssetInfo storage info = assetInfo[token];
uint256 realBalance = IERC20(token).balanceOf(address(this)); // check tokens owned by vault
uint256 amount = realBalance - info.balance; // amount = 120000-80000
.
.
.
IDToken(info.dToken).mint(user, dTokenAmount);
info.balance = realBalance;
emit UserDeposit(user, token, amount);
}
Impact
Loss of funds from vault. The loss will be equal to 2x amount of borrowed tokens that a D3MM pool repays using D3VaultFunding.sol poolRepayAll
Code Snippet
D3VaultFunding.sol _poolRepayAll()
function _poolRepayAll(address pool, address token) internal {
.
.
info.totalBorrows = info.totalBorrows - amount;
info.balance = info.balance - amount; // vulnerability: amount should be added here
IERC20(token).safeTransferFrom(pool, address(this), amount);
emit PoolRepay(pool, token, amount, interests);
}
Tool used
Manual Review
Recommendation
In D3VaultFunding.sol _poolRepayAll, do the following changes:
Current code:
info.balance = info.balance - amount;
New (replace '-' with '+'):
info.balance = info.balance + amount;
Discussion
traceurl
Fixed in this PR: https://github.com/DODOEX/new-dodo-v3/pull/26
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
Loss of funds from the vault equal to twice the amount repaid by the pool.
What the attacker needed
A D3MM pool repays all borrowed funds; attacker can deposit and withdraw
The fix
The code was fixed by changing the subtraction to an addition when updating the vault's recorded balance.
How it was found
- A pool borrows funds from the vault.
- The pool calls repayAll to repay the borrowed funds.
- The vault's recorded balance is decreased instead of increased, creating a gap between recorded and actual balance.
- An attacker waits for the repayAll call and then calls userDeposit, depositing an amount equal to the gap.
- The attacker calls userWithdraw to withdraw the deposited amount, gaining the gap as profit.
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 vault is like a shared bank that tracks how much money it holds. When a pool paid back a loan, the vault's record was updated in the wrong direction, making it look like the vault had less money than it really did. An attacker could then deposit the missing amount, which the vault would mistakenly treat as new money, and withdraw it for profit.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.