Blame SOURCES/coreutils-8.32-tail-use-poll.patch

e63663
From c7a04cef4075da864a3468e63a5bb79334d8f556 Mon Sep 17 00:00:00 2001
e63663
From: Paul Eggert <eggert@cs.ucla.edu>
e63663
Date: Sat, 26 Jun 2021 18:23:52 -0700
e63663
Subject: [PATCH] tail: use poll, not select
e63663
MIME-Version: 1.0
e63663
Content-Type: text/plain; charset=UTF-8
e63663
Content-Transfer-Encoding: 8bit
e63663
e63663
This fixes an unlikely stack out-of-bounds write reported by
e63663
Stepan Broz via Kamil Dudka (Bug#49209).
e63663
* src/tail.c: Do not include <sys/select.h>.
e63663
[!_AIX]: Include poll.h.
e63663
(check_output_alive) [!_AIX]: Use poll instead of select.
e63663
(tail_forever_inotify): Likewise.  Simplify logic, as there is no
e63663
need for a ‘while (len <= evbuf_off)’ loop.
e63663
e63663
Upstream-commit: da0d448bca62c6305fc432f67e2c5ccc2da75346
e63663
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
e63663
---
e63663
 src/tail.c | 100 +++++++++++++++++++++--------------------------------
e63663
 1 file changed, 39 insertions(+), 61 deletions(-)
e63663
e63663
diff --git a/src/tail.c b/src/tail.c
e63663
index 1c88723..5b4f21a 100644
e63663
--- a/src/tail.c
e63663
+++ b/src/tail.c
e63663
@@ -28,12 +28,9 @@
e63663
 #include <stdio.h>
e63663
 #include <assert.h>
e63663
 #include <getopt.h>
e63663
-#include <sys/select.h>
e63663
+#include <poll.h>
e63663
 #include <sys/types.h>
e63663
 #include <signal.h>
e63663
-#ifdef _AIX
e63663
-# include <poll.h>
e63663
-#endif
e63663
 
e63663
 #include "system.h"
e63663
 #include "argmatch.h"
e63663
@@ -351,27 +348,12 @@ check_output_alive (void)
e63663
   if (! monitor_output)
e63663
     return;
e63663
 
e63663
-#ifdef _AIX
e63663
-  /* select on AIX was seen to give a readable event immediately.  */
e63663
   struct pollfd pfd;
e63663
   pfd.fd = STDOUT_FILENO;
e63663
   pfd.events = POLLERR;
e63663
 
e63663
   if (poll (&pfd, 1, 0) >= 0 && (pfd.revents & POLLERR))
e63663
     die_pipe ();
e63663
-#else
e63663
-  struct timeval delay;
e63663
-  delay.tv_sec = delay.tv_usec = 0;
e63663
-
e63663
-  fd_set rfd;
e63663
-  FD_ZERO (&rfd;;
e63663
-  FD_SET (STDOUT_FILENO, &rfd;;
e63663
-
e63663
-  /* readable event on STDOUT is equivalent to POLLERR,
e63663
-     and implies an error condition on output like broken pipe.  */
e63663
-  if (select (STDOUT_FILENO + 1, &rfd, NULL, NULL, &delay) == 1)
e63663
-    die_pipe ();
e63663
-#endif
e63663
 }
e63663
 
e63663
 static bool
e63663
@@ -1612,7 +1594,7 @@ tail_forever_inotify (int wd, struct File_spec *f, size_t n_files,
e63663
   /* Wait for inotify events and handle them.  Events on directories
e63663
      ensure that watched files can be re-added when following by name.
e63663
      This loop blocks on the 'safe_read' call until a new event is notified.
e63663
-     But when --pid=P is specified, tail usually waits via the select.  */
e63663
+     But when --pid=P is specified, tail usually waits via poll.  */
e63663
   while (1)
e63663
     {
e63663
       struct File_spec *fspec;
e63663
@@ -1629,54 +1611,51 @@ tail_forever_inotify (int wd, struct File_spec *f, size_t n_files,
e63663
           return false;
e63663
         }
e63663
 
e63663
-      /* When watching a PID, ensure that a read from WD will not block
e63663
-         indefinitely.  */
e63663
-      while (len <= evbuf_off)
e63663
+      if (len <= evbuf_off)
e63663
         {
e63663
-          struct timeval delay; /* how long to wait for file changes.  */
e63663
+          /* Poll for inotify events.  When watching a PID, ensure
e63663
+             that a read from WD will not block indefinitely.
e63663
+             If MONITOR_OUTPUT, also poll for a broken output pipe.  */
e63663
 
e63663
-          if (pid)
e63663
+          int file_change;
e63663
+          struct pollfd pfd[2];
e63663
+          do
e63663
             {
e63663
-              if (writer_is_dead)
e63663
-                exit (EXIT_SUCCESS);
e63663
+              /* How many ms to wait for changes.  -1 means wait forever.  */
e63663
+              int delay = -1;
e63663
 
e63663
-              writer_is_dead = (kill (pid, 0) != 0 && errno != EPERM);
e63663
-
e63663
-              if (writer_is_dead)
e63663
-                delay.tv_sec = delay.tv_usec = 0;
e63663
-              else
e63663
+              if (pid)
e63663
                 {
e63663
-                  delay.tv_sec = (time_t) sleep_interval;
e63663
-                  delay.tv_usec = 1000000 * (sleep_interval - delay.tv_sec);
e63663
+                  if (writer_is_dead)
e63663
+                    exit (EXIT_SUCCESS);
e63663
+
e63663
+                  writer_is_dead = (kill (pid, 0) != 0 && errno != EPERM);
e63663
+
e63663
+                  if (writer_is_dead || sleep_interval <= 0)
e63663
+                    delay = 0;
e63663
+                  else if (sleep_interval < INT_MAX / 1000 - 1)
e63663
+                    {
e63663
+                      /* delay = ceil (sleep_interval * 1000), sans libm.  */
e63663
+                      double ddelay = sleep_interval * 1000;
e63663
+                      delay = ddelay;
e63663
+                      delay += delay < ddelay;
e63663
+                    }
e63663
                 }
e63663
+
e63663
+              pfd[0].fd = wd;
e63663
+              pfd[0].events = POLLIN;
e63663
+              pfd[1].fd = STDOUT_FILENO;
e63663
+              pfd[1].events = pfd[1].revents = 0;
e63663
+              file_change = poll (pfd, monitor_output + 1, delay);
e63663
             }
e63663
+          while (file_change == 0);
e63663
 
e63663
-           fd_set rfd;
e63663
-           FD_ZERO (&rfd;;
e63663
-           FD_SET (wd, &rfd;;
e63663
-           if (monitor_output)
e63663
-             FD_SET (STDOUT_FILENO, &rfd;;
e63663
-
e63663
-           int file_change = select (MAX (wd, STDOUT_FILENO) + 1,
e63663
-                                     &rfd, NULL, NULL, pid ? &delay: NULL);
e63663
-
e63663
-           if (file_change == 0)
e63663
-             continue;
e63663
-           else if (file_change == -1)
e63663
-             die (EXIT_FAILURE, errno,
e63663
-                  _("error waiting for inotify and output events"));
e63663
-           else if (FD_ISSET (STDOUT_FILENO, &rfd))
e63663
-             {
e63663
-               /* readable event on STDOUT is equivalent to POLLERR,
e63663
-                  and implies an error on output like broken pipe.  */
e63663
-               die_pipe ();
e63663
-             }
e63663
-           else
e63663
-             break;
e63663
-        }
e63663
+          if (file_change < 0)
e63663
+            die (EXIT_FAILURE, errno,
e63663
+                 _("error waiting for inotify and output events"));
e63663
+          if (pfd[1].revents)
e63663
+            die_pipe ();
e63663
 
e63663
-      if (len <= evbuf_off)
e63663
-        {
e63663
           len = safe_read (wd, evbuf, evlen);
e63663
           evbuf_off = 0;
e63663
 
e63663
@@ -2437,8 +2416,7 @@ main (int argc, char **argv)
e63663
   if (forever && ignore_fifo_and_pipe (F, n_files))
e63663
     {
e63663
       /* If stdout is a fifo or pipe, then monitor it
e63663
-         so that we exit if the reader goes away.
e63663
-         Note select() on a regular file is always readable.  */
e63663
+         so that we exit if the reader goes away.  */
e63663
       struct stat out_stat;
e63663
       if (fstat (STDOUT_FILENO, &out_stat) < 0)
e63663
         die (EXIT_FAILURE, errno, _("standard output"));
e63663
-- 
e63663
2.31.1
e63663