|
@@ -1,8 +1,8 @@
|
|
|
|
|
|
|
|
#include <sys/ptrace.h>
|
|
#include <sys/ptrace.h>
|
|
|
#include <sys/types.h>
|
|
#include <sys/types.h>
|
|
|
-#include <sys/user.h>
|
|
|
|
|
#include <sys/wait.h>
|
|
#include <sys/wait.h>
|
|
|
|
|
+#include <strings.h>
|
|
|
#include <unistd.h>
|
|
#include <unistd.h>
|
|
|
#include "sandbox.h"
|
|
#include "sandbox.h"
|
|
|
|
|
|
|
@@ -14,29 +14,66 @@ void doExec(int pid_parent, const t_param *params)
|
|
|
{
|
|
{
|
|
|
char **argv = (char **)params->cmd;
|
|
char **argv = (char **)params->cmd;
|
|
|
|
|
|
|
|
- ptrace(PTRACE_TRACEME, 0, 0, 0);
|
|
|
|
|
- kill(getpid(), SIGINT);
|
|
|
|
|
|
|
+ //TODO check return value
|
|
|
execvp(argv[0], argv);
|
|
execvp(argv[0], argv);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+int manageSyscall(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;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static inline void init_syscalls(struct s_sandboxenv *env)
|
|
|
|
|
+{
|
|
|
|
|
+ bzero(env->functions, sizeof(*(env->functions)) * NR_syscalls);
|
|
|
|
|
+ env->functions[__NR_write] = ovr_write;
|
|
|
|
|
+ env->functions[__NR_open] = ovr_open;
|
|
|
|
|
+ env->functions[__NR_close] = ovr_close;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static inline int waitForSyscall(const int pid)
|
|
|
|
|
+{
|
|
|
|
|
+ int status;
|
|
|
|
|
+
|
|
|
|
|
+ ptrace(PTRACE_SYSCALL, pid, NULL, 0);
|
|
|
|
|
+ if (waitpid(pid, &status, 0) == -1)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
void doTrace(int pid, const t_param *params)
|
|
void doTrace(int pid, const t_param *params)
|
|
|
{
|
|
{
|
|
|
int status;
|
|
int status;
|
|
|
- struct user_regs_struct registers;
|
|
|
|
|
|
|
+ struct s_sandboxenv sandbox_env;
|
|
|
|
|
|
|
|
|
|
+ ptrace(PTRACE_ATTACH, pid, 0, 0);
|
|
|
|
|
+ kill(pid, SIGTRAP);
|
|
|
waitpid(pid, &status, 0);
|
|
waitpid(pid, &status, 0);
|
|
|
|
|
+ sandbox_env.params = params;
|
|
|
|
|
+ sandbox_env.child_pid = pid;
|
|
|
|
|
+ init_syscalls(&sandbox_env);
|
|
|
|
|
+
|
|
|
while (1)
|
|
while (1)
|
|
|
{
|
|
{
|
|
|
- ptrace(PTRACE_SYSCALL, pid, NULL, 0);
|
|
|
|
|
- if (waitpid(pid, &status, 0) == -1)
|
|
|
|
|
|
|
+ if (waitForSyscall(pid))
|
|
|
break;
|
|
break;
|
|
|
- ptrace(PTRACE_GETREGS, pid, 0, ®isters);
|
|
|
|
|
- printf("CALL %llu\n", (int) registers.orig_rax);
|
|
|
|
|
- fflush(stdout);
|
|
|
|
|
|
|
|
|
|
- /* syscall return */
|
|
|
|
|
- ptrace(PTRACE_SYSCALL, pid, NULL, 0);
|
|
|
|
|
- if (waitpid(pid, &status, 0) == -1)
|
|
|
|
|
|
|
+ ptrace(PTRACE_GETREGS, pid, 0, &(sandbox_env.registers));
|
|
|
|
|
+ // getregsset ?
|
|
|
|
|
+ if (manageSyscall(&sandbox_env))
|
|
|
|
|
+ ptrace(PTRACE_SETREGS, pid, 0, &(sandbox_env.registers));
|
|
|
|
|
+
|
|
|
|
|
+ if (waitForSyscall(pid))
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|