[3/4] Add endbr64 to x86_64 assembly functions

Submitted by Joao Moreira on Oct. 19, 2020, 10:43 p.m.

Details

Message ID 20201019224342.53303-4-joao.moreira@intel.com
State New
Series "Add CET IBT Support to MUSL"
Headers show

Commit Message

Joao Moreira Oct. 19, 2020, 10:43 p.m.
Include ibt.s file in x86_64 assembly files. This file contains the macro
definitions for endbr64 instructions and is placed in the beginning of each
assembly function. If CET_IBT is not defined for compilation, the macro is
defined to nothing and the generated binary is not affected.

Signed-off-by: Joao Moreira <joao.moreira@intel.com>
---
 arch/x86_64/crt_arch.h                | 3 +++
 crt/x86_64/crti.s                     | 4 ++++
 crt/x86_64/crtn.s                     | 4 ++++
 src/fenv/x86_64/fenv.s                | 9 +++++++++
 src/ldso/x86_64/dlsym.s               | 3 +++
 src/ldso/x86_64/tlsdesc.s             | 4 ++++
 src/math/x86_64/acosl.s               | 2 ++
 src/math/x86_64/asinl.s               | 3 +++
 src/math/x86_64/atan2l.s              | 3 +++
 src/math/x86_64/atanl.s               | 3 +++
 src/math/x86_64/exp2l.s               | 4 ++++
 src/math/x86_64/expl.s                | 3 +++
 src/math/x86_64/floorl.s              | 5 +++++
 src/math/x86_64/log10l.s              | 3 +++
 src/math/x86_64/log1pl.s              | 3 +++
 src/math/x86_64/log2l.s               | 3 +++
 src/math/x86_64/logl.s                | 3 +++
 src/process/x86_64/vfork.s            | 3 +++
 src/setjmp/x86_64/longjmp.s           | 3 +++
 src/setjmp/x86_64/setjmp.s            | 3 +++
 src/signal/x86_64/restore.s           | 3 +++
 src/signal/x86_64/sigsetjmp.s         | 3 +++
 src/string/x86_64/memcpy.s            | 3 +++
 src/string/x86_64/memmove.s           | 3 +++
 src/string/x86_64/memset.s            | 3 +++
 src/thread/x86_64/__set_thread_area.s | 3 +++
 src/thread/x86_64/__unmapself.s       | 3 +++
 src/thread/x86_64/clone.s             | 3 +++
 src/thread/x86_64/syscall_cp.s        | 4 +++-
 29 files changed, 98 insertions(+), 1 deletion(-)

Patch hide | download patch | download mbox

