Skip to content

Commit 040a37d

Browse files
chen-hu-97cmb69
authored andcommitted
Add IBT support for fiber
Indirect Branch Tracking (IBT) is part of Intel's Control-Flow Enforcement Technology (CET). IBT is hardware based, forward edge Control-Flow-Integrity mechanism where any indirect CALL/JMP must target an ENDBR instruction or suffer #CP. This commit adds IBT support for fiber: 1. Add endbr32/64 in assembly 2. Inform compiler jump_fcontext may return via indirect branch Furthermore: gcc support CET since v8.1 and set it to default since gcc 11. That is, the ELF header of sapi/cli/php has a property named IBT. However, such property is lost since PHP8.1 because the assembly introduced by Fiber. This commit also fixes this. Closes GH-8339 Signed-off-by: Chen, Hu <hu1.chen@intel.com> Co-authored-by: Christoph M. Becker <cmbecker69@gmx.de>
1 parent 2236b2d commit 040a37d

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.1.8
44

5+
- Core:
6+
. Fixed bug GH-8338 (Intel CET is disabled unintentionally). (Chen, Hu)
7+
58
- MBString:
69
. mb_detect_encoding recognizes all letters in Czech alphabet (alexdowad)
710
. mb_detect_encoding recognizes all letters in Hungarian alphabet (alexdowad)

Zend/asm/jump_x86_64_sysv_elf_gas.S

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,18 @@
2424
* *
2525
****************************************************************************************/
2626

27+
# if defined __CET__
28+
# include <cet.h>
29+
# else
30+
# define _CET_ENDBR
31+
# endif
2732
.file "jump_x86_64_sysv_elf_gas.S"
2833
.text
2934
.globl jump_fcontext
3035
.type jump_fcontext,@function
3136
.align 16
3237
jump_fcontext:
38+
_CET_ENDBR
3339
leaq -0x38(%rsp), %rsp /* prepare stack */
3440

3541
#if !defined(BOOST_USE_TSX)

Zend/asm/make_x86_64_sysv_elf_gas.S

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,18 @@
2424
* *
2525
****************************************************************************************/
2626

27+
# if defined __CET__
28+
# include <cet.h>
29+
# else
30+
# define _CET_ENDBR
31+
# endif
2732
.file "make_x86_64_sysv_elf_gas.S"
2833
.text
2934
.globl make_fcontext
3035
.type make_fcontext,@function
3136
.align 16
3237
make_fcontext:
38+
_CET_ENDBR
3339
/* first arg of make_fcontext() == top of context-stack */
3440
movq %rdi, %rax
3541

@@ -66,11 +72,13 @@ make_fcontext:
6672
trampoline:
6773
/* store return address on stack */
6874
/* fix stack alignment */
75+
_CET_ENDBR
6976
push %rbp
7077
/* jump to context-function */
7178
jmp *%rbx
7279

7380
finish:
81+
_CET_ENDBR
7482
/* exit code is zero */
7583
xorq %rdi, %rdi
7684
/* exit application */

Zend/zend_fibers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ typedef struct {
141141

142142
/* These functions are defined in assembler files provided by boost.context (located in "Zend/asm"). */
143143
extern void *make_fcontext(void *sp, size_t size, void (*fn)(boost_context_data));
144-
extern boost_context_data jump_fcontext(void *to, zend_fiber_transfer *transfer);
144+
extern ZEND_INDIRECT_RETURN boost_context_data jump_fcontext(void *to, zend_fiber_transfer *transfer);
145145
#endif
146146

147147
ZEND_API zend_class_entry *zend_ce_fiber;

Zend/zend_portability.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,4 +679,10 @@ extern "C++" {
679679
# define ZEND_VOIDP(ptr) (ptr)
680680
#endif
681681

682+
#if defined(__GNUC__) && ZEND_GCC_VERSION >= 9000
683+
# define ZEND_INDIRECT_RETURN __attribute__((__indirect_return__))
684+
#else
685+
# define ZEND_INDIRECT_RETURN
686+
#endif
687+
682688
#endif /* ZEND_PORTABILITY_H */

0 commit comments

Comments
 (0)