Christmas Dinner

First Flight #31
Beginner FriendlyFoundrySolidity
100 EXP
View results
Submission Details
Severity: high
Valid

Ineffective `nonReentrant` Modifier Leading to Reentrancy Vulnerability

Summary:

The nonReentrant modifier in the Christmas Dinner contract does not effectively lock the function against reentrancy attacks. It fails to set the locked state to true before function execution, rendering it ineffective. This exposes the contract to potential reentrancy vulnerabilities, especially in the refund() function.

Vulnerability Details:

Root Cause:

The nonReentrant modifier is incorrectly implemented:

modifier nonReentrant() {
require(!locked, "No re-entrancy");
_;
locked = false;
}

The locked variable is only reset to false after the function call but is not set to true initially, allowing attackers to re-enter the function before the lock is applied.

Affected Functions:

  • refund()

  • Any future functions that rely on this modifier for reentrancy protection.

Impact:

An attacker could exploit the refund() function to call it multiple times in a single transaction, draining the contract’s balance and causing a loss of funds for legitimate participants.

Tools Used:

  • Manual review of the contract code.

  • Static analysis using tools such as Slither to identify reentrancy patterns.

  • Dynamic testing via Echidna to simulate potential exploits.

Recommendations:

Fix:

Update the nonReentrant modifier to lock the function correctly:

modifier nonReentrant() {
require(!locked, "No re-entrancy");
locked = true; // Set lock before executing function
_;
locked = false; // Release lock after execution
}

Implementation:

Replace the existing nonReentrant modifier in the contract with the fixed version above.

Additional Safeguard:

Consider using OpenZeppelin’s ReentrancyGuard for a standardized and well-tested implementation:

import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
contract ChristmasDinner is ReentrancyGuard {
// Functions marked with nonReentrant
function refund() external nonReentrant {
// Refund logic
}
}
Updates

Lead Judging Commences

0xtimefliez Lead Judge 7 months ago
Submission Judgement Published
Validated
Assigned finding tags:

mutex lock incomplete

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.