From patchwork Wed Dec 22 13:26:58 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: 49179 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 79CB83858405 for ; Wed, 22 Dec 2021 13:29:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 79CB83858405 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1640179748; bh=EDsiKJUb+tSr3jcvXbBEXJXG5ieMt9Bfub3ljJlxFbk=; 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=ipuIA6pnHjkeJDLLu+MvbL/EIlrgjMbjfVfBZ49STlTFrppOYfkylFxPt90eIYczP tDvgWuXaL7ih4Mjdd+u7ZKXQnfQLDwHoB3wmFHKY7E35Qtz9JV2K8aGVlHXyGcVnbm wx/SAX6we/FYqfDVeqlQyRM+pe+UntXHajI8y6lU= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x735.google.com (mail-qk1-x735.google.com [IPv6:2607:f8b0:4864:20::735]) by sourceware.org (Postfix) with ESMTPS id 277463858D35 for ; Wed, 22 Dec 2021 13:27:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 277463858D35 Received: by mail-qk1-x735.google.com with SMTP id m186so2257892qkb.4 for ; Wed, 22 Dec 2021 05:27:20 -0800 (PST) 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=EDsiKJUb+tSr3jcvXbBEXJXG5ieMt9Bfub3ljJlxFbk=; b=eXfeGCswT6Wz/jIQw9Ya/2TSbxfNxTxswdeEojuB6Adn/9qeWJPHryHCUfUbpoiW9s RC+EQo+DDwHJHWkR3gl72s1qeMkFOX8idBbjiP7vm6OElilAQ8LjE5gjq/m2yAA4K66h anQDiLvAkMIrFlDMBiU8boagimInXKIAtcICIcJu+yInMgeJyVAEd/8kXoTsDcFHjEru cQie0LI3Z+Ih1vmWR8YjDD5eVkHfDd390HC/vmCxfWib7KgPUJCNhZypsAwOv5SqKhJO KZ0TBZWksAmA3HZbfIyHqngbqBLag8SxTKFHPS8gxUdAG8NDtDC0PMph5PintjxMfnho /bLA== X-Gm-Message-State: AOAM533pnJGV49FMBaSZZwHJNwTu///9/Mdw5lw7YKTmyaZvx1z/eyfB Gk8BZGjZbPD6X0GlX8oE3hHgbj23aDrFXw== X-Google-Smtp-Source: ABdhPJx3ZoezHgguL1aaWC6mzOPj7QwYvOaTqNVLj5KiOx4WEEzTKNyFbrQ1iSlQli3WBDYjMi5kNA== X-Received: by 2002:a05:620a:1641:: with SMTP id c1mr1437932qko.54.1640179639501; Wed, 22 Dec 2021 05:27:19 -0800 (PST) Received: from birita.. ([2804:431:c7cb:3b1e:1ba8:c11f:6224:efe1]) by smtp.gmail.com with ESMTPSA id x13sm1942849qkp.102.2021.12.22.05.27.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Dec 2021 05:27:19 -0800 (PST) To: libc-alpha@sourceware.org, fweimer@redhat.com Subject: [PATCH v7 02/16] elf: Add _dl_audit_activity_map and _dl_audit_activity_nsid Date: Wed, 22 Dec 2021 10:26:58 -0300 Message-Id: <20211222132712.523295-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211222132712.523295-1-adhemerval.zanella@linaro.org> References: <20211222132712.523295-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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" It consolidates the code required to call la_activity audit callback. Also 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, i686-linux-gnu, and aarch64-linux-gnu. Reviewed-by: Florian Weimer --- elf/dl-audit.c | 30 ++++++++++++++++++++++++ elf/dl-close.c | 47 ++------------------------------------ elf/dl-load.c | 20 ++-------------- elf/dl-open.c | 20 +--------------- elf/rtld.c | 31 ++----------------------- sysdeps/generic/ldsodefs.h | 10 ++++++++ 6 files changed, 47 insertions(+), 111 deletions(-) diff --git a/elf/dl-audit.c b/elf/dl-audit.c index 4066dfe851..b44ecde135 100644 --- a/elf/dl-audit.c +++ b/elf/dl-audit.c @@ -18,6 +18,36 @@ #include +void +_dl_audit_activity_map (struct link_map *l, int action) +{ + 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 (l, cnt)->cookie, action); + afct = afct->next; + } +} + +void +_dl_audit_activity_nsid (Lmid_t nsid, int action) +{ + /* Auditing checkpoint: we have deleted all objects. If head is NULL, the + namespace has become empty, and the audit interface does not give us a + way to signal LA_ACT_CONSISTENT for it because the first loaded module + is used to identify the namespace. + + Furthermore, do not notify auditors of the cleanup of a failed audit + module loading attempt. */ + struct link_map *head = GL(dl_ns)[nsid]._ns_loaded; + if (__glibc_likely (GLRO(dl_naudit) == 0) + || head == NULL || head->l_auditing) + return; + + _dl_audit_activity_map (head, action); +} + void _dl_audit_objopen (struct link_map *l, Lmid_t nsid) { diff --git a/elf/dl-close.c b/elf/dl-close.c index 4f5cfcc1c3..b9a610981f 100644 --- a/elf/dl-close.c +++ b/elf/dl-close.c @@ -472,25 +472,7 @@ _dl_close_worker (struct link_map *map, bool force) #ifdef SHARED /* Auditing checkpoint: we will start deleting objects. */ - if (__glibc_unlikely (do_audit)) - { - struct link_map *head = ns->_ns_loaded; - struct audit_ifaces *afct = GLRO(dl_audit); - /* Do not call the functions for any auditing object. */ - if (head->l_auditing == 0) - { - for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) - { - if (afct->activity != NULL) - { - struct auditstate *state = link_map_audit_state (head, cnt); - afct->activity (&state->cookie, LA_ACT_DELETE); - } - - afct = afct->next; - } - } - } + _dl_audit_activity_nsid (nsid, LA_ACT_DELETE); #endif /* Notify the debugger we are about to remove some loaded objects. */ @@ -785,32 +767,7 @@ _dl_close_worker (struct link_map *map, bool force) __rtld_lock_unlock_recursive (GL(dl_load_tls_lock)); #ifdef SHARED - /* Auditing checkpoint: we have deleted all objects. */ - if (__glibc_unlikely (do_audit)) - { - struct link_map *head = ns->_ns_loaded; - /* If head is NULL, the namespace has become empty, and the - audit interface does not give us a way to signal - LA_ACT_CONSISTENT for it because the first loaded module is - used to identify the namespace. - - Furthermore, do not notify auditors of the cleanup of a - failed audit module loading attempt. */ - if (head != NULL && 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) - { - struct auditstate *state = link_map_audit_state (head, cnt); - afct->activity (&state->cookie, LA_ACT_CONSISTENT); - } - - afct = afct->next; - } - } - } + _dl_audit_activity_nsid (nsid, LA_ACT_CONSISTENT); #endif if (__builtin_expect (ns->_ns_loaded == NULL, 0) diff --git a/elf/dl-load.c b/elf/dl-load.c index 57de24491a..d1d02fa70e 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -1488,24 +1488,8 @@ cannot enable executable stack as shared object requires"); /* 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; - } - } - } + if ((mode & __RTLD_AUDIT) == 0) + _dl_audit_activity_nsid (nsid, LA_ACT_ADD); #endif /* Notify the debugger we have added some objects. We need to diff --git a/elf/dl-open.c b/elf/dl-open.c index 6ea5dd2457..e2f2e713e7 100644 --- a/elf/dl-open.c +++ b/elf/dl-open.c @@ -611,25 +611,7 @@ dl_open_worker_begin (void *a) #ifdef SHARED /* Auditing checkpoint: we have added all objects. */ - if (__glibc_unlikely (GLRO(dl_naudit) > 0)) - { - struct link_map *head = GL(dl_ns)[new->l_ns]._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) - { - struct auditstate *state = link_map_audit_state (head, cnt); - afct->activity (&state->cookie, LA_ACT_CONSISTENT); - } - - afct = afct->next; - } - } - } + _dl_audit_activity_nsid (new->l_ns, LA_ACT_CONSISTENT); #endif /* Notify the debugger all new objects are now ready to go. */ diff --git a/elf/rtld.c b/elf/rtld.c index 6b8316e8a9..4952170621 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -1794,18 +1794,7 @@ dl_main (const ElfW(Phdr) *phdr, /* Auditing checkpoint: we are ready to signal that the initial map is being constructed. */ - if (__glibc_unlikely (GLRO(dl_naudit) > 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 (main_map, cnt)->cookie, - LA_ACT_ADD); - - afct = afct->next; - } - } + _dl_audit_activity_map (main_map, LA_ACT_ADD); /* We have two ways to specify objects to preload: via environment variable and via the file /etc/ld.so.preload. The latter can also @@ -2486,23 +2475,7 @@ dl_main (const ElfW(Phdr) *phdr, #ifdef SHARED /* Auditing checkpoint: we have added all objects. */ - if (__glibc_unlikely (GLRO(dl_naudit) > 0)) - { - struct link_map *head = GL(dl_ns)[LM_ID_BASE]._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_CONSISTENT); - - afct = afct->next; - } - } - } + _dl_audit_activity_nsid (LM_ID_BASE, LA_ACT_CONSISTENT); #endif /* Notify the debugger all new objects are now ready to go. We must re-get diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index c38a89f3d0..e54efe65c8 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -1396,6 +1396,16 @@ link_map_audit_state (struct link_map *l, size_t index) } } +/* Call the la_activity() from the audit modules from the link map L + and issues the ACTION argument. */ +void _dl_audit_activity_map (struct link_map *l, int action) + attribute_hidden; + +/* Call the la_activity() from the audit modules from the link map + from the namespace NSID and issues the ACTION argument. */ +void _dl_audit_activity_nsid (Lmid_t nsid, int action) + attribute_hidden; + /* Call the la_objopen from the audit modules for the link_map L on the namespace identification NSID. */ void _dl_audit_objopen (struct link_map *l, Lmid_t nsid)