BathToken.sol#_deposit() attacker can mint more shares with re-entrancy from hookable tokens
The deposit function in BathToken.sol calculates the deposited amount by comparing the contract's token balance before and after a transferFrom call, but lacks a reentrancy guard. An attacker using a hookable token like ERC777 could reenter the deposit function during the transfer callback and mint more shares than the actual deposit.
Submitted by WatchPug
BathToken.sol#_deposit() calculates the actual transferred amount by comparing the before and after balance, however, since there is no reentrancy guard on this function, there is a risk of re-entrancy attack to mint more shares.
Some token standards, such as ERC777, allow a callback to the source of the funds (the from address) before the balances are updated in transferFrom(). This callback could be used to re-enter the function and inflate the amount.
function _deposit(uint256 assets, address receiver)
internal
returns (uint256 shares)
{
uint256 _pool = underlyingBalance();
uint256 _before = underlyingToken.balanceOf(address(this));
// **Assume caller is depositor**
underlyingToken.transferFrom(msg.sender, address(this), assets);
uint256 _after = underlyingToken.balanceOf(address(this));
assets = _after.sub(_before); // Additional check for deflationary tokens
...
Proof of Concept
With a ERC777 token by using the ERC777TokensSender tokensToSend hook to re-enter the deposit() function.
Given:
underlyingBalance():100_000e18 XYZ.totalSupply:1e18
The attacker can create a contract with tokensToSend() function, then:
deposit(1)
- preBalance = `100_000e18`;
- `underlyingToken.transferFrom(msg.sender, address(this), 1)`
-
reenter using
tokensToSendhook for the 2nd call:deposit(1_000e18)- preBalance =
100_000e18; underlyingToken.transferFrom(msg.sender, address(this), 1_000e18)- postBalance =
101_000e18; - assets (actualDepositAmount) =
101_000e18 - 100_000e18 = 1_000e18; - mint
1000shares;
- preBalance =
-
continue with the first
deposit()call:underlyingToken.transferFrom(msg.sender, address(this), 1)- postBalance =
101_000e18 + 1; - assets (actualDepositAmount) =
(101_000e18 + 1) - 100_000e18 = 1_000e18 + 1; - mint
1000shares;
As a result, with only 1 + 1_000e18 transferred to the contract, the attacker minted 2_000e18 XYZ worth of shares.
Recommendation
Consider adding nonReentrant modifier from OZ’s ReentrancyGuard.
bghughes (Rubicon) marked as duplicate and commented:
Duplicate of #283 #410. Note that no ERC777 tokens will be created and this will be patched, making it a non-issue in practice.
Not sure what is meant by “no ERC777 tokens will be created”, since it’s transferring the underlying token which is an arbitrary ERC20, and by extension, ERC777.
The best practice is to break the CEI pattern for deposits and perform the interaction first. Or simply add reentrancy guards.
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
Impact
An attacker could mint more shares than the actual deposit, diluting other depositors, but the program owner noted that no ERC777 tokens will be created and this will be patched, making it a non-issue in practice.
What the attacker needed
Underlying token is ERC777 or hookable; attacker can deploy a contract
The fix
The recommendation is to add a nonReentrant modifier from OpenZeppelin's ReentrancyGuard, or follow the checks-effects-interactions pattern by performing the interaction first.
How it was found
- Attacker deploys a contract with a tokensToSend hook.
- Attacker calls deposit(1) on the BathToken contract.
- During the transferFrom, the tokensToSend hook reenters and calls deposit(1_000e18).
- The reentrant call completes, minting shares based on the inflated balance difference.
- The original deposit call resumes and also mints shares based on the now-larger balance difference.
- Attacker ends up with more shares than the total tokens actually transferred.
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 deposit tokens into a contract, it checks how many tokens arrived by looking at its balance before and after the transfer. If the token allows the sender to run code during the transfer (like ERC777 does), an attacker can call the deposit function again before the first deposit finishes. This lets them trick the contract into thinking they deposited more than they actually did, so they get extra shares.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.