code-423n4 / 2021-12-pooltogether-findings

0 stars 0 forks source link

Large _epochId value impacts rewards calculation #122

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Handle

sirhashalot

Vulnerability details

Impact

The _epochId value is a uint256 that can be provided by the user in the _epochIds array in the important claimRewards and getRewardsAmount functions. The epochId value should be between 0 and 255, as evidenced by the bit shifting of a uint256 type in the functions _updateClaimedEpoch and _isClaimedEpoch. However, while the epochId value will not be negative because it is an unsigned integer, the edge case of epochId > 255 is not handled well. A user may submit an epochId of 300, or 300000, which can result in the following unexpected situations:

In summary, the case of epochId > 255 results in unexpected edge cases which allow a user to manipulate the rewards calculation in unexpected ways, especially if further code modifications do not examine this edge case. Edge case complexity would be substantially reduced if epochId is required to be <= 255 in the TwabRewards.sol file.

Proof of Concept

Two external functions of TwabRewards.sol allow a user to provide epochId values.

  1. The claimRewards function: https://github.com/pooltogether/v4-periphery/blob/b520faea26bcf60371012f6cb246aa149abd3c7d/contracts/TwabRewards.sol#L162-L191
  2. The getRewards function: https://github.com/pooltogether/v4-periphery/blob/b520faea26bcf60371012f6cb246aa149abd3c7d/contracts/TwabRewards.sol#L209-L222

However, the _epochId value has cascading impact due to external function calls and the impact can extend to other pooltogether contracts. For example, the call flow for the claimRewards function is: claimRewards() → _calculateRewardAmount() → Ticket.getAverageBalanceBetween (out of current scope)→ TwabLib.getAverageBalanceBetween (out of current scope)

Recommended Mitigation Steps

PierrickGT commented 2 years ago

This will be fixed in the following issue by casting _epochIds to uint8: https://github.com/code-423n4/2021-12-pooltogether-findings/issues/3

PierrickGT commented 2 years ago

Duplicate of https://github.com/code-423n4/2021-12-pooltogether-findings/issues/3