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