File ld-so-always-use-map-copy.patch of Package glibc.35625

From 6259ab39410cdefc80077afea48ca9cb057f6005 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Thu, 22 Jun 2023 14:30:31 -0700
Subject: [PATCH] ld.so: Always use MAP_COPY to map the first segment [BZ
 #30452]

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

Index: glibc-2.31/elf/dl-map-segments.h
===================================================================
--- glibc-2.31.orig/elf/dl-map-segments.h
+++ glibc-2.31/elf/dl-map-segments.h
@@ -35,7 +35,7 @@ _dl_map_segment (const struct loadcmd *c
 		       : (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;
openSUSE Build Service is sponsored by