Increasing reserves breaks PrizePool accounting
The PrizePool contract's accounting does not update when someone calls increaseReserve, so the accounted balance remains lower than the actual token balance. This allows any vault to call contributePrizeTokens and effectively steal the reserve contribution, double-counting the tokens.
Submitted by dirk_y, also found by Jeiwan, 0xStalin, and seeques (1, 2)
Lines of code
https://github.com/GenerationSoftware/pt-v5-prize-pool/blob/4bc8a12b857856828c018510b5500d722b79ca3a/src/PrizePool.sol#L498-L502 https://github.com/GenerationSoftware/pt-v5-prize-pool/blob/4bc8a12b857856828c018510b5500d722b79ca3a/src/PrizePool.sol#L743-L746 https://github.com/GenerationSoftware/pt-v5-prize-pool/blob/4bc8a12b857856828c018510b5500d722b79ca3a/src/PrizePool.sol#L312
Impact
When anyone calls the increaseReserve method in PrizePool.sol the accounted balance in the prize pool isn’t properly updated. This allows a vault to effectively steal the prize token contribution and this contribution gets distributed during draws; effectively double counting the initial injection into the reserves. The actual prize token balance of the prize pool will be below the accounted balance of the prize pool as time goes on.
Proof of Concept
As mentioned in the audit README:
“the balance of prize tokens held by the contract must always be equal to the sum of the available tier liquidity and the reserve. When contributing liquidity, the prize pool will temporarily hold a balance greater than the accounted balance, but otherwise the two should match”.
Unfortunately, this is broken when anyone contributes directly to the reserve by calling increaseReserve.
In the normal audit flow, the reserve is increased when a draw is closed by the draw manager. During calls to closeDraw, the next draw is started with a given number of tiers and the contributions for the round are calculated and split across the tiers and the reserve:
_nextDraw(_nextNumberOfTiers, uint96(_contributionsForDraw(lastClosedDrawId + 1)));
Under the hood, this calls _computeNewDistributions which calculates the amount to increase the reserves, based on the number of reserve shares and the new prize token liquidity being contributed in this round. During this flow, the actual balance of reward tokens held in the prize pool are equal to the accounted balance.
The break in accounting occurs when calling increaseReserve:
function increaseReserve(uint104 _amount) external {
_reserve += _amount;
prizeToken.safeTransferFrom(msg.sender, address(this), _amount);
emit IncreaseReserve(msg.sender, _amount);
}
As you can see, the prize tokens are transferred into the pool and the reserve increased. But the accounted balance is unchanged:
function _accountedBalance() internal view returns (uint256) {
Observation memory obs = DrawAccumulatorLib.newestObservation(totalAccumulator);
return (obs.available + obs.disbursed) - _totalWithdrawn;
}
Because the accounted balance is unchanged, any vault can now call contributePrizeTokens to effectively steal the funds meant for the reserve:
function contributePrizeTokens(address _prizeVault, uint256 _amount) external returns (uint256) {
uint256 _deltaBalance = prizeToken.balanceOf(address(this)) - _accountedBalance();
This increases the relevant vault accumulator and the total accumulator; thereby, effectively double counting the same prize tokens, since we’ve already increased _reserve.
Recommended Mitigation Steps
The accounted balance of the prize pool should be updated when increaseReserve is called. I think the easiest way of achieving this is having a tracker for “reserve injections”:
diff --git a/src/PrizePool.sol b/src/PrizePool.sol
index a42a27e..3c14476 100644
--- a/src/PrizePool.sol
+++ b/src/PrizePool.sol
@@ -233,6 +233,9 @@ contract PrizePool is TieredLiquidityDistributor {
/// @notice The total amount of prize tokens that have been claimed for all time.
uint256 internal _totalWithdrawn;
+ /// @notice The total amount of reserve injections that have been performed for all time.
+ uint256 internal _reserveInjections;
+
/// @notice The winner random number for the last closed draw.
uint256 internal _winningRandomNumber;
@@ -497,6 +500,7 @@ contract PrizePool is TieredLiquidityDistributor {
/// @param _amount The amount of tokens to increase the reserve by
function increaseReserve(uint104 _amount) external {
_reserve += _amount;
+ _reserveInjections += amount;
prizeToken.safeTransferFrom(msg.sender, address(this), _amount);
emit IncreaseReserve(msg.sender, _amount);
}
@@ -742,7 +746,7 @@ contract PrizePool is TieredLiquidityDistributor {
/// @return The balance of tokens that have been accounted for
function _accountedBalance() internal view returns (uint256) {
Observation memory obs = DrawAccumulatorLib.newestObservation(totalAccumulator);
- return (obs.available + obs.disbursed) - _totalWithdrawn;
+ return (obs.available + obs.disbursed) - _totalWithdrawn + _reserveInjections;
}
/// @notice Returns the start time of the draw for the next successful closeDraw
Assessed type
Math
asselstine (PoolTogether) confirmed and commented via duplicate issue #200:
Going to add some notes here:
The issue is that
_accountedBalance()computes(obs.available + obs.disbursed) - _totalWithdrawn, which basically means the total contributed liquidity minus the withdrawn liquidity.However, when adding liquidity manually, via
increaseReserve, it does not increase the contributed liquidity.This issue points to back-running as the problem, but that’s not the case. The case is improper accounting, as in this issue that was closed as a duplicate.
Fixed reserve accounting. PR: https://github.com/GenerationSoftware/pt-v5-prize-pool/pull/18
Status: Mitigation confirmed. Full details in reports from dirk_y, rvierdiiev and 0xStalin.
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 _accountedBalance function does not include reserve injections, so the accounted balance is lower than the actual token balance after increaseReserve is called.
Impact
A vault could steal the prize token contribution intended for the reserve, and the actual prize token balance will fall below the accounted balance over time.
What the attacker needed
Anyone can call increaseReserve; a vault calls contributePrizeTokens
The fix
The accounted balance should be updated when increaseReserve is called, for example by tracking reserve injections and adding them to the accounted balance; the issue was mitigated with a fix for reserve accounting.
How it was found
- Call increaseReserve to add tokens to the reserve.
- Call contributePrizeTokens from a vault to claim the excess tokens as contributions.
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 keeps a record of how many tokens it should have. When someone adds tokens directly to the reserve, the record is not updated, so the pool thinks it has fewer tokens than it really does. Another function then lets a vault claim those extra tokens as if they were new contributions, taking money that was meant for the reserve.
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.