Domain pricing relies on pool price, which can be manipulated
Domain pricing in the usernames module is calculated directly from the spot price of a DEX liquidity pool, which can be manipulated by large deposits or swaps. An attacker could buy domains at a lower price or cause other users to overpay.
0xluk3 Follow with 0xcb90f054, den-sosnowsky and 3 others
Originally disclosed on Code4rena
Submitted by 0xluk3, also found by 0xcb90f054, den-sosnowsky, gss1, Heavyweight_hunters, and p4y4b13
Finding description and impact
Payment for domains (registration, extensions) relies on direct spot price from the Dex module which is directly related to pool reserves. This can be manipulated with a flash loan or a large amount deposit, resulting in:
- buying a domain in a lower price
- making other users overpay for their domains
Calculating the price based directly on a liquidity pool reserves is a well known insecure pattern.
Proof of Concept
In usernames module, in function get_cost_amount, it calls dex module in line 603:
let spot_price = dex::get_spot_price(object::address_to_object<PairConfig>(@pair), get_init_metadata());
Function get_spot_price in dex:
#[view]
/// Calculate spot price
/// https://balancer.fi/whitepaper.pdf (2)
public fun get_spot_price(
pair: Object<Config>, base_coin: Object<Metadata>
): BigDecimal acquires Config, Pool, FlashSwapLock {
let (coin_a_pool, coin_b_pool, coin_a_weight, coin_b_weight, _) =
pool_info(pair, false);
let pair_key = generate_pair_key(pair);
let base_addr = object::object_address(&base_coin);
assert!(
base_addr == pair_key.coin_a || base_addr == pair_key.coin_b,
error::invalid_argument(ECOIN_TYPE)
);
let is_base_a = base_addr == pair_key.coin_a;
let (base_pool, quote_pool, base_weight, quote_weight) =
if (is_base_a) {
(coin_a_pool, coin_b_pool, coin_a_weight, coin_b_weight)
} else {
(coin_b_pool, coin_a_pool, coin_b_weight, coin_a_weight)
};
bigdecimal::div(
bigdecimal::mul_by_u64(base_weight, quote_pool),
bigdecimal::mul_by_u64(quote_weight, base_pool)
)
}
The function uses the pool reserves amounts to calculate the price. Please note, that even if that dex module would implement any lock during the loan, the funds used for manipulation might come from other source, e.g. direct deposit or another dex existing in the future, allowing flash loans.
Recommended mitigation steps
Use a TWAP price source instead, or use an oracle, e.g. Slinky to calculate the price.
andrew (Initia) confirmed and commented:
Flash loan price manipulation is prevented in
dex.move, but attacks through swaps are still possible. While this makes attacks costly when there is sufficient liquidity, it can be an easy target in the early stages.Therefore, we plan to hardcode the price as 1 at launch and update it later when slinky adds an initial price. Accordingly, we will modify the current code to hardcode the price as 1 and update it later using the slinky oracle.
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
The domain pricing function uses the instantaneous spot price from a DEX pool, which is derived from pool reserves and can be manipulated within a single transaction.
Impact
An attacker could manipulate the pool to buy domains at a lower price or force other users to overpay; the project confirmed that swap-based attacks are still possible, though flash-loan manipulation is prevented.
What the attacker needed
Ability to perform large swaps or deposits; sufficient liquidity in the pool
The fix
The project plans to hardcode the price as 1 at launch and later update it using the Slinky oracle; more generally, a TWAP or oracle should be used instead of spot pool reserves.
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 domain name service sets its prices by looking at how much of two tokens are in a trading pool. Someone with a lot of tokens can temporarily change that balance, making the price wrong for a moment. This lets them buy cheaply or makes others pay too much.