TokenDistributor: ERC777 tokensToSend hook can be exploited to drain contract
The TokenDistributor contract allows ERC777 tokens, whose tokensToSend hook is called before balance updates, enabling a reentrancy attack. An attacker can create a new distribution during the hook using a stale token balance, then claim tokens belonging to another party.
Submitted by Lambda
Impact
TokenDistributor.createERC20Distribution can be used to create token distributions for ERC777 tokens (which are backwards-compatible with ERC20). However, this introduces a reentrancy vulnerability which allows a party to get the tokens of another party. The problem is the tokensToSend hook which is executed BEFORE balance updates happens (see https://eips.ethereum.org/EIPS/eip-777). When this hook is executed, token.balanceOf(address(this)) therefore still returns the old value, but _storedBalances[balanceID] was already decreased.
Proof Of Concept
Party A and Party B have a balance of 1,000,000 tokens (of some arbitrary ERC777 token) in the distributor. Let’s say for the sake of simplicity that both parties only have one user (user A in party A, user B in party B). User A (or rather his smart contract) performs the following attack:
- He calls
claim, which transfers 1,000,000 tokens to his contract address. In_transfer,_storedBalances[balanceId]is decreased by 1,000,000 and therefore now has a value of 1,000,000. - In the
tokensToSendhook, he initiates another distribution for his party A by callingPartyGovernance.distributewhich callsTokenDistributor.createERC20Distribution(we assume for the sake of simplicity that the party does not have more of these tokens, so the call transfers 0 tokens to the distributor).TokenDistributor.createERC20Distributionpassestoken.balanceOf(address(this))to_createDistribution. Note that this is still 2,000,000 because we are in thetokensToSendhook. - The supply of this distribution is calculated as
(args.currentTokenBalance - _storedBalances[balanceId]) = 2,000,000 - 1,000,000 = 1,000,000. - When the
tokensToSendhook is exited (and the first transfer has finished), he can retrieve the tokens of the second distribution (that was created in the hook) to steal the 1,000,000 tokens of party B.
Recommended Mitigation Steps
Do not allow reentrancy in these functions.
merklejerk (PartyDAO) confirmed and commented:
Very few legitimate ERC777s so we think the probability of this happening to a party is somewhat low. Also, it only impacts distributions for that token. However, we will be implementing a reentrancy guard to fix it.
HardlyDifficult (judge) commented:
Agree that it does not seem very probable - but if 777 assets are distributed, it does appear to be a way of stealing from other users in the party and therefore High risk.
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 contract reads the token balance before the transfer completes, and the ERC777 hook allows reentrancy before the balance is updated.
Impact
An attacker could steal tokens from another party, but only for ERC777 tokens and the probability is considered low.
What the attacker needed
ERC777 token used in distribution; attacker is a party member with a balance; attacker can deploy a contract to receive tokens
The fix
A reentrancy guard was implemented to fix it.
How it was found
- User A calls claim, which transfers tokens to their contract.
- In the tokensToSend hook, user A calls PartyGovernance.distribute, which calls TokenDistributor.createERC20Distribution.
- createERC20Distribution reads token.balanceOf(address(this)), which still returns the old total balance.
- The supply of the new distribution is calculated using the stale balance, giving user A extra tokens.
- After the hook exits, user A retrieves the tokens from the new distribution, stealing party B's tokens.
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.
ERC777 tokens have a feature that lets the sender run code before the token balance is updated. The contract reads the token balance at that moment, so it sees an old, higher number. An attacker can use this to trick the contract into creating a fake distribution and then steal another party's tokens.