Access Control
Missing or incorrect access control is one of the most common smart contract vulnerabilities. Functions meant to be restricted can be called by anyone.
Authorised Targets Only
Common Access Control Issues
The bug is usually an omission rather than a mistake in logic: a function that was written as an admin action and never decorated. Read the modifier list of every state-changing function and ask who can reach it.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
// VULNERABLE: missing access control
contract Vulnerable {
address public owner;
constructor() {
owner = msg.sender;
}
// Anyone can call this! Whoever calls it becomes the owner.
function setOwner(address newOwner) public {
owner = newOwner;
}
// Missing modifier - and it pays the new owner, whoever that is.
function withdrawAll() public {
(bool ok, ) = payable(owner).call{value: address(this).balance}("");
require(ok, "Transfer failed");
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
// VULNERABLE: missing access control
contract Vulnerable {
address public owner;
constructor() {
owner = msg.sender;
}
// Anyone can call this! Whoever calls it becomes the owner.
function setOwner(address newOwner) public {
owner = newOwner;
}
// Missing modifier - and it pays the new owner, whoever that is.
function withdrawAll() public {
(bool ok, ) = payable(owner).call{value: address(this).balance}("");
require(ok, "Transfer failed");
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
// SECURE: proper access control
contract Secure {
address public owner;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "Not owner");
_;
}
function setOwner(address newOwner) public onlyOwner {
owner = newOwner;
}
function withdrawAll() public onlyOwner {
(bool ok, ) = payable(owner).call{value: address(this).balance}("");
require(ok, "Transfer failed");
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
// SECURE: proper access control
contract Secure {
address public owner;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "Not owner");
_;
}
function setOwner(address newOwner) public onlyOwner {
owner = newOwner;
}
function withdrawAll() public onlyOwner {
(bool ok, ) = payable(owner).call{value: address(this).balance}("");
require(ok, "Transfer failed");
}
}
The ordering of those two calls is what makes the pair dangerous:
setOwner hands the
caller ownership, and withdrawAll
then pays it out in the next transaction. One missing modifier on either function is enough.
tx.origin Phishing
This is the case where the check looks correct. The victim genuinely tests for the owner — but against
tx.origin, which
is the EOA that signed the transaction and is unchanged by any intermediate contract call. The attacker only
needs the owner to interact with their contract once; the victim's own balance is then transferred to the
attacker's caller.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
// VULNERABLE: using tx.origin for authorisation
contract TxOriginVictim {
address public owner;
constructor() {
owner = msg.sender;
}
function withdrawAll() public {
require(tx.origin == owner, "Not owner"); // BAD!
payable(msg.sender).transfer(address(this).balance);
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
// VULNERABLE: using tx.origin for authorisation
contract TxOriginVictim {
address public owner;
constructor() {
owner = msg.sender;
}
function withdrawAll() public {
require(tx.origin == owner, "Not owner"); // BAD!
payable(msg.sender).transfer(address(this).balance);
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
interface ITxOriginVictim {
function withdrawAll() external;
}
// ATTACK: trick the owner into calling this contract
contract TxOriginAttacker {
ITxOriginVictim victim;
address attacker;
constructor(address _victim) {
victim = ITxOriginVictim(_victim);
attacker = msg.sender;
}
// The owner calls this function (e.g. via a phishing page that asks
// them to "claim rewards"). tx.origin is still the owner, so the
// victim's check passes even though msg.sender is this contract.
function attack() public {
victim.withdrawAll();
}
// The victim pays msg.sender, i.e. this contract, so it must be able
// to accept ETH or the withdrawal reverts and nothing is stolen.
receive() external payable {}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
interface ITxOriginVictim {
function withdrawAll() external;
}
// ATTACK: trick the owner into calling this contract
contract TxOriginAttacker {
ITxOriginVictim victim;
address attacker;
constructor(address _victim) {
victim = ITxOriginVictim(_victim);
attacker = msg.sender;
}
// The owner calls this function (e.g. via a phishing page that asks
// them to "claim rewards"). tx.origin is still the owner, so the
// victim's check passes even though msg.sender is this contract.
function attack() public {
victim.withdrawAll();
}
// The victim pays msg.sender, i.e. this contract, so it must be able
// to accept ETH or the withdrawal reverts and nothing is stolen.
receive() external payable {}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
// FIXED: authorise the immediate caller, not the transaction origin
contract TxOriginSafe {
address public owner;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "Not owner");
_;
}
function withdrawAll() public onlyOwner {
(bool ok, ) = payable(owner).call{value: address(this).balance}("");
require(ok, "Transfer failed");
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
// FIXED: authorise the immediate caller, not the transaction origin
contract TxOriginSafe {
address public owner;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "Not owner");
_;
}
function withdrawAll() public onlyOwner {
(bool ok, ) = payable(owner).call{value: address(this).balance}("");
require(ok, "Transfer failed");
}
}
The fix is one identifier: msg.sender,
which is the caller of this function and therefore changes as soon as a contract is in the middle. The same
substitution appears in
Solidity Security.
EIP-7702, live since Pectra in May 2025, gives the rule a second reason. An account can now set its own code by
signing an authorisation tuple, so
tx.origin is no
longer guaranteed to be code-free, and
msg.sender == tx.origin
no longer means "topmost frame" because a delegated account can make further calls inside the same transaction.
The lure is cheaper as well: one signed authorisation points the victim's account at the attacker's delegate,
which then has unrestricted access until it is overwritten or cleared by delegating to the zero address.
Always Use msg.sender
Related Topics
Integer Bugs
The arithmetic class that a missing modifier usually exposes alongside the privileged function.
DeFi Attacks
What an unprivileged caller can do to a protocol once one privileged path is reachable.
Solidity Security
The delegatecall and unchecked-transfer pairs that sit next to this one in most audit findings.