isundil před 9 roky
rodič
revize
e9d9ee60ca

+ 7 - 1
CMakeLists.txt

@@ -1,7 +1,7 @@
 cmake_minimum_required(VERSION 2.8)
 
 add_library(sllist STATIC lib/sllist/src/create.c lib/sllist/src/add.c lib/sllist/src/at.c lib/sllist/src/del.c lib/sllist/src/find.c)
-add_executable(sandbox src/main.c src/mem.c src/exec.c src/param.c src/sandbox.c src/environment.c src/pathutil.c
+add_executable(sandbox src/main.c src/mem.c src/apply.c src/apply_fs.c src/exec.c src/param.c src/sandbox.c src/environment.c src/pathutil.c
 	src/ovr_syscall/ovr_write.c src/ovr_syscall/ovr_open.c src/ovr_syscall/ovr_close.c)
 
 set_property(TARGET sandbox PROPERTY RUNTIME_OUTPUT_DIRECTORY bin)
@@ -18,7 +18,13 @@ add_executable(test_sllist test/sllist/main.c)
 set_property(TARGET test_sllist PROPERTY RUNTIME_OUTPUT_DIRECTORY test/bin)
 target_link_libraries(test_sllist sllist)
 
+add_executable(test_write test/write/main.c test/write/functions.c src/ovr_syscall/ovr_close.c src/ovr_syscall/ovr_open.c src/ovr_syscall/ovr_write.c src/pathutil.c)
+set_property(TARGET test_write PROPERTY RUNTIME_OUTPUT_DIRECTORY test/bin)
+target_link_libraries(test_write sllist)
+
+
 enable_testing()
 add_test(sllist test/bin/test_sllist)
+add_test(write test/bin/test_write)
 #add_subdirectory(test/open)
 #add_subdirectory(test/test)

+ 27 - 0
src/apply.c

@@ -0,0 +1,27 @@
+#include "sandbox.h"
+#include <stdlib.h>
+#include <string.h>
+
+void sandbox_apply(struct s_sandboxenv *env)
+{
+	apply_fs(env);
+}
+
+void prompt_sandbox(struct s_sandboxenv *env)
+{
+	char *c = NULL;
+	size_t size;
+
+	do
+	{
+		if (c)
+			free(c);
+		c = NULL;
+		printf("Apply (a) / Cancel (c) changes ?\n");
+		getline(&c, &size, stdin);
+	} while (strcmp("a\n", c) && strcmp("c\n", c));
+	if (*c == 'c')
+		return;
+	sandbox_apply(env);
+}
+

+ 18 - 0
src/apply_fs.c

@@ -0,0 +1,18 @@
+#include "sandbox.h"
+
+int apply_file(void **_data, void *_env)
+{
+	struct s_sandboxenv *env = * (struct s_sandboxenv **) _env;
+	t_fileinfo *fi = (t_fileinfo *) _data;
+
+#warning todo
+	(void) fi;
+	(void) env;
+	return 0;
+}
+
+void apply_fs(struct s_sandboxenv *env)
+{
+	sllist_foreach(env->filetable, apply_file, env);
+}
+

+ 2 - 1
src/environment.c

@@ -2,7 +2,6 @@
 #include <sys/stat.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdio.h>
 #include <errno.h>
 #include <time.h>
 
@@ -68,6 +67,8 @@ int init_env(t_param *params)
 		fprintf(stderr, "Cannot create directory %s: %s\n", params->tmppath, strerror(errno));
 		return -1;
 	}
+	if (params->verbose)
+		fprintf(params->verbose, "Created env at %s\n", params->tmppath);
 	return 0;
 }
 

+ 0 - 1
src/exec.c

@@ -1,5 +1,4 @@
 #include <string.h>
-#include <stdio.h>
 #include <errno.h>
 #include "sandbox.h"
 

+ 0 - 4
src/ovr_syscall/ovr_close.c

@@ -1,12 +1,8 @@
-
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include "sandbox.h"
 
