Inaccurate Perp debt calculation
The PerpDepository.getDebtValue function calculates the account debt value without including the owed realized PnL, which is part of the Perp protocol's definition of account value. This makes the debt calculation inaccurate and could lead to incorrect decisions when used by other contracts or off-chain.
peanuts Follow with hl_, berndartmueller and 1 other
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-01-uxd-judging/issues/346
Found by
peanuts, hl_, berndartmueller, HollaDieWaldfee
Summary
The anticipated Perp account debt value calculation via PerpDepository.getDebtValue is inaccurate and does not incorporate the (not yet settled) owed realized PnL owedRealizedPnl.
Vulnerability Detail
The PerpDepository.getDebtValue function calculates the account debt value by subtracting the pending funding payments and fees from the quote token balance and unrealized PnL. However, the owed realized PnL (owedRealizedPnl) is not considered in the calculation. The owed realized PnL is the realized PnL owed to the account but has not yet been settled.
Perp provides the Vault.getSettlementTokenValue() function to calculate the settlement token value of an account and uses it to determine the accounts' debt (if < 0, see docs). For example, it is used to determine if an account is liquidable - see Vault.isLiquidatable#L434
Perps' specs define the value of an account as (see here for reference):
$$ \begin{aligned} accountValue &= \underbrace{collateral + owedRealizedPnl + pendingFundingPayment + pendingFee}{totalCollateralValue} + \underbrace{\sum{market}{unrealizedPnl_{market}}}_{totalUnrealizedPnl} \end{aligned} $$
Impact
The Perp account debt calculation is inaccurate and deviates from the calculation by the Perp protocol itself. Even though the PerpDepository.getDebtValue function is external, it could lead to issues when querying from another contract or off-chain to use as decision criteria or manifest as a serious issue when used in an upgraded version of the contract.
Code Snippet
integrations/perp/PerpDepository.sol#L773
/// @notice Get the quote token balance of this user
/// @dev THe total debt is computed as:
/// quote token balance + unrealized PnL - Pending fee - pending funding payments
/// @param account The account to return the debt for
/// @return debt The account debt, or zero if no debt.
function getDebtValue(address account) external view returns (uint256) {
IAccountBalance perpAccountBalance = IAccountBalance(
clearingHouse.getAccountBalance()
);
IExchange perpExchange = IExchange(clearingHouse.getExchange());
int256 accountQuoteTokenBalance = vault.getBalance(account);
if (accountQuoteTokenBalance < 0) {
revert InvalidQuoteTokenBalance(accountQuoteTokenBalance);
}
int256 fundingPayment = perpExchange.getAllPendingFundingPayment(
account
);
uint256 quoteTokenBalance = uint256(accountQuoteTokenBalance)
.fromDecimalToDecimal(ERC20(quoteToken).decimals(), 18);
(
, // @audit-info `owedRealizedPnl` is omitted here and missing in the calculation below
int256 perpUnrealizedPnl,
uint256 perpPendingFee
) = perpAccountBalance.getPnlAndPendingFee(account);
int256 debt = int256(quoteTokenBalance) +
perpUnrealizedPnl -
int256(perpPendingFee) -
fundingPayment;
return (debt > 0) ? 0 : _abs(debt);
}
Vault._getSettlementTokenValue
Vault._getSettlementTokenValue is called internally by Perp's public Vault.getSettlementTokenValue() function.
function _getSettlementTokenValue(address trader) internal view returns (int256 settlementTokenValueX10_18) {
(int256 settlementBalanceX10_18, int256 unrealizedPnlX10_18) =
_getSettlementTokenBalanceAndUnrealizedPnl(trader);
return settlementBalanceX10_18.add(unrealizedPnlX10_18);
}
Vault._getSettlementTokenBalanceAndUnrealizedPnl
/// @notice Get the specified trader's settlement token balance, including pending fee, funding payment,
/// owed realized PnL, but without unrealized PnL)
/// @dev Note the difference between the return argument`settlementTokenBalanceX10_18` and
/// the return value of `getSettlementTokenValue()`.
/// The first one is settlement token balance with pending fee, funding payment, owed realized PnL;
/// The second one is the first one plus unrealized PnL.
/// @return settlementTokenBalanceX10_18 Settlement amount in 18 decimals
/// @return unrealizedPnlX10_18 Unrealized PnL in 18 decimals
function _getSettlementTokenBalanceAndUnrealizedPnl(address trader)
internal
view
returns (int256 settlementTokenBalanceX10_18, int256 unrealizedPnlX10_18)
{
int256 fundingPaymentX10_18 = IExchange(_exchange).getAllPendingFundingPayment(trader);
int256 owedRealizedPnlX10_18;
uint256 pendingFeeX10_18;
(owedRealizedPnlX10_18, unrealizedPnlX10_18, pendingFeeX10_18) = IAccountBalance(_accountBalance)
.getPnlAndPendingFee(trader);
settlementTokenBalanceX10_18 = getBalance(trader).parseSettlementToken(_decimals).add(
pendingFeeX10_18.toInt256().sub(fundingPaymentX10_18).add(owedRealizedPnlX10_18) // @audit-info owed realized PnL is added here
);
return (settlementTokenBalanceX10_18, unrealizedPnlX10_18);
}
Tool used
Manual Review
Recommendation
Consider using the Vault.getSettlementTokenValue() function to determine the accounts' debt (see docs).
Discussion
hrishibhat
Fix: https://github.com/UXDProtocol/uxd-evm/pull/28
IAm0x52
Fix looks good. Contract now uses Vault.getSettlementTokenValue() which will return the total debt as long as return value is negative. Positive values are assumed to mean there is no debt, which lines up with Perp docs
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 getDebtValue function omits the owedRealizedPnl from the debt calculation, unlike the Perp protocol's own formula.
Impact
The inaccurate debt calculation could lead to issues when other contracts or off-chain systems use it as a decision criterion, or manifest as a serious issue in an upgraded version of the contract.
What the attacker needed
None
The fix
The fix was to use Vault.getSettlementTokenValue() to determine the account's debt.
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 report is about a smart contract function that calculates how much debt an account has. The function forgets to include a component called 'owed realized PnL', which is profit or loss that has been earned but not yet paid out. Because of this, the calculated debt can be wrong, which might cause problems if other programs rely on it.
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.