From patchwork Wed Jul 15 08:06:37 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 495507 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 8657F140284 for ; Wed, 15 Jul 2015 18:07:00 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b=gYUkGL+V; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; q=dns; s= default; b=Kjy3Bv2Q31MVAZz9SY3S945en99fMunCxYkWhKE+xFmHNVLYURpJt 3SHt0a3iqDej4bSuQB8qTmtvFwEyHrJTF2rrDbns624slZFVxnzw0JFv294ZEN69 xJlzjfSuU+jOzaaWIMktliebaCN6EbQxBy8qsZp7C7VC1VKImBd5DE= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; s=default; bh=HZ0VCnO/7x4D50Vn4EzoRHZqilM=; b=gYUkGL+VLF65QKato9VWXn2qlq0r nnNJpsCU+VKNdDzczCcOYlz6MqfVEvCPyV4SKlnPO0B4RgMH3/r26r0XzPk7HN+g KpUcSJR8BioYf/0MDQUOpqdOWF1eR49WgRe4455SFv/hE1JKOIN1UD8jmHXFZ1Jm I3r+vF/tlLumxN4= Received: (qmail 103265 invoked by alias); 15 Jul 2015 08:06:52 -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 103252 invoked by uid 89); 15 Jul 2015 08:06:51 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL, BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com From: "Siddhesh Poyarekar" To: libc-alpha@sourceware.org Subject: [PATCH] Set NODELETE flag when opening already open objects with RTLD_NODELETE Date: Wed, 15 Jul 2015 13:36:37 +0530 Message-Id: <1436947597-3800-1-git-send-email-siddhesh@redhat.com> The DF_1_NODELETE flag is set too late when opening a DSO, due to which, if a DSO is already open, subsequently opening it with RTLD_NODELETE fails to set the DF_1_NODELETE flag. This patch fixes this by setting the flag immediately after bumping the opencount. Verified on x86_64. Siddhesh [BZ #18676] * elf/tst-nodelete-opened.c: New test case. * elf/tst-nodelete-opened-lib.c: New test case module. * elf/Makefile (tests, modules-names): Use them. * elf/dl-open.c (dl_open_worker): Set DF_1_NODELETE flag early. --- elf/Makefile | 6 +++-- elf/dl-open.c | 11 ++++---- elf/tst-nodelete-opened-lib.c | 19 ++++++++++++++ elf/tst-nodelete-opened.c | 58 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 elf/tst-nodelete-opened-lib.c create mode 100644 elf/tst-nodelete-opened.c diff --git a/elf/Makefile b/elf/Makefile index 90d5058..4ceeaf8 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -148,7 +148,7 @@ tests += loadtest restest1 preloadtest loadfail multiload origtest resolvfail \ tst-unique1 tst-unique2 $(if $(CXX),tst-unique3 tst-unique4 \ tst-nodelete) \ tst-initorder tst-initorder2 tst-relsort1 tst-null-argv \ - tst-ptrguard1 tst-tlsalign tst-tlsalign-extern + tst-ptrguard1 tst-tlsalign tst-tlsalign-extern tst-nodelete-opened # reldep9 ifeq ($(build-hardcoded-path-in-tests),yes) tests += tst-dlopen-aout @@ -218,7 +218,7 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \ tst-initorder2d \ tst-relsort1mod1 tst-relsort1mod2 tst-array2dep \ tst-array5dep tst-null-argv-lib \ - tst-tlsalign-lib + tst-tlsalign-lib tst-nodelete-opened-lib ifeq (yes,$(have-protected-data)) modules-names += tst-protected1moda tst-protected1modb tests += tst-protected1a tst-protected1b @@ -525,6 +525,8 @@ $(objpfx)tst-initordera4.so: $(objpfx)tst-initordera3.so $(objpfx)tst-initorder: $(objpfx)tst-initordera4.so $(objpfx)tst-initordera1.so $(objpfx)tst-initorderb2.so $(objpfx)tst-null-argv: $(objpfx)tst-null-argv-lib.so $(objpfx)tst-tlsalign: $(objpfx)tst-tlsalign-lib.so +$(objpfx)tst-nodelete-opened.out: $(objpfx)tst-nodelete-opened-lib.so +$(objpfx)tst-nodelete-opened: $(libdl) $(objpfx)tst-tlsalign-extern: $(objpfx)tst-tlsalign-vars.o $(objpfx)tst-tlsalign-extern-static: $(objpfx)tst-tlsalign-vars.o diff --git a/elf/dl-open.c b/elf/dl-open.c index 027c1e0..2db1c02 100644 --- a/elf/dl-open.c +++ b/elf/dl-open.c @@ -226,6 +226,12 @@ dl_open_worker (void *a) args->map = new = _dl_map_object (call_map, file, lt_loaded, 0, mode | __RTLD_CALLMAP, args->nsid); + /* Mark the object as not deletable if the RTLD_NODELETE flags was passed. + Do this early so that we don't skip marking the object if it was + already loaded. */ + if (__glibc_unlikely (mode & RTLD_NODELETE)) + new->l_flags_1 |= DF_1_NODELETE; + /* If the pointer returned is NULL this means the RTLD_NOLOAD flag is set and the object is not already loaded. */ if (new == NULL) @@ -564,11 +570,6 @@ TLS generation counter wrapped! Please report this.")); /* It failed. */ return; - /* Mark the object as not deletable if the RTLD_NODELETE flags was - passed. */ - if (__glibc_unlikely (mode & RTLD_NODELETE)) - new->l_flags_1 |= DF_1_NODELETE; - #ifndef SHARED /* We must be the static _dl_open in libc.a. A static program that has loaded a dynamic object now has competition. */ diff --git a/elf/tst-nodelete-opened-lib.c b/elf/tst-nodelete-opened-lib.c new file mode 100644 index 0000000..994702a --- /dev/null +++ b/elf/tst-nodelete-opened-lib.c @@ -0,0 +1,19 @@ +/* Verify that objects opened with RTLD_NODELETE are not unloaded - the DSO. + Copyright (C) 2015 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 + . */ + +int foo_var = 42; diff --git a/elf/tst-nodelete-opened.c b/elf/tst-nodelete-opened.c new file mode 100644 index 0000000..2e54156 --- /dev/null +++ b/elf/tst-nodelete-opened.c @@ -0,0 +1,58 @@ +/* Verify that an already opened DSO opened agained with RTLD_NODELETE actually + sets the NODELETE flag. + + Copyright (C) 2015 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 + +int +do_test (void) +{ + void *h1 = dlopen ("$ORIGIN/tst-nodelete-opened-lib.so", RTLD_LAZY); + if (h1 == NULL) + { + printf ("h1: failed to open DSO: %s\n", dlerror ()); + return 1; + } + + void *h2 = dlopen ("$ORIGIN/tst-nodelete-opened-lib.so", + RTLD_LAZY | RTLD_NODELETE); + if (h2 == NULL) + { + printf ("h2: failed to open DSO: %s\n", dlerror ()); + return 1; + } + + int *foo = dlsym (h2, "foo_var"); + if (foo == NULL) + { + printf ("failed to load symbol foo_var: %s\n", dlerror ()); + return 1; + } + + dlclose (h1); + dlclose (h2); + + printf ("foo == %d\n", *foo); + + return 0; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c"