Attacker can drain funds from GTELaunchPadV2Pair using swap
The pair contract calculates the amount of tokens a user has paid in by comparing the current balance with the stored reserve, but the balance includes accrued launchpad fees while the reserve does not. An attacker can call swap with an output amount equal to the accrued fees and no actual input, tricking the contract into thinking tokens were paid in, and repeat this to drain the pair's funds.
Submitted by hgrano, also found by 0rpse, 0xPhantom, AasifUsmani, AvantGard, BowTiedOriole, dimulski, hezze, IvanAlexandur, jesjupyter, Matin, max10afternoon, nuthan2x, Nyxaris, saraswati, serial-coder, VAD37, Web3Vikings, and ZeroEx
launchpad/uniswap/GTELaunchpadV2Pair.sol #L250-L251
If we consider a situation where there are some non-zero accrued launchpad fees on the GTELaunchPadV2Pair, then the reserves are calculated as current balances subtract accrued fees within the _update function (GTELaunchPadV2Pair.sol lines 154-155):
reserve0 = _reserve0 = uint112(balance0) - totalLaunchpadFee0;
reserve1 = _reserve1 = uint112(balance1) - totalLaunchpadFee1;
Now an attacker can call swap and the function infers the amount(s) transferred into the pair based on the below calculation (lines 250-251):
uint256 amount0In = balance0 > _reserve0 - amount0Out ? balance0 - (_reserve0 - amount0Out) : 0;
uint256 amount1In = balance1 > _reserve1 - amount1Out ? balance1 - (_reserve1 - amount1Out) : 0;
The issue is that balance0 and balance1 are inclusive of swap fees, but reserve0 and reserve1 are not. Let’s say the attacker chooses amount0Out = totalLaunchpadFee0 and does not transfer any tokens into the contract prior to calling swap. In this case balance0 (the balance measured after transferring amount0Out to the attacker) is just the same as reserves. Therefore:
amount0In = balance0 - (_reserve0 - amount0Out) = _reserve0 - (_reserve0 - totalLaunchpadFee0) = totalLaunchpadFee0
The contract incorrectly infers that the attacker has transferred tokens in when they haven’t transferred anything. To make it work, the attacker needs to reduce amount0Out by 0.3% so the k invariant is still satisfied when considering fees. The same principle applies for amount1 also.
After the attacker’s call to swap, the issue can be exploited again due to the fact that the reserves are just updated to be current balances minus the accrued swap fees (assume the attacker performs the attack within the same block as when the fees were accrued, so this way the accrued fees are not cleared). Therefore the attacker can repeatedly execute swap in this manner in one transaction and drain a large amount of funds from the protocol.
Additionally, the pair does not correctly implement the k invariant as we can see on lines 256-261:
uint256 balance0Adjusted = balance0.mul(1000).sub(amount0In.mul(3));
uint256 balance1Adjusted = balance1.mul(1000).sub(amount1In.mul(3));
if (balance0Adjusted.mul(balance1Adjusted) < uint256(_reserve0).mul(_reserve1).mul(1000 ** 2)) {
revert("UniswapV2: K");
}
This is because the adjusted balances are actually inflated by the inclusion of accrued swap fees in them.
Impact: theft of up to 100% of tokens held by the pair.
Recommended mitigation steps
When inferring the amount(s) transferred in by the user, subtract off the accrued launchpad fees so the amounts can be accurately measured. Also, you need to take away the accrued fees from the balances before checking the k invariant.
Essentially, the accrued launchpad fees are no longer part of the available liquidity of the protocol, and so this needs to be carefully considered.
View detailed Proof of Concept
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 swap function compares the pair's actual token balance, which includes accrued launchpad fees, against a reserve that excludes those fees, so the inferred input amount is overstated.
Impact
The attacker could steal up to 100% of the tokens held by the pair.
What the attacker needed
Non-zero accrued launchpad fees on the pair; attacker can call swap; attack within same block as fee accrual
The fix
The amount transferred in should be calculated after subtracting accrued launchpad fees from the balances, and the k invariant check should also use balances with accrued fees removed.
How it was found
- Ensure there are non-zero accrued launchpad fees on the pair.
- Call swap with an output amount equal to the accrued fees (reduced by 0.3% to satisfy the k invariant) without transferring any tokens in.
- Repeat the swap within the same block, since reserves are updated to current balances minus accrued fees, to drain more funds.
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.
A liquidity pair holds two types of tokens and lets people swap one for the other. It tracks how many tokens it should have (the reserve) and compares that to its actual balance to work out how much a user just paid in. Here, the actual balance included some fees that had built up but the reserve did not, so the comparison gave a false positive: the contract thought the attacker had paid in tokens when they had not, letting the attacker take tokens out for free.
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.