1
0

access.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Loco program runner core
  3. * Copyright (C) 2011 Lodevil(Du Jiong)
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "access.h"
  19. #include <sys/syscall.h>
  20. #include <sys/ptrace.h>
  21. #include <sys/user.h>
  22. #include <fcntl.h>
  23. #include <string.h>
  24. int fileAccess(PyObject *files, const char *file, long flags) {
  25. PyObject *perm_obj;
  26. long perm;
  27. if ((perm_obj = PyDict_GetItemString(files, file)) == NULL) {
  28. return 0;
  29. }
  30. //printf("%s:%d\n",file,flags);
  31. perm = PyLong_AsLong(perm_obj);
  32. if (perm == flags)
  33. return 1;
  34. return 0;
  35. }
  36. static long file_temp[100];
  37. int checkAccess(struct Runobj *runobj, int pid, struct user_regs_struct *regs) {
  38. #if __WORDSIZE == 64
  39. if (!runobj->inttable[regs->orig_rax])
  40. #else
  41. if (!runobj->inttable[regs->orig_eax])
  42. #endif
  43. return ACCESS_CALL_ERR;
  44. #if __WORDSIZE == 64
  45. switch (regs->orig_rax)
  46. #else
  47. switch (regs->orig_eax)
  48. #endif
  49. {
  50. case SYS_open: {
  51. int i, j;
  52. for (i = 0; i < 100; i++) {
  53. #if __WORDSIZE == 64
  54. long t = ptrace(PTRACE_PEEKDATA, pid,
  55. regs->rdi + i * sizeof(long), NULL);
  56. #else
  57. long t = ptrace(PTRACE_PEEKDATA, pid, regs->ebx + i*sizeof(long), NULL);
  58. #endif
  59. file_temp[i] = t;
  60. const char* test = (const char*) &file_temp[i];
  61. for (j = 0; j < sizeof(long); j++) {
  62. if (!test[j]) {
  63. goto l_cont;
  64. }
  65. }
  66. }
  67. l_cont: file_temp[99] = 0;
  68. #if __WORDSIZE == 64
  69. if (fileAccess(runobj->files, (const char *) file_temp, regs->rsi))
  70. #else
  71. if(fileAccess(runobj->files, (const char *)file_temp, regs->ecx))
  72. #endif
  73. return ACCESS_OK;
  74. return ACCESS_FILE_ERR;
  75. }
  76. }
  77. return ACCESS_OK;
  78. }
  79. const char* lastFileAccess(void) {
  80. file_temp[99] = 0;
  81. return (const char*) file_temp;
  82. }