• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	bool ruid_new, euid_new, suid_new;
648 
649 	kruid = make_kuid(ns, ruid);
650 	keuid = make_kuid(ns, euid);
651 	ksuid = make_kuid(ns, suid);
652 
653 	if ((ruid != (uid_t) -1) && !uid_valid(kruid))
654 		return -EINVAL;
655 
656 	if ((euid != (uid_t) -1) && !uid_valid(keuid))
657 		return -EINVAL;
658 
659 	if ((suid != (uid_t) -1) && !uid_valid(ksuid))
660 		return -EINVAL;
661 
662 	old = current_cred();
663 
664 	/* check for no-op */
665 	if ((ruid == (uid_t) -1 || uid_eq(kruid, old->uid)) &&
666 	    (euid == (uid_t) -1 || (uid_eq(keuid, old->euid) &&
667 				    uid_eq(keuid, old->fsuid))) &&
668 	    (suid == (uid_t) -1 || uid_eq(ksuid, old->suid)))
669 		return 0;
670 
671 	ruid_new = ruid != (uid_t) -1        && !uid_eq(kruid, old->uid) &&
672 		   !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid);
673 	euid_new = euid != (uid_t) -1        && !uid_eq(keuid, old->uid) &&
674 		   !uid_eq(keuid, old->euid) && !uid_eq(keuid, old->suid);
675 	suid_new = suid != (uid_t) -1        && !uid_eq(ksuid, old->uid) &&
676 		   !uid_eq(ksuid, old->euid) && !uid_eq(ksuid, old->suid);
677 	if ((ruid_new || euid_new || suid_new) &&
678 	    !ns_capable_setid(old->user_ns, CAP_SETUID))
679 		return -EPERM;
680 
681 	new = prepare_creds();
682 	if (!new)
683 		return -ENOMEM;
684 
685 	if (ruid != (uid_t) -1) {
686 		new->uid = kruid;
687 		if (!uid_eq(kruid, old->uid)) {
688 			retval = set_user(new);
689 			if (retval < 0)
690 				goto error;
691 		}
692 	}
693 	if (euid != (uid_t) -1)
694 		new->euid = keuid;
695 	if (suid != (uid_t) -1)
696 		new->suid = ksuid;
697 	new->fsuid = new->euid;
698 
699 	retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
700 	if (retval < 0)
701 		goto error;
702 
703 	return commit_creds(new);
704 
705 error:
706 	abort_creds(new);
707 	return retval;
708 }
709 
SYSCALL_DEFINE3(setresuid,uid_t,ruid,uid_t,euid,uid_t,suid)710 SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
711 {
712 	return __sys_setresuid(ruid, euid, suid);
713 }
714 
SYSCALL_DEFINE3(getresuid,uid_t __user *,ruidp,uid_t __user *,euidp,uid_t __user *,suidp)715 SYSCALL_DEFINE3(getresuid, uid_t __user *, ruidp, uid_t __user *, euidp, uid_t __user *, suidp)
716 {
717 	const struct cred *cred = current_cred();
718 	int retval;
719 	uid_t ruid, euid, suid;
720 
721 	ruid = from_kuid_munged(cred->user_ns, cred->uid);
722 	euid = from_kuid_munged(cred->user_ns, cred->euid);
723 	suid = from_kuid_munged(cred->user_ns, cred->suid);
724 
725 	retval = put_user(ruid, ruidp);
726 	if (!retval) {
727 		retval = put_user(euid, euidp);
728 		if (!retval)
729 			return put_user(suid, suidp);
730 	}
731 	return retval;
732 }
733 
734 /*
735  * Same as above, but for rgid, egid, sgid.
736  */
__sys_setresgid(gid_t rgid,gid_t egid,gid_t sgid)737 long __sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
738 {
739 	struct user_namespace *ns = current_user_ns();
740 	const struct cred *old;
741 	struct cred *new;
742 	int retval;
743 	kgid_t krgid, kegid, ksgid;
744 	bool rgid_new, egid_new, sgid_new;
745 
746 	krgid = make_kgid(ns, rgid);
747 	kegid = make_kgid(ns, egid);
748 	ksgid = make_kgid(ns, sgid);
749 
750 	if ((rgid != (gid_t) -1) && !gid_valid(krgid))
751 		return -EINVAL;
752 	if ((egid != (gid_t) -1) && !gid_valid(kegid))
753 		return -EINVAL;
754 	if ((sgid != (gid_t) -1) && !gid_valid(ksgid))
755 		return -EINVAL;
756 
757 	old = current_cred();
758 
759 	/* check for no-op */
760 	if ((rgid == (gid_t) -1 || gid_eq(krgid, old->gid)) &&
761 	    (egid == (gid_t) -1 || (gid_eq(kegid, old->egid) &&
762 				    gid_eq(kegid, old->fsgid))) &&
763 	    (sgid == (gid_t) -1 || gid_eq(ksgid, old->sgid)))
764 		return 0;
765 
766 	rgid_new = rgid != (gid_t) -1        && !gid_eq(krgid, old->gid) &&
767 		   !gid_eq(krgid, old->egid) && !gid_eq(krgid, old->sgid);
768 	egid_new = egid != (gid_t) -1        && !gid_eq(kegid, old->gid) &&
769 		   !gid_eq(kegid, old->egid) && !gid_eq(kegid, old->sgid);
770 	sgid_new = sgid != (gid_t) -1        && !gid_eq(ksgid, old->gid) &&
771 		   !gid_eq(ksgid, old->egid) && !gid_eq(ksgid, old->sgid);
772 	if ((rgid_new || egid_new || sgid_new) &&
773 	    !ns_capable_setid(old->user_ns, CAP_SETGID))
774 		return -EPERM;
775 
776 	new = prepare_creds();
777 	if (!new)
778 		return -ENOMEM;
779 
780 	if (rgid != (gid_t) -1)
781 		new->gid = krgid;
782 	if (egid != (gid_t) -1)
783 		new->egid = kegid;
784 	if (sgid != (gid_t) -1)
785 		new->sgid = ksgid;
786 	new->fsgid = new->egid;
787 
788 	retval = security_task_fix_setgid(new, old, LSM_SETID_RES);
789 	if (retval < 0)
790 		goto error;
791 
792 	return commit_creds(new);
793 
794 error:
795 	abort_creds(new);
796 	return retval;
797 }
798 
SYSCALL_DEFINE3(setresgid,gid_t,rgid,gid_t,egid,gid_t,sgid)799 SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
800 {
801 	return __sys_setresgid(rgid, egid, sgid);
802 }
803 
SYSCALL_DEFINE3(getresgid,gid_t __user *,rgidp,gid_t __user *,egidp,gid_t __user *,sgidp)804 SYSCALL_DEFINE3(getresgid, gid_t __user *, rgidp, gid_t __user *, egidp, gid_t __user *, sgidp)
805 {
806 	const struct cred *cred = current_cred();
807 	int retval;
808 	gid_t rgid, egid, sgid;
809 
810 	rgid = from_kgid_munged(cred->user_ns, cred->gid);
811 	egid = from_kgid_munged(cred->user_ns, cred->egid);
812 	sgid = from_kgid_munged(cred->user_ns, cred->sgid);
813 
814 	retval = put_user(rgid, rgidp);
815 	if (!retval) {
816 		retval = put_user(egid, egidp);
817 		if (!retval)
818 			retval = put_user(sgid, sgidp);
819 	}
820 
821 	return retval;
822 }
823 
824 
825 /*
826  * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
827  * is used for "access()" and for the NFS daemon (letting nfsd stay at
828  * whatever uid it wants to). It normally shadows "euid", except when
829  * explicitly set by setfsuid() or for access..
830  */
__sys_setfsuid(uid_t uid)831 long __sys_setfsuid(uid_t uid)
832 {
833 	const struct cred *old;
834 	struct cred *new;
835 	uid_t old_fsuid;
836 	kuid_t kuid;
837 
838 	old = current_cred();
839 	old_fsuid = from_kuid_munged(old->user_ns, old->fsuid);
840 
841 	kuid = make_kuid(old->user_ns, uid);
842 	if (!uid_valid(kuid))
843 		return old_fsuid;
844 
845 	new = prepare_creds();
846 	if (!new)
847 		return old_fsuid;
848 
849 	if (uid_eq(kuid, old->uid)  || uid_eq(kuid, old->euid)  ||
850 	    uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) ||
851 	    ns_capable_setid(old->user_ns, CAP_SETUID)) {
852 		if (!uid_eq(kuid, old->fsuid)) {
853 			new->fsuid = kuid;
854 			if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
855 				goto change_okay;
856 		}
857 	}
858 
859 	abort_creds(new);
860 	return old_fsuid;
861 
862 change_okay:
863 	commit_creds(new);
864 	return old_fsuid;
865 }
866 
SYSCALL_DEFINE1(setfsuid,uid_t,uid)867 SYSCALL_DEFINE1(setfsuid, uid_t, uid)
868 {
869 	return __sys_setfsuid(uid);
870 }
871 
872 /*
873  * Samma på svenska..
874  */
__sys_setfsgid(gid_t gid)875 long __sys_setfsgid(gid_t gid)
876 {
877 	const struct cred *old;
878 	struct cred *new;
879 	gid_t old_fsgid;
880 	kgid_t kgid;
881 
882 	old = current_cred();
883 	old_fsgid = from_kgid_munged(old->user_ns, old->fsgid);
884 
885 	kgid = make_kgid(old->user_ns, gid);
886 	if (!gid_valid(kgid))
887 		return old_fsgid;
888 
889 	new = prepare_creds();
890 	if (!new)
891 		return old_fsgid;
892 
893 	if (gid_eq(kgid, old->gid)  || gid_eq(kgid, old->egid)  ||
894 	    gid_eq(kgid, old->sgid) || gid_eq(kgid, old->fsgid) ||
895 	    ns_capable_setid(old->user_ns, CAP_SETGID)) {
896 		if (!gid_eq(kgid, old->fsgid)) {
897 			new->fsgid = kgid;
898 			if (security_task_fix_setgid(new,old,LSM_SETID_FS) == 0)
899 				goto change_okay;
900 		}
901 	}
902 
903 	abort_creds(new);
904 	return old_fsgid;
905 
906 change_okay:
907 	commit_creds(new);
908 	return old_fsgid;
909 }
910 
SYSCALL_DEFINE1(setfsgid,gid_t,gid)911 SYSCALL_DEFINE1(setfsgid, gid_t, gid)
912 {
913 	return __sys_setfsgid(gid);
914 }
915 #endif /* CONFIG_MULTIUSER */
916 
917 /**
918  * sys_getpid - return the thread group id of the current process
919  *
920  * Note, despite the name, this returns the tgid not the pid.  The tgid and
921  * the pid are identical unless CLONE_THREAD was specified on clone() in
922  * which case the tgid is the same in all threads of the same group.
923  *
924  * This is SMP safe as current->tgid does not change.
925  */
SYSCALL_DEFINE0(getpid)926 SYSCALL_DEFINE0(getpid)
927 {
928 	return task_tgid_vnr(current);
929 }
930 
931 /* Thread ID - the internal kernel "pid" */
SYSCALL_DEFINE0(gettid)932 SYSCALL_DEFINE0(gettid)
933 {
934 	return task_pid_vnr(current);
935 }
936 
937 /*
938  * Accessing ->real_parent is not SMP-safe, it could
939  * change from under us. However, we can use a stale
940  * value of ->real_parent under rcu_read_lock(), see
941  * release_task()->call_rcu(delayed_put_task_struct).
942  */
SYSCALL_DEFINE0(getppid)943 SYSCALL_DEFINE0(getppid)
944 {
945 	int pid;
946 
947 	rcu_read_lock();
948 	pid = task_tgid_vnr(rcu_dereference(current->real_parent));
949 	rcu_read_unlock();
950 
951 	return pid;
952 }
953 
SYSCALL_DEFINE0(getuid)954 SYSCALL_DEFINE0(getuid)
955 {
956 	/* Only we change this so SMP safe */
957 	return from_kuid_munged(current_user_ns(), current_uid());
958 }
959 
SYSCALL_DEFINE0(geteuid)960 SYSCALL_DEFINE0(geteuid)
961 {
962 	/* Only we change this so SMP safe */
963 	return from_kuid_munged(current_user_ns(), current_euid());
964 }
965 
SYSCALL_DEFINE0(getgid)966 SYSCALL_DEFINE0(getgid)
967 {
968 	/* Only we change this so SMP safe */
969 	return from_kgid_munged(current_user_ns(), current_gid());
970 }
971 
SYSCALL_DEFINE0(getegid)972 SYSCALL_DEFINE0(getegid)
973 {
974 	/* Only we change this so SMP safe */
975 	return from_kgid_munged(current_user_ns(), current_egid());
976 }
977 
do_sys_times(struct tms * tms)978 static void do_sys_times(struct tms *tms)
979 {
980 	u64 tgutime, tgstime, cutime, cstime;
981 
982 	thread_group_cputime_adjusted(current, &tgutime, &tgstime);
983 	cutime = current->signal->cutime;
984 	cstime = current->signal->cstime;
985 	tms->tms_utime = nsec_to_clock_t(tgutime);
986 	tms->tms_stime = nsec_to_clock_t(tgstime);
987 	tms->tms_cutime = nsec_to_clock_t(cutime);
988 	tms->tms_cstime = nsec_to_clock_t(cstime);
989 }
990 
SYSCALL_DEFINE1(times,struct tms __user *,tbuf)991 SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
992 {
993 	if (tbuf) {
994 		struct tms tmp;
995 
996 		do_sys_times(&tmp);
997 		if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
998 			return -EFAULT;
999 	}
1000 	force_successful_syscall_return();
1001 	return (long) jiffies_64_to_clock_t(get_jiffies_64());
1002 }
1003 
1004 #ifdef CONFIG_COMPAT
clock_t_to_compat_clock_t(clock_t x)1005 static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
1006 {
1007 	return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
1008 }
1009 
COMPAT_SYSCALL_DEFINE1(times,struct compat_tms __user *,tbuf)1010 COMPAT_SYSCALL_DEFINE1(times, struct compat_tms __user *, tbuf)
1011 {
1012 	if (tbuf) {
1013 		struct tms tms;
1014 		struct compat_tms tmp;
1015 
1016 		do_sys_times(&tms);
1017 		/* Convert our struct tms to the compat version. */
1018 		tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
1019 		tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
1020 		tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
1021 		tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
1022 		if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
1023 			return -EFAULT;
1024 	}
1025 	force_successful_syscall_return();
1026 	return compat_jiffies_to_clock_t(jiffies);
1027 }
1028 #endif
1029 
1030 /*
1031  * This needs some heavy checking ...
1032  * I just haven't the stomach for it. I also don't fully
1033  * understand sessions/pgrp etc. Let somebody who does explain it.
1034  *
1035  * OK, I think I have the protection semantics right.... this is really
1036  * only important on a multi-user system anyway, to make sure one user
1037  * can't send a signal to a process owned by another.  -TYT, 12/12/91
1038  *
1039  * !PF_FORKNOEXEC check to conform completely to POSIX.
1040  */
SYSCALL_DEFINE2(setpgid,pid_t,pid,pid_t,pgid)1041 SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
1042 {
1043 	struct task_struct *p;
1044 	struct task_struct *group_leader = current->group_leader;
1045 	struct pid *pgrp;
1046 	int err;
1047 
1048 	if (!pid)
1049 		pid = task_pid_vnr(group_leader);
1050 	if (!pgid)
1051 		pgid = pid;
1052 	if (pgid < 0)
1053 		return -EINVAL;
1054 	rcu_read_lock();
1055 
1056 	/* From this point forward we keep holding onto the tasklist lock
1057 	 * so that our parent does not change from under us. -DaveM
1058 	 */
1059 	write_lock_irq(&tasklist_lock);
1060 
1061 	err = -ESRCH;
1062 	p = find_task_by_vpid(pid);
1063 	if (!p)
1064 		goto out;
1065 
1066 	err = -EINVAL;
1067 	if (!thread_group_leader(p))
1068 		goto out;
1069 
1070 	if (same_thread_group(p->real_parent, group_leader)) {
1071 		err = -EPERM;
1072 		if (task_session(p) != task_session(group_leader))
1073 			goto out;
1074 		err = -EACCES;
1075 		if (!(p->flags & PF_FORKNOEXEC))
1076 			goto out;
1077 	} else {
1078 		err = -ESRCH;
1079 		if (p != group_leader)
1080 			goto out;
1081 	}
1082 
1083 	err = -EPERM;
1084 	if (p->signal->leader)
1085 		goto out;
1086 
1087 	pgrp = task_pid(p);
1088 	if (pgid != pid) {
1089 		struct task_struct *g;
1090 
1091 		pgrp = find_vpid(pgid);
1092 		g = pid_task(pgrp, PIDTYPE_PGID);
1093 		if (!g || task_session(g) != task_session(group_leader))
1094 			goto out;
1095 	}
1096 
1097 	err = security_task_setpgid(p, pgid);
1098 	if (err)
1099 		goto out;
1100 
1101 	if (task_pgrp(p) != pgrp)
1102 		change_pid(p, PIDTYPE_PGID, pgrp);
1103 
1104 	err = 0;
1105 out:
1106 	/* All paths lead to here, thus we are safe. -DaveM */
1107 	write_unlock_irq(&tasklist_lock);
1108 	rcu_read_unlock();
1109 	return err;
1110 }
1111 
do_getpgid(pid_t pid)1112 static int do_getpgid(pid_t pid)
1113 {
1114 	struct task_struct *p;
1115 	struct pid *grp;
1116 	int retval;
1117 
1118 	rcu_read_lock();
1119 	if (!pid)
1120 		grp = task_pgrp(current);
1121 	else {
1122 		retval = -ESRCH;
1123 		p = find_task_by_vpid(pid);
1124 		if (!p)
1125 			goto out;
1126 		grp = task_pgrp(p);
1127 		if (!grp)
1128 			goto out;
1129 
1130 		retval = security_task_getpgid(p);
1131 		if (retval)
1132 			goto out;
1133 	}
1134 	retval = pid_vnr(grp);
1135 out:
1136 	rcu_read_unlock();
1137 	return retval;
1138 }
1139 
SYSCALL_DEFINE1(getpgid,pid_t,pid)1140 SYSCALL_DEFINE1(getpgid, pid_t, pid)
1141 {
1142 	return do_getpgid(pid);
1143 }
1144 
1145 #ifdef __ARCH_WANT_SYS_GETPGRP
1146 
SYSCALL_DEFINE0(getpgrp)1147 SYSCALL_DEFINE0(getpgrp)
1148 {
1149 	return do_getpgid(0);
1150 }
1151 
1152 #endif
1153 
SYSCALL_DEFINE1(getsid,pid_t,pid)1154 SYSCALL_DEFINE1(getsid, pid_t, pid)
1155 {
1156 	struct task_struct *p;
1157 	struct pid *sid;
1158 	int retval;
1159 
1160 	rcu_read_lock();
1161 	if (!pid)
1162 		sid = task_session(current);
1163 	else {
1164 		retval = -ESRCH;
1165 		p = find_task_by_vpid(pid);
1166 		if (!p)
1167 			goto out;
1168 		sid = task_session(p);
1169 		if (!sid)
1170 			goto out;
1171 
1172 		retval = security_task_getsid(p);
1173 		if (retval)
1174 			goto out;
1175 	}
1176 	retval = pid_vnr(sid);
1177 out:
1178 	rcu_read_unlock();
1179 	return retval;
1180 }
1181 
set_special_pids(struct pid * pid)1182 static void set_special_pids(struct pid *pid)
1183 {
1184 	struct task_struct *curr = current->group_leader;
1185 
1186 	if (task_session(curr) != pid)
1187 		change_pid(curr, PIDTYPE_SID, pid);
1188 
1189 	if (task_pgrp(curr) != pid)
1190 		change_pid(curr, PIDTYPE_PGID, pid);
1191 }
1192 
ksys_setsid(void)1193 int ksys_setsid(void)
1194 {
1195 	struct task_struct *group_leader = current->group_leader;
1196 	struct pid *sid = task_pid(group_leader);
1197 	pid_t session = pid_vnr(sid);
1198 	int err = -EPERM;
1199 
1200 	write_lock_irq(&tasklist_lock);
1201 	/* Fail if I am already a session leader */
1202 	if (group_leader->signal->leader)
1203 		goto out;
1204 
1205 	/* Fail if a process group id already exists that equals the
1206 	 * proposed session id.
1207 	 */
1208 	if (pid_task(sid, PIDTYPE_PGID))
1209 		goto out;
1210 
1211 	group_leader->signal->leader = 1;
1212 	set_special_pids(sid);
1213 
1214 	proc_clear_tty(group_leader);
1215 
1216 	err = session;
1217 out:
1218 	write_unlock_irq(&tasklist_lock);
1219 	if (err > 0) {
1220 		proc_sid_connector(group_leader);
1221 		sched_autogroup_create_attach(group_leader);
1222 	}
1223 	return err;
1224 }
1225 
SYSCALL_DEFINE0(setsid)1226 SYSCALL_DEFINE0(setsid)
1227 {
1228 	return ksys_setsid();
1229 }
1230 
1231 DECLARE_RWSEM(uts_sem);
1232 
1233 #ifdef COMPAT_UTS_MACHINE
1234 #define override_architecture(name) \
1235 	(personality(current->personality) == PER_LINUX32 && \
1236 	 copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1237 		      sizeof(COMPAT_UTS_MACHINE)))
1238 #else
1239 #define override_architecture(name)	0
1240 #endif
1241 
1242 /*
1243  * Work around broken programs that cannot handle "Linux 3.0".
1244  * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40
1245  * And we map 4.x and later versions to 2.6.60+x, so 4.0/5.0/6.0/... would be
1246  * 2.6.60.
1247  */
override_release(char __user * release,size_t len)1248 static int override_release(char __user *release, size_t len)
1249 {
1250 	int ret = 0;
1251 
1252 	if (current->personality & UNAME26) {
1253 		const char *rest = UTS_RELEASE;
1254 		char buf[65] = { 0 };
1255 		int ndots = 0;
1256 		unsigned v;
1257 		size_t copy;
1258 
1259 		while (*rest) {
1260 			if (*rest == '.' && ++ndots >= 3)
1261 				break;
1262 			if (!isdigit(*rest) && *rest != '.')
1263 				break;
1264 			rest++;
1265 		}
1266 		v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 60;
1267 		copy = clamp_t(size_t, len, 1, sizeof(buf));
1268 		copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
1269 		ret = copy_to_user(release, buf, copy + 1);
1270 	}
1271 	return ret;
1272 }
1273 
SYSCALL_DEFINE1(newuname,struct new_utsname __user *,name)1274 SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
1275 {
1276 	struct new_utsname tmp;
1277 
1278 	down_read(&uts_sem);
1279 	memcpy(&tmp, utsname(), sizeof(tmp));
1280 	up_read(&uts_sem);
1281 	if (copy_to_user(name, &tmp, sizeof(tmp)))
1282 		return -EFAULT;
1283 
1284 	if (override_release(name->release, sizeof(name->release)))
1285 		return -EFAULT;
1286 	if (override_architecture(name))
1287 		return -EFAULT;
1288 	return 0;
1289 }
1290 
1291 #ifdef __ARCH_WANT_SYS_OLD_UNAME
1292 /*
1293  * Old cruft
1294  */
SYSCALL_DEFINE1(uname,struct old_utsname __user *,name)1295 SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
1296 {
1297 	struct old_utsname tmp;
1298 
1299 	if (!name)
1300 		return -EFAULT;
1301 
1302 	down_read(&uts_sem);
1303 	memcpy(&tmp, utsname(), sizeof(tmp));
1304 	up_read(&uts_sem);
1305 	if (copy_to_user(name, &tmp, sizeof(tmp)))
1306 		return -EFAULT;
1307 
1308 	if (override_release(name->release, sizeof(name->release)))
1309 		return -EFAULT;
1310 	if (override_architecture(name))
1311 		return -EFAULT;
1312 	return 0;
1313 }
1314 
SYSCALL_DEFINE1(olduname,struct oldold_utsname __user *,name)1315 SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
1316 {
1317 	struct oldold_utsname tmp;
1318 
1319 	if (!name)
1320 		return -EFAULT;
1321 
1322 	memset(&tmp, 0, sizeof(tmp));
1323 
1324 	down_read(&uts_sem);
1325 	memcpy(&tmp.sysname, &utsname()->sysname, __OLD_UTS_LEN);
1326 	memcpy(&tmp.nodename, &utsname()->nodename, __OLD_UTS_LEN);
1327 	memcpy(&tmp.release, &utsname()->release, __OLD_UTS_LEN);
1328 	memcpy(&tmp.version, &utsname()->version, __OLD_UTS_LEN);
1329 	memcpy(&tmp.machine, &utsname()->machine, __OLD_UTS_LEN);
1330 	up_read(&uts_sem);
1331 	if (copy_to_user(name, &tmp, sizeof(tmp)))
1332 		return -EFAULT;
1333 
1334 	if (override_architecture(name))
1335 		return -EFAULT;
1336 	if (override_release(name->release, sizeof(name->release)))
1337 		return -EFAULT;
1338 	return 0;
1339 }
1340 #endif
1341 
SYSCALL_DEFINE2(sethostname,char __user *,name,int,len)1342 SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
1343 {
1344 	int errno;
1345 	char tmp[__NEW_UTS_LEN];
1346 
1347 	if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
1348 		return -EPERM;
1349 
1350 	if (len < 0 || len > __NEW_UTS_LEN)
1351 		return -EINVAL;
1352 	errno = -EFAULT;
1353 	if (!copy_from_user(tmp, name, len)) {
1354 		struct new_utsname *u;
1355 
1356 		down_write(&uts_sem);
1357 		u = utsname();
1358 		memcpy(u->nodename, tmp, len);
1359 		memset(u->nodename + len, 0, sizeof(u->nodename) - len);
1360 		errno = 0;
1361 		uts_proc_notify(UTS_PROC_HOSTNAME);
1362 		up_write(&uts_sem);
1363 	}
1364 	return errno;
1365 }
1366 
1367 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1368 
SYSCALL_DEFINE2(gethostname,char __user *,name,int,len)1369 SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
1370 {
1371 	int i;
1372 	struct new_utsname *u;
1373 	char tmp[__NEW_UTS_LEN + 1];
1374 
1375 	if (len < 0)
1376 		return -EINVAL;
1377 	down_read(&uts_sem);
1378 	u = utsname();
1379 	i = 1 + strlen(u->nodename);
1380 	if (i > len)
1381 		i = len;
1382 	memcpy(tmp, u->nodename, i);
1383 	up_read(&uts_sem);
1384 	if (copy_to_user(name, tmp, i))
1385 		return -EFAULT;
1386 	return 0;
1387 }
1388 
1389 #endif
1390 
1391 /*
1392  * Only setdomainname; getdomainname can be implemented by calling
1393  * uname()
1394  */
SYSCALL_DEFINE2(setdomainname,char __user *,name,int,len)1395 SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
1396 {
1397 	int errno;
1398 	char tmp[__NEW_UTS_LEN];
1399 
1400 	if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
1401 		return -EPERM;
1402 	if (len < 0 || len > __NEW_UTS_LEN)
1403 		return -EINVAL;
1404 
1405 	errno = -EFAULT;
1406 	if (!copy_from_user(tmp, name, len)) {
1407 		struct new_utsname *u;
1408 
1409 		down_write(&uts_sem);
1410 		u = utsname();
1411 		memcpy(u->domainname, tmp, len);
1412 		memset(u->domainname + len, 0, sizeof(u->domainname) - len);
1413 		errno = 0;
1414 		uts_proc_notify(UTS_PROC_DOMAINNAME);
1415 		up_write(&uts_sem);
1416 	}
1417 	return errno;
1418 }
1419 
SYSCALL_DEFINE2(getrlimit,unsigned int,resource,struct rlimit __user *,rlim)1420 SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1421 {
1422 	struct rlimit value;
1423 	int ret;
1424 
1425 	ret = do_prlimit(current, resource, NULL, &value);
1426 	if (!ret)
1427 		ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1428 
1429 	return ret;
1430 }
1431 
1432 #ifdef CONFIG_COMPAT
1433 
COMPAT_SYSCALL_DEFINE2(setrlimit,unsigned int,resource,struct compat_rlimit __user *,rlim)1434 COMPAT_SYSCALL_DEFINE2(setrlimit, unsigned int, resource,
1435 		       struct compat_rlimit __user *, rlim)
1436 {
1437 	struct rlimit r;
1438 	struct compat_rlimit r32;
1439 
1440 	if (copy_from_user(&r32, rlim, sizeof(struct compat_rlimit)))
1441 		return -EFAULT;
1442 
1443 	if (r32.rlim_cur == COMPAT_RLIM_INFINITY)
1444 		r.rlim_cur = RLIM_INFINITY;
1445 	else
1446 		r.rlim_cur = r32.rlim_cur;
1447 	if (r32.rlim_max == COMPAT_RLIM_INFINITY)
1448 		r.rlim_max = RLIM_INFINITY;
1449 	else
1450 		r.rlim_max = r32.rlim_max;
1451 	return do_prlimit(current, resource, &r, NULL);
1452 }
1453 
COMPAT_SYSCALL_DEFINE2(getrlimit,unsigned int,resource,struct compat_rlimit __user *,rlim)1454 COMPAT_SYSCALL_DEFINE2(getrlimit, unsigned int, resource,
1455 		       struct compat_rlimit __user *, rlim)
1456 {
1457 	struct rlimit r;
1458 	int ret;
1459 
1460 	ret = do_prlimit(current, resource, NULL, &r);
1461 	if (!ret) {
1462 		struct compat_rlimit r32;
1463 		if (r.rlim_cur > COMPAT_RLIM_INFINITY)
1464 			r32.rlim_cur = COMPAT_RLIM_INFINITY;
1465 		else
1466 			r32.rlim_cur = r.rlim_cur;
1467 		if (r.rlim_max > COMPAT_RLIM_INFINITY)
1468 			r32.rlim_max = COMPAT_RLIM_INFINITY;
1469 		else
1470 			r32.rlim_max = r.rlim_max;
1471 
1472 		if (copy_to_user(rlim, &r32, sizeof(struct compat_rlimit)))
1473 			return -EFAULT;
1474 	}
1475 	return ret;
1476 }
1477 
1478 #endif
1479 
1480 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1481 
1482 /*
1483  *	Back compatibility for getrlimit. Needed for some apps.
1484  */
SYSCALL_DEFINE2(old_getrlimit,unsigned int,resource,struct rlimit __user *,rlim)1485 SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1486 		struct rlimit __user *, rlim)
1487 {
1488 	struct rlimit x;
1489 	if (resource >= RLIM_NLIMITS)
1490 		return -EINVAL;
1491 
1492 	resource = array_index_nospec(resource, RLIM_NLIMITS);
1493 	task_lock(current->group_leader);
1494 	x = current->signal->rlim[resource];
1495 	task_unlock(current->group_leader);
1496 	if (x.rlim_cur > 0x7FFFFFFF)
1497 		x.rlim_cur = 0x7FFFFFFF;
1498 	if (x.rlim_max > 0x7FFFFFFF)
1499 		x.rlim_max = 0x7FFFFFFF;
1500 	return copy_to_user(rlim, &x, sizeof(x)) ? -EFAULT : 0;
1501 }
1502 
1503 #ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE2(old_getrlimit,unsigned int,resource,struct compat_rlimit __user *,rlim)1504 COMPAT_SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1505 		       struct compat_rlimit __user *, rlim)
1506 {
1507 	struct rlimit r;
1508 
1509 	if (resource >= RLIM_NLIMITS)
1510 		return -EINVAL;
1511 
1512 	resource = array_index_nospec(resource, RLIM_NLIMITS);
1513 	task_lock(current->group_leader);
1514 	r = current->signal->rlim[resource];
1515 	task_unlock(current->group_leader);
1516 	if (r.rlim_cur > 0x7FFFFFFF)
1517 		r.rlim_cur = 0x7FFFFFFF;
1518 	if (r.rlim_max > 0x7FFFFFFF)
1519 		r.rlim_max = 0x7FFFFFFF;
1520 
1521 	if (put_user(r.rlim_cur, &rlim->rlim_cur) ||
1522 	    put_user(r.rlim_max, &rlim->rlim_max))
1523 		return -EFAULT;
1524 	return 0;
1525 }
1526 #endif
1527 
1528 #endif
1529 
rlim64_is_infinity(__u64 rlim64)1530 static inline bool rlim64_is_infinity(__u64 rlim64)
1531 {
1532 #if BITS_PER_LONG < 64
1533 	return rlim64 >= ULONG_MAX;
1534 #else
1535 	return rlim64 == RLIM64_INFINITY;
1536 #endif
1537 }
1538 
rlim_to_rlim64(const struct rlimit * rlim,struct rlimit64 * rlim64)1539 static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1540 {
1541 	if (rlim->rlim_cur == RLIM_INFINITY)
1542 		rlim64->rlim_cur = RLIM64_INFINITY;
1543 	else
1544 		rlim64->rlim_cur = rlim->rlim_cur;
1545 	if (rlim->rlim_max == RLIM_INFINITY)
1546 		rlim64->rlim_max = RLIM64_INFINITY;
1547 	else
1548 		rlim64->rlim_max = rlim->rlim_max;
1549 }
1550 
rlim64_to_rlim(const struct rlimit64 * rlim64,struct rlimit * rlim)1551 static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1552 {
1553 	if (rlim64_is_infinity(rlim64->rlim_cur))
1554 		rlim->rlim_cur = RLIM_INFINITY;
1555 	else
1556 		rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1557 	if (rlim64_is_infinity(rlim64->rlim_max))
1558 		rlim->rlim_max = RLIM_INFINITY;
1559 	else
1560 		rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1561 }
1562 
1563 /* 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)1564 int do_prlimit(struct task_struct *tsk, unsigned int resource,
1565 		struct rlimit *new_rlim, struct rlimit *old_rlim)
1566 {
1567 	struct rlimit *rlim;
1568 	int retval = 0;
1569 
1570 	if (resource >= RLIM_NLIMITS)
1571 		return -EINVAL;
1572 	resource = array_index_nospec(resource, RLIM_NLIMITS);
1573 
1574 	if (new_rlim) {
1575 		if (new_rlim->rlim_cur > new_rlim->rlim_max)
1576 			return -EINVAL;
1577 		if (resource == RLIMIT_NOFILE &&
1578 				new_rlim->rlim_max > sysctl_nr_open)
1579 			return -EPERM;
1580 	}
1581 
1582 	/* protect tsk->signal and tsk->sighand from disappearing */
1583 	read_lock(&tasklist_lock);
1584 	if (!tsk->sighand) {
1585 		retval = -ESRCH;
1586 		goto out;
1587 	}
1588 
1589 	rlim = tsk->signal->rlim + resource;
1590 	task_lock(tsk->group_leader);
1591 	if (new_rlim) {
1592 		/* Keep the capable check against init_user_ns until
1593 		   cgroups can contain all limits */
1594 		if (new_rlim->rlim_max > rlim->rlim_max &&
1595 				!capable(CAP_SYS_RESOURCE))
1596 			retval = -EPERM;
1597 		if (!retval)
1598 			retval = security_task_setrlimit(tsk, resource, new_rlim);
1599 	}
1600 	if (!retval) {
1601 		if (old_rlim)
1602 			*old_rlim = *rlim;
1603 		if (new_rlim)
1604 			*rlim = *new_rlim;
1605 	}
1606 	task_unlock(tsk->group_leader);
1607 
1608 	/*
1609 	 * RLIMIT_CPU handling. Arm the posix CPU timer if the limit is not
1610 	 * infite. In case of RLIM_INFINITY the posix CPU timer code
1611 	 * ignores the rlimit.
1612 	 */
1613 	 if (!retval && new_rlim && resource == RLIMIT_CPU &&
1614 	     new_rlim->rlim_cur != RLIM_INFINITY &&
1615 	     IS_ENABLED(CONFIG_POSIX_TIMERS))
1616 		update_rlimit_cpu(tsk, new_rlim->rlim_cur);
1617 out:
1618 	read_unlock(&tasklist_lock);
1619 	return retval;
1620 }
1621 
1622 /* rcu lock must be held */
check_prlimit_permission(struct task_struct * task,unsigned int flags)1623 static int check_prlimit_permission(struct task_struct *task,
1624 				    unsigned int flags)
1625 {
1626 	const struct cred *cred = current_cred(), *tcred;
1627 	bool id_match;
1628 
1629 	if (current == task)
1630 		return 0;
1631 
1632 	tcred = __task_cred(task);
1633 	id_match = (uid_eq(cred->uid, tcred->euid) &&
1634 		    uid_eq(cred->uid, tcred->suid) &&
1635 		    uid_eq(cred->uid, tcred->uid)  &&
1636 		    gid_eq(cred->gid, tcred->egid) &&
1637 		    gid_eq(cred->gid, tcred->sgid) &&
1638 		    gid_eq(cred->gid, tcred->gid));
1639 	if (!id_match && !ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
1640 		return -EPERM;
1641 
1642 	return security_task_prlimit(cred, tcred, flags);
1643 }
1644 
SYSCALL_DEFINE4(prlimit64,pid_t,pid,unsigned int,resource,const struct rlimit64 __user *,new_rlim,struct rlimit64 __user *,old_rlim)1645 SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1646 		const struct rlimit64 __user *, new_rlim,
1647 		struct rlimit64 __user *, old_rlim)
1648 {
1649 	struct rlimit64 old64, new64;
1650 	struct rlimit old, new;
1651 	struct task_struct *tsk;
1652 	unsigned int checkflags = 0;
1653 	int ret;
1654 
1655 	if (old_rlim)
1656 		checkflags |= LSM_PRLIMIT_READ;
1657 
1658 	if (new_rlim) {
1659 		if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1660 			return -EFAULT;
1661 		rlim64_to_rlim(&new64, &new);
1662 		checkflags |= LSM_PRLIMIT_WRITE;
1663 	}
1664 
1665 	rcu_read_lock();
1666 	tsk = pid ? find_task_by_vpid(pid) : current;
1667 	if (!tsk) {
1668 		rcu_read_unlock();
1669 		return -ESRCH;
1670 	}
1671 	ret = check_prlimit_permission(tsk, checkflags);
1672 	if (ret) {
1673 		rcu_read_unlock();
1674 		return ret;
1675 	}
1676 	get_task_struct(tsk);
1677 	rcu_read_unlock();
1678 
1679 	ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1680 			old_rlim ? &old : NULL);
1681 
1682 	if (!ret && old_rlim) {
1683 		rlim_to_rlim64(&old, &old64);
1684 		if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1685 			ret = -EFAULT;
1686 	}
1687 
1688 	put_task_struct(tsk);
1689 	return ret;
1690 }
1691 
SYSCALL_DEFINE2(setrlimit,unsigned int,resource,struct rlimit __user *,rlim)1692 SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1693 {
1694 	struct rlimit new_rlim;
1695 
1696 	if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1697 		return -EFAULT;
1698 	return do_prlimit(current, resource, &new_rlim, NULL);
1699 }
1700 
1701 /*
1702  * It would make sense to put struct rusage in the task_struct,
1703  * except that would make the task_struct be *really big*.  After
1704  * task_struct gets moved into malloc'ed memory, it would
1705  * make sense to do this.  It will make moving the rest of the information
1706  * a lot simpler!  (Which we're not doing right now because we're not
1707  * measuring them yet).
1708  *
1709  * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1710  * races with threads incrementing their own counters.  But since word
1711  * reads are atomic, we either get new values or old values and we don't
1712  * care which for the sums.  We always take the siglock to protect reading
1713  * the c* fields from p->signal from races with exit.c updating those
1714  * fields when reaping, so a sample either gets all the additions of a
1715  * given child after it's reaped, or none so this sample is before reaping.
1716  *
1717  * Locking:
1718  * We need to take the siglock for CHILDEREN, SELF and BOTH
1719  * for  the cases current multithreaded, non-current single threaded
1720  * non-current multithreaded.  Thread traversal is now safe with
1721  * the siglock held.
1722  * Strictly speaking, we donot need to take the siglock if we are current and
1723  * single threaded,  as no one else can take our signal_struct away, no one
1724  * else can  reap the  children to update signal->c* counters, and no one else
1725  * can race with the signal-> fields. If we do not take any lock, the
1726  * signal-> fields could be read out of order while another thread was just
1727  * exiting. So we should  place a read memory barrier when we avoid the lock.
1728  * On the writer side,  write memory barrier is implied in  __exit_signal
1729  * as __exit_signal releases  the siglock spinlock after updating the signal->
1730  * fields. But we don't do this yet to keep things simple.
1731  *
1732  */
1733 
accumulate_thread_rusage(struct task_struct * t,struct rusage * r)1734 static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
1735 {
1736 	r->ru_nvcsw += t->nvcsw;
1737 	r->ru_nivcsw += t->nivcsw;
1738 	r->ru_minflt += t->min_flt;
1739 	r->ru_majflt += t->maj_flt;
1740 	r->ru_inblock += task_io_get_inblock(t);
1741 	r->ru_oublock += task_io_get_oublock(t);
1742 }
1743 
getrusage(struct task_struct * p,int who,struct rusage * r)1744 void getrusage(struct task_struct *p, int who, struct rusage *r)
1745 {
1746 	struct task_struct *t;
1747 	unsigned long flags;
1748 	u64 tgutime, tgstime, utime, stime;
1749 	unsigned long maxrss;
1750 	struct mm_struct *mm;
1751 	struct signal_struct *sig = p->signal;
1752 	unsigned int seq = 0;
1753 
1754 retry:
1755 	memset(r, 0, sizeof(*r));
1756 	utime = stime = 0;
1757 	maxrss = 0;
1758 
1759 	if (who == RUSAGE_THREAD) {
1760 		task_cputime_adjusted(current, &utime, &stime);
1761 		accumulate_thread_rusage(p, r);
1762 		maxrss = sig->maxrss;
1763 		goto out_thread;
1764 	}
1765 
1766 	flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
1767 
1768 	switch (who) {
1769 	case RUSAGE_BOTH:
1770 	case RUSAGE_CHILDREN:
1771 		utime = sig->cutime;
1772 		stime = sig->cstime;
1773 		r->ru_nvcsw = sig->cnvcsw;
1774 		r->ru_nivcsw = sig->cnivcsw;
1775 		r->ru_minflt = sig->cmin_flt;
1776 		r->ru_majflt = sig->cmaj_flt;
1777 		r->ru_inblock = sig->cinblock;
1778 		r->ru_oublock = sig->coublock;
1779 		maxrss = sig->cmaxrss;
1780 
1781 		if (who == RUSAGE_CHILDREN)
1782 			break;
1783 		fallthrough;
1784 
1785 	case RUSAGE_SELF:
1786 		r->ru_nvcsw += sig->nvcsw;
1787 		r->ru_nivcsw += sig->nivcsw;
1788 		r->ru_minflt += sig->min_flt;
1789 		r->ru_majflt += sig->maj_flt;
1790 		r->ru_inblock += sig->inblock;
1791 		r->ru_oublock += sig->oublock;
1792 		if (maxrss < sig->maxrss)
1793 			maxrss = sig->maxrss;
1794 
1795 		rcu_read_lock();
1796 		__for_each_thread(sig, t)
1797 			accumulate_thread_rusage(t, r);
1798 		rcu_read_unlock();
1799 
1800 		break;
1801 
1802 	default:
1803 		BUG();
1804 	}
1805 
1806 	if (need_seqretry(&sig->stats_lock, seq)) {
1807 		seq = 1;
1808 		goto retry;
1809 	}
1810 	done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
1811 
1812 	if (who == RUSAGE_CHILDREN)
1813 		goto out_children;
1814 
1815 	thread_group_cputime_adjusted(p, &tgutime, &tgstime);
1816 	utime += tgutime;
1817 	stime += tgstime;
1818 
1819 out_thread:
1820 	mm = get_task_mm(p);
1821 	if (mm) {
1822 		setmax_mm_hiwater_rss(&maxrss, mm);
1823 		mmput(mm);
1824 	}
1825 
1826 out_children:
1827 	r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
1828 	r->ru_utime = ns_to_kernel_old_timeval(utime);
1829 	r->ru_stime = ns_to_kernel_old_timeval(stime);
1830 }
1831 
SYSCALL_DEFINE2(getrusage,int,who,struct rusage __user *,ru)1832 SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
1833 {
1834 	struct rusage r;
1835 
1836 	if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1837 	    who != RUSAGE_THREAD)
1838 		return -EINVAL;
1839 
1840 	getrusage(current, who, &r);
1841 	return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1842 }
1843 
1844 #ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE2(getrusage,int,who,struct compat_rusage __user *,ru)1845 COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru)
1846 {
1847 	struct rusage r;
1848 
1849 	if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1850 	    who != RUSAGE_THREAD)
1851 		return -EINVAL;
1852 
1853 	getrusage(current, who, &r);
1854 	return put_compat_rusage(&r, ru);
1855 }
1856 #endif
1857 
SYSCALL_DEFINE1(umask,int,mask)1858 SYSCALL_DEFINE1(umask, int, mask)
1859 {
1860 	mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1861 	return mask;
1862 }
1863 
prctl_set_mm_exe_file(struct mm_struct * mm,unsigned int fd)1864 static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
1865 {
1866 	struct fd exe;
1867 	struct file *old_exe, *exe_file;
1868 	struct inode *inode;
1869 	int err;
1870 
1871 	exe = fdget(fd);
1872 	if (!exe.file)
1873 		return -EBADF;
1874 
1875 	inode = file_inode(exe.file);
1876 
1877 	/*
1878 	 * Because the original mm->exe_file points to executable file, make
1879 	 * sure that this one is executable as well, to avoid breaking an
1880 	 * overall picture.
1881 	 */
1882 	err = -EACCES;
1883 	if (!S_ISREG(inode->i_mode) || path_noexec(&exe.file->f_path))
1884 		goto exit;
1885 
1886 	err = inode_permission(inode, MAY_EXEC);
1887 	if (err)
1888 		goto exit;
1889 
1890 	/*
1891 	 * Forbid mm->exe_file change if old file still mapped.
1892 	 */
1893 	exe_file = get_mm_exe_file(mm);
1894 	err = -EBUSY;
1895 	if (exe_file) {
1896 		struct vm_area_struct *vma;
1897 
1898 		mmap_read_lock(mm);
1899 		for (vma = mm->mmap; vma; vma = vma->vm_next) {
1900 			if (!vma->vm_file)
1901 				continue;
1902 			if (path_equal(&vma->vm_file->f_path,
1903 				       &exe_file->f_path))
1904 				goto exit_err;
1905 		}
1906 
1907 		mmap_read_unlock(mm);
1908 		fput(exe_file);
1909 	}
1910 
1911 	err = 0;
1912 	/* set the new file, lockless */
1913 	get_file(exe.file);
1914 	old_exe = xchg(&mm->exe_file, exe.file);
1915 	if (old_exe)
1916 		fput(old_exe);
1917 exit:
1918 	fdput(exe);
1919 	return err;
1920 exit_err:
1921 	mmap_read_unlock(mm);
1922 	fput(exe_file);
1923 	goto exit;
1924 }
1925 
1926 /*
1927  * Check arithmetic relations of passed addresses.
1928  *
1929  * WARNING: we don't require any capability here so be very careful
1930  * in what is allowed for modification from userspace.
1931  */
validate_prctl_map_addr(struct prctl_mm_map * prctl_map)1932 static int validate_prctl_map_addr(struct prctl_mm_map *prctl_map)
1933 {
1934 	unsigned long mmap_max_addr = TASK_SIZE;
1935 	int error = -EINVAL, i;
1936 
1937 	static const unsigned char offsets[] = {
1938 		offsetof(struct prctl_mm_map, start_code),
1939 		offsetof(struct prctl_mm_map, end_code),
1940 		offsetof(struct prctl_mm_map, start_data),
1941 		offsetof(struct prctl_mm_map, end_data),
1942 		offsetof(struct prctl_mm_map, start_brk),
1943 		offsetof(struct prctl_mm_map, brk),
1944 		offsetof(struct prctl_mm_map, start_stack),
1945 		offsetof(struct prctl_mm_map, arg_start),
1946 		offsetof(struct prctl_mm_map, arg_end),
1947 		offsetof(struct prctl_mm_map, env_start),
1948 		offsetof(struct prctl_mm_map, env_end),
1949 	};
1950 
1951 	/*
1952 	 * Make sure the members are not somewhere outside
1953 	 * of allowed address space.
1954 	 */
1955 	for (i = 0; i < ARRAY_SIZE(offsets); i++) {
1956 		u64 val = *(u64 *)((char *)prctl_map + offsets[i]);
1957 
1958 		if ((unsigned long)val >= mmap_max_addr ||
1959 		    (unsigned long)val < mmap_min_addr)
1960 			goto out;
1961 	}
1962 
1963 	/*
1964 	 * Make sure the pairs are ordered.
1965 	 */
1966 #define __prctl_check_order(__m1, __op, __m2)				\
1967 	((unsigned long)prctl_map->__m1 __op				\
1968 	 (unsigned long)prctl_map->__m2) ? 0 : -EINVAL
1969 	error  = __prctl_check_order(start_code, <, end_code);
1970 	error |= __prctl_check_order(start_data,<=, end_data);
1971 	error |= __prctl_check_order(start_brk, <=, brk);
1972 	error |= __prctl_check_order(arg_start, <=, arg_end);
1973 	error |= __prctl_check_order(env_start, <=, env_end);
1974 	if (error)
1975 		goto out;
1976 #undef __prctl_check_order
1977 
1978 	error = -EINVAL;
1979 
1980 	/*
1981 	 * Neither we should allow to override limits if they set.
1982 	 */
1983 	if (check_data_rlimit(rlimit(RLIMIT_DATA), prctl_map->brk,
1984 			      prctl_map->start_brk, prctl_map->end_data,
1985 			      prctl_map->start_data))
1986 			goto out;
1987 
1988 	error = 0;
1989 out:
1990 	return error;
1991 }
1992 
1993 #ifdef CONFIG_CHECKPOINT_RESTORE
prctl_set_mm_map(int opt,const void __user * addr,unsigned long data_size)1994 static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data_size)
1995 {
1996 	struct prctl_mm_map prctl_map = { .exe_fd = (u32)-1, };
1997 	unsigned long user_auxv[AT_VECTOR_SIZE];
1998 	struct mm_struct *mm = current->mm;
1999 	int error;
2000 
2001 	BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
2002 	BUILD_BUG_ON(sizeof(struct prctl_mm_map) > 256);
2003 
2004 	if (opt == PR_SET_MM_MAP_SIZE)
2005 		return put_user((unsigned int)sizeof(prctl_map),
2006 				(unsigned int __user *)addr);
2007 
2008 	if (data_size != sizeof(prctl_map))
2009 		return -EINVAL;
2010 
2011 	if (copy_from_user(&prctl_map, addr, sizeof(prctl_map)))
2012 		return -EFAULT;
2013 
2014 	error = validate_prctl_map_addr(&prctl_map);
2015 	if (error)
2016 		return error;
2017 
2018 	if (prctl_map.auxv_size) {
2019 		/*
2020 		 * Someone is trying to cheat the auxv vector.
2021 		 */
2022 		if (!prctl_map.auxv ||
2023 				prctl_map.auxv_size > sizeof(mm->saved_auxv))
2024 			return -EINVAL;
2025 
2026 		memset(user_auxv, 0, sizeof(user_auxv));
2027 		if (copy_from_user(user_auxv,
2028 				   (const void __user *)prctl_map.auxv,
2029 				   prctl_map.auxv_size))
2030 			return -EFAULT;
2031 
2032 		/* Last entry must be AT_NULL as specification requires */
2033 		user_auxv[AT_VECTOR_SIZE - 2] = AT_NULL;
2034 		user_auxv[AT_VECTOR_SIZE - 1] = AT_NULL;
2035 	}
2036 
2037 	if (prctl_map.exe_fd != (u32)-1) {
2038 		/*
2039 		 * Check if the current user is checkpoint/restore capable.
2040 		 * At the time of this writing, it checks for CAP_SYS_ADMIN
2041 		 * or CAP_CHECKPOINT_RESTORE.
2042 		 * Note that a user with access to ptrace can masquerade an
2043 		 * arbitrary program as any executable, even setuid ones.
2044 		 * This may have implications in the tomoyo subsystem.
2045 		 */
2046 		if (!checkpoint_restore_ns_capable(current_user_ns()))
2047 			return -EPERM;
2048 
2049 		error = prctl_set_mm_exe_file(mm, prctl_map.exe_fd);
2050 		if (error)
2051 			return error;
2052 	}
2053 
2054 	/*
2055 	 * arg_lock protects concurent updates but we still need mmap_lock for
2056 	 * read to exclude races with sys_brk.
2057 	 */
2058 	mmap_read_lock(mm);
2059 
2060 	/*
2061 	 * We don't validate if these members are pointing to
2062 	 * real present VMAs because application may have correspond
2063 	 * VMAs already unmapped and kernel uses these members for statistics
2064 	 * output in procfs mostly, except
2065 	 *
2066 	 *  - @start_brk/@brk which are used in do_brk_flags but kernel lookups
2067 	 *    for VMAs when updating these memvers so anything wrong written
2068 	 *    here cause kernel to swear at userspace program but won't lead
2069 	 *    to any problem in kernel itself
2070 	 */
2071 
2072 	spin_lock(&mm->arg_lock);
2073 	mm->start_code	= prctl_map.start_code;
2074 	mm->end_code	= prctl_map.end_code;
2075 	mm->start_data	= prctl_map.start_data;
2076 	mm->end_data	= prctl_map.end_data;
2077 	mm->start_brk	= prctl_map.start_brk;
2078 	mm->brk		= prctl_map.brk;
2079 	mm->start_stack	= prctl_map.start_stack;
2080 	mm->arg_start	= prctl_map.arg_start;
2081 	mm->arg_end	= prctl_map.arg_end;
2082 	mm->env_start	= prctl_map.env_start;
2083 	mm->env_end	= prctl_map.env_end;
2084 	spin_unlock(&mm->arg_lock);
2085 
2086 	/*
2087 	 * Note this update of @saved_auxv is lockless thus
2088 	 * if someone reads this member in procfs while we're
2089 	 * updating -- it may get partly updated results. It's
2090 	 * known and acceptable trade off: we leave it as is to
2091 	 * not introduce additional locks here making the kernel
2092 	 * more complex.
2093 	 */
2094 	if (prctl_map.auxv_size)
2095 		memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
2096 
2097 	mmap_read_unlock(mm);
2098 	return 0;
2099 }
2100 #endif /* CONFIG_CHECKPOINT_RESTORE */
2101 
prctl_set_auxv(struct mm_struct * mm,unsigned long addr,unsigned long len)2102 static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr,
2103 			  unsigned long len)
2104 {
2105 	/*
2106 	 * This doesn't move the auxiliary vector itself since it's pinned to
2107 	 * mm_struct, but it permits filling the vector with new values.  It's
2108 	 * up to the caller to provide sane values here, otherwise userspace
2109 	 * tools which use this vector might be unhappy.
2110 	 */
2111 	unsigned long user_auxv[AT_VECTOR_SIZE];
2112 
2113 	if (len > sizeof(user_auxv))
2114 		return -EINVAL;
2115 
2116 	if (copy_from_user(user_auxv, (const void __user *)addr, len))
2117 		return -EFAULT;
2118 
2119 	/* Make sure the last entry is always AT_NULL */
2120 	user_auxv[AT_VECTOR_SIZE - 2] = 0;
2121 	user_auxv[AT_VECTOR_SIZE - 1] = 0;
2122 
2123 	BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
2124 
2125 	task_lock(current);
2126 	memcpy(mm->saved_auxv, user_auxv, len);
2127 	task_unlock(current);
2128 
2129 	return 0;
2130 }
2131 
prctl_set_mm(int opt,unsigned long addr,unsigned long arg4,unsigned long arg5)2132 static int prctl_set_mm(int opt, unsigned long addr,
2133 			unsigned long arg4, unsigned long arg5)
2134 {
2135 	struct mm_struct *mm = current->mm;
2136 	struct prctl_mm_map prctl_map = {
2137 		.auxv = NULL,
2138 		.auxv_size = 0,
2139 		.exe_fd = -1,
2140 	};
2141 	struct vm_area_struct *vma;
2142 	int error;
2143 
2144 	if (arg5 || (arg4 && (opt != PR_SET_MM_AUXV &&
2145 			      opt != PR_SET_MM_MAP &&
2146 			      opt != PR_SET_MM_MAP_SIZE)))
2147 		return -EINVAL;
2148 
2149 #ifdef CONFIG_CHECKPOINT_RESTORE
2150 	if (opt == PR_SET_MM_MAP || opt == PR_SET_MM_MAP_SIZE)
2151 		return prctl_set_mm_map(opt, (const void __user *)addr, arg4);
2152 #endif
2153 
2154 	if (!capable(CAP_SYS_RESOURCE))
2155 		return -EPERM;
2156 
2157 	if (opt == PR_SET_MM_EXE_FILE)
2158 		return prctl_set_mm_exe_file(mm, (unsigned int)addr);
2159 
2160 	if (opt == PR_SET_MM_AUXV)
2161 		return prctl_set_auxv(mm, addr, arg4);
2162 
2163 	if (addr >= TASK_SIZE || addr < mmap_min_addr)
2164 		return -EINVAL;
2165 
2166 	error = -EINVAL;
2167 
2168 	/*
2169 	 * arg_lock protects concurent updates of arg boundaries, we need
2170 	 * mmap_lock for a) concurrent sys_brk, b) finding VMA for addr
2171 	 * validation.
2172 	 */
2173 	mmap_read_lock(mm);
2174 	vma = find_vma(mm, addr);
2175 
2176 	spin_lock(&mm->arg_lock);
2177 	prctl_map.start_code	= mm->start_code;
2178 	prctl_map.end_code	= mm->end_code;
2179 	prctl_map.start_data	= mm->start_data;
2180 	prctl_map.end_data	= mm->end_data;
2181 	prctl_map.start_brk	= mm->start_brk;
2182 	prctl_map.brk		= mm->brk;
2183 	prctl_map.start_stack	= mm->start_stack;
2184 	prctl_map.arg_start	= mm->arg_start;
2185 	prctl_map.arg_end	= mm->arg_end;
2186 	prctl_map.env_start	= mm->env_start;
2187 	prctl_map.env_end	= mm->env_end;
2188 
2189 	switch (opt) {
2190 	case PR_SET_MM_START_CODE:
2191 		prctl_map.start_code = addr;
2192 		break;
2193 	case PR_SET_MM_END_CODE:
2194 		prctl_map.end_code = addr;
2195 		break;
2196 	case PR_SET_MM_START_DATA:
2197 		prctl_map.start_data = addr;
2198 		break;
2199 	case PR_SET_MM_END_DATA:
2200 		prctl_map.end_data = addr;
2201 		break;
2202 	case PR_SET_MM_START_STACK:
2203 		prctl_map.start_stack = addr;
2204 		break;
2205 	case PR_SET_MM_START_BRK:
2206 		prctl_map.start_brk = addr;
2207 		break;
2208 	case PR_SET_MM_BRK:
2209 		prctl_map.brk = addr;
2210 		break;
2211 	case PR_SET_MM_ARG_START:
2212 		prctl_map.arg_start = addr;
2213 		break;
2214 	case PR_SET_MM_ARG_END:
2215 		prctl_map.arg_end = addr;
2216 		break;
2217 	case PR_SET_MM_ENV_START:
2218 		prctl_map.env_start = addr;
2219 		break;
2220 	case PR_SET_MM_ENV_END:
2221 		prctl_map.env_end = addr;
2222 		break;
2223 	default:
2224 		goto out;
2225 	}
2226 
2227 	error = validate_prctl_map_addr(&prctl_map);
2228 	if (error)
2229 		goto out;
2230 
2231 	switch (opt) {
2232 	/*
2233 	 * If command line arguments and environment
2234 	 * are placed somewhere else on stack, we can
2235 	 * set them up here, ARG_START/END to setup
2236 	 * command line argumets and ENV_START/END
2237 	 * for environment.
2238 	 */
2239 	case PR_SET_MM_START_STACK:
2240 	case PR_SET_MM_ARG_START:
2241 	case PR_SET_MM_ARG_END:
2242 	case PR_SET_MM_ENV_START:
2243 	case PR_SET_MM_ENV_END:
2244 		if (!vma) {
2245 			error = -EFAULT;
2246 			goto out;
2247 		}
2248 	}
2249 
2250 	mm->start_code	= prctl_map.start_code;
2251 	mm->end_code	= prctl_map.end_code;
2252 	mm->start_data	= prctl_map.start_data;
2253 	mm->end_data	= prctl_map.end_data;
2254 	mm->start_brk	= prctl_map.start_brk;
2255 	mm->brk		= prctl_map.brk;
2256 	mm->start_stack	= prctl_map.start_stack;
2257 	mm->arg_start	= prctl_map.arg_start;
2258 	mm->arg_end	= prctl_map.arg_end;
2259 	mm->env_start	= prctl_map.env_start;
2260 	mm->env_end	= prctl_map.env_end;
2261 
2262 	error = 0;
2263 out:
2264 	spin_unlock(&mm->arg_lock);
2265 	mmap_read_unlock(mm);
2266 	return error;
2267 }
2268 
2269 #ifdef CONFIG_CHECKPOINT_RESTORE
prctl_get_tid_address(struct task_struct * me,int __user * __user * tid_addr)2270 static int prctl_get_tid_address(struct task_struct *me, int __user * __user *tid_addr)
2271 {
2272 	return put_user(me->clear_child_tid, tid_addr);
2273 }
2274 #else
prctl_get_tid_address(struct task_struct * me,int __user * __user * tid_addr)2275 static int prctl_get_tid_address(struct task_struct *me, int __user * __user *tid_addr)
2276 {
2277 	return -EINVAL;
2278 }
2279 #endif
2280 
propagate_has_child_subreaper(struct task_struct * p,void * data)2281 static int propagate_has_child_subreaper(struct task_struct *p, void *data)
2282 {
2283 	/*
2284 	 * If task has has_child_subreaper - all its decendants
2285 	 * already have these flag too and new decendants will
2286 	 * inherit it on fork, skip them.
2287 	 *
2288 	 * If we've found child_reaper - skip descendants in
2289 	 * it's subtree as they will never get out pidns.
2290 	 */
2291 	if (p->signal->has_child_subreaper ||
2292 	    is_child_reaper(task_pid(p)))
2293 		return 0;
2294 
2295 	p->signal->has_child_subreaper = 1;
2296 	return 1;
2297 }
2298 
arch_prctl_spec_ctrl_get(struct task_struct * t,unsigned long which)2299 int __weak arch_prctl_spec_ctrl_get(struct task_struct *t, unsigned long which)
2300 {
2301 	return -EINVAL;
2302 }
2303 
arch_prctl_spec_ctrl_set(struct task_struct * t,unsigned long which,unsigned long ctrl)2304 int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which,
2305 				    unsigned long ctrl)
2306 {
2307 	return -EINVAL;
2308 }
2309 
2310 #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)2311 static int prctl_update_vma_anon_name(struct vm_area_struct *vma,
2312 		struct vm_area_struct **prev,
2313 		unsigned long start, unsigned long end,
2314 		const char __user *name_addr)
2315 {
2316 	struct mm_struct *mm = vma->vm_mm;
2317 	int error = 0;
2318 	pgoff_t pgoff;
2319 
2320 	if (name_addr == vma_get_anon_name(vma)) {
2321 		*prev = vma;
2322 		goto out;
2323 	}
2324 
2325 	pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
2326 	*prev = vma_merge(mm, *prev, start, end, vma->vm_flags, vma->anon_vma,
2327 				vma->vm_file, pgoff, vma_policy(vma),
2328 				vma->vm_userfaultfd_ctx, name_addr);
2329 	if (*prev) {
2330 		vma = *prev;
2331 		goto success;
2332 	}
2333 
2334 	*prev = vma;
2335 
2336 	if (start != vma->vm_start) {
2337 		error = split_vma(mm, vma, start, 1);
2338 		if (error)
2339 			goto out;
2340 	}
2341 
2342 	if (end != vma->vm_end) {
2343 		error = split_vma(mm, vma, end, 0);
2344 		if (error)
2345 			goto out;
2346 	}
2347 
2348 success:
2349 	if (!vma->vm_file)
2350 		vma->anon_name = name_addr;
2351 
2352 out:
2353 	if (error == -ENOMEM)
2354 		error = -EAGAIN;
2355 	return error;
2356 }
2357 
prctl_set_vma_anon_name(unsigned long start,unsigned long end,unsigned long arg)2358 static int prctl_set_vma_anon_name(unsigned long start, unsigned long end,
2359 			unsigned long arg)
2360 {
2361 	unsigned long tmp;
2362 	struct vm_area_struct *vma, *prev;
2363 	int unmapped_error = 0;
2364 	int error = -EINVAL;
2365 
2366 	/*
2367 	 * If the interval [start,end) covers some unmapped address
2368 	 * ranges, just ignore them, but return -ENOMEM at the end.
2369 	 * - this matches the handling in madvise.
2370 	 */
2371 	vma = find_vma_prev(current->mm, start, &prev);
2372 	if (vma && start > vma->vm_start)
2373 		prev = vma;
2374 
2375 	for (;;) {
2376 		/* Still start < end. */
2377 		error = -ENOMEM;
2378 		if (!vma)
2379 			return error;
2380 
2381 		/* Here start < (end|vma->vm_end). */
2382 		if (start < vma->vm_start) {
2383 			unmapped_error = -ENOMEM;
2384 			start = vma->vm_start;
2385 			if (start >= end)
2386 				return error;
2387 		}
2388 
2389 		/* Here vma->vm_start <= start < (end|vma->vm_end) */
2390 		tmp = vma->vm_end;
2391 		if (end < tmp)
2392 			tmp = end;
2393 
2394 		/* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
2395 		error = prctl_update_vma_anon_name(vma, &prev, start, tmp,
2396 				(const char __user *)arg);
2397 		if (error)
2398 			return error;
2399 		start = tmp;
2400 		if (prev && start < prev->vm_end)
2401 			start = prev->vm_end;
2402 		error = unmapped_error;
2403 		if (start >= end)
2404 			return error;
2405 		if (prev)
2406 			vma = prev->vm_next;
2407 		else	/* madvise_remove dropped mmap_lock */
2408 			vma = find_vma(current->mm, start);
2409 	}
2410 }
2411 
prctl_set_vma(unsigned long opt,unsigned long start,unsigned long len_in,unsigned long arg)2412 static int prctl_set_vma(unsigned long opt, unsigned long start,
2413 		unsigned long len_in, unsigned long arg)
2414 {
2415 	struct mm_struct *mm = current->mm;
2416 	int error;
2417 	unsigned long len;
2418 	unsigned long end;
2419 
2420 	if (start & ~PAGE_MASK)
2421 		return -EINVAL;
2422 	len = (len_in + ~PAGE_MASK) & PAGE_MASK;
2423 
2424 	/* Check to see whether len was rounded up from small -ve to zero */
2425 	if (len_in && !len)
2426 		return -EINVAL;
2427 
2428 	end = start + len;
2429 	if (end < start)
2430 		return -EINVAL;
2431 
2432 	if (end == start)
2433 		return 0;
2434 
2435 	mmap_write_lock(mm);
2436 
2437 	switch (opt) {
2438 	case PR_SET_VMA_ANON_NAME:
2439 		error = prctl_set_vma_anon_name(start, end, arg);
2440 		break;
2441 	default:
2442 		error = -EINVAL;
2443 	}
2444 
2445 	mmap_write_unlock(mm);
2446 
2447 	return error;
2448 }
2449 #else /* CONFIG_MMU */
prctl_set_vma(unsigned long opt,unsigned long start,unsigned long len_in,unsigned long arg)2450 static int prctl_set_vma(unsigned long opt, unsigned long start,
2451 		unsigned long len_in, unsigned long arg)
2452 {
2453 	return -EINVAL;
2454 }
2455 #endif
2456 
2457 #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE)
2458 
SYSCALL_DEFINE5(prctl,int,option,unsigned long,arg2,unsigned long,arg3,unsigned long,arg4,unsigned long,arg5)2459 SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
2460 		unsigned long, arg4, unsigned long, arg5)
2461 {
2462 	struct task_struct *me = current;
2463 	unsigned char comm[sizeof(me->comm)];
2464 	long error;
2465 
2466 	error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2467 	if (error != -ENOSYS)
2468 		return error;
2469 
2470 	error = 0;
2471 	switch (option) {
2472 	case PR_SET_PDEATHSIG:
2473 		if (!valid_signal(arg2)) {
2474 			error = -EINVAL;
2475 			break;
2476 		}
2477 		me->pdeath_signal = arg2;
2478 		break;
2479 	case PR_GET_PDEATHSIG:
2480 		error = put_user(me->pdeath_signal, (int __user *)arg2);
2481 		break;
2482 	case PR_GET_DUMPABLE:
2483 		error = get_dumpable(me->mm);
2484 		break;
2485 	case PR_SET_DUMPABLE:
2486 		if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) {
2487 			error = -EINVAL;
2488 			break;
2489 		}
2490 		set_dumpable(me->mm, arg2);
2491 		break;
2492 
2493 	case PR_SET_UNALIGN:
2494 		error = SET_UNALIGN_CTL(me, arg2);
2495 		break;
2496 	case PR_GET_UNALIGN:
2497 		error = GET_UNALIGN_CTL(me, arg2);
2498 		break;
2499 	case PR_SET_FPEMU:
2500 		error = SET_FPEMU_CTL(me, arg2);
2501 		break;
2502 	case PR_GET_FPEMU:
2503 		error = GET_FPEMU_CTL(me, arg2);
2504 		break;
2505 	case PR_SET_FPEXC:
2506 		error = SET_FPEXC_CTL(me, arg2);
2507 		break;
2508 	case PR_GET_FPEXC:
2509 		error = GET_FPEXC_CTL(me, arg2);
2510 		break;
2511 	case PR_GET_TIMING:
2512 		error = PR_TIMING_STATISTICAL;
2513 		break;
2514 	case PR_SET_TIMING:
2515 		if (arg2 != PR_TIMING_STATISTICAL)
2516 			error = -EINVAL;
2517 		break;
2518 	case PR_SET_NAME:
2519 		comm[sizeof(me->comm) - 1] = 0;
2520 		if (strncpy_from_user(comm, (char __user *)arg2,
2521 				      sizeof(me->comm) - 1) < 0)
2522 			return -EFAULT;
2523 		set_task_comm(me, comm);
2524 		proc_comm_connector(me);
2525 		break;
2526 	case PR_GET_NAME:
2527 		get_task_comm(comm, me);
2528 		if (copy_to_user((char __user *)arg2, comm, sizeof(comm)))
2529 			return -EFAULT;
2530 		break;
2531 	case PR_GET_ENDIAN:
2532 		error = GET_ENDIAN(me, arg2);
2533 		break;
2534 	case PR_SET_ENDIAN:
2535 		error = SET_ENDIAN(me, arg2);
2536 		break;
2537 	case PR_GET_SECCOMP:
2538 		error = prctl_get_seccomp();
2539 		break;
2540 	case PR_SET_SECCOMP:
2541 		error = prctl_set_seccomp(arg2, (char __user *)arg3);
2542 		break;
2543 	case PR_GET_TSC:
2544 		error = GET_TSC_CTL(arg2);
2545 		break;
2546 	case PR_SET_TSC:
2547 		error = SET_TSC_CTL(arg2);
2548 		break;
2549 	case PR_TASK_PERF_EVENTS_DISABLE:
2550 		error = perf_event_task_disable();
2551 		break;
2552 	case PR_TASK_PERF_EVENTS_ENABLE:
2553 		error = perf_event_task_enable();
2554 		break;
2555 	case PR_GET_TIMERSLACK:
2556 		if (current->timer_slack_ns > ULONG_MAX)
2557 			error = ULONG_MAX;
2558 		else
2559 			error = current->timer_slack_ns;
2560 		break;
2561 	case PR_SET_TIMERSLACK:
2562 		if (arg2 <= 0)
2563 			current->timer_slack_ns =
2564 					current->default_timer_slack_ns;
2565 		else
2566 			current->timer_slack_ns = arg2;
2567 		break;
2568 	case PR_MCE_KILL:
2569 		if (arg4 | arg5)
2570 			return -EINVAL;
2571 		switch (arg2) {
2572 		case PR_MCE_KILL_CLEAR:
2573 			if (arg3 != 0)
2574 				return -EINVAL;
2575 			current->flags &= ~PF_MCE_PROCESS;
2576 			break;
2577 		case PR_MCE_KILL_SET:
2578 			current->flags |= PF_MCE_PROCESS;
2579 			if (arg3 == PR_MCE_KILL_EARLY)
2580 				current->flags |= PF_MCE_EARLY;
2581 			else if (arg3 == PR_MCE_KILL_LATE)
2582 				current->flags &= ~PF_MCE_EARLY;
2583 			else if (arg3 == PR_MCE_KILL_DEFAULT)
2584 				current->flags &=
2585 						~(PF_MCE_EARLY|PF_MCE_PROCESS);
2586 			else
2587 				return -EINVAL;
2588 			break;
2589 		default:
2590 			return -EINVAL;
2591 		}
2592 		break;
2593 	case PR_MCE_KILL_GET:
2594 		if (arg2 | arg3 | arg4 | arg5)
2595 			return -EINVAL;
2596 		if (current->flags & PF_MCE_PROCESS)
2597 			error = (current->flags & PF_MCE_EARLY) ?
2598 				PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
2599 		else
2600 			error = PR_MCE_KILL_DEFAULT;
2601 		break;
2602 	case PR_SET_MM:
2603 		error = prctl_set_mm(arg2, arg3, arg4, arg5);
2604 		break;
2605 	case PR_GET_TID_ADDRESS:
2606 		error = prctl_get_tid_address(me, (int __user * __user *)arg2);
2607 		break;
2608 	case PR_SET_CHILD_SUBREAPER:
2609 		me->signal->is_child_subreaper = !!arg2;
2610 		if (!arg2)
2611 			break;
2612 
2613 		walk_process_tree(me, propagate_has_child_subreaper, NULL);
2614 		break;
2615 	case PR_GET_CHILD_SUBREAPER:
2616 		error = put_user(me->signal->is_child_subreaper,
2617 				 (int __user *)arg2);
2618 		break;
2619 	case PR_SET_NO_NEW_PRIVS:
2620 		if (arg2 != 1 || arg3 || arg4 || arg5)
2621 			return -EINVAL;
2622 
2623 		task_set_no_new_privs(current);
2624 		break;
2625 	case PR_GET_NO_NEW_PRIVS:
2626 		if (arg2 || arg3 || arg4 || arg5)
2627 			return -EINVAL;
2628 		return task_no_new_privs(current) ? 1 : 0;
2629 	case PR_GET_THP_DISABLE:
2630 		if (arg2 || arg3 || arg4 || arg5)
2631 			return -EINVAL;
2632 		error = !!test_bit(MMF_DISABLE_THP, &me->mm->flags);
2633 		break;
2634 	case PR_SET_THP_DISABLE:
2635 		if (arg3 || arg4 || arg5)
2636 			return -EINVAL;
2637 		if (mmap_write_lock_killable(me->mm))
2638 			return -EINTR;
2639 		if (arg2)
2640 			set_bit(MMF_DISABLE_THP, &me->mm->flags);
2641 		else
2642 			clear_bit(MMF_DISABLE_THP, &me->mm->flags);
2643 		mmap_write_unlock(me->mm);
2644 		break;
2645 	case PR_MPX_ENABLE_MANAGEMENT:
2646 	case PR_MPX_DISABLE_MANAGEMENT:
2647 		/* No longer implemented: */
2648 		return -EINVAL;
2649 	case PR_SET_FP_MODE:
2650 		error = SET_FP_MODE(me, arg2);
2651 		break;
2652 	case PR_GET_FP_MODE:
2653 		error = GET_FP_MODE(me);
2654 		break;
2655 	case PR_SVE_SET_VL:
2656 		error = SVE_SET_VL(arg2);
2657 		break;
2658 	case PR_SVE_GET_VL:
2659 		error = SVE_GET_VL();
2660 		break;
2661 	case PR_GET_SPECULATION_CTRL:
2662 		if (arg3 || arg4 || arg5)
2663 			return -EINVAL;
2664 		error = arch_prctl_spec_ctrl_get(me, arg2);
2665 		break;
2666 	case PR_SET_SPECULATION_CTRL:
2667 		if (arg4 || arg5)
2668 			return -EINVAL;
2669 		error = arch_prctl_spec_ctrl_set(me, arg2, arg3);
2670 		break;
2671 	case PR_SET_VMA:
2672 		error = prctl_set_vma(arg2, arg3, arg4, arg5);
2673 		break;
2674 	case PR_PAC_RESET_KEYS:
2675 		if (arg3 || arg4 || arg5)
2676 			return -EINVAL;
2677 		error = PAC_RESET_KEYS(me, arg2);
2678 		break;
2679 	case PR_PAC_SET_ENABLED_KEYS:
2680 		if (arg4 || arg5)
2681 			return -EINVAL;
2682 		error = PAC_SET_ENABLED_KEYS(me, arg2, arg3);
2683 		break;
2684 	case PR_PAC_GET_ENABLED_KEYS:
2685 		if (arg2 || arg3 || arg4 || arg5)
2686 			return -EINVAL;
2687 		error = PAC_GET_ENABLED_KEYS(me);
2688 		break;
2689 	case PR_SET_TAGGED_ADDR_CTRL:
2690 		if (arg3 || arg4 || arg5)
2691 			return -EINVAL;
2692 		error = SET_TAGGED_ADDR_CTRL(arg2);
2693 		break;
2694 	case PR_GET_TAGGED_ADDR_CTRL:
2695 		if (arg2 || arg3 || arg4 || arg5)
2696 			return -EINVAL;
2697 		error = GET_TAGGED_ADDR_CTRL();
2698 		break;
2699 	case PR_SET_IO_FLUSHER:
2700 		if (!capable(CAP_SYS_RESOURCE))
2701 			return -EPERM;
2702 
2703 		if (arg3 || arg4 || arg5)
2704 			return -EINVAL;
2705 
2706 		if (arg2 == 1)
2707 			current->flags |= PR_IO_FLUSHER;
2708 		else if (!arg2)
2709 			current->flags &= ~PR_IO_FLUSHER;
2710 		else
2711 			return -EINVAL;
2712 		break;
2713 	case PR_GET_IO_FLUSHER:
2714 		if (!capable(CAP_SYS_RESOURCE))
2715 			return -EPERM;
2716 
2717 		if (arg2 || arg3 || arg4 || arg5)
2718 			return -EINVAL;
2719 
2720 		error = (current->flags & PR_IO_FLUSHER) == PR_IO_FLUSHER;
2721 		break;
2722 	default:
2723 		error = -EINVAL;
2724 		break;
2725 	}
2726 	trace_android_vh_syscall_prctl_finished(option, me);
2727 	return error;
2728 }
2729 
SYSCALL_DEFINE3(getcpu,unsigned __user *,cpup,unsigned __user *,nodep,struct getcpu_cache __user *,unused)2730 SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
2731 		struct getcpu_cache __user *, unused)
2732 {
2733 	int err = 0;
2734 	int cpu = raw_smp_processor_id();
2735 
2736 	if (cpup)
2737 		err |= put_user(cpu, cpup);
2738 	if (nodep)
2739 		err |= put_user(cpu_to_node(cpu), nodep);
2740 	return err ? -EFAULT : 0;
2741 }
2742 
2743 /**
2744  * do_sysinfo - fill in sysinfo struct
2745  * @info: pointer to buffer to fill
2746  */
do_sysinfo(struct sysinfo * info)2747 static int do_sysinfo(struct sysinfo *info)
2748 {
2749 	unsigned long mem_total, sav_total;
2750 	unsigned int mem_unit, bitcount;
2751 	struct timespec64 tp;
2752 
2753 	memset(info, 0, sizeof(struct sysinfo));
2754 
2755 	ktime_get_boottime_ts64(&tp);
2756 	timens_add_boottime(&tp);
2757 	info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
2758 
2759 	get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
2760 
2761 	info->procs = nr_threads;
2762 
2763 	si_meminfo(info);
2764 	si_swapinfo(info);
2765 
2766 	/*
2767 	 * If the sum of all the available memory (i.e. ram + swap)
2768 	 * is less than can be stored in a 32 bit unsigned long then
2769 	 * we can be binary compatible with 2.2.x kernels.  If not,
2770 	 * well, in that case 2.2.x was broken anyways...
2771 	 *
2772 	 *  -Erik Andersen <andersee@debian.org>
2773 	 */
2774 
2775 	mem_total = info->totalram + info->totalswap;
2776 	if (mem_total < info->totalram || mem_total < info->totalswap)
2777 		goto out;
2778 	bitcount = 0;
2779 	mem_unit = info->mem_unit;
2780 	while (mem_unit > 1) {
2781 		bitcount++;
2782 		mem_unit >>= 1;
2783 		sav_total = mem_total;
2784 		mem_total <<= 1;
2785 		if (mem_total < sav_total)
2786 			goto out;
2787 	}
2788 
2789 	/*
2790 	 * If mem_total did not overflow, multiply all memory values by
2791 	 * info->mem_unit and set it to 1.  This leaves things compatible
2792 	 * with 2.2.x, and also retains compatibility with earlier 2.4.x
2793 	 * kernels...
2794 	 */
2795 
2796 	info->mem_unit = 1;
2797 	info->totalram <<= bitcount;
2798 	info->freeram <<= bitcount;
2799 	info->sharedram <<= bitcount;
2800 	info->bufferram <<= bitcount;
2801 	info->totalswap <<= bitcount;
2802 	info->freeswap <<= bitcount;
2803 	info->totalhigh <<= bitcount;
2804 	info->freehigh <<= bitcount;
2805 
2806 out:
2807 	return 0;
2808 }
2809 
SYSCALL_DEFINE1(sysinfo,struct sysinfo __user *,info)2810 SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
2811 {
2812 	struct sysinfo val;
2813 
2814 	do_sysinfo(&val);
2815 
2816 	if (copy_to_user(info, &val, sizeof(struct sysinfo)))
2817 		return -EFAULT;
2818 
2819 	return 0;
2820 }
2821 
2822 #ifdef CONFIG_COMPAT
2823 struct compat_sysinfo {
2824 	s32 uptime;
2825 	u32 loads[3];
2826 	u32 totalram;
2827 	u32 freeram;
2828 	u32 sharedram;
2829 	u32 bufferram;
2830 	u32 totalswap;
2831 	u32 freeswap;
2832 	u16 procs;
2833 	u16 pad;
2834 	u32 totalhigh;
2835 	u32 freehigh;
2836 	u32 mem_unit;
2837 	char _f[20-2*sizeof(u32)-sizeof(int)];
2838 };
2839 
COMPAT_SYSCALL_DEFINE1(sysinfo,struct compat_sysinfo __user *,info)2840 COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
2841 {
2842 	struct sysinfo s;
2843 	struct compat_sysinfo s_32;
2844 
2845 	do_sysinfo(&s);
2846 
2847 	/* Check to see if any memory value is too large for 32-bit and scale
2848 	 *  down if needed
2849 	 */
2850 	if (upper_32_bits(s.totalram) || upper_32_bits(s.totalswap)) {
2851 		int bitcount = 0;
2852 
2853 		while (s.mem_unit < PAGE_SIZE) {
2854 			s.mem_unit <<= 1;
2855 			bitcount++;
2856 		}
2857 
2858 		s.totalram >>= bitcount;
2859 		s.freeram >>= bitcount;
2860 		s.sharedram >>= bitcount;
2861 		s.bufferram >>= bitcount;
2862 		s.totalswap >>= bitcount;
2863 		s.freeswap >>= bitcount;
2864 		s.totalhigh >>= bitcount;
2865 		s.freehigh >>= bitcount;
2866 	}
2867 
2868 	memset(&s_32, 0, sizeof(s_32));
2869 	s_32.uptime = s.uptime;
2870 	s_32.loads[0] = s.loads[0];
2871 	s_32.loads[1] = s.loads[1];
2872 	s_32.loads[2] = s.loads[2];
2873 	s_32.totalram = s.totalram;
2874 	s_32.freeram = s.freeram;
2875 	s_32.sharedram = s.sharedram;
2876 	s_32.bufferram = s.bufferram;
2877 	s_32.totalswap = s.totalswap;
2878 	s_32.freeswap = s.freeswap;
2879 	s_32.procs = s.procs;
2880 	s_32.totalhigh = s.totalhigh;
2881 	s_32.freehigh = s.freehigh;
2882 	s_32.mem_unit = s.mem_unit;
2883 	if (copy_to_user(info, &s_32, sizeof(s_32)))
2884 		return -EFAULT;
2885 	return 0;
2886 }
2887 #endif /* CONFIG_COMPAT */
2888