Solidity Security
Solidity is the primary language for Ethereum smart contracts. Understanding its quirks and common vulnerabilities is essential for security auditing.
Authorised Targets Only
Common Vulnerability Patterns
The three pairs below are the ones that keep reappearing in audit findings. All samples target
solc 0.8.34,
so the arithmetic checks discussed on
Integer Bugs
are already active and are not what these examples are about.
1. tx.origin as an Authentication Check
tx.origin is the
EOA that signed the transaction, and it stays the same through every intermediate call. Any contract the owner
is tricked into calling can therefore pass this check on the owner's behalf — the phishing contract in
Access Control
is the working version. Authorise
msg.sender instead.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
contract Wallet {
address public owner;
constructor() {
owner = msg.sender;
}
// VULNERABLE: tx.origin is the account that started the whole
// transaction, not the contract that is calling right now.
function withdraw() public {
require(tx.origin == owner, "not owner");
payable(msg.sender).transfer(address(this).balance);
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
contract Wallet {
address public owner;
constructor() {
owner = msg.sender;
}
// VULNERABLE: tx.origin is the account that started the whole
// transaction, not the contract that is calling right now.
function withdraw() public {
require(tx.origin == owner, "not owner");
payable(msg.sender).transfer(address(this).balance);
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
contract Wallet {
address public owner;
constructor() {
owner = msg.sender;
}
// FIXED: authorise the immediate caller, and use call so a
// contract owner is not blocked by the 2300-gas stipend.
function withdraw() public {
require(msg.sender == owner, "not owner");
(bool ok, ) = payable(msg.sender).call{value: address(this).balance}("");
require(ok, "transfer failed");
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
contract Wallet {
address public owner;
constructor() {
owner = msg.sender;
}
// FIXED: authorise the immediate caller, and use call so a
// contract owner is not blocked by the 2300-gas stipend.
function withdraw() public {
require(msg.sender == owner, "not owner");
(bool ok, ) = payable(msg.sender).call{value: address(this).balance}("");
require(ok, "transfer failed");
}
}2. Unchecked ERC-20 Transfer
transfer returns
a bool, and plenty of live tokens return false
instead of reverting while others return nothing at all. Ignoring the result turns both cases into a silent
no-op that your accounting already treats as paid.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
interface IERC20 {
function transfer(address to, uint256 amount) external returns (bool);
}
contract Payout {
IERC20 public immutable token;
constructor(address token_) {
token = IERC20(token_);
}
// VULNERABLE: the return value is discarded, so a token that
// returns false fails silently and the caller sees success.
function pay(address to, uint256 amount) public {
token.transfer(to, amount);
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
interface IERC20 {
function transfer(address to, uint256 amount) external returns (bool);
}
contract Payout {
IERC20 public immutable token;
constructor(address token_) {
token = IERC20(token_);
}
// VULNERABLE: the return value is discarded, so a token that
// returns false fails silently and the caller sees success.
function pay(address to, uint256 amount) public {
token.transfer(to, amount);
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
contract Payout {
using SafeERC20 for IERC20;
IERC20 public immutable token;
constructor(address token_) {
token = IERC20(token_);
}
// FIXED: safeTransfer reverts on a false return and tolerates
// tokens such as USDT that return nothing at all.
function pay(address to, uint256 amount) public {
token.safeTransfer(to, amount);
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
contract Payout {
using SafeERC20 for IERC20;
IERC20 public immutable token;
constructor(address token_) {
token = IERC20(token_);
}
// FIXED: safeTransfer reverts on a false return and tolerates
// tokens such as USDT that return nothing at all.
function pay(address to, uint256 amount) public {
token.safeTransfer(to, amount);
}
}3. delegatecall to a Caller-Supplied Target
delegatecall runs
the target's bytecode against the calling contract's storage, so a caller who controls the target also
controls your storage layout. The original page shipped only the vulnerable half; the mitigation is not a
language feature you can switch on, it is a design constraint — pin the implementation address at deployment,
restrict who may invoke it, and never pass a target chosen by the caller.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
contract Proxy {
address public owner;
constructor() {
owner = msg.sender;
}
// VULNERABLE: the caller chooses the target AND the calldata.
// delegatecall executes in THIS contract's storage, so the target
// can run "owner = attacker" and take the proxy over.
function execute(address target, bytes calldata data) public {
(bool ok, ) = target.delegatecall(data);
require(ok, "delegatecall failed");
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
contract Proxy {
address public owner;
constructor() {
owner = msg.sender;
}
// VULNERABLE: the caller chooses the target AND the calldata.
// delegatecall executes in THIS contract's storage, so the target
// can run "owner = attacker" and take the proxy over.
function execute(address target, bytes calldata data) public {
(bool ok, ) = target.delegatecall(data);
require(ok, "delegatecall failed");
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
contract Proxy {
address public owner;
address public immutable logic;
constructor(address logic_) {
owner = msg.sender;
logic = logic_;
}
// FIXED: the implementation is fixed at deployment, so calldata can
// no longer select it; only the owner may invoke it; and the
// implementation is trusted to write only the storage it owns.
function execute(bytes calldata data) public {
require(msg.sender == owner, "not owner");
(bool ok, ) = logic.delegatecall(data);
require(ok, "delegatecall failed");
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
contract Proxy {
address public owner;
address public immutable logic;
constructor(address logic_) {
owner = msg.sender;
logic = logic_;
}
// FIXED: the implementation is fixed at deployment, so calldata can
// no longer select it; only the owner may invoke it; and the
// implementation is trusted to write only the storage it owns.
function execute(bytes calldata data) public {
require(msg.sender == owner, "not owner");
(bool ok, ) = logic.delegatecall(data);
require(ok, "delegatecall failed");
}
}Why the Fixed Proxy Is Only Half the Story
Static Analysis with Slither
# Run Slither on contract
slither contracts/MyContract.sol
# Output to JSON for further processing
slither contracts/ --json output.json
# Check specific detector
slither . --detect reentrancy-eth
# Common findings:
# - reentrancy-eth (high severity)
# - uninitialized-state
# - arbitrary-send
# - controlled-delegatecall
# - unchecked-transfer
# Integrate with CI/CD
slither . --fail-pedanticRelated Topics
Fundamentals
The EVM model these patterns depend on, and a local chain to reproduce them on.
Reentrancy
The external-call ordering bug that turns a correct authorisation check into a drained contract.
Source Code Review
The general sink-first review method to apply when a static analyser returns nothing useful.