d8307d
commit 341da5b4b6253de9a7581a066f33f89cacb44dec
d8307d
Author: Florian Weimer <fweimer@redhat.com>
d8307d
Date:   Thu Aug 15 10:30:23 2019 +0200
d8307d
d8307d
    login: Fix updwtmp, updwtmx unlocking
d8307d
    
d8307d
    Commit 5a3afa9738f3dbbaf8c0a35665318c1af782111b (login: Replace
d8307d
    macro-based control flow with function calls in utmp) introduced
d8307d
    a regression because after it, __libc_updwtmp attempts to unlock
d8307d
    the wrong file descriptor.
d8307d
d8307d
diff --git a/login/Makefile b/login/Makefile
d8307d
index 8b31991be835fa8e..81986ab6bd8560ea 100644
d8307d
--- a/login/Makefile
d8307d
+++ b/login/Makefile
d8307d
@@ -43,7 +43,7 @@ endif
d8307d
 subdir-dirs = programs
d8307d
 vpath %.c programs
d8307d
 
d8307d
-tests := tst-utmp tst-utmpx tst-grantpt tst-ptsname tst-getlogin
d8307d
+tests := tst-utmp tst-utmpx tst-grantpt tst-ptsname tst-getlogin tst-updwtmpx
d8307d
 
d8307d
 # Build the -lutil library with these extra functions.
d8307d
 extra-libs      := libutil
