Bond operations will always revert at certain time when putOptionsRequired is true
When putOptionsRequired is true, bond operations revert for about 14 minutes before each funding epoch because the option pricing library truncates time-to-expiry to zero days, causing a division by zero in the Black-Scholes calculation. An attacker could not exploit this directly, but it causes a denial of service for legitimate bond users during that window.
Submitted by said
https://github.com/code-423n4/2023-08-dopex/blob/main/contracts/core/RdpxV2Core.sol#L481-L483
when putOptionsRequired is true, there is period of time where bond operations will always revert when try to purchase options from perp atlantic vault.
Proof of Concept
When bond is called and putOptionsRequired is true, it will call _purchaseOptions providing the calculated rdpxRequired.
https://github.com/code-423n4/2023-08-dopex/blob/main/contracts/core/RdpxV2Core.sol#L920-L922
function bond(
uint256 _amount,
uint256 rdpxBondId,
address _to
) public returns (uint256 receiptTokenAmount) {
_whenNotPaused();
// Validate amount
_validate(_amount > 0, 4);
// Compute the bond cost
(uint256 rdpxRequired, uint256 wethRequired) = calculateBondCost(
_amount,
rdpxBondId
);
IERC20WithBurn(weth).safeTransferFrom(
msg.sender,
address(this),
wethRequired
);
// update weth reserve
reserveAsset[reservesIndex["WETH"]].tokenBalance += wethRequired;
// purchase options
uint256 premium;
if (putOptionsRequired) {
>>> premium = _purchaseOptions(rdpxRequired);
}
_transfer(rdpxRequired, wethRequired - premium, _amount, rdpxBondId);
// Stake the ETH in the ReceiptToken contract
receiptTokenAmount = _stake(_to, _amount);
// reLP
if (isReLPActive) IReLP(addresses.reLPContract).reLP(_amount);
emit LogBond(rdpxRequired, wethRequired, receiptTokenAmount);
}
Inside _purchaseOptions, it will call PerpetualAtlanticVault.purchase providing the amount and address(this) as receiver :
https://github.com/code-423n4/2023-08-dopex/blob/main/contracts/core/RdpxV2Core.sol#L471-L487
function _purchaseOptions(
uint256 _amount
) internal returns (uint256 premium) {
/**
* Purchase options and store ERC721 option id
* Note that the amount of options purchased is the amount of rDPX received
* from the user to sufficiently collateralize the underlying DpxEth stored in the bond
**/
uint256 optionId;
>>> (premium, optionId) = IPerpetualAtlanticVault(
addresses.perpetualAtlanticVault
).purchase(_amount, address(this));
optionsOwned[optionId] = true;
reserveAsset[reservesIndex["WETH"]].tokenBalance -= premium;
}
Then inside purchase, it will calculate premium using calculatePremium function, providing strike, amount, timeToExpiry.
function purchase(
uint256 amount,
address to
)
external
nonReentrant
onlyRole(RDPXV2CORE_ROLE)
returns (uint256 premium, uint256 tokenId)
{
_whenNotPaused();
_validate(amount > 0, 2);
updateFunding();
uint256 currentPrice = getUnderlyingPrice(); // price of underlying wrt collateralToken
uint256 strike = roundUp(currentPrice - (currentPrice / 4)); // 25% below the current price
IPerpetualAtlanticVaultLP perpetualAtlanticVaultLp = IPerpetualAtlanticVaultLP(
addresses.perpetualAtlanticVaultLP
);
// Check if vault has enough collateral to write the options
uint256 requiredCollateral = (amount * strike) / 1e8;
_validate(
requiredCollateral <= perpetualAtlanticVaultLp.totalAvailableCollateral(),
3
);
uint256 timeToExpiry = nextFundingPaymentTimestamp() - block.timestamp;
// Get total premium for all options being purchased
>>> premium = calculatePremium(strike, amount, timeToExpiry, 0);
// ... rest of operations
}
Inside this calculatePremium, it will get price from option pricing :
function calculatePremium(
uint256 _strike,
uint256 _amount,
uint256 timeToExpiry,
uint256 _price
) public view returns (uint256 premium) {
premium = ((IOptionPricing(addresses.optionPricing).getOptionPrice(
_strike,
_price > 0 ? _price : getUnderlyingPrice(),
getVolatility(_strike),
timeToExpiry
) * _amount) / 1e8);
}
The provided OptionPricingSimple.getOptionPrice will use Black-Scholes model to calculate the price :
https://github.com/code-423n4/2023-08-dopex/blob/main/contracts/libraries/BlackScholes.sol#L33-L89
function calculate(
uint8 optionType,
uint256 price,
uint256 strike,
uint256 timeToExpiry,
uint256 riskFreeRate,
uint256 volatility
) internal pure returns (uint256) {
bytes16 S = ABDKMathQuad.fromUInt(price);
bytes16 X = ABDKMathQuad.fromUInt(strike);
bytes16 T = ABDKMathQuad.div(
ABDKMathQuad.fromUInt(timeToExpiry),
ABDKMathQuad.fromUInt(36500) // 365 * 10 ^ DAYS_PRECISION
);
bytes16 r = ABDKMathQuad.div(
ABDKMathQuad.fromUInt(riskFreeRate),
ABDKMathQuad.fromUInt(10000)
);
bytes16 v = ABDKMathQuad.div(
ABDKMathQuad.fromUInt(volatility),
ABDKMathQuad.fromUInt(100)
);
bytes16 d1 = ABDKMathQuad.div(
ABDKMathQuad.add(
ABDKMathQuad.ln(ABDKMathQuad.div(S, X)),
ABDKMathQuad.mul(
ABDKMathQuad.add(
r,
ABDKMathQuad.mul(v, ABDKMathQuad.div(v, ABDKMathQuad.fromUInt(2)))
),
T
)
),
ABDKMathQuad.mul(v, ABDKMathQuad.sqrt(T))
);
bytes16 d2 = ABDKMathQuad.sub(
d1,
ABDKMathQuad.mul(v, ABDKMathQuad.sqrt(T))
);
if (optionType == OPTION_TYPE_CALL) {
return
ABDKMathQuad.toUInt(
ABDKMathQuad.mul(
_calculateCallTimeDecay(S, d1, X, r, T, d2),
ABDKMathQuad.fromUInt(DIVISOR)
)
);
} else if (optionType == OPTION_TYPE_PUT) {
return
ABDKMathQuad.toUInt(
ABDKMathQuad.mul(
_calculatePutTimeDecay(X, r, T, d2, S, d1),
ABDKMathQuad.fromUInt(DIVISOR)
)
);
} else return 0;
}
The problem lies inside calculatePremium due to not properly check if current time less than 864 seconds (around 14 minutes). because getOptionPrice will use time expiry in days multiply by 100.
uint256 timeToExpiry = (expiry * 100) / 86400;
If nextFundingPaymentTimestamp() - block.timestamp is less than 864 seconds, it will cause timeToExpiry inside option pricing is 0 and the call will always revert. This will cause an unexpected revert period around 14 minutes every funding epoch (in this case every week).
Foundry PoC :
Try to simulate calculatePremium when nextFundingPaymentTimestamp() - block.timestamp is less than 864 seconds.
Add this test to Unit contract inside /tests/rdpxV2-core/Unit.t.sol, also add import "forge-std/console.sol"; and import {OptionPricingSimple} from "contracts/libraries/OptionPricingSimple.sol"; in the contract :
function testOptionPricingRevert() public {
OptionPricingSimple optionPricingSimple;
optionPricingSimple = new OptionPricingSimple(100, 5e6);
(uint256 rdpxRequired, uint256 wethRequired) = rdpxV2Core
.calculateBondCost(1 * 1e18, 0);
uint256 currentPrice = vault.getUnderlyingPrice(); // price of underlying wrt collateralToken
uint256 strike = vault.roundUp(currentPrice - (currentPrice / 4)); // 25% below the current price
// around 14 minutes before next funding payment
vm.warp(block.timestamp + 7 days - 863 seconds);
uint256 timeToExpiry = vault.nextFundingPaymentTimestamp() -
block.timestamp;
console.log("What is the current price");
console.log(currentPrice);
console.log("What is the strike");
console.log(strike);
console.log("What is time to expiry");
console.log(timeToExpiry);
uint256 price = vault.getUnderlyingPrice();
// will revert
vm.expectRevert();
optionPricingSimple.getOptionPrice(strike, price, 100, timeToExpiry);
}
Recommended Mitigation Steps
Set minimum timeToExpiry inside calculatePremium :
function calculatePremium(
uint256 _strike,
uint256 _amount,
uint256 timeToExpiry,
uint256 _price
) public view returns (uint256 premium) {
premium = ((IOptionPricing(addresses.optionPricing).getOptionPrice(
_strike,
_price > 0 ? _price : getUnderlyingPrice(),
getVolatility(_strike),
- timeToExpiry
+ timeToExpiry < 864 ? 864 : timeToExpiry
) * _amount) / 1e8);
}
psytama (Dopex) confirmed and commented:
Modify time to expiry with a check for less than 864 seconds and make it more robust.
Alex the Entreprenerd (Judge) commented:
The DOS is not conditional on an external requirement because it consistently happens, I’ll ask judges, but am maintaining High Severity at this time.
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 option pricing library converts time-to-expiry to whole days by integer division, so any remaining time under 864 seconds becomes zero and causes a division by zero in the Black-Scholes formula.
Impact
Bond operations consistently revert for about 14 minutes before each weekly funding epoch, causing a denial of service for legitimate users; the judge noted the DOS is not conditional on an external requirement.
What the attacker needed
PutOptionsRequired is true; transaction occurs within 864 seconds of next funding
The fix
The report recommends setting a minimum timeToExpiry of 864 seconds inside calculatePremium, and the project confirmed and planned to make the check more robust.
How it was found
- Set putOptionsRequired to true.
- Call bond, which calls _purchaseOptions and then PerpetualAtlanticVault.purchase.
- purchase calls calculatePremium with timeToExpiry equal to nextFundingPaymentTimestamp() minus block.timestamp.
- When that timeToExpiry is less than 864 seconds, OptionPricingSimple.getOptionPrice computes timeToExpiry as zero days.
- The Black-Scholes calculation divides by zero and the transaction reverts.
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 smart contract calculates the cost of an option using a formula that divides by the time left until the option expires. If that time is less than one day, the code rounds it down to zero, and dividing by zero makes the transaction fail. So for a short window each week, users cannot create bonds because the price calculation always crashes.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.