]> Savannah Git Hosting - gnulib.git/commitdiff
af_alg: avoid hangs when reading from streams
authorPádraig Brady <P@draigBrady.com>
Sun, 24 Jun 2018 08:46:10 +0000 (01:46 -0700)
committerPádraig Brady <P@draigBrady.com>
Sun, 24 Jun 2018 20:53:45 +0000 (13:53 -0700)
* lib/af_alg.c (afalg_stream): Don't assume EOF is sticky,
and thus avoid doing a fread() when feof() is set.
* lib/md5.c: Ensure feof() is called before fread().
* lib/sha1.c: Likewise.
* lib/sha256.c: Likewise.
* lib/sha512.c: Likewise.

ChangeLog
lib/af_alg.c
lib/md5.c
lib/sha1.c
lib/sha256.c
lib/sha512.c

index 093ec41fb5a533cb01955b2af44c2511b3c5b2fb..fbf20a9bd20059f6ee45119aff536d27baac022f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
        * tests/test-wcwidth.c (main): If the wchar-single module is present,
        skip the tests in the C locale.
 
+2018-06-24  Pádraig Brady  <P@draigBrady.com>
+
+       af_alg: avoid hangs when reading from streams
+       * lib/af_alg.c (afalg_stream): Don't assume EOF is sticky,
+       and thus avoid doing a fread() when feof() is set.
+       * lib/md5.c: Ensure feof() is called before fread().
+       * lib/sha1.c: Likewise.
+       * lib/sha256.c: Likewise.
+       * lib/sha512.c: Likewise.
+
 2018-06-24  Pádraig Brady  <P@draigBrady.com>
 
        af_alg: fix error handling when hash not returned
index f362caba8b3f164bb5f2d913330feaa8054eb81b..9630d03912914640f1dbec30593b84be24452a5f 100644 (file)
@@ -120,9 +120,7 @@ afalg_stream (FILE *stream, const char *alg,
     }
   else
     {
-     /* sendfile not possible, do a classic read-write loop.
-        Defer to glibc as to whether EOF is sticky; see
-        <https://sourceware.org/bugzilla/show_bug.cgi?id=19476>.  */
+     /* sendfile not possible, do a classic read-write loop.  */
       for (;;)
         {
           char buf[BLOCKSIZE];
@@ -141,6 +139,14 @@ afalg_stream (FILE *stream, const char *alg,
                         ? -EAFNOSUPPORT : -EIO);
               break;
             }
+
+          /* Assume EOF is not sticky. See:
+             <https://sourceware.org/bugzilla/show_bug.cgi?id=19476>.  */
+          if (feof (stream))
+            {
+              result = 0;
+              break;
+            }
         }
     }
 
index a9b65b019168fcbc23f37a12c0a79d8cc8994229..ea69a59d8eea590d54df7a9a0cec117bc88d99a7 100644 (file)
--- a/lib/md5.c
+++ b/lib/md5.c
@@ -170,6 +170,12 @@ md5_stream (FILE *stream, void *resblock)
       /* Read block.  Take care for partial reads.  */
       while (1)
         {
+          /* Either process a partial fread() from this loop,
+             or the fread() in afalg_stream may have gotten EOF.
+             We need to avoid a subsequent fread() due to glibc BZ 1190.  */
+          if (feof (stream))
+            goto process_partial_block;
+
           n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
 
           sum += n;
@@ -189,12 +195,6 @@ md5_stream (FILE *stream, void *resblock)
                 }
               goto process_partial_block;
             }
-
-          /* We've read at least one byte, so ignore errors.  But always
-             check for EOF, since feof may be true even though N > 0.
-             Otherwise, we could end up calling fread after EOF.  */
-          if (feof (stream))
-            goto process_partial_block;
         }
 
       /* Process buffer with BLOCKSIZE bytes.  Note that
index 6cfd6c6f37ae98b2df2d5fc9dd67dc98f1647780..1295d051cf1277a07176609a96eee6fa33e7e141 100644 (file)
@@ -158,6 +158,12 @@ sha1_stream (FILE *stream, void *resblock)
       /* Read block.  Take care for partial reads.  */
       while (1)
         {
+          /* Either process a partial fread() from this loop,
+             or the fread() in afalg_stream may have gotten EOF.
+             We need to avoid a subsequent fread() due to glibc BZ 1190.  */
+          if (feof (stream))
+            goto process_partial_block;
+
           n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
 
           sum += n;
@@ -177,12 +183,6 @@ sha1_stream (FILE *stream, void *resblock)
                 }
               goto process_partial_block;
             }
-
-          /* We've read at least one byte, so ignore errors.  But always
-             check for EOF, since feof may be true even though N > 0.
-             Otherwise, we could end up calling fread after EOF.  */
-          if (feof (stream))
-            goto process_partial_block;
         }
 
       /* Process buffer with BLOCKSIZE bytes.  Note that
index d45cd184cbc38a5378921b94d6c299f8b5757d41..bdea1bda60b46cd3a51022e5d68c142b9111e1cf 100644 (file)
@@ -208,6 +208,12 @@ shaxxx_stream (FILE *stream, char const *alg, void *resblock,
       /* Read block.  Take care for partial reads.  */
       while (1)
         {
+          /* Either process a partial fread() from this loop,
+             or the fread() in afalg_stream may have gotten EOF.
+             We need to avoid a subsequent fread() due to glibc BZ 1190.  */
+          if (feof (stream))
+            goto process_partial_block;
+
           n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
 
           sum += n;
@@ -227,12 +233,6 @@ shaxxx_stream (FILE *stream, char const *alg, void *resblock,
                 }
               goto process_partial_block;
             }
-
-          /* We've read at least one byte, so ignore errors.  But always
-             check for EOF, since feof may be true even though N > 0.
-             Otherwise, we could end up calling fread after EOF.  */
-          if (feof (stream))
-            goto process_partial_block;
         }
 
       /* Process buffer with BLOCKSIZE bytes.  Note that
index 503a54fe8ae417e2539f4c2330cfcd8da4fac666..863d6ba7ac6d75a3a4032a91654e92e124d29972 100644 (file)
@@ -209,6 +209,12 @@ shaxxx_stream (FILE *stream, char const *alg, void *resblock,
       /* Read block.  Take care for partial reads.  */
       while (1)
         {
+          /* Either process a partial fread() from this loop,
+             or the fread() in afalg_stream may have gotten EOF.
+             We need to avoid a subsequent fread() due to glibc BZ 1190.  */
+          if (feof (stream))
+            goto process_partial_block;
+
           n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
 
           sum += n;
@@ -228,12 +234,6 @@ shaxxx_stream (FILE *stream, char const *alg, void *resblock,
                 }
               goto process_partial_block;
             }
-
-          /* We've read at least one byte, so ignore errors.  But always
-             check for EOF, since feof may be true even though N > 0.
-             Otherwise, we could end up calling fread after EOF.  */
-          if (feof (stream))
-            goto process_partial_block;
         }
 
       /* Process buffer with BLOCKSIZE bytes.  Note that