futex_compat.c 4.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9 10 11
/*
 * linux/kernel/futex_compat.c
 *
 * Futex compatibililty routines.
 *
 * Copyright 2006, Red Hat, Inc., Ingo Molnar
 */

#include <linux/linkage.h>
#include <linux/compat.h>
12
#include <linux/nsproxy.h>
13
#include <linux/futex.h>
14
#include <linux/ptrace.h>
15
#include <linux/syscalls.h>
16

17
#include <linux/uaccess.h>
18

19 20 21 22 23 24

/*
 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
 */
static inline int
fetch_robust_entry(compat_uptr_t *uentry, struct robust_list __user **entry,
25
		   compat_uptr_t __user *head, unsigned int *pi)
26 27 28 29 30 31 32 33 34 35
{
	if (get_user(*uentry, head))
		return -EFAULT;

	*entry = compat_ptr((*uentry) & ~1);
	*pi = (unsigned int)(*uentry) & 1;

	return 0;
}

Al Viro's avatar
Al Viro committed
36
static void __user *futex_uaddr(struct robust_list __user *entry,
37 38 39 40 41 42 43 44
				compat_long_t futex_offset)
{
	compat_uptr_t base = ptr_to_compat(entry);
	void __user *uaddr = compat_ptr(base + futex_offset);

	return uaddr;
}

45 46 47 48 49 50 51 52 53
/*
 * Walk curr->robust_list (very carefully, it's a userspace list!)
 * and mark any locks found there dead, and notify any waiters.
 *
 * We silently return on any sign of list-walking problem.
 */
void compat_exit_robust_list(struct task_struct *curr)
{
	struct compat_robust_list_head __user *head = curr->compat_robust_list;
54
	struct robust_list __user *entry, *next_entry, *pending;
55 56
	unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
	unsigned int uninitialized_var(next_pi);
57
	compat_uptr_t uentry, next_uentry, upending;
58
	compat_long_t futex_offset;
59
	int rc;
60

61 62 63
	if (!futex_cmpxchg_enabled)
		return;

64 65 66 67
	/*
	 * Fetch the list head (which was registered earlier, via
	 * sys_set_robust_list()):
	 */
68
	if (fetch_robust_entry(&uentry, &entry, &head->list.next, &pi))
69 70 71 72 73 74 75 76 77 78
		return;
	/*
	 * Fetch the relative futex offset:
	 */
	if (get_user(futex_offset, &head->futex_offset))
		return;
	/*
	 * Fetch any possibly pending lock-add first, and handle it
	 * if it exists:
	 */
79
	if (fetch_robust_entry(&upending, &pending,
80
			       &head->list_op_pending, &pip))
81 82
		return;

83
	next_entry = NULL;	/* avoid warning with gcc */
84
	while (entry != (struct robust_list __user *) &head->list) {
85 86 87 88 89 90
		/*
		 * Fetch the next entry in the list before calling
		 * handle_futex_death:
		 */
		rc = fetch_robust_entry(&next_uentry, &next_entry,
			(compat_uptr_t __user *)&entry->next, &next_pi);
91 92 93 94
		/*
		 * A pending lock might already be on the list, so
		 * dont process it twice:
		 */
95 96
		if (entry != pending) {
			void __user *uaddr = futex_uaddr(entry, futex_offset);
97

98 99 100
			if (handle_futex_death(uaddr, curr, pi))
				return;
		}
101
		if (rc)
102
			return;
103 104 105
		uentry = next_uentry;
		entry = next_entry;
		pi = next_pi;
106 107 108 109 110 111 112 113
		/*
		 * Avoid excessively long or circular lists:
		 */
		if (!--limit)
			break;

		cond_resched();
	}
114 115 116 117 118
	if (pending) {
		void __user *uaddr = futex_uaddr(pending, futex_offset);

		handle_futex_death(uaddr, curr, pip);
	}
119 120
}

121 122 123
COMPAT_SYSCALL_DEFINE2(set_robust_list,
		struct compat_robust_list_head __user *, head,
		compat_size_t, len)
124
{
125 126 127
	if (!futex_cmpxchg_enabled)
		return -ENOSYS;

128 129 130 131 132 133 134 135
	if (unlikely(len != sizeof(*head)))
		return -EINVAL;

	current->compat_robust_list = head;

	return 0;
}

136 137 138
COMPAT_SYSCALL_DEFINE3(get_robust_list, int, pid,
			compat_uptr_t __user *, head_ptr,
			compat_size_t __user *, len_ptr)
139
{
140
	struct compat_robust_list_head __user *head;
141
	unsigned long ret;
142
	struct task_struct *p;
143

144 145 146
	if (!futex_cmpxchg_enabled)
		return -ENOSYS;

147 148 149
	rcu_read_lock();

	ret = -ESRCH;
150
	if (!pid)
151
		p = current;
152
	else {
153
		p = find_task_by_vpid(pid);
154 155 156 157
		if (!p)
			goto err_unlock;
	}

158
	ret = -EPERM;
159
	if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
160 161 162 163 164
		goto err_unlock;

	head = p->compat_robust_list;
	rcu_read_unlock();

165 166 167 168 169
	if (put_user(sizeof(*head), len_ptr))
		return -EFAULT;
	return put_user(ptr_to_compat(head), head_ptr);

err_unlock:
170
	rcu_read_unlock();
171 172 173 174

	return ret;
}

175 176 177
COMPAT_SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
		struct compat_timespec __user *, utime, u32 __user *, uaddr2,
		u32, val3)
178
{
179 180
	struct timespec ts;
	ktime_t t, *tp = NULL;
181
	int val2 = 0;
182
	int cmd = op & FUTEX_CMD_MASK;
183

184
	if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
185 186
		      cmd == FUTEX_WAIT_BITSET ||
		      cmd == FUTEX_WAIT_REQUEUE_PI)) {
187
		if (compat_get_timespec(&ts, utime))
188
			return -EFAULT;
189
		if (!timespec_valid(&ts))
190
			return -EINVAL;
191 192

		t = timespec_to_ktime(ts);
193
		if (cmd == FUTEX_WAIT)
194
			t = ktime_add_safe(ktime_get(), t);
195
		tp = &t;
196
	}
197 198
	if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
	    cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
199 200
		val2 = (int) (unsigned long) utime;

201
	return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
202
}