CBSE - Certified Blockchain Security Expert Smart Contract Vulnerabilities Questions and Answers 1 — Questions and Answers
Question 1: A smart contract function updates a user's balance after making an external call to an untrusted address. Which vulnerability is most likely to be exploited in this scenario?
- Integer Overflow
- Reentrancy (Correct answer)
- Timestamp Dependency
- Gas Limit DoS
Correct answer: Reentrancy
A reentrancy attack occurs when a function makes an external call to another contract before it updates its own state. An attacker can create a malicious contract with a fallback function that calls back into the original function repeatedly, draining funds before the balance is updated. The recommended mitigation is to follow the 'Checks-Effects-Interactions' pattern, where state changes are made *before* external calls.
Question 2: A DeFi lending platform uses `block.timestamp` to determine when a user's locked assets can be withdrawn. A miner with significant hash power successfully manipulates the timestamps of the blocks they mine. Which of the following vulnerabilities are they exploiting?
- Front-Running
- Access Control Violation
- Timestamp Dependency (Correct answer)
- Integer Underflow
Correct answer: Timestamp Dependency
Timestamp dependency occurs when a smart contract relies on the block's timestamp for critical operations. Miners have a degree of control over the timestamp of a block they mine (within a certain range), which they can manipulate to their advantage. This could allow them to withdraw assets prematurely or affect other time-sensitive logic within the contract.
Question 3: A developer is creating an ERC-20 token contract using Solidity version 0.7.0. The `transfer` function subtracts the amount from the sender's balance without using a safe math library. What vulnerability could a user with a balance of 100 tokens exploit if they try to transfer 110 tokens?
- Reentrancy
- Short Address Attack
- Denial of Service
- Integer Underflow (Correct answer)
Correct answer: Integer Underflow
Integer underflow happens when an arithmetic operation results in a value smaller than the minimum value for that data type, causing it to wrap around to the maximum value. In Solidity versions before 0.8.0, subtracting 110 from 100 would underflow the `uint` balance, resulting in a very large number instead of reverting the transaction. Modern Solidity versions (0.8.0+) have built-in protection against this.
Question 4: An attacker observes a large buy order for a specific token in the mempool of a decentralized exchange. They quickly submit their own buy order for the same token with a higher gas fee, followed immediately by a sell order. What is this type of attack called?
- Timestamp Dependency
- Front-Running (Sandwich Attack) (Correct answer)
- Integer Overflow
- Unchecked External Call
Correct answer: Front-Running (Sandwich Attack)
This is a classic example of a front-running attack, specifically a 'sandwich attack'. The attacker sees a pending transaction in the mempool and places a transaction before it (by paying a higher gas fee) and another one after it. This allows them to profit from the price slippage caused by the victim's large trade.
Question 5: A smart contract for a decentralized lottery game uses the `blockhash` of a future block as a source of randomness to determine the winner. Why is this a potential vulnerability?
- The blockhash function is computationally expensive and will lead to a DoS.
- A miner can withhold a block if they don't like the outcome, re-mining for a favorable hash. (Correct answer)
- Blockhashes are not unique and can cause collisions in the winner selection.
- This will cause an integer overflow when converting the hash to a number.
Correct answer: A miner can withhold a block if they don't like the outcome, re-mining for a favorable hash.
Using future blockhashes for randomness is insecure because miners have some control over the block's content and can influence the resulting hash. A miner participating in the lottery could solve a block, check if the resulting hash makes them the winner, and decide to discard and re-mine the block if it doesn't. This manipulation gives them an unfair advantage.
Question 6: Which of the following describes a Denial of Service (DoS) vulnerability caused by an unbounded loop in a smart contract function that distributes rewards to a list of users?
- An attacker repeatedly calls the function to drain its ether balance through reentrancy.
- The function's gas cost grows with the number of users, eventually exceeding the block gas limit and becoming impossible to execute. (Correct answer)
- A malicious user provides a malformed address that causes the external call to fail, reverting the entire transaction.
- The contract relies on a manipulatable timestamp, allowing an attacker to claim rewards indefinitely.
Correct answer: The function's gas cost grows with the number of users, eventually exceeding the block gas limit and becoming impossible to execute.
A common DoS vector in smart contracts occurs when a function iterates over an array that can grow indefinitely (unbounded). As more users are added, the gas required to execute the loop increases. Eventually, the total gas cost will exceed the block gas limit, making the function impossible to call successfully and effectively freezing that functionality. This can trap funds or render the contract unusable.
A smart contract function updates a user's balance after making an external call to an untrusted address.
Which vulnerability is most likely to be exploited in this scenario?