Skip to content
Smart contracts Audit finding Intermediate

Unsafe Type Casting in Token Amount Handling

Multiple contracts in the protocol perform unsafe downcasting from uint256 to uint160 when handling token amounts in Permit2 transfers, allowing users to create orders with mismatched amounts. An attacker could create an order with a high amount but pay very little, then modify or cancel the order to drain the contract.

bughuntoor Follow with t.aksoy

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-11-oku-judging/issues/64

Found by

bughuntoor, t.aksoy

Summary

Multiple contracts in the rotocol perform unsafe downcasting from uint256 to uint160 when handling token amounts in Permit2 transfers. This can lead to silent overflow/underflow conditions, potentially allowing users to create orders with mismatched amounts, leading to fund loss or system manipulation.

Root Cause

While Solidity 0.8.x provides built-in overflow/underflow protection for arithmetic operations, it does not protect against data loss during type casting. The contracts perform direct casting of uint256 to uint160 without validation in several critical functions:

Bracket.sol: procureTokens() , modifyOrder() StopLimit.sol: createOrder(), modifyOrder() OracleLess.sol: procureTokens()

As an example, the StopLimit::modifyOrder() function takes uint256 amountIn as input. This variable is cast to uint160 inside the handlePermit function. Due to overflow, if the user sets the amount higher than the uint160 limit, the amount would become very small, and the contract would transfer this small amount. When setting orders, it uses amountIn as uint256. As a result, the user creates an order with a high amount but pays very little to the protocol. The user can then drain the contract by modifying their order.

Solidity
    ///@notice see @IStopLimit
    function createOrder(
        uint256 stopLimitPrice,
        uint256 takeProfit,
        uint256 stopPrice,
        uint256 amountIn,
        IERC20 tokenIn,
        IERC20 tokenOut,
        address recipient,
        uint16 feeBips,
        uint16 takeProfitSlippage,
        uint16 stopSlippage,
        uint16 swapSlippage,
        bool swapOnFill,
        bool permit,
        bytes calldata permitPayload
    ) external override nonReentrant {
        if (permit) {
            handlePermit(
                recipient,
                permitPayload,
                uint160(amountIn),
                address(tokenIn)
            );
}

    function handlePermit(
        address owner,
        bytes calldata permitPayload,
        uint160 amount,
        address token
    ) internal {
        Permit2Payload memory payload = abi.decode(
            permitPayload,
            (Permit2Payload)
        );

        permit2.permit(owner, payload.permitSingle, payload.signature);
        permit2.transferFrom(owner, address(this), amount, token);
    }


https://github.com/sherlock-audit/2024-11-oku/blob/ee3f781a73d65e33fb452c9a44eb1337c5cfdbd6/oku-custom-order-types/contracts/automatedTrigger/StopLimit.sol#L146

Internal pre-conditions

No response

External pre-conditions

User must have enough tokens to create an order Amount must be greater than type(uint160).max User must be able to interact with the contract's order creation functions

Attack Path

  1. Attacker prepares: maliciousAmount = type(uint160).max + minPosSize;
  2. Attacker creates an order with this amount
  3. Due to unsafe casting: The order is created with maliciousAmount (full uint256) but only transfers minPosSize
  4. User can cancel or modify his order to drain the contract

Impact

Protocol receives fewer tokens than the order amount indicates and user can modify order to drain the protocol

PoC

No response

Mitigation

Implement OpenZeppelin's SafeCast library

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/gfx-labs/oku-custom-order-types/pull/1

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

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

The contracts cast uint256 token amounts directly to uint160 without checking for overflow, so amounts larger than the uint160 maximum are truncated.

Impact

The protocol receives fewer tokens than the order amount indicates, and the user can modify the order to drain the protocol.

What the attacker needed

User has enough tokens to create an order; Amount greater than type(uint160).max; User can interact with order creation functions

The fix

The issue was fixed by implementing OpenZeppelin's SafeCast library.

How it was found

  1. Attacker prepares an amount equal to the maximum uint160 value plus a small positive size.
  2. Attacker creates an order with this amount.
  3. Due to unsafe casting, the order is created with the full uint256 amount but only transfers the small positive size.
  4. User can cancel or modify the order to drain the contract.

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 a program stores a number in a smaller container than it was created for, the extra digits are silently dropped. Here, the contract takes a large token amount and squeezes it into a smaller type, so the actual amount transferred is much smaller than the order says. An attacker can exploit this to pay less than the order requires and then take funds from the contract.

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 bughuntoor

View all

16 more disclosed reports, best first.

More from Oku's New Order Types Contract Contest

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.