[RFC] elf: Define DEFAULT_STACK_PERMS to (PF_R|PF_W)

Message ID 20240613054405.144545-1-hjl.tools@gmail.com
State Rejected
Headers
Series [RFC] elf: Define DEFAULT_STACK_PERMS to (PF_R|PF_W) |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed

Commit Message

H.J. Lu June 13, 2024, 5:44 a.m. UTC
  Add stackperms.h to define DEFAULT_STACK_PERMS to (PF_R|PF_W) to disable
executable stack by default.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 elf/dl-load.c                  |  6 +-----
 elf/dl-support.c               |  2 +-
 elf/rtld.c                     |  2 +-
 support/support_stack_alloc.c  | 10 ++++++++--
 sysdeps/aarch64/stackinfo.h    |  3 ---
 sysdeps/alpha/stackinfo.h      |  4 ----
 sysdeps/arc/stackinfo.h        |  3 ---
 sysdeps/arm/stackinfo.h        |  4 ----
 sysdeps/csky/stackinfo.h       |  2 --
 sysdeps/generic/stackperms.h   | 25 +++++++++++++++++++++++++
 sysdeps/hppa/stackinfo.h       |  4 ----
 sysdeps/i386/stackinfo.h       |  4 ----
 sysdeps/loongarch/stackinfo.h  |  3 ---
 sysdeps/m68k/stackinfo.h       |  4 ----
 sysdeps/microblaze/stackinfo.h |  4 ----
 sysdeps/mips/stackinfo.h       |  4 ----
 sysdeps/nios2/stackinfo.h      |  3 ---
 sysdeps/or1k/stackinfo.h       |  4 ----
 sysdeps/powerpc/stackinfo.h    |  9 ---------
 sysdeps/riscv/stackinfo.h      |  3 ---
 sysdeps/s390/stackinfo.h       |  4 ----
 sysdeps/sh/stackinfo.h         |  4 ----
 sysdeps/sparc/stackinfo.h      |  4 ----
 sysdeps/x86_64/stackinfo.h     |  4 ----
 24 files changed, 36 insertions(+), 83 deletions(-)
 create mode 100644 sysdeps/generic/stackperms.h
  

Comments

Florian Weimer June 13, 2024, 5:53 a.m. UTC | #1
* H. J. Lu:

> diff --git a/sysdeps/generic/stackperms.h b/sysdeps/generic/stackperms.h
> new file mode 100644
> index 0000000000..ca7c51eda4
> --- /dev/null
> +++ b/sysdeps/generic/stackperms.h

> +#define DEFAULT_STACK_PERMS (PF_R|PF_X)

Surely that should be (PF_R|PF_W)?

Thanks,
Florian
  

Patch

diff --git a/elf/dl-load.c b/elf/dl-load.c
index 8a89b71016..77526a1434 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -61,7 +61,6 @@  struct filebuf
 #include "dynamic-link.h"
 #include "get-dynamic-info.h"
 #include <abi-tag.h>
-#include <stackinfo.h>
 #include <sysdep.h>
 #include <stap-probe.h>
 #include <libc-pointer-arith.h>
@@ -1069,10 +1068,7 @@  _dl_map_object_from_fd (const char *name, const char *origname, int fd,
 	}
     }
 
