StakingToken.sol doesn’t properly handle FOT, rebasing tokens or those with variable which will lead to accounting issues downstream
The StakingToken contract records deposit and withdrawal amounts without accounting for tokens that charge a fee on transfer or change balances outside transfers, so the contract's internal balance can exceed its actual token balance. This can cause later users to be unable to claim rewards or withdraw their full stake.
ZanyBonzy Follow with anticl0ck, MSaptarshi and 5 others
Originally disclosed on Code4rena
Submitted by ZanyBonzy, also found by anticl0ck, MSaptarshi, biakia, JanuaryPersimmon2024, 0xBugSlayer, rbserver (1, 2), ArsenLupin, EV_om (1, 2), and yotov721
https://github.com/code-423n4/2024-05-olas/blob/3ce502ec8b475885b90668e617f3983cea3ae29f/registries/contracts/staking/StakingToken.sol#L125 https://github.com/code-423n4/2024-05-olas/blob/3ce502ec8b475885b90668e617f3983cea3ae29f/registries/contracts/staking/StakingToken.sol#L108
Impact
The protocol aims to work with all sorts of ERC20 tokens including tokens that charge a fee on transfer including PAXG. Deposits and withdrawals of these tokens when being used as staking tokens in StakingToken.sol are not properly handled which can cause a number of accounting issues; most especially transfer of the losses due to fees to the last unistaker or reward claimer.
The same effect, although to a less degree, can be observed with tokens like stETH which has the 1 wei corner case issue in which the token amount transferred can be a bit less than the amount entered in. This also includes other rebasing tokens and tokens with variable balances or airdrops, as the direction of rebasing can lead to the same effect as FOT tokens; with token amounts being less than internal accounting suggests. The same as extra tokens from positive rebasing and airdrops being lost due it also not matching internal accounting and no other way to claim these tokens.
Proof of Concept
From the readme,
ERC20 token behaviors - In scope Fee on transfer - Yes Balance changes outside of transfers - Yes
Looking at StakingToken.sol, we can see that the amount of the stakingToken is directly transferred as is and is also recorded into the contract’s balance and avaliableRewards. Now if stakingToken is a token like PAXG that charges a fee on transfer, the actual amount transferred from msg.sender upon deposit will be different from amount received by StakingToken.sol and recorded into the balance/availableRewards. Here, there becomes a mismatch between the contract’s accounting and the contract’s actual balance.
function deposit(uint256 amount) external {
// Add to the contract and available rewards balances
uint256 newBalance = balance + amount;
uint256 newAvailableRewards = availableRewards + amount;
// Record the new actual balance and available rewards
balance = newBalance;
availableRewards = newAvailableRewards;
// Add to the overall balance
SafeTransferLib.safeTransferFrom(stakingToken, msg.sender, address(this), amount);
emit Deposit(msg.sender, amount, newBalance, newAvailableRewards);
}
The same can be seen in _withdraw which is called when a user claims or unstakes. The amount the user receives will be less that he expects while the wrong amount is emitted to him.
function _withdraw(address to, uint256 amount) internal override {
// Update the contract balance
balance -= amount;
SafeTransferLib.safeTransfer(stakingToken, to, amount);
emit Withdraw(to, amount);
}
The ultimate effect will be seen during user claims and unstake in which a point will be reached where the stakingToken balance in the contract will be zero, while the internal accounting still registers that there are still rewards available for users to claim.
In an attempt to rescue the situation, the protocol will incur extra costs in sending tokens to the StakingToken contract address to momentarily balance out the the token balance and the internal amount tracking. This same effect can be observed in rebasing tokens when they rebase negatively or during the 1 wei corner case, as the actual available balance is less that accounting balance. Funds will also be lost if the contract incurs extra airdrops from the stakingToken or it positively rebases.
Recommended Mitigation Steps
Query token balance before and after transfers or disable support for these token types. A skim function can also be introduced to remove any excess tokens from positive rebases.
Assessed type
Token-Transfer
kupermind (Olas) disputed and commented:
The information in README is clearly incorrect and happens to be out of sync with the code. We don’t handle these token behaviors.
Out of fairness to wardens we’ll have to follow the README here, I’m gonna sustain the medium severity for this one.
Fixed, updated the readme.
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 contract updates its internal accounting using the requested transfer amount rather than the actual change in token balance, so fee-on-transfer or rebasing tokens cause a mismatch between recorded and real balances.
Impact
Users may receive less than expected on withdrawal or be unable to claim rewards, and the protocol may need to add extra tokens to cover the shortfall; positive rebases or airdrops can also be lost.
What the attacker needed
Staking token charges a fee on transfer or rebases
The fix
The contract should measure the token balance before and after each transfer and use the actual received amount for accounting, or it should explicitly disallow such tokens; a skim function could recover excess 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.
Some tokens take a small fee when they are sent, so the receiver gets less than the sender sent. The contract here assumes it always receives exactly the amount it records, so its records say it has more tokens than it really does. Eventually, when people try to withdraw or claim rewards, there may not be enough tokens to pay everyone.
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.