diff --git a/arch/x86_64/crt_arch.h b/arch/x86_64/crt_arch.h
index 3eec61bd..c54fa073 100644
--- a/arch/x86_64/crt_arch.h
+++ b/arch/x86_64/crt_arch.h
@@ -2,6 +2,9 @@  __asm__(
 ".text \n"
 ".global " START " \n"
 START ": \n"
+#ifdef CET_IBT
+" endbr64 \n"
+#endif
 "	xor %rbp,%rbp \n"
 "	mov %rsp,%rdi \n"
 ".weak _DYNAMIC \n"
diff --git a/crt/x86_64/crti.s b/crt/x86_64/crti.s
index 4788968b..fbc7e479 100644
--- a/crt/x86_64/crti.s
+++ b/crt/x86_64/crti.s
@@ -1,9 +1,13 @@ 
+.include "ibt.s"
+
 .section .init
 .global _init
 _init:
+	ENDBRANCH64
 	push %rax
 
 .section .fini
 .global _fini
 _fini:
+	ENDBRANCH64
 	push %rax
diff --git a/crt/x86_64/crtn.s b/crt/x86_64/crtn.s
index 29198b77..87f85fae 100644
--- a/crt/x86_64/crtn.s
+++ b/crt/x86_64/crtn.s
@@ -1,7 +1,11 @@ 
+.include "ibt.s"
+
 .section .init
+	ENDBRANCH64
 	pop %rax
 	ret
 
 .section .fini
+	ENDBRANCH64
 	pop %rax
 	ret
diff --git a/src/fenv/x86_64/fenv.s b/src/fenv/x86_64/fenv.s
index 98d876da..7b47f5d0 100644
--- a/src/fenv/x86_64/fenv.s
+++ b/src/fenv/x86_64/fenv.s
@@ -1,6 +1,9 @@ 
+.include "ibt.s"
+
 .global feclearexcept
 .type feclearexcept,@function
 feclearexcept:
+	ENDBRANCH64
 		# maintain exceptions in the sse mxcsr, clear x87 exceptions
 	mov %edi,%ecx
 	and $0x3f,%ecx
@@ -22,6 +25,7 @@  feclearexcept:
 .global feraiseexcept
 .type feraiseexcept,@function
 feraiseexcept:
+	ENDBRANCH64
 	and $0x3f,%edi
 	stmxcsr -8(%rsp)
 	or %edi,-8(%rsp)
@@ -33,6 +37,7 @@  feraiseexcept:
 .hidden __fesetround
 .type __fesetround,@function
 __fesetround:
+	ENDBRANCH64
 	push %rax
 	xor %eax,%eax
 	mov %edi,%ecx
@@ -51,6 +56,7 @@  __fesetround:
 .global fegetround
 .type fegetround,@function
 fegetround:
+	ENDBRANCH64
 	push %rax
 	stmxcsr (%rsp)
 	pop %rax
@@ -61,6 +67,7 @@  fegetround:
 .global fegetenv
 .type fegetenv,@function
 fegetenv:
+	ENDBRANCH64
 	xor %eax,%eax
 	fnstenv (%rdi)
 	stmxcsr 28(%rdi)
@@ -69,6 +76,7 @@  fegetenv:
 .global fesetenv
 .type fesetenv,@function
 fesetenv:
+	ENDBRANCH64
 	xor %eax,%eax
 	inc %rdi
 	jz 1f
@@ -88,6 +96,7 @@  fesetenv:
 .global fetestexcept
 .type fetestexcept,@function
 fetestexcept:
+	ENDBRANCH64
 	and $0x3f,%edi
 	push %rax
 	stmxcsr (%rsp)
diff --git a/src/ldso/x86_64/dlsym.s b/src/ldso/x86_64/dlsym.s
index d840b955..ada2fefd 100644
--- a/src/ldso/x86_64/dlsym.s
+++ b/src/ldso/x86_64/dlsym.s
@@ -1,7 +1,10 @@ 
+.include "ibt.s"
+
 .text
 .global dlsym
 .hidden __dlsym
 .type dlsym,@function
 dlsym:
+	ENDBRANCH64
 	mov (%rsp),%rdx
 	jmp __dlsym
diff --git a/src/ldso/x86_64/tlsdesc.s b/src/ldso/x86_64/tlsdesc.s
index e08f1d7d..a608d68f 100644
--- a/src/ldso/x86_64/tlsdesc.s
+++ b/src/ldso/x86_64/tlsdesc.s
@@ -1,8 +1,11 @@ 
+.include "ibt.s"
+
 .text
 .global __tlsdesc_static
 .hidden __tlsdesc_static
 .type __tlsdesc_static,@function
 __tlsdesc_static:
+	ENDBRANCH64
 	mov 8(%rax),%rax
 	ret
 
@@ -10,6 +13,7 @@  __tlsdesc_static:
 .hidden __tlsdesc_dynamic
 .type __tlsdesc_dynamic,@function
 __tlsdesc_dynamic:
+	ENDBRANCH64
 	mov 8(%rax),%rax
 	push %rdx
 	mov %fs:8,%rdx
diff --git a/src/math/x86_64/acosl.s b/src/math/x86_64/acosl.s
index 88e01b49..9a4da9a3 100644
--- a/src/math/x86_64/acosl.s
+++ b/src/math/x86_64/acosl.s
@@ -1,8 +1,10 @@ 
 # see ../i386/acos.s
+.include "ibt.s"
 
 .global acosl
 .type acosl,@function
 acosl:
+	ENDBRANCH64
 	fldt 8(%rsp)
 1:	fld %st(0)
 	fld1
diff --git a/src/math/x86_64/asinl.s b/src/math/x86_64/asinl.s
index ed212d9a..38ea6cd1 100644
--- a/src/math/x86_64/asinl.s
+++ b/src/math/x86_64/asinl.s
@@ -1,6 +1,9 @@ 
+.include "ibt.s"
+
 .global asinl
 .type asinl,@function
 asinl:
+	ENDBRANCH64
 	fldt 8(%rsp)
 1:	fld %st(0)
 	fld1
diff --git a/src/math/x86_64/atan2l.s b/src/math/x86_64/atan2l.s
index e5f0a3de..1ffb8705 100644
--- a/src/math/x86_64/atan2l.s
+++ b/src/math/x86_64/atan2l.s
@@ -1,6 +1,9 @@ 
+.include "ibt.s"
+
 .global atan2l
 .type atan2l,@function
 atan2l:
+	ENDBRANCH64
 	fldt 8(%rsp)
 	fldt 24(%rsp)
 	fpatan
diff --git a/src/math/x86_64/atanl.s b/src/math/x86_64/atanl.s
index df76de5d..0b06fe2e 100644
--- a/src/math/x86_64/atanl.s
+++ b/src/math/x86_64/atanl.s
@@ -1,6 +1,9 @@ 
+.include "ibt.s"
+
 .global atanl
 .type atanl,@function
 atanl:
+	ENDBRANCH64
 	fldt 8(%rsp)
 	fld1
 	fpatan
diff --git a/src/math/x86_64/exp2l.s b/src/math/x86_64/exp2l.s
index effab2bd..b2b5fa30 100644
--- a/src/math/x86_64/exp2l.s
+++ b/src/math/x86_64/exp2l.s
@@ -1,6 +1,9 @@ 
+.include "ibt.s"
+
 .global expm1l
 .type expm1l,@function
 expm1l:
+	ENDBRANCH64
 	fldt 8(%rsp)
 	fldl2e
 	fmulp
@@ -30,6 +33,7 @@  expm1l:
 .global exp2l
 .type exp2l,@function
 exp2l:
+	ENDBRANCH64
 	fldt 8(%rsp)
 1:	fld %st(0)
 	sub $16,%rsp
diff --git a/src/math/x86_64/expl.s b/src/math/x86_64/expl.s
index 798261d2..99444dc9 100644
--- a/src/math/x86_64/expl.s
+++ b/src/math/x86_64/expl.s
@@ -1,3 +1,5 @@ 
+.include "ibt.s"
+
 # exp(x) = 2^hi + 2^hi (2^lo - 1)
 # where hi+lo = log2e*x with 128bit precision
 # exact log2e*x calculation depends on nearest rounding mode
@@ -6,6 +8,7 @@ 
 .global expl
 .type expl,@function
 expl:
+	ENDBRANCH64
 	fldt 8(%rsp)
 
 		# interesting case: 0x1p-32 <= |x| < 16384
diff --git a/src/math/x86_64/floorl.s b/src/math/x86_64/floorl.s
index 80da4660..191b47e4 100644
--- a/src/math/x86_64/floorl.s
+++ b/src/math/x86_64/floorl.s
@@ -1,6 +1,9 @@ 
+.include "ibt.s"
+
 .global floorl
 .type floorl,@function
 floorl:
+	ENDBRANCH64
 	fldt 8(%rsp)
 1:	mov $0x7,%al
 1:	fstcw 8(%rsp)
@@ -15,6 +18,7 @@  floorl:
 .global ceill
 .type ceill,@function
 ceill:
+	ENDBRANCH64
 	fldt 8(%rsp)
 	mov $0xb,%al
 	jmp 1b
@@ -22,6 +26,7 @@  ceill:
 .global truncl
 .type truncl,@function
 truncl:
+	ENDBRANCH64
 	fldt 8(%rsp)
 	mov $0xf,%al
 	jmp 1b
diff --git a/src/math/x86_64/log10l.s b/src/math/x86_64/log10l.s
index 48ea4af7..23eb3d4b 100644
--- a/src/math/x86_64/log10l.s
+++ b/src/math/x86_64/log10l.s
@@ -1,6 +1,9 @@ 
+.include "ibt.s"
+
 .global log10l
 .type log10l,@function
 log10l:
+	ENDBRANCH64
 	fldlg2
 	fldt 8(%rsp)
 	fyl2x
diff --git a/src/math/x86_64/log1pl.s b/src/math/x86_64/log1pl.s
index 955c9dbf..5d7793ae 100644
--- a/src/math/x86_64/log1pl.s
+++ b/src/math/x86_64/log1pl.s
@@ -1,6 +1,9 @@ 
+.include "ibt.s"
+
 .global log1pl
 .type log1pl,@function
 log1pl:
+	ENDBRANCH64
 	mov 14(%rsp),%eax
 	fldln2
 	and $0x7fffffff,%eax
diff --git a/src/math/x86_64/log2l.s b/src/math/x86_64/log2l.s
index ba08b9fb..38e848bd 100644
--- a/src/math/x86_64/log2l.s
+++ b/src/math/x86_64/log2l.s
@@ -1,6 +1,9 @@ 
+.include "ibt.s"
+
 .global log2l
 .type log2l,@function
 log2l:
+	ENDBRANCH64
 	fld1
 	fldt 8(%rsp)
 	fyl2x
diff --git a/src/math/x86_64/logl.s b/src/math/x86_64/logl.s
index 20dd1f81..24f58e1f 100644
--- a/src/math/x86_64/logl.s
+++ b/src/math/x86_64/logl.s
@@ -1,6 +1,9 @@ 
+.include "ibt.s"
+
 .global logl
 .type logl,@function
 logl:
+	ENDBRANCH64
 	fldln2
 	fldt 8(%rsp)
 	fyl2x
diff --git a/src/process/x86_64/vfork.s b/src/process/x86_64/vfork.s
index 91144390..6c141a6b 100644
--- a/src/process/x86_64/vfork.s
+++ b/src/process/x86_64/vfork.s
@@ -1,6 +1,9 @@ 
+.include "ibt.s"
+
 .global vfork
 .type vfork,@function
 vfork:
+	ENDBRANCH64
 	pop %rdx
 	mov $58,%eax
 	syscall
diff --git a/src/setjmp/x86_64/longjmp.s b/src/setjmp/x86_64/longjmp.s
index 1b2661c3..4f2bbc8c 100644
--- a/src/setjmp/x86_64/longjmp.s
+++ b/src/setjmp/x86_64/longjmp.s
@@ -1,10 +1,13 @@ 
 /* Copyright 2011-2012 Nicholas J. Kain, licensed under standard MIT license */
+.include "ibt.s"
+
 .global _longjmp
 .global longjmp
 .type _longjmp,@function
 .type longjmp,@function
 _longjmp:
 longjmp:
+	ENDBRANCH64
 	xor %eax,%eax
 	cmp $1,%esi             /* CF = val ? 0 : 1 */
 	adc %esi,%eax           /* eax = val + !val */
diff --git a/src/setjmp/x86_64/setjmp.s b/src/setjmp/x86_64/setjmp.s
index d95e4853..3a9f6fd6 100644
--- a/src/setjmp/x86_64/setjmp.s
+++ b/src/setjmp/x86_64/setjmp.s
@@ -1,4 +1,6 @@ 
 /* Copyright 2011-2012 Nicholas J. Kain, licensed under standard MIT license */
+.include "ibt.s"
+
 .global __setjmp
 .global _setjmp
 .global setjmp
@@ -8,6 +10,7 @@ 
 __setjmp:
 _setjmp:
 setjmp:
+	ENDBRANCH64
 	mov %rbx,(%rdi)         /* rdi is jmp_buf, move registers onto it */
 	mov %rbp,8(%rdi)
 	mov %r12,16(%rdi)
diff --git a/src/signal/x86_64/restore.s b/src/signal/x86_64/restore.s
index 27d6cf31..f6bda936 100644
--- a/src/signal/x86_64/restore.s
+++ b/src/signal/x86_64/restore.s
@@ -1,8 +1,11 @@ 
+.include "ibt.s"
+
 	nop
 .global __restore_rt
 .hidden __restore_rt
 .type __restore_rt,@function
 __restore_rt:
+	ENDBRANCH64
 	mov $15, %rax
 	syscall
 .size __restore_rt,.-__restore_rt
diff --git a/src/signal/x86_64/sigsetjmp.s b/src/signal/x86_64/sigsetjmp.s
index 9a7695f9..91f33f19 100644
--- a/src/signal/x86_64/sigsetjmp.s
+++ b/src/signal/x86_64/sigsetjmp.s
@@ -1,9 +1,12 @@ 
+.include "ibt.s"
+
 .global sigsetjmp
 .global __sigsetjmp
 .type sigsetjmp,@function
 .type __sigsetjmp,@function
 sigsetjmp:
 __sigsetjmp:
+	ENDBRANCH64
 	test %esi,%esi
 	jz 1f
 
diff --git a/src/string/x86_64/memcpy.s b/src/string/x86_64/memcpy.s
index 3d960efa..374235c8 100644
--- a/src/string/x86_64/memcpy.s
+++ b/src/string/x86_64/memcpy.s
@@ -1,8 +1,11 @@ 
+.include "ibt.s"
+
 .global memcpy
 .global __memcpy_fwd
 .hidden __memcpy_fwd
 .type memcpy,@function
 memcpy:
+	ENDBRANCH64
 __memcpy_fwd:
 	mov %rdi,%rax
 	cmp $8,%rdx
diff --git a/src/string/x86_64/memmove.s b/src/string/x86_64/memmove.s
index 172c0252..4782daa1 100644
--- a/src/string/x86_64/memmove.s
+++ b/src/string/x86_64/memmove.s
@@ -1,6 +1,9 @@ 
+.include "ibt.s"
+
 .global memmove
 .type memmove,@function
 memmove:
+	ENDBRANCH64
 	mov %rdi,%rax
 	sub %rsi,%rax
 	cmp %rdx,%rax
diff --git a/src/string/x86_64/memset.s b/src/string/x86_64/memset.s
index 2d3f5e52..802d2e8b 100644
--- a/src/string/x86_64/memset.s
+++ b/src/string/x86_64/memset.s
@@ -1,6 +1,9 @@ 
+.include "ibt.s"
+
 .global memset
 .type memset,@function
 memset:
+	ENDBRANCH64
 	movzbq %sil,%rax
 	mov $0x101010101010101,%r8
 	imul %r8,%rax
diff --git a/src/thread/x86_64/__set_thread_area.s b/src/thread/x86_64/__set_thread_area.s
index 7347ff4d..299845a6 100644
--- a/src/thread/x86_64/__set_thread_area.s
+++ b/src/thread/x86_64/__set_thread_area.s
@@ -1,9 +1,12 @@ 
 /* Copyright 2011-2012 Nicholas J. Kain, licensed under standard MIT license */
+.include "ibt.s"
+
 .text
 .global __set_thread_area
 .hidden __set_thread_area
 .type __set_thread_area,@function
 __set_thread_area:
+	ENDBRANCH64
 	mov %rdi,%rsi           /* shift for syscall */
 	movl $0x1002,%edi       /* SET_FS register */
 	movl $158,%eax          /* set fs segment to */
diff --git a/src/thread/x86_64/__unmapself.s b/src/thread/x86_64/__unmapself.s
index e2689e65..dfb84a13 100644
--- a/src/thread/x86_64/__unmapself.s
+++ b/src/thread/x86_64/__unmapself.s
@@ -1,8 +1,11 @@ 
 /* Copyright 2011-2012 Nicholas J. Kain, licensed under standard MIT license */
+.include "ibt.s"
+
 .text
 .global __unmapself
 .type   __unmapself,@function
 __unmapself:
+	ENDBRANCH64
 	movl $11,%eax   /* SYS_munmap */
 	syscall         /* munmap(arg2,arg3) */
 	xor %rdi,%rdi   /* exit() args: always return success */
diff --git a/src/thread/x86_64/clone.s b/src/thread/x86_64/clone.s
index 6e47bc0a..6b79832c 100644
--- a/src/thread/x86_64/clone.s
+++ b/src/thread/x86_64/clone.s
@@ -1,8 +1,11 @@ 
+.include "ibt.s"
+
 .text
 .global __clone
 .hidden __clone
 .type   __clone,@function
 __clone:
+	ENDBRANCH64
 	xor %eax,%eax
 	mov $56,%al
 	mov %rdi,%r11
diff --git a/src/thread/x86_64/syscall_cp.s b/src/thread/x86_64/syscall_cp.s
index 4f101716..b14ed3e4 100644
--- a/src/thread/x86_64/syscall_cp.s
+++ b/src/thread/x86_64/syscall_cp.s
@@ -1,3 +1,5 @@ 
+.include "ibt.s"
+
 .text
 .global __cp_begin
 .hidden __cp_begin
@@ -10,7 +12,7 @@ 
 .hidden __syscall_cp_asm
 .type   __syscall_cp_asm,@function
 __syscall_cp_asm:
-
+	ENDBRANCH64
 __cp_begin:
 	mov (%rdi),%eax
 	test %eax,%eax