|
@@ -33,7 +33,7 @@ asmsh_env_t* asmsh_env(const char *childpath)
|
33
|
33
|
if((res = malloc(sizeof(*res))) == NULL)
|
34
|
34
|
{
|
35
|
35
|
err = errno;
|
36
|
|
- perror("Unable to allocate env");
|
|
36
|
+ asmsh_log_perror("Unable to allocate env");
|
37
|
37
|
errno = err;
|
38
|
38
|
return NULL;
|
39
|
39
|
}
|
|
@@ -110,7 +110,7 @@ int asmsh_env_write_mem(asmsh_env_t *env, void *addr, const unsigned char *buf,
|
110
|
110
|
if(errno)
|
111
|
111
|
{
|
112
|
112
|
err = errno;
|
113
|
|
- perror("Unable to peektext in order to allign write");
|
|
113
|
+ asmsh_log_perror("Unable to peektext in order to allign write");
|
114
|
114
|
errno = err;
|
115
|
115
|
return -1;
|
116
|
116
|
}
|
|
@@ -118,7 +118,7 @@ int asmsh_env_write_mem(asmsh_env_t *env, void *addr, const unsigned char *buf,
|
118
|
118
|
if(ptrace(PTRACE_POKETEXT, env->pid, wr_addr, data) < 0)
|
119
|
119
|
{
|
120
|
120
|
err = errno;
|
121
|
|
- perror("Unable to poketext in order to allign write");
|
|
121
|
+ asmsh_log_perror("Unable to poketext in order to allign write");
|
122
|
122
|
errno = err;
|
123
|
123
|
return -1;
|
124
|
124
|
}
|
|
@@ -147,7 +147,7 @@ int asmsh_env_write_mem(asmsh_env_t *env, void *addr, const unsigned char *buf,
|
147
|
147
|
if(ptrace(PTRACE_POKETEXT, env->pid, env->code_write_ptr, data) < 0)
|
148
|
148
|
{
|
149
|
149
|
err = errno;
|
150
|
|
- perror("Unable to poketext");
|
|
150
|
+ asmsh_log_perror("Unable to poketext");
|
151
|
151
|
errno = err;
|
152
|
152
|
return -1;
|
153
|
153
|
}
|
|
@@ -174,13 +174,13 @@ int asmsh_env_step(asmsh_env_t *env, int *status)
|
174
|
174
|
if(ptrace(PTRACE_SINGLESTEP, env->pid, NULL, 0) < 0)
|
175
|
175
|
{
|
176
|
176
|
err = errno;
|
177
|
|
- perror("Unable to ptrace singlestep");
|
|
177
|
+ asmsh_log_perror("Unable to ptrace singlestep");
|
178
|
178
|
goto err;
|
179
|
179
|
}
|
180
|
180
|
if(waitpid(env->pid, &env->status, 0) < 0)
|
181
|
181
|
{
|
182
|
182
|
err = errno;
|
183
|
|
- perror("Unable to wait for child process to stop on step");
|
|
183
|
+ asmsh_log_perror("Unable to wait for child process to stop on step");
|
184
|
184
|
goto err;
|
185
|
185
|
}
|
186
|
186
|
if(status) { *status = env->status; }
|
|
@@ -254,7 +254,7 @@ int asmsh_env_update_regs(asmsh_env_t *asmenv)
|
254
|
254
|
if(ptrace(PTRACE_GETREGS, asmenv->pid, NULL, &(asmenv->regs)) < 0)
|
255
|
255
|
{
|
256
|
256
|
int err = errno;
|
257
|
|
- perror("ptrace getregs error");
|
|
257
|
+ asmsh_log_perror("ptrace getregs error");
|
258
|
258
|
errno = err;
|
259
|
259
|
return -1;
|
260
|
260
|
}
|
|
@@ -277,7 +277,7 @@ static int _asmsh_env_spawn(asmsh_env_t *env)
|
277
|
277
|
if((env->pid = fork()) == -1)
|
278
|
278
|
{
|
279
|
279
|
err = errno;
|
280
|
|
- perror("Unable to fork!");
|
|
280
|
+ asmsh_log_perror("Unable to fork!");
|
281
|
281
|
goto err_fork;
|
282
|
282
|
}
|
283
|
283
|
else if(env->pid == 0)
|
|
@@ -288,7 +288,7 @@ static int _asmsh_env_spawn(asmsh_env_t *env)
|
288
|
288
|
if(waitpid(env->pid, &wstatus, WUNTRACED) < 0)
|
289
|
289
|
{
|
290
|
290
|
err=errno;
|
291
|
|
- perror("Unable to wait for child process");
|
|
291
|
+ asmsh_log_perror("Unable to wait for child process");
|
292
|
292
|
goto err;
|
293
|
293
|
}
|
294
|
294
|
if(!WIFSTOPPED(wstatus))
|
|
@@ -300,14 +300,14 @@ static int _asmsh_env_spawn(asmsh_env_t *env)
|
300
|
300
|
if(ptrace(PTRACE_ATTACH, env->pid, 0, 0) == -1)
|
301
|
301
|
{
|
302
|
302
|
err=errno;
|
303
|
|
- perror("Unable to attach to child process");
|
|
303
|
+ asmsh_log_perror("Unable to attach to child process");
|
304
|
304
|
goto err;
|
305
|
305
|
}
|
306
|
306
|
|
307
|
307
|
if(waitpid(env->pid, &wstatus, 0) < 0)
|
308
|
308
|
{
|
309
|
309
|
err=errno;
|
310
|
|
- perror("Unable to wait for child process");
|
|
310
|
+ asmsh_log_perror("Unable to wait for child process");
|
311
|
311
|
goto err;
|
312
|
312
|
}
|
313
|
313
|
if(!WIFSTOPPED(wstatus))
|
|
@@ -321,7 +321,7 @@ static int _asmsh_env_spawn(asmsh_env_t *env)
|
321
|
321
|
if(ptrace(PTRACE_SETOPTIONS, env->pid, NULL, PTRACE_O_TRACEEXEC) < 0)
|
322
|
322
|
{
|
323
|
323
|
err = errno;
|
324
|
|
- perror("ptrace setoptions failed");
|
|
324
|
+ asmsh_log_perror("ptrace setoptions failed");
|
325
|
325
|
goto err;
|
326
|
326
|
}
|
327
|
327
|
|
|
@@ -330,14 +330,14 @@ static int _asmsh_env_spawn(asmsh_env_t *env)
|
330
|
330
|
if(ptrace(PTRACE_CONT, env->pid, NULL, 0) < 0)
|
331
|
331
|
{
|
332
|
332
|
err = errno;
|
333
|
|
- perror("ptrace CONT failed after attach");
|
|
333
|
+ asmsh_log_perror("ptrace CONT failed after attach");
|
334
|
334
|
goto err;
|
335
|
335
|
}
|
336
|
336
|
|
337
|
337
|
if(waitpid(env->pid, &wstatus, 0) < 0)
|
338
|
338
|
{
|
339
|
339
|
err = errno;
|
340
|
|
- perror("Unable to wait for child process to stop after exec");
|
|
340
|
+ asmsh_log_perror("Unable to wait for child process to stop after exec");
|
341
|
341
|
goto err;
|
342
|
342
|
}
|
343
|
343
|
if(wstatus >> 8 != (SIGTRAP | (PTRACE_EVENT_EXEC<<8)) && \
|
|
@@ -353,14 +353,14 @@ static int _asmsh_env_spawn(asmsh_env_t *env)
|
353
|
353
|
if(ptrace(PTRACE_SYSCALL, env->pid, NULL, 0) < 0)
|
354
|
354
|
{
|
355
|
355
|
err = errno;
|
356
|
|
- perror("Unable to ptrace syscall");
|
357
|
|
- dprintf(2, "ptrace syscall failed on %d time\n", i+1);
|
|
356
|
+ asmsh_log_error("Unable to ptrace syscall on %dth time : %s",
|
|
357
|
+ i+1, strerror(errno));
|
358
|
358
|
goto err;
|
359
|
359
|
}
|
360
|
360
|
if(waitpid(env->pid, &wstatus, 0) < 0)
|
361
|
361
|
{
|
362
|
362
|
err = errno;
|
363
|
|
- perror("Unable to wait for child process to stop on syscall");
|
|
363
|
+ asmsh_log_perror("Unable to wait for child process to stop on syscall");
|
364
|
364
|
goto err;
|
365
|
365
|
}
|
366
|
366
|
if(wstatus != 1407 && wstatus != 263551)
|
|
@@ -437,6 +437,8 @@ static void _asmsh_env_child(const char *childpath)
|
437
|
437
|
{
|
438
|
438
|
int err = errno;
|
439
|
439
|
perror("Unable to strdupa asmsh childpath :/");
|
|
440
|
+ //asmsh_log_perror("Unable to strdupa asmsh childpath :/");
|
|
441
|
+ ///! @todo dump logs before exit !
|
440
|
442
|
exit(err?err:-1);
|
441
|
443
|
}
|
442
|
444
|
|
|
@@ -444,29 +446,39 @@ static void _asmsh_env_child(const char *childpath)
|
444
|
446
|
execve(childpath, argv, envp);
|
445
|
447
|
int err = errno;
|
446
|
448
|
perror("Unable to execve");
|
|
449
|
+ //asmsh_log_perror("Child is unable to execve");
|
|
450
|
+ ///! @todo dump logs before exit !
|
447
|
451
|
exit(err?err:-1);
|
448
|
452
|
}
|
449
|
453
|
|
450
|
454
|
static char *asmsh_env_tmpexec()
|
451
|
455
|
{
|
452
|
456
|
char *ret = strdup("asmsh_child_XXXXXXXXX");
|
|
457
|
+ int err;
|
453
|
458
|
if(!ret)
|
454
|
459
|
{
|
455
|
|
- perror("getting a temporary file name");
|
|
460
|
+ err = errno;
|
|
461
|
+ asmsh_log_perror("getting a temporary file name");
|
|
462
|
+ errno = err;
|
456
|
463
|
return NULL;
|
457
|
464
|
}
|
458
|
465
|
int tmpfd = mkstemp(ret);
|
459
|
466
|
if(tmpfd < 0)
|
460
|
467
|
{
|
461
|
|
- perror("Unable to mk temporary file");
|
|
468
|
+ err = errno;
|
|
469
|
+ asmsh_log_perror("Unable to mk temporary file");
|
|
470
|
+ errno = err;
|
462
|
471
|
return NULL;
|
463
|
472
|
}
|
464
|
473
|
const int sz = &_binary_child_end - &_binary_child_start;
|
465
|
474
|
int rsz = write(tmpfd, &_binary_child_start, sz);
|
466
|
475
|
if(rsz<sz)
|
467
|
476
|
{
|
468
|
|
- perror("Unable to write the child executable");
|
|
477
|
+ // TODO : differenciate incomplete write & error !
|
|
478
|
+ err = errno;
|
|
479
|
+ asmsh_log_perror("Unable to write the child executable");
|
469
|
480
|
free(ret);
|
|
481
|
+ errno = err;
|
470
|
482
|
return NULL;
|
471
|
483
|
}
|
472
|
484
|
fchmod(tmpfd, 0555);
|