5f7b84
commit 61d3db428176d9d0822e4e680305fe34285edff2
5f7b84
Author: Florian Weimer <fweimer@redhat.com>
5f7b84
Date:   Wed Aug 28 11:59:45 2019 +0200
5f7b84
5f7b84
    login: pututxline could fail to overwrite existing entries [BZ #24902]
5f7b84
    
5f7b84
    The internal_getut_r function updates the file_offset variable and
5f7b84
    therefore must always update last_entry as well.
5f7b84
    
5f7b84
    Previously, if pututxline could not upgrade the read lock to a
5f7b84
    write lock, internal_getut_r would update file_offset only,
5f7b84
    without updating last_entry, and a subsequent call would not
5f7b84
    overwrite the existing utmpx entry at file_offset, instead
5f7b84
    creating a new entry.  This has been observed to cause unbounded
5f7b84
    file growth in high-load situations.
5f7b84
    
5f7b84
    This commit removes the buffer argument to internal_getut_r and
5f7b84
    updates the last_entry variable directly, along with file_offset.
5f7b84
    
5f7b84
    Initially reported and fixed by Ondřej Lysoněk.
5f7b84
    
5f7b84
    Reviewed-by: Gabriel F. T. Gomes <gabrielftg@linux.ibm.com>
5f7b84
5f7b84
diff --git a/login/Makefile b/login/Makefile
5f7b84
index 81986ab6bd8560ea..82132c83fd799357 100644
5f7b84
--- a/login/Makefile
5f7b84
+++ b/login/Makefile
5f7b84
@@ -43,7 +43,8 @@ endif
5f7b84
 subdir-dirs = programs
5f7b84
 vpath %.c programs
5f7b84
 
5f7b84
-tests := tst-utmp tst-utmpx tst-grantpt tst-ptsname tst-getlogin tst-updwtmpx
5f7b84
+tests := tst-utmp tst-utmpx tst-grantpt tst-ptsname tst-getlogin tst-updwtmpx \
5f7b84
+  tst-pututxline-lockfail
5f7b84
 
5f7b84
 # Build the -lutil library with these extra functions.
5f7b84
 extra-libs      := libutil
5f7b84
@@ -71,3 +72,5 @@ endif
5f7b84
 $(inst_libexecdir)/pt_chown: $(objpfx)pt_chown $(+force)
5f7b84
 	$(make-target-directory)
5f7b84
 	-$(INSTALL_PROGRAM) -m 4755 -o root $< $@
5f7b84
+
5f7b84
+$(objpfx)tst-pututxline-lockfail: $(shared-thread-library)
5f7b84
diff --git a/login/tst-pututxline-lockfail.c b/login/tst-pututxline-lockfail.c
5f7b84
new file mode 100644
5f7b84
index 0000000000000000..47c25dc0658d3c60
5f7b84
--- /dev/null
5f7b84
+++ b/login/tst-pututxline-lockfail.c
5f7b84
@@ -0,0 +1,176 @@
5f7b84
+/* Test the lock upgrade path in tst-pututxline.
5f7b84
+   Copyright (C) 2019 Free Software Foundation, Inc.
5f7b84
+   This file is part of the GNU C Library.
5f7b84
+
5f7b84
+   The GNU C Library is free software; you can redistribute it and/or
5f7b84
+   modify it under the terms of the GNU Lesser General Public License as
5f7b84
+   published by the Free Software Foundation; either version 2.1 of the
5f7b84
+   License, or (at your option) any later version.
5f7b84
+
5f7b84
+   The GNU C Library is distributed in the hope that it will be useful,
5f7b84
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
5f7b84
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5f7b84
+   Lesser General Public License for more details.
5f7b84
+
5f7b84
+   You should have received a copy of the GNU Lesser General Public
5f7b84
+   License along with the GNU C Library; see the file COPYING.LIB.  If
5f7b84
+   not, see <http://www.gnu.org/licenses/>.  */
5f7b84
+
5f7b84
+/* pututxline upgrades the read lock on the file to a write lock.
5f7b84
+   This test verifies that if the lock upgrade fails, the utmp
5f7b84
+   subsystem remains in a consistent state, so that pututxline can be
5f7b84
+   called again.  */
5f7b84
+
5f7b84
+#include <errno.h>
5f7b84
+#include <fcntl.h>
5f7b84
+#include <stdlib.h>
5f7b84
+#include <support/check.h>
5f7b84
+#include <support/namespace.h>
5f7b84
+#include <support/support.h>
5f7b84
+#include <support/temp_file.h>
5f7b84
+#include <support/xthread.h>
5f7b84
+#include <support/xunistd.h>
5f7b84
+#include <unistd.h>
5f7b84
+#include <utmp.h>
5f7b84
+#include <utmpx.h>
5f7b84
+
5f7b84
+/* Path to the temporary utmp file.   */
5f7b84
+static char *path;
5f7b84
+
5f7b84
+/* Used to synchronize the subprocesses.  The barrier itself is
5f7b84
+   allocated in shared memory.  */
5f7b84
+static pthread_barrier_t *barrier;
5f7b84
+
5f7b84
+/* Use pututxline to write an entry for PID.  */
5f7b84
+static struct utmpx *
5f7b84
+write_entry (pid_t pid)
5f7b84
+{
5f7b84
+  struct utmpx ut =
5f7b84
+    {
5f7b84
+     .ut_type = LOGIN_PROCESS,
5f7b84
+     .ut_id = "1",
5f7b84
+     .ut_user = "root",
5f7b84
+     .ut_pid = pid,
5f7b84
+     .ut_line = "entry",
5f7b84
+     .ut_host = "localhost",
5f7b84
+    };
5f7b84
+  return pututxline (&ut);
5f7b84
+}
5f7b84
+
5f7b84
+/* Create the initial entry in a subprocess, so that the utmp
5f7b84
+   subsystem in the original process is not disturbed.  */
5f7b84
+static void
5f7b84
+subprocess_create_entry (void *closure)
5f7b84
+{
5f7b84
+  TEST_COMPARE (utmpname (path), 0);
5f7b84
+  TEST_VERIFY (write_entry (101) != NULL);
5f7b84
+}
5f7b84
+
5f7b84
+/* Acquire an advisory read lock on PATH.  */
5f7b84
+__attribute__ ((noreturn)) static void
5f7b84
+subprocess_lock_file (void)
5f7b84
+{
5f7b84
+  int fd = xopen (path, O_RDONLY, 0);
5f7b84
+
5f7b84
+  struct flock64 fl =
5f7b84
+    {
5f7b84
+     .l_type = F_RDLCK,
5f7b84
+     fl.l_whence = SEEK_SET,
5f7b84
+    };
5f7b84
+  TEST_COMPARE (fcntl64 (fd, F_SETLKW, &fl), 0);
5f7b84
+
5f7b84
+  /* Signal to the main process that the lock has been acquired.  */
5f7b84
+  xpthread_barrier_wait (barrier);
5f7b84
+
5f7b84
+  /* Wait for the unlock request from the main process.  */
5f7b84
+  xpthread_barrier_wait (barrier);
5f7b84
+
5f7b84
+  /* Implicitly unlock the file.  */
5f7b84
+  xclose (fd);
5f7b84
+
5f7b84
+  /* Overwrite the existing entry.  */
5f7b84
+  TEST_COMPARE (utmpname (path), 0);
5f7b84
+  errno = 0;
5f7b84
+  setutxent ();
5f7b84
+  TEST_COMPARE (errno, 0);
5f7b84
+  TEST_VERIFY (write_entry (102) != NULL);
5f7b84
+  errno = 0;
5f7b84
+  endutxent ();
5f7b84
+  TEST_COMPARE (errno, 0);
5f7b84
+
5f7b84
+  _exit (0);
5f7b84
+}
5f7b84
+
5f7b84
+static int
5f7b84
+do_test (void)
5f7b84
+{
5f7b84
+  xclose (create_temp_file ("tst-pututxline-lockfail-", &path));
5f7b84
+
5f7b84
+  {
5f7b84
+    pthread_barrierattr_t attr;
5f7b84
+    xpthread_barrierattr_init (&attr);
5f7b84
+    xpthread_barrierattr_setpshared (&attr, PTHREAD_SCOPE_PROCESS);
5f7b84
+    barrier = support_shared_allocate (sizeof (*barrier));
5f7b84
+    xpthread_barrier_init (barrier, &attr, 2);
5f7b84
+    xpthread_barrierattr_destroy (&attr);
5f7b84
+  }
5f7b84
+
5f7b84
+  /* Write the initial entry.  */
5f7b84
+  support_isolate_in_subprocess (subprocess_create_entry, NULL);
5f7b84
+
5f7b84
+  pid_t locker_pid = xfork ();
5f7b84
+  if (locker_pid == 0)
5f7b84
+    subprocess_lock_file ();
5f7b84
+
5f7b84
+  /* Wait for the file locking to complete.  */
5f7b84
+  xpthread_barrier_wait (barrier);
5f7b84
+
5f7b84
+  /* Try to add another entry.  This attempt will fail, with EINTR or
5f7b84
+     EAGAIN.  */
5f7b84
+  TEST_COMPARE (utmpname (path), 0);
5f7b84
+  TEST_VERIFY (write_entry (102) == NULL);
5f7b84
+  if (errno != EINTR)
5f7b84
+    TEST_COMPARE (errno, EAGAIN);
5f7b84
+
5f7b84
+  /* Signal the subprocess to overwrite the entry.  */
5f7b84
+  xpthread_barrier_wait (barrier);
5f7b84
+
5f7b84
+  /* Wait for write and unlock to complete.  */
5f7b84
+  {
5f7b84
+    int status;
5f7b84
+    xwaitpid (locker_pid, &status, 0);
5f7b84
+    TEST_COMPARE (status, 0);
5f7b84
+  }
5f7b84
+
5f7b84
+  /* The file is no longer locked, so this operation will succeed.  */
5f7b84
+  TEST_VERIFY (write_entry (103) != NULL);
5f7b84
+  errno = 0;
5f7b84
+  endutxent ();
5f7b84
+  TEST_COMPARE (errno, 0);
5f7b84
+
5f7b84
+  /* Check that there is just one entry with the expected contents.
5f7b84
+     If pututxline becomes desynchronized internally, the entry is not
5f7b84
+     overwritten (bug 24902).  */
5f7b84
+  errno = 0;
5f7b84
+  setutxent ();
5f7b84
+  TEST_COMPARE (errno, 0);
5f7b84
+  struct utmpx *ut = getutxent ();
5f7b84
+  TEST_VERIFY_EXIT (ut != NULL);
5f7b84
+  TEST_COMPARE (ut->ut_type, LOGIN_PROCESS);
5f7b84
+  TEST_COMPARE_STRING (ut->ut_id, "1");
5f7b84
+  TEST_COMPARE_STRING (ut->ut_user, "root");
5f7b84
+  TEST_COMPARE (ut->ut_pid, 103);
5f7b84
+  TEST_COMPARE_STRING (ut->ut_line, "entry");
5f7b84
+  TEST_COMPARE_STRING (ut->ut_host, "localhost");
5f7b84
+  TEST_VERIFY (getutxent () == NULL);
5f7b84
+  errno = 0;
5f7b84
+  endutxent ();
5f7b84
+  TEST_COMPARE (errno, 0);
5f7b84
+
5f7b84
+  xpthread_barrier_destroy (barrier);
5f7b84
+  support_shared_free (barrier);
5f7b84
+  free (path);
5f7b84
+  return 0;
5f7b84
+}
5f7b84
+
5f7b84
+#include <support/test-driver.c>
5f7b84
diff --git a/login/utmp_file.c b/login/utmp_file.c
5f7b84
index a736d3d25e005920..cbc53d06de280af9 100644
5f7b84
--- a/login/utmp_file.c
5f7b84
+++ b/login/utmp_file.c
5f7b84
@@ -185,9 +185,11 @@ __libc_getutent_r (struct utmp *buffer, struct utmp **result)
5f7b84
 }
