From patchwork Tue Jun 11 15:27:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 1946406 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=iCdMvUNU; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VzCQ92bYsz20Tk for ; Wed, 12 Jun 2024 01:34:09 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 804A5385842C for ; Tue, 11 Jun 2024 15:34:07 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by sourceware.org (Postfix) with ESMTPS id D2D7A385DDE5 for ; Tue, 11 Jun 2024 15:32:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D2D7A385DDE5 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D2D7A385DDE5 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::435 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718119962; cv=none; b=wJhtbpTPXDt2MNqOET2Mrj4VQjFREbiXIVPwfeKCiLnekRiRXx9l+xFn2YxgpGp8F7NILawx9gDTATmCofCr7lVCxGzzkKexPcnQr8kd7Sa56PmdARdT7EIMxonSJFveHZcvO+zzh12nuPgwSjzzWR8/Aplx97p3QaOCSBafhYM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718119962; c=relaxed/simple; bh=DfbxejrHFnW0UR7Qxn/DyvdFw8uu83lTIIi6oBEtmPs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Q/X6ixvknF+XsRf8jzdDEms6Yz1/hljo5P2Y+qsWOLujKZ0tE9FnLpMf8SmM6Hd+5+r5FxHCa0oLd1XFBdTlFxeCXwJOIp3ysFQZAx/+4UBhIHpX92HGQF+E/ZRc/wZrOcnmUFeOQOKmX7HciLM7gVNy7/JqCxZ+EdWlKhOiSCk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-70436ac8882so1143704b3a.2 for ; Tue, 11 Jun 2024 08:32:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1718119958; x=1718724758; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=d7OwYwsoI1uYihWPd3RXq9N6NWQFWWHaUyyCVwn96ak=; b=iCdMvUNUNeFmmr37dRKwrzOlGJGnkrncJu+b0D/9yBnCPCKCppzfzqtcH5reKiu6fc WQc7itXKYWgxOwkhWMLBPWwvfzILKHYB3GnemjXTpNHuo3Vadvx4jj8uk74KmmG4JQuw g0lFeG4gZ6/89FjKgq7QxnMFdkuAQZEXR0R8oceg+X0nYsQ3ITjXbQUcUo/OuPZX6IkG WLNZkMe7wPPCgGuGLWzx9TmbgYLqW8hLM3hQeZ4PZNBXIHQ4LupsafR6JG0gc7UiB1ap U7Zvwrs/3O2muQU8vcbb66GO5zWI5NCzOl8rG2rfY1R9aBSnZgkzalUP/pP3GU8tOT+I kt4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718119958; x=1718724758; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=d7OwYwsoI1uYihWPd3RXq9N6NWQFWWHaUyyCVwn96ak=; b=CGhWWSCIVtQEGERTeN4avPuFYXLW+KCdYdMZVziFtoz1oPbUEzcFXGudOvqmijXKTP G+6dlH0qprCNFRcaos6Vp1BGLwwt4sjfJiCNiZOHoClJWa0z3RpSq/KUM0IQoKjDf2+O C/VfHm0ne6ATharI8xDxlobHwy8pMVNsv/3MYYIj9kqHRiwJSHo3gKsVsSSKSUFbAn4i b6Rt20W80vA1TMR0joK5/8YBLjeYeHiVaU6DyLZ2m3UBvTe++dmrLh20ij0BD/z7T8gF 3he2o1ISMs9I1xD934/ib4JmTxjb/A6uKUrpD43jBrQCB9TWUZ256Xl2+UOnRy8oXySd LAzg== X-Gm-Message-State: AOJu0YyX34XlF6R5sIxh26mvpSyPMOA75e4gkquVtdXu3R86kRFVyYQH Yl0ojtrF5YZ82I/bpRWNN5XxfbsPkGx0u7j8GmyZKkdDYT8zuf1a6szC/gNNK3nFIEOGXmQ2Lpz 7 X-Google-Smtp-Source: AGHT+IHAa19gLiN928e7T2hOpu9sdrrrAyjC+LPMwSSnRNURQm6TTRbh8v+8j89YOt19ZAP7OlaO2A== X-Received: by 2002:a05:6a20:9755:b0:1af:f8bd:1e4e with SMTP id adf61e73a8af0-1b2f9dcc579mr9879024637.62.1718119958148; Tue, 11 Jun 2024 08:32:38 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:c5fb:1cf6:d480:34ef:aedf]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70422c977dasm6023811b3a.62.2024.06.11.08.32.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jun 2024 08:32:37 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Stephen Roettger , jeffxu@chromium.org, Carlos O'Donell , Florian Weimer Subject: [RFC 4/5] elf: Enable RTLD_NODELETE on __libc_unwind_link_get Date: Tue, 11 Jun 2024 12:27:07 -0300 Message-ID: <20240611153220.165430-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240611153220.165430-1-adhemerval.zanella@linaro.org> References: <20240611153220.165430-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 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, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org The libgcc_s.so can also be sealed. The library is loaded once and not unloaded during process execution (only for memory debug with __libc_unwind_link_freeres). Checked on x86_64-linux-gnu and aarch64-linux-gnu. --- include/dlfcn.h | 2 ++ manual/tunables.texi | 4 ++++ misc/unwind-link.c | 5 +++-- sysdeps/unix/sysv/linux/tst-dl_mseal.c | 13 +++++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/include/dlfcn.h b/include/dlfcn.h index f49ee1b0c9..06e2ecbdd2 100644 --- a/include/dlfcn.h +++ b/include/dlfcn.h @@ -50,6 +50,8 @@ extern char **__libc_argv attribute_hidden; better error handling semantics for the library. */ #define __libc_dlopen(name) \ __libc_dlopen_mode (name, RTLD_NOW | __RTLD_DLOPEN) +#define __libc_dlopen_nodelete(name) \ + __libc_dlopen_mode (name, RTLD_NODELETE | RTLD_NOW | __RTLD_DLOPEN) extern void *__libc_dlopen_mode (const char *__name, int __mode) attribute_hidden; extern void *__libc_dlsym (void *__map, const char *__name) diff --git a/manual/tunables.texi b/manual/tunables.texi index 26fba6641d..be36d52cf9 100644 --- a/manual/tunables.texi +++ b/manual/tunables.texi @@ -381,6 +381,10 @@ Any preload libraries. @item Any library loaded with @code{dlopen} with @code{RTLD_NODELETE} flag. + +@item +Any runtime library used for process unwind (such as required by @code{backtrace} +or @code{pthread_exit}). @end itemize The tunable accepts three diferent values: @samp{0} where sealing is disabled, diff --git a/misc/unwind-link.c b/misc/unwind-link.c index 213a0162a4..7267ecbec3 100644 --- a/misc/unwind-link.c +++ b/misc/unwind-link.c @@ -48,7 +48,7 @@ __libc_unwind_link_get (void) /* Initialize a copy of the data, so that we do not need about unlocking in case the dynamic loader somehow triggers unwinding. */ - void *local_libgcc_handle = __libc_dlopen (LIBGCC_S_SO); + void *local_libgcc_handle = __libc_dlopen_nodelete (LIBGCC_S_SO); if (local_libgcc_handle == NULL) { __libc_lock_unlock (lock); @@ -100,7 +100,8 @@ __libc_unwind_link_get (void) __libc_lock_lock (lock); if (atomic_load_relaxed (&global_libgcc_handle) != NULL) - /* This thread lost the race. Clean up. */ + /* This thread lost the race. Drop the l_direct_opencount and issue + the debug log. */ __libc_dlclose (local_libgcc_handle); else { diff --git a/sysdeps/unix/sysv/linux/tst-dl_mseal.c b/sysdeps/unix/sysv/linux/tst-dl_mseal.c index 72a33d04c7..da1a3ebe5a 100644 --- a/sysdeps/unix/sysv/linux/tst-dl_mseal.c +++ b/sysdeps/unix/sysv/linux/tst-dl_mseal.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +32,7 @@ #include #include #include +#include #define LIB_PRELOAD "lib-tst-dl_mseal-preload.so" @@ -70,6 +72,7 @@ static const char *expected_sealed_libs[] = LIB_NEEDED_2, LIB_DLOPEN_NODELETE, LIB_DLOPEN_NODELETE_DEP, + LIBGCC_S_SO, #endif "[vdso]", }; @@ -100,6 +103,13 @@ is_in_string_list (const char *s, const char *const list[], size_t len) return -1; } +static void * +tf (void *closure) +{ + pthread_exit (NULL); + return NULL; +} + static int handle_restart (void) { @@ -108,6 +118,9 @@ handle_restart (void) xdlopen (LIB_DLOPEN_DEFAULT, RTLD_NOW); #endif + /* pthread_exit will load LIBGCC_S_SO. */ + xpthread_join (xpthread_create (NULL, tf, NULL)); + FILE *fp = xfopen ("/proc/self/maps", "r"); char *line = NULL; size_t linesiz = 0;