• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #include <linux/syscalls.h>
4 #include <linux/time_namespace.h>
5 
6 #include "futex.h"
7 #include <trace/hooks/futex.h>
8 
9 /*
10  * Support for robust futexes: the kernel cleans up held futexes at
11  * thread exit time.
12  *
13  * Implementation: user-space maintains a per-thread list of locks it
14  * is holding. Upon do_exit(), the kernel carefully walks this list,
15  * and marks all locks that are owned by this thread with the
16  * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
17  * always manipulated with the lock held, so the list is private and
18  * per-thread. Userspace also maintains a per-thread 'list_op_pending'
19  * field, to allow the kernel to clean up if the thread dies after
20  * acquiring the lock, but just before it could have added itself to
21  * the list. There can only be one such pending lock.
22  */
23 
24 /**
25  * sys_set_robust_list() - Set the robust-futex list head of a task
26  * @head:	pointer to the list-head
27  * @len:	length of the list-head, as userspace expects
28  */
SYSCALL_DEFINE2(set_robust_list,struct robust_list_head __user *,head,size_t,len)29 SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head,
30 		size_t, len)
31 {
32 	/*
33 	 * The kernel knows only one size for now:
34 	 */
35 	if (unlikely(len != sizeof(*head)))
36 		return -EINVAL;
37 
38 	current->robust_list = head;
39 
40 	return 0;
41 }
42 
43 /**
44  * sys_get_robust_list() - Get the robust-futex list head of a task
45  * @pid:	pid of the process [zero for current task]
46  * @head_ptr:	pointer to a list-head pointer, the kernel fills it in
47  * @len_ptr:	pointer to a length field, the kernel fills in the header size
48  */
SYSCALL_DEFINE3(get_robust_list,int,pid,struct robust_list_head __user * __user *,head_ptr,size_t __user *,len_ptr)49 SYSCALL_DEFINE3(get_robust_list, int, pid,
50 		struct robust_list_head __user * __user *, head_ptr,
51 		size_t __user *, len_ptr)
52 {
53 	struct robust_list_head __user *head;
54 	unsigned long ret;
55 	struct task_struct *p;
56 
57 	rcu_read_lock();
58 
59 	ret = -ESRCH;
60 	if (!pid)
61 		p = current;
62 	else {
63 		p = find_task_by_vpid(pid);
64 		if (!p)
65 			goto err_unlock;
66 	}
67 
68 	ret = -EPERM;
69 	if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
70 		goto err_unlock;
71 
72 	head = p->robust_list;
73 	rcu_read_unlock();
74 
75 	if (put_user(sizeof(*head), len_ptr))
76 		return -EFAULT;
77 	return put_user(head, head_ptr);
78 
79 err_unlock:
80 	rcu_read_unlock();
81 
82 	return ret;
83 }
84 
do_futex(u32 __user * uaddr,int op,u32 val,ktime_t * timeout,u32 __user * uaddr2,u32 val2,u32 val3)85 long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
86 		u32 __user *uaddr2, u32 val2, u32 val3)
87 {
88 	unsigned int flags = futex_to_flags(op);
89 	int cmd = op & FUTEX_CMD_MASK;
90 
91 	if (flags & FLAGS_CLOCKRT) {
92 		if (cmd != FUTEX_WAIT_BITSET &&
93 		    cmd != FUTEX_WAIT_REQUEUE_PI &&
94 		    cmd != FUTEX_LOCK_PI2)
95 			return -ENOSYS;
96 	}
97 
98 	trace_android_vh_do_futex(cmd, &flags, uaddr2);
99 	switch (cmd) {
100 	case FUTEX_WAIT:
101 		val3 = FUTEX_BITSET_MATCH_ANY;
102 		fallthrough;
103 	case FUTEX_WAIT_BITSET:
104 		return futex_wait(uaddr, flags, val, timeout, val3);
105 	case FUTEX_WAKE:
106 		val3 = FUTEX_BITSET_MATCH_ANY;
107 		fallthrough;
108 	case FUTEX_WAKE_BITSET:
109 		return futex_wake(uaddr, flags, val, val3);
110 	case FUTEX_REQUEUE:
111 		return futex_requeue(uaddr, flags, uaddr2, flags, val, val2, NULL, 0);
112 	case FUTEX_CMP_REQUEUE:
113 		return futex_requeue(uaddr, flags, uaddr2, flags, val, val2, &val3, 0);
114 	case FUTEX_WAKE_OP:
115 		return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3);
116 	case FUTEX_LOCK_PI:
117 		flags |= FLAGS_CLOCKRT;
118 		fallthrough;
119 	case FUTEX_LOCK_PI2:
120 		return futex_lock_pi(uaddr, flags, timeout, 0);
121 	case FUTEX_UNLOCK_PI:
122 		return futex_unlock_pi(uaddr, flags);
123 	case FUTEX_TRYLOCK_PI:
124 		return futex_lock_pi(uaddr, flags, NULL, 1);
125 	case FUTEX_WAIT_REQUEUE_PI:
126 		val3 = FUTEX_BITSET_MATCH_ANY;
127 		return futex_wait_requeue_pi(uaddr, flags, val, timeout, val3,
128 					     uaddr2);
129 	case FUTEX_CMP_REQUEUE_PI:
130 		return futex_requeue(uaddr, flags, uaddr2, flags, val, val2, &val3, 1);
131 	}
132 	return -ENOSYS;
133 }
134 
futex_cmd_has_timeout(u32 cmd)135 static __always_inline bool futex_cmd_has_timeout(u32 cmd)
136 {
137 	switch (cmd) {
138 	case FUTEX_WAIT:
139 	case FUTEX_LOCK_PI:
140 	case FUTEX_LOCK_PI2:
141 	case FUTEX_WAIT_BITSET:
142 	case FUTEX_WAIT_REQUEUE_PI:
143 		return true;
144 	}
145 	return false;
146 }
147 
148 static __always_inline int
futex_init_timeout(u32 cmd,u32 op,struct timespec64 * ts,ktime_t * t)149 futex_init_timeout(u32 cmd, u32 op, struct timespec64 *ts, ktime_t *t)
150 {
151 	if (!timespec64_valid(ts))
152 		return -EINVAL;
153 
154 	*t = timespec64_to_ktime(*ts);
155 	if (cmd == FUTEX_WAIT)
156 		*t = ktime_add_safe(ktime_get(), *t);
157 	else if (cmd != FUTEX_LOCK_PI && !(op & FUTEX_CLOCK_REALTIME))
158 		*t = timens_ktime_to_host(CLOCK_MONOTONIC, *t);
159 	return 0;
160 }
161 
SYSCALL_DEFINE6(futex,u32 __user *,uaddr,int,op,u32,val,const struct __kernel_timespec __user *,utime,u32 __user *,uaddr2,u32,val3)162 SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
163 		const struct __kernel_timespec __user *, utime,
164 		u32 __user *, uaddr2, u32, val3)
165 {
166 	int ret, cmd = op & FUTEX_CMD_MASK;
167 	ktime_t t, *tp = NULL;
168 	struct timespec64 ts;
169 
170 	if (utime && futex_cmd_has_timeout(cmd)) {
171 		if (unlikely(should_fail_futex(!(op & FUTEX_PRIVATE_FLAG))))
172 			return -EFAULT;
173 		if (get_timespec64(&ts, utime))
174 			return -EFAULT;
175 		ret = futex_init_timeout(cmd, op, &ts, &t);
176 		if (ret)
177 			return ret;
178 		tp = &t;
179 	}
180 
181 	return do_futex(uaddr, op, val, tp, uaddr2, (unsigned long)utime, val3);
182 }
183 
184 /**
185  * futex_parse_waitv - Parse a waitv array from userspace
186  * @futexv:	Kernel side list of waiters to be filled
187  * @uwaitv:     Userspace list to be parsed
188  * @nr_futexes: Length of futexv
189  * @wake:	Wake to call when futex is woken
190  * @wake_data:	Data for the wake handler
191  *
192  * Return: Error code on failure, 0 on success
193  */
futex_parse_waitv(struct futex_vector * futexv,struct futex_waitv __user * uwaitv,unsigned int nr_futexes,futex_wake_fn * wake,void * wake_data)194 int futex_parse_waitv(struct futex_vector *futexv,
195 		      struct futex_waitv __user *uwaitv,
196 		      unsigned int nr_futexes, futex_wake_fn *wake,
197 		      void *wake_data)
198 {
199 	struct futex_waitv aux;
200 	unsigned int i;
201 
202 	for (i = 0; i < nr_futexes; i++) {
203 		unsigned int flags;
204 
205 		if (copy_from_user(&aux, &uwaitv[i], sizeof(aux)))
206 			return -EFAULT;
207 
208 		if ((aux.flags & ~FUTEX2_VALID_MASK) || aux.__reserved)
209 			return -EINVAL;
210 
211 		flags = futex2_to_flags(aux.flags);
212 		if (!futex_flags_valid(flags))
213 			return -EINVAL;
214 
215 		if (!futex_validate_input(flags, aux.val))
216 			return -EINVAL;
217 
218 		futexv[i].w.flags = flags;
219 		futexv[i].w.val = aux.val;
220 		futexv[i].w.uaddr = aux.uaddr;
221 		futexv[i].q = futex_q_init;
222 		futexv[i].q.wake = wake;
223 		futexv[i].q.wake_data = wake_data;
224 	}
225 
226 	return 0;
227 }
228 
futex2_setup_timeout(struct __kernel_timespec __user * timeout,clockid_t clockid,struct hrtimer_sleeper * to)229 static int futex2_setup_timeout(struct __kernel_timespec __user *timeout,
230 				clockid_t clockid, struct hrtimer_sleeper *to)
231 {
232 	int flag_clkid = 0, flag_init = 0;
233 	struct timespec64 ts;
234 	ktime_t time;
235 	int ret;
236 
237 	if (!timeout)
238 		return 0;
239 
240 	if (clockid == CLOCK_REALTIME) {
241 		flag_clkid = FLAGS_CLOCKRT;
242 		flag_init = FUTEX_CLOCK_REALTIME;
243 	}
244 
245 	if (clockid != CLOCK_REALTIME && clockid != CLOCK_MONOTONIC)
246 		return -EINVAL;
247 
248 	if (get_timespec64(&ts, timeout))
249 		return -EFAULT;
250 
251 	/*
252 	 * Since there's no opcode for futex_waitv, use
253 	 * FUTEX_WAIT_BITSET that uses absolute timeout as well
254 	 */
255 	ret = futex_init_timeout(FUTEX_WAIT_BITSET, flag_init, &ts, &time);
256 	if (ret)
257 		return ret;
258 
259 	futex_setup_timer(&time, to, flag_clkid, 0);
260 	return 0;
261 }
262 
futex2_destroy_timeout(struct hrtimer_sleeper * to)263 static inline void futex2_destroy_timeout(struct hrtimer_sleeper *to)
264 {
265 	hrtimer_cancel(&to->timer);
266 	destroy_hrtimer_on_stack(&to->timer);
267 }
268 
269 /**
270  * sys_futex_waitv - Wait on a list of futexes
271  * @waiters:    List of futexes to wait on
272  * @nr_futexes: Length of futexv
273  * @flags:      Flag for timeout (monotonic/realtime)
274  * @timeout:	Optional absolute timeout.
275  * @clockid:	Clock to be used for the timeout, realtime or monotonic.
276  *
277  * Given an array of `struct futex_waitv`, wait on each uaddr. The thread wakes
278  * if a futex_wake() is performed at any uaddr. The syscall returns immediately
279  * if any waiter has *uaddr != val. *timeout is an optional timeout value for
280  * the operation. Each waiter has individual flags. The `flags` argument for
281  * the syscall should be used solely for specifying the timeout as realtime, if
282  * needed. Flags for private futexes, sizes, etc. should be used on the
283  * individual flags of each waiter.
284  *
285  * Returns the array index of one of the woken futexes. No further information
286  * is provided: any number of other futexes may also have been woken by the
287  * same event, and if more than one futex was woken, the retrned index may
288  * refer to any one of them. (It is not necessaryily the futex with the
289  * smallest index, nor the one most recently woken, nor...)
290  */
291 
SYSCALL_DEFINE5(futex_waitv,struct futex_waitv __user *,waiters,unsigned int,nr_futexes,unsigned int,flags,struct __kernel_timespec __user *,timeout,clockid_t,clockid)292 SYSCALL_DEFINE5(futex_waitv, struct futex_waitv __user *, waiters,
293 		unsigned int, nr_futexes, unsigned int, flags,
294 		struct __kernel_timespec __user *, timeout, clockid_t, clockid)
295 {
296 	struct hrtimer_sleeper to;
297 	struct futex_vector *futexv;
298 	int ret;
299 
300 	/* This syscall supports no flags for now */
301 	if (flags)
302 		return -EINVAL;
303 
304 	if (!nr_futexes || nr_futexes > FUTEX_WAITV_MAX || !waiters)
305 		return -EINVAL;
306 
307 	if (timeout && (ret = futex2_setup_timeout(timeout, clockid, &to)))
308 		return ret;
309 
310 	futexv = kcalloc(nr_futexes, sizeof(*futexv), GFP_KERNEL);
311 	if (!futexv) {
312 		ret = -ENOMEM;
313 		goto destroy_timer;
314 	}
315 
316 	ret = futex_parse_waitv(futexv, waiters, nr_futexes, futex_wake_mark,
317 				NULL);
318 	if (!ret)
319 		ret = futex_wait_multiple(futexv, nr_futexes, timeout ? &to : NULL);
320 
321 	kfree(futexv);
322 
323 destroy_timer:
324 	if (timeout)
325 		futex2_destroy_timeout(&to);
326 	return ret;
327 }
328 
329 /*
330  * sys_futex_wake - Wake a number of futexes
331  * @uaddr:	Address of the futex(es) to wake
332  * @mask:	bitmask
333  * @nr:		Number of the futexes to wake
334  * @flags:	FUTEX2 flags
335  *
336  * Identical to the traditional FUTEX_WAKE_BITSET op, except it is part of the
337  * futex2 family of calls.
338  */
339 
SYSCALL_DEFINE4(futex_wake,void __user *,uaddr,unsigned long,mask,int,nr,unsigned int,flags)340 SYSCALL_DEFINE4(futex_wake,
341 		void __user *, uaddr,
342 		unsigned long, mask,
343 		int, nr,
344 		unsigned int, flags)
345 {
346 	if (flags & ~FUTEX2_VALID_MASK)
347 		return -EINVAL;
348 
349 	flags = futex2_to_flags(flags);
350 	if (!futex_flags_valid(flags))
351 		return -EINVAL;
352 
353 	if (!futex_validate_input(flags, mask))
354 		return -EINVAL;
355 
356 	return futex_wake(uaddr, FLAGS_STRICT | flags, nr, mask);
357 }
358 
359 /*
360  * sys_futex_wait - Wait on a futex
361  * @uaddr:	Address of the futex to wait on
362  * @val:	Value of @uaddr
363  * @mask:	bitmask
364  * @flags:	FUTEX2 flags
365  * @timeout:	Optional absolute timeout
366  * @clockid:	Clock to be used for the timeout, realtime or monotonic
367  *
368  * Identical to the traditional FUTEX_WAIT_BITSET op, except it is part of the
369  * futex2 familiy of calls.
370  */
371 
SYSCALL_DEFINE6(futex_wait,void __user *,uaddr,unsigned long,val,unsigned long,mask,unsigned int,flags,struct __kernel_timespec __user *,timeout,clockid_t,clockid)372 SYSCALL_DEFINE6(futex_wait,
373 		void __user *, uaddr,
374 		unsigned long, val,
375 		unsigned long, mask,
376 		unsigned int, flags,
377 		struct __kernel_timespec __user *, timeout,
378 		clockid_t, clockid)
379 {
380 	struct hrtimer_sleeper to;
381 	int ret;
382 
383 	if (flags & ~FUTEX2_VALID_MASK)
384 		return -EINVAL;
385 
386 	flags = futex2_to_flags(flags);
387 	if (!futex_flags_valid(flags))
388 		return -EINVAL;
389 
390 	if (!futex_validate_input(flags, val) ||
391 	    !futex_validate_input(flags, mask))
392 		return -EINVAL;
393 
394 	if (timeout && (ret = futex2_setup_timeout(timeout, clockid, &to)))
395 		return ret;
396 
397 	ret = __futex_wait(uaddr, flags, val, timeout ? &to : NULL, mask);
398 
399 	if (timeout)
400 		futex2_destroy_timeout(&to);
401 
402 	return ret;
403 }
404 
405 /*
406  * sys_futex_requeue - Requeue a waiter from one futex to another
407  * @waiters:	array describing the source and destination futex
408  * @flags:	unused
409  * @nr_wake:	number of futexes to wake
410  * @nr_requeue:	number of futexes to requeue
411  *
412  * Identical to the traditional FUTEX_CMP_REQUEUE op, except it is part of the
413  * futex2 family of calls.
414  */
415 
SYSCALL_DEFINE4(futex_requeue,struct futex_waitv __user *,waiters,unsigned int,flags,int,nr_wake,int,nr_requeue)416 SYSCALL_DEFINE4(futex_requeue,
417 		struct futex_waitv __user *, waiters,
418 		unsigned int, flags,
419 		int, nr_wake,
420 		int, nr_requeue)
421 {
422 	struct futex_vector futexes[2];
423 	u32 cmpval;
424 	int ret;
425 
426 	if (flags)
427 		return -EINVAL;
428 
429 	if (!waiters)
430 		return -EINVAL;
431 
432 	ret = futex_parse_waitv(futexes, waiters, 2, futex_wake_mark, NULL);
433 	if (ret)
434 		return ret;
435 
436 	cmpval = futexes[0].w.val;
437 
438 	return futex_requeue(u64_to_user_ptr(futexes[0].w.uaddr), futexes[0].w.flags,
439 			     u64_to_user_ptr(futexes[1].w.uaddr), futexes[1].w.flags,
440 			     nr_wake, nr_requeue, &cmpval, 0);
441 }
442 
443 #ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE2(set_robust_list,struct compat_robust_list_head __user *,head,compat_size_t,len)444 COMPAT_SYSCALL_DEFINE2(set_robust_list,
445 		struct compat_robust_list_head __user *, head,
446 		compat_size_t, len)
447 {
448 	if (unlikely(len != sizeof(*head)))
449 		return -EINVAL;
450 
451 	current->compat_robust_list = head;
452 
453 	return 0;
454 }
455 
COMPAT_SYSCALL_DEFINE3(get_robust_list,int,pid,compat_uptr_t __user *,head_ptr,compat_size_t __user *,len_ptr)456 COMPAT_SYSCALL_DEFINE3(get_robust_list, int, pid,
457 			compat_uptr_t __user *, head_ptr,
458 			compat_size_t __user *, len_ptr)
459 {
460 	struct compat_robust_list_head __user *head;
461 	unsigned long ret;
462 	struct task_struct *p;
463 
464 	rcu_read_lock();
465 
466 	ret = -ESRCH;
467 	if (!pid)
468 		p = current;
469 	else {
470 		p = find_task_by_vpid(pid);
471 		if (!p)
472 			goto err_unlock;
473 	}
474 
475 	ret = -EPERM;
476 	if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
477 		goto err_unlock;
478 
479 	head = p->compat_robust_list;
480 	rcu_read_unlock();
481 
482 	if (put_user(sizeof(*head), len_ptr))
483 		return -EFAULT;
484 	return put_user(ptr_to_compat(head), head_ptr);
485 
486 err_unlock:
487 	rcu_read_unlock();
488 
489 	return ret;
490 }
491 #endif /* CONFIG_COMPAT */
492 
493 #ifdef CONFIG_COMPAT_32BIT_TIME
SYSCALL_DEFINE6(futex_time32,u32 __user *,uaddr,int,op,u32,val,const struct old_timespec32 __user *,utime,u32 __user *,uaddr2,u32,val3)494 SYSCALL_DEFINE6(futex_time32, u32 __user *, uaddr, int, op, u32, val,
495 		const struct old_timespec32 __user *, utime, u32 __user *, uaddr2,
496 		u32, val3)
497 {
498 	int ret, cmd = op & FUTEX_CMD_MASK;
499 	ktime_t t, *tp = NULL;
500 	struct timespec64 ts;
501 
502 	if (utime && futex_cmd_has_timeout(cmd)) {
503 		if (get_old_timespec32(&ts, utime))
504 			return -EFAULT;
505 		ret = futex_init_timeout(cmd, op, &ts, &t);
506 		if (ret)
507 			return ret;
508 		tp = &t;
509 	}
510 
511 	return do_futex(uaddr, op, val, tp, uaddr2, (unsigned long)utime, val3);
512 }
513 #endif /* CONFIG_COMPAT_32BIT_TIME */
514 
515