Skip to content
Business logic Audit finding Intermediate

Incorrect calculation to check remaining ratio after reward in StableConfig.sol

The contract's check for the remaining collateral ratio after liquidation rewards used a subtraction instead of a percentage calculation, so the actual buffer could fall below the intended 105% of the borrowed amount. This could leave the position less protected against market volatility than the protocol intended.

t0x1c Follow with 0xpiken, klau5 and 1 other

Originally disclosed on Code4rena

Submitted by t0x1c, also found by 0xpiken, klau5, and haxatron

https://github.com/code-423n4/2024-01-salty/blob/main/src/stable/StableConfig.sol#L51-L54

https://github.com/code-423n4/2024-01-salty/blob/main/src/stable/StableConfig.sol#L127-L130

changeMinimumCollateralRatioPercent() allows the owner to change the minimumCollateralRatioPercent while the changeRewardPercentForCallingLiquidation() function allows to change the rewardPercentForCallingLiquidation. The protocol aims to maintain a remaining ratio of 105% as is evident by the comments in these 2 functions:

// Don't decrease the minimumCollateralRatioPercent if the remainingRatio after the rewards would be less than 105% - to ensure that the position will be liquidatable for more than the originally borrowed USDS amount (assume reasonable market volatility)

and

// Don't increase rewardPercentForCallingLiquidation if the remainingRatio after the rewards would be less than 105% - to ensure that the position will be liquidatable for more than the originally borrowed USDS amount (assume reasonable market volatility)

That is, if borrow amount is 100, after the calls to any of these two functions, there should be at least 105 collateral remaining which will act as buffer against market volatility. The current calculation however is incorrect and there is really no direct relationship (in the way the developer assumes) between the rewardPercentForCallingLiquidation and minimumCollateralRatioPercent. Consider this:

  • minimumCollateralRatioPercent of 110% means that for a borrow amount of 200, collateral should not go below 220. This is 110% of 200.
  • However, rewardPercentForCallingLiquidation of 5% is calculated on the collateral amount and NOT the borrowed amount as is evident in the comments too here and here. So the liquidator will receive 5% of 220 (let’s assume boundary values for rounded calculations) which is 11.
  • The remaining amount would be 220 - 11 = 209 which is 104.5% of the borrowed amount. This is less than the 105% the protocol was aiming for. In fact, this figure of 104.5% goes down further to 103.5% when rewardPercentForCallingLiquidation = 5% and minimumCollateralRatioPercent = 115% which is much lower than protocol’s buffer target. Not being aware of this risk can cause unexpected loss of funds.

A table outlining the real buffer upper limit values is provided below. Another table showing the actual desirable gap in values is also provided so that the buffer always is above 105%.

Straightaway, it can be seen that the current default protocol values of 5% and 110% give a buffer of less than 105% and hence either the minimumCollateralRatioPercent needs to have a lower limit of 111 instead of 110, or there should be agreement to the fact that 103.5% is an acceptable remainingRatio figure under the current scheme of things.

Proof of Concept

All figures expressed as % of borrowed amount i.e. 104.5 means 100 was borrowed.

Current Implementation:

rewardPercentForCallingLiquidation | minimumCollateralRatioPercent | Actual price fluctuation upper limit (instead of the expected 105%) |

5 | 110 | 104.5 |

6 | 111 | 104.34 |

7 | 112 | 104.16 |

8 | 113 | 103.96 |

9 | 114 | 103.74 |

10 | 115 | 103.5 |

Desired figures for maintaining an upper limit of >= 105% of borrowed amount:

rewardPercentForCallingLiquidation | Desired minimumCollateralRatioPercent | Resultant price fluctuation upper limit |

5 | 111 | 105.45 |

6 | 112 | 105.28 |

7 | 113 | 105.09 |

8 | 115 | 105.8 |

9 | 116 | 105.56 |

10 | 117 | 105.3 |

Recommended Mitigation Steps

