Blog

Arbitrary delegatecall: Explanation and Risks

1. What is delegatecall?

delegatecall is a special kind of function call that allows one contract (the caller) to execute code from another contract (the callee), but with the caller's storage, execution context, and Ether balance. In other words, delegatecall lets the calling contract run code from the callee contract as if it were its own.

The storage layout of the calling contract is used.

The code in the callee contract executes in the context of the caller.

The msg.sender and msg.value are preserved from the original call.


2. How delegatecall Works

A contract can use delegatecall to invoke logic stored in another contract. This is commonly used in upgradeable contracts (proxy patterns), where one contract holds the storage (proxy) while another contract (logic/implementation contract) handles the logic.

3. Risk of Arbitrary delegatecall

The ability to delegate to arbitrary logic contracts allows for the full stealing of funds in the contract.

If a contract accepts arbitrary addresses to perform delegatecall without proper validation, an attacker could craft malicious contracts that exploit this behavior in several ways:


Stealing Funds

An attacker can craft a malicious contract that drains funds from the calling contract by leveraging the arbitrary delegatecall vulnerability. Here’s an example of how this can be exploited: