Sfoglia il codice sorgente

simple strace test

isundil 10 anni fa
parent
commit
2382fea4f5
7 ha cambiato i file con 136 aggiunte e 2 eliminazioni
  1. 1 0
      .gitignore
  2. 4 0
      CMakeLists.txt
  3. 1 2
      src/CMakeLists.txt
  4. 12 0
      src/main.c
  5. 47 0
      src/param.c
  6. 56 0
      src/sandbox.c
  7. 15 0
      src/sandbox.h

+ 1 - 0
.gitignore

@@ -40,4 +40,5 @@ Makefile
 CMakeFiles
 cmake_install.cmake
 /CMakeCache.txt
+/bin
 

+ 4 - 0
CMakeLists.txt

@@ -1,5 +1,9 @@
 cmake_minimum_required(VERSION 2.8)
 project(sandbox)
 
+SET(EXECUTABLE_OUTPUT_PATH ../bin)
+add_definitions ("-Wall")
+add_definitions ("-g3")
+
 add_subdirectory(src)
  

+ 1 - 2
src/CMakeLists.txt

@@ -1,2 +1 @@
-add_executable(sandbox main.c)
- 
+add_executable(sandbox main.c param.c sandbox.c)

+ 12 - 0
src/main.c

@@ -1,6 +1,18 @@
 
+#include <stdlib.h>
+#include "sandbox.h"
+
 int main(int argc, char **argv)
 {
+	t_param *params;
+
+	if (argc < 2)
+		print_help(*argv, EXIT_FAILURE);
+	params = parse_argv((const char **) argv);
+	if (params == NULL)
+		return 0;
+	launch_program(params);
+	free(params);
 	return 0;
 }
 

+ 47 - 0
src/param.c

@@ -0,0 +1,47 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "sandbox.h"
+
+void print_help(const char *progname, int exit_status)
+{
+	fprintf(stderr, "Usage: %s [options] [--] command\n", progname);
+	exit(exit_status);
+}
+
+static int parse_param(t_param *result, const char **av)
+{
+	printf("Parsing -%s-\n", *av);
+	if (!strcmp(*av, "--"))
+	{
+		av++;
+		result->cmd = av;
+	}
+	return 1;
+}
+
+t_param *parse_argv(const char **av)
+{
+	t_param *result;
+	const char *progname;
+
+	result = (t_param *) malloc(sizeof(*result));
+	if (result == NULL)
+		return NULL;
+	for (progname = *(av++); *av && !result->cmd; ++av)
+	{
+		if (**av == '-')
+		{
+ 			if (!parse_param(result, av))
+			{
+				free(result);
+				print_help(progname, EXIT_FAILURE);
+			}
+		}
+		else
+			result->cmd = av;
+	}
+	return result;
+}
+

+ 56 - 0
src/sandbox.c

@@ -0,0 +1,56 @@
+
+#include <sys/ptrace.h>
+#include <sys/types.h>
+#include <sys/user.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include "sandbox.h"
+
+
+/* DEBUG HEADER */
+#include <stdio.h>
+
+void doExec(int pid_parent, const t_param *params)
+{
+	char **argv = (char **)params->cmd;
+
+	ptrace(PTRACE_TRACEME, 0, 0, 0);
+	kill(getpid(), SIGINT);
+	execvp(argv[0], argv);
+}
+
+void doTrace(int pid, const t_param *params)
+{
+	int status;
+	struct user_regs_struct registers;
+
+	waitpid(pid, &status, 0);
+	while (1)
+	{
+		ptrace(PTRACE_SYSCALL, pid, NULL, 0);
+		if (waitpid(pid, &status, 0) == -1)
+			break;
+		ptrace(PTRACE_GETREGS, pid, 0, &registers);
+		printf("CALL %llu\n", (int) registers.orig_rax);
+		fflush(stdout);
+
+		/* syscall return */
+		ptrace(PTRACE_SYSCALL, pid, NULL, 0);
+		if (waitpid(pid, &status, 0) == -1)
+			break;
+	}
+}
+
+int launch_program(const t_param *params)
+{
+	int child = fork();
+
+	if (child == -1)
+		return -1;
+	else if (child == 0)
+		doExec(getpid(), params);
+	else
+		doTrace(child, params);
+	return 0;
+}
+

+ 15 - 0
src/sandbox.h

@@ -0,0 +1,15 @@
+#ifndef   SANDBOX_H__
+# define  SANDBOX_H__
+
+typedef struct {
+	const char **cmd;
+} t_param;
+
+/* params.c */
+void print_help(const char *progname, int exit_status);
+t_param *parse_argv(const char **av);
+
+/* sandbox.c */
+int launch_program(const t_param *params);
+
+#endif /* SANDBOX_H__ */