delegatecalllow level function, how it works, how it can be used to delegate operations to on-chain libraries, and what implications it has on execution scope.
delegatecallis useful. Recall that if a smart contract is deployed to the blockchain then we can't be modified its code no more. All the logic implemented in the contract will stay there forever, unless we selfdestruct the contract and deploy a new one. To get around this, we can use
delegatecallto call a "library" contract where new logic is implemented.
fallback()function that executes
Delegateimplements a suspicious function
msg.senderas the owner.
fallback()function in B. Here B initializes the delegatecall B -> C. If you watch Smart Contract Programmer's video, you should know that:
msg.sender == A.
pwn()is called, the
ownerstate variable refers to B's
owner(instead of C's owner, since it is a delegatecall). The
msg.senderhere is A. In other words, the function
pwn()sets A as B's owner, and we are done.
Delegationand calls the
pwn()is not defined in
fallback()function will be triggered. After that,
delegatecall()and calls the
pwn()is called, it updates
msg.sender, which is the attacker.
ownerrefers to the owner of
Delegationis doing the
delegatecallis particularly risky and has been used as an attack vector on multiple historic hacks. With it, your contract is practically saying "here, -other contract- or -other library-, do whatever you want with my state". Delegates have complete access to your contract's state. The
delegatecallfunction is a powerful feature, but a dangerous one, and must be used with extreme care.