Submitted by sirhashalot
The \_calculateRewardAmount function casts epoch timestamps from uint256 to uint64 and these may overflow. The epochStartTimestamp value is a function of the user-supplied \_epochId value, which could be extremely large (up to 2**255  1). While Solidity 0.8.x checks for overflows on arithmetic operations, it does not do so for casting  the OpenZeppelin SafeCast library offers this. The overflow condition could cause \_epochStartTimestamp > \_epochEndTimestamp, which the Ticket.sol getAverageBalanceBetween may not be expected to handle. The \_epochStartTimestamp could overflow to have a value before the actual start of the promotion, also impacting the rewards calculation.
There are 4 uint64 casting operations in the \_calculateRewardAmount function of TwabRewards.sol:
https://github.com/pooltogether/v4-periphery/blob/b520faea26bcf60371012f6cb246aa149abd3c7d/contracts/TwabRewards.sol#L304-L312
While requiring \_epochId <= 255 may help, it does not remove the issue entirely, because a very large \_epochDuration value can still cause an overflow in the product (\_epochDuration \* \_epochId) used in \_epochStartTimestamp. However, other options exist:
PierrickGT (PoolTogether) commented:
LSDan (judge) commented:
PierrickGT (PoolTogether) confirmed and resolved:
