kcmp.c 4.3 KB

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