5f7b84
commit 341da5b4b6253de9a7581a066f33f89cacb44dec
5f7b84
Author: Florian Weimer <fweimer@redhat.com>
5f7b84
Date:   Thu Aug 15 10:30:23 2019 +0200
5f7b84
5f7b84
    login: Fix updwtmp, updwtmx unlocking
5f7b84
    
5f7b84
    Commit 5a3afa9738f3dbbaf8c0a35665318c1af782111b (login: Replace
5f7b84
    macro-based control flow with function calls in utmp) introduced
5f7b84
    a regression because after it, __libc_updwtmp attempts to unlock
5f7b84
    the wrong file descriptor.
5f7b84
5f7b84
diff --git a/login/Makefile b/login/Makefile
5f7b84
index 8b31991be835fa8e..81986ab6bd8560ea 100644
5f7b84
--- a/login/Makefile
5f7b84
+++ b/login/Makefile
5f7b84
@@ -43,7 +43,7 @@ endif
5f7b84
 subdir-dirs = programs
5f7b84
 vpath %.c programs
5f7b84
 
5f7b84
-tests := tst-utmp tst-utmpx tst-grantpt tst-ptsname tst-getlogin
5f7b84
+tests := tst-utmp tst-utmpx tst-grantpt tst-ptsname tst-getlogin tst-updwtmpx
5f7b84
 
5f7b84
 # Build the -lutil library with these extra functions.
5f7b84
 extra-libs      := libutil
5f7b84
diff --git a/login/tst-updwtmpx.c b/login/tst-updwtmpx.c
5f7b84
new file mode 100644
5f7b84
index 0000000000000000..0a4a27daeb0440fd
5f7b84
--- /dev/null
5f7b84
+++ b/login/tst-updwtmpx.c
5f7b84
@@ -0,0 +1,112 @@
5f7b84
+/* Basic test coverage for updwtmpx.
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
+/* This program runs a series of tests.  Each one calls updwtmpx
5f7b84
+   twice, to write two records, optionally with misalignment in the
5f7b84
+   file, and reads back the results.  */
5f7b84
+
5f7b84
+#include <errno.h>
5f7b84
+#include <stdlib.h>
5f7b84
+#include <support/check.h>
5f7b84
+#include <support/descriptors.h>
5f7b84
+#include <support/support.h>
5f7b84
+#include <support/temp_file.h>
5f7b84
+#include <support/test-driver.h>
5f7b84
+#include <support/xunistd.h>
5f7b84
+#include <unistd.h>
5f7b84
+#include <utmpx.h>
5f7b84
+
5f7b84
+static int
5f7b84
+do_test (void)
5f7b84
+{
5f7b84
+  /* Two entries filled with an arbitrary bit pattern.  */
5f7b84
+  struct utmpx entries[2];
5f7b84
+  unsigned char pad;
5f7b84
+  {
5f7b84
+    unsigned char *p = (unsigned char *) &entries[0];
5f7b84
+    for (size_t i = 0; i < sizeof (entries); ++i)
5f7b84
+      {
5f7b84
+        p[i] = i;
5f7b84
+      }
5f7b84
+    /* Make sure that the first and second entry and the padding are
5f7b84
+       different.  */
5f7b84
+    p[sizeof (struct utmpx)] = p[0] + 1;
5f7b84
+    pad = p[0] + 2;
5f7b84
+  }
5f7b84
+
5f7b84
+  char *path;
5f7b84
+  int fd = create_temp_file ("tst-updwtmpx-", &path);
5f7b84
+
5f7b84
+  /* Used to check that updwtmpx does not leave an open file
5f7b84
+     descriptor around.  */
5f7b84
+  struct support_descriptors *descriptors = support_descriptors_list ();
5f7b84
+
5f7b84
+  /* updwtmpx is expected to remove misalignment.  Optionally insert
5f7b84
+     one byte of misalignment at the start and in the middle (after
5f7b84
+     the first entry).  */
5f7b84
+  for (int misaligned_start = 0; misaligned_start < 2; ++misaligned_start)
5f7b84
+    for (int misaligned_middle = 0; misaligned_middle < 2; ++misaligned_middle)
5f7b84
+      {
5f7b84
+        if (test_verbose > 0)
5f7b84
+          printf ("info: misaligned_start=%d misaligned_middle=%d\n",
5f7b84
+                  misaligned_start, misaligned_middle);
5f7b84
+
5f7b84
+        xftruncate (fd, 0);
5f7b84
+        TEST_COMPARE (pwrite64 (fd, &pad, misaligned_start, 0),
5f7b84
+                      misaligned_start);
5f7b84
+
5f7b84
+        /* Write first entry and check it.  */
5f7b84
+        errno = 0;
5f7b84
+        updwtmpx (path, &entries[0]);
5f7b84
+        TEST_COMPARE (errno, 0);
5f7b84
+        support_descriptors_check (descriptors);
5f7b84
+        TEST_COMPARE (xlseek (fd, 0, SEEK_END), sizeof (struct utmpx));
5f7b84
+        struct utmpx buffer;
5f7b84
+        TEST_COMPARE (pread64 (fd, &buffer, sizeof (buffer), 0),
5f7b84
+                      sizeof (buffer));
5f7b84
+        TEST_COMPARE_BLOB (&entries[0], sizeof (entries[0]),
5f7b84
+                           &buffer, sizeof (buffer));
5f7b84
+
5f7b84
+        /* Middle mis-alignmet.  */
5f7b84
+        TEST_COMPARE (pwrite64 (fd, &pad, misaligned_middle,
5f7b84
+                                sizeof (struct utmpx)), misaligned_middle);
5f7b84
+
5f7b84
+        /* Write second entry and check both entries.  */
5f7b84
+        errno = 0;
5f7b84
+        updwtmpx (path, &entries[1]);
5f7b84
+        TEST_COMPARE (errno, 0);
5f7b84
+        support_descriptors_check (descriptors);
5f7b84
+        TEST_COMPARE (xlseek (fd, 0, SEEK_END), 2 * sizeof (struct utmpx));
5f7b84
+        TEST_COMPARE (pread64 (fd, &buffer, sizeof (buffer), 0),
5f7b84
+                      sizeof (buffer));
5f7b84
+        TEST_COMPARE_BLOB (&entries[0], sizeof (entries[0]),
5f7b84
+                           &buffer, sizeof (buffer));
5f7b84
+        TEST_COMPARE (pread64 (fd, &buffer, sizeof (buffer), sizeof (buffer)),
5f7b84
+                      sizeof (buffer));
5f7b84
+        TEST_COMPARE_BLOB (&entries[1], sizeof (entries[1]),
5f7b84
+                           &buffer, sizeof (buffer));
5f7b84
+      }
5f7b84
+
5f7b84
+  support_descriptors_free (descriptors);
5f7b84
+  free (path);
5f7b84
+  xclose (fd);
5f7b84
+
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 812de8fd3d099ce9..54f424fd6165bae7 100644
5f7b84
--- a/login/utmp_file.c
5f7b84
+++ b/login/utmp_file.c
5f7b84
@@ -503,7 +503,7 @@ __libc_updwtmp (const char *file, const struct utmp *utmp)
5f7b84
   result = 0;
5f7b84
 
5f7b84
 unlock_return:
5f7b84
-  file_unlock (file_fd);
5f7b84
+  file_unlock (fd);
5f7b84
   file_lock_restore (&fl);
5f7b84
 
5f7b84
   /* Close WTMP file.  */