erc777 cross function re-entrancy
The vault contract mints shares before transferring the underlying asset, and if that asset is an ERC777 token, its transfer callback allows an attacker to re-enter a different minting function. This lets the attacker obtain twice as many shares as the assets they actually deposit.
Submitted by 0xWeiss
When an erc777 is used as asset for the vault, you can re-enter the \_mint function by minting the double you would have minted with a normal erc20 token.
Vulnerability Details
The following 2 functions allow minting in the vault. One is for depositing a certain amount of assets, in this case, erc777 and getting shares in exchange, and the second one is to calculate the number of assets you have to send to mint “x” shares. The problem relies on the following lines:
deposit function:
_mint(receiver, shares);
asset.safeTransferFrom(msg.sender, address(this), assets);
adapter.deposit(assets, address(this));
The erc777 has a callback to the owner of the tokens before doing transferFrom. In this case, it is a vulnerable function because it is minting the shares before we transfer the tokens. So, on the callback that transferFrom makes to our attacker contract, we directly call the other mint function that is also publicly callable by anyone. The reason why we can’t re-enter the deposit is that it has a nonReentrant modifier, so we have to perform a cross-function re-entrancy.
mint function:
_mint(receiver, shares);
asset.safeTransferFrom(msg.sender, address(this), assets);
adapter.deposit(assets, address(this));
So eventually, you will be able to get twice as many shares every time you deposit.
Impact
Eventually you will be able to mint twice as much as you provide assets.
Recommended Mitigation Steps
The changes that have to be made are 2:
Either state clearly that erc777 are not supported, or
Change the flow of the function, transferring first the assets and then getting the shares.
asset.safeTransferFrom(msg.sender, address(this), assets); //changed to the top of the function
_mint(receiver, shares);
adapter.deposit(assets, address(this));
RedVeil (Popcorn) confirmed, but disagreed with severity
LSDan (judge) decreased severity to Medium
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 mints shares before transferring the underlying asset, and the two minting functions lack a shared reentrancy guard, allowing a cross-function reentrancy via the ERC777 transfer callback.
Impact
An attacker could mint twice as many shares as the assets they provide, but this only works if the vault uses an ERC777 token as its asset.
What the attacker needed
Vault uses an ERC777 token as asset; attacker can deploy a contract to receive callback
The fix
The contract should either explicitly state that ERC777 tokens are not supported, or change the function flow to transfer assets before minting shares.
How it was found
- Use an ERC777 token as the vault asset.
- Call the deposit function, which mints shares before transferring tokens.
- During the ERC777 transfer callback, call the other mint function (which is not protected by nonReentrant).
- Receive shares from both calls, effectively doubling the shares for the same deposit.
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 vault lets you deposit tokens and gives you shares in return. Some tokens (ERC777) call back to the sender during a transfer, and the vault gives out shares before taking the tokens, so an attacker can use that callback to call another function that also gives shares. This means they get more shares than they paid for.
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.