-   /* On most platforms presume that PT_GNU_STACK is absent and the stack is
-    * executable.  Other platforms default to a nonexecutable stack and don't
-    * need PT_GNU_STACK to do so.  */
-   unsigned int stack_flags = DEFAULT_STACK_PERMS;
+   unsigned int stack_flags = GL(dl_stack_flags);
 
   {
     /* Scan the program header table, collecting its load commands.  */
diff --git a/elf/dl-support.c b/elf/dl-support.c
index 451932dd03..1f349978b1 100644
--- a/elf/dl-support.c
+++ b/elf/dl-support.c
@@ -38,7 +38,7 @@ 
 #include <dl-procinfo.h>
 #include <unsecvars.h>
 #include <hp-timing.h>
-#include <stackinfo.h>
+#include <stackperms.h>
 #include <dl-vdso.h>
 #include <dl-vdso-setup.h>
 #include <dl-auxv.h>
diff --git a/elf/rtld.c b/elf/rtld.c
index e9525ea987..fba0e6bc00 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -41,7 +41,7 @@ 
 #include <dl-vdso-setup.h>
 #include <tls.h>
 #include <stap-probe.h>
-#include <stackinfo.h>
+#include <stackperms.h>
 #include <not-cancel.h>
 #include <array_length.h>
 #include <libc-early-init.h>
diff --git a/support/support_stack_alloc.c b/support/support_stack_alloc.c
index edf3dbfc21..762d9ab3bf 100644
--- a/support/support_stack_alloc.c
+++ b/support/support_stack_alloc.c
@@ -21,10 +21,16 @@ 
 #include <support/xunistd.h>
 #include <stdint.h>
 #include <string.h>
-#include <stackinfo.h>
+#include <elf.h>
 #include <sys/mman.h>
 #include <sys/param.h> /* roundup, MAX  */
 
+/* Get GL(dl_stack_flags).  NB: This only works with dynamic tests.  */
+#undef SHARED
+#define SHARED
+#include <ldsodefs.h>
+#undef SHARED
+
 #ifndef MAP_NORESERVE
 # define MAP_NORESERVE 0
 #endif
@@ -68,7 +74,7 @@  support_stack_alloc (size_t size)
      However since glibc does not export such information with a proper ABI,
      it uses the historical permissions.  */
   int prot = PROT_READ | PROT_WRITE
-	     | (DEFAULT_STACK_PERMS & PF_X ? PROT_EXEC : 0);
+	     | (GL(dl_stack_flags) & PF_X ? PROT_EXEC : 0);
   xmprotect (alloc_base + guardsize, stacksize, prot);
   memset (alloc_base + guardsize, 0xA5, stacksize);
   return (struct support_stack) { alloc_base + guardsize, stacksize, guardsize };
diff --git a/sysdeps/aarch64/stackinfo.h b/sysdeps/aarch64/stackinfo.h
index 5a4dce7309..c1fbf08298 100644
--- a/sysdeps/aarch64/stackinfo.h
+++ b/sysdeps/aarch64/stackinfo.h
@@ -27,7 +27,4 @@ 
 /* On AArch64 the stack grows down.  */
 #define _STACK_GROWS_DOWN	1
 
-/* Default to a non-executable stack. */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W)
-
 #endif	/* stackinfo.h */
diff --git a/sysdeps/alpha/stackinfo.h b/sysdeps/alpha/stackinfo.h
index d45c280cb8..3bae7f2fc7 100644
--- a/sysdeps/alpha/stackinfo.h
+++ b/sysdeps/alpha/stackinfo.h
@@ -26,8 +26,4 @@ 
 /* On Alpha the stack grows down.  */
 #define _STACK_GROWS_DOWN	1
 
-/* Default to an executable stack.  PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent.  */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
-
 #endif	/* stackinfo.h */
diff --git a/sysdeps/arc/stackinfo.h b/sysdeps/arc/stackinfo.h
index 99a7f56ea0..dd20edc78d 100644
--- a/sysdeps/arc/stackinfo.h
+++ b/sysdeps/arc/stackinfo.h
@@ -27,7 +27,4 @@ 
 /* On ARC the stack grows down.  */
 #define _STACK_GROWS_DOWN	1
 
-/* Default to a non-executable stack.  */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W)
-
 #endif	/* stackinfo.h */
diff --git a/sysdeps/arm/stackinfo.h b/sysdeps/arm/stackinfo.h
index 6ef3b5e316..1cac24d6f6 100644
--- a/sysdeps/arm/stackinfo.h
+++ b/sysdeps/arm/stackinfo.h
@@ -26,8 +26,4 @@ 
 /* On Arm the stack grows down.  */
 #define _STACK_GROWS_DOWN	1
 
-/* Default to an executable stack.  PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent.  */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
-
 #endif	/* stackinfo.h */
diff --git a/sysdeps/csky/stackinfo.h b/sysdeps/csky/stackinfo.h
index ef22fa3249..2a08fa8fdf 100644
--- a/sysdeps/csky/stackinfo.h
+++ b/sysdeps/csky/stackinfo.h
@@ -24,6 +24,4 @@ 
 /* On C-SKY the stack grows down.  */
 #define _STACK_GROWS_DOWN	1
 
-#define DEFAULT_STACK_PERMS (PF_R|PF_W)
-
 #endif	/* stackinfo.h */
