Skip to content
Business logic Audit finding Intermediate

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.

Solidity
    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.

Solidity
    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:

  1. Decrease the borrowed funds by the repaid amount
  2. Increase the token balance by the same amount #vulnerability
  3. Transfer the borrowed funds from pool to vault

However, _poolRepayAll() is decreasing the token balance instead.

Solidity
    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:

  1. totalBorrows => 20,000 - 20,000 => 0 // info.totalBorrows - amount
  2. balance => 100,000 - 20,000 => 80,000 // info.balance - amount
  3. tokens owned by vault => 100,000 + 20,000 => 120,000 USDC // 20,000 USDC is transferred from pool to vault (repayment)
  4. 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.

Solidity
    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()

Solidity
    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

The original report as published on Sherlock, by 0x4db5362c. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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

  1. A pool borrows funds from the vault.
  2. The pool calls repayAll to repay the borrowed funds.
  3. The vault's recorded balance is decreased instead of increased, creating a gap between recorded and actual balance.
  4. An attacker waits for the repayAll call and then calls userDeposit, depositing an amount equal to the gap.
  5. 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.

More by 0x4db5362c

View all

2 more disclosed reports, best first.

More from DODO V3 update

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.