Overview
Curated: · Written: · Reviewed:
Smart Contract Security & Common Vulnerabilities
In one line
Smart contract security means showing that a contract's invariants (solvency, conservation, authorization) still hold when anyone can call any function, in any order, from a contract the attacker wrote. Reach for it whenever a contract holds value, is upgradeable, or depends on prices, signatures, randomness or external tokens.
Key definitions
- Invariant: a predicate that must hold in every reachable state, such as assets >= outstanding claims.
- Reentrancy: control returns to a caller through a callback before the contract has restored its state.
- Trust boundary: any input you do not control: a token, an oracle, an admin key, a signature, a callee.
- msg.sender vs tx.origin: the immediate caller vs the original transaction signer.
- Storage collision: a proxy and its implementation disagree on slot layout under
delegatecall. - Read-only reentrancy: a view returns intermediate state that another protocol trusts and acts on.
Formulas and complexity
transfer and send forward a fixed 2300 gas.
How it works
A CALL hands control to the callee, which can run arbitrary code and call back into your contract. If the contract updates its state after that call, the callee sees the old state and can repeat the action. The fix is to restore the invariant before control leaves.
In the vulnerable order, the balance is zeroed after the transfer, so each re-entry pays out again. In the fixed order (effect before interaction), the inner call reads zero, fails its check, and the outer require(ok) reverts the whole transaction.
When to use and when not to
| Decision | Choose | Avoid | What decides it |
|---|---|---|---|
| Ether transfer | call with return check, or pull payments | transfer / send to logic-bearing recipients | the 2300-gas stipend breaks any recipient with logic |
| Price source | TWAP or independent feed | spot pool reserves | cost to move the price within one transaction |
| Upgradeability | proxy with timelocked admin | proxy with no upgrade path | the upgrade authority is itself a trust boundary |
| Randomness | VRF with callback | prevrandao, block.timestamp for value | value at stake vs proposer bias window |
| Pause | scoped pause on the risky path | global freeze that traps users | whether the incident outruns governance |
Traps interviewers probe
- Authorization on
tx.origin. An intermediary contract preserves the victim astx.origin, so a phished user's call passes the check. Fix: checkmsg.sender, and enforce roles at the state-changing entry point. - Trusting a reentrancy guard. Guards are per function and per contract, so they miss sibling functions, other contracts sharing state, and read-only reentrancy. Fix: checks-effects-interactions first, then guard, then test malicious callbacks across the whole call graph.
- Trusting call success or return values. A
truefrom a low-level call only means no revert at the EVM boundary, and some tokens return false or take fees. Fix: check the balance delta your invariant needs. - Leaving the initializer open. Proxy constructors do not initialize proxy storage, so the first caller of an exposed
initializebecomes owner. Fix: initialize atomically in the deployment transaction, lock the implementation, and test an already-initialized state. - Pricing collateral from a spot pool. A flash loan can move the reserves inside one transaction. Fix: use a TWAP or an independently governed feed, with staleness checks, decimal normalization and a defined fallback.
Interview tips
Open with the three questions: which invariant, who can trigger the break, and what bounds the damage. Then name the attack and the mitigation with its trade-off.
Follow-ups to expect:
- Why is
tx.originunsafe butmsg.senderfine? - Can reentrancy happen without Ether? Yes: ERC-777 hooks,
onERC721Received, any callback. - Why is read-only reentrancy dangerous if nothing is written?
callvstransfer, and why?- How do you test this? Stateful invariant fuzzing with malicious callbacks and fork tests against real integrations.
- What is your maximum credible loss?
30-second answer outline:
- Name the invariant the contract must keep (for example, conservation of balances).
- Name the adversary and the callback or input that breaks it.
- Trace the two or three calls that reach the broken state.
- State the fix: effect before interaction,
msg.sender, a balance-delta check, or a TWAP. - State the residual risk and what bounds it: a timelock, a scoped pause, or a cap.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.