From c02b26455b3c9d33c8a94e56eed1958d7f751b72 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Mon, 24 Apr 2023 00:55:26 +0300 Subject: hurd: Implement prefer_map_32bit_exec tunable This makes the prefer_map_32bit_exec tunable no longer Linux-specific. Signed-off-by: Sergey Bugaev Message-Id: <20230423215526.346009-4-bugaevc@gmail.com> --- sysdeps/mach/hurd/dl-sysdep.c | 5 ++++ sysdeps/mach/hurd/mmap.c | 6 ++++ sysdeps/unix/sysv/linux/x86_64/64/Makefile | 23 --------------- sysdeps/unix/sysv/linux/x86_64/64/dl-tunables.list | 29 ------------------ .../unix/sysv/linux/x86_64/64/tst-map-32bit-1a.c | 34 ---------------------- .../unix/sysv/linux/x86_64/64/tst-map-32bit-1b.c | 1 - .../unix/sysv/linux/x86_64/64/tst-map-32bit-mod.c | 33 --------------------- sysdeps/x86_64/64/Makefile | 22 ++++++++++++++ sysdeps/x86_64/64/dl-tunables.list | 29 ++++++++++++++++++ sysdeps/x86_64/64/tst-map-32bit-1a.c | 34 ++++++++++++++++++++++ sysdeps/x86_64/64/tst-map-32bit-1b.c | 1 + sysdeps/x86_64/64/tst-map-32bit-mod.c | 33 +++++++++++++++++++++ 12 files changed, 130 insertions(+), 120 deletions(-) delete mode 100644 sysdeps/unix/sysv/linux/x86_64/64/dl-tunables.list delete mode 100644 sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-1a.c delete mode 100644 sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-1b.c delete mode 100644 sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-mod.c create mode 100644 sysdeps/x86_64/64/Makefile create mode 100644 sysdeps/x86_64/64/dl-tunables.list create mode 100644 sysdeps/x86_64/64/tst-map-32bit-1a.c create mode 100644 sysdeps/x86_64/64/tst-map-32bit-1b.c create mode 100644 sysdeps/x86_64/64/tst-map-32bit-mod.c diff --git a/sysdeps/mach/hurd/dl-sysdep.c b/sysdeps/mach/hurd/dl-sysdep.c index 25a1277..79ebb0c 100644 --- a/sysdeps/mach/hurd/dl-sysdep.c +++ b/sysdeps/mach/hurd/dl-sysdep.c @@ -462,6 +462,11 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset) if (prot & PROT_EXEC) vmprot |= VM_PROT_EXECUTE; +#ifdef __LP64__ + if ((addr == NULL) && (prot & PROT_EXEC) + && HAS_ARCH_FEATURE (Prefer_MAP_32BIT_EXEC)) + flags |= MAP_32BIT; +#endif mask = (flags & MAP_32BIT) ? ~(vm_address_t) 0x7FFFFFFF : 0; if (flags & MAP_ANON) diff --git a/sysdeps/mach/hurd/mmap.c b/sysdeps/mach/hurd/mmap.c index 790eb23..5aa7008 100644 --- a/sysdeps/mach/hurd/mmap.c +++ b/sysdeps/mach/hurd/mmap.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -55,6 +56,11 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset) copy = ! (flags & MAP_SHARED); +#ifdef __LP64__ + if ((addr == NULL) && (prot & PROT_EXEC) + && HAS_ARCH_FEATURE (Prefer_MAP_32BIT_EXEC)) + flags |= MAP_32BIT; +#endif mask = (flags & MAP_32BIT) ? ~(vm_address_t) 0x7FFFFFFF : 0; switch (flags & MAP_TYPE) diff --git a/sysdeps/unix/sysv/linux/x86_64/64/Makefile b/sysdeps/unix/sysv/linux/x86_64/64/Makefile index 1bf7d52..a7b6dc5 100644 --- a/sysdeps/unix/sysv/linux/x86_64/64/Makefile +++ b/sysdeps/unix/sysv/linux/x86_64/64/Makefile @@ -1,25 +1,2 @@ # The default ABI is 64. default-abi := 64 - -ifeq ($(subdir),elf) - -tests-map-32bit = \ - tst-map-32bit-1a \ - tst-map-32bit-1b \ -# tests-map-32bit -tst-map-32bit-1a-no-pie = yes -tst-map-32bit-1b-no-pie = yes -tests += $(tests-map-32bit) - -modules-map-32bit = \ - tst-map-32bit-mod \ -# modules-map-32bit -modules-names += $(modules-map-32bit) - -$(objpfx)tst-map-32bit-mod.so: $(libsupport) -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 - -endif diff --git a/sysdeps/unix/sysv/linux/x86_64/64/dl-tunables.list b/sysdeps/unix/sysv/linux/x86_64/64/dl-tunables.list deleted file mode 100644 index 0aab52e..0000000 --- a/sysdeps/unix/sysv/linux/x86_64/64/dl-tunables.list +++ /dev/null @@ -1,29 +0,0 @@ -# x86-64 specific tunables. -# Copyright (C) 2023 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 -# . - -glibc { - cpu { - prefer_map_32bit_exec { - type: INT_32 - minval: 0 - maxval: 1 - env_alias: LD_PREFER_MAP_32BIT_EXEC - security_level: SXID_IGNORE - } - } -} diff --git a/sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-1a.c b/sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-1a.c deleted file mode 100644 index abc3965..0000000 --- a/sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-1a.c +++ /dev/null @@ -1,34 +0,0 @@ -/* Check that LD_PREFER_MAP_32BIT_EXEC works in PDE and shared library. - Copyright (C) 2023 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 - . */ - -#include -#include -#include - -extern void dso_check_map_32bit (void); - -static int -do_test (void) -{ - printf ("do_test: %p\n", do_test); - TEST_VERIFY ((uintptr_t) do_test < 0xffffffffUL); - dso_check_map_32bit (); - return 0; -} - -#include diff --git a/sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-1b.c b/sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-1b.c deleted file mode 100644 index 34ab01c..0000000 --- a/sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-1b.c +++ /dev/null @@ -1 +0,0 @@ -#include "tst-map-32bit-1a.c" diff --git a/sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-mod.c b/sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-mod.c deleted file mode 100644 index 78d4b61..0000000 --- a/sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-mod.c +++ /dev/null @@ -1,33 +0,0 @@ -/* Check that LD_PREFER_MAP_32BIT_EXEC works in shared library. - Copyright (C) 2023 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 - . */ - -#include -#include -#include - -static void -dso_do_test (void) -{ -} - -void -dso_check_map_32bit (void) -{ - printf ("dso_do_test: %p\n", dso_do_test); - TEST_VERIFY ((uintptr_t) dso_do_test < 0xffffffffUL); -} diff --git a/sysdeps/x86_64/64/Makefile b/sysdeps/x86_64/64/Makefile new file mode 100644 index 0000000..73fcfe0 --- /dev/null +++ b/sysdeps/x86_64/64/Makefile @@ -0,0 +1,22 @@ +ifeq ($(subdir),elf) + +tests-map-32bit = \ + tst-map-32bit-1a \ + tst-map-32bit-1b \ +# tests-map-32bit +tst-map-32bit-1a-no-pie = yes +tst-map-32bit-1b-no-pie = yes +tests += $(tests-map-32bit) + +modules-map-32bit = \ + tst-map-32bit-mod \ +# modules-map-32bit +modules-names += $(modules-map-32bit) + +$(objpfx)tst-map-32bit-mod.so: $(libsupport) +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 + +endif diff --git a/sysdeps/x86_64/64/dl-tunables.list b/sysdeps/x86_64/64/dl-tunables.list new file mode 100644 index 0000000..0aab52e --- /dev/null +++ b/sysdeps/x86_64/64/dl-tunables.list @@ -0,0 +1,29 @@ +# x86-64 specific tunables. +# Copyright (C) 2023 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 +# . + +glibc { + cpu { + prefer_map_32bit_exec { + type: INT_32 + minval: 0 + maxval: 1 + env_alias: LD_PREFER_MAP_32BIT_EXEC + security_level: SXID_IGNORE + } + } +} diff --git a/sysdeps/x86_64/64/tst-map-32bit-1a.c b/sysdeps/x86_64/64/tst-map-32bit-1a.c new file mode 100644 index 0000000..abc3965 --- /dev/null +++ b/sysdeps/x86_64/64/tst-map-32bit-1a.c @@ -0,0 +1,34 @@ +/* Check that LD_PREFER_MAP_32BIT_EXEC works in PDE and shared library. + Copyright (C) 2023 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 + . */ + +#include +#include +#include + +extern void dso_check_map_32bit (void); + +static int +do_test (void) +{ + printf ("do_test: %p\n", do_test); + TEST_VERIFY ((uintptr_t) do_test < 0xffffffffUL); + dso_check_map_32bit (); + return 0; +} + +#include diff --git a/sysdeps/x86_64/64/tst-map-32bit-1b.c b/sysdeps/x86_64/64/tst-map-32bit-1b.c new file mode 100644 index 0000000..34ab01c --- /dev/null +++ b/sysdeps/x86_64/64/tst-map-32bit-1b.c @@ -0,0 +1 @@ +#include "tst-map-32bit-1a.c" diff --git a/sysdeps/x86_64/64/tst-map-32bit-mod.c b/sysdeps/x86_64/64/tst-map-32bit-mod.c new file mode 100644 index 0000000..78d4b61 --- /dev/null +++ b/sysdeps/x86_64/64/tst-map-32bit-mod.c @@ -0,0 +1,33 @@ +/* Check that LD_PREFER_MAP_32BIT_EXEC works in shared library. + Copyright (C) 2023 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 + . */ + +#include +#include +#include + +static void +dso_do_test (void) +{ +} + +void +dso_check_map_32bit (void) +{ + printf ("dso_do_test: %p\n", dso_do_test); + TEST_VERIFY ((uintptr_t) dso_do_test < 0xffffffffUL); +} -- cgit v1.1