# Storage collision: when two contracts write the same slot

URL: https://shield.quillaudits.com/learn/vulnerabilities/storage-collision/

A storage collision is when two pieces of code that share one contract's storage, usually a proxy and its implementation or two versions of the same implementation, use the same slot for different variables. One side writes an address, the other reads it as a balance or a flag, and the contract's state quietly stops meaning what either side thinks it means. Keep proxy variables in hashed slots, keep implementation state in a namespace or only ever append to it, and compare storage layouts before every upgrade.

Last updated 3 December 2026. 6 minute read. By the QuillAudits Team.

## How does Solidity decide where a variable lives?

By declaration order. The [Solidity docs](https://docs.soliditylang.org/en/latest/internals/layout_in_storage.html) say data is stored 'contiguously item after item starting with the first state variable, which is stored in slot `0`', and items smaller than 32 bytes are packed into one slot where they fit. With inheritance, variables follow 'the C3-linearized order of contracts starting with the most base-ward contract'. A mapping's values sit at `keccak256(h(k) . p)`, a hash of the key and the mapping's own slot.

Names don't matter, only positions. When two contracts share storage, which is what delegatecall does, slot 3 of one is slot 3 of the other, whatever each calls it.

## How do storage collisions happen?

- **Proxy and implementation.** The proxy keeps its implementation address or its admin in slot 0, and the implementation's first variable is also slot 0.
- **A variable inserted or reordered in an upgrade.** Version 2 adds a variable above existing ones, and every variable after it now reads its neighbour's old value.
- **A new parent contract.** Parents come first in the layout, so adding a base contract with state shifts every variable of the child.
- **A changed type.** A `uint128` packed next to another small variable shares a slot with it; changing it to `uint256` moves everything after it.
- **Shared storage between modules.** Libraries, plugins or facets that are called with delegatecall and declare their own state from slot 0. [ERC-7201](https://eips.ethereum.org/EIPS/eip-7201) names modular designs with shared delegatecall storage as one reason it exists.

## What does vulnerable code look like?

This proxy keeps its implementation address in slot 0 and its admin in slot 1. The sale contract behind it declares `owner` and `raised` in the same two slots.

**Vulnerable: proxy and implementation share slots 0 and 1 (illustrative, not audited code)** `CollidingProxy.sol`

```
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

contract SimpleProxy {
    address public implementation; // slot 0
    address public admin; // slot 1

    constructor(address implementation_) {
        implementation = implementation_;
        admin = msg.sender;
    }

    function upgradeTo(address newImplementation) external {
        require(msg.sender == admin, "not admin");
        implementation = newImplementation;
    }

    fallback() external payable {
        address impl = implementation;
        assembly {
            calldatacopy(0, 0, calldatasize())
            let ok := delegatecall(gas(), impl, 0, calldatasize(), 0, 0)
            returndatacopy(0, 0, returndatasize())
            switch ok
            case 0 { revert(0, returndatasize()) }
            default { return(0, returndatasize()) }
        }
    }

    receive() external payable {}
}

contract SaleV1 {
    address public owner; // slot 0: where the proxy keeps its implementation address
    uint256 public raised; // slot 1: where the proxy keeps its admin
    bool public initialized; // slot 2

    function initialize(address owner_) external {
        require(!initialized, "already initialized");
        initialized = true;
        owner = owner_; // through the proxy, this replaces the implementation address
    }

    function buy() external payable {
        raised += msg.value; // through the proxy, this changes the admin address
    }
}
```

Called through the proxy, `initialize` writes the new owner into slot 0, so the proxy now delegates every call to the owner's address. If that is a wallet with no code, every call 'succeeds' and does nothing, because the EVM treats a call to an address without code as a success. And `buy` writes slot 1 on every purchase, so any purchase made while the proxy still points at the sale contract shifts the admin to a new address that nobody is likely to control, and the proxy can no longer be upgraded.

## How do you fix it?

Move the proxy's own variables to slots derived from a hash, where nothing declared in order will land, and give the implementation's state a namespace of its own. ERC-7201 defines that namespace as `erc7201(id: string) = keccak256(keccak256(id)-1) & ~0xff`, and its rationale says the inner hash minus one is 'chosen as a location that is unused by Solidity'.

**Fixed: hashed proxy slots and ERC-7201 namespaced state (illustrative, not audited code)** `NamespacedProxy.sol`

```
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

contract SlotProxy {
    // Slots taken from a hash. Variables declared in order never reach them.
    bytes32 private constant IMPLEMENTATION_SLOT = bytes32(uint256(keccak256("example.proxy.implementation")) - 1);
    bytes32 private constant ADMIN_SLOT = bytes32(uint256(keccak256("example.proxy.admin")) - 1);

    constructor(address implementation_) {
        _setAddress(IMPLEMENTATION_SLOT, implementation_);
        _setAddress(ADMIN_SLOT, msg.sender);
    }

    function upgradeTo(address newImplementation) external {
        require(msg.sender == _getAddress(ADMIN_SLOT), "not admin");
        require(newImplementation.code.length > 0, "no code");
        _setAddress(IMPLEMENTATION_SLOT, newImplementation);
    }

    function _getAddress(bytes32 slot) private view returns (address value) {
        assembly {
            value := sload(slot)
        }
    }

    function _setAddress(bytes32 slot, address value) private {
        assembly {
            sstore(slot, value)
        }
    }

    fallback() external payable {
        address impl = _getAddress(IMPLEMENTATION_SLOT);
        assembly {
            calldatacopy(0, 0, calldatasize())
            let ok := delegatecall(gas(), impl, 0, calldatasize(), 0, 0)
            returndatacopy(0, 0, returndatasize())
            switch ok
            case 0 { revert(0, returndatasize()) }
            default { return(0, returndatasize()) }
        }
    }

    receive() external payable {}
}

contract SaleV2 {
    /// @custom:storage-location erc7201:example.sale
    struct SaleStorage {
        address owner;
        uint256 raised;
        bool initialized;
    }

    // keccak256(abi.encode(uint256(keccak256("example.sale")) - 1)) & ~bytes32(uint256(0xff))
    // = 0x0799ab80bb22c7a52b2f077b2f255a7b031d7366e36c383726ef58f91f7a7100
    bytes32 private constant SALE_STORAGE =
        keccak256(abi.encode(uint256(keccak256("example.sale")) - 1)) & ~bytes32(uint256(0xff));

    constructor() {
        _sale().initialized = true; // locks the implementation itself
    }

    function _sale() private pure returns (SaleStorage storage s) {
        bytes32 slot = SALE_STORAGE;
        assembly {
            s.slot := slot
        }
    }

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

    function buy() external payable {
        _sale().raised += msg.value;
    }

    function owner() external view returns (address) {
        return _sale().owner;
    }

    function raised() external view returns (uint256) {
        return _sale().raised;
    }
}
```

- **Keep proxy state out of the ordered slots.** Store the implementation and admin addresses at hashed slots, as `SlotProxy` does.
- **Give each contract's state a namespace.** ERC-7201 records it with the NatSpec annotation `@custom:storage-location erc7201:<id>` on the struct. New variables go at the end of the struct.
- **Never reorder, remove or retype a variable in an upgrade,** and never add a parent contract with state above existing variables. Unused fixed-size arrays at the end of a base contract (storage gaps) are an older way to leave room, and they need resizing with every change.
- **Compare layouts before every upgrade.** The compiler reports each contract's layout through its standard JSON interface, with the `slot`, `offset`, `label` and `type` of every variable, so a pull request can diff version 1 against version 2.

## How has a storage collision been exploited?

| Protocol | Audius |
| --- | --- |
| Date | 23 July 2022 |
| Chain | Ethereum |
| Loss | 18,564,497 AUDIO, about $6M |
| What happened | Audius's proxy kept its proxyAdmin address in storage slot 0, the slot where the OpenZeppelin Initializable code in its contracts kept the initialized and initializing flags. Read as those flags, the address's bytes came out true, so the initializer check always passed and initialize could be called again. From 22:54 UTC on 23 July 2022 an attacker re-initialized Audius's Governance, Staking and DelegateManagerV2 contracts, recorded delegations of 10 trillion AUDIO to themselves, passed a governance proposal and moved 18,564,497 AUDIO out of the community treasury. |
| Sources | [Audius](https://blog.audius.co/posts/audius-governance-takeover-post-mortem-7-23-22), [rekt.news](https://rekt.news/audius-rekt) |

[rekt.news](https://rekt.news/audius-rekt) valued the AUDIO at about $6M and reports that the attacker sold it in one Uniswap v2 trade for 704 ETH, about $1M. Audius's [post-mortem](https://blog.audius.co/posts/audius-governance-takeover-post-mortem-7-23-22) describes the fix: a modified Initializable with two padding fields, so the admin address and the flags no longer share a slot, and only the proxyAdmin can initialize the contracts.

## What should a reviewer check?

- For every proxy, where does it keep its implementation and admin addresses: slot 0 and 1, or hashed slots?
- For every upgrade, diff the old and new storage layouts. Did any existing variable move, change type or disappear?
- Did the upgrade add a parent contract, or reorder the inheritance list?
- Do libraries, plugins or facets called with delegatecall declare state variables of their own?
- Is each namespace id unique across the codebase, and does the slot constant match the ERC-7201 formula?
- Is there assembly that writes `.slot` or calls `sstore` directly? The [Solidity docs](https://docs.soliditylang.org/en/latest/assembly.html) warn that using `.slot` to overlap variables lies outside the compiler's guarantees.
- Is there a test that upgrades a deployed proxy with real state and checks every variable reads the same afterwards?

## How does QuillShield check for storage collisions?

A collision is invisible in any single file: it only appears when two layouts are laid side by side, the proxy's and the implementation's, or version 1's and version 2's. A QuillShield full audit reads the whole Solidity codebase in your Foundry project, builds a security graph of the code and lists the invariants it must keep, and a separate reviewer agent tries to disprove each finding before you see it. Each finding that holds up includes the exact lines, the impact and a recommended fix, with a proof of concept on full audits.

Previous audit reports are among the context you can add before an audit, along with the whitepaper, trust assumptions and privileged roles; for an upgrade, include the report for the version that is live. After that, QuillShield checks each pull request on GitHub or GitLab.com against the audit's security graph, which is where a reordered variable or a new parent contract usually turns up.

A full audit is priced from the lines of Solidity in scope and starts at $49; the [pricing page](https://shield.quillaudits.com/pricing/) lists examples. [Start free](https://shield.quillaudits.com/register) to get a quote for your own upgrade.

## FAQ

### Does the order of state variables matter in an upgradeable contract?

Yes. Slots come from declaration order and inheritance order, so moving, inserting or removing a variable shifts every variable after it. Append only, or use namespaced storage.

### What are storage gaps?

A fixed-size array left unused at the end of a base contract, such as `uint256[50] __gap`, that later versions shrink when they add variables. They work, but each change has to resize the gap exactly; ERC-7201 namespaces avoid that bookkeeping.

### Can a storage collision happen without a proxy?

Yes, whenever code runs in another contract's storage through delegatecall: libraries with state, plugins and diamond facets. See [delegatecall vulnerability](https://shield.quillaudits.com/learn/vulnerabilities/delegatecall-vulnerability/).

### Is storage collision in the SWC Registry?

Not as such. The nearest entry, [SWC-124, Write to Arbitrary Storage Location](https://swcregistry.io/docs/SWC-124/), covers a different bug: an attacker writing to a storage location of their choosing, such as the slot that holds the owner.

## Sources

- [Layout of state variables in storage and transient storage, Solidity documentation](https://docs.soliditylang.org/en/latest/internals/layout_in_storage.html) · checked 3 December 2026
- [ERC-7201: Namespaced Storage Layout, Ethereum Improvement Proposals](https://eips.ethereum.org/EIPS/eip-7201) · checked 3 December 2026
- [Inline assembly, Solidity documentation](https://docs.soliditylang.org/en/latest/assembly.html) · checked 3 December 2026
- [Expressions and control structures: external function calls, Solidity documentation](https://docs.soliditylang.org/en/latest/control-structures.html) · checked 3 December 2026
- [SWC-124: Write to Arbitrary Storage Location, SWC Registry](https://swcregistry.io/docs/SWC-124/) · checked 3 December 2026
- [Audius Governance Takeover Post-Mortem 7/23/22, Audius](https://blog.audius.co/posts/audius-governance-takeover-post-mortem-7-23-22) · checked 3 December 2026
- [Audius (REKT), rekt.news](https://rekt.news/audius-rekt) · checked 3 December 2026
