Open c4-bot-6 opened 3 months ago
hansfriese marked the issue as satisfactory
hansfriese marked the issue as primary issue
good find, will fix this issue in decreaseCollateral() instead of cancelShort()
Hi @hansfriese , left a comment in https://github.com/code-423n4/2024-07-dittoeth-findings/issues/8#issuecomment-2232415388
hansfriese marked the issue as selected for report
Lines of code
https://github.com/code-423n4/2024-07-dittoeth/blob/ca3c5bf8e13d0df6a2c1f8a9c66ad95bbad35bce/contracts/facets/ShortRecordFacet.sol#L83-L107
Vulnerability details
Summary
An attacker can use
decreaseCollateral()
function to reduce the collateral of a Short Recordn (SR) andcancelOrder()
to cancel the corresponding Short Order having a collateral ratio < 1. The Short Record ends up having less collateral than debt and is now liquidatable.Description
A user can create a Short Order with a collateral ratio (CR) of less than 1. The protocol ensures that at least
minShortErc
in the Short Orders Short Record has enough collateral by filling the Short Record with collateral to coverminShortErc
before the Short Order is matched or placed on the order book.In addition, during the match the bid also provides equivalent collateral to the
ercDebt
it creates. So if the Short Order CR is 0.7 it ends up being 1.7 CR after matching.By utilising the
cancelShort()
anddecreaseCollateral()
functions on a short Order with CR < 1, an attacker can create free DUSD and make the Short Record liquidatable.The cancelShort() function lets the caller cancel a Short Order. If the
ercDebt
is smaller thanminShortErc
as shown in line 953 below, it mints the difference to the shorter (i.e the address that created the Short Order) in line 982. The amount of collateral needed is calculated in line 960 and usescr
.cr
is the collateral ratio and if it is less than 1 then the protocol would be minting DUSD with lesser collateral. But since it has already ensured thatminShortErc
has enough collateral in the Short Record before the Short Order was created, this shouldn't be a problem.LibOrders.sol#L946-L976
The decreaseCollateral() function lets the caller reduce the collateral he has in the Short Record.
An attacker can use
decreaseCollateral()
to reduce the collateral of the Short Record before cancelling. Thus, removing the collateral added forminShortErc
and the protocol ends up minting DUSD with less Ethereum collateral.The attacker earns the DUSD minted while providing less collateral value. The Short Record will become liquidatable and the attacker can further liquidate it to earn liquidation rewards.
Impact
The protocol mints DUSD for free and pays liquidation rewards when the bad debt position is liquidated.
Proof of Concept
To execute the POC the Short Record has to have debt so that
decreaseCollateral()
andcancelShort()
can be called on it successfully. To do this, the POC creates two Short Orders, a normal Short Order and the other with CR < 1. It then uses a bid order to fill the normal Short Order and a tiny amount of the order with CR < 1.The test can be run in the Shorts.t.sol file.
Tools Used
Manual Analysis
Recommended Mitigation Steps
Consider adding a check to
cancelOrder()
to ensure the resulting SR Collateral Ratio is not below the redemption and liquidation collateral ratios.Assessed type
Other