kcmp.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 <asm/unistd.h>
  15. /*
  16. * We don't expose the real in-memory order of objects for security reasons.
  17. * But still the comparison results should be suitable for sorting. So we
  18. * obfuscate kernel pointers values and compare the production instead.
  19. *
  20. * The obfuscation is done in two steps. First we xor the kernel pointer with
  21. * a random value, which puts pointer into a new position in a reordered space.
  22. * Secondly we multiply the xor production with a large odd random number to
  23. * permute its bits even more (the odd multiplier guarantees that the product
  24. * is unique ever after the high bits are truncated, since any odd number is
  25. * relative prime to 2^n).
  26. *
  27. * Note also that the obfuscation itself is invisible to userspace and if needed
  28. * it can be changed to an alternate scheme.
  29. */
  30. static unsigned long cookies[KCMP_TYPES][2] __read_mostly;
  31. static long kptr_obfuscate(long v, int type)
  32. {
  33. return (v ^ cookies[type][0]) * cookies[type][1];
  34. }
  35. /*
  36. * 0 - equal, i.e. v1 = v2
  37. * 1 - less than, i.e. v1 < v2
  38. * 2 - greater than, i.e. v1 > v2
  39. * 3 - not equal but ordering unavailable (reserved for future)
  40. */
  41. static int kcmp_ptr(void *v1, void *v2, enum kcmp_type type)
  42. {
  43. long t1, t2;
  44. t1 = kptr_obfuscate((long)v1, type);
  45. t2 = kptr_obfuscate((long)v2, type);
  46. return (t1 < t2) | ((t1 > t2) << 1);
  47. }
  48. /* The caller must have pinned the task */
  49. static struct file *
  50. get_file_raw_ptr(struct task_struct *task, unsigned int idx)
  51. {
  52. struct file *file = NULL;
  53. task_lock(task);
  54. rcu_read_lock();
  55. if (task->files)
  56. file = fcheck_files(task->files, idx);
  57. rcu_read_unlock();
  58. task_unlock(task);
  59. return file;
  60. }
  61. static void kcmp_unlock(struct mutex *m1, struct mutex *m2)
  62. {
  63. if (likely(m2 != m1))
  64. mutex_unlock(m2);
  65. mutex_unlock(m1);
  66. }
  67. static int kcmp_lock(struct mutex *m1, struct mutex *m2)
  68. {
  69. int err;
  70. if (m2 > m1)
  71. swap(m1, m2);
  72. err = mutex_lock_killable(m1);
  73. if (!err && likely(m1 != m2)) {
  74. err = mutex_lock_killable_nested(m2, SINGLE_DEPTH_NESTING);
  75. if (err)
  76. mutex_unlock(m1);
  77. }
  78. return err;
  79. }
  80. SYSCALL_DEFINE5(kcmp, pid_t, pid1, pid_t, pid2, int, type,
  81. unsigned long, idx1, unsigned long, idx2)
  82. {
  83. struct task_struct *task1, *task2;
  84. int ret;
  85. rcu_read_lock();
  86. /*
  87. * Tasks are looked up in caller's PID namespace only.
  88. */
  89. task1 = find_task_by_vpid(pid1);
  90. task2 = find_task_by_vpid(pid2);
  91. if (!task1 || !task2)
  92. goto err_no_task;
  93. get_task_struct(task1);
  94. get_task_struct(task2);
  95. rcu_read_unlock();
  96. /*
  97. * One should have enough rights to inspect task details.
  98. */
  99. ret = kcmp_lock(&task1->signal->cred_guard_mutex,
  100. &task2->signal->cred_guard_mutex);
  101. if (ret)
  102. goto err;
  103. if (!ptrace_may_access(task1, PTRACE_MODE_READ_REALCREDS) ||
  104. !ptrace_may_access(task2, PTRACE_MODE_READ_REALCREDS)) {
  105. ret = -EPERM;
  106. goto err_unlock;
  107. }
  108. switch (type) {
  109. case KCMP_FILE: {
  110. struct file *filp1, *filp2;
  111. filp1 = get_file_raw_ptr(task1, idx1);
  112. filp2 = get_file_raw_ptr(task2, idx2);
  113. if (filp1 && filp2)
  114. ret = kcmp_ptr(filp1, filp2, KCMP_FILE);
  115. else
  116. ret = -EBADF;
  117. break;
  118. }
  119. case KCMP_VM:
  120. ret = kcmp_ptr(task1->mm, task2->mm, KCMP_VM);
  121. break;
  122. case KCMP_FILES:
  123. ret = kcmp_ptr(task1->files, task2->files, KCMP_FILES);
  124. break;
  125. case KCMP_FS:
  126. ret = kcmp_ptr(task1->fs, task2->fs, KCMP_FS);
  127. break;
  128. case KCMP_SIGHAND:
  129. ret = kcmp_ptr(task1->sighand, task2->sighand, KCMP_SIGHAND);
  130. break;
  131. case KCMP_IO:
  132. ret = kcmp_ptr(task1->io_context, task2->io_context, KCMP_IO);
  133. break;
  134. case KCMP_SYSVSEM:
  135. #ifdef CONFIG_SYSVIPC
  136. ret = kcmp_ptr(task1->sysvsem.undo_list,
  137. task2->sysvsem.undo_list,
  138. KCMP_SYSVSEM);
  139. #else
  140. ret = -EOPNOTSUPP;
  141. #endif
  142. break;
  143. default:
  144. ret = -EINVAL;
  145. break;
  146. }
  147. err_unlock:
  148. kcmp_unlock(&task1->signal->cred_guard_mutex,
  149. &task2->signal->cred_guard_mutex);
  150. err:
  151. put_task_struct(task1);
  152. put_task_struct(task2);
  153. return ret;
  154. err_no_task:
  155. rcu_read_unlock();
  156. return -ESRCH;
  157. }
  158. static __init int kcmp_cookies_init(void)
  159. {
  160. int i;
  161. get_random_bytes(cookies, sizeof(cookies));
  162. for (i = 0; i < KCMP_TYPES; i++)
  163. cookies[i][1] |= (~(~0UL >> 1) | 1);
  164. return 0;
  165. }
  166. arch_initcall(kcmp_cookies_init);