From 6218e81f5dc2200f8d202bccd65c0c97da8f751c Mon Sep 17 00:00:00 2001 From: "John Hodge (sonata)" Date: Tue, 11 Mar 2014 07:59:52 +0800 Subject: [PATCH] AcessNative - Spawn in libacess-native (with AN_GETPATH syscall) --- AcessNative/acesskernel_src/Makefile | 2 +- AcessNative/acesskernel_src/main.c | 2 +- AcessNative/acesskernel_src/syscall_getpath.c | 51 +++++++++++++++++++ AcessNative/acesskernel_src/syscalls.c | 32 ++++++++---- AcessNative/ld-acess_src/Makefile | 2 +- AcessNative/ld-acess_src/exports.c | 5 +- AcessNative/ld-acess_src/exports.h | 3 +- AcessNative/ld-acess_src/syscalls.c | 8 ++- AcessNative/libacess-native.so_src/Makefile | 8 ++- AcessNative/libacess-native.so_src/common.h | 14 +++++ AcessNative/libacess-native.so_src/exports.c | 37 +++++++++----- AcessNative/libacess-native.so_src/main.c | 50 +++++++++++++++--- AcessNative/syscalls_list.h | 2 + 13 files changed, 181 insertions(+), 35 deletions(-) create mode 100644 AcessNative/acesskernel_src/syscall_getpath.c create mode 100644 AcessNative/libacess-native.so_src/common.h diff --git a/AcessNative/acesskernel_src/Makefile b/AcessNative/acesskernel_src/Makefile index 00ba7757..9e8ce35f 100644 --- a/AcessNative/acesskernel_src/Makefile +++ b/AcessNative/acesskernel_src/Makefile @@ -30,7 +30,7 @@ N_OBJ := main.o net_wrap.o # - Local objects (use the kernel includes) OBJ := helpers.o threads.o threads_glue.o server.o syscalls.o time.o OBJ += video.o keyboard.o mouse.o nativefs.o vfs_handle.o ui_sdl.o -OBJ += net.o +OBJ += net.o syscall_getpath.o BUILDINFO_OBJ := obj-$(PLATFORM)/buildinfo.o BUILDINFO_SRC := $(BUILDINFO_OBJ:%.o=%.c) diff --git a/AcessNative/acesskernel_src/main.c b/AcessNative/acesskernel_src/main.c index 61ed0fd8..ead4f33d 100644 --- a/AcessNative/acesskernel_src/main.c +++ b/AcessNative/acesskernel_src/main.c @@ -39,7 +39,7 @@ extern const char gsGitHash[]; extern int giBuildNumber; // === GLOBALS === -const char *gsAcessDir = "../Usermode/Output/x86_64"; +const char *gsAcessDir = "../Usermode/Output/native"; // === CODE === #ifndef __WIN32__ diff --git a/AcessNative/acesskernel_src/syscall_getpath.c b/AcessNative/acesskernel_src/syscall_getpath.c new file mode 100644 index 00000000..a47e6c97 --- /dev/null +++ b/AcessNative/acesskernel_src/syscall_getpath.c @@ -0,0 +1,51 @@ +/* + * AcessNative Kernel + * + * syscall_getpath.c + * - Implementation of the SYS_AN_GETPATH system call + */ + +#include +#include + +extern char *getcwd(char *buf, size_t size); + +extern tVFS_NodeType gNativeFS_FileNodeType; +extern tVFS_NodeType gNativeFS_DirNodeType; + +int Syscall_AN_GetPath_Real(char *Dst, size_t DstLen, const char *Path) +{ + tVFS_Node *node = VFS_ParsePath(Path, NULL, NULL); + if(!node) return -1; + + const char *relpath = NULL; + + if( node->Type == &gNativeFS_FileNodeType || node->Type == &gNativeFS_DirNodeType ) + { + relpath = node->Data; + } + else + { + relpath = NULL; + } + + size_t ret; + if( relpath ) + { + if( relpath[0] == '/' ) { + ret = snprintf(Dst, DstLen, "%s", relpath); + } + else { + getcwd(Dst, DstLen); + ret = strlen(Dst); + ret += snprintf(Dst+ret, DstLen-ret, "/%s", relpath); + } + } + else + { + ret = 0; + } + + _CloseNode(node); + return ret; +} diff --git a/AcessNative/acesskernel_src/syscalls.c b/AcessNative/acesskernel_src/syscalls.c index ae2c92c3..344745fd 100644 --- a/AcessNative/acesskernel_src/syscalls.c +++ b/AcessNative/acesskernel_src/syscalls.c @@ -16,14 +16,22 @@ // === IMPORTS === extern int Threads_Fork(void); // AcessNative only function extern int Threads_Spawn(int nFD, int FDs[], const void *info); +extern int Syscall_AN_GetPath_Real(char *Dest, size_t DstLen, const char *Path); // === TYPES === typedef int (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int *Sizes); // === MACROS === +#define _SYSCALL_CHKFMT(_name,_fmtstr,Fmt) do{ \ + if(strcmp(Fmt,_fmtstr) != 0) {\ + *Errno = EINVAL;\ + Log_Error("Syscalls", "Call %s takes args '%s', given '%s'", #_name, _fmtstr, Fmt);\ + return -1;\ + }\ +} while(0) #define SYSCALL6(_name, _fmtstr, _t0, _t1, _t2, _t3, _t4, _t5, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\ _t0 a0;_t1 a1;_t2 a2;_t3 a3;_t4 a4;_t5 a5;\ - if(strcmp(Fmt,_fmtstr)!=0)return 0;\ + _SYSCALL_CHKFMT(_name,_fmtstr,Fmt);\ a0 = *(_t0*)Args;Args+=sizeof(_t0);\ a1 = *(_t1*)Args;Args+=sizeof(_t1);\ a2 = *(_t2*)Args;Args+=sizeof(_t2);\ @@ -35,7 +43,7 @@ typedef int (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int } #define SYSCALL5(_name, _fmtstr, _t0, _t1, _t2, _t3, _t4, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\ _t0 a0;_t1 a1;_t2 a2;_t3 a3;_t4 a4;\ - if(strcmp(Fmt,_fmtstr)!=0)return 0;\ + _SYSCALL_CHKFMT(_name,_fmtstr,Fmt);\ a0 = *(_t0*)Args;Args+=sizeof(_t0);\ a1 = *(_t1*)Args;Args+=sizeof(_t1);\ a2 = *(_t2*)Args;Args+=sizeof(_t2);\ @@ -46,7 +54,7 @@ typedef int (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int } #define SYSCALL4(_name, _fmtstr, _t0, _t1, _t2, _t3, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\ _t0 a0;_t1 a1;_t2 a2;_t3 a3;\ - if(strcmp(Fmt,_fmtstr)!=0)return 0;\ + _SYSCALL_CHKFMT(_name,_fmtstr,Fmt);\ a0 = *(_t0*)Args;Args+=sizeof(_t0);\ a1 = *(_t1*)Args;Args+=sizeof(_t1);\ a2 = *(_t2*)Args;Args+=sizeof(_t2);\ @@ -57,7 +65,7 @@ typedef int (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int #define SYSCALL3(_name, _fmtstr, _t0, _t1, _t2, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\ _t0 a0;_t1 a1;_t2 a2;\ - if(strcmp(Fmt,_fmtstr)!=0)return 0;\ + _SYSCALL_CHKFMT(_name,_fmtstr,Fmt);\ a0 = *(_t0*)Args;Args+=sizeof(_t0);\ a1 = *(_t1*)Args;Args+=sizeof(_t1);\ a2 = *(_t2*)Args;Args+=sizeof(_t2);\ @@ -67,7 +75,7 @@ typedef int (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int #define SYSCALL2(_name, _fmtstr, _t0, _t1, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\ _t0 a0;_t1 a1;\ - if(strcmp(Fmt,_fmtstr)!=0)return 0;\ + _SYSCALL_CHKFMT(_name,_fmtstr,Fmt);\ a0 = *(_t0*)Args;Args+=sizeof(_t0);\ a1 = *(_t1*)Args;Args+=sizeof(_t1);\ LOG("SYSCALL2 '%s' %p %p", Fmt, (intptr_t)a0,(intptr_t)a1);\ @@ -76,14 +84,14 @@ typedef int (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int #define SYSCALL1(_name, _fmtstr, _t0, _call) int _name(Uint*Errno,const char*Fmt, void*Args,int*Sizes){\ _t0 a0;\ - if(strcmp(Fmt,_fmtstr)!=0)return 0;\ + _SYSCALL_CHKFMT(_name,_fmtstr,Fmt);\ a0 = *(_t0*)Args;Args+=sizeof(_t0);\ LOG("SYSCALL1 '%s' %p", Fmt,(intptr_t)a0);\ _call;\ } #define SYSCALL0(_name, _call) int _name(Uint*Errno,const char*Fmt, void*Args,int*Sizes){\ - if(strcmp(Fmt,"")!=0)return 0;\ + _SYSCALL_CHKFMT(_name,"",Fmt);\ LOG("SYSCALL0");\ _call;\ } @@ -167,6 +175,11 @@ SYSCALL4(Syscall_Mount, "ssss", const char *, const char *, const char *, const SYSCALL1(Syscall_Chdir, "s", const char *, return VFS_ChDir(a0); ); + +SYSCALL2(Syscall_AN_Getpath, "ds", char *, const char *, + return Syscall_AN_GetPath_Real(a0, Sizes[0], a1); +); + SYSCALL0(Syscall_Sleep, Threads_Sleep(); return 0; @@ -254,6 +267,7 @@ const tSyscallHandler caSyscalls[] = { [SYS_MOUNT] = Syscall_Mount, [SYS_REOPEN] = NULL, // SYS_REOPEN [SYS_CHDIR] = Syscall_Chdir, + [SYS_AN_GETPATH] = Syscall_AN_Getpath, [SYS_WAITTID] = Syscall_WaitTID, [SYS_SETUID] = Syscall_SetUID, @@ -265,8 +279,8 @@ const tSyscallHandler caSyscalls[] = { Syscall_GetGID, Syscall_Sleep, - Syscall_AN_Fork, - Syscall_AN_Spawn, + [SYS_AN_FORK] = Syscall_AN_Fork, + [SYS_AN_SPAWN] = Syscall_AN_Spawn, Syscall_SendMessage, Syscall_GetMessage, diff --git a/AcessNative/ld-acess_src/Makefile b/AcessNative/ld-acess_src/Makefile index 78e07856..b118824e 100644 --- a/AcessNative/ld-acess_src/Makefile +++ b/AcessNative/ld-acess_src/Makefile @@ -22,7 +22,7 @@ endif CFLAGS += -Wall CFLAGS += -Werror -CFLAGS += -g +CFLAGS += -g -std=c99 CPPFLAGS += -DARCHDIR_is_x86_64=1 LDFLAGS += -g -Wl,-T,obj-$(PLATFORM)/link.ld diff --git a/AcessNative/ld-acess_src/exports.c b/AcessNative/ld-acess_src/exports.c index 6ee25381..a898b5a3 100644 --- a/AcessNative/ld-acess_src/exports.c +++ b/AcessNative/ld-acess_src/exports.c @@ -49,7 +49,7 @@ int acess__SysChdir(const char *Path) return _Syscall(SYS_CHDIR, ">s", Path); } -int acess__SysOpen(const char *Path, int Flags) +int acess__SysOpen(const char *Path, unsigned int Flags) { if( strncmp(Path, "$$$$", 4) == 0 ) { @@ -342,7 +342,8 @@ int acess__SysSpawn(const char *binary, const char **argv, const char **envp, in int kernel_tid; int newID; - newID = _Syscall(SYS_AN_SPAWN, "d >d", sizeof(int), &kernel_tid, + newID = _Syscall(SYS_AN_SPAWN, "d >d", + sizeof(int), &kernel_tid, nfd*sizeof(int), fds, info ? sizeof(*info) : 0, info); diff --git a/AcessNative/ld-acess_src/exports.h b/AcessNative/ld-acess_src/exports.h index e9a4b3ba..e8f2e4c8 100644 --- a/AcessNative/ld-acess_src/exports.h +++ b/AcessNative/ld-acess_src/exports.h @@ -9,6 +9,7 @@ #define _EXPORTS_H_ #include +#include // Syscall request (used by acess_*) extern uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...); @@ -26,7 +27,7 @@ extern int native_execve(const char *filename, const char *const argv[], const c extern int native_spawn(const char *filename, const char *const argv[], const char *const envp[]); // Syscalls used by the linker -extern int acess__SysOpen(const char *Path, int Flags); +extern int acess__SysOpen(const char *Path, unsigned int Flags); extern void acess__SysClose(int FD); extern size_t acess__SysRead(int FD, void *Dest, size_t Bytes); extern int acess__SysSeek(int FD, int64_t Offset, int Dir); diff --git a/AcessNative/ld-acess_src/syscalls.c b/AcessNative/ld-acess_src/syscalls.c index 43dfe26e..60b7d952 100644 --- a/AcessNative/ld-acess_src/syscalls.c +++ b/AcessNative/ld-acess_src/syscalls.c @@ -342,12 +342,18 @@ int native_execve(const char *filename, const char *const argv[], const char *co int native_spawn(const char *filename, const char *const argv[], const char *const envp[]) { int rv; - + + fprintf(stderr, "native_spawn('%s')\n", filename); + #if __WIN32__ rv = _spawnve(_P_NOWAIT, filename, argv, envp); #else rv = posix_spawn(NULL, filename, NULL, NULL, (void*)argv, (void*)envp); #endif + if( rv == 0 ) { + perror("native_spawn"); + } + return rv; } diff --git a/AcessNative/libacess-native.so_src/Makefile b/AcessNative/libacess-native.so_src/Makefile index d67f319e..92783114 100644 --- a/AcessNative/libacess-native.so_src/Makefile +++ b/AcessNative/libacess-native.so_src/Makefile @@ -15,6 +15,8 @@ endif ifeq ($(PLATFORM),lin) BIN := ../libacess-native.so endif +BINLINK := ../../Usermode/Output/native/Libs/$(notdir $(BIN)) +$(warning $(BINLINK)) CFLAGS += -Wall CFLAGS += -Werror @@ -27,7 +29,7 @@ DEPFILES := $(DEPFILES:%=%.dep) .PHONY: all clean -all: $(BIN) +all: $(BIN) $(BINLINK) clean: $(RM) $(BIN) $(OBJ) $(DEPFILES) @@ -36,6 +38,10 @@ $(BIN): $(OBJ) $(CC) -o $@ $(OBJ) $(LDFLAGS) objdump -S $@ > $@.dsm +$(BINLINK): $(BIN) + @mkdir -p $(dir $@) + @cd $(dir $@) && ln -sf ../../../../AcessNative/$(notdir $@) + obj-$(PLATFORM)/%.o: %.c @mkdir -p $(dir $@) @echo [CC] -o $@ diff --git a/AcessNative/libacess-native.so_src/common.h b/AcessNative/libacess-native.so_src/common.h new file mode 100644 index 00000000..7e14f24c --- /dev/null +++ b/AcessNative/libacess-native.so_src/common.h @@ -0,0 +1,14 @@ + +#ifndef _LIBACESSNATIVE_COMMON_H_ +#define _LIBACESSNATIVE_COMMON_H_ + +extern int giSyscall_ClientID; +extern void Request_Preinit(void); +extern int acess__SysOpen(const char *Path, unsigned int flags); +extern int acessnative_spawn(const char *Binary, int SyscallID, const char * const * argv, const char * const * envp); + +#define ENV_VAR_PREOPENS "AN_PREOPEN" +#define ENV_VAR_KEY "ACESSNATIVE_KEY" + +#endif + diff --git a/AcessNative/libacess-native.so_src/exports.c b/AcessNative/libacess-native.so_src/exports.c index 70c7d774..2f960c4f 100644 --- a/AcessNative/libacess-native.so_src/exports.c +++ b/AcessNative/libacess-native.so_src/exports.c @@ -1,3 +1,4 @@ +#include "common.h" #define acess__SysSpawn _disabled_acess__SysSpawn #include "../ld-acess_src/exports.c" @@ -14,26 +15,38 @@ int *libc_geterrno(void) int acess__SysSpawn(const char *binary, const char **argv, const char **envp, int nfd, int fds[], struct s_sys_spawninfo *info) { int argc = 0; - while( argv[argc++] ); + while( argv[argc++] ) + ; Debug("_SysSpawn('%s', %p (%i), %p, %i, %p, %p)", binary, argv, argc, envp, nfd, fds, info); - int kernel_tid; - int newID; - newID = _Syscall(SYS_AN_SPAWN, "d >d", sizeof(int), &kernel_tid, + char realpath[256]; + realpath[255] = 0; + + if( _Syscall(SYS_AN_GETPATH, "s", sizeof(realpath)-1, realpath, binary) <= 0 ) { + Warning("No translation for path '%s'", binary); + acess__errno = -11; + return -1; + } + + Warning("TODO: Spawn '%s' = '%s'", binary, realpath); + + int emulated_tid; + int newID = _Syscall(SYS_AN_SPAWN, "d >d", + sizeof(emulated_tid), &emulated_tid, nfd*sizeof(int), fds, - info ? sizeof(*info) : 0, info); - + (info ? sizeof(*info) : 0), info + ); - Warning("TODO: Spawn '%s'", binary); - // TODO: Translate internal path to actual path + if( newID <= 0 ) { + return -1; + } - // TODO: set environment variables for libacess-native - // > ACESSNATIVE_KEY=`newID` - //native_spawn(binary, argv, envp); + if( acessnative_spawn(realpath, newID, argv, envp) ) { + } - return 0; + return emulated_tid; } void ldacess_DumpLoadedLibraries(void) diff --git a/AcessNative/libacess-native.so_src/main.c b/AcessNative/libacess-native.so_src/main.c index 900a14c0..74d9d3c6 100644 --- a/AcessNative/libacess-native.so_src/main.c +++ b/AcessNative/libacess-native.so_src/main.c @@ -5,10 +5,10 @@ #include #include #include - -extern int giSyscall_ClientID; -extern void Request_Preinit(void); -extern int acess__SysOpen(const char *Path, unsigned int flags); +#include +#include "common.h" +#include +#include "../ld-acess_src/exports.h" #ifdef __WINDOWS__ int DllMain(void) @@ -40,7 +40,7 @@ int libacessnative_init(int argc, char *argv[], char **envp) { Request_Preinit(); - const char *preopens = getenv_p(envp, "AN_PREOPEN"); + const char *preopens = getenv_p(envp, ENV_VAR_PREOPENS); printf("preopens = %s\n", preopens); if( preopens ) { @@ -68,12 +68,44 @@ int libacessnative_init(int argc, char *argv[], char **envp) } } -// if( !getenv("ACESSNATIVE_ID") +// 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[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[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++] = "LD_LIBRARY_PATH=Libs/"; // HACK + newenv[i] = NULL; + + // TODO: Detect native_spawn failing + return native_spawn(Binary, argv, newenv); +} void Debug(const char *format, ...) { @@ -103,3 +135,9 @@ void __libc_csu_init() { } +void __stack_chk_fail(void) +{ + fprintf(stderr, "__stack_chk_fail"); + exit(1); +} + diff --git a/AcessNative/syscalls_list.h b/AcessNative/syscalls_list.h index 209380bb..5698fd47 100644 --- a/AcessNative/syscalls_list.h +++ b/AcessNative/syscalls_list.h @@ -19,6 +19,8 @@ _(SYS_GETACL), _(SYS_MOUNT), _(SYS_CHDIR), +_(SYS_AN_GETPATH), + _(SYS_WAITTID), _(SYS_SETUID), _(SYS_SETGID), -- 2.20.1