“TWAP” used is an observation-weighted-average-price, not a time-weighted one
The price oracle's moving average calculation ignores the time between observations, so it is not a true time-weighted average price. An attacker could exploit this by skipping or front-running updates to trade at an outdated price.
Submitted by IllIllI
While users are incentivized to call the heartbeat, the incentive may be removed later, or it may be more profitable to use old prices, so users may not call the heartbeat during unfavorable prices, leading to the TWAP price being incorrect, and users getting the wrong price for their assets.
A similar case of an incomplete TWAP algorithm was found to be of Medium risk.
Proof of Concept
A TWAP is a Time-Weighted average price, but the algorithm below does not take into account the time between observations:
File: /src/modules/PRICE.sol #1
134 // Calculate new moving average
135 if (currentPrice > earliestPrice) {
136 _movingAverage += (currentPrice - earliestPrice) / numObs;
137 } else {
138 _movingAverage -= (earliestPrice - currentPrice) / numObs;
139 }
140
141 // Push new observation into storage and store timestamp taken at
142 observations[nextObsIndex] = currentPrice;
143 lastObservationTime = uint48(block.timestamp);
144: nextObsIndex = (nextObsIndex + 1) % numObs;
While the Heart policy enforces an upper bound on how frequently updates are added to the average, there is no guarantee that users call beat() in a timely manner:
File: /src/policies/Heart.sol #2
92 function beat() external nonReentrant {
93 if (!active) revert Heart_BeatStopped();
94: if (block.timestamp < lastBeat + frequency()) revert Heart_OutOfCycle();
The incentive may be set to too low an amount:
File: /src/policies/Heart.sol #3
140 function setRewardTokenAndAmount(ERC20 token_, uint256 reward_)
141 external
142 onlyRole("heart_admin")
143 {
144 rewardToken = token_;
145 reward = reward_;
146 emit RewardUpdated(token_, reward_);
147: }
Or users may find it more profitable to skip a particular update, or front-run an unfavorable update, with a transaction that trades assets at the old price
Recommended Mitigation Steps
Always call an internal version of beat() that doesn’t revert, in functions that swap user assets. The code should also track the timestamps of when each beat() is called, and include the amount of time that has passed since the last beat, in the TWAP calculation
Oighty (Olympus) disagreed with severity and commented:
The referenced issue is a bit different than our use case since we will be using a much longer duration moving average. The goal is to get an approximate moving average over a certain period of time (e.g. 120 days) vs. an exact number since, as you say, the time of each observation cannot be guaranteed to be at a specific time. We believe that using a long duration with a sufficient number of observations will make this value close enough to the true value it is approximating, and prevents actors from manipulating the value by waiting to provide a specific value (1 out of ~360 obs doesn’t move the needle). The use of the “TWAP” term may be semantically inaccurate.
As for not guaranteeing that the update will be called or issues with several observations close to each other, see comments on #405 and #79.
The mitigations suggested do not seem to provide a solution that improves the system. Calling
beat()on user actions would not have the observations roughly evenly spaced. Tracking timestamps is possible, but I don’t see how it improves the data.
@Oighty - I think the warden is suggesting that the call to beat() in the user actions would do more to ensure that the “TWAP” stays up to date. If the call isn’t past the correct period, it would just return and make no change (costing some amount of gas, ofc).
I do think it may be worth considering, that way no user action can take place without the TWAP being as up to date as possible and no additional calls to the contract may be necessary if users are interacting with the contract frequently enough.
While this is related to #79 - I think the points raised here and the mitigation is sufficiently different to warrant this issue to stand alone.
That’s a fair point. One issue with calling
beaton user actions, e.g.Operator.swap, is that it would update the wall price that the user is swapping at. Therefore, the call could fail due to the slippage check. This could be confusing behavior and may have unintended consequences of DOS’ing the system. Additionally, the gas cost ofbeatis highly variable (sometimes up to 600k gas when opening a bond market) and would cause some users to unexpectedly pay a lot more gas for a swap.I’ll discuss with the team, but I don’t think the pros exceed the cons.
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 moving average calculation does not weight each observation by the time elapsed since the previous one, so it is an observation-weighted average rather than a time-weighted one.
Impact
Users could trade assets at an incorrect price if the heartbeat is not called regularly, but the protocol argues that a long duration and many observations make manipulation impractical.
What the attacker needed
Heartbeat not called regularly; attacker can time transactions
The fix
The code should track the timestamps of each observation and include the time elapsed since the last beat in the average calculation, and an internal non-reverting beat should be called during user swaps.
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 price (TWAP) is supposed to give more weight to prices that last longer. Here, the code just averages the last few prices equally, ignoring how much time passed between them. This means someone could manipulate the average by choosing when to update it, potentially buying or selling at a wrong price.
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.