Access control in Solidity smart contracts
Updated November 5, 2026
Definition
What access control is
Access control fails when a caller who should not have a privilege can still run the function behind it. In DeFi that means who may mint or burn, move reserves, pause the system, upgrade a proxy, or change roles. The check can be a simple onlyOwner modifier, a role table, a governance vote, a guardian multisig, or a bridge message that must prove it came from a trusted chain.
OWASP lists the usual mistakes: missing modifiers or role checks, wrong assumptions about msg.sender through delegate calls or meta-transactions, unprotected initializers, and mistaken trusted addresses (OWASP SC01:2026). Combined with a logic or upgrade bug, one missing check can hand over the protocol.
Access control is SWC-105 (unprotected Ether withdrawal), SWC-106 (unprotected selfdestruct), and SWC-115 (authorization through tx.origin) in the SWC Registry. It is SC01 in the OWASP Smart Contract Top 10 for 2026 and the first item on Guardix's OWASP walk-through.
Patterns
Common failure patterns
- Missing check on a privileged function. Anyone can call
transferOwnership,emergencyWithdraw,upgradeTo, orsetOraclebecause the modifier was never added. - Wrong
msg.sender. A function trustsmsg.senderafter adelegatecall, a meta-transaction forwarder, or a Uniswap V4 hook entry that never verifies the PoolManager (OWASP Cork case). - Unprotected initializer. A proxy's
initializecan run again, or a first-run path stays open, and an attacker becomes admin. - Trusted address by accident. A Hardhat test key, a deployer EOA, or a user-supplied "owner" field is treated as governance.
tx.originfor auth. A malicious contract relays a user's call sotx.originlooks authorized (SWC-115).
Example
A vulnerable contract
LiquidityPoolVulnerable lets anyone take ownership and drain the pooled balance. Example adapted from OWASP SC01.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
// THIS CONTRACT CONTAINS BUGS. DO NOT USE.
contract LiquidityPoolVulnerable {
address public owner;
mapping(address => uint256) public balances;
constructor() {
owner = msg.sender;
}
// Anyone can set a new owner.
function transferOwnership(address newOwner) external {
owner = newOwner;
}
// Missing: require(msg.sender == owner)
function emergencyWithdraw(address to, uint256 amount) external {
require(balances[address(this)] >= amount, "insufficient");
balances[address(this)] -= amount;
balances[to] += amount;
}
}Attack
The attack
- Call
transferOwnership(attacker). - Call
emergencyWithdraw(attacker, balances[address(this)]), or skip step 1 and callemergencyWithdrawdirectly because it has no owner check either.
No flash loan and no reentrancy are required. The bug is the missing gate.
Fix
The fix: explicit roles
Use battle-tested primitives such as OpenZeppelin Ownable or AccessControl, keep privileged roles few, and hold them on a multisig or governance module when the funds matter. The fixed sketch below (also from OWASP SC01) separates admin, governance, and guardian.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol";
contract LiquidityPoolSecure is AccessControl {
bytes32 public constant GOVERNANCE_ROLE = keccak256("GOVERNANCE_ROLE");
bytes32 public constant GUARDIAN_ROLE = keccak256("GUARDIAN_ROLE");
mapping(address => uint256) public balances;
event EmergencyWithdraw(address indexed to, uint256 amount, address indexed triggeredBy);
constructor(address governance, address guardian) {
_grantRole(DEFAULT_ADMIN_ROLE, governance);
_grantRole(GOVERNANCE_ROLE, governance);
_grantRole(GUARDIAN_ROLE, guardian);
}
function emergencyWithdraw(address to, uint256 amount)
external
onlyRole(GUARDIAN_ROLE)
{
require(to != address(0), "invalid to");
require(balances[address(this)] >= amount, "insufficient");
balances[address(this)] -= amount;
balances[to] += amount;
emit EmergencyWithdraw(to, amount, msg.sender);
}
}For upgradeable contracts, lock initializers after first use (initializer / reinitializer) so a second call cannot seize admin.
Testing
How to test for access control
Call every privileged function from an outsider
List every external and public function that changes state, moves funds, upgrades code, or changes roles. For each one, write a test that calls it from an unprivileged address and expects a revert. Then call each initializer a second time and confirm it reverts. The access control section of the checklist has the full list.
Search for weak patterns
rg -n "tx\.origin|selfdestruct|onlyOwner|transferOwnership|initialize\(" src/Confirm every hit has a real gate, not a comment that the check was "coming later."
Run a static analyzer
Slither and similar tools flag unprotected withdrawals and some missing modifiers. They do not prove that your role model matches the product design. Business-logic privilege still needs tests that encode "who may do what."
Scan the whole repository
A smart contract scanner runs these checks across every contract at once. Guardix lists access control among the risk classes each audit covers (Guardix pricing, smart contract audit for $200). A scan does not replace a manual review of who holds each role on mainnet.
Hacks
Real access control hacks
| Date | Protocol | Loss (reported) | What happened | Sources |
|---|---|---|---|---|
| Oct 18 to 19, 2026 | AlpsCon AI Trade Hub | 152,809 USDC (BNB Chain) | An access gate trusted the default Hardhat test account (public private key), chained with a reward logic bug | Guardix: DeFi hacks, Tozvan |
| May 2025 | Cork Protocol | About $11-12M | Uniswap V4 hook entry (beforeSwap) lacked onlyPoolManager; callers set arbitrary parameters | OWASP SC01, Dedaub |
| Aug 10, 2021 | Poly Network | $611M | A cross-chain message called a privileged function | Guardix OWASP SC01, rekt.news |
AlpsCon, October 2026. Guardix's DeFi hacks list records 152,809 USDC lost when a new deposit could receive rewards before it existed and the access gate trusted a public Hardhat test key (Tozvan write-up). Full entry: DeFi hacks in 2026.
Cork Protocol, May 2025. OWASP SC01 cites missing caller validation on Uniswap V4 hook callbacks. Attackers called the hook directly and credited themselves with derivative tokens (OWASP SC01).
Poly Network, 2021. A cross-chain message reached a privileged function. Guardix's OWASP page uses it as the older SC01 case next to AlpsCon.
More incidents by bug class: DeFi hacks in 2026. Ranked class list: OWASP Smart Contract Top 10 (2026), with SC01 at /vulnerabilities/owasp-smart-contract-top-10/#sc01-access-control.
FAQ
FAQ
- What is an access control vulnerability in a smart contract?
- A privileged action (withdraw, mint, upgrade, change roles) can be run by a caller who should not have that privilege, because the check is missing, wrong, or based on a spoofable identity.
- How do you fix missing access control in Solidity?
- Add an explicit check or role modifier on every privileged function, use libraries such as OpenZeppelin
OwnableorAccessControl, lock initializers, and put high-value roles behind a multisig or governance process you can rotate. - Is `onlyOwner` enough?
- It is enough only when a single owner is the real design and that owner key is protected. Most DeFi systems need separate roles for upgrades, pauses, and day-to-day ops, plus tests that an outsider cannot call any of them.
- Does a scanner find every access control bug?
- No. Pattern tools catch many unprotected withdrawals and missing modifiers. Wrong trust across modules, hooks, and bridges needs tests and review that follow each privileged path.
- Where does this sit in OWASP?
- SC01 (Access Control Vulnerabilities), first in the 2025 and 2026 Top 10 lists. Deep walk-through on Guardix: SC01 section. Official page: OWASP SC01:2026.