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

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