kcmp.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include <linux/kernel.h>
  2. #include <linux/syscalls.h>
  3. #include <linux/fdtable.h>
  4. #include <linux/string.h>
  5. #include <linux/random.h>
  6. #include <linux/module.h>
  7. #include <linux/ptrace.h>
  8. #include <linux/init.h>
  9. #include <linux/errno.h>
  10. #include <linux/cache.h>
  11. #include <linux/bug.h>
  12. #include <linux/err.h>
  13. #include <linux/kcmp.h>
  14. #include <linux/capability.h>
  15. #include <linux/list.h>
  16. #include <linux/eventpoll.h>
  17. #include <linux/file.h>
  18. #include <asm/unistd.h>
  19. /*
  20. * We don't expose the real in-memory order of objects for security reasons.
  21. * But still the comparison results should be suitable for sorting. So we
  22. * obfuscate kernel pointers values and compare the production instead.
  23. *
  24. * The obfuscation is done in two steps. First we xor the kernel pointer with
  25. * a random value, which puts pointer into a new position in a reordered space.
  26. * Secondly we multiply the xor production with a large odd random number to
  27. * permute its bits even more (the odd multiplier guarantees that the product
  28. * is unique ever after the high bits are truncated, since any odd number is
  29. * relative prime to 2^n).
  30. *
  31. * Note also that the obfuscation itself is invisible to userspace and if needed
  32. * it can be changed to an alternate scheme.
  33. */
  34. static unsigned long cookies[KCMP_TYPES][2] __read_mostly;
  35. static long kptr_obfuscate(long v, int type)
  36. {
  37. return (v ^ cookies[type][0]) * cookies[type][1];
  38. }
  39. /*
  40. * 0 - equal, i.e. v1 = v2
  41. * 1 - less than, i.e. v1 < v2
  42. * 2 - greater than, i.e. v1 > v2
  43. * 3 - not equal but ordering unavailable (reserved for future)
  44. */
  45. static int kcmp_ptr(void *v1, void *v2, enum kcmp_type type)
  46. {
  47. long t1, t2;
  48. t1 = kptr_obfuscate((long)v1, type);
  49. t2 = kptr_obfuscate((long)v2, type);
  50. return (t1 < t2) | ((t1 > t2) << 1);
  51. }
  52. /* The caller must have pinned the task */
  53. static struct file *
  54. get_file_raw_ptr(struct task_struct *task, unsigned int idx)
  55. {
  56. struct file *file = NULL;
  57. task_lock(task);
  58. rcu_read_lock();
  59. if (task->files)
  60. file = fcheck_files(task->files, idx);
  61. rcu_read_unlock();
  62. task_unlock(task);
  63. return file;
  64. }
  65. static void kcmp_unlock(struct mutex *m1, struct mutex *m2)
  66. {
  67. if (likely(m2 != m1))
  68. mutex_unlock(m2);
  69. mutex_unlock(m1);
  70. }
  71. static int kcmp_lock(struct mutex *m1, struct mutex *m2)
  72. {
  73. int err;
  74. if (m2 > m1)
  75. swap(m1, m2);
  76. err = mutex_lock_killable(m1);
  77. if (!err && likely(m1 != m2)) {
  78. err = mutex_lock_killable_nested(m2, SINGLE_DEPTH_NESTING);
  79. if (err)
  80. mutex_unlock(m1);
  81. }
  82. return err;
  83. }
  84. #ifdef CONFIG_EPOLL
  85. static int kcmp_epoll_target(struct task_struct *task1,
  86. struct task_struct *task2,
  87. unsigned long idx1,
  88. struct kcmp_epoll_slot __user *uslot)
  89. {
  90. struct file *filp, *filp_epoll, *filp_tgt;
  91. struct kcmp_epoll_slot slot;
  92. struct files_struct *files;
  93. if (copy_from_user(&slot, uslot, sizeof(slot)))
  94. return -EFAULT;
  95. filp = get_file_raw_ptr(task1, idx1);
  96. if (!filp)
  97. return -EBADF;
  98. files = get_files_struct(task2);
  99. if (!files)
  100. return -EBADF;
  101. spin_lock(&files->file_lock);
  102. filp_epoll = fcheck_files(files, slot.efd);
  103. if (filp_epoll)
  104. get_file(filp_epoll);
  105. else
  106. filp_tgt = ERR_PTR(-EBADF);
  107. spin_unlock(&files->file_lock);
  108. put_files_struct(files);
  109. if (filp_epoll) {
  110. filp_tgt = get_epoll_tfile_raw_ptr(filp_epoll, slot.tfd, slot.toff);
  111. fput(filp_epoll);
  112. } else
  113. if (IS_ERR(filp_tgt))
  114. return PTR_ERR(filp_tgt);
  115. return kcmp_ptr(filp, filp_tgt, KCMP_FILE);
  116. }
  117. #else
  118. static int kcmp_epoll_target(struct task_struct *task1,
  119. struct task_struct *task2,
  120. unsigned long idx1,
  121. struct kcmp_epoll_slot __user *uslot)
  122. {
  123. return -EOPNOTSUPP;
  124. }
  125. #endif
  126. SYSCALL_DEFINE5(kcmp, pid_t, pid1, pid_t, pid2, int, type,
  127. unsigned long, idx1, unsigned long, idx2)
  128. {
  129. struct task_struct *task1, *task2;
  130. int ret;
  131. rcu_read_lock();
  132. /*
  133. * Tasks are looked up in caller's PID namespace only.
  134. */
  135. task1 = find_task_by_vpid(pid1);
  136. task2 = find_task_by_vpid(pid2);
  137. if (!task1 || !task2)
  138. goto err_no_task;
  139. get_task_struct(task1);
  140. get_task_struct(task2);
  141. rcu_read_unlock();
  142. /*
  143. * One should have enough rights to inspect task details.
  144. */
  145. ret = kcmp_lock(&task1->signal->cred_guard_mutex,
  146. &task2->signal->cred_guard_mutex);
  147. if (ret)
  148. goto err;
  149. if (!ptrace_may_access(task1, PTRACE_MODE_READ_REALCREDS) ||
  150. !ptrace_may_access(task2, PTRACE_MODE_READ_REALCREDS)) {
  151. ret = -EPERM;
  152. goto err_unlock;
  153. }
  154. switch (type) {
  155. case KCMP_FILE: {
  156. struct file *filp1, *filp2;
  157. filp1 = get_file_raw_ptr(task1, idx1);
  158. filp2 = get_file_raw_ptr(task2, idx2);
  159. if (filp1 && filp2)
  160. ret = kcmp_ptr(filp1, filp2, KCMP_FILE);
  161. else
  162. ret = -EBADF;
  163. break;
  164. }
  165. case KCMP_VM:
  166. ret = kcmp_ptr(task1->mm, task2->mm, KCMP_VM);
  167. break;
  168. case KCMP_FILES:
  169. ret = kcmp_ptr(task1->files, task2->files, KCMP_FILES);
  170. break;
  171. case KCMP_FS:
  172. ret = kcmp_ptr(task1->fs, task2->fs, KCMP_FS);
  173. break;
  174. case KCMP_SIGHAND:
  175. ret = kcmp_ptr(task1->sighand, task2->sighand, KCMP_SIGHAND);
  176. break;
  177. case KCMP_IO:
  178. ret = kcmp_ptr(task1->io_context, task2->io_context, KCMP_IO);
  179. break;
  180. case KCMP_SYSVSEM:
  181. #ifdef CONFIG_SYSVIPC
  182. ret = kcmp_ptr(task1->sysvsem.undo_list,
  183. task2->sysvsem.undo_list,
  184. KCMP_SYSVSEM);
  185. #else
  186. ret = -EOPNOTSUPP;
  187. #endif
  188. break;
  189. case KCMP_EPOLL_TFD:
  190. ret = kcmp_epoll_target(task1, task2, idx1, (void *)idx2);
  191. break;
  192. default:
  193. ret = -EINVAL;
  194. break;
  195. }
  196. err_unlock:
  197. kcmp_unlock(&task1->signal->cred_guard_mutex,
  198. &task2->signal->cred_guard_mutex);
  199. err:
  200. put_task_struct(task1);
  201. put_task_struct(task2);
  202. return ret;
  203. err_no_task:
  204. rcu_read_unlock();
  205. return -ESRCH;
  206. }
  207. static __init int kcmp_cookies_init(void)
  208. {
  209. int i;
  210. get_random_bytes(cookies, sizeof(cookies));
  211. for (i = 0; i < KCMP_TYPES; i++)
  212. cookies[i][1] |= (~(~0UL >> 1) | 1);
  213. return 0;
  214. }
  215. arch_initcall(kcmp_cookies_init);