From patchwork Mon Nov 15 18:37:18 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: 47701 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 AB5873858405 for ; Mon, 15 Nov 2021 18:40:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AB5873858405 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1637001656; bh=P2rp8xS2TNE/sYtCL1xjjy6mV38GsS4JLueAW63lW7U=; 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=pekg7exOXN1fs/ELKCKDi1WIfIWASAyw3WX8FSckWG82HJjUnIJGhpaYtZPwx1xPT 4RqXT3Jx6pjdaMexUI557+QJKL+MlSPxMvC8wKZmenF0R89p95wJlb23M3Kpns9g6m xA+4vQ8s/Hpc3RvQM3FUpgrw/4Ba1cl+37v0Gj58= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ua1-x92d.google.com (mail-ua1-x92d.google.com [IPv6:2607:f8b0:4864:20::92d]) by sourceware.org (Postfix) with ESMTPS id D98F33857C72 for ; Mon, 15 Nov 2021 18:37:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D98F33857C72 Received: by mail-ua1-x92d.google.com with SMTP id p2so36881889uad.11 for ; Mon, 15 Nov 2021 10:37:45 -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=P2rp8xS2TNE/sYtCL1xjjy6mV38GsS4JLueAW63lW7U=; b=XEGP7hpffK139CVxu0w+k1GNzmB2kIEJ2J5Ep/dqNHOabNIoNS0sl9dcz6VKaTbaPI y9v9B6Q+wXHTTm0WBZkXf7re/7d8gKhEvw8dKKZ3PAVEZXzLpvfqrK5f+5t6L+kgrhvw nPub02zM8Y/84seB8vbxHeT1UmBtPwIpvcDl8OVJbP+hR6XEypZR0t/GuCieBL6KdLfH VJTklGggQagnKfTGWIqzwa6f6dgt+f7f/lHi5nTNUpMRXS6ERPLV5zd02GJmMjJFcM81 pAyU2niM+5zDkOK/ZZSYoGf4mbR4Bo1SLXkwvwkfQI/QIa5vxEb6EnL6152LamDLMrFt FW3g== X-Gm-Message-State: AOAM530efm1M0cEqrDSOqXGiIFgPP6v1NjxVP61+heT4+18nq5onzOnb WKWSMYz/Ga1exsgxY160lt+D0ZkQHQoX7w== X-Google-Smtp-Source: ABdhPJwMjt3Yee2+/3VxcUqe3eZ5vjLg9Af1OLAvKPYOClIWOdbYeDJMLCrUCEPiCe63yD8SyuRX0g== X-Received: by 2002:a05:6102:105b:: with SMTP id h27mr46447732vsq.60.1637001465246; Mon, 15 Nov 2021 10:37:45 -0800 (PST) Received: from birita.. ([2804:431:c7ca:66dc:13f5:e2fb:5a0d:90]) by smtp.gmail.com with ESMTPSA id e7sm8976565vkn.20.2021.11.15.10.37.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Nov 2021 10:37:45 -0800 (PST) To: libc-alpha@sourceware.org, Florian Weimer Subject: [PATCH v6 04/20] elf: Add _dl_audit_objsearch Date: Mon, 15 Nov 2021 15:37:18 -0300 Message-Id: <20211115183734.531155-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211115183734.531155-1-adhemerval.zanella@linaro.org> References: <20211115183734.531155-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, 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_objsearch() audit callback. Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu. --- elf/dl-audit.c | 37 +++++++++++++++++++++++ elf/dl-load.c | 61 ++++---------------------------------- sysdeps/generic/ldsodefs.h | 2 ++ 3 files changed, 44 insertions(+), 56 deletions(-) diff --git a/elf/dl-audit.c b/elf/dl-audit.c index 5fbc76a36c..de85ef1ddd 100644 --- a/elf/dl-audit.c +++ b/elf/dl-audit.c @@ -42,6 +42,43 @@ _dl_audit_activity_nsid (Lmid_t nsid, int action) _dl_audit_activity_map (head, action); } +bool +_dl_audit_objsearch (const char **name, const char **origname, + struct link_map *l, unsigned int code) +{ + if (__glibc_likely (GLRO(dl_naudit) == 0) + || l == NULL || l->l_auditing + || code == 0) + return true; + + struct audit_ifaces *afct = GLRO(dl_audit); + for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) + { + if (afct->objsearch != NULL) + { + const char *before = *name; + struct auditstate *state = link_map_audit_state (l, cnt); + *name = afct->objsearch (*name, &state->cookie, code); + if (*name == NULL) + return false; + + if (origname != NULL && before != *name + && strcmp (before, *name) != 0) + { + if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES)) + _dl_debug_printf ("audit changed filename %s -> %s\n", + before, *name); + + if (*origname == NULL) + *origname = before; + } + } + afct = afct->next; + } + + return true; +} + void _dl_audit_objopen (struct link_map *l, Lmid_t nsid, bool check_audit) { diff --git a/elf/dl-load.c b/elf/dl-load.c index 848d3e08eb..e28893b779 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -1595,33 +1595,8 @@ open_verify (const char *name, int fd, #ifdef SHARED /* Give the auditing libraries a chance. */ - if (__glibc_unlikely (GLRO(dl_naudit) > 0) && whatcode != 0 - && loader->l_auditing == 0) - { - const char *original_name = name; - struct audit_ifaces *afct = GLRO(dl_audit); - for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) - { - if (afct->objsearch != NULL) - { - struct auditstate *state = link_map_audit_state (loader, cnt); - name = afct->objsearch (name, &state->cookie, whatcode); - if (name == NULL) - /* Ignore the path. */ - return -1; - } - - afct = afct->next; - } - - if (fd != -1 && name != original_name && strcmp (name, original_name)) - { - /* An audit library changed what we're supposed to open, - so FD no longer matches it. */ - __close_nocancel (fd); - fd = -1; - } - } + if (!_dl_audit_objsearch (&name, NULL, loader, whatcode)) + return -1; #endif if (fd == -1) @@ -2059,36 +2034,10 @@ _dl_map_object (struct link_map *loader, const char *name, #ifdef SHARED /* Give the auditing libraries a chance to change the name before we try anything. */ - if (__glibc_unlikely (GLRO(dl_naudit) > 0) - && (loader == NULL || loader->l_auditing == 0)) + if (!_dl_audit_objsearch (&name, &origname, loader, LA_SER_ORIG)) { - struct audit_ifaces *afct = GLRO(dl_audit); - for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) - { - if (afct->objsearch != NULL) - { - const char *before = name; - struct auditstate *state = link_map_audit_state (loader, cnt); - name = afct->objsearch (name, &state->cookie, LA_SER_ORIG); - if (name == NULL) - { - /* Do not try anything further. */ - fd = -1; - goto no_file; - } - if (before != name && strcmp (before, name) != 0) - { - if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES)) - _dl_debug_printf ("audit changed filename %s -> %s\n", - before, name); - - if (origname == NULL) - origname = before; - } - } - - afct = afct->next; - } + fd = -1; + goto no_file; } #endif diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index ea481cbe84..b1c76361d2 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -1392,6 +1392,8 @@ link_map_audit_state (struct link_map *l, size_t index) } } +bool _dl_audit_objsearch (const char **name, const char **origname, + struct link_map *l, unsigned int code); /* 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)