Back to home page

LXR

 
 

    


0001 /*
0002  * linux/kernel/futex_compat.c
0003  *
0004  * Futex compatibililty routines.
0005  *
0006  * Copyright 2006, Red Hat, Inc., Ingo Molnar
0007  */
0008 
0009 #include <linux/linkage.h>
0010 #include <linux/compat.h>
0011 #include <linux/nsproxy.h>
0012 #include <linux/futex.h>
0013 #include <linux/ptrace.h>
0014 #include <linux/syscalls.h>
0015 
0016 #include <linux/uaccess.h>
0017 
0018 
0019 /*
0020  * Fetch a robust-list pointer. Bit 0 signals PI futexes:
0021  */
0022 static inline int
0023 fetch_robust_entry(compat_uptr_t *uentry, struct robust_list __user **entry,
0024            compat_uptr_t __user *head, unsigned int *pi)
0025 {
0026     if (get_user(*uentry, head))
0027         return -EFAULT;
0028 
0029     *entry = compat_ptr((*uentry) & ~1);
0030     *pi = (unsigned int)(*uentry) & 1;
0031 
0032     return 0;
0033 }
0034 
0035 static void __user *futex_uaddr(struct robust_list __user *entry,
0036                 compat_long_t futex_offset)
0037 {
0038     compat_uptr_t base = ptr_to_compat(entry);
0039     void __user *uaddr = compat_ptr(base + futex_offset);
0040 
0041     return uaddr;
0042 }
0043 
0044 /*
0045  * Walk curr->robust_list (very carefully, it's a userspace list!)
0046  * and mark any locks found there dead, and notify any waiters.
0047  *
0048  * We silently return on any sign of list-walking problem.
0049  */
0050 void compat_exit_robust_list(struct task_struct *curr)
0051 {
0052     struct compat_robust_list_head __user *head = curr->compat_robust_list;
0053     struct robust_list __user *entry, *next_entry, *pending;
0054     unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
0055     unsigned int uninitialized_var(next_pi);
0056     compat_uptr_t uentry, next_uentry, upending;
0057     compat_long_t futex_offset;
0058     int rc;
0059 
0060     if (!futex_cmpxchg_enabled)
0061         return;
0062 
0063     /*
0064      * Fetch the list head (which was registered earlier, via
0065      * sys_set_robust_list()):
0066      */
0067     if (fetch_robust_entry(&uentry, &entry, &head->list.next, &pi))
0068         return;
0069     /*
0070      * Fetch the relative futex offset:
0071      */
0072     if (get_user(futex_offset, &head->futex_offset))
0073         return;
0074     /*
0075      * Fetch any possibly pending lock-add first, and handle it
0076      * if it exists:
0077      */
0078     if (fetch_robust_entry(&upending, &pending,
0079                    &head->list_op_pending, &pip))
0080         return;
0081 
0082     next_entry = NULL;  /* avoid warning with gcc */
0083     while (entry != (struct robust_list __user *) &head->list) {
0084         /*
0085          * Fetch the next entry in the list before calling
0086          * handle_futex_death:
0087          */
0088         rc = fetch_robust_entry(&next_uentry, &next_entry,
0089             (compat_uptr_t __user *)&entry->next, &next_pi);
0090         /*
0091          * A pending lock might already be on the list, so
0092          * dont process it twice:
0093          */
0094         if (entry != pending) {
0095             void __user *uaddr = futex_uaddr(entry, futex_offset);
0096 
0097             if (handle_futex_death(uaddr, curr, pi))
0098                 return;
0099         }
0100         if (rc)
0101             return;
0102         uentry = next_uentry;
0103         entry = next_entry;
0104         pi = next_pi;
0105         /*
0106          * Avoid excessively long or circular lists:
0107          */
0108         if (!--limit)
0109             break;
0110 
0111         cond_resched();
0112     }
0113     if (pending) {
0114         void __user *uaddr = futex_uaddr(pending, futex_offset);
0115 
0116         handle_futex_death(uaddr, curr, pip);
0117     }
0118 }
0119 
0120 COMPAT_SYSCALL_DEFINE2(set_robust_list,
0121         struct compat_robust_list_head __user *, head,
0122         compat_size_t, len)
0123 {
0124     if (!futex_cmpxchg_enabled)
0125         return -ENOSYS;
0126 
0127     if (unlikely(len != sizeof(*head)))
0128         return -EINVAL;
0129 
0130     current->compat_robust_list = head;
0131 
0132     return 0;
0133 }
0134 
0135 COMPAT_SYSCALL_DEFINE3(get_robust_list, int, pid,
0136             compat_uptr_t __user *, head_ptr,
0137             compat_size_t __user *, len_ptr)
0138 {
0139     struct compat_robust_list_head __user *head;
0140     unsigned long ret;
0141     struct task_struct *p;
0142 
0143     if (!futex_cmpxchg_enabled)
0144         return -ENOSYS;
0145 
0146     rcu_read_lock();
0147 
0148     ret = -ESRCH;
0149     if (!pid)
0150         p = current;
0151     else {
0152         p = find_task_by_vpid(pid);
0153         if (!p)
0154             goto err_unlock;
0155     }
0156 
0157     ret = -EPERM;
0158     if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
0159         goto err_unlock;
0160 
0161     head = p->compat_robust_list;
0162     rcu_read_unlock();
0163 
0164     if (put_user(sizeof(*head), len_ptr))
0165         return -EFAULT;
0166     return put_user(ptr_to_compat(head), head_ptr);
0167 
0168 err_unlock:
0169     rcu_read_unlock();
0170 
0171     return ret;
0172 }
0173 
0174 COMPAT_SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
0175         struct compat_timespec __user *, utime, u32 __user *, uaddr2,
0176         u32, val3)
0177 {
0178     struct timespec ts;
0179     ktime_t t, *tp = NULL;
0180     int val2 = 0;
0181     int cmd = op & FUTEX_CMD_MASK;
0182 
0183     if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
0184               cmd == FUTEX_WAIT_BITSET ||
0185               cmd == FUTEX_WAIT_REQUEUE_PI)) {
0186         if (compat_get_timespec(&ts, utime))
0187             return -EFAULT;
0188         if (!timespec_valid(&ts))
0189             return -EINVAL;
0190 
0191         t = timespec_to_ktime(ts);
0192         if (cmd == FUTEX_WAIT)
0193             t = ktime_add_safe(ktime_get(), t);
0194         tp = &t;
0195     }
0196     if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
0197         cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
0198         val2 = (int) (unsigned long) utime;
0199 
0200     return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
0201 }