SELinux compatibility: memalign -> mmap

This commit is contained in:
WPMGPRoSToTeMa 2016-01-01 05:12:55 +03:00
parent 866339eff6
commit 48d7a04c73
7 changed files with 33 additions and 17 deletions

View File

@ -300,10 +300,10 @@ int load_amxscript(AMX *amx, void **program, const char *filename, char error[64
#elif defined(__GNUC__) #elif defined(__GNUC__)
# if defined(__APPLE__) # if defined(__APPLE__)
amx->base = (unsigned char *)valloc(amx->code_size); amx->base = (unsigned char *)valloc(amx->code_size);
# else
amx->base = (unsigned char *)memalign(sysconf(_SC_PAGESIZE), amx->code_size);
# endif
mprotect((void *)amx->base, amx->code_size, PROT_READ | PROT_WRITE | PROT_EXEC); mprotect((void *)amx->base, amx->code_size, PROT_READ | PROT_WRITE | PROT_EXEC);
# else
amx->base = (unsigned char *)mmap(nullptr, amx->code_size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
# endif
#endif #endif
if (amx->base) if (amx->base)
memcpy(amx->base, np, amx->code_size); memcpy(amx->base, np, amx->code_size);
@ -562,6 +562,7 @@ int unload_amxscript(AMX* amx, void** program)
{ {
#if defined JIT #if defined JIT
int flags = amx->flags; int flags = amx->flags;
long code_size = amx->code_size;
#endif #endif
Debugger *pDebugger = (Debugger *)amx->userdata[UD_DEBUGGER]; Debugger *pDebugger = (Debugger *)amx->userdata[UD_DEBUGGER];
@ -592,12 +593,16 @@ int unload_amxscript(AMX* amx, void** program)
{ {
delete [] prg; delete [] prg;
} else { } else {
#ifdef __linux__
munmap(prg, code_size);
#else
#ifdef free #ifdef free
#undef free #undef free
free(prg); free(prg);
#define free(ptr) m_deallocator(__FILE__, __LINE__, __FUNCTION__, m_alloc_free, ptr) #define free(ptr) m_deallocator(__FILE__, __LINE__, __FUNCTION__, m_alloc_free, ptr)
#else #else
free(prg); free(prg);
#endif
#endif #endif
} }
#elif defined WIN32 #elif defined WIN32

View File

@ -468,10 +468,10 @@ static cell AMX_NATIVE_CALL register_native(AMX *amx, cell *params)
#elif defined(__GNUC__) #elif defined(__GNUC__)
# if defined(__APPLE__) # if defined(__APPLE__)
pNative->pfn = (char *)valloc(size+10); pNative->pfn = (char *)valloc(size+10);
# else
pNative->pfn = (char *)memalign(sysconf(_SC_PAGESIZE), size+10);
# endif
mprotect((void *)pNative->pfn, size + 10, PROT_READ | PROT_WRITE | PROT_EXEC); mprotect((void *)pNative->pfn, size + 10, PROT_READ | PROT_WRITE | PROT_EXEC);
# else
pNative->pfn = (char *)mmap(nullptr, size + 10, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
# endif
#endif #endif
int id = (int)g_RegNatives.length(); int id = (int)g_RegNatives.length();
@ -492,7 +492,11 @@ void ClearPluginLibraries()
ClearLibraries(LibSource_Plugin); ClearLibraries(LibSource_Plugin);
for (size_t i=0; i<g_RegNatives.length(); i++) for (size_t i=0; i<g_RegNatives.length(); i++)
{ {
#ifdef __linux__
munmap(g_RegNatives[i]->pfn, amxx_DynaCodesize() + 10);
#else
delete [] g_RegNatives[i]->pfn; delete [] g_RegNatives[i]->pfn;
#endif
delete g_RegNatives[i]; delete g_RegNatives[i];
} }
g_RegNatives.clear(); g_RegNatives.clear();

View File

@ -562,10 +562,10 @@ namespace Trampolines
#elif defined(__GNUC__) #elif defined(__GNUC__)
# if defined(__APPLE__) # if defined(__APPLE__)
void *ret = valloc(m_size); void *ret = valloc(m_size);
# else
void *ret=memalign(sysconf(_SC_PAGESIZE), m_size);
# endif
mprotect(ret,m_size,PROT_READ|PROT_WRITE|PROT_EXEC); mprotect(ret,m_size,PROT_READ|PROT_WRITE|PROT_EXEC);
# else
void *ret=mmap(nullptr, m_size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
# endif
#endif #endif
memcpy(ret, m_buffer, m_size); memcpy(ret, m_buffer, m_size);
@ -588,7 +588,7 @@ namespace Trampolines
/** /**
* Utility to make a generic trampoline. * Utility to make a generic trampoline.
*/ */
inline void *CreateGenericTrampoline(bool thiscall, bool voidcall, bool retbuf, int paramcount, void *extraptr, void *callee) inline void *CreateGenericTrampoline(bool thiscall, bool voidcall, bool retbuf, int paramcount, void *extraptr, void *callee, int *size)
{ {
Trampolines::TrampolineMaker tramp; Trampolines::TrampolineMaker tramp;
@ -628,7 +628,7 @@ inline void *CreateGenericTrampoline(bool thiscall, bool voidcall, bool retbuf,
} }
#endif #endif
return tramp.Finish(NULL); return tramp.Finish(size);
}; };

View File

@ -37,9 +37,10 @@ public:
int del; // 1 if this hook should be destroyed after exec int del; // 1 if this hook should be destroyed after exec
void *tramp; // trampoline for this hook void *tramp; // trampoline for this hook
char *ent; // ent name that's being hooked char *ent; // ent name that's being hooked
int trampSize;
Hook(void **vtable_, int entry_, void *target_, bool voidcall, bool retbuf, int paramcount, char *name) : Hook(void **vtable_, int entry_, void *target_, bool voidcall, bool retbuf, int paramcount, char *name) :
func(NULL), vtable(vtable_), entry(entry_), target(target_), exec(0), del(0), tramp(NULL) func(NULL), vtable(vtable_), entry(entry_), target(target_), exec(0), del(0), tramp(NULL), trampSize(0)
{ {
// original function is vtable[entry] // original function is vtable[entry]
// to not make the compiler whine, cast vtable to int ** // to not make the compiler whine, cast vtable to int **
@ -48,7 +49,7 @@ public:
// now install a trampoline // now install a trampoline
// (int thiscall, int voidcall, int paramcount, void *extraptr) // (int thiscall, int voidcall, int paramcount, void *extraptr)
tramp = CreateGenericTrampoline(true, voidcall, retbuf, paramcount, (void*)this, target); tramp = CreateGenericTrampoline(true, voidcall, retbuf, paramcount, (void*)this, target, &trampSize);
// Insert into vtable // Insert into vtable
#if defined(_WIN32) #if defined(_WIN32)
@ -82,7 +83,9 @@ public:
ivtable[entry]=(int *)func; ivtable[entry]=(int *)func;
#if defined(_WIN32) #if defined(_WIN32)
VirtualFree(tramp, 0, MEM_RELEASE); VirtualFree(tramp, 0, MEM_RELEASE);
#elif defined(__linux__) || defined(__APPLE__) #elif defined(__linux__)
munmap(tramp, trampSize);
#elif defined(__APPLE__)
free(tramp); free(tramp);
#endif #endif

View File

@ -77,18 +77,20 @@ inline unsigned char *AllocatePageMemory(size_t size)
#elif defined __GNUC__ #elif defined __GNUC__
#if defined __APPLE__ #if defined __APPLE__
unsigned char *addr = (unsigned char *)valloc(size); unsigned char *addr = (unsigned char *)valloc(size);
#else
unsigned char *addr = (unsigned char *)memalign(sysconf(_SC_PAGESIZE), size);
#endif
mprotect(addr, size, PROT_READ | PROT_WRITE | PROT_EXEC); mprotect(addr, size, PROT_READ | PROT_WRITE | PROT_EXEC);
#else
unsigned char *addr = (unsigned char *)mmap(nullptr, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
#endif
return addr; return addr;
#endif #endif
} }
inline void FreePageMemory(void *addr) inline void FreePageMemory(void *addr, size_t size)
{ {
#if defined(WIN32) #if defined(WIN32)
VirtualFree(addr, 0, MEM_RELEASE); VirtualFree(addr, 0, MEM_RELEASE);
#elif defined(__linux__)
munmap(addr, size);
#else #else
free(addr); free(addr);
#endif #endif

View File

@ -186,6 +186,7 @@ jit_rewind:
//spengine->SetReadWrite(wr.outbase); //spengine->SetReadWrite(wr.outbase);
wr.outptr = wr.outbase; wr.outptr = wr.outbase;
detour_trampoline = wr.outbase; detour_trampoline = wr.outbase;
detour_trampolineSize = CodeSize;
goto jit_rewind; goto jit_rewind;
} }
@ -206,7 +207,7 @@ void CDetour::DeleteDetour()
if (detour_trampoline) if (detour_trampoline)
{ {
/* Free the allocated trampoline memory */ /* Free the allocated trampoline memory */
FreePageMemory(detour_trampoline); FreePageMemory(detour_trampoline, detour_trampolineSize);
detour_trampoline = NULL; detour_trampoline = NULL;
} }
} }

View File

@ -199,6 +199,7 @@ private:
void *detour_address; void *detour_address;
/* Address of the allocated trampoline function */ /* Address of the allocated trampoline function */
void *detour_trampoline; void *detour_trampoline;
size_t detour_trampolineSize;
/* Address of the callback handler */ /* Address of the callback handler */
void *detour_callback; void *detour_callback;
/* The function pointer used to call our trampoline */ /* The function pointer used to call our trampoline */