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.
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
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
- Deploy a malicious contract that implements onERC721Received.
- Call participate so that _safeMint triggers the callback.
- In the callback, call exitPosition to release the position before expiry is set.
- 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.