Skip to content
DoS Audit finding Intermediate

MarginTradingHook users could potentially be DOSed

A modifier that refunds leftover wrapped native tokens to the caller would revert the whole call if the caller contract cannot accept native currency. An attacker could send a tiny amount of wrapped native tokens to the contract's balance, causing every call from such a caller to fail.

sashik_eth Follow

Originally disclosed on Code4rena

Submitted by sashik_eth

The refundNative modifier unwraps and sends a native coin to the msg.sender at the end of the function execution. However, it could send native even if the case function call was not related to native coin trading - for example, if wrapped tokens were already on the MarginTradingHook balance. At the same time if the function caller is the contract that has no function except native transfer - the whole transaction would be reverted. Malicious actors could DOS such contracts by sending dust amounts of wrapped native tokens to the MarginTradingHook balance.

File: MarginTradingHook.sol
72:     modifier refundNative() {
73:         _;
74:         // refund native token
75:         uint wNativeBal = IERC20(WNATIVE).balanceOf(address(this));
76:         // NOTE: no need receive function since we will use TransparentUpgradeableProxyReceiveETH
77:         if (wNativeBal != 0) IWNative(WNATIVE).withdraw(wNativeBal);
78:         uint nativeBal = address(this).balance;
79:         if (nativeBal != 0) {
80:             (bool success,) = payable(msg.sender).call{value: nativeBal}('');
81:             _require(success, Errors.CALL_FAILED);
82:         }
83:     }

Impact

Contracts that use MarginTradingHook could be DOSed in case they have no function to receive native.

Proof of Concept

Consider the next scenario:

  1. Alice creates a contract that she would use as the caller of MarginTradingHook. Since Alice planning to work only with ERC20 tokens, she doesn’t implement the receive or payable fallback function on her contract.
  2. Alice using a contract in combination with MarginTradingHook for her trading activity for some time.
  3. Bob sends to MarginTradingHook 1 wei of wrapped native token.
  4. Now each call from Alice’s contract to MarginTradingHook functions that include the refundNative modifier would revert since MarginTradingHook unwraps 1 wei of WNATIVE and tries to send it to the Alice contract. Since the last one can’t accept native transfers - calls revert.

Bob could effectively continue DOS even if native funds were withdrawn by other users, by spamming dust amount txs each block, at the same time Alice could lose funds on her active positions due to changing market conditions.

Recommended Mitigation Steps

Consider adding the explicit parameter refundNative that users could set to true in case they expect a native transfer. Alternatively, remove the require statement at the end of the refundNative modifier code.

fez-init (INIT) disputed and commented:

This can be QA. We will add a note for hook users to be able to receive native tokens. @hansfriese

hansfriese (Judge) decreased severity to Medium and commented:

Medium is appropriate because:

  • There is no relevant note that hook users should be able to receive native tokens.
  • Attackers might DOS others with 1 wei.

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

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 refundNative modifier unconditionally unwraps and forwards any wrapped native balance to msg.sender and reverts on failure, even when the call had nothing to do with native tokens.

Impact

Contracts using the hook that cannot receive native currency could be blocked from calling it, and the reporter notes the victim could lose funds on active positions as markets move; the maintainer disputed the finding and the judge rated it Medium.

What the attacker needed

Victim uses a contract that cannot receive native currency; attacker can send dust wrapped native tokens

The fix

The reporter suggested an explicit opt-in parameter for native refunds or removing the require so a failed refund does not revert the call; the maintainer said they would add a note telling hook users to be able to receive native tokens.

How it was found

  1. Alice deploys a caller contract without a receive or payable fallback function.
  2. Alice uses that contract with MarginTradingHook for ERC20 trading.
  3. Bob sends 1 wei of wrapped native token to MarginTradingHook.
  4. Calls from Alice's contract to functions with the refundNative modifier now revert.

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.

Some smart contracts hold a small amount of the network's own currency (like ether) and send it back to whoever called them. If the caller is itself a contract that was never built to accept that currency, the transfer fails and the entire action is undone. An attacker can force this by leaving a tiny amount of that currency in the shared contract, so the refund always happens and always fails.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by sashik_eth

View all

5 more disclosed reports, best first.

More from INIT Capital Invitational

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.