Processing all withdrawals before all deposits can cause some deposit to not be delegated in processL1Operations
The contract processes all withdrawals before all deposits, which can cause some deposit amounts to fail delegation because the staking balance is reduced by withdrawals first. This leaves a small amount of HYPE undelegated, reducing the vault's expected rewards.
The way withdrawals and deposits are processed in processL1Operations (all withdrawals requests first, then all deposits) can lead in some cases, to balance not being delegated, which ultimately reduce earned rewards from validator, making the vault less profitable than expected.
Kinetiq allow users to deposit HYPE (native currency of Hyperliquid) into the StakingManager, and receive kHYPE (a share of the vault) in exchange. The HYPE tokens are then sent to the L1 by the StakingManager in order to delegate the tokens to a validator and earn rewards, redistributed to depositor through it shares.
Before diving into the flow of the issue, two mechanisms are important to understand.
First, the different balances that exists in Hyperliquid:
EVM balance: this is the balance of the asset on the HyperEVM, if its the native token it can be read with address(...).balance as we would do on EVM for ETH.
L1 Spot balance: this balance lives outside of the HyperEVM, on the HyperCore, and is transferred that way
L1 Staking balance: it is an intermediary balance, from where assets can then be delegated to validators. Users can move tokens from “spot” to “staking” (or the opposite direction) using the L1Write contract
L1 Delegated balance: this balance is a subset of the staking balance, only “staking” balance can be delegated. Undelegating assets will move then from “delegated” back to “staking”.
Second, we must understand which functions are taking part in the process, and how these different balances are affected (in the following part, the StakingManager contract will be referred as SM):
SM delegate “staking” (every time the function is called, undelegate cannot be called for 1 day) (code)
Now, we can start to describe the following scenario (we’ll set an exchange rate of 1:1 for HYPE/KHYPE, and 10% withdrawal fees for simplicity):
Alice stake 10 HYPE (and receive 10 kHYPE)
Bob stake 1 HYPE (and receive 1 kHYPE)
Carol stake 1 HYPE (and receive 1 kHYPE)
Alice queue withdrawal for 10 kHYPE (which result in 9 HYPE to be withdrawn after fees)
Bob queue withdrawal for 1 kHYPE (which result in 0.9 HYPE to be withdrawn)
After these operations, the processL1Operation will be called by an operator in order to update the balances and ensure assets are delegated and earning rewards.
This means that in the above example, things will happen that way:
Alice’s withdrawal is processed first: undelegation fails (as nothing has been delegated yet), withdrawal 9 HYPE from “staking” to “spot” succeed (reduce the staking balance available to delegate)
Bob’s withdrawal is processed second: undelegation fails (same reason), withdrawal of 0.9 HYPE from “staking” to “spot” succeed, now equal to 9.9 HYPE being unstaked.
Alice’s deposit is processed, which tries to delegate 9 HYPE, but as they are already in the withdrawal process, this fails as there isn’t enough “staking” balance to delegate.
Bob’s deposit is processed for 1 HYPE and successfully pass, making the delegated balance equal to 1 HYPE.
Carol’s deposit is processed for 1 HYPE and successfully pass, making the delegated balance equal to 2 HYPE.
So, at the end of the whole process we will have this state:
L1 Spot: 0 HYPE (still in unstaking queue for 7 days)
L1 “Unstaking Queue”: 9.9 HYPE
L1 Staking: 0.1 HYPE
L1 Delegated: 2 HYPE
But now, let’s see what should have been the balance if everything went correctly.
Alice deposited 10 and withdrawn 9, so 1 HYPE should be delegated
Bob deposited 1 and withdrawn 0.9, so a total of 1.1 HYPE should be delegated
Carol deposited 1, so a total of 2.1 HYPE should be delegated
We now see that 0.1 HYPE are missing in delegation.
This discrepancy in delegated balance will reduce the vault profitability as it will earn less rewards than expected.
Impact
Discrepancy in L1 balances management, causing some amount to not be delegated, thus reducing profitability of the vault from what is expected.
Recommended Mitigation Steps
Care must be taken for the implementation of a fix here, as the below “solution” only works for operationType related to user deposits and withdrawals, and specific processes might be necessary for other operationType.
In a loop, adds up all withdrawal and deposit request amount to be processed, and only processes the needed amount. E.g, int256 amountToDelegate = deposit.amounts - withdrawal.amounts to finally check the sign of amount and act accordingly: withdraw to spot the withdrawal amount, and delegate the remaining.
This will also have the potential to reduce the gas consumption, as this will lower the number of external calls made to the L1Write contract.
Kinetiq disputed and commented:
If any scenario occurs as Infect3D mentioned, we can append an L1 operation to the deposit queue with 0.1 HYPE as the rebalance operation.
The full workflow will be:
ORIGINALLY
1. Alice stake 10 HYPE (and receive 10 kHYPE)
2. Bob stake 1 HYPE (and receive 1 kHYPE)
3. Carol stake 1 HYPE (and receive 1 kHYPE)
4. Alice queue withdrawal for 10 kHYPE (which result in 9 HYPE to be withdrawn after fees)
5. Bob queue withdrawal for 1 kHYPE (which result in 0.9 HYPE to be withdrawn)
L1 Operation logics:
1. Alice's withdrawal is processed first: undelegation fails (as nothing has been delegated yet), withdrawal 9 HYPE from "staking" to "spot" succeed (reduce the staking balance available to delegate)
2. Bob's withdrawal is processed second: undelegation fails (same reason), withdrawal of 0.9 HYPE from "staking" to "spot" succeed, now equal to 9.9 HYPE being unstaked.
3. Alice's deposit is processed, which tries to delegate 9 HYPE, but as they are already in the withdrawal process, this fails as there isn't enough "staking" balance to delegate.
4. Bob's deposit is processed for 1 HYPE and successfully pass, making the delegated balance equal to 1 HYPE.
5. Carol's deposit is processed for 1 HYPE and successfully pass, making the delegated balance equal to 2 HYPE.
So, at the end of the whole process we will have this state:
L1 Spot: 0 HYPE (still in unstaking queue for 7 days)
L1 "Unstaking Queue": 9.9 HYPE
L1 Staking: 0.1 HYPE
L1 Delegated: 2 HYPE
A rebalance deposit of 0.1 HYPE will be provided by queueL1Operations(V, 0.1, RebalanceDeposit) to deposit the retained 0.1 HYPE for L1 Staking.
The original report as published on Code4rena, by Infect3d. 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 40 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 contract processes withdrawals before deposits without netting the amounts, so withdrawals reduce the staking balance available for subsequent deposits to delegate.
Impact
Some HYPE remains undelegated, reducing the vault's profitability from what is expected.
What the attacker needed
Users have deposited and queued withdrawals; operator calls processL1Operations
The fix
The contract should net withdrawal and deposit amounts and only process the difference, delegating the remaining amount.
How it was found
Alice stakes 10 HYPE, Bob stakes 1 HYPE, Carol stakes 1 HYPE.
Alice queues withdrawal for 10 kHYPE (9 HYPE after fees), Bob queues withdrawal for 1 kHYPE (0.9 HYPE after fees).
Operator calls processL1Operations: Alice's withdrawal is processed first, undelegation fails but 9 HYPE moves from staking to spot.
Bob's withdrawal is processed second, undelegation fails but 0.9 HYPE moves from staking to spot.
Alice's deposit is processed, tries to delegate 9 HYPE but fails due to insufficient staking balance.
Bob's and Carol's deposits are processed successfully, delegating 1 HYPE each, leaving 0.1 HYPE undelegated.
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 40 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
When users deposit and withdraw, the contract handles all withdrawals first and then all deposits. If a withdrawal takes out tokens that were meant to be delegated from a deposit, that deposit cannot be fully delegated, so some tokens sit idle and earn no rewards.
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.