Lesson 3 of 5 Smart Contract Vulnerabilities

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.

RiskExampleMitigation
unchecked blocksA user-controlled value subtracted inside uncheckedUse unchecked only where bounds are proven
Unsafe downcastsuint8(x) with x = 300 silently becomes 44Check ranges or use OpenZeppelin SafeCast
Rounding downamount * 3 / 1000 is 0 for small amountsSet minimums; round in the protocol's favour
Divide before multiplya / b * c loses precisionMultiply first: a * c / b
Older compilersContracts compiled with 0.7 or earlierCheck the pragma; review with SafeMath assumptions
Fill in the number

Arithmetic that still goes wrong

Exam skills 3.1.2

Calculate the results that checked arithmetic does not protect against.

Log in to try this activity Interactive activities are free with an account.

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, withdrawAll or upgradeTo, with no modifier.
  • An initialize function 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.
Select all that apply

Spotting access control flaws

Exam skills 3.1.2

Identify which designs contain an access control flaw.

Log in to try this activity Interactive activities are free with an account.

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.

Check tx.origin == owner
Value inside the wallettx.origin = owner
ResultCheck passes: funds are stolen
Check msg.sender == owner
Value inside the walletmsg.sender = malicious contract
ResultCheck fails: call reverts

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.

Scenario questions

msg.sender or tx.origin?

Exam skills 3.1.2

Work out what each value holds when the owner is tricked into calling a malicious contract.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Worked example

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.

Check yourself

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.