kcmp.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 ret;
  44. ret = kptr_obfuscate((long)v1, type) - kptr_obfuscate((long)v2, type);
  45. return (ret < 0) | ((ret > 0) << 1);
  46. }
  47. /* The caller must have pinned the task */
  48. static struct file *
  49. get_file_raw_ptr(struct task_struct *task, unsigned int idx)
  50. {
  51. struct file *file = NULL;
  52. task_lock(task);
  53. rcu_read_lock();
  54. if (task->files)
  55. file = fcheck_files(task->files, idx);
  56. rcu_read_unlock();
  57. task_unlock(task);
  58. return file;
  59. }
  60. static void kcmp_unlock(struct mutex *m1, struct mutex *m2)
  61. {
  62. if (likely(m2 != m1))
  63. mutex_unlock(m2);
  64. mutex_unlock(m1);
  65. }
  66. static int kcmp_lock(struct mutex *m1, struct mutex *m2)
  67. {
  68. int err;
  69. if (m2 > m1)
  70. swap(m1, m2);
  71. err = mutex_lock_killable(m1);
  72. if (!err && likely(m1 != m2)) {
  73. err = mutex_lock_killable_nested(m2, SINGLE_DEPTH_NESTING);
  74. if (err)
  75. mutex_unlock(m1);
  76. }
  77. return err;
  78. }
  79. SYSCALL_DEFINE5(kcmp, pid_t, pid1, pid_t, pid2, int, type,
  80. unsigned long, idx1, unsigned long, idx2)
  81. {
  82. struct task_struct *task1, *task2;
  83. int ret;
  84. rcu_read_lock();
  85. /*
  86. * Tasks are looked up in caller's PID namespace only.
  87. */
  88. task1 = find_task_by_vpid(pid1);
  89. task2 = find_task_by_vpid(pid2);
  90. if (!task1 || !task2)
  91. goto err_no_task;
  92. get_task_struct(task1);
  93. get_task_struct(task2);
  94. rcu_read_unlock();
  95. /*
  96. * One should have enough rights to inspect task details.
  97. */
  98. ret = kcmp_lock(&task1->signal->cred_guard_mutex,
  99. &task2->signal->cred_guard_mutex);
  100. if (ret)
  101. goto err;
  102. if (!ptrace_may_access(task1, PTRACE_MODE_READ) ||
  103. !ptrace_may_access(task2, PTRACE_MODE_READ)) {
  104. ret = -EPERM;
  105. goto err_unlock;
  106. }
  107. switch (type) {
  108. case KCMP_FILE: {
  109. struct file *filp1, *filp2;
  110. filp1 = get_file_raw_ptr(task1, idx1);
  111. filp2 = get_file_raw_ptr(task2, idx2);
  112. if (filp1 && filp2)
  113. ret = kcmp_ptr(filp1, filp2, KCMP_FILE);
  114. else
  115. ret = -EBADF;
  116. break;
  117. }
  118. case KCMP_VM:
  119. ret = kcmp_ptr(task1->mm, task2->mm, KCMP_VM);
  120. break;
  121. case KCMP_FILES:
  122. ret = kcmp_ptr(task1->files, task2->files, KCMP_FILES);
  123. break;
  124. case KCMP_FS:
  125. ret = kcmp_ptr(task1->fs, task2->fs, KCMP_FS);
  126. break;
  127. case KCMP_SIGHAND:
  128. ret = kcmp_ptr(task1->sighand, task2->sighand, KCMP_SIGHAND);
  129. break;
  130. case KCMP_IO:
  131. ret = kcmp_ptr(task1->io_context, task2->io_context, KCMP_IO);
  132. break;
  133. case KCMP_SYSVSEM:
  134. #ifdef CONFIG_SYSVIPC
  135. ret = kcmp_ptr(task1->sysvsem.undo_list,
  136. task2->sysvsem.undo_list,
  137. KCMP_SYSVSEM);
  138. #else
  139. ret = -EOPNOTSUPP;
  140. #endif
  141. break;
  142. default:
  143. ret = -EINVAL;
  144. break;
  145. }
  146. err_unlock:
  147. kcmp_unlock(&task1->signal->cred_guard_mutex,
  148. &task2->signal->cred_guard_mutex);
  149. err:
  150. put_task_struct(task1);
  151. put_task_struct(task2);
  152. return ret;
  153. err_no_task:
  154. rcu_read_unlock();
  155. return -ESRCH;
  156. }
  157. static __init int kcmp_cookies_init(void)
  158. {
  159. int i;
  160. get_random_bytes(cookies, sizeof(cookies));
  161. for (i = 0; i < KCMP_TYPES; i++)
  162. cookies[i][1] |= (~(~0UL >> 1) | 1);
  163. return 0;
  164. }
  165. arch_initcall(kcmp_cookies_init);