From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 67167 invoked by alias); 8 Feb 2020 19:01:16 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Received: (qmail 67104 invoked by uid 89); 8 Feb 2020 19:01:16 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-18.0 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,KAM_SHORT,KAM_STOCKGEN,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy=Auditing, auditing X-HELO: us-smtp-delivery-1.mimecast.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581188472; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DHZ1+5syt4FYsdT0jjbiphEcmBeJxOfn4ao29fjpt8U=; b=btvwqbN2SuKTa2FhNcFFoFOdKDE2bak6/reW5HI/dyqa+Psqbo5ES2HvCMiXY0C/fG9Vrf aFpUypVgRaIa8uOcBU1Tc9bR8mN5CJg+5e3AM2Tu/g74FZbZt81/yzAZ/ZL5yYV3E2vmPh jolzESn/DwW2wp2bVJZfweQ4TfcBhOo= From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 2/4] elf: Extract _dl_sym_post, _dl_sym_find_caller_map from elf/dl-sym.c In-Reply-To: References: Message-Id: <68608ef426b88e73e0e3178497bcfe34dd8a66ef.1581182210.git.fweimer@redhat.com> Date: Sat, 08 Feb 2020 19:01:00 -0000 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: quoted-printable X-SW-Source: 2020-02/txt/msg00178.txt.bz2 The definitions are moved into a new file, elf/dl-sym-post.h, so that this code can be used by the dynamic loader as well. --- elf/dl-sym-post.h | 106 ++++++++++++++++++++++++++++++++++++++++++++++ elf/dl-sym.c | 86 ++----------------------------------- 2 files changed, 110 insertions(+), 82 deletions(-) create mode 100644 elf/dl-sym-post.h diff --git a/elf/dl-sym-post.h b/elf/dl-sym-post.h new file mode 100644 index 0000000000..4c4f574633 --- /dev/null +++ b/elf/dl-sym-post.h @@ -0,0 +1,106 @@ +/* Post-processing of a symbol produced by dlsym, dlvsym. + Copyright (C) 1999-2020 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 + . */ + + +/* Return the link map containing the caller address. */ +static struct link_map * +_dl_sym_find_caller_link_map (ElfW(Addr) caller) +{ + struct link_map *l =3D _dl_find_dso_for_object (caller); + if (l !=3D NULL) + return l; + else + /* If the address is not recognized the call comes from the main + program (we hope). */ + return GL(dl_ns)[LM_ID_BASE]._ns_loaded; +} + +/* Translates RESULT, *REF, VALUE into a symbol address from the point + of view of MATCH. Performs IFUNC resolution and auditing if + necessary. If MATCH is NULL, CALLER is used to determine it. */ +static void * +_dl_sym_post (lookup_t result, const ElfW(Sym) *ref, void *value, + ElfW(Addr) caller, struct link_map *match) +{ + /* Resolve indirect function address. */ + if (__glibc_unlikely (ELFW(ST_TYPE) (ref->st_info) =3D=3D STT_GNU_IFUNC)) + { + DL_FIXUP_VALUE_TYPE fixup + =3D DL_FIXUP_MAKE_VALUE (result, (ElfW(Addr)) value); + fixup =3D elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (fixup)); + value =3D (void *) DL_FIXUP_VALUE_CODE_ADDR (fixup); + } + +#ifdef SHARED + /* Auditing checkpoint: we have a new binding. Provide the + auditing libraries the possibility to change the value and + tell us whether further auditing is wanted. */ + if (__glibc_unlikely (GLRO(dl_naudit) > 0)) + { + const char *strtab =3D (const char *) D_PTR (result, + l_info[DT_STRTAB]); + /* Compute index of the symbol entry in the symbol table of + the DSO with the definition. */ + unsigned int ndx =3D (ref - (ElfW(Sym) *) D_PTR (result, + l_info[DT_SYMTAB])); + + if (match =3D=3D NULL) + match =3D _dl_sym_find_caller_link_map (caller); + + if ((match->l_audit_any_plt | result->l_audit_any_plt) !=3D 0) + { + unsigned int altvalue =3D 0; + struct audit_ifaces *afct =3D GLRO(dl_audit); + /* Synthesize a symbol record where the st_value field is + the result. */ + ElfW(Sym) sym =3D *ref; + sym.st_value =3D (ElfW(Addr)) value; + + for (unsigned int cnt =3D 0; cnt < GLRO(dl_naudit); ++cnt) + { + struct auditstate *match_audit + =3D link_map_audit_state (match, cnt); + struct auditstate *result_audit + =3D link_map_audit_state (result, cnt); + if (afct->symbind !=3D NULL + && ((match_audit->bindflags & LA_FLG_BINDFROM) !=3D 0 + || ((result_audit->bindflags & LA_FLG_BINDTO) + !=3D 0))) + { + unsigned int flags =3D altvalue | LA_SYMB_DLSYM; + uintptr_t new_value + =3D afct->symbind (&sym, ndx, + &match_audit->cookie, + &result_audit->cookie, + &flags, strtab + ref->st_name); + if (new_value !=3D (uintptr_t) sym.st_value) + { + altvalue =3D LA_SYMB_ALTVALUE; + sym.st_value =3D new_value; + } + } + + afct =3D afct->next; + } + + value =3D (void *) sym.st_value; + } + } +#endif + return value; +} diff --git a/elf/dl-sym.c b/elf/dl-sym.c index b43a50e544..361b926ea9 100644 --- a/elf/dl-sym.c +++ b/elf/dl-sym.c @@ -28,6 +28,7 @@ #include #include #include +#include =20 =20 #ifdef SHARED @@ -80,19 +81,6 @@ call_dl_lookup (void *ptr) args->flags, NULL); } =20 -/* Return the link map containing the caller address. */ -static inline struct link_map * -find_caller_link_map (ElfW(Addr) caller) -{ - struct link_map *l =3D _dl_find_dso_for_object (caller); - if (l !=3D NULL) - return l; - else - /* If the address is not recognized the call comes from the main - program (we hope). */ - return GL(dl_ns)[LM_ID_BASE]._ns_loaded; -} - static void * do_sym (void *handle, const char *name, void *who, struct r_found_version *vers, int flags) @@ -106,7 +94,7 @@ do_sym (void *handle, const char *name, void *who, =20 if (handle =3D=3D RTLD_DEFAULT) { - match =3D find_caller_link_map (caller); + match =3D _dl_sym_find_caller_link_map (caller); =20 /* Search the global scope. We have the simple case where we look up in the scope of an object which was part of @@ -140,7 +128,7 @@ do_sym (void *handle, const char *name, void *who, } else if (handle =3D=3D RTLD_NEXT) { - match =3D find_caller_link_map (caller); + match =3D _dl_sym_find_caller_link_map (caller); =20 if (__glibc_unlikely (match =3D=3D GL(dl_ns)[LM_ID_BASE]._ns_loaded)) { @@ -179,73 +167,7 @@ RTLD_NEXT used in code not dynamically loaded")); #endif value =3D DL_SYMBOL_ADDRESS (result, ref); =20 - /* Resolve indirect function address. */ - if (__glibc_unlikely (ELFW(ST_TYPE) (ref->st_info) =3D=3D STT_GNU_IF= UNC)) - { - DL_FIXUP_VALUE_TYPE fixup - =3D DL_FIXUP_MAKE_VALUE (result, (ElfW(Addr)) value); - fixup =3D elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (fixup)); - value =3D (void *) DL_FIXUP_VALUE_CODE_ADDR (fixup); - } - -#ifdef SHARED - /* Auditing checkpoint: we have a new binding. Provide the - auditing libraries the possibility to change the value and - tell us whether further auditing is wanted. */ - if (__glibc_unlikely (GLRO(dl_naudit) > 0)) - { - const char *strtab =3D (const char *) D_PTR (result, - l_info[DT_STRTAB]); - /* Compute index of the symbol entry in the symbol table of - the DSO with the definition. */ - unsigned int ndx =3D (ref - (ElfW(Sym) *) D_PTR (result, - l_info[DT_SYMTAB])); - - if (match =3D=3D NULL) - match =3D find_caller_link_map (caller); - - if ((match->l_audit_any_plt | result->l_audit_any_plt) !=3D 0) - { - unsigned int altvalue =3D 0; - struct audit_ifaces *afct =3D GLRO(dl_audit); - /* Synthesize a symbol record where the st_value field is - the result. */ - ElfW(Sym) sym =3D *ref; - sym.st_value =3D (ElfW(Addr)) value; - - for (unsigned int cnt =3D 0; cnt < GLRO(dl_naudit); ++cnt) - { - struct auditstate *match_audit - =3D link_map_audit_state (match, cnt); - struct auditstate *result_audit - =3D link_map_audit_state (result, cnt); - if (afct->symbind !=3D NULL - && ((match_audit->bindflags & LA_FLG_BINDFROM) !=3D 0 - || ((result_audit->bindflags & LA_FLG_BINDTO) - !=3D 0))) - { - unsigned int flags =3D altvalue | LA_SYMB_DLSYM; - uintptr_t new_value - =3D afct->symbind (&sym, ndx, - &match_audit->cookie, - &result_audit->cookie, - &flags, strtab + ref->st_name); - if (new_value !=3D (uintptr_t) sym.st_value) - { - altvalue =3D LA_SYMB_ALTVALUE; - sym.st_value =3D new_value; - } - } - - afct =3D afct->next; - } - - value =3D (void *) sym.st_value; - } - } -#endif - - return value; + return _dl_sym_post (result, ref, value, caller, match); } =20 return NULL; --=20 2.24.1