xref: /linux/kernel/sys.c (revision df0f9a664be55a8529362a1ada847a19a91e4807)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/kernel/sys.c
4  *
5  *  Copyright (C) 1991, 1992  Linus Torvalds
6  */
7 
8 #include <linux/export.h>
9 #include <linux/mm.h>
10 #include <linux/mm_inline.h>
11 #include <linux/utsname.h>
12 #include <linux/mman.h>
13 #include <linux/reboot.h>
14 #include <linux/prctl.h>
15 #include <linux/highuid.h>
16 #include <linux/fs.h>
17 #include <linux/kmod.h>
18 #include <linux/ksm.h>
19 #include <linux/perf_event.h>
20 #include <linux/resource.h>
21 #include <linux/kernel.h>
22 #include <linux/workqueue.h>
23 #include <linux/capability.h>
24 #include <linux/device.h>
25 #include <linux/key.h>
26 #include <linux/times.h>
27 #include <linux/posix-timers.h>
28 #include <linux/security.h>
29 #include <linux/random.h>
30 #include <linux/suspend.h>
31 #include <linux/tty.h>
32 #include <linux/signal.h>
33 #include <linux/cn_proc.h>
34 #include <linux/task_io_accounting_ops.h>
35 #include <linux/seccomp.h>
36 #include <linux/cpu.h>
37 #include <linux/personality.h>
38 #include <linux/ptrace.h>
39 #include <linux/fs_struct.h>
40 #include <linux/file.h>
41 #include <linux/mount.h>
42 #include <linux/gfp.h>
43 #include <linux/syscore_ops.h>
44 #include <linux/version.h>
45 #include <linux/ctype.h>
46 #include <linux/syscall_user_dispatch.h>
47 
48 #include <linux/compat.h>
49 #include <linux/syscalls.h>
50 #include <linux/kprobes.h>
51 #include <linux/user_namespace.h>
52 #include <linux/time_namespace.h>
53 #include <linux/binfmts.h>
54 #include <linux/futex.h>
55 
56 #include <linux/sched.h>
57 #include <linux/sched/autogroup.h>
58 #include <linux/sched/loadavg.h>
59 #include <linux/sched/stat.h>
60 #include <linux/sched/mm.h>
61 #include <linux/sched/coredump.h>
62 #include <linux/sched/task.h>
63 #include <linux/sched/cputime.h>
64 #include <linux/rcupdate.h>
65 #include <linux/uidgid.h>
66 #include <linux/cred.h>
67 
68 #include <linux/nospec.h>
69 
70 #include <linux/kmsg_dump.h>
71 /* Move somewhere else to avoid recompiling? */
72 #include <generated/utsrelease.h>
73 
74 #include <linux/uaccess.h>
75 #include <asm/io.h>
76 #include <asm/unistd.h>
77 
78 #include <trace/events/task.h>
79 
80 #include "uid16.h"
81 
82 #ifndef SET_UNALIGN_CTL
83 # define SET_UNALIGN_CTL(a, b)	(-EINVAL)
84 #endif
85 #ifndef GET_UNALIGN_CTL
86 # define GET_UNALIGN_CTL(a, b)	(-EINVAL)
87 #endif
88 #ifndef SET_FPEMU_CTL
89 # define SET_FPEMU_CTL(a, b)	(-EINVAL)
90 #endif
91 #ifndef GET_FPEMU_CTL
92 # define GET_FPEMU_CTL(a, b)	(-EINVAL)
93 #endif
94 #ifndef SET_FPEXC_CTL
95 # define SET_FPEXC_CTL(a, b)	(-EINVAL)
96 #endif
97 #ifndef GET_FPEXC_CTL
98 # define GET_FPEXC_CTL(a, b)	(-EINVAL)
99 #endif
100 #ifndef GET_ENDIAN
101 # define GET_ENDIAN(a, b)	(-EINVAL)
102 #endif
103 #ifndef SET_ENDIAN
104 # define SET_ENDIAN(a, b)	(-EINVAL)
105 #endif
106 #ifndef GET_TSC_CTL
107 # define GET_TSC_CTL(a)		(-EINVAL)
108 #endif
109 #ifndef SET_TSC_CTL
110 # define SET_TSC_CTL(a)		(-EINVAL)
111 #endif
112 #ifndef GET_FP_MODE
113 # define GET_FP_MODE(a)		(-EINVAL)
114 #endif
115 #ifndef SET_FP_MODE
116 # define SET_FP_MODE(a,b)	(-EINVAL)
117 #endif
118 #ifndef SVE_SET_VL
119 # define SVE_SET_VL(a)		(-EINVAL)
120 #endif
121 #ifndef SVE_GET_VL
122 # define SVE_GET_VL()		(-EINVAL)
123 #endif
124 #ifndef SME_SET_VL
125 # define SME_SET_VL(a)		(-EINVAL)
126 #endif
127 #ifndef SME_GET_VL
128 # define SME_GET_VL()		(-EINVAL)
129 #endif
130 #ifndef PAC_RESET_KEYS
131 # define PAC_RESET_KEYS(a, b)	(-EINVAL)
132 #endif
133 #ifndef PAC_SET_ENABLED_KEYS
134 # define PAC_SET_ENABLED_KEYS(a, b, c)	(-EINVAL)
135 #endif
136 #ifndef PAC_GET_ENABLED_KEYS
137 # define PAC_GET_ENABLED_KEYS(a)	(-EINVAL)
138 #endif
139 #ifndef SET_TAGGED_ADDR_CTRL
140 # define SET_TAGGED_ADDR_CTRL(a)	(-EINVAL)
141 #endif
142 #ifndef GET_TAGGED_ADDR_CTRL
143 # define GET_TAGGED_ADDR_CTRL()		(-EINVAL)
144 #endif
145 #ifndef RISCV_V_SET_CONTROL
146 # define RISCV_V_SET_CONTROL(a)		(-EINVAL)
147 #endif
148 #ifndef RISCV_V_GET_CONTROL
149 # define RISCV_V_GET_CONTROL()		(-EINVAL)
150 #endif
151 #ifndef RISCV_SET_ICACHE_FLUSH_CTX
152 # define RISCV_SET_ICACHE_FLUSH_CTX(a, b)	(-EINVAL)
153 #endif
154 #ifndef PPC_GET_DEXCR_ASPECT
155 # define PPC_GET_DEXCR_ASPECT(a, b)	(-EINVAL)
156 #endif
157 #ifndef PPC_SET_DEXCR_ASPECT
158 # define PPC_SET_DEXCR_ASPECT(a, b, c)	(-EINVAL)
159 #endif
160 
161 /*
162  * this is where the system-wide overflow UID and GID are defined, for
163  * architectures that now have 32-bit UID/GID but didn't in the past
164  */
165 
166 int overflowuid = DEFAULT_OVERFLOWUID;
167 int overflowgid = DEFAULT_OVERFLOWGID;
168 
169 EXPORT_SYMBOL(overflowuid);
170 EXPORT_SYMBOL(overflowgid);
171 
172 /*
173  * the same as above, but for filesystems which can only store a 16-bit
174  * UID and GID. as such, this is needed on all architectures
175  */
176 
177 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
178 int fs_overflowgid = DEFAULT_FS_OVERFLOWGID;
179 
180 EXPORT_SYMBOL(fs_overflowuid);
181 EXPORT_SYMBOL(fs_overflowgid);
182 
183 static const struct ctl_table overflow_sysctl_table[] = {
184 	{
185 		.procname	= "overflowuid",
186 		.data		= &overflowuid,
187 		.maxlen		= sizeof(int),
188 		.mode		= 0644,
189 		.proc_handler	= proc_dointvec_minmax,
190 		.extra1		= SYSCTL_ZERO,
191 		.extra2		= SYSCTL_MAXOLDUID,
192 	},
193 	{
194 		.procname	= "overflowgid",
195 		.data		= &overflowgid,
196 		.maxlen		= sizeof(int),
197 		.mode		= 0644,
198 		.proc_handler	= proc_dointvec_minmax,
199 		.extra1		= SYSCTL_ZERO,
200 		.extra2		= SYSCTL_MAXOLDUID,
201 	},
202 };
203 
204 static int __init init_overflow_sysctl(void)
205 {
206 	register_sysctl_init("kernel", overflow_sysctl_table);
207 	return 0;
208 }
209 
210 postcore_initcall(init_overflow_sysctl);
211 
212 /*
213  * Returns true if current's euid is same as p's uid or euid,
214  * or has CAP_SYS_NICE to p's user_ns.
215  *
216  * Called with rcu_read_lock, creds are safe
217  */
218 static bool set_one_prio_perm(struct task_struct *p)
219 {
220 	const struct cred *cred = current_cred(), *pcred = __task_cred(p);
221 
222 	if (uid_eq(pcred->uid,  cred->euid) ||
223 	    uid_eq(pcred->euid, cred->euid))
224 		return true;
225 	if (ns_capable(pcred->user_ns, CAP_SYS_NICE))
226 		return true;
227 	return false;
228 }
229 
230 /*
231  * set the priority of a task
232  * - the caller must hold the RCU read lock
233  */
234 static int set_one_prio(struct task_struct *p, int niceval, int error)
235 {
236 	int no_nice;
237 
238 	if (!set_one_prio_perm(p)) {
239 		error = -EPERM;
240 		goto out;
241 	}
242 	if (niceval < task_nice(p) && !can_nice(p, niceval)) {
243 		error = -EACCES;
244 		goto out;
245 	}
246 	no_nice = security_task_setnice(p, niceval);
247 	if (no_nice) {
248 		error = no_nice;
249 		goto out;
250 	}
251 	if (error == -ESRCH)
252 		error = 0;
253 	set_user_nice(p, niceval);
254 out:
255 	return error;
256 }
257 
258 SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
259 {
260 	struct task_struct *g, *p;
261 	struct user_struct *user;
262 	const struct cred *cred = current_cred();
263 	int error = -EINVAL;
264 	struct pid *pgrp;
265 	kuid_t uid;
266 
267 	if (which > PRIO_USER || which < PRIO_PROCESS)
268 		goto out;
269 
270 	/* normalize: avoid signed division (rounding problems) */
271 	error = -ESRCH;
272 	if (niceval < MIN_NICE)
273 		niceval = MIN_NICE;
274 	if (niceval > MAX_NICE)
275 		niceval = MAX_NICE;
276 
277 	rcu_read_lock();
278 	switch (which) {
279 	case PRIO_PROCESS:
280 		if (who)
281 			p = find_task_by_vpid(who);
282 		else
283 			p = current;
284 		if (p)
285 			error = set_one_prio(p, niceval, error);
286 		break;
287 	case PRIO_PGRP:
288 		if (who)
289 			pgrp = find_vpid(who);
290 		else
291 			pgrp = task_pgrp(current);
292 		read_lock(&tasklist_lock);
293 		do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
294 			error = set_one_prio(p, niceval, error);
295 		} while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
296 		read_unlock(&tasklist_lock);
297 		break;
298 	case PRIO_USER:
299 		uid = make_kuid(cred->user_ns, who);
300 		user = cred->user;
301 		if (!who)
302 			uid = cred->uid;
303 		else if (!uid_eq(uid, cred->uid)) {
304 			user = find_user(uid);
305 			if (!user)
306 				goto out_unlock;	/* No processes for this user */
307 		}
308 		for_each_process_thread(g, p) {
309 			if (uid_eq(task_uid(p), uid) && task_pid_vnr(p))
310 				error = set_one_prio(p, niceval, error);
311 		}
312 		if (!uid_eq(uid, cred->uid))
313 			free_uid(user);		/* For find_user() */
314 		break;
315 	}
316 out_unlock:
317 	rcu_read_unlock();
318 out:
319 	return error;
320 }
321 
322 /*
323  * Ugh. To avoid negative return values, "getpriority()" will
324  * not return the normal nice-value, but a negated value that
325  * has been offset by 20 (ie it returns 40..1 instead of -20..19)
326  * to stay compatible.
327  */
328 SYSCALL_DEFINE2(getpriority, int, which, int, who)
329 {
330 	struct task_struct *g, *p;
331 	struct user_struct *user;
332 	const struct cred *cred = current_cred();
333 	long niceval, retval = -ESRCH;
334 	struct pid *pgrp;
335 	kuid_t uid;
336 
337 	if (which > PRIO_USER || which < PRIO_PROCESS)
338 		return -EINVAL;
339 
340 	rcu_read_lock();
341 	switch (which) {
342 	case PRIO_PROCESS:
343 		if (who)
344 			p = find_task_by_vpid(who);
345 		else
346 			p = current;
347 		if (p) {
348 			niceval = nice_to_rlimit(task_nice(p));
349 			if (niceval > retval)
350 				retval = niceval;
351 		}
352 		break;
353 	case PRIO_PGRP:
354 		if (who)
355 			pgrp = find_vpid(who);
356 		else
357 			pgrp = task_pgrp(current);
358 		read_lock(&tasklist_lock);
359 		do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
360 			niceval = nice_to_rlimit(task_nice(p));
361 			if (niceval > retval)
362 				retval = niceval;
363 		} while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
364 		read_unlock(&tasklist_lock);
365 		break;
366 	case PRIO_USER:
367 		uid = make_kuid(cred->user_ns, who);
368 		user = cred->user;
369 		if (!who)
370 			uid = cred->uid;
371 		else if (!uid_eq(uid, cred->uid)) {
372 			user = find_user(uid);
373 			if (!user)
374 				goto out_unlock;	/* No processes for this user */
375 		}
376 		for_each_process_thread(g, p) {
377 			if (uid_eq(task_uid(p), uid) && task_pid_vnr(p)) {
378 				niceval = nice_to_rlimit(task_nice(p));
379 				if (niceval > retval)
380 					retval = niceval;
381 			}
382 		}
383 		if (!uid_eq(uid, cred->uid))
384 			free_uid(user);		/* for find_user() */
385 		break;
386 	}
387 out_unlock:
388 	rcu_read_unlock();
389 
390 	return retval;
391 }
392 
393 /*
394  * Unprivileged users may change the real gid to the effective gid
395  * or vice versa.  (BSD-style)
396  *
397  * If you set the real gid at all, or set the effective gid to a value not
398  * equal to the real gid, then the saved gid is set to the new effective gid.
399  *
400  * This makes it possible for a setgid program to completely drop its
401  * privileges, which is often a useful assertion to make when you are doing
402  * a security audit over a program.
403  *
404  * The general idea is that a program which uses just setregid() will be
405  * 100% compatible with BSD.  A program which uses just setgid() will be
406  * 100% compatible with POSIX with saved IDs.
407  *
408  * SMP: There are not races, the GIDs are checked only by filesystem
409  *      operations (as far as semantic preservation is concerned).
410  */
411 #ifdef CONFIG_MULTIUSER
412 long __sys_setregid(gid_t rgid, gid_t egid)
413 {
414 	struct user_namespace *ns = current_user_ns();
415 	const struct cred *old;
416 	struct cred *new;
417 	int retval;
418 	kgid_t krgid, kegid;
419 
420 	krgid = make_kgid(ns, rgid);
421 	kegid = make_kgid(ns, egid);
422 
423 	if ((rgid != (gid_t) -1) && !gid_valid(krgid))
424 		return -EINVAL;
425 	if ((egid != (gid_t) -1) && !gid_valid(kegid))
426 		return -EINVAL;
427 
428 	new = prepare_creds();
429 	if (!new)
430 		return -ENOMEM;
431 	old = current_cred();
432 
433 	retval = -EPERM;
434 	if (rgid != (gid_t) -1) {
435 		if (gid_eq(old->gid, krgid) ||
436 		    gid_eq(old->egid, krgid) ||
437 		    ns_capable_setid(old->user_ns, CAP_SETGID))
438 			new->gid = krgid;
439 		else
440 			goto error;
441 	}
442 	if (egid != (gid_t) -1) {
443 		if (gid_eq(old->gid, kegid) ||
444 		    gid_eq(old->egid, kegid) ||
445 		    gid_eq(old->sgid, kegid) ||
446 		    ns_capable_setid(old->user_ns, CAP_SETGID))
447 			new->egid = kegid;
448 		else
449 			goto error;
450 	}
451 
452 	if (rgid != (gid_t) -1 ||
453 	    (egid != (gid_t) -1 && !gid_eq(kegid, old->gid)))
454 		new->sgid = new->egid;
455 	new->fsgid = new->egid;
456 
457 	retval = security_task_fix_setgid(new, old, LSM_SETID_RE);
458 	if (retval < 0)
459 		goto error;
460 
461 	return commit_creds(new);
462 
463 error:
464 	abort_creds(new);
465 	return retval;
466 }
467 
468 SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
469 {
470 	return __sys_setregid(rgid, egid);
471 }
472 
473 /*
474  * setgid() is implemented like SysV w/ SAVED_IDS
475  *
476  * SMP: Same implicit races as above.
477  */
478 long __sys_setgid(gid_t gid)
479 {
480 	struct user_namespace *ns = current_user_ns();
481 	const struct cred *old;
482 	struct cred *new;
483 	int retval;
484 	kgid_t kgid;
485 
486 	kgid = make_kgid(ns, gid);
487 	if (!gid_valid(kgid))
488 		return -EINVAL;
489 
490 	new = prepare_creds();
491 	if (!new)
492 		return -ENOMEM;
493 	old = current_cred();
494 
495 	retval = -EPERM;
496 	if (ns_capable_setid(old->user_ns, CAP_SETGID))
497 		new->gid = new->egid = new->sgid = new->fsgid = kgid;
498 	else if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->sgid))
499 		new->egid = new->fsgid = kgid;
500 	else
501 		goto error;
502 
503 	retval = security_task_fix_setgid(new, old, LSM_SETID_ID);
504 	if (retval < 0)
505 		goto error;
506 
507 	return commit_creds(new);
508 
509 error:
510 	abort_creds(new);
511 	return retval;
512 }
513 
514 SYSCALL_DEFINE1(setgid, gid_t, gid)
515 {
516 	return __sys_setgid(gid);
517 }
518 
519 /*
520  * change the user struct in a credentials set to match the new UID
521  */
522 static int set_user(struct cred *new)
523 {
524 	struct user_struct *new_user;
525 
526 	new_user = alloc_uid(new->uid);
527 	if (!new_user)
528 		return -EAGAIN;
529 
530 	free_uid(new->user);
531 	new->user = new_user;
532 	return 0;
533 }
534 
535 static void flag_nproc_exceeded(struct cred *new)
536 {
537 	if (new->ucounts == current_ucounts())
538 		return;
539 
540 	/*
541 	 * We don't fail in case of NPROC limit excess here because too many
542 	 * poorly written programs don't check set*uid() return code, assuming
543 	 * it never fails if called by root.  We may still enforce NPROC limit
544 	 * for programs doing set*uid()+execve() by harmlessly deferring the
545 	 * failure to the execve() stage.
546 	 */
547 	if (is_rlimit_overlimit(new->ucounts, UCOUNT_RLIMIT_NPROC, rlimit(RLIMIT_NPROC)) &&
548 			new->user != INIT_USER)
549 		current->flags |= PF_NPROC_EXCEEDED;
550 	else
551 		current->flags &= ~PF_NPROC_EXCEEDED;
552 }
553 
554 /*
555  * Unprivileged users may change the real uid to the effective uid
556  * or vice versa.  (BSD-style)
557  *
558  * If you set the real uid at all, or set the effective uid to a value not
559  * equal to the real uid, then the saved uid is set to the new effective uid.
560  *
561  * This makes it possible for a setuid program to completely drop its
562  * privileges, which is often a useful assertion to make when you are doing
563  * a security audit over a program.
564  *
565  * The general idea is that a program which uses just setreuid() will be
566  * 100% compatible with BSD.  A program which uses just setuid() will be
567  * 100% compatible with POSIX with saved IDs.
568  */
569 long __sys_setreuid(uid_t ruid, uid_t euid)
570 {
571 	struct user_namespace *ns = current_user_ns();
572 	const struct cred *old;
573 	struct cred *new;
574 	int retval;
575 	kuid_t kruid, keuid;
576 
577 	kruid = make_kuid(ns, ruid);
578 	keuid = make_kuid(ns, euid);
579 
580 	if ((ruid != (uid_t) -1) && !uid_valid(kruid))
581 		return -EINVAL;
582 	if ((euid != (uid_t) -1) && !uid_valid(keuid))
583 		return -EINVAL;
584 
585 	new = prepare_creds();
586 	if (!new)
587 		return -ENOMEM;
588 	old = current_cred();
589 
590 	retval = -EPERM;
591 	if (ruid != (uid_t) -1) {
592 		new->uid = kruid;
593 		if (!uid_eq(old->uid, kruid) &&
594 		    !uid_eq(old->euid, kruid) &&
595 		    !ns_capable_setid(old->user_ns, CAP_SETUID))
596 			goto error;
597 	}
598 
599 	if (euid != (uid_t) -1) {
600 		new->euid = keuid;
601 		if (!uid_eq(old->uid, keuid) &&
602 		    !uid_eq(old->euid, keuid) &&
603 		    !uid_eq(old->suid, keuid) &&
604 		    !ns_capable_setid(old->user_ns, CAP_SETUID))
605 			goto error;
606 	}
607 
608 	if (!uid_eq(new->uid, old->uid)) {
609 		retval = set_user(new);
610 		if (retval < 0)
611 			goto error;
612 	}
613 	if (ruid != (uid_t) -1 ||
614 	    (euid != (uid_t) -1 && !uid_eq(keuid, old->uid)))
615 		new->suid = new->euid;
616 	new->fsuid = new->euid;
617 
618 	retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
619 	if (retval < 0)
620 		goto error;
621 
622 	retval = set_cred_ucounts(new);
623 	if (retval < 0)
624 		goto error;
625 
626 	flag_nproc_exceeded(new);
627 	return commit_creds(new);
628 
629 error:
630 	abort_creds(new);
631 	return retval;
632 }
633 
634 SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
635 {
636 	return __sys_setreuid(ruid, euid);
637 }
638 
639 /*
640  * setuid() is implemented like SysV with SAVED_IDS
641  *
642  * Note that SAVED_ID's is deficient in that a setuid root program
643  * like sendmail, for example, cannot set its uid to be a normal
644  * user and then switch back, because if you're root, setuid() sets
645  * the saved uid too.  If you don't like this, blame the bright people
646  * in the POSIX committee and/or USG.  Note that the BSD-style setreuid()
647  * will allow a root program to temporarily drop privileges and be able to
648  * regain them by swapping the real and effective uid.
649  */
650 long __sys_setuid(uid_t uid)
651 {
652 	struct user_namespace *ns = current_user_ns();
653 	const struct cred *old;
654 	struct cred *new;
655 	int retval;
656 	kuid_t kuid;
657 
658 	kuid = make_kuid(ns, uid);
659 	if (!uid_valid(kuid))
660 		return -EINVAL;
661 
662 	new = prepare_creds();
663 	if (!new)
664 		return -ENOMEM;
665 	old = current_cred();
666 
667 	retval = -EPERM;
668 	if (ns_capable_setid(old->user_ns, CAP_SETUID)) {
669 		new->suid = new->uid = kuid;
670 		if (!uid_eq(kuid, old->uid)) {
671 			retval = set_user(new);
672 			if (retval < 0)
673 				goto error;
674 		}
675 	} else if (!uid_eq(kuid, old->uid) && !uid_eq(kuid, new->suid)) {
676 		goto error;
677 	}
678 
679 	new->fsuid = new->euid = kuid;
680 
681 	retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
682 	if (retval < 0)
683 		goto error;
684 
685 	retval = set_cred_ucounts(new);
686 	if (retval < 0)
687 		goto error;
688 
689 	flag_nproc_exceeded(new);
690 	return commit_creds(new);
691 
692 error:
693 	abort_creds(new);
694 	return retval;
695 }
696 
697 SYSCALL_DEFINE1(setuid, uid_t, uid)
698 {
699 	return __sys_setuid(uid);
700 }
701 
702 
703 /*
704  * This function implements a generic ability to update ruid, euid,
705  * and suid.  This allows you to implement the 4.4 compatible seteuid().
706  */
707 long __sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
708 {
709 	struct user_namespace *ns = current_user_ns();
710 	const struct cred *old;
711 	struct cred *new;
712 	int retval;
713 	kuid_t kruid, keuid, ksuid;
714 	bool ruid_new, euid_new, suid_new;
715 
716 	kruid = make_kuid(ns, ruid);
717 	keuid = make_kuid(ns, euid);
718 	ksuid = make_kuid(ns, suid);
719 
720 	if ((ruid != (uid_t) -1) && !uid_valid(kruid))
721 		return -EINVAL;
722 
723 	if ((euid != (uid_t) -1) && !uid_valid(keuid))
724 		return -EINVAL;
725 
726 	if ((suid != (uid_t) -1) && !uid_valid(ksuid))
727 		return -EINVAL;
728 
729 	old = current_cred();
730 
731 	/* check for no-op */
732 	if ((ruid == (uid_t) -1 || uid_eq(kruid, old->uid)) &&
733 	    (euid == (uid_t) -1 || (uid_eq(keuid, old->euid) &&
734 				    uid_eq(keuid, old->fsuid))) &&
735 	    (suid == (uid_t) -1 || uid_eq(ksuid, old->suid)))
736 		return 0;
737 
738 	ruid_new = ruid != (uid_t) -1        && !uid_eq(kruid, old->uid) &&
739 		   !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid);
740 	euid_new = euid != (uid_t) -1        && !uid_eq(keuid, old->uid) &&
741 		   !uid_eq(keuid, old->euid) && !uid_eq(keuid, old->suid);
742 	suid_new = suid != (uid_t) -1        && !uid_eq(ksuid, old->uid) &&
743 		   !uid_eq(ksuid, old->euid) && !uid_eq(ksuid, old->suid);
744 	if ((ruid_new || euid_new || suid_new) &&
745 	    !ns_capable_setid(old->user_ns, CAP_SETUID))
746 		return -EPERM;
747 
748 	new = prepare_creds();
749 	if (!new)
750 		return -ENOMEM;
751 
752 	if (ruid != (uid_t) -1) {
753 		new->uid = kruid;
754 		if (!uid_eq(kruid, old->uid)) {
755 			retval = set_user(new);
756 			if (retval < 0)
757 				goto error;
758 		}
759 	}
760 	if (euid != (uid_t) -1)
761 		new->euid = keuid;
762 	if (suid != (uid_t) -1)
763 		new->suid = ksuid;
764 	new->fsuid = new->euid;
765 
766 	retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
767 	if (retval < 0)
768 		goto error;
769 
770 	retval = set_cred_ucounts(new);
771 	if (retval < 0)
772 		goto error;
773 
774 	flag_nproc_exceeded(new);
775 	return commit_creds(new);
776 
777 error:
778 	abort_creds(new);
779 	return retval;
780 }
781 
782 SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
783 {
784 	return __sys_setresuid(ruid, euid, suid);
785 }
786 
787 SYSCALL_DEFINE3(getresuid, uid_t __user *, ruidp, uid_t __user *, euidp, uid_t __user *, suidp)
788 {
789 	const struct cred *cred = current_cred();
790 	int retval;
791 	uid_t ruid, euid, suid;
792 
793 	ruid = from_kuid_munged(cred->user_ns, cred->uid);
794 	euid = from_kuid_munged(cred->user_ns, cred->euid);
795 	suid = from_kuid_munged(cred->user_ns, cred->suid);
796 
797 	retval = put_user(ruid, ruidp);
798 	if (!retval) {
799 		retval = put_user(euid, euidp);
800 		if (!retval)
801 			return put_user(suid, suidp);
802 	}
803 	return retval;
804 }
805 
806 /*
807  * Same as above, but for rgid, egid, sgid.
808  */
809 long __sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
810 {
811 	struct user_namespace *ns = current_user_ns();
812 	const struct cred *old;
813 	struct cred *new;
814 	int retval;
815 	kgid_t krgid, kegid, ksgid;
816 	bool rgid_new, egid_new, sgid_new;
817 
818 	krgid = make_kgid(ns, rgid);
819 	kegid = make_kgid(ns, egid);
820 	ksgid = make_kgid(ns, sgid);
821 
822 	if ((rgid != (gid_t) -1) && !gid_valid(krgid))
823 		return -EINVAL;
824 	if ((egid != (gid_t) -1) && !gid_valid(kegid))
825 		return -EINVAL;
826 	if ((sgid != (gid_t) -1) && !gid_valid(ksgid))
827 		return -EINVAL;
828 
829 	old = current_cred();
830 
831 	/* check for no-op */
832 	if ((rgid == (gid_t) -1 || gid_eq(krgid, old->gid)) &&
833 	    (egid == (gid_t) -1 || (gid_eq(kegid, old->egid) &&
834 				    gid_eq(kegid, old->fsgid))) &&
835 	    (sgid == (gid_t) -1 || gid_eq(ksgid, old->sgid)))
836 		return 0;
837 
838 	rgid_new = rgid != (gid_t) -1        && !gid_eq(krgid, old->gid) &&
839 		   !gid_eq(krgid, old->egid) && !gid_eq(krgid, old->sgid);
840 	egid_new = egid != (gid_t) -1        && !gid_eq(kegid, old->gid) &&
841 		   !gid_eq(kegid, old->egid) && !gid_eq(kegid, old->sgid);
842 	sgid_new = sgid != (gid_t) -1        && !gid_eq(ksgid, old->gid) &&
843 		   !gid_eq(ksgid, old->egid) && !gid_eq(ksgid, old->sgid);
844 	if ((rgid_new || egid_new || sgid_new) &&
845 	    !ns_capable_setid(old->user_ns, CAP_SETGID))
846 		return -EPERM;
847 
848 	new = prepare_creds();
849 	if (!new)
850 		return -ENOMEM;
851 
852 	if (rgid != (gid_t) -1)
853 		new->gid = krgid;
854 	if (egid != (gid_t) -1)
855 		new->egid = kegid;
856 	if (sgid != (gid_t) -1)
857 		new->sgid = ksgid;
858 	new->fsgid = new->egid;
859 
860 	retval = security_task_fix_setgid(new, old, LSM_SETID_RES);
861 	if (retval < 0)
862 		goto error;
863 
864 	return commit_creds(new);
865 
866 error:
867 	abort_creds(new);
868 	return retval;
869 }
870 
871 SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
872 {
873 	return __sys_setresgid(rgid, egid, sgid);
874 }
875 
876 SYSCALL_DEFINE3(getresgid, gid_t __user *, rgidp, gid_t __user *, egidp, gid_t __user *, sgidp)
877 {
878 	const struct cred *cred = current_cred();
879 	int retval;
880 	gid_t rgid, egid, sgid;
881 
882 	rgid = from_kgid_munged(cred->user_ns, cred->gid);
883 	egid = from_kgid_munged(cred->user_ns, cred->egid);
884 	sgid = from_kgid_munged(cred->user_ns, cred->sgid);
885 
886 	retval = put_user(rgid, rgidp);
887 	if (!retval) {
888 		retval = put_user(egid, egidp);
889 		if (!retval)
890 			retval = put_user(sgid, sgidp);
891 	}
892 
893 	return retval;
894 }
895 
896 
897 /*
898  * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
899  * is used for "access()" and for the NFS daemon (letting nfsd stay at
900  * whatever uid it wants to). It normally shadows "euid", except when
901  * explicitly set by setfsuid() or for access..
902  */
903 long __sys_setfsuid(uid_t uid)
904 {
905 	const struct cred *old;
906 	struct cred *new;
907 	uid_t old_fsuid;
908 	kuid_t kuid;
909 
910 	old = current_cred();
911 	old_fsuid = from_kuid_munged(old->user_ns, old->fsuid);
912 
913 	kuid = make_kuid(old->user_ns, uid);
914 	if (!uid_valid(kuid))
915 		return old_fsuid;
916 
917 	new = prepare_creds();
918 	if (!new)
919 		return old_fsuid;
920 
921 	if (uid_eq(kuid, old->uid)  || uid_eq(kuid, old->euid)  ||
922 	    uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) ||
923 	    ns_capable_setid(old->user_ns, CAP_SETUID)) {
924 		if (!uid_eq(kuid, old->fsuid)) {
925 			new->fsuid = kuid;
926 			if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
927 				goto change_okay;
928 		}
929 	}
930 
931 	abort_creds(new);
932 	return old_fsuid;
933 
934 change_okay:
935 	commit_creds(new);
936 	return old_fsuid;
937 }
938 
939 SYSCALL_DEFINE1(setfsuid, uid_t, uid)
940 {
941 	return __sys_setfsuid(uid);
942 }
943 
944 /*
945  * Samma på svenska..
946  */
947 long __sys_setfsgid(gid_t gid)
948 {
949 	const struct cred *old;
950 	struct cred *new;
951 	gid_t old_fsgid;
952 	kgid_t kgid;
953 
954 	old = current_cred();
955 	old_fsgid = from_kgid_munged(old->user_ns, old->fsgid);
956 
957 	kgid = make_kgid(old->user_ns, gid);
958 	if (!gid_valid(kgid))
959 		return old_fsgid;
960 
961 	new = prepare_creds();
962 	if (!new)
963 		return old_fsgid;
964 
965 	if (gid_eq(kgid, old->gid)  || gid_eq(kgid, old->egid)  ||
966 	    gid_eq(kgid, old->sgid) || gid_eq(kgid, old->fsgid) ||
967 	    ns_capable_setid(old->user_ns, CAP_SETGID)) {
968 		if (!gid_eq(kgid, old->fsgid)) {
969 			new->fsgid = kgid;
970 			if (security_task_fix_setgid(new,old,LSM_SETID_FS) == 0)
971 				goto change_okay;
972 		}
973 	}
974 
975 	abort_creds(new);
976 	return old_fsgid;
977 
978 change_okay:
979 	commit_creds(new);
980 	return old_fsgid;
981 }
982 
983 SYSCALL_DEFINE1(setfsgid, gid_t, gid)
984 {
985 	return __sys_setfsgid(gid);
986 }
987 #endif /* CONFIG_MULTIUSER */
988 
989 /**
990  * sys_getpid - return the thread group id of the current process
991  *
992  * Note, despite the name, this returns the tgid not the pid.  The tgid and
993  * the pid are identical unless CLONE_THREAD was specified on clone() in
994  * which case the tgid is the same in all threads of the same group.
995  *
996  * This is SMP safe as current->tgid does not change.
997  */
998 SYSCALL_DEFINE0(getpid)
999 {
1000 	return task_tgid_vnr(current);
1001 }
1002 
1003 /* Thread ID - the internal kernel "pid" */
1004 SYSCALL_DEFINE0(gettid)
1005 {
1006 	return task_pid_vnr(current);
1007 }
1008 
1009 /*
1010  * Accessing ->real_parent is not SMP-safe, it could
1011  * change from under us. However, we can use a stale
1012  * value of ->real_parent under rcu_read_lock(), see
1013  * release_task()->call_rcu(delayed_put_task_struct).
1014  */
1015 SYSCALL_DEFINE0(getppid)
1016 {
1017 	int pid;
1018 
1019 	rcu_read_lock();
1020 	pid = task_tgid_vnr(rcu_dereference(current->real_parent));
1021 	rcu_read_unlock();
1022 
1023 	return pid;
1024 }
1025 
1026 SYSCALL_DEFINE0(getuid)
1027 {
1028 	/* Only we change this so SMP safe */
1029 	return from_kuid_munged(current_user_ns(), current_uid());
1030 }
1031 
1032 SYSCALL_DEFINE0(geteuid)
1033 {
1034 	/* Only we change this so SMP safe */
1035 	return from_kuid_munged(current_user_ns(), current_euid());
1036 }
1037 
1038 SYSCALL_DEFINE0(getgid)
1039 {
1040 	/* Only we change this so SMP safe */
1041 	return from_kgid_munged(current_user_ns(), current_gid());
1042 }
1043 
1044 SYSCALL_DEFINE0(getegid)
1045 {
1046 	/* Only we change this so SMP safe */
1047 	return from_kgid_munged(current_user_ns(), current_egid());
1048 }
1049 
1050 static void do_sys_times(struct tms *tms)
1051 {
1052 	u64 tgutime, tgstime, cutime, cstime;
1053 
1054 	thread_group_cputime_adjusted(current, &tgutime, &tgstime);
1055 	cutime = current->signal->cutime;
1056 	cstime = current->signal->cstime;
1057 	tms->tms_utime = nsec_to_clock_t(tgutime);
1058 	tms->tms_stime = nsec_to_clock_t(tgstime);
1059 	tms->tms_cutime = nsec_to_clock_t(cutime);
1060 	tms->tms_cstime = nsec_to_clock_t(cstime);
1061 }
1062 
1063 SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
1064 {
1065 	if (tbuf) {
1066 		struct tms tmp;
1067 
1068 		do_sys_times(&tmp);
1069 		if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1070 			return -EFAULT;
1071 	}
1072 	force_successful_syscall_return();
1073 	return (long) jiffies_64_to_clock_t(get_jiffies_64());
1074 }
1075 
1076 #ifdef CONFIG_COMPAT
1077 static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
1078 {
1079 	return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
1080 }
1081 
1082 COMPAT_SYSCALL_DEFINE1(times, struct compat_tms __user *, tbuf)
1083 {
1084 	if (tbuf) {
1085 		struct tms tms;
1086 		struct compat_tms tmp;
1087 
1088 		do_sys_times(&tms);
1089 		/* Convert our struct tms to the compat version. */
1090 		tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
1091 		tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
1092 		tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
1093 		tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
1094 		if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
1095 			return -EFAULT;
1096 	}
1097 	force_successful_syscall_return();
1098 	return compat_jiffies_to_clock_t(jiffies);
1099 }
1100 #endif
1101 
1102 /*
1103  * This needs some heavy checking ...
1104  * I just haven't the stomach for it. I also don't fully
1105  * understand sessions/pgrp etc. Let somebody who does explain it.
1106  *
1107  * OK, I think I have the protection semantics right.... this is really
1108  * only important on a multi-user system anyway, to make sure one user
1109  * can't send a signal to a process owned by another.  -TYT, 12/12/91
1110  *
1111  * !PF_FORKNOEXEC check to conform completely to POSIX.
1112  */
1113 SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
1114 {
1115 	struct task_struct *p;
1116 	struct task_struct *group_leader = current->group_leader;
1117 	struct pid *pids[PIDTYPE_MAX] = { 0 };
1118 	struct pid *pgrp;
1119 	int err;
1120 
1121 	if (!pid)
1122 		pid = task_pid_vnr(group_leader);
1123 	if (!pgid)
1124 		pgid = pid;
1125 	if (pgid < 0)
1126 		return -EINVAL;
1127 	rcu_read_lock();
1128 
1129 	/* From this point forward we keep holding onto the tasklist lock
1130 	 * so that our parent does not change from under us. -DaveM
1131 	 */
1132 	write_lock_irq(&tasklist_lock);
1133 
1134 	err = -ESRCH;
1135 	p = find_task_by_vpid(pid);
1136 	if (!p)
1137 		goto out;
1138 
1139 	err = -EINVAL;
1140 	if (!thread_group_leader(p))
1141 		goto out;
1142 
1143 	if (same_thread_group(p->real_parent, group_leader)) {
1144 		err = -EPERM;
1145 		if (task_session(p) != task_session(group_leader))
1146 			goto out;
1147 		err = -EACCES;
1148 		if (!(p->flags & PF_FORKNOEXEC))
1149 			goto out;
1150 	} else {
1151 		err = -ESRCH;
1152 		if (p != group_leader)
1153 			goto out;
1154 	}
1155 
1156 	err = -EPERM;
1157 	if (p->signal->leader)
1158 		goto out;
1159 
1160 	pgrp = task_pid(p);
1161 	if (pgid != pid) {
1162 		struct task_struct *g;
1163 
1164 		pgrp = find_vpid(pgid);
1165 		g = pid_task(pgrp, PIDTYPE_PGID);
1166 		if (!g || task_session(g) != task_session(group_leader))
1167 			goto out;
1168 	}
1169 
1170 	err = security_task_setpgid(p, pgid);
1171 	if (err)
1172 		goto out;
1173 
1174 	if (task_pgrp(p) != pgrp)
1175 		change_pid(pids, p, PIDTYPE_PGID, pgrp);
1176 
1177 	err = 0;
1178 out:
1179 	/* All paths lead to here, thus we are safe. -DaveM */
1180 	write_unlock_irq(&tasklist_lock);
1181 	rcu_read_unlock();
1182 	free_pids(pids);
1183 	return err;
1184 }
1185 
1186 static int do_getpgid(pid_t pid)
1187 {
1188 	struct task_struct *p;
1189 	struct pid *grp;
1190 	int retval;
1191 
1192 	rcu_read_lock();
1193 	if (!pid)
1194 		grp = task_pgrp(current);
1195 	else {
1196 		retval = -ESRCH;
1197 		p = find_task_by_vpid(pid);
1198 		if (!p)
1199 			goto out;
1200 		grp = task_pgrp(p);
1201 		if (!grp)
1202 			goto out;
1203 
1204 		retval = security_task_getpgid(p);
1205 		if (retval)
1206 			goto out;
1207 	}
1208 	retval = pid_vnr(grp);
1209 out:
1210 	rcu_read_unlock();
1211 	return retval;
1212 }
1213 
1214 SYSCALL_DEFINE1(getpgid, pid_t, pid)
1215 {
1216 	return do_getpgid(pid);
1217 }
1218 
1219 #ifdef __ARCH_WANT_SYS_GETPGRP
1220 
1221 SYSCALL_DEFINE0(getpgrp)
1222 {
1223 	return do_getpgid(0);
1224 }
1225 
1226 #endif
1227 
1228 SYSCALL_DEFINE1(getsid, pid_t, pid)
1229 {
1230 	struct task_struct *p;
1231 	struct pid *sid;
1232 	int retval;
1233 
1234 	rcu_read_lock();
1235 	if (!pid)
1236 		sid = task_session(current);
1237 	else {
1238 		retval = -ESRCH;
1239 		p = find_task_by_vpid(pid);
1240 		if (!p)
1241 			goto out;
1242 		sid = task_session(p);
1243 		if (!sid)
1244 			goto out;
1245 
1246 		retval = security_task_getsid(p);
1247 		if (retval)
1248 			goto out;
1249 	}
1250 	retval = pid_vnr(sid);
1251 out:
1252 	rcu_read_unlock();
1253 	return retval;
1254 }
1255 
1256 static void set_special_pids(struct pid **pids, struct pid *pid)
1257 {
1258 	struct task_struct *curr = current->group_leader;
1259 
1260 	if (task_session(curr) != pid)
1261 		change_pid(pids, curr, PIDTYPE_SID, pid);
1262 
1263 	if (task_pgrp(curr) != pid)
1264 		change_pid(pids, curr, PIDTYPE_PGID, pid);
1265 }
1266 
1267 int ksys_setsid(void)
1268 {
1269 	struct task_struct *group_leader = current->group_leader;
1270 	struct pid *sid = task_pid(group_leader);
1271 	struct pid *pids[PIDTYPE_MAX] = { 0 };
1272 	pid_t session = pid_vnr(sid);
1273 	int err = -EPERM;
1274 
1275 	write_lock_irq(&tasklist_lock);
1276 	/* Fail if I am already a session leader */
1277 	if (group_leader->signal->leader)
1278 		goto out;
1279 
1280 	/* Fail if a process group id already exists that equals the
1281 	 * proposed session id.
1282 	 */
1283 	if (pid_task(sid, PIDTYPE_PGID))
1284 		goto out;
1285 
1286 	group_leader->signal->leader = 1;
1287 	set_special_pids(pids, sid);
1288 
1289 	proc_clear_tty(group_leader);
1290 
1291 	err = session;
1292 out:
1293 	write_unlock_irq(&tasklist_lock);
1294 	free_pids(pids);
1295 	if (err > 0) {
1296 		proc_sid_connector(group_leader);
1297 		sched_autogroup_create_attach(group_leader);
1298 	}
1299 	return err;
1300 }
1301 
1302 SYSCALL_DEFINE0(setsid)
1303 {
1304 	return ksys_setsid();
1305 }
1306 
1307 DECLARE_RWSEM(uts_sem);
1308 
1309 #ifdef COMPAT_UTS_MACHINE
1310 #define override_architecture(name) \
1311 	(personality(current->personality) == PER_LINUX32 && \
1312 	 copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1313 		      sizeof(COMPAT_UTS_MACHINE)))
1314 #else
1315 #define override_architecture(name)	0
1316 #endif
1317 
1318 /*
1319  * Work around broken programs that cannot handle "Linux 3.0".
1320  * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40
1321  * And we map 4.x and later versions to 2.6.60+x, so 4.0/5.0/6.0/... would be
1322  * 2.6.60.
1323  */
1324 static int override_release(char __user *release, size_t len)
1325 {
1326 	int ret = 0;
1327 
1328 	if (current->personality & UNAME26) {
1329 		const char *rest = UTS_RELEASE;
1330 		char buf[65] = { 0 };
1331 		int ndots = 0;
1332 		unsigned v;
1333 		size_t copy;
1334 
1335 		while (*rest) {
1336 			if (*rest == '.' && ++ndots >= 3)
1337 				break;
1338 			if (!isdigit(*rest) && *rest != '.')
1339 				break;
1340 			rest++;
1341 		}
1342 		v = LINUX_VERSION_PATCHLEVEL + 60;
1343 		copy = clamp_t(size_t, len, 1, sizeof(buf));
1344 		copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
1345 		ret = copy_to_user(release, buf, copy + 1);
1346 	}
1347 	return ret;
1348 }
1349 
1350 SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
1351 {
1352 	struct new_utsname tmp;
1353 
1354 	down_read(&uts_sem);
1355 	memcpy(&tmp, utsname(), sizeof(tmp));
1356 	up_read(&uts_sem);
1357 	if (copy_to_user(name, &tmp, sizeof(tmp)))
1358 		return -EFAULT;
1359 
1360 	if (override_release(name->release, sizeof(name->release)))
1361 		return -EFAULT;
1362 	if (override_architecture(name))
1363 		return -EFAULT;
1364 	return 0;
1365 }
1366 
1367 #ifdef __ARCH_WANT_SYS_OLD_UNAME
1368 /*
1369  * Old cruft
1370  */
1371 SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
1372 {
1373 	struct old_utsname tmp;
1374 
1375 	if (!name)
1376 		return -EFAULT;
1377 
1378 	down_read(&uts_sem);
1379 	memcpy(&tmp, utsname(), sizeof(tmp));
1380 	up_read(&uts_sem);
1381 	if (copy_to_user(name, &tmp, sizeof(tmp)))
1382 		return -EFAULT;
1383 
1384 	if (override_release(name->release, sizeof(name->release)))
1385 		return -EFAULT;
1386 	if (override_architecture(name))
1387 		return -EFAULT;
1388 	return 0;
1389 }
1390 
1391 SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
1392 {
1393 	struct oldold_utsname tmp;
1394 
1395 	if (!name)
1396 		return -EFAULT;
1397 
1398 	memset(&tmp, 0, sizeof(tmp));
1399 
1400 	down_read(&uts_sem);
1401 	memcpy(&tmp.sysname, &utsname()->sysname, __OLD_UTS_LEN);
1402 	memcpy(&tmp.nodename, &utsname()->nodename, __OLD_UTS_LEN);
1403 	memcpy(&tmp.release, &utsname()->release, __OLD_UTS_LEN);
1404 	memcpy(&tmp.version, &utsname()->version, __OLD_UTS_LEN);
1405 	memcpy(&tmp.machine, &utsname()->machine, __OLD_UTS_LEN);
1406 	up_read(&uts_sem);
1407 	if (copy_to_user(name, &tmp, sizeof(tmp)))
1408 		return -EFAULT;
1409 
1410 	if (override_architecture(name))
1411 		return -EFAULT;
1412 	if (override_release(name->release, sizeof(name->release)))
1413 		return -EFAULT;
1414 	return 0;
1415 }
1416 #endif
1417 
1418 SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
1419 {
1420 	int errno;
1421 	char tmp[__NEW_UTS_LEN];
1422 
1423 	if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
1424 		return -EPERM;
1425 
1426 	if (len < 0 || len > __NEW_UTS_LEN)
1427 		return -EINVAL;
1428 	errno = -EFAULT;
1429 	if (!copy_from_user(tmp, name, len)) {
1430 		struct new_utsname *u;
1431 
1432 		add_device_randomness(tmp, len);
1433 		down_write(&uts_sem);
1434 		u = utsname();
1435 		memcpy(u->nodename, tmp, len);
1436 		memset(u->nodename + len, 0, sizeof(u->nodename) - len);
1437 		errno = 0;
1438 		uts_proc_notify(UTS_PROC_HOSTNAME);
1439 		up_write(&uts_sem);
1440 	}
1441 	return errno;
1442 }
1443 
1444 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1445 
1446 SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
1447 {
1448 	int i;
1449 	struct new_utsname *u;
1450 	char tmp[__NEW_UTS_LEN + 1];
1451 
1452 	if (len < 0)
1453 		return -EINVAL;
1454 	down_read(&uts_sem);
1455 	u = utsname();
1456 	i = 1 + strlen(u->nodename);
1457 	if (i > len)
1458 		i = len;
1459 	memcpy(tmp, u->nodename, i);
1460 	up_read(&uts_sem);
1461 	if (copy_to_user(name, tmp, i))
1462 		return -EFAULT;
1463 	return 0;
1464 }
1465 
1466 #endif
1467 
1468 /*
1469  * Only setdomainname; getdomainname can be implemented by calling
1470  * uname()
1471  */
1472 SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
1473 {
1474 	int errno;
1475 	char tmp[__NEW_UTS_LEN];
1476 
1477 	if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
1478 		return -EPERM;
1479 	if (len < 0 || len > __NEW_UTS_LEN)
1480 		return -EINVAL;
1481 
1482 	errno = -EFAULT;
1483 	if (!copy_from_user(tmp, name, len)) {
1484 		struct new_utsname *u;
1485 
1486 		add_device_randomness(tmp, len);
1487 		down_write(&uts_sem);
1488 		u = utsname();
1489 		memcpy(u->domainname, tmp, len);
1490 		memset(u->domainname + len, 0, sizeof(u->domainname) - len);
1491 		errno = 0;
1492 		uts_proc_notify(UTS_PROC_DOMAINNAME);
1493 		up_write(&uts_sem);
1494 	}
1495 	return errno;
1496 }
1497 
1498 /* make sure you are allowed to change @tsk limits before calling this */
1499 static int do_prlimit(struct task_struct *tsk, unsigned int resource,
1500 		      struct rlimit *new_rlim, struct rlimit *old_rlim)
1501 {
1502 	struct rlimit *rlim;
1503 	int retval = 0;
1504 
1505 	if (resource >= RLIM_NLIMITS)
1506 		return -EINVAL;
1507 	resource = array_index_nospec(resource, RLIM_NLIMITS);
1508 
1509 	if (new_rlim) {
1510 		if (new_rlim->rlim_cur > new_rlim->rlim_max)
1511 			return -EINVAL;
1512 		if (resource == RLIMIT_NOFILE &&
1513 				new_rlim->rlim_max > sysctl_nr_open)
1514 			return -EPERM;
1515 	}
1516 
1517 	/* Holding a refcount on tsk protects tsk->signal from disappearing. */
1518 	rlim = tsk->signal->rlim + resource;
1519 	task_lock(tsk->group_leader);
1520 	if (new_rlim) {
1521 		/*
1522 		 * Keep the capable check against init_user_ns until cgroups can
1523 		 * contain all limits.
1524 		 */
1525 		if (new_rlim->rlim_max > rlim->rlim_max &&
1526 				!capable(CAP_SYS_RESOURCE))
1527 			retval = -EPERM;
1528 		if (!retval)
1529 			retval = security_task_setrlimit(tsk, resource, new_rlim);
1530 	}
1531 	if (!retval) {
1532 		if (old_rlim)
1533 			*old_rlim = *rlim;
1534 		if (new_rlim)
1535 			*rlim = *new_rlim;
1536 	}
1537 	task_unlock(tsk->group_leader);
1538 
1539 	/*
1540 	 * RLIMIT_CPU handling. Arm the posix CPU timer if the limit is not
1541 	 * infinite. In case of RLIM_INFINITY the posix CPU timer code
1542 	 * ignores the rlimit.
1543 	 */
1544 	if (!retval && new_rlim && resource == RLIMIT_CPU &&
1545 	    new_rlim->rlim_cur != RLIM_INFINITY &&
1546 	    IS_ENABLED(CONFIG_POSIX_TIMERS)) {
1547 		/*
1548 		 * update_rlimit_cpu can fail if the task is exiting, but there
1549 		 * may be other tasks in the thread group that are not exiting,
1550 		 * and they need their cpu timers adjusted.
1551 		 *
1552 		 * The group_leader is the last task to be released, so if we
1553 		 * cannot update_rlimit_cpu on it, then the entire process is
1554 		 * exiting and we do not need to update at all.
1555 		 */
1556 		update_rlimit_cpu(tsk->group_leader, new_rlim->rlim_cur);
1557 	}
1558 
1559 	return retval;
1560 }
1561 
1562 SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1563 {
1564 	struct rlimit value;
1565 	int ret;
1566 
1567 	ret = do_prlimit(current, resource, NULL, &value);
1568 	if (!ret)
1569 		ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1570 
1571 	return ret;
1572 }
1573 
1574 #ifdef CONFIG_COMPAT
1575 
1576 COMPAT_SYSCALL_DEFINE2(setrlimit, unsigned int, resource,
1577 		       struct compat_rlimit __user *, rlim)
1578 {
1579 	struct rlimit r;
1580 	struct compat_rlimit r32;
1581 
1582 	if (copy_from_user(&r32, rlim, sizeof(struct compat_rlimit)))
1583 		return -EFAULT;
1584 
1585 	if (r32.rlim_cur == COMPAT_RLIM_INFINITY)
1586 		r.rlim_cur = RLIM_INFINITY;
1587 	else
1588 		r.rlim_cur = r32.rlim_cur;
1589 	if (r32.rlim_max == COMPAT_RLIM_INFINITY)
1590 		r.rlim_max = RLIM_INFINITY;
1591 	else
1592 		r.rlim_max = r32.rlim_max;
1593 	return do_prlimit(current, resource, &r, NULL);
1594 }
1595 
1596 COMPAT_SYSCALL_DEFINE2(getrlimit, unsigned int, resource,
1597 		       struct compat_rlimit __user *, rlim)
1598 {
1599 	struct rlimit r;
1600 	int ret;
1601 
1602 	ret = do_prlimit(current, resource, NULL, &r);
1603 	if (!ret) {
1604 		struct compat_rlimit r32;
1605 		if (r.rlim_cur > COMPAT_RLIM_INFINITY)
1606 			r32.rlim_cur = COMPAT_RLIM_INFINITY;
1607 		else
1608 			r32.rlim_cur = r.rlim_cur;
1609 		if (r.rlim_max > COMPAT_RLIM_INFINITY)
1610 			r32.rlim_max = COMPAT_RLIM_INFINITY;
1611 		else
1612 			r32.rlim_max = r.rlim_max;
1613 
1614 		if (copy_to_user(rlim, &r32, sizeof(struct compat_rlimit)))
1615 			return -EFAULT;
1616 	}
1617 	return ret;
1618 }
1619 
1620 #endif
1621 
1622 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1623 
1624 /*
1625  *	Back compatibility for getrlimit. Needed for some apps.
1626  */
1627 SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1628 		struct rlimit __user *, rlim)
1629 {
1630 	struct rlimit x;
1631 	if (resource >= RLIM_NLIMITS)
1632 		return -EINVAL;
1633 
1634 	resource = array_index_nospec(resource, RLIM_NLIMITS);
1635 	task_lock(current->group_leader);
1636 	x = current->signal->rlim[resource];
1637 	task_unlock(current->group_leader);
1638 	if (x.rlim_cur > 0x7FFFFFFF)
1639 		x.rlim_cur = 0x7FFFFFFF;
1640 	if (x.rlim_max > 0x7FFFFFFF)
1641 		x.rlim_max = 0x7FFFFFFF;
1642 	return copy_to_user(rlim, &x, sizeof(x)) ? -EFAULT : 0;
1643 }
1644 
1645 #ifdef CONFIG_COMPAT
1646 COMPAT_SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1647 		       struct compat_rlimit __user *, rlim)
1648 {
1649 	struct rlimit r;
1650 
1651 	if (resource >= RLIM_NLIMITS)
1652 		return -EINVAL;
1653 
1654 	resource = array_index_nospec(resource, RLIM_NLIMITS);
1655 	task_lock(current->group_leader);
1656 	r = current->signal->rlim[resource];
1657 	task_unlock(current->group_leader);
1658 	if (r.rlim_cur > 0x7FFFFFFF)
1659 		r.rlim_cur = 0x7FFFFFFF;
1660 	if (r.rlim_max > 0x7FFFFFFF)
1661 		r.rlim_max = 0x7FFFFFFF;
1662 
1663 	if (put_user(r.rlim_cur, &rlim->rlim_cur) ||
1664 	    put_user(r.rlim_max, &rlim->rlim_max))
1665 		return -EFAULT;
1666 	return 0;
1667 }
1668 #endif
1669 
1670 #endif
1671 
1672 static inline bool rlim64_is_infinity(__u64 rlim64)
1673 {
1674 #if BITS_PER_LONG < 64
1675 	return rlim64 >= ULONG_MAX;
1676 #else
1677 	return rlim64 == RLIM64_INFINITY;
1678 #endif
1679 }
1680 
1681 static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1682 {
1683 	if (rlim->rlim_cur == RLIM_INFINITY)
1684 		rlim64->rlim_cur = RLIM64_INFINITY;
1685 	else
1686 		rlim64->rlim_cur = rlim->rlim_cur;
1687 	if (rlim->rlim_max == RLIM_INFINITY)
1688 		rlim64->rlim_max = RLIM64_INFINITY;
1689 	else
1690 		rlim64->rlim_max = rlim->rlim_max;
1691 }
1692 
1693 static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1694 {
1695 	if (rlim64_is_infinity(rlim64->rlim_cur))
1696 		rlim->rlim_cur = RLIM_INFINITY;
1697 	else
1698 		rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1699 	if (rlim64_is_infinity(rlim64->rlim_max))
1700 		rlim->rlim_max = RLIM_INFINITY;
1701 	else
1702 		rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1703 }
1704 
1705 /* rcu lock must be held */
1706 static int check_prlimit_permission(struct task_struct *task,
1707 				    unsigned int flags)
1708 {
1709 	const struct cred *cred = current_cred(), *tcred;
1710 	bool id_match;
1711 
1712 	if (current == task)
1713 		return 0;
1714 
1715 	tcred = __task_cred(task);
1716 	id_match = (uid_eq(cred->uid, tcred->euid) &&
1717 		    uid_eq(cred->uid, tcred->suid) &&
1718 		    uid_eq(cred->uid, tcred->uid)  &&
1719 		    gid_eq(cred->gid, tcred->egid) &&
1720 		    gid_eq(cred->gid, tcred->sgid) &&
1721 		    gid_eq(cred->gid, tcred->gid));
1722 	if (!id_match && !ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
1723 		return -EPERM;
1724 
1725 	return security_task_prlimit(cred, tcred, flags);
1726 }
1727 
1728 SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1729 		const struct rlimit64 __user *, new_rlim,
1730 		struct rlimit64 __user *, old_rlim)
1731 {
1732 	struct rlimit64 old64, new64;
1733 	struct rlimit old, new;
1734 	struct task_struct *tsk;
1735 	unsigned int checkflags = 0;
1736 	bool need_tasklist;
1737 	int ret;
1738 
1739 	if (old_rlim)
1740 		checkflags |= LSM_PRLIMIT_READ;
1741 
1742 	if (new_rlim) {
1743 		if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1744 			return -EFAULT;
1745 		rlim64_to_rlim(&new64, &new);
1746 		checkflags |= LSM_PRLIMIT_WRITE;
1747 	}
1748 
1749 	rcu_read_lock();
1750 	tsk = pid ? find_task_by_vpid(pid) : current;
1751 	if (!tsk) {
1752 		rcu_read_unlock();
1753 		return -ESRCH;
1754 	}
1755 	ret = check_prlimit_permission(tsk, checkflags);
1756 	if (ret) {
1757 		rcu_read_unlock();
1758 		return ret;
1759 	}
1760 	get_task_struct(tsk);
1761 	rcu_read_unlock();
1762 
1763 	need_tasklist = !same_thread_group(tsk, current);
1764 	if (need_tasklist) {
1765 		/*
1766 		 * Ensure we can't race with group exit or de_thread(),
1767 		 * so tsk->group_leader can't be freed or changed until
1768 		 * read_unlock(tasklist_lock) below.
1769 		 */
1770 		read_lock(&tasklist_lock);
1771 		if (!pid_alive(tsk))
1772 			ret = -ESRCH;
1773 	}
1774 
1775 	if (!ret) {
1776 		ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1777 				old_rlim ? &old : NULL);
1778 	}
1779 
1780 	if (need_tasklist)
1781 		read_unlock(&tasklist_lock);
1782 
1783 	if (!ret && old_rlim) {
1784 		rlim_to_rlim64(&old, &old64);
1785 		if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1786 			ret = -EFAULT;
1787 	}
1788 
1789 	put_task_struct(tsk);
1790 	return ret;
1791 }
1792 
1793 SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1794 {
1795 	struct rlimit new_rlim;
1796 
1797 	if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1798 		return -EFAULT;
1799 	return do_prlimit(current, resource, &new_rlim, NULL);
1800 }
1801 
1802 /*
1803  * It would make sense to put struct rusage in the task_struct,
1804  * except that would make the task_struct be *really big*.  After
1805  * task_struct gets moved into malloc'ed memory, it would
1806  * make sense to do this.  It will make moving the rest of the information
1807  * a lot simpler!  (Which we're not doing right now because we're not
1808  * measuring them yet).
1809  *
1810  * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1811  * races with threads incrementing their own counters.  But since word
1812  * reads are atomic, we either get new values or old values and we don't
1813  * care which for the sums.  We always take the siglock to protect reading
1814  * the c* fields from p->signal from races with exit.c updating those
1815  * fields when reaping, so a sample either gets all the additions of a
1816  * given child after it's reaped, or none so this sample is before reaping.
1817  *
1818  * Locking:
1819  * We need to take the siglock for CHILDEREN, SELF and BOTH
1820  * for  the cases current multithreaded, non-current single threaded
1821  * non-current multithreaded.  Thread traversal is now safe with
1822  * the siglock held.
1823  * Strictly speaking, we donot need to take the siglock if we are current and
1824  * single threaded,  as no one else can take our signal_struct away, no one
1825  * else can  reap the  children to update signal->c* counters, and no one else
1826  * can race with the signal-> fields. If we do not take any lock, the
1827  * signal-> fields could be read out of order while another thread was just
1828  * exiting. So we should  place a read memory barrier when we avoid the lock.
1829  * On the writer side,  write memory barrier is implied in  __exit_signal
1830  * as __exit_signal releases  the siglock spinlock after updating the signal->
1831  * fields. But we don't do this yet to keep things simple.
1832  *
1833  */
1834 
1835 static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
1836 {
1837 	r->ru_nvcsw += t->nvcsw;
1838 	r->ru_nivcsw += t->nivcsw;
1839 	r->ru_minflt += t->min_flt;
1840 	r->ru_majflt += t->maj_flt;
1841 	r->ru_inblock += task_io_get_inblock(t);
1842 	r->ru_oublock += task_io_get_oublock(t);
1843 }
1844 
1845 void getrusage(struct task_struct *p, int who, struct rusage *r)
1846 {
1847 	struct task_struct *t;
1848 	unsigned long flags;
1849 	u64 tgutime, tgstime, utime, stime;
1850 	unsigned long maxrss;
1851 	struct mm_struct *mm;
1852 	struct signal_struct *sig = p->signal;
1853 	unsigned int seq = 0;
1854 
1855 retry:
1856 	memset(r, 0, sizeof(*r));
1857 	utime = stime = 0;
1858 	maxrss = 0;
1859 
1860 	if (who == RUSAGE_THREAD) {
1861 		task_cputime_adjusted(current, &utime, &stime);
1862 		accumulate_thread_rusage(p, r);
1863 		maxrss = sig->maxrss;
1864 		goto out_thread;
1865 	}
1866 
1867 	flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
1868 
1869 	switch (who) {
1870 	case RUSAGE_BOTH:
1871 	case RUSAGE_CHILDREN:
1872 		utime = sig->cutime;
1873 		stime = sig->cstime;
1874 		r->ru_nvcsw = sig->cnvcsw;
1875 		r->ru_nivcsw = sig->cnivcsw;
1876 		r->ru_minflt = sig->cmin_flt;
1877 		r->ru_majflt = sig->cmaj_flt;
1878 		r->ru_inblock = sig->cinblock;
1879 		r->ru_oublock = sig->coublock;
1880 		maxrss = sig->cmaxrss;
1881 
1882 		if (who == RUSAGE_CHILDREN)
1883 			break;
1884 		fallthrough;
1885 
1886 	case RUSAGE_SELF:
1887 		r->ru_nvcsw += sig->nvcsw;
1888 		r->ru_nivcsw += sig->nivcsw;
1889 		r->ru_minflt += sig->min_flt;
1890 		r->ru_majflt += sig->maj_flt;
1891 		r->ru_inblock += sig->inblock;
1892 		r->ru_oublock += sig->oublock;
1893 		if (maxrss < sig->maxrss)
1894 			maxrss = sig->maxrss;
1895 
1896 		rcu_read_lock();
1897 		__for_each_thread(sig, t)
1898 			accumulate_thread_rusage(t, r);
1899 		rcu_read_unlock();
1900 
1901 		break;
1902 
1903 	default:
1904 		BUG();
1905 	}
1906 
1907 	if (need_seqretry(&sig->stats_lock, seq)) {
1908 		seq = 1;
1909 		goto retry;
1910 	}
1911 	done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
1912 
1913 	if (who == RUSAGE_CHILDREN)
1914 		goto out_children;
1915 
1916 	thread_group_cputime_adjusted(p, &tgutime, &tgstime);
1917 	utime += tgutime;
1918 	stime += tgstime;
1919 
1920 out_thread:
1921 	mm = get_task_mm(p);
1922 	if (mm) {
1923 		setmax_mm_hiwater_rss(&maxrss, mm);
1924 		mmput(mm);
1925 	}
1926 
1927 out_children:
1928 	r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
1929 	r->ru_utime = ns_to_kernel_old_timeval(utime);
1930 	r->ru_stime = ns_to_kernel_old_timeval(stime);
1931 }
1932 
1933 SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
1934 {
1935 	struct rusage r;
1936 
1937 	if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1938 	    who != RUSAGE_THREAD)
1939 		return -EINVAL;
1940 
1941 	getrusage(current, who, &r);
1942 	return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1943 }
1944 
1945 #ifdef CONFIG_COMPAT
1946 COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru)
1947 {
1948 	struct rusage r;
1949 
1950 	if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1951 	    who != RUSAGE_THREAD)
1952 		return -EINVAL;
1953 
1954 	getrusage(current, who, &r);
1955 	return put_compat_rusage(&r, ru);
1956 }
1957 #endif
1958 
1959 SYSCALL_DEFINE1(umask, int, mask)
1960 {
1961 	mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1962 	return mask;
1963 }
1964 
1965 static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
1966 {
1967 	CLASS(fd, exe)(fd);
1968 	struct inode *inode;
1969 	int err;
1970 
1971 	if (fd_empty(exe))
1972 		return -EBADF;
1973 
1974 	inode = file_inode(fd_file(exe));
1975 
1976 	/*
1977 	 * Because the original mm->exe_file points to executable file, make
1978 	 * sure that this one is executable as well, to avoid breaking an
1979 	 * overall picture.
1980 	 */
1981 	if (!S_ISREG(inode->i_mode) || path_noexec(&fd_file(exe)->f_path))
1982 		return -EACCES;
1983 
1984 	err = file_permission(fd_file(exe), MAY_EXEC);
1985 	if (err)
1986 		return err;
1987 
1988 	return replace_mm_exe_file(mm, fd_file(exe));
1989 }
1990 
1991 /*
1992  * Check arithmetic relations of passed addresses.
1993  *
1994  * WARNING: we don't require any capability here so be very careful
1995  * in what is allowed for modification from userspace.
1996  */
1997 static int validate_prctl_map_addr(struct prctl_mm_map *prctl_map)
1998 {
1999 	unsigned long mmap_max_addr = TASK_SIZE;
2000 	int error = -EINVAL, i;
2001 
2002 	static const unsigned char offsets[] = {
2003 		offsetof(struct prctl_mm_map, start_code),
2004 		offsetof(struct prctl_mm_map, end_code),
2005 		offsetof(struct prctl_mm_map, start_data),
2006 		offsetof(struct prctl_mm_map, end_data),
2007 		offsetof(struct prctl_mm_map, start_brk),
2008 		offsetof(struct prctl_mm_map, brk),
2009 		offsetof(struct prctl_mm_map, start_stack),
2010 		offsetof(struct prctl_mm_map, arg_start),
2011 		offsetof(struct prctl_mm_map, arg_end),
2012 		offsetof(struct prctl_mm_map, env_start),
2013 		offsetof(struct prctl_mm_map, env_end),
2014 	};
2015 
2016 	/*
2017 	 * Make sure the members are not somewhere outside
2018 	 * of allowed address space.
2019 	 */
2020 	for (i = 0; i < ARRAY_SIZE(offsets); i++) {
2021 		u64 val = *(u64 *)((char *)prctl_map + offsets[i]);
2022 
2023 		if ((unsigned long)val >= mmap_max_addr ||
2024 		    (unsigned long)val < mmap_min_addr)
2025 			goto out;
2026 	}
2027 
2028 	/*
2029 	 * Make sure the pairs are ordered.
2030 	 */
2031 #define __prctl_check_order(__m1, __op, __m2)				\
2032 	((unsigned long)prctl_map->__m1 __op				\
2033 	 (unsigned long)prctl_map->__m2) ? 0 : -EINVAL
2034 	error  = __prctl_check_order(start_code, <, end_code);
2035 	error |= __prctl_check_order(start_data,<=, end_data);
2036 	error |= __prctl_check_order(start_brk, <=, brk);
2037 	error |= __prctl_check_order(arg_start, <=, arg_end);
2038 	error |= __prctl_check_order(env_start, <=, env_end);
2039 	if (error)
2040 		goto out;
2041 #undef __prctl_check_order
2042 
2043 	error = -EINVAL;
2044 
2045 	/*
2046 	 * Neither we should allow to override limits if they set.
2047 	 */
2048 	if (check_data_rlimit(rlimit(RLIMIT_DATA), prctl_map->brk,
2049 			      prctl_map->start_brk, prctl_map->end_data,
2050 			      prctl_map->start_data))
2051 			goto out;
2052 
2053 	error = 0;
2054 out:
2055 	return error;
2056 }
2057 
2058 #ifdef CONFIG_CHECKPOINT_RESTORE
2059 static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data_size)
2060 {
2061 	struct prctl_mm_map prctl_map = { .exe_fd = (u32)-1, };
2062 	unsigned long user_auxv[AT_VECTOR_SIZE];
2063 	struct mm_struct *mm = current->mm;
2064 	int error;
2065 
2066 	BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
2067 	BUILD_BUG_ON(sizeof(struct prctl_mm_map) > 256);
2068 
2069 	if (opt == PR_SET_MM_MAP_SIZE)
2070 		return put_user((unsigned int)sizeof(prctl_map),
2071 				(unsigned int __user *)addr);
2072 
2073 	if (data_size != sizeof(prctl_map))
2074 		return -EINVAL;
2075 
2076 	if (copy_from_user(&prctl_map, addr, sizeof(prctl_map)))
2077 		return -EFAULT;
2078 
2079 	error = validate_prctl_map_addr(&prctl_map);
2080 	if (error)
2081 		return error;
2082 
2083 	if (prctl_map.auxv_size) {
2084 		/*
2085 		 * Someone is trying to cheat the auxv vector.
2086 		 */
2087 		if (!prctl_map.auxv ||
2088 				prctl_map.auxv_size > sizeof(mm->saved_auxv))
2089 			return -EINVAL;
2090 
2091 		memset(user_auxv, 0, sizeof(user_auxv));
2092 		if (copy_from_user(user_auxv,
2093 				   (const void __user *)prctl_map.auxv,
2094 				   prctl_map.auxv_size))
2095 			return -EFAULT;
2096 
2097 		/* Last entry must be AT_NULL as specification requires */
2098 		user_auxv[AT_VECTOR_SIZE - 2] = AT_NULL;
2099 		user_auxv[AT_VECTOR_SIZE - 1] = AT_NULL;
2100 	}
2101 
2102 	if (prctl_map.exe_fd != (u32)-1) {
2103 		/*
2104 		 * Check if the current user is checkpoint/restore capable.
2105 		 * At the time of this writing, it checks for CAP_SYS_ADMIN
2106 		 * or CAP_CHECKPOINT_RESTORE.
2107 		 * Note that a user with access to ptrace can masquerade an
2108 		 * arbitrary program as any executable, even setuid ones.
2109 		 * This may have implications in the tomoyo subsystem.
2110 		 */
2111 		if (!checkpoint_restore_ns_capable(current_user_ns()))
2112 			return -EPERM;
2113 
2114 		error = prctl_set_mm_exe_file(mm, prctl_map.exe_fd);
2115 		if (error)
2116 			return error;
2117 	}
2118 
2119 	/*
2120 	 * arg_lock protects concurrent updates but we still need mmap_lock for
2121 	 * read to exclude races with sys_brk.
2122 	 */
2123 	mmap_read_lock(mm);
2124 
2125 	/*
2126 	 * We don't validate if these members are pointing to
2127 	 * real present VMAs because application may have correspond
2128 	 * VMAs already unmapped and kernel uses these members for statistics
2129 	 * output in procfs mostly, except
2130 	 *
2131 	 *  - @start_brk/@brk which are used in do_brk_flags but kernel lookups
2132 	 *    for VMAs when updating these members so anything wrong written
2133 	 *    here cause kernel to swear at userspace program but won't lead
2134 	 *    to any problem in kernel itself
2135 	 */
2136 
2137 	spin_lock(&mm->arg_lock);
2138 	mm->start_code	= prctl_map.start_code;
2139 	mm->end_code	= prctl_map.end_code;
2140 	mm->start_data	= prctl_map.start_data;
2141 	mm->end_data	= prctl_map.end_data;
2142 	mm->start_brk	= prctl_map.start_brk;
2143 	mm->brk		= prctl_map.brk;
2144 	mm->start_stack	= prctl_map.start_stack;
2145 	mm->arg_start	= prctl_map.arg_start;
2146 	mm->arg_end	= prctl_map.arg_end;
2147 	mm->env_start	= prctl_map.env_start;
2148 	mm->env_end	= prctl_map.env_end;
2149 	spin_unlock(&mm->arg_lock);
2150 
2151 	/*
2152 	 * Note this update of @saved_auxv is lockless thus
2153 	 * if someone reads this member in procfs while we're
2154 	 * updating -- it may get partly updated results. It's
2155 	 * known and acceptable trade off: we leave it as is to
2156 	 * not introduce additional locks here making the kernel
2157 	 * more complex.
2158 	 */
2159 	if (prctl_map.auxv_size)
2160 		memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
2161 
2162 	mmap_read_unlock(mm);
2163 	return 0;
2164 }
2165 #endif /* CONFIG_CHECKPOINT_RESTORE */
2166 
2167 static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr,
2168 			  unsigned long len)
2169 {
2170 	/*
2171 	 * This doesn't move the auxiliary vector itself since it's pinned to
2172 	 * mm_struct, but it permits filling the vector with new values.  It's
2173 	 * up to the caller to provide sane values here, otherwise userspace
2174 	 * tools which use this vector might be unhappy.
2175 	 */
2176 	unsigned long user_auxv[AT_VECTOR_SIZE] = {};
2177 
2178 	if (len > sizeof(user_auxv))
2179 		return -EINVAL;
2180 
2181 	if (copy_from_user(user_auxv, (const void __user *)addr, len))
2182 		return -EFAULT;
2183 
2184 	/* Make sure the last entry is always AT_NULL */
2185 	user_auxv[AT_VECTOR_SIZE - 2] = 0;
2186 	user_auxv[AT_VECTOR_SIZE - 1] = 0;
2187 
2188 	BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
2189 
2190 	task_lock(current);
2191 	memcpy(mm->saved_auxv, user_auxv, len);
2192 	task_unlock(current);
2193 
2194 	return 0;
2195 }
2196 
2197 static int prctl_set_mm(int opt, unsigned long addr,
2198 			unsigned long arg4, unsigned long arg5)
2199 {
2200 	struct mm_struct *mm = current->mm;
2201 	struct prctl_mm_map prctl_map = {
2202 		.auxv = NULL,
2203 		.auxv_size = 0,
2204 		.exe_fd = -1,
2205 	};
2206 	struct vm_area_struct *vma;
2207 	int error;
2208 
2209 	if (arg5 || (arg4 && (opt != PR_SET_MM_AUXV &&
2210 			      opt != PR_SET_MM_MAP &&
2211 			      opt != PR_SET_MM_MAP_SIZE)))
2212 		return -EINVAL;
2213 
2214 #ifdef CONFIG_CHECKPOINT_RESTORE
2215 	if (opt == PR_SET_MM_MAP || opt == PR_SET_MM_MAP_SIZE)
2216 		return prctl_set_mm_map(opt, (const void __user *)addr, arg4);
2217 #endif
2218 
2219 	if (!capable(CAP_SYS_RESOURCE))
2220 		return -EPERM;
2221 
2222 	if (opt == PR_SET_MM_EXE_FILE)
2223 		return prctl_set_mm_exe_file(mm, (unsigned int)addr);
2224 
2225 	if (opt == PR_SET_MM_AUXV)
2226 		return prctl_set_auxv(mm, addr, arg4);
2227 
2228 	if (addr >= TASK_SIZE || addr < mmap_min_addr)
2229 		return -EINVAL;
2230 
2231 	error = -EINVAL;
2232 
2233 	/*
2234 	 * arg_lock protects concurrent updates of arg boundaries, we need
2235 	 * mmap_lock for a) concurrent sys_brk, b) finding VMA for addr
2236 	 * validation.
2237 	 */
2238 	mmap_read_lock(mm);
2239 	vma = find_vma(mm, addr);
2240 
2241 	spin_lock(&mm->arg_lock);
2242 	prctl_map.start_code	= mm->start_code;
2243 	prctl_map.end_code	= mm->end_code;
2244 	prctl_map.start_data	= mm->start_data;
2245 	prctl_map.end_data	= mm->end_data;
2246 	prctl_map.start_brk	= mm->start_brk;
2247 	prctl_map.brk		= mm->brk;
2248 	prctl_map.start_stack	= mm->start_stack;
2249 	prctl_map.arg_start	= mm->arg_start;
2250 	prctl_map.arg_end	= mm->arg_end;
2251 	prctl_map.env_start	= mm->env_start;
2252 	prctl_map.env_end	= mm->env_end;
2253 
2254 	switch (opt) {
2255 	case PR_SET_MM_START_CODE:
2256 		prctl_map.start_code = addr;
2257 		break;
2258 	case PR_SET_MM_END_CODE:
2259 		prctl_map.end_code = addr;
2260 		break;
2261 	case PR_SET_MM_START_DATA:
2262 		prctl_map.start_data = addr;
2263 		break;
2264 	case PR_SET_MM_END_DATA:
2265 		prctl_map.end_data = addr;
2266 		break;
2267 	case PR_SET_MM_START_STACK:
2268 		prctl_map.start_stack = addr;
2269 		break;
2270 	case PR_SET_MM_START_BRK:
2271 		prctl_map.start_brk = addr;
2272 		break;
2273 	case PR_SET_MM_BRK:
2274 		prctl_map.brk = addr;
2275 		break;
2276 	case PR_SET_MM_ARG_START:
2277 		prctl_map.arg_start = addr;
2278 		break;
2279 	case PR_SET_MM_ARG_END:
2280 		prctl_map.arg_end = addr;
2281 		break;
2282 	case PR_SET_MM_ENV_START:
2283 		prctl_map.env_start = addr;
2284 		break;
2285 	case PR_SET_MM_ENV_END:
2286 		prctl_map.env_end = addr;
2287 		break;
2288 	default:
2289 		goto out;
2290 	}
2291 
2292 	error = validate_prctl_map_addr(&prctl_map);
2293 	if (error)
2294 		goto out;
2295 
2296 	switch (opt) {
2297 	/*
2298 	 * If command line arguments and environment
2299 	 * are placed somewhere else on stack, we can
2300 	 * set them up here, ARG_START/END to setup
2301 	 * command line arguments and ENV_START/END
2302 	 * for environment.
2303 	 */
2304 	case PR_SET_MM_START_STACK:
2305 	case PR_SET_MM_ARG_START:
2306 	case PR_SET_MM_ARG_END:
2307 	case PR_SET_MM_ENV_START:
2308 	case PR_SET_MM_ENV_END:
2309 		if (!vma) {
2310 			error = -EFAULT;
2311 			goto out;
2312 		}
2313 	}
2314 
2315 	mm->start_code	= prctl_map.start_code;
2316 	mm->end_code	= prctl_map.end_code;
2317 	mm->start_data	= prctl_map.start_data;
2318 	mm->end_data	= prctl_map.end_data;
2319 	mm->start_brk	= prctl_map.start_brk;
2320 	mm->brk		= prctl_map.brk;
2321 	mm->start_stack	= prctl_map.start_stack;
2322 	mm->arg_start	= prctl_map.arg_start;
2323 	mm->arg_end	= prctl_map.arg_end;
2324 	mm->env_start	= prctl_map.env_start;
2325 	mm->env_end	= prctl_map.env_end;
2326 
2327 	error = 0;
2328 out:
2329 	spin_unlock(&mm->arg_lock);
2330 	mmap_read_unlock(mm);
2331 	return error;
2332 }
2333 
2334 #ifdef CONFIG_CHECKPOINT_RESTORE
2335 static int prctl_get_tid_address(struct task_struct *me, int __user * __user *tid_addr)
2336 {
2337 	return put_user(me->clear_child_tid, tid_addr);
2338 }
2339 #else
2340 static int prctl_get_tid_address(struct task_struct *me, int __user * __user *tid_addr)
2341 {
2342 	return -EINVAL;
2343 }
2344 #endif
2345 
2346 static int propagate_has_child_subreaper(struct task_struct *p, void *data)
2347 {
2348 	/*
2349 	 * If task has has_child_subreaper - all its descendants
2350 	 * already have these flag too and new descendants will
2351 	 * inherit it on fork, skip them.
2352 	 *
2353 	 * If we've found child_reaper - skip descendants in
2354 	 * it's subtree as they will never get out pidns.
2355 	 */
2356 	if (p->signal->has_child_subreaper ||
2357 	    is_child_reaper(task_pid(p)))
2358 		return 0;
2359 
2360 	p->signal->has_child_subreaper = 1;
2361 	return 1;
2362 }
2363 
2364 int __weak arch_prctl_spec_ctrl_get(struct task_struct *t, unsigned long which)
2365 {
2366 	return -EINVAL;
2367 }
2368 
2369 int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which,
2370 				    unsigned long ctrl)
2371 {
2372 	return -EINVAL;
2373 }
2374 
2375 int __weak arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status)
2376 {
2377 	return -EINVAL;
2378 }
2379 
2380 int __weak arch_set_shadow_stack_status(struct task_struct *t, unsigned long status)
2381 {
2382 	return -EINVAL;
2383 }
2384 
2385 int __weak arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status)
2386 {
2387 	return -EINVAL;
2388 }
2389 
2390 #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE)
2391 
2392 static int prctl_set_vma(unsigned long opt, unsigned long addr,
2393 			 unsigned long size, unsigned long arg)
2394 {
2395 	int error;
2396 
2397 	switch (opt) {
2398 	case PR_SET_VMA_ANON_NAME:
2399 		error = set_anon_vma_name(addr, size, (const char __user *)arg);
2400 		break;
2401 	default:
2402 		error = -EINVAL;
2403 	}
2404 
2405 	return error;
2406 }
2407 
2408 static inline unsigned long get_current_mdwe(void)
2409 {
2410 	unsigned long ret = 0;
2411 
2412 	if (mm_flags_test(MMF_HAS_MDWE, current->mm))
2413 		ret |= PR_MDWE_REFUSE_EXEC_GAIN;
2414 	if (mm_flags_test(MMF_HAS_MDWE_NO_INHERIT, current->mm))
2415 		ret |= PR_MDWE_NO_INHERIT;
2416 
2417 	return ret;
2418 }
2419 
2420 static inline int prctl_set_mdwe(unsigned long bits, unsigned long arg3,
2421 				 unsigned long arg4, unsigned long arg5)
2422 {
2423 	unsigned long current_bits;
2424 
2425 	if (arg3 || arg4 || arg5)
2426 		return -EINVAL;
2427 
2428 	if (bits & ~(PR_MDWE_REFUSE_EXEC_GAIN | PR_MDWE_NO_INHERIT))
2429 		return -EINVAL;
2430 
2431 	/* NO_INHERIT only makes sense with REFUSE_EXEC_GAIN */
2432 	if (bits & PR_MDWE_NO_INHERIT && !(bits & PR_MDWE_REFUSE_EXEC_GAIN))
2433 		return -EINVAL;
2434 
2435 	/*
2436 	 * EOPNOTSUPP might be more appropriate here in principle, but
2437 	 * existing userspace depends on EINVAL specifically.
2438 	 */
2439 	if (!arch_memory_deny_write_exec_supported())
2440 		return -EINVAL;
2441 
2442 	current_bits = get_current_mdwe();
2443 	if (current_bits && current_bits != bits)
2444 		return -EPERM; /* Cannot unset the flags */
2445 
2446 	if (bits & PR_MDWE_NO_INHERIT)
2447 		mm_flags_set(MMF_HAS_MDWE_NO_INHERIT, current->mm);
2448 	if (bits & PR_MDWE_REFUSE_EXEC_GAIN)
2449 		mm_flags_set(MMF_HAS_MDWE, current->mm);
2450 
2451 	return 0;
2452 }
2453 
2454 static inline int prctl_get_mdwe(unsigned long arg2, unsigned long arg3,
2455 				 unsigned long arg4, unsigned long arg5)
2456 {
2457 	if (arg2 || arg3 || arg4 || arg5)
2458 		return -EINVAL;
2459 	return get_current_mdwe();
2460 }
2461 
2462 static int prctl_get_auxv(void __user *addr, unsigned long len)
2463 {
2464 	struct mm_struct *mm = current->mm;
2465 	unsigned long size = min_t(unsigned long, sizeof(mm->saved_auxv), len);
2466 
2467 	if (size && copy_to_user(addr, mm->saved_auxv, size))
2468 		return -EFAULT;
2469 	return sizeof(mm->saved_auxv);
2470 }
2471 
2472 static int prctl_get_thp_disable(unsigned long arg2, unsigned long arg3,
2473 				 unsigned long arg4, unsigned long arg5)
2474 {
2475 	struct mm_struct *mm = current->mm;
2476 
2477 	if (arg2 || arg3 || arg4 || arg5)
2478 		return -EINVAL;
2479 
2480 	/* If disabled, we return "1 | flags", otherwise 0. */
2481 	if (mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm))
2482 		return 1;
2483 	else if (mm_flags_test(MMF_DISABLE_THP_EXCEPT_ADVISED, mm))
2484 		return 1 | PR_THP_DISABLE_EXCEPT_ADVISED;
2485 	return 0;
2486 }
2487 
2488 static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
2489 				 unsigned long arg4, unsigned long arg5)
2490 {
2491 	struct mm_struct *mm = current->mm;
2492 
2493 	if (arg4 || arg5)
2494 		return -EINVAL;
2495 
2496 	/* Flags are only allowed when disabling. */
2497 	if ((!thp_disable && flags) || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
2498 		return -EINVAL;
2499 	if (mmap_write_lock_killable(current->mm))
2500 		return -EINTR;
2501 	if (thp_disable) {
2502 		if (flags & PR_THP_DISABLE_EXCEPT_ADVISED) {
2503 			mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
2504 			mm_flags_set(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
2505 		} else {
2506 			mm_flags_set(MMF_DISABLE_THP_COMPLETELY, mm);
2507 			mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
2508 		}
2509 	} else {
2510 		mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
2511 		mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
2512 	}
2513 	mmap_write_unlock(current->mm);
2514 	return 0;
2515 }
2516 
2517 SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
2518 		unsigned long, arg4, unsigned long, arg5)
2519 {
2520 	struct task_struct *me = current;
2521 	unsigned char comm[sizeof(me->comm)];
2522 	long error;
2523 
2524 	error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2525 	if (error != -ENOSYS)
2526 		return error;
2527 
2528 	error = 0;
2529 	switch (option) {
2530 	case PR_SET_PDEATHSIG:
2531 		if (!valid_signal(arg2)) {
2532 			error = -EINVAL;
2533 			break;
2534 		}
2535 		/*
2536 		 * Ensure that either:
2537 		 *
2538 		 * 1. Subsequent getppid() calls reflect the parent process having died.
2539 		 * 2. forget_original_parent() will send the new me->pdeath_signal.
2540 		 *
2541 		 * Also prevent the read of me->pdeath_signal from being a data race.
2542 		 */
2543 		read_lock(&tasklist_lock);
2544 		me->pdeath_signal = arg2;
2545 		read_unlock(&tasklist_lock);
2546 		break;
2547 	case PR_GET_PDEATHSIG:
2548 		error = put_user(me->pdeath_signal, (int __user *)arg2);
2549 		break;
2550 	case PR_GET_DUMPABLE:
2551 		error = get_dumpable(me->mm);
2552 		break;
2553 	case PR_SET_DUMPABLE:
2554 		if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) {
2555 			error = -EINVAL;
2556 			break;
2557 		}
2558 		set_dumpable(me->mm, arg2);
2559 		break;
2560 
2561 	case PR_SET_UNALIGN:
2562 		error = SET_UNALIGN_CTL(me, arg2);
2563 		break;
2564 	case PR_GET_UNALIGN:
2565 		error = GET_UNALIGN_CTL(me, arg2);
2566 		break;
2567 	case PR_SET_FPEMU:
2568 		error = SET_FPEMU_CTL(me, arg2);
2569 		break;
2570 	case PR_GET_FPEMU:
2571 		error = GET_FPEMU_CTL(me, arg2);
2572 		break;
2573 	case PR_SET_FPEXC:
2574 		error = SET_FPEXC_CTL(me, arg2);
2575 		break;
2576 	case PR_GET_FPEXC:
2577 		error = GET_FPEXC_CTL(me, arg2);
2578 		break;
2579 	case PR_GET_TIMING:
2580 		error = PR_TIMING_STATISTICAL;
2581 		break;
2582 	case PR_SET_TIMING:
2583 		if (arg2 != PR_TIMING_STATISTICAL)
2584 			error = -EINVAL;
2585 		break;
2586 	case PR_SET_NAME:
2587 		comm[sizeof(me->comm) - 1] = 0;
2588 		if (strncpy_from_user(comm, (char __user *)arg2,
2589 				      sizeof(me->comm) - 1) < 0)
2590 			return -EFAULT;
2591 		set_task_comm(me, comm);
2592 		proc_comm_connector(me);
2593 		break;
2594 	case PR_GET_NAME:
2595 		get_task_comm(comm, me);
2596 		if (copy_to_user((char __user *)arg2, comm, sizeof(comm)))
2597 			return -EFAULT;
2598 		break;
2599 	case PR_GET_ENDIAN:
2600 		error = GET_ENDIAN(me, arg2);
2601 		break;
2602 	case PR_SET_ENDIAN:
2603 		error = SET_ENDIAN(me, arg2);
2604 		break;
2605 	case PR_GET_SECCOMP:
2606 		error = prctl_get_seccomp();
2607 		break;
2608 	case PR_SET_SECCOMP:
2609 		error = prctl_set_seccomp(arg2, (char __user *)arg3);
2610 		break;
2611 	case PR_GET_TSC:
2612 		error = GET_TSC_CTL(arg2);
2613 		break;
2614 	case PR_SET_TSC:
2615 		error = SET_TSC_CTL(arg2);
2616 		break;
2617 	case PR_TASK_PERF_EVENTS_DISABLE:
2618 		error = perf_event_task_disable();
2619 		break;
2620 	case PR_TASK_PERF_EVENTS_ENABLE:
2621 		error = perf_event_task_enable();
2622 		break;
2623 	case PR_GET_TIMERSLACK:
2624 		if (current->timer_slack_ns > ULONG_MAX)
2625 			error = ULONG_MAX;
2626 		else
2627 			error = current->timer_slack_ns;
2628 		break;
2629 	case PR_SET_TIMERSLACK:
2630 		if (rt_or_dl_task_policy(current))
2631 			break;
2632 		if (arg2 <= 0)
2633 			current->timer_slack_ns =
2634 					current->default_timer_slack_ns;
2635 		else
2636 			current->timer_slack_ns = arg2;
2637 		break;
2638 	case PR_MCE_KILL:
2639 		if (arg4 | arg5)
2640 			return -EINVAL;
2641 		switch (arg2) {
2642 		case PR_MCE_KILL_CLEAR:
2643 			if (arg3 != 0)
2644 				return -EINVAL;
2645 			current->flags &= ~PF_MCE_PROCESS;
2646 			break;
2647 		case PR_MCE_KILL_SET:
2648 			current->flags |= PF_MCE_PROCESS;
2649 			if (arg3 == PR_MCE_KILL_EARLY)
2650 				current->flags |= PF_MCE_EARLY;
2651 			else if (arg3 == PR_MCE_KILL_LATE)
2652 				current->flags &= ~PF_MCE_EARLY;
2653 			else if (arg3 == PR_MCE_KILL_DEFAULT)
2654 				current->flags &=
2655 						~(PF_MCE_EARLY|PF_MCE_PROCESS);
2656 			else
2657 				return -EINVAL;
2658 			break;
2659 		default:
2660 			return -EINVAL;
2661 		}
2662 		break;
2663 	case PR_MCE_KILL_GET:
2664 		if (arg2 | arg3 | arg4 | arg5)
2665 			return -EINVAL;
2666 		if (current->flags & PF_MCE_PROCESS)
2667 			error = (current->flags & PF_MCE_EARLY) ?
2668 				PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
2669 		else
2670 			error = PR_MCE_KILL_DEFAULT;
2671 		break;
2672 	case PR_SET_MM:
2673 		error = prctl_set_mm(arg2, arg3, arg4, arg5);
2674 		break;
2675 	case PR_GET_TID_ADDRESS:
2676 		error = prctl_get_tid_address(me, (int __user * __user *)arg2);
2677 		break;
2678 	case PR_SET_CHILD_SUBREAPER:
2679 		me->signal->is_child_subreaper = !!arg2;
2680 		if (!arg2)
2681 			break;
2682 
2683 		walk_process_tree(me, propagate_has_child_subreaper, NULL);
2684 		break;
2685 	case PR_GET_CHILD_SUBREAPER:
2686 		error = put_user(me->signal->is_child_subreaper,
2687 				 (int __user *)arg2);
2688 		break;
2689 	case PR_SET_NO_NEW_PRIVS:
2690 		if (arg2 != 1 || arg3 || arg4 || arg5)
2691 			return -EINVAL;
2692 
2693 		task_set_no_new_privs(current);
2694 		break;
2695 	case PR_GET_NO_NEW_PRIVS:
2696 		if (arg2 || arg3 || arg4 || arg5)
2697 			return -EINVAL;
2698 		return task_no_new_privs(current) ? 1 : 0;
2699 	case PR_GET_THP_DISABLE:
2700 		error = prctl_get_thp_disable(arg2, arg3, arg4, arg5);
2701 		break;
2702 	case PR_SET_THP_DISABLE:
2703 		error = prctl_set_thp_disable(arg2, arg3, arg4, arg5);
2704 		break;
2705 	case PR_MPX_ENABLE_MANAGEMENT:
2706 	case PR_MPX_DISABLE_MANAGEMENT:
2707 		/* No longer implemented: */
2708 		return -EINVAL;
2709 	case PR_SET_FP_MODE:
2710 		error = SET_FP_MODE(me, arg2);
2711 		break;
2712 	case PR_GET_FP_MODE:
2713 		error = GET_FP_MODE(me);
2714 		break;
2715 	case PR_SVE_SET_VL:
2716 		error = SVE_SET_VL(arg2);
2717 		break;
2718 	case PR_SVE_GET_VL:
2719 		error = SVE_GET_VL();
2720 		break;
2721 	case PR_SME_SET_VL:
2722 		error = SME_SET_VL(arg2);
2723 		break;
2724 	case PR_SME_GET_VL:
2725 		error = SME_GET_VL();
2726 		break;
2727 	case PR_GET_SPECULATION_CTRL:
2728 		if (arg3 || arg4 || arg5)
2729 			return -EINVAL;
2730 		error = arch_prctl_spec_ctrl_get(me, arg2);
2731 		break;
2732 	case PR_SET_SPECULATION_CTRL:
2733 		if (arg4 || arg5)
2734 			return -EINVAL;
2735 		error = arch_prctl_spec_ctrl_set(me, arg2, arg3);
2736 		break;
2737 	case PR_PAC_RESET_KEYS:
2738 		if (arg3 || arg4 || arg5)
2739 			return -EINVAL;
2740 		error = PAC_RESET_KEYS(me, arg2);
2741 		break;
2742 	case PR_PAC_SET_ENABLED_KEYS:
2743 		if (arg4 || arg5)
2744 			return -EINVAL;
2745 		error = PAC_SET_ENABLED_KEYS(me, arg2, arg3);
2746 		break;
2747 	case PR_PAC_GET_ENABLED_KEYS:
2748 		if (arg2 || arg3 || arg4 || arg5)
2749 			return -EINVAL;
2750 		error = PAC_GET_ENABLED_KEYS(me);
2751 		break;
2752 	case PR_SET_TAGGED_ADDR_CTRL:
2753 		if (arg3 || arg4 || arg5)
2754 			return -EINVAL;
2755 		error = SET_TAGGED_ADDR_CTRL(arg2);
2756 		break;
2757 	case PR_GET_TAGGED_ADDR_CTRL:
2758 		if (arg2 || arg3 || arg4 || arg5)
2759 			return -EINVAL;
2760 		error = GET_TAGGED_ADDR_CTRL();
2761 		break;
2762 	case PR_SET_IO_FLUSHER:
2763 		if (!capable(CAP_SYS_RESOURCE))
2764 			return -EPERM;
2765 
2766 		if (arg3 || arg4 || arg5)
2767 			return -EINVAL;
2768 
2769 		if (arg2 == 1)
2770 			current->flags |= PR_IO_FLUSHER;
2771 		else if (!arg2)
2772 			current->flags &= ~PR_IO_FLUSHER;
2773 		else
2774 			return -EINVAL;
2775 		break;
2776 	case PR_GET_IO_FLUSHER:
2777 		if (!capable(CAP_SYS_RESOURCE))
2778 			return -EPERM;
2779 
2780 		if (arg2 || arg3 || arg4 || arg5)
2781 			return -EINVAL;
2782 
2783 		error = (current->flags & PR_IO_FLUSHER) == PR_IO_FLUSHER;
2784 		break;
2785 	case PR_SET_SYSCALL_USER_DISPATCH:
2786 		error = set_syscall_user_dispatch(arg2, arg3, arg4,
2787 						  (char __user *) arg5);
2788 		break;
2789 #ifdef CONFIG_SCHED_CORE
2790 	case PR_SCHED_CORE:
2791 		error = sched_core_share_pid(arg2, arg3, arg4, arg5);
2792 		break;
2793 #endif
2794 	case PR_SET_MDWE:
2795 		error = prctl_set_mdwe(arg2, arg3, arg4, arg5);
2796 		break;
2797 	case PR_GET_MDWE:
2798 		error = prctl_get_mdwe(arg2, arg3, arg4, arg5);
2799 		break;
2800 	case PR_PPC_GET_DEXCR:
2801 		if (arg3 || arg4 || arg5)
2802 			return -EINVAL;
2803 		error = PPC_GET_DEXCR_ASPECT(me, arg2);
2804 		break;
2805 	case PR_PPC_SET_DEXCR:
2806 		if (arg4 || arg5)
2807 			return -EINVAL;
2808 		error = PPC_SET_DEXCR_ASPECT(me, arg2, arg3);
2809 		break;
2810 	case PR_SET_VMA:
2811 		error = prctl_set_vma(arg2, arg3, arg4, arg5);
2812 		break;
2813 	case PR_GET_AUXV:
2814 		if (arg4 || arg5)
2815 			return -EINVAL;
2816 		error = prctl_get_auxv((void __user *)arg2, arg3);
2817 		break;
2818 #ifdef CONFIG_KSM
2819 	case PR_SET_MEMORY_MERGE:
2820 		if (arg3 || arg4 || arg5)
2821 			return -EINVAL;
2822 		if (mmap_write_lock_killable(me->mm))
2823 			return -EINTR;
2824 
2825 		if (arg2)
2826 			error = ksm_enable_merge_any(me->mm);
2827 		else
2828 			error = ksm_disable_merge_any(me->mm);
2829 		mmap_write_unlock(me->mm);
2830 		break;
2831 	case PR_GET_MEMORY_MERGE:
2832 		if (arg2 || arg3 || arg4 || arg5)
2833 			return -EINVAL;
2834 
2835 		error = !!mm_flags_test(MMF_VM_MERGE_ANY, me->mm);
2836 		break;
2837 #endif
2838 	case PR_RISCV_V_SET_CONTROL:
2839 		error = RISCV_V_SET_CONTROL(arg2);
2840 		break;
2841 	case PR_RISCV_V_GET_CONTROL:
2842 		error = RISCV_V_GET_CONTROL();
2843 		break;
2844 	case PR_RISCV_SET_ICACHE_FLUSH_CTX:
2845 		error = RISCV_SET_ICACHE_FLUSH_CTX(arg2, arg3);
2846 		break;
2847 	case PR_GET_SHADOW_STACK_STATUS:
2848 		if (arg3 || arg4 || arg5)
2849 			return -EINVAL;
2850 		error = arch_get_shadow_stack_status(me, (unsigned long __user *) arg2);
2851 		break;
2852 	case PR_SET_SHADOW_STACK_STATUS:
2853 		if (arg3 || arg4 || arg5)
2854 			return -EINVAL;
2855 		error = arch_set_shadow_stack_status(me, arg2);
2856 		break;
2857 	case PR_LOCK_SHADOW_STACK_STATUS:
2858 		if (arg3 || arg4 || arg5)
2859 			return -EINVAL;
2860 		error = arch_lock_shadow_stack_status(me, arg2);
2861 		break;
2862 	case PR_TIMER_CREATE_RESTORE_IDS:
2863 		if (arg3 || arg4 || arg5)
2864 			return -EINVAL;
2865 		error = posixtimer_create_prctl(arg2);
2866 		break;
2867 	case PR_FUTEX_HASH:
2868 		error = futex_hash_prctl(arg2, arg3, arg4);
2869 		break;
2870 	default:
2871 		trace_task_prctl_unknown(option, arg2, arg3, arg4, arg5);
2872 		error = -EINVAL;
2873 		break;
2874 	}
2875 	return error;
2876 }
2877 
2878 SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep, void __user *, unused)
2879 {
2880 	int err = 0;
2881 	int cpu = raw_smp_processor_id();
2882 
2883 	if (cpup)
2884 		err |= put_user(cpu, cpup);
2885 	if (nodep)
2886 		err |= put_user(cpu_to_node(cpu), nodep);
2887 	return err ? -EFAULT : 0;
2888 }
2889 
2890 /**
2891  * do_sysinfo - fill in sysinfo struct
2892  * @info: pointer to buffer to fill
2893  */
2894 static int do_sysinfo(struct sysinfo *info)
2895 {
2896 	unsigned long mem_total, sav_total;
2897 	unsigned int mem_unit, bitcount;
2898 	struct timespec64 tp;
2899 
2900 	memset(info, 0, sizeof(struct sysinfo));
2901 
2902 	ktime_get_boottime_ts64(&tp);
2903 	timens_add_boottime(&tp);
2904 	info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
2905 
2906 	get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
2907 
2908 	info->procs = nr_threads;
2909 
2910 	si_meminfo(info);
2911 	si_swapinfo(info);
2912 
2913 	/*
2914 	 * If the sum of all the available memory (i.e. ram + swap)
2915 	 * is less than can be stored in a 32 bit unsigned long then
2916 	 * we can be binary compatible with 2.2.x kernels.  If not,
2917 	 * well, in that case 2.2.x was broken anyways...
2918 	 *
2919 	 *  -Erik Andersen <andersee@debian.org>
2920 	 */
2921 
2922 	mem_total = info->totalram + info->totalswap;
2923 	if (mem_total < info->totalram || mem_total < info->totalswap)
2924 		goto out;
2925 	bitcount = 0;
2926 	mem_unit = info->mem_unit;
2927 	while (mem_unit > 1) {
2928 		bitcount++;
2929 		mem_unit >>= 1;
2930 		sav_total = mem_total;
2931 		mem_total <<= 1;
2932 		if (mem_total < sav_total)
2933 			goto out;
2934 	}
2935 
2936 	/*
2937 	 * If mem_total did not overflow, multiply all memory values by
2938 	 * info->mem_unit and set it to 1.  This leaves things compatible
2939 	 * with 2.2.x, and also retains compatibility with earlier 2.4.x
2940 	 * kernels...
2941 	 */
2942 
2943 	info->mem_unit = 1;
2944 	info->totalram <<= bitcount;
2945 	info->freeram <<= bitcount;
2946 	info->sharedram <<= bitcount;
2947 	info->bufferram <<= bitcount;
2948 	info->totalswap <<= bitcount;
2949 	info->freeswap <<= bitcount;
2950 	info->totalhigh <<= bitcount;
2951 	info->freehigh <<= bitcount;
2952 
2953 out:
2954 	return 0;
2955 }
2956 
2957 SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
2958 {
2959 	struct sysinfo val;
2960 
2961 	do_sysinfo(&val);
2962 
2963 	if (copy_to_user(info, &val, sizeof(struct sysinfo)))
2964 		return -EFAULT;
2965 
2966 	return 0;
2967 }
2968 
2969 #ifdef CONFIG_COMPAT
2970 struct compat_sysinfo {
2971 	s32 uptime;
2972 	u32 loads[3];
2973 	u32 totalram;
2974 	u32 freeram;
2975 	u32 sharedram;
2976 	u32 bufferram;
2977 	u32 totalswap;
2978 	u32 freeswap;
2979 	u16 procs;
2980 	u16 pad;
2981 	u32 totalhigh;
2982 	u32 freehigh;
2983 	u32 mem_unit;
2984 	char _f[20-2*sizeof(u32)-sizeof(int)];
2985 };
2986 
2987 COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
2988 {
2989 	struct sysinfo s;
2990 	struct compat_sysinfo s_32;
2991 
2992 	do_sysinfo(&s);
2993 
2994 	/* Check to see if any memory value is too large for 32-bit and scale
2995 	 *  down if needed
2996 	 */
2997 	if (upper_32_bits(s.totalram) || upper_32_bits(s.totalswap)) {
2998 		int bitcount = 0;
2999 
3000 		while (s.mem_unit < PAGE_SIZE) {
3001 			s.mem_unit <<= 1;
3002 			bitcount++;
3003 		}
3004 
3005 		s.totalram >>= bitcount;
3006 		s.freeram >>= bitcount;
3007 		s.sharedram >>= bitcount;
3008 		s.bufferram >>= bitcount;
3009 		s.totalswap >>= bitcount;
3010 		s.freeswap >>= bitcount;
3011 		s.totalhigh >>= bitcount;
3012 		s.freehigh >>= bitcount;
3013 	}
3014 
3015 	memset(&s_32, 0, sizeof(s_32));
3016 	s_32.uptime = s.uptime;
3017 	s_32.loads[0] = s.loads[0];
3018 	s_32.loads[1] = s.loads[1];
3019 	s_32.loads[2] = s.loads[2];
3020 	s_32.totalram = s.totalram;
3021 	s_32.freeram = s.freeram;
3022 	s_32.sharedram = s.sharedram;
3023 	s_32.bufferram = s.bufferram;
3024 	s_32.totalswap = s.totalswap;
3025 	s_32.freeswap = s.freeswap;
3026 	s_32.procs = s.procs;
3027 	s_32.totalhigh = s.totalhigh;
3028 	s_32.freehigh = s.freehigh;
3029 	s_32.mem_unit = s.mem_unit;
3030 	if (copy_to_user(info, &s_32, sizeof(s_32)))
3031 		return -EFAULT;
3032 	return 0;
3033 }
3034 #endif /* CONFIG_COMPAT */
3035