kcmp.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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_fdget_rcu(task, idx);
  60. rcu_read_unlock();
  61. if (file)
  62. fput(file);
  63. return file;
  64. }
  65. static void kcmp_unlock(struct rw_semaphore *l1, struct rw_semaphore *l2)
  66. {
  67. if (likely(l2 != l1))
  68. up_read(l2);
  69. up_read(l1);
  70. }
  71. static int kcmp_lock(struct rw_semaphore *l1, struct rw_semaphore *l2)
  72. {
  73. int err;
  74. if (l2 > l1)
  75. swap(l1, l2);
  76. err = down_read_killable(l1);
  77. if (!err && likely(l1 != l2)) {
  78. err = down_read_killable_nested(l2, SINGLE_DEPTH_NESTING);
  79. if (err)
  80. up_read(l1);
  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. if (copy_from_user(&slot, uslot, sizeof(slot)))
  93. return -EFAULT;
  94. filp = get_file_raw_ptr(task1, idx1);
  95. if (!filp)
  96. return -EBADF;
  97. filp_epoll = fget_task(task2, slot.efd);
  98. if (!filp_epoll)
  99. return -EBADF;
  100. filp_tgt = get_epoll_tfile_raw_ptr(filp_epoll, slot.tfd, slot.toff);
  101. fput(filp_epoll);
  102. if (IS_ERR(filp_tgt))
  103. return PTR_ERR(filp_tgt);
  104. return kcmp_ptr(filp, filp_tgt, KCMP_FILE);
  105. }
  106. #else
  107. static int kcmp_epoll_target(struct task_struct *task1,
  108. struct task_struct *task2,
  109. unsigned long idx1,
  110. struct kcmp_epoll_slot __user *uslot)
  111. {
  112. return -EOPNOTSUPP;
  113. }
  114. #endif
  115. SYSCALL_DEFINE5(kcmp, pid_t, pid1, pid_t, pid2, int, type,
  116. unsigned long, idx1, unsigned long, idx2)
  117. {
  118. struct task_struct *task1, *task2;
  119. int ret;
  120. rcu_read_lock();
  121. /*
  122. * Tasks are looked up in caller's PID namespace only.
  123. */
  124. task1 = find_task_by_vpid(pid1);
  125. task2 = find_task_by_vpid(pid2);
  126. if (!task1 || !task2)
  127. goto err_no_task;
  128. get_task_struct(task1);
  129. get_task_struct(task2);
  130. rcu_read_unlock();
  131. /*
  132. * One should have enough rights to inspect task details.
  133. */
  134. ret = kcmp_lock(&task1->signal->exec_update_lock,
  135. &task2->signal->exec_update_lock);
  136. if (ret)
  137. goto err;
  138. if (!ptrace_may_access(task1, PTRACE_MODE_READ_REALCREDS) ||
  139. !ptrace_may_access(task2, PTRACE_MODE_READ_REALCREDS)) {
  140. ret = -EPERM;
  141. goto err_unlock;
  142. }
  143. switch (type) {
  144. case KCMP_FILE: {
  145. struct file *filp1, *filp2;
  146. filp1 = get_file_raw_ptr(task1, idx1);
  147. filp2 = get_file_raw_ptr(task2, idx2);
  148. if (filp1 && filp2)
  149. ret = kcmp_ptr(filp1, filp2, KCMP_FILE);
  150. else
  151. ret = -EBADF;
  152. break;
  153. }
  154. case KCMP_VM:
  155. ret = kcmp_ptr(task1->mm, task2->mm, KCMP_VM);
  156. break;
  157. case KCMP_FILES:
  158. ret = kcmp_ptr(task1->files, task2->files, KCMP_FILES);
  159. break;
  160. case KCMP_FS:
  161. ret = kcmp_ptr(task1->fs, task2->fs, KCMP_FS);
  162. break;
  163. case KCMP_SIGHAND:
  164. ret = kcmp_ptr(task1->sighand, task2->sighand, KCMP_SIGHAND);
  165. break;
  166. case KCMP_IO:
  167. ret = kcmp_ptr(task1->io_context, task2->io_context, KCMP_IO);
  168. break;
  169. case KCMP_SYSVSEM:
  170. #ifdef CONFIG_SYSVIPC
  171. ret = kcmp_ptr(task1->sysvsem.undo_list,
  172. task2->sysvsem.undo_list,
  173. KCMP_SYSVSEM);
  174. #else
  175. ret = -EOPNOTSUPP;
  176. #endif
  177. break;
  178. case KCMP_EPOLL_TFD:
  179. ret = kcmp_epoll_target(task1, task2, idx1, (void *)idx2);
  180. break;
  181. default:
  182. ret = -EINVAL;
  183. break;
  184. }
  185. err_unlock:
  186. kcmp_unlock(&task1->signal->exec_update_lock,
  187. &task2->signal->exec_update_lock);
  188. err:
  189. put_task_struct(task1);
  190. put_task_struct(task2);
  191. return ret;
  192. err_no_task:
  193. rcu_read_unlock();
  194. return -ESRCH;
  195. }
  196. static __init int kcmp_cookies_init(void)
  197. {
  198. int i;
  199. get_random_bytes(cookies, sizeof(cookies));
  200. for (i = 0; i < KCMP_TYPES; i++)
  201. cookies[i][1] |= (~(~0UL >> 1) | 1);
  202. return 0;
  203. }
  204. arch_initcall(kcmp_cookies_init);