Unintended under/overflow of the amount already swapped in/out due to unmatching logic
The swap calculation in a Rust implementation of a Uniswap-like pool uses unchecked subtraction and addition for the amount already swapped, which can underflow or overflow in release mode. An attacker could potentially cause incorrect swap amounts, though the report does not demonstrate a practical exploit.
Submitted by DadeKuma
A possible wrong amount of the amount already swapped in/out of the input/output asset of a swap due to an unintended under/overflow.
Proof of Concept
In Rust release mode, the - and + operations will not revert when an underflow/overflow occurs.
As such, the amount can under/overflow in the while loop:
match exact_in {
true => {
state.amount_remaining -=
I256::unchecked_from(step_amount_in + step_fee_amount);
-> state.amount_calculated -= I256::unchecked_from(step_amount_out); //@audit this can underflow with -=
}
false => {
state.amount_remaining += I256::unchecked_from(step_amount_out);
-> state.amount_calculated += //@audit this can overflow with +=
I256::unchecked_from(step_amount_in + step_fee_amount);
}
}
The issue is that these are not meant to under/overflow as seen in the original Uniswap logic, as they use SafeMath:
if (exactInput) {
state.amountSpecifiedRemaining -= (step.amountIn + step.feeAmount).toInt256();
-> state.amountCalculated = state.amountCalculated.sub(step.amountOut.toInt256());
} else {
state.amountSpecifiedRemaining += step.amountOut.toInt256();
-> state.amountCalculated = state.amountCalculated.add((step.amountIn + step.feeAmount).toInt256());
}
Uniswap tests that state.amountSpecifiedRemaining > step.amountIn + step.feeAmount and that’s why the operation is safe to use unchecked, but this is not the case with the second one, which must be checked:
https://github.com/Uniswap/v3-core/blob/0.8/contracts/UniswapV3Pool.sol#L681-L685
Recommended Mitigation Steps
Consider using checked_sub instead of - (and checked_add instead of +):
match exact_in {
true => {
state.amount_remaining -=
I256::unchecked_from(step_amount_in + step_fee_amount);
- state.amount_calculated -= I256::unchecked_from(step_amount_out);
+ state.amount_calculated = state.amount_calculated.checked_sub(I256::unchecked_from(step_amount_out));
}
false => {
state.amount_remaining += I256::unchecked_from(step_amount_out);
- state.amount_calculated +=
- I256::unchecked_from(step_amount_in + step_fee_amount);
+ state.amount_calculated = state.amount_calculated.checked_add(
+ I256::unchecked_from(step_amount_in + step_fee_amount));
}
}
Assessed type
Uniswap
af-afk (Superposition) confirmed
The submission details that the swap step calculations will perform an insecure subtraction or addition which might result in uncaught overflows/underflows occurring that should normally be prevented.
While I am inclined to retain a medium-risk severity rating for this submission for now, I would advise the Warden to substantiate their claim via a PoC that demonstrates the vulnerability in practice. If no PoC is provided, I will re-evaluate after PJQA and potentially downgrade to QA (L) due to insufficient proof.
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 use of unchecked arithmetic operations (`-` and `+=`) on signed integers in Rust release mode, where overflow/underflow does not panic, instead of checked operations as in the original Uniswap code.
Impact
The amount already swapped in/out could be incorrectly calculated due to underflow/overflow, potentially leading to wrong swap outcomes, but no concrete exploit is shown.
What the attacker needed
None
The fix
The code should be fixed by using `checked_sub` and `checked_add` instead of the unchecked operators.
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.
In some programming languages, arithmetic operations like subtraction can wrap around if the result is negative, instead of causing an error. This report points out that a swap function uses such unchecked operations, which could lead to wrong calculations. The author suggests using checked operations that would stop the program if an error occurs.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.