Skip to content
Business logic Audit finding Intermediate

Incorrect comparison logic in post-operation checks

The post-operation validation function in LeverageMacroBase has reversed comparison logic for the gte and lte operators, so checks that should enforce a minimum or maximum value instead enforce the opposite. This could allow operations to pass validation when they should fail, potentially enabling manipulation of CDP states.

Nexarion Follow with chaduke, SBSecurity and 5 others

Originally disclosed on Code4rena

Submitted by Nexarion, also found by chaduke, SBSecurity, lian886, Drynooo, stacey, jesjupyter, and Rhaydden

The _doCheckValueType function, which is crucial for post-operation validation, contains reversed comparison logic for the gte (greater than or equal to) and lte (less than or equal to) operators. This reversal causes the function to perform the opposite checks than intended, potentially allowing operations to pass validation when they should fail, or fail when they should pass.

This issue could lead to:

  1. Incorrect validation of CDP (Collateralized Debt Position) states after operations.
  2. Potential manipulation of the system by exploiting these misaligned checks.

Proof of Concept

The issue is located in the _doCheckValueType function of the LeverageMacroBase contract:

https://github.com/code-423n4/2024-06-badger/blob/9173558ee1ac8a78a7ae0a39b97b50ff0dd9e0f8/ebtc-protocol/packages/contracts/contracts/LeverageMacroBase.sol#L277-L289

Specifically, the problematic comparisons are:

} else if (check.operator == Operator.gte) {
    require(check.value >= valueToCheck, "!LeverageMacroReference: gte post check");
} else if (check.operator == Operator.lte) {
    require(check.value <= valueToCheck, "!LeverageMacroReference: let post check");
}

These comparisons are reversed and should be:

} else if (check.operator == Operator.gte) {
    require(valueToCheck >= check.value, "!LeverageMacroReference: gte post check");
} else if (check.operator == Operator.lte) {
    require(valueToCheck <= check.value, "!LeverageMacroReference: lte post check");
}

Recommended Mitigation Steps

To fix this issue:

  1. Reverse the comparisons in the _doCheckValueType function for the gte and lte operators.
  2. Update the function as follows:
function _doCheckValueType(CheckValueAndType memory check, uint256 valueToCheck) internal {
    if (check.operator == Operator.skip) {
        // Early return
        return;
    } else if (check.operator == Operator.gte) {
        require(valueToCheck >= check.value, "!LeverageMacroReference: gte post check");
    } else if (check.operator == Operator.lte) {
        require(valueToCheck <= check.value, "!LeverageMacroReference: lte post check");
    } else if (check.operator == Operator.equal) {
        require(check.value == valueToCheck, "!LeverageMacroReference: equal post check");
    } else {
        revert("Operator not found");
    }
}

Assessed type

Context

wtj2021 (Badger) acknowledged via duplicate Issue #18

0xsomeone (judge) increased severity to High

GalloDaSballo (Badger) commented:

We disagree that the finding is of High Severity and believe it’s logically equivalent to a lack of a slippage check, more commonly classified as Medium Severity.

Additionally a slippage check is implicitly present when using 0x for swaps.

The check is a important check to have but a lack of it working well doesn’t open up to meaningful losses due to other safeguards.

0xsomeone (judge) commented:

@GalloDaSballo - My initial judgment of this submission as high was based on the fact that the code performs an invalid operation and is a sensitive dependency with a high likelihood of vulnerability re-surfacing if left unpatched due to its presence in a utility library. In detail, the impact would be considered medium but the incidence itself merits a “high” rating as it is incorrectly executed every time the function is invoked.

Given that the function is present in a generic library, its impact would vary depending on the library’s usage and it is not unreasonable to assume that, if left unpatched, it would lead to a higher severity vulnerability surfacing.

I believe a high-severity rating is justified based on the above analysis, however, I am keen to hear more feedback especially if you believe that any of my statements are incorrect.

GalloDaSballo (Badger) commented:

From our perspective the check is tightly coupled with these parameters.

And was meant to be a generic way to perform checks on those values not to be re-used in other contracts.

0xsomeone (judge) decreased severity to Medium and commented:

After closely reviewing the codebase, the function does not appear to be utilized anywhere else except for the LeverageMacroBase contract itself. Based on the fact that the demonstratable vulnerability would be capped at medium severity and the prospective vulnerabilities that can arise from this code flaw are not readily apparent or guaranteed, I will proceed with downgrading this submission to medium risk.

This decision is final and no further feedback is expected in relation to this submission.


The original report as published on Code4rena, by Nexarion. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 comparison operands in the gte and lte checks were swapped, so the function compared the wrong value against the threshold.

Impact

The faulty check could allow operations to pass validation when they should fail, potentially enabling manipulation of CDP states, though the report does not demonstrate a concrete exploit.

What the attacker needed

None

The fix

The comparisons in the _doCheckValueType function should be reversed so that gte checks valueToCheck >= check.value and lte checks valueToCheck <= check.value.

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.

Smart contracts often check that a value is above or below a limit after an operation. Here, the code that performs those checks had the comparison backwards, so it could accept a value that should have been rejected. This means a safety check was not working as intended.

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.

More from eBTC Zap Router

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.