Incorrect reserve mapping leads to fault swap
The function `_getSwapAmt` incorrectly assumes that the first reserve returned by Uniswap V2 corresponds to the first token argument, but reserves are sorted by token address. This mismatch can cause swap calculations to be wrong, leading to failed swaps or unintended behaviour that may be exploited for arbitrage.
Source: https://github.com/sherlock-audit/2025-01-peapods-finance-judging/issues/512
Found by
Etherking, Honour, ZoA, pkqs90
Summary
The function _getSwapAmt incorrectly assumes that _r0 corresponds to _t0 and _r1 corresponds to _t1. However, Uniswap V2's getReserves() returns reserves based on the sorted order of token addresses, not based on input order. This mismatch leads to incorrect swap calculations, resulting in potential losses and arbitrage opportunities for attackers.
Root Cause
-
The function retrieves reserves via:
Solidity (uint112 _r0, uint112 _r1) = DEX_ADAPTER.getReserves(DEX_ADAPTER.getV2Pool(_t0, _t1)); -
UniswapDexAdaptercontract:Solidity function getReserves(address _pool) external view virtual override returns (uint112 _reserve0, uint112 _reserve1) { (_reserve0, _reserve1,) = IUniswapV2Pair(_pool).getReserves(); } -
It assumes
_r0belongs to_t0and_r1belongs to_t1. -
V2 reserves are always returned in ascending order of token addresses.
-
If
_t0 > _t1,_r0will belong to_t1, and_r1will belong to_t0, breaking the swap calculation.
Internal Pre-conditions
External Pre-conditions
Attack Path
Impact
The amount of _pairedLpToken that needs to be swapped may be miscalculated, causing the swap to fail.
Even if the swap succeeds, it does not work as intended by the protocol, which breaks the design of V2Pool and the AutoCompoundingPodLp contract.
PoC
No response
Mitigation
function _getSwapAmt(address _t0, address _t1, address _swapT, uint256 _fullAmt) internal view returns (uint256) {
(uint112 _r0, uint112 _r1) = DEX_ADAPTER.getReserves(DEX_ADAPTER.getV2Pool(_t0, _t1));
+++ (address token0,) = sortTokens(_t0, _t1);
+++ (_r0, _r1) = _t0 == token0 ? (_r0, _r1) : (_r1, _r0);
uint112 _r = _swapT == _t0 ? _r0 : _r1;
return (_sqrt(_r * (_fullAmt * 3988000 + _r * 3988009)) - (_r * 1997)) / 1994;
}
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 code assumes that the reserves returned by Uniswap V2's `getReserves()` are in the same order as the input token addresses, but they are actually sorted by token address.
Impact
The amount of `_pairedLpToken` to swap may be miscalculated, causing the swap to fail or not work as intended, which breaks the protocol design and could create arbitrage opportunities.
What the attacker needed
None
The fix
The code should sort the token addresses and reorder the reserves accordingly before using them in the swap calculation.
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 a Uniswap V2 pool, the two tokens are always listed in a fixed order based on their contract addresses, not the order you pass them in. The code assumed the first reserve value always belonged to the first token you specified, which is not true. This mix-up can make the swap amount calculation wrong, so swaps might fail or give unexpected results.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.