5f7b84
commit 481c30cb9573a280649fbf27251e6a0f4af1b2b1
5f7b84
Author: Alexandra Hájková <ahajkova@redhat.com>
5f7b84
Date:   Thu May 9 13:51:40 2019 +0200
5f7b84
5f7b84
    elf: Add tst-ldconfig-bad-aux-cache test [BZ #18093]
5f7b84
    
5f7b84
    This test corrupts /var/cache/ldconfig/aux-cache and executes ldconfig
5f7b84
    to check it will not segfault using the corrupted aux_cache. The test
5f7b84
    uses the test-in-container framework. Verified no regressions on
5f7b84
    x86_64.
5f7b84
5f7b84
diff --git a/elf/Makefile b/elf/Makefile
5f7b84
index 139d072e136284e1..8e907e69eb35e089 100644
5f7b84
--- a/elf/Makefile
5f7b84
+++ b/elf/Makefile
5f7b84
@@ -156,6 +156,9 @@ tests-static-internal := tst-tls1-static tst-tls2-static \
5f7b84
 CRT-tst-tls1-static-non-pie := $(csu-objpfx)crt1.o
5f7b84
 tst-tls1-static-non-pie-no-pie = yes
5f7b84
 
5f7b84
+tests-container = \
5f7b84
+			  tst-ldconfig-bad-aux-cache
5f7b84
+
5f7b84
 tests := tst-tls9 tst-leaks1 \
5f7b84
 	tst-array1 tst-array2 tst-array3 tst-array4 tst-array5 \
5f7b84
 	tst-auxv
5f7b84
diff --git a/elf/tst-ldconfig-bad-aux-cache.c b/elf/tst-ldconfig-bad-aux-cache.c
5f7b84
new file mode 100644
5f7b84
index 0000000000000000..68ce90a95648f6ab
5f7b84
--- /dev/null
5f7b84
+++ b/elf/tst-ldconfig-bad-aux-cache.c
5f7b84
@@ -0,0 +1,117 @@
5f7b84
+/* Test ldconfig does not segfault when aux-cache is corrupted (Bug 18093).
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 test does the following:
5f7b84
+   Run ldconfig to create the caches.
5f7b84
+   Corrupt the caches.
5f7b84
+   Run ldconfig again.
5f7b84
+   At each step we verify that ldconfig does not crash.  */
5f7b84
+
5f7b84
+#include <stdio.h>
5f7b84
+#include <stdlib.h>
5f7b84
+#include <string.h>
5f7b84
+#include <unistd.h>
5f7b84
+#include <errno.h>
5f7b84
+#include <sys/wait.h>
5f7b84
+#include <ftw.h>
5f7b84
+#include <stdint.h>
5f7b84
+
5f7b84
+#include <support/check.h>
5f7b84
+#include <support/support.h>
5f7b84
+#include <support/xunistd.h>
5f7b84
+
5f7b84
+#include <dirent.h>
5f7b84
+
5f7b84
+static int
5f7b84
+display_info (const char *fpath, const struct stat *sb,
5f7b84
+              int tflag, struct FTW *ftwbuf)
5f7b84
+{
5f7b84
+  printf ("info: %-3s %2d %7jd   %-40s %d %s\n",
5f7b84
+          (tflag == FTW_D) ? "d" : (tflag == FTW_DNR) ? "dnr" :
5f7b84
+          (tflag == FTW_DP) ? "dp" : (tflag == FTW_F) ? "f" :
5f7b84
+          (tflag == FTW_NS) ? "ns" : (tflag == FTW_SL) ? "sl" :
5f7b84
+          (tflag == FTW_SLN) ? "sln" : "???",
5f7b84
+          ftwbuf->level, (intmax_t) sb->st_size,
5f7b84
+          fpath, ftwbuf->base, fpath + ftwbuf->base);
5f7b84
+  /* To tell nftw to continue.  */
5f7b84
+  return 0;
5f7b84
+}
5f7b84
+
5f7b84
+/* Run ldconfig with a corrupt aux-cache, in particular we test for size
5f7b84
+   truncation that might happen if a previous ldconfig run failed or if
5f7b84
+   there were storage or power issues while we were writing the file.
5f7b84
+   We want ldconfig not to crash, and it should be able to do so by
5f7b84
+   computing the expected size of the file (bug 18093).  */
5f7b84
+static int
5f7b84
+do_test (void)
5f7b84
+{
5f7b84
+  char *prog = xasprintf ("%s/ldconfig", support_install_rootsbindir);
5f7b84
+  char *const args[] = { prog, NULL };
5f7b84
+  const char *path = "/var/cache/ldconfig/aux-cache";
5f7b84
+  struct stat64 fs;
5f7b84
+  long int size, new_size, i;
5f7b84
+  int status;
5f7b84
+  pid_t pid;
5f7b84
+
5f7b84
+  /* Create the needed directories. */
5f7b84
+  xmkdirp ("/var/cache/ldconfig", 0777);
5f7b84
+
5f7b84
+  pid = xfork ();
5f7b84
+  /* Run ldconfig fist to generate the aux-cache.  */
5f7b84
+  if (pid == 0)
5f7b84
+    {
5f7b84
+      execv (args[0], args);
5f7b84
+      _exit (1);
5f7b84
+    }
5f7b84
+  else
5f7b84
+    {
5f7b84
+      xwaitpid (pid, &status, 0);
5f7b84
+      TEST_COMPARE(status, 0);
5f7b84
+      xstat (path, &fs);
5f7b84
+
5f7b84
+      size = fs.st_size;
5f7b84
+      /* Run 3 tests, each truncating aux-cache shorter and shorter.  */
5f7b84
+      for (i = 3; i > 0; i--)
5f7b84
+        {
5f7b84
+          new_size = size * i / 4;
5f7b84
+          if (truncate (path, new_size))
5f7b84
+              FAIL_EXIT1 ("truncation failed: %m");
5f7b84
+          if (nftw (path, display_info, 1000, 0) == -1)
5f7b84
+              FAIL_EXIT1 ("nftw failed.");
5f7b84
+
5f7b84
+          pid = xfork ();
5f7b84
+          /* Verify that ldconfig can run with a truncated
5f7b84
+             aux-cache and doesn't crash.  */
5f7b84
+          if (pid == 0)
5f7b84
+            {
5f7b84
+              execv (args[0], args);
5f7b84
+              _exit (1);
5f7b84
+            }
5f7b84
+          else
5f7b84
+            {
5f7b84
+              xwaitpid (pid, &status, 0);
5f7b84
+              TEST_COMPARE(status, 0);
5f7b84
+            }
5f7b84
+        }
5f7b84
+    }
5f7b84
+
5f7b84
+  free (prog);
5f7b84
+  return 0;
5f7b84
+}
5f7b84
+
5f7b84
+#include <support/test-driver.c>
5f7b84
diff --git a/elf/tst-ldconfig-bad-aux-cache.root/etc/ld.so.conf b/elf/tst-ldconfig-bad-aux-cache.root/etc/ld.so.conf
5f7b84
new file mode 100644
5f7b84
index 0000000000000000..e1e74dbda2bf3dfa
5f7b84
--- /dev/null
5f7b84
+++ b/elf/tst-ldconfig-bad-aux-cache.root/etc/ld.so.conf
5f7b84
@@ -0,0 +1,2 @@
5f7b84
+# This file was created to suppress a warning from ldconfig:
5f7b84
+# /sbin/ldconfig: Warning: ignoring configuration file that cannot be opened: /etc/ld.so.conf: No such file or directory
5f7b84
diff --git a/elf/tst-ldconfig-bad-aux-cache.root/postclean.req b/elf/tst-ldconfig-bad-aux-cache.root/postclean.req
5f7b84
new file mode 100644
5f7b84
index 0000000000000000..e69de29bb2d1d643