This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Negative test for a reentrant attack on the core relayer forward mech…
…anism (#83) * Modifies the relayer simulation to be easier to use in negative tests. * Adds negative test for a reentrancy attack on the forward mechanism. * `forge fmt` run.
- Loading branch information
1 parent
b2242c9
commit cfc5b69
Showing
2 changed files
with
193 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.17; | ||
|
||
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
|
||
import "../interfaces/IWormhole.sol"; | ||
import "../interfaces/IWormholeReceiver.sol"; | ||
import "../interfaces/ICoreRelayer.sol"; | ||
|
||
/** | ||
* This contract is a malicious "integration" that attempts to attack the forward mechanism. | ||
*/ | ||
contract AttackForwardIntegration is IWormholeReceiver { | ||
mapping(bytes32 => bool) consumedMessages; | ||
address attackerReward; | ||
IWormhole wormhole; | ||
ICoreRelayer core_relayer; | ||
uint32 nonce = 1; | ||
uint16 targetChainId; | ||
|
||
// Capture 30k gas for fees | ||
// This just needs to be enough to pay for the call to the destination address. | ||
uint32 SAFE_DELIVERY_GAS_CAPTURE = 30000; | ||
|
||
constructor(IWormhole initWormhole, ICoreRelayer initCoreRelayer, uint16 chainId, address initAttackerReward) { | ||
attackerReward = initAttackerReward; | ||
wormhole = initWormhole; | ||
core_relayer = initCoreRelayer; | ||
targetChainId = chainId; | ||
} | ||
|
||
// This is the function which receives all messages from the remote contracts. | ||
function receiveWormholeMessages(bytes[] memory vaas, bytes[] memory additionalData) public payable override { | ||
// Do nothing. The attacker doesn't care about this message; he sends it himself. | ||
} | ||
|
||
receive() external payable { | ||
// Request forward from the relayer network | ||
// The core relayer could in principle accept the request due to this being the target of the message at the same time as being the refund address. | ||
// Note that, if succesful, this forward request would be processed after the time for processing forwards is past. | ||
// Thus, the request would "linger" in the forward request cache and be attended to in the next delivery. | ||
requestForward(targetChainId, toWormholeFormat(attackerReward)); | ||
} | ||
|
||
function requestForward(uint16 targetChain, bytes32 attackerRewardAddress) internal { | ||
uint256 computeBudget = core_relayer.quoteGasDeliveryFee( | ||
targetChain, SAFE_DELIVERY_GAS_CAPTURE, core_relayer.getDefaultRelayProvider() | ||
); | ||
|
||
ICoreRelayer.DeliveryRequest memory request = ICoreRelayer.DeliveryRequest({ | ||
targetChain: targetChain, | ||
targetAddress: attackerRewardAddress, | ||
// All remaining funds will be returned to the attacker | ||
refundAddress: attackerRewardAddress, | ||
computeBudget: computeBudget, | ||
applicationBudget: 0, | ||
relayParameters: core_relayer.getDefaultRelayParams() | ||
}); | ||
|
||
core_relayer.requestForward{value: computeBudget}(request, nonce, core_relayer.getDefaultRelayProvider()); | ||
} | ||
|
||
function toWormholeFormat(address addr) public pure returns (bytes32 whFormat) { | ||
return bytes32(uint256(uint160(addr))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters