SWC-112 Delegatecall to Untrusted Callee.
There exists a special variant of a message call, named delegatecall which is identical to a message call apart from the fact that the code at the target address is executed in the context of the calling contract and msg.sender and msg.value do not change their values.
This allows a smart contract to dynamically load code from a different address at runtime. Storage, current address and balance still refer to the calling contract.
Calling into untrusted contracts is very dangerous, as the code at the target address can change any storage values of the caller and has full control over the caller's balance.
Proof of Concept
Provide direct links to all referenced code in GitHub. Add screenshots, logs, or any other relevant proof that illustrates the concept.
switch to first account (as alice) and deploy the victim contract.
switch to second account (as eve) and deploy the attack contract passing deploy to address option as victim contract address.
switch to attack account for eve and click attack() button.
call to data is successful.
click function name called proxy() (button) and contract address of victim is passed.
PoC
// SPDX-License-Identifier: MIT
pragma solidity 0.8.12;
/**
* @title Proxy // This is the user's wallet
* @notice Basic proxy that delegates all calls to a fixed implementing contract.
*/
import "./Proxy.sol";
contract AttackProxy {
/* This is the keccak-256 hash of "biconomy.scw.proxy.implementation" subtracted by 1, and is validated in the constructor */
bytes32 internal constant _IMPLEMENTATION_SLOT = 0x37722d148fb373b961a84120b6c8d209709b45377878a466db32bbc40d95af26;
event Received(uint indexed value, address indexed sender, bytes data);
Proxy public proxy;
constructor(Proxy _proxy) {
proxy = Proxy(_proxy);
}
function attack() public payable {
address(proxy).delegatecall(abi.encodeWithSignature("fallback()"));
address(proxy).delegatecall(abi.encodeWithSignature("receive()"));
address(proxy).delegatecall(abi.encodeWithSignature("Received(1, _proxy, 0x37722d148fb373b961a84120b6c8d209709b45377878a466db32bbc40d95af26)"));
}
}
Use delegatecall with caution and make sure to never call into untrusted contracts.
If the target address is derived from user input ensure to check it against a whitelist of trusted contracts.
Lines of code
https://github.com/code-423n4/2023-01-biconomy/blob/53c8c3823175aeb26dee5529eeefa81240a406ba/scw-contracts/contracts/smart-contract-wallet/Proxy.sol#L28
Vulnerability details
Impact
Detailed description of the impact of this finding.
URL
File
SWC-112 Delegatecall to Untrusted Callee.
There exists a special variant of a message call, named delegatecall which is identical to a message call apart from the fact that the code at the target address is executed in the context of the calling contract and msg.sender and msg.value do not change their values. This allows a smart contract to dynamically load code from a different address at runtime. Storage, current address and balance still refer to the calling contract. Calling into untrusted contracts is very dangerous, as the code at the target address can change any storage values of the caller and has full control over the caller's balance.
Proof of Concept
Provide direct links to all referenced code in GitHub. Add screenshots, logs, or any other relevant proof that illustrates the concept.
URL
Steps to reproduce
NB: Using Remix (VM) London
PoC
victim address:
attacker address:
Output of attack() button
Output of proxy() button
Tools Used
Remix IDE
Recommended Mitigation Steps
Use delegatecall with caution and make sure to never call into untrusted contracts. If the target address is derived from user input ensure to check it against a whitelist of trusted contracts.