futex_compat.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * linux/kernel/futex_compat.c
  3. *
  4. * Futex compatibililty routines.
  5. *
  6. * Copyright 2006, Red Hat, Inc., Ingo Molnar
  7. */
  8. #include <linux/linkage.h>
  9. #include <linux/compat.h>
  10. #include <linux/nsproxy.h>
  11. #include <linux/futex.h>
  12. #include <asm/uaccess.h>
  13. /*
  14. * Fetch a robust-list pointer. Bit 0 signals PI futexes:
  15. */
  16. static inline int
  17. fetch_robust_entry(compat_uptr_t *uentry, struct robust_list __user **entry,
  18. compat_uptr_t __user *head, unsigned int *pi)
  19. {
  20. if (get_user(*uentry, head))
  21. return -EFAULT;
  22. *entry = compat_ptr((*uentry) & ~1);
  23. *pi = (unsigned int)(*uentry) & 1;
  24. return 0;
  25. }
  26. static void __user *futex_uaddr(struct robust_list __user *entry,
  27. compat_long_t futex_offset)
  28. {
  29. compat_uptr_t base = ptr_to_compat(entry);
  30. void __user *uaddr = compat_ptr(base + futex_offset);
  31. return uaddr;
  32. }
  33. /*
  34. * Walk curr->robust_list (very carefully, it's a userspace list!)
  35. * and mark any locks found there dead, and notify any waiters.
  36. *
  37. * We silently return on any sign of list-walking problem.
  38. */
  39. void compat_exit_robust_list(struct task_struct *curr)
  40. {
  41. struct compat_robust_list_head __user *head = curr->compat_robust_list;
  42. struct robust_list __user *entry, *next_entry, *pending;
  43. unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
  44. unsigned int uninitialized_var(next_pi);
  45. compat_uptr_t uentry, next_uentry, upending;
  46. compat_long_t futex_offset;
  47. int rc;
  48. if (!futex_cmpxchg_enabled)
  49. return;
  50. /*
  51. * Fetch the list head (which was registered earlier, via
  52. * sys_set_robust_list()):
  53. */
  54. if (fetch_robust_entry(&uentry, &entry, &head->list.next, &pi))
  55. return;
  56. /*
  57. * Fetch the relative futex offset:
  58. */
  59. if (get_user(futex_offset, &head->futex_offset))
  60. return;
  61. /*
  62. * Fetch any possibly pending lock-add first, and handle it
  63. * if it exists:
  64. */
  65. if (fetch_robust_entry(&upending, &pending,
  66. &head->list_op_pending, &pip))
  67. return;
  68. next_entry = NULL; /* avoid warning with gcc */
  69. while (entry != (struct robust_list __user *) &head->list) {
  70. /*
  71. * Fetch the next entry in the list before calling
  72. * handle_futex_death:
  73. */
  74. rc = fetch_robust_entry(&next_uentry, &next_entry,
  75. (compat_uptr_t __user *)&entry->next, &next_pi);
  76. /*
  77. * A pending lock might already be on the list, so
  78. * dont process it twice:
  79. */
  80. if (entry != pending) {
  81. void __user *uaddr = futex_uaddr(entry, futex_offset);
  82. if (handle_futex_death(uaddr, curr, pi))
  83. return;
  84. }
  85. if (rc)
  86. return;
  87. uentry = next_uentry;
  88. entry = next_entry;
  89. pi = next_pi;
  90. /*
  91. * Avoid excessively long or circular lists:
  92. */
  93. if (!--limit)
  94. break;
  95. cond_resched();
  96. }
  97. if (pending) {
  98. void __user *uaddr = futex_uaddr(pending, futex_offset);
  99. handle_futex_death(uaddr, curr, pip);
  100. }
  101. }
  102. asmlinkage long
  103. compat_sys_set_robust_list(struct compat_robust_list_head __user *head,
  104. compat_size_t len)
  105. {
  106. if (!futex_cmpxchg_enabled)
  107. return -ENOSYS;
  108. if (unlikely(len != sizeof(*head)))
  109. return -EINVAL;
  110. current->compat_robust_list = head;
  111. return 0;
  112. }
  113. asmlinkage long
  114. compat_sys_get_robust_list(int pid, compat_uptr_t __user *head_ptr,
  115. compat_size_t __user *len_ptr)
  116. {
  117. struct compat_robust_list_head __user *head;
  118. unsigned long ret;
  119. const struct cred *cred = current_cred(), *pcred;
  120. if (!futex_cmpxchg_enabled)
  121. return -ENOSYS;
  122. if (!pid)
  123. head = current->compat_robust_list;
  124. else {
  125. struct task_struct *p;
  126. ret = -ESRCH;
  127. rcu_read_lock();
  128. p = find_task_by_vpid(pid);
  129. if (!p)
  130. goto err_unlock;
  131. ret = -EPERM;
  132. pcred = __task_cred(p);
  133. /* If victim is in different user_ns, then uids are not
  134. comparable, so we must have CAP_SYS_PTRACE */
  135. if (cred->user->user_ns != pcred->user->user_ns) {
  136. if (!ns_capable(pcred->user->user_ns, CAP_SYS_PTRACE))
  137. goto err_unlock;
  138. goto ok;
  139. }
  140. /* If victim is in same user_ns, then uids are comparable */
  141. if (cred->euid != pcred->euid &&
  142. cred->euid != pcred->uid &&
  143. !ns_capable(pcred->user->user_ns, CAP_SYS_PTRACE))
  144. goto err_unlock;
  145. ok:
  146. head = p->compat_robust_list;
  147. rcu_read_unlock();
  148. }
  149. if (put_user(sizeof(*head), len_ptr))
  150. return -EFAULT;
  151. return put_user(ptr_to_compat(head), head_ptr);
  152. err_unlock:
  153. rcu_read_unlock();
  154. return ret;
  155. }
  156. asmlinkage long compat_sys_futex(u32 __user *uaddr, int op, u32 val,
  157. struct compat_timespec __user *utime, u32 __user *uaddr2,
  158. u32 val3)
  159. {
  160. struct timespec ts;
  161. ktime_t t, *tp = NULL;
  162. int val2 = 0;
  163. int cmd = op & FUTEX_CMD_MASK;
  164. if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
  165. cmd == FUTEX_WAIT_BITSET ||
  166. cmd == FUTEX_WAIT_REQUEUE_PI)) {
  167. if (get_compat_timespec(&ts, utime))
  168. return -EFAULT;
  169. if (!timespec_valid(&ts))
  170. return -EINVAL;
  171. t = timespec_to_ktime(ts);
  172. if (cmd == FUTEX_WAIT)
  173. t = ktime_add_safe(ktime_get(), t);
  174. tp = &t;
  175. }
  176. if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
  177. cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
  178. val2 = (int) (unsigned long) utime;
  179. return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
  180. }