d8307d
diff --git a/login/tst-updwtmpx.c b/login/tst-updwtmpx.c
d8307d
new file mode 100644
d8307d
index 0000000000000000..0a4a27daeb0440fd
d8307d
--- /dev/null
d8307d
+++ b/login/tst-updwtmpx.c
d8307d
@@ -0,0 +1,112 @@
d8307d
+/* Basic test coverage for updwtmpx.
d8307d
+   Copyright (C) 2019 Free Software Foundation, Inc.
d8307d
+   This file is part of the GNU C Library.
d8307d
+
d8307d
+   The GNU C Library is free software; you can redistribute it and/or
d8307d
+   modify it under the terms of the GNU Lesser General Public License as
d8307d
+   published by the Free Software Foundation; either version 2.1 of the
d8307d
+   License, or (at your option) any later version.
d8307d
+
d8307d
+   The GNU C Library is distributed in the hope that it will be useful,
d8307d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
d8307d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d8307d
+   Lesser General Public License for more details.
d8307d
+
d8307d
+   You should have received a copy of the GNU Lesser General Public
d8307d
+   License along with the GNU C Library; see the file COPYING.LIB.  If
d8307d
+   not, see <http://www.gnu.org/licenses/>.  */
d8307d
+
d8307d
+/* This program runs a series of tests.  Each one calls updwtmpx
d8307d
+   twice, to write two records, optionally with misalignment in the
d8307d
+   file, and reads back the results.  */
d8307d
+
d8307d
+#include <errno.h>
d8307d
+#include <stdlib.h>
d8307d
+#include <support/check.h>
d8307d
+#include <support/descriptors.h>
d8307d
+#include <support/support.h>
d8307d
+#include <support/temp_file.h>
d8307d
+#include <support/test-driver.h>
d8307d
+#include <support/xunistd.h>
d8307d
+#include <unistd.h>
d8307d
+#include <utmpx.h>
d8307d
+
d8307d
+static int
d8307d
+do_test (void)
d8307d
+{
d8307d
+  /* Two entries filled with an arbitrary bit pattern.  */
d8307d
+  struct utmpx entries[2];
d8307d
+  unsigned char pad;
d8307d
+  {
d8307d
+    unsigned char *p = (unsigned char *) &entries[0];
d8307d
+    for (size_t i = 0; i < sizeof (entries); ++i)
d8307d
+      {
d8307d
+        p[i] = i;
d8307d
+      }
d8307d
+    /* Make sure that the first and second entry and the padding are
d8307d
+       different.  */
d8307d
+    p[sizeof (struct utmpx)] = p[0] + 1;
d8307d
+    pad = p[0] + 2;
d8307d
+  }
d8307d
+
d8307d
+  char *path;
d8307d
+  int fd = create_temp_file ("tst-updwtmpx-", &path);
d8307d
+
d8307d
+  /* Used to check that updwtmpx does not leave an open file
d8307d
+     descriptor around.  */
d8307d
+  struct support_descriptors *descriptors = support_descriptors_list ();
d8307d
+
d8307d
+  /* updwtmpx is expected to remove misalignment.  Optionally insert
d8307d
+     one byte of misalignment at the start and in the middle (after
d8307d
+     the first entry).  */
d8307d
+  for (int misaligned_start = 0; misaligned_start < 2; ++misaligned_start)
d8307d
+    for (int misaligned_middle = 0; misaligned_middle < 2; ++misaligned_middle)
d8307d
+      {
d8307d
+        if (test_verbose > 0)
d8307d
+          printf ("info: misaligned_start=%d misaligned_middle=%d\n",
d8307d
+                  misaligned_start, misaligned_middle);
d8307d
+
d8307d
+        xftruncate (fd, 0);
d8307d
+        TEST_COMPARE (pwrite64 (fd, &pad, misaligned_start, 0),
d8307d
+                      misaligned_start);
d8307d
+
d8307d
+        /* Write first entry and check it.  */
d8307d
+        errno = 0;
d8307d
+        updwtmpx (path, &entries[0]);
d8307d
+        TEST_COMPARE (errno, 0);
d8307d
+        support_descriptors_check (descriptors);
d8307d
+        TEST_COMPARE (xlseek (fd, 0, SEEK_END), sizeof (struct utmpx));
d8307d
+        struct utmpx buffer;
d8307d
+        TEST_COMPARE (pread64 (fd, &buffer, sizeof (buffer), 0),
d8307d
+                      sizeof (buffer));
d8307d
+        TEST_COMPARE_BLOB (&entries[0], sizeof (entries[0]),
d8307d
+                           &buffer, sizeof (buffer));
d8307d
+
d8307d
+        /* Middle mis-alignmet.  */
d8307d
+        TEST_COMPARE (pwrite64 (fd, &pad, misaligned_middle,
d8307d
+                                sizeof (struct utmpx)), misaligned_middle);
d8307d
+
d8307d
+        /* Write second entry and check both entries.  */
d8307d
+        errno = 0;
d8307d
+        updwtmpx (path, &entries[1]);
d8307d
+        TEST_COMPARE (errno, 0);
d8307d
+        support_descriptors_check (descriptors);
d8307d
+        TEST_COMPARE (xlseek (fd, 0, SEEK_END), 2 * sizeof (struct utmpx));
d8307d
+        TEST_COMPARE (pread64 (fd, &buffer, sizeof (buffer), 0),
d8307d
+                      sizeof (buffer));
d8307d
+        TEST_COMPARE_BLOB (&entries[0], sizeof (entries[0]),
d8307d
+                           &buffer, sizeof (buffer));
d8307d
+        TEST_COMPARE (pread64 (fd, &buffer, sizeof (buffer), sizeof (buffer)),
d8307d
+                      sizeof (buffer));
d8307d
+        TEST_COMPARE_BLOB (&entries[1], sizeof (entries[1]),
d8307d
+                           &buffer, sizeof (buffer));
d8307d
+      }
d8307d
+
d8307d
+  support_descriptors_free (descriptors);
d8307d
+  free (path);
d8307d
+  xclose (fd);
d8307d
+
d8307d
+  return 0;
d8307d
+}
d8307d
+
d8307d
+#include <support/test-driver.c>
d8307d
diff --git a/login/utmp_file.c b/login/utmp_file.c
d8307d
index 812de8fd3d099ce9..54f424fd6165bae7 100644
d8307d
--- a/login/utmp_file.c
d8307d
+++ b/login/utmp_file.c
d8307d
@@ -503,7 +503,7 @@ __libc_updwtmp (const char *file, const struct utmp *utmp)
d8307d
   result = 0;
d8307d
 
d8307d
 unlock_return:
d8307d
-  file_unlock (file_fd);
d8307d
+  file_unlock (fd);
d8307d
   file_lock_restore (&fl);
d8307d
 
d8307d
   /* Close WTMP file.  */