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.
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 accounts reward debt is not increased at all!
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.
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.
Output:
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 its the same money!
Substrate
The calculation of move_balance should be changed to saturated round up instead of rounding down. This will ensure that the receiving accounts 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.
Math
xlc (Acala) confirmed and commented:
