Underflow risk in swapDsforRa and swapRaforDsin DsFlashSwap.sol :getAmountOutSellDs
Summary
The getAmountOutSellDS function in the DsFlashSwaphttps://github.com/sherlock-audit/2024-08-cork-protocol/blob/main/Depeg-swap/contracts/libraries/DsFlashSwap.sol#L170 library has a potential underflow vulnerability. This occurs when the repaymentAmount exceeds the amount parameter, leading to a panic error when attempting to compute amountOut. This situation can arise during the calculation of repayment amounts based on the reserves, particularly when the reserves are imbalanced.
Root Cause
Conditions for repaymentAmount > amount
Insufficient Reserves: If the reserves of raReserve and ctReserve are imbalanced, particularly if ctReserve is significantly lower than amount, the calculation may yield a high repaymentAmount.
High Amount Input: If the amount (the CT amount being sold) is large relative to the available reserves, it can lead to a situation where the required repayment amount exceeds the input amount.
Market Conditions: If the market conditions (e.g., price slippage, liquidity) are such that the amount of RA that needs to be borrowed to cover the CT being sold is disproportionately high, this can also lead to a higher repaymentAmount.
Internal pre-conditions
No response
External pre-conditions
No response
Attack Path
No response
Impact
No response
PoC
No response
Mitigation
To mitigate this risk, the function should include a check to ensure that repaymentAmount does not exceed amount before performing the subtraction. If the check fails, the function should revert with an appropriate error message. Here’s a suggested modification:
This modification ensures that the function will revert if an underflow condition is detected, thus preventing potential exploitation and maintaining the integrity of the contract.
Melodic Vermilion Coyote
Low/Info
Underflow risk in
swapDsforRa
andswapRaforDs
inDsFlashSwap.sol :getAmountOutSellDs
Summary
The
getAmountOutSellDS
function in theDsFlashSwap
https://github.com/sherlock-audit/2024-08-cork-protocol/blob/main/Depeg-swap/contracts/libraries/DsFlashSwap.sol#L170 library has a potential underflow vulnerability. This occurs when the repaymentAmount exceeds the amount parameter, leading to a panic error when attempting to computeamountOut
. This situation can arise during the calculation of repayment amounts based on the reserves, particularly when the reserves are imbalanced.Root Cause
Conditions for repaymentAmount > amount
raReserve
andctReserve
are imbalanced, particularly ifctReserve
is significantly lower than amount, the calculation may yield a high repaymentAmount.Internal pre-conditions
No response
External pre-conditions
No response
Attack Path
No response
Impact
No response
PoC
No response
Mitigation
To mitigate this risk, the function should include a check to ensure that
repaymentAmount
does not exceedamount
before performing the subtraction. If the check fails, the function should revert with an appropriate error message. Here’s a suggested modification:This modification ensures that the function will revert if an underflow condition is detected, thus preventing potential exploitation and maintaining the integrity of the contract.