Skip to content
Audit finding Intermediate

transfer_share_and_rewards can be used to transfer out shares without transferring reward debt due to rounding

The transfer_share_and_rewards function calculates the reward debt to transfer using integer division that rounds down, so a small share transfer can result in zero reward debt being moved. An attacker could split their position into multiple accounts, each with shares but no reward debt, and claim rewards repeatedly to drain the reward pool.

carrotsmuggler Follow with AM

Originally disclosed on Code4rena

Submitted by carrotsmuggler, also found by AM

The function transfer_share_and_rewards can be used to split up the position in a single account into multiple accounts. The contract sends some of the shares to be held by the second account, and similarly also updates the reward debt of the receiving account so that the receiver cannot take out more rewards than they deserve.

This is calculated in the following snippet. move_balance is the amount of the reward debt that is to be transferred to the receiver.

let move_balance = U256::from(balance.to_owned().saturated_into::<u128>())
    * U256::from(move_share.to_owned().saturated_into::<u128>())
    / U256::from(share.to_owned().saturated_into::<u128>());

Here we see the calculation is simple and by default is rounded down. So if balance*move_share is lower than share, move_balance evaluates to 0. So the receiving account’s reward debt is not increased at all!

increased_rewards
    .entry(*reward_currency)
    .and_modify(|increased_reward| {
        *increased_reward = increased_reward.saturating_add(move_balance);

Since move_balance is 0, the increased_reward is not updated. This means the new account now has shares, but no reward debt. So the receiving account can claim rewards that were already claimed.

This can be done multiple times to drain the reward pool.

The criteria is that balance*move_share has to be lower than share. This can be achieved by sending a small fraction of the funds to the receiving account, such that move_share is much lower than share. Also, if balance, the reward debt of the sender is low, this facilitates the attack more.

Proof of Concept

A short POC is shown here demonstrating the issue. The attacker sends to the receiver a small share of their total. The receiver is shown to have no reward debt, while the sender does have reward debt. This shows that the receiver can claim rewards already claimed by the sender.

#[test]
fn test_rounding() {
ExtBuilder::default().build().execute_with(|| {
    RewardsModule::add_share(&ALICE, &DOT_POOL, 1000);
    assert_ok!(RewardsModule::accumulate_reward(&DOT_POOL, NATIVE_COIN, 100));
    // RewardsModule::add_share(&BOB, &DOT_POOL, 100);
    RewardsModule::claim_rewards(&ALICE, &DOT_POOL);

    let user_stat = RewardsModule::shares_and_withdrawn_rewards(DOT_POOL, ALICE);
    println!("ALICE stat before transfer: {:?}", user_stat);
    let user_stat = RewardsModule::shares_and_withdrawn_rewards(DOT_POOL, BOB);
    println!("BOB stat before transfer: {:?}", user_stat);

    assert_ok!(RewardsModule::transfer_share_and_rewards(&ALICE, &DOT_POOL, 5, &BOB));

    let user_stat = RewardsModule::shares_and_withdrawn_rewards(DOT_POOL, ALICE);
    println!("ALICE stat after transfer: {:?}", user_stat);
    let user_stat = RewardsModule::shares_and_withdrawn_rewards(DOT_POOL, BOB);
    println!("BOB stat after transfer: {:?}", user_stat);
});
}

Output:

# Output is of the form ( ${share_balance}, {0: ${reward_debt}} )
ALICE stat before transfer: (1000, {0: 100})
BOB stat before transfer: (0, {})
ALICE stat after transfer: (995, {0: 100})
BOB stat after transfer: (5, {0: 0})
test tests::test_rounding ... ok

The output shows that ALICE has 1000 shares and 100 reward debt, since ALICE just claimed her rewards. Alice sends BOB 5 shares. BOB ends up with 5 shares and 0 reward debt. So BOB can claim rewards again, even though it’s the same money!

Tools Used

Substrate

Recommended Mitigation Steps

The calculation of move_balance should be changed to saturated round up instead of rounding down. This will ensure that the receiving account’s reward debt is updated correctly. The saturated rounding up is important since the reward debt should never be larger than the reward pool, or it will cause underflow errors when subtracting.

Another option is to revert transfer_share_and_rewards operations if the reward debt of the receiving account is calculated to be 0, unless the sending account ALSO has a reward debt of 0.

Assessed type

Math

xlc (Acala) confirmed and commented:

Just want to highlight that transfer_share_and_rewards is not currently used.

We will choose to not fix this issue as the impact are relatively small and a complete fix is non-trivial. I don’t think it is possible to make profit that is more than transaction fee anyway.


The original report as published on Code4rena, by carrotsmuggler. 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.

How it worked

Root cause

Likely the reward debt calculation uses integer division that rounds down, so small transfers result in zero debt being moved.

Impact

An attacker could claim rewards multiple times and potentially drain the reward pool, though the Acala team noted the function is not currently used and any profit may not exceed transaction fees.

What the attacker needed

Attacker has shares in the reward pool; attacker has already claimed some rewards; transfer_share_and_rewards is callable

The fix

The calculation should round up instead of down, or the transfer should be reverted if the calculated reward debt is zero while the sender has non-zero debt.

How it was found

  1. The attacker has shares and has already claimed rewards, so their reward debt is recorded.
  2. The attacker calls transfer_share_and_rewards to send a very small number of shares to a second account.
  3. Because the transferred share amount is small, the calculated reward debt to move rounds down to zero.
  4. The receiving account now holds shares but has no reward debt.
  5. The receiving account claims rewards that were already claimed by the sender.
  6. The attacker repeats this with multiple accounts to drain the reward pool.

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 move shares between accounts, the system also moves a proportional amount of 'reward debt' to prevent double-claiming rewards. Because the calculation rounds down, a tiny transfer can move zero debt, letting the new account claim rewards that were already paid out. Doing this many times could empty the reward pool.

More by carrotsmuggler

View all

42 more disclosed reports, best first.

More from Acala

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.