5f7b84
 
5f7b84
 
5f7b84
+/* Search for *ID, updating last_entry and file_offset.  Return 0 on
5f7b84
+   success and -1 on failure.  If the locking operation failed, write
5f7b84
+   true to *LOCK_FAILED.  */
5f7b84
 static int
5f7b84
-internal_getut_r (const struct utmp *id, struct utmp *buffer,
5f7b84
-		  bool *lock_failed)
5f7b84
+internal_getut_r (const struct utmp *id, bool *lock_failed)
5f7b84
 {
5f7b84
   int result = -1;
5f7b84
 
5f7b84
@@ -206,7 +208,7 @@ internal_getut_r (const struct utmp *id, struct utmp *buffer,
5f7b84
       while (1)
5f7b84
 	{
5f7b84
 	  /* Read the next entry.  */
5f7b84
-	  if (__read_nocancel (file_fd, buffer, sizeof (struct utmp))
5f7b84
+	  if (__read_nocancel (file_fd, &last_entry, sizeof (struct utmp))
5f7b84
 	      != sizeof (struct utmp))
5f7b84
 	    {
5f7b84
 	      __set_errno (ESRCH);
5f7b84
@@ -215,7 +217,7 @@ internal_getut_r (const struct utmp *id, struct utmp *buffer,
5f7b84
 	    }
5f7b84
 	  file_offset += sizeof (struct utmp);
5f7b84
 
5f7b84
-	  if (id->ut_type == buffer->ut_type)
5f7b84
+	  if (id->ut_type == last_entry.ut_type)
5f7b84
 	    break;
5f7b84
 	}
5f7b84
     }
5f7b84
@@ -227,7 +229,7 @@ internal_getut_r (const struct utmp *id, struct utmp *buffer,
5f7b84
       while (1)
5f7b84
 	{
5f7b84
 	  /* Read the next entry.  */
5f7b84
-	  if (__read_nocancel (file_fd, buffer, sizeof (struct utmp))
5f7b84
+	  if (__read_nocancel (file_fd, &last_entry, sizeof (struct utmp))
5f7b84
 	      != sizeof (struct utmp))
5f7b84
 	    {
5f7b84
 	      __set_errno (ESRCH);
5f7b84
@@ -236,7 +238,7 @@ internal_getut_r (const struct utmp *id, struct utmp *buffer,
5f7b84
 	    }
5f7b84
 	  file_offset += sizeof (struct utmp);
5f7b84
 
5f7b84
-	  if (__utmp_equal (buffer, id))
5f7b84
+	  if (__utmp_equal (&last_entry, id))
5f7b84
 	    break;
5f7b84
 	}
5f7b84
     }
5f7b84
@@ -265,7 +267,7 @@ __libc_getutid_r (const struct utmp *id, struct utmp *buffer,
5f7b84
   /* We don't have to distinguish whether we can lock the file or
5f7b84
      whether there is no entry.  */
5f7b84
   bool lock_failed = false;
5f7b84
-  if (internal_getut_r (id, &last_entry, &lock_failed) < 0)
5f7b84
+  if (internal_getut_r (id, &lock_failed) < 0)
5f7b84
     {
5f7b84
       *result = NULL;
5f7b84
       return -1;
5f7b84
@@ -330,10 +332,9 @@ unlock_return:
5f7b84
 struct utmp *
5f7b84
 __libc_pututline (const struct utmp *data)
5f7b84
 {
5f7b84
-  if (!maybe_setutent ())
5f7b84
+  if (!maybe_setutent () || file_offset == -1l)
5f7b84
     return NULL;
5f7b84
 
5f7b84
-  struct utmp buffer;
5f7b84
   struct utmp *pbuf;
5f7b84
   int found;
5f7b84
 
5f7b84
@@ -369,7 +370,7 @@ __libc_pututline (const struct utmp *data)
5f7b84
   else
5f7b84
     {
5f7b84
       bool lock_failed = false;
5f7b84
-      found = internal_getut_r (data, &buffer, &lock_failed);
5f7b84
+      found = internal_getut_r (data, &lock_failed);
5f7b84
 
5f7b84
       if (__builtin_expect (lock_failed, false))
5f7b84
 	{