Division by zero error can block RewardsPool#startRewardCycle if all multisig wallet are disabled
The RewardsPool contract's startRewardsCycle function divides the multisig reward allotment by the number of enabled multisig wallets. If all multisig wallets are disabled, this division by zero causes the transaction to revert, blocking the reward cycle from starting.
ladboy233 Follow with pauliax, unforgiven and 3 others
Originally disclosed on Code4rena
Submitted by ladboy233, also found by pauliax, unforgiven, 141345, peakbolt, and rvierdiiev
https://github.com/code-423n4/2022-12-gogopool/blob/aec9928d8bdce8a5a4efe45f54c39d4fc7313731/contracts/contract/RewardsPool.sol#L229 https://github.com/code-423n4/2022-12-gogopool/blob/aec9928d8bdce8a5a4efe45f54c39d4fc7313731/contracts/contract/RewardsPool.sol#L156
A user needs to call the function startRewardsCycle in RewardsPool.sol
/// @notice Public function that will run a GGP rewards cycle if possible
function startRewardsCycle() external {
which calls:
uint256 multisigClaimContractAllotment = getClaimingContractDistribution("ClaimMultisig");
uint256 nopClaimContractAllotment = getClaimingContractDistribution("ClaimNodeOp");
uint256 daoClaimContractAllotment = getClaimingContractDistribution("ClaimProtocolDAO");
if (daoClaimContractAllotment + nopClaimContractAllotment + multisigClaimContractAllotment > getRewardsCycleTotalAmt()) {
revert IncorrectRewardsDistribution();
}
TokenGGP ggp = TokenGGP(getContractAddress("TokenGGP"));
Vault vault = Vault(getContractAddress("Vault"));
if (daoClaimContractAllotment > 0) {
emit ProtocolDAORewardsTransfered(daoClaimContractAllotment);
vault.transferToken("ClaimProtocolDAO", ggp, daoClaimContractAllotment);
}
if (multisigClaimContractAllotment > 0) {
emit MultisigRewardsTransfered(multisigClaimContractAllotment);
distributeMultisigAllotment(multisigClaimContractAllotment, vault, ggp);
}
if (nopClaimContractAllotment > 0) {
emit ClaimNodeOpRewardsTransfered(nopClaimContractAllotment);
ClaimNodeOp nopClaim = ClaimNodeOp(getContractAddress("ClaimNodeOp"));
nopClaim.setRewardsCycleTotal(nopClaimContractAllotment);
vault.transferToken("ClaimNodeOp", ggp, nopClaimContractAllotment);
}
We need to pay special attention to the code block below:
if (multisigClaimContractAllotment > 0) {
emit MultisigRewardsTransfered(multisigClaimContractAllotment);
distributeMultisigAllotment(multisigClaimContractAllotment, vault, ggp);
}
which calls:
/// @notice Distributes GGP to enabled Multisigs
/// @param allotment Total GGP for Multisigs
/// @param vault Vault contract
/// @param ggp TokenGGP contract
function distributeMultisigAllotment(
uint256 allotment,
Vault vault,
TokenGGP ggp
) internal {
MultisigManager mm = MultisigManager(getContractAddress("MultisigManager"));
uint256 enabledCount;
uint256 count = mm.getCount();
address[] memory enabledMultisigs = new address[](count);
// there should never be more than a few multisigs, so a loop should be fine here
for (uint256 i = 0; i < count; i++) {
(address addr, bool enabled) = mm.getMultisig(i);
if (enabled) {
enabledMultisigs[enabledCount] = addr;
enabledCount++;
}
}
// Dirty hack to cut unused elements off end of return value (from RP)
// solhint-disable-next-line no-inline-assembly
assembly {
mstore(enabledMultisigs, enabledCount)
}
uint256 tokensPerMultisig = allotment / enabledCount;
for (uint256 i = 0; i < enabledMultisigs.length; i++) {
vault.withdrawToken(enabledMultisigs[i], ggp, tokensPerMultisig);
}
}
The code distributes the reward to all multisig evenly.
uint256 tokensPerMultisig = allotment / enabledCount;
However, if the enabledCount is 0, meaning no multisig wallet is enabled, the transactions revert in division by zero error and revert the startRewardsCycle transaction.
As shown in POC.
In RewardsPool.t.sol,
we change the name from testStartRewardsCycle to testStartRewardsCycle_POC
we add the code to disable all multisig wallet. before calling rewardsPool.startRewardsCycle
// disable all multisg wallet
vm.prank(guardian);
ocyticus.disableAllMultisigs();
Then we run the test
forge test -vvv --match testStartRewardsCycle_POC
the transaction revert in division by zero error, which block the startRewardsCycle
│ ├─ emit MultisigRewardsTransfered(value: 13499352589262561353689)
│ ├─ [537] Storage::getAddress(0xcda836d09bcf3adcec2f52ddddeceac31738a574d5063511c887064e499593df) [staticcall]
│ │ └─ ← MultisigManager: [0xA12E9172eB5A8B9054F897cC231Cd7a2751D6D93]
│ ├─ [1313] MultisigManager::getCount() [staticcall]
│ │ ├─ [549] Storage::getUint(0x778484468bc504108f077f6bf471293e4138c2d117c6f33607855518cf4bda79) [staticcall]
│ │ │ └─ ← 1
│ │ └─ ← 1
│ ├─ [3050] MultisigManager::getMultisig(0) [staticcall]
│ │ ├─ [537] Storage::getAddress(0xfebe6f39b65f18e050b53df1d0c8d45b8c5cce333324eb048b67b8ee5f26b7a3) [staticcall]
│ │ │ └─ ← RialtoSimulator: [0x98D1613BC08756f51f46E841409E61C32f576F2f]
│ │ ├─ [539] Storage::getBool(0x7ef800e7ca09c0c1063313b56290c06f6bc4bae0e9b7af3899bb7d5ade0403c8) [staticcall]
│ │ │ └─ ← false
│ │ └─ ← RialtoSimulator: [0x98D1613BC08756f51f46E841409E61C32f576F2f], false
│ └─ ← "Division or modulo by 0"
└─ ← "Division or modulo by 0"
Test result: FAILED. 0 passed; 1 failed; finished in 11.64ms
Failing tests:
Encountered 1 failing test in test/unit/RewardsPool.t.sol:RewardsPoolTest
[FAIL. Reason: Division or modulo by 0] testStartRewardsCycle_POC() (gas: 332890)
Recommended Mitigation Steps
We recommend the project handle the case when the number of enabled multisig is 0 gracefully to not block the startRewardCycle transaction.
emersoncloud (GoGoPool) confirmed
Alex the Entreprenerd (judge) commented:
The Warden has shown a scenario that could cause the call to
startRewardsCycleto revert.When all multisigs are disabled (or no multisig is added), the division by zero will cause reverts.
While Admin Privilege is out of scope for this contest, the Warden has identified how a lack of zero-check can cause an open function to revert.
For this reason, I agree with Medium Severity.
emersoncloud (GoGoPool) mitigated:
Prevents division by zero error blocking startRewardCycle(): multisig-labs/gogopool#37
Status: Mitigation confirmed, but a new medium severity issue was found. Full details in reports from hansfriese and ladboy233. Also included in Mitigation Review section below.
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 code does not check whether the number of enabled multisig wallets is zero before performing a division by that count.
Impact
The startRewardsCycle function reverts, preventing the reward cycle from starting and blocking reward distribution until at least one multisig wallet is enabled.
What the attacker needed
All multisig wallets are disabled; Ability to call startRewardsCycle
The fix
The project mitigated the issue by preventing the division by zero error from blocking startRewardCycle().
How it was found
- Disable all multisig wallets by calling disableAllMultisigs as the guardian.
- Call startRewardsCycle on the RewardsPool contract.
- Observe the transaction revert with a division by zero error.
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 function that distributes rewards to multiple wallets calculates how much each wallet gets by dividing the total reward by the number of wallets. If there are no wallets to receive rewards, the division is by zero, which is not allowed in the programming language used, so the whole function fails and no rewards can be distributed.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.