kcmp.c 5.4 KB

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