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