Skip to content
Business logic Audit finding Intermediate

TwabLib::getTwabBetween can return inaccurate balances if _startTime and _endTime aren’t safely bound

The PrizePool contract calls TwabLib::getTwabBetween without ensuring the queried time range is safe, so it may use extrapolated balances that are not final. This could cause prizes to be awarded to the wrong users or not awarded to the right ones.

Infect3d Follow with Aymen0909

Originally disclosed on Code4rena

Submitted by Infect3d, also found by Aymen0909

Here’s the documentation of the TwabLib::getTwabBetween function:

File: twab-controller\src\libraries\TwabLib.sol

278:   /**
279:    * @notice Looks up a users TWAB for a time range.
280:    * @dev If the timestamps in the range are not exact matches of observations, the balance is extrapolated using the previous observation.
281:    * @dev Ensure timestamps are safe using isTimeRangeSafe.
282:    * @param _observations The circular buffer of observations
283:    * @param _accountDetails The account details to query with
284:    * @param _startTime The start of the time range
285:    * @param _endTime The end of the time range
286:    * @return twab The TWAB for the time range
287:    */
288:   function getTwabBetween(  //@audit - M01: dev comment (isTimeRangeSafe) doesn't seems to be followed in PrizePool calling it
289:     uint32 PERIOD_OFFSET,
290:     ObservationLib.Observation[MAX_CARDINALITY] storage _observations,
291:     AccountDetails memory _accountDetails,
292:     uint32 _startTime,
293:     uint32 _endTime
294:   ) internal view returns (uint256) {

TwabLib::getTwabBetween -function gets called in. Then, PrizePool::_getVaultUserBalanceAndTotalSupplyTwab gets called by PrizePool::claimPrize, returning twab for a specified time range, which finally is called by PrizePool::isWinner in PrizePool::claimPrize.

The thing is, as explained by the documentation, values from balance history aren’t guaranteed to be accurate if the timestamp at which time it is queried do not fall between:

  • The newest observation in a period.
  • The end of that period (period must have ended).

That is because until a period has ended, we cannot be sure the value we are querying hasn’t changed in the future (from pov of the timestamp).

Impact

This means, that if a prize is claimed, but timestamps at which the twab is getting requested do not follow these rules, the Prize Pool contract might distribute prizes based on inaccurate data. This could lead to the prizes being distributed to users who should not have won, or not being distributed to users who should have won.

File: prize-pool\src\PrizePool.sol

920:   function _getVaultUserBalanceAndTotalSupplyTwab(
921:     address _vault,
922:     address _user,
923:     uint256 _drawDuration
924:   ) internal view returns (uint256 twab, uint256 twabTotalSupply) {
925:     uint32 _endTimestamp = uint32(_lastClosedDrawStartedAt + drawPeriodSeconds);
926:     uint32 _startTimestamp = uint32(_endTimestamp - _drawDuration * drawPeriodSeconds);
927:     //@audit - dev comment for TwabLib::getTwabBetween doesn't seems to be followed in PrizePool calling it
928:     twab = twabController.getTwabBetween(_vault, _user, _startTimestamp, _endTimestamp);
929:
930:     twabTotalSupply = twabController.getTotalSupplyTwabBetween(
931:       _vault,
932:       _startTimestamp,
933:       _endTimestamp
934:     );
935:   }

Recommended Mitigation Steps

Add a isTimeRangeSafe check (will depend on how devs want to handle it, either a revert or another error management solution) before getTwabBetween to ensure queried balances are accurate; otherwise, prize distribution could be unfair.

  function _getVaultUserBalanceAndTotalSupplyTwab(
    address _vault,
    address _user,
    uint256 _drawDuration
  ) internal view returns (uint256 twab, uint256 twabTotalSupply) {
    uint32 _endTimestamp = uint32(_lastClosedDrawStartedAt + drawPeriodSeconds);
    uint32 _startTimestamp = uint32(_endTimestamp - _drawDuration * drawPeriodSeconds);
    //@audit dev comment doesn't seems to be followed in PrizePool calling it
    //@audit added isTimeRangeSafe before getTwabBetween
    require(twabController.isTimeRangeSafe(_vault, _user, _startTimestamp, _endTimestamp), "Time range not safe");
    twab = twabController.getTwabBetween(_vault, _user, _startTimestamp, _endTimestamp);

    twabTotalSupply = twabController.getTotalSupplyTwabBetween(
      _vault,
      _startTimestamp,
      _endTimestamp
    );
  }

Assessed type

Invalid Validation

asselstine (PoolTogether) confirmed

PoolTogether mitigated:

Ensure search timestamps are on or at period end timestamps. PR: https://github.com/GenerationSoftware/pt-v5-twab-controller/pull/5

Status: Mitigation confirmed. Full details in reports from dirk_y and rvierdiiev.


The original report as published on Code4rena, by Infect3d. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 PrizePool contract failed to call isTimeRangeSafe before querying getTwabBetween, so it used a time range that was not guaranteed to be final.

Impact

Prizes could be distributed based on inaccurate balance data, potentially awarding users who should not have won or failing to award those who should have.

What the attacker needed

Prize claim occurs; time range not safe

The fix

The issue was mitigated by ensuring search timestamps are on or at period end timestamps.

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.

A time-weighted average balance (TWAB) is a way to track how much a user held over a period. The code that calculates it can give wrong answers if you ask about a time range that hasn't fully settled. Here, the prize pool asked for such a range without checking it was safe, so prizes could go to the wrong people.

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.

More by Infect3d

View all

8 more disclosed reports, best first.

More from PoolTogether

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.