Incorrect referral fee calculations
The referral fee calculation in the marketplace contract subtracted a small percentage from the total buyer fee instead of taking a percentage of it, so the referral payout came out far larger than intended. An attacker could name themselves as referrer and receive up to roughly 90% of the buyer fee total rather than the expected 10%.
zanderbyte Follow with 0xlucky, 0xNirix and 47 others
Originally disclosed on Code4rena
Submitted by zanderbyte, also found by 0xlucky, 0xNirix, 0XRolko, 0xStalin, Abhan, Agontuk, aster, BajagaSec, BenRai, c0pp3rscr3w3r, ChainSentry, DanielArmstrong, DharkArtz, Drynooo, elvin-a-block, EPSec, franfran20, Gaurav2811, Gaurav2811, Gosho, inh3l, JustUzair, JustUzair, KupiaSec, Lamsy, macart224, macart224, newspacexyz, nslavchev, ogKapten, oualidpro, oualidpro, Rampage, Rhaydden, rspadi, Ryonen, Sabit, sl1, smbv-1923, TheFabled, TheKhans, Uddercover, udo, udo, wickie0x, xKeywordx, y0ng0p3, YouCrossTheLineAlfie, and zia_d_k
When a purchase of listed tokens is performed, the caller can provide a referral address. This address should receive a referral fee as a reward for introducing users to the project. According to the dev team, the referral payment will be done off-chain.
However, in the current implementation, the referralFeeCost calculations are incorrect, which results in much higher fees (ou to 90% of buyersFeeTotal) for the referral than expected.
Proof of Concept
Assume the default protocol values for buyerFee = 250 and referralFee = 1000. Let’s say a user wants to purchase tokens worth 1000 USDT. The buyer fee will be charged on the baseAmount, and for simplicity, we can assume that the _amount is not affected by any discount.
The buyerFeeTotal is calculated as:
buyerFeeTotal = (1000e6 * 250) / 10000 = 25e6
The referrallFeeCost is calculated as:
referralFeeCost =
buyerFeeTotal -
(baseAmount * bfee * IMarketplaceSetting(marketplaceSetting).referralFee()) /
(BASE * BASE);
Substituting the values:
referralFeeCost = 25e6 - (1000e6 * 250 * 1000) / (10000 * 10000)
= 25e6 - 2.5e6
= 22.5e6
This resulted in a referral fee of 22.5e6 which is much higher than expected.
Recommended mitigation steps
The referralFeeCost should be calculated as a percentage of buyerFeeTotal.
The correct calculation should be:
referralFeeCost =
- buyerFeeTotal -
- (baseAmount * bfee * IMarketplaceSetting(marketplaceSetting).referralFee()) /
- (BASE * BASE);
+ buyerFeeTotal * IMarketplaceSetting(marketplaceSetting).referralFee() / BASE
This will result in referralFee = 2.5e6 (exactly 10% of buyerFeeTotal)
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 referral fee was computed by subtracting a percentage-scaled term from the total buyer fee rather than multiplying the total buyer fee by the referral percentage.
Impact
A caller could obtain a referral payout of up to about 90% of the buyer fee total instead of the intended 10%, though the text notes referral payments are made off-chain.
What the attacker needed
Ability to purchase listed tokens; ability to supply a referral address
The fix
The referral fee should be calculated as the buyer fee total multiplied by the referral fee percentage divided by the base, which was the recommended mitigation.
How it was found
- A user purchases listed tokens and supplies a referral address.
- The contract computes the total buyer fee from the base amount.
- The contract computes the referral fee by subtracting a scaled term from the total buyer fee.
- The resulting referral fee is far larger than the intended percentage of the buyer fee.
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 marketplace charges a small fee when someone buys tokens, and part of that fee is meant to go to a referrer who introduced the buyer. The code worked out the referrer's share by subtracting a tiny amount from the whole fee, so the referrer got almost all of it instead of a small slice. Anyone who could set the referral address could take that oversized payout.
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.