Funds can be stolen from the `FixedStrikeOptionTeller` contract by creating put option tokens without providing collateral
A rounding error in the create function of the FixedStrikeOptionTeller contract allows an attacker to mint put option tokens without providing the required quote token collateral. The attacker can then call reclaim after expiry to drain collateral supplied by other users.
berndartmueller Follow with pks_ and techOptimizor
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-06-bond-judging/issues/61
Found by
berndartmueller, pks_, techOptimizor
Summary
Due to a rounding error when calculating the quoteAmount in the create function of the FixedStrikeOptionTeller contract, it is possible to create (issue) option tokens without providing the necessary collateral. A malicious receiver can exploit this to steal funds from the FixedStrikeOptionTeller contract.
Vulnerability Detail
Anyone can create (issue) put option tokens with the create function in the FixedStrikeOptionTeller contract. However, by specifying a very small amount_, the quoteAmount calculation in line 283 can potentially round down to zero. This is the case if the result of the multiplication in line 283, $amount * strikePrice$ is smaller than $10^{decimals}$, where decimals is the number of decimals of the payout token.
For example, assume the following scenario:
| Parameter | Description |
|---|---|
| Quote token | $USDC. 6 decimals |
| Payout token | $GMX. 18 decimals |
| $payoutToken_{decimals}$ | 18 decimals |
| $amount$ | 1e10. Amount (amount_) supplied to the create function, in payout token decimal precision. |
| $strikePrice$ | 50e6 ~ 50 USD. The strike price of the option token, in quote tokens. |
Please note that the option token has the same amount of decimals as the payout token.
Calculating quoteAmount leads to the following result:
$$ \begin{align} quoteAmount &= \dfrac{amount * strikePrice}{10^{payoutToken_{decimals}}} \ &= \dfrac{1e10 * 50e6}{10^{18}} \ &= \dfrac{5e17}{10^{18}} \ &= \dfrac{5 * 10^{17}}{10^{18}} \ &= 0 \end{align} $$
As observed, the result is rounded down to zero due to the numerator being smaller than the denominator.
This results in 0 quote tokens to be transferred from the caller to the contract, and in return, the caller receives 1e10 ($amount$) option tokens. This process can be repeated to mint an arbitrary amount of option tokens for free.
Impact
Put options can be minted for free without providing the required quoteToken collateral.
This is intensified by the fact that a malicious receiver, which anyone can be, can exploit this issue by deploying a new option token (optionally with a very short expiry), repeatedly minting free put options to accumulate option tokens, and then, once the option expires, call reclaim to receive quote token collateral.
This collateral, however, was supplied by other users who issued (created) option tokens with the same quote token. Thus, the malicious receiver can drain funds from other users and cause undercollateralization of those affected option tokens.
Code Snippet
src/fixed-strike/FixedStrikeOptionTeller.sol#L283
236: function create(
237: FixedStrikeOptionToken optionToken_,
238: uint256 amount_
239: ) external override nonReentrant {
... // [...]
268:
269: // Transfer in collateral
270: // If call option, transfer in payout tokens equivalent to the amount of option tokens being issued
271: // If put option, transfer in quote tokens equivalent to the amount of option tokens being issued * strike price
272: if (call) {
... // [...]
281: } else {
282: // Calculate amount of quote tokens required to mint
283: @> uint256 quoteAmount = amount_.mulDiv(strikePrice, 10 ** payoutToken.decimals()); // @audit-issue Rounds down
284:
285: // Transfer quote tokens from user
286: // Check that amount received is not less than amount expected
287: // Handles edge cases like fee-on-transfer tokens (which are not supported)
288: uint256 startBalance = quoteToken.balanceOf(address(this));
289: quoteToken.safeTransferFrom(msg.sender, address(this), quoteAmount);
290: uint256 endBalance = quoteToken.balanceOf(address(this));
291: if (endBalance < startBalance + quoteAmount)
292: revert Teller_UnsupportedToken(address(quoteToken));
293: }
294:
295: // Mint new option tokens to sender
296: optionToken.mint(msg.sender, amount_);
297: }
Tool used
Manual Review
Recommendation
Consider adding a check after line 283 to ensure quoteAmount is not zero.
Discussion
Oighty
Agree with this issue. Checking for a zero quote amount is a potential solution, but it would be better to fix the precision issue. I'm going to explore other ways to do this (including something like the price scale system used in the other bond protocol contracts).
ctf-sec
I agree, if only checking the amount > 0, the precision loss can still be large and round down to 1 wei
scaling amount with decimals seems the way to go!
ctf-sec
In report #62
the auditor is correct the user can game the code to not pay the quote amount and receive the payout token just like the receiver
but there is no loss of fund, because when minting option, the payout token collateral has to be supplied.
In this report
it is true the user can game the code to not supply the collateral and get the option token and then call reclaim the drain the fund.
the impact is clearly more severe
but the reclaim issue has been extensively covered in report:
https://github.com/sherlock-audit/2023-06-bond-judging/issues/79
the report takes the advantage of the rounding
in fact in my report:
https://github.com/sherlock-audit/2023-06-bond-judging/issues/90
I highlight the decimals is large and collateral is round down to 0 then call reclaim to drain the fund as well
After internal discussion, Agree with the de-duplication and leave as a separate medium
0x3b33
I mean the issue is valid in a way that there needs to be a check for quote of 0, but how is anyone gonna profit from it? The attacker's profit is only 1e10, compared to GMX's decimals 18, he is making nothing! If my calculations are not wrong, the attacker will make 0.00000055$ per call... When I press the button to turn my PC on I spend more money, let alone to run a TX on any network.
ctf-sec
I mean the issue is valid in a way that there needs to be a check for quote of 0, but how is anyone gonna profit from it? The attacker's profit is only 1e10, compared to GMX's decimals 18, he is making nothing! If my calculations are not wrong, the attacker will make 0.00000055$ per call... When I press the button to turn my PC on I spend more money, let alone to run a TX on any network.
To profits, the attacker would need to repeated call reclaim (which is another issue)
The decimal rounding plays a role as well (which is another issue)
Even the fact that can mint token without providing collateral make it a valid issue.
Oighty
Fix implemented at https://github.com/Bond-Protocol/options/pull/11
ctf-sec
Fix looks good
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 integer division in the quoteAmount calculation rounds down to zero when the numerator is smaller than the denominator, and there is no check to prevent a zero quote amount.
Impact
Put options can be minted for free without providing the required quoteToken collateral, and a malicious receiver can drain funds from other users and cause undercollateralization of those affected option tokens.
What the attacker needed
None
The fix
The issue was fixed by adding a check to ensure quoteAmount is not zero, and the fix was implemented in a pull request.
How it was found
- Deploy a new option token with the same quote token as other users, optionally with a very short expiry.
- Call the create function with a very small amount_ so that quoteAmount rounds down to zero.
- Repeat the minting to accumulate option tokens without paying collateral.
- After the option expires, call reclaim to receive quote token collateral from other users.
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.
When you create a put option, the contract calculates how much collateral you must pay. Because of how numbers are rounded down in the code, a very small amount can make that required payment become zero. An attacker can then create many free options and later take other people's collateral.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.