Skip to content
Smart contracts Audit finding Intermediate

Incorrect address is used as spender for ERC20 permit signature verification

The ERC20 permit signature verification in the transfer helper used the recipient address as the spender instead of the contract itself. This caused the permit check to fail and revert the whole increaseLever function when the recipient differed from the contract.

0xpiken Follow with Rhaydden, hash and 1 other

Originally disclosed on Code4rena

Submitted by 0xpiken, also found by Rhaydden, hash, and pkqs90

The failure of permit signature verification might revert the whole function. PositionAction#increaseLever() might revert if ERC20 permit signature is used as permitParams

Proof of Concept

The IERC20Permit(token).safePermit() call is supposed to allow spender to spend up to approvalAmount of token on behalf of from. The spender in _transferFrom() should be the contract itself:

            IERC20Permit(token).safePermit(
                from,
                to,
                params.approvalAmount,
                params.deadline,
                params.v,
                params.r,
                params.s
            );
@>          IERC20(token).safeTransferFrom(from, to, amount);

However, IERC20Permit(token).safePermit() use to as spender to verify the permit signature. The safePermit() call will be reverted if to is not same as the contract itself.

Copy below codes to PositionAction20.t.sol and run forge test --match-test test_increaseLeverWithInvalidPermission:

    function test_increaseLeverWithInvalidPermission() public {
        uint256 upFrontUnderliers = 20_000 ether;
        uint256 borrowAmount = 70_000 ether;
        uint256 amountOutMin = 69_000 ether;

        deal(address(USDC), user, upFrontUnderliers);

        // build increase lever params
        address[] memory assets = new address[](2);
        assets[0] = address(underlyingToken);
        assets[1] = address(USDC);

        LeverParams memory leverParams = LeverParams({
            position: address(userProxy),
            vault: address(vault),
            collateralToken: address(USDC),
            primarySwap: SwapParams({
                swapProtocol: SwapProtocol.UNIV3,
                swapType: SwapType.EXACT_IN,
                assetIn: address(underlyingToken),
                amount: borrowAmount,
                limit: amountOutMin,
                recipient: address(positionAction),
                deadline: block.timestamp + 100,
                args: abi.encode(weightedPoolIdArray, assets)
            }),
            auxSwap: emptySwap,
            auxAction: emptyPoolActionParams
        });

        PermitParams memory permitParams;

        uint256 deadline = block.timestamp + 100;
        //@audit-info the spender is userProxy
        (uint8 v, bytes32 r, bytes32 s) = PermitMaker.getPermitTransferFromSignature(
            address(USDC),
            address(userProxy),
            20_000 ether,
            0,
            deadline,
            0x12341234
        );
        permitParams = PermitParams({
            approvalType: ApprovalType.PERMIT,
            approvalAmount: 20_000 ether,
            nonce: 0,
            deadline: deadline,
            v: v,
            r: r,
            s: s
        });

        // call increaseLever
        vm.startPrank(user);
        //@audit-info revert due to invalid signature because `to` is used as `spender` to verify the signature,
        //@audit-info while `to` inside the function `increaseLever()` is address(positionAction) instead of `userProxy`
        vm.expectRevert("EIP2612: invalid signature");
        userProxy.execute(
            address(positionAction),
            abi.encodeWithSelector(
                positionAction.increaseLever.selector,
                leverParams,
                address(USDC),
                20_000 ether,
                address(user),
                permitParams
            )
        );
        vm.stopPrank();
    }

Recommended Mitigation Steps

Use address(this) as spender for safePermit():

            IERC20Permit(token).safePermit(
                from,
-               to,
+               address(this),
                params.approvalAmount,
                params.deadline,
                params.v,
                params.r,
                params.s
            );
            IERC20(token).safeTransferFrom(from, to, amount);

Assessed type

Context

amarcu (Loopfi) confirmed

Infect3d (warden) commented:

I believe the issue is invalid. Every instance of the code where TransferAction::_transferFrom is called uses to == address(this) (or self which is address(this)):

pkqs90 (warden) commented:

As stated in the report, the PositionAction#increaseLever() will have an issue. Because self is not address(this) when triggered by delegatecall through a PRBProxy. self is the PositionAction.sol, while address(this) is the proxy’s address.

Koolex (judge) commented:

No further change on this.


The original report as published on Code4rena, by 0xpiken. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 code passed the recipient address (to) as the spender argument to safePermit instead of the contract's own address.

Impact

The increaseLever function could revert when a permit signature is used, blocking the operation; a commenter argued the issue is invalid because all internal calls use the contract as recipient, but another noted that under delegatecall the recipient differs from the contract address.

What the attacker needed

User has a proxy contract; permit signature is used; recipient differs from contract address

The fix

The code should use address(this) as the spender when calling safePermit.

How it was found

  1. Set up a user proxy and a position action contract.
  2. Create a permit signature where the spender is the user proxy.
  3. Call increaseLever with permit parameters.
  4. Observe the transaction reverts with an invalid signature error.

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 spend your tokens without a separate approval transaction. The code checked the signature against the wrong address, so a valid permit was rejected and the operation failed.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by 0xpiken

View all

29 more disclosed reports, best first.

More from LoopFi

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.