From patchwork Thu Jun 22 21:30:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1798701 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org 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=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=x0Ikx7UU; dkim-atps=neutral Received: from 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 (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QnD7b2MTGz20Wk for ; Fri, 23 Jun 2023 07:30:51 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E65203858D37 for ; Thu, 22 Jun 2023 21:30:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E65203858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1687469448; bh=xumTnJk+dc17Bti7hP+1bJ1YLu3Qxm7y1azRtA6sKpk=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=x0Ikx7UU3awb0kCxZ0D8BGI9La0SzCw4kOGcJSR61ViAyV7qrLaoTTpoCaHMEiGIe BTe/ipPXDrTiUosB0tKtz7vsH3DmBycxTMewqGFA17Y1k9kd6EyFKsOAqg64/bL9z5 K9pgL7TXQcVqZLiYUcfc7jGWdmE1MlyMDwvE4PJg= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x32e.google.com (mail-ot1-x32e.google.com [IPv6:2607:f8b0:4864:20::32e]) by sourceware.org (Postfix) with ESMTPS id AB7053858D37 for ; Thu, 22 Jun 2023 21:30:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AB7053858D37 Received: by mail-ot1-x32e.google.com with SMTP id 46e09a7af769-6b71ee710edso19839a34.2 for ; Thu, 22 Jun 2023 14:30:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687469434; x=1690061434; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=xumTnJk+dc17Bti7hP+1bJ1YLu3Qxm7y1azRtA6sKpk=; b=D98rNpeaUa++xFCfnrT3pPKCa+FRNOSvwaWFAdj6/s10WpvdZ3fycG56Y2+MtdEtPY yLJ4S/oBg92nUXYw6wv2YIMKA5LEDFFYNuLDpKr/IQkBVr5GDTob3yBhmnAbpjTnUfBz ToYy2noNJXoBC/hboyVx64pucwljfqh8mxu4MTFAsQauXo/dRUt3AdTF07LmSkgsoJ9l Fl2NVYXg8bv4eY6Su/n60FDPEpJLTR6qmdTs8CUxnFrDlt/S+MbcBpBTgt/GyOIPr0Kk 0TAnRpRL6kwuSwYebvg7d77Ggg1c3oH2SvRwfYX87G6Mryxda8anFBpJRoRiYdJkqZNC U1eg== X-Gm-Message-State: AC+VfDxOlplqu2La5wTX6FwJ1TPmAwaEbyr4j+30aQG1b5nE5QEahUEL UEWz914nSw3BMgwKAhV0x2wEhecD9p0= X-Google-Smtp-Source: ACHHUZ4WhIM0l9ZItmTEpjXT4fwRp28Dxv6UTJ243nVgdpdJb80DABiMDPCJc5feR0y+RSy0Sb/dEw== X-Received: by 2002:a05:6358:c130:b0:12e:e736:5103 with SMTP id fh48-20020a056358c13000b0012ee7365103mr13549828rwb.8.1687469433560; Thu, 22 Jun 2023 14:30:33 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.59.160.123]) by smtp.gmail.com with ESMTPSA id r7-20020a63fc47000000b005533f154df1sm5235132pgk.2.2023.06.22.14.30.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Jun 2023 14:30:33 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 09A98740143 for ; Thu, 22 Jun 2023 14:30:32 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2] ld.so: Always use MAP_COPY to map the first segment [BZ #30452] Date: Thu, 22 Jun 2023 14:30:31 -0700 Message-Id: <20230622213031.746114-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3025.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" The first segment in a shared library may be read-only, not executable. To support LD_PREFER_MAP_32BIT_EXEC on such shared libraries, we also check MAP_DENYWRITE to decide if MAP_32BIT should be passed to mmap. Normally the first segment is mapped with MAP_COPY, which is defined as (MAP_PRIVATE | MAP_DENYWRITE). But if the segment alignment is greater than the page size, MAP_COPY isn't used to allocate enough space to ensure that the segment can be properly aligned. Map the first segment with MAP_COPY in this case to fix BZ #30452. --- elf/dl-map-segments.h | 2 +- sysdeps/x86_64/64/Makefile | 7 +++++++ sysdeps/x86_64/64/tst-map-32bit-2.c | 1 + sysdeps/x86_64/64/tst-map-32bit-mod-2.c | 1 + 4 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 sysdeps/x86_64/64/tst-map-32bit-2.c create mode 100644 sysdeps/x86_64/64/tst-map-32bit-mod-2.c diff --git a/elf/dl-map-segments.h b/elf/dl-map-segments.h index 504cfc0a41..ac10182d58 100644 --- a/elf/dl-map-segments.h +++ b/elf/dl-map-segments.h @@ -36,7 +36,7 @@ _dl_map_segment (const struct loadcmd *c, ElfW(Addr) mappref, : (2 * c->mapalign)); ElfW(Addr) map_start = (ElfW(Addr)) __mmap ((void *) mappref, maplen, PROT_NONE, - MAP_ANONYMOUS|MAP_PRIVATE, + MAP_ANONYMOUS|MAP_COPY, -1, 0); if (__glibc_unlikely ((void *) map_start == MAP_FAILED)) return map_start; diff --git a/sysdeps/x86_64/64/Makefile b/sysdeps/x86_64/64/Makefile index 73fcfe0bb0..17f0081b19 100644 --- a/sysdeps/x86_64/64/Makefile +++ b/sysdeps/x86_64/64/Makefile @@ -3,13 +3,16 @@ ifeq ($(subdir),elf) tests-map-32bit = \ tst-map-32bit-1a \ tst-map-32bit-1b \ + tst-map-32bit-2 \ # tests-map-32bit tst-map-32bit-1a-no-pie = yes tst-map-32bit-1b-no-pie = yes +tst-map-32bit-2-no-pie = yes tests += $(tests-map-32bit) modules-map-32bit = \ tst-map-32bit-mod \ + tst-map-32bit-mod-2 \ # modules-map-32bit modules-names += $(modules-map-32bit) @@ -18,5 +21,9 @@ tst-map-32bit-1a-ENV = LD_PREFER_MAP_32BIT_EXEC=1 $(objpfx)tst-map-32bit-1a: $(objpfx)tst-map-32bit-mod.so tst-map-32bit-1b-ENV = GLIBC_TUNABLES=glibc.cpu.prefer_map_32bit_exec=1 $(objpfx)tst-map-32bit-1b: $(objpfx)tst-map-32bit-mod.so +LDFLAGS-tst-map-32bit-mod-2.so += -Wl,-z,max-page-size=0x200000 +$(objpfx)tst-map-32bit-mod-2.so: $(libsupport) +tst-map-32bit-2-ENV = LD_PREFER_MAP_32BIT_EXEC=1 +$(objpfx)tst-map-32bit-2: $(objpfx)tst-map-32bit-mod-2.so endif diff --git a/sysdeps/x86_64/64/tst-map-32bit-2.c b/sysdeps/x86_64/64/tst-map-32bit-2.c new file mode 100644 index 0000000000..34ab01c773 --- /dev/null +++ b/sysdeps/x86_64/64/tst-map-32bit-2.c @@ -0,0 +1 @@ +#include "tst-map-32bit-1a.c" diff --git a/sysdeps/x86_64/64/tst-map-32bit-mod-2.c b/sysdeps/x86_64/64/tst-map-32bit-mod-2.c new file mode 100644 index 0000000000..69e795c024 --- /dev/null +++ b/sysdeps/x86_64/64/tst-map-32bit-mod-2.c @@ -0,0 +1 @@ +#include "tst-map-32bit-mod.c"