Calculated token0TVL may be zero under certain scenarios
Integer division inside brackets was performed before multiplication when computing token0TVL, so the result could round down to zero for tokens with many decimals. An attacker could then obtain a wrong, near-zero LP token price from the oracle.
Submitted by hickuphh3, also found by 0xNazgul, 0xSky, CertoraInc, Deivitto, hansfriese, Jeiwan, linmiaomiao, rbserver, and SinceJuly
uint token0TVL = assetReserves[i] * (prices[i] / decimals);
Because of the brackets, the division of prices[i] / decimals is executed before multiplication, causing token0TVL to potentially be zero.
Proof of Concept
Add the following test in oracle.test.ts. Note: getPriceLP() should have its visibility changed from internal to public as the test relies on it.
To summarise what the test is doing, a stablecoin of 24 decimals is deployed, whose address will be greater than the note address so that token0 = note. It will enter the following case:
if (pair.stable()) { // stable pairs will be priced in terms of Note
if (token0 == note) { //token0 is the unit, token1 will be priced with respect to this asset initially
decimals = 10 ** (erc20(token1).decimals()); // we must normalize the price of token1 to 18 decimals
prices = pair.sample(token1, decimals, 8, 1);
(unitReserves, assetReserves) = pair.sampleReserves(8, 1);
such that the prices’s denomination is smaller than the stablecoin’s decimals of 24.
To see the difference in test results, apply the recommended fix after running the test once. In essence, the LP’s price will double from 999500000001499 to 1999999998838589, which is expected since the LP token should be worth the combined value of both stablecoins.
it.only("will have 0 token0TVL", async () => {
// NOTE: change getPriceLP() from internal to public so that function can be called
let tokenFactory = await ethers.getContractFactory("ERC20", dep)
let stablecoin = await tokenFactory.deploy("STABLE","STABLE",ethers.utils.parseUnits("100000", "24"), 24)
await stablecoin.deployed()
// we want note to be token0
// redeploy till it is
while (stablecoin.address < note.address) {
stablecoin = await tokenFactory.deploy("STABLE","STABLE",ethers.utils.parseUnits("100000", "24"), 24)
await stablecoin.deployed()
}
// give token approvals to router
let noteIn = ethers.utils.parseUnits("10000", "18")
let stableIn = ethers.utils.parseUnits("10000", "24")
await (await note.approve(router.address, ethers.constants.MaxUint256)).wait()
await (await stablecoin.approve(router.address, ethers.constants.MaxUint256)).wait()
// borrow note
await (await comptroller._supportMarket(cUsdc.address)).wait()
// set collateral factors for cCanto
await (await comptroller._setCollateralFactor(cUsdc.address, ethers.utils.parseUnits("0.9", "18"))).wait()
// borrow note against usdc
await (await comptroller.enterMarkets([cUsdc.address, cNote.address])).wait()
await (await usdc.approve(cUsdc.address, ethers.utils.parseUnits("1000"))).wait()
// supply usdc
await (await cUsdc.mint(ethers.utils.parseUnits("100000000", "6"))).wait()
// borrow note
await (await cNote.borrow(ethers.utils.parseUnits("9000000", "18"))).wait()
// add liquidity
await (await router.addLiquidity(
note.address,
stablecoin.address,
true,
noteIn,
stableIn,
0,
0,
dep.address,
9999999999,
)).wait()
// get pair address
let pairAddr = await factory.getPair(note.address, stablecoin.address, true)
pair = await ethers.getContractAt("BaseV1Pair", pairAddr)
//set period size to zero for instant observations
await (await factory.setPeriodSize(0)).wait()
// swap 10 times for price observations
for(var i = 0; i < 10; i++) {
if (i % 2) {
//swap 0.01 note for stable
await (await router.swapExactTokensForTokensSimple(
ethers.utils.parseUnits("10", "18"),
0,
note.address,
stablecoin.address,
true,
dep.address,
9999999999999
)).wait()
} else {
//swap stable for note
await (await router.swapExactTokensForTokensSimple(
ethers.utils.parseUnits("10", "24"),
0,
stablecoin.address,
note.address,
true,
dep.address,
9999999999999
)).wait()
}
}
// check lpToken price
// Actual price calculated is 999500000001499
// But expected price (after removing brackets) is 1999999998838589
console.log((await router.getPriceLP(pairAddr)).toString());
});
Recommended Mitigation Steps
- uint token0TVL = assetReserves[i] * (prices[i] / decimals);
+ uint token0TVL = assetReserves[i] * prices[i] / decimals;
0xean (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
Root cause
Likely the multiplication and division were grouped so that integer division happened first, losing precision before the larger multiplication.
Impact
The LP token price could be calculated as roughly half its true value, which may misprice the asset wherever that oracle value is used.
What the attacker needed
A stablecoin with unusually high decimals; the token sorts as token0 in the pair
The fix
The calculation should multiply the reserves by the price first and divide by the scaling factor afterwards.
How it was found
- Deploy a stablecoin with 24 decimals whose address sorts so it becomes token0.
- Add liquidity between note and the stablecoin and set the observation period size to zero.
- Perform several swaps in both directions to create price observations.
- Call getPriceLP and observe the returned price is about half the expected value.
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.
Computers store whole numbers, so dividing before multiplying can throw away the remainder. Here the code divided a price by a scaling number first, which could round down to zero, and then multiplied by the reserves, leaving zero. That made the calculated value of a liquidity pool token wrong.
What is Crypto?
Encryption, signatures, randomness or certificate checks that do not protect what they claim to.