Skip to content

Commit c833353

Browse files
committed
sljit/exec: provide function to test runtime availability of rwx maps
SELinux or PaX/grsecurity based kernels may deny creating writable and executable mappings, leading to errors when trying to allocate JIT memory, even though JIT support is generally available. Provide a function to probe for the runtime availability of rwx maps to support users like libpcre2 which can use it to announce the lack of JIT and fall back to the interpreter instead. This function is only needed for Linux and only if we're using the default JIT memory allocator, as all others implement workarounds via double mappings. Signed-off-by: Mathias Krause <[email protected]>
1 parent a134b62 commit c833353

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/sljit/sljitConfigInternal.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,17 @@ SLJIT_API_FUNC_ATTRIBUTE sljit_sw sljit_exec_offset(void* ptr);
633633
#define SLJIT_EXEC_OFFSET(ptr) sljit_exec_offset(ptr)
634634
#else
635635
#define SLJIT_EXEC_OFFSET(ptr) 0
636+
637+
/* SELinux or grsecurity kernels may deny creating rwx mappings, so we need
638+
to probe at runtime if JIT memory is supported. */
639+
#if defined __linux__ && \
640+
(!defined SLJIT_PROT_EXECUTABLE_ALLOCATOR || !SLJIT_PROT_EXECUTABLE_ALLOCATOR) && \
641+
(!defined SLJIT_WX_EXECUTABLE_ALLOCATOR || !SLJIT_WX_EXECUTABLE_ALLOCATOR)
642+
SLJIT_API_FUNC_ATTRIBUTE int sljit_get_runtime_support(void);
643+
#else
644+
#define sljit_get_runtime_support() 1
645+
#endif
646+
636647
#endif
637648

638649
#endif /* SLJIT_EXECUTABLE_ALLOCATOR */

src/sljit/sljitExecAllocator.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,51 @@ static SLJIT_INLINE void free_chunk(void *chunk, sljit_uw size)
9494

9595
#else /* POSIX */
9696

97+
#ifdef __linux__
98+
SLJIT_API_FUNC_ATTRIBUTE int sljit_get_runtime_support(void)
99+
{
100+
int status = -1;
101+
size_t size;
102+
void *addr;
103+
FILE *f;
104+
105+
/* Try to get the status from /proc/self/status, looking for PaX flags. */
106+
f = fopen("/proc/self/status", "r");
107+
if (f) {
108+
char *buf = NULL;
109+
size_t len;
110+
111+
while (getline(&buf, &len, f) != -1) {
112+
if (strncmp(buf, "PaX:", 4))
113+
continue;
114+
115+
/* Look for 'm', indicating PaX MPROTECT is disabled. */
116+
status = !!strchr(buf+4, 'm');
117+
break;
118+
}
119+
120+
fclose(f);
121+
free(buf);
122+
123+
if (status != -1)
124+
return status;
125+
}
126+
127+
/*
128+
* Create a temporary mapping and try to make it writable to probe for rwx
129+
* support without generating a log message.
130+
*/
131+
size = get_page_alignment() + 1;
132+
addr = mmap(NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANON, -1, 0);
133+
if (addr == MAP_FAILED)
134+
return 0;
135+
136+
munmap(addr, size);
137+
138+
return 1;
139+
}
140+
#endif
141+
97142
#if defined(__APPLE__) && defined(MAP_JIT)
98143
/*
99144
On macOS systems, returns MAP_JIT if it is defined _and_ we're running on a

0 commit comments

Comments
 (0)