Inaccurate swap amount calculation in ReLP leads to stuck tokens and lost liquidity
The ReLP contract calculates the amount of WETH to swap to RDPX inaccurately, which can leave dust WETH stuck in the contract and reduce the total value of the liquidity position after each reLP operation. An attacker could repeatedly trigger reLP to grief the contract, causing ongoing token loss.
degensec Follow with KmanOfficial, QiuhaoLi and 19 others
Originally disclosed on Code4rena
Submitted by degensec, also found by KmanOfficial, QiuhaoLi, jasonxiale, bart1e, nirlin, pep7siup, qpzm, ubermensch, peanuts, tapir, kutugu, mert_eren, WoolCentaur, peakbolt, ayden, 0xnev, T1MOH, HChang26, Yanchuan, 0x3b, and wintermute
ReLPContract contains logic that removes WETH-RDPX liquidity from Uniswap V2, swaps some amount WETH to RDPX and re-adds the liquidity.
The calculation logic for the amount of tokens to swap is unreliable and sometimes reverts or leaves dust WETH in the reLP contract. WETH cannot be recovered unless another reLP operation is triggered. However, since reLP-ing is accessible to all users in the bonding logic in RdpxV2Core, the contract may be griefed on an ongoing basis, intentional or not.
The presence of excess WETH also means that the total value of the LP decreases abnormally after each reLP operation.
Proof of Concept
The following Foundry test demonstrates the vulnerability.
- Paste the code in a new file
PoC.t.solundertests/ - Run the test with
forge test --match-test test_poc_relp_dust -vvv
// File: PoC.t.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.19;
import "forge-std/Test.sol";
import "forge-std/console.sol";
import { Math } from "@openzeppelin/contracts/utils/math/Math.sol";
import {IERC20WithBurn} from "contracts/interfaces/IERC20WithBurn.sol";
import {MockToken} from "contracts/mocks/MockToken.sol";
import {MockRdpxEthPriceOracle} from "contracts/mocks/MockRdpxEthPriceOracle.sol";
import {ReLPContract} from "contracts/reLP/ReLPContract.sol";
import {RdpxReserve} from "contracts/reserve/RdpxReserve.sol";
import {IUniswapV2Factory} from "contracts/uniswap_V2/IUniswapV2Factory.sol";
import {IUniswapV2Router} from "contracts/uniswap_V2/IUniswapV2Router.sol";
import {IUniswapV2Pair} from "contracts/uniswap_V2/IUniswapV2Pair.sol";
contract MockUniV2Amo {
function sync() external {}
function approve(address token, address to, uint256 amount) external {
IERC20WithBurn(token).approve(to, amount);
}
}
contract MockRdpxV2Core {
function sync() external {}
}
contract PoC is Test {
string internal constant ARBITRUM_RPC_URL =
"https://arbitrum-mainnet.infura.io/v3/c088bb4e4cc643d5a0d3bb668a400685";
uint256 internal constant BLOCK_NUM = 24023149; // 2022/09/13
MockToken weth;
MockToken rdpx;
MockRdpxV2Core core;
RdpxReserve reserve;
MockUniV2Amo amo;
MockRdpxEthPriceOracle oracle;
IUniswapV2Factory factory;
IUniswapV2Router router;
IUniswapV2Pair pair;
ReLPContract re;
function setUp() public {
uint256 forkId = vm.createFork(ARBITRUM_RPC_URL, BLOCK_NUM);
vm.selectFork(forkId);
weth = new MockToken("WETH", "weth");
rdpx = new MockToken("RDPX", "rdpx");
core = new MockRdpxV2Core();
reserve = new RdpxReserve(address(rdpx));
amo = new MockUniV2Amo();
oracle = new MockRdpxEthPriceOracle();
factory =
IUniswapV2Factory(0xc35DADB65012eC5796536bD9864eD8773aBc74C4);
router =
IUniswapV2Router(0x1b02dA8Cb0d097eB8D57A175b88c7D8b47997506);
pair = IUniswapV2Pair(
factory.createPair(address(rdpx), address(weth))
);
re = new ReLPContract();
re.setAddresses(
address(rdpx),
address(weth),
address(pair),
address(core),
address(reserve),
address(amo),
address(oracle),
address(factory),
address(router)
);
re.setreLpFactor(1e8);
}
function test_poc_relp_dust() public {
weth.mint(address(this), 1000e18);
rdpx.mint(address(this), 1000e18);
rdpx.mint(address(reserve), 10e18);
weth.approve(address(router), type(uint256).max);
rdpx.approve(address(router), type(uint256).max);
// Initial liquidity in the pair (give to amo)
router.addLiquidity(
address(rdpx),
address(weth),
100e18,
30e18,
100e18,
30e18,
address(amo),
block.timestamp
);
// pool is at 1 rdpx = 0.3 eth
oracle.updateRdpxPrice(0.3e8);
amo.approve(address(pair), address(re), type(uint256).max);
logInfo("Before relp");
(uint112 tokenAReserve,,) = pair.getReserves();
re.reLP(Math.sqrt(uint256(tokenAReserve)) * 250000);
logInfo("After relp");
}
function logInfo(string memory label) private view {
console.log(label);
uint256 lpBalanceAmo = pair.balanceOf(address(amo));
uint256 lpBalanceRelp = pair.balanceOf(address(re));
uint256 rdpxBalanceCore = rdpx.balanceOf(address(core));
uint256 wethBalanceRelp = weth.balanceOf(address(re));
console.log(" lp: amo=%s, relp=%s", lpBalanceAmo, lpBalanceRelp);
console.log(" rdpx: core=%s", rdpxBalanceCore);
console.log(" weth: relp=%s", wethBalanceRelp);
console.log();
}
}
// Output
Running 1 test for tests/ReLpDustPoc.t.sol:ReLpDustPoc
[PASS] test_poc_relp_dust() (gas: 738137)
Logs:
Before relp
lp: amo=54772255750516610345, relp=0
rdpx: core=0
weth: relp=0
After relp
lp: amo=54685393436705721902, relp=0
rdpx: core=316227765999999999
weth: relp=67290285273869
Tools Used
Optimal One-sided Supply to Uniswap 🦄
Recommended Mitigation Steps
There is an analytic formula to provide one-sided liquidity to Uniswap V2 without leaving dust. This formula can be adapted to ReLP’s use-case such that reLPFactor specifies what part of the WETH make-up of the position will be swapped to RDPX according to the formula.
Still, up to 2 wei of dust may be left after the operation, so the remainding RDPX and WETH should be sent to the core contract.
Alex the Entreprenerd (Judge) decreased severity to Medium
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
Impact
Excess WETH can be left stuck in the ReLP contract, and since reLP is callable by any user, the contract may be griefed repeatedly, reducing the total value of the LP position.
What the attacker needed
Ability to call reLP via bonding logic; Uniswap V2 pair with RDPX and WETH
How it was found
- Set up a Uniswap V2 pair with initial RDPX and WETH liquidity.
- Set the RDPX price in the oracle.
- Approve the pair for the ReLP contract to spend tokens.
- Call reLP with a computed amount.
- Observe that after reLP, a small amount of WETH remains in the ReLP contract.
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 is a smart contract bug in a DeFi protocol. The contract tries to rebalance a liquidity pool by swapping some tokens, but its maths is slightly off, so a tiny amount of WETH (a token) gets left behind and stuck. Because anyone can trigger this rebalancing, someone could keep doing it to slowly drain value from the pool.
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.