AutoCompoundingPodLp `_pairedLpTokenToPodLp()` does not correctly handle leftover pTKNs
The AutoCompoundingPodLp contract's _pairedLpTokenToPodLp function does not account for existing pTKN balance when swapping pairedLpTKN to pTKN, leading to an imbalance that can cause liquidity addition to fail and pTKN to become stuck. An attacker can donate pTKN to trigger this imbalance, potentially causing a denial of service.
RampageAudit Follow with X77, future2_22 and 2 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2025-01-peapods-finance-judging/issues/442
Found by
RampageAudit, X77, future2_22, pkqs90, super_jack
Summary
AutoCompoundingPodLp _pairedLpTokenToPodLp() does not correctly handle leftover pTKNs, leading to either stuck of funds or potential DoS when adding LP.
Root Cause
First, let's see how the autocompounding process works: 1) swap rewardTKN -> pairedLpTKN, 2) swap a portion (roughly half) of pairedLpTKN -> pTKN, 3) add pairedLpTKN, pTKN to UniV2 LP, 4) stake LP token to spTKN.
In step 3, if add LP fails due to slippage, the pairedLpTKN and pTKN would remain in the contract, and try to process in the next autocompounding epoch.
However, in the following epochs, roughly half of the pairedLpTKN balance would still be swapped to pTKN again. This would make the pairedLpTKN and pTKN amount even more unbalanced.
For example:
- Init status: 10000 pairedLpTKN.
- After epoch 1 swap: 5000 pairedLpTKN, 5000 pTKN.
- After epoch 2 swap: 2500 pairedLpTKN, 7500 pTKN.
...
The core issue is:
- When calculating the amount of pairedLpTKN to swap to pTKN in the
_getSwapAmt()function, it does not take the current pTKN balance into account. This would lead to having too much pTKN left in the contract. - When adding liquidity, the entire pairedLpTKN and pTKN balance is used. If this is unbalanced, it is very likely to fail due to slippage.
Note that an attacker can always come and donate pTKN to the contract to trigger the initial unbalanced state of pairedLpTKN/pTKN, which would trigger the snowballing effect leading to more pTKN.
function _tokenToPodLp(address _token, uint256 _amountIn, uint256 _amountLpOutMin, uint256 _deadline)
internal
returns (uint256 _lpAmtOut)
{
uint256 _pairedOut = _tokenToPairedLpToken(_token, _amountIn);
if (_pairedOut > 0) {
uint256 _pairedFee = (_pairedOut * protocolFee) / 1000;
if (_pairedFee > 0) {
_protocolFees += _pairedFee;
_pairedOut -= _pairedFee;
}
_lpAmtOut = _pairedLpTokenToPodLp(_pairedOut, _deadline);
require(_lpAmtOut >= _amountLpOutMin, "M");
}
}
function _pairedLpTokenToPodLp(uint256 _amountIn, uint256 _deadline) internal returns (uint256 _amountOut) {
address _pairedLpToken = pod.PAIRED_LP_TOKEN();
@> uint256 _pairedSwapAmt = _getSwapAmt(_pairedLpToken, address(pod), _pairedLpToken, _amountIn);
uint256 _pairedRemaining = _amountIn - _pairedSwapAmt;
uint256 _minPtknOut;
if (address(podOracle) != address(0)) {
// calculate the min out with 5% slippage
_minPtknOut = (
podOracle.getPodPerBasePrice() * _pairedSwapAmt * 10 ** IERC20Metadata(address(pod)).decimals() * 95
) / 10 ** IERC20Metadata(_pairedLpToken).decimals() / 10 ** 18 / 100;
}
IERC20(_pairedLpToken).safeIncreaseAllowance(address(DEX_ADAPTER), _pairedSwapAmt);
try DEX_ADAPTER.swapV2Single(_pairedLpToken, address(pod), _pairedSwapAmt, _minPtknOut, address(this)) returns (
uint256 _podAmountOut
) {
// reset here to local balances to accommodate any residual leftover from previous runs
@> _podAmountOut = pod.balanceOf(address(this));
@> _pairedRemaining = IERC20(_pairedLpToken).balanceOf(address(this)) - _protocolFees;
IERC20(pod).safeIncreaseAllowance(address(indexUtils), _podAmountOut);
IERC20(_pairedLpToken).safeIncreaseAllowance(address(indexUtils), _pairedRemaining);
try indexUtils.addLPAndStake(
pod, _podAmountOut, _pairedLpToken, _pairedRemaining, _pairedRemaining, lpSlippage, _deadline
) returns (uint256 _lpTknOut) {
_amountOut = _lpTknOut;
} catch {
IERC20(pod).safeDecreaseAllowance(address(indexUtils), _podAmountOut);
IERC20(_pairedLpToken).safeDecreaseAllowance(address(indexUtils), _pairedRemaining);
emit AddLpAndStakeError(address(pod), _amountIn);
}
} catch {
IERC20(_pairedLpToken).safeDecreaseAllowance(address(DEX_ADAPTER), _pairedSwapAmt);
emit AddLpAndStakeV2SwapError(_pairedLpToken, address(pod), _pairedRemaining);
}
}
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));
uint112 _r = _swapT == _t0 ? _r0 : _r1;
return (_sqrt(_r * (_fullAmt * 3988000 + _r * 3988009)) - (_r * 1997)) / 1994;
}
See how slippage is handled. If the input pairedLpTKN and pTKN amount is unbalanced, this is very likely to fail. (e.g. The pool is 1:1, and input amount is 1:3, even if we set slippage=50%, this would still fail.)
function addLiquidityV2(
uint256 _pTKNLPTokens,
uint256 _pairedLPTokens,
uint256 _slippage, // 100 == 10%, 1000 == 100%
uint256 _deadline
) external override lock noSwapOrFee returns (uint256) {
...
DEX_HANDLER.addLiquidity(
address(this),
PAIRED_LP_TOKEN,
_pTKNLPTokens,
_pairedLPTokens,
@> (_pTKNLPTokens * (1000 - _slippage)) / 1000,
@> (_pairedLPTokens * (1000 - _slippage)) / 1000,
_msgSender(),
_deadline
);
..
}
Internal pre-conditions
- Add liquidity pairedLpTKN, pTKN fails due to slippage, leading to leftover pTKNs.
External pre-conditions
N/A
Attack Path
Attacker can expedite the unbalance pairedLpTKN/pTKN token ratio by donating pTKN.
Impact
- pTKN is stuck in the contract.
- Add liquidity DoS.
PoC
N/A
Mitigation
If there are leftover pTKNs, before swapping pairedLpTKN to pTKN, calculate the amount of pairedLpTKN that can be directly paired up with pTKN to add liquidity. This clears up the leftover pTKN, then we can proceed with the original process.
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 swap amount calculation ignores the existing pTKN balance, and the subsequent liquidity addition uses the entire unbalanced balances, causing slippage failures.
Impact
pTKN tokens could become stuck in the contract, and adding liquidity could be repeatedly denied, causing a denial of service.
What the attacker needed
Attacker can donate pTKN to the contract
The fix
The contract should calculate the amount of pairedLpTKN needed to pair with existing pTKN before swapping, to clear leftover pTKN and avoid imbalance.
How it was found
- An attacker donates pTKN to the contract to create an initial imbalance.
- The autocompounding process swaps roughly half of the pairedLpTKN to pTKN, further unbalancing the ratio.
- When adding liquidity, the unbalanced amounts cause slippage checks to fail, leaving tokens stuck.
- The imbalance worsens in subsequent epochs, leading to a denial of service.
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.
This report describes a flaw in a smart contract that automatically compounds rewards. The contract swaps tokens and adds them to a liquidity pool, but it doesn't properly track leftover tokens from previous failed attempts. An attacker can donate a small amount of a token to cause the contract to accumulate an unbalanced mix, which then prevents it from adding liquidity, effectively freezing funds.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.