Functions withdrawWithConfirm and depositWithConfirm incorrectly update genesisValidators state
The staking library fails to keep the genesisValidators list in sync before a subnet is bootstrapped: a validator who adds stake keeps an outdated weight, and a validator who leaves is not removed from the list. This leaves the genesis validator set and weights incorrect.
Originally disclosed on Code4rena
Submitted by sin1st3r__
Finding description and impact
The vulnerable functions are LibStaking::withdrawWithConfirm() and LibStaking::depositWithConfirm().
When the subnet is not yet bootstrapped and a validator has joined the subnet and staked funds, LibStaking::depositWithConfirm() does not update the weight of the validator if he increased his stake by calling addStake(). This will lead to the validator having an outdated weight (less than the weight he should have).
function depositWithConfirm(address validator, uint256 amount) internal {
..........
if (!s.bootstrapped) {
// add to initial validators avoiding duplicates if it
// is a genesis validator.
bool alreadyValidator;
uint256 length = s.genesisValidators.length;
for (uint256 i; i < length; ) {
if (s.genesisValidators[i].addr == validator) {
alreadyValidator = true;
break;
}
unchecked {
++i;
}
}
-----> if (!alreadyValidator) {
uint256 collateral = s.validatorSet.validators[validator].confirmedCollateral;
Validator memory val = Validator({
addr: validator,
weight: collateral,
metadata: s.validatorSet.validators[validator].metadata
});
s.genesisValidators.push(val);
}
}
}
When the subnet is not yet bootstrapped and a validator has joined the subnet and staked funds and then left before the subnet is bootstrapped by calling the function leave(), LibStaking::withdrawWithConfirm() does not update the genesisValidators state in a subnet which is not yet bootstrapped, which will lead to genesisValidators reflecting an incorrect list of genesis validators.
function withdrawWithConfirm(address validator, uint256 amount) internal {
SubnetActorStorage storage s = LibSubnetActorStorage.appStorage();
// record deposit that updates the total collateral
s.validatorSet.recordWithdraw(validator, amount);
// confirm deposit that updates the confirmed collateral
s.validatorSet.confirmWithdraw(validator, amount);
}
Recommended mitigation steps
Ensure that the genesisValidators state is updated in the function withdrawWithConfirm to ensure that changes are reflected properly if validators leave pre-bootstrap. Additionally, update the weight of the validator who increases his stake in depositWithConfirm
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 genesisValidators list is only appended to on first deposit and is never updated on later stake increases or withdrawals before bootstrap.
Impact
The genesis validator set and weights could be inaccurate before bootstrap, which may affect how the subnet is initialised.
What the attacker needed
Subnet not yet bootstrapped; validator has joined and staked
The fix
The genesisValidators state should be updated on withdrawal and the validator's weight updated on deposit before bootstrap.
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 subnet collects a list of founding validators before it starts running. This code adds a validator to that list once but never updates their weight when they stake more, and never removes them if they leave. So the founding list can show wrong weights or people who are no longer there.
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.