Unsafe uint64 casting may overflow
The `_calculateRewardAmount` function casts user-controlled epoch timestamps from uint256 to uint64 without overflow checks, so a very large `_epochId` could cause the start timestamp to overflow and become greater than the end timestamp. This could lead to incorrect reward calculations and potential loss of funds.
Originally disclosed on Code4rena
Submitted by sirhashalot
Impact
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.
Proof of Concept
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
Recommended Mitigation Steps
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:
- Making these uint256 variables of type uint64 and therefore removing the casting of uint256 to the small uint64 would remove this risk and probably be the most gas-efficient solution.
- Add
require(_epochEndTimestamp > _epochStartTimestamp);to line 299, next to the existing require statement and before the uint64 casting operations - Use the OpenZeppelin SafeCast library to prevent unexpected overflows.
PierrickGT (PoolTogether) commented:
Duplicate of https://github.com/code-423n4/2021-12-pooltogether-findings/issues/58
I do not consider this to be a duplicate of 58 because it describes an actual impact that, while extremely unlikely, could result in loss of funds. This also makes it a medium severity issue.
2 — Med (M): vulns have a risk of 2 and are considered “Medium” severity when assets are not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.
PierrickGT (PoolTogether) confirmed and resolved:
Fixed by casting to
uint64. Relevant code: https://github.com/pooltogether/v4-periphery/blob/e0010b689fb170daac77af5f62abba7ca1397524/contracts/TwabRewards.sol#L415-L417 https://github.com/pooltogether/v4-periphery/blob/master/contracts/interfaces/ITwabRewards.sol#L26-L28
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 code casts a user-controlled uint256 to uint64 without checking for overflow, and Solidity 0.8.x does not check casts by default.
Impact
This could cause incorrect reward calculations and potentially loss of funds, though the report notes the scenario is extremely unlikely.
What the attacker needed
User supplies a very large _epochId; extremely unlikely condition
The fix
The issue was fixed by casting to uint64, as confirmed by the project.
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.
Smart contracts often store numbers in fixed-size containers. Casting a large number into a smaller container can wrap it around to a small value, like an odometer rolling over. Here, a user could supply a huge epoch number that makes the start time wrap around, breaking the reward calculation.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.