ERC777 and similar token implementations allow a stealing of funds when transferring tokens
The token manager's _takeToken function lacked reentrancy protection, allowing a malicious contract to reenter during an ERC777 transfer hook and cause the balance difference to be counted twice. This let the attacker receive more tokens on the destination chain than they sent on the source chain.
Jeiwan Follow with nobody2018
Originally disclosed on Code4rena
Submitted by Jeiwan, also found by nobody2018
A malicious actor can trick a TokenManager into thinking that a bigger amount of tokens were transferred. On the destination chain, the malicious actor will be able to receive more tokens than they sent on the source chain.
Proof of Concept
TokenManagerLockUnlock and TokenManagerLiquidityPool are TokenManager implementations that transfer tokens from/to users when sending tokens cross-chain. The low-level _takeToken function (TokenManagerLiquidityPool._takeToken, TokenManagerLockUnlock._takeToken) is used to take tokens from a user on the source chain before emitting a cross-chain message, e.g. via the TokenManager.sendToken function. The function computes the difference in the balance of the liquidity pool or the token manager before and after the transfer, to track the actual amount of tokens transferred. The amount is then passed in the cross-chain message to tell the InterchainTokenService contract on the destination chain how many tokens to give to the recipient.
The _takeToken function, however, is not protected from reentrance, which opens up the following attack scenario:
- A malicious contract initiates transferring of 100 ERC777 tokens by calling TokenManager.sendToken.
- The
_takeTokenfunction callstransferFromon the ERC777 token contract, which calls the tokensToSend hook on the malicious contract (the sender). - In the hook, the malicious contract makes another call to
TokenManager.sendTokenand sends 100 more tokens. - In the nested
_takeTokencall, the balance change will equal 100 since, in ERC777, the balance state is updated only after thetokensToSendhook, so only the re-entered token transfer will be counted. - The re-entered call to
TokenManager.sendTokenwill result in 100 tokens transferred cross-chain. - In the first
_takeTokencall, the balance change will equal 200 because the balance of the receiver will increase twice during thetransferFromcall; once for the first call and once for the re-entered call. - As a result, the malicious contract will transfer 100+100 = 200 tokens, but the
TokenManagercontract will emit two cross-chain messages; one will transfer 100 tokens (the re-entered call) and the other will transfer 200 tokens (the first call). This will let the malicious actor to receive 300 tokens on the destination chain, while spending only 200 tokens on the source chain.
Since the protocol is expected to support different implementations of ERC20 tokens, including custom ones, the attack scenario is valid for any token implementation that uses hooks during transfers.
Recommended Mitigation Steps
Consider adding re-entrancy protection to the TokenManagerLiquidityPool._takeToken and TokenManagerLockUnlock._takeToken functions, for example by using the ReentrancyGuard from OpenZeppelin.
Assessed type
Reentrancy
deanamiel (Axelar) confirmed and commented:
We have added a separate token manager for fee on transfer tokens, which is protected from reentrancy.
Link to the public PR: https://github.com/axelarnetwork/interchain-token-service/pull/96.
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 _takeToken function computes the token balance difference without reentrancy protection, so a reentrant call during the ERC777 tokensToSend hook causes the balance change to be counted twice.
Impact
The attacker could receive 300 tokens on the destination chain while spending only 200 tokens on the source chain.
What the attacker needed
A malicious contract; an ERC777 or hook-based token; the token manager supports that token
The fix
Reentrancy protection should be added to the _takeToken functions, for example using OpenZeppelin's ReentrancyGuard; the project added a separate token manager for fee-on-transfer tokens that is protected from reentrancy.
How it was found
- A malicious contract calls TokenManager.sendToken to transfer 100 ERC777 tokens.
- The _takeToken function calls transferFrom on the ERC777 token, which triggers the tokensToSend hook on the malicious contract.
- In the hook, the malicious contract calls TokenManager.sendToken again to send another 100 tokens.
- In the nested _takeToken call, the balance change equals 100 because ERC777 updates balances only after the hook.
- The re-entered sendToken results in 100 tokens transferred cross-chain.
- In the first _takeToken call, the balance change equals 200 because the receiver's balance increased twice during transferFrom.
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 send tokens across chains, the system checks how many tokens it actually received by comparing balances before and after the transfer. A malicious contract can use a callback (a hook) that runs during the transfer to start another transfer, confusing the balance check. This lets the attacker trick the system into thinking they sent more tokens than they really did, so they get extra tokens on the other chain.
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.