Submitted by JCN, also found by carrotsmuggler, Chinmay, beber89, XDZIBECX, serial-coder, Varun_05, Infect3d, stackachu, AlexCzm, 0xdice91, jasonxiale, HighDuty, imare, cats, developerjordy, 0xaltego, 0xadrii, DanielArmstrong, cccz, sl1, wangxx2026, Akali, SECURITISE, smiling_heretic, Timeless, KupiaSec, PENGUN, alexzoid, Stormreckson, 0xpiken, SweetDream, whitehat-boys, klau5, btk, 0xivas, santipu_, asui, grearlake, TheSchnilch, kaden, Inference, and ether_sky
Users can stake into a gauge directly via the GuildToken or indirectly via the SurplusGuildMinter. When a user stakes into a new gauge (i.e. their weight goes from 0 to > 0) via GuildToken::incrementGauge, their lastGaugeLossApplied mapping for this gauge, which is how the system keeps track of whether or not the user deserves to be slashed, is set to the current timestamp:
GuildToken.sol#L247-L256
This ensures that any loss that occurred in the gauge, before the user staked, will result in the following condition: lastGaugeLossApplied[gauge][user] > lastGaugeLoss[gauge]. This means the user staked into the gauge after the gauge experienced a loss and therefore, they can not be slashed:
GuildToken.sol#L133-L140
The above function showcases the requirements that need to be met in order for a user to be slashed: the user must have been staked in the gauge when the gauge experienced a loss in order for the user to be slashed. With this in mind, let us observe the process that occurs when users stake via the SurplusGuildMinter:
When a user stakes into a gauge via the SurpluGuildMinter::stake function the SurplusGuildMinter::getRewards function is invoked:
SurplusGuildMinter.sol#L216-L236
As seen above, this function will retrieve the lastGaugeLoss for the specified gauge (term) the user is staking into and will identify this user as being slashed, i.e. slashed = true, if lastGaugeLoss > userStake.lastGaugeLoss. The issue lies in the fact that, at this point in the code execution, the userStake struct is a freshly initialized memory struct and therefore, all of the structs fields are set to 0. Thus, the check on lines 229-230 are really doing the following:
The above code will always set slashed to true if the specified gauge has experienced any loss in its history. Therefore, a gauge can have experienced a loss, been off-boarded, and then been re-onboarded at a future time and The SurplusGuildMinter will consider any user who stakes into this gauge to be slashed.
The code execution will then continue to lines 235-236, where the users stake is retrieved from storage (it is initialized to all 0s since the user has not staked yet) and if the stakeTime field is 0 (it is), the execution returns to the GuildToken::stake function:
SurplusGuildMinter.sol#L114-L125
The above code illustrates the only validation checks that are performed in this stake function. As long as the user is not attempting to stake into a gauge in the same block that the gauge experienced a loss, and the user is staking at least 1e18, the users stake position will be initialized (the user will be allowed to stake their Credit):
SurplusGuildMinter.sol#L114-L125
The user would naturally perform the next actions: They can call SurplusGuildMinter::getRewards when they want to receive rewards and they can call SurplusGuildMinter::unstake when they want to unstake from their position, i.e. withdraw their deposited Credit. It is important to note that when the unstake function is called, similar to the stake function, the getRewards function will first be invoked. As we previously observed, the getRewards function will consider the user slashed if the gauge had previously experienced any loss in its history. Therefore, after a user has staked, any call to getRewards will result in the following logic to execute:
SurplusGuildMinter.sol#L274-L289
As we can see above, when a user calls getRewards or unstake after staking into a gauge that has experienced a loss sometime in its history, the users stake position will be deleted (slashed). If the user is attempting to unstake then the execution flow will continue in the unstake function:
SurplusGuildMinter.sol#L158-L166C25
Since the user has been considered slashed, the execution will return on line 166 and the user will not be allowed to withdraw their staked Credit.
I would also like to note that the user will still have a chance to receive some earned Credit after the gauge experiences a profit. However, since the user is considered slashed, they will not be given any guild rewards:
SurplusGuildMinter.sol#L247-L264
As seen above, if the user is eligible to claim rewards (the gauge they staked into has experienced a profit), then they will be sent creditReward of Credit. However, since they are considered slashed, their guildReward is set to 0. This scenario will only occur if no one calls getReward for this user before the gauge generates a profit. If any call to getReward for this user is invoked before that, the user will not be able to receive any rewards and in both situations they will lose their staked Credit.
An additional, lesser effect, is that the Guild which was minted on behalf of the user who staked will not be unstaked from the gauge, it will not be burned, and the RateLimitedGuildMinters buffer will not be replenished. I.e. the following code from the unstake function will not execute:
SurplusGuildMinter.sol#L205-L208
Users utilizing the SurplusGuildMinter to stake into a gauge, which has previously experienced a loss sometime in its history, can be immediately slashed. This will result in the user losing out on any guild rewards (if they were staked into the gauge while it experienced a profit), but most importantly this will result in the user losing their staked principle (Credit).
To further illustrate the impact of this vulnerability, lets consider the following scenario:
A gauge experiences a loss. The gauge is then off-boarded. The gauge is re-onboarded in the future. A user stakes into this gauge via the SurplusGuildMinter. A malicious actor immediately calls getRewards(gauge, user) and the user is slashed and loses their staked Credit.
The following test describes the main impact highlighted above, in which a user stakes into a previously lossy gauge and is immediately slashed:
Place the following test inside of test/unit/loan/SurplusGuildMinter.t.sol:
Similar to how the GuildToken::incrementGauge function initializes a users lastGaugeLossApplied value, the SurplusGuildMinter should initialize a users userStake.lastGaugeLoss to block.timestamp. It should then compare the lastGaugeLoss to the users stored userStake.lastGaugeLoss instead of comparing the lastGaugeLoss to a freshly initialized userStake memory struct, whose fields are all 0.
eswak (Ethereum Credit Guild) confirmed via duplicate issue #1164
Note: For full discussion, see here.
