Unbond_instant removes incorrect amount of shares
In the unbond_instant function, the share-removal callback was called with the post-fee amount rather than the full bonded amount, so a portion of shares was never removed. Over time these stuck shares kept earning rewards that other users could no longer receive, and the one-share-equals-one-token relationship broke.
TheSchnilch Follow with Aymen0909
Originally disclosed on Code4rena
Submitted by TheSchnilch, also found by Aymen0909
With unbond_instant, a user can unbond a bonded amount directly without having to wait. However, they must pay a fee for this:
let amount = change.change;
let fee = fee_ratio.mul_ceil(amount);
let final_amount = amount.saturating_sub(fee);
let unbalance = T::Currency::withdraw(&who, fee, WithdrawReasons::TRANSFER, ExistenceRequirement::KeepAlive)?;
T::OnUnstakeFee::on_unbalanced(unbalance);
T::OnUnbonded::happened(&(who.clone(), final_amount));
Here, the mistake is that T::OnUnbonded::happened is called with final_amount, meaning without a fee. As a result, a portion of the shares that were added as shares during bonding can no longer be removed. This, in turn, leads to these shares still receiving rewards that other users cannot receive.
If you insert a println! statement into the functions bond and unbond_instant to display the amounts with which OnBonded::happened and OnUnbonded::happened are called, you will see when you execute the following code that not all shares are removed when the bonded amount of a user is removed with unbond_instant:
For bond:
+ println!("change.change: {:?}", change.change);
144: T::OnBonded::happened(&(who.clone(), change.change));
145: Self::deposit_event(Event::Bonded {
146: who,
147: amount: change.change,
148: });
For unbond_instant:
+ println!("final_amount: {:?}", final_amount);
196: T::OnUnbonded::happened(&(who.clone(), final_amount));
197: Self::deposit_event(Event::InstantUnbonded {
198: who,
199: amount: final_amount,
200: fee,
201: });
Code that can be inserted into the file modules/earning/src/tests.rs to test both functions:
#[test]
fn poc() {
ExtBuilder::default().build().execute_with(|| {
assert_ok!(Earning::bond(RuntimeOrigin::signed(ALICE), 100)); //100 tokens are bonded. change.change is 100
assert_ok!(Earning::unbond_instant(RuntimeOrigin::signed(ALICE), 100)); //All bonded tokens will be unbonded immediately but only 90 shares will be removed because of the fee amount.
});
}
The code can be started with the command cargo test poc -- --nocapture.
So every time a user calls unbond_instant, some shares will remain in the reward system and continue to accumulate rewards that other users who actually bond can no longer get. These shares can then no longer be removed and are stuck. With a maximum fee of 20%, some shares could accumulate over a period of time and these shares would accumulate rewards. The owners of the shares can still claim these rewards. In addition, one share is no longer the same as one underlying token.
Recommendation
For unbond_instant, the final_amount should not be used to remove the shares, but change.change should be used instead.
xlc (Acala) confirmed and commented:
See the fix here.
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 instant-unbond path passed the fee-deducted final amount to the share-removal hook instead of the full bonded amount, so shares equal to the fee were never burned.
Impact
Each instant unbond leaves stuck shares that keep accruing rewards, so other users receive less and the share-to-token ratio drifts; with a maximum fee of 20% this could accumulate over time.
What the attacker needed
A bonded position; calling unbond_instant
The fix
The instant-unbond path was changed to remove shares using the full bonded amount rather than the post-fee amount, and the project confirmed a fix was published.
How it was found
- Bond a chosen amount of tokens as a user.
- Call unbond_instant for the same full amount.
- Observe that only the post-fee portion of shares is removed, leaving the rest stuck in the reward system.
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 bond tokens, the system gives you 'shares' that represent your portion of the pool and earn rewards. Instant unbonding charges a fee, but the code told the system to remove only the amount after the fee, leaving some shares behind. Those leftover shares kept collecting rewards even though the user had taken their tokens back.
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.