1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/export.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>
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>
46 #include <linux/mempolicy.h>
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>
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>
66 #include <linux/nospec.h>
68 #include <linux/kmsg_dump.h>
69 /* Move somewhere else to avoid recompiling? */
70 #include <generated/utsrelease.h>
72 #include <linux/uaccess.h>
74 #include <asm/unistd.h>
76 #ifndef SET_UNALIGN_CTL
77 # define SET_UNALIGN_CTL(a, b) (-EINVAL)
79 #ifndef GET_UNALIGN_CTL
80 # define GET_UNALIGN_CTL(a, b) (-EINVAL)
83 # define SET_FPEMU_CTL(a, b) (-EINVAL)
86 # define GET_FPEMU_CTL(a, b) (-EINVAL)
89 # define SET_FPEXC_CTL(a, b) (-EINVAL)
92 # define GET_FPEXC_CTL(a, b) (-EINVAL)
95 # define GET_ENDIAN(a, b) (-EINVAL)
98 # define SET_ENDIAN(a, b) (-EINVAL)
101 # define GET_TSC_CTL(a) (-EINVAL)
104 # define SET_TSC_CTL(a) (-EINVAL)
106 #ifndef MPX_ENABLE_MANAGEMENT
107 # define MPX_ENABLE_MANAGEMENT() (-EINVAL)
109 #ifndef MPX_DISABLE_MANAGEMENT
110 # define MPX_DISABLE_MANAGEMENT() (-EINVAL)
113 # define GET_FP_MODE(a) (-EINVAL)
116 # define SET_FP_MODE(a,b) (-EINVAL)
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
124 int overflowuid
= DEFAULT_OVERFLOWUID
;
125 int overflowgid
= DEFAULT_OVERFLOWGID
;
127 EXPORT_SYMBOL(overflowuid
);
128 EXPORT_SYMBOL(overflowgid
);
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
135 int fs_overflowuid
= DEFAULT_FS_OVERFLOWUID
;
136 int fs_overflowgid
= DEFAULT_FS_OVERFLOWUID
;
138 EXPORT_SYMBOL(fs_overflowuid
);
139 EXPORT_SYMBOL(fs_overflowgid
);
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.
145 * Called with rcu_read_lock, creds are safe
147 static bool set_one_prio_perm(struct task_struct
*p
)
149 const struct cred
*cred
= current_cred(), *pcred
= __task_cred(p
);
151 if (uid_eq(pcred
->uid
, cred
->euid
) ||
152 uid_eq(pcred
->euid
, cred
->euid
))
154 if (ns_capable(pcred
->user_ns
, CAP_SYS_NICE
))
160 * set the priority of a task
161 * - the caller must hold the RCU read lock
163 static int set_one_prio(struct task_struct
*p
, int niceval
, int error
)
167 if (!set_one_prio_perm(p
)) {
171 if (niceval
< task_nice(p
) && !can_nice(p
, niceval
)) {
175 no_nice
= security_task_setnice(p
, niceval
);
182 set_user_nice(p
, niceval
);
187 SYSCALL_DEFINE3(setpriority
, int, which
, int, who
, int, niceval
)
189 struct task_struct
*g
, *p
;
190 struct user_struct
*user
;
191 const struct cred
*cred
= current_cred();
196 if (which
> PRIO_USER
|| which
< PRIO_PROCESS
)
199 /* normalize: avoid signed division (rounding problems) */
201 if (niceval
< MIN_NICE
)
203 if (niceval
> MAX_NICE
)
207 read_lock(&tasklist_lock
);
211 p
= find_task_by_vpid(who
);
215 error
= set_one_prio(p
, niceval
, error
);
219 pgrp
= find_vpid(who
);
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
);
227 uid
= make_kuid(cred
->user_ns
, who
);
231 else if (!uid_eq(uid
, cred
->uid
)) {
232 user
= find_user(uid
);
234 goto out_unlock
; /* No processes for this user */
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() */
245 read_unlock(&tasklist_lock
);
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.
257 SYSCALL_DEFINE2(getpriority
, int, which
, int, who
)
259 struct task_struct
*g
, *p
;
260 struct user_struct
*user
;
261 const struct cred
*cred
= current_cred();
262 long niceval
, retval
= -ESRCH
;
266 if (which
> PRIO_USER
|| which
< PRIO_PROCESS
)
270 read_lock(&tasklist_lock
);
274 p
= find_task_by_vpid(who
);
278 niceval
= nice_to_rlimit(task_nice(p
));
279 if (niceval
> retval
)
285 pgrp
= find_vpid(who
);
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
)
292 } while_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
);
295 uid
= make_kuid(cred
->user_ns
, who
);
299 else if (!uid_eq(uid
, cred
->uid
)) {
300 user
= find_user(uid
);
302 goto out_unlock
; /* No processes for this user */
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
)
310 } while_each_thread(g
, p
);
311 if (!uid_eq(uid
, cred
->uid
))
312 free_uid(user
); /* for find_user() */
316 read_unlock(&tasklist_lock
);
323 * Unprivileged users may change the real gid to the effective gid
324 * or vice versa. (BSD-style)
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.
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.
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.
337 * SMP: There are not races, the GIDs are checked only by filesystem
338 * operations (as far as semantic preservation is concerned).
340 #ifdef CONFIG_MULTIUSER
341 SYSCALL_DEFINE2(setregid
, gid_t
, rgid
, gid_t
, egid
)
343 struct user_namespace
*ns
= current_user_ns();
344 const struct cred
*old
;
349 krgid
= make_kgid(ns
, rgid
);
350 kegid
= make_kgid(ns
, egid
);
352 if ((rgid
!= (gid_t
) -1) && !gid_valid(krgid
))
354 if ((egid
!= (gid_t
) -1) && !gid_valid(kegid
))
357 new = prepare_creds();
360 old
= current_cred();
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
))
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
))
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
;
386 return commit_creds(new);
394 * setgid() is implemented like SysV w/ SAVED_IDS
396 * SMP: Same implicit races as above.
398 SYSCALL_DEFINE1(setgid
, gid_t
, gid
)
400 struct user_namespace
*ns
= current_user_ns();
401 const struct cred
*old
;
406 kgid
= make_kgid(ns
, gid
);
407 if (!gid_valid(kgid
))
410 new = prepare_creds();
413 old
= current_cred();
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
;
423 return commit_creds(new);
431 * change the user struct in a credentials set to match the new UID
433 static int set_user(struct cred
*new)
435 struct user_struct
*new_user
;
437 new_user
= alloc_uid(new->uid
);
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.
448 if (atomic_read(&new_user
->processes
) >= rlimit(RLIMIT_NPROC
) &&
449 new_user
!= INIT_USER
)
450 current
->flags
|= PF_NPROC_EXCEEDED
;
452 current
->flags
&= ~PF_NPROC_EXCEEDED
;
455 new->user
= new_user
;
460 * Unprivileged users may change the real uid to the effective uid
461 * or vice versa. (BSD-style)
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.
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.
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.
474 SYSCALL_DEFINE2(setreuid
, uid_t
, ruid
, uid_t
, euid
)
476 struct user_namespace
*ns
= current_user_ns();
477 const struct cred
*old
;
482 kruid
= make_kuid(ns
, ruid
);
483 keuid
= make_kuid(ns
, euid
);
485 if ((ruid
!= (uid_t
) -1) && !uid_valid(kruid
))
487 if ((euid
!= (uid_t
) -1) && !uid_valid(keuid
))
490 new = prepare_creds();
493 old
= current_cred();
496 if (ruid
!= (uid_t
) -1) {
498 if (!uid_eq(old
->uid
, kruid
) &&
499 !uid_eq(old
->euid
, kruid
) &&
500 !ns_capable(old
->user_ns
, CAP_SETUID
))
504 if (euid
!= (uid_t
) -1) {
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
))
513 if (!uid_eq(new->uid
, old
->uid
)) {
514 retval
= set_user(new);
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
;
523 retval
= security_task_fix_setuid(new, old
, LSM_SETID_RE
);
527 return commit_creds(new);
535 * setuid() is implemented like SysV with SAVED_IDS
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.
545 SYSCALL_DEFINE1(setuid
, uid_t
, uid
)
547 struct user_namespace
*ns
= current_user_ns();
548 const struct cred
*old
;
553 kuid
= make_kuid(ns
, uid
);
554 if (!uid_valid(kuid
))
557 new = prepare_creds();
560 old
= current_cred();
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);
570 } else if (!uid_eq(kuid
, old
->uid
) && !uid_eq(kuid
, new->suid
)) {
574 new->fsuid
= new->euid
= kuid
;
576 retval
= security_task_fix_setuid(new, old
, LSM_SETID_ID
);
580 return commit_creds(new);
589 * This function implements a generic ability to update ruid, euid,
590 * and suid. This allows you to implement the 4.4 compatible seteuid().
592 SYSCALL_DEFINE3(setresuid
, uid_t
, ruid
, uid_t
, euid
, uid_t
, suid
)
594 struct user_namespace
*ns
= current_user_ns();
595 const struct cred
*old
;
598 kuid_t kruid
, keuid
, ksuid
;
600 kruid
= make_kuid(ns
, ruid
);
601 keuid
= make_kuid(ns
, euid
);
602 ksuid
= make_kuid(ns
, suid
);
604 if ((ruid
!= (uid_t
) -1) && !uid_valid(kruid
))
607 if ((euid
!= (uid_t
) -1) && !uid_valid(keuid
))
610 if ((suid
!= (uid_t
) -1) && !uid_valid(ksuid
))
613 new = prepare_creds();
617 old
= current_cred();
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
))
624 if (euid
!= (uid_t
) -1 && !uid_eq(keuid
, old
->uid
) &&
625 !uid_eq(keuid
, old
->euid
) && !uid_eq(keuid
, old
->suid
))
627 if (suid
!= (uid_t
) -1 && !uid_eq(ksuid
, old
->uid
) &&
628 !uid_eq(ksuid
, old
->euid
) && !uid_eq(ksuid
, old
->suid
))
632 if (ruid
!= (uid_t
) -1) {
634 if (!uid_eq(kruid
, old
->uid
)) {
635 retval
= set_user(new);
640 if (euid
!= (uid_t
) -1)
642 if (suid
!= (uid_t
) -1)
644 new->fsuid
= new->euid
;
646 retval
= security_task_fix_setuid(new, old
, LSM_SETID_RES
);
650 return commit_creds(new);
657 SYSCALL_DEFINE3(getresuid
, uid_t __user
*, ruidp
, uid_t __user
*, euidp
, uid_t __user
*, suidp
)
659 const struct cred
*cred
= current_cred();
661 uid_t ruid
, euid
, suid
;
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
);
667 retval
= put_user(ruid
, ruidp
);
669 retval
= put_user(euid
, euidp
);
671 return put_user(suid
, suidp
);
677 * Same as above, but for rgid, egid, sgid.
679 SYSCALL_DEFINE3(setresgid
, gid_t
, rgid
, gid_t
, egid
, gid_t
, sgid
)
681 struct user_namespace
*ns
= current_user_ns();
682 const struct cred
*old
;
685 kgid_t krgid
, kegid
, ksgid
;
687 krgid
= make_kgid(ns
, rgid
);
688 kegid
= make_kgid(ns
, egid
);
689 ksgid
= make_kgid(ns
, sgid
);
691 if ((rgid
!= (gid_t
) -1) && !gid_valid(krgid
))
693 if ((egid
!= (gid_t
) -1) && !gid_valid(kegid
))
695 if ((sgid
!= (gid_t
) -1) && !gid_valid(ksgid
))
698 new = prepare_creds();
701 old
= current_cred();
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
))
708 if (egid
!= (gid_t
) -1 && !gid_eq(kegid
, old
->gid
) &&
709 !gid_eq(kegid
, old
->egid
) && !gid_eq(kegid
, old
->sgid
))
711 if (sgid
!= (gid_t
) -1 && !gid_eq(ksgid
, old
->gid
) &&
712 !gid_eq(ksgid
, old
->egid
) && !gid_eq(ksgid
, old
->sgid
))
716 if (rgid
!= (gid_t
) -1)
718 if (egid
!= (gid_t
) -1)
720 if (sgid
!= (gid_t
) -1)
722 new->fsgid
= new->egid
;
724 return commit_creds(new);
731 SYSCALL_DEFINE3(getresgid
, gid_t __user
*, rgidp
, gid_t __user
*, egidp
, gid_t __user
*, sgidp
)
733 const struct cred
*cred
= current_cred();
735 gid_t rgid
, egid
, sgid
;
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
);
741 retval
= put_user(rgid
, rgidp
);
743 retval
= put_user(egid
, egidp
);
745 retval
= put_user(sgid
, sgidp
);
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..
758 SYSCALL_DEFINE1(setfsuid
, uid_t
, uid
)
760 const struct cred
*old
;
765 old
= current_cred();
766 old_fsuid
= from_kuid_munged(old
->user_ns
, old
->fsuid
);
768 kuid
= make_kuid(old
->user_ns
, uid
);
769 if (!uid_valid(kuid
))
772 new = prepare_creds();
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
)) {
781 if (security_task_fix_setuid(new, old
, LSM_SETID_FS
) == 0)
795 * Samma på svenska..
797 SYSCALL_DEFINE1(setfsgid
, gid_t
, gid
)
799 const struct cred
*old
;
804 old
= current_cred();
805 old_fsgid
= from_kgid_munged(old
->user_ns
, old
->fsgid
);
807 kgid
= make_kgid(old
->user_ns
, gid
);
808 if (!gid_valid(kgid
))
811 new = prepare_creds();
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
)) {
831 #endif /* CONFIG_MULTIUSER */
834 * sys_getpid - return the thread group id of the current process
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.
840 * This is SMP safe as current->tgid does not change.
842 SYSCALL_DEFINE0(getpid
)
844 return task_tgid_vnr(current
);
847 /* Thread ID - the internal kernel "pid" */
848 SYSCALL_DEFINE0(gettid
)
850 return task_pid_vnr(current
);
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).
859 SYSCALL_DEFINE0(getppid
)
864 pid
= task_tgid_vnr(rcu_dereference(current
->real_parent
));
870 SYSCALL_DEFINE0(getuid
)
872 /* Only we change this so SMP safe */
873 return from_kuid_munged(current_user_ns(), current_uid());
876 SYSCALL_DEFINE0(geteuid
)
878 /* Only we change this so SMP safe */
879 return from_kuid_munged(current_user_ns(), current_euid());
882 SYSCALL_DEFINE0(getgid
)
884 /* Only we change this so SMP safe */
885 return from_kgid_munged(current_user_ns(), current_gid());
888 SYSCALL_DEFINE0(getegid
)
890 /* Only we change this so SMP safe */
891 return from_kgid_munged(current_user_ns(), current_egid());
894 static void do_sys_times(struct tms
*tms
)
896 u64 tgutime
, tgstime
, cutime
, cstime
;
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
);
907 SYSCALL_DEFINE1(times
, struct tms __user
*, tbuf
)
913 if (copy_to_user(tbuf
, &tmp
, sizeof(struct tms
)))
916 force_successful_syscall_return();
917 return (long) jiffies_64_to_clock_t(get_jiffies_64());
921 static compat_clock_t
clock_t_to_compat_clock_t(clock_t x
)
923 return compat_jiffies_to_clock_t(clock_t_to_jiffies(x
));
926 COMPAT_SYSCALL_DEFINE1(times
, struct compat_tms __user
*, tbuf
)
930 struct compat_tms tmp
;
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
)))
941 force_successful_syscall_return();
942 return compat_jiffies_to_clock_t(jiffies
);
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.
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
955 * !PF_FORKNOEXEC check to conform completely to POSIX.
957 SYSCALL_DEFINE2(setpgid
, pid_t
, pid
, pid_t
, pgid
)
959 struct task_struct
*p
;
960 struct task_struct
*group_leader
= current
->group_leader
;
965 pid
= task_pid_vnr(group_leader
);
972 /* From this point forward we keep holding onto the tasklist lock
973 * so that our parent does not change from under us. -DaveM
975 write_lock_irq(&tasklist_lock
);
978 p
= find_task_by_vpid(pid
);
983 if (!thread_group_leader(p
))
986 if (same_thread_group(p
->real_parent
, group_leader
)) {
988 if (task_session(p
) != task_session(group_leader
))
991 if (!(p
->flags
& PF_FORKNOEXEC
))
995 if (p
!= group_leader
)
1000 if (p
->signal
->leader
)
1005 struct task_struct
*g
;
1007 pgrp
= find_vpid(pgid
);
1008 g
= pid_task(pgrp
, PIDTYPE_PGID
);
1009 if (!g
|| task_session(g
) != task_session(group_leader
))
1013 err
= security_task_setpgid(p
, pgid
);
1017 if (task_pgrp(p
) != pgrp
)
1018 change_pid(p
, PIDTYPE_PGID
, pgrp
);
1022 /* All paths lead to here, thus we are safe. -DaveM */
1023 write_unlock_irq(&tasklist_lock
);
1028 SYSCALL_DEFINE1(getpgid
, pid_t
, pid
)
1030 struct task_struct
*p
;
1036 grp
= task_pgrp(current
);
1039 p
= find_task_by_vpid(pid
);
1046 retval
= security_task_getpgid(p
);
1050 retval
= pid_vnr(grp
);
1056 #ifdef __ARCH_WANT_SYS_GETPGRP
1058 SYSCALL_DEFINE0(getpgrp
)
1060 return sys_getpgid(0);
1065 SYSCALL_DEFINE1(getsid
, pid_t
, pid
)
1067 struct task_struct
*p
;
1073 sid
= task_session(current
);
1076 p
= find_task_by_vpid(pid
);
1079 sid
= task_session(p
);
1083 retval
= security_task_getsid(p
);
1087 retval
= pid_vnr(sid
);
1093 static void set_special_pids(struct pid
*pid
)
1095 struct task_struct
*curr
= current
->group_leader
;
1097 if (task_session(curr
) != pid
)
1098 change_pid(curr
, PIDTYPE_SID
, pid
);
1100 if (task_pgrp(curr
) != pid
)
1101 change_pid(curr
, PIDTYPE_PGID
, pid
);
1104 SYSCALL_DEFINE0(setsid
)
1106 struct task_struct
*group_leader
= current
->group_leader
;
1107 struct pid
*sid
= task_pid(group_leader
);
1108 pid_t session
= pid_vnr(sid
);
1111 write_lock_irq(&tasklist_lock
);
1112 /* Fail if I am already a session leader */
1113 if (group_leader
->signal
->leader
)
1116 /* Fail if a process group id already exists that equals the
1117 * proposed session id.
1119 if (pid_task(sid
, PIDTYPE_PGID
))
1122 group_leader
->signal
->leader
= 1;
1123 set_special_pids(sid
);
1125 proc_clear_tty(group_leader
);
1129 write_unlock_irq(&tasklist_lock
);
1131 proc_sid_connector(group_leader
);
1132 sched_autogroup_create_attach(group_leader
);
1137 DECLARE_RWSEM(uts_sem
);
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)))
1145 #define override_architecture(name) 0
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.
1153 static int override_release(char __user
*release
, size_t len
)
1157 if (current
->personality
& UNAME26
) {
1158 const char *rest
= UTS_RELEASE
;
1159 char buf
[65] = { 0 };
1165 if (*rest
== '.' && ++ndots
>= 3)
1167 if (!isdigit(*rest
) && *rest
!= '.')
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);
1179 SYSCALL_DEFINE1(newuname
, struct new_utsname __user
*, name
)
1183 down_read(&uts_sem
);
1184 if (copy_to_user(name
, utsname(), sizeof *name
))
1188 if (!errno
&& override_release(name
->release
, sizeof(name
->release
)))
1190 if (!errno
&& override_architecture(name
))
1195 #ifdef __ARCH_WANT_SYS_OLD_UNAME
1199 SYSCALL_DEFINE1(uname
, struct old_utsname __user
*, name
)
1206 down_read(&uts_sem
);
1207 if (copy_to_user(name
, utsname(), sizeof(*name
)))
1211 if (!error
&& override_release(name
->release
, sizeof(name
->release
)))
1213 if (!error
&& override_architecture(name
))
1218 SYSCALL_DEFINE1(olduname
, struct oldold_utsname __user
*, name
)
1224 if (!access_ok(VERIFY_WRITE
, name
, sizeof(struct oldold_utsname
)))
1227 down_read(&uts_sem
);
1228 error
= __copy_to_user(&name
->sysname
, &utsname()->sysname
,
1230 error
|= __put_user(0, name
->sysname
+ __OLD_UTS_LEN
);
1231 error
|= __copy_to_user(&name
->nodename
, &utsname()->nodename
,
1233 error
|= __put_user(0, name
->nodename
+ __OLD_UTS_LEN
);
1234 error
|= __copy_to_user(&name
->release
, &utsname()->release
,
1236 error
|= __put_user(0, name
->release
+ __OLD_UTS_LEN
);
1237 error
|= __copy_to_user(&name
->version
, &utsname()->version
,
1239 error
|= __put_user(0, name
->version
+ __OLD_UTS_LEN
);
1240 error
|= __copy_to_user(&name
->machine
, &utsname()->machine
,
1242 error
|= __put_user(0, name
->machine
+ __OLD_UTS_LEN
);
1245 if (!error
&& override_architecture(name
))
1247 if (!error
&& override_release(name
->release
, sizeof(name
->release
)))
1249 return error
? -EFAULT
: 0;
1253 SYSCALL_DEFINE2(sethostname
, char __user
*, name
, int, len
)
1256 char tmp
[__NEW_UTS_LEN
];
1258 if (!ns_capable(current
->nsproxy
->uts_ns
->user_ns
, CAP_SYS_ADMIN
))
1261 if (len
< 0 || len
> __NEW_UTS_LEN
)
1263 down_write(&uts_sem
);
1265 if (!copy_from_user(tmp
, name
, len
)) {
1266 struct new_utsname
*u
= utsname();
1268 memcpy(u
->nodename
, tmp
, len
);
1269 memset(u
->nodename
+ len
, 0, sizeof(u
->nodename
) - len
);
1271 uts_proc_notify(UTS_PROC_HOSTNAME
);
1277 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1279 SYSCALL_DEFINE2(gethostname
, char __user
*, name
, int, len
)
1282 struct new_utsname
*u
;
1286 down_read(&uts_sem
);
1288 i
= 1 + strlen(u
->nodename
);
1292 if (copy_to_user(name
, u
->nodename
, i
))
1301 * Only setdomainname; getdomainname can be implemented by calling
1304 SYSCALL_DEFINE2(setdomainname
, char __user
*, name
, int, len
)
1307 char tmp
[__NEW_UTS_LEN
];
1309 if (!ns_capable(current
->nsproxy
->uts_ns
->user_ns
, CAP_SYS_ADMIN
))
1311 if (len
< 0 || len
> __NEW_UTS_LEN
)
1314 down_write(&uts_sem
);
1316 if (!copy_from_user(tmp
, name
, len
)) {
1317 struct new_utsname
*u
= utsname();
1319 memcpy(u
->domainname
, tmp
, len
);
1320 memset(u
->domainname
+ len
, 0, sizeof(u
->domainname
) - len
);
1322 uts_proc_notify(UTS_PROC_DOMAINNAME
);
1328 SYSCALL_DEFINE2(getrlimit
, unsigned int, resource
, struct rlimit __user
*, rlim
)
1330 struct rlimit value
;
1333 ret
= do_prlimit(current
, resource
, NULL
, &value
);
1335 ret
= copy_to_user(rlim
, &value
, sizeof(*rlim
)) ? -EFAULT
: 0;
1340 #ifdef CONFIG_COMPAT
1342 COMPAT_SYSCALL_DEFINE2(setrlimit
, unsigned int, resource
,
1343 struct compat_rlimit __user
*, rlim
)
1346 struct compat_rlimit r32
;
1348 if (copy_from_user(&r32
, rlim
, sizeof(struct compat_rlimit
)))
1351 if (r32
.rlim_cur
== COMPAT_RLIM_INFINITY
)
1352 r
.rlim_cur
= RLIM_INFINITY
;
1354 r
.rlim_cur
= r32
.rlim_cur
;
1355 if (r32
.rlim_max
== COMPAT_RLIM_INFINITY
)
1356 r
.rlim_max
= RLIM_INFINITY
;
1358 r
.rlim_max
= r32
.rlim_max
;
1359 return do_prlimit(current
, resource
, &r
, NULL
);
1362 COMPAT_SYSCALL_DEFINE2(getrlimit
, unsigned int, resource
,
1363 struct compat_rlimit __user
*, rlim
)
1368 ret
= do_prlimit(current
, resource
, NULL
, &r
);
1370 struct compat_rlimit r32
;
1371 if (r
.rlim_cur
> COMPAT_RLIM_INFINITY
)
1372 r32
.rlim_cur
= COMPAT_RLIM_INFINITY
;
1374 r32
.rlim_cur
= r
.rlim_cur
;
1375 if (r
.rlim_max
> COMPAT_RLIM_INFINITY
)
1376 r32
.rlim_max
= COMPAT_RLIM_INFINITY
;
1378 r32
.rlim_max
= r
.rlim_max
;
1380 if (copy_to_user(rlim
, &r32
, sizeof(struct compat_rlimit
)))
1388 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1391 * Back compatibility for getrlimit. Needed for some apps.
1393 SYSCALL_DEFINE2(old_getrlimit
, unsigned int, resource
,
1394 struct rlimit __user
*, rlim
)
1397 if (resource
>= RLIM_NLIMITS
)
1400 resource
= array_index_nospec(resource
, RLIM_NLIMITS
);
1401 task_lock(current
->group_leader
);
1402 x
= current
->signal
->rlim
[resource
];
1403 task_unlock(current
->group_leader
);
1404 if (x
.rlim_cur
> 0x7FFFFFFF)
1405 x
.rlim_cur
= 0x7FFFFFFF;
1406 if (x
.rlim_max
> 0x7FFFFFFF)
1407 x
.rlim_max
= 0x7FFFFFFF;
1408 return copy_to_user(rlim
, &x
, sizeof(x
)) ? -EFAULT
: 0;
1411 #ifdef CONFIG_COMPAT
1412 COMPAT_SYSCALL_DEFINE2(old_getrlimit
, unsigned int, resource
,
1413 struct compat_rlimit __user
*, rlim
)
1417 if (resource
>= RLIM_NLIMITS
)
1420 resource
= array_index_nospec(resource
, RLIM_NLIMITS
);
1421 task_lock(current
->group_leader
);
1422 r
= current
->signal
->rlim
[resource
];
1423 task_unlock(current
->group_leader
);
1424 if (r
.rlim_cur
> 0x7FFFFFFF)
1425 r
.rlim_cur
= 0x7FFFFFFF;
1426 if (r
.rlim_max
> 0x7FFFFFFF)
1427 r
.rlim_max
= 0x7FFFFFFF;
1429 if (put_user(r
.rlim_cur
, &rlim
->rlim_cur
) ||
1430 put_user(r
.rlim_max
, &rlim
->rlim_max
))
1438 static inline bool rlim64_is_infinity(__u64 rlim64
)
1440 #if BITS_PER_LONG < 64
1441 return rlim64
>= ULONG_MAX
;
1443 return rlim64
== RLIM64_INFINITY
;
1447 static void rlim_to_rlim64(const struct rlimit
*rlim
, struct rlimit64
*rlim64
)
1449 if (rlim
->rlim_cur
== RLIM_INFINITY
)
1450 rlim64
->rlim_cur
= RLIM64_INFINITY
;
1452 rlim64
->rlim_cur
= rlim
->rlim_cur
;
1453 if (rlim
->rlim_max
== RLIM_INFINITY
)
1454 rlim64
->rlim_max
= RLIM64_INFINITY
;
1456 rlim64
->rlim_max
= rlim
->rlim_max
;
1459 static void rlim64_to_rlim(const struct rlimit64
*rlim64
, struct rlimit
*rlim
)
1461 if (rlim64_is_infinity(rlim64
->rlim_cur
))
1462 rlim
->rlim_cur
= RLIM_INFINITY
;
1464 rlim
->rlim_cur
= (unsigned long)rlim64
->rlim_cur
;
1465 if (rlim64_is_infinity(rlim64
->rlim_max
))
1466 rlim
->rlim_max
= RLIM_INFINITY
;
1468 rlim
->rlim_max
= (unsigned long)rlim64
->rlim_max
;
1471 /* make sure you are allowed to change @tsk limits before calling this */
1472 int do_prlimit(struct task_struct
*tsk
, unsigned int resource
,
1473 struct rlimit
*new_rlim
, struct rlimit
*old_rlim
)
1475 struct rlimit
*rlim
;
1478 if (resource
>= RLIM_NLIMITS
)
1481 if (new_rlim
->rlim_cur
> new_rlim
->rlim_max
)
1483 if (resource
== RLIMIT_NOFILE
&&
1484 new_rlim
->rlim_max
> sysctl_nr_open
)
1488 /* protect tsk->signal and tsk->sighand from disappearing */
1489 read_lock(&tasklist_lock
);
1490 if (!tsk
->sighand
) {
1495 rlim
= tsk
->signal
->rlim
+ resource
;
1496 task_lock(tsk
->group_leader
);
1498 /* Keep the capable check against init_user_ns until
1499 cgroups can contain all limits */
1500 if (new_rlim
->rlim_max
> rlim
->rlim_max
&&
1501 !capable(CAP_SYS_RESOURCE
))
1504 retval
= security_task_setrlimit(tsk
, resource
, new_rlim
);
1505 if (resource
== RLIMIT_CPU
&& new_rlim
->rlim_cur
== 0) {
1507 * The caller is asking for an immediate RLIMIT_CPU
1508 * expiry. But we use the zero value to mean "it was
1509 * never set". So let's cheat and make it one second
1512 new_rlim
->rlim_cur
= 1;
1521 task_unlock(tsk
->group_leader
);
1524 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1525 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1526 * very long-standing error, and fixing it now risks breakage of
1527 * applications, so we live with it
1529 if (!retval
&& new_rlim
&& resource
== RLIMIT_CPU
&&
1530 new_rlim
->rlim_cur
!= RLIM_INFINITY
&&
1531 IS_ENABLED(CONFIG_POSIX_TIMERS
))
1532 update_rlimit_cpu(tsk
, new_rlim
->rlim_cur
);
1534 read_unlock(&tasklist_lock
);
1538 /* rcu lock must be held */
1539 static int check_prlimit_permission(struct task_struct
*task
,
1542 const struct cred
*cred
= current_cred(), *tcred
;
1545 if (current
== task
)
1548 tcred
= __task_cred(task
);
1549 id_match
= (uid_eq(cred
->uid
, tcred
->euid
) &&
1550 uid_eq(cred
->uid
, tcred
->suid
) &&
1551 uid_eq(cred
->uid
, tcred
->uid
) &&
1552 gid_eq(cred
->gid
, tcred
->egid
) &&
1553 gid_eq(cred
->gid
, tcred
->sgid
) &&
1554 gid_eq(cred
->gid
, tcred
->gid
));
1555 if (!id_match
&& !ns_capable(tcred
->user_ns
, CAP_SYS_RESOURCE
))
1558 return security_task_prlimit(cred
, tcred
, flags
);
1561 SYSCALL_DEFINE4(prlimit64
, pid_t
, pid
, unsigned int, resource
,
1562 const struct rlimit64 __user
*, new_rlim
,
1563 struct rlimit64 __user
*, old_rlim
)
1565 struct rlimit64 old64
, new64
;
1566 struct rlimit old
, new;
1567 struct task_struct
*tsk
;
1568 unsigned int checkflags
= 0;
1572 checkflags
|= LSM_PRLIMIT_READ
;
1575 if (copy_from_user(&new64
, new_rlim
, sizeof(new64
)))
1577 rlim64_to_rlim(&new64
, &new);
1578 checkflags
|= LSM_PRLIMIT_WRITE
;
1582 tsk
= pid
? find_task_by_vpid(pid
) : current
;
1587 ret
= check_prlimit_permission(tsk
, checkflags
);
1592 get_task_struct(tsk
);
1595 ret
= do_prlimit(tsk
, resource
, new_rlim
? &new : NULL
,
1596 old_rlim
? &old
: NULL
);
1598 if (!ret
&& old_rlim
) {
1599 rlim_to_rlim64(&old
, &old64
);
1600 if (copy_to_user(old_rlim
, &old64
, sizeof(old64
)))
1604 put_task_struct(tsk
);
1608 SYSCALL_DEFINE2(setrlimit
, unsigned int, resource
, struct rlimit __user
*, rlim
)
1610 struct rlimit new_rlim
;
1612 if (copy_from_user(&new_rlim
, rlim
, sizeof(*rlim
)))
1614 return do_prlimit(current
, resource
, &new_rlim
, NULL
);
1618 * It would make sense to put struct rusage in the task_struct,
1619 * except that would make the task_struct be *really big*. After
1620 * task_struct gets moved into malloc'ed memory, it would
1621 * make sense to do this. It will make moving the rest of the information
1622 * a lot simpler! (Which we're not doing right now because we're not
1623 * measuring them yet).
1625 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1626 * races with threads incrementing their own counters. But since word
1627 * reads are atomic, we either get new values or old values and we don't
1628 * care which for the sums. We always take the siglock to protect reading
1629 * the c* fields from p->signal from races with exit.c updating those
1630 * fields when reaping, so a sample either gets all the additions of a
1631 * given child after it's reaped, or none so this sample is before reaping.
1634 * We need to take the siglock for CHILDEREN, SELF and BOTH
1635 * for the cases current multithreaded, non-current single threaded
1636 * non-current multithreaded. Thread traversal is now safe with
1638 * Strictly speaking, we donot need to take the siglock if we are current and
1639 * single threaded, as no one else can take our signal_struct away, no one
1640 * else can reap the children to update signal->c* counters, and no one else
1641 * can race with the signal-> fields. If we do not take any lock, the
1642 * signal-> fields could be read out of order while another thread was just
1643 * exiting. So we should place a read memory barrier when we avoid the lock.
1644 * On the writer side, write memory barrier is implied in __exit_signal
1645 * as __exit_signal releases the siglock spinlock after updating the signal->
1646 * fields. But we don't do this yet to keep things simple.
1650 static void accumulate_thread_rusage(struct task_struct
*t
, struct rusage
*r
)
1652 r
->ru_nvcsw
+= t
->nvcsw
;
1653 r
->ru_nivcsw
+= t
->nivcsw
;
1654 r
->ru_minflt
+= t
->min_flt
;
1655 r
->ru_majflt
+= t
->maj_flt
;
1656 r
->ru_inblock
+= task_io_get_inblock(t
);
1657 r
->ru_oublock
+= task_io_get_oublock(t
);
1660 void getrusage(struct task_struct
*p
, int who
, struct rusage
*r
)
1662 struct task_struct
*t
;
1663 unsigned long flags
;
1664 u64 tgutime
, tgstime
, utime
, stime
;
1665 unsigned long maxrss
= 0;
1667 memset((char *)r
, 0, sizeof (*r
));
1670 if (who
== RUSAGE_THREAD
) {
1671 task_cputime_adjusted(current
, &utime
, &stime
);
1672 accumulate_thread_rusage(p
, r
);
1673 maxrss
= p
->signal
->maxrss
;
1677 if (!lock_task_sighand(p
, &flags
))
1682 case RUSAGE_CHILDREN
:
1683 utime
= p
->signal
->cutime
;
1684 stime
= p
->signal
->cstime
;
1685 r
->ru_nvcsw
= p
->signal
->cnvcsw
;
1686 r
->ru_nivcsw
= p
->signal
->cnivcsw
;
1687 r
->ru_minflt
= p
->signal
->cmin_flt
;
1688 r
->ru_majflt
= p
->signal
->cmaj_flt
;
1689 r
->ru_inblock
= p
->signal
->cinblock
;
1690 r
->ru_oublock
= p
->signal
->coublock
;
1691 maxrss
= p
->signal
->cmaxrss
;
1693 if (who
== RUSAGE_CHILDREN
)
1697 thread_group_cputime_adjusted(p
, &tgutime
, &tgstime
);
1700 r
->ru_nvcsw
+= p
->signal
->nvcsw
;
1701 r
->ru_nivcsw
+= p
->signal
->nivcsw
;
1702 r
->ru_minflt
+= p
->signal
->min_flt
;
1703 r
->ru_majflt
+= p
->signal
->maj_flt
;
1704 r
->ru_inblock
+= p
->signal
->inblock
;
1705 r
->ru_oublock
+= p
->signal
->oublock
;
1706 if (maxrss
< p
->signal
->maxrss
)
1707 maxrss
= p
->signal
->maxrss
;
1710 accumulate_thread_rusage(t
, r
);
1711 } while_each_thread(p
, t
);
1717 unlock_task_sighand(p
, &flags
);
1720 r
->ru_utime
= ns_to_timeval(utime
);
1721 r
->ru_stime
= ns_to_timeval(stime
);
1723 if (who
!= RUSAGE_CHILDREN
) {
1724 struct mm_struct
*mm
= get_task_mm(p
);
1727 setmax_mm_hiwater_rss(&maxrss
, mm
);
1731 r
->ru_maxrss
= maxrss
* (PAGE_SIZE
/ 1024); /* convert pages to KBs */
1734 SYSCALL_DEFINE2(getrusage
, int, who
, struct rusage __user
*, ru
)
1738 if (who
!= RUSAGE_SELF
&& who
!= RUSAGE_CHILDREN
&&
1739 who
!= RUSAGE_THREAD
)
1742 getrusage(current
, who
, &r
);
1743 return copy_to_user(ru
, &r
, sizeof(r
)) ? -EFAULT
: 0;
1746 #ifdef CONFIG_COMPAT
1747 COMPAT_SYSCALL_DEFINE2(getrusage
, int, who
, struct compat_rusage __user
*, ru
)
1751 if (who
!= RUSAGE_SELF
&& who
!= RUSAGE_CHILDREN
&&
1752 who
!= RUSAGE_THREAD
)
1755 getrusage(current
, who
, &r
);
1756 return put_compat_rusage(&r
, ru
);
1760 SYSCALL_DEFINE1(umask
, int, mask
)
1762 mask
= xchg(¤t
->fs
->umask
, mask
& S_IRWXUGO
);
1766 static int prctl_set_mm_exe_file(struct mm_struct
*mm
, unsigned int fd
)
1769 struct file
*old_exe
, *exe_file
;
1770 struct inode
*inode
;
1777 inode
= file_inode(exe
.file
);
1780 * Because the original mm->exe_file points to executable file, make
1781 * sure that this one is executable as well, to avoid breaking an
1785 if (!S_ISREG(inode
->i_mode
) || path_noexec(&exe
.file
->f_path
))
1788 err
= inode_permission(inode
, MAY_EXEC
);
1793 * Forbid mm->exe_file change if old file still mapped.
1795 exe_file
= get_mm_exe_file(mm
);
1798 struct vm_area_struct
*vma
;
1800 down_read(&mm
->mmap_sem
);
1801 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
1804 if (path_equal(&vma
->vm_file
->f_path
,
1809 up_read(&mm
->mmap_sem
);
1814 /* set the new file, lockless */
1816 old_exe
= xchg(&mm
->exe_file
, exe
.file
);
1823 up_read(&mm
->mmap_sem
);
1829 * WARNING: we don't require any capability here so be very careful
1830 * in what is allowed for modification from userspace.
1832 static int validate_prctl_map(struct prctl_mm_map
*prctl_map
)
1834 unsigned long mmap_max_addr
= TASK_SIZE
;
1835 struct mm_struct
*mm
= current
->mm
;
1836 int error
= -EINVAL
, i
;
1838 static const unsigned char offsets
[] = {
1839 offsetof(struct prctl_mm_map
, start_code
),
1840 offsetof(struct prctl_mm_map
, end_code
),
1841 offsetof(struct prctl_mm_map
, start_data
),
1842 offsetof(struct prctl_mm_map
, end_data
),
1843 offsetof(struct prctl_mm_map
, start_brk
),
1844 offsetof(struct prctl_mm_map
, brk
),
1845 offsetof(struct prctl_mm_map
, start_stack
),
1846 offsetof(struct prctl_mm_map
, arg_start
),
1847 offsetof(struct prctl_mm_map
, arg_end
),
1848 offsetof(struct prctl_mm_map
, env_start
),
1849 offsetof(struct prctl_mm_map
, env_end
),
1853 * Make sure the members are not somewhere outside
1854 * of allowed address space.
1856 for (i
= 0; i
< ARRAY_SIZE(offsets
); i
++) {
1857 u64 val
= *(u64
*)((char *)prctl_map
+ offsets
[i
]);
1859 if ((unsigned long)val
>= mmap_max_addr
||
1860 (unsigned long)val
< mmap_min_addr
)
1865 * Make sure the pairs are ordered.
1867 #define __prctl_check_order(__m1, __op, __m2) \
1868 ((unsigned long)prctl_map->__m1 __op \
1869 (unsigned long)prctl_map->__m2) ? 0 : -EINVAL
1870 error
= __prctl_check_order(start_code
, <, end_code
);
1871 error
|= __prctl_check_order(start_data
, <, end_data
);
1872 error
|= __prctl_check_order(start_brk
, <=, brk
);
1873 error
|= __prctl_check_order(arg_start
, <=, arg_end
);
1874 error
|= __prctl_check_order(env_start
, <=, env_end
);
1877 #undef __prctl_check_order
1882 * @brk should be after @end_data in traditional maps.
1884 if (prctl_map
->start_brk
<= prctl_map
->end_data
||
1885 prctl_map
->brk
<= prctl_map
->end_data
)
1889 * Neither we should allow to override limits if they set.
1891 if (check_data_rlimit(rlimit(RLIMIT_DATA
), prctl_map
->brk
,
1892 prctl_map
->start_brk
, prctl_map
->end_data
,
1893 prctl_map
->start_data
))
1897 * Someone is trying to cheat the auxv vector.
1899 if (prctl_map
->auxv_size
) {
1900 if (!prctl_map
->auxv
|| prctl_map
->auxv_size
> sizeof(mm
->saved_auxv
))
1905 * Finally, make sure the caller has the rights to
1906 * change /proc/pid/exe link: only local sys admin should
1909 if (prctl_map
->exe_fd
!= (u32
)-1) {
1910 if (!ns_capable(current_user_ns(), CAP_SYS_ADMIN
))
1919 #ifdef CONFIG_CHECKPOINT_RESTORE
1920 static int prctl_set_mm_map(int opt
, const void __user
*addr
, unsigned long data_size
)
1922 struct prctl_mm_map prctl_map
= { .exe_fd
= (u32
)-1, };
1923 unsigned long user_auxv
[AT_VECTOR_SIZE
];
1924 struct mm_struct
*mm
= current
->mm
;
1927 BUILD_BUG_ON(sizeof(user_auxv
) != sizeof(mm
->saved_auxv
));
1928 BUILD_BUG_ON(sizeof(struct prctl_mm_map
) > 256);
1930 if (opt
== PR_SET_MM_MAP_SIZE
)
1931 return put_user((unsigned int)sizeof(prctl_map
),
1932 (unsigned int __user
*)addr
);
1934 if (data_size
!= sizeof(prctl_map
))
1937 if (copy_from_user(&prctl_map
, addr
, sizeof(prctl_map
)))
1940 error
= validate_prctl_map(&prctl_map
);
1944 if (prctl_map
.auxv_size
) {
1945 memset(user_auxv
, 0, sizeof(user_auxv
));
1946 if (copy_from_user(user_auxv
,
1947 (const void __user
*)prctl_map
.auxv
,
1948 prctl_map
.auxv_size
))
1951 /* Last entry must be AT_NULL as specification requires */
1952 user_auxv
[AT_VECTOR_SIZE
- 2] = AT_NULL
;
1953 user_auxv
[AT_VECTOR_SIZE
- 1] = AT_NULL
;
1956 if (prctl_map
.exe_fd
!= (u32
)-1) {
1957 error
= prctl_set_mm_exe_file(mm
, prctl_map
.exe_fd
);
1962 down_write(&mm
->mmap_sem
);
1965 * We don't validate if these members are pointing to
1966 * real present VMAs because application may have correspond
1967 * VMAs already unmapped and kernel uses these members for statistics
1968 * output in procfs mostly, except
1970 * - @start_brk/@brk which are used in do_brk but kernel lookups
1971 * for VMAs when updating these memvers so anything wrong written
1972 * here cause kernel to swear at userspace program but won't lead
1973 * to any problem in kernel itself
1976 mm
->start_code
= prctl_map
.start_code
;
1977 mm
->end_code
= prctl_map
.end_code
;
1978 mm
->start_data
= prctl_map
.start_data
;
1979 mm
->end_data
= prctl_map
.end_data
;
1980 mm
->start_brk
= prctl_map
.start_brk
;
1981 mm
->brk
= prctl_map
.brk
;
1982 mm
->start_stack
= prctl_map
.start_stack
;
1983 mm
->arg_start
= prctl_map
.arg_start
;
1984 mm
->arg_end
= prctl_map
.arg_end
;
1985 mm
->env_start
= prctl_map
.env_start
;
1986 mm
->env_end
= prctl_map
.env_end
;
1989 * Note this update of @saved_auxv is lockless thus
1990 * if someone reads this member in procfs while we're
1991 * updating -- it may get partly updated results. It's
1992 * known and acceptable trade off: we leave it as is to
1993 * not introduce additional locks here making the kernel
1996 if (prctl_map
.auxv_size
)
1997 memcpy(mm
->saved_auxv
, user_auxv
, sizeof(user_auxv
));
1999 up_write(&mm
->mmap_sem
);
2002 #endif /* CONFIG_CHECKPOINT_RESTORE */
2004 static int prctl_set_auxv(struct mm_struct
*mm
, unsigned long addr
,
2008 * This doesn't move the auxiliary vector itself since it's pinned to
2009 * mm_struct, but it permits filling the vector with new values. It's
2010 * up to the caller to provide sane values here, otherwise userspace
2011 * tools which use this vector might be unhappy.
2013 unsigned long user_auxv
[AT_VECTOR_SIZE
];
2015 if (len
> sizeof(user_auxv
))
2018 if (copy_from_user(user_auxv
, (const void __user
*)addr
, len
))
2021 /* Make sure the last entry is always AT_NULL */
2022 user_auxv
[AT_VECTOR_SIZE
- 2] = 0;
2023 user_auxv
[AT_VECTOR_SIZE
- 1] = 0;
2025 BUILD_BUG_ON(sizeof(user_auxv
) != sizeof(mm
->saved_auxv
));
2028 memcpy(mm
->saved_auxv
, user_auxv
, len
);
2029 task_unlock(current
);
2034 static int prctl_set_mm(int opt
, unsigned long addr
,
2035 unsigned long arg4
, unsigned long arg5
)
2037 struct mm_struct
*mm
= current
->mm
;
2038 struct prctl_mm_map prctl_map
;
2039 struct vm_area_struct
*vma
;
2042 if (arg5
|| (arg4
&& (opt
!= PR_SET_MM_AUXV
&&
2043 opt
!= PR_SET_MM_MAP
&&
2044 opt
!= PR_SET_MM_MAP_SIZE
)))
2047 #ifdef CONFIG_CHECKPOINT_RESTORE
2048 if (opt
== PR_SET_MM_MAP
|| opt
== PR_SET_MM_MAP_SIZE
)
2049 return prctl_set_mm_map(opt
, (const void __user
*)addr
, arg4
);
2052 if (!capable(CAP_SYS_RESOURCE
))
2055 if (opt
== PR_SET_MM_EXE_FILE
)
2056 return prctl_set_mm_exe_file(mm
, (unsigned int)addr
);
2058 if (opt
== PR_SET_MM_AUXV
)
2059 return prctl_set_auxv(mm
, addr
, arg4
);
2061 if (addr
>= TASK_SIZE
|| addr
< mmap_min_addr
)
2066 down_write(&mm
->mmap_sem
);
2067 vma
= find_vma(mm
, addr
);
2069 prctl_map
.start_code
= mm
->start_code
;
2070 prctl_map
.end_code
= mm
->end_code
;
2071 prctl_map
.start_data
= mm
->start_data
;
2072 prctl_map
.end_data
= mm
->end_data
;
2073 prctl_map
.start_brk
= mm
->start_brk
;
2074 prctl_map
.brk
= mm
->brk
;
2075 prctl_map
.start_stack
= mm
->start_stack
;
2076 prctl_map
.arg_start
= mm
->arg_start
;
2077 prctl_map
.arg_end
= mm
->arg_end
;
2078 prctl_map
.env_start
= mm
->env_start
;
2079 prctl_map
.env_end
= mm
->env_end
;
2080 prctl_map
.auxv
= NULL
;
2081 prctl_map
.auxv_size
= 0;
2082 prctl_map
.exe_fd
= -1;
2085 case PR_SET_MM_START_CODE
:
2086 prctl_map
.start_code
= addr
;
2088 case PR_SET_MM_END_CODE
:
2089 prctl_map
.end_code
= addr
;
2091 case PR_SET_MM_START_DATA
:
2092 prctl_map
.start_data
= addr
;
2094 case PR_SET_MM_END_DATA
:
2095 prctl_map
.end_data
= addr
;
2097 case PR_SET_MM_START_STACK
:
2098 prctl_map
.start_stack
= addr
;
2100 case PR_SET_MM_START_BRK
:
2101 prctl_map
.start_brk
= addr
;
2104 prctl_map
.brk
= addr
;
2106 case PR_SET_MM_ARG_START
:
2107 prctl_map
.arg_start
= addr
;
2109 case PR_SET_MM_ARG_END
:
2110 prctl_map
.arg_end
= addr
;
2112 case PR_SET_MM_ENV_START
:
2113 prctl_map
.env_start
= addr
;
2115 case PR_SET_MM_ENV_END
:
2116 prctl_map
.env_end
= addr
;
2122 error
= validate_prctl_map(&prctl_map
);
2128 * If command line arguments and environment
2129 * are placed somewhere else on stack, we can
2130 * set them up here, ARG_START/END to setup
2131 * command line argumets and ENV_START/END
2134 case PR_SET_MM_START_STACK
:
2135 case PR_SET_MM_ARG_START
:
2136 case PR_SET_MM_ARG_END
:
2137 case PR_SET_MM_ENV_START
:
2138 case PR_SET_MM_ENV_END
:
2145 mm
->start_code
= prctl_map
.start_code
;
2146 mm
->end_code
= prctl_map
.end_code
;
2147 mm
->start_data
= prctl_map
.start_data
;
2148 mm
->end_data
= prctl_map
.end_data
;
2149 mm
->start_brk
= prctl_map
.start_brk
;
2150 mm
->brk
= prctl_map
.brk
;
2151 mm
->start_stack
= prctl_map
.start_stack
;
2152 mm
->arg_start
= prctl_map
.arg_start
;
2153 mm
->arg_end
= prctl_map
.arg_end
;
2154 mm
->env_start
= prctl_map
.env_start
;
2155 mm
->env_end
= prctl_map
.env_end
;
2159 up_write(&mm
->mmap_sem
);
2163 #ifdef CONFIG_CHECKPOINT_RESTORE
2164 static int prctl_get_tid_address(struct task_struct
*me
, int __user
**tid_addr
)
2166 return put_user(me
->clear_child_tid
, tid_addr
);
2169 static int prctl_get_tid_address(struct task_struct
*me
, int __user
**tid_addr
)
2175 static int propagate_has_child_subreaper(struct task_struct
*p
, void *data
)
2178 * If task has has_child_subreaper - all its decendants
2179 * already have these flag too and new decendants will
2180 * inherit it on fork, skip them.
2182 * If we've found child_reaper - skip descendants in
2183 * it's subtree as they will never get out pidns.
2185 if (p
->signal
->has_child_subreaper
||
2186 is_child_reaper(task_pid(p
)))
2189 p
->signal
->has_child_subreaper
= 1;
2194 static int prctl_update_vma_anon_name(struct vm_area_struct
*vma
,
2195 struct vm_area_struct
**prev
,
2196 unsigned long start
, unsigned long end
,
2197 const char __user
*name_addr
)
2199 struct mm_struct
*mm
= vma
->vm_mm
;
2203 if (name_addr
== vma_get_anon_name(vma
)) {
2208 pgoff
= vma
->vm_pgoff
+ ((start
- vma
->vm_start
) >> PAGE_SHIFT
);
2209 *prev
= vma_merge(mm
, *prev
, start
, end
, vma
->vm_flags
, vma
->anon_vma
,
2210 vma
->vm_file
, pgoff
, vma_policy(vma
),
2211 vma
->vm_userfaultfd_ctx
, name_addr
);
2219 if (start
!= vma
->vm_start
) {
2220 error
= split_vma(mm
, vma
, start
, 1);
2225 if (end
!= vma
->vm_end
) {
2226 error
= split_vma(mm
, vma
, end
, 0);
2233 vma
->anon_name
= name_addr
;
2236 if (error
== -ENOMEM
)
2241 static int prctl_set_vma_anon_name(unsigned long start
, unsigned long end
,
2245 struct vm_area_struct
*vma
, *prev
;
2246 int unmapped_error
= 0;
2247 int error
= -EINVAL
;
2250 * If the interval [start,end) covers some unmapped address
2251 * ranges, just ignore them, but return -ENOMEM at the end.
2252 * - this matches the handling in madvise.
2254 vma
= find_vma_prev(current
->mm
, start
, &prev
);
2255 if (vma
&& start
> vma
->vm_start
)
2259 /* Still start < end. */
2264 /* Here start < (end|vma->vm_end). */
2265 if (start
< vma
->vm_start
) {
2266 unmapped_error
= -ENOMEM
;
2267 start
= vma
->vm_start
;
2272 /* Here vma->vm_start <= start < (end|vma->vm_end) */
2277 /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
2278 error
= prctl_update_vma_anon_name(vma
, &prev
, start
, tmp
,
2279 (const char __user
*)arg
);
2283 if (prev
&& start
< prev
->vm_end
)
2284 start
= prev
->vm_end
;
2285 error
= unmapped_error
;
2289 vma
= prev
->vm_next
;
2290 else /* madvise_remove dropped mmap_sem */
2291 vma
= find_vma(current
->mm
, start
);
2295 static int prctl_set_vma(unsigned long opt
, unsigned long start
,
2296 unsigned long len_in
, unsigned long arg
)
2298 struct mm_struct
*mm
= current
->mm
;
2303 if (start
& ~PAGE_MASK
)
2305 len
= (len_in
+ ~PAGE_MASK
) & PAGE_MASK
;
2307 /* Check to see whether len was rounded up from small -ve to zero */
2318 down_write(&mm
->mmap_sem
);
2321 case PR_SET_VMA_ANON_NAME
:
2322 error
= prctl_set_vma_anon_name(start
, end
, arg
);
2328 up_write(&mm
->mmap_sem
);
2332 #else /* CONFIG_MMU */
2333 static int prctl_set_vma(unsigned long opt
, unsigned long start
,
2334 unsigned long len_in
, unsigned long arg
)
2340 int __weak
arch_prctl_spec_ctrl_get(struct task_struct
*t
, unsigned long which
)
2345 int __weak
arch_prctl_spec_ctrl_set(struct task_struct
*t
, unsigned long which
,
2351 SYSCALL_DEFINE5(prctl
, int, option
, unsigned long, arg2
, unsigned long, arg3
,
2352 unsigned long, arg4
, unsigned long, arg5
)
2354 struct task_struct
*me
= current
;
2355 unsigned char comm
[sizeof(me
->comm
)];
2358 error
= security_task_prctl(option
, arg2
, arg3
, arg4
, arg5
);
2359 if (error
!= -ENOSYS
)
2364 case PR_SET_PDEATHSIG
:
2365 if (!valid_signal(arg2
)) {
2369 me
->pdeath_signal
= arg2
;
2371 case PR_GET_PDEATHSIG
:
2372 error
= put_user(me
->pdeath_signal
, (int __user
*)arg2
);
2374 case PR_GET_DUMPABLE
:
2375 error
= get_dumpable(me
->mm
);
2377 case PR_SET_DUMPABLE
:
2378 if (arg2
!= SUID_DUMP_DISABLE
&& arg2
!= SUID_DUMP_USER
) {
2382 set_dumpable(me
->mm
, arg2
);
2385 case PR_SET_UNALIGN
:
2386 error
= SET_UNALIGN_CTL(me
, arg2
);
2388 case PR_GET_UNALIGN
:
2389 error
= GET_UNALIGN_CTL(me
, arg2
);
2392 error
= SET_FPEMU_CTL(me
, arg2
);
2395 error
= GET_FPEMU_CTL(me
, arg2
);
2398 error
= SET_FPEXC_CTL(me
, arg2
);
2401 error
= GET_FPEXC_CTL(me
, arg2
);
2404 error
= PR_TIMING_STATISTICAL
;
2407 if (arg2
!= PR_TIMING_STATISTICAL
)
2411 comm
[sizeof(me
->comm
) - 1] = 0;
2412 if (strncpy_from_user(comm
, (char __user
*)arg2
,
2413 sizeof(me
->comm
) - 1) < 0)
2415 set_task_comm(me
, comm
);
2416 proc_comm_connector(me
);
2419 get_task_comm(comm
, me
);
2420 if (copy_to_user((char __user
*)arg2
, comm
, sizeof(comm
)))
2424 error
= GET_ENDIAN(me
, arg2
);
2427 error
= SET_ENDIAN(me
, arg2
);
2429 case PR_GET_SECCOMP
:
2430 error
= prctl_get_seccomp();
2432 case PR_SET_SECCOMP
:
2433 error
= prctl_set_seccomp(arg2
, (char __user
*)arg3
);
2436 error
= GET_TSC_CTL(arg2
);
2439 error
= SET_TSC_CTL(arg2
);
2441 case PR_TASK_PERF_EVENTS_DISABLE
:
2442 error
= perf_event_task_disable();
2444 case PR_TASK_PERF_EVENTS_ENABLE
:
2445 error
= perf_event_task_enable();
2447 case PR_GET_TIMERSLACK
:
2448 if (current
->timer_slack_ns
> ULONG_MAX
)
2451 error
= current
->timer_slack_ns
;
2453 case PR_SET_TIMERSLACK
:
2455 current
->timer_slack_ns
=
2456 current
->default_timer_slack_ns
;
2458 current
->timer_slack_ns
= arg2
;
2464 case PR_MCE_KILL_CLEAR
:
2467 current
->flags
&= ~PF_MCE_PROCESS
;
2469 case PR_MCE_KILL_SET
:
2470 current
->flags
|= PF_MCE_PROCESS
;
2471 if (arg3
== PR_MCE_KILL_EARLY
)
2472 current
->flags
|= PF_MCE_EARLY
;
2473 else if (arg3
== PR_MCE_KILL_LATE
)
2474 current
->flags
&= ~PF_MCE_EARLY
;
2475 else if (arg3
== PR_MCE_KILL_DEFAULT
)
2477 ~(PF_MCE_EARLY
|PF_MCE_PROCESS
);
2485 case PR_MCE_KILL_GET
:
2486 if (arg2
| arg3
| arg4
| arg5
)
2488 if (current
->flags
& PF_MCE_PROCESS
)
2489 error
= (current
->flags
& PF_MCE_EARLY
) ?
2490 PR_MCE_KILL_EARLY
: PR_MCE_KILL_LATE
;
2492 error
= PR_MCE_KILL_DEFAULT
;
2495 error
= prctl_set_mm(arg2
, arg3
, arg4
, arg5
);
2497 case PR_GET_TID_ADDRESS
:
2498 error
= prctl_get_tid_address(me
, (int __user
**)arg2
);
2500 case PR_SET_CHILD_SUBREAPER
:
2501 me
->signal
->is_child_subreaper
= !!arg2
;
2505 walk_process_tree(me
, propagate_has_child_subreaper
, NULL
);
2507 case PR_GET_CHILD_SUBREAPER
:
2508 error
= put_user(me
->signal
->is_child_subreaper
,
2509 (int __user
*)arg2
);
2511 case PR_SET_NO_NEW_PRIVS
:
2512 if (arg2
!= 1 || arg3
|| arg4
|| arg5
)
2515 task_set_no_new_privs(current
);
2517 case PR_GET_NO_NEW_PRIVS
:
2518 if (arg2
|| arg3
|| arg4
|| arg5
)
2520 return task_no_new_privs(current
) ? 1 : 0;
2521 case PR_GET_THP_DISABLE
:
2522 if (arg2
|| arg3
|| arg4
|| arg5
)
2524 error
= !!test_bit(MMF_DISABLE_THP
, &me
->mm
->flags
);
2526 case PR_SET_THP_DISABLE
:
2527 if (arg3
|| arg4
|| arg5
)
2529 if (down_write_killable(&me
->mm
->mmap_sem
))
2532 set_bit(MMF_DISABLE_THP
, &me
->mm
->flags
);
2534 clear_bit(MMF_DISABLE_THP
, &me
->mm
->flags
);
2535 up_write(&me
->mm
->mmap_sem
);
2537 case PR_MPX_ENABLE_MANAGEMENT
:
2538 if (arg2
|| arg3
|| arg4
|| arg5
)
2540 error
= MPX_ENABLE_MANAGEMENT();
2542 case PR_MPX_DISABLE_MANAGEMENT
:
2543 if (arg2
|| arg3
|| arg4
|| arg5
)
2545 error
= MPX_DISABLE_MANAGEMENT();
2547 case PR_SET_FP_MODE
:
2548 error
= SET_FP_MODE(me
, arg2
);
2550 case PR_GET_FP_MODE
:
2551 error
= GET_FP_MODE(me
);
2553 case PR_GET_SPECULATION_CTRL
:
2554 if (arg3
|| arg4
|| arg5
)
2556 error
= arch_prctl_spec_ctrl_get(me
, arg2
);
2558 case PR_SET_SPECULATION_CTRL
:
2561 error
= arch_prctl_spec_ctrl_set(me
, arg2
, arg3
);
2564 error
= prctl_set_vma(arg2
, arg3
, arg4
, arg5
);
2573 SYSCALL_DEFINE3(getcpu
, unsigned __user
*, cpup
, unsigned __user
*, nodep
,
2574 struct getcpu_cache __user
*, unused
)
2577 int cpu
= raw_smp_processor_id();
2580 err
|= put_user(cpu
, cpup
);
2582 err
|= put_user(cpu_to_node(cpu
), nodep
);
2583 return err
? -EFAULT
: 0;
2587 * do_sysinfo - fill in sysinfo struct
2588 * @info: pointer to buffer to fill
2590 static int do_sysinfo(struct sysinfo
*info
)
2592 unsigned long mem_total
, sav_total
;
2593 unsigned int mem_unit
, bitcount
;
2596 memset(info
, 0, sizeof(struct sysinfo
));
2598 get_monotonic_boottime(&tp
);
2599 info
->uptime
= tp
.tv_sec
+ (tp
.tv_nsec
? 1 : 0);
2601 get_avenrun(info
->loads
, 0, SI_LOAD_SHIFT
- FSHIFT
);
2603 info
->procs
= nr_threads
;
2609 * If the sum of all the available memory (i.e. ram + swap)
2610 * is less than can be stored in a 32 bit unsigned long then
2611 * we can be binary compatible with 2.2.x kernels. If not,
2612 * well, in that case 2.2.x was broken anyways...
2614 * -Erik Andersen <andersee@debian.org>
2617 mem_total
= info
->totalram
+ info
->totalswap
;
2618 if (mem_total
< info
->totalram
|| mem_total
< info
->totalswap
)
2621 mem_unit
= info
->mem_unit
;
2622 while (mem_unit
> 1) {
2625 sav_total
= mem_total
;
2627 if (mem_total
< sav_total
)
2632 * If mem_total did not overflow, multiply all memory values by
2633 * info->mem_unit and set it to 1. This leaves things compatible
2634 * with 2.2.x, and also retains compatibility with earlier 2.4.x
2639 info
->totalram
<<= bitcount
;
2640 info
->freeram
<<= bitcount
;
2641 info
->sharedram
<<= bitcount
;
2642 info
->bufferram
<<= bitcount
;
2643 info
->totalswap
<<= bitcount
;
2644 info
->freeswap
<<= bitcount
;
2645 info
->totalhigh
<<= bitcount
;
2646 info
->freehigh
<<= bitcount
;
2652 SYSCALL_DEFINE1(sysinfo
, struct sysinfo __user
*, info
)
2658 if (copy_to_user(info
, &val
, sizeof(struct sysinfo
)))
2664 #ifdef CONFIG_COMPAT
2665 struct compat_sysinfo
{
2679 char _f
[20-2*sizeof(u32
)-sizeof(int)];
2682 COMPAT_SYSCALL_DEFINE1(sysinfo
, struct compat_sysinfo __user
*, info
)
2688 /* Check to see if any memory value is too large for 32-bit and scale
2691 if (upper_32_bits(s
.totalram
) || upper_32_bits(s
.totalswap
)) {
2694 while (s
.mem_unit
< PAGE_SIZE
) {
2699 s
.totalram
>>= bitcount
;
2700 s
.freeram
>>= bitcount
;
2701 s
.sharedram
>>= bitcount
;
2702 s
.bufferram
>>= bitcount
;
2703 s
.totalswap
>>= bitcount
;
2704 s
.freeswap
>>= bitcount
;
2705 s
.totalhigh
>>= bitcount
;
2706 s
.freehigh
>>= bitcount
;
2709 if (!access_ok(VERIFY_WRITE
, info
, sizeof(struct compat_sysinfo
)) ||
2710 __put_user(s
.uptime
, &info
->uptime
) ||
2711 __put_user(s
.loads
[0], &info
->loads
[0]) ||
2712 __put_user(s
.loads
[1], &info
->loads
[1]) ||
2713 __put_user(s
.loads
[2], &info
->loads
[2]) ||
2714 __put_user(s
.totalram
, &info
->totalram
) ||
2715 __put_user(s
.freeram
, &info
->freeram
) ||
2716 __put_user(s
.sharedram
, &info
->sharedram
) ||
2717 __put_user(s
.bufferram
, &info
->bufferram
) ||
2718 __put_user(s
.totalswap
, &info
->totalswap
) ||
2719 __put_user(s
.freeswap
, &info
->freeswap
) ||
2720 __put_user(s
.procs
, &info
->procs
) ||
2721 __put_user(s
.totalhigh
, &info
->totalhigh
) ||
2722 __put_user(s
.freehigh
, &info
->freehigh
) ||
2723 __put_user(s
.mem_unit
, &info
->mem_unit
))
2728 #endif /* CONFIG_COMPAT */