futex_compat.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/futex.h>
  11. #include <asm/uaccess.h>
  12. /*
  13. * Fetch a robust-list pointer. Bit 0 signals PI futexes:
  14. */
  15. static inline int
  16. fetch_robust_entry(compat_uptr_t *uentry, struct robust_list __user **entry,
  17. compat_uptr_t __user *head, int *pi)
  18. {
  19. if (get_user(*uentry, head))
  20. return -EFAULT;
  21. *entry = compat_ptr((*uentry) & ~1);
  22. *pi = (unsigned int)(*uentry) & 1;
  23. return 0;
  24. }
  25. /*
  26. * Walk curr->robust_list (very carefully, it's a userspace list!)
  27. * and mark any locks found there dead, and notify any waiters.
  28. *
  29. * We silently return on any sign of list-walking problem.
  30. */
  31. void compat_exit_robust_list(struct task_struct *curr)
  32. {
  33. struct compat_robust_list_head __user *head = curr->compat_robust_list;
  34. struct robust_list __user *entry, *pending;
  35. unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
  36. compat_uptr_t uentry, upending;
  37. compat_long_t futex_offset;
  38. /*
  39. * Fetch the list head (which was registered earlier, via
  40. * sys_set_robust_list()):
  41. */
  42. if (fetch_robust_entry(&uentry, &entry, &head->list.next, &pi))
  43. return;
  44. /*
  45. * Fetch the relative futex offset:
  46. */
  47. if (get_user(futex_offset, &head->futex_offset))
  48. return;
  49. /*
  50. * Fetch any possibly pending lock-add first, and handle it
  51. * if it exists:
  52. */
  53. if (fetch_robust_entry(&upending, &pending,
  54. &head->list_op_pending, &pip))
  55. return;
  56. if (upending)
  57. handle_futex_death((void __user *)pending + futex_offset, curr, pip);
  58. while (compat_ptr(uentry) != &head->list) {
  59. /*
  60. * A pending lock might already be on the list, so
  61. * dont process it twice:
  62. */
  63. if (entry != pending)
  64. if (handle_futex_death((void __user *)entry + futex_offset,
  65. curr, pi))
  66. return;
  67. /*
  68. * Fetch the next entry in the list:
  69. */
  70. if (fetch_robust_entry(&uentry, &entry,
  71. (compat_uptr_t __user *)&entry->next, &pi))
  72. return;
  73. /*
  74. * Avoid excessively long or circular lists:
  75. */
  76. if (!--limit)
  77. break;
  78. cond_resched();
  79. }
  80. }
  81. asmlinkage long
  82. compat_sys_set_robust_list(struct compat_robust_list_head __user *head,
  83. compat_size_t len)
  84. {
  85. if (unlikely(len != sizeof(*head)))
  86. return -EINVAL;
  87. current->compat_robust_list = head;
  88. return 0;
  89. }
  90. asmlinkage long
  91. compat_sys_get_robust_list(int pid, compat_uptr_t __user *head_ptr,
  92. compat_size_t __user *len_ptr)
  93. {
  94. struct compat_robust_list_head __user *head;
  95. unsigned long ret;
  96. if (!pid)
  97. head = current->compat_robust_list;
  98. else {
  99. struct task_struct *p;
  100. ret = -ESRCH;
  101. read_lock(&tasklist_lock);
  102. p = find_task_by_pid(pid);
  103. if (!p)
  104. goto err_unlock;
  105. ret = -EPERM;
  106. if ((current->euid != p->euid) && (current->euid != p->uid) &&
  107. !capable(CAP_SYS_PTRACE))
  108. goto err_unlock;
  109. head = p->compat_robust_list;
  110. read_unlock(&tasklist_lock);
  111. }
  112. if (put_user(sizeof(*head), len_ptr))
  113. return -EFAULT;
  114. return put_user(ptr_to_compat(head), head_ptr);
  115. err_unlock:
  116. read_unlock(&tasklist_lock);
  117. return ret;
  118. }
  119. asmlinkage long compat_sys_futex(u32 __user *uaddr, int op, u32 val,
  120. struct compat_timespec __user *utime, u32 __user *uaddr2,
  121. u32 val3)
  122. {
  123. struct timespec t;
  124. unsigned long timeout = MAX_SCHEDULE_TIMEOUT;
  125. int val2 = 0;
  126. if (utime && (op == FUTEX_WAIT || op == FUTEX_LOCK_PI)) {
  127. if (get_compat_timespec(&t, utime))
  128. return -EFAULT;
  129. if (!timespec_valid(&t))
  130. return -EINVAL;
  131. if (op == FUTEX_WAIT)
  132. timeout = timespec_to_jiffies(&t) + 1;
  133. else {
  134. timeout = t.tv_sec;
  135. val2 = t.tv_nsec;
  136. }
  137. }
  138. if (op == FUTEX_REQUEUE || op == FUTEX_CMP_REQUEUE)
  139. val2 = (int) (unsigned long) utime;
  140. return do_futex(uaddr, op, val, timeout, uaddr2, val2, val3);
  141. }