LP Redelegation Uses Inaccurate Internal Tracker Amount, Leading to Potential Failures or Orphaned Funds
The redelegation function uses an internal tracker amount that can diverge from the actual staked amount due to rewards or slashing. This can cause redelegation to fail or leave funds staked with the old validator while the pool points to the new one.
Submitted by edoscoba
Summary
The redelegate_lp function, called during validator changes for LP pools, uses the internal pool.amount tracker to specify the amount for MsgBeginRedelegate. This tracker can diverge from the actual staked amount due to unreflected rewards or slashing, potentially causing redelegation failures or leaving funds staked with the old validator.
Finding Description
The pool_router::change_validator function allows the deployer (@staking_addr) to migrate staked assets managed by a specific StakePool object from one validator to another. For LP token pools, it calls the internal helper function redelegate_lp located in pool_router.move#L327-L339.
The redelegate_lp function constructs a MsgBeginRedelegate message to be sent via cosmos::stargate. The amount of tokens to be redelegated in this message is taken directly from the pool.amount field of the StakePool resource:
fun redelegate_lp(pool: &StakePool, new_validator_address: String) {
let denom = coin::metadata_to_denom(pool.metadata);
let coin = Coin { denom, amount: pool.amount }; // <<< Uses pool.amount
let msg = MsgBeginRedelegate {
// ... other fields ...
amount: vector[coin] // <<< Amount specified in the message
};
cosmos::stargate(&object::generate_signer_for_extending(&pool.ref), marshal(&msg));
}
However, the pool.amount is merely an internal counter updated by pool_router::add_stake and pool_router::unstake and pool_router::unlock_lp. It does not automatically reflect changes in the actual staked balance within the underlying mstaking module due to:
- Accrued Rewards: Rewards earned by the staked LP tokens increase the actual delegation shares/amount but are not reflected in
pool.amountuntilcompound_lp_pool_rewardsruns (triggered by user actions) and subsequently callsadd_stake. - Slashing: If the validator is slashed, the actual delegation shares/amount decreases, but
pool.amountis never updated to reflect this loss.
Therefore, pool.amount can easily drift from the true staked amount. Sending a MsgBeginRedelegate with this potentially inaccurate amount breaks the expectation that the administrative function correctly manages the entirety of the funds associated with the StakePool object.
Impact
Using an inaccurate amount in MsgBeginRedelegate leads to two primary negative outcomes:
- Redelegation Failure:If
pool.amountis greater than the actual staked amount (e.g., due to slashing), the underlyingmstakingmodule will reject the request, causing thecosmos::stargatecall and the entirechange_validatortransaction to abort. This prevents the deployer from migrating funds away from a potentially slashed or undesirable validator. - Partial Redelegation / Orphaned Funds: If
pool.amountis less than the actual staked amount (e.g., due to accrued rewards not yet reflected), themstakingmodule will likely succeed in redelegating only the specifiedpool.amount. The remaining tokens (the difference) will be left staked with the original validator. However, thechange_validatorfunction proceeds to updatepool.validatorto the new address. This creates an inconsistent state where theStakePoolobject points to the new validator, but some funds remain with the old one, potentially becoming difficult to track, manage, or withdraw through the router’s standard logic.
Likelihood
The likelihood of pool.amount becoming inaccurate is High. Staking rewards are expected to accrue over time. If users don’t frequently stake or unstake from a specific LP pool, the compound_lp_pool_rewards function won’t run often, causing pool.amount to lag behind the actual staked amount (actual > tracker). Slashing events, while less frequent, would cause the tracker to exceed the actual amount.
Therefore, drift between pool.amount and the real staked value is highly likely. The likelihood of this drift causing a problem during a change_validator call is Medium, as it depends on when the deployer chooses to execute this administrative action relative to the drift.
Recommended mitigation steps
Modify the redelegate_lp function to query the actual delegation amount from the underlying mstaking module before constructing the MsgBeginRedelegate message. This can be done using a query_stargate call similar to the one used in get_lp_real_stakes. Use this queried, accurate amount instead of pool.amount.
Apply the following conceptual change (exact query path and response parsing might need adjustment based on Initia’s mstaking module specifics) to pool_router.move#L327-L339:
fun redelegate_lp(pool: &StakePool, new_validator_address: String) {
let denom = coin::metadata_to_denom(pool.metadata);
- let coin = Coin { denom, amount: pool.amount };
+ let pool_addr = object::address_from_extend_ref(&pool.ref);
+ // Query the actual staked amount instead of relying on the internal tracker
+ let path = b"/initia.mstaking.v1.Query/Delegation"; // Adjust path if needed
+ let request = DelegationRequest { validator_addr: pool.validator, delegator_addr: address::to_sdk(pool_addr) };
+ let response_bytes = query_stargate(path, marshal(&request));
+ // Note: Need robust parsing and error handling for the query response here.
+ // Assuming successful query and parsing to get the actual_staked_amount:
+ let actual_staked_amount = parse_delegation_response_amount(response_bytes, denom); // Placeholder for parsing logic
+ assert!(actual_staked_amount > 0, error::invalid_state(0)); // Add appropriate error code
+
+ let coin = Coin { denom, amount: actual_staked_amount }; // Use the queried amount
let msg = MsgBeginRedelegate {
_type_: string::utf8(b"/initia.mstaking.v1.MsgBeginRedelegate"),
delegator_address: to_sdk(object::address_from_extend_ref(&pool.ref)),
(Note: The parse_delegation_response_amount function is illustrative; the actual implementation would involve using unmarshal and navigating the DelegationResponse struct as done in get_lp_real_stakes to extract the correct amount for the given denom.)
Proof of Concept
- Setup: Configure an LP pool using
add_pool. Stake some LP tokens viacabal::stake_asset(which callspool_router::add_stake), settingpool.amountto, say, 1,000,000. - Scenario 1 (Rewards Accrued): Assume rewards accrue in the underlying
mstakingmodule, increasing the actual staked amount to 1,050,000, but no user actions trigger compounding, sopool.amountremains 1,000,000. - Action: The deployer calls
change_validatorfor this pool.redelegate_lpis called. - Execution:
redelegate_lpconstructsMsgBeginRedelegatewithamount = 1,000,000. - Outcome: The
mstakingmodule successfully redelegates 1,000,000 tokens. 50,000 tokens remain staked with the old validator.change_validatorupdatespool.validatorto the new address. The 50,000 tokens are now potentially orphaned from the router’s perspective. - Scenario 2 (Slashing Occurred): Assume the validator was slashed, reducing the actual staked amount to 950,000, but
pool.amountremains 1,000,000. - Action: The deployer calls
change_validator.redelegate_lpis called. - Execution:
reredelegate_lpconstructsMsgBeginRedelegatewithamount = 1,000,000. - Outcome: The
mstakingmodule rejects the request because only 950,000 tokens are available. Thecosmos::stargatecall fails, causing thechange_validatortransaction to abort. The validator cannot be changed.
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 code uses an internal counter (`pool.amount`) that is not updated for accrued rewards or slashing, instead of querying the actual staked amount from the staking module.
Impact
If the tracker is too high, redelegation fails and the validator cannot be changed; if too low, some funds remain with the old validator and may become orphaned.
What the attacker needed
Deployer role; LP pool with staked tokens; rewards accrued or slashing occurred
The fix
The redelegation function should query the actual staked amount from the staking module and use that instead of the internal tracker.
How it was found
- Configure an LP pool and stake LP tokens, setting pool.amount to a value.
- Allow rewards to accrue or a slashing event to occur, so the actual staked amount differs from pool.amount.
- The deployer calls change_validator for the pool, which calls redelegate_lp.
- redelegate_lp constructs a MsgBeginRedelegate with the outdated pool.amount.
- The staking module either redelegates only the specified amount (leaving the rest with the old validator) or rejects the request if the amount exceeds the actual stake.
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 stake tokens, the amount you actually have can change over time because you earn rewards or because the validator loses some tokens (slashing). The code here uses an old saved number instead of checking the real amount. So when it tries to move the tokens to a new validator, it might ask for too many or too few, causing an error or leaving some tokens behind.
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.