Recreated pools receive a wrong AVAX amount due to miscalculated compounded liquid staker amount
The recreateMinipool function incorrectly compounds rewards by assuming node operators and liquid stakers earn equal rewards, but liquid stakers receive less due to a commission fee. This causes the liquid staker amount to be overstated, potentially requiring more AVAX from the liquid staking pool than available and affecting accounting.
After recreation, minipools will receive more AVAX than the sum of their owners’ current stake and the rewards that they generated.
Proof of Concept
Multipools that successfully finished validation may be recreated by multisigs, before staked GGP and deposited AVAX have been withdrawn by minipool owners (MinipoolManager.sol#L444). The function compounds deposited AVAX by adding the rewards earned during previous validation periods to the AVAX amounts deposited and requested from stakers (MinipoolManager.sol#L450-L452):
Minipool memory mp = getMinipool(minipoolIndex);
// Compound the avax plus rewards
// NOTE Assumes a 1:1 nodeOp:liqStaker funds ratio
uint256 compoundedAvaxNodeOpAmt = mp.avaxNodeOpAmt + mp.avaxNodeOpRewardAmt;
setUint(keccak256(abi.encodePacked("minipool.item", minipoolIndex, ".avaxNodeOpAmt")), compoundedAvaxNodeOpAmt);
setUint(keccak256(abi.encodePacked("minipool.item", minipoolIndex, ".avaxLiquidStakerAmt")), compoundedAvaxNodeOpAmt);
The function assumes that a node operator and liquid stakers earned an equal reward amount: compoundedAvaxNodeOpAmt is calculated as the sum of the current AVAX deposit of the minipool owner and the node operator reward earned so far. However, liquid stakers get a smaller reward than node operators: the minipool node commission fee is applied to their share (MinipoolManager.sol#L417):
As a result, the avaxLiquidStakerAmt set in the recreateMinipool function will always be bigger than the actual amount since it equals to the compounded node operator amount, which includes node operator rewards.
Next, in the recreateMinipool function, the assigned AVAX amount is increased by the amount borrowed from liquid stakers + the node operator amount, which is again wrong because the assigned AVAX amount can only be increased by the liquid stakers’ reward share (MinipoolManager.sol#L457-L459):
As a result, the amount of AVAX borrowed from liquid stakers by the minipool will be increased by the minipool node commission fee, the increased amount will be sent to the validator, and it will be required to end the validation period.
The following PoC demonstrates the wrong calculation:
// test/unit/MinipoolManager.t.sol
function testRecreateMinipoolWrongLiquidStakerReward_AUDIT() public {
uint256 duration = 4 weeks;
uint256 depositAmt = 1000 ether;
uint256 avaxAssignmentRequest = 1000 ether;
uint256 validationAmt = depositAmt + avaxAssignmentRequest;
// Enough to start but not to re-stake, we will add more later
uint128 ggpStakeAmt = 100 ether;
vm.startPrank(nodeOp);
ggp.approve(address(staking), MAX_AMT);
staking.stakeGGP(ggpStakeAmt);
MinipoolManager.Minipool memory mp = createMinipool(depositAmt, avaxAssignmentRequest, duration);
vm.stopPrank();
address liqStaker1 = getActorWithTokens("liqStaker1", MAX_AMT, MAX_AMT);
vm.prank(liqStaker1);
ggAVAX.depositAVAX{value: MAX_AMT}();
vm.prank(address(rialto));
minipoolMgr.claimAndInitiateStaking(mp.nodeID);
bytes32 txID = keccak256("txid");
vm.prank(address(rialto));
minipoolMgr.recordStakingStart(mp.nodeID, txID, block.timestamp);
skip(duration / 2);
// Give rialto the rewards it needs
uint256 rewards = 10 ether;
deal(address(rialto), address(rialto).balance + rewards);
// Pay out the rewards
vm.prank(address(rialto));
minipoolMgr.recordStakingEnd{value: validationAmt + rewards}(mp.nodeID, block.timestamp, rewards);
MinipoolManager.Minipool memory mpAfterEnd = minipoolMgr.getMinipoolByNodeID(mp.nodeID);
assertEq(mpAfterEnd.avaxNodeOpAmt, depositAmt);
assertEq(mpAfterEnd.avaxLiquidStakerAmt, avaxAssignmentRequest);
// After the validation periods has ended, the node operator and liquid stakers got different rewards,
// since a fee was taken from the liquid stakers' share.
uint256 nodeOpReward = 5.75 ether;
uint256 liquidStakerReward = 4.25 ether;
assertEq(mpAfterEnd.avaxNodeOpRewardAmt, nodeOpReward);
assertEq(mpAfterEnd.avaxLiquidStakerRewardAmt, liquidStakerReward);
// Add a bit more collateral to cover the compounding rewards
vm.prank(nodeOp);
staking.stakeGGP(1 ether);
vm.prank(address(rialto));
minipoolMgr.recreateMinipool(mp.nodeID);
MinipoolManager.Minipool memory mpCompounded = minipoolMgr.getMinipoolByNodeID(mp.nodeID);
// After pool was recreated, node operator's amounts were increased correctly.
assertEq(mpCompounded.avaxNodeOpAmt, mp.avaxNodeOpAmt + nodeOpReward);
assertEq(mpCompounded.avaxNodeOpAmt, mp.avaxNodeOpInitialAmt + nodeOpReward);
assertEq(staking.getAVAXStake(mp.owner), mp.avaxNodeOpAmt + nodeOpReward);
// However, liquid stakers' amount were increased incorrectly: nodeOpReward was added instead of liquidStakerReward.
// These assertions will fail:
// expected: 1004.25 ether, actual 1005.75 ether
assertEq(mpCompounded.avaxLiquidStakerAmt, mp.avaxLiquidStakerAmt + liquidStakerReward);
assertEq(staking.getAVAXAssigned(mp.owner), mp.avaxLiquidStakerAmt + liquidStakerReward);
}
Recommended Mitigation Steps
When compounding rewards in the recreateMinipool function, consider using an average reward so that node operator’s and liquid stakers’ deposits are increased equally and the entire reward amount is used:
--- a/contracts/contract/MinipoolManager.sol
+++ b/contracts/contract/MinipoolManager.sol
@@ -443,18 +443,22 @@ contract MinipoolManager is Base, ReentrancyGuard, IWithdrawer {
/// @param nodeID 20-byte Avalanche node ID
function recreateMinipool(address nodeID) external whenNotPaused {
int256 minipoolIndex = onlyValidMultisig(nodeID);
requireValidStateTransition(minipoolIndex, MinipoolStatus.Prelaunch);
Minipool memory mp = getMinipool(minipoolIndex);
// Compound the avax plus rewards
// NOTE Assumes a 1:1 nodeOp:liqStaker funds ratio
- uint256 compoundedAvaxNodeOpAmt = mp.avaxNodeOpAmt + mp.avaxNodeOpRewardAmt;
+ uint256 nodeOpRewardAmt = getUint(keccak256(abi.encodePacked("minipool.item", minipoolIndex, ".avaxNodeOpRewardAmt")));
+ uint256 liquidStakerRewardAmt = getUint(keccak256(abi.encodePacked("minipool.item", minipoolIndex, ".avaxLiquidStakerRewardAmt")));
+ uint256 avgRewardAmt = (nodeOpRewardAmt + liquidStakerRewardAmt) / 2;
+
+ uint256 compoundedAvaxNodeOpAmt = mp.avaxNodeOpAmt + avgRewardAmt;
setUint(keccak256(abi.encodePacked("minipool.item", minipoolIndex, ".avaxNodeOpAmt")), compoundedAvaxNodeOpAmt);
setUint(keccak256(abi.encodePacked("minipool.item", minipoolIndex, ".avaxLiquidStakerAmt")), compoundedAvaxNodeOpAmt);
Staking staking = Staking(getContractAddress("Staking"));
// Only increase AVAX stake by rewards amount we are compounding
// since AVAX stake is only decreased by withdrawMinipool()
- staking.increaseAVAXStake(mp.owner, mp.avaxNodeOpRewardAmt);
+ staking.increaseAVAXStake(mp.owner, avgRewardAmt);
staking.increaseAVAXAssigned(mp.owner, compoundedAvaxNodeOpAmt);
staking.increaseMinipoolCount(mp.owner);
Also, consider sending equal amounts of rewards to the vault and the ggAVAX token in the recordStakingEnd function.
Talked to the rest of the team and this is not valid - it is working as designed. We are matching 1:1 with what the node operators are earning/staking.
The math is as follows:
-> Principal Deposited
-> Earned Rewards (both for Operator and Stakers)
-> Re-assing all -> Assigned amount has grown “too much” but in reality it’s the correct value
-> When withdrawing, the operator only get’s their portion of AVAX, meaning that the assigned as grown more, but they don’t get an anomalous amount of rewards
When recreating a minipool, we intend to allow Node Operators to compound their staking rewards for the next cycle.
Say that a minipool with 2000 AVAX was running. After one cycle the minipool was rewarded 20 AVAX. 15 AVAX goes to Node Operators and 5 AVAX to Liquid Stakers (in this example).
When we recreate the minipool we want to allow the Node Operator to run a minipool with 1015 AVAX. Right now we require a 1:1 match of Node Operator to Liquid Staker funds, so that means we’ll withdraw 1015 AVAX from the Liquid Staking pool. That’s 10 AVAX more than we deposited from this one minipool. We are relying on there being 10 free floating AVAX in the Liquid Staking fund to recreate this minipool.
The Warden says “The function assumes that a node operator and liquid stakers earned an equal reward amount”. We’re were not assuming that, but we are assuming that there will be some free AVAX in the Liquid Staker pool to withdraw more than we’ve just deposited from rewards.
All that being said, we do not like this assumption and will change to use avaxLiquidStakerAmt instead of avaxNodeOpAmt for both. Ensuring that we will always have enough Liquid Staker AVAX to recreate the minipool and we will maintain the one-to-one match.
The warden has shown a potential risk when it comes to accounting, fees and the requirement on liquid funds, more specifically the accounting in storage will use the values earned by the validator, however when recreating new funds will need to be pulled.
The discrepancy may cause issues.
I believe this report has shown a potential risk, but I also think the Warden should have spent more time explaining it in depth vs stopping at the potential invariant being broken.
I’m flagging this out of caution after considering scrapping it / inviting the Wardens to follow up in mitigation review.
The Warden says “The function assumes that a node operator and liquid stakers earned an equal reward amount”. We’re were not assuming that
As can be seen from the linked code snippet (the comments, specifically):
// Compound the avax plus rewards
// NOTE Assumes a 1:1 nodeOp:liqStaker funds ratio
uint256 compoundedAvaxNodeOpAmt = mp.avaxNodeOpAmt + mp.avaxNodeOpRewardAmt;
The assumption is that the 1:1 funds ratio is preserved after rewards have been accounted. This can only be true when the reward amounts are equal, which is violated due to the fees applied to avaxLiquidStakerRewardAmt.
@Alex the Entreprenerd - I’m sorry for not providing more details on how this can affect the entire system. Yes, my assumption was that extra staker funds would be required to recreate a pool, and there might be not enough funds staked (and deeper accounting may be affected, as pointed out by @Franfran), while the earned rewards + the previous staked amounts would be enough to recreate a pool.
Thus, the mitigation that uses avaxLiquidStakerAmt for both amounts looks good to me, since, out of the two amounts, the smaller one will always be picked, which won’t require pulling extra funds from stakers. The difference between this mitigation and the mitigation suggested in the report, is that the latter uses the full reward amount in a recreated pool, while using avaxLiquidStakerAmt for both amounts leaves a portion of the reward idle.
I also agree with the medium severity, the High Risk label was probably a misclick.
Status: Mitigation not confirmed. Full details in report from RaymondFam, and also included in Mitigation Review section below.
The original report as published on Code4rena, by Jeiwan. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
The code assumes a 1:1 ratio between node operator and liquid staker rewards when compounding, but the commission fee makes liquid staker rewards smaller.
Impact
The liquid staker amount is overstated, which may require pulling extra AVAX from the liquid staking pool to recreate the minipool, potentially causing accounting issues or failures if insufficient free AVAX exists.
What the attacker needed
A minipool that has completed validation; the minipool is recreated by a multisig; sufficient free AVAX in the liquid staking pool
The fix
The fix was to use the liquid staker AVAX amount instead of the node operator amount for both, ensuring a 1:1 match and avoiding the need for extra funds.
How it was found
Create a minipool with a deposit and assignment request.
Stake GGP and deposit AVAX as a liquid staker.
Start and end the validation period, recording rewards.
Recreate the minipool.
Observe that the liquid staker amount is increased by the node operator reward instead of the liquid staker reward.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
When a staking pool is recreated, the code adds rewards to the deposited amounts. It mistakenly gives liquid stakers the same reward as node operators, but liquid stakers actually get less because of a fee. This means the pool thinks it has more AVAX from liquid stakers than it really does, which could cause problems when trying to withdraw funds.
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.