futex_compat.c 4.4 KB

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