Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Zend/zend_fibers.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,14 @@
#endif

# if defined __CET__
# include <sys/syscall.h>
# include <unistd.h>
# include <cet.h>
# define SHSTK_ENABLED (__CET__ & 0x2)
# define BOOST_CONTEXT_SHADOW_STACK (SHSTK_ENABLED && SHADOW_STACK_SYSCALL)
# define __NR_map_shadow_stack 451
# ifndef SYS_map_shadow_stack
# define SYS_map_shadow_stack 453
# endif
# ifndef SHADOW_STACK_SET_TOKEN
# define SHADOW_STACK_SET_TOKEN 0x1
#endif
Expand Down Expand Up @@ -275,15 +279,11 @@ static zend_fiber_stack *zend_fiber_stack_allocate(size_t size)
stack->size = stack_size;

#if !defined(ZEND_FIBER_UCONTEXT) && BOOST_CONTEXT_SHADOW_STACK
/* shadow stack saves ret address only, need less space */
stack->ss_size= stack_size >> 5;

/* align shadow stack to 8 bytes. */
stack->ss_size = (stack->ss_size + 7) & ~7;
stack->ss_size = stack_size;

/* issue syscall to create shadow stack for the new fcontext */
/* SHADOW_STACK_SET_TOKEN option will put "restore token" on the new shadow stack */
stack->ss_base = (void *)syscall(__NR_map_shadow_stack, 0, stack->ss_size, SHADOW_STACK_SET_TOKEN);
stack->ss_base = (void *)syscall(SYS_map_shadow_stack, 0, stack->ss_size, SHADOW_STACK_SET_TOKEN);

if (stack->ss_base == MAP_FAILED) {
zend_throw_exception_ex(NULL, 0, "Fiber shadow stack allocate failed: mmap failed: %s (%d)", strerror(errno), errno);
Expand Down
11 changes: 9 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1294,11 +1294,18 @@ fi
AC_CACHE_CHECK([whether syscall to create shadow stack exists],
[php_cv_have_shadow_stack_syscall],
[AC_RUN_IFELSE([AC_LANG_SOURCE([
#include <sys/syscall.h>
#include <unistd.h>
#include <sys/mman.h>
#ifndef SYS_map_shadow_stack
# define SYS_map_shadow_stack 453
#endif
#ifndef SHADOW_STACK_SET_TOKEN
# define SHADOW_STACK_SET_TOKEN 0x1
#endif
int main(void) {
/* test if syscall 451, i.e., map_shadow_stack is available */
void* base = (void *)syscall(451, 0, 0x20000, 0x1);
/* test if map_shadow_stack is available */
void* base = (void *)syscall(SYS_map_shadow_stack, 0, 0x20000, SHADOW_STACK_SET_TOKEN);
if (base != (void*)-1) {
munmap(base, 0x20000);
return 0;
Expand Down
Loading