The implementation of pullTokensWithPermit poses a risk, allowing malicious actors to steal tokens
The router's pullTokensWithPermit function does not validate the caller, allowing an attacker to frontrun a user's permit signature and transfer the user's tokens to the router, then sweep them to the attacker. The user's original transaction fails because the permit is already used, resulting in token loss.
shaflow2 Follow with 0xlemon and MrPotatoMagic
Originally disclosed on Code4rena
Submitted by shaflow2, also found by 0xlemon and MrPotatoMagic
Finding description and impact
In batch operations interacting with the router, users are allowed to input tokens into the router using the permit method. This approach may be vulnerable to frontrunning attacks, allowing malicious actors to steal the user’s tokens.
Proof of Concept
function pullTokensWithPermit(
IERC20Permit token,
uint256 amount,
address owner,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal virtual {
// Permit the VaultRouter to spend tokens on behalf of the owner
IERC20Permit(token).permit(owner, address(this), amount, deadline, v, r, s);
// Transfer the tokens from the owner to this contract
IERC20(address(token)).safeTransferFrom(owner, address(this), amount);
}
Users can deposit tokens into the router via the pullTokensWithPermit function. However, the router contract does not validate the caller’s information, making it possible for a malicious actor to frontrun the user and exploit their permit signature to steal tokens.
Consider the following scenario:
-
The user interacts with the router contract:
- Step 1: Calls
pullTokensWithPermitto transfer 1000 tokens to the router. - Step 2: Deposits the tokens into a designated vault.
- Step 1: Calls
-
A malicious actor observes the user’s transaction in the mempool and constructs a malicious transaction to steal the user’s tokens:
- Step 1: The attacker calls
pullTokensWithPermitusing the user’s permit signature, causing the user to transfer 1000 tokens to the router. - Step 2: The attacker immediately calls sweepTokens to transfer the tokens to their own account. When the user’s original transaction is executed:
- Step 1: The attacker calls
-
The permit signature has already been used, causing the user’s transaction to fail. As a result, the user loses 1000 tokens.
Additionally, an attacker could frontrun the permit function without using the router and then call pushTokenFrom directly to steal tokens.
Recommended mitigation
The current router is not suitable for integrating permit to handle token input.
chefkenji (BakerFi) confirmed
Status: Mitigation confirmed. Full details in reports from 0xlemon and shaflow2.
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 router's pullTokensWithPermit function does not verify that the caller is the owner or otherwise authorised, so anyone can use a valid permit signature to pull tokens.
Impact
An attacker could steal the user's tokens by frontrunning the permit, and the user's transaction would fail, causing loss of funds.
What the attacker needed
User signs a permit for the router; attacker can see the transaction in mempool
The fix
The router should not integrate permit in this way; the report states the current router is not suitable for integrating permit to handle token input, and the project mitigated it via a pull request.
How it was found
- User calls pullTokensWithPermit to transfer 1000 tokens to the router.
- User deposits the tokens into a designated vault.
- Attacker observes the user's transaction in the mempool.
- Attacker calls pullTokensWithPermit using the user's permit signature, causing the user to transfer 1000 tokens to the router.
- Attacker immediately calls sweepTokens to transfer the tokens to their own account.
- User's original transaction fails because the permit signature has already been used.
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 permit is a signed message that lets someone else move your tokens without you sending a transaction. Here, the router accepted any permit and did not check who was calling, so an attacker could copy the user's signed permit, use it first to move the tokens to the router, and then take them. The user's own transaction then failed because the permit was already used.
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.