Smart contract access control vulnerabilities: who can call what

An access control vulnerability is a function that lets the wrong caller change owners, move funds, upgrade code or destroy a contract. Most cases come down to a missing check, an initializer anyone can call, or a privileged contract that runs calls an attacker chooses. Map every privileged function to the role that should call it, and test each one from an account that should be refused.

Last updated . 5 minute read. By the QuillShield team.

How do access control bugs happen?

  • A missing check. A function that sets a fee, an oracle or an owner has no onlyOwner or role check.
  • An open initializer. Contracts behind a proxy are set up by an initialize function instead of a constructor. If anyone can call it, or call it twice, the caller becomes the owner.
  • A privileged contract that runs chosen calls. A bridge, router or multicall contract that holds a role and executes a target and calldata taken from user input lends that role to every user.
  • The wrong caller. Checking tx.origin instead of msg.sender, or putting a role check on the wrong function.

What does vulnerable code look like?

This treasury checks the owner on withdrawals, but anyone can call initialize and become the owner, at any time.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

interface IERC20 {
    function transfer(address to, uint256 amount) external returns (bool);
}

contract Treasury {
    address public owner;

    // Meant to run once, right after deployment. Anyone can call it, at any time.
    function initialize(address owner_) external {
        owner = owner_;
    }

    function withdraw(IERC20 token, address to, uint256 amount) external {
        require(msg.sender == owner, "not owner");
        require(token.transfer(to, amount), "transfer failed");
    }
}

How do you fix it?

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

interface IERC20 {
    function transfer(address to, uint256 amount) external returns (bool);
}

contract SafeTreasury {
    address public owner;
    bool private initialized;

    constructor() {
        // Locks the implementation behind a proxy, so nobody can claim it.
        initialized = true;
    }

    function initialize(address owner_) external {
        require(!initialized, "already initialized");
        require(owner_ != address(0), "zero owner");
        initialized = true;
        owner = owner_;
    }

    function withdraw(IERC20 token, address to, uint256 amount) external {
        require(msg.sender == owner, "not owner");
        require(token.transfer(to, amount), "transfer failed");
    }
}

The fixed contract allows initialize once and rejects a zero owner. Its constructor marks the implementation as initialized, so behind a proxy only the proxy's own storage can be set up, by a deployment that calls initialize in the same transaction. OpenZeppelin's Initializable does the same with the initializer modifier and _disableInitializers().

  • Give each power its own role, so the key that pauses a market can't also upgrade it.
  • Change owners in two steps, propose then accept, so a typo can't hand the protocol to a dead address.
  • Never let a privileged contract call targets or calldata that users choose, or block calls to anything that holds a role.
  • Keep admin roles in a multisig, and put a timelock on upgrades.

How has it been exploited?

Parity's post-mortem describes the best-known case. Its multi-sig wallets deployed after 20 July 2017 shared one library contract, which had the same functions as a wallet and still needed initialization. Nobody had initialized it. On 6 November 2017 a user called the initialization function, became the library's owner, and then called its self-destruct function. With the library gone, 587 wallets holding 513,774.16 ETH, plus other tokens, could no longer move their funds.

Access checks can also fail one step away. In August 2021 Poly Network's cross-chain manager contract would execute a call named in a cross-chain message, and it owned the contract that stored the keepers' public keys. According to analyses by SlowMist and CertiK, an attacker sent a message whose call replaced those keys with their own, then moved about $610M.

What should a reviewer check?

  • List every function that changes owners, roles, fees, prices, oracles, pauses or upgrades, or that moves funds, with the role that should call it.
  • Does each of those functions check the caller itself, rather than relying on a front end or a router?
  • Are initializers protected, called in the deployment transaction and disabled on the implementation behind a proxy?
  • Does any contract with a role make calls to targets or with calldata that a user chooses?
  • Is msg.sender used for authorization, never tx.origin?
  • Are ownership changes two-step, and can every role be revoked?
  • Do tests call each privileged function from an unauthorized account and expect a revert?

How does QuillShield check for it?

Access control bugs are about intent, because the code can't tell a reviewer which account should hold which power. Before a QuillShield full audit you can add context such as the whitepaper, trust assumptions, privileged roles and previous audit reports. Write down who should hold each role, because the audit covers the whole codebase and can only compare the code with the intent it is given. Each finding that holds up comes with the exact lines, the impact and a recommended fix.

A new admin function or a changed modifier can arrive in any pull request. QuillShield checks every pull request against the audit, and on GitHub, with branch protection or rulesets, a required QuillShield check blocks the merge while the pull request has a High or Critical finding.

Full audits start at $49 and are priced from the lines of Solidity in scope, as the pricing page shows. The guide to running your first audit walks through getting a quote for your own code. Sign up free and you see the exact price before anything is charged.

Common questions

Is onlyOwner enough?

For one admin it can be, if the owner is a multisig and ownership changes in two steps. Larger protocols need separate roles, so one key can't do everything.

Why do initializers need protection?

A contract behind a proxy has no constructor for its storage, so an initialize function sets it up. If anyone can call that function, or call it twice, they can make themselves the owner, as happened to Parity's wallet library in 2017.

Should I check tx.origin?

No. tx.origin is the account that started the transaction, so any contract the user calls can act as them. Check msg.sender.

How do I test access control in Foundry?

For each privileged function, call it with vm.prank from an account without the role and expect a revert, then call it with the right role and check the effect.

Sources

QuillShield is building the security layer between AI and money, so you can keep shipping between audits.

Sign-up is free, and new accounts get $50 in credit, good for up to half of an order.

  1. 1Sign up and import a repo
  2. 2See the exact price before anything is charged
  3. 3Approve it, and the audit starts