Internal `OptionMath._getPositivePlaceValues()` function do not handle values below `185`
An internal maths helper in the option pricing library reverts when given a value below 185, because a later division by a ruler that has become zero causes a division-by-zero error. This could stop auctions from starting, since the auction setup path calls this helper through the pricer's rounding functions.
shung Follow with ArbitraryExecution
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2022-09-knox-judging/issues/43
Found by
shung, ArbitraryExecution
Summary
Internal OptionMath._getPositivePlaceValues() function do not handle values below 185.
Vulnerability Detail
OptionMath._getPositivePlaceValues() is a library function used by special floor and ceiling functions which are in turn used in the calculation of the strike price. However, _getPositivePlaceValues() function incorrectly reverts when the provided value is below 185. This happens because a 64x64 185 equals to a wad value with one digit, which is represented internally with one extra decimal. Since the division by 100 will return zero for a number with less than three digits, the division in the following line reverts due to division by zero.
Impact
This might prevent auctions from starting due to the following execution flow VaultAdmin.initializeAuction() -> VaultInternal._setOptionParameters() -> Pricer.snapToGrid64x64() -> OptionMath.ceil64x64() -> OptionMath._getPositivePlaceValues().
There can also be more serious issues if this library is reused in other places.
Code Snippet
Tool used
Manual Review
Recommendation
Applying the following diff will properly handle division by zero, such that values below 185 can still be rounded up or down.
diff --git a/knox-contracts/contracts/libraries/OptionMath.sol b/knox-contracts/contracts/libraries/OptionMath.sol
index 746dd78..8ed2bbc 100644
--- a/knox-contracts/contracts/libraries/OptionMath.sol
+++ b/knox-contracts/contracts/libraries/OptionMath.sol
@@ -92,15 +92,21 @@ library OptionMath {
// setup the first place value
values[0].ruler = ruler;
- values[0].value = (integer / values[0].ruler) % 10;
-
- // setup the second place value
- values[1].ruler = ruler / 10;
- values[1].value = (integer / values[1].ruler) % 10;
-
- // setup the third place value
- values[2].ruler = ruler / 100;
- values[2].value = (integer / values[2].ruler) % 10;
+ if (values[0].ruler != 0) {
+ values[0].value = (integer / values[0].ruler) % 10;
+
+ // setup the second place value
+ values[1].ruler = ruler / 10;
+ if (values[1].ruler != 0) {
+ values[1].value = (integer / values[1].ruler) % 10;
+
+ // setup the third place value
+ values[2].ruler = ruler / 100;
+ if (values[2].ruler != 0) {
+ values[2].value = (integer / values[2].ruler) % 10;
+ }
+ }
+ }
return (integer, values);
}
Discussion
0xCourtney
https://github.com/KnoxFinance/knox-contracts/pull/81
rcstanciu
Reply from @arbitrary-CodeBeholder
looks good
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 helper divides by place-value rulers without checking they are non-zero, so small inputs produce a zero ruler and a division-by-zero revert.
Impact
This might prevent auctions from starting, and there could be more serious issues if the library is reused elsewhere.
What the attacker needed
A value below 185 reaches the helper
The fix
The recommendation adds non-zero checks around each place-value division so values below 185 can still be rounded up or down.
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 use fixed-point maths, where numbers are stored scaled up by a large factor to keep decimals. A helper that splits a number into digit place values divides by a 'ruler' value, and for small inputs that ruler becomes zero, so the contract crashes instead of rounding. Because the auction setup calls this helper, a small input could block an auction from starting.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.