Skip to content

Commit

Permalink
replace valloc() with mmap() when allocating executable pages for cal…
Browse files Browse the repository at this point in the history
…lbacks

Under linux, if SELinux is enabled (i.e. not permisive or disabled), then
the heap memory allocated with vmalloc() and marked as executable with mprotect()
will fail.  In order to have this work with SELinux enabled (and not providing
a module policy file to allow execheap) mmap() should be used.

Note, it might be possible to remove the stdlib.h and sys/mman.h header
file references.
  • Loading branch information
bwestergaard committed Oct 30, 2018
1 parent df9064a commit 8c4b25a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
7 changes: 2 additions & 5 deletions platforms/Cross/plugins/IA32ABI/ia32abicc.c
Expand Up @@ -292,14 +292,11 @@ allocateExecutablePage(long *size)
#else
long pagesize = getpagesize();

if (!(mem = valloc(pagesize)))
if (!(mem = mmap(0, pagesize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0)))
return 0;

// MAP_ANON should zero out the allocated page, but explicitly doing it shouldn't hurt
memset(mem, 0, pagesize);
if (mprotect(mem, pagesize, PROT_READ | PROT_WRITE | PROT_EXEC) < 0) {
free(mem);
return 0;
}
*size = pagesize;
#endif
return mem;
Expand Down
7 changes: 2 additions & 5 deletions platforms/Cross/plugins/IA32ABI/x64sysvabicc.c
Expand Up @@ -282,14 +282,11 @@ allocateExecutablePage(long *size)
#else
long pagesize = getpagesize();

if (!(mem = valloc(pagesize)))
if (!(mem = mmap(0, pagesize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0)))
return 0;

// MAP_ANON should zero out the allocated page, but explicitly doing it shouldn't hurt
memset(mem, 0, pagesize);
if (mprotect(mem, pagesize, PROT_READ | PROT_WRITE | PROT_EXEC) < 0) {
free(mem);
return 0;
}
*size = pagesize;
#endif
return mem;
Expand Down

0 comments on commit 8c4b25a

Please sign in to comment.