hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame elf/tst-ldconfig-bad-aux-cache.c

Packit 8c24b4
/* Test ldconfig does not segfault when aux-cache is corrupted (Bug 18093).
Packit 8c24b4
   Copyright (C) 2019 Free Software Foundation, Inc.
Packit 8c24b4
   This file is part of the GNU C Library.
Packit 8c24b4
Packit 8c24b4
   The GNU C Library is free software; you can redistribute it and/or
Packit 8c24b4
   modify it under the terms of the GNU Lesser General Public License as
Packit 8c24b4
   published by the Free Software Foundation; either version 2.1 of the
Packit 8c24b4
   License, or (at your option) any later version.
Packit 8c24b4
Packit 8c24b4
   The GNU C Library is distributed in the hope that it will be useful,
Packit 8c24b4
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8c24b4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 8c24b4
   Lesser General Public License for more details.
Packit 8c24b4
Packit 8c24b4
   You should have received a copy of the GNU Lesser General Public
Packit 8c24b4
   License along with the GNU C Library; see the file COPYING.LIB.  If
Packit 8c24b4
   not, see <http://www.gnu.org/licenses/>.  */
Packit 8c24b4
Packit 8c24b4
/* This test does the following:
Packit 8c24b4
   Run ldconfig to create the caches.
Packit 8c24b4
   Corrupt the caches.
Packit 8c24b4
   Run ldconfig again.
Packit 8c24b4
   At each step we verify that ldconfig does not crash.  */
Packit 8c24b4
Packit 8c24b4
#include <stdio.h>
Packit 8c24b4
#include <stdlib.h>
Packit 8c24b4
#include <string.h>
Packit 8c24b4
#include <unistd.h>
Packit 8c24b4
#include <errno.h>
Packit 8c24b4
#include <sys/wait.h>
Packit 8c24b4
#include <ftw.h>
Packit 8c24b4
#include <stdint.h>
Packit 8c24b4
Packit 697b60
#include <support/capture_subprocess.h>
Packit 8c24b4
#include <support/check.h>
Packit 8c24b4
#include <support/support.h>
Packit 8c24b4
#include <support/xunistd.h>
Packit 8c24b4
Packit 8c24b4
#include <dirent.h>
Packit 8c24b4
Packit 8c24b4
static int
Packit 8c24b4
display_info (const char *fpath, const struct stat *sb,
Packit 8c24b4
              int tflag, struct FTW *ftwbuf)
Packit 8c24b4
{
Packit 8c24b4
  printf ("info: %-3s %2d %7jd   %-40s %d %s\n",
Packit 8c24b4
          (tflag == FTW_D) ? "d" : (tflag == FTW_DNR) ? "dnr" :
Packit 8c24b4
          (tflag == FTW_DP) ? "dp" : (tflag == FTW_F) ? "f" :
Packit 8c24b4
          (tflag == FTW_NS) ? "ns" : (tflag == FTW_SL) ? "sl" :
Packit 8c24b4
          (tflag == FTW_SLN) ? "sln" : "???",
Packit 8c24b4
          ftwbuf->level, (intmax_t) sb->st_size,
Packit 8c24b4
          fpath, ftwbuf->base, fpath + ftwbuf->base);
Packit 8c24b4
  /* To tell nftw to continue.  */
Packit 8c24b4
  return 0;
Packit 8c24b4
}
Packit 8c24b4
Packit 697b60
static void
Packit 697b60
execv_wrapper (void *args)
Packit 697b60
{
Packit 697b60
  char **argv = args;
Packit 697b60
Packit 697b60
  execv (argv[0], argv);
Packit 697b60
  FAIL_EXIT1 ("execv: %m");
Packit 697b60
}
Packit 697b60
Packit 8c24b4
/* Run ldconfig with a corrupt aux-cache, in particular we test for size
Packit 8c24b4
   truncation that might happen if a previous ldconfig run failed or if
Packit 8c24b4
   there were storage or power issues while we were writing the file.
Packit 8c24b4
   We want ldconfig not to crash, and it should be able to do so by
Packit 8c24b4
   computing the expected size of the file (bug 18093).  */
Packit 8c24b4
static int
Packit 8c24b4
do_test (void)
Packit 8c24b4
{
Packit 8c24b4
  char *prog = xasprintf ("%s/ldconfig", support_install_rootsbindir);
Packit 697b60
  char *args[] = { prog, NULL };
Packit 8c24b4
  const char *path = "/var/cache/ldconfig/aux-cache";
Packit 8c24b4
  struct stat64 fs;
Packit 8c24b4
  long int size, new_size, i;
Packit 8c24b4
Packit 8c24b4
  /* Create the needed directories. */
Packit 8c24b4
  xmkdirp ("/var/cache/ldconfig", 0777);
Packit 8c24b4
Packit 697b60
  /* Run ldconfig first to generate the aux-cache.  */
Packit 697b60
  struct support_capture_subprocess result;
Packit 697b60
  result = support_capture_subprocess (execv_wrapper, args);
Packit 697b60
  support_capture_subprocess_check (&result, "execv", 0, sc_allow_none);
Packit 697b60
  support_capture_subprocess_free (&result);
Packit 697b60
Packit 697b60
  xstat (path, &fs);
Packit 697b60
Packit 697b60
  size = fs.st_size;
Packit 697b60
  /* Run 3 tests, each truncating aux-cache shorter and shorter.  */
Packit 697b60
  for (i = 3; i > 0; i--)
Packit 8c24b4
    {
Packit 697b60
      new_size = size * i / 4;
Packit 697b60
      if (truncate (path, new_size))
Packit 697b60
        FAIL_EXIT1 ("truncation failed: %m");
Packit 697b60
      if (nftw (path, display_info, 1000, 0) == -1)
Packit 697b60
        FAIL_EXIT1 ("nftw failed.");
Packit 697b60
Packit 697b60
      /* Verify that ldconfig can run with a truncated
Packit 697b60
         aux-cache and doesn't crash.  */
Packit 697b60
      struct support_capture_subprocess result;
Packit 697b60
      result = support_capture_subprocess (execv_wrapper, args);
Packit 697b60
      support_capture_subprocess_check (&result, "execv", 0, sc_allow_none);
Packit 697b60
      support_capture_subprocess_free (&result);
Packit 8c24b4
    }
Packit 8c24b4
Packit 8c24b4
  free (prog);
Packit 8c24b4
  return 0;
Packit 8c24b4
}
Packit 8c24b4
Packit 8c24b4
#include <support/test-driver.c>