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