|
|
@@ -13,16 +13,6 @@
|
|
|
|
|
|
static inline void get_args(struct s_sandboxenv *env)
|
|
|
{
|
|
|
-#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)
|
|
|
@@ -33,11 +23,15 @@ static inline void init_syscalls(struct s_sandboxenv *env)
|
|
|
env->functions[__NR_close] = ovr_close;
|
|
|
}
|
|
|
|
|
|
-static inline int waitForSyscall(const int pid)
|
|
|
+int waitForSyscall(const int pid, int _status)
|
|
|
{
|
|
|
int status;
|
|
|
+ static int current_status = SANDBOX_SYS_EXIT;
|
|
|
|
|
|
+ if (current_status == _status || (_status != SANDBOX_SYS_ENTER && _status != SANDBOX_SYS_EXIT))
|
|
|
+ return 0;
|
|
|
ptrace(PTRACE_SYSCALL, pid, NULL, 0);
|
|
|
+ current_status = _status;
|
|
|
if (waitpid(pid, &status, 0) == -1)
|
|
|
return -1;
|
|
|
return 0;
|
|
|
@@ -46,51 +40,64 @@ static inline int waitForSyscall(const int pid)
|
|
|
int manageSyscall(struct s_sandboxenv *env)
|
|
|
{
|
|
|
t_syscall_fnc ovr_fnc;
|
|
|
- unsigned int syscall_nr;
|
|
|
-
|
|
|
-#ifdef __x86_64__
|
|
|
- syscall_nr = (unsigned int) env->registers.orig_rax;
|
|
|
-#else
|
|
|
- syscall_nr = (unsigned int) env->registers.orig_eax;
|
|
|
-#endif
|
|
|
|
|
|
- if (syscall_nr >= NR_syscalls ||
|
|
|
- !(ovr_fnc = env->functions[syscall_nr]))
|
|
|
+ if (env->syscall_no.syscall_no >= NR_syscalls ||
|
|
|
+ !(ovr_fnc = env->functions[env->syscall_no.syscall_no]))
|
|
|
{
|
|
|
- /* TODO verbose -v */
|
|
|
+ /* Unrecognized syscall */
|
|
|
return 0;
|
|
|
}
|
|
|
- get_args(env);
|
|
|
- (ovr_fnc)(env);
|
|
|
- return 1;
|
|
|
+ return (ovr_fnc)(env);
|
|
|
+}
|
|
|
+
|
|
|
+void read_registers(struct s_sandboxenv *env)
|
|
|
+{
|
|
|
+ struct iovec iov = { &(env->registers), sizeof(env->registers) };
|
|
|
+
|
|
|
+ ptrace(PTRACE_GETREGSET, env->child_pid, NT_PRSTATUS, &iov);
|
|
|
+#ifdef __x86_64__
|
|
|
+ env->syscall_no.syscall_no = (unsigned int) env->registers.orig_rax;
|
|
|
+ env->syscall_no.syscall_return = (unsigned int) env->registers.rax;
|
|
|
+ REGISTER_TYPE result[] = { env->registers.rdi, env->registers.rsi,
|
|
|
+ env->registers.rdx, env->registers.r10,
|
|
|
+ env->registers.r8, env->registers.r9 };
|
|
|
+#else
|
|
|
+#error "Register result"
|
|
|
+ env->syscall_no.syscall_no = (unsigned int) env->registers.orig_eax;
|
|
|
+ env->syscall_no.syscall_return = (unsigned int) env->registers.eax;
|
|
|
+ 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);
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
waitpid(pid, &status, 0);
|
|
|
sandbox_env.params = params;
|
|
|
sandbox_env.child_pid = pid;
|
|
|
+ sandbox_env.filetable = sllist_create();
|
|
|
init_syscalls(&sandbox_env);
|
|
|
|
|
|
while (1)
|
|
|
{
|
|
|
- if (waitForSyscall(pid))
|
|
|
+ if (waitForSyscall(pid, SANDBOX_SYS_ENTER))
|
|
|
break;
|
|
|
- ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov);
|
|
|
+ read_registers(&sandbox_env);
|
|
|
if (manageSyscall(&sandbox_env))
|
|
|
ptrace(PTRACE_SETREGS, pid, 0, &(sandbox_env.registers));
|
|
|
- if (waitForSyscall(pid))
|
|
|
+ if (waitForSyscall(pid, SANDBOX_SYS_EXIT))
|
|
|
break;
|
|
|
- iov.iov_len = sizeof(sandbox_env.registers);
|
|
|
- ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov);
|
|
|
- manageSyscall(&sandbox_env);
|
|
|
}
|
|
|
+
|
|
|
+ /* TODO */
|
|
|
+ sllist_destroy(sandbox_env.filetable);
|
|
|
}
|
|
|
|
|
|
int launch_program(const t_param *params)
|