|
|
@@ -1,36 +1,29 @@
|
|
|
-
|
|
|
#include <sys/ptrace.h>
|
|
|
#include <sys/types.h>
|
|
|
#include <sys/wait.h>
|
|
|
+#include <sys/reg.h>
|
|
|
+#include <sys/uio.h>
|
|
|
#include <strings.h>
|
|
|
+#include <string.h>
|
|
|
#include <unistd.h>
|
|
|
+#include <elf.h>
|
|
|
#include "sandbox.h"
|
|
|
|
|
|
-
|
|
|
/* DEBUG HEADER */
|
|
|
#include <stdio.h>
|
|
|
|
|
|
-void doExec(int pid_parent, const t_param *params)
|
|
|
-{
|
|
|
- char **argv = (char **)params->cmd;
|
|
|
-
|
|
|
- //TODO check return value
|
|
|
- execvp(argv[0], argv);
|
|
|
-}
|
|
|
-
|
|
|
-int manageSyscall(struct s_sandboxenv *env)
|
|
|
+static inline void get_args(struct s_sandboxenv *env)
|
|
|
{
|
|
|
- t_syscall_fnc ovr_fnc;
|
|
|
-
|
|
|
- /*
|
|
|
- printf("CALL %d\n", (int) env->registers.orig_rax);
|
|
|
- fflush(stdout);
|
|
|
- */
|
|
|
- if (env->registers.orig_rax >= NR_syscalls ||
|
|
|
- !(ovr_fnc = env->functions[(int) env->registers.orig_rax]))
|
|
|
- return 0;
|
|
|
- (ovr_fnc)(env, env->registers.rbx, env->registers.rcx, env->registers.rdx);
|
|
|
- return 1;
|
|
|
+#ifdef __x86_64__
|
|
|
+ REGISTER_TYPE result[] = { env->registers.rdi, env->registers.rsi,
|
|
|
+ env->registers.rdx, env->registers.r10,
|
|
|
+ env->registers.r8, env->registers.r9 };
|
|
|
+#else
|
|
|
+ REGISTER_TYPE result[] = { env->registers.ebx, env->registers.ecx,
|
|
|
+ env->registers.edx, env->registers.esi,
|
|
|
+ env->registers.edi, env->registers.ebp };
|
|
|
+#endif
|
|
|
+ memcpy(env->syscall_args, result, sizeof(REGISTER_TYPE) * 6);
|
|
|
}
|
|
|
|
|
|
static inline void init_syscalls(struct s_sandboxenv *env)
|
|
|
@@ -51,10 +44,23 @@ static inline int waitForSyscall(const int pid)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+int manageSyscall(struct s_sandboxenv *env)
|
|
|
+{
|
|
|
+ t_syscall_fnc ovr_fnc;
|
|
|
+
|
|
|
+ if (env->registers.orig_rax >= NR_syscalls ||
|
|
|
+ !(ovr_fnc = env->functions[(int) env->registers.orig_rax]))
|
|
|
+ return 0;
|
|
|
+ get_args(env);
|
|
|
+ (ovr_fnc)(env);
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
void doTrace(int pid, const t_param *params)
|
|
|
{
|
|
|
int status;
|
|
|
struct s_sandboxenv sandbox_env;
|
|
|
+ struct iovec iov = { &(sandbox_env.registers), sizeof(sandbox_env.registers) };
|
|
|
|
|
|
ptrace(PTRACE_ATTACH, pid, 0, 0);
|
|
|
kill(pid, SIGTRAP);
|
|
|
@@ -67,14 +73,14 @@ void doTrace(int pid, const t_param *params)
|
|
|
{
|
|
|
if (waitForSyscall(pid))
|
|
|
break;
|
|
|
-
|
|
|
- ptrace(PTRACE_GETREGS, pid, 0, &(sandbox_env.registers));
|
|
|
- // getregsset ?
|
|
|
+ ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov);
|
|
|
if (manageSyscall(&sandbox_env))
|
|
|
ptrace(PTRACE_SETREGS, pid, 0, &(sandbox_env.registers));
|
|
|
-
|
|
|
if (waitForSyscall(pid))
|
|
|
break;
|
|
|
+ iov.iov_len = sizeof(sandbox_env.registers);
|
|
|
+ ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov);
|
|
|
+ manageSyscall(&sandbox_env);
|
|
|
}
|
|
|
}
|
|
|
|