Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shadowstack syscall number redefinition #270

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
27 changes: 15 additions & 12 deletions include/boost/context/fiber_fcontext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@
#if defined(__CET__) && defined(__unix__)
# include <cet.h>
# include <sys/mman.h>
# include <unistd.h>
# define SHSTK_ENABLED (__CET__ & 0x2)
# define BOOST_CONTEXT_SHADOW_STACK (SHSTK_ENABLED && SHADOW_STACK_SYSCALL)
# define __NR_map_shadow_stack 451
# if !defined(__NR_map_shadow_stack)
# define __NR_map_shadow_stack 453
# endif
#ifndef SHADOW_STACK_SET_TOKEN
# define SHADOW_STACK_SET_TOKEN 0x1
#endif
Expand Down Expand Up @@ -168,8 +171,8 @@ template< typename Record, typename StackAlloc, typename Fn >
fcontext_t create_fiber1( StackAlloc && salloc, Fn && fn) {
auto sctx = salloc.allocate();
// reserve space for control structure
void * storage = reinterpret_cast< void * >(
( reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sizeof( Record) ) )
void * storage = reinterpret_cast< void * >(
( reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sizeof( Record) ) )
& ~static_cast< uintptr_t >( 0xff) );
// placment new for control structure on context stack
Record * record = new ( storage) Record{
Expand All @@ -186,16 +189,16 @@ fcontext_t create_fiber1( StackAlloc && salloc, Fn && fn) {
#if BOOST_CONTEXT_SHADOW_STACK
std::size_t ss_size = size >> 5;
// align shadow stack to 8 bytes.
ss_size = (ss_size + 7) & ~7;
ss_size = (ss_size + 7) & ~7;
// Todo: shadow stack occupies at least 4KB
ss_size = (ss_size > 4096) ? size : 4096;
// create shadow stack
void *ss_base = (void *)syscall(__NR_map_shadow_stack, 0, ss_size, SHADOW_STACK_SET_TOKEN);
BOOST_ASSERT(ss_base != -1);
unsigned long ss_sp = (unsigned long)ss_base + ss_size;
/* pass the shadow stack pointer to make_fcontext
i.e., link the new shadow stack with the new fcontext
TODO should be a better way? */
i.e., link the new shadow stack with the new fcontext
TODO should be a better way? */
*((unsigned long*)(reinterpret_cast< uintptr_t >( stack_top)- 8)) = ss_sp;
/* Todo: place shadow stack info in 64byte gap */
*((unsigned long*)(reinterpret_cast< uintptr_t >( storage)- 8)) = (unsigned long) ss_base;
Expand Down Expand Up @@ -227,16 +230,16 @@ fcontext_t create_fiber2( preallocated palloc, StackAlloc && salloc, Fn && fn) {
#if BOOST_CONTEXT_SHADOW_STACK
std::size_t ss_size = size >> 5;
// align shadow stack to 8 bytes.
ss_size = (ss_size + 7) & ~7;
ss_size = (ss_size + 7) & ~7;
// Todo: shadow stack occupies at least 4KB
ss_size = (ss_size > 4096) ? size : 4096;
// create shadow stack
void *ss_base = (void *)syscall(__NR_map_shadow_stack, 0, ss_size, SHADOW_STACK_SET_TOKEN);
BOOST_ASSERT(ss_base != -1);
unsigned long ss_sp = (unsigned long)ss_base + ss_size;
/* pass the shadow stack pointer to make_fcontext
i.e., link the new shadow stack with the new fcontext
TODO should be a better way? */
i.e., link the new shadow stack with the new fcontext
TODO should be a better way? */
*((unsigned long*)(reinterpret_cast< uintptr_t >( stack_top)- 8)) = ss_sp;
/* Todo: place shadow stack info in 64byte gap */
*((unsigned long*)(reinterpret_cast< uintptr_t >( storage)- 8)) = (unsigned long) ss_base;
Expand Down Expand Up @@ -359,7 +362,7 @@ class fiber {
}

#if !defined(BOOST_EMBTC)

template< typename charT, class traitsT >
friend std::basic_ostream< charT, traitsT > &
operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other) {
Expand All @@ -371,7 +374,7 @@ class fiber {
}

#else

template< typename charT, class traitsT >
friend std::basic_ostream< charT, traitsT > &
operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other);
Expand All @@ -396,7 +399,7 @@ class fiber {
}

#endif

inline
void swap( fiber & l, fiber & r) noexcept {
l.swap( r);
Expand Down
Loading