@@ -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. */
@@ -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>
@@ -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>
@@ -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 };
@@ -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 */
@@ -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 */
@@ -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 */
@@ -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 */
@@ -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 */
new file mode 100644
@@ -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
@@ -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
@@ -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. */
@@ -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 */
@@ -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__; })
@@ -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. */
@@ -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 */
@@ -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 */
@@ -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 */
@@ -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 */
@@ -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 */
@@ -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 */
@@ -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 */
@@ -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 */
@@ -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. */
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