This is the mail archive of the
glibc-cvs@sourceware.org
mailing list for the glibc project.
[glibc/hjl/cet/mark-legacy-code/master] x86/CET: Use ARCH_CET_MARK_LEGACY_CODE
- From: H.J.Lu <hjl at sourceware dot org>
- To: glibc-cvs at sourceware dot org
- Date: 25 Jun 2019 20:34:22 -0000
- Subject: [glibc/hjl/cet/mark-legacy-code/master] x86/CET: Use ARCH_CET_MARK_LEGACY_CODE
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=f03497395204e29d93a4aeeeca874c0dc137464a
commit f03497395204e29d93a4aeeeca874c0dc137464a
Author: H.J. Lu <hjl.tools@gmail.com>
Date: Tue Jun 25 09:50:52 2019 -0700
x86/CET: Use ARCH_CET_MARK_LEGACY_CODE
Use ARCH_CET_MARK_LEGACY_CODE to mark legacy code region.
* sysdeps/unix/sysv/linux/x86/dl-cet.h
(dl_cet_allocate_legacy_bitmap): Removed.
(dl_cet_mark_legacy_code): New.
* sysdeps/unix/sysv/linux/x86/include/asm/prctl.h
(ARCH_CET_LEGACY_BITMAP): Removed.
(ARCH_CET_MARK_LEGACY_CODE): New.
* sysdeps/x86/dl-cet.c (dl_cet_mark_legacy_region): Call
dl_cet_mark_legacy_code.
(dl_cet_check): Remove GL(dl_x86_legacy_bitmap) access.
* sysdeps/x86/dl-procruntime.c (_dl_x86_legacy_bitmap): Removed.
Diff:
---
sysdeps/unix/sysv/linux/x86/dl-cet.h | 22 +++----
sysdeps/unix/sysv/linux/x86/include/asm/prctl.h | 3 +-
sysdeps/x86/dl-cet.c | 83 ++-----------------------
sysdeps/x86/dl-procruntime.c | 11 ----
4 files changed, 17 insertions(+), 102 deletions(-)
diff --git a/sysdeps/unix/sysv/linux/x86/dl-cet.h b/sysdeps/unix/sysv/linux/x86/dl-cet.h
index a8b794d..efe8add 100644
--- a/sysdeps/unix/sysv/linux/x86/dl-cet.h
+++ b/sysdeps/unix/sysv/linux/x86/dl-cet.h
@@ -19,23 +19,23 @@
#include <asm/prctl.h>
static inline int __attribute__ ((always_inline))
-dl_cet_allocate_legacy_bitmap (unsigned long *legacy_bitmap)
+dl_cet_mark_legacy_code (unsigned long *legacy_bitmap)
{
/* Allocate legacy bitmap. */
INTERNAL_SYSCALL_DECL (err);
#ifdef __LP64__
+ legacy_bitmap[2] = 1;
return (int) INTERNAL_SYSCALL (arch_prctl, err, 2,
- ARCH_CET_LEGACY_BITMAP, legacy_bitmap);
+ ARCH_CET_MARK_LEGACY_CODE,
+ legacy_bitmap);
#else
- unsigned long long legacy_bitmap_u64[2];
- int res = INTERNAL_SYSCALL (arch_prctl, err, 2,
- ARCH_CET_LEGACY_BITMAP, legacy_bitmap_u64);
- if (res == 0)
- {
- legacy_bitmap[0] = legacy_bitmap_u64[0];
- legacy_bitmap[1] = legacy_bitmap_u64[1];
- }
- return res;
+ unsigned long long legacy_bitmap_u64[3];
+ legacy_bitmap_u64[0] = legacy_bitmap[0];
+ legacy_bitmap_u64[1] = legacy_bitmap[1];
+ legacy_bitmap_u64[2] = 1;
+ return (int) INTERNAL_SYSCALL (arch_prctl, err, 2,
+ ARCH_CET_MARK_LEGACY_CODE,
+ legacy_bitmap_u64);
#endif
}
diff --git a/sysdeps/unix/sysv/linux/x86/include/asm/prctl.h b/sysdeps/unix/sysv/linux/x86/include/asm/prctl.h
index f67f329..d727650 100644
--- a/sysdeps/unix/sysv/linux/x86/include/asm/prctl.h
+++ b/sysdeps/unix/sysv/linux/x86/include/asm/prctl.h
@@ -27,6 +27,7 @@
/* Return legacy region bitmap info in unsigned long long *addr:
address: addr[0].
size: addr[1].
+ set: addr[2].
*/
-# define ARCH_CET_LEGACY_BITMAP 0x3005
+# define ARCH_CET_MARK_LEGACY_CODE 0x3007
#endif /* ARCH_CET_STATUS */
diff --git a/sysdeps/x86/dl-cet.c b/sysdeps/x86/dl-cet.c
index 4f70497..1bfc5b2 100644
--- a/sysdeps/x86/dl-cet.c
+++ b/sysdeps/x86/dl-cet.c
@@ -39,52 +39,14 @@ dl_cet_mark_legacy_region (struct link_map *l)
/* Mark PT_LOAD segments with PF_X in legacy code page bitmap. */
size_t i, phnum = l->l_phnum;
const ElfW(Phdr) *phdr = l->l_phdr;
-#ifdef __x86_64__
- typedef unsigned long long word_t;
-#else
- typedef unsigned long word_t;
-#endif
- unsigned int bits_to_set;
- word_t mask_to_set;
-#define BITS_PER_WORD (sizeof (word_t) * 8)
-#define BITMAP_FIRST_WORD_MASK(start) \
- (~((word_t) 0) << ((start) & (BITS_PER_WORD - 1)))
-#define BITMAP_LAST_WORD_MASK(nbits) \
- (~((word_t) 0) >> (-(nbits) & (BITS_PER_WORD - 1)))
-
- word_t *bitmap = (word_t *) GL(dl_x86_legacy_bitmap)[0];
- word_t bitmap_size = GL(dl_x86_legacy_bitmap)[1];
- word_t *p;
- size_t page_size = GLRO(dl_pagesize);
+ unsigned long legacy_bitmap[3];
for (i = 0; i < phnum; i++)
if (phdr[i].p_type == PT_LOAD && (phdr[i].p_flags & PF_X))
{
- /* One bit in legacy bitmap represents a page. */
- ElfW(Addr) start = (phdr[i].p_vaddr + l->l_addr) / page_size;
- ElfW(Addr) len = (phdr[i].p_memsz + page_size - 1) / page_size;
- ElfW(Addr) end = start + len;
-
- if ((end / 8) > bitmap_size)
- return -EINVAL;
-
- p = bitmap + (start / BITS_PER_WORD);
- bits_to_set = BITS_PER_WORD - (start % BITS_PER_WORD);
- mask_to_set = BITMAP_FIRST_WORD_MASK (start);
-
- while (len >= bits_to_set)
- {
- *p |= mask_to_set;
- len -= bits_to_set;
- bits_to_set = BITS_PER_WORD;
- mask_to_set = ~((word_t) 0);
- p++;
- }
- if (len)
- {
- mask_to_set &= BITMAP_LAST_WORD_MASK (end);
- *p |= mask_to_set;
- }
+ legacy_bitmap[0] = phdr[i].p_vaddr + l->l_addr;
+ legacy_bitmap[1] = phdr[i].p_memsz;
+ dl_cet_mark_legacy_code (legacy_bitmap);
}
return 0;
@@ -186,38 +148,6 @@ dl_cet_check (struct link_map *m, const char *program)
if (need_legacy_bitmap)
{
- if (GL(dl_x86_legacy_bitmap)[0])
- {
- /* Change legacy bitmap to writable. */
- if (__mprotect ((void *) GL(dl_x86_legacy_bitmap)[0],
- GL(dl_x86_legacy_bitmap)[1],
- PROT_READ | PROT_WRITE) < 0)
- {
-mprotect_failure:
- if (program)
- _dl_fatal_printf ("%s: mprotect legacy bitmap failed\n",
- l->l_name);
- else
- _dl_signal_error (EINVAL, l->l_name, "dlopen",
- N_("mprotect legacy bitmap failed"));
- }
- }
- else
- {
- /* Allocate legacy bitmap. */
- int res = dl_cet_allocate_legacy_bitmap
- (GL(dl_x86_legacy_bitmap));
- if (res != 0)
- {
- if (program)
- _dl_fatal_printf ("%s: legacy bitmap isn't available\n",
- l->l_name);
- else
- _dl_signal_error (EINVAL, l->l_name, "dlopen",
- N_("legacy bitmap isn't available"));
- }
- }
-
/* Put legacy shared objects in legacy bitmap. */
for (i = first_legacy; i <= last_legacy; i++)
{
@@ -247,11 +177,6 @@ mprotect_failure:
N_("failed to mark legacy code region"));
}
}
-
- /* Change legacy bitmap to read-only. */
- if (__mprotect ((void *) GL(dl_x86_legacy_bitmap)[0],
- GL(dl_x86_legacy_bitmap)[1], PROT_READ) < 0)
- goto mprotect_failure;
}
}
diff --git a/sysdeps/x86/dl-procruntime.c b/sysdeps/x86/dl-procruntime.c
index 54fed66..20923b1 100644
--- a/sysdeps/x86/dl-procruntime.c
+++ b/sysdeps/x86/dl-procruntime.c
@@ -54,15 +54,4 @@ PROCINFO_CLASS unsigned int _dl_x86_feature_1[2]
# else
,
# endif
-
-# if !defined PROCINFO_DECL && defined SHARED
- ._dl_x86_legacy_bitmap
-# else
-PROCINFO_CLASS unsigned long _dl_x86_legacy_bitmap[2]
-# endif
-# if !defined SHARED || defined PROCINFO_DECL
-;
-# else
-,
-# endif
#endif