|
@@ -15,8 +15,15 @@
|
15
|
15
|
*/
|
16
|
16
|
#include "asm_env.h"
|
17
|
17
|
|
18
|
|
-static int _asmsh_env_spawn(asmsh_env_t *asmenv, const char *childpath);
|
|
18
|
+static int _asmsh_env_spawn(asmsh_env_t *asmenv);
|
19
|
19
|
static void _asmsh_env_child(const char *childpath);
|
|
20
|
+/** Return a path (that should be freed) of a temporary executable
|
|
21
|
+ * child that can be exec on */
|
|
22
|
+static char *asmsh_env_tmpexec();
|
|
23
|
+
|
|
24
|
+/* binary buffer of the child elf */
|
|
25
|
+extern unsigned char _binary_child_start;
|
|
26
|
+extern unsigned char _binary_child_end;
|
20
|
27
|
|
21
|
28
|
asmsh_env_t* asmsh_env(const char *childpath)
|
22
|
29
|
{
|
|
@@ -32,12 +39,13 @@ asmsh_env_t* asmsh_env(const char *childpath)
|
32
|
39
|
}
|
33
|
40
|
child_mmap_init(&(res->mmap));
|
34
|
41
|
|
35
|
|
- if((res->childpath = strdup(childpath)) == NULL)
|
|
42
|
+ res->childpath = NULL;
|
|
43
|
+ if(childpath && (res->childpath = strdup(childpath)) == NULL)
|
36
|
44
|
{
|
37
|
45
|
goto err_pathdup;
|
38
|
46
|
}
|
39
|
47
|
|
40
|
|
- if(_asmsh_env_spawn(res, childpath) < 0)
|
|
48
|
+ if(_asmsh_env_spawn(res) < 0)
|
41
|
49
|
{
|
42
|
50
|
err = errno;
|
43
|
51
|
goto err;
|
|
@@ -249,11 +257,13 @@ int asmsh_env_update_regs(asmsh_env_t *asmenv)
|
249
|
257
|
}
|
250
|
258
|
|
251
|
259
|
|
252
|
|
-static int _asmsh_env_spawn(asmsh_env_t *env, const char *childpath)
|
|
260
|
+static int _asmsh_env_spawn(asmsh_env_t *env)
|
253
|
261
|
{
|
254
|
262
|
int err;
|
255
|
263
|
int wstatus;
|
256
|
264
|
|
|
265
|
+ const char *childpath = env->childpath?env->childpath:asmsh_env_tmpexec();
|
|
266
|
+
|
257
|
267
|
if((env->pid = fork()) == -1)
|
258
|
268
|
{
|
259
|
269
|
err = errno;
|
|
@@ -263,7 +273,7 @@ static int _asmsh_env_spawn(asmsh_env_t *env, const char *childpath)
|
263
|
273
|
else if(env->pid == 0)
|
264
|
274
|
{
|
265
|
275
|
free(env);
|
266
|
|
- _asmsh_env_child(childpath?childpath:ASMSH_CHILD_PATH_DEFAULT);
|
|
276
|
+ _asmsh_env_child(childpath);
|
267
|
277
|
}
|
268
|
278
|
|
269
|
279
|
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)
|
347
|
357
|
}
|
348
|
358
|
}
|
349
|
359
|
|
|
360
|
+ if(!env->childpath) { unlink(childpath); } // rm tmp child exec
|
|
361
|
+
|
350
|
362
|
return 0;
|
351
|
363
|
|
352
|
364
|
/// TODO replace by an utility function that logs ?
|
|
@@ -378,6 +390,9 @@ err_wstatus:
|
378
|
390
|
err:
|
379
|
391
|
kill(env->pid, SIGKILL);
|
380
|
392
|
err_fork:
|
|
393
|
+
|
|
394
|
+ if(!env->childpath) { unlink(childpath); } // rm tmp child exec
|
|
395
|
+
|
381
|
396
|
errno = err;
|
382
|
397
|
return -1;
|
383
|
398
|
}
|
|
@@ -399,3 +414,20 @@ static void _asmsh_env_child(const char *childpath)
|
399
|
414
|
exit(err?err:-1);
|
400
|
415
|
}
|
401
|
416
|
|
|
417
|
+static char *asmsh_env_tmpexec()
|
|
418
|
+{
|
|
419
|
+ char *ret = strdup("asmsh_child_XXXXXXXXX");
|
|
420
|
+ int tmpfd = mkstemp(ret);
|
|
421
|
+ const int sz = &_binary_child_end - &_binary_child_start;
|
|
422
|
+ int rsz = write(tmpfd, &_binary_child_start, sz);
|
|
423
|
+ if(rsz<sz)
|
|
424
|
+ {
|
|
425
|
+ perror("Unable to write the child executable");
|
|
426
|
+ free(ret);
|
|
427
|
+ return NULL;
|
|
428
|
+ }
|
|
429
|
+ fchmod(tmpfd, 0555);
|
|
430
|
+ close(tmpfd);
|
|
431
|
+ return ret;
|
|
432
|
+}
|
|
433
|
+
|