Skip to content
Race condition Audit finding Advanced

twAML::participate - reentrancy via _safeMint can be used to brick reward distribution

The twAML contract mints a voting position with _safeMint before updating weekly vote totals, allowing a malicious contract to reenter exitPosition and release the position early. This lets the attacker inflate netActiveVotes without locking tokens, which later causes reward distribution to divide by zero.

cergyk Follow

Originally disclosed on Code4rena

Submitted by cergyk

A malicious user can use reentrancy in twAML to brick reward distribution

Proof of Concept

As we can see in participate in twAML, the function _safeMint is used to mint the voting position to the user;

However this function executes a callback on the destination contract: onERC721Received, which can then be used to reenter:

// Mint twTAP position
tokenId = ++mintedTWTap;
_safeMint(_participant, tokenId);

The _participant contract can reenter in exitPosition, and release the position since,

require(position.expiry <= block.timestamp, "twTAP: Lock not expired");

position.expiry is not set yet.

However we see that the following effects are executed after _safeMint:

weekTotals[w0 + 1].netActiveVotes += int256(votes);
weekTotals[w1 + 1].netActiveVotes -= int256(votes);

And these have a direct impact on reward distribution; The malicious user can use reentrancy to increase weekTotals[w0 + 1].netActiveVotes by big amounts without even locking her tokens;

Later when the operator wants to distribute the rewards:

function distributeReward(
    uint256 _rewardTokenId,
    uint256 _amount
) external {
    require(
        lastProcessedWeek == currentWeek(),
        "twTAP: Advance week first"
    );
    WeekTotals storage totals = weekTotals[lastProcessedWeek];
    IERC20 rewardToken = rewardTokens[_rewardTokenId];
    // If this is a DBZ then there are no positions to give the reward to.
    // Since reward eligibility starts in the week after locking, there is
    // no way to give out rewards THIS week.
    // Cast is safe: `netActiveVotes` is at most zero by construction of
    // weekly totals and the requirement that they are up to date.
    // TODO: Word this better
    totals.totalDistPerVote[_rewardTokenId] +=
        (_amount * DIST_PRECISION) /
        uint256(totals.netActiveVotes);
    rewardToken.safeTransferFrom(msg.sender, address(this), _amount);
}

totals.totalDistPerVote[_rewardTokenId] becomes zero

Recommended Mitigation Steps

Use any of these:

  • Move effects before _safeMint
  • Use nonReentrant modifier

0xRektora (Tapioca) confirmed


The original report as published on Code4rena, by cergyk. 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

Likely the contract performs an external call (_safeMint) before updating state variables (weekTotals), violating the checks-effects-interactions pattern.

Impact

Reward distribution can be bricked because netActiveVotes is inflated, causing a division by zero when calculating totalDistPerVote.

What the attacker needed

Attacker can deploy a contract; attacker has tokens to participate

The fix

The fix should move the weekTotals updates before _safeMint or add a nonReentrant modifier.

How it was found

  1. Deploy a malicious contract that implements onERC721Received.
  2. Call participate so that _safeMint triggers the callback.
  3. In the callback, call exitPosition to release the position before expiry is set.
  4. The contract then continues and updates weekTotals with the votes, inflating netActiveVotes.

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.

When you lock tokens, the contract gives you a receipt (an NFT) and records your voting power. The contract gives you the receipt before it records the power, so a malicious contract can call back into the contract during that gap and undo its lock. This leaves fake voting power that breaks reward maths later.

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 cergyk

View all

29 more disclosed reports, best first.

More from Tapioca Invitational

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.