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