From nobody Thu Nov 14 01:34:28 2024 X-Original-To: dev-commits-src-main@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 4XpjPJ5gv3z5csqT; Thu, 14 Nov 2024 01:34:28 +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 4XpjPJ1lckz42bY; Thu, 14 Nov 2024 01:34:28 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1731548068; 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=dRnwVA4sIkIhCUN5Vk2VnVVXq+ZvMc1ufbh1gXEicwg=; b=RNvgPL1ZaXZ/hY8aEK7kJUSsqVxUpKgemFkjs+zqKjrqUcWa14fZjyMIwC9pTRD1d2DsLA vrKRVuWBb40in2QWDje8zMOZXIOUOjNFDL16fH/LE3qJ0Q4lcAFgqf1cMIU865RWLOyxxw C3IL7+tGUoKSLWDb8L+McXyTmePVc+rJhbxMP2O9D7ijiGNUrl6w5rymr1KouEv5NPajqF WP9nQtfx1xSWII9RZhXCmO9R+VbvttQiDptT1ru1ewEwLyH1whcMo3xYNJe2+ezH6CMt8Q M8XJTWoZ2MB1HpSaxb9bnE9AwaOkuhlpyVdRSDmWqA1Igj+qld/KMAjNBwhztg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1731548068; 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=dRnwVA4sIkIhCUN5Vk2VnVVXq+ZvMc1ufbh1gXEicwg=; b=XZIJRg2bCXU+TRwEURDwBo+4j+0fJBDi2atLtjUq7KpZIPEkrpywdJHVwfSG9mM6r/4y/j XYNdAoNIPdHueVGCX32aL1SJBlcV/pL1LWgQTKlX0NLed3sQawSuFBVhhpHQX1zMk8qC+i ieZJzAqO/SkRrrIWB2CtFb3uyov74kS+a/XBapQd0dxBWIU5ZOulU2IjLNrHEa/2a5Y8LM RjK9dNMqpIeygfHFv1mG7AzTUELnWNoShDWwCzX95lj7Tw7eRaZhdRigu1opFA/Ls8Hfew OT4Ue4VIteONvhFDqpMzICPStuSH7hDEBde4NxwMRuygEeCbEe5nLkaFzLp8MQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1731548068; a=rsa-sha256; cv=none; b=BvuWC71G+Wn6Dn/i4X66zyRdJOw8IFgVgSBd8quMTaMzEXqdKs8cZREBK8LuofjhH8fplo jDbftUXj8+lWOH1qPvY0vOek+V6Hg3cP15lI9ne0X64aTDHx8kxyNvewKJV3bEVPuZ7ICV YKPMHmccjjrJD3+0WAWtQBuSrGfy4a7f0eB5qyyYUuNEEzQ4g0QS5vA1NKyAkC55zr7H+F ouj8zJmxOQh3NOO4F4J33KURNbfqEJoUw6nlYsOaSo8T1kIs8CVlCkFX4+ToigsR1lS172 0f6QHPZoghw9v0/127miZFZ2TMdqjcrKTLSVL807eDc0l9GdLgWx8ImzJxFvng== 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 4XpjPJ1M5JzJm3; Thu, 14 Nov 2024 01:34:28 +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 4AE1YSjY018072; Thu, 14 Nov 2024 01:34:28 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4AE1YSun018069; Thu, 14 Nov 2024 01:34:28 GMT (envelope-from git) Date: Thu, 14 Nov 2024 01:34:28 GMT Message-Id: <202411140134.4AE1YSun018069@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kyle Evans Subject: git: 1426fd6cff06 - main - rtld: implement _dl_iterate_phdr_locked List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: 1426fd6cff0603f0ee275b99f2ba35dc36f3d0c2 Auto-Submitted: auto-generated The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=1426fd6cff0603f0ee275b99f2ba35dc36f3d0c2 commit 1426fd6cff0603f0ee275b99f2ba35dc36f3d0c2 Author: Kyle Evans AuthorDate: 2024-11-14 01:33:59 +0000 Commit: Kyle Evans CommitDate: 2024-11-14 01:33:59 +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 Differential Revision: https://reviews.freebsd.org/D47558 --- 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 df950697cf23..4f77023dad72 100644 --- a/lib/libc/gen/Symbol.map +++ b/lib/libc/gen/Symbol.map @@ -464,6 +464,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 f1075bc75d5f..9e5876e26453 100644 --- a/libexec/rtld-elf/rtld.c +++ b/libexec/rtld-elf/rtld.c @@ -252,6 +252,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; @@ -4201,6 +4202,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) {