Discounted fee calculation is imprecise and calculates less fees than anticipated
The fee calculation in the Juicebox smart contract used an incorrect formula that divided by the sum of the discounted fee and the maximum fee, leading to a lower fee being charged than intended. An attacker could exploit this to pay less than the required fee when making payments through the protocol.
Originally disclosed on Code4rena
Submitted by berndartmueller, also found by 0x52, hyh, and Ruhum
The JBPayoutRedemptionPaymentTerminal._feeAmount function is used to calculate the fee based on a given _amount, a fee rate _fee and an optional discount _feeDiscount.
However, the current implementation calculates the fee in a way that leads to inaccuracy and to fewer fees being paid than anticipated by the protocol.
Proof of Concept
JBPayoutRedemptionPaymentTerminal._feeAmount
function _feeAmount(
uint256 _amount,
uint256 _fee,
uint256 _feeDiscount
) internal pure returns (uint256) {
// Calculate the discounted fee.
uint256 _discountedFee = _fee -
PRBMath.mulDiv(_fee, _feeDiscount, JBConstants.MAX_FEE_DISCOUNT);
// The amount of tokens from the `_amount` to pay as a fee.
return
_amount - PRBMath.mulDiv(_amount, JBConstants.MAX_FEE, _discountedFee + JBConstants.MAX_FEE);
}
Example:
Given the following (don’t mind the floating point arithmetic, this is only for simplicity. The issues still applies with integer arithmetic and higher decimal precision):
amount- 1000fee- 5 (5%)feeDiscount- 10 (10%)MAX_FEE_DISCOUNT- 100MAX_FEE- 100
$discountedFee = fee - {{fee \ast feeDiscount} \over MAX_FEE_DISCOUNT}$ $discountedFee = 5 - {{5 \ast 10} \over 100}$ $discountedFee = 4.5$
Calculating the fee amount based on the discounted fee of $4.5$:
$fee_{Amount} = amount - {{amount \ast MAX_FEE} \over {discountedFee + MAX_FEE}}$ $fee_{Amount} = 1000 - {{1000 \ast 100} \over {4.5 + 100}}$ $fee_{Amount} = 1000 - 956.93779904$ $fee_{Amount} = 43.06220096$
The calculated and wrong fee amount is ~43, instead, it should be 45. The issue comes from dividing by _discountedFee + JBConstants.MAX_FEE.
Now the correct way:
I omitted the discountedFee calculation as this formula is correct.
$fee_{Amount} = {{amount \ast discountedFee} \over {MAX_FEE}}$ $fee_{Amount} = {{1000 \ast 4.5} \over {100}}$ $fee_{Amount} = 45$
Recommended Mitigation Steps
Fix the discounted fee calculation by adjusting the formula to:
$fee_{Amount} = amount \ast {fee - fee \ast {discount \over MAX_{FEE_DISCOUNT}} \over MAX_{FEE}}$
In Solidity:
function _feeAmount(
uint256 _amount,
uint256 _fee,
uint256 _feeDiscount
) internal pure returns (uint256) {
// Calculate the discounted fee.
uint256 _discountedFee = _fee -
PRBMath.mulDiv(_fee, _feeDiscount, JBConstants.MAX_FEE_DISCOUNT);
// The amount of tokens from the `_amount` to pay as a fee.
return PRBMath.mulDiv(_amount, _discountedFee, JBConstants.MAX_FEE);
}
mejango (Juicebox) acknowledged
jack-the-pug (judge) commented:
Great job! One of the best write-ups I have ever seen, simple and clean.
Here is a trophy for you: 🏆
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 fee amount was calculated by subtracting from the amount the result of multiplying the amount by MAX_FEE and dividing by (discountedFee + MAX_FEE), instead of simply multiplying the amount by discountedFee and dividing by MAX_FEE.
What the attacker needed
None
The fix
The fee calculation should be fixed by using the formula: feeAmount = amount * discountedFee / MAX_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.
This is a smart contract bug where the code that calculates a fee uses the wrong maths. Instead of multiplying the amount by the fee rate and dividing by 100, it divides by the fee rate plus 100, which makes the fee smaller. So users pay less than they should, and the protocol loses money.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.