The `_fee()` function is wrongly implemented in the code
The fee calculation function incorrectly computes the unit fee, causing the protocol to receive less in fees than intended and traders to pay less. An attacker could exploit this to reduce their trading costs at the protocol's expense.
Source: https://github.com/sherlock-audit/2022-11-buffer-judging/issues/95
Found by
Ch_301
Summary
_fee() function is wrongly implemented in the code so the protocol will get fewer fees and the trader will earn more
Vulnerability Detail
(uint256 unitFee, , ) = _fees(10**decimals(), settlementFeePercentage);
amount = (newFee * 10**decimals()) / unitFee;
let's say we have:
newFee 100 USDC
USDC Decimals is 6
settlementFeePercentage is 20% ==> 200
The unitFee will be 520_000
amount = (100 * 1_000_000) / 520_000
amount = 192 USDC
Which is supposed to be amount = 160 USDC
Impact
The protocol will earn fees less than expected
Code Snippet
function checkParams(OptionParams calldata optionParams)
external
view
override
returns (
uint256 amount,
uint256 revisedFee,
bool isReferralValid
)
{
require(
assetCategory != AssetCategory.Forex ||
isInCreationWindow(optionParams.period),
"O30"
);
uint256 maxAmount = getMaxUtilization();
// Calculate the max fee due to the max txn limit
uint256 maxPerTxnFee = ((pool.availableBalance() *
config.optionFeePerTxnLimitPercent()) / 100e2);
uint256 newFee = min(optionParams.totalFee, maxPerTxnFee);
// Calculate the amount here from the new fees
uint256 settlementFeePercentage;
(
settlementFeePercentage,
isReferralValid
) = _getSettlementFeePercentage(
referral.codeOwner(optionParams.referralCode),
optionParams.user,
_getbaseSettlementFeePercentage(optionParams.isAbove),
optionParams.traderNFTId
);
(uint256 unitFee, , ) = _fees(10**decimals(), settlementFeePercentage);
amount = (newFee * 10**decimals()) / unitFee;
function _fees(uint256 amount, uint256 settlementFeePercentage)
internal
pure
returns (
uint256 total,
uint256 settlementFee,
uint256 premium
)
{
// Probability for ATM options will always be 0.5 due to which we can skip using BSM
premium = amount / 2;
settlementFee = (amount * settlementFeePercentage) / 1e4;
total = settlementFee + premium;
}
Tool used
Manual Review
Recommendation
The _fee() function needs to calculate the fees in this way
total_fee = (5000 * amount)/ (10000 - sf)
Discussion
0x00052
Fixed in PR#22
Changes look good. New math returns correct values. Validated for both unit fee and option amount.
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 fee calculation incorrectly divides by the total fee (premium plus settlement fee) instead of using the correct formula that accounts for the settlement fee percentage.
What the attacker needed
None
The fix
The fee calculation was fixed in a pull request to use the correct formula.
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 is a smart contract bug where the code that calculates fees uses a wrong formula. Because of that, when someone trades, they are charged too little, so the platform loses money. It's like a shop charging you £1.60 instead of £1.92 because the till was programmed wrong.
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.