From patchwork Tue Aug 6 08:48:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 95354 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 A14A4385E45D for ; Tue, 6 Aug 2024 08:49:09 +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.133.124]) by sourceware.org (Postfix) with ESMTP id D00AE385DDD1 for ; Tue, 6 Aug 2024 08:48:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D00AE385DDD1 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 D00AE385DDD1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722934116; cv=none; b=eJwfo0meRBQH6844g623usFao0LQGdj5+9Ga8g3VJvaDrMn7W3PAiOzN5noJt/wWb37GzfEcMMuck5ZdinGDKJatMt3ALX1Q0EQqmy3ycCwolaD7BVmnBcm4xY3hbkee5TSqgppMZ12Py5sQbCDOQiJ4UgQSSgAuekk+wz1ToOY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722934116; c=relaxed/simple; bh=us6p72EJWxirs8cwO+JgQPUzTf6pJOyav3/g9vJBa7c=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=Ka9IFR0lfpP8nhNrwXeBTPPyo+YxbkH5drpc6TsyKNMEObFPJAoqfQsrI72o7ZZMFbEklXuQgRZcZNrt7tMKx0ZN2twqsgA6p4dNEdVDx7E56nSCBY6YQ0uWA/kdjo21u7KAjuEfuVYCoVAkBPwrn7c6AkucebDxo1xcxHqIl6w= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722934114; 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=FKQ2DRPpzprXbjuCOO/BR6NYyoWEnaeM7nivSH9A0rA=; b=RnJOUn9ZyiBkn9YrIl+ic8nJPg0hcleUwTHBS47PlqY3WJMf/Kh4gMpSus46w10kiGAt4C DIGYwAYXUePv2/8Yd0L7CL+VRfGfzWActg0yRSSvt2lvD2C9wGyK+od6bNSS0t4N90oFXV wBo89MiY/09tq1eDn8gYTMnhepLICjY= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-214-Q-ijACm4MnO4yVlRwNrAwA-1; Tue, 06 Aug 2024 04:48:32 -0400 X-MC-Unique: Q-ijACm4MnO4yVlRwNrAwA-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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 mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D29571955D59 for ; Tue, 6 Aug 2024 08:48:31 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.45.224.40]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 083DB3001D83 for ; Tue, 6 Aug 2024 08:48:30 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 2/3] elf: Introduce _dl_relocate_object_no_relro In-Reply-To: Message-ID: <200b2c6531a6a325c0b8824fe411ee70e622ed2f.1722934012.git.fweimer@redhat.com> References: X-From-Line: 200b2c6531a6a325c0b8824fe411ee70e622ed2f Mon Sep 17 00:00:00 2001 Date: Tue, 06 Aug 2024 10:48:27 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 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 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 And make _dl_protect_relro apply RELRO conditionally. --- elf/dl-reloc.c | 24 ++++++++++++++---------- sysdeps/generic/ldsodefs.h | 7 +++++++ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/elf/dl-reloc.c b/elf/dl-reloc.c index 4bf7aec88b..bdb271fdfc 100644 --- a/elf/dl-reloc.c +++ b/elf/dl-reloc.c @@ -202,12 +202,9 @@ resolve_map (lookup_t l, struct r_scope_elem *scope[], const ElfW(Sym) **ref, #include "dynamic-link.h" void -_dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[], - int reloc_mode, int consider_profiling) +_dl_relocate_object_no_relro (struct link_map *l, struct r_scope_elem *scope[], + int reloc_mode, int consider_profiling) { - if (l->l_relocated) - return; - struct textrels { caddr_t start; @@ -342,17 +339,24 @@ _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[], textrels = textrels->next; } - - /* In case we can protect the data now that the relocations are - done, do it. */ - if (l->l_relro_size != 0) - _dl_protect_relro (l); } +void +_dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[], + int reloc_mode, int consider_profiling) +{ + if (l->l_relocated) + return; + _dl_relocate_object_no_relro (l, scope, reloc_mode, consider_profiling); + _dl_protect_relro (l); +} void _dl_protect_relro (struct link_map *l) { + if (l->l_relro_size == 0) + return; + ElfW(Addr) start = ALIGN_DOWN((l->l_addr + l->l_relro_addr), GLRO(dl_pagesize)); diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index 259ce2e7d6..91447a5e77 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -1014,6 +1014,13 @@ extern void _dl_relocate_object (struct link_map *map, int reloc_mode, int consider_profiling) attribute_hidden; +/* Perform relocation, but do not apply RELRO. Does not check + L->relocated. Otherwise the same as _dl_relocate_object. */ +void _dl_relocate_object_no_relro (struct link_map *map, + struct r_scope_elem *scope[], + int reloc_mode, int consider_profiling) + attribute_hidden; + /* Protect PT_GNU_RELRO area. */ extern void _dl_protect_relro (struct link_map *map) attribute_hidden;