StakingModule's stakedByAt() can report erroneous values
The stakedByAt() function returns incorrect historical staking balances when multiple stake() calls occur in the same block, because it reads only the first checkpoint of that block. This can cause downstream systems like vaults to make decisions based on stale data, potentially leading to user losses.
Source: https://github.com/sherlock-audit/2023-02-telcoin-judging/issues/36
Found by
hyh
Summary
stakedByAt() is vulnerable to producing manipulated readings when staking was performed more than once in the same block.
For example, if there were two stake() calls in one block, then stakedByAt() will report the state resulting from only the first one due to Checkpoints returning the value of the first checkpoint of the block.
Vulnerability Detail
Currently only exiting can't be carried out more than once in the same block, staking can happen more than once.
This will yield lower than actual stakedByAt() and balanceOfAt() readings whenever several staking calls happened in one block.
Impact
Erroneous readings can and most probably will impact downstream systems and can lead to their user's losses.
Reading other system's balance is a common component of decision making in a typical Vault contract. StakingModule can be a strategy therein and readings of the current holdings of the Vault will impact the course of its actions. Vault can have been depositing more than once in a block say as a result of actions of their users, for example it could been two deposits from different users in the same block, and parts of each of them was staked with StakingModule.
By having stakedByAt() reported value associated with the first deposit only, StakingModule biases the actions of the Vault, which can lead to losses for its users and then to removal of Telcoin integration, which is loss of market share that can later translates to Telcoin value.
Code Snippet
Historical requests used in stakedByAt() are vulnerable to stale Checkpoints readings:
/// @return Amount staked by an account at a specific block number excluding claimable yield.
/// @param account Account to query staked amount
/// @param blockNumber Block at which to query staked amount
function stakedByAt(address account, uint256 blockNumber) public view returns (uint256) {
return _stakes[account].getAtBlock(blockNumber);
}
stakedByAt() is used for historical balance readings:
function balanceOfAt(address account, uint256 blockNumber, bytes calldata auxData) external view returns (uint256) {
return stakedByAt(account, blockNumber) + claimableAt(account, blockNumber, auxData);
}
checkpointProtection() is added to some functions (_partialExit() and _claimAndExit()), but _stake() is left unprotected:
https://github.com/sherlock-audit/2022-11-telcoin-judging/issues/83
modifier checkpointProtection(address account) {
uint256 numCheckpoints = _stakes[account]._checkpoints.length;
require(numCheckpoints == 0 || _stakes[account]._checkpoints[numCheckpoints - 1]._blockNumber != block.number, "StakingModule: Cannot exit in the same block as another stake or exit");
_;
}
function _stake(address account, address from, uint256 amount) private {
require(amount > 0, "Cannot stake 0");
uint256 stakedBefore = _stakes[account].latest();
uint256 stakedAfter = stakedBefore + amount;
// notify plugins
_notifyStakeChangeAllPlugins(account, stakedBefore, stakedAfter);
// update _stakes
_stakes[account].push(stakedAfter);
I.e. what currently implemented is a fix for the flash loan vector, but stakedByAt() readings can still be wrong.
Tool used
Manual Review
Recommendation
Consider adding the checkpointProtection() check to _stake():
- function _stake(address account, address from, uint256 amount) private {
+ function _stake(address account, address from, uint256 amount) private checkpointProtection(account) {
require(amount > 0, "Cannot stake 0");
uint256 stakedBefore = _stakes[account].latest();
uint256 stakedAfter = stakedBefore + amount;
// notify plugins
_notifyStakeChangeAllPlugins(account, stakedBefore, stakedAfter);
// update _stakes
_stakes[account].push(stakedAfter);
This way both staking and unstaking will be restricted to once per block due to the usage of Checkpoints, which needs to be documented as a known limitation.
Discussion
amshirif
Two different issues are in the same PR because they both stem from the same modifier.
amshirif
https://github.com/telcoin/telcoin-audit/pull/6
dmitriia
Looks ok, but since checkpointProtection is removed it needs to be documented that stakedByAt() and balanceOfAt() return first known state instead of the final state of any block due to Checkpoints logic.
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 checkpoint mechanism only stores one value per block, and the stake function lacks a guard to prevent multiple stakes in the same block, so historical queries return the first checkpoint instead of the final state.
Impact
Erroneous readings can and most probably will impact downstream systems and can lead to their user's losses.
What the attacker needed
Multiple stake calls in one block
The fix
The recommendation was to add the checkpointProtection modifier to _stake(), but the discussion indicates that the modifier was removed and the limitation should be documented instead.
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.
A smart contract tracks how much each user has staked over time. When someone stakes twice in the same block, the contract's history only records the first stake for that block, so later queries about the balance at that block give a wrong, lower number. Other contracts that rely on this number might make bad decisions.
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.