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; +}