Blockchain & Cryptocurrency Glossary

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

  • search-icon Clear Definitions
  • search-icon Practical
  • search-icon Technical
  • search-icon Related Terms

Delegatecall Injection

2 min read
Pronunciation
[del-i-git-kawl in-jek-shuhn]
Analogy
Imagine a main office (calling contract) that sometimes needs a specialist from another department (target contract library) to perform a task using the main office's resources and authority (`delegatecall`). A delegatecall injection is like an attacker tricking the main office into calling a malicious 'specialist' of their choice, or providing manipulated instructions to a legitimate specialist. This malicious specialist then uses the main office's authority and resources to wreak havoc, like re-writing files or emptying the safe, all while appearing to be the main office.
Definition
Delegatecall injection is a specific type of smart contract vulnerability where an attacker can manipulate the parameters or the target address of a `delegatecall` in a vulnerable contract. This allows the attacker to execute arbitrary code from another contract within the context (storage, balance, `msg.sender`) of the calling contract, potentially leading to unauthorized state changes, fund theft, or logic manipulation.
Key Points Intro
This vulnerability allows an attacker to execute arbitrary code with the privileges and storage context of the calling contract by controlling aspects of a `delegatecall`.
Key Points

Exploits `delegatecall`: Leverages the `delegatecall` opcode which executes external code in the caller's context.

Context Hijacking: Attacker's code runs with the calling contract's storage, balance, and identity (`msg.sender`, `msg.value`).

Parameter Manipulation: Occurs if the target contract address or function signature/data for `delegatecall` can be influenced by an attacker.

Severe Consequences: Can lead to complete compromise of the calling contract, including draining funds or altering ownership.

Example
A proxy contract uses `delegatecall` to forward calls to an implementation contract. If the proxy allows an admin to update the implementation contract's address, and this update function is not properly protected, an attacker could become admin (through another vulnerability) and set the implementation address to a malicious contract they deployed. Subsequent calls to the proxy would then `delegatecall` to the attacker's code, running with the proxy's storage and privileges.
Technical Deep Dive
`delegatecall` is a powerful low-level opcode in Ethereum that allows a contract (caller) to execute code from another contract (callee/library) but with the state (storage, `msg.sender`, `msg.value`) of the caller. If an attacker can control the `target_address` to which the `delegatecall` is made, or the `calldata` (function signature and arguments) passed to it, they can make the vulnerable contract execute malicious logic. This is especially dangerous in proxy patterns or when calling external libraries if the target is not hardcoded or strictly controlled.
Security Warning
Use `delegatecall` with extreme caution. Always ensure the target address and calldata are either hardcoded, strictly validated, or come from a trusted, immutable source. Avoid using `delegatecall` with user-supplied addresses or data. Unchecked `delegatecall` is a major security risk.
Caveat
While `delegatecall` is essential for upgradeable proxy patterns and library usage, its power makes it inherently risky if not handled with meticulous care. Understanding its context-preserving nature is critical for secure development.

Delegatecall Injection - Related Articles

No related articles for this term.