Arithmetic and Access Control Flaws
Incorrect arithmetic and missing authorisation are two of the most common causes of loss. This lesson explains how Solidity 0.8 changed overflow handling, the arithmetic risks that remain, and the access control mistakes to look for, including tx.origin.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how Solidity 0.8 changed overflow handling.
- Identify the arithmetic risks that remain.
- Identify common access control flaws.
- Explain why tx.origin must not be used for authorisation.
Overflow before and after Solidity 0.8
Integer types have fixed ranges: a uint8 holds 0 to 255 and a uint256 holds 0 to 2256 − 1. Before Solidity 0.8.0, arithmetic that exceeded the range wrapped around silently: 255 + 1 became 0 in a uint8, and 0 − 1 became the maximum value. Attackers used this to create enormous token balances, and developers relied on the SafeMath library to add checks.
Since 0.8.0, overflow and underflow revert with a Panic(0x11) error by default. Wrapping happens only inside an unchecked { ... } block, which developers use deliberately to save gas where overflow is impossible. SafeMath is no longer needed in 0.8 code.
Arithmetic risks that remain
Checked arithmetic does not remove every numeric risk. The following table lists those that remain.
| Risk | Example | Mitigation |
|---|---|---|
| unchecked blocks | A user-controlled value subtracted inside unchecked | Use unchecked only where bounds are proven |
| Unsafe downcasts | uint8(x) with x = 300 silently becomes 44 | Check ranges or use OpenZeppelin SafeCast |
| Rounding down | amount * 3 / 1000 is 0 for small amounts | Set minimums; round in the protocol's favour |
| Divide before multiply | a / b * c loses precision | Multiply first: a * c / b |
| Older compilers | Contracts compiled with 0.7 or earlier | Check the pragma; review with SafeMath assumptions |
Arithmetic that still goes wrong
Access control flaws
Access control decides who may call each function. It tops the 2025 OWASP Smart Contract Top 10 because a single missing check can hand an attacker the whole contract. Typical flaws are listed below.
- A sensitive function, such as
mint,setPrice,withdrawAllorupgradeTo, with no modifier. - An
initializefunction that anyone can call, or call again, to make themselves owner. - A function intended to be internal declared
public. - Authorisation based on
tx.origin. - Roles that are never revoked, or a single externally owned account holding every privilege.
Spotting access control flaws
Why tx.origin is unsafe
tx.origin is the externally owned account that signed the transaction, however many contracts the call passes through; msg.sender is the immediate caller. The following comparison shows the difference when the owner is tricked into calling a malicious contract that then calls the wallet.
Always authorise with msg.sender. The only legitimate use of tx.origin is rare, such as checking msg.sender == tx.origin to detect a direct call from an EOA, and even that is weakened by EIP-7702 delegation.
msg.sender or tx.origin?
An unprotected initialiser
An upgradeable contract's implementation has function initialize(address admin) public { owner = admin; }. The deployment script calls it on the proxy one block after deployment. What can go wrong?
Anyone watching the mempool can call initialize first with their own address and become owner, and nothing stops a second call later. The function needs OpenZeppelin's initializer modifier so it runs only once, the proxy should be initialised in the same transaction as its deployment (by passing the call data to the proxy's constructor), and the implementation should call _disableInitializers() in its constructor.
A fee that rounds to zero
A protocol charges fee = amount * 25 / 10000 (0.25%). An attacker makes many transfers of 399 units. How much fee do they pay?
399 × 25 = 9,975, and 9,975 / 10,000 = 0 in integer division, so each transfer pays no fee. Splitting amounts avoids fees entirely. The protocol should enforce a minimum amount or round the fee up, for example with OpenZeppelin's Math.mulDiv rounding mode.
In Solidity 0.8, what happens when a uint256 subtraction underflows outside an unchecked block?
Show answer
The transaction reverts with a Panic(0x11) error. Wrapping only happens inside unchecked blocks.
Exam Tips
Remember for the exam
- Solidity 0.8 reverts on overflow except inside unchecked; SafeMath is unnecessary in 0.8.
- Downcasts truncate silently; multiply before dividing; watch for rounding to zero.
- Every sensitive function and initialiser needs explicit access control.
- Authorise with msg.sender, never tx.origin.