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