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

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