PerpDepository#_placePerpOrder miscalculates fees paid when shorting
The fee calculation in PerpDepository's _placePerpOrder applied the exchange fee percentage to the quote amount returned after a short, but that amount had already had fees deducted, so the recorded fee was too low. This made the totalFeesPaid counter inaccurate, which could affect other contracts that rely on it.
berndartmueller Follow with cccz, keccak123 and 5 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-01-uxd-judging/issues/271
Found by
berndartmueller, cccz, keccak123, peanuts, Jeiwan, rvierdiiev, 0x52, GimelSec
Summary
PerpDepository#_placePerpOrder calculates the fee as a percentage of the quoteToken received. The issue is that this amount already has the fees taken so the fee percentage is being applied incorrectly.
Vulnerability Detail
function _placePerpOrder(
uint256 amount,
bool isShort,
bool amountIsInput,
uint160 sqrtPriceLimit
) private returns (uint256, uint256) {
uint256 upperBound = 0; // 0 = no limit, limit set by sqrtPriceLimit
IClearingHouse.OpenPositionParams memory params = IClearingHouse
.OpenPositionParams({
baseToken: market,
isBaseToQuote: isShort, // true for short
isExactInput: amountIsInput, // we specify exact input amount
amount: amount, // collateral amount - fees
oppositeAmountBound: upperBound, // output upper bound
// solhint-disable-next-line not-rely-on-time
deadline: block.timestamp,
sqrtPriceLimitX96: sqrtPriceLimit, // max slippage
referralCode: 0x0
});
(uint256 baseAmount, uint256 quoteAmount) = clearingHouse.openPosition(
params
);
uint256 feeAmount = _calculatePerpOrderFeeAmount(quoteAmount);
totalFeesPaid += feeAmount;
emit PositionOpened(isShort, amount, amountIsInput, sqrtPriceLimit);
return (baseAmount, quoteAmount);
}
function _calculatePerpOrderFeeAmount(uint256 amount)
internal
view
returns (uint256)
{
return amount.mulWadUp(getExchangeFeeWad());
}
When calculating fees, PerpDepository#_placePerpOrder use the quote amount retuned when opening the new position. It always uses exactIn which means that for shorts the amount of baseAsset being sold is specified. The result is that quote amount returned is already less the fees. If we look at how the fee is calculated we can see that it is incorrect.
Example: Imagine the market price of ETH is $1000 and there is a market fee of 1%. The 1 ETH is sold and the contract receives 990 USD. Using the math above it would calculated the fee as $99 (990 * 1%) but actually the fee is $100.
It have submitted this as a medium because it is not clear from the given contracts what the fee totals are used for and I cannot fully assess the implications of the fee value being incorrect.
Impact
totalFeesPaid will be inaccurate which could lead to disparities in other contracts depending on how it is used
Code Snippet
Tool used
Manual Review
Recommendation
Rewrite _calculatePerpOrderFeeAmount to correctly calculate the fees paid:
- function _calculatePerpOrderFeeAmount(uint256 amount)
+ function _calculatePerpOrderFeeAmount(uint256 amount, bool isShort)
internal
view
returns (uint256)
{
+ if (isShort) {
+ return amount.divWadDown(WAD - getExchangeFeeWad()) - amount;
+ } else {
return amount.mulWadUp(getExchangeFeeWad());
+ }
}
Discussion
hrishibhat
Fix: https://github.com/UXDProtocol/uxd-evm/pull/31
IAm0x52
Fix looks good. _placePerpOrder now uses openPositionFor which returns the fee paid directly
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 was calculated as a percentage of the post-fee quote amount instead of the pre-fee amount, so for shorts the fee was understated.
Impact
totalFeesPaid will be inaccurate, which could lead to disparities in other contracts depending on how it is used; the reporter could not fully assess the implications.
What the attacker needed
Shorting through the perp depository
The fix
The fee calculation was fixed so that _placePerpOrder uses openPositionFor, which returns the fee paid directly.
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 sell something on an exchange, the exchange keeps a small cut called a fee. This code tried to work out that cut by taking a percentage of the money received, but the money received had already had the cut removed, so the number came out too small. The contract's running total of fees paid was therefore 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.