DoS in bigdecimal's sqrt function due to miscalculation of loop iterations
The BigDecimal#sqrt method in Ruby's bigdecimal extension incorrectly used a maximum iteration constant as a lower bound, allowing an attacker-controlled precision argument to cause excessive loop iterations. An attacker who can control the argument could make a Ruby program hang for a long time, requiring SIGKILL to terminate.
- Triaged in 21 hours
- Disclosed after 16 days
Vulnerability
Affected Product: bigdecimal extension in https://github.com/ruby/ruby
Affected Versions: At least version 3.2.2, I didn't test any previous versions
The current implementation of BigDecimal#sqrt in ext/bigdecimal/bigdecimal.c erroneously checks its parameter
and allows users of the function to control how long it will run. This may lead to
a DoS if the parameter to the function can be controlled by an attacker.
The implementation of BigDecimal#sqrt involves a loop that iteratively calculates
the value of the square root:
do {
y->MaxPrec *= 2;
if (y->MaxPrec > y_prec) y->MaxPrec = y_prec;
f->MaxPrec = y->MaxPrec;
VpDivd(f, r, x, y); /* f = x/y */
VpAddSub(r, f, y, -1); /* r = f - y */
VpMult(f, VpConstPt5, r); /* f = 0.5*r */
if (VpIsZero(f))
goto converge;
VpAddSub(r, f, y, 1); /* r = y + f */
VpAsgn(y, r, 1); /* y = r */
} while (++nr < n);
The number of iterations is determined by the number n, which is derived from the
parameter of the sqrt function.
The application tries to impose a limit on the number of iterations, as can be seen
in line 4659:
#define maxnr 100UL /* Maximum iterations for calculating sqrt. */
However, the calculation of n is erroneous and uses maxnr as a lower bound and not
an upper bound for n as can be seen in line 7220:
if (n < (SIGNED_VALUE)maxnr) n = (SIGNED_VALUE)maxnr;
This may cause the program to have more iterations than originally intended.
Proof of Concept
The following ruby program iterates 10000 times instead of 100 and takes longer than 10 min to complete on my machine:
require 'bigdecimal'
BigDecimal("6E19").sqrt(10000)
Furthermore, it can be observed the ruby interpreter stalls completely. The program has to be killed with SIGKILL.
Solution
The following patch resolves the error:
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index 07c2bcf0b5..31e5574574 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -7217,7 +7217,7 @@ VpSqrt(Real *y, Real *x)
y->MaxPrec = Min((size_t)n , y_prec);
f->MaxPrec = y->MaxPrec + 1;
n = (SIGNED_VALUE)(y_prec * BASE_FIG);
- if (n < (SIGNED_VALUE)maxnr) n = (SIGNED_VALUE)maxnr;
+ if (n > (SIGNED_VALUE)maxnr) n = (SIGNED_VALUE)maxnr;
/*
* Perform: y_{n+1} = (y_n - x/y_n) / 2
This change maintains the correctness of the implementation. I have checked this against the test suite from https://github.com/ruby/bigdecimal and all the tests still pass.
Impact
If an attacker can control the parameter to BigDecimal#sqrt he/she can cause a ruby program to hang
for a long time.
Furtermore, since the loop is inside of a function of an extension it blocks the interpreter / execution engine
as a whole hindering the delivery of events or signals.
As seen above, a ruby program that is caught up in such a loop can only be interrupted by a SIGKILL signal.
Since the bug is
- Easy to trigger if the necessary conditions are met
- Has a huge effect for relatively small input values
I chose the severity medium.
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 a comparison error in the C code where the iteration limit was applied as a minimum instead of a maximum, so a large precision argument increased the loop count beyond the intended cap.
Impact
If an attacker can control the parameter to BigDecimal#sqrt, they could cause a Ruby program to hang for a long time, blocking the interpreter and requiring SIGKILL to stop.
What the attacker needed
Attacker controls the sqrt argument
The fix
The comparison was changed from less-than to greater-than so that the iteration count is capped at maxnr, and the fix was verified against the test suite.
How it was found
- Call BigDecimal#sqrt with a large precision argument, such as 10000.
- Observe that the program iterates 10000 times instead of the intended 100 and takes longer than 10 minutes.
- The Ruby interpreter stalls completely and must be killed with SIGKILL.
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 square root function in a Ruby library uses a loop to calculate the result. The number of times the loop runs is supposed to be capped, but a mistake meant that a large input number could make the loop run far more times than intended. If an attacker can supply that input, the program can freeze for a very long time.
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.