Solidity Solidity Security and Vulnerabilities 1 — Questions and Answers
Question 1: Which attack exploits a contract that calls an external contract before updating its state?
- Reentrancy attack (Correct answer)
- Front-running attack
- Overflow attack
- Denial of service attack
Correct answer: Reentrancy attack
A reentrancy attack occurs when an external contract repeatedly calls back into the vulnerable contract before the original function finishes updating its state.
Question 2: What is the recommended pattern to prevent reentrancy attacks in Solidity?
- Checks-Effects-Interactions pattern (Correct answer)
- Factory pattern
- Proxy pattern
- Observer pattern
Correct answer: Checks-Effects-Interactions pattern
The Checks-Effects-Interactions pattern requires you to perform all state changes (effects) before calling external contracts (interactions), preventing reentrancy.
Question 3: What Solidity modifier from OpenZeppelin prevents reentrancy by locking the function?
- nonReentrant (Correct answer)
- onlyOwner
- whenNotPaused
- safeCall
Correct answer: nonReentrant
The `nonReentrant` modifier from OpenZeppelin's ReentrancyGuard sets a flag that reverts any recursive calls into the same function.
Question 4: Which vulnerability allows an attacker to manipulate transaction ordering to their advantage?
- Front-running (MEV) (Correct answer)
- Integer overflow
- Reentrancy
- Delegatecall injection
Correct answer: Front-running (MEV)
Front-running (Miner Extractable Value) lets attackers observe pending transactions and submit their own transactions with higher gas fees to execute first.
Question 5: What is the danger of using `tx.origin` for authentication in Solidity?
- It can be spoofed via phishing contracts to impersonate users (Correct answer)
- It costs more gas than msg.sender
- It is deprecated in Solidity 0.8
- It only works with EOAs
Correct answer: It can be spoofed via phishing contracts to impersonate users
A malicious contract can trick a user into calling it, then use `tx.origin` (the original EOA) to pass authentication checks in a target contract.
Question 6: What is a 'griefing' attack in the context of smart contract security?
- An attack that causes a contract to revert or waste gas without direct profit to the attacker (Correct answer)
- An attack that steals ETH from a contract
- An attack that upgrades a proxy to malicious code
- An attack that drains ERC-20 allowances
Correct answer: An attack that causes a contract to revert or waste gas without direct profit to the attacker
A griefing attack causes a contract to malfunction or waste resources, harming users without necessarily benefiting the attacker financially.
Which attack exploits a contract that calls an external contract before updating its state?