main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "common.h"
  2. #include "sandbox.h"
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. /*
  6. _assertNotNull(a);
  7. _assertEqual(sllist_count(a), 0);
  8. */
  9. int fakeopen(const char *path, int flags, int mod, struct s_sandboxenv *env)
  10. {
  11. int fd;
  12. #ifdef __x86_64__
  13. env->syscall_no.syscall_no = env->registers.orig_rax = (REGISTER_TYPE) __NR_open;
  14. env->syscall_no.syscall_return = fd = open(path, flags, mod);
  15. env->syscall_args[0] = env->registers.rdi = (REGISTER_TYPE) path;
  16. env->syscall_args[1] = env->registers.rsi = flags;
  17. env->syscall_args[2] = env->registers.rdx = mod;
  18. #else
  19. # error "non-x86 unsupported"
  20. #endif
  21. ovr_open(env);
  22. return fd;
  23. }
  24. int fakeclose(int fd, struct s_sandboxenv *env)
  25. {
  26. #ifdef __x86_64__
  27. env->syscall_no.syscall_no = env->registers.orig_rax = (REGISTER_TYPE) __NR_close;
  28. env->syscall_args[0] = env->registers.rdi = fd;
  29. #else
  30. # error "non-x86 unsupported"
  31. #endif
  32. ovr_close(env);
  33. return fd;
  34. }
  35. int fakewrite(int fd, char *buf, int len, struct s_sandboxenv *env)
  36. {
  37. #ifdef __x86_64__
  38. env->syscall_no.syscall_no = env->registers.orig_rax = (REGISTER_TYPE) __NR_write;
  39. env->syscall_args[0] = env->registers.rdi = fd;
  40. env->syscall_args[1] = env->registers.rsi = (REGISTER_TYPE) buf;
  41. env->syscall_args[2] = env->registers.rdx = len;
  42. #else
  43. # error "non-x86 unsupported"
  44. #endif
  45. ovr_write(env);
  46. if (env->registers.orig_rax == __NR_write)
  47. env->registers.rax = write(fd, buf, len);
  48. return env->registers.rax;
  49. }
  50. int test_open(struct s_sandboxenv *env, int *final_fd)
  51. {
  52. t_fileinfo *fi;
  53. t_fd *fd_struct;
  54. close(open("_test", O_CREAT, 0644));
  55. _assertEqual(sllist_count(env->filetable), 0);
  56. int fd = fakeopen("_test", O_RDONLY, 0, env);
  57. _assertDiff(fd, -1);
  58. _assertEqual(env->syscall_no.syscall_return, fd);
  59. _assertEqual(sllist_count(env->filetable), 0);
  60. close(fd);
  61. fd = fakeopen("_test", O_WRONLY, 0646, env);
  62. _assertDiff(fd, -1);
  63. _assertEqual(env->syscall_no.syscall_return, fd);
  64. _assertEqual(sllist_count(env->filetable), 1);
  65. fi = (t_fileinfo *)sllist_at(env->filetable, 0);
  66. _assertEqual(sllist_count(fi->fds), 1);
  67. _assertTrue(fi->local_fd > 0);
  68. int fd2 = fakeopen("_test", O_RDONLY, 0, env);
  69. _assertTrue(fd2 > 0);
  70. _assertEqual(sllist_count(env->filetable), 1);
  71. _assertEqual(sllist_count(fi->fds), 2);
  72. fakeclose(fd2, env);
  73. _assertEqual(sllist_count(fi->fds), 1);
  74. fd_struct = sllist_at(fi->fds, 0);
  75. _assertNotNull(fd_struct);
  76. _assertEqual(fd_struct->fd, fd);
  77. _assertEqual(fd_struct->flags, O_WRONLY);
  78. _assertEqual(fd_struct->mod, 0646);
  79. *final_fd = fd;
  80. return 0;
  81. }
  82. int test_write(struct s_sandboxenv *env, int fd)
  83. {
  84. _assertEqual(fakewrite(fd, "test", 5, env), 5);
  85. #warning TODO
  86. return 0;
  87. }
  88. int main()
  89. {
  90. int success = 1;
  91. struct s_sandboxenv env;
  92. int fd;
  93. t_param params;
  94. tests_init_env(&env, &params);
  95. success &= !test_open(&env, &fd);
  96. success &= !test_write(&env, fd);
  97. fakeclose(fd, &env);
  98. tests_release_env(&env, &params);
  99. unlink("_test");
  100. exit(success ? EXIT_SUCCESS: EXIT_FAILURE);
  101. }