Skip to content
Business logic Audit finding Intermediate

MarginTrading.sol: The whole balance and not just the traded funds are deposited into Aave when a trade is opened

The MarginTrading contract deposits the entire token balance into Aave when opening a trade, rather than only the funds acquired from the swap. This can unintentionally lock pre-existing funds as margin, increasing potential losses.

roguereddwarf Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-05-dodo-judging/issues/72

Found by

roguereddwarf

Summary

It's expected by the protocol that funds can be in the MarginTrading contract without being deposited into Aave as margin.

We can see this by looking at the MarginTradingFactory.depositMarginTradingETH and MarginTradingFactory.depositMarginTradingERC20 functions.

If the user sets margin=false as the parameter, the funds are only sent to the MarginTrading contract but NOT deposited into Aave.

https://github.com/sherlock-audit/2023-05-dodo/blob/main/dodo-margin-trading-contracts/contracts/marginTrading/MarginTradingFactory.sol#L203-L211

https://github.com/sherlock-audit/2023-05-dodo/blob/main/dodo-margin-trading-contracts/contracts/marginTrading/MarginTradingFactory.sol#L259-L272

So clearly there is the expectation for funds to be in the MarginTrading contract that should not be deposited into Aave.

This becomes an issue when a trade is opened.

Vulnerability Detail

Let's look at the MarginTrading._openTrade function that is called when a trade is opened:

https://github.com/sherlock-audit/2023-05-dodo/blob/main/dodo-margin-trading-contracts/contracts/marginTrading/MarginTrading.sol#L257-L279

The whole balance of the token will be deposited into Aave:

Solidity
_tradeAmounts[i] = IERC20(_tradeAssets[i]).balanceOf(address(this)); 
_lendingPoolDeposit(_tradeAssets[i], _tradeAmounts[i], 1); 

Not just those funds that have been acquired by the swap. This means that funds that should stay in the MarginTrading contract might also be deposited as margin.

Impact

When opening a trade funds can be deposited into Aave unintentionally. Thereby the funds act as margin and the trade can incur a larger loss than expected.

Code Snippet

https://github.com/sherlock-audit/2023-05-dodo/blob/main/dodo-margin-trading-contracts/contracts/marginTrading/MarginTradingFactory.sol#L203-L211

https://github.com/sherlock-audit/2023-05-dodo/blob/main/dodo-margin-trading-contracts/contracts/marginTrading/MarginTradingFactory.sol#L259-L272

https://github.com/sherlock-audit/2023-05-dodo/blob/main/dodo-margin-trading-contracts/contracts/marginTrading/MarginTrading.sol#L257-L279

Tool used

Manual Review

Recommendation

It is necessary to differentiate the funds that are acquired by the swap and those funds that were there before and should stay in the contract:

Diff
diff --git a/dodo-margin-trading-contracts/contracts/marginTrading/MarginTrading.sol b/dodo-margin-trading-contracts/contracts/marginTrading/MarginTrading.sol
index f68c1f3..42f96cf 100644
--- a/dodo-margin-trading-contracts/contracts/marginTrading/MarginTrading.sol
+++ b/dodo-margin-trading-contracts/contracts/marginTrading/MarginTrading.sol
@@ -261,6 +261,10 @@ contract MarginTrading is OwnableUpgradeable, IMarginTrading, IFlashLoanReceiver
         bytes memory _swapParams,
         address[] memory _tradeAssets
     ) internal {
+        int256[] memory _amountsBefore = new uint256[](_tradeAssets.length);
+        for (uint256 i = 0; i < _tradeAssets.length; i++) {
+            _amountsBefore[i] = IERC20(_tradeAssets[i]).balanceOf(address(this));
+        }
         if (_swapParams.length > 0) {
             // approve to swap route
             for (uint256 i = 0; i < _swapApproveToken.length; i++) {
@@ -272,8 +276,10 @@ contract MarginTrading is OwnableUpgradeable, IMarginTrading, IFlashLoanReceiver
         }
         uint256[] memory _tradeAmounts = new uint256[](_tradeAssets.length);
         for (uint256 i = 0; i < _tradeAssets.length; i++) {
-            _tradeAmounts[i] = IERC20(_tradeAssets[i]).balanceOf(address(this));
-            _lendingPoolDeposit(_tradeAssets[i], _tradeAmounts[i], 1);
+            if (_amountsBefore[i] < IERC20(_tradeAssets[i]).balanceOf(address(this))) {
+                _tradeAmounts[i] = IERC20(_tradeAssets[i]).balanceOf(address(this)) - _amountsBefore[i];
+                _lendingPoolDeposit(_tradeAssets[i], _tradeAmounts[i], 1);
+            }
         }
         emit OpenPosition(_swapAddress, _swapApproveToken, _tradeAssets, _tradeAmounts);
     }

If funds that were in the contract prior to the swap should be deposited there is the separate MarginTrading.lendingPoolDeposit function to achieve this.

Discussion

Zack995

In terms of product design, users do not have a separate concept of balance. However, the contract is designed to be more flexible and allows for balances to be maintained. Users will not perceive or interact with balances in terms of user experience or operations.

roguereddwarf

Based on the smart contract logic there is clearly the notion of balance that is not intended to be used as collateral (but e.g. used to repay a loan). If this notion of a separate balance is not exposed on the front-end this is not a sufficient mitigation of the issue since the issue is clearly present in the smart contract.

securitygrid

Escalate for 10 USDC This is valid low/info as stated by the sponsor. No bad impact.

sherlock-admin

Escalate for 10 USDC This is valid low/info as stated by the sponsor. No bad impact.

You've created a valid escalation for 10 USDC!

To remove the escalation from consideration: Delete your comment.

You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.

ctf-sec

can consider #80 duplicate of this one

hrishibhat

Result: Medium Has duplicates Considering this issue as valid medium based on the above comments from smart contract perspective and enforcing in the front end is not a mitigation as mentioned above.

sherlock-admin

Escalations have been resolved successfully!

Escalation status:

Attens1423

In fact we need tokens are deposited in aave. We suppose users use frontend to operate their assets and frontend will ensure all assets will be deposited in aave. If there are tokens left, users could deposit them in aave by themselves. So we think this design is not a problem.

MLON33

Interpreting sponsor's comment as "Won't Fix."

roguereddwarf

Mitigation Review: While Sherlock considers this a valid "Medium", the sponsor decided that there is no need to implement a fix.

The original report as published on Sherlock, by roguereddwarf. 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

Root cause

The _openTrade function uses the contract's total token balance instead of only the amount gained from the swap.

Impact

Funds that should remain in the contract may be deposited into Aave as margin, potentially causing larger losses than expected.

What the attacker needed

Funds already in the contract; user opens a trade

The fix

The contract should record balances before the swap and deposit only the difference after the swap.

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 open a trade, the contract takes all of a certain token it holds and puts it into a lending pool called Aave. This includes tokens that were already there and not meant to be used for that trade. So your extra funds might get used as collateral without you wanting that.

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 roguereddwarf

View all

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