Unstaking from LP pools will cause underflow and lock user funds
The unstake routine in the pool router removes the entire remaining undelegation amount from the last pool in the list, so if that pool holds fewer tokens than the remainder the subtraction underflows and the transaction aborts. This can leave users unable to fully withdraw their staked LP tokens, with funds locked until other deposits arrive.
Submitted by givn, also found by 0xAlix2, bareli, and den-sosnowsky
Description
When users unstake their LP tokens they call initiate_unstake for the required amount. This creates UnbondingEntry and increases the pending unstake amount - unstaked_pending_amounts[unstaking_type] + unbonding_amount.
At some point an admin (or user) will invoke batch_undelegate_pending_lps():
for (i in 0..vector::length(&m_store.unbond_period)) {
// undelegate
pool_router::unlock(m_store.stake_token_metadata[i], m_store.unstaked_pending_amounts[i]);
// clear pending
m_store.unstaked_pending_amounts[i] = 0;
};
The pool_router::unlock calculates what % of every pool should be undelegated so that the desired LP token amount is reached. This happens by calculating a fraction, iterating over the pools and subtracting an amount equal to that fraction. The issue is that when the last pool element is reached, the remaining amount is all removed from there:
let temp_amount = if (i == vector::length(&pools) - 1) {
remain_amount
} else {
bigdecimal::mul_by_u64_truncate(ratio, temp_pool.amount)
};
remain_amount = remain_amount - temp_amount;
This means that if the last pool is empty or with insufficient funds an underflow will occur here:
temp_pool.amount = temp_pool.amount - temp_amount;
The protocol tries to always fund the pool with least staked tokens by using get_most_underutilized_pool, but this does not prevent situations of imbalance, like:
-
The most underutilized pool receives a very big deposit and dwarfs the rest
-
New pool is being freshly added
-
Users in large numbers withdrawing their funds. Thus, the subtraction can still underflow in situations that are likely to happen over time.
Impact
-
Staked LP tokens can’t be fully withdrawn from protocol.
-
The amount of funds locked can vary greatly, depending on the stake/unstake & operation patterns.
-
Once undelegate amount has been requested it can’t be reduced to try to unlock a smaller amount and get the maximum funds possible. Delegations are locked until someone else deposits.
Root Cause
Trying to withdraw too much from pool when funds are located in other pools.
Proof of Concept
The following code replicates the undelegate calculations of
pool_router::unlockand demonstrates that not all the funds can be withdrawn.
Place this test in pool_router.move. Run it with yarn run test- test_unlock_lp_amounts.
#[test, expected_failure()]
public fun test_unlock_lp_amounts() {
let unlock_amount = 2_000_000u64; // Unlock LP
let pools = vector[ // LP staked in each pool
20_000_000,
20_000_000,
10_000
];
let i = 20;
loop {
debug::print(&string::utf8(b"Begin undelegation round"));
pools = calculate_undelegates(pools, unlock_amount);
i = i - 1;
debug::print(&string::utf8(b""));
if(i == 0) {
break;
}
};
// Pool amounts after last iteration
// [debug] "New pool stake amounts"
// [debug] 4500
// [debug] 4500
// [debug] 0
// Now we continue undelegating smaller amounts, but action will underflow
debug::print(&string::utf8(b" ---- Undelegate smaller amount #1 ---- "));
pools = calculate_undelegates(pools, 1_000);
debug::print(&string::utf8(b" ---- Undelegate smaller amount #2 ---- "));
pools = calculate_undelegates(pools, 1_000);
}
/// Simplified version of pool_router::unlock_lp
#[test_only]
fun calculate_undelegates(pools: vector<u64>, unlock_amount: u64): vector<u64> {
let pools_length = vector::length(&pools);
let total_stakes = vector::fold(pools, 0u64, |acc, elem| acc + elem); // LP staked in across all pools
let remain_amount: u64 = unlock_amount;
let ratio = bigdecimal::from_ratio_u64(unlock_amount, total_stakes);
debug::print(&string::utf8(b"Total staked before undelegate"));
debug::print(&total_stakes);
assert!(total_stakes >= unlock_amount, 1000777);
for (i in 0..pools_length) {
let pool_stake = vector::borrow_mut(&mut pools, i);
let undelegate_amount = if (i == pools_length - 1) {
remain_amount
} else {
bigdecimal::mul_by_u64_truncate(ratio, *pool_stake)
};
remain_amount = remain_amount - undelegate_amount;
// Update state tracking
*pool_stake = *pool_stake - undelegate_amount;
};
debug::print(&string::utf8(b"New pool stake amounts"));
let total_staked_after_undelegate = vector::fold(pools, 0u64, |acc, elem| {
debug::print(&elem);
acc + elem
});
debug::print(&string::utf8(b"Total staked after undelegate"));
debug::print(&total_staked_after_undelegate);
pools
}
Recommended mitigation steps
Instead of doing one iteration over the pools and subtracting the remaining amount from the last one, use an loop and modulo arithmetic to iterate multiple times and subtract any possible remaining amounts from the other pools.
Separate undelegate amount calculation from the stargate calls so that multiple MsgUndelegate messages are not sent for the same validator.
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 undelegation loop assigns the entire remaining amount to the last pool without checking that the pool actually holds that many tokens, so the subtraction can go negative.
Impact
Staked LP tokens may not be fully withdrawable, and the locked amount could vary greatly depending on stake and unstake patterns; once an undelegation is requested it cannot be reduced, and delegations stay locked until someone else deposits.
What the attacker needed
User has staked LP tokens; pool balances become imbalanced; last pool holds less than remainder
The fix
The undelegation calculation should iterate over the pools multiple times using modulo arithmetic so any remaining amount is spread across pools that still hold funds, rather than all being taken from the last pool.
How it was found
- A user initiates an unstake, creating an unbonding entry and increasing the pending unstake amount.
- An admin or user calls batch_undelegate_pending_lps, which calls pool_router::unlock for each unstaking type.
- unlock computes a ratio and subtracts a proportional amount from each pool, taking the full remainder from the last pool.
- If the last pool holds less than the remainder, the subtraction underflows and the call fails.
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.
When you unstake, the contract has to take your tokens back out of several pools. It works out a share for each pool, but for the final pool it simply takes whatever is left over. If that last pool does not hold enough, the maths goes below zero and the whole operation fails, so your tokens stay stuck.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.