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