All funds can be stolen from FixedStrikeOptionTeller using a token with malicious decimals
The FixedStrikeOptionTeller contract calculates quote token amounts using the payout token's decimals fetched at call time, allowing an attacker to deploy a malicious payout token whose decimals value can be changed between calls. By first setting a large decimals value to create option tokens for zero quote tokens, then setting a small decimals value to exercise or reclaim for a huge quote token payout, the attacker can drain all ERC20 tokens held by the contract.
Juntao Follow with berndartmueller, bin2chen and 1 other
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-06-bond-judging/issues/90
Found by
Juntao, berndartmueller, bin2chen, ctf_sec
Summary
FixedStrikeOptionTeller is a single contract which deploys multiple option tokens. Hence this single contract holds significant payout/quote tokens as collateral. Also the deploy, create & exercise functions of this contract can be called by anyone.
This mechanism can be exploited to drain FixedStrikeOptionTeller of all tokens.
Vulnerability Detail
This is how the create functions looks like:
function create(
FixedStrikeOptionToken optionToken_,
uint256 amount_
) external override nonReentrant {
...
if (call) {
...
} else {
uint256 quoteAmount = amount_.mulDiv(strikePrice, 10 ** payoutToken.decimals());
...
quoteToken.safeTransferFrom(msg.sender, address(this), quoteAmount);
...
}
optionToken.mint(msg.sender, amount_);
}
exercise function:
function exercise(
FixedStrikeOptionToken optionToken_,
uint256 amount_
) external override nonReentrant {
...
uint256 quoteAmount = amount_.mulDiv(strikePrice, 10 ** payoutToken.decimals());
if (msg.sender != receiver) {
...
}
optionToken.burn(msg.sender, amount_);
if (call) {
...
} else {
quoteToken.safeTransfer(msg.sender, quoteAmount);
}
}
Consider this attack scenario:
Let's suppose the FixedStrikeOptionTeller holds some DAI tokens.
-
An attacker can create a malicious payout token of which he can control the
decimals. -
The attacker calls
deployto create an option token with malicious payout token and DAI as quote token andputoption type -
Make
payoutToken.decimalsreturn a large number and callFixedStrikeOptionTeller.createwith input X. HerequoteAmountwill be calculated as0.
// Calculate amount of quote tokens required to mint
uint256 quoteAmount = amount_.mulDiv(strikePrice, 10 ** payoutToken.decimals());
// Transfer quote tokens from user
// Check that amount received is not less than amount expected
// Handles edge cases like fee-on-transfer tokens (which are not supported)
uint256 startBalance = quoteToken.balanceOf(address(this));
quoteToken.safeTransferFrom(msg.sender, address(this), quoteAmount);
So 0 DAI will be pulled from the attacker's account but he will receive X option token.
- Make
payoutToken.decimalsreturn a small value and callFixedStrikeOptionTeller.exercisewith X input. HerequoteAmountwill be calculated as a very high number (which represents number of DAI tokens). So he will receive huge amount of DAI against his X option tokens when exercise the option or when reclaim the token
// Transfer remaining collateral to receiver
uint256 amount = optionToken.totalSupply();
if (call) {
payoutToken.safeTransfer(receiver, amount);
} else {
// Calculate amount of quote tokens equivalent to amount at strike price
uint256 quoteAmount = amount.mulDiv(strikePrice, 10 ** payoutToken.decimals());
quoteToken.safeTransfer(receiver, quoteAmount);
}
Hence, the attacker was able to drain all DAI tokens from the FixedStrikeOptionTeller contract. The same mechanism can be repeated to drain all other ERC20 tokens from the FixedStrikeOptionTeller contract by changing the return value of the decimal external call
Impact
Anyone can drain FixedStrikeOptionTeller contract of all ERC20 tokens. The cost of attack is negligible (only gas cost).
High impact, high likelyhood.
Code Snippet
Tool used
Manual Review
Recommendation
Consider storing the payoutToken.decimals value locally instead of fetching it real-time on all exercise or reclaim calls.
or support payout token and quote token whitelist, if the payout token and quote token are permissionless created, there will always be high risk
Discussion
ctf-sec
https://github.com/sherlock-audit/2023-06-bond-judging/issues/8 is the duplicate of this issue
Oighty
Agree with this issue. The simplest solution seems to be stored the decimal values used when the option token is deployed.
Oighty
Fix implemented at https://github.com/Bond-Protocol/options/pull/3
ctf-sec
This is a big one and a important one, will look into the fix
ctf-sec
The fix looks good, the decimals call is properly cached, I highly recommend adding a test to make sure it is working as intended
Oighty
I added some tests to confirm the cached decimal behavior on create and exercise to the PR.
ctf-sec
Thanks for adding the test cases! All 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
The contract reads the payout token's decimals dynamically on every create, exercise, and reclaim call instead of storing it at deployment, so a malicious token can return different values to manipulate the calculated quote amount.
Impact
Anyone can drain all ERC20 tokens from the FixedStrikeOptionTeller contract at negligible cost, as the attack only requires gas.
What the attacker needed
Ability to deploy a malicious ERC20 token; contract holds ERC20 tokens as collateral; no whitelist on payout tokens
The fix
The fix was implemented by caching the decimals value at deployment and using the stored value in create and exercise, with tests added to confirm the behaviour.
How it was found
- Deploy a malicious payout token whose decimals value can be controlled by the attacker.
- Call deploy to create an option token using the malicious payout token and DAI as quote token, with put option type.
- Set the malicious token's decimals to a large number and call create with amount X, so quoteAmount calculates to zero and no DAI is transferred.
- Set the malicious token's decimals to a small number and call exercise with amount X, so quoteAmount calculates to a huge number of DAI.
- Receive the large DAI payout, draining the contract's DAI balance.
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.
This contract lets anyone create and trade options. It uses a number called 'decimals' from a token to work out how much of another token you must pay or receive. An attacker made their own token and kept changing that number, so they paid nothing to get options and then received a huge payout, emptying the contract.
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.