git: b63c36db2065 - stable/12 - libc: implement rtld_get_stack_prot() for real

Konstantin Belousov kib at FreeBSD.org
Sun Jan 17 05:10:40 UTC 2021


The branch stable/12 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=b63c36db2065770a0b17925cb3b51cde6af95add

commit b63c36db2065770a0b17925cb3b51cde6af95add
Author:     Konstantin Belousov <kib at FreeBSD.org>
AuthorDate: 2021-01-10 03:05:42 +0000
Commit:     Konstantin Belousov <kib at FreeBSD.org>
CommitDate: 2021-01-17 04:44:43 +0000

    libc: implement rtld_get_stack_prot() for real
    
    (cherry picked from commit 81b3a0a34145ee6c855f50c8035728f76d63c3f0)
---
 lib/libc/gen/dlfcn.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/lib/libc/gen/dlfcn.c b/lib/libc/gen/dlfcn.c
index f7f162753b4b..395a6d9402e8 100644
--- a/lib/libc/gen/dlfcn.c
+++ b/lib/libc/gen/dlfcn.c
@@ -34,7 +34,9 @@ __FBSDID("$FreeBSD$");
 /*
  * Linkage to services provided by the dynamic linker.
  */
+#include <sys/types.h>
 #include <sys/mman.h>
+#include <machine/atomic.h>
 #include <dlfcn.h>
 #include <link.h>
 #include <stddef.h>
@@ -256,8 +258,30 @@ _rtld_addr_phdr(const void *addr __unused,
 int
 _rtld_get_stack_prot(void)
 {
+#ifndef IN_LIBDL
+	unsigned i;
+	int r;
+	static int ret;
+
+	r = atomic_load_int(&ret);
+	if (r != 0)
+		return (r);
 
-	return (PROT_EXEC | PROT_READ | PROT_WRITE);
+	_once(&dl_phdr_info_once, dl_init_phdr_info);
+	r = PROT_EXEC | PROT_READ | PROT_WRITE;
+	for (i = 0; i < phdr_info.dlpi_phnum; i++) {
+		if (phdr_info.dlpi_phdr[i].p_type != PT_GNU_STACK)
+			continue;
+		r = PROT_READ | PROT_WRITE;
+		if ((phdr_info.dlpi_phdr[i].p_flags & PF_X) != 0)
+			r |= PROT_EXEC;
+		break;
+	}
+	atomic_store_int(&ret, r);
+	return (r);
+#else
+	return (0);
+#endif
 }
 
 #pragma weak _rtld_is_dlopened


More information about the dev-commits-src-all mailing list