diff --git a/sysdeps/generic/stackperms.h b/sysdeps/generic/stackperms.h
new file mode 100644
index 0000000000..ca7c51eda4
--- /dev/null
+++ b/sysdeps/generic/stackperms.h
@@ -0,0 +1,25 @@ 
+/* Define DEFAULT_STACK_PERMS which is the default stack permissions
+   when PT_GNU_STACK is absent.  Generic version.
+   Copyright (C) 2024 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _STACKPERMS_H
+#define _STACKPERMS_H	1
+
+#define DEFAULT_STACK_PERMS (PF_R|PF_X)
+
+#endif
diff --git a/sysdeps/hppa/stackinfo.h b/sysdeps/hppa/stackinfo.h
index 6298a9eab7..157dc4dae8 100644
--- a/sysdeps/hppa/stackinfo.h
+++ b/sysdeps/hppa/stackinfo.h
@@ -23,10 +23,6 @@ 
 
 #include <elf.h>
 
-/* Default to an executable stack.  PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent.  */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
-
 /* On PA the stack grows up.  */
 #define _STACK_GROWS_UP	1
 
diff --git a/sysdeps/i386/stackinfo.h b/sysdeps/i386/stackinfo.h
index d44c4894f5..979cfbc383 100644
--- a/sysdeps/i386/stackinfo.h
+++ b/sysdeps/i386/stackinfo.h
@@ -26,10 +26,6 @@ 
 /* On x86 the stack grows down.  */
 #define _STACK_GROWS_DOWN	1
 
-/* Default to an executable stack.  PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent.  */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
-
 /* Access to the stack pointer.  The macros are used in alloca_account
    for which they need to act as barriers as well, hence the additional
    (unnecessary) parameters.  */
diff --git a/sysdeps/loongarch/stackinfo.h b/sysdeps/loongarch/stackinfo.h
index 6ee50552b7..3447377096 100644
--- a/sysdeps/loongarch/stackinfo.h
+++ b/sysdeps/loongarch/stackinfo.h
@@ -27,7 +27,4 @@ 
 /* On LoongArch the stack grows down.  */
 #define _STACK_GROWS_DOWN 1
 
-/* Default to a non-executable stack.  */
-#define DEFAULT_STACK_PERMS (PF_R | PF_W)
-
 #endif /* stackinfo.h */
diff --git a/sysdeps/m68k/stackinfo.h b/sysdeps/m68k/stackinfo.h
index 7fdb7c04c2..bc7936c531 100644
--- a/sysdeps/m68k/stackinfo.h
+++ b/sysdeps/m68k/stackinfo.h
@@ -26,10 +26,6 @@ 
 /* On m68k the stack grows down.  */
 #define _STACK_GROWS_DOWN	1
 
-/* Default to an executable stack.  PF_X can be overridden if PT_GNU_STACK
-   is present, but it is presumed absent.  */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
-
 /* Access to the stack pointer.  */
 #define stackinfo_get_sp() \
   ({ void *p__; asm volatile ("move.l %%sp, %0" : "=r" (p__)); p__; })
diff --git a/sysdeps/microblaze/stackinfo.h b/sysdeps/microblaze/stackinfo.h
index d0b3f16d2f..83f0ac7c7d 100644
--- a/sysdeps/microblaze/stackinfo.h
+++ b/sysdeps/microblaze/stackinfo.h
@@ -27,8 +27,4 @@ 
 /* On MicroBlaze the stack grows down.  */
 # define _STACK_GROWS_DOWN	1
 
-/* Default to an executable stack.  PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent.  */
-# define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
-
 #endif	/* stackinfo.h.  */
diff --git a/sysdeps/mips/stackinfo.h b/sysdeps/mips/stackinfo.h
index 10f80bf495..bb339d5c69 100644
--- a/sysdeps/mips/stackinfo.h
+++ b/sysdeps/mips/stackinfo.h
@@ -26,8 +26,4 @@ 
 /* On MIPS the stack grows down.  */
 #define _STACK_GROWS_DOWN	1
 
-/* Default to an executable stack.  PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent.  */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
-
 #endif	/* stackinfo.h */
diff --git a/sysdeps/nios2/stackinfo.h b/sysdeps/nios2/stackinfo.h
index e67d53ee87..c302bd4607 100644
--- a/sysdeps/nios2/stackinfo.h
+++ b/sysdeps/nios2/stackinfo.h
@@ -27,7 +27,4 @@ 
 /* On Nios II the stack grows down.  */
 #define _STACK_GROWS_DOWN	1
 
