# Unsafe Type Casting in Token Amount Handling

Unsafe uint256→uint160 downcast in Oku StopLimit/Bracket/OracleLess lets attacker create large orders while transferring only a tiny Permit2 amount, then drain funds.

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

**Target:** Solidity order contracts (StopLimit, Bracket, OracleLess) using Permit2
**Root cause:** amountIn is stored as uint256 for the order but truncated to uint160 when passed to handlePermit/Permit2 transferFrom, so the recorded order amount exceeds the tokens actually pulled.
**Steps:**
1. Set `amountIn = type(uint160).max + minPosSize`
2. Call `StopLimit.createOrder(...)` with `permit=true` and a valid Permit2 signature
3. Observe order recorded with full uint256 amountIn but only `minPosSize` (truncated uint160) transferred via `permit2.transferFrom`
4. Cancel or modify the order to extract value exceeding the deposited amount
**How found:** Code review of Permit2 integration: noticed explicit `uint160(amountIn)` casts in handlePermit calls while order structs retain uint256 amountIn, spotting the mismatch between recorded and transferred amounts.
**Exploitation:** Attacker signs a Permit2 permit and creates an order whose stored amountIn is huge while the truncated uint160 transfer pulls only a tiny amount; the order book credits the full amount, and cancel/modify paths let the attacker withdraw more than deposited, draining contract funds.
**Insight:** When a contract stores an amount as uint256 but passes it to Permit2 (uint160 API), check every cast site for truncation; the stored-vs-transferred mismatch is the exploitable invariant, not the cast itself.

