What is a reentrancy attack in blockchain smart contracts?
A reentrancy attack happens when a contract calls another contract that calls back into the first contract before the first call finishes, allowing the attacker to drain funds repeatedly.
Click to reveal answer
beginner
Why is integer overflow a risk in smart contracts?
Integer overflow occurs when a number exceeds the maximum value a variable can hold, causing it to wrap around and produce incorrect results, which attackers can exploit to manipulate contract logic.
Click to reveal answer
intermediate
What does 'front-running' mean in blockchain transactions?
Front-running is when an attacker observes a pending transaction and submits their own transaction with higher fees to get processed first, gaining unfair advantage.
Click to reveal answer
intermediate
Explain the 'unchecked call' vulnerability.
Unchecked call vulnerability happens when a contract calls another contract without checking if the call succeeded, which can lead to unexpected behavior or loss of funds.
Click to reveal answer
advanced
What is the risk of using tx.origin for authentication?
Using tx.origin for authentication is risky because it can be tricked by phishing attacks where a malicious contract initiates a call, making the contract think the original user authorized it.
Click to reveal answer
What vulnerability allows repeated withdrawal of funds before state updates?
AInteger overflow
BReentrancy attack
CFront-running
DUnchecked call
✗ Incorrect
Reentrancy attack lets an attacker call back into the contract before the first call finishes, enabling repeated withdrawals.
Which vulnerability involves numbers wrapping around their max value?
AInteger overflow
BFront-running
CUnchecked call
DReentrancy
✗ Incorrect
Integer overflow happens when numbers exceed their max limit and wrap around, causing errors.
What is front-running in blockchain?
ACalling a contract without checking success
BUsing tx.origin for authentication
CManipulating transaction order by paying higher fees
DOverflowing integers
✗ Incorrect
Front-running is when someone pays higher fees to get their transaction processed before others.
Why is using tx.origin for authentication dangerous?
AIt ignores call success
BIt causes integer overflow
CIt allows reentrancy
DIt can be tricked by phishing attacks
✗ Incorrect
tx.origin can be manipulated by malicious contracts, making authentication insecure.
What happens if a contract does not check the success of an external call?
AIt can lead to unchecked call vulnerability
BIt causes front-running
CIt prevents reentrancy
DIt fixes integer overflow
✗ Incorrect
Not checking call success can cause unexpected behavior or loss of funds.
Describe three common vulnerability patterns in blockchain smart contracts and how attackers exploit them.
Think about how contracts interact and how attackers can manipulate transaction order or data.
You got /4 concepts.
Explain why using tx.origin for authentication is unsafe and suggest a safer alternative.
Consider who initiates the call and how contracts verify the caller.
You got /3 concepts.
Practice
(1/5)
1. Which of the following is a common vulnerability in blockchain smart contracts that allows attackers to repeatedly call a function before the first call finishes?
easy
A. Phishing attack
B. Integer overflow
C. Timestamp dependency
D. Reentrancy attack
Solution
Step 1: Understand reentrancy attack
Reentrancy happens when a contract calls another contract and the called contract calls back before the first call finishes.
Step 2: Identify the vulnerability type
This allows attackers to exploit the contract by repeatedly calling a function, draining funds.
Final Answer:
Reentrancy attack -> Option D
Quick Check:
Repeated calls before completion = Reentrancy attack [OK]
Hint: Repeated calls before finish? It's reentrancy [OK]
Mixing phishing attack with smart contract vulnerabilities
2. Which of the following Solidity code snippets correctly prevents integer overflow using Solidity 0.8+ built-in checks?
easy
A. uint256 x = a + b; // no extra check needed
B. uint256 x = SafeMath.add(a, b);
C. uint256 x = a + b; require(x >= a);
D. uint256 x = a - b; // prevents overflow
Solution
Step 1: Recall Solidity 0.8+ overflow behavior
From Solidity 0.8, arithmetic operations revert on overflow automatically.
Step 2: Identify correct prevention method
So, just using uint256 x = a + b; is safe without extra checks or libraries.
Final Answer:
uint256 x = a + b; // no extra check needed -> Option A
Quick Check:
Solidity 0.8+ auto checks overflow = uint256 x = a + b; // no extra check needed [OK]
Hint: Solidity 0.8+ auto checks overflow, no SafeMath needed [OK]
Common Mistakes:
Using SafeMath in Solidity 0.8+ unnecessarily
Adding manual require checks that are redundant
Confusing subtraction with addition overflow
3. Consider this Solidity function snippet:
function withdraw(uint amount) public {
require(balances[msg.sender] >= amount);
(bool success, ) = msg.sender.call{value: amount}('');
require(success);
balances[msg.sender] -= amount;
}
What is the main vulnerability in this code?
medium
A. Integer overflow in balances update
B. Reentrancy attack due to external call before state update
C. Timestamp dependency in withdrawal timing
D. Unchecked call return value
Solution
Step 1: Analyze call order in function
The external call to msg.sender.call happens before updating balances[msg.sender].
Step 2: Identify vulnerability type
This allows reentrancy: attacker can call withdraw again before balance is reduced.
Final Answer:
Reentrancy attack due to external call before state update -> Option B
Quick Check:
External call before balance update = Reentrancy risk [OK]
Hint: External call before state update? Watch for reentrancy [OK]
Common Mistakes:
Thinking integer overflow is the issue here
Ignoring call return value is not the main problem
Confusing timestamp dependency with this pattern
4. This Solidity code snippet is intended to prevent reentrancy. What is the bug?
mapping(address => uint) public balances;
function withdraw(uint amount) public {
require(balances[msg.sender] >= amount);
balances[msg.sender] -= amount;
(bool success, ) = msg.sender.call{value: amount}('');
require(success);
balances[msg.sender] = 0;
}
medium
A. Balance is set to zero after sending funds, causing loss of remaining balance for partial withdrawals
B. No require check on call success
C. Balances mapping is not initialized
D. Function lacks payable modifier
Solution
Step 1: Check balance update order
Balance is reduced before sending funds (good for reentrancy prevention), but then reset to zero after sending.
Step 2: Identify bug impact
For partial withdrawals (e.g., balance=20, amount=10), after -=10 and send 10, setting to 0 discards the remaining 10 without sending it, stealing user funds.
Final Answer:
Balance is set to zero after sending funds, causing loss of remaining balance for partial withdrawals -> Option A
Quick Check:
Post-call balance=0 after partial -= = remaining balance theft [OK]
Hint: Extra balance=0 after partial -= and send? Steals remainder [OK]
Common Mistakes:
Ignoring that balance reset happens after external call
Thinking missing payable modifier causes this bug
Assuming call success check is missing
5. You want to write a Solidity function that safely transfers tokens and prevents both reentrancy and integer overflow. Which approach is best?
hard
A. Call external contracts first, then update balances with SafeMath
B. Use timestamp checks to limit withdrawals and SafeMath for math
C. Update state variables before external calls and use Solidity 0.8+ arithmetic
D. Avoid updating balances and rely on external contract trust
Solution
Step 1: Prevent reentrancy by state update order
Update all internal state variables before making any external calls to avoid reentrancy.
Step 2: Prevent integer overflow using Solidity 0.8+
Use Solidity 0.8+ built-in overflow checks instead of external libraries.
Final Answer:
Update state variables before external calls and use Solidity 0.8+ arithmetic -> Option C
Quick Check:
State update first + Solidity 0.8+ overflow checks = Safe transfer [OK]
Hint: Update state before calls; Solidity 0.8+ handles overflow [OK]
Common Mistakes:
Calling external contracts before state update
Relying on timestamp checks for safety
Ignoring overflow checks in older Solidity versions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.