Implements (untested not debuged) child embeding
This commit is contained in:
parent
0d4b246520
commit
ff6aa5c8e0
4 changed files with 43 additions and 10 deletions
42
asm_env.c
42
asm_env.c
|
|
@ -15,8 +15,15 @@
|
|||
*/
|
||||
#include "asm_env.h"
|
||||
|
||||
static int _asmsh_env_spawn(asmsh_env_t *asmenv, const char *childpath);
|
||||
static int _asmsh_env_spawn(asmsh_env_t *asmenv);
|
||||
static void _asmsh_env_child(const char *childpath);
|
||||
/** Return a path (that should be freed) of a temporary executable
|
||||
* child that can be exec on */
|
||||
static char *asmsh_env_tmpexec();
|
||||
|
||||
/* binary buffer of the child elf */
|
||||
extern unsigned char _binary_child_start;
|
||||
extern unsigned char _binary_child_end;
|
||||
|
||||
asmsh_env_t* asmsh_env(const char *childpath)
|
||||
{
|
||||
|
|
@ -32,12 +39,13 @@ asmsh_env_t* asmsh_env(const char *childpath)
|
|||
}
|
||||
child_mmap_init(&(res->mmap));
|
||||
|
||||
if((res->childpath = strdup(childpath)) == NULL)
|
||||
res->childpath = NULL;
|
||||
if(childpath && (res->childpath = strdup(childpath)) == NULL)
|
||||
{
|
||||
goto err_pathdup;
|
||||
}
|
||||
|
||||
if(_asmsh_env_spawn(res, childpath) < 0)
|
||||
if(_asmsh_env_spawn(res) < 0)
|
||||
{
|
||||
err = errno;
|
||||
goto err;
|
||||
|
|
@ -249,11 +257,13 @@ int asmsh_env_update_regs(asmsh_env_t *asmenv)
|
|||
}
|
||||
|
||||
|
||||
static int _asmsh_env_spawn(asmsh_env_t *env, const char *childpath)
|
||||
static int _asmsh_env_spawn(asmsh_env_t *env)
|
||||
{
|
||||
int err;
|
||||
int wstatus;
|
||||
|
||||
const char *childpath = env->childpath?env->childpath:asmsh_env_tmpexec();
|
||||
|
||||
if((env->pid = fork()) == -1)
|
||||
{
|
||||
err = errno;
|
||||
|
|
@ -263,7 +273,7 @@ static int _asmsh_env_spawn(asmsh_env_t *env, const char *childpath)
|
|||
else if(env->pid == 0)
|
||||
{
|
||||
free(env);
|
||||
_asmsh_env_child(childpath?childpath:ASMSH_CHILD_PATH_DEFAULT);
|
||||
_asmsh_env_child(childpath);
|
||||
}
|
||||
|
||||
if(ptrace(PTRACE_ATTACH, env->pid, 0, 0) == -1)
|
||||
|
|
@ -347,6 +357,8 @@ static int _asmsh_env_spawn(asmsh_env_t *env, const char *childpath)
|
|||
}
|
||||
}
|
||||
|
||||
if(!env->childpath) { unlink(childpath); } // rm tmp child exec
|
||||
|
||||
return 0;
|
||||
|
||||
/// TODO replace by an utility function that logs ?
|
||||
|
|
@ -378,6 +390,9 @@ err_wstatus:
|
|||
err:
|
||||
kill(env->pid, SIGKILL);
|
||||
err_fork:
|
||||
|
||||
if(!env->childpath) { unlink(childpath); } // rm tmp child exec
|
||||
|
||||
errno = err;
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -399,3 +414,20 @@ static void _asmsh_env_child(const char *childpath)
|
|||
exit(err?err:-1);
|
||||
}
|
||||
|
||||
static char *asmsh_env_tmpexec()
|
||||
{
|
||||
char *ret = strdup("asmsh_child_XXXXXXXXX");
|
||||
int tmpfd = mkstemp(ret);
|
||||
const int sz = &_binary_child_end - &_binary_child_start;
|
||||
int rsz = write(tmpfd, &_binary_child_start, sz);
|
||||
if(rsz<sz)
|
||||
{
|
||||
perror("Unable to write the child executable");
|
||||
free(ret);
|
||||
return NULL;
|
||||
}
|
||||
fchmod(tmpfd, 0555);
|
||||
close(tmpfd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -31,8 +31,6 @@
|
|||
///! Initial size of the child's memory map with PROT_EXEC permission
|
||||
#define ASMSH_CHILD_TEXT_MAP_SZ 0x1000 // defined in child.s
|
||||
|
||||
#define ASMSH_CHILD_PATH_DEFAULT "./child"
|
||||
|
||||
typedef struct asmsh_env_s asmsh_env_t;
|
||||
|
||||
struct asmsh_env_s
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@
|
|||
/* Define to 1 if you have the `fork' function. */
|
||||
#undef HAVE_FORK
|
||||
|
||||
/* Define to 1 if you have the `gmtime_r' function. */
|
||||
#undef HAVE_GMTIME_R
|
||||
/* Define to 1 if you have the `gmtime_r,' function. */
|
||||
#undef HAVE_GMTIME_R_
|
||||
|
||||
/* Define to 1 if you have the <inttypes.h> header file. */
|
||||
#undef HAVE_INTTYPES_H
|
||||
|
|
@ -28,6 +28,9 @@
|
|||
/* Define to 1 if you have the <memory.h> header file. */
|
||||
#undef HAVE_MEMORY_H
|
||||
|
||||
/* Define to 1 if you have the `ptrace' function. */
|
||||
#undef HAVE_PTRACE
|
||||
|
||||
/* Define to 1 if your system has a GNU libc compatible `realloc' function,
|
||||
and to 0 otherwise. */
|
||||
#undef HAVE_REALLOC
|
||||
|
|
|
|||
|
|
@ -73,7 +73,7 @@ AC_TYPE_SIZE_T
|
|||
AC_FUNC_FORK
|
||||
AC_FUNC_MALLOC
|
||||
AC_FUNC_REALLOC
|
||||
AC_CHECK_FUNCS([bzero strtoull, gmtime_r])
|
||||
AC_CHECK_FUNCS([bzero strtoull, gmtime_r, ptrace])
|
||||
|
||||
AC_CONFIG_FILES([Makefile tests/Makefile tests/samples/Makefile])
|
||||
AC_OUTPUT
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue