code-423n4 / 2024-03-dittoeth-findings

0 stars 0 forks source link

Unhandled edge case in bid matching algorithm #249

Closed c4-bot-10 closed 4 months ago

c4-bot-10 commented 5 months ago

Lines of code

https://github.com/code-423n4/2024-03-dittoeth/blob/91faf46078bb6fe8ce9f55bcb717e5d2d302d22e/contracts/facets/BidOrdersFacet.sol#L169-L179

Vulnerability details

Impact

The identified issue in the bid matching algorithm could have several significant impacts:

Proof of Concept

The issue was identified through a manual review of the bid matching algorithm code in the provided GitHub repository. Specifically, the relevant code snippet where the concern was raised is as follows:

 function bidMatchAlgo(
        address asset,
        STypes.Order memory incomingBid,
        MTypes.OrderHint[] memory orderHintArray,
        MTypes.BidMatchAlgo memory b
    ) private returns (uint88 ethFilled, uint88 ercAmountLeft) {
            // more code

         if (incomingBid.ercAmount == lowestSell.ercAmount) {
            // @audit-issue Oversight, if we don't update lowestSell.ercAmount = 0 here, it means that it will remain unchanged, leading to issues

             if (lowestSell.isShort()) {
                  b.matchedShortId = lowestSell.id;
                  b.prevShortId = lowestSell.prevId;
                  LibOrders.matchOrder(s.shorts, asset, lowestSell.id);
               } else {
                   b.matchedAskId = lowestSell.id;
                    LibOrders.matchOrder(s.asks, asset, lowestSell.id);
                      }
               }

As the audit-issue comment suggests, lowestSell.ercAmount should be set to 0 after a bid matches exactly with the sell amount. However, there is no such update in the code, leaving lowestSell.ercAmount unchanged on the order book. This could lead to the same sell order being executed for all bids that match it, allowing malicious actors to exploit the system for financial gain.

Tools Used

VSCode

Recommended Mitigation Steps

To address this issue, it is recommended to update the lowestSell.ercAmount to 0 after an exact match occurs. This ensures that the order book accurately reflects the filled orders and prevents the risk of double execution.

Assessed type

Other

c4-pre-sort commented 5 months ago

raymondfam marked the issue as insufficient quality report

raymondfam commented 5 months ago

Inadequate elaboration and proof given.

c4-pre-sort commented 5 months ago

raymondfam marked the issue as primary issue

c4-judge commented 4 months ago

hansfriese marked the issue as unsatisfactory: Insufficient proof