addDust does not achieve the goal correctly and may overflow revert
The addDust function in a liquidity management contract calculates a dust amount using oracle prices rather than the actual token amounts returned by getTokenAmounts, so it fails to ensure both token amounts exceed 100 units and can even cause a revert due to overflow. An attacker could not directly exploit this, but it can lead to failed transactions or incorrect liquidity provision.
Submitted by kutugu
The purpose of addDust is to ensure that both the token0Amount and token1Amount are greater than 100 units. The current implementation is to calculate the value of 100 units 1e18 scales of token0 and token1, take the maximum value as liquidity, and add to the repayAmount.
The calculation does not take into account the actual getTokenAmounts result:
- The calculated dust amount is based on the oracle price, while the actual amount consumed is based on the lp tick price
- Even if the price of lp tick is equal to the spot price, which can’t guarantee that the token0Amount and token1Amount of getTokenAmounts will be greater than 100 units.
Proof of Concept
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "forge-std/Test.sol";
import "../contracts/lib/LiquidityAmounts.sol";
import "../contracts/lib/TickMath.sol";
interface IERC20 {
function decimals() external view returns (uint8);
}
interface IUniswapV3Pool{
function slot0()
external
view
returns (
uint160 sqrtPriceX96,
int24 tick,
uint16 observationIndex,
uint16 observationCardinality,
uint16 observationCardinalityNext,
uint8 feeProtocol,
bool unlocked
);
function token0() external view returns (address);
function token1() external view returns (address);
function tickSpacing() external view returns (int24);
}
contract TestDust is Test {
IUniswapV3Pool constant uniswapPool = IUniswapV3Pool(0xC2e9F25Be6257c210d7Adf0D4Cd6E3E881ba25f8);
address constant token0 = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
address constant token1 = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
uint256 constant token0Price = 1e8;
uint256 constant token1Price = 1830e8;
function setUp() public {
vm.createSelectFork("https://rpc.ankr.com/eth", 17863266);
}
function testDustPrice() public {
(uint token0Amount, uint token1Amount) = getTokenAmountsExcludingFees(getDust());
console.log(token0Amount, token1Amount);
}
function getTokenAmountsExcludingFees(uint amount) public view returns (uint token0Amount, uint token1Amount){
(uint160 sqrtPriceX96, int24 tick,,,,,) = IUniswapV3Pool(uniswapPool).slot0();
int24 tickSpacing = IUniswapV3Pool(uniswapPool).tickSpacing();
(token0Amount, token1Amount) = LiquidityAmounts.getAmountsForLiquidity(sqrtPriceX96, TickMath.getSqrtRatioAtTick(tick), TickMath.getSqrtRatioAtTick(tick + tickSpacing), uint128(amount));
}
function getDust() internal view returns (uint amount){
uint scale0 = 10**(20 - IERC20(token0).decimals()) * token0Price / 1e8;
uint scale1 = 10**(20 - IERC20(token1).decimals()) * token1Price / 1e8;
if (scale0 > scale1) amount = scale0;
else amount = scale1;
}
}
For DAI/ETH pool, the token0Amount and token1Amount are 23278 and 0, dust value is close to 0, doesn’t seem work.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "forge-std/Test.sol";
import "../contracts/lib/LiquidityAmounts.sol";
import "../contracts/lib/TickMath.sol";
interface IERC20 {
function decimals() external view returns (uint8);
}
interface IUniswapV3Pool{
function slot0()
external
view
returns (
uint160 sqrtPriceX96,
int24 tick,
uint16 observationIndex,
uint16 observationCardinality,
uint16 observationCardinalityNext,
uint8 feeProtocol,
bool unlocked
);
function token0() external view returns (address);
function token1() external view returns (address);
function tickSpacing() external view returns (int24);
}
contract TestDust is Test {
IUniswapV3Pool constant uniswapPool = IUniswapV3Pool(0xCBCdF9626bC03E24f779434178A73a0B4bad62eD);
address constant token0 = 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599;
address constant token1 = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
uint256 constant token0Price = 29000e8;
uint256 constant token1Price = 1830e8;
function setUp() public {
vm.createSelectFork("https://rpc.ankr.com/eth", 17863266);
}
function testDustPrice() public {
(uint token0Amount, uint token1Amount) = getTokenAmountsExcludingFees(getDust());
}
function getTokenAmountsExcludingFees(uint amount) public view returns (uint token0Amount, uint token1Amount){
(uint160 sqrtPriceX96, int24 tick,,,,,) = IUniswapV3Pool(uniswapPool).slot0();
int24 tickSpacing = IUniswapV3Pool(uniswapPool).tickSpacing();
(token0Amount, token1Amount) = LiquidityAmounts.getAmountsForLiquidity(sqrtPriceX96, TickMath.getSqrtRatioAtTick(tick), TickMath.getSqrtRatioAtTick(tick + tickSpacing), uint128(amount));
}
function getDust() internal view returns (uint amount){
uint scale0 = 10**(20 - IERC20(token0).decimals()) * token0Price / 1e8;
uint scale1 = 10**(20 - IERC20(token1).decimals()) * token1Price / 1e8;
if (scale0 > scale1) amount = scale0;
else amount = scale1;
}
}
For BTC/ETH pool and current tick, the dust calculation will revert.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "forge-std/Test.sol";
import "../contracts/lib/LiquidityAmounts.sol";
import "../contracts/lib/TickMath.sol";
interface IERC20 {
function decimals() external view returns (uint8);
}
interface IUniswapV3Pool{
function slot0()
external
view
returns (
uint160 sqrtPriceX96,
int24 tick,
uint16 observationIndex,
uint16 observationCardinality,
uint16 observationCardinalityNext,
uint8 feeProtocol,
bool unlocked
);
function token0() external view returns (address);
function token1() external view returns (address);
function tickSpacing() external view returns (int24);
}
contract TestDust is Test {
IUniswapV3Pool constant uniswapPool = IUniswapV3Pool(0xCBCdF9626bC03E24f779434178A73a0B4bad62eD);
address constant token0 = 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599;
address constant token1 = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
uint256 constant token0Price = 29000e8;
uint256 constant token1Price = 1830e8;
function setUp() public {
vm.createSelectFork("https://rpc.ankr.com/eth", 17863266);
}
function testDustPrice() public {
(uint token0Amount, uint token1Amount) = getTokenAmountsExcludingFees(getDust());
}
function getTokenAmountsExcludingFees(uint amount) public view returns (uint token0Amount, uint token1Amount){
(uint160 sqrtPriceX96, int24 tick,,,,,) = IUniswapV3Pool(uniswapPool).slot0();
int24 tickSpacing = IUniswapV3Pool(uniswapPool).tickSpacing();
(token0Amount, token1Amount) = LiquidityAmounts.getAmountsForLiquidity(sqrtPriceX96, TickMath.getSqrtRatioAtTick(tick - 10 * tickSpacing), TickMath.getSqrtRatioAtTick(tick), uint128(amount));
}
function getDust() internal view returns (uint amount){
uint scale0 = 10**(20 - IERC20(token0).decimals()) * token0Price / 1e8;
uint scale1 = 10**(20 - IERC20(token1).decimals()) * token1Price / 1e8;
if (scale0 > scale1) amount = scale0;
else amount = scale1;
}
}
For the last 10 ticks, the token0Amount and token1Amount are 0 and 341236635433582778849. That’s a huge number, not dust.
Tools Used
Foundry
Recommended Mitigation Steps
Check the amount of token0Amount and token1Amount corresponding to repayAmount instead of adding dust manually
Keref (Good Entry) acknowledged and commented:
There is a misunderstanding here, in the case of price being below a tick no amount of dust will bring token1 above 0. In that case we want the active token to be above 100. The reason is, as currently is, depositing liquidity may return less than the expected liquidity, due to rounding. We want to add dust such that any necessary token is in enough excess that the liquidity recreated repays the debt fully.
However we agree that we’ve approached the problem in a wrong way and will modify the TokenisableRange so that it supports depositing an exact liquidity, and not care about that in the OPM.
Removed
addDustmechanism, replaced bydepositExactlyin TR. PR: https://github.com/GoodEntry-io/ge/pull/8
Status: Mitigation confirmed. Full details in reports from kutugu and 3docSec.
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 dust calculation uses oracle prices and a fixed scale instead of the actual token amounts returned by getTokenAmounts, and does not account for the current tick range or token decimals.
Impact
The dust mechanism may fail to ensure both token amounts exceed 100 units, and in some cases the calculation can revert, potentially causing transactions to fail.
What the attacker needed
None
The fix
The addDust mechanism was removed and replaced by a depositExactly function that supports depositing an exact liquidity amount.
How it was found
- Set up a Foundry test that forks mainnet at a specific block.
- Call getDust() to compute the dust amount using oracle prices and token decimals.
- Call getTokenAmountsExcludingFees() with that dust amount to see the resulting token0 and token1 amounts.
- Observe that for some pools the token amounts are not both above 100, and for others the calculation reverts.
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 is about a smart contract function that is supposed to add a tiny amount of tokens (dust) to ensure both tokens in a liquidity position are above a minimum. The function uses price data from an oracle instead of checking the actual token amounts that will be used, so it sometimes adds too little or too much, and in some cases causes the transaction to fail. This is a logic bug, not a security vulnerability that lets someone steal funds.
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.