openssl: Infinite loop in BIO_printf

There is a deadlock in the BIO_print and similar functions:

This can be used to demonstrate the impact:

--- a/apps/openssl.c
+++ b/apps/openssl.c
@@ -250,6 +250,18 @@ int main(int argc, char *argv[])
     bio_in = dup_bio_in(FORMAT_TEXT);
     bio_out = dup_bio_out(FORMAT_TEXT);
     bio_err = dup_bio_err(FORMAT_TEXT);
+    fprintf(stdout, "INF=%f\n", 1.0/0.0);
+    fprintf(stdout, "-INF=%f\n", -1.0/0.0);
+    fprintf(stdout, "NAN=%f\n", 0.0/0.0);
+    fprintf(stdout, "-1f=%f\n", -1.0);
+    fprintf(stdout, "-1e=%e\n", -1.0);
+    fprintf(stdout, "-1g=%g\n", -1.0);
+    BIO_printf(bio_out, "INF=%f\n", 1.0/0.0);
+    BIO_printf(bio_out, "-INF=%f\n", -1.0/0.0);
+    BIO_printf(bio_out, "NAN=%f\n", 0.0/0.0);
+    BIO_printf(bio_out, "-1f=%f\n", -1.0);
+    BIO_printf(bio_out, "-1e=%e\n", -1.0);
+    BIO_printf(bio_out, "-1g=%g\n", -1.0);
 
 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
     argv = copy_argv(&argc, argv);

with openssl 1.1.1 to 3.5 I get

$ apps/openssl version
INF=inf
-INF=-inf
NAN=-nan
-1f=-1.000000
-1e=-1.000000e+00
-1g=-1
NAN=9223372036854775809.775808
-1f=-1.000000

and now the program hangs in an endless loop, so the BIO_printf does not output anything for +/-INF and NAN is somehow simply wrong. But the %e and %g format is stuck in an endless loop for any negative value, or +infinity by crypto/bio/bio_print.c:

            while (tmpvalue < 1) {
                tmpvalue *= 10;
                exp--;
            }
            while (tmpvalue > 10) {
                tmpvalue /= 10;
                exp++;
            }

since tmpvalue is negative, thus always < 1. likewise for +infinity which stays always > 10.

For 1.0.2 I see no deadlock here, because probably the %e and %g format is not implemented and thus ignored:

$ apps/openssl version
INF=inf
-INF=-inf
NAN=-nan
-1f=-1.000000
-1e=-1.000000e+00
-1g=-1
INF=8ffeeaf0eb9cdaac909.aac90a
-INF=-8ffeeaf0eb9cdaac909.aac90a
NAN=8ffeeaf0eb9cdaac909.aac909
-1f=-1.000000
-1e=
-1g=

but the output of +/-INF and NAN invokes an out of bounds array access in crypto/bio/b_print.c:

    intpart = (long)ufvalue;

    /* convert integer part */
    do {
        iconvert[iplace++] = "0123456789"[intpart % 10];
        intpart = (intpart / 10);
    } while (intpart && (iplace < (int)sizeof(iconvert)));

because intpart is -LONG_MAX for +/-INF or NAN, thus negative. Newer openssl versions use unsigned long here, but the value is of course still bogus.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 17 (17 by maintainers)

Commits related to this issue

Most upvoted comments

But, if we keep it, it seems like the bug itself is that we don’t recognize ±INF and NaN. Would it be a sufficient fix if we did, instead of trying to print them as numbers?

No, it’s worse than that. The user’s code hangs when trying to print a negative number using the %g format specifier.

How much is this still needed in modern environments? I imagine it may be hard to completely get rid of it on some very old targets, but perhaps you all could start relying on the libc’s snprintf on most platforms. That would limit the blast radius of bugs like this.