DeFiFoundry
60,000 USDC
View results
Submission Details
Severity: high
Invalid

Incorrect Margin Validation in `validateMarginRequirement()` Function

Summary

validateMarginRequirement() validates if the given account will still meet margin requirements after a given operation. This function is intended to revert if the required margin, including fees, is greater than or equal to the account's margin balance.

/// @dev Reverts if the new account margin state is invalid (requiredMargin >= marginBalance).

However, the current implementation only reverts if the required margin is strictly greater than the margin balance.

Vulnerability Details

TradingAccount::validateMarginRequirement() function does not correctly handle the case where the required margin is exactly equal to margin balance.

>> if (requiredMarginUsdX18.add(totalFeesUsdX18).intoSD59x18().gt(marginBalanceUsdX18)) {
revert Errors.InsufficientMargin(
self.id,
marginBalanceUsdX18.intoInt256(),
requiredMarginUsdX18.intoUint256(),
totalFeesUsdX18.intoUint256()
);
}

As seen, the function uses the gt (greater than) operator instead of the gte (greater than or equal to) operator.

Impact

The function does not revert when the required margin is exactly equal to the margin balance, potentially allowing operations that should not be permitted. This could lead to scenarios where accounts are allowed to operate with insufficient margin, increasing the risk of liquidation and hence financial loss.

Tools Used

Manual Review

Recommendations

The condition should be changed to gte to ensure it reverts when requiredMargin is equal to or greater than marginBalance.

- if (requiredMarginUsdX18.add(totalFeesUsdX18).intoSD59x18().gt(marginBalanceUsdX18)) {
+ if (requiredMarginUsdX18.add(totalFeesUsdX18).intoSD59x18().gte(marginBalanceUsdX18)) {
revert Errors.InsufficientMargin(
self.id,
marginBalanceUsdX18.intoInt256(),
requiredMarginUsdX18.intoUint256(),
totalFeesUsdX18.intoUint256()
);
}
Updates

Lead Judging Commences

inallhonesty Lead Judge
12 months ago
inallhonesty Lead Judge 11 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.