sys_arm.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * linux/arch/arm/kernel/sys_arm.c
  3. *
  4. * Copyright (C) People who wrote linux/arch/i386/kernel/sys_i386.c
  5. * Copyright (C) 1995, 1996 Russell King.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This file contains various random system calls that
  12. * have a non-standard calling sequence on the Linux/arm
  13. * platform.
  14. */
  15. #include <linux/export.h>
  16. #include <linux/errno.h>
  17. #include <linux/sched.h>
  18. #include <linux/mm.h>
  19. #include <linux/sem.h>
  20. #include <linux/msg.h>
  21. #include <linux/shm.h>
  22. #include <linux/stat.h>
  23. #include <linux/syscalls.h>
  24. #include <linux/mman.h>
  25. #include <linux/fs.h>
  26. #include <linux/file.h>
  27. #include <linux/ipc.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/slab.h>
  30. /* Fork a new task - this creates a new program thread.
  31. * This is called indirectly via a small wrapper
  32. */
  33. asmlinkage int sys_fork(struct pt_regs *regs)
  34. {
  35. #ifdef CONFIG_MMU
  36. return do_fork(SIGCHLD, regs->ARM_sp, regs, 0, NULL, NULL);
  37. #else
  38. /* can not support in nommu mode */
  39. return(-EINVAL);
  40. #endif
  41. }
  42. /* Clone a task - this clones the calling program thread.
  43. * This is called indirectly via a small wrapper
  44. */
  45. asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
  46. int __user *parent_tidptr, int tls_val,
  47. int __user *child_tidptr, struct pt_regs *regs)
  48. {
  49. if (!newsp)
  50. newsp = regs->ARM_sp;
  51. return do_fork(clone_flags, newsp, regs, 0, parent_tidptr, child_tidptr);
  52. }
  53. asmlinkage int sys_vfork(struct pt_regs *regs)
  54. {
  55. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->ARM_sp, regs, 0, NULL, NULL);
  56. }
  57. #if defined CONFIG_SEC_RESTRICT_FORK
  58. #if defined CONFIG_SEC_RESTRICT_ROOTING_LOG
  59. #define PRINT_LOG(...) printk(KERN_ERR __VA_ARGS__)
  60. #else
  61. #define PRINT_LOG(...)
  62. #endif // End of CONFIG_SEC_RESTRICT_ROOTING_LOG
  63. #define CHECK_ROOT_UID(x) (x->cred->uid == 0 || x->cred->gid == 0 || \
  64. x->cred->euid == 0 || x->cred->egid == 0 || \
  65. x->cred->suid == 0 || x->cred->sgid == 0)
  66. /* sec_check_execpath
  67. return value : give task's exec path is matched or not
  68. */
  69. int sec_check_execpath(struct mm_struct *mm, char *denypath)
  70. {
  71. struct file *exe_file;
  72. char *path, *pathbuf = NULL;
  73. unsigned int path_length = 0, denypath_length = 0;
  74. int ret = 0;
  75. if (mm == NULL)
  76. return 0;
  77. if (!(exe_file = get_mm_exe_file(mm))) {
  78. PRINT_LOG("Cannot get exe from task->mm.\n");
  79. goto out_nofile;
  80. }
  81. if (!(pathbuf = kmalloc(PATH_MAX, GFP_TEMPORARY))) {
  82. PRINT_LOG("failed to kmalloc for pathbuf\n");
  83. goto out;
  84. }
  85. path = d_path(&exe_file->f_path, pathbuf, PATH_MAX);
  86. if (IS_ERR(path)) {
  87. PRINT_LOG("Error get path..\n");
  88. goto out;
  89. }
  90. path_length = strlen(path);
  91. denypath_length = strlen(denypath);
  92. if (!strncmp(path, denypath, (path_length < denypath_length) ?
  93. path_length : denypath_length)) {
  94. ret = 1;
  95. }
  96. out:
  97. fput(exe_file);
  98. out_nofile:
  99. if (pathbuf)
  100. kfree(pathbuf);
  101. return ret;
  102. }
  103. EXPORT_SYMBOL(sec_check_execpath);
  104. static int sec_restrict_fork(void)
  105. {
  106. struct cred *shellcred;
  107. int ret = 0;
  108. struct task_struct *parent_tsk;
  109. struct mm_struct *parent_mm = NULL;
  110. const struct cred *parent_cred;
  111. read_lock(&tasklist_lock);
  112. parent_tsk = current->parent;
  113. if (!parent_tsk) {
  114. read_unlock(&tasklist_lock);
  115. return 0;
  116. }
  117. get_task_struct(parent_tsk);
  118. /* holding on to the task struct is enough so just release
  119. * the tasklist lock here */
  120. read_unlock(&tasklist_lock);
  121. if (current->pid == 1 || parent_tsk->pid == 1)
  122. goto out;
  123. /* get current->parent's mm struct to access it's mm
  124. * and to keep it alive */
  125. parent_mm = get_task_mm(parent_tsk);
  126. if (current->mm == NULL || parent_mm == NULL)
  127. goto out;
  128. if (sec_check_execpath(parent_mm, "/sbin/adbd")) {
  129. shellcred = prepare_creds();
  130. if (!shellcred) {
  131. ret = 1;
  132. goto out;
  133. }
  134. shellcred->uid = 2000;
  135. shellcred->gid = 2000;
  136. shellcred->euid = 2000;
  137. shellcred->egid = 2000;
  138. commit_creds(shellcred);
  139. ret = 0;
  140. goto out;
  141. }
  142. if (sec_check_execpath(current->mm, "/data/")) {
  143. ret = 1;
  144. goto out;
  145. }
  146. parent_cred = get_task_cred(parent_tsk);
  147. if (!parent_cred)
  148. goto out;
  149. if (!CHECK_ROOT_UID(parent_tsk))
  150. {
  151. if(!sec_check_execpath(current->mm, "/system/bin/logwrapper"))
  152. ret = 1;
  153. }
  154. put_cred(parent_cred);
  155. out:
  156. if (parent_mm)
  157. mmput(parent_mm);
  158. put_task_struct(parent_tsk);
  159. return ret;
  160. }
  161. #endif /* End of CONFIG_SEC_RESTRICT_FORK */
  162. /* sys_execve() executes a new program.
  163. * This is called indirectly via a small wrapper
  164. */
  165. asmlinkage int sys_execve(const char __user *filenamei,
  166. const char __user *const __user *argv,
  167. const char __user *const __user *envp, struct pt_regs *regs)
  168. {
  169. int error;
  170. char * filename;
  171. filename = getname(filenamei);
  172. error = PTR_ERR(filename);
  173. if (IS_ERR(filename))
  174. goto out;
  175. #if defined CONFIG_SEC_RESTRICT_FORK
  176. if(CHECK_ROOT_UID(current))
  177. if(sec_restrict_fork())
  178. {
  179. PRINT_LOG("Restricted making process. PID = %d(%s) "
  180. "PPID = %d(%s)\n",
  181. current->pid, current->comm,
  182. current->parent->pid, current->parent->comm);
  183. return -EACCES;
  184. }
  185. #endif // End of CONFIG_SEC_RESTRICT_FORK
  186. error = do_execve(filename, argv, envp, regs);
  187. putname(filename);
  188. out:
  189. return error;
  190. }
  191. int kernel_execve(const char *filename,
  192. const char *const argv[],
  193. const char *const envp[])
  194. {
  195. struct pt_regs regs;
  196. int ret;
  197. memset(&regs, 0, sizeof(struct pt_regs));
  198. ret = do_execve(filename,
  199. (const char __user *const __user *)argv,
  200. (const char __user *const __user *)envp, &regs);
  201. if (ret < 0)
  202. goto out;
  203. /*
  204. * Save argc to the register structure for userspace.
  205. */
  206. regs.ARM_r0 = ret;
  207. /*
  208. * We were successful. We won't be returning to our caller, but
  209. * instead to user space by manipulating the kernel stack.
  210. */
  211. asm( "add r0, %0, %1\n\t"
  212. "mov r1, %2\n\t"
  213. "mov r2, %3\n\t"
  214. "bl memmove\n\t" /* copy regs to top of stack */
  215. "mov r8, #0\n\t" /* not a syscall */
  216. "mov r9, %0\n\t" /* thread structure */
  217. "mov sp, r0\n\t" /* reposition stack pointer */
  218. "b ret_to_user"
  219. :
  220. : "r" (current_thread_info()),
  221. "Ir" (THREAD_START_SP - sizeof(regs)),
  222. "r" (&regs),
  223. "Ir" (sizeof(regs))
  224. : "r0", "r1", "r2", "r3", "r8", "r9", "ip", "lr", "memory");
  225. out:
  226. return ret;
  227. }
  228. EXPORT_SYMBOL(kernel_execve);
  229. /*
  230. * Since loff_t is a 64 bit type we avoid a lot of ABI hassle
  231. * with a different argument ordering.
  232. */
  233. asmlinkage long sys_arm_fadvise64_64(int fd, int advice,
  234. loff_t offset, loff_t len)
  235. {
  236. return sys_fadvise64_64(fd, offset, len, advice);
  237. }