From patchwork Thu Oct 14 17:47:43 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: 46235 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 2FF453857C53 for ; Thu, 14 Oct 2021 17:57:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2FF453857C53 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1634234226; bh=Kr/LUSUTQxbgHgE5paHs9QZ3UrCOgsALkVjIyJYsXh4=; 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=peKsrMOPIDkT710iP/2feQiMwNdyzihWEub/15VAkNSw1h5/U+FEcgPVL5sCj8K3I qPfSaF5AK/cCNEQBDaqum1K7lLO/Vf+TKZ5uH0yYlEzFHo5YY+2AE6/IJKXw4wHrxV RFX+4KBlkafc4GhftDtboRaKcaPyNaVV006mT1Fk= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-vk1-xa2d.google.com (mail-vk1-xa2d.google.com [IPv6:2607:f8b0:4864:20::a2d]) by sourceware.org (Postfix) with ESMTPS id CB5183858419 for ; Thu, 14 Oct 2021 17:48:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CB5183858419 Received: by mail-vk1-xa2d.google.com with SMTP id z202so3892943vkd.1 for ; Thu, 14 Oct 2021 10:48:17 -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=Kr/LUSUTQxbgHgE5paHs9QZ3UrCOgsALkVjIyJYsXh4=; b=0FGKbYu7hRl8cZ0bvKnC6QAWJAHNcPYXHdg7gWvZEJnP5iEa/7Uas4ph7WkB9m9xu2 f9fVgKFT6Ez1qBEeJuR4Pe5BCk90FXDHHvntGoP4egU1Gic3jY4LgjeZNN7dsM9Sostc 721sLk9RAzQtSu88GP4iSyoika+rhAYNf9WiDXJ9ZVTlN0K84NGlSKDM8gAyEOh8lGVh slgtpMtgJLqI+KxVcxp7+zWWnM5rVaVMUiAJSy4TFh4/umF22EEqfTuCP3zgFnS0E0Hi 8uFJ+UOiWR0N3QWMSxTOGufBkRxxysY/jBliA9gWmKHFgjL7V1R5M3gJiDYE4jKr/Rep lCLQ== X-Gm-Message-State: AOAM532WP7SYeJdLRPaZzf//nylY1DSkknmPhYsvQioJ6xPYp4/7V85I 7joQtAiQwo3GdZd3BwsSM9c0sNMYolA= X-Google-Smtp-Source: ABdhPJwa09i31wiJWH81EjspirUK1pCj65UrP7sMChSb42+kJ9aym/Bk36Lll8MOaVgcfZIf5C9J7A== X-Received: by 2002:a1f:2f8d:: with SMTP id v135mr7578433vkv.18.1634233697216; Thu, 14 Oct 2021 10:48:17 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:c6c7:3c65:db0e:6fcc:a8e9]) by smtp.gmail.com with ESMTPSA id q26sm2107237vkn.40.2021.10.14.10.48.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Oct 2021 10:48:17 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v4 12/21] elf: Add _dl_audit_pltenter Date: Thu, 14 Oct 2021 14:47:43 -0300 Message-Id: <20211014174752.1651347-13-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211014174752.1651347-1-adhemerval.zanella@linaro.org> References: <20211014174752.1651347-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, KAM_STOCKGEN, 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_pltenter() audit callback. Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu. --- elf/dl-audit.c | 77 ++++++++++++++++++++++++++++++++++++++ elf/dl-runtime.c | 73 +----------------------------------- sysdeps/generic/ldsodefs.h | 3 ++ 3 files changed, 81 insertions(+), 72 deletions(-) diff --git a/elf/dl-audit.c b/elf/dl-audit.c index a968cbde16..7d410bc128 100644 --- a/elf/dl-audit.c +++ b/elf/dl-audit.c @@ -17,7 +17,9 @@ . */ #include +#include #include +#include #ifdef SHARED void @@ -257,4 +259,79 @@ _dl_audit_symbind (struct link_map *l, struct reloc_result *reloc_result, reloc_result->flags = flags; *value = DL_FIXUP_ADDR_VALUE (sym.st_value); } + +void +_dl_audit_pltenter (struct link_map *l, struct reloc_result *reloc_result, + DL_FIXUP_VALUE_TYPE *value, void *regs, long int *framesize) +{ + /* Don't do anything if no auditor wants to intercept this call. */ + if (GLRO(dl_naudit) == 0 + || (reloc_result->enterexit & LA_SYMB_NOPLTENTER)) + return; + + /* Sanity check: DL_FIXUP_VALUE_CODE_ADDR (value) should have been + initialized earlier in this function or in another thread. */ + assert (DL_FIXUP_VALUE_CODE_ADDR (*value) != 0); + ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound, + l_info[DT_SYMTAB]) + + reloc_result->boundndx); + + /* Set up the sym parameter. */ + ElfW(Sym) sym = *defsym; + sym.st_value = DL_FIXUP_VALUE_ADDR (*value); + + /* Get the symbol name. */ + const char *strtab = (const void *) D_PTR (reloc_result->bound, + l_info[DT_STRTAB]); + const char *symname = strtab + sym.st_name; + + /* Keep track of overwritten addresses. */ + unsigned int flags = reloc_result->flags; + + struct audit_ifaces *afct = GLRO(dl_audit); + for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) + { + if (afct->ARCH_LA_PLTENTER != NULL + && (reloc_result->enterexit + & (LA_SYMB_NOPLTENTER << (2 * (cnt + 1)))) == 0) + { + long int new_framesize = -1; + struct auditstate *l_state = link_map_audit_state (l, cnt); + struct auditstate *bound_state + = link_map_audit_state (reloc_result->bound, cnt); + uintptr_t new_value + = afct->ARCH_LA_PLTENTER (&sym, reloc_result->boundndx, + &l_state->cookie, &bound_state->cookie, + regs, &flags, symname, &new_framesize); + if (new_value != (uintptr_t) sym.st_value) + { + flags |= LA_SYMB_ALTVALUE; + sym.st_value = new_value; + } + + /* Remember the results for every audit library and store a summary + in the first two bits. */ + reloc_result->enterexit |= ((flags & (LA_SYMB_NOPLTENTER + | LA_SYMB_NOPLTEXIT)) + << (2 * (cnt + 1))); + + if ((reloc_result->enterexit & (LA_SYMB_NOPLTEXIT + << (2 * (cnt + 1)))) + == 0 && new_framesize != -1 && *framesize != -2) + { + /* If this is the first call providing information, use it. */ + if (*framesize == -1) + *framesize = new_framesize; + /* If two pltenter calls provide conflicting information, use + the larger value. */ + else if (new_framesize != *framesize) + *framesize = MAX (new_framesize, *framesize); + } + } + + afct = afct->next; + } + + *value = DL_FIXUP_ADDR_VALUE (sym.st_value); +} #endif diff --git a/elf/dl-runtime.c b/elf/dl-runtime.c index bfbe8f4be4..c3fc6777ff 100644 --- a/elf/dl-runtime.c +++ b/elf/dl-runtime.c @@ -352,78 +352,7 @@ _dl_profile_fixup ( #ifdef SHARED /* Auditing checkpoint: report the PLT entering and allow the auditors to change the value. */ - if (GLRO(dl_naudit) > 0 - /* Don't do anything if no auditor wants to intercept this call. */ - && (reloc_result->enterexit & LA_SYMB_NOPLTENTER) == 0) - { - /* Sanity check: DL_FIXUP_VALUE_CODE_ADDR (value) should have been - initialized earlier in this function or in another thread. */ - assert (DL_FIXUP_VALUE_CODE_ADDR (value) != 0); - ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound, - l_info[DT_SYMTAB]) - + reloc_result->boundndx); - - /* Set up the sym parameter. */ - ElfW(Sym) sym = *defsym; - sym.st_value = DL_FIXUP_VALUE_ADDR (value); - - /* Get the symbol name. */ - const char *strtab = (const void *) D_PTR (reloc_result->bound, - l_info[DT_STRTAB]); - const char *symname = strtab + sym.st_name; - - /* Keep track of overwritten addresses. */ - unsigned int flags = reloc_result->flags; - - struct audit_ifaces *afct = GLRO(dl_audit); - for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) - { - if (afct->ARCH_LA_PLTENTER != NULL - && (reloc_result->enterexit - & (LA_SYMB_NOPLTENTER << (2 * (cnt + 1)))) == 0) - { - long int new_framesize = -1; - struct auditstate *l_state = link_map_audit_state (l, cnt); - struct auditstate *bound_state - = link_map_audit_state (reloc_result->bound, cnt); - uintptr_t new_value - = afct->ARCH_LA_PLTENTER (&sym, reloc_result->boundndx, - &l_state->cookie, - &bound_state->cookie, - regs, &flags, symname, - &new_framesize); - if (new_value != (uintptr_t) sym.st_value) - { - flags |= LA_SYMB_ALTVALUE; - sym.st_value = new_value; - } - - /* Remember the results for every audit library and - store a summary in the first two bits. */ - reloc_result->enterexit - |= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT)) - << (2 * (cnt + 1))); - - if ((reloc_result->enterexit & (LA_SYMB_NOPLTEXIT - << (2 * (cnt + 1)))) - == 0 && new_framesize != -1 && framesize != -2) - { - /* If this is the first call providing information, - use it. */ - if (framesize == -1) - framesize = new_framesize; - /* If two pltenter calls provide conflicting information, - use the larger value. */ - else if (new_framesize != framesize) - framesize = MAX (new_framesize, framesize); - } - } - - afct = afct->next; - } - - value = DL_FIXUP_ADDR_VALUE (sym.st_value); - } + _dl_audit_pltenter (l, reloc_result, &value, regs, &framesize); #endif /* Store the frame size information. */ diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index c69c54200f..39762e1db3 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -1382,6 +1382,9 @@ void _dl_audit_symbind (struct link_map *l, struct reloc_result *reloc_result, void _dl_audit_symbind_alt (struct link_map *l, const ElfW(Sym) *ref, void **value, lookup_t result); rtld_hidden_proto (_dl_audit_symbind_alt) +void _dl_audit_pltenter (struct link_map *l, struct reloc_result *reloc_result, + DL_FIXUP_VALUE_TYPE *value, void *regs, + long int *framesize); #endif /* SHARED */ #if PTHREAD_IN_LIBC && defined SHARED