-/* Default to a non-executable stack.  */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W)
-
 #endif	/* stackinfo.h */
diff --git a/sysdeps/or1k/stackinfo.h b/sysdeps/or1k/stackinfo.h
index 6d37e0e2f0..76f94b28ad 100644
--- a/sysdeps/or1k/stackinfo.h
+++ b/sysdeps/or1k/stackinfo.h
@@ -27,8 +27,4 @@ 
 /* On or1k the stack grows down.  */
 #define _STACK_GROWS_DOWN	1
 
-/* Default to an executable stack.  PF_X can be overridden if PT_GNU_STACK is
-   present, but it is presumed absent.  */
-#define DEFAULT_STACK_PERMS (PF_R | PF_W | PF_X)
-
 #endif /* stackinfo.h */
diff --git a/sysdeps/powerpc/stackinfo.h b/sysdeps/powerpc/stackinfo.h
index 410689bc27..52aafb52ba 100644
--- a/sysdeps/powerpc/stackinfo.h
+++ b/sysdeps/powerpc/stackinfo.h
@@ -26,13 +26,4 @@ 
 /* On PPC the stack grows down.  */
 #define _STACK_GROWS_DOWN	1
 
-#if __WORDSIZE == 64
-/* PPC64 doesn't need an executable stack and doesn't need PT_GNU_STACK
- * to make the stack nonexecutable.  */
-# define DEFAULT_STACK_PERMS (PF_R|PF_W)
-#else
-/* PF_X can be overridden if PT_GNU_STACK is present but is presumed absent.  */
-# define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
-#endif
-
 #endif	/* stackinfo.h */
diff --git a/sysdeps/riscv/stackinfo.h b/sysdeps/riscv/stackinfo.h
index a786d4f9b0..cc28f7080c 100644
--- a/sysdeps/riscv/stackinfo.h
+++ b/sysdeps/riscv/stackinfo.h
@@ -27,7 +27,4 @@ 
 /* On RISC-V the stack grows down.  */
 #define _STACK_GROWS_DOWN	1
 
-/* Default to a non-executable stack.  */
-#define DEFAULT_STACK_PERMS (PF_R | PF_W)
-
 #endif	/* stackinfo.h */
diff --git a/sysdeps/s390/stackinfo.h b/sysdeps/s390/stackinfo.h
index c89371c35f..ac5b668bac 100644
--- a/sysdeps/s390/stackinfo.h
+++ b/sysdeps/s390/stackinfo.h
@@ -26,8 +26,4 @@ 
 /* On s390 the stack grows down.  */
 #define _STACK_GROWS_DOWN	1
 
-/* Default to an executable stack.  PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent.  */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
-
 #endif	/* stackinfo.h */
diff --git a/sysdeps/sh/stackinfo.h b/sysdeps/sh/stackinfo.h
index f5caac3f1d..de95e8b744 100644
--- a/sysdeps/sh/stackinfo.h
+++ b/sysdeps/sh/stackinfo.h
@@ -26,8 +26,4 @@ 
 /* On SH the stack grows down.  */
 #define _STACK_GROWS_DOWN	1
 
-/* Default to an executable stack.  PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent.  */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
-
 #endif	/* stackinfo.h */
diff --git a/sysdeps/sparc/stackinfo.h b/sysdeps/sparc/stackinfo.h
index f1e706bc58..ce6210ce42 100644
--- a/sysdeps/sparc/stackinfo.h
+++ b/sysdeps/sparc/stackinfo.h
@@ -26,8 +26,4 @@ 
 /* On sparc the stack grows down.  */
 #define _STACK_GROWS_DOWN	1
 
-/* Default to an executable stack.  PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent.  */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
-
 #endif	/* stackinfo.h */
diff --git a/sysdeps/x86_64/stackinfo.h b/sysdeps/x86_64/stackinfo.h
index 2cb24a74bb..35b4c9cd6c 100644
--- a/sysdeps/x86_64/stackinfo.h
+++ b/sysdeps/x86_64/stackinfo.h
@@ -32,10 +32,6 @@ 
 /* On x86_64 the stack grows down.  */
 #define _STACK_GROWS_DOWN	1
 
-/* Default to an executable stack.  PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent.  */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
-
 /* Access to the stack pointer.  The macros are used in alloca_account
    for which they need to act as barriers as well, hence the additional
    (unnecessary) parameters.  */