1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/export.h>
10 #include <linux/mm_inline.h>
11 #include <linux/utsname.h>
12 #include <linux/mman.h>
13 #include <linux/reboot.h>
14 #include <linux/prctl.h>
15 #include <linux/highuid.h>
17 #include <linux/kmod.h>
18 #include <linux/ksm.h>
19 #include <linux/perf_event.h>
20 #include <linux/resource.h>
21 #include <linux/kernel.h>
22 #include <linux/workqueue.h>
23 #include <linux/capability.h>
24 #include <linux/device.h>
25 #include <linux/key.h>
26 #include <linux/times.h>
27 #include <linux/posix-timers.h>
28 #include <linux/security.h>
29 #include <linux/random.h>
30 #include <linux/suspend.h>
31 #include <linux/tty.h>
32 #include <linux/signal.h>
33 #include <linux/cn_proc.h>
34 #include <linux/getcpu.h>
35 #include <linux/task_io_accounting_ops.h>
36 #include <linux/seccomp.h>
37 #include <linux/cpu.h>
38 #include <linux/personality.h>
39 #include <linux/ptrace.h>
40 #include <linux/fs_struct.h>
41 #include <linux/file.h>
42 #include <linux/mount.h>
43 #include <linux/gfp.h>
44 #include <linux/syscore_ops.h>
45 #include <linux/version.h>
46 #include <linux/ctype.h>
47 #include <linux/syscall_user_dispatch.h>
49 #include <linux/compat.h>
50 #include <linux/syscalls.h>
51 #include <linux/kprobes.h>
52 #include <linux/user_namespace.h>
53 #include <linux/time_namespace.h>
54 #include <linux/binfmts.h>
56 #include <linux/sched.h>
57 #include <linux/sched/autogroup.h>
58 #include <linux/sched/loadavg.h>
59 #include <linux/sched/stat.h>
60 #include <linux/sched/mm.h>
61 #include <linux/sched/coredump.h>
62 #include <linux/sched/task.h>
63 #include <linux/sched/cputime.h>
64 #include <linux/rcupdate.h>
65 #include <linux/uidgid.h>
66 #include <linux/cred.h>
68 #include <linux/nospec.h>
70 #include <linux/kmsg_dump.h>
71 /* Move somewhere else to avoid recompiling? */
72 #include <generated/utsrelease.h>
74 #include <linux/uaccess.h>
76 #include <asm/unistd.h>
80 #ifndef SET_UNALIGN_CTL
81 # define SET_UNALIGN_CTL(a, b) (-EINVAL)
83 #ifndef GET_UNALIGN_CTL
84 # define GET_UNALIGN_CTL(a, b) (-EINVAL)
87 # define SET_FPEMU_CTL(a, b) (-EINVAL)
90 # define GET_FPEMU_CTL(a, b) (-EINVAL)
93 # define SET_FPEXC_CTL(a, b) (-EINVAL)
96 # define GET_FPEXC_CTL(a, b) (-EINVAL)
99 # define GET_ENDIAN(a, b) (-EINVAL)
102 # define SET_ENDIAN(a, b) (-EINVAL)
105 # define GET_TSC_CTL(a) (-EINVAL)
108 # define SET_TSC_CTL(a) (-EINVAL)
111 # define GET_FP_MODE(a) (-EINVAL)
114 # define SET_FP_MODE(a,b) (-EINVAL)
117 # define SVE_SET_VL(a) (-EINVAL)
120 # define SVE_GET_VL() (-EINVAL)
123 # define SME_SET_VL(a) (-EINVAL)
126 # define SME_GET_VL() (-EINVAL)
128 #ifndef PAC_RESET_KEYS
129 # define PAC_RESET_KEYS(a, b) (-EINVAL)
131 #ifndef PAC_SET_ENABLED_KEYS
132 # define PAC_SET_ENABLED_KEYS(a, b, c) (-EINVAL)
134 #ifndef PAC_GET_ENABLED_KEYS
135 # define PAC_GET_ENABLED_KEYS(a) (-EINVAL)
137 #ifndef SET_TAGGED_ADDR_CTRL
138 # define SET_TAGGED_ADDR_CTRL(a) (-EINVAL)
140 #ifndef GET_TAGGED_ADDR_CTRL
141 # define GET_TAGGED_ADDR_CTRL() (-EINVAL)
143 #ifndef RISCV_V_SET_CONTROL
144 # define RISCV_V_SET_CONTROL(a) (-EINVAL)
146 #ifndef RISCV_V_GET_CONTROL
147 # define RISCV_V_GET_CONTROL() (-EINVAL)
149 #ifndef PPC_GET_DEXCR_ASPECT
150 # define PPC_GET_DEXCR_ASPECT(a, b) (-EINVAL)
152 #ifndef PPC_SET_DEXCR_ASPECT
153 # define PPC_SET_DEXCR_ASPECT(a, b, c) (-EINVAL)
157 * this is where the system-wide overflow UID and GID are defined, for
158 * architectures that now have 32-bit UID/GID but didn't in the past
161 int overflowuid = DEFAULT_OVERFLOWUID;
162 int overflowgid = DEFAULT_OVERFLOWGID;
164 EXPORT_SYMBOL(overflowuid);
165 EXPORT_SYMBOL(overflowgid);
168 * the same as above, but for filesystems which can only store a 16-bit
169 * UID and GID. as such, this is needed on all architectures
172 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
173 int fs_overflowgid = DEFAULT_FS_OVERFLOWGID;
175 EXPORT_SYMBOL(fs_overflowuid);
176 EXPORT_SYMBOL(fs_overflowgid);
179 * Returns true if current's euid is same as p's uid or euid,
180 * or has CAP_SYS_NICE to p's user_ns.
182 * Called with rcu_read_lock, creds are safe
184 static bool set_one_prio_perm(struct task_struct *p)
186 const struct cred *cred = current_cred(), *pcred = __task_cred(p);
188 if (uid_eq(pcred->uid, cred->euid) ||
189 uid_eq(pcred->euid, cred->euid))
191 if (ns_capable(pcred->user_ns, CAP_SYS_NICE))
197 * set the priority of a task
198 * - the caller must hold the RCU read lock
200 static int set_one_prio(struct task_struct *p, int niceval, int error)
204 if (!set_one_prio_perm(p)) {
208 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
212 no_nice = security_task_setnice(p, niceval);
219 set_user_nice(p, niceval);
224 SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
226 struct task_struct *g, *p;
227 struct user_struct *user;
228 const struct cred *cred = current_cred();
233 if (which > PRIO_USER || which < PRIO_PROCESS)
236 /* normalize: avoid signed division (rounding problems) */
238 if (niceval < MIN_NICE)
240 if (niceval > MAX_NICE)
247 p = find_task_by_vpid(who);
251 error = set_one_prio(p, niceval, error);
255 pgrp = find_vpid(who);
257 pgrp = task_pgrp(current);
258 read_lock(&tasklist_lock);
259 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
260 error = set_one_prio(p, niceval, error);
261 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
262 read_unlock(&tasklist_lock);
265 uid = make_kuid(cred->user_ns, who);
269 else if (!uid_eq(uid, cred->uid)) {
270 user = find_user(uid);
272 goto out_unlock; /* No processes for this user */
274 for_each_process_thread(g, p) {
275 if (uid_eq(task_uid(p), uid) && task_pid_vnr(p))
276 error = set_one_prio(p, niceval, error);
278 if (!uid_eq(uid, cred->uid))
279 free_uid(user); /* For find_user() */
289 * Ugh. To avoid negative return values, "getpriority()" will
290 * not return the normal nice-value, but a negated value that
291 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
292 * to stay compatible.
294 SYSCALL_DEFINE2(getpriority, int, which, int, who)
296 struct task_struct *g, *p;
297 struct user_struct *user;
298 const struct cred *cred = current_cred();
299 long niceval, retval = -ESRCH;
303 if (which > PRIO_USER || which < PRIO_PROCESS)
310 p = find_task_by_vpid(who);
314 niceval = nice_to_rlimit(task_nice(p));
315 if (niceval > retval)
321 pgrp = find_vpid(who);
323 pgrp = task_pgrp(current);
324 read_lock(&tasklist_lock);
325 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
326 niceval = nice_to_rlimit(task_nice(p));
327 if (niceval > retval)
329 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
330 read_unlock(&tasklist_lock);
333 uid = make_kuid(cred->user_ns, who);
337 else if (!uid_eq(uid, cred->uid)) {
338 user = find_user(uid);
340 goto out_unlock; /* No processes for this user */
342 for_each_process_thread(g, p) {
343 if (uid_eq(task_uid(p), uid) && task_pid_vnr(p)) {
344 niceval = nice_to_rlimit(task_nice(p));
345 if (niceval > retval)
349 if (!uid_eq(uid, cred->uid))
350 free_uid(user); /* for find_user() */
360 * Unprivileged users may change the real gid to the effective gid
361 * or vice versa. (BSD-style)
363 * If you set the real gid at all, or set the effective gid to a value not
364 * equal to the real gid, then the saved gid is set to the new effective gid.
366 * This makes it possible for a setgid program to completely drop its
367 * privileges, which is often a useful assertion to make when you are doing
368 * a security audit over a program.
370 * The general idea is that a program which uses just setregid() will be
371 * 100% compatible with BSD. A program which uses just setgid() will be
372 * 100% compatible with POSIX with saved IDs.
374 * SMP: There are not races, the GIDs are checked only by filesystem
375 * operations (as far as semantic preservation is concerned).
377 #ifdef CONFIG_MULTIUSER
378 long __sys_setregid(gid_t rgid, gid_t egid)
380 struct user_namespace *ns = current_user_ns();
381 const struct cred *old;
386 krgid = make_kgid(ns, rgid);
387 kegid = make_kgid(ns, egid);
389 if ((rgid != (gid_t) -1) && !gid_valid(krgid))
391 if ((egid != (gid_t) -1) && !gid_valid(kegid))
394 new = prepare_creds();
397 old = current_cred();
400 if (rgid != (gid_t) -1) {
401 if (gid_eq(old->gid, krgid) ||
402 gid_eq(old->egid, krgid) ||
403 ns_capable_setid(old->user_ns, CAP_SETGID))
408 if (egid != (gid_t) -1) {
409 if (gid_eq(old->gid, kegid) ||
410 gid_eq(old->egid, kegid) ||
411 gid_eq(old->sgid, kegid) ||
412 ns_capable_setid(old->user_ns, CAP_SETGID))
418 if (rgid != (gid_t) -1 ||
419 (egid != (gid_t) -1 && !gid_eq(kegid, old->gid)))
420 new->sgid = new->egid;
421 new->fsgid = new->egid;
423 retval = security_task_fix_setgid(new, old, LSM_SETID_RE);
427 return commit_creds(new);
434 SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
436 return __sys_setregid(rgid, egid);
440 * setgid() is implemented like SysV w/ SAVED_IDS
442 * SMP: Same implicit races as above.
444 long __sys_setgid(gid_t gid)
446 struct user_namespace *ns = current_user_ns();
447 const struct cred *old;
452 kgid = make_kgid(ns, gid);
453 if (!gid_valid(kgid))
456 new = prepare_creds();
459 old = current_cred();
462 if (ns_capable_setid(old->user_ns, CAP_SETGID))
463 new->gid = new->egid = new->sgid = new->fsgid = kgid;
464 else if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->sgid))
465 new->egid = new->fsgid = kgid;
469 retval = security_task_fix_setgid(new, old, LSM_SETID_ID);
473 return commit_creds(new);
480 SYSCALL_DEFINE1(setgid, gid_t, gid)
482 return __sys_setgid(gid);
486 * change the user struct in a credentials set to match the new UID
488 static int set_user(struct cred *new)
490 struct user_struct *new_user;
492 new_user = alloc_uid(new->uid);
497 new->user = new_user;
501 static void flag_nproc_exceeded(struct cred *new)
503 if (new->ucounts == current_ucounts())
507 * We don't fail in case of NPROC limit excess here because too many
508 * poorly written programs don't check set*uid() return code, assuming
509 * it never fails if called by root. We may still enforce NPROC limit
510 * for programs doing set*uid()+execve() by harmlessly deferring the
511 * failure to the execve() stage.
513 if (is_rlimit_overlimit(new->ucounts, UCOUNT_RLIMIT_NPROC, rlimit(RLIMIT_NPROC)) &&
514 new->user != INIT_USER)
515 current->flags |= PF_NPROC_EXCEEDED;
517 current->flags &= ~PF_NPROC_EXCEEDED;
521 * Unprivileged users may change the real uid to the effective uid
522 * or vice versa. (BSD-style)
524 * If you set the real uid at all, or set the effective uid to a value not
525 * equal to the real uid, then the saved uid is set to the new effective uid.
527 * This makes it possible for a setuid program to completely drop its
528 * privileges, which is often a useful assertion to make when you are doing
529 * a security audit over a program.
531 * The general idea is that a program which uses just setreuid() will be
532 * 100% compatible with BSD. A program which uses just setuid() will be
533 * 100% compatible with POSIX with saved IDs.
535 long __sys_setreuid(uid_t ruid, uid_t euid)
537 struct user_namespace *ns = current_user_ns();
538 const struct cred *old;
543 kruid = make_kuid(ns, ruid);
544 keuid = make_kuid(ns, euid);
546 if ((ruid != (uid_t) -1) && !uid_valid(kruid))
548 if ((euid != (uid_t) -1) && !uid_valid(keuid))
551 new = prepare_creds();
554 old = current_cred();
557 if (ruid != (uid_t) -1) {
559 if (!uid_eq(old->uid, kruid) &&
560 !uid_eq(old->euid, kruid) &&
561 !ns_capable_setid(old->user_ns, CAP_SETUID))
565 if (euid != (uid_t) -1) {
567 if (!uid_eq(old->uid, keuid) &&
568 !uid_eq(old->euid, keuid) &&
569 !uid_eq(old->suid, keuid) &&
570 !ns_capable_setid(old->user_ns, CAP_SETUID))
574 if (!uid_eq(new->uid, old->uid)) {
575 retval = set_user(new);
579 if (ruid != (uid_t) -1 ||
580 (euid != (uid_t) -1 && !uid_eq(keuid, old->uid)))
581 new->suid = new->euid;
582 new->fsuid = new->euid;
584 retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
588 retval = set_cred_ucounts(new);
592 flag_nproc_exceeded(new);
593 return commit_creds(new);
600 SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
602 return __sys_setreuid(ruid, euid);
606 * setuid() is implemented like SysV with SAVED_IDS
608 * Note that SAVED_ID's is deficient in that a setuid root program
609 * like sendmail, for example, cannot set its uid to be a normal
610 * user and then switch back, because if you're root, setuid() sets
611 * the saved uid too. If you don't like this, blame the bright people
612 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
613 * will allow a root program to temporarily drop privileges and be able to
614 * regain them by swapping the real and effective uid.
616 long __sys_setuid(uid_t uid)
618 struct user_namespace *ns = current_user_ns();
619 const struct cred *old;
624 kuid = make_kuid(ns, uid);
625 if (!uid_valid(kuid))
628 new = prepare_creds();
631 old = current_cred();
634 if (ns_capable_setid(old->user_ns, CAP_SETUID)) {
635 new->suid = new->uid = kuid;
636 if (!uid_eq(kuid, old->uid)) {
637 retval = set_user(new);
641 } else if (!uid_eq(kuid, old->uid) && !uid_eq(kuid, new->suid)) {
645 new->fsuid = new->euid = kuid;
647 retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
651 retval = set_cred_ucounts(new);
655 flag_nproc_exceeded(new);
656 return commit_creds(new);
663 SYSCALL_DEFINE1(setuid, uid_t, uid)
665 return __sys_setuid(uid);
670 * This function implements a generic ability to update ruid, euid,
671 * and suid. This allows you to implement the 4.4 compatible seteuid().
673 long __sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
675 struct user_namespace *ns = current_user_ns();
676 const struct cred *old;
679 kuid_t kruid, keuid, ksuid;
680 bool ruid_new, euid_new, suid_new;
682 kruid = make_kuid(ns, ruid);
683 keuid = make_kuid(ns, euid);
684 ksuid = make_kuid(ns, suid);
686 if ((ruid != (uid_t) -1) && !uid_valid(kruid))
689 if ((euid != (uid_t) -1) && !uid_valid(keuid))
692 if ((suid != (uid_t) -1) && !uid_valid(ksuid))
695 old = current_cred();
697 /* check for no-op */
698 if ((ruid == (uid_t) -1 || uid_eq(kruid, old->uid)) &&
699 (euid == (uid_t) -1 || (uid_eq(keuid, old->euid) &&
700 uid_eq(keuid, old->fsuid))) &&
701 (suid == (uid_t) -1 || uid_eq(ksuid, old->suid)))
704 ruid_new = ruid != (uid_t) -1 && !uid_eq(kruid, old->uid) &&
705 !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid);
706 euid_new = euid != (uid_t) -1 && !uid_eq(keuid, old->uid) &&
707 !uid_eq(keuid, old->euid) && !uid_eq(keuid, old->suid);
708 suid_new = suid != (uid_t) -1 && !uid_eq(ksuid, old->uid) &&
709 !uid_eq(ksuid, old->euid) && !uid_eq(ksuid, old->suid);
710 if ((ruid_new || euid_new || suid_new) &&
711 !ns_capable_setid(old->user_ns, CAP_SETUID))
714 new = prepare_creds();
718 if (ruid != (uid_t) -1) {
720 if (!uid_eq(kruid, old->uid)) {
721 retval = set_user(new);
726 if (euid != (uid_t) -1)
728 if (suid != (uid_t) -1)
730 new->fsuid = new->euid;
732 retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
736 retval = set_cred_ucounts(new);
740 flag_nproc_exceeded(new);
741 return commit_creds(new);
748 SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
750 return __sys_setresuid(ruid, euid, suid);
753 SYSCALL_DEFINE3(getresuid, uid_t __user *, ruidp, uid_t __user *, euidp, uid_t __user *, suidp)
755 const struct cred *cred = current_cred();
757 uid_t ruid, euid, suid;
759 ruid = from_kuid_munged(cred->user_ns, cred->uid);
760 euid = from_kuid_munged(cred->user_ns, cred->euid);
761 suid = from_kuid_munged(cred->user_ns, cred->suid);
763 retval = put_user(ruid, ruidp);
765 retval = put_user(euid, euidp);
767 return put_user(suid, suidp);
773 * Same as above, but for rgid, egid, sgid.
775 long __sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
777 struct user_namespace *ns = current_user_ns();
778 const struct cred *old;
781 kgid_t krgid, kegid, ksgid;
782 bool rgid_new, egid_new, sgid_new;
784 krgid = make_kgid(ns, rgid);
785 kegid = make_kgid(ns, egid);
786 ksgid = make_kgid(ns, sgid);
788 if ((rgid != (gid_t) -1) && !gid_valid(krgid))
790 if ((egid != (gid_t) -1) && !gid_valid(kegid))
792 if ((sgid != (gid_t) -1) && !gid_valid(ksgid))
795 old = current_cred();
797 /* check for no-op */
798 if ((rgid == (gid_t) -1 || gid_eq(krgid, old->gid)) &&
799 (egid == (gid_t) -1 || (gid_eq(kegid, old->egid) &&
800 gid_eq(kegid, old->fsgid))) &&
801 (sgid == (gid_t) -1 || gid_eq(ksgid, old->sgid)))
804 rgid_new = rgid != (gid_t) -1 && !gid_eq(krgid, old->gid) &&
805 !gid_eq(krgid, old->egid) && !gid_eq(krgid, old->sgid);
806 egid_new = egid != (gid_t) -1 && !gid_eq(kegid, old->gid) &&
807 !gid_eq(kegid, old->egid) && !gid_eq(kegid, old->sgid);
808 sgid_new = sgid != (gid_t) -1 && !gid_eq(ksgid, old->gid) &&
809 !gid_eq(ksgid, old->egid) && !gid_eq(ksgid, old->sgid);
810 if ((rgid_new || egid_new || sgid_new) &&
811 !ns_capable_setid(old->user_ns, CAP_SETGID))
814 new = prepare_creds();
818 if (rgid != (gid_t) -1)
820 if (egid != (gid_t) -1)
822 if (sgid != (gid_t) -1)
824 new->fsgid = new->egid;
826 retval = security_task_fix_setgid(new, old, LSM_SETID_RES);
830 return commit_creds(new);
837 SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
839 return __sys_setresgid(rgid, egid, sgid);
842 SYSCALL_DEFINE3(getresgid, gid_t __user *, rgidp, gid_t __user *, egidp, gid_t __user *, sgidp)
844 const struct cred *cred = current_cred();
846 gid_t rgid, egid, sgid;
848 rgid = from_kgid_munged(cred->user_ns, cred->gid);
849 egid = from_kgid_munged(cred->user_ns, cred->egid);
850 sgid = from_kgid_munged(cred->user_ns, cred->sgid);
852 retval = put_user(rgid, rgidp);
854 retval = put_user(egid, egidp);
856 retval = put_user(sgid, sgidp);
864 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
865 * is used for "access()" and for the NFS daemon (letting nfsd stay at
866 * whatever uid it wants to). It normally shadows "euid", except when
867 * explicitly set by setfsuid() or for access..
869 long __sys_setfsuid(uid_t uid)
871 const struct cred *old;
876 old = current_cred();
877 old_fsuid = from_kuid_munged(old->user_ns, old->fsuid);
879 kuid = make_kuid(old->user_ns, uid);
880 if (!uid_valid(kuid))
883 new = prepare_creds();
887 if (uid_eq(kuid, old->uid) || uid_eq(kuid, old->euid) ||
888 uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) ||
889 ns_capable_setid(old->user_ns, CAP_SETUID)) {
890 if (!uid_eq(kuid, old->fsuid)) {
892 if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
905 SYSCALL_DEFINE1(setfsuid, uid_t, uid)
907 return __sys_setfsuid(uid);
911 * Samma på svenska..
913 long __sys_setfsgid(gid_t gid)
915 const struct cred *old;
920 old = current_cred();
921 old_fsgid = from_kgid_munged(old->user_ns, old->fsgid);
923 kgid = make_kgid(old->user_ns, gid);
924 if (!gid_valid(kgid))
927 new = prepare_creds();
931 if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->egid) ||
932 gid_eq(kgid, old->sgid) || gid_eq(kgid, old->fsgid) ||
933 ns_capable_setid(old->user_ns, CAP_SETGID)) {
934 if (!gid_eq(kgid, old->fsgid)) {
936 if (security_task_fix_setgid(new,old,LSM_SETID_FS) == 0)
949 SYSCALL_DEFINE1(setfsgid, gid_t, gid)
951 return __sys_setfsgid(gid);
953 #endif /* CONFIG_MULTIUSER */
956 * sys_getpid - return the thread group id of the current process
958 * Note, despite the name, this returns the tgid not the pid. The tgid and
959 * the pid are identical unless CLONE_THREAD was specified on clone() in
960 * which case the tgid is the same in all threads of the same group.
962 * This is SMP safe as current->tgid does not change.
964 SYSCALL_DEFINE0(getpid)
966 return task_tgid_vnr(current);
969 /* Thread ID - the internal kernel "pid" */
970 SYSCALL_DEFINE0(gettid)
972 return task_pid_vnr(current);
976 * Accessing ->real_parent is not SMP-safe, it could
977 * change from under us. However, we can use a stale
978 * value of ->real_parent under rcu_read_lock(), see
979 * release_task()->call_rcu(delayed_put_task_struct).
981 SYSCALL_DEFINE0(getppid)
986 pid = task_tgid_vnr(rcu_dereference(current->real_parent));
992 SYSCALL_DEFINE0(getuid)
994 /* Only we change this so SMP safe */
995 return from_kuid_munged(current_user_ns(), current_uid());
998 SYSCALL_DEFINE0(geteuid)
1000 /* Only we change this so SMP safe */
1001 return from_kuid_munged(current_user_ns(), current_euid());
1004 SYSCALL_DEFINE0(getgid)
1006 /* Only we change this so SMP safe */
1007 return from_kgid_munged(current_user_ns(), current_gid());
1010 SYSCALL_DEFINE0(getegid)
1012 /* Only we change this so SMP safe */
1013 return from_kgid_munged(current_user_ns(), current_egid());
1016 static void do_sys_times(struct tms *tms)
1018 u64 tgutime, tgstime, cutime, cstime;
1020 thread_group_cputime_adjusted(current, &tgutime, &tgstime);
1021 cutime = current->signal->cutime;
1022 cstime = current->signal->cstime;
1023 tms->tms_utime = nsec_to_clock_t(tgutime);
1024 tms->tms_stime = nsec_to_clock_t(tgstime);
1025 tms->tms_cutime = nsec_to_clock_t(cutime);
1026 tms->tms_cstime = nsec_to_clock_t(cstime);
1029 SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
1035 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1038 force_successful_syscall_return();
1039 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1042 #ifdef CONFIG_COMPAT
1043 static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
1045 return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
1048 COMPAT_SYSCALL_DEFINE1(times, struct compat_tms __user *, tbuf)
1052 struct compat_tms tmp;
1055 /* Convert our struct tms to the compat version. */
1056 tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
1057 tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
1058 tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
1059 tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
1060 if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
1063 force_successful_syscall_return();
1064 return compat_jiffies_to_clock_t(jiffies);
1069 * This needs some heavy checking ...
1070 * I just haven't the stomach for it. I also don't fully
1071 * understand sessions/pgrp etc. Let somebody who does explain it.
1073 * OK, I think I have the protection semantics right.... this is really
1074 * only important on a multi-user system anyway, to make sure one user
1075 * can't send a signal to a process owned by another. -TYT, 12/12/91
1077 * !PF_FORKNOEXEC check to conform completely to POSIX.
1079 SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
1081 struct task_struct *p;
1082 struct task_struct *group_leader = current->group_leader;
1087 pid = task_pid_vnr(group_leader);
1094 /* From this point forward we keep holding onto the tasklist lock
1095 * so that our parent does not change from under us. -DaveM
1097 write_lock_irq(&tasklist_lock);
1100 p = find_task_by_vpid(pid);
1105 if (!thread_group_leader(p))
1108 if (same_thread_group(p->real_parent, group_leader)) {
1110 if (task_session(p) != task_session(group_leader))
1113 if (!(p->flags & PF_FORKNOEXEC))
1117 if (p != group_leader)
1122 if (p->signal->leader)
1127 struct task_struct *g;
1129 pgrp = find_vpid(pgid);
1130 g = pid_task(pgrp, PIDTYPE_PGID);
1131 if (!g || task_session(g) != task_session(group_leader))
1135 err = security_task_setpgid(p, pgid);
1139 if (task_pgrp(p) != pgrp)
1140 change_pid(p, PIDTYPE_PGID, pgrp);
1144 /* All paths lead to here, thus we are safe. -DaveM */
1145 write_unlock_irq(&tasklist_lock);
1150 static int do_getpgid(pid_t pid)
1152 struct task_struct *p;
1158 grp = task_pgrp(current);
1161 p = find_task_by_vpid(pid);
1168 retval = security_task_getpgid(p);
1172 retval = pid_vnr(grp);
1178 SYSCALL_DEFINE1(getpgid, pid_t, pid)
1180 return do_getpgid(pid);
1183 #ifdef __ARCH_WANT_SYS_GETPGRP
1185 SYSCALL_DEFINE0(getpgrp)
1187 return do_getpgid(0);
1192 SYSCALL_DEFINE1(getsid, pid_t, pid)
1194 struct task_struct *p;
1200 sid = task_session(current);
1203 p = find_task_by_vpid(pid);
1206 sid = task_session(p);
1210 retval = security_task_getsid(p);
1214 retval = pid_vnr(sid);
1220 static void set_special_pids(struct pid *pid)
1222 struct task_struct *curr = current->group_leader;
1224 if (task_session(curr) != pid)
1225 change_pid(curr, PIDTYPE_SID, pid);
1227 if (task_pgrp(curr) != pid)
1228 change_pid(curr, PIDTYPE_PGID, pid);
1231 int ksys_setsid(void)
1233 struct task_struct *group_leader = current->group_leader;
1234 struct pid *sid = task_pid(group_leader);
1235 pid_t session = pid_vnr(sid);
1238 write_lock_irq(&tasklist_lock);
1239 /* Fail if I am already a session leader */
1240 if (group_leader->signal->leader)
1243 /* Fail if a process group id already exists that equals the
1244 * proposed session id.
1246 if (pid_task(sid, PIDTYPE_PGID))
1249 group_leader->signal->leader = 1;
1250 set_special_pids(sid);
1252 proc_clear_tty(group_leader);
1256 write_unlock_irq(&tasklist_lock);
1258 proc_sid_connector(group_leader);
1259 sched_autogroup_create_attach(group_leader);
1264 SYSCALL_DEFINE0(setsid)
1266 return ksys_setsid();
1269 DECLARE_RWSEM(uts_sem);
1271 #ifdef COMPAT_UTS_MACHINE
1272 #define override_architecture(name) \
1273 (personality(current->personality) == PER_LINUX32 && \
1274 copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1275 sizeof(COMPAT_UTS_MACHINE)))
1277 #define override_architecture(name) 0
1281 * Work around broken programs that cannot handle "Linux 3.0".
1282 * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40
1283 * And we map 4.x and later versions to 2.6.60+x, so 4.0/5.0/6.0/... would be
1286 static int override_release(char __user *release, size_t len)
1290 if (current->personality & UNAME26) {
1291 const char *rest = UTS_RELEASE;
1292 char buf[65] = { 0 };
1298 if (*rest == '.' && ++ndots >= 3)
1300 if (!isdigit(*rest) && *rest != '.')
1304 v = LINUX_VERSION_PATCHLEVEL + 60;
1305 copy = clamp_t(size_t, len, 1, sizeof(buf));
1306 copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
1307 ret = copy_to_user(release, buf, copy + 1);
1312 SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
1314 struct new_utsname tmp;
1316 down_read(&uts_sem);
1317 memcpy(&tmp, utsname(), sizeof(tmp));
1319 if (copy_to_user(name, &tmp, sizeof(tmp)))
1322 if (override_release(name->release, sizeof(name->release)))
1324 if (override_architecture(name))
1329 #ifdef __ARCH_WANT_SYS_OLD_UNAME
1333 SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
1335 struct old_utsname tmp;
1340 down_read(&uts_sem);
1341 memcpy(&tmp, utsname(), sizeof(tmp));
1343 if (copy_to_user(name, &tmp, sizeof(tmp)))
1346 if (override_release(name->release, sizeof(name->release)))
1348 if (override_architecture(name))
1353 SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
1355 struct oldold_utsname tmp;
1360 memset(&tmp, 0, sizeof(tmp));
1362 down_read(&uts_sem);
1363 memcpy(&tmp.sysname, &utsname()->sysname, __OLD_UTS_LEN);
1364 memcpy(&tmp.nodename, &utsname()->nodename, __OLD_UTS_LEN);
1365 memcpy(&tmp.release, &utsname()->release, __OLD_UTS_LEN);
1366 memcpy(&tmp.version, &utsname()->version, __OLD_UTS_LEN);
1367 memcpy(&tmp.machine, &utsname()->machine, __OLD_UTS_LEN);
1369 if (copy_to_user(name, &tmp, sizeof(tmp)))
1372 if (override_architecture(name))
1374 if (override_release(name->release, sizeof(name->release)))
1380 SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
1383 char tmp[__NEW_UTS_LEN];
1385 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
1388 if (len < 0 || len > __NEW_UTS_LEN)
1391 if (!copy_from_user(tmp, name, len)) {
1392 struct new_utsname *u;
1394 add_device_randomness(tmp, len);
1395 down_write(&uts_sem);
1397 memcpy(u->nodename, tmp, len);
1398 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
1400 uts_proc_notify(UTS_PROC_HOSTNAME);
1406 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1408 SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
1411 struct new_utsname *u;
1412 char tmp[__NEW_UTS_LEN + 1];
1416 down_read(&uts_sem);
1418 i = 1 + strlen(u->nodename);
1421 memcpy(tmp, u->nodename, i);
1423 if (copy_to_user(name, tmp, i))
1431 * Only setdomainname; getdomainname can be implemented by calling
1434 SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
1437 char tmp[__NEW_UTS_LEN];
1439 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
1441 if (len < 0 || len > __NEW_UTS_LEN)
1445 if (!copy_from_user(tmp, name, len)) {
1446 struct new_utsname *u;
1448 add_device_randomness(tmp, len);
1449 down_write(&uts_sem);
1451 memcpy(u->domainname, tmp, len);
1452 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
1454 uts_proc_notify(UTS_PROC_DOMAINNAME);
1460 /* make sure you are allowed to change @tsk limits before calling this */
1461 static int do_prlimit(struct task_struct *tsk, unsigned int resource,
1462 struct rlimit *new_rlim, struct rlimit *old_rlim)
1464 struct rlimit *rlim;
1467 if (resource >= RLIM_NLIMITS)
1469 resource = array_index_nospec(resource, RLIM_NLIMITS);
1472 if (new_rlim->rlim_cur > new_rlim->rlim_max)
1474 if (resource == RLIMIT_NOFILE &&
1475 new_rlim->rlim_max > sysctl_nr_open)
1479 /* Holding a refcount on tsk protects tsk->signal from disappearing. */
1480 rlim = tsk->signal->rlim + resource;
1481 task_lock(tsk->group_leader);
1484 * Keep the capable check against init_user_ns until cgroups can
1485 * contain all limits.
1487 if (new_rlim->rlim_max > rlim->rlim_max &&
1488 !capable(CAP_SYS_RESOURCE))
1491 retval = security_task_setrlimit(tsk, resource, new_rlim);
1499 task_unlock(tsk->group_leader);
1502 * RLIMIT_CPU handling. Arm the posix CPU timer if the limit is not
1503 * infinite. In case of RLIM_INFINITY the posix CPU timer code
1504 * ignores the rlimit.
1506 if (!retval && new_rlim && resource == RLIMIT_CPU &&
1507 new_rlim->rlim_cur != RLIM_INFINITY &&
1508 IS_ENABLED(CONFIG_POSIX_TIMERS)) {
1510 * update_rlimit_cpu can fail if the task is exiting, but there
1511 * may be other tasks in the thread group that are not exiting,
1512 * and they need their cpu timers adjusted.
1514 * The group_leader is the last task to be released, so if we
1515 * cannot update_rlimit_cpu on it, then the entire process is
1516 * exiting and we do not need to update at all.
1518 update_rlimit_cpu(tsk->group_leader, new_rlim->rlim_cur);
1524 SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1526 struct rlimit value;
1529 ret = do_prlimit(current, resource, NULL, &value);
1531 ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1536 #ifdef CONFIG_COMPAT
1538 COMPAT_SYSCALL_DEFINE2(setrlimit, unsigned int, resource,
1539 struct compat_rlimit __user *, rlim)
1542 struct compat_rlimit r32;
1544 if (copy_from_user(&r32, rlim, sizeof(struct compat_rlimit)))
1547 if (r32.rlim_cur == COMPAT_RLIM_INFINITY)
1548 r.rlim_cur = RLIM_INFINITY;
1550 r.rlim_cur = r32.rlim_cur;
1551 if (r32.rlim_max == COMPAT_RLIM_INFINITY)
1552 r.rlim_max = RLIM_INFINITY;
1554 r.rlim_max = r32.rlim_max;
1555 return do_prlimit(current, resource, &r, NULL);
1558 COMPAT_SYSCALL_DEFINE2(getrlimit, unsigned int, resource,
1559 struct compat_rlimit __user *, rlim)
1564 ret = do_prlimit(current, resource, NULL, &r);
1566 struct compat_rlimit r32;
1567 if (r.rlim_cur > COMPAT_RLIM_INFINITY)
1568 r32.rlim_cur = COMPAT_RLIM_INFINITY;
1570 r32.rlim_cur = r.rlim_cur;
1571 if (r.rlim_max > COMPAT_RLIM_INFINITY)
1572 r32.rlim_max = COMPAT_RLIM_INFINITY;
1574 r32.rlim_max = r.rlim_max;
1576 if (copy_to_user(rlim, &r32, sizeof(struct compat_rlimit)))
1584 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1587 * Back compatibility for getrlimit. Needed for some apps.
1589 SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1590 struct rlimit __user *, rlim)
1593 if (resource >= RLIM_NLIMITS)
1596 resource = array_index_nospec(resource, RLIM_NLIMITS);
1597 task_lock(current->group_leader);
1598 x = current->signal->rlim[resource];
1599 task_unlock(current->group_leader);
1600 if (x.rlim_cur > 0x7FFFFFFF)
1601 x.rlim_cur = 0x7FFFFFFF;
1602 if (x.rlim_max > 0x7FFFFFFF)
1603 x.rlim_max = 0x7FFFFFFF;
1604 return copy_to_user(rlim, &x, sizeof(x)) ? -EFAULT : 0;
1607 #ifdef CONFIG_COMPAT
1608 COMPAT_SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1609 struct compat_rlimit __user *, rlim)
1613 if (resource >= RLIM_NLIMITS)
1616 resource = array_index_nospec(resource, RLIM_NLIMITS);
1617 task_lock(current->group_leader);
1618 r = current->signal->rlim[resource];
1619 task_unlock(current->group_leader);
1620 if (r.rlim_cur > 0x7FFFFFFF)
1621 r.rlim_cur = 0x7FFFFFFF;
1622 if (r.rlim_max > 0x7FFFFFFF)
1623 r.rlim_max = 0x7FFFFFFF;
1625 if (put_user(r.rlim_cur, &rlim->rlim_cur) ||
1626 put_user(r.rlim_max, &rlim->rlim_max))
1634 static inline bool rlim64_is_infinity(__u64 rlim64)
1636 #if BITS_PER_LONG < 64
1637 return rlim64 >= ULONG_MAX;
1639 return rlim64 == RLIM64_INFINITY;
1643 static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1645 if (rlim->rlim_cur == RLIM_INFINITY)
1646 rlim64->rlim_cur = RLIM64_INFINITY;
1648 rlim64->rlim_cur = rlim->rlim_cur;
1649 if (rlim->rlim_max == RLIM_INFINITY)
1650 rlim64->rlim_max = RLIM64_INFINITY;
1652 rlim64->rlim_max = rlim->rlim_max;
1655 static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1657 if (rlim64_is_infinity(rlim64->rlim_cur))
1658 rlim->rlim_cur = RLIM_INFINITY;
1660 rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1661 if (rlim64_is_infinity(rlim64->rlim_max))
1662 rlim->rlim_max = RLIM_INFINITY;
1664 rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1667 /* rcu lock must be held */
1668 static int check_prlimit_permission(struct task_struct *task,
1671 const struct cred *cred = current_cred(), *tcred;
1674 if (current == task)
1677 tcred = __task_cred(task);
1678 id_match = (uid_eq(cred->uid, tcred->euid) &&
1679 uid_eq(cred->uid, tcred->suid) &&
1680 uid_eq(cred->uid, tcred->uid) &&
1681 gid_eq(cred->gid, tcred->egid) &&
1682 gid_eq(cred->gid, tcred->sgid) &&
1683 gid_eq(cred->gid, tcred->gid));
1684 if (!id_match && !ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
1687 return security_task_prlimit(cred, tcred, flags);
1690 SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1691 const struct rlimit64 __user *, new_rlim,
1692 struct rlimit64 __user *, old_rlim)
1694 struct rlimit64 old64, new64;
1695 struct rlimit old, new;
1696 struct task_struct *tsk;
1697 unsigned int checkflags = 0;
1701 checkflags |= LSM_PRLIMIT_READ;
1704 if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1706 rlim64_to_rlim(&new64, &new);
1707 checkflags |= LSM_PRLIMIT_WRITE;
1711 tsk = pid ? find_task_by_vpid(pid) : current;
1716 ret = check_prlimit_permission(tsk, checkflags);
1721 get_task_struct(tsk);
1724 ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1725 old_rlim ? &old : NULL);
1727 if (!ret && old_rlim) {
1728 rlim_to_rlim64(&old, &old64);
1729 if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1733 put_task_struct(tsk);
1737 SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1739 struct rlimit new_rlim;
1741 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1743 return do_prlimit(current, resource, &new_rlim, NULL);
1747 * It would make sense to put struct rusage in the task_struct,
1748 * except that would make the task_struct be *really big*. After
1749 * task_struct gets moved into malloc'ed memory, it would
1750 * make sense to do this. It will make moving the rest of the information
1751 * a lot simpler! (Which we're not doing right now because we're not
1752 * measuring them yet).
1754 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1755 * races with threads incrementing their own counters. But since word
1756 * reads are atomic, we either get new values or old values and we don't
1757 * care which for the sums. We always take the siglock to protect reading
1758 * the c* fields from p->signal from races with exit.c updating those
1759 * fields when reaping, so a sample either gets all the additions of a
1760 * given child after it's reaped, or none so this sample is before reaping.
1763 * We need to take the siglock for CHILDEREN, SELF and BOTH
1764 * for the cases current multithreaded, non-current single threaded
1765 * non-current multithreaded. Thread traversal is now safe with
1767 * Strictly speaking, we donot need to take the siglock if we are current and
1768 * single threaded, as no one else can take our signal_struct away, no one
1769 * else can reap the children to update signal->c* counters, and no one else
1770 * can race with the signal-> fields. If we do not take any lock, the
1771 * signal-> fields could be read out of order while another thread was just
1772 * exiting. So we should place a read memory barrier when we avoid the lock.
1773 * On the writer side, write memory barrier is implied in __exit_signal
1774 * as __exit_signal releases the siglock spinlock after updating the signal->
1775 * fields. But we don't do this yet to keep things simple.
1779 static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
1781 r->ru_nvcsw += t->nvcsw;
1782 r->ru_nivcsw += t->nivcsw;
1783 r->ru_minflt += t->min_flt;
1784 r->ru_majflt += t->maj_flt;
1785 r->ru_inblock += task_io_get_inblock(t);
1786 r->ru_oublock += task_io_get_oublock(t);
1789 void getrusage(struct task_struct *p, int who, struct rusage *r)
1791 struct task_struct *t;
1792 unsigned long flags;
1793 u64 tgutime, tgstime, utime, stime;
1794 unsigned long maxrss;
1795 struct mm_struct *mm;
1796 struct signal_struct *sig = p->signal;
1797 unsigned int seq = 0;
1800 memset(r, 0, sizeof(*r));
1804 if (who == RUSAGE_THREAD) {
1805 task_cputime_adjusted(current, &utime, &stime);
1806 accumulate_thread_rusage(p, r);
1807 maxrss = sig->maxrss;
1811 flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
1815 case RUSAGE_CHILDREN:
1816 utime = sig->cutime;
1817 stime = sig->cstime;
1818 r->ru_nvcsw = sig->cnvcsw;
1819 r->ru_nivcsw = sig->cnivcsw;
1820 r->ru_minflt = sig->cmin_flt;
1821 r->ru_majflt = sig->cmaj_flt;
1822 r->ru_inblock = sig->cinblock;
1823 r->ru_oublock = sig->coublock;
1824 maxrss = sig->cmaxrss;
1826 if (who == RUSAGE_CHILDREN)
1831 r->ru_nvcsw += sig->nvcsw;
1832 r->ru_nivcsw += sig->nivcsw;
1833 r->ru_minflt += sig->min_flt;
1834 r->ru_majflt += sig->maj_flt;
1835 r->ru_inblock += sig->inblock;
1836 r->ru_oublock += sig->oublock;
1837 if (maxrss < sig->maxrss)
1838 maxrss = sig->maxrss;
1841 __for_each_thread(sig, t)
1842 accumulate_thread_rusage(t, r);
1851 if (need_seqretry(&sig->stats_lock, seq)) {
1855 done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
1857 if (who == RUSAGE_CHILDREN)
1860 thread_group_cputime_adjusted(p, &tgutime, &tgstime);
1865 mm = get_task_mm(p);
1867 setmax_mm_hiwater_rss(&maxrss, mm);
1872 r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
1873 r->ru_utime = ns_to_kernel_old_timeval(utime);
1874 r->ru_stime = ns_to_kernel_old_timeval(stime);
1877 SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
1881 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1882 who != RUSAGE_THREAD)
1885 getrusage(current, who, &r);
1886 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1889 #ifdef CONFIG_COMPAT
1890 COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru)
1894 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1895 who != RUSAGE_THREAD)
1898 getrusage(current, who, &r);
1899 return put_compat_rusage(&r, ru);
1903 SYSCALL_DEFINE1(umask, int, mask)
1905 mask = xchg(¤t->fs->umask, mask & S_IRWXUGO);
1909 static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
1912 struct inode *inode;
1919 inode = file_inode(exe.file);
1922 * Because the original mm->exe_file points to executable file, make
1923 * sure that this one is executable as well, to avoid breaking an
1927 if (!S_ISREG(inode->i_mode) || path_noexec(&exe.file->f_path))
1930 err = file_permission(exe.file, MAY_EXEC);
1934 err = replace_mm_exe_file(mm, exe.file);
1941 * Check arithmetic relations of passed addresses.
1943 * WARNING: we don't require any capability here so be very careful
1944 * in what is allowed for modification from userspace.
1946 static int validate_prctl_map_addr(struct prctl_mm_map *prctl_map)
1948 unsigned long mmap_max_addr = TASK_SIZE;
1949 int error = -EINVAL, i;
1951 static const unsigned char offsets[] = {
1952 offsetof(struct prctl_mm_map, start_code),
1953 offsetof(struct prctl_mm_map, end_code),
1954 offsetof(struct prctl_mm_map, start_data),
1955 offsetof(struct prctl_mm_map, end_data),
1956 offsetof(struct prctl_mm_map, start_brk),
1957 offsetof(struct prctl_mm_map, brk),
1958 offsetof(struct prctl_mm_map, start_stack),
1959 offsetof(struct prctl_mm_map, arg_start),
1960 offsetof(struct prctl_mm_map, arg_end),
1961 offsetof(struct prctl_mm_map, env_start),
1962 offsetof(struct prctl_mm_map, env_end),
1966 * Make sure the members are not somewhere outside
1967 * of allowed address space.
1969 for (i = 0; i < ARRAY_SIZE(offsets); i++) {
1970 u64 val = *(u64 *)((char *)prctl_map + offsets[i]);
1972 if ((unsigned long)val >= mmap_max_addr ||
1973 (unsigned long)val < mmap_min_addr)
1978 * Make sure the pairs are ordered.
1980 #define __prctl_check_order(__m1, __op, __m2) \
1981 ((unsigned long)prctl_map->__m1 __op \
1982 (unsigned long)prctl_map->__m2) ? 0 : -EINVAL
1983 error = __prctl_check_order(start_code, <, end_code);
1984 error |= __prctl_check_order(start_data,<=, end_data);
1985 error |= __prctl_check_order(start_brk, <=, brk);
1986 error |= __prctl_check_order(arg_start, <=, arg_end);
1987 error |= __prctl_check_order(env_start, <=, env_end);
1990 #undef __prctl_check_order
1995 * Neither we should allow to override limits if they set.
1997 if (check_data_rlimit(rlimit(RLIMIT_DATA), prctl_map->brk,
1998 prctl_map->start_brk, prctl_map->end_data,
1999 prctl_map->start_data))
2007 #ifdef CONFIG_CHECKPOINT_RESTORE
2008 static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data_size)
2010 struct prctl_mm_map prctl_map = { .exe_fd = (u32)-1, };
2011 unsigned long user_auxv[AT_VECTOR_SIZE];
2012 struct mm_struct *mm = current->mm;
2015 BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
2016 BUILD_BUG_ON(sizeof(struct prctl_mm_map) > 256);
2018 if (opt == PR_SET_MM_MAP_SIZE)
2019 return put_user((unsigned int)sizeof(prctl_map),
2020 (unsigned int __user *)addr);
2022 if (data_size != sizeof(prctl_map))
2025 if (copy_from_user(&prctl_map, addr, sizeof(prctl_map)))
2028 error = validate_prctl_map_addr(&prctl_map);
2032 if (prctl_map.auxv_size) {
2034 * Someone is trying to cheat the auxv vector.
2036 if (!prctl_map.auxv ||
2037 prctl_map.auxv_size > sizeof(mm->saved_auxv))
2040 memset(user_auxv, 0, sizeof(user_auxv));
2041 if (copy_from_user(user_auxv,
2042 (const void __user *)prctl_map.auxv,
2043 prctl_map.auxv_size))
2046 /* Last entry must be AT_NULL as specification requires */
2047 user_auxv[AT_VECTOR_SIZE - 2] = AT_NULL;
2048 user_auxv[AT_VECTOR_SIZE - 1] = AT_NULL;
2051 if (prctl_map.exe_fd != (u32)-1) {
2053 * Check if the current user is checkpoint/restore capable.
2054 * At the time of this writing, it checks for CAP_SYS_ADMIN
2055 * or CAP_CHECKPOINT_RESTORE.
2056 * Note that a user with access to ptrace can masquerade an
2057 * arbitrary program as any executable, even setuid ones.
2058 * This may have implications in the tomoyo subsystem.
2060 if (!checkpoint_restore_ns_capable(current_user_ns()))
2063 error = prctl_set_mm_exe_file(mm, prctl_map.exe_fd);
2069 * arg_lock protects concurrent updates but we still need mmap_lock for
2070 * read to exclude races with sys_brk.
2075 * We don't validate if these members are pointing to
2076 * real present VMAs because application may have correspond
2077 * VMAs already unmapped and kernel uses these members for statistics
2078 * output in procfs mostly, except
2080 * - @start_brk/@brk which are used in do_brk_flags but kernel lookups
2081 * for VMAs when updating these members so anything wrong written
2082 * here cause kernel to swear at userspace program but won't lead
2083 * to any problem in kernel itself
2086 spin_lock(&mm->arg_lock);
2087 mm->start_code = prctl_map.start_code;
2088 mm->end_code = prctl_map.end_code;
2089 mm->start_data = prctl_map.start_data;
2090 mm->end_data = prctl_map.end_data;
2091 mm->start_brk = prctl_map.start_brk;
2092 mm->brk = prctl_map.brk;
2093 mm->start_stack = prctl_map.start_stack;
2094 mm->arg_start = prctl_map.arg_start;
2095 mm->arg_end = prctl_map.arg_end;
2096 mm->env_start = prctl_map.env_start;
2097 mm->env_end = prctl_map.env_end;
2098 spin_unlock(&mm->arg_lock);
2101 * Note this update of @saved_auxv is lockless thus
2102 * if someone reads this member in procfs while we're
2103 * updating -- it may get partly updated results. It's
2104 * known and acceptable trade off: we leave it as is to
2105 * not introduce additional locks here making the kernel
2108 if (prctl_map.auxv_size)
2109 memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
2111 mmap_read_unlock(mm);
2114 #endif /* CONFIG_CHECKPOINT_RESTORE */
2116 static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr,
2120 * This doesn't move the auxiliary vector itself since it's pinned to
2121 * mm_struct, but it permits filling the vector with new values. It's
2122 * up to the caller to provide sane values here, otherwise userspace
2123 * tools which use this vector might be unhappy.
2125 unsigned long user_auxv[AT_VECTOR_SIZE] = {};
2127 if (len > sizeof(user_auxv))
2130 if (copy_from_user(user_auxv, (const void __user *)addr, len))
2133 /* Make sure the last entry is always AT_NULL */
2134 user_auxv[AT_VECTOR_SIZE - 2] = 0;
2135 user_auxv[AT_VECTOR_SIZE - 1] = 0;
2137 BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
2140 memcpy(mm->saved_auxv, user_auxv, len);
2141 task_unlock(current);
2146 static int prctl_set_mm(int opt, unsigned long addr,
2147 unsigned long arg4, unsigned long arg5)
2149 struct mm_struct *mm = current->mm;
2150 struct prctl_mm_map prctl_map = {
2155 struct vm_area_struct *vma;
2158 if (arg5 || (arg4 && (opt != PR_SET_MM_AUXV &&
2159 opt != PR_SET_MM_MAP &&
2160 opt != PR_SET_MM_MAP_SIZE)))
2163 #ifdef CONFIG_CHECKPOINT_RESTORE
2164 if (opt == PR_SET_MM_MAP || opt == PR_SET_MM_MAP_SIZE)
2165 return prctl_set_mm_map(opt, (const void __user *)addr, arg4);
2168 if (!capable(CAP_SYS_RESOURCE))
2171 if (opt == PR_SET_MM_EXE_FILE)
2172 return prctl_set_mm_exe_file(mm, (unsigned int)addr);
2174 if (opt == PR_SET_MM_AUXV)
2175 return prctl_set_auxv(mm, addr, arg4);
2177 if (addr >= TASK_SIZE || addr < mmap_min_addr)
2183 * arg_lock protects concurrent updates of arg boundaries, we need
2184 * mmap_lock for a) concurrent sys_brk, b) finding VMA for addr
2188 vma = find_vma(mm, addr);
2190 spin_lock(&mm->arg_lock);
2191 prctl_map.start_code = mm->start_code;
2192 prctl_map.end_code = mm->end_code;
2193 prctl_map.start_data = mm->start_data;
2194 prctl_map.end_data = mm->end_data;
2195 prctl_map.start_brk = mm->start_brk;
2196 prctl_map.brk = mm->brk;
2197 prctl_map.start_stack = mm->start_stack;
2198 prctl_map.arg_start = mm->arg_start;
2199 prctl_map.arg_end = mm->arg_end;
2200 prctl_map.env_start = mm->env_start;
2201 prctl_map.env_end = mm->env_end;
2204 case PR_SET_MM_START_CODE:
2205 prctl_map.start_code = addr;
2207 case PR_SET_MM_END_CODE:
2208 prctl_map.end_code = addr;
2210 case PR_SET_MM_START_DATA:
2211 prctl_map.start_data = addr;
2213 case PR_SET_MM_END_DATA:
2214 prctl_map.end_data = addr;
2216 case PR_SET_MM_START_STACK:
2217 prctl_map.start_stack = addr;
2219 case PR_SET_MM_START_BRK:
2220 prctl_map.start_brk = addr;
2223 prctl_map.brk = addr;
2225 case PR_SET_MM_ARG_START:
2226 prctl_map.arg_start = addr;
2228 case PR_SET_MM_ARG_END:
2229 prctl_map.arg_end = addr;
2231 case PR_SET_MM_ENV_START:
2232 prctl_map.env_start = addr;
2234 case PR_SET_MM_ENV_END:
2235 prctl_map.env_end = addr;
2241 error = validate_prctl_map_addr(&prctl_map);
2247 * If command line arguments and environment
2248 * are placed somewhere else on stack, we can
2249 * set them up here, ARG_START/END to setup
2250 * command line arguments and ENV_START/END
2253 case PR_SET_MM_START_STACK:
2254 case PR_SET_MM_ARG_START:
2255 case PR_SET_MM_ARG_END:
2256 case PR_SET_MM_ENV_START:
2257 case PR_SET_MM_ENV_END:
2264 mm->start_code = prctl_map.start_code;
2265 mm->end_code = prctl_map.end_code;
2266 mm->start_data = prctl_map.start_data;
2267 mm->end_data = prctl_map.end_data;
2268 mm->start_brk = prctl_map.start_brk;
2269 mm->brk = prctl_map.brk;
2270 mm->start_stack = prctl_map.start_stack;
2271 mm->arg_start = prctl_map.arg_start;
2272 mm->arg_end = prctl_map.arg_end;
2273 mm->env_start = prctl_map.env_start;
2274 mm->env_end = prctl_map.env_end;
2278 spin_unlock(&mm->arg_lock);
2279 mmap_read_unlock(mm);
2283 #ifdef CONFIG_CHECKPOINT_RESTORE
2284 static int prctl_get_tid_address(struct task_struct *me, int __user * __user *tid_addr)
2286 return put_user(me->clear_child_tid, tid_addr);
2289 static int prctl_get_tid_address(struct task_struct *me, int __user * __user *tid_addr)
2295 static int propagate_has_child_subreaper(struct task_struct *p, void *data)
2298 * If task has has_child_subreaper - all its descendants
2299 * already have these flag too and new descendants will
2300 * inherit it on fork, skip them.
2302 * If we've found child_reaper - skip descendants in
2303 * it's subtree as they will never get out pidns.
2305 if (p->signal->has_child_subreaper ||
2306 is_child_reaper(task_pid(p)))
2309 p->signal->has_child_subreaper = 1;
2313 int __weak arch_prctl_spec_ctrl_get(struct task_struct *t, unsigned long which)
2318 int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which,
2324 #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE)
2326 #ifdef CONFIG_ANON_VMA_NAME
2328 #define ANON_VMA_NAME_MAX_LEN 80
2329 #define ANON_VMA_NAME_INVALID_CHARS "\\`$[]"
2331 static inline bool is_valid_name_char(char ch)
2333 /* printable ascii characters, excluding ANON_VMA_NAME_INVALID_CHARS */
2334 return ch > 0x1f && ch < 0x7f &&
2335 !strchr(ANON_VMA_NAME_INVALID_CHARS, ch);
2338 static int prctl_set_vma(unsigned long opt, unsigned long addr,
2339 unsigned long size, unsigned long arg)
2341 struct mm_struct *mm = current->mm;
2342 const char __user *uname;
2343 struct anon_vma_name *anon_name = NULL;
2347 case PR_SET_VMA_ANON_NAME:
2348 uname = (const char __user *)arg;
2352 name = strndup_user(uname, ANON_VMA_NAME_MAX_LEN);
2354 return PTR_ERR(name);
2356 for (pch = name; *pch != '\0'; pch++) {
2357 if (!is_valid_name_char(*pch)) {
2362 /* anon_vma has its own copy */
2363 anon_name = anon_vma_name_alloc(name);
2370 mmap_write_lock(mm);
2371 error = madvise_set_anon_name(mm, addr, size, anon_name);
2372 mmap_write_unlock(mm);
2373 anon_vma_name_put(anon_name);
2382 #else /* CONFIG_ANON_VMA_NAME */
2383 static int prctl_set_vma(unsigned long opt, unsigned long start,
2384 unsigned long size, unsigned long arg)
2388 #endif /* CONFIG_ANON_VMA_NAME */
2390 static inline unsigned long get_current_mdwe(void)
2392 unsigned long ret = 0;
2394 if (test_bit(MMF_HAS_MDWE, ¤t->mm->flags))
2395 ret |= PR_MDWE_REFUSE_EXEC_GAIN;
2396 if (test_bit(MMF_HAS_MDWE_NO_INHERIT, ¤t->mm->flags))
2397 ret |= PR_MDWE_NO_INHERIT;
2402 static inline int prctl_set_mdwe(unsigned long bits, unsigned long arg3,
2403 unsigned long arg4, unsigned long arg5)
2405 unsigned long current_bits;
2407 if (arg3 || arg4 || arg5)
2410 if (bits & ~(PR_MDWE_REFUSE_EXEC_GAIN | PR_MDWE_NO_INHERIT))
2413 /* NO_INHERIT only makes sense with REFUSE_EXEC_GAIN */
2414 if (bits & PR_MDWE_NO_INHERIT && !(bits & PR_MDWE_REFUSE_EXEC_GAIN))
2418 * EOPNOTSUPP might be more appropriate here in principle, but
2419 * existing userspace depends on EINVAL specifically.
2421 if (!arch_memory_deny_write_exec_supported())
2424 current_bits = get_current_mdwe();
2425 if (current_bits && current_bits != bits)
2426 return -EPERM; /* Cannot unset the flags */
2428 if (bits & PR_MDWE_NO_INHERIT)
2429 set_bit(MMF_HAS_MDWE_NO_INHERIT, ¤t->mm->flags);
2430 if (bits & PR_MDWE_REFUSE_EXEC_GAIN)
2431 set_bit(MMF_HAS_MDWE, ¤t->mm->flags);
2436 static inline int prctl_get_mdwe(unsigned long arg2, unsigned long arg3,
2437 unsigned long arg4, unsigned long arg5)
2439 if (arg2 || arg3 || arg4 || arg5)
2441 return get_current_mdwe();
2444 static int prctl_get_auxv(void __user *addr, unsigned long len)
2446 struct mm_struct *mm = current->mm;
2447 unsigned long size = min_t(unsigned long, sizeof(mm->saved_auxv), len);
2449 if (size && copy_to_user(addr, mm->saved_auxv, size))
2451 return sizeof(mm->saved_auxv);
2454 SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
2455 unsigned long, arg4, unsigned long, arg5)
2457 struct task_struct *me = current;
2458 unsigned char comm[sizeof(me->comm)];
2461 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2462 if (error != -ENOSYS)
2467 case PR_SET_PDEATHSIG:
2468 if (!valid_signal(arg2)) {
2472 me->pdeath_signal = arg2;
2474 case PR_GET_PDEATHSIG:
2475 error = put_user(me->pdeath_signal, (int __user *)arg2);
2477 case PR_GET_DUMPABLE:
2478 error = get_dumpable(me->mm);
2480 case PR_SET_DUMPABLE:
2481 if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) {
2485 set_dumpable(me->mm, arg2);
2488 case PR_SET_UNALIGN:
2489 error = SET_UNALIGN_CTL(me, arg2);
2491 case PR_GET_UNALIGN:
2492 error = GET_UNALIGN_CTL(me, arg2);
2495 error = SET_FPEMU_CTL(me, arg2);
2498 error = GET_FPEMU_CTL(me, arg2);
2501 error = SET_FPEXC_CTL(me, arg2);
2504 error = GET_FPEXC_CTL(me, arg2);
2507 error = PR_TIMING_STATISTICAL;
2510 if (arg2 != PR_TIMING_STATISTICAL)
2514 comm[sizeof(me->comm) - 1] = 0;
2515 if (strncpy_from_user(comm, (char __user *)arg2,
2516 sizeof(me->comm) - 1) < 0)
2518 set_task_comm(me, comm);
2519 proc_comm_connector(me);
2522 get_task_comm(comm, me);
2523 if (copy_to_user((char __user *)arg2, comm, sizeof(comm)))
2527 error = GET_ENDIAN(me, arg2);
2530 error = SET_ENDIAN(me, arg2);
2532 case PR_GET_SECCOMP:
2533 error = prctl_get_seccomp();
2535 case PR_SET_SECCOMP:
2536 error = prctl_set_seccomp(arg2, (char __user *)arg3);
2539 error = GET_TSC_CTL(arg2);
2542 error = SET_TSC_CTL(arg2);
2544 case PR_TASK_PERF_EVENTS_DISABLE:
2545 error = perf_event_task_disable();
2547 case PR_TASK_PERF_EVENTS_ENABLE:
2548 error = perf_event_task_enable();
2550 case PR_GET_TIMERSLACK:
2551 if (current->timer_slack_ns > ULONG_MAX)
2554 error = current->timer_slack_ns;
2556 case PR_SET_TIMERSLACK:
2558 current->timer_slack_ns =
2559 current->default_timer_slack_ns;
2561 current->timer_slack_ns = arg2;
2567 case PR_MCE_KILL_CLEAR:
2570 current->flags &= ~PF_MCE_PROCESS;
2572 case PR_MCE_KILL_SET:
2573 current->flags |= PF_MCE_PROCESS;
2574 if (arg3 == PR_MCE_KILL_EARLY)
2575 current->flags |= PF_MCE_EARLY;
2576 else if (arg3 == PR_MCE_KILL_LATE)
2577 current->flags &= ~PF_MCE_EARLY;
2578 else if (arg3 == PR_MCE_KILL_DEFAULT)
2580 ~(PF_MCE_EARLY|PF_MCE_PROCESS);
2588 case PR_MCE_KILL_GET:
2589 if (arg2 | arg3 | arg4 | arg5)
2591 if (current->flags & PF_MCE_PROCESS)
2592 error = (current->flags & PF_MCE_EARLY) ?
2593 PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
2595 error = PR_MCE_KILL_DEFAULT;
2598 error = prctl_set_mm(arg2, arg3, arg4, arg5);
2600 case PR_GET_TID_ADDRESS:
2601 error = prctl_get_tid_address(me, (int __user * __user *)arg2);
2603 case PR_SET_CHILD_SUBREAPER:
2604 me->signal->is_child_subreaper = !!arg2;
2608 walk_process_tree(me, propagate_has_child_subreaper, NULL);
2610 case PR_GET_CHILD_SUBREAPER:
2611 error = put_user(me->signal->is_child_subreaper,
2612 (int __user *)arg2);
2614 case PR_SET_NO_NEW_PRIVS:
2615 if (arg2 != 1 || arg3 || arg4 || arg5)
2618 task_set_no_new_privs(current);
2620 case PR_GET_NO_NEW_PRIVS:
2621 if (arg2 || arg3 || arg4 || arg5)
2623 return task_no_new_privs(current) ? 1 : 0;
2624 case PR_GET_THP_DISABLE:
2625 if (arg2 || arg3 || arg4 || arg5)
2627 error = !!test_bit(MMF_DISABLE_THP, &me->mm->flags);
2629 case PR_SET_THP_DISABLE:
2630 if (arg3 || arg4 || arg5)
2632 if (mmap_write_lock_killable(me->mm))
2635 set_bit(MMF_DISABLE_THP, &me->mm->flags);
2637 clear_bit(MMF_DISABLE_THP, &me->mm->flags);
2638 mmap_write_unlock(me->mm);
2640 case PR_MPX_ENABLE_MANAGEMENT:
2641 case PR_MPX_DISABLE_MANAGEMENT:
2642 /* No longer implemented: */
2644 case PR_SET_FP_MODE:
2645 error = SET_FP_MODE(me, arg2);
2647 case PR_GET_FP_MODE:
2648 error = GET_FP_MODE(me);
2651 error = SVE_SET_VL(arg2);
2654 error = SVE_GET_VL();
2657 error = SME_SET_VL(arg2);
2660 error = SME_GET_VL();
2662 case PR_GET_SPECULATION_CTRL:
2663 if (arg3 || arg4 || arg5)
2665 error = arch_prctl_spec_ctrl_get(me, arg2);
2667 case PR_SET_SPECULATION_CTRL:
2670 error = arch_prctl_spec_ctrl_set(me, arg2, arg3);
2672 case PR_PAC_RESET_KEYS:
2673 if (arg3 || arg4 || arg5)
2675 error = PAC_RESET_KEYS(me, arg2);
2677 case PR_PAC_SET_ENABLED_KEYS:
2680 error = PAC_SET_ENABLED_KEYS(me, arg2, arg3);
2682 case PR_PAC_GET_ENABLED_KEYS:
2683 if (arg2 || arg3 || arg4 || arg5)
2685 error = PAC_GET_ENABLED_KEYS(me);
2687 case PR_SET_TAGGED_ADDR_CTRL:
2688 if (arg3 || arg4 || arg5)
2690 error = SET_TAGGED_ADDR_CTRL(arg2);
2692 case PR_GET_TAGGED_ADDR_CTRL:
2693 if (arg2 || arg3 || arg4 || arg5)
2695 error = GET_TAGGED_ADDR_CTRL();
2697 case PR_SET_IO_FLUSHER:
2698 if (!capable(CAP_SYS_RESOURCE))
2701 if (arg3 || arg4 || arg5)
2705 current->flags |= PR_IO_FLUSHER;
2707 current->flags &= ~PR_IO_FLUSHER;
2711 case PR_GET_IO_FLUSHER:
2712 if (!capable(CAP_SYS_RESOURCE))
2715 if (arg2 || arg3 || arg4 || arg5)
2718 error = (current->flags & PR_IO_FLUSHER) == PR_IO_FLUSHER;
2720 case PR_SET_SYSCALL_USER_DISPATCH:
2721 error = set_syscall_user_dispatch(arg2, arg3, arg4,
2722 (char __user *) arg5);
2724 #ifdef CONFIG_SCHED_CORE
2726 error = sched_core_share_pid(arg2, arg3, arg4, arg5);
2730 error = prctl_set_mdwe(arg2, arg3, arg4, arg5);
2733 error = prctl_get_mdwe(arg2, arg3, arg4, arg5);
2735 case PR_PPC_GET_DEXCR:
2736 if (arg3 || arg4 || arg5)
2738 error = PPC_GET_DEXCR_ASPECT(me, arg2);
2740 case PR_PPC_SET_DEXCR:
2743 error = PPC_SET_DEXCR_ASPECT(me, arg2, arg3);
2746 error = prctl_set_vma(arg2, arg3, arg4, arg5);
2751 error = prctl_get_auxv((void __user *)arg2, arg3);
2754 case PR_SET_MEMORY_MERGE:
2755 if (arg3 || arg4 || arg5)
2757 if (mmap_write_lock_killable(me->mm))
2761 error = ksm_enable_merge_any(me->mm);
2763 error = ksm_disable_merge_any(me->mm);
2764 mmap_write_unlock(me->mm);
2766 case PR_GET_MEMORY_MERGE:
2767 if (arg2 || arg3 || arg4 || arg5)
2770 error = !!test_bit(MMF_VM_MERGE_ANY, &me->mm->flags);
2773 case PR_RISCV_V_SET_CONTROL:
2774 error = RISCV_V_SET_CONTROL(arg2);
2776 case PR_RISCV_V_GET_CONTROL:
2777 error = RISCV_V_GET_CONTROL();
2786 SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
2787 struct getcpu_cache __user *, unused)
2790 int cpu = raw_smp_processor_id();
2793 err |= put_user(cpu, cpup);
2795 err |= put_user(cpu_to_node(cpu), nodep);
2796 return err ? -EFAULT : 0;
2800 * do_sysinfo - fill in sysinfo struct
2801 * @info: pointer to buffer to fill
2803 static int do_sysinfo(struct sysinfo *info)
2805 unsigned long mem_total, sav_total;
2806 unsigned int mem_unit, bitcount;
2807 struct timespec64 tp;
2809 memset(info, 0, sizeof(struct sysinfo));
2811 ktime_get_boottime_ts64(&tp);
2812 timens_add_boottime(&tp);
2813 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
2815 get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
2817 info->procs = nr_threads;
2823 * If the sum of all the available memory (i.e. ram + swap)
2824 * is less than can be stored in a 32 bit unsigned long then
2825 * we can be binary compatible with 2.2.x kernels. If not,
2826 * well, in that case 2.2.x was broken anyways...
2828 * -Erik Andersen <andersee@debian.org>
2831 mem_total = info->totalram + info->totalswap;
2832 if (mem_total < info->totalram || mem_total < info->totalswap)
2835 mem_unit = info->mem_unit;
2836 while (mem_unit > 1) {
2839 sav_total = mem_total;
2841 if (mem_total < sav_total)
2846 * If mem_total did not overflow, multiply all memory values by
2847 * info->mem_unit and set it to 1. This leaves things compatible
2848 * with 2.2.x, and also retains compatibility with earlier 2.4.x
2853 info->totalram <<= bitcount;
2854 info->freeram <<= bitcount;
2855 info->sharedram <<= bitcount;
2856 info->bufferram <<= bitcount;
2857 info->totalswap <<= bitcount;
2858 info->freeswap <<= bitcount;
2859 info->totalhigh <<= bitcount;
2860 info->freehigh <<= bitcount;
2866 SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
2872 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
2878 #ifdef CONFIG_COMPAT
2879 struct compat_sysinfo {
2893 char _f[20-2*sizeof(u32)-sizeof(int)];
2896 COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
2899 struct compat_sysinfo s_32;
2903 /* Check to see if any memory value is too large for 32-bit and scale
2906 if (upper_32_bits(s.totalram) || upper_32_bits(s.totalswap)) {
2909 while (s.mem_unit < PAGE_SIZE) {
2914 s.totalram >>= bitcount;
2915 s.freeram >>= bitcount;
2916 s.sharedram >>= bitcount;
2917 s.bufferram >>= bitcount;
2918 s.totalswap >>= bitcount;
2919 s.freeswap >>= bitcount;
2920 s.totalhigh >>= bitcount;
2921 s.freehigh >>= bitcount;
2924 memset(&s_32, 0, sizeof(s_32));
2925 s_32.uptime = s.uptime;
2926 s_32.loads[0] = s.loads[0];
2927 s_32.loads[1] = s.loads[1];
2928 s_32.loads[2] = s.loads[2];
2929 s_32.totalram = s.totalram;
2930 s_32.freeram = s.freeram;
2931 s_32.sharedram = s.sharedram;
2932 s_32.bufferram = s.bufferram;
2933 s_32.totalswap = s.totalswap;
2934 s_32.freeswap = s.freeswap;
2935 s_32.procs = s.procs;
2936 s_32.totalhigh = s.totalhigh;
2937 s_32.freehigh = s.freehigh;
2938 s_32.mem_unit = s.mem_unit;
2939 if (copy_to_user(info, &s_32, sizeof(s_32)))
2943 #endif /* CONFIG_COMPAT */