From patchwork Thu Dec 7 10:32:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 81641 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 34FB3384DEEC for ; Thu, 7 Dec 2023 10:32:54 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 25A3A386180B for ; Thu, 7 Dec 2023 10:32:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 25A3A386180B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 25A3A386180B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701945133; cv=none; b=v/5jrzbpWvU6YqqDR+YFOE5eAY1VwYm7MBxZzRrapwBsqVpAaqYOQYQ1Wq/tYuLspduqMHCFe6ligVb7QO7hno2hojgGDBC52SrHeEDF9DwiiZjMFhxuX/eTXl/pdysguRS9e6631z4WuW/VvVFqA8PlmyhwVcEdBVqQegZpzGU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701945133; c=relaxed/simple; bh=3qBgHZMLurR4MZeUqrAJBBWXE+fGpvbAsoyPGTx5iYc=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=Zcm0oMep/0JpSpLqUMlSb2S4tK1dzke9aWSpWLoQ2k2Th6S6i2cdPT6AUaj5TVEHaeQsVVLCMG/Hr2LJlLACkRsYgwTDU6LWnENUoUMjOLjhpGvoltVuE6OnRXBB0NOUfSkt9wmeSaVlkDcIm+sT5+On7NzG3t56nCakgVcJFq8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701945131; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=1lAzh19TJQgRszNi/2RLunuvLVWf/CdtMgRRay6USSk=; b=WevCpe5navwkfh0jXiTfb9zcRCAjVzISdRnvl3E19st3EvcAKDZdaTG9kU1hGyzguTHCWe IiijsLS5X+3NCoTf+9dbyEo6dX8S0fkLkRd+wIw0DgwZYoJ4w9owpDjngTpc5i0fP01yC6 aM1m7oFUEIpT8sjOUYz8012epm8ohC4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-453-KUWGMqJjOXC6_vIv4o2wVQ-1; Thu, 07 Dec 2023 05:32:10 -0500 X-MC-Unique: KUWGMqJjOXC6_vIv4o2wVQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0F0AA83BA89 for ; Thu, 7 Dec 2023 10:32:10 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.131]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7F2481121312 for ; Thu, 7 Dec 2023 10:32:09 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v3 17/32] elf: Allocate auditor state after read-write link map In-Reply-To: Message-ID: <2135e6ae1fe527d3aa74666e6d609972a147f587.1701944612.git.fweimer@redhat.com> References: X-From-Line: 2135e6ae1fe527d3aa74666e6d609972a147f587 Mon Sep 17 00:00:00 2001 Date: Thu, 07 Dec 2023 11:32:07 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.3 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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+patchwork=sourceware.org@sourceware.org Auditors can write to the cookie member, so it has to remain read-write even if other parts of the link map are write-protected. --- elf/dl-object.c | 6 +++--- elf/rtld.c | 9 +++++++-- include/link.h | 9 +++++---- sysdeps/generic/ldsodefs.h | 17 +++-------------- 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/elf/dl-object.c b/elf/dl-object.c index 1a9b04dd3c..0741371b80 100644 --- a/elf/dl-object.c +++ b/elf/dl-object.c @@ -89,12 +89,12 @@ _dl_new_object (char *realname, const char *libname, int type, # define audit_space 0 #endif - new = calloc (sizeof (*new) + audit_space + new = calloc (sizeof (*new) + sizeof (struct link_map_private *) + sizeof (*newname) + libname_len, 1); if (new == NULL) return NULL; - new->l_rw = calloc (1, sizeof (*new->l_rw)); + new->l_rw = calloc (1, sizeof (*new->l_rw) + audit_space); if (new->l_rw == NULL) { free (new); @@ -103,7 +103,7 @@ _dl_new_object (char *realname, const char *libname, int type, new->l_real = new; new->l_symbolic_searchlist.r_list - = (struct link_map_private **) ((char *) (new + 1) + audit_space); + = (struct link_map_private **) ((char *) (new + 1)); new->l_libname = newname = (struct libname_list *) (new->l_symbolic_searchlist.r_list + 1); diff --git a/elf/rtld.c b/elf/rtld.c index 25a9c8aa58..b2f0b478bb 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -458,8 +458,13 @@ _dl_start_final (void *arg, struct dl_start_final_info *info) interfere with __rtld_static_init. */ GLRO (dl_find_object) = &_dl_find_object; - static struct link_map_rw rtld_map_rw; - GL (dl_rtld_map).l_rw = &rtld_map_rw; + /* Pre-allocated read-write status of the ld.so link map. */ + static struct + { + struct link_map_rw l; + struct auditstate _dl_rtld_auditstate[DL_NNS]; + } rtld_map_rw; + GL (dl_rtld_map).l_rw = &rtld_map_rw.l; #if NO_TLS_OFFSET != 0 GL (dl_rtld_map).l_rw->l_tls_offset = NO_TLS_OFFSET; #endif diff --git a/include/link.h b/include/link.h index 220926248c..c752bc2cb7 100644 --- a/include/link.h +++ b/include/link.h @@ -372,15 +372,16 @@ l_next (struct link_map_private *l) #include -/* Information used by audit modules. For most link maps, this data - immediate follows the link map in memory. For the dynamic linker, - it is allocated separately. See link_map_audit_state in - . */ +/* Information used by audit modules. An array of size GLRO (naudit) + elements follows the l_rw link map data in memory (in some cases + conservatively extended to to DL_NNS). */ struct auditstate { uintptr_t cookie; unsigned int bindflags; }; +_Static_assert (__alignof (struct auditstate) <= __alignof (struct link_map_rw), + "auditstate alignment compatible with link_map_rw alignment"); /* This is the hidden instance of struct r_debug_extended used by the diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index 53cc428421..d6d45f8c69 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -396,11 +396,6 @@ struct rtld_global /* Structure describing the dynamic linker itself. */ EXTERN struct link_map_private _dl_rtld_map; -#ifdef SHARED - /* Used to store the audit information for the link map of the - dynamic loader. */ - struct auditstate _dl_rtld_auditstate[DL_NNS]; -#endif #if !PTHREAD_IN_LIBC && defined SHARED \ && defined __rtld_lock_default_lock_recursive @@ -1323,15 +1318,9 @@ rtld_active (void) static inline struct auditstate * link_map_audit_state (struct link_map_private *l, size_t index) { - if (l == &GL (dl_rtld_map)) - /* The auditstate array is stored separately. */ - return &GL (dl_rtld_auditstate) [index]; - else - { - /* The auditstate array follows the link map in memory. */ - struct auditstate *base = (struct auditstate *) (l + 1); - return &base[index]; - } + /* The auditstate array follows the read-write link map part in memory. */ + struct auditstate *base = (struct auditstate *) (l->l_rw + 1); + return &base[index]; } /* Call the la_objsearch from the audit modules from the link map L. If