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

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