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