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