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