From patchwork Wed Jul 31 20:02:04 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: 1967281 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=qar0Ei83; 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 4WZ32948HNz1yYq for ; Thu, 1 Aug 2024 06:03:45 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 09555385C6C3 for ; Wed, 31 Jul 2024 20:03:43 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by sourceware.org (Postfix) with ESMTPS id E352A3858C33 for ; Wed, 31 Jul 2024 20:03:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E352A3858C33 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 E352A3858C33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722456199; cv=none; b=Xa1kwJW6hnrECUTC/ONGQzTruaI4DT/bLFEUNQuToJKepcmfBDvY3ZrEyR0ohSThNtYpsu1sf+OhVHnYUGCSyH5zy25QlTnaYPmQpghFJN5lRNhrandIZXvu0L0zhLo5TsDloNi87dbcKymgcBYWSuMtrSLrBlYqPz/wzs13efo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722456199; c=relaxed/simple; bh=0ysnPjRdIheuSDpDvLtlabkeqckUw4zQQ+KkPfVO7bA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=EA3TMVItULIFNUqJCV3CoKmQ6qNj+ajp1WxnP/d9l4cleeTpjVZdAY4tdT9byatAMjdHacVeGONWosDJQ8466LZ9RRy6fkIPCmstjd7PVpz7o5MYIsP2uxn84BBmcWcGLfzwX4KbCgAfRZZZnwPXpvkrfr+AVe9rSsJrD5YEm9g= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1fc5239faebso42705425ad.1 for ; Wed, 31 Jul 2024 13:03:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722456196; x=1723060996; 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=RHvyD6Fsry74hIiTf1TwSqxwHjliffo16SLcTmTnzGQ=; b=qar0Ei83fz5pufylWmlnXcE4gT3oErSnadetGk8KW35BBCH88JN/yBIfoCu1acCaWR qtFqhmUT/6eeLm9bHhApKnS4HHKbdD6f8MQuwiu93Jz2AfqWMXTh+twHKjcux+FalAb0 Pg4bRMRRjvm847R8CVrLR0z+JYpbfL9+peFzKPbTPmrc2/uxwZXOFKZuayvxiPSNgf0g XoCxykYaIId80DS4NYDgplG5upSk5iNaXeeriCMmYLI/ZSa5XKHeGrqHq0JxDhRppJRW 43JbK0fwgXGOqFizvFYmcsE92IxJ31hAwhkqs3bsTh6CEH3+sni1Be881Xy7m0UtRXPP C8sA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722456196; x=1723060996; 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=RHvyD6Fsry74hIiTf1TwSqxwHjliffo16SLcTmTnzGQ=; b=Vei9G5vj32ONMamzEKrl6lWcuMeDyl1a0dimsShcC6lmK6ZIqFp8ENWbyFKzBEUCP0 CDmt/ihSknaUv1FROfk++gwJhjRHzMqm5zVqlFFoG0Nq0pEOjfQ+y6MqZ4ek0jnPzofm 6r8FTpvlJyaFdSLIXHTt/Uarh9hqSxJWFhKwkQO449k5+GItwjKDyGqXdjmHVgij6jUC gAG4czVRTx+iYXF30pAQbkkToOSjB6Dddb8N1C4BMiyuqm5OW7NCacOhrui1PGQrgq+y FCGge6J4Lqlm2vInbKEBOSMNSbR5t0VonDYmxlAGQzlZ8/fzYV7TdQ1rl+AijgnqF/Cq YbjA== X-Gm-Message-State: AOJu0YzmJ1Jk8JBl3TEfxd7Nqk0FwsyOHKUh7KKf4PeTJ/osEwjO3egr hxoFJO4o+lP46jg+9cMFFl3OO4qEt7pDWqSQjggCLZ51Hu8cCd+TynbtbRT/FNhvF+/h/Q4wFcM 1 X-Google-Smtp-Source: AGHT+IHjSQLr9p8qN5gQBioI2rwLXjeyo4+VIPvu8WfcVAwvJOUupyLF2MBSLIAPexOPLinOQ1AwLQ== X-Received: by 2002:a17:902:f707:b0:1f9:f906:9088 with SMTP id d9443c01a7336-1ff4ce7ca63mr4809105ad.22.1722456196109; Wed, 31 Jul 2024 13:03:16 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:1944:b913:6070:fef0:3852]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7fbd8d4sm124106725ad.271.2024.07.31.13.03.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jul 2024 13:03:14 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Stephen Roettger , Jeff Xu , Florian Weimer , Mike Hommey Subject: [PATCH v2 1/5] elf: Use RTLD_NODELETE is more places Date: Wed, 31 Jul 2024 17:02:04 -0300 Message-ID: <20240731200307.2269811-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240731200307.2269811-1-adhemerval.zanella@linaro.org> References: <20240731200307.2269811-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 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 * On dlopen dependencies for objects opened with RTLD_NODELETE (since it does not make sense to unload a dependency of a RTLD_NODELETE object); * For the main map executable. * For the main map DT_NEEDED dependencies. * On __libc_unwind_link_get for libgcc_s.so (used for backtrace and unwind). Checked on x86_64-linux-gnu. --- elf/dl-open.c | 3 ++- elf/rtld.c | 6 +++--- include/dlfcn.h | 2 ++ misc/unwind-link.c | 5 +++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/elf/dl-open.c b/elf/dl-open.c index c378da16c0..076b32b977 100644 --- a/elf/dl-open.c +++ b/elf/dl-open.c @@ -636,7 +636,8 @@ dl_open_worker_begin (void *a) /* Load that object's dependencies. */ _dl_map_object_deps (new, NULL, 0, 0, - mode & (__RTLD_DLOPEN | RTLD_DEEPBIND | __RTLD_AUDIT)); + mode & (__RTLD_DLOPEN | RTLD_DEEPBIND | __RTLD_AUDIT + | RTLD_NODELETE)); /* So far, so good. Now check the versions. */ for (unsigned int i = 0; i < new->l_searchlist.r_nlist; ++i) diff --git a/elf/rtld.c b/elf/rtld.c index bfdf632e77..1090d89bfe 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -811,7 +811,7 @@ do_preload (const char *fname, struct link_map *main_map, const char *where) args.str = fname; args.loader = main_map; - args.mode = __RTLD_SECURE; + args.mode = __RTLD_SECURE | RTLD_NODELETE; unsigned int old_nloaded = GL(dl_ns)[LM_ID_BASE]._ns_nloaded; @@ -1638,7 +1638,7 @@ dl_main (const ElfW(Phdr) *phdr, /* Create a link_map for the executable itself. This will be what dlopen on "" returns. */ main_map = _dl_new_object ((char *) "", "", lt_executable, NULL, - __RTLD_OPENEXEC, LM_ID_BASE); + __RTLD_OPENEXEC | RTLD_NODELETE, LM_ID_BASE); assert (main_map != NULL); main_map->l_phdr = phdr; main_map->l_phnum = phnum; @@ -1966,7 +1966,7 @@ dl_main (const ElfW(Phdr) *phdr, RTLD_TIMING_VAR (start); rtld_timer_start (&start); _dl_map_object_deps (main_map, preloads, npreloads, - state.mode == rtld_mode_trace, 0); + state.mode == rtld_mode_trace, RTLD_NODELETE); rtld_timer_accum (&load_time, start); } 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/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 {