Submitted by 0xTheC0der, also found by nirlin, __141345__, QiuhaoLi (1, 2), bin2chen, 0xbranded, jasonxiale, 0Kage, pep7siup, 836541, joaovwfreire, bart1e, ABA, alexfilippov314, peakbolt, ayden, 0xDING99YA, T1MOH, chaduke, SpicyMeatball, degensec, Kow, rvierdiiev, 0xHelium, and tnquanghuy0512
The return value of PerpetualAtlanticVault.nextFundingPaymentTimestamp(), which marks the beginning of the next funding epoch, scales linearly with the current fundingDuration. (Note that latestFundingPaymentPointer is strictly monotonically increasing with each epoch.)
Therefore, epochs are separated by a period of fundingDuration.
However, the owner can change the fundingDuration at any time via PerpetualAtlanticVault.updateFundingDuration() which is an intended feature of the contract, otherwise the funding duration would be immutable.
Assume the owner changes the fundingDuration by a value delta, such that: new fundingDuration = old fundingDuration + delta.
As a result, the return value of PerpetualAtlanticVault.nextFundingPaymentTimestamp() moves by a value of latestFundingPaymentPointer * delta into the future or past (depending on the sign of delta).
Numerical example:
The fundingDuration = 7 days and is slightly reduced by delta = -1 hour.
The protocol is alread live for nearly 2 years, therefore latestFundingPaymentPointer = 96.
Consequently, the next funding epoch begins latestFundingPaymentPointer * delta = -96 hours = -4 days in the past.
The PerpetualAtlanticVault.nextFundingPaymentTimestamp() is called at 14 instances throughout the PerpetualAtlanticVault contract and once by the RdpxV2Core contract for bonding cost calculation, see RdpxV2Core.calculateBondCost().
Due the scattered impacts of the present bug, there arise multiple problems. Nevertheless, this report solely focuses on the following consquences in order to emphasize the severity of this issue.
This helper method is (implicitly) called by the updateFunding(), purchase(...), settle(...) and calculateFunding(...) methods, but not by the payFunding() method of the contract.
It is responsible for iteratively bringing the latestFundingPaymentPointer into the present while funding the LP with collateral for each epoch.
In case of time travel by changing the fundingDuration it will do the following:
This method is called by the core contract when bonding in order to buy options. Thereby the option premium is heavily dependent on the return value of nextFundingPaymentTimestamp(), see L283-L286.
In case of an overly long epoch old fundingDuration + delta * latestFundingPaymentPointer due to an increase of the fundingDuration by delta, the option premium is excessively increased leading to a loss of funds for the user/protocol by using up a great part of the reserve WETH for the option, see core contract L918-L924 and PoC.
Subsequently the bonding gets more expensive, i.e. the user has to supply more WETH, see also RdpxV2Core.calculateBondCost().
For reference, the correct epoch duration in this case should be old fundingDuration + delta.
The affected entry point methods are RdpxV2Core.bond() and RdpxV2Core.bondWithDelegate().
This is also time critical in another way, since the user cannot know if the fundingDuration was changed after calculating the bonding cost via RdpxV2Core.calculateBondCost(), but before his bonding transaction.
The resulting funding period / epoch inconsistencies by jumping in time, as well as the execessivley overpriced options, which do lead to loss of funds when bought during that epoch, are sufficient to strongly disincentivize users from bonding. This consequently endagers the peg which can be considered the most valuable asset of the protocol.
Futhermore, the user cannot know if the fundingDuration was changed before his transaction is executed which poses an additional risk.
In order to prove the above claims, three new test cases were added. The last assertion in each of those cases fails in order to demonstrate the issues.
Just apply the diff below and run the test cases with forge test -vv --match-test testUpdateFundingPaymentPointer and forge test -vv --match-test testPurchaseExtendedEpoch:
Option 1 - Complete Fix:
Reconsider if it is really necessary for the protocol to change the funding duration throughout its lifecycle. If no, declare the fundingDuration as immutable.
Option 2 - Mitigation:
Store the lastFundingPaymentTimestamp and incease it by fundingDuration in updateFundingPaymentPointer() on each new epoch to accomplish the expected timing behaviour.
Note that the scaling issue does not only appear in PerpetualAtlanticVault.nextFundingPaymentTimestamp(), but also in L426-427.
Alex the Entreprenerd (Judge) decreased severity to Medium and commented:
witherblock (Dopex) confirmed via duplicate issue 980
