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