Insufficient check on asset decimals input in create_pool allows malicious pool to be created with invalid swap results
The create_pool function allows anyone to create a pool with arbitrary asset decimals, and these incorrect decimals are used in stableswap calculations, leading to invalid swap results. An attacker could create a pool with wrong decimals and potentially sandwich less sophisticated users, as slippage protection may be ineffective.
Submitted by oakcobalt
create_pool is permissionless and asset decimals are inputs from pool creators.
The vulnerability is there are insufficient check on asset decimals are valid. If a pool is created with incorrect asset decimals, stableswap will use incorrect decimals to scale assets, resulting in invalid swap results.
Proof of Concept
//contracts/pool-manager/src/manager/commands.rs
pub fn create_pool(
deps: DepsMut,
env: Env,
info: MessageInfo,
asset_denoms: Vec<String>,
|> asset_decimals: Vec<u8>,
pool_fees: PoolFee,
pool_type: PoolType,
pool_identifier: Option<String>,
) -> Result<Response, ContractError> {
...
//@audit only check on asset_decimals is the array length.
ensure!(
!asset_denoms.is_empty()
&& asset_denoms.len() >= MIN_ASSETS_PER_POOL
&& asset_denoms.len() == asset_decimals.len(),
ContractError::AssetMismatch
);
...
POOLS.save(
deps.storage,
&identifier,
&PoolInfo {
pool_identifier: identifier.clone(),
asset_denoms,
pool_type: pool_type.clone(),
lp_denom: lp_asset.clone(),
|> asset_decimals,//@audit incorrect decimals will be directly saved and used in stableswap.
pool_fees,
assets,
},
)?;
/contracts/pool-manager/src/manager/commands.rs#L196
We see that in compute_swap. If the asset decimals are incorrect, offer_pool, ask_pool and offer_amount will be scaled to incorrect value before being used in calculate_stableswap_y, resulting in incorrect swap result.
//contracts/pool-manager/src/helpers.rs
pub fn compute_swap(
n_coins: Uint256,
offer_pool: Uint128,
ask_pool: Uint128,
offer_amount: Uint128,
pool_fees: PoolFee,
swap_type: &PoolType,
offer_precision: u8,
ask_precision: u8,
) -> Result<SwapComputation, ContractError> {
...
PoolType::StableSwap { amp } => {
|> let offer_pool = Decimal256::decimal_with_precision(offer_pool, offer_precision)?;
|> let ask_pool = Decimal256::decimal_with_precision(ask_pool, ask_precision)?;
|> let offer_amount = Decimal256::decimal_with_precision(offer_amount, offer_precision)?;
let new_pool = calculate_stableswap_y(
n_coins,
offer_pool,
ask_pool,
offer_amount,
amp,
ask_precision,
StableSwapDirection::Simulate,
)?;
/contracts/pool-manager/src/helpers.rs#L196-L198
Recommended mitigation steps
I don’t see a direct way to query asset decimals during create_pool for verification. However, the protocol can implement a registry contract with trusted token metadata info, such that create_pool can query the registry contract to validate asset decimals are correct atomically.
jvr0x (MANTRA) acknowledged and commented:
Valid point, but that solution was already thought through. If there was such an asset registry contract, it would need to be gated, and then the pool manager wouldn’t really be permissionless.
@3docSec - I agree that this is valid, but I respectfully believe it should be low/QA at best.
Ok, a malicious user can create a pool with messed-up decimals, but there’s nothing that obliges users to use that pool. If only one pool was allowed for X denoms, ok, that would be a medium severity issue as users who want to swap these denoms are obligated to use that pool. Users can provide any pool identifier they want when swapping or when providing/withdrawing liquidity. If a user used that “malicious” pool identifier then that’s a user error!
carrotsmuggler (warden) commented:
This should be marked low/QA. The reason is that the exploit hinges on users adding liquidity to misconfigured pools. The data is on-chain for all to see, so sophisticated users can just check if the decimals are configured correctly.
Also, if the pool decimals are configured differently, it will be obvious when simulating swaps or liquidity addition, which is mitigated by slippage. So if a user accepts a transaction with very high slippage where they swap 10 usdc for 1 usdt or something, that’s on them.
@carrotsmuggler - for a stableswap pool, liquidity addition and its slippage calculation rely on correct assets decimals for correct results. If a stableswap pool has incorrect asset decimals, the slippage check for liquidity is also incorrect. In this case, a user cannot rely on slippage protection to prevent loss.
Vulnerable case:
provide_liquidityin a stableswap pool with incorrect asset decimals will result in incorrect liquidity calculation and incorrect slippage implementation. A user cannot rely on slippage protection to prevent losses.Attack: Sophisticated users can sandwich less sophisticated users on a pool with incorrect asset decimals. This also gives incentives for malicious pool creators to profit from pool users.
Note that pools with incorrect asset decimals are not correctable. It’s different from a faulty initialization pool price that can be arbitraged to normal price.
Based on C4 guideline assets can be at risks out of users control, with attack path. I think it should be Medium.
2 — Med: Assets not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.
I agree with Medium. It’s a non-obvious attack path that can fall through users’ due diligence checks, and the ineffectiveness of slippage protection is key here.
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 create_pool function does not validate that the provided asset decimals match the actual token decimals, and these unchecked values are stored and used in swap calculations.
Impact
A malicious pool creator could set incorrect decimals, causing invalid swap results and potentially allowing sophisticated users to sandwich less sophisticated users; slippage protection may not prevent losses.
What the attacker needed
Permissionless pool creation; attacker creates pool with wrong decimals; victim uses that pool
The fix
The protocol should implement a registry contract with trusted token metadata so that create_pool can validate asset decimals atomically.
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 token has a 'decimals' setting that says how many decimal places it uses. The pool creation function lets anyone set these decimals without checking if they match the real token. If they are wrong, the maths for swapping tokens gives wrong answers, which can be exploited to steal value from 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.