Эх сурвалжийг харах

strace open, write, close

isundil 10 жил өмнө
parent
commit
feb011d56c

+ 1 - 1
src/CMakeLists.txt

@@ -1 +1 @@
-add_executable(sandbox main.c exec.c param.c sandbox.c ovr_syscall/ovr_write.c ovr_syscall/ovr_open.c ovr_syscall/ovr_close.c)
+add_executable(sandbox main.c mem.c exec.c param.c sandbox.c ovr_syscall/ovr_write.c ovr_syscall/ovr_open.c ovr_syscall/ovr_close.c)

+ 0 - 1
src/exec.c

@@ -1,4 +1,3 @@
-#include <unistd.h>
 #include "sandbox.h"
 
 void doExec(int pid_parent, const t_param *params)

+ 0 - 1
src/main.c

@@ -1,4 +1,3 @@
-
 #include <stdlib.h>
 #include "sandbox.h"
 

+ 40 - 0
src/mem.c

@@ -0,0 +1,40 @@
+#include <sys/uio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "sandbox.h"
+
+static inline size_t process_vm_read(int pid, struct iovec *local, struct iovec *remote)
+{
+	return process_vm_readv(pid, local, 1, remote, 1, 0);
+}
+
+void *getMem(const struct s_sandboxenv *env, size_t ptr, int *readlen)
+{
+	char *result;
+	int seg = 1;
+	struct iovec local, remote;
+	size_t read;
+
+	local.iov_base = result = (char *) malloc(sizeof(*result) * 2048);
+	local.iov_len = 2048;
+	remote.iov_base = (char *) ptr;
+	remote.iov_len = 2048;
+
+	while (1)
+	{
+		read = process_vm_read(env->child_pid, &local, &remote);
+		if (read < 2048)
+		{
+			((char*)local.iov_base)[read] = 0;
+			break;
+		}
+		if ((read = strnlen(local.iov_base, 2048)) < 2048)
+			break;
+		result = realloc(result, (++seg) * 2048);
+		local.iov_base += 2048;
+	}
+	if (readlen)
+		*readlen = (2048 * seg) + read;
+	return result;
+}
+

+ 2 - 3
src/ovr_syscall/ovr_open.c

@@ -9,13 +9,12 @@
 
 int ovr_open(struct s_sandboxenv *env)
 {
-	const char *pathname = (const char *) env->syscall_args[0];
+	const char *pathname = getMem(env, (size_t) env->syscall_args[0], NULL);
 	int flags = (int) env->syscall_args[1];
 	mode_t mode = (mode_t) env->syscall_args[2];
 
-	printf("DO open ! ([%x], [%d], [%d])\n", pathname, flags, mode);
+	printf("DO open ! ([%s], [%d], [%d])\n", pathname, flags, mode);
 	fflush(stdout);
-	//env->registers.orig_rax = -1;
 	return 0;
 }
 

+ 4 - 3
src/ovr_syscall/ovr_write.c

@@ -1,4 +1,5 @@
 
+#include <stdlib.h>
 #include "sandbox.h"
 
 /* DEBUG HEADER */
@@ -7,12 +8,12 @@
 int ovr_write(struct s_sandboxenv *env)
 {
 	int fd = (int) env->syscall_args[0];
-	char *buf = (char *) env->syscall_args[1];
+	char *buf = getMem(env, (size_t) env->syscall_args[1], NULL);
 	int buflen = (int) env->syscall_args[2];
 
-
-	printf("DO WRITE ! ([%d], [%x], [%d])\n", fd, buf, buflen);
+	printf("DO WRITE ! ([%d], [%s], [%d])\n", fd, buf, buflen);
 	fflush(stdout);
+	free(buf);
 	//env->registers.orig_rax = -1;
 	return 0;
 }

+ 0 - 1
src/param.c

@@ -1,4 +1,3 @@
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>

+ 3 - 4
src/sandbox.c

@@ -5,7 +5,6 @@
 #include <sys/uio.h>
 #include <strings.h>
 #include <string.h>
-#include <unistd.h>
 #include <elf.h>
 #include "sandbox.h"
 
@@ -47,12 +46,12 @@ static inline int waitForSyscall(const int pid)
 int manageSyscall(struct s_sandboxenv *env)
 {
 	t_syscall_fnc ovr_fnc;
-	int syscall_nr;
+	unsigned int syscall_nr;
 
 #ifdef __x86_64__
-	syscall_nr = (int) env->registers.orig_rax;
+	syscall_nr = (unsigned int) env->registers.orig_rax;
 #else
-	syscall_nr = (int) env->registers.orig_eax;
+	syscall_nr = (unsigned int) env->registers.orig_eax;
 #endif
 
 	if (syscall_nr >= NR_syscalls ||

+ 9 - 0
src/sandbox.h

@@ -3,6 +3,7 @@
 
 # include <sys/user.h>
 # include <sys/syscall.h>
+# include <unistd.h>
 
 # ifndef NR_syscalls
 #  define NR_syscalls 386
@@ -41,6 +42,14 @@ void doTrace(int pid, const t_param *params);
 /* exec.c */
 void doExec(int pid_parent, const t_param *params);
 
+/* mem.c */
+/**
+ * Get memory segment from addr ptr to first nullbyte
+ * The returned addr is allocated with malloc and should be freed
+ * readlen indicate successfully read bytes (nullable)
+**/
+void *getMem(const struct s_sandboxenv *env, size_t ptr, int *readlen);
+
 # include "sandbox_syscall.h"
 
 #endif /* SANDBOX_H__ */