From patchwork Wed Jul 7 18:26:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 44209 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BA8A5396E41A for ; Wed, 7 Jul 2021 18:27:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BA8A5396E41A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1625682442; bh=AKq2qbd99jzUNis23hTpUXHtG6G+oFwa7eW+PcHpCJE=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=X8PyQXphCNpjuHK+6/xek2FqlyI15Leum+Clzl7mwQ96XXQse0y+YUSkDGc/6jwCb A9wtdbqlq69pveut7o318O+MYbsGeOvfcviYkdahQE/Rvc5zD8nmLBBsLBpfHZy/dT LxOjQbQ+Q+NnESC0VEqSva59+UnZHsCf/fc9pnTc= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by sourceware.org (Postfix) with ESMTPS id B6970386EC2F for ; Wed, 7 Jul 2021 18:26:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B6970386EC2F Received: by mail-pg1-x531.google.com with SMTP id y4so699848pgl.10 for ; Wed, 07 Jul 2021 11:26:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=AKq2qbd99jzUNis23hTpUXHtG6G+oFwa7eW+PcHpCJE=; b=hN041lAsj2M49KG/ElxqzxBAzzcIi6BfzBiDHmoB2MM9CkPrVmQMMv4H16k6BN8rpa 6RKM/a7DzSlGbIm9zCDIazW4BYBtjcB0qfGWG9fydFOFbka1ZV2d7FL1berBhE7Gn6f2 7bUn59h2ykYMX+kLEI41r5ZKrSn9aaKYQmqU9kV/kSwjg4RTQzXE9uot2Z5/PtuEz55q XMvZVe383rwTGLTLWPXt2Ib5cBDqH9Stb+LcTNap7dlJkgylfNMlEY15DdxBz/vK3JvH W+bQ29A9rIMwRgpXUWgU7xHdqQb6Tan7sy0n+FcHA6iohwINE3TO5ur2IwoP9pQvKaJ4 qMwA== X-Gm-Message-State: AOAM530XlYf5Aqw6Zf/cm91vlSb/48QIn+PxUPENoOtclRE7ytwBI4Nm ZtUZ/SDLV1at/L0N27ZJZKDWZRazY5lcbw== X-Google-Smtp-Source: ABdhPJwvg3VuuWmu07mIZdvbrWpJh7UVHlBoYebCgllZlX1VN4WHG8VfqGOthZ2N3YanHfKBznPIFw== X-Received: by 2002:a05:6a00:1508:b029:30a:2b2:b2ea with SMTP id q8-20020a056a001508b029030a02b2b2eamr27237497pfu.30.1625682377617; Wed, 07 Jul 2021 11:26:17 -0700 (PDT) Received: from birita.. ([177.194.59.218]) by smtp.gmail.com with ESMTPSA id fv8sm18868662pjb.21.2021.07.07.11.26.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 11:26:17 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 1/5] elf: Fix audit regression Date: Wed, 7 Jul 2021 15:26:06 -0300 Message-Id: <20210707182610.3940620-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210707182610.3940620-1-adhemerval.zanella@linaro.org> References: <20210707182610.3940620-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLACK autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Cc: John Mellor-Crummey Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Commit 03e187a41d9 added a regression when an audit module does not have libc as DT_NEEDED (although unusual it is possible). Checked on x86_64-linux-gnu. --- elf/Makefile | 12 +++++++++++- elf/dl-open.c | 2 +- elf/tst-audit17.c | 25 +++++++++++++++++++++++++ elf/tst-auditmod17.c | 24 ++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 elf/tst-audit17.c create mode 100644 elf/tst-auditmod17.c diff --git a/elf/Makefile b/elf/Makefile index b1e01d9516..5214196de6 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -219,7 +219,7 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \ tst-dlopen-self tst-auditmany tst-initfinilazyfail tst-dlopenfail \ tst-dlopenfail-2 \ tst-filterobj tst-filterobj-dlopen tst-auxobj tst-auxobj-dlopen \ - tst-audit14 tst-audit15 tst-audit16 \ + tst-audit14 tst-audit15 tst-audit16 tst-audit17 \ tst-single_threaded tst-single_threaded-pthread \ tst-tls-ie tst-tls-ie-dlmopen argv0test \ tst-glibc-hwcaps tst-glibc-hwcaps-prepend tst-glibc-hwcaps-mask \ @@ -361,6 +361,7 @@ modules-names += tst-gnu2-tls1mod $(objpfx)tst-gnu2-tls1: $(objpfx)tst-gnu2-tls1mod.so tst-gnu2-tls1mod.so-no-z-defs = yes CFLAGS-tst-gnu2-tls1mod.c += -mtls-dialect=gnu2 + endif ifeq (yes,$(have-protected-data)) modules-names += tst-protected1moda tst-protected1modb @@ -1467,6 +1468,15 @@ $(objpfx)tst-auditlogmod-3.so: $(libsupport) $(objpfx)tst-audit16.out: \ $(objpfx)tst-auditlogmod-1.so $(objpfx)tst-auditlogmod-2.so \ $(objpfx)tst-auditlogmod-3.so +$(objpfx)tst-audit17.out: $(objpfx)tst-auditmod17.so +# The test check if a audit library without libc.so on DT_NEEDED works as +# intended, so it uses an explicit link rule. +$(objpfx)tst-auditmod17.so: $(objpfx)tst-auditmod17.os + $(CC) -nostdlib -nostartfiles -shared -o $@.new \ + $(filter-out $(map-file),$^) + $(call after-link,$@.new) + mv -f $@.new $@ +tst-audit17-ENV = LD_AUDIT=$(objpfx)tst-auditmod17.so # tst-sonamemove links against an older implementation of the library. LDFLAGS-tst-sonamemove-linkmod1.so = \ diff --git a/elf/dl-open.c b/elf/dl-open.c index a066f39bd0..66ec9d7ed5 100644 --- a/elf/dl-open.c +++ b/elf/dl-open.c @@ -771,7 +771,7 @@ dl_open_worker (void *a) { struct link_map *libc_map = GL(dl_ns)[args->nsid].libc_map; #ifdef SHARED - bool initial = libc_map->l_ns == LM_ID_BASE; + bool initial = libc_map != NULL ? libc_map->l_ns == LM_ID_BASE : false; #else /* In the static case, there is only one namespace, but it contains a secondary libc (the primary libc is statically diff --git a/elf/tst-audit17.c b/elf/tst-audit17.c new file mode 100644 index 0000000000..92986699d4 --- /dev/null +++ b/elf/tst-audit17.c @@ -0,0 +1,25 @@ +/* Check DT_AUDIT with audit not linked against libc. + Copyright (C) 2021 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 + . */ + +static int +do_test (void) +{ + return 0; +} + +#include diff --git a/elf/tst-auditmod17.c b/elf/tst-auditmod17.c new file mode 100644 index 0000000000..762de376e2 --- /dev/null +++ b/elf/tst-auditmod17.c @@ -0,0 +1,24 @@ +/* Check DT_AUDIT with audit not linked against libc. + Copyright (C) 2021 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 + . */ + +unsigned int +la_version (unsigned int version) +{ + return version; +} + From patchwork Wed Jul 7 18:26:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 44210 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D303D396E837 for ; Wed, 7 Jul 2021 18:29:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D303D396E837 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1625682594; bh=cs2KKroWMWXktGb+fCfLLskmj8w6pVKkpkwu7esfFxw=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=JW3mWJQh+QIwFYmEucbRnGWoGMl5DomfVO1GdPp09FE45Gb5toihea/wAizSeAW8Q R1Yq7AtoA0unrDD24iDAHFPDRjp6I6OwQZkuiQlmIxaE7skIxergz9lRaruL1/aWDB xMXZwlrBWCL2LDTnfNl5YPpKQuVgctEtRdU25rDY= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by sourceware.org (Postfix) with ESMTPS id 0A0B7396E05C for ; Wed, 7 Jul 2021 18:29:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0A0B7396E05C Received: by mail-pf1-x42e.google.com with SMTP id a127so2986934pfa.10 for ; Wed, 07 Jul 2021 11:29:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cs2KKroWMWXktGb+fCfLLskmj8w6pVKkpkwu7esfFxw=; b=Lur4ZarYeP1BoLOTgBhZuLI1Lgk2USuM1wmeaktxs/tTbdxVq7SMoe0r+DVV5bQ3ax 9y7CQn8DrFY4tLl8Fsk854UXfb8o6gweUwKTlKUZkX5EacuIW49aArzWFdAPiV0Pj9UQ YIv3gw0xV3wwmnpiR7oLrb5/wrhEVZ9A3EpDKdv6H2viCMXQUi+mOxtIcDXfjp4ihlCn e3uN5JROLQaG2ROMw5NBAzofBaa6hYM1l9XC8ulg3ZD4EtJ1YH+9LzFw+Jix1kyP8nd9 ay/87KvB5jCIiNEA48XX37n2KCPdm+Tz3TB0BLaTna5/d7oLzbxkcqgz1JosqjAEtnT0 uJ3w== X-Gm-Message-State: AOAM530UryLjq1FozT6lJeuFOZZ9aCoxR2jwBQNbpvqcoEawKZAD6eTC 5YHgO3hVX4wFlzrfzZQNRxxIj5i7TSg3JQ== X-Google-Smtp-Source: ABdhPJxLA8+7irv7OJA8LfPLu3A5zqLYjYn02DBku5JDt5c3VAlPtJpsXHmvXzVxuh/qj82Sxka+Lw== X-Received: by 2002:aa7:8491:0:b029:310:927a:df90 with SMTP id u17-20020aa784910000b0290310927adf90mr26884004pfn.46.1625682570920; Wed, 07 Jul 2021 11:29:30 -0700 (PDT) Received: from birita.. ([177.194.59.218]) by smtp.gmail.com with ESMTPSA id fv8sm18868662pjb.21.2021.07.07.11.29.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 11:29:30 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 2/5] elf: Avoid unnecessary slowdown from profiling with audit (BZ#15533) Date: Wed, 7 Jul 2021 15:26:07 -0300 Message-Id: <20210707182610.3940620-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210707182610.3940620-1-adhemerval.zanella@linaro.org> References: <20210707182610.3940620-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLACK autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Cc: Alexander Monakov , John Mellor-Crummey Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" The rtld-audit interfaces introduce a slowdown due to enabling profiling instrumentation (as if LD_AUDIT implied LD_PROFILE). However, instrumenting is only necessary if one of audit libraries provides PLT (la_plt{enter,exit} symbols) or symbol bynding (la_symbind{32,64})hooks. Otherwise, the slowdown can be avoided. The following patch adjusts the logic that enables profiling to iterate over all audit modules and check if any of those provides a PLT hook. Co-authored-by: Alexander Monakov --- elf/Makefile | 13 +++++++++++- elf/dl-reloc.c | 13 +++++++++++- elf/rtld.c | 8 +------- elf/tst-audit18a.c | 39 +++++++++++++++++++++++++++++++++++ elf/tst-audit18b.c | 30 +++++++++++++++++++++++++++ elf/tst-audit18bmod.c | 22 ++++++++++++++++++++ elf/tst-auditmod18a.c | 25 ++++++++++++++++++++++ elf/tst-auditmod18b.c | 48 +++++++++++++++++++++++++++++++++++++++++++ include/link.h | 2 ++ 9 files changed, 191 insertions(+), 9 deletions(-) create mode 100644 elf/tst-audit18a.c create mode 100644 elf/tst-audit18b.c create mode 100644 elf/tst-audit18bmod.c create mode 100644 elf/tst-auditmod18a.c create mode 100644 elf/tst-auditmod18b.c diff --git a/elf/Makefile b/elf/Makefile index 5214196de6..a26f5ce320 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -219,7 +219,8 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \ tst-dlopen-self tst-auditmany tst-initfinilazyfail tst-dlopenfail \ tst-dlopenfail-2 \ tst-filterobj tst-filterobj-dlopen tst-auxobj tst-auxobj-dlopen \ - tst-audit14 tst-audit15 tst-audit16 tst-audit17 \ + tst-audit14 tst-audit15 tst-audit16 tst-audit17 tst-audit18a \ + tst-audit18b \ tst-single_threaded tst-single_threaded-pthread \ tst-tls-ie tst-tls-ie-dlmopen argv0test \ tst-glibc-hwcaps tst-glibc-hwcaps-prepend tst-glibc-hwcaps-mask \ @@ -294,6 +295,7 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \ tst-unique1mod1 tst-unique1mod2 \ tst-unique2mod1 tst-unique2mod2 \ tst-auditmod9a tst-auditmod9b \ + tst-auditmod18a tst-audit18bmod tst-auditmod18b \ $(if $(CXX),tst-unique3lib tst-unique3lib2 tst-unique4lib \ tst-nodelete-uniquemod tst-nodelete-rtldmod \ tst-nodelete-zmod \ @@ -1478,6 +1480,15 @@ $(objpfx)tst-auditmod17.so: $(objpfx)tst-auditmod17.os mv -f $@.new $@ tst-audit17-ENV = LD_AUDIT=$(objpfx)tst-auditmod17.so +$(objpfx)tst-audit18a.out: $(objpfx)tst-auditmod18a.so +tst-audit18a-ENV = LD_AUDIT=$(objpfx)tst-auditmod18a.so +$(objpfx)tst-audit18b.out: $(objpfx)tst-auditmod18b.so \ + $(objpfx)tst-audit18bmod.so +$(objpfx)tst-audit18b: $(objpfx)tst-audit18bmod.so +LDFLAGS-tst-audit18bmod.so = -Wl,-z,now +LDFLAGS-tst-audit18b = -Wl,-z,now +tst-audit18b-ENV = LD_AUDIT=$(objpfx)tst-auditmod18b.so + # tst-sonamemove links against an older implementation of the library. LDFLAGS-tst-sonamemove-linkmod1.so = \ -Wl,--version-script=tst-sonamemove-linkmod1.map \ diff --git a/elf/dl-reloc.c b/elf/dl-reloc.c index e13a672ade..998cfef099 100644 --- a/elf/dl-reloc.c +++ b/elf/dl-reloc.c @@ -181,7 +181,18 @@ _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[], #ifdef SHARED /* If we are auditing, install the same handlers we need for profiling. */ if ((reloc_mode & __RTLD_AUDIT) == 0) - consider_profiling |= GLRO(dl_audit) != NULL; + { + struct audit_ifaces *afct = GLRO(dl_audit); + for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) + { + /* Profiling is needed only if PLT hooks are provided. */ + if (afct->symbind != NULL + || afct->ARCH_LA_PLTENTER != NULL + || afct->ARCH_LA_PLTEXIT != NULL) + consider_profiling = 1; + afct = afct->next; + } + } #elif defined PROF /* Never use dynamic linker profiling for gprof profiling code. */ # define consider_profiling 0 diff --git a/elf/rtld.c b/elf/rtld.c index fbbd60b446..bf9eb5a33e 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -1014,13 +1014,7 @@ ERROR: audit interface '%s' requires version %d (maximum supported version %d); "la_objsearch\0" "la_objopen\0" "la_preinit\0" -#if __ELF_NATIVE_CLASS == 32 - "la_symbind32\0" -#elif __ELF_NATIVE_CLASS == 64 - "la_symbind64\0" -#else -# error "__ELF_NATIVE_CLASS must be defined" -#endif + LA_SYMBIND "\0" #define STRING(s) __STRING (s) "la_" STRING (ARCH_LA_PLTENTER) "\0" "la_" STRING (ARCH_LA_PLTEXIT) "\0" diff --git a/elf/tst-audit18a.c b/elf/tst-audit18a.c new file mode 100644 index 0000000000..36b781f9be --- /dev/null +++ b/elf/tst-audit18a.c @@ -0,0 +1,39 @@ +/* Check if DT_AUDIT a module without la_plt{enter,exit} symbols does not incur + in profiling (BZ#15533). + Copyright (C) 2021 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 + +/* We interpose the profile resolver and if it is called it means profiling is + enabled. */ +void +_dl_runtime_profile (ElfW(Word) addr) +{ + volatile int *p = NULL; + *p = 0; +} + +static int +do_test (void) +{ + printf ("..."); + return 0; +} + +#include diff --git a/elf/tst-audit18b.c b/elf/tst-audit18b.c new file mode 100644 index 0000000000..21381fe950 --- /dev/null +++ b/elf/tst-audit18b.c @@ -0,0 +1,30 @@ +/* Check if DT_AUDIT a module built with bind-now does trigger la_symbind. + Copyright (C) 2021 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 + . */ + +extern int foo (void); + +int +do_test (void) +{ + foo (); + /* If audit la_symbind() callback is called it will exit the test with + EXIT_SUCCESS. */ + return 1; +} + +#include diff --git a/elf/tst-audit18bmod.c b/elf/tst-audit18bmod.c new file mode 100644 index 0000000000..91f171b5bc --- /dev/null +++ b/elf/tst-audit18bmod.c @@ -0,0 +1,22 @@ +/* Check if DT_AUDIT a module built with bind-now does trigger la_symbind. + Copyright (C) 2021 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 (void) +{ + return 0; +} diff --git a/elf/tst-auditmod18a.c b/elf/tst-auditmod18a.c new file mode 100644 index 0000000000..333db0077c --- /dev/null +++ b/elf/tst-auditmod18a.c @@ -0,0 +1,25 @@ +/* Check if DT_ADIT a module without la_plt{enter,exit} symbols does not incur + in profiling (BZ#15533). + Copyright (C) 2021 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 + . */ + +unsigned int +la_version (unsigned int version) +{ + return version; +} + diff --git a/elf/tst-auditmod18b.c b/elf/tst-auditmod18b.c new file mode 100644 index 0000000000..40ebcd5be2 --- /dev/null +++ b/elf/tst-auditmod18b.c @@ -0,0 +1,48 @@ +/* Check if DT_AUDIT a module built with bind-now does trigger la_symbind. + Copyright (C) 2021 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 +#include + +unsigned int +la_version (unsigned int version) +{ + return version; +} + +unsigned int +la_objopen (struct link_map* map, Lmid_t lmid, uintptr_t* cookie) +{ + return LA_FLG_BINDFROM | LA_FLG_BINDTO; +} + +uintptr_t +#if __ELF_NATIVE_CLASS == 32 +la_symbind32 (Elf32_Sym *sym, unsigned int ndx, uintptr_t *refcook, + uintptr_t *defcook, unsigned int *flags, const char *symname) +#else +la_symbind64 (Elf64_Sym *sym, unsigned int ndx, uintptr_t *refcook, + uintptr_t *defcook, unsigned int *flags, const char *symname) +#endif +{ + /* Filter out glibc own symbols libc.so is not built with -z,now. */ + if (strcmp (symname, "foo") == 0) + exit (EXIT_SUCCESS); + return sym->st_value; +} diff --git a/include/link.h b/include/link.h index 4af16cb596..ebd0f511e2 100644 --- a/include/link.h +++ b/include/link.h @@ -355,8 +355,10 @@ struct auditstate #if __ELF_NATIVE_CLASS == 32 # define symbind symbind32 +# define LA_SYMBIND "la_symbind32" #elif __ELF_NATIVE_CLASS == 64 # define symbind symbind64 +# define LA_SYMBIND "la_symbind64" #else # error "__ELF_NATIVE_CLASS must be defined" #endif From patchwork Wed Jul 7 18:26:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 44211 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7B6E4396E83D for ; Wed, 7 Jul 2021 18:30:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7B6E4396E83D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1625682639; bh=oXjSLxT5riEDki3i2nI0fVeTLNjnUvg8UZQZG6tmv/s=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=n78El5h+jz8stmimSBtTBYldxUt+wqT/F6CcogvMZJdTEYS8wQyRB2BgDF4Mxjv0K UZfv0YIKSGzJIPfPPMo3L4WSrJHPjnRCBfruIXz+vCfh1IHm9UFbT4hR9LLm6X1NH8 4ovpCioeuZNj8PUnAPU3/9qtif8IVxcU76dnfHcY= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by sourceware.org (Postfix) with ESMTPS id EB553396E408 for ; Wed, 7 Jul 2021 18:29:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EB553396E408 Received: by mail-pj1-x1034.google.com with SMTP id x21-20020a17090aa395b029016e25313bfcso2210341pjp.2 for ; Wed, 07 Jul 2021 11:29:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oXjSLxT5riEDki3i2nI0fVeTLNjnUvg8UZQZG6tmv/s=; b=gW4NDnaiHUBurrQCcPf2mR65Jjqw20+1dEyLKAlhUKGxxMcWRddM0bqJqmHXnI9+Oy udJ03hlVcZVDtpBnNWEl/TBKqoSipsiBbRQpSumFwzThb2ky0wtGUrG+5UZIvVLAzahQ XAjtBQvJMCaRf31tvfVlZjCqKQGbyMLaEh1iCjaPTkn4CZju8P+izH9FIwCWz7au+Jqi bB3+Vpugx2tCYuFTYoRba3WtBzPAVpKo05oAW/k9YJiD+rGr37VE6YI3gIJaeLcGfDWC YcV69qZ2ZRnJh7IH5Aj3DtRyyPg2RmoL7repgKltkIsQRkxa3MG8zqn5ZOIlKDkihgZn hf7Q== X-Gm-Message-State: AOAM5308U14whAmbWHpNpaEJ003ZjJ6/NdP/U0WM2SNaMOiP3xSElBoM V+SO3u3j6bg0N2PnQ4GaQaGk5fnymdBQhg== X-Google-Smtp-Source: ABdhPJwLW2LwKaaae1vCaNaIDvsnlIyuwTGOIcF7GwFUsHbkjwfEFrrX9i+GTNW+MKBankc7mL/aMQ== X-Received: by 2002:a17:90a:aa14:: with SMTP id k20mr402801pjq.88.1625682572849; Wed, 07 Jul 2021 11:29:32 -0700 (PDT) Received: from birita.. ([177.194.59.218]) by smtp.gmail.com with ESMTPSA id fv8sm18868662pjb.21.2021.07.07.11.29.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 11:29:32 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 3/5] elf: Add audit tests for modules with TLSDESC Date: Wed, 7 Jul 2021 15:26:08 -0300 Message-Id: <20210707182610.3940620-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210707182610.3940620-1-adhemerval.zanella@linaro.org> References: <20210707182610.3940620-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Cc: John Mellor-Crummey Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" --- elf/Makefile | 13 +++++++ elf/tst-audit-tlsdesc-audit.c | 23 ++++++++++++ elf/tst-audit-tlsdesc-dlopen.c | 67 ++++++++++++++++++++++++++++++++++ elf/tst-audit-tlsdesc.c | 60 ++++++++++++++++++++++++++++++ elf/tst-auditmod-tlsdesc1.c | 41 +++++++++++++++++++++ elf/tst-auditmod-tlsdesc2.c | 33 +++++++++++++++++ 6 files changed, 237 insertions(+) create mode 100644 elf/tst-audit-tlsdesc-audit.c create mode 100644 elf/tst-audit-tlsdesc-dlopen.c create mode 100644 elf/tst-audit-tlsdesc.c create mode 100644 elf/tst-auditmod-tlsdesc1.c create mode 100644 elf/tst-auditmod-tlsdesc2.c diff --git a/elf/Makefile b/elf/Makefile index a26f5ce320..19f885679a 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -364,6 +364,19 @@ $(objpfx)tst-gnu2-tls1: $(objpfx)tst-gnu2-tls1mod.so tst-gnu2-tls1mod.so-no-z-defs = yes CFLAGS-tst-gnu2-tls1mod.c += -mtls-dialect=gnu2 +tests += tst-audit-tlsdesc tst-audit-tlsdesc-dlopen +modules-names += tst-auditmod-tlsdesc1 tst-auditmod-tlsdesc2 tst-audit-tlsdesc-audit +$(objpfx)tst-audit-tlsdesc: $(objpfx)tst-auditmod-tlsdesc1.so \ + $(objpfx)tst-auditmod-tlsdesc2.so \ + $(shared-thread-library) +CFLAGS-tst-auditmod-tlsdesc1.c += -mtls-dialect=gnu2 +CFLAGS-tst-auditmod-tlsdesc2.c += -mtls-dialect=gnu2 +$(objpfx)tst-audit-tlsdesc-dlopen: $(shared-thread-library) +$(objpfx)tst-audit-tlsdesc-dlopen.out: $(objpfx)tst-auditmod-tlsdesc1.so \ + $(objpfx)tst-auditmod-tlsdesc2.so +$(objpfx)tst-auditmod-tlsdesc1.so: $(objpfx)tst-auditmod-tlsdesc2.so +tst-audit-tlsdesc-ENV = LD_AUDIT=$(objpfx)tst-audit-tlsdesc-audit.so +tst-audit-tlsdesc-dlopen-ENV = LD_AUDIT=$(objpfx)tst-audit-tlsdesc-audit.so endif ifeq (yes,$(have-protected-data)) modules-names += tst-protected1moda tst-protected1modb diff --git a/elf/tst-audit-tlsdesc-audit.c b/elf/tst-audit-tlsdesc-audit.c new file mode 100644 index 0000000000..53993830c9 --- /dev/null +++ b/elf/tst-audit-tlsdesc-audit.c @@ -0,0 +1,23 @@ +/* DT_AUDIT with modules with TLSDESC. + Copyright (C) 2021 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 + . */ + +unsigned int +la_version (unsigned int version) +{ + return version; +} diff --git a/elf/tst-audit-tlsdesc-dlopen.c b/elf/tst-audit-tlsdesc-dlopen.c new file mode 100644 index 0000000000..e4d631fc94 --- /dev/null +++ b/elf/tst-audit-tlsdesc-dlopen.c @@ -0,0 +1,67 @@ +/* DT_AUDIT with modules with TLSDESC. + Copyright (C) 2021 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 +#include + +static void * +thr_func (void *mod) +{ + int* (*get_global1)(void) = xdlsym (mod, "get_global1"); + int* (*get_global2)(void) = xdlsym (mod, "get_global2"); + void (*set_global2)(int) = xdlsym (mod, "set_global2"); + int* (*get_local1)(void) = xdlsym (mod, "get_local1"); + int* (*get_local2)(void) = xdlsym (mod, "get_local2"); + + int *global1 = get_global1 (); + TEST_COMPARE (*global1, 0); + ++*global1; + + int *global2 = get_global2 (); + TEST_COMPARE (*global2, 0); + ++*global2; + TEST_COMPARE (*global2, 1); + + set_global2 (10); + TEST_COMPARE (*global2, 10); + + int *local1 = get_local1 (); + TEST_COMPARE (*local1, 0); + ++*local1; + + int *local2 = get_local2 (); + TEST_COMPARE (*local2, 0); + ++*local2; + + return 0; +} + +static int +do_test (void) +{ + void *mod = xdlopen ("tst-auditmod-tlsdesc1.so", RTLD_LAZY); + + pthread_t thr = xpthread_create (NULL, thr_func, mod); + void *r = xpthread_join (thr); + TEST_VERIFY (r == NULL); + + return 0; +} + +#include diff --git a/elf/tst-audit-tlsdesc.c b/elf/tst-audit-tlsdesc.c new file mode 100644 index 0000000000..3c8be81c95 --- /dev/null +++ b/elf/tst-audit-tlsdesc.c @@ -0,0 +1,60 @@ +/* DT_AUDIT with modules with TLSDESC. + Copyright (C) 2021 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 + +extern __thread int global1; +extern __thread int global2; +void *get_local1 (void); +void set_global2 (int v); +void *get_local2 (void); + +static void * +thr_func (void *clousure) +{ + TEST_COMPARE (global1, 0); + ++global1; + TEST_COMPARE (global2, 0); + ++global2; + TEST_COMPARE (global2, 1); + + set_global2 (10); + TEST_COMPARE (global2, 10); + + int *local1 = get_local1 (); + TEST_COMPARE (*local1, 0); + ++*local1; + + int *local2 = get_local2 (); + TEST_COMPARE (*local2, 0); + ++*local2; + + return 0; +} + +static int +do_test (void) +{ + pthread_t thr = xpthread_create (NULL, thr_func, NULL); + void *r = xpthread_join (thr); + TEST_VERIFY (r == NULL); + return 0; +} + +#include diff --git a/elf/tst-auditmod-tlsdesc1.c b/elf/tst-auditmod-tlsdesc1.c new file mode 100644 index 0000000000..61c7dd99a2 --- /dev/null +++ b/elf/tst-auditmod-tlsdesc1.c @@ -0,0 +1,41 @@ +/* DT_AUDIT with modules with TLSDESC. + Copyright (C) 2021 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 + . */ + +__thread int global1; + +int * +get_global1 (void) +{ + return &global1; +} + +static __thread int local1; + +void * +get_local1 (void) +{ + return &local1; +} + +extern __thread int global2; + +void +set_global2 (int v) +{ + global2 = v; +} diff --git a/elf/tst-auditmod-tlsdesc2.c b/elf/tst-auditmod-tlsdesc2.c new file mode 100644 index 0000000000..28aef635f6 --- /dev/null +++ b/elf/tst-auditmod-tlsdesc2.c @@ -0,0 +1,33 @@ +/* DT_AUDIT with modules with TLSDESC. + Copyright (C) 2021 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 + . */ + +__thread int global2; + +int * +get_global2 (void) +{ + return &global2; +} + +static __thread int local2; + +void * +get_local2 (void) +{ + return &local2; +} From patchwork Wed Jul 7 18:26:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 44212 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1E04C396E807 for ; Wed, 7 Jul 2021 18:31:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1E04C396E807 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1625682690; bh=S075vDMoDQcjjTFDJ5tKGDnLYgnn6P74mhIcuoO4ymQ=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=AVEXJTKY1K/ipnuFAn/gj1UHaTuFDxiNafDw7omLxbt+w7r+qWes6Rkxzvav/EoPX npH0YGoJUKyoXartnKOrQFlnR0lhUTMTZAVVNs/BOxaXsudl7LEjOe3JENBsyMY9/e foUfu0mk77olMkBL83bqJd3C119aBWDyEauMUDZg= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by sourceware.org (Postfix) with ESMTPS id E5E98396E473 for ; Wed, 7 Jul 2021 18:29:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E5E98396E473 Received: by mail-pg1-x52b.google.com with SMTP id h4so3182860pgp.5 for ; Wed, 07 Jul 2021 11:29:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=S075vDMoDQcjjTFDJ5tKGDnLYgnn6P74mhIcuoO4ymQ=; b=e4LdCWNXFdkootk9fq/aG+pcE8hpUPmxbphV958+PWkzz50eLoH3nYUieP099anTVe +ued9/9ThH2gKecbBG57fMMJ8laXyqmb06JxWegKCYfySVRsF+7uDalGiloQ0PV1bMen d+2VmCNft6shdYV/2054U05X8+KtLxr47uKVTPui3L3QUTRaTYGItOgkiHRAzPyTvxno VZnDcHGiMTqyaLraUbrmoJkXS+0HZIMEOQq7vo6hhZcq/g6QWdWhtT1TQ2QBVrM+vwEr B/28EKNxqKC/nAyOOnMX//PgyxKMCzPJ/DWHmDOyUZDYelrF7lwPWAVDw86MWmawlH05 DU9g== X-Gm-Message-State: AOAM532SiQQWpq8UbIo21aTLoC3bb3O18l7fk1j4E32JaS2qsIBvcxpP 5mUQ5VGGTg9qnUv7ecROVCbWxLN3n26/fg== X-Google-Smtp-Source: ABdhPJynoz56K0rWPrub8ACsEgkMVNEqTWCMFSJADbnIuvE9C+7oatufE3pI9YX3uOmxUR+qtozVlw== X-Received: by 2002:aa7:8254:0:b029:2ed:b41:fefc with SMTP id e20-20020aa782540000b02902ed0b41fefcmr27242228pfn.42.1625682574818; Wed, 07 Jul 2021 11:29:34 -0700 (PDT) Received: from birita.. ([177.194.59.218]) by smtp.gmail.com with ESMTPSA id fv8sm18868662pjb.21.2021.07.07.11.29.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 11:29:34 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 4/5] elf: Do not fail for failed dlmopen on audit modules (BZ #28061) Date: Wed, 7 Jul 2021 15:26:09 -0300 Message-Id: <20210707182610.3940620-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210707182610.3940620-1-adhemerval.zanella@linaro.org> References: <20210707182610.3940620-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Cc: John Mellor-Crummey Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" The dl_main() sets the LM_ID_BASE to RT_ADD just before starting to add load new shared objects. The state is set to R_CONSISTENT just after all objects are loaded. However if a audit modules tries to dlmopen() an inexistent module, the _dl_open() will assert that the namespace is in an inconsistent state. This is different than dlopen(), since first it will not use LM_ID_BASE and second _dl_map_object_from_fd() is the sole responsible to set and reset the r_state value. So the assert() on _dl_open() can not really see if the state is consistent since it is _dt_main() that reset is. This patch removes the assert. Checked on x86_64-linux-gnu. --- elf/Makefile | 7 ++++-- elf/dl-open.c | 2 -- elf/tst-audit19.c | 25 +++++++++++++++++++ elf/tst-auditmod19.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 elf/tst-audit19.c create mode 100644 elf/tst-auditmod19.c diff --git a/elf/Makefile b/elf/Makefile index 19f885679a..513b7e68a2 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -220,7 +220,7 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \ tst-dlopenfail-2 \ tst-filterobj tst-filterobj-dlopen tst-auxobj tst-auxobj-dlopen \ tst-audit14 tst-audit15 tst-audit16 tst-audit17 tst-audit18a \ - tst-audit18b \ + tst-audit18b tst-audit19 \ tst-single_threaded tst-single_threaded-pthread \ tst-tls-ie tst-tls-ie-dlmopen argv0test \ tst-glibc-hwcaps tst-glibc-hwcaps-prepend tst-glibc-hwcaps-mask \ @@ -295,7 +295,7 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \ tst-unique1mod1 tst-unique1mod2 \ tst-unique2mod1 tst-unique2mod2 \ tst-auditmod9a tst-auditmod9b \ - tst-auditmod18a tst-audit18bmod tst-auditmod18b \ + tst-auditmod18a tst-audit18bmod tst-auditmod18b tst-auditmod19 \ $(if $(CXX),tst-unique3lib tst-unique3lib2 tst-unique4lib \ tst-nodelete-uniquemod tst-nodelete-rtldmod \ tst-nodelete-zmod \ @@ -1502,6 +1502,9 @@ LDFLAGS-tst-audit18bmod.so = -Wl,-z,now LDFLAGS-tst-audit18b = -Wl,-z,now tst-audit18b-ENV = LD_AUDIT=$(objpfx)tst-auditmod18b.so +$(objpfx)tst-audit19.out: $(objpfx)tst-auditmod19.so +tst-audit19-ENV = LD_AUDIT=$(objpfx)tst-auditmod19.so + # tst-sonamemove links against an older implementation of the library. LDFLAGS-tst-sonamemove-linkmod1.so = \ -Wl,--version-script=tst-sonamemove-linkmod1.map \ diff --git a/elf/dl-open.c b/elf/dl-open.c index 66ec9d7ed5..696de8172f 100644 --- a/elf/dl-open.c +++ b/elf/dl-open.c @@ -916,8 +916,6 @@ no more namespaces available for dlmopen()")); the flag here. */ } - assert (_dl_debug_initialize (0, args.nsid)->r_state == RT_CONSISTENT); - /* Release the lock. */ __rtld_lock_unlock_recursive (GL(dl_load_lock)); diff --git a/elf/tst-audit19.c b/elf/tst-audit19.c new file mode 100644 index 0000000000..6f39ccee86 --- /dev/null +++ b/elf/tst-audit19.c @@ -0,0 +1,25 @@ +/* Check dlopen failure on audit modules. + Copyright (C) 2021 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 + . */ + +static int +do_test (void) +{ + return 0; +} + +#include diff --git a/elf/tst-auditmod19.c b/elf/tst-auditmod19.c new file mode 100644 index 0000000000..c57e50ee4e --- /dev/null +++ b/elf/tst-auditmod19.c @@ -0,0 +1,57 @@ +/* Check dlopen failure on audit modules. + Copyright (C) 2021 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 +#include + +unsigned int +la_version (unsigned int v) +{ + return LAV_CURRENT; +} + +static void +check (void) +{ + { + void *mod = dlopen ("nonexistent.so", RTLD_NOW); + if (mod != NULL) + abort (); + } + + { + void *mod = dlmopen (LM_ID_BASE, "nonexistent.so", RTLD_NOW); + if (mod != NULL) + abort (); + } +} + +void +la_activity (uintptr_t *cookie, unsigned int flag) +{ + if (flag != LA_ACT_CONSISTENT) + return; + check (); +} + +void +la_preinit (uintptr_t *cookie) +{ + check (); +} From patchwork Wed Jul 7 18:26:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 44213 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 816AE396E806 for ; Wed, 7 Jul 2021 18:32:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 816AE396E806 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1625682734; bh=rNMeptvDsOwqUCm8J6V3Q6Cgw1ZmeVnbwAKheI53MCc=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=VFKNiZCh1bc9oR+Goj7ytc7v+BEbR2DS8UnA+eyLn1oCcOtxUQInsRt1+4Ef4TGw2 3WEPiSgncyFmR3J5sLtZfPGctp2VI/Ik3MmIMHavbdVZ4Qe9InUe7T0rImP4B0E1KC 83Z11vt2crS+Rba4CWmz4JlZVkq+ZECnuhgr3Ihk= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by sourceware.org (Postfix) with ESMTPS id B3E69396E435 for ; Wed, 7 Jul 2021 18:29:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B3E69396E435 Received: by mail-pj1-x102f.google.com with SMTP id oj11-20020a17090b4d8bb029017338c124dcso60845pjb.0 for ; Wed, 07 Jul 2021 11:29:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rNMeptvDsOwqUCm8J6V3Q6Cgw1ZmeVnbwAKheI53MCc=; b=X1c/o/UrQv6vSWa0829vxmt9Q7e5O9ptdDuXLnFEdRAUVG8OlgfoQpergIAbujNEeM GYtG8NSyHQEe6ytxuFmn7jaRmoOBmM0j0H11RA0owuGeFtyEcCJBx1jnG5E69dsoNk+n /tncBIPRzXlYaDO93bNAfUWz98mI9VLeAvhivR3ZK3sT5iqi7Ni+mVQRuNoZR0ag3ddK 5TqsKMD5D6kBosviFH6q+UQjU8gn8eW6/DagPths1fu12IpZI6YoTaPpyGD6Z2sdfxs/ oxl+u8AknSd3zGwms/AkaAaImC+j5Nh4DCLQgoR6nolDpfn1aJzD+YHw2ieetnKEmbjV Cblw== X-Gm-Message-State: AOAM532Czgh1fb0JAdrMmzn98zB6thBt1TxIanZDA+gxuFejT+3ci78m 5sPS1nxd2xkNcVjNmUDWoAh2CFtMwD+8bw== X-Google-Smtp-Source: ABdhPJzFrC91AcDb7gGa678wcgaV12PRlX6m+tf2cxCzQfTh9USdOMpwMp1gxDFdmKB0jcmrd6HIiw== X-Received: by 2002:a17:902:6bcb:b029:127:9c65:f1fb with SMTP id m11-20020a1709026bcbb02901279c65f1fbmr22495879plt.64.1625682579595; Wed, 07 Jul 2021 11:29:39 -0700 (PDT) Received: from birita.. ([177.194.59.218]) by smtp.gmail.com with ESMTPSA id fv8sm18868662pjb.21.2021.07.07.11.29.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 11:29:39 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 5/5] elf: Suppress audit calls when a (new) namespace is empty (BZ #28062) Date: Wed, 7 Jul 2021 15:26:10 -0300 Message-Id: <20210707182610.3940620-6-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210707182610.3940620-1-adhemerval.zanella@linaro.org> References: <20210707182610.3940620-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Cc: John Mellor-Crummey Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" From: Vivek Das Mohapatra For a new Lmid_t the namespace link_map list are empty, so it requires to check if before using it. This can happen for when audit module is used along with dlmopen. Checked on x86_64-linux-gnu. Co-authored-by: Adhemerval Zanella --- elf/Makefile | 7 ++- elf/dl-load.c | 7 ++- elf/tst-audit20.c | 128 +++++++++++++++++++++++++++++++++++++++++++ elf/tst-audit20mod.c | 26 +++++++++ elf/tst-auditmod20.c | 73 ++++++++++++++++++++++++ 5 files changed, 238 insertions(+), 3 deletions(-) create mode 100644 elf/tst-audit20.c create mode 100644 elf/tst-audit20mod.c create mode 100644 elf/tst-auditmod20.c diff --git a/elf/Makefile b/elf/Makefile index 513b7e68a2..53868909c5 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -220,7 +220,7 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \ tst-dlopenfail-2 \ tst-filterobj tst-filterobj-dlopen tst-auxobj tst-auxobj-dlopen \ tst-audit14 tst-audit15 tst-audit16 tst-audit17 tst-audit18a \ - tst-audit18b tst-audit19 \ + tst-audit18b tst-audit19 tst-audit20 \ tst-single_threaded tst-single_threaded-pthread \ tst-tls-ie tst-tls-ie-dlmopen argv0test \ tst-glibc-hwcaps tst-glibc-hwcaps-prepend tst-glibc-hwcaps-mask \ @@ -296,6 +296,7 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \ tst-unique2mod1 tst-unique2mod2 \ tst-auditmod9a tst-auditmod9b \ tst-auditmod18a tst-audit18bmod tst-auditmod18b tst-auditmod19 \ + tst-auditmod20 tst-audit20mod \ $(if $(CXX),tst-unique3lib tst-unique3lib2 tst-unique4lib \ tst-nodelete-uniquemod tst-nodelete-rtldmod \ tst-nodelete-zmod \ @@ -1505,6 +1506,10 @@ tst-audit18b-ENV = LD_AUDIT=$(objpfx)tst-auditmod18b.so $(objpfx)tst-audit19.out: $(objpfx)tst-auditmod19.so tst-audit19-ENV = LD_AUDIT=$(objpfx)tst-auditmod19.so +$(objpfx)tst-audit20.out: $(objpfx)tst-auditmod20.so \ + $(objpfx)tst-audit20mod.so +tst-audit20-ARGS = -- $(host-test-program-cmd) + # tst-sonamemove links against an older implementation of the library. LDFLAGS-tst-sonamemove-linkmod1.so = \ -Wl,--version-script=tst-sonamemove-linkmod1.map \ diff --git a/elf/dl-load.c b/elf/dl-load.c index a08df001af..d2075f3c02 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -1062,8 +1062,11 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, && __glibc_unlikely (GLRO(dl_naudit) > 0)) { struct link_map *head = GL(dl_ns)[nsid]._ns_loaded; - /* Do not call the functions for any auditing object. */ - if (head->l_auditing == 0) + /* Do not call the functions for any auditing object and also do not + try to call auditing functions if the namespace is currently + empty. This happens when opening the first DSO in a new + namespace. */ + if (head != NULL && head->l_auditing == 0) { struct audit_ifaces *afct = GLRO(dl_audit); for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) diff --git a/elf/tst-audit20.c b/elf/tst-audit20.c new file mode 100644 index 0000000000..5c5d42b723 --- /dev/null +++ b/elf/tst-audit20.c @@ -0,0 +1,128 @@ +/* Check DT_AUDIT with dlmopen. + Copyright (C) 2021 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static int restart; +#define CMDLINE_OPTIONS \ + { "restart", no_argument, &restart, 1 }, + +static int +handle_restart (void) +{ + { + void *h = xdlmopen (LM_ID_NEWLM, LIBC_SO, RTLD_NOW); + + pid_t (*s)(void) = xdlsym (h, "getpid"); + TEST_COMPARE (s (), getpid ()); + + xdlclose (h); + } + + { + void *h = xdlmopen (LM_ID_NEWLM, "tst-audit20mod.so", RTLD_NOW); + + int (*foo)(void) = xdlsym (h, "foo"); + TEST_COMPARE (foo (), 10); + + xdlclose (h); + } + + return 0; +} + +static int +do_test (int argc, char *argv[]) +{ + /* We must have either: + - One our fource parameters left if called initially: + + path to ld.so optional + + "--library-path" optional + + the library path optional + + the application name */ + + if (restart) + return handle_restart (); + + char *spargv[9]; + int i = 0; + for (; i < argc - 1; i++) + spargv[i] = argv[i + 1]; + spargv[i++] = (char *) "--direct"; + spargv[i++] = (char *) "--restart"; + spargv[i] = NULL; + + setenv ("LD_AUDIT", "tst-auditmod20.so", 0); + struct support_capture_subprocess result + = support_capture_subprogram (spargv[0], spargv); + support_capture_subprocess_check (&result, "tst-audit20", 0, sc_allow_stderr); + + struct + { + const char *name; + bool found; + } audit_iface[] = + { + { "la_version", false }, + { "la_objsearch", false }, + { "la_activity", false }, + { "la_objopen", false }, + { "la_objclose", false }, + { "la_preinit", false }, +#if __WORDSIZE == 32 + { "la_symbind32", false }, +#elif __WORDSIZE == 64 + { "la_symbind64", false }, +#endif + }; + + /* Some hooks are called more than once but it only checkis if they are + called at least once. */ + FILE *out = fmemopen (result.err.buffer, result.err.length, "r"); + TEST_VERIFY (out != NULL); + char *buffer = NULL; + size_t buffer_length = 0; + while (xgetline (&buffer, &buffer_length, out)) { + for (int i = 0; i < array_length (audit_iface); i++) + if (strncmp (buffer, audit_iface[i].name, + strlen (audit_iface[i].name)) == 0) + audit_iface[i].found = true; + } + free (buffer); + xfclose (out); + + for (int i = 0; i < array_length (audit_iface); i++) + TEST_COMPARE (audit_iface[i].found, true); + + support_capture_subprocess_free (&result); + + return 0; +} + +#define TEST_FUNCTION_ARGV do_test +#include diff --git a/elf/tst-audit20mod.c b/elf/tst-audit20mod.c new file mode 100644 index 0000000000..f229c4139b --- /dev/null +++ b/elf/tst-audit20mod.c @@ -0,0 +1,26 @@ +/* Check DT_AUDIT with dlmopen. + Copyright (C) 2021 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 +foo (void) +{ + return 10; +} diff --git a/elf/tst-auditmod20.c b/elf/tst-auditmod20.c new file mode 100644 index 0000000000..182992e9fd --- /dev/null +++ b/elf/tst-auditmod20.c @@ -0,0 +1,73 @@ +/* Check DT_AUDIT with dlmopen. + Copyright (C) 2021 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 + +unsigned int +la_version (unsigned int version) +{ + fprintf (stderr, "%s\n", __func__); + return LAV_CURRENT; +} + +char * +la_objsearch (const char *name, uintptr_t *cookie, unsigned int flag) +{ + fprintf (stderr, "%s\n", __func__); + return (char *) name; +} + +void +la_activity (uintptr_t *cookie, unsigned int flag) +{ + fprintf (stderr, "%s\n", __func__); +} + +unsigned int +la_objopen (struct link_map *map, Lmid_t lmid, uintptr_t *cookie) +{ + fprintf (stderr, "%s\n", __func__); + return LA_FLG_BINDTO | LA_FLG_BINDFROM; +} + +unsigned int +la_objclose (uintptr_t *cookie) +{ + fprintf (stderr, "%s\n", __func__); + return 0; +} + +void +la_preinit (uintptr_t *cookie) +{ + fprintf (stderr, "%s\n", __func__); +} + +uintptr_t +#if __ELF_NATIVE_CLASS == 32 +la_symbind32 (Elf32_Sym *sym, unsigned int ndx, uintptr_t *refcook, + uintptr_t *defcook, unsigned int *flags, const char *symname) +#else +la_symbind64 (Elf64_Sym *sym, unsigned int ndx, uintptr_t *refcook, + uintptr_t *defcook, unsigned int *flags, const char *symname) +#endif +{ + fprintf (stderr, "%s\n", __func__); + return sym->st_value; +}