Delegate call in Vault#_execute can alter Vault’s ownership
The Vault contract's execute function uses delegatecall to a target contract, which can modify the Vault's storage. An attacker with permission to call a malicious target contract could reset the nonce to zero, allowing the Vault to be re-initialized and ownership taken over.
Originally disclosed on Code4rena
Submitted by byterocket, also found by 242, 141345, 0x1f8b, ACai, ayeslick, berndartmueller, BradMoon, cccz, Chom, giovannidisiena, infosec_us_team, Lambda, minhtrng, nine9, oyc_109, PwnedNoMore, reassor, scaraven, slywaters, sseefried, tofunmi, Twpony, and unforgiven
https://github.com/code-423n4/2022-07-fractional/blob/main/src/Vault.sol#L62
https://github.com/code-423n4/2022-07-fractional/blob/main/src/Vault.sol#L126
https://github.com/code-423n4/2022-07-fractional/blob/main/src/Vault.sol#L25
Impact
The Vault#execute function calls a target contract’s function via delegatecall if the caller is either the owner of the Vault or the target contract is part of a merkle tree, indicating a permission to call the target contract.
// Check that the caller is either a module with permission to call or the owner.
if (!MerkleProof.verify(_proof, merkleRoot, leaf)) {
if (msg.sender != owner)
revert NotAuthorized(msg.sender, _target, selector);
}
(See Vault#execute)
If the checks succeed, the internal _execute() function is used to execute the call via delegatecall.
delegatecalls have to be used with caution because the contract being called is using the caller’s contract storage, i.e. the callee contract can alter the caller’s contract state (for more info, see Solidity docs).
The developers seem to be aware of the danger that the callee contract is able to overtake the Vaults ownership, by changing the Vaults owner variable, as the owner is cached before the delegatecall and afterwards checked that the variable did not change:
// ...
address owner_ = owner;
// ...
(success, response) = _target.delegatecall{gas:stipend}(_data);
if (owner_ != owner) revert OwnerChanged(owner_, owner);
// ...
(See Vault#_execute)
However, changing the owner variable is not the only way the callee contract is able to overtake the Vaults ownership. If the nonce variable is re-set to 0, the Vault’s init function becomes callable again, granting ownership to the caller:
function init() external {
if (nonce != 0) revert Initialized(owner, msg.sender, nonce);
nonce = 1;
owner = msg.sender;
}
(See Vault#init)
Note that other storage variables (i.e. merkleRoot and methods) could also be altered, but this would not lead to a loss in ownership, i.e. the project could re-set the variables.
Nevertheless, a contract trying, due to being malicious or faulty, to change the Vaults ownership first needs to be permissioned by the owner by adding it to the merkle tree. Otherwise, the contract can not be called.
Due to the fact that the owner variable check is included, meaning the project rates operational management already as being error-prone, and the high number of security issues in connection to faulty usage of delegatecall, the severity is rated as MEDIUM (HIGH impact with a LOW likelihood).
Proof of Concept
Add the following code to the test/Vault.t.sol file and run forge test --match-test "testExecuteNoRevertIfReinitialized" -vvvv.
If the test succeeds, the Vault got re-initialized due to a delegatecall altering the Vault’s nonce variable.
// Inside contract VaultTest.
function testExecuteNoRevertIfReinitialized() public {
vaultProxy.init(); // address(this) is owner
HackyTargetContract targetContract = new HackyTargetContract();
bytes32[] memory proof = new bytes32[](1);
bytes memory data = abi.encodeCall(
targetContract.changeNonce,
()
);
// Note that the call does NOT revert.
vaultProxy.execute(address(targetContract), data, proof);
// Note that the Vault can now be re-initialized as the execute
// call above set the Vault's nonce to zero.
vm.prank(address(1));
vaultProxy.init();
assertEq(vaultProxy.owner(), address(1));
}
// Outside contract VaultTest.
contract HackyTargetContract {
address public gap_owner;
bytes32 public gap_merkleRoot;
uint256 public nonce;
function changeNonce() public {
nonce = 0;
}
}
Recommended Mitigation Steps
Check the nonce variable before and after the delegatecall inside the _execute() function as well, e.g.:
address owner_ = owner;
uint256 nonce_ = nonce;
// Execute delegatecall
if (owner_ != owner || nonce_ != nonce) {
revert InvalidStateChange();
}
// ...
mehtaculous (Fractional) confirmed
HardlyDifficult (judge) commented:
Due to the use of delegate call,
executeand/or the fallback function could lead to changing the proxy’s storage or even self destructing the proxy instance. If this were to happen, users funds could be put at risk. These attacks are predicated on the current vault owner to maliciously or unintentionally directly call or approve the calling of a malicious plugin — because of this, I agree with the warden here that this is a Medium risk issue.
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 delegatecall allows the target contract to modify the Vault's storage, and the code only checks that the owner variable did not change, not the nonce.
Impact
If the owner maliciously or unintentionally approves a malicious plugin, the Vault's ownership could be taken over, putting user funds at risk.
What the attacker needed
Owner adds malicious contract to merkle tree; attacker can call execute with that contract
The fix
The fix is to check the nonce variable before and after the delegatecall, reverting if it changed.
How it was found
- The Vault owner adds a malicious target contract to the merkle tree, granting permission to call it.
- The attacker calls Vault#execute with the malicious target contract and data that triggers a function to set nonce to 0.
- The delegatecall executes, resetting the Vault's nonce to 0.
- The attacker calls Vault#init, which now succeeds because nonce is 0, setting the attacker as the new 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 delegatecall lets one contract run another contract's code but use its own storage. Here, a malicious contract could change a number (nonce) that controls whether the Vault can be set up again, letting an attacker become the owner.
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.