From patchwork Tue Sep 26 00:33:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 23143 Received: (qmail 38471 invoked by alias); 26 Sep 2017 00:33:27 -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 38460 invoked by uid 89); 26 Sep 2017 00:33:25 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.4 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy=verbatim, Hx-languages-length:3672 X-HELO: mail-io0-f172.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=Q47WUwuB4RYtgejq7PWr51tjdielFXjAMWgEl3H23wk=; b=OlICavoDA42SH2TNKHlfXBkIu8nm3XxYt6w4I2s5ShHF27gGxEemdODYl4DHlIYisJ R11+MWaUufz1jekq4TOAe/aTgjvkJSRlsgzEpcFMMi+CY63q/oydBkDKPDEbmJ+ueTLa J9Wg9es/r65fVWHR08FbY16BPRPhOjXpT05ur+wc/GD5lYdb9n0d+4nBh3Sd0o5wltQN bwj0kxaAmLYfd7bUZjpkLmHklIeOYJSwdV273zjrCBCL+zaQw88P9sSMSRBoQ5icJ+82 z6EpnDrkXNPYCpZg5VQye8FWjTx+u4trxYr5sq2XhZbOeXCWFk2KIQe0LpaZ/7zOI+us 0yZA== X-Gm-Message-State: AHPjjUi2S57emgDI+p0ljHKzagRIznI8z6OCyB2+phb1VICrLedp55AT i5prR8DAjqGqz5dnjYOkiTtbqFsi X-Google-Smtp-Source: AOwi7QD1KlymVxglozT72W8YcHURoW0Y0dnIqY4hx/snfVO8/0XVUaOSil4sqyVIpsBH60oALJzNQA== X-Received: by 10.107.17.132 with SMTP id 4mr12078416ior.165.1506386002017; Mon, 25 Sep 2017 17:33:22 -0700 (PDT) Date: Mon, 25 Sep 2017 17:33:14 -0700 From: "H.J. Lu" To: GNU C Library Subject: [PATCH] Skip PT_DYNAMIC segment if its p_filesz == 0 [BZ #22101] Message-ID: <20170926003314.GA18765@gmail.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.8.3 (2017-05-23) ELF object generated with "objcopy --only-keep-debug" has Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align DYNAMIC 0x0+e28 0x0+200e40 0x0+200e40 0x0+ 0x0+1a0 RW 0x8 with 0 file size. ld.so should skip such PT_DYNAMIC segments. Tested on x86-64. OK for master? H.J. --- [BZ #22101] * elf/Makefile (tests): Add tst-debug1. ($(objpfx)tst-debug1): New. ($(objpfx)tst-debug1.out): Likewise. ($(objpfx)tst-debug1mod1.so): Likewise. * elf/dl-load.c (_dl_map_object_from_fd): Skip PT_DYNAMIC segment if its p_filesz == 0. * elf/tst-debug1.c: New file. --- elf/Makefile | 9 ++++++++- elf/dl-load.c | 7 +++++-- elf/tst-debug1.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 elf/tst-debug1.c diff --git a/elf/Makefile b/elf/Makefile index 7cf959aabd..e21f37e30b 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -181,7 +181,8 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \ tst-initorder tst-initorder2 tst-relsort1 tst-null-argv \ tst-tlsalign tst-tlsalign-extern tst-nodelete-opened \ tst-nodelete2 tst-audit11 tst-audit12 tst-dlsym-error tst-noload \ - tst-latepthread tst-tls-manydynamic tst-nodelete-dlclose + tst-latepthread tst-tls-manydynamic tst-nodelete-dlclose \ + tst-debug1 # reldep9 tests-internal += loadtest unload unload2 circleload1 \ neededtest neededtest2 neededtest3 neededtest4 \ @@ -1417,3 +1418,9 @@ tst-env-setuid-ENV = MALLOC_CHECK_=2 MALLOC_MMAP_THRESHOLD_=4096 \ LD_HWCAP_MASK=0x1 tst-env-setuid-tunables-ENV = \ GLIBC_TUNABLES=glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096 + +$(objpfx)tst-debug1: $(libdl) +$(objpfx)tst-debug1.out: $(objpfx)tst-debug1mod1.so + +$(objpfx)tst-debug1mod1.so: $(objpfx)testobj1.so + $(OBJCOPY) --only-keep-debug $< $@ diff --git a/elf/dl-load.c b/elf/dl-load.c index a067760cc6..261ec997c8 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -1052,8 +1052,11 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, segments are mapped in. We record the addresses it says verbatim, and later correct for the run-time load address. */ case PT_DYNAMIC: - l->l_ld = (void *) ph->p_vaddr; - l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn)); + if (ph->p_filesz) + { + l->l_ld = (void *) ph->p_vaddr; + l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn)); + } break; case PT_PHDR: diff --git a/elf/tst-debug1.c b/elf/tst-debug1.c new file mode 100644 index 0000000000..aa2f4886bf --- /dev/null +++ b/elf/tst-debug1.c @@ -0,0 +1,34 @@ +/* Unit test for dlopen on ELF object from "objcopy --only-keep-debug". + Copyright (C) 2017 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; if not, see + . */ + +#include +#include + +static int +do_test (void) +{ + void *h = dlopen ("tst-debug1mod1.so", RTLD_LAZY); + if (h != NULL) + { + puts ("shouldn't load tst-debug1mod1.so"); + return 1; + } + return 0; +} + +#include