Assuming that the protocol wants to calculate an actual 105% remainingRatio, changes along these lines need to be made. Please note that you may have to additionally make sure rounding errors & precision loss do not creep in. These suggestions point towards a general direction: Update the two functions:

	function changeRewardPercentForCallingLiquidation(bool increase) external onlyOwner
        {
        if (increase)
            {
			// Don't increase rewardPercentForCallingLiquidation if the remainingRatio after the rewards would be less than 105% - to ensure that the position will be liquidatable for more than the originally borrowed USDS amount (assume reasonable market volatility)
+           uint256 afterIncrease = rewardPercentForCallingLiquidation + 1;
+           uint256 remainingRatio = minimumCollateralRatioPercent - minimumCollateralRatioPercent * afterIncrease / 100;
-			uint256 remainingRatioAfterReward = minimumCollateralRatioPercent - rewardPercentForCallingLiquidation - 1;

-           if (remainingRatioAfterReward >= 105 && rewardPercentForCallingLiquidation < 10)
+           if (remainingRatio >= 105 && rewardPercentForCallingLiquidation < 10)
                rewardPercentForCallingLiquidation += 1;
            }
        else
            {
            if (rewardPercentForCallingLiquidation > 5)
                rewardPercentForCallingLiquidation -= 1;
            }

		emit RewardPercentForCallingLiquidationChanged(rewardPercentForCallingLiquidation);
        }

and

	function changeMinimumCollateralRatioPercent(bool increase) external onlyOwner
        {
        if (increase)
            {
            if (minimumCollateralRatioPercent < 120)
                minimumCollateralRatioPercent += 1;
            }
        else
            {
			// Don't decrease the minimumCollateralRatioPercent if the remainingRatio after the rewards would be less than 105% - to ensure that the position will be liquidatable for more than the originally borrowed USDS amount (assume reasonable market volatility)
+           uint256 afterDecrease = minimumCollateralRatioPercent - 1;
+           uint256 remainingRatio = afterDecrease - afterDecrease * rewardPercentForCallingLiquidation / 100;
-			uint256 remainingRatioAfterReward = minimumCollateralRatioPercent - 1 - rewardPercentForCallingLiquidation;

-           if (remainingRatioAfterReward >= 105 && minimumCollateralRatioPercent > 110)
+           if (remainingRatio >= 105 && minimumCollateralRatioPercent > 111)
                minimumCollateralRatioPercent -= 1;
            }

		emit MinimumCollateralRatioPercentChanged(minimumCollateralRatioPercent);
        }

Also L39:

- 39:     uint256 public minimumCollateralRatioPercent = 110;
+ 39:     uint256 public minimumCollateralRatioPercent = 111;

Picodes (Judge) commented:

This report shows how an invariant of the protocol is broken so Medium severity seems appropriate.

othernet-global (Salty.IO) acknowledged and commented:

The stablecoin framework: /stablecoin, /price_feed, WBTC/WETH collateral, PriceAggregator, price feeds and USDS have been removed:

https://github.com/othernet-global/salty-io/commit/88b7fd1f3f5e037a155424a85275efd79f3e9bf9

Status: Mitigation confirmed. Full details in reports from zzebra83, 0xpiken, and t0x1c.


The original report as published on Code4rena, by t0x1c. 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

Likely the code subtracted the reward percentage directly from the collateral ratio percentage instead of computing the reward as a percentage of the collateral amount, so the remaining ratio was overestimated.

Impact

The actual remaining collateral ratio after a liquidation reward could be as low as 103.5% instead of the intended 105%, reducing the buffer against market volatility and potentially causing unexpected loss of funds.

What the attacker needed

Owner calls config functions; none for attacker

The fix

The calculation should compute the reward as a percentage of the collateral ratio (e.g., minimumCollateralRatioPercent * rewardPercent / 100) and the default minimum collateral ratio should be raised to 111; the protocol later removed the stablecoin framework entirely.

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.

A stablecoin lets people borrow money by putting up more valuable collateral. If the collateral's value drops, a liquidator can step in, sell some collateral, and take a reward. The protocol wanted to make sure that even after paying that reward, enough collateral remains to cover the loan plus a safety margin. This report shows the check used the wrong maths, so the safety margin could be smaller than planned.

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 by t0x1c

View all

19 more disclosed reports, best first.

More from Salty.IO

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.