-/* DEBUG HEADER */
-#include <stdio.h>
-
 int ovr_close(struct s_sandboxenv *env)
 {
 	int fd = (int) env->syscall_args[0];

+ 29 - 4
src/ovr_syscall/ovr_open.c

@@ -1,22 +1,42 @@
 #include "sandbox.h"
+#include "sllist.h"
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <stdlib.h>
+#include <string.h>
 #include <fcntl.h>
-#include <stdio.h>
+
+static t_fileinfo *open_local(struct s_sandboxenv *env, char *filename)
+{
+	t_fileinfo *result;
+	unsigned long long int filename_hash;
+	char *localpath;
+
+	filename_hash = hash_path(filename);
+	asprintf(&localpath, "%s/%llu", env->params->tmppath, filename_hash);
+	result = malloc(sizeof(*result));
+	result->filename = filename;
+	result->local_fd = open(localpath, O_CREAT, 0600);
+	result->fds = sllist_create();
+	sllist_pushback(env->filetable, result);
+	free(localpath);
+	return result;
+}
 
 int ovr_open(struct s_sandboxenv *env)
 {
 	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];
-	int ro = !(flags & (O_RDWR | O_WRONLY | O_CREAT | O_TRUNC));
+	int ro = !(flags & (O_RDWR | O_WRONLY | O_TRUNC));
 	t_fileinfo *fileentry;
 
 	if (*pathname != '/')
 		find_fullpath(&pathname, 1);
 	fileentry = get_fileinfo(env, pathname);
-	if (fileentry == NULL && ro)
+	if (!fileentry && ro && (flags & O_CREAT) && !file_exists(pathname))
+		ro = 0;
+	if (!fileentry && ro)
 	{
 		printf("DO open %s (unmanaged)\n", pathname);
 		fflush(stdout);
@@ -25,7 +45,12 @@ int ovr_open(struct s_sandboxenv *env)
 	}
 	waitForSyscall(env->child_pid, SANDBOX_SYS_EXIT);
 	read_registers(env);
-	printf("DO open ! ([%s], [%d], [%d])%s = %d\n", pathname, flags, mode, ro ? "- RO" : "- RW", env->syscall_no.syscall_return);
+	if (!fileentry)
+		fileentry = open_local(env, strdup(pathname));
+
+	sllist_pushback(fileentry->fds, (void *) env->syscall_no.syscall_return);
+
+	printf("DO open ! ([%s], [%d], [%d])%s = %d\n", pathname, flags, mode, ro ? "- RO" : "- RW", (int) env->syscall_no.syscall_return);
 	fflush(stdout);
 	free(pathname);
 	return 0;

+ 5 - 7
src/ovr_syscall/ovr_write.c

@@ -1,20 +1,18 @@
-
 #include <stdlib.h>
 #include "sandbox.h"
 
-/* DEBUG HEADER */
-#include <stdio.h>
-
 int ovr_write(struct s_sandboxenv *env)
 {
 	int fd = (int) env->syscall_args[0];
 	char *buf = getMem(env, (size_t) env->syscall_args[1], NULL);
 	int buflen = (int) env->syscall_args[2];
+	t_fileinfo *file = get_fileinfo_fd(env, fd);
 
-	/*
-	printf("DO WRITE ! ([%d], [%s], [%d])\n", fd, buf, buflen);
+	if (file)
+		printf("DO WRITE ! ([%s], [%s], [%d])\n", file->filename, buf, buflen);
+	else
+		printf("DO WRITE ! ([unmanaged], [%s], [%d])\n", buf, buflen);
 	fflush(stdout);
-	*/
 	free(buf);
 	//env->registers.orig_rax = -1;
 	return 0;

+ 3 - 3
src/param.c

@@ -1,4 +1,3 @@
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "sandbox.h"
@@ -12,11 +11,12 @@ void print_help(const char *progname, int exit_status)
 
 static int parse_param(t_param *result, const char **av)
 {
-	printf("Parsing -%s-\n", *av);
 	if (!strcmp(*av, "--"))
 		result->cmd = ++av;
-	else if (!strcmp(*av, "-tmp-dir"))
+	else if (!strcmp(*av, "--tmp-dir"))
 		result->tmpdir = *(++av);
+	else if (!strcmp(*av, "-v") || !(strcmp(*av, "--verbose")))
+		result->verbose = stderr;
 	return 1;
 }
 

+ 36 - 2
src/pathutil.c

@@ -1,8 +1,9 @@
 #include "sandbox.h"
+#include <sys/stat.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdio.h>
+#include <errno.h>
 
 void find_fullpath(char **path, int _free)
 {
@@ -22,7 +23,12 @@ int fileinfo_compare(const void *a, const void *b)
 	return strcmp(((t_fileinfo *)a)->filename, (char *)b) == 0;
 }
 
-t_fileinfo *get_fileinfo(struct s_sandboxenv *env, const char * filename)
+int fileinfo_compare_fd(const void *a, const void *fd)
+{
+	return sllist_find(((t_fileinfo *)a)->fds, sllist_cmp_int, fd) != -1;
+}
+
+t_fileinfo *get_fileinfo(const struct s_sandboxenv *env, const char * filename)
 {
 	int i;
 
@@ -32,3 +38,31 @@ t_fileinfo *get_fileinfo(struct s_sandboxenv *env, const char * filename)
 	return sllist_at(env->filetable, i);
 }
 
+t_fileinfo *get_fileinfo_fd(const struct s_sandboxenv *env, const int fd)
+{
+	int i;
+
+	i = sllist_find(env->filetable, fileinfo_compare_fd, &fd);
+	if (i == -1)
+		return NULL;
+	return sllist_at(env->filetable, i);
+}
+
+int file_exists(const char*path)
+{
+	struct stat buf;
+
+	if (stat(path, &buf) == -1 && errno == ENOENT)
+		return 0;
+	return 1;
+}
+
+unsigned long long int hash_path(const char *filename)
+{
+	unsigned long long int result;
+
+	for (result =0; *filename; ++filename)
+		result = (result << 5) - result + *filename;
+	return result;
+}
+

+ 1 - 4
src/sandbox.c

@@ -8,9 +8,6 @@
 #include <elf.h>
 #include "sandbox.h"
 
-/* DEBUG HEADER */
-#include <stdio.h>
-
 static inline void get_args(struct s_sandboxenv *env)
 {
 }
@@ -95,7 +92,7 @@ void doTrace(int pid, const t_param *params)
 			break;
 	}
 
-	/* TODO */
+	prompt_sandbox(&sandbox_env);
 	sllist_destroy(sandbox_env.filetable);
 }
 

+ 12 - 2
src/sandbox.h

@@ -6,6 +6,7 @@
 # include <sys/syscall.h>
 # include <sys/user.h>
 # include <unistd.h>
+# include <stdio.h>
 
 # include "sllist.h"
 
@@ -38,6 +39,7 @@ typedef struct {
 	const char *tmpdir;
 	char *cmdpath;
 	char *tmppath;
+	FILE *verbose;
 } t_param;
 
 struct s_sandboxenv;
@@ -48,7 +50,7 @@ struct s_sandboxenv {
 	t_syscall_fnc functions[NR_syscalls];
 	int child_pid;
 	struct user_regs_struct registers;
-	struct { unsigned int syscall_no; int syscall_return; } syscall_no; 
+	struct { REGISTER_TYPE syscall_no; REGISTER_TYPE syscall_return; } syscall_no; 
 	sl_list *filetable;
 	REGISTER_TYPE syscall_args[6];
 };
@@ -81,8 +83,16 @@ void release_env(t_param *);
 
 /* pathutil.c */
 void find_fullpath(char **path, int free);
-t_fileinfo *get_fileinfo(struct s_sandboxenv *env, const char *filename);
+t_fileinfo *get_fileinfo(const struct s_sandboxenv *env, const char *filename);
+t_fileinfo *get_fileinfo_fd(const struct s_sandboxenv *env, const int fd);
+unsigned long long int hash_path(const char *filename);
+int file_exists(const char *filename);
+
+/* applay.c */
+void prompt_sandbox(struct s_sandboxenv *env);
+void apply_fs(struct s_sandboxenv *env);
 
 # include "sandbox_syscall.h"
 
 #endif /* SANDBOX_H__ */
+

+ 27 - 0
test/write/functions.c

@@ -0,0 +1,27 @@
+#include "sandbox.h"
+#include <string.h>
+
+void *getMem(const struct s_sandboxenv *env, size_t ptr, int *readlen)
+{
+	(void) env;
+	char *result;
+
+	result = strdup((char *) ptr);
+	if (readlen)
+		*readlen = strlen(result);
+	return result;
+}
+
+int waitForSyscall(const int pid, int status)
+{
+	(void) pid;
+	(void) status;
+
+	return 0;
+}
+
+void read_registers(struct s_sandboxenv *env)
+{
+	(void) env;
+}
+

+ 18 - 0
test/write/main.c

@@ -0,0 +1,18 @@
+#include "common.h"
+#include "sandbox.h"
+
+/*
+	_assertNotNull(a);
+	_assertEqual(sllist_count(a), 0);
+*/
+
+int main()
+{
+	int success = 1;
+	/*
+	success &= !test_create();
+	success &= !test_add();
+	*/
+	exit(success ? EXIT_SUCCESS: EXIT_FAILURE);
+}
+