From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 36AF23858025 for ; Tue, 12 Apr 2022 18:42:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 36AF23858025 Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-629-gW7vMkgxNc6-5IujSvUSbA-1; Tue, 12 Apr 2022 14:42:25 -0400 X-MC-Unique: gW7vMkgxNc6-5IujSvUSbA-1 Received: by mail-qk1-f200.google.com with SMTP id d4-20020a05620a240400b0069c24a33cc6so4308886qkn.4 for ; Tue, 12 Apr 2022 11:42:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tLBhT5eNV8qPm2bSvnj7Pw5NRVEjVgvfwQf3HfMYVDk=; b=DhsA6UrTaMqK457T2mbHetcWbG8MpyZHNlXoCAScpZEv/OjPImndL7nA0Zz60g9O/l Hbb3xF5VOyXd7MX11dAllun3YiJnA0S8uB+j1rtYTdrk+oWwqkoWkf9Ep5aaAeFnBgr2 fWK0rjtYiobA9d04uawpXW3823K0B5iaLhgjmWOIZM9nrPHDTTxe9mMx4bddI4kQzheE 4l0UI5b6x39RPJOQdPcT9anpzjDOKQkahowUkiPTDrN1cuocxilyKbKrh6ejxL105kVo Cfo1XXFO9kLyRV6Pns0nutngs9MqjthPbh4fbrSvoIUc9JrdV3JRY3Ewj67Tgqfos7Os 3pAQ== X-Gm-Message-State: AOAM532puUXes8CkGEPbPh1d4VqvXzsd040e+FE1SzCn6HIxRj+46qto w49u/7Ab/QSRtwfd8O3XC4pgwqBSyode/7wmHz/wTr/xm0d/HPH8Hi/n/JGU5aHD3yBYm0ucwRC buclyRXg5+yC6YXDxEZC9boGIrY6H0sOJktTghcz509LT6wiySsz2DpjpKIpQrDccHzqnjoM= X-Received: by 2002:ac8:5855:0:b0:2eb:87af:ab50 with SMTP id h21-20020ac85855000000b002eb87afab50mr4446428qth.406.1649788943893; Tue, 12 Apr 2022 11:42:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzsWngWhsT9DSyrvDpqyIQ0eC6M1/+f9P7hrC/kNg/12eZvXtcsi8CptEymVWWoyUYA4G3aug== X-Received: by 2002:ac8:5855:0:b0:2eb:87af:ab50 with SMTP id h21-20020ac85855000000b002eb87afab50mr4446403qth.406.1649788943427; Tue, 12 Apr 2022 11:42:23 -0700 (PDT) Received: from athas.redhat.com (135-23-175-80.cpe.pppoe.ca. [135.23.175.80]) by smtp.gmail.com with ESMTPSA id j19-20020a05622a039300b002ecc2ebfd87sm10953840qtx.32.2022.04.12.11.42.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 11:42:23 -0700 (PDT) From: Carlos O'Donell To: libc-stable@sourceware.org Cc: Adhemerval Zanella , Florian Weimer Subject: [PATCH 07/27] elf: Move la_activity (LA_ACT_ADD) after _dl_add_to_namespace_list() (BZ #28062) Date: Tue, 12 Apr 2022 14:41:45 -0400 Message-Id: <20220412184205.3343677-8-carlos@redhat.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220412184205.3343677-1-carlos@redhat.com> References: <20220412184205.3343677-1-carlos@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: application/octet-stream; x-default=true X-Spam-Status: No, score=-5.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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-stable@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-stable mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Apr 2022 18:42:27 -0000 From: Adhemerval Zanella It ensures that the the namespace is guaranteed to not be empty. Checked on x86_64-linux-gnu. Reviewed-by: Florian Weimer (cherry picked from commit ed3ce71f5c64c5f07cbde0ef03554ea8950d8f2c) Resolved conflicts: elf/Makefile --- elf/Makefile | 7 +++ elf/dl-load.c | 74 +++++++++++++------------ elf/tst-audit18.c | 129 +++++++++++++++++++++++++++++++++++++++++++ elf/tst-audit18mod.c | 23 ++++++++ elf/tst-auditmod18.c | 73 ++++++++++++++++++++++++ 5 files changed, 270 insertions(+), 36 deletions(-) create mode 100644 elf/tst-audit18.c create mode 100644 elf/tst-audit18mod.c create mode 100644 elf/tst-auditmod18.c diff --git a/elf/Makefile b/elf/Makefile index e9cc85544d..4818a6b3fe 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -352,6 +352,7 @@ tests += \ tst-audit15 \ tst-audit16 \ tst-audit17 \ + tst-audit18 \ tst-auditmany \ tst-auxobj \ tst-auxobj-dlopen \ @@ -607,6 +608,7 @@ modules-names = \ tst-audit12mod2 \ tst-audit12mod3 \ tst-audit13mod1 \ + tst-audit18mod \ tst-auditlogmod-1 \ tst-auditlogmod-2 \ tst-auditlogmod-3 \ @@ -624,6 +626,7 @@ modules-names = \ tst-auditmod9b \ tst-auditmod11 \ tst-auditmod12 \ + tst-auditmod18 \ tst-auxvalmod \ tst-big-note-lib \ tst-deep1mod1 \ @@ -1955,6 +1958,10 @@ $(objpfx)tst-auditmod17.so: $(objpfx)tst-auditmod17.os CFLAGS-.os += $(call elide-stack-protector,.os,tst-auditmod17) tst-audit17-ENV = LD_AUDIT=$(objpfx)tst-auditmod17.so +$(objpfx)tst-audit18.out: $(objpfx)tst-auditmod18.so \ + $(objpfx)tst-audit18mod.so +tst-audit18-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 a920b12a90..a8c6df3959 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -1054,42 +1054,6 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, /* This is the ELF header. We read it in `open_verify'. */ header = (void *) fbp->buf; - /* Signal that we are going to add new objects. */ - if (r->r_state == RT_CONSISTENT) - { -#ifdef SHARED - /* Auditing checkpoint: we are going to add new objects. */ - if ((mode & __RTLD_AUDIT) == 0 - && __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) - { - struct audit_ifaces *afct = GLRO(dl_audit); - for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) - { - if (afct->activity != NULL) - afct->activity (&link_map_audit_state (head, cnt)->cookie, - LA_ACT_ADD); - - afct = afct->next; - } - } - } -#endif - - /* Notify the debugger we have added some objects. We need to - call _dl_debug_initialize in a static program in case dynamic - linking has not been used before. */ - r->r_state = RT_ADD; - _dl_debug_state (); - LIBC_PROBE (map_start, 2, nsid, r); - make_consistent = true; - } - else - assert (r->r_state == RT_ADD); - /* Enter the new object in the list of loaded objects. */ l = _dl_new_object (realname, name, l_type, loader, mode, nsid); if (__glibc_unlikely (l == NULL)) @@ -1511,6 +1475,44 @@ cannot enable executable stack as shared object requires"); /* Now that the object is fully initialized add it to the object list. */ _dl_add_to_namespace_list (l, nsid); + /* Signal that we are going to add new objects. */ + if (r->r_state == RT_CONSISTENT) + { +#ifdef SHARED + /* Auditing checkpoint: we are going to add new objects. Since this + is called after _dl_add_to_namespace_list the namespace is guaranteed + to not be empty. */ + if ((mode & __RTLD_AUDIT) == 0 + && __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) + { + struct audit_ifaces *afct = GLRO(dl_audit); + for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) + { + if (afct->activity != NULL) + afct->activity (&link_map_audit_state (head, cnt)->cookie, + LA_ACT_ADD); + + afct = afct->next; + } + } + } +#endif + + /* Notify the debugger we have added some objects. We need to + call _dl_debug_initialize in a static program in case dynamic + linking has not been used before. */ + r->r_state = RT_ADD; + _dl_debug_state (); + LIBC_PROBE (map_start, 2, nsid, r); + make_consistent = true; + } + else + assert (r->r_state == RT_ADD); + #ifdef SHARED /* Auditing checkpoint: we have a new object. */ if (__glibc_unlikely (GLRO(dl_naudit) > 0) diff --git a/elf/tst-audit18.c b/elf/tst-audit18.c new file mode 100644 index 0000000000..ef784908f6 --- /dev/null +++ b/elf/tst-audit18.c @@ -0,0 +1,129 @@ +/* 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-audit18mod.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-auditmod18.so", 0); + struct support_capture_subprocess result + = support_capture_subprogram (spargv[0], spargv); + support_capture_subprocess_check (&result, "tst-audit18", 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 the test only check if any + is 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-audit18mod.c b/elf/tst-audit18mod.c new file mode 100644 index 0000000000..096a9167c9 --- /dev/null +++ b/elf/tst-audit18mod.c @@ -0,0 +1,23 @@ +/* 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 + . */ + +int +foo (void) +{ + return 10; +} diff --git a/elf/tst-auditmod18.c b/elf/tst-auditmod18.c new file mode 100644 index 0000000000..182992e9fd --- /dev/null +++ b/elf/tst-auditmod18.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; +} -- 2.35.1