|
@@ -1,114 +1,152 @@
|
|
|
#include "common.h"
|
|
#include "common.h"
|
|
|
#include "sandbox.h"
|
|
#include "sandbox.h"
|
|
|
#include <sys/stat.h>
|
|
#include <sys/stat.h>
|
|
|
|
|
+#include <string.h>
|
|
|
#include <fcntl.h>
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
/*
|
|
/*
|
|
|
- _assertNotNull(a);
|
|
|
|
|
- _assertEqual(sllist_count(a), 0);
|
|
|
|
|
|
|
+ _assertNotNull(a);
|
|
|
|
|
+ _assertEqual(sllist_count(a), 0);
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
|
|
+int fakeread(int fd, char *buf, int buflen, struct s_sandboxenv *env)
|
|
|
|
|
+{
|
|
|
|
|
+#ifdef __x86_64__
|
|
|
|
|
+ env->syscall_no.syscall_no = env->registers.orig_rax = (REGISTER_TYPE) __NR_read;
|
|
|
|
|
+ env->syscall_args[0] = env->registers.rdi = (REGISTER_TYPE) fd;
|
|
|
|
|
+ env->syscall_args[1] = env->registers.rsi = (REGISTER_TYPE) buf;
|
|
|
|
|
+ env->syscall_args[2] = env->registers.rdx = (REGISTER_TYPE) buflen;
|
|
|
|
|
+#else
|
|
|
|
|
+# error "non-x86 unsupported"
|
|
|
|
|
+#endif
|
|
|
|
|
+ ovr_read(env);
|
|
|
|
|
+ return fd;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+int fakelseek(int fd, off_t offset, int whence, struct s_sandboxenv *env)
|
|
|
|
|
+{
|
|
|
|
|
+#ifdef __x86_64__
|
|
|
|
|
+ env->syscall_no.syscall_no = env->registers.orig_rax = (REGISTER_TYPE) __NR_lseek;
|
|
|
|
|
+ env->syscall_args[0] = env->registers.rdi = (REGISTER_TYPE) fd;
|
|
|
|
|
+ env->syscall_args[1] = env->registers.rsi = (REGISTER_TYPE) offset;
|
|
|
|
|
+ env->syscall_args[2] = env->registers.rdx = (REGISTER_TYPE) whence;
|
|
|
|
|
+#else
|
|
|
|
|
+# error "non-x86 unsupported"
|
|
|
|
|
+#endif
|
|
|
|
|
+ ovr_lseek(env);
|
|
|
|
|
+ return fd;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
int fakeopen(const char *path, int flags, int mod, struct s_sandboxenv *env)
|
|
int fakeopen(const char *path, int flags, int mod, struct s_sandboxenv *env)
|
|
|
{
|
|
{
|
|
|
- int fd;
|
|
|
|
|
|
|
+ int fd;
|
|
|
#ifdef __x86_64__
|
|
#ifdef __x86_64__
|
|
|
- env->syscall_no.syscall_no = env->registers.orig_rax = (REGISTER_TYPE) __NR_open;
|
|
|
|
|
- env->syscall_no.syscall_return = fd = open(path, flags, mod);
|
|
|
|
|
- env->syscall_args[0] = env->registers.rdi = (REGISTER_TYPE) path;
|
|
|
|
|
- env->syscall_args[1] = env->registers.rsi = flags;
|
|
|
|
|
- env->syscall_args[2] = env->registers.rdx = mod;
|
|
|
|
|
|
|
+ env->syscall_no.syscall_no = env->registers.orig_rax = (REGISTER_TYPE) __NR_open;
|
|
|
|
|
+ env->syscall_no.syscall_return = fd = open(path, flags, mod);
|
|
|
|
|
+ env->syscall_args[0] = env->registers.rdi = (REGISTER_TYPE) path;
|
|
|
|
|
+ env->syscall_args[1] = env->registers.rsi = flags;
|
|
|
|
|
+ env->syscall_args[2] = env->registers.rdx = mod;
|
|
|
#else
|
|
#else
|
|
|
# error "non-x86 unsupported"
|
|
# error "non-x86 unsupported"
|
|
|
#endif
|
|
#endif
|
|
|
- ovr_open(env);
|
|
|
|
|
- return fd;
|
|
|
|
|
|
|
+ ovr_open(env);
|
|
|
|
|
+ return fd;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int fakeclose(int fd, struct s_sandboxenv *env)
|
|
int fakeclose(int fd, struct s_sandboxenv *env)
|
|
|
{
|
|
{
|
|
|
#ifdef __x86_64__
|
|
#ifdef __x86_64__
|
|
|
- env->syscall_no.syscall_no = env->registers.orig_rax = (REGISTER_TYPE) __NR_close;
|
|
|
|
|
- env->syscall_args[0] = env->registers.rdi = fd;
|
|
|
|
|
|
|
+ env->syscall_no.syscall_no = env->registers.orig_rax = (REGISTER_TYPE) __NR_close;
|
|
|
|
|
+ env->syscall_args[0] = env->registers.rdi = fd;
|
|
|
#else
|
|
#else
|
|
|
# error "non-x86 unsupported"
|
|
# error "non-x86 unsupported"
|
|
|
#endif
|
|
#endif
|
|
|
- ovr_close(env);
|
|
|
|
|
- return fd;
|
|
|
|
|
|
|
+ ovr_close(env);
|
|
|
|
|
+ return fd;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int fakewrite(int fd, char *buf, int len, struct s_sandboxenv *env)
|
|
int fakewrite(int fd, char *buf, int len, struct s_sandboxenv *env)
|
|
|
{
|
|
{
|
|
|
#ifdef __x86_64__
|
|
#ifdef __x86_64__
|
|
|
- env->syscall_no.syscall_no = env->registers.orig_rax = (REGISTER_TYPE) __NR_write;
|
|
|
|
|
- env->syscall_args[0] = env->registers.rdi = fd;
|
|
|
|
|
- env->syscall_args[1] = env->registers.rsi = (REGISTER_TYPE) buf;
|
|
|
|
|
- env->syscall_args[2] = env->registers.rdx = len;
|
|
|
|
|
|
|
+ env->syscall_no.syscall_no = env->registers.orig_rax = (REGISTER_TYPE) __NR_write;
|
|
|
|
|
+ env->syscall_args[0] = env->registers.rdi = fd;
|
|
|
|
|
+ env->syscall_args[1] = env->registers.rsi = (REGISTER_TYPE) buf;
|
|
|
|
|
+ env->syscall_args[2] = env->registers.rdx = len;
|
|
|
#else
|
|
#else
|
|
|
# error "non-x86 unsupported"
|
|
# error "non-x86 unsupported"
|
|
|
#endif
|
|
#endif
|
|
|
- ovr_write(env);
|
|
|
|
|
- if (env->registers.orig_rax == __NR_write)
|
|
|
|
|
- env->registers.rax = write(fd, buf, len);
|
|
|
|
|
- return env->registers.rax;
|
|
|
|
|
|
|
+ ovr_write(env);
|
|
|
|
|
+ if (env->registers.orig_rax == __NR_write)
|
|
|
|
|
+ env->registers.rax = write(fd, buf, len);
|
|
|
|
|
+ return env->registers.rax;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
int test_open(struct s_sandboxenv *env, int *final_fd)
|
|
int test_open(struct s_sandboxenv *env, int *final_fd)
|
|
|
{
|
|
{
|
|
|
- t_fileinfo *fi;
|
|
|
|
|
- t_fd *fd_struct;
|
|
|
|
|
-
|
|
|
|
|
- close(open("_test", O_CREAT, 0644));
|
|
|
|
|
- _assertEqual(sllist_count(env->filetable), 0);
|
|
|
|
|
- int fd = fakeopen("_test", O_RDONLY, 0, env);
|
|
|
|
|
- _assertDiff(fd, -1);
|
|
|
|
|
- _assertEqual(env->syscall_no.syscall_return, fd);
|
|
|
|
|
- _assertEqual(sllist_count(env->filetable), 0);
|
|
|
|
|
- close(fd);
|
|
|
|
|
- fd = fakeopen("_test", O_WRONLY, 0646, env);
|
|
|
|
|
- _assertDiff(fd, -1);
|
|
|
|
|
- _assertEqual(env->syscall_no.syscall_return, fd);
|
|
|
|
|
- _assertEqual(sllist_count(env->filetable), 1);
|
|
|
|
|
- fi = (t_fileinfo *)sllist_at(env->filetable, 0);
|
|
|
|
|
- _assertEqual(sllist_count(fi->fds), 1);
|
|
|
|
|
- _assertTrue(fi->local_fd > 0);
|
|
|
|
|
- int fd2 = fakeopen("_test", O_RDONLY, 0, env);
|
|
|
|
|
- _assertTrue(fd2 > 0);
|
|
|
|
|
- _assertEqual(sllist_count(env->filetable), 1);
|
|
|
|
|
- _assertEqual(sllist_count(fi->fds), 2);
|
|
|
|
|
- fakeclose(fd2, env);
|
|
|
|
|
- _assertEqual(sllist_count(fi->fds), 1);
|
|
|
|
|
- fd_struct = sllist_at(fi->fds, 0);
|
|
|
|
|
- _assertNotNull(fd_struct);
|
|
|
|
|
- _assertEqual(fd_struct->fd, fd);
|
|
|
|
|
- _assertEqual(fd_struct->flags, O_WRONLY);
|
|
|
|
|
- _assertEqual(fd_struct->mod, 0646);
|
|
|
|
|
- *final_fd = fd;
|
|
|
|
|
- return 0;
|
|
|
|
|
|
|
+ t_fileinfo *fi;
|
|
|
|
|
+ t_fd *fd_struct;
|
|
|
|
|
+
|
|
|
|
|
+ close(open("_test", O_CREAT, 0644));
|
|
|
|
|
+ _assertEqual(sllist_count(env->filetable), 0);
|
|
|
|
|
+ int fd = fakeopen("_test", O_RDONLY, 0, env);
|
|
|
|
|
+ _assertDiff(fd, -1);
|
|
|
|
|
+ _assertEqual(env->syscall_no.syscall_return, fd);
|
|
|
|
|
+ _assertEqual(sllist_count(env->filetable), 0);
|
|
|
|
|
+ close(fd);
|
|
|
|
|
+ fd = fakeopen("_test", O_WRONLY, 0646, env);
|
|
|
|
|
+ _assertDiff(fd, -1);
|
|
|
|
|
+ _assertEqual(env->syscall_no.syscall_return, fd);
|
|
|
|
|
+ _assertEqual(sllist_count(env->filetable), 1);
|
|
|
|
|
+ fi = (t_fileinfo *)sllist_at(env->filetable, 0);
|
|
|
|
|
+ _assertEqual(sllist_count(fi->fds), 1);
|
|
|
|
|
+ _assertTrue(fi->local_fd > 0);
|
|
|
|
|
+ int fd2 = fakeopen("_test", O_RDONLY, 0, env);
|
|
|
|
|
+ _assertTrue(fd2 > 0);
|
|
|
|
|
+ _assertEqual(sllist_count(env->filetable), 1);
|
|
|
|
|
+ _assertEqual(sllist_count(fi->fds), 2);
|
|
|
|
|
+ fakeclose(fd2, env);
|
|
|
|
|
+ _assertEqual(sllist_count(fi->fds), 1);
|
|
|
|
|
+ fd_struct = sllist_at(fi->fds, 0);
|
|
|
|
|
+ _assertNotNull(fd_struct);
|
|
|
|
|
+ _assertEqual(fd_struct->fd, fd);
|
|
|
|
|
+ _assertEqual(fd_struct->flags, O_WRONLY);
|
|
|
|
|
+ _assertEqual(fd_struct->mod, 0646);
|
|
|
|
|
+ *final_fd = fd;
|
|
|
|
|
+ return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int test_write(struct s_sandboxenv *env, int fd)
|
|
int test_write(struct s_sandboxenv *env, int fd)
|
|
|
{
|
|
{
|
|
|
- _assertEqual(fakewrite(fd, "test", 5, env), 5);
|
|
|
|
|
|
|
+ struct stat st;
|
|
|
|
|
+ char buf[4];
|
|
|
|
|
+
|
|
|
|
|
+ _assertEqual(fakewrite(fd, "test", 4, env), 4);
|
|
|
|
|
+ _assertEqual(stat("_test", &st), 0);
|
|
|
|
|
+ _assertEqual(st.st_size, 0);
|
|
|
#warning TODO
|
|
#warning TODO
|
|
|
- return 0;
|
|
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ _assertEqual(fakelseek(fd, 0, SEEK_SET, env), 0);
|
|
|
|
|
+ _assertEqual(fakeread(fd, buf, 4, env), 4);
|
|
|
|
|
+ _assertEqual(strcmp(buf, "test"), 0);
|
|
|
|
|
+ return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
int main()
|
|
|
{
|
|
{
|
|
|
- int success = 1;
|
|
|
|
|
- struct s_sandboxenv env;
|
|
|
|
|
- int fd;
|
|
|
|
|
- t_param params;
|
|
|
|
|
|
|
+ int success = 1;
|
|
|
|
|
+ struct s_sandboxenv env;
|
|
|
|
|
+ int fd;
|
|
|
|
|
+ t_param params;
|
|
|
|
|
|
|
|
- tests_init_env(&env, ¶ms);
|
|
|
|
|
|
|
+ tests_init_env(&env, ¶ms);
|
|
|
|
|
|
|
|
- success &= !test_open(&env, &fd);
|
|
|
|
|
- success &= !test_write(&env, fd);
|
|
|
|
|
|
|
+ success &= !test_open(&env, &fd);
|
|
|
|
|
+ success &= !test_write(&env, fd);
|
|
|
|
|
|
|
|
- fakeclose(fd, &env);
|
|
|
|
|
- tests_release_env(&env, ¶ms);
|
|
|
|
|
- unlink("_test");
|
|
|
|
|
- exit(success ? EXIT_SUCCESS: EXIT_FAILURE);
|
|
|
|
|
|
|
+ fakeclose(fd, &env);
|
|
|
|
|
+ tests_release_env(&env, ¶ms);
|
|
|
|
|
+ unlink("_test");
|
|
|
|
|
+ exit(success ? EXIT_SUCCESS: EXIT_FAILURE);
|
|
|
}
|
|
}
|
|
|
|
|
|