Browse Source

Implements (untested not debuged) child embeding

Yann Weber 1 year ago
parent
commit
ff6aa5c8e0
4 changed files with 43 additions and 10 deletions
  1. 37
    5
      asm_env.c
  2. 0
    2
      asm_env.h
  3. 5
    2
      config.h.in
  4. 1
    1
      configure.ac

+ 37
- 5
asm_env.c View File

@@ -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
+

+ 0
- 2
asm_env.h View File

@@ -31,8 +31,6 @@
31 31
 ///! Initial size of the child's memory map with PROT_EXEC permission
32 32
 #define ASMSH_CHILD_TEXT_MAP_SZ 0x1000 // defined in child.s
33 33
 
34
-#define ASMSH_CHILD_PATH_DEFAULT "./child"
35
-
36 34
 typedef struct asmsh_env_s asmsh_env_t;
37 35
 
38 36
 struct asmsh_env_s

+ 5
- 2
config.h.in View File

@@ -9,8 +9,8 @@
9 9
 /* Define to 1 if you have the `fork' function. */
10 10
 #undef HAVE_FORK
11 11
 
12
-/* Define to 1 if you have the `gmtime_r' function. */
13
-#undef HAVE_GMTIME_R
12
+/* Define to 1 if you have the `gmtime_r,' function. */
13
+#undef HAVE_GMTIME_R_
14 14
 
15 15
 /* Define to 1 if you have the <inttypes.h> header file. */
16 16
 #undef HAVE_INTTYPES_H
@@ -28,6 +28,9 @@
28 28
 /* Define to 1 if you have the <memory.h> header file. */
29 29
 #undef HAVE_MEMORY_H
30 30
 
31
+/* Define to 1 if you have the `ptrace' function. */
32
+#undef HAVE_PTRACE
33
+
31 34
 /* Define to 1 if your system has a GNU libc compatible `realloc' function,
32 35
    and to 0 otherwise. */
33 36
 #undef HAVE_REALLOC

+ 1
- 1
configure.ac View File

@@ -73,7 +73,7 @@ AC_TYPE_SIZE_T
73 73
 AC_FUNC_FORK
74 74
 AC_FUNC_MALLOC
75 75
 AC_FUNC_REALLOC
76
-AC_CHECK_FUNCS([bzero strtoull, gmtime_r])
76
+AC_CHECK_FUNCS([bzero strtoull, gmtime_r, ptrace])
77 77
 
78 78
 AC_CONFIG_FILES([Makefile tests/Makefile tests/samples/Makefile])
79 79
 AC_OUTPUT

Loading…
Cancel
Save