From nobody Wed Nov 27 01:16:19 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XyhNM6cjFz5fJPf; Wed, 27 Nov 2024 01:16:19 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XyhNM63vsz4b0x; Wed, 27 Nov 2024 01:16:19 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732670179; 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; bh=gjunk7IWpRRVMjfwNOSvh+rr1Zvntt/Wf8oJyLwWYBo=; b=eEldMbqTCOkfXA+VMPnQRoszwplWBcXksv3j7DzdVtTV13zYpPFPt0I5BHgyAkHUGg9+XU r2ppUy8pDWPY6jH0HcKhAbMK3lYgZBcksqjogUlZxciSLqK/KTbcdknJ0g3bLw/oAJnfFv waVBZVcvnIytqBQ45ROWy5WfpJWH3CL9ySgKCLrEfoUZ6d/78UiYweHAnKG5ZB9EoJPU6c YK2NaWucL9bnjZ+Nt3/Hgf/XkTjtzTJyce6cPjR/r0YpnisikWCYy4EcOSD2hwdnWvEgdY 9C3/9+nueRv9YM7IjYTlTjj+7lh9t0KqFgT6f60m65P/98xL7lb1Sv5Ee+w7OQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732670179; 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; bh=gjunk7IWpRRVMjfwNOSvh+rr1Zvntt/Wf8oJyLwWYBo=; b=w39ZpUE75YcmsxK4/6dpcTM+ycluTC2xqBkoby4DRPzQh64UvOYND/s/1ysMwOf2UI6m3h 5kKtqyYh2pjmH++ByJV52RjrI2NSaaXI02UPJC0eGvmatSEw1kukx8cYnUAghPEq0SXfvY jOzAi3/MDSOUJ/TyeFKa7s2Zj5syqkAbMX3dN6jCBs4PG54oAKymdgXrtlmdXsHfsZ9J86 Psb3WgRyBTfSMy2b8FDVFoi7xLLUeXG3I+PrOOIrVCkWLxeP9l/c+8G7pQ4J9056hmzjkr Q/YgWeqsnWr49Tm3JLdSM9R1JltWgYw1zgq9O7g+oZcA0KNkhVfbKfFRtZ64jg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1732670179; a=rsa-sha256; cv=none; b=dwhQ4VGdKzFp4oiEcImLNTHHu7dCFU/nHdUb5dRxwexH+TboOmBfWH1jWkDPN3TI0/mdnA XoJKagP+bxRCKuyxyh5duvLr8UdVj7xZRNH5M/DNzcoxH0/U8zsVIL9HDynFsO35zmfe0x cDHWIQiyYwIkJ30Sq0Whfbs0eP+oNq0jUtFk7jKI+9SESBuF+RFMdA2jF1T0m4wiL7XvlV FKNCUi15NhMS9oU89N2zwTjrNgs6zIlDeC3E+gN0C8O5D8XX2UZgVtm3e9Wf9JJKcgPRGQ FKbwbC4cSbUR97vGa2HknuxJjNC8rBCflUXThdJ0HbGW3yvUR5Un1QfEP9QEIw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XyhNM5XplzpSK; Wed, 27 Nov 2024 01:16:19 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4AR1GJTX055755; Wed, 27 Nov 2024 01:16:19 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4AR1GJs3055752; Wed, 27 Nov 2024 01:16:19 GMT (envelope-from git) Date: Wed, 27 Nov 2024 01:16:19 GMT Message-Id: <202411270116.4AR1GJs3055752@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kyle Evans Subject: git: 708106fb8086 - stable/14 - rtld: implement _dl_iterate_phdr_locked List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 708106fb8086e85afa4a9aed3872a329c702797b Auto-Submitted: auto-generated The branch stable/14 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=708106fb8086e85afa4a9aed3872a329c702797b commit 708106fb8086e85afa4a9aed3872a329c702797b Author: Kyle Evans AuthorDate: 2024-11-14 01:33:59 +0000 Commit: Kyle Evans CommitDate: 2024-11-27 01:15:56 +0000 rtld: implement _dl_iterate_phdr_locked Some sanitizers need to be able to use dl_iterate_phdr() after stopping the rest of the process, but it's very hard to do so reliably as a non-participant in the main logic of the program. Introduce _dl_iterate_phdr_locked to bypass the locking that's normally required for dl_iterate_phdr() and slap some scary warning on it. It will remain undocumented and probably shouldn't be used for anything else. Reviewed by: kib (cherry picked from commit 1426fd6cff0603f0ee275b99f2ba35dc36f3d0c2) --- lib/libc/gen/Symbol.map | 1 + lib/libc/gen/dlfcn.c | 24 ++++++++++++++++++++---- libexec/rtld-elf/Symbol.map | 1 + libexec/rtld-elf/rtld.c | 24 ++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/lib/libc/gen/Symbol.map b/lib/libc/gen/Symbol.map index 8e4a26df8f08..5178ce56da33 100644 --- a/lib/libc/gen/Symbol.map +++ b/lib/libc/gen/Symbol.map @@ -469,6 +469,7 @@ FBSDprivate_1.0 { /* needed by thread libraries */ __thr_jtable; + _dl_iterate_phdr_locked; _pthread_atfork; _pthread_attr_destroy; _pthread_attr_getdetachstate; diff --git a/lib/libc/gen/dlfcn.c b/lib/libc/gen/dlfcn.c index b30cb82e5e43..b814fdc845c4 100644 --- a/lib/libc/gen/dlfcn.c +++ b/lib/libc/gen/dlfcn.c @@ -203,9 +203,10 @@ dl_init_phdr_info(void) } #endif -#pragma weak dl_iterate_phdr +#pragma weak _dl_iterate_phdr_locked int -dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, size_t, void *) __unused, +_dl_iterate_phdr_locked( + int (*callback)(struct dl_phdr_info *, size_t, void *) __unused, void *data __unused) { #if defined IN_LIBDL @@ -227,14 +228,29 @@ dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, size_t, void *) __unused, _once(&dl_phdr_info_once, dl_init_phdr_info); ti.ti_module = 1; ti.ti_offset = 0; - mutex_lock(&dl_phdr_info_lock); phdr_info.dlpi_tls_data = __tls_get_addr(&ti); ret = callback(&phdr_info, sizeof(phdr_info), data); - mutex_unlock(&dl_phdr_info_lock); return (ret); #endif } +#pragma weak dl_iterate_phdr +int +dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, size_t, void *) __unused, + void *data __unused) +{ + int error; + +#if !defined(IN_LIBDL) && !defined(PIC) + mutex_lock(&dl_phdr_info_lock); +#endif + error = _dl_iterate_phdr_locked(callback, data); +#if !defined(IN_LIBDL) && !defined(PIC) + mutex_unlock(&dl_phdr_info_lock); +#endif + return (error); +} + #pragma weak fdlopen void * fdlopen(int fd __unused, int mode __unused) diff --git a/libexec/rtld-elf/Symbol.map b/libexec/rtld-elf/Symbol.map index 3cdbb30d04a0..9e9e702a1261 100644 --- a/libexec/rtld-elf/Symbol.map +++ b/libexec/rtld-elf/Symbol.map @@ -27,6 +27,7 @@ FBSD_1.8 { }; FBSDprivate_1.0 { + _dl_iterate_phdr_locked; _rtld_thread_init; _rtld_allocate_tls; _rtld_free_tls; diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c index 32d0b879c5bc..e1813217a854 100644 --- a/libexec/rtld-elf/rtld.c +++ b/libexec/rtld-elf/rtld.c @@ -253,6 +253,7 @@ int dladdr(const void *, Dl_info *) __exported; void dllockinit(void *, void *(*)(void *), void (*)(void *), void (*)(void *), void (*)(void *), void (*)(void *), void (*)(void *)) __exported; int dlinfo(void *, int , void *) __exported; +int _dl_iterate_phdr_locked(__dl_iterate_hdr_callback, void *) __exported; int dl_iterate_phdr(__dl_iterate_hdr_callback, void *) __exported; int _rtld_addr_phdr(const void *, struct dl_phdr_info *) __exported; int _rtld_get_stack_prot(void) __exported; @@ -4208,6 +4209,29 @@ rtld_fill_dl_phdr_info(const Obj_Entry *obj, struct dl_phdr_info *phdr_info) phdr_info->dlpi_subs = obj_loads - obj_count; } +/* + * It's completely UB to actually use this, so extreme caution is advised. It's + * probably not what you want. + */ +int +_dl_iterate_phdr_locked(__dl_iterate_hdr_callback callback, void *param) +{ + struct dl_phdr_info phdr_info; + Obj_Entry *obj; + int error; + + for (obj = globallist_curr(TAILQ_FIRST(&obj_list)); obj != NULL; + obj = globallist_next(obj)) { + rtld_fill_dl_phdr_info(obj, &phdr_info); + error = callback(&phdr_info, sizeof(phdr_info), param); + if (error != 0) + return (error); + } + + rtld_fill_dl_phdr_info(&obj_rtld, &phdr_info); + return (callback(&phdr_info, sizeof(phdr_info), param)); +} + int dl_iterate_phdr(__dl_iterate_hdr_callback callback, void *param) {