From patchwork Mon Apr 15 18:18:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= X-Patchwork-Id: 32294 Received: (qmail 65326 invoked by alias); 15 Apr 2019 18:18:38 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 65318 invoked by uid 89); 15 Apr 2019 18:18:38 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=Verified X-HELO: mail-wm1-f67.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=V5VCRfexF0NBgTwXN7iafkwKLy9rOhKayxb690NpXvE=; b=bn2X+CA7pvfSlzLxtexcWkRtqWb8Z4k6jDtDdF1i56z9dRfkuQs9raRxaN1wcpxtGP i6ZK1mDfhBI8Z4B1ZefelHJNEuoldkvZ4wwfuH1jyHazE49ZxoYIxB1e5voAxaUYIyi7 7Jk3DgkYp70BmVlMqO4MSFUvPlfSJS1Ut1yIydm6TsJhbRI0iofE0OATTEtdSK+royTD f6nNcO5U3D5mXlMNA6GBSkKQCg3Xdf3OL6kGmocXl0aMqAae6/bpOfmVAQjYkFG4PSSk omL1AtaR8j0xlLc7kiq5fOuDVaDje/HhLDPp5CME/YQ8Jl/nuMEknt1kMB+IQ7A6IaEe /GDA== Return-Path: From: =?UTF-8?Q?Alexandra_H=C3=A1jkov=C3=A1?= To: libc-alpha@sourceware.org Cc: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Subject: [PATCH] elf: Add tst-ldconfig-bad-aux-cache test [BZ #18093] Date: Mon, 15 Apr 2019 20:18:21 +0200 Message-Id: <20190415181821.17616-1-ahajkova@redhat.com> MIME-Version: 1.0 From: Alexandra Hájková This test corrupts /var/cache/ldconfig/aux-cache and executes ldconfig to check it will not segfault using the corrupted aux_cache. The test uses the test-in-container framework. Verified no regressions on x86_64. ChangeLog: 2019-04-15 Alexandra Hajkova * elf/Makefile (test-container): Add tst-ldconfig-bad-aux-cache. * elf/tst-ldconfig-bad-aux-cache.c: New file. * elf/tst-ldconfig_aux-cache.root: New directory. * elf/tst-ldconfig_aux-cache.root/var: Likewise. * elf/tst-ldconfig_aux-cache.root/var/cache: Likewise. * elf/tst-ldconfig_aux-cache.root/var/cache/.gitignore: New file. --- elf/Makefile | 3 + elf/tst-ldconfig-bad-aux-cache.c | 123 ++++++++++++++++++ .../var/cache/.gitignore | 0 3 files changed, 126 insertions(+) create mode 100644 elf/tst-ldconfig-bad-aux-cache.c create mode 100644 elf/tst-ldconfig-bad-aux-cache.root/var/cache/.gitignore diff --git a/elf/tst-ldconfig-bad-aux-cache.root/var/cache/.gitignore b/elf/tst-ldconfig-bad-aux-cache.root/var/cache/.gitignore new file mode 100644 index 0000000000..e69de29bb2 diff --git a/elf/Makefile b/elf/Makefile index 310a37cc13..35052b2768 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -155,6 +155,9 @@ tests-static-internal := tst-tls1-static tst-tls2-static \ CRT-tst-tls1-static-non-pie := $(csu-objpfx)crt1.o tst-tls1-static-non-pie-no-pie = yes +tests-container = \ + tst-ldconfig-bad-aux-cache + tests := tst-tls9 tst-leaks1 \ tst-array1 tst-array2 tst-array3 tst-array4 tst-array5 \ tst-auxv diff --git a/elf/tst-ldconfig-bad-aux-cache.c b/elf/tst-ldconfig-bad-aux-cache.c new file mode 100644 index 0000000000..d7e591b9ca --- /dev/null +++ b/elf/tst-ldconfig-bad-aux-cache.c @@ -0,0 +1,123 @@ +/* Test ldconfig does not segfault when aux-cache is corrupted (Bug 18093). + Copyright (C) 2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; see the file COPYING.LIB. If + not, see . */ + +/* This test does the following: + Run ldconfig to create the caches. + Corrupt the caches. + Run ldconfig again. + At each step we verify that ldconfig does not crash. */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +static int +display_info (const char *fpath, const struct stat *sb, + int tflag, struct FTW *ftwbuf) +{ + printf ("%-3s %2d %7jd %-40s %d %s\n", + (tflag == FTW_D) ? "d" : (tflag == FTW_DNR) ? "dnr" : + (tflag == FTW_DP) ? "dp" : (tflag == FTW_F) ? "f" : + (tflag == FTW_NS) ? "ns" : (tflag == FTW_SL) ? "sl" : + (tflag == FTW_SLN) ? "sln" : "???", + ftwbuf->level, (intmax_t) sb->st_size, + fpath, ftwbuf->base, fpath + ftwbuf->base); + /* To tell nftw() to continue */ + return 0; +} + +/* Run ldconfig with a corrupt aux-cache, in particular we test for size + truncation that might happen if a previous ldconfig run failed or if + there were storage or power issues while we were writing the file. + We want ldconfig not to crash, and it should be able to do so by + computing the expected size of the file (bug 18093). */ +static int +do_test (void) +{ + char *args[] = { (char *) "/sbin/ldconfig", NULL }; + struct stat fs; + long int size, new_size, i; + int status; + pid_t pid = fork (); + + /* Run ldconfig fist to generate the aux-cache. */ + if (!pid) + { + execv (args[0], args); + } + else + { + if (pid) + { + waitpid (pid, &status, 0); + if (!(WIFEXITED (status))) + FAIL_EXIT1 ("ldconfig was aborted"); + if (stat ("/var/cache/ldconfig/aux-cache", &fs) < 0) + { + if (errno == ENOENT) + FAIL_EXIT1 ("aux-cache does not exist\n"); + else + FAIL_EXIT1 ("Failed to open aux-cache."); + return -1; + } + + size = fs.st_size; + /* Run 3 tests, each truncating aux-cache shorter and shorter. */ + for (i = 3; i > 0; i--) + { + new_size = size * i / 4; + truncate ("/var/cache/ldconfig/aux-cache", new_size); + if (errno) + FAIL_EXIT1 ("Truncation failed."); + if (nftw + ("/var/cache/ldconfig/aux-cache", display_info, 1000, + 0) == -1) + { + FAIL_EXIT1 ("nftw failed."); + return -1; + } + + pid = fork (); + /* Verify that ldconfig can run with a truncated + aux-cache and doesn't crash. */ + if (!pid) + { + execv (args[0], args); + } + else + { + waitpid (pid, &status, 0); + if (!(WIFEXITED (status))) + FAIL_EXIT1 ("ldconfig exited with non-zero status"); + } + + } + } + } + + return 0; +} + +#include