futex_compat.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. HANDLE_DEATH_LIST))
  85. return;
  86. }
  87. if (rc)
  88. return;
  89. uentry = next_uentry;
  90. entry = next_entry;
  91. pi = next_pi;
  92. /*
  93. * Avoid excessively long or circular lists:
  94. */
  95. if (!--limit)
  96. break;
  97. cond_resched();
  98. }
  99. if (pending) {
  100. void __user *uaddr = futex_uaddr(pending, futex_offset);
  101. handle_futex_death(uaddr, curr, pip, HANDLE_DEATH_PENDING);
  102. }
  103. }
  104. asmlinkage long
  105. compat_sys_set_robust_list(struct compat_robust_list_head __user *head,
  106. compat_size_t len)
  107. {
  108. if (!futex_cmpxchg_enabled)
  109. return -ENOSYS;
  110. if (unlikely(len != sizeof(*head)))
  111. return -EINVAL;
  112. current->compat_robust_list = head;
  113. return 0;
  114. }
  115. asmlinkage long
  116. compat_sys_get_robust_list(int pid, compat_uptr_t __user *head_ptr,
  117. compat_size_t __user *len_ptr)
  118. {
  119. struct compat_robust_list_head __user *head;
  120. unsigned long ret;
  121. struct task_struct *p;
  122. if (!futex_cmpxchg_enabled)
  123. return -ENOSYS;
  124. rcu_read_lock();
  125. ret = -ESRCH;
  126. if (!pid)
  127. p = current;
  128. else {
  129. p = find_task_by_vpid(pid);
  130. if (!p)
  131. goto err_unlock;
  132. }
  133. ret = -EPERM;
  134. if (!ptrace_may_access(p, PTRACE_MODE_READ))
  135. goto err_unlock;
  136. head = p->compat_robust_list;
  137. rcu_read_unlock();
  138. if (put_user(sizeof(*head), len_ptr))
  139. return -EFAULT;
  140. return put_user(ptr_to_compat(head), head_ptr);
  141. err_unlock:
  142. rcu_read_unlock();
  143. return ret;
  144. }
  145. asmlinkage long compat_sys_futex(u32 __user *uaddr, int op, u32 val,
  146. struct compat_timespec __user *utime, u32 __user *uaddr2,
  147. u32 val3)
  148. {
  149. struct timespec ts;
  150. ktime_t t, *tp = NULL;
  151. int val2 = 0;
  152. int cmd = op & FUTEX_CMD_MASK;
  153. if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
  154. cmd == FUTEX_WAIT_BITSET ||
  155. cmd == FUTEX_WAIT_REQUEUE_PI)) {
  156. if (get_compat_timespec(&ts, utime))
  157. return -EFAULT;
  158. if (!timespec_valid(&ts))
  159. return -EINVAL;
  160. t = timespec_to_ktime(ts);
  161. if (cmd == FUTEX_WAIT)
  162. t = ktime_add_safe(ktime_get(), t);
  163. tp = &t;
  164. }
  165. if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
  166. cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
  167. val2 = (int) (unsigned long) utime;
  168. return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
  169. }