X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;ds=sidebyside;f=AcessNative%2Flibacess-native.so_src%2Fmain.c;h=d1e868474dfb8ec10f45f06aeae736682fe763dd;hb=372f6414f56b17c0e9a4306f8e77355c59814f9c;hp=4bf908bf84d664bce995058657870c9004f6dfa0;hpb=edbf6589036993a821a4d2f04af62af8f87a3289;p=tpg%2Facess2.git diff --git a/AcessNative/libacess-native.so_src/main.c b/AcessNative/libacess-native.so_src/main.c index 4bf908bf..d1e86847 100644 --- a/AcessNative/libacess-native.so_src/main.c +++ b/AcessNative/libacess-native.so_src/main.c @@ -1,23 +1,115 @@ /* */ #include +#include #include +#include +#include +#include +#include "common.h" +#include +#include "../ld-acess_src/exports.h" + +extern int gbSyscallDebugEnabled; #ifdef __WINDOWS__ int DllMain(void) { + fprintf(stderr, "TODO: Windows libacessnative setup\n"); return 0; } #endif #ifdef __linux__ -int main(int argc, char *argv[], char **envp) +int __attribute__ ((constructor(102))) libacessnative_init(int argc, char *argv[], char **envp); + +const char *getenv_p(char **envp, const char *name) +{ + size_t namelen = strlen(name); + for(; *envp; envp ++) + { + if( strncmp(*envp, name, namelen) != 0 ) + continue ; + if( (*envp)[namelen] != '=' ) + continue ; + return (*envp)+namelen+1; + } + return 0; +} + +int libacessnative_init(int argc, char *argv[], char **envp) { + Request_Preinit(); + + //gbSyscallDebugEnabled = 1; + + const char *preopens = getenv_p(envp, ENV_VAR_PREOPENS); + printf("preopens = %s\n", preopens); + if( preopens ) + { + while( *preopens ) + { + const char *splitter = strchr(preopens, ':'); + size_t len; + if( !splitter ) { + len = strlen(preopens); + } + else { + len = splitter - preopens; + } + char path[len+1]; + memcpy(path, preopens, len); + path[len] = 0; + int fd = acess__SysOpen(path, 6); // WRITE,READ,no EXEC + if( fd == -1 ) { + fprintf(stderr, "Unable to preopen '%s' errno=%i\n", path, acess__errno); + } + + if( !splitter ) + break; + preopens = splitter + 1; + } + } + +// if( !getenv(ENV_VAR_KEY) + return 0; } #endif +int acessnative_spawn(const char *Binary, int SyscallID, const char * const * argv, const char * const * envp) +{ + int envc = 0; + while( envp && envp[envc++] ) + envc ++; + + // Set environment variables for libacess-native + // > ACESSNATIVE_KEY=`newID` + size_t keystr_len = snprintf(NULL, 0, "%s=%i", ENV_VAR_KEY, SyscallID); + char keystr[keystr_len+1]; + snprintf(keystr, keystr_len+1, "%s=%i", ENV_VAR_KEY, SyscallID); + bool bKeyHit = false; + + const char *newenv[envc+2+1]; + int i = 0; + for( ; envp && envp[i]; i ++ ) + { + const char *ev = envp[i]; + if( strncmp(ev, ENV_VAR_KEY"=", sizeof(ENV_VAR_KEY"=")) == 0 ) { + ev = keystr; + bKeyHit = true; + } + newenv[i] = ev; + } + if( !bKeyHit ) + newenv[i++] = keystr; + newenv[i++] = getenv("LD_LIBRARY_PATH") - (sizeof("LD_LIBRARY_PATH=")-1); // VERY hacky + newenv[i] = NULL; + + // TODO: Detect native_spawn failing + return native_spawn(Binary, argv, newenv); +} void Debug(const char *format, ...) { @@ -39,3 +131,17 @@ void Warning(const char *format, ...) printf("\n"); } +void __libc_csu_fini() +{ +} + +void __libc_csu_init() +{ +} + +void __stack_chk_fail(void) +{ + fprintf(stderr, "__stack_chk_fail"); + exit(1); +} +