ReLPContract wrongfully assumes protocol owns all of the liquidity in the UniswapV2 pool
The ReLPContract calculated how many LP tokens to remove from the UniswapV2 pool by treating the pool's entire reserves as if the protocol owned them, rather than using the protocol's actual LP balance. When the protocol held only a minority of the pool, the computed amount exceeded its balance and the bond transaction reverted, causing a denial of service.
deadrxsezzz Follow with kutugu, said and 4 others
Originally disclosed on Code4rena
Submitted by deadrxsezzz, also found by kutugu, said, QiuhaoLi, 0xMango, pep7siup, and 0xDING99YA
Possible full DoS for ReLPContract
Proof of Concept
When taking out a bond in RdpxV2Core if isReLPActive == true, a call to ReLPContract#reLP is made which ‘re-LPs the pool` (takes out an amount of RDPX, while still perfectly maintaining the token ratio of the pool).
(uint256 reserveA, uint256 reserveB) = UniswapV2Library.getReserves( // @audit - here it gets the reserves of the pool and assumes them as owned by the protocol
addresses.ammFactory,
tokenASorted,
tokenBSorted
);
TokenAInfo memory tokenAInfo = TokenAInfo(0, 0, 0);
// get tokenA reserves
tokenAInfo.tokenAReserve = IRdpxReserve(addresses.tokenAReserve)
.rdpxReserve(); // rdpx reserves
// get rdpx price
tokenAInfo.tokenAPrice = IRdpxEthOracle(addresses.rdpxOracle)
.getRdpxPriceInEth();
tokenAInfo.tokenALpReserve = addresses.tokenA == tokenASorted
? reserveA
: reserveB;
uint256 baseReLpRatio = (reLPFactor *
Math.sqrt(tokenAInfo.tokenAReserve) *
1e2) / (Math.sqrt(1e18)); // 1e6 precision
uint256 tokenAToRemove = ((((_amount * 4) * 1e18) /
tokenAInfo.tokenAReserve) *
tokenAInfo.tokenALpReserve * // @audit - here the total RDPX reserve in the pool is assumed to be owned by the protocol
baseReLpRatio) / (1e18 * DEFAULT_PRECISION * 1e2);
uint256 totalLpSupply = IUniswapV2Pair(addresses.pair).totalSupply();
uint256 lpToRemove = (tokenAToRemove * totalLpSupply) /
tokenAInfo.tokenALpReserve;
The problem is that the protocol wrongfully assumes that it owns all of the liquidity within the pool. This leads to faulty calculations. In best case scenario wrong amounts are passed. However, when the protocol doesn’t own the majority of the pool LP balance, this could lead to full DoS, as lpToRemove will be calculated to be more than the LP balance of UniV2LiquidityAmo and the transaction will revert.
This can all be easily proven by a simple PoC (add the test to the given Periphery.t.sol) Note: there’s an added console.log in ReLPContract#reLP, just before the transferFrom in order to better showcase the issue
uint256 lpToRemove = (tokenAToRemove * totalLpSupply) /
tokenAInfo.tokenALpReserve;
console.log("lpToRemove value: ", lpToRemove); // @audit - added console.log to prove the underflow
// transfer LP tokens from the AMO
IERC20WithBurn(addresses.pair).transferFrom(
addresses.amo,
address(this),
lpToRemove
);
function testReLpContract() public {
testV2Amo();
// set address in reLP contract and grant role
reLpContract.setAddresses(
address(rdpx),
address(weth),
address(pair),
address(rdpxV2Core),
address(rdpxReserveContract),
address(uniV2LiquidityAMO),
address(rdpxPriceOracle),
address(factory),
address(router)
);
reLpContract.grantRole(reLpContract.RDPXV2CORE_ROLE(), address(rdpxV2Core));
reLpContract.setreLpFactor(9e4);
// add liquidity
uniV2LiquidityAMO.addLiquidity(5e18, 1e18, 0, 0);
uniV2LiquidityAMO.approveContractToSpend(
address(pair),
address(reLpContract),
type(uint256).max
);
rdpxV2Core.setIsreLP(true);
(uint256 reserveA, uint256 reserveB, ) = pair.getReserves();
weth.mint(address(2), reserveB * 10);
rdpx.mint(address(2), reserveA * 10);
vm.startPrank(address(2));
weth.approve(address(router), reserveB * 10);
rdpx.approve(address(router), reserveA * 10);
router.addLiquidity(address(rdpx), address(weth), reserveA * 10, reserveB * 10, 0, 0, address(2), 12731316317831123);
vm.stopPrank();
console.log("UniV2Amo balance isn't enough and will underflow");
uint pairBalance = pair.balanceOf(address(uniV2LiquidityAMO));
console.log("UniV2Amo LP balance: ", pairBalance);
vm.expectRevert("ds-math-sub-underflow");
rdpxV2Core.bond(1 * 1e18, 0, address(this));
}
And the logs:
[PASS] testReLpContract() (gas: 3946961)
Logs:
UniV2Amo balance isn't enough and will underflow
UniV2Amo LP balance: 2235173550604750304
lpToRemove value: 17832559500122488916
Recommended Mitigation Steps
Change the logic and base all calculations on the pair balance of UniV2LiquidityAmo
psytama (Dopex) confirmed and commented:
The re-LP formula used is incorrect.
Alex the Entreprenerd (Judge) commented:
The incorrect assumption does indeed cause reverts.
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 re-LP calculation used the pool's total reserves as the protocol's share instead of the protocol's own LP token balance, so the derived amount to remove could exceed what the protocol held.
Impact
When the protocol does not own the majority of the pool, bonding can revert entirely, giving a full denial of service; in milder cases only wrong amounts are passed.
What the attacker needed
Re-LP enabled on the core contract; protocol holds a minority of pool LP
The fix
The calculation should be based on the AMO's actual pair balance rather than the pool's total reserves.
How it was found
- Set up the ReLP contract addresses and grant it the core role.
- Add liquidity through the AMO and approve the ReLP contract to spend the pair tokens.
- Enable re-LP on the core contract.
- Have a third party add a much larger amount of liquidity to the same pool.
- Call bond on the core contract and observe the revert from the underflow.
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 pool is a shared pot of two tokens that many people add to. This contract worked out how much of the pot to take back by looking at the whole pot, not just the part the protocol had put in. So it tried to withdraw more than it actually owned, and the transaction failed.
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.