Vault portion calculation in `PrizePool::getVaultPortion()` is incorrect as `_startDrawIdInclusive` has been erased
The vault portion calculation in PrizePool::getVaultPortion() is incorrect because it uses a start draw ID that has been overwritten in the total accumulator but not in the vault and donation accumulators. This can give users higher than expected chances of winning prizes or cause claims to revert due to underflow.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-05-pooltogether-judging/issues/96
Found by
0x73696d616f
Summary
The vault portion calculation in PrizePool::getVaultPortion() is incorrect because it fetches a _startDrawIdInclusive that has been overwritten in the total accumulator but not in the vaults accumulator or the donation accumulator.
Vulnerability Detail
To illustrate the issue, consider the grand prize, where odds are 1 / g = 0.00273972602e18 with g == 365 days. The estimated prize frequency is 1e18 / 0.00273972602e18 == 365.000000985 rounded up to the nearest integer, 366. If lastAwardedDrawId_ is 366, then startDrawIdInclusive is 366 - 366 + 1 == 1.
This means that it will fetch the total, vault and donation accumulators since the opening of drawId == 1.
However, when lastAwardedDrawId_ == 366, the current open drawId is 367, which is written to index 0 in the circular buffer, overwriting drawId == 1. But, in the donation and vault cases, the accumulator still has drawId == 1, as the buffer will likely not have been overwritten (draw periods take 1 day, it is highly likely the individual accumulators are not written to every day, but the total accumulator is, or users may exploit this on purpose). As the buffer is circular, this will keep happening every time after the buffer has been filled and indexes start being overwritten.
Thus, due to this, the vault portion will be bigger than it should, as the total accumulator is calculated between 365 days (from 2 to 366, as 1 has been erased), but the vault and donation accumulators for 366 days (between 1 and 366). Users will have bigger than expected chances of winning prizes and in the worst case, they may even not be able to claim prizes at all, in case the erased drawId had a significant contribution to the prizes through a donation, and then it underflows when doing totalSupply = totalContributed - totalDonated; in PrizePool::getVaultShares().
Add the following test to PrizePool.t.sol and log the vault and total portions in PrizePool::getVaultShares(). The contribution of the vault will be bigger than the total.
function testAccountedBalance_POC() public {
for (uint i = 0; i < 366; i++) {
contribute(100e18);
awardDraw(1);
mockTwab(address(this), msg.sender, 0);
}
address otherVault = makeAddr("otherVault");
vm.prank(otherVault);
prizePool.contributePrizeTokens(otherVault, 0);
uint256 prize = claimPrize(msg.sender, 0, 0);
}
Impact
Users get better prize chances than supposed and/or it can lead to permanent reverts when trying to claim prizes.
Code Snippet
Tool used
Manual Review
Vscode
Recommendation
When calculating PrizePool::computeRangeStartDrawIdInclusive(), the rangeSize should be capped to 365 to ensure the right startDrawIdInclusive is fetched, not one that has already been erased. In the scenario above, if the range is capped at 365, when lastAwardedDrawId_ == 366, startDrawIdInclusive would be 366 - 365 + 1 == 2, which has not been erased as the current drawId is 367, having only overwritten drawId == 1, but 2 still has the information.
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/GenerationSoftware/pt-v5-prize-pool/pull/115
10xhash
Fixed.
Now the max range of draws to look for is limited to grandPrizePeriodDraws
sherlock-admin2
The Lead Senior Watson signed off on the fix.
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 start draw ID used in getVaultPortion() can refer to a draw that has been overwritten in the total accumulator but not in the vault and donation accumulators, causing inconsistent ranges.
Impact
Users could have higher than expected chances of winning prizes, and in the worst case, prize claims may revert permanently due to underflow.
What the attacker needed
The prize pool has been running for at least 366 draws; a user contributes to the prize pool
The fix
The range size in computeRangeStartDrawIdInclusive() should be capped to 365 (or grandPrizePeriodDraws) to avoid fetching an erased draw ID; the protocol team fixed this issue.
How it was found
- Contribute to the prize pool and award draws repeatedly until the circular buffer wraps around.
- Observe that the total accumulator has overwritten the earliest draw while the vault and donation accumulators still contain it.
- Call getVaultPortion() and note that the calculated vault portion is larger than the total portion.
- Attempt to claim a prize and observe potential underflow or revert.
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.
The prize pool tracks contributions over time using a circular buffer that overwrites old data. When calculating a user's share of the vault, it looks back a certain number of draws, but sometimes the starting draw has already been overwritten in one record but not another, leading to mismatched totals. This can make users' chances of winning larger than intended or even break prize claims.
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.