1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/kernel/sys.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds
61da177e4SLinus Torvalds */
71da177e4SLinus Torvalds
89984de1aSPaul Gortmaker #include <linux/export.h>
91da177e4SLinus Torvalds #include <linux/mm.h>
105c26f6acSSuren Baghdasaryan #include <linux/mm_inline.h>
111da177e4SLinus Torvalds #include <linux/utsname.h>
121da177e4SLinus Torvalds #include <linux/mman.h>
131da177e4SLinus Torvalds #include <linux/reboot.h>
141da177e4SLinus Torvalds #include <linux/prctl.h>
151da177e4SLinus Torvalds #include <linux/highuid.h>
161da177e4SLinus Torvalds #include <linux/fs.h>
1774da1ff7SPaul Gortmaker #include <linux/kmod.h>
18d7597f59SStefan Roesch #include <linux/ksm.h>
19cdd6c482SIngo Molnar #include <linux/perf_event.h>
203e88c553SDaniel Walker #include <linux/resource.h>
21dc009d92SEric W. Biederman #include <linux/kernel.h>
221da177e4SLinus Torvalds #include <linux/workqueue.h>
23c59ede7bSRandy.Dunlap #include <linux/capability.h>
241da177e4SLinus Torvalds #include <linux/device.h>
251da177e4SLinus Torvalds #include <linux/key.h>
261da177e4SLinus Torvalds #include <linux/times.h>
271da177e4SLinus Torvalds #include <linux/posix-timers.h>
281da177e4SLinus Torvalds #include <linux/security.h>
2937608ba3SJason A. Donenfeld #include <linux/random.h>
301da177e4SLinus Torvalds #include <linux/suspend.h>
311da177e4SLinus Torvalds #include <linux/tty.h>
327ed20e1aSJesper Juhl #include <linux/signal.h>
339f46080cSMatt Helsley #include <linux/cn_proc.h>
343cfc348bSAndi Kleen #include <linux/getcpu.h>
356eaeeabaSEric Dumazet #include <linux/task_io_accounting_ops.h>
361d9d02feSAndrea Arcangeli #include <linux/seccomp.h>
374047727eSMark Lord #include <linux/cpu.h>
38e28cbf22SChristoph Hellwig #include <linux/personality.h>
39e3d5a27dSPaul Mackerras #include <linux/ptrace.h>
405ad4e53bSAl Viro #include <linux/fs_struct.h>
41b32dfe37SCyrill Gorcunov #include <linux/file.h>
42b32dfe37SCyrill Gorcunov #include <linux/mount.h>
435a0e3ad6STejun Heo #include <linux/gfp.h>
4440dc166cSRafael J. Wysocki #include <linux/syscore_ops.h>
45be27425dSAndi Kleen #include <linux/version.h>
46be27425dSAndi Kleen #include <linux/ctype.h>
471446e1dfSGabriel Krisman Bertazi #include <linux/syscall_user_dispatch.h>
481da177e4SLinus Torvalds
491da177e4SLinus Torvalds #include <linux/compat.h>
501da177e4SLinus Torvalds #include <linux/syscalls.h>
5100d7c05aSKeshavamurthy Anil S #include <linux/kprobes.h>
52acce292cSCedric Le Goater #include <linux/user_namespace.h>
53ecc421e0SCyril Hrubis #include <linux/time_namespace.h>
547fe5e042SChen Gang #include <linux/binfmts.h>
551da177e4SLinus Torvalds
564a22f166SStephen Rothwell #include <linux/sched.h>
574eb5aaa3SIngo Molnar #include <linux/sched/autogroup.h>
584f17722cSIngo Molnar #include <linux/sched/loadavg.h>
5903441a34SIngo Molnar #include <linux/sched/stat.h>
606e84f315SIngo Molnar #include <linux/sched/mm.h>
61f7ccbae4SIngo Molnar #include <linux/sched/coredump.h>
6229930025SIngo Molnar #include <linux/sched/task.h>
6332ef5517SIngo Molnar #include <linux/sched/cputime.h>
644a22f166SStephen Rothwell #include <linux/rcupdate.h>
654a22f166SStephen Rothwell #include <linux/uidgid.h>
664a22f166SStephen Rothwell #include <linux/cred.h>
674a22f166SStephen Rothwell
68b617cfc8SThomas Gleixner #include <linux/nospec.h>
69b617cfc8SThomas Gleixner
7004c6862cSSeiji Aguchi #include <linux/kmsg_dump.h>
71be27425dSAndi Kleen /* Move somewhere else to avoid recompiling? */
72be27425dSAndi Kleen #include <generated/utsrelease.h>
7304c6862cSSeiji Aguchi
747c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
751da177e4SLinus Torvalds #include <asm/io.h>
761da177e4SLinus Torvalds #include <asm/unistd.h>
771da177e4SLinus Torvalds
78e530dca5SDominik Brodowski #include "uid16.h"
79e530dca5SDominik Brodowski
801da177e4SLinus Torvalds #ifndef SET_UNALIGN_CTL
811da177e4SLinus Torvalds # define SET_UNALIGN_CTL(a, b) (-EINVAL)
821da177e4SLinus Torvalds #endif
831da177e4SLinus Torvalds #ifndef GET_UNALIGN_CTL
841da177e4SLinus Torvalds # define GET_UNALIGN_CTL(a, b) (-EINVAL)
851da177e4SLinus Torvalds #endif
861da177e4SLinus Torvalds #ifndef SET_FPEMU_CTL
871da177e4SLinus Torvalds # define SET_FPEMU_CTL(a, b) (-EINVAL)
881da177e4SLinus Torvalds #endif
891da177e4SLinus Torvalds #ifndef GET_FPEMU_CTL
901da177e4SLinus Torvalds # define GET_FPEMU_CTL(a, b) (-EINVAL)
911da177e4SLinus Torvalds #endif
921da177e4SLinus Torvalds #ifndef SET_FPEXC_CTL
931da177e4SLinus Torvalds # define SET_FPEXC_CTL(a, b) (-EINVAL)
941da177e4SLinus Torvalds #endif
951da177e4SLinus Torvalds #ifndef GET_FPEXC_CTL
961da177e4SLinus Torvalds # define GET_FPEXC_CTL(a, b) (-EINVAL)
971da177e4SLinus Torvalds #endif
98651d765dSAnton Blanchard #ifndef GET_ENDIAN
99651d765dSAnton Blanchard # define GET_ENDIAN(a, b) (-EINVAL)
100651d765dSAnton Blanchard #endif
101651d765dSAnton Blanchard #ifndef SET_ENDIAN
102651d765dSAnton Blanchard # define SET_ENDIAN(a, b) (-EINVAL)
103651d765dSAnton Blanchard #endif
1048fb402bcSErik Bosman #ifndef GET_TSC_CTL
1058fb402bcSErik Bosman # define GET_TSC_CTL(a) (-EINVAL)
1068fb402bcSErik Bosman #endif
1078fb402bcSErik Bosman #ifndef SET_TSC_CTL
1088fb402bcSErik Bosman # define SET_TSC_CTL(a) (-EINVAL)
1098fb402bcSErik Bosman #endif
1109791554bSPaul Burton #ifndef GET_FP_MODE
1119791554bSPaul Burton # define GET_FP_MODE(a) (-EINVAL)
1129791554bSPaul Burton #endif
1139791554bSPaul Burton #ifndef SET_FP_MODE
1149791554bSPaul Burton # define SET_FP_MODE(a,b) (-EINVAL)
1159791554bSPaul Burton #endif
1162d2123bcSDave Martin #ifndef SVE_SET_VL
1172d2123bcSDave Martin # define SVE_SET_VL(a) (-EINVAL)
1182d2123bcSDave Martin #endif
1192d2123bcSDave Martin #ifndef SVE_GET_VL
1202d2123bcSDave Martin # define SVE_GET_VL() (-EINVAL)
1212d2123bcSDave Martin #endif
1229e4ab6c8SMark Brown #ifndef SME_SET_VL
1239e4ab6c8SMark Brown # define SME_SET_VL(a) (-EINVAL)
1249e4ab6c8SMark Brown #endif
1259e4ab6c8SMark Brown #ifndef SME_GET_VL
1269e4ab6c8SMark Brown # define SME_GET_VL() (-EINVAL)
1279e4ab6c8SMark Brown #endif
128ba830885SKristina Martsenko #ifndef PAC_RESET_KEYS
129ba830885SKristina Martsenko # define PAC_RESET_KEYS(a, b) (-EINVAL)
130ba830885SKristina Martsenko #endif
13120169862SPeter Collingbourne #ifndef PAC_SET_ENABLED_KEYS
13220169862SPeter Collingbourne # define PAC_SET_ENABLED_KEYS(a, b, c) (-EINVAL)
13320169862SPeter Collingbourne #endif
13420169862SPeter Collingbourne #ifndef PAC_GET_ENABLED_KEYS
13520169862SPeter Collingbourne # define PAC_GET_ENABLED_KEYS(a) (-EINVAL)
13620169862SPeter Collingbourne #endif
13763f0c603SCatalin Marinas #ifndef SET_TAGGED_ADDR_CTRL
13863f0c603SCatalin Marinas # define SET_TAGGED_ADDR_CTRL(a) (-EINVAL)
13963f0c603SCatalin Marinas #endif
14063f0c603SCatalin Marinas #ifndef GET_TAGGED_ADDR_CTRL
14163f0c603SCatalin Marinas # define GET_TAGGED_ADDR_CTRL() (-EINVAL)
14263f0c603SCatalin Marinas #endif
1431fd96a3eSAndy Chiu #ifndef RISCV_V_SET_CONTROL
1441fd96a3eSAndy Chiu # define RISCV_V_SET_CONTROL(a) (-EINVAL)
1451fd96a3eSAndy Chiu #endif
1461fd96a3eSAndy Chiu #ifndef RISCV_V_GET_CONTROL
1471fd96a3eSAndy Chiu # define RISCV_V_GET_CONTROL() (-EINVAL)
1481fd96a3eSAndy Chiu #endif
1491da177e4SLinus Torvalds
1501da177e4SLinus Torvalds /*
1511da177e4SLinus Torvalds * this is where the system-wide overflow UID and GID are defined, for
1521da177e4SLinus Torvalds * architectures that now have 32-bit UID/GID but didn't in the past
1531da177e4SLinus Torvalds */
1541da177e4SLinus Torvalds
1551da177e4SLinus Torvalds int overflowuid = DEFAULT_OVERFLOWUID;
1561da177e4SLinus Torvalds int overflowgid = DEFAULT_OVERFLOWGID;
1571da177e4SLinus Torvalds
1581da177e4SLinus Torvalds EXPORT_SYMBOL(overflowuid);
1591da177e4SLinus Torvalds EXPORT_SYMBOL(overflowgid);
1601da177e4SLinus Torvalds
1611da177e4SLinus Torvalds /*
1621da177e4SLinus Torvalds * the same as above, but for filesystems which can only store a 16-bit
1631da177e4SLinus Torvalds * UID and GID. as such, this is needed on all architectures
1641da177e4SLinus Torvalds */
1651da177e4SLinus Torvalds
1661da177e4SLinus Torvalds int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
1678b2770a4SWolffhardt Schwabe int fs_overflowgid = DEFAULT_FS_OVERFLOWGID;
1681da177e4SLinus Torvalds
1691da177e4SLinus Torvalds EXPORT_SYMBOL(fs_overflowuid);
1701da177e4SLinus Torvalds EXPORT_SYMBOL(fs_overflowgid);
1711da177e4SLinus Torvalds
1721da177e4SLinus Torvalds /*
173fc832ad3SSerge E. Hallyn * Returns true if current's euid is same as p's uid or euid,
174fc832ad3SSerge E. Hallyn * or has CAP_SYS_NICE to p's user_ns.
175fc832ad3SSerge E. Hallyn *
176fc832ad3SSerge E. Hallyn * Called with rcu_read_lock, creds are safe
177fc832ad3SSerge E. Hallyn */
set_one_prio_perm(struct task_struct * p)178fc832ad3SSerge E. Hallyn static bool set_one_prio_perm(struct task_struct *p)
179fc832ad3SSerge E. Hallyn {
180fc832ad3SSerge E. Hallyn const struct cred *cred = current_cred(), *pcred = __task_cred(p);
181fc832ad3SSerge E. Hallyn
1825af66203SEric W. Biederman if (uid_eq(pcred->uid, cred->euid) ||
1835af66203SEric W. Biederman uid_eq(pcred->euid, cred->euid))
184fc832ad3SSerge E. Hallyn return true;
185c4a4d603SEric W. Biederman if (ns_capable(pcred->user_ns, CAP_SYS_NICE))
186fc832ad3SSerge E. Hallyn return true;
187fc832ad3SSerge E. Hallyn return false;
188fc832ad3SSerge E. Hallyn }
189fc832ad3SSerge E. Hallyn
190fc832ad3SSerge E. Hallyn /*
191c69e8d9cSDavid Howells * set the priority of a task
192c69e8d9cSDavid Howells * - the caller must hold the RCU read lock
193c69e8d9cSDavid Howells */
set_one_prio(struct task_struct * p,int niceval,int error)1941da177e4SLinus Torvalds static int set_one_prio(struct task_struct *p, int niceval, int error)
1951da177e4SLinus Torvalds {
1961da177e4SLinus Torvalds int no_nice;
1971da177e4SLinus Torvalds
198fc832ad3SSerge E. Hallyn if (!set_one_prio_perm(p)) {
1991da177e4SLinus Torvalds error = -EPERM;
2001da177e4SLinus Torvalds goto out;
2011da177e4SLinus Torvalds }
202e43379f1SMatt Mackall if (niceval < task_nice(p) && !can_nice(p, niceval)) {
2031da177e4SLinus Torvalds error = -EACCES;
2041da177e4SLinus Torvalds goto out;
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds no_nice = security_task_setnice(p, niceval);
2071da177e4SLinus Torvalds if (no_nice) {
2081da177e4SLinus Torvalds error = no_nice;
2091da177e4SLinus Torvalds goto out;
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds if (error == -ESRCH)
2121da177e4SLinus Torvalds error = 0;
2131da177e4SLinus Torvalds set_user_nice(p, niceval);
2141da177e4SLinus Torvalds out:
2151da177e4SLinus Torvalds return error;
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds
SYSCALL_DEFINE3(setpriority,int,which,int,who,int,niceval)218754fe8d2SHeiko Carstens SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
2191da177e4SLinus Torvalds {
2201da177e4SLinus Torvalds struct task_struct *g, *p;
2211da177e4SLinus Torvalds struct user_struct *user;
22286a264abSDavid Howells const struct cred *cred = current_cred();
2231da177e4SLinus Torvalds int error = -EINVAL;
22441487c65SEric W. Biederman struct pid *pgrp;
2257b44ab97SEric W. Biederman kuid_t uid;
2261da177e4SLinus Torvalds
2273e88c553SDaniel Walker if (which > PRIO_USER || which < PRIO_PROCESS)
2281da177e4SLinus Torvalds goto out;
2291da177e4SLinus Torvalds
2301da177e4SLinus Torvalds /* normalize: avoid signed division (rounding problems) */
2311da177e4SLinus Torvalds error = -ESRCH;
232c4a4d2f4SDongsheng Yang if (niceval < MIN_NICE)
233c4a4d2f4SDongsheng Yang niceval = MIN_NICE;
234c4a4d2f4SDongsheng Yang if (niceval > MAX_NICE)
235c4a4d2f4SDongsheng Yang niceval = MAX_NICE;
2361da177e4SLinus Torvalds
237d4581a23SThomas Gleixner rcu_read_lock();
2381da177e4SLinus Torvalds switch (which) {
2391da177e4SLinus Torvalds case PRIO_PROCESS:
24041487c65SEric W. Biederman if (who)
241228ebcbeSPavel Emelyanov p = find_task_by_vpid(who);
24241487c65SEric W. Biederman else
24341487c65SEric W. Biederman p = current;
2441da177e4SLinus Torvalds if (p)
2451da177e4SLinus Torvalds error = set_one_prio(p, niceval, error);
2461da177e4SLinus Torvalds break;
2471da177e4SLinus Torvalds case PRIO_PGRP:
24841487c65SEric W. Biederman if (who)
249b488893aSPavel Emelyanov pgrp = find_vpid(who);
25041487c65SEric W. Biederman else
25141487c65SEric W. Biederman pgrp = task_pgrp(current);
2527f8ca0edSDavidlohr Bueso read_lock(&tasklist_lock);
2532d70b68dSKen Chen do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
2541da177e4SLinus Torvalds error = set_one_prio(p, niceval, error);
2552d70b68dSKen Chen } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
2567f8ca0edSDavidlohr Bueso read_unlock(&tasklist_lock);
2571da177e4SLinus Torvalds break;
2581da177e4SLinus Torvalds case PRIO_USER:
2597b44ab97SEric W. Biederman uid = make_kuid(cred->user_ns, who);
26074ba508fSEric W. Biederman user = cred->user;
2611da177e4SLinus Torvalds if (!who)
262078de5f7SEric W. Biederman uid = cred->uid;
263ec94fc3dSvishnu.ps else if (!uid_eq(uid, cred->uid)) {
264ec94fc3dSvishnu.ps user = find_user(uid);
265ec94fc3dSvishnu.ps if (!user)
2661da177e4SLinus Torvalds goto out_unlock; /* No processes for this user */
267ec94fc3dSvishnu.ps }
2687f8ca0edSDavidlohr Bueso for_each_process_thread(g, p) {
2698639b461SBen Segall if (uid_eq(task_uid(p), uid) && task_pid_vnr(p))
2701da177e4SLinus Torvalds error = set_one_prio(p, niceval, error);
2717f8ca0edSDavidlohr Bueso }
272078de5f7SEric W. Biederman if (!uid_eq(uid, cred->uid))
2731da177e4SLinus Torvalds free_uid(user); /* For find_user() */
2741da177e4SLinus Torvalds break;
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds out_unlock:
277d4581a23SThomas Gleixner rcu_read_unlock();
2781da177e4SLinus Torvalds out:
2791da177e4SLinus Torvalds return error;
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds
2821da177e4SLinus Torvalds /*
2831da177e4SLinus Torvalds * Ugh. To avoid negative return values, "getpriority()" will
2841da177e4SLinus Torvalds * not return the normal nice-value, but a negated value that
2851da177e4SLinus Torvalds * has been offset by 20 (ie it returns 40..1 instead of -20..19)
2861da177e4SLinus Torvalds * to stay compatible.
2871da177e4SLinus Torvalds */
SYSCALL_DEFINE2(getpriority,int,which,int,who)288754fe8d2SHeiko Carstens SYSCALL_DEFINE2(getpriority, int, which, int, who)
2891da177e4SLinus Torvalds {
2901da177e4SLinus Torvalds struct task_struct *g, *p;
2911da177e4SLinus Torvalds struct user_struct *user;
29286a264abSDavid Howells const struct cred *cred = current_cred();
2931da177e4SLinus Torvalds long niceval, retval = -ESRCH;
29441487c65SEric W. Biederman struct pid *pgrp;
2957b44ab97SEric W. Biederman kuid_t uid;
2961da177e4SLinus Torvalds
2973e88c553SDaniel Walker if (which > PRIO_USER || which < PRIO_PROCESS)
2981da177e4SLinus Torvalds return -EINVAL;
2991da177e4SLinus Torvalds
30070118837STetsuo Handa rcu_read_lock();
3011da177e4SLinus Torvalds switch (which) {
3021da177e4SLinus Torvalds case PRIO_PROCESS:
30341487c65SEric W. Biederman if (who)
304228ebcbeSPavel Emelyanov p = find_task_by_vpid(who);
30541487c65SEric W. Biederman else
30641487c65SEric W. Biederman p = current;
3071da177e4SLinus Torvalds if (p) {
3087aa2c016SDongsheng Yang niceval = nice_to_rlimit(task_nice(p));
3091da177e4SLinus Torvalds if (niceval > retval)
3101da177e4SLinus Torvalds retval = niceval;
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds break;
3131da177e4SLinus Torvalds case PRIO_PGRP:
31441487c65SEric W. Biederman if (who)
315b488893aSPavel Emelyanov pgrp = find_vpid(who);
31641487c65SEric W. Biederman else
31741487c65SEric W. Biederman pgrp = task_pgrp(current);
3187f8ca0edSDavidlohr Bueso read_lock(&tasklist_lock);
3192d70b68dSKen Chen do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
3207aa2c016SDongsheng Yang niceval = nice_to_rlimit(task_nice(p));
3211da177e4SLinus Torvalds if (niceval > retval)
3221da177e4SLinus Torvalds retval = niceval;
3232d70b68dSKen Chen } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
3247f8ca0edSDavidlohr Bueso read_unlock(&tasklist_lock);
3251da177e4SLinus Torvalds break;
3261da177e4SLinus Torvalds case PRIO_USER:
3277b44ab97SEric W. Biederman uid = make_kuid(cred->user_ns, who);
32874ba508fSEric W. Biederman user = cred->user;
3291da177e4SLinus Torvalds if (!who)
330078de5f7SEric W. Biederman uid = cred->uid;
331ec94fc3dSvishnu.ps else if (!uid_eq(uid, cred->uid)) {
332ec94fc3dSvishnu.ps user = find_user(uid);
333ec94fc3dSvishnu.ps if (!user)
3341da177e4SLinus Torvalds goto out_unlock; /* No processes for this user */
335ec94fc3dSvishnu.ps }
3367f8ca0edSDavidlohr Bueso for_each_process_thread(g, p) {
3378639b461SBen Segall if (uid_eq(task_uid(p), uid) && task_pid_vnr(p)) {
3387aa2c016SDongsheng Yang niceval = nice_to_rlimit(task_nice(p));
3391da177e4SLinus Torvalds if (niceval > retval)
3401da177e4SLinus Torvalds retval = niceval;
3411da177e4SLinus Torvalds }
3427f8ca0edSDavidlohr Bueso }
343078de5f7SEric W. Biederman if (!uid_eq(uid, cred->uid))
3441da177e4SLinus Torvalds free_uid(user); /* for find_user() */
3451da177e4SLinus Torvalds break;
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds out_unlock:
34870118837STetsuo Handa rcu_read_unlock();
3491da177e4SLinus Torvalds
3501da177e4SLinus Torvalds return retval;
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds
3531da177e4SLinus Torvalds /*
3541da177e4SLinus Torvalds * Unprivileged users may change the real gid to the effective gid
3551da177e4SLinus Torvalds * or vice versa. (BSD-style)
3561da177e4SLinus Torvalds *
3571da177e4SLinus Torvalds * If you set the real gid at all, or set the effective gid to a value not
3581da177e4SLinus Torvalds * equal to the real gid, then the saved gid is set to the new effective gid.
3591da177e4SLinus Torvalds *
3601da177e4SLinus Torvalds * This makes it possible for a setgid program to completely drop its
3611da177e4SLinus Torvalds * privileges, which is often a useful assertion to make when you are doing
3621da177e4SLinus Torvalds * a security audit over a program.
3631da177e4SLinus Torvalds *
3641da177e4SLinus Torvalds * The general idea is that a program which uses just setregid() will be
3651da177e4SLinus Torvalds * 100% compatible with BSD. A program which uses just setgid() will be
3661da177e4SLinus Torvalds * 100% compatible with POSIX with saved IDs.
3671da177e4SLinus Torvalds *
3681da177e4SLinus Torvalds * SMP: There are not races, the GIDs are checked only by filesystem
3691da177e4SLinus Torvalds * operations (as far as semantic preservation is concerned).
3701da177e4SLinus Torvalds */
3712813893fSIulia Manda #ifdef CONFIG_MULTIUSER
__sys_setregid(gid_t rgid,gid_t egid)372e530dca5SDominik Brodowski long __sys_setregid(gid_t rgid, gid_t egid)
3731da177e4SLinus Torvalds {
374a29c33f4SEric W. Biederman struct user_namespace *ns = current_user_ns();
375d84f4f99SDavid Howells const struct cred *old;
376d84f4f99SDavid Howells struct cred *new;
3771da177e4SLinus Torvalds int retval;
378a29c33f4SEric W. Biederman kgid_t krgid, kegid;
379a29c33f4SEric W. Biederman
380a29c33f4SEric W. Biederman krgid = make_kgid(ns, rgid);
381a29c33f4SEric W. Biederman kegid = make_kgid(ns, egid);
382a29c33f4SEric W. Biederman
383a29c33f4SEric W. Biederman if ((rgid != (gid_t) -1) && !gid_valid(krgid))
384a29c33f4SEric W. Biederman return -EINVAL;
385a29c33f4SEric W. Biederman if ((egid != (gid_t) -1) && !gid_valid(kegid))
386a29c33f4SEric W. Biederman return -EINVAL;
3871da177e4SLinus Torvalds
388d84f4f99SDavid Howells new = prepare_creds();
389d84f4f99SDavid Howells if (!new)
390d84f4f99SDavid Howells return -ENOMEM;
391d84f4f99SDavid Howells old = current_cred();
392d84f4f99SDavid Howells
393d84f4f99SDavid Howells retval = -EPERM;
3941da177e4SLinus Torvalds if (rgid != (gid_t) -1) {
395a29c33f4SEric W. Biederman if (gid_eq(old->gid, krgid) ||
396a29c33f4SEric W. Biederman gid_eq(old->egid, krgid) ||
397111767c1SThomas Cedeno ns_capable_setid(old->user_ns, CAP_SETGID))
398a29c33f4SEric W. Biederman new->gid = krgid;
3991da177e4SLinus Torvalds else
400d84f4f99SDavid Howells goto error;
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds if (egid != (gid_t) -1) {
403a29c33f4SEric W. Biederman if (gid_eq(old->gid, kegid) ||
404a29c33f4SEric W. Biederman gid_eq(old->egid, kegid) ||
405a29c33f4SEric W. Biederman gid_eq(old->sgid, kegid) ||
406111767c1SThomas Cedeno ns_capable_setid(old->user_ns, CAP_SETGID))
407a29c33f4SEric W. Biederman new->egid = kegid;
408756184b7SCal Peake else
409d84f4f99SDavid Howells goto error;
4101da177e4SLinus Torvalds }
411d84f4f99SDavid Howells
4121da177e4SLinus Torvalds if (rgid != (gid_t) -1 ||
413a29c33f4SEric W. Biederman (egid != (gid_t) -1 && !gid_eq(kegid, old->gid)))
414d84f4f99SDavid Howells new->sgid = new->egid;
415d84f4f99SDavid Howells new->fsgid = new->egid;
416d84f4f99SDavid Howells
41739030e13SThomas Cedeno retval = security_task_fix_setgid(new, old, LSM_SETID_RE);
41839030e13SThomas Cedeno if (retval < 0)
41939030e13SThomas Cedeno goto error;
42039030e13SThomas Cedeno
421d84f4f99SDavid Howells return commit_creds(new);
422d84f4f99SDavid Howells
423d84f4f99SDavid Howells error:
424d84f4f99SDavid Howells abort_creds(new);
425d84f4f99SDavid Howells return retval;
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds
SYSCALL_DEFINE2(setregid,gid_t,rgid,gid_t,egid)428e530dca5SDominik Brodowski SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
429e530dca5SDominik Brodowski {
430e530dca5SDominik Brodowski return __sys_setregid(rgid, egid);
431e530dca5SDominik Brodowski }
432e530dca5SDominik Brodowski
4331da177e4SLinus Torvalds /*
4341da177e4SLinus Torvalds * setgid() is implemented like SysV w/ SAVED_IDS
4351da177e4SLinus Torvalds *
4361da177e4SLinus Torvalds * SMP: Same implicit races as above.
4371da177e4SLinus Torvalds */
__sys_setgid(gid_t gid)438e530dca5SDominik Brodowski long __sys_setgid(gid_t gid)
4391da177e4SLinus Torvalds {
440a29c33f4SEric W. Biederman struct user_namespace *ns = current_user_ns();
441d84f4f99SDavid Howells const struct cred *old;
442d84f4f99SDavid Howells struct cred *new;
4431da177e4SLinus Torvalds int retval;
444a29c33f4SEric W. Biederman kgid_t kgid;
445a29c33f4SEric W. Biederman
446a29c33f4SEric W. Biederman kgid = make_kgid(ns, gid);
447a29c33f4SEric W. Biederman if (!gid_valid(kgid))
448a29c33f4SEric W. Biederman return -EINVAL;
4491da177e4SLinus Torvalds
450d84f4f99SDavid Howells new = prepare_creds();
451d84f4f99SDavid Howells if (!new)
452d84f4f99SDavid Howells return -ENOMEM;
453d84f4f99SDavid Howells old = current_cred();
454d84f4f99SDavid Howells
455d84f4f99SDavid Howells retval = -EPERM;
456111767c1SThomas Cedeno if (ns_capable_setid(old->user_ns, CAP_SETGID))
457a29c33f4SEric W. Biederman new->gid = new->egid = new->sgid = new->fsgid = kgid;
458a29c33f4SEric W. Biederman else if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->sgid))
459a29c33f4SEric W. Biederman new->egid = new->fsgid = kgid;
4601da177e4SLinus Torvalds else
461d84f4f99SDavid Howells goto error;
4621da177e4SLinus Torvalds
46339030e13SThomas Cedeno retval = security_task_fix_setgid(new, old, LSM_SETID_ID);
46439030e13SThomas Cedeno if (retval < 0)
46539030e13SThomas Cedeno goto error;
46639030e13SThomas Cedeno
467d84f4f99SDavid Howells return commit_creds(new);
468d84f4f99SDavid Howells
469d84f4f99SDavid Howells error:
470d84f4f99SDavid Howells abort_creds(new);
471d84f4f99SDavid Howells return retval;
4721da177e4SLinus Torvalds }
4731da177e4SLinus Torvalds
SYSCALL_DEFINE1(setgid,gid_t,gid)474e530dca5SDominik Brodowski SYSCALL_DEFINE1(setgid, gid_t, gid)
475e530dca5SDominik Brodowski {
476e530dca5SDominik Brodowski return __sys_setgid(gid);
477e530dca5SDominik Brodowski }
478e530dca5SDominik Brodowski
479d84f4f99SDavid Howells /*
480d84f4f99SDavid Howells * change the user struct in a credentials set to match the new UID
481d84f4f99SDavid Howells */
set_user(struct cred * new)482d84f4f99SDavid Howells static int set_user(struct cred *new)
4831da177e4SLinus Torvalds {
4841da177e4SLinus Torvalds struct user_struct *new_user;
4851da177e4SLinus Torvalds
486078de5f7SEric W. Biederman new_user = alloc_uid(new->uid);
4871da177e4SLinus Torvalds if (!new_user)
4881da177e4SLinus Torvalds return -EAGAIN;
4891da177e4SLinus Torvalds
490c923a8e7SEric W. Biederman free_uid(new->user);
491c923a8e7SEric W. Biederman new->user = new_user;
492c923a8e7SEric W. Biederman return 0;
493c923a8e7SEric W. Biederman }
494c923a8e7SEric W. Biederman
flag_nproc_exceeded(struct cred * new)495c923a8e7SEric W. Biederman static void flag_nproc_exceeded(struct cred *new)
496c923a8e7SEric W. Biederman {
497c923a8e7SEric W. Biederman if (new->ucounts == current_ucounts())
498c923a8e7SEric W. Biederman return;
499c923a8e7SEric W. Biederman
50072fa5997SVasiliy Kulikov /*
50172fa5997SVasiliy Kulikov * We don't fail in case of NPROC limit excess here because too many
50272fa5997SVasiliy Kulikov * poorly written programs don't check set*uid() return code, assuming
50372fa5997SVasiliy Kulikov * it never fails if called by root. We may still enforce NPROC limit
50472fa5997SVasiliy Kulikov * for programs doing set*uid()+execve() by harmlessly deferring the
50572fa5997SVasiliy Kulikov * failure to the execve() stage.
50672fa5997SVasiliy Kulikov */
507de399236SAlexey Gladkov if (is_rlimit_overlimit(new->ucounts, UCOUNT_RLIMIT_NPROC, rlimit(RLIMIT_NPROC)) &&
508c923a8e7SEric W. Biederman new->user != INIT_USER)
50972fa5997SVasiliy Kulikov current->flags |= PF_NPROC_EXCEEDED;
51072fa5997SVasiliy Kulikov else
51172fa5997SVasiliy Kulikov current->flags &= ~PF_NPROC_EXCEEDED;
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds
5141da177e4SLinus Torvalds /*
5151da177e4SLinus Torvalds * Unprivileged users may change the real uid to the effective uid
5161da177e4SLinus Torvalds * or vice versa. (BSD-style)
5171da177e4SLinus Torvalds *
5181da177e4SLinus Torvalds * If you set the real uid at all, or set the effective uid to a value not
5191da177e4SLinus Torvalds * equal to the real uid, then the saved uid is set to the new effective uid.
5201da177e4SLinus Torvalds *
5211da177e4SLinus Torvalds * This makes it possible for a setuid program to completely drop its
5221da177e4SLinus Torvalds * privileges, which is often a useful assertion to make when you are doing
5231da177e4SLinus Torvalds * a security audit over a program.
5241da177e4SLinus Torvalds *
5251da177e4SLinus Torvalds * The general idea is that a program which uses just setreuid() will be
5261da177e4SLinus Torvalds * 100% compatible with BSD. A program which uses just setuid() will be
5271da177e4SLinus Torvalds * 100% compatible with POSIX with saved IDs.
5281da177e4SLinus Torvalds */
__sys_setreuid(uid_t ruid,uid_t euid)529e530dca5SDominik Brodowski long __sys_setreuid(uid_t ruid, uid_t euid)
5301da177e4SLinus Torvalds {
531a29c33f4SEric W. Biederman struct user_namespace *ns = current_user_ns();
532d84f4f99SDavid Howells const struct cred *old;
533d84f4f99SDavid Howells struct cred *new;
5341da177e4SLinus Torvalds int retval;
535a29c33f4SEric W. Biederman kuid_t kruid, keuid;
536a29c33f4SEric W. Biederman
537a29c33f4SEric W. Biederman kruid = make_kuid(ns, ruid);
538a29c33f4SEric W. Biederman keuid = make_kuid(ns, euid);
539a29c33f4SEric W. Biederman
540a29c33f4SEric W. Biederman if ((ruid != (uid_t) -1) && !uid_valid(kruid))
541a29c33f4SEric W. Biederman return -EINVAL;
542a29c33f4SEric W. Biederman if ((euid != (uid_t) -1) && !uid_valid(keuid))
543a29c33f4SEric W. Biederman return -EINVAL;
5441da177e4SLinus Torvalds
545d84f4f99SDavid Howells new = prepare_creds();
546d84f4f99SDavid Howells if (!new)
547d84f4f99SDavid Howells return -ENOMEM;
548d84f4f99SDavid Howells old = current_cred();
549d84f4f99SDavid Howells
550d84f4f99SDavid Howells retval = -EPERM;
5511da177e4SLinus Torvalds if (ruid != (uid_t) -1) {
552a29c33f4SEric W. Biederman new->uid = kruid;
553a29c33f4SEric W. Biederman if (!uid_eq(old->uid, kruid) &&
554a29c33f4SEric W. Biederman !uid_eq(old->euid, kruid) &&
55540852275SMicah Morton !ns_capable_setid(old->user_ns, CAP_SETUID))
556d84f4f99SDavid Howells goto error;
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds
5591da177e4SLinus Torvalds if (euid != (uid_t) -1) {
560a29c33f4SEric W. Biederman new->euid = keuid;
561a29c33f4SEric W. Biederman if (!uid_eq(old->uid, keuid) &&
562a29c33f4SEric W. Biederman !uid_eq(old->euid, keuid) &&
563a29c33f4SEric W. Biederman !uid_eq(old->suid, keuid) &&
56440852275SMicah Morton !ns_capable_setid(old->user_ns, CAP_SETUID))
565d84f4f99SDavid Howells goto error;
5661da177e4SLinus Torvalds }
5671da177e4SLinus Torvalds
568a29c33f4SEric W. Biederman if (!uid_eq(new->uid, old->uid)) {
56954e99124SDhaval Giani retval = set_user(new);
57054e99124SDhaval Giani if (retval < 0)
571d84f4f99SDavid Howells goto error;
57254e99124SDhaval Giani }
5731da177e4SLinus Torvalds if (ruid != (uid_t) -1 ||
574a29c33f4SEric W. Biederman (euid != (uid_t) -1 && !uid_eq(keuid, old->uid)))
575d84f4f99SDavid Howells new->suid = new->euid;
576d84f4f99SDavid Howells new->fsuid = new->euid;
5771da177e4SLinus Torvalds
578d84f4f99SDavid Howells retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
579d84f4f99SDavid Howells if (retval < 0)
580d84f4f99SDavid Howells goto error;
5811da177e4SLinus Torvalds
582905ae01cSAlexey Gladkov retval = set_cred_ucounts(new);
583905ae01cSAlexey Gladkov if (retval < 0)
584905ae01cSAlexey Gladkov goto error;
585905ae01cSAlexey Gladkov
586c923a8e7SEric W. Biederman flag_nproc_exceeded(new);
587d84f4f99SDavid Howells return commit_creds(new);
588d84f4f99SDavid Howells
589d84f4f99SDavid Howells error:
590d84f4f99SDavid Howells abort_creds(new);
591d84f4f99SDavid Howells return retval;
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds
SYSCALL_DEFINE2(setreuid,uid_t,ruid,uid_t,euid)594e530dca5SDominik Brodowski SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
595e530dca5SDominik Brodowski {
596e530dca5SDominik Brodowski return __sys_setreuid(ruid, euid);
597e530dca5SDominik Brodowski }
598e530dca5SDominik Brodowski
5991da177e4SLinus Torvalds /*
6001da177e4SLinus Torvalds * setuid() is implemented like SysV with SAVED_IDS
6011da177e4SLinus Torvalds *
6021da177e4SLinus Torvalds * Note that SAVED_ID's is deficient in that a setuid root program
6031da177e4SLinus Torvalds * like sendmail, for example, cannot set its uid to be a normal
6041da177e4SLinus Torvalds * user and then switch back, because if you're root, setuid() sets
6051da177e4SLinus Torvalds * the saved uid too. If you don't like this, blame the bright people
6061da177e4SLinus Torvalds * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
6071da177e4SLinus Torvalds * will allow a root program to temporarily drop privileges and be able to
6081da177e4SLinus Torvalds * regain them by swapping the real and effective uid.
6091da177e4SLinus Torvalds */
__sys_setuid(uid_t uid)610e530dca5SDominik Brodowski long __sys_setuid(uid_t uid)
6111da177e4SLinus Torvalds {
612a29c33f4SEric W. Biederman struct user_namespace *ns = current_user_ns();
613d84f4f99SDavid Howells const struct cred *old;
614d84f4f99SDavid Howells struct cred *new;
6151da177e4SLinus Torvalds int retval;
616a29c33f4SEric W. Biederman kuid_t kuid;
617a29c33f4SEric W. Biederman
618a29c33f4SEric W. Biederman kuid = make_kuid(ns, uid);
619a29c33f4SEric W. Biederman if (!uid_valid(kuid))
620a29c33f4SEric W. Biederman return -EINVAL;
6211da177e4SLinus Torvalds
622d84f4f99SDavid Howells new = prepare_creds();
623d84f4f99SDavid Howells if (!new)
624d84f4f99SDavid Howells return -ENOMEM;
625d84f4f99SDavid Howells old = current_cred();
626d84f4f99SDavid Howells
627d84f4f99SDavid Howells retval = -EPERM;
62840852275SMicah Morton if (ns_capable_setid(old->user_ns, CAP_SETUID)) {
629a29c33f4SEric W. Biederman new->suid = new->uid = kuid;
630a29c33f4SEric W. Biederman if (!uid_eq(kuid, old->uid)) {
63154e99124SDhaval Giani retval = set_user(new);
63254e99124SDhaval Giani if (retval < 0)
633d84f4f99SDavid Howells goto error;
6341da177e4SLinus Torvalds }
635a29c33f4SEric W. Biederman } else if (!uid_eq(kuid, old->uid) && !uid_eq(kuid, new->suid)) {
636d84f4f99SDavid Howells goto error;
637d84f4f99SDavid Howells }
6381da177e4SLinus Torvalds
639a29c33f4SEric W. Biederman new->fsuid = new->euid = kuid;
6401da177e4SLinus Torvalds
641d84f4f99SDavid Howells retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
642d84f4f99SDavid Howells if (retval < 0)
643d84f4f99SDavid Howells goto error;
644d84f4f99SDavid Howells
645905ae01cSAlexey Gladkov retval = set_cred_ucounts(new);
646905ae01cSAlexey Gladkov if (retval < 0)
647905ae01cSAlexey Gladkov goto error;
648905ae01cSAlexey Gladkov
649c923a8e7SEric W. Biederman flag_nproc_exceeded(new);
650d84f4f99SDavid Howells return commit_creds(new);
651d84f4f99SDavid Howells
652d84f4f99SDavid Howells error:
653d84f4f99SDavid Howells abort_creds(new);
654d84f4f99SDavid Howells return retval;
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds
SYSCALL_DEFINE1(setuid,uid_t,uid)657e530dca5SDominik Brodowski SYSCALL_DEFINE1(setuid, uid_t, uid)
658e530dca5SDominik Brodowski {
659e530dca5SDominik Brodowski return __sys_setuid(uid);
660e530dca5SDominik Brodowski }
661e530dca5SDominik Brodowski
6621da177e4SLinus Torvalds
6631da177e4SLinus Torvalds /*
6641da177e4SLinus Torvalds * This function implements a generic ability to update ruid, euid,
6651da177e4SLinus Torvalds * and suid. This allows you to implement the 4.4 compatible seteuid().
6661da177e4SLinus Torvalds */
__sys_setresuid(uid_t ruid,uid_t euid,uid_t suid)667e530dca5SDominik Brodowski long __sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
6681da177e4SLinus Torvalds {
669a29c33f4SEric W. Biederman struct user_namespace *ns = current_user_ns();
670d84f4f99SDavid Howells const struct cred *old;
671d84f4f99SDavid Howells struct cred *new;
6721da177e4SLinus Torvalds int retval;
673a29c33f4SEric W. Biederman kuid_t kruid, keuid, ksuid;
674659c0ce1SOndrej Mosnacek bool ruid_new, euid_new, suid_new;
675a29c33f4SEric W. Biederman
676a29c33f4SEric W. Biederman kruid = make_kuid(ns, ruid);
677a29c33f4SEric W. Biederman keuid = make_kuid(ns, euid);
678a29c33f4SEric W. Biederman ksuid = make_kuid(ns, suid);
679a29c33f4SEric W. Biederman
680a29c33f4SEric W. Biederman if ((ruid != (uid_t) -1) && !uid_valid(kruid))
681a29c33f4SEric W. Biederman return -EINVAL;
682a29c33f4SEric W. Biederman
683a29c33f4SEric W. Biederman if ((euid != (uid_t) -1) && !uid_valid(keuid))
684a29c33f4SEric W. Biederman return -EINVAL;
685a29c33f4SEric W. Biederman
686a29c33f4SEric W. Biederman if ((suid != (uid_t) -1) && !uid_valid(ksuid))
687a29c33f4SEric W. Biederman return -EINVAL;
6881da177e4SLinus Torvalds
689659c0ce1SOndrej Mosnacek old = current_cred();
690659c0ce1SOndrej Mosnacek
691659c0ce1SOndrej Mosnacek /* check for no-op */
692659c0ce1SOndrej Mosnacek if ((ruid == (uid_t) -1 || uid_eq(kruid, old->uid)) &&
693659c0ce1SOndrej Mosnacek (euid == (uid_t) -1 || (uid_eq(keuid, old->euid) &&
694659c0ce1SOndrej Mosnacek uid_eq(keuid, old->fsuid))) &&
695659c0ce1SOndrej Mosnacek (suid == (uid_t) -1 || uid_eq(ksuid, old->suid)))
696659c0ce1SOndrej Mosnacek return 0;
697659c0ce1SOndrej Mosnacek
698659c0ce1SOndrej Mosnacek ruid_new = ruid != (uid_t) -1 && !uid_eq(kruid, old->uid) &&
699659c0ce1SOndrej Mosnacek !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid);
700659c0ce1SOndrej Mosnacek euid_new = euid != (uid_t) -1 && !uid_eq(keuid, old->uid) &&
701659c0ce1SOndrej Mosnacek !uid_eq(keuid, old->euid) && !uid_eq(keuid, old->suid);
702659c0ce1SOndrej Mosnacek suid_new = suid != (uid_t) -1 && !uid_eq(ksuid, old->uid) &&
703659c0ce1SOndrej Mosnacek !uid_eq(ksuid, old->euid) && !uid_eq(ksuid, old->suid);
704659c0ce1SOndrej Mosnacek if ((ruid_new || euid_new || suid_new) &&
705659c0ce1SOndrej Mosnacek !ns_capable_setid(old->user_ns, CAP_SETUID))
706659c0ce1SOndrej Mosnacek return -EPERM;
707659c0ce1SOndrej Mosnacek
708d84f4f99SDavid Howells new = prepare_creds();
709d84f4f99SDavid Howells if (!new)
710d84f4f99SDavid Howells return -ENOMEM;
711d84f4f99SDavid Howells
7121da177e4SLinus Torvalds if (ruid != (uid_t) -1) {
713a29c33f4SEric W. Biederman new->uid = kruid;
714a29c33f4SEric W. Biederman if (!uid_eq(kruid, old->uid)) {
71554e99124SDhaval Giani retval = set_user(new);
71654e99124SDhaval Giani if (retval < 0)
717d84f4f99SDavid Howells goto error;
7181da177e4SLinus Torvalds }
71954e99124SDhaval Giani }
720d84f4f99SDavid Howells if (euid != (uid_t) -1)
721a29c33f4SEric W. Biederman new->euid = keuid;
7221da177e4SLinus Torvalds if (suid != (uid_t) -1)
723a29c33f4SEric W. Biederman new->suid = ksuid;
724d84f4f99SDavid Howells new->fsuid = new->euid;
7251da177e4SLinus Torvalds
726d84f4f99SDavid Howells retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
727d84f4f99SDavid Howells if (retval < 0)
728d84f4f99SDavid Howells goto error;
7291da177e4SLinus Torvalds
730905ae01cSAlexey Gladkov retval = set_cred_ucounts(new);
731905ae01cSAlexey Gladkov if (retval < 0)
732905ae01cSAlexey Gladkov goto error;
733905ae01cSAlexey Gladkov
734c923a8e7SEric W. Biederman flag_nproc_exceeded(new);
735d84f4f99SDavid Howells return commit_creds(new);
736d84f4f99SDavid Howells
737d84f4f99SDavid Howells error:
738d84f4f99SDavid Howells abort_creds(new);
739d84f4f99SDavid Howells return retval;
7401da177e4SLinus Torvalds }
7411da177e4SLinus Torvalds
SYSCALL_DEFINE3(setresuid,uid_t,ruid,uid_t,euid,uid_t,suid)742e530dca5SDominik Brodowski SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
743e530dca5SDominik Brodowski {
744e530dca5SDominik Brodowski return __sys_setresuid(ruid, euid, suid);
745e530dca5SDominik Brodowski }
746e530dca5SDominik Brodowski
SYSCALL_DEFINE3(getresuid,uid_t __user *,ruidp,uid_t __user *,euidp,uid_t __user *,suidp)747a29c33f4SEric W. Biederman SYSCALL_DEFINE3(getresuid, uid_t __user *, ruidp, uid_t __user *, euidp, uid_t __user *, suidp)
7481da177e4SLinus Torvalds {
74986a264abSDavid Howells const struct cred *cred = current_cred();
7501da177e4SLinus Torvalds int retval;
751a29c33f4SEric W. Biederman uid_t ruid, euid, suid;
7521da177e4SLinus Torvalds
753a29c33f4SEric W. Biederman ruid = from_kuid_munged(cred->user_ns, cred->uid);
754a29c33f4SEric W. Biederman euid = from_kuid_munged(cred->user_ns, cred->euid);
755a29c33f4SEric W. Biederman suid = from_kuid_munged(cred->user_ns, cred->suid);
756a29c33f4SEric W. Biederman
757ec94fc3dSvishnu.ps retval = put_user(ruid, ruidp);
758ec94fc3dSvishnu.ps if (!retval) {
759ec94fc3dSvishnu.ps retval = put_user(euid, euidp);
760ec94fc3dSvishnu.ps if (!retval)
761ec94fc3dSvishnu.ps return put_user(suid, suidp);
762ec94fc3dSvishnu.ps }
7631da177e4SLinus Torvalds return retval;
7641da177e4SLinus Torvalds }
7651da177e4SLinus Torvalds
7661da177e4SLinus Torvalds /*
7671da177e4SLinus Torvalds * Same as above, but for rgid, egid, sgid.
7681da177e4SLinus Torvalds */
__sys_setresgid(gid_t rgid,gid_t egid,gid_t sgid)769e530dca5SDominik Brodowski long __sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
7701da177e4SLinus Torvalds {
771a29c33f4SEric W. Biederman struct user_namespace *ns = current_user_ns();
772d84f4f99SDavid Howells const struct cred *old;
773d84f4f99SDavid Howells struct cred *new;
7741da177e4SLinus Torvalds int retval;
775a29c33f4SEric W. Biederman kgid_t krgid, kegid, ksgid;
776659c0ce1SOndrej Mosnacek bool rgid_new, egid_new, sgid_new;
777a29c33f4SEric W. Biederman
778a29c33f4SEric W. Biederman krgid = make_kgid(ns, rgid);
779a29c33f4SEric W. Biederman kegid = make_kgid(ns, egid);
780a29c33f4SEric W. Biederman ksgid = make_kgid(ns, sgid);
781a29c33f4SEric W. Biederman
782a29c33f4SEric W. Biederman if ((rgid != (gid_t) -1) && !gid_valid(krgid))
783a29c33f4SEric W. Biederman return -EINVAL;
784a29c33f4SEric W. Biederman if ((egid != (gid_t) -1) && !gid_valid(kegid))
785a29c33f4SEric W. Biederman return -EINVAL;
786a29c33f4SEric W. Biederman if ((sgid != (gid_t) -1) && !gid_valid(ksgid))
787a29c33f4SEric W. Biederman return -EINVAL;
7881da177e4SLinus Torvalds
789659c0ce1SOndrej Mosnacek old = current_cred();
790659c0ce1SOndrej Mosnacek
791659c0ce1SOndrej Mosnacek /* check for no-op */
792659c0ce1SOndrej Mosnacek if ((rgid == (gid_t) -1 || gid_eq(krgid, old->gid)) &&
793659c0ce1SOndrej Mosnacek (egid == (gid_t) -1 || (gid_eq(kegid, old->egid) &&
794659c0ce1SOndrej Mosnacek gid_eq(kegid, old->fsgid))) &&
795659c0ce1SOndrej Mosnacek (sgid == (gid_t) -1 || gid_eq(ksgid, old->sgid)))
796659c0ce1SOndrej Mosnacek return 0;
797659c0ce1SOndrej Mosnacek
798659c0ce1SOndrej Mosnacek rgid_new = rgid != (gid_t) -1 && !gid_eq(krgid, old->gid) &&
799659c0ce1SOndrej Mosnacek !gid_eq(krgid, old->egid) && !gid_eq(krgid, old->sgid);
800659c0ce1SOndrej Mosnacek egid_new = egid != (gid_t) -1 && !gid_eq(kegid, old->gid) &&
801659c0ce1SOndrej Mosnacek !gid_eq(kegid, old->egid) && !gid_eq(kegid, old->sgid);
802659c0ce1SOndrej Mosnacek sgid_new = sgid != (gid_t) -1 && !gid_eq(ksgid, old->gid) &&
803659c0ce1SOndrej Mosnacek !gid_eq(ksgid, old->egid) && !gid_eq(ksgid, old->sgid);
804659c0ce1SOndrej Mosnacek if ((rgid_new || egid_new || sgid_new) &&
805659c0ce1SOndrej Mosnacek !ns_capable_setid(old->user_ns, CAP_SETGID))
806659c0ce1SOndrej Mosnacek return -EPERM;
807659c0ce1SOndrej Mosnacek
808d84f4f99SDavid Howells new = prepare_creds();
809d84f4f99SDavid Howells if (!new)
810d84f4f99SDavid Howells return -ENOMEM;
8111da177e4SLinus Torvalds
812d84f4f99SDavid Howells if (rgid != (gid_t) -1)
813a29c33f4SEric W. Biederman new->gid = krgid;
814d84f4f99SDavid Howells if (egid != (gid_t) -1)
815a29c33f4SEric W. Biederman new->egid = kegid;
816d84f4f99SDavid Howells if (sgid != (gid_t) -1)
817a29c33f4SEric W. Biederman new->sgid = ksgid;
818d84f4f99SDavid Howells new->fsgid = new->egid;
819d84f4f99SDavid Howells
82039030e13SThomas Cedeno retval = security_task_fix_setgid(new, old, LSM_SETID_RES);
82139030e13SThomas Cedeno if (retval < 0)
82239030e13SThomas Cedeno goto error;
82339030e13SThomas Cedeno
824d84f4f99SDavid Howells return commit_creds(new);
825d84f4f99SDavid Howells
826d84f4f99SDavid Howells error:
827d84f4f99SDavid Howells abort_creds(new);
828d84f4f99SDavid Howells return retval;
8291da177e4SLinus Torvalds }
8301da177e4SLinus Torvalds
SYSCALL_DEFINE3(setresgid,gid_t,rgid,gid_t,egid,gid_t,sgid)831e530dca5SDominik Brodowski SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
832e530dca5SDominik Brodowski {
833e530dca5SDominik Brodowski return __sys_setresgid(rgid, egid, sgid);
834e530dca5SDominik Brodowski }
835e530dca5SDominik Brodowski
SYSCALL_DEFINE3(getresgid,gid_t __user *,rgidp,gid_t __user *,egidp,gid_t __user *,sgidp)836a29c33f4SEric W. Biederman SYSCALL_DEFINE3(getresgid, gid_t __user *, rgidp, gid_t __user *, egidp, gid_t __user *, sgidp)
8371da177e4SLinus Torvalds {
83886a264abSDavid Howells const struct cred *cred = current_cred();
8391da177e4SLinus Torvalds int retval;
840a29c33f4SEric W. Biederman gid_t rgid, egid, sgid;
8411da177e4SLinus Torvalds
842a29c33f4SEric W. Biederman rgid = from_kgid_munged(cred->user_ns, cred->gid);
843a29c33f4SEric W. Biederman egid = from_kgid_munged(cred->user_ns, cred->egid);
844a29c33f4SEric W. Biederman sgid = from_kgid_munged(cred->user_ns, cred->sgid);
845a29c33f4SEric W. Biederman
846ec94fc3dSvishnu.ps retval = put_user(rgid, rgidp);
847ec94fc3dSvishnu.ps if (!retval) {
848ec94fc3dSvishnu.ps retval = put_user(egid, egidp);
849ec94fc3dSvishnu.ps if (!retval)
850a29c33f4SEric W. Biederman retval = put_user(sgid, sgidp);
851ec94fc3dSvishnu.ps }
8521da177e4SLinus Torvalds
8531da177e4SLinus Torvalds return retval;
8541da177e4SLinus Torvalds }
8551da177e4SLinus Torvalds
8561da177e4SLinus Torvalds
8571da177e4SLinus Torvalds /*
8581da177e4SLinus Torvalds * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
8591da177e4SLinus Torvalds * is used for "access()" and for the NFS daemon (letting nfsd stay at
8601da177e4SLinus Torvalds * whatever uid it wants to). It normally shadows "euid", except when
8611da177e4SLinus Torvalds * explicitly set by setfsuid() or for access..
8621da177e4SLinus Torvalds */
__sys_setfsuid(uid_t uid)863e530dca5SDominik Brodowski long __sys_setfsuid(uid_t uid)
8641da177e4SLinus Torvalds {
865d84f4f99SDavid Howells const struct cred *old;
866d84f4f99SDavid Howells struct cred *new;
867d84f4f99SDavid Howells uid_t old_fsuid;
868a29c33f4SEric W. Biederman kuid_t kuid;
869a29c33f4SEric W. Biederman
870a29c33f4SEric W. Biederman old = current_cred();
871a29c33f4SEric W. Biederman old_fsuid = from_kuid_munged(old->user_ns, old->fsuid);
872a29c33f4SEric W. Biederman
873a29c33f4SEric W. Biederman kuid = make_kuid(old->user_ns, uid);
874a29c33f4SEric W. Biederman if (!uid_valid(kuid))
875a29c33f4SEric W. Biederman return old_fsuid;
8761da177e4SLinus Torvalds
877d84f4f99SDavid Howells new = prepare_creds();
878d84f4f99SDavid Howells if (!new)
879a29c33f4SEric W. Biederman return old_fsuid;
8801da177e4SLinus Torvalds
881a29c33f4SEric W. Biederman if (uid_eq(kuid, old->uid) || uid_eq(kuid, old->euid) ||
882a29c33f4SEric W. Biederman uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) ||
88340852275SMicah Morton ns_capable_setid(old->user_ns, CAP_SETUID)) {
884a29c33f4SEric W. Biederman if (!uid_eq(kuid, old->fsuid)) {
885a29c33f4SEric W. Biederman new->fsuid = kuid;
886d84f4f99SDavid Howells if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
887d84f4f99SDavid Howells goto change_okay;
8881da177e4SLinus Torvalds }
8891da177e4SLinus Torvalds }
8901da177e4SLinus Torvalds
891d84f4f99SDavid Howells abort_creds(new);
892d84f4f99SDavid Howells return old_fsuid;
8931da177e4SLinus Torvalds
894d84f4f99SDavid Howells change_okay:
895d84f4f99SDavid Howells commit_creds(new);
8961da177e4SLinus Torvalds return old_fsuid;
8971da177e4SLinus Torvalds }
8981da177e4SLinus Torvalds
SYSCALL_DEFINE1(setfsuid,uid_t,uid)899e530dca5SDominik Brodowski SYSCALL_DEFINE1(setfsuid, uid_t, uid)
900e530dca5SDominik Brodowski {
901e530dca5SDominik Brodowski return __sys_setfsuid(uid);
902e530dca5SDominik Brodowski }
903e530dca5SDominik Brodowski
9041da177e4SLinus Torvalds /*
905f42df9e6SJohn Anthony Kazos Jr * Samma på svenska..
9061da177e4SLinus Torvalds */
__sys_setfsgid(gid_t gid)907e530dca5SDominik Brodowski long __sys_setfsgid(gid_t gid)
9081da177e4SLinus Torvalds {
909d84f4f99SDavid Howells const struct cred *old;
910d84f4f99SDavid Howells struct cred *new;
911d84f4f99SDavid Howells gid_t old_fsgid;
912a29c33f4SEric W. Biederman kgid_t kgid;
913a29c33f4SEric W. Biederman
914a29c33f4SEric W. Biederman old = current_cred();
915a29c33f4SEric W. Biederman old_fsgid = from_kgid_munged(old->user_ns, old->fsgid);
916a29c33f4SEric W. Biederman
917a29c33f4SEric W. Biederman kgid = make_kgid(old->user_ns, gid);
918a29c33f4SEric W. Biederman if (!gid_valid(kgid))
919a29c33f4SEric W. Biederman return old_fsgid;
9201da177e4SLinus Torvalds
921d84f4f99SDavid Howells new = prepare_creds();
922d84f4f99SDavid Howells if (!new)
923a29c33f4SEric W. Biederman return old_fsgid;
924d84f4f99SDavid Howells
925a29c33f4SEric W. Biederman if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->egid) ||
926a29c33f4SEric W. Biederman gid_eq(kgid, old->sgid) || gid_eq(kgid, old->fsgid) ||
927111767c1SThomas Cedeno ns_capable_setid(old->user_ns, CAP_SETGID)) {
928a29c33f4SEric W. Biederman if (!gid_eq(kgid, old->fsgid)) {
929a29c33f4SEric W. Biederman new->fsgid = kgid;
93039030e13SThomas Cedeno if (security_task_fix_setgid(new,old,LSM_SETID_FS) == 0)
931d84f4f99SDavid Howells goto change_okay;
9321da177e4SLinus Torvalds }
9331da177e4SLinus Torvalds }
934d84f4f99SDavid Howells
935d84f4f99SDavid Howells abort_creds(new);
936d84f4f99SDavid Howells return old_fsgid;
937d84f4f99SDavid Howells
938d84f4f99SDavid Howells change_okay:
939d84f4f99SDavid Howells commit_creds(new);
9401da177e4SLinus Torvalds return old_fsgid;
9411da177e4SLinus Torvalds }
942e530dca5SDominik Brodowski
SYSCALL_DEFINE1(setfsgid,gid_t,gid)943e530dca5SDominik Brodowski SYSCALL_DEFINE1(setfsgid, gid_t, gid)
944e530dca5SDominik Brodowski {
945e530dca5SDominik Brodowski return __sys_setfsgid(gid);
946e530dca5SDominik Brodowski }
9472813893fSIulia Manda #endif /* CONFIG_MULTIUSER */
9481da177e4SLinus Torvalds
9494a22f166SStephen Rothwell /**
9504a22f166SStephen Rothwell * sys_getpid - return the thread group id of the current process
9514a22f166SStephen Rothwell *
9524a22f166SStephen Rothwell * Note, despite the name, this returns the tgid not the pid. The tgid and
9534a22f166SStephen Rothwell * the pid are identical unless CLONE_THREAD was specified on clone() in
9544a22f166SStephen Rothwell * which case the tgid is the same in all threads of the same group.
9554a22f166SStephen Rothwell *
9564a22f166SStephen Rothwell * This is SMP safe as current->tgid does not change.
9574a22f166SStephen Rothwell */
SYSCALL_DEFINE0(getpid)9584a22f166SStephen Rothwell SYSCALL_DEFINE0(getpid)
9594a22f166SStephen Rothwell {
9604a22f166SStephen Rothwell return task_tgid_vnr(current);
9614a22f166SStephen Rothwell }
9624a22f166SStephen Rothwell
9634a22f166SStephen Rothwell /* Thread ID - the internal kernel "pid" */
SYSCALL_DEFINE0(gettid)9644a22f166SStephen Rothwell SYSCALL_DEFINE0(gettid)
9654a22f166SStephen Rothwell {
9664a22f166SStephen Rothwell return task_pid_vnr(current);
9674a22f166SStephen Rothwell }
9684a22f166SStephen Rothwell
9694a22f166SStephen Rothwell /*
9704a22f166SStephen Rothwell * Accessing ->real_parent is not SMP-safe, it could
9714a22f166SStephen Rothwell * change from under us. However, we can use a stale
9724a22f166SStephen Rothwell * value of ->real_parent under rcu_read_lock(), see
9734a22f166SStephen Rothwell * release_task()->call_rcu(delayed_put_task_struct).
9744a22f166SStephen Rothwell */
SYSCALL_DEFINE0(getppid)9754a22f166SStephen Rothwell SYSCALL_DEFINE0(getppid)
9764a22f166SStephen Rothwell {
9774a22f166SStephen Rothwell int pid;
9784a22f166SStephen Rothwell
9794a22f166SStephen Rothwell rcu_read_lock();
9804a22f166SStephen Rothwell pid = task_tgid_vnr(rcu_dereference(current->real_parent));
9814a22f166SStephen Rothwell rcu_read_unlock();
9824a22f166SStephen Rothwell
9834a22f166SStephen Rothwell return pid;
9844a22f166SStephen Rothwell }
9854a22f166SStephen Rothwell
SYSCALL_DEFINE0(getuid)9864a22f166SStephen Rothwell SYSCALL_DEFINE0(getuid)
9874a22f166SStephen Rothwell {
9884a22f166SStephen Rothwell /* Only we change this so SMP safe */
9894a22f166SStephen Rothwell return from_kuid_munged(current_user_ns(), current_uid());
9904a22f166SStephen Rothwell }
9914a22f166SStephen Rothwell
SYSCALL_DEFINE0(geteuid)9924a22f166SStephen Rothwell SYSCALL_DEFINE0(geteuid)
9934a22f166SStephen Rothwell {
9944a22f166SStephen Rothwell /* Only we change this so SMP safe */
9954a22f166SStephen Rothwell return from_kuid_munged(current_user_ns(), current_euid());
9964a22f166SStephen Rothwell }
9974a22f166SStephen Rothwell
SYSCALL_DEFINE0(getgid)9984a22f166SStephen Rothwell SYSCALL_DEFINE0(getgid)
9994a22f166SStephen Rothwell {
10004a22f166SStephen Rothwell /* Only we change this so SMP safe */
10014a22f166SStephen Rothwell return from_kgid_munged(current_user_ns(), current_gid());
10024a22f166SStephen Rothwell }
10034a22f166SStephen Rothwell
SYSCALL_DEFINE0(getegid)10044a22f166SStephen Rothwell SYSCALL_DEFINE0(getegid)
10054a22f166SStephen Rothwell {
10064a22f166SStephen Rothwell /* Only we change this so SMP safe */
10074a22f166SStephen Rothwell return from_kgid_munged(current_user_ns(), current_egid());
10084a22f166SStephen Rothwell }
10094a22f166SStephen Rothwell
do_sys_times(struct tms * tms)1010ca2406edSAl Viro static void do_sys_times(struct tms *tms)
1011f06febc9SFrank Mayhar {
10125613fda9SFrederic Weisbecker u64 tgutime, tgstime, cutime, cstime;
1013f06febc9SFrank Mayhar
1014e80d0a1aSFrederic Weisbecker thread_group_cputime_adjusted(current, &tgutime, &tgstime);
1015f06febc9SFrank Mayhar cutime = current->signal->cutime;
1016f06febc9SFrank Mayhar cstime = current->signal->cstime;
10175613fda9SFrederic Weisbecker tms->tms_utime = nsec_to_clock_t(tgutime);
10185613fda9SFrederic Weisbecker tms->tms_stime = nsec_to_clock_t(tgstime);
10195613fda9SFrederic Weisbecker tms->tms_cutime = nsec_to_clock_t(cutime);
10205613fda9SFrederic Weisbecker tms->tms_cstime = nsec_to_clock_t(cstime);
1021f06febc9SFrank Mayhar }
1022f06febc9SFrank Mayhar
SYSCALL_DEFINE1(times,struct tms __user *,tbuf)102358fd3aa2SHeiko Carstens SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
10241da177e4SLinus Torvalds {
10251da177e4SLinus Torvalds if (tbuf) {
10261da177e4SLinus Torvalds struct tms tmp;
10271da177e4SLinus Torvalds
1028f06febc9SFrank Mayhar do_sys_times(&tmp);
10291da177e4SLinus Torvalds if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
10301da177e4SLinus Torvalds return -EFAULT;
10311da177e4SLinus Torvalds }
1032e3d5a27dSPaul Mackerras force_successful_syscall_return();
10331da177e4SLinus Torvalds return (long) jiffies_64_to_clock_t(get_jiffies_64());
10341da177e4SLinus Torvalds }
10351da177e4SLinus Torvalds
1036ca2406edSAl Viro #ifdef CONFIG_COMPAT
clock_t_to_compat_clock_t(clock_t x)1037ca2406edSAl Viro static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
1038ca2406edSAl Viro {
1039ca2406edSAl Viro return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
1040ca2406edSAl Viro }
1041ca2406edSAl Viro
COMPAT_SYSCALL_DEFINE1(times,struct compat_tms __user *,tbuf)1042ca2406edSAl Viro COMPAT_SYSCALL_DEFINE1(times, struct compat_tms __user *, tbuf)
1043ca2406edSAl Viro {
1044ca2406edSAl Viro if (tbuf) {
1045ca2406edSAl Viro struct tms tms;
1046ca2406edSAl Viro struct compat_tms tmp;
1047ca2406edSAl Viro
1048ca2406edSAl Viro do_sys_times(&tms);
1049ca2406edSAl Viro /* Convert our struct tms to the compat version. */
1050ca2406edSAl Viro tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
1051ca2406edSAl Viro tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
1052ca2406edSAl Viro tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
1053ca2406edSAl Viro tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
1054ca2406edSAl Viro if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
1055ca2406edSAl Viro return -EFAULT;
1056ca2406edSAl Viro }
1057ca2406edSAl Viro force_successful_syscall_return();
1058ca2406edSAl Viro return compat_jiffies_to_clock_t(jiffies);
1059ca2406edSAl Viro }
1060ca2406edSAl Viro #endif
1061ca2406edSAl Viro
10621da177e4SLinus Torvalds /*
10631da177e4SLinus Torvalds * This needs some heavy checking ...
10641da177e4SLinus Torvalds * I just haven't the stomach for it. I also don't fully
10651da177e4SLinus Torvalds * understand sessions/pgrp etc. Let somebody who does explain it.
10661da177e4SLinus Torvalds *
10671da177e4SLinus Torvalds * OK, I think I have the protection semantics right.... this is really
10681da177e4SLinus Torvalds * only important on a multi-user system anyway, to make sure one user
10691da177e4SLinus Torvalds * can't send a signal to a process owned by another. -TYT, 12/12/91
10701da177e4SLinus Torvalds *
107198611e4eSOleg Nesterov * !PF_FORKNOEXEC check to conform completely to POSIX.
10721da177e4SLinus Torvalds */
SYSCALL_DEFINE2(setpgid,pid_t,pid,pid_t,pgid)1073b290ebe2SHeiko Carstens SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
10741da177e4SLinus Torvalds {
10751da177e4SLinus Torvalds struct task_struct *p;
1076ee0acf90SOleg Nesterov struct task_struct *group_leader = current->group_leader;
10774e021306SOleg Nesterov struct pid *pgrp;
10784e021306SOleg Nesterov int err;
10791da177e4SLinus Torvalds
10801da177e4SLinus Torvalds if (!pid)
1081b488893aSPavel Emelyanov pid = task_pid_vnr(group_leader);
10821da177e4SLinus Torvalds if (!pgid)
10831da177e4SLinus Torvalds pgid = pid;
10841da177e4SLinus Torvalds if (pgid < 0)
10851da177e4SLinus Torvalds return -EINVAL;
1086950eaacaSPaul E. McKenney rcu_read_lock();
10871da177e4SLinus Torvalds
10881da177e4SLinus Torvalds /* From this point forward we keep holding onto the tasklist lock
10891da177e4SLinus Torvalds * so that our parent does not change from under us. -DaveM
10901da177e4SLinus Torvalds */
10911da177e4SLinus Torvalds write_lock_irq(&tasklist_lock);
10921da177e4SLinus Torvalds
10931da177e4SLinus Torvalds err = -ESRCH;
10944e021306SOleg Nesterov p = find_task_by_vpid(pid);
10951da177e4SLinus Torvalds if (!p)
10961da177e4SLinus Torvalds goto out;
10971da177e4SLinus Torvalds
10981da177e4SLinus Torvalds err = -EINVAL;
10991da177e4SLinus Torvalds if (!thread_group_leader(p))
11001da177e4SLinus Torvalds goto out;
11011da177e4SLinus Torvalds
11024e021306SOleg Nesterov if (same_thread_group(p->real_parent, group_leader)) {
11031da177e4SLinus Torvalds err = -EPERM;
110441487c65SEric W. Biederman if (task_session(p) != task_session(group_leader))
11051da177e4SLinus Torvalds goto out;
11061da177e4SLinus Torvalds err = -EACCES;
110798611e4eSOleg Nesterov if (!(p->flags & PF_FORKNOEXEC))
11081da177e4SLinus Torvalds goto out;
11091da177e4SLinus Torvalds } else {
11101da177e4SLinus Torvalds err = -ESRCH;
1111ee0acf90SOleg Nesterov if (p != group_leader)
11121da177e4SLinus Torvalds goto out;
11131da177e4SLinus Torvalds }
11141da177e4SLinus Torvalds
11151da177e4SLinus Torvalds err = -EPERM;
11161da177e4SLinus Torvalds if (p->signal->leader)
11171da177e4SLinus Torvalds goto out;
11181da177e4SLinus Torvalds
11194e021306SOleg Nesterov pgrp = task_pid(p);
11201da177e4SLinus Torvalds if (pgid != pid) {
1121b488893aSPavel Emelyanov struct task_struct *g;
11221da177e4SLinus Torvalds
11234e021306SOleg Nesterov pgrp = find_vpid(pgid);
11244e021306SOleg Nesterov g = pid_task(pgrp, PIDTYPE_PGID);
112541487c65SEric W. Biederman if (!g || task_session(g) != task_session(group_leader))
11261da177e4SLinus Torvalds goto out;
11271da177e4SLinus Torvalds }
11281da177e4SLinus Torvalds
11291da177e4SLinus Torvalds err = security_task_setpgid(p, pgid);
11301da177e4SLinus Torvalds if (err)
11311da177e4SLinus Torvalds goto out;
11321da177e4SLinus Torvalds
11331b0f7ffdSOleg Nesterov if (task_pgrp(p) != pgrp)
113483beaf3cSOleg Nesterov change_pid(p, PIDTYPE_PGID, pgrp);
11351da177e4SLinus Torvalds
11361da177e4SLinus Torvalds err = 0;
11371da177e4SLinus Torvalds out:
11381da177e4SLinus Torvalds /* All paths lead to here, thus we are safe. -DaveM */
11391da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock);
1140950eaacaSPaul E. McKenney rcu_read_unlock();
11411da177e4SLinus Torvalds return err;
11421da177e4SLinus Torvalds }
11431da177e4SLinus Torvalds
do_getpgid(pid_t pid)1144192c5807SDominik Brodowski static int do_getpgid(pid_t pid)
11451da177e4SLinus Torvalds {
11461da177e4SLinus Torvalds struct task_struct *p;
114712a3de0aSOleg Nesterov struct pid *grp;
114812a3de0aSOleg Nesterov int retval;
11491da177e4SLinus Torvalds
115012a3de0aSOleg Nesterov rcu_read_lock();
115112a3de0aSOleg Nesterov if (!pid)
115212a3de0aSOleg Nesterov grp = task_pgrp(current);
115312a3de0aSOleg Nesterov else {
11541da177e4SLinus Torvalds retval = -ESRCH;
115512a3de0aSOleg Nesterov p = find_task_by_vpid(pid);
115612a3de0aSOleg Nesterov if (!p)
115712a3de0aSOleg Nesterov goto out;
115812a3de0aSOleg Nesterov grp = task_pgrp(p);
115912a3de0aSOleg Nesterov if (!grp)
116012a3de0aSOleg Nesterov goto out;
116112a3de0aSOleg Nesterov
11621da177e4SLinus Torvalds retval = security_task_getpgid(p);
116312a3de0aSOleg Nesterov if (retval)
116412a3de0aSOleg Nesterov goto out;
11651da177e4SLinus Torvalds }
116612a3de0aSOleg Nesterov retval = pid_vnr(grp);
116712a3de0aSOleg Nesterov out:
116812a3de0aSOleg Nesterov rcu_read_unlock();
11691da177e4SLinus Torvalds return retval;
11701da177e4SLinus Torvalds }
11711da177e4SLinus Torvalds
SYSCALL_DEFINE1(getpgid,pid_t,pid)1172192c5807SDominik Brodowski SYSCALL_DEFINE1(getpgid, pid_t, pid)
1173192c5807SDominik Brodowski {
1174192c5807SDominik Brodowski return do_getpgid(pid);
1175192c5807SDominik Brodowski }
1176192c5807SDominik Brodowski
11771da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_GETPGRP
11781da177e4SLinus Torvalds
SYSCALL_DEFINE0(getpgrp)1179dbf040d9SHeiko Carstens SYSCALL_DEFINE0(getpgrp)
11801da177e4SLinus Torvalds {
1181192c5807SDominik Brodowski return do_getpgid(0);
11821da177e4SLinus Torvalds }
11831da177e4SLinus Torvalds
11841da177e4SLinus Torvalds #endif
11851da177e4SLinus Torvalds
SYSCALL_DEFINE1(getsid,pid_t,pid)1186dbf040d9SHeiko Carstens SYSCALL_DEFINE1(getsid, pid_t, pid)
11871da177e4SLinus Torvalds {
11881da177e4SLinus Torvalds struct task_struct *p;
11891dd768c0SOleg Nesterov struct pid *sid;
11901dd768c0SOleg Nesterov int retval;
1191b488893aSPavel Emelyanov
1192ac9a8e3fSOleg Nesterov rcu_read_lock();
11931dd768c0SOleg Nesterov if (!pid)
11941dd768c0SOleg Nesterov sid = task_session(current);
11951dd768c0SOleg Nesterov else {
11961da177e4SLinus Torvalds retval = -ESRCH;
11971dd768c0SOleg Nesterov p = find_task_by_vpid(pid);
11981dd768c0SOleg Nesterov if (!p)
11991dd768c0SOleg Nesterov goto out;
12001dd768c0SOleg Nesterov sid = task_session(p);
12011dd768c0SOleg Nesterov if (!sid)
12021dd768c0SOleg Nesterov goto out;
12031dd768c0SOleg Nesterov
12041da177e4SLinus Torvalds retval = security_task_getsid(p);
12051dd768c0SOleg Nesterov if (retval)
12061dd768c0SOleg Nesterov goto out;
12071da177e4SLinus Torvalds }
12081dd768c0SOleg Nesterov retval = pid_vnr(sid);
12091dd768c0SOleg Nesterov out:
1210ac9a8e3fSOleg Nesterov rcu_read_unlock();
12111da177e4SLinus Torvalds return retval;
12121da177e4SLinus Torvalds }
12131da177e4SLinus Torvalds
set_special_pids(struct pid * pid)121481dabb46SOleg Nesterov static void set_special_pids(struct pid *pid)
121581dabb46SOleg Nesterov {
121681dabb46SOleg Nesterov struct task_struct *curr = current->group_leader;
121781dabb46SOleg Nesterov
121881dabb46SOleg Nesterov if (task_session(curr) != pid)
121981dabb46SOleg Nesterov change_pid(curr, PIDTYPE_SID, pid);
122081dabb46SOleg Nesterov
122181dabb46SOleg Nesterov if (task_pgrp(curr) != pid)
122281dabb46SOleg Nesterov change_pid(curr, PIDTYPE_PGID, pid);
122381dabb46SOleg Nesterov }
122481dabb46SOleg Nesterov
ksys_setsid(void)1225e2aaa9f4SDominik Brodowski int ksys_setsid(void)
12261da177e4SLinus Torvalds {
1227e19f247aSOren Laadan struct task_struct *group_leader = current->group_leader;
1228e4cc0a9cSOleg Nesterov struct pid *sid = task_pid(group_leader);
1229e4cc0a9cSOleg Nesterov pid_t session = pid_vnr(sid);
12301da177e4SLinus Torvalds int err = -EPERM;
12311da177e4SLinus Torvalds
12321da177e4SLinus Torvalds write_lock_irq(&tasklist_lock);
1233390e2ff0SEric W. Biederman /* Fail if I am already a session leader */
1234390e2ff0SEric W. Biederman if (group_leader->signal->leader)
1235390e2ff0SEric W. Biederman goto out;
1236390e2ff0SEric W. Biederman
1237430c6231SOleg Nesterov /* Fail if a process group id already exists that equals the
1238430c6231SOleg Nesterov * proposed session id.
1239390e2ff0SEric W. Biederman */
12406806aac6SOleg Nesterov if (pid_task(sid, PIDTYPE_PGID))
12411da177e4SLinus Torvalds goto out;
12421da177e4SLinus Torvalds
1243e19f247aSOren Laadan group_leader->signal->leader = 1;
124481dabb46SOleg Nesterov set_special_pids(sid);
124524ec839cSPeter Zijlstra
12469c9f4dedSAlan Cox proc_clear_tty(group_leader);
124724ec839cSPeter Zijlstra
1248e4cc0a9cSOleg Nesterov err = session;
12491da177e4SLinus Torvalds out:
12501da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock);
12515091faa4SMike Galbraith if (err > 0) {
12520d0df599SChristian Borntraeger proc_sid_connector(group_leader);
12535091faa4SMike Galbraith sched_autogroup_create_attach(group_leader);
12545091faa4SMike Galbraith }
12551da177e4SLinus Torvalds return err;
12561da177e4SLinus Torvalds }
12571da177e4SLinus Torvalds
SYSCALL_DEFINE0(setsid)1258e2aaa9f4SDominik Brodowski SYSCALL_DEFINE0(setsid)
1259e2aaa9f4SDominik Brodowski {
1260e2aaa9f4SDominik Brodowski return ksys_setsid();
1261e2aaa9f4SDominik Brodowski }
1262e2aaa9f4SDominik Brodowski
12631da177e4SLinus Torvalds DECLARE_RWSEM(uts_sem);
12641da177e4SLinus Torvalds
1265e28cbf22SChristoph Hellwig #ifdef COMPAT_UTS_MACHINE
1266e28cbf22SChristoph Hellwig #define override_architecture(name) \
126746da2766SAndreas Schwab (personality(current->personality) == PER_LINUX32 && \
1268e28cbf22SChristoph Hellwig copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1269e28cbf22SChristoph Hellwig sizeof(COMPAT_UTS_MACHINE)))
1270e28cbf22SChristoph Hellwig #else
1271e28cbf22SChristoph Hellwig #define override_architecture(name) 0
1272e28cbf22SChristoph Hellwig #endif
1273e28cbf22SChristoph Hellwig
1274be27425dSAndi Kleen /*
1275be27425dSAndi Kleen * Work around broken programs that cannot handle "Linux 3.0".
1276be27425dSAndi Kleen * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40
1277b7285b42SJonathan Neuschäfer * And we map 4.x and later versions to 2.6.60+x, so 4.0/5.0/6.0/... would be
1278b7285b42SJonathan Neuschäfer * 2.6.60.
1279be27425dSAndi Kleen */
override_release(char __user * release,size_t len)12802702b152SKees Cook static int override_release(char __user *release, size_t len)
1281be27425dSAndi Kleen {
1282be27425dSAndi Kleen int ret = 0;
1283be27425dSAndi Kleen
1284be27425dSAndi Kleen if (current->personality & UNAME26) {
12852702b152SKees Cook const char *rest = UTS_RELEASE;
12862702b152SKees Cook char buf[65] = { 0 };
1287be27425dSAndi Kleen int ndots = 0;
1288be27425dSAndi Kleen unsigned v;
12892702b152SKees Cook size_t copy;
1290be27425dSAndi Kleen
1291be27425dSAndi Kleen while (*rest) {
1292be27425dSAndi Kleen if (*rest == '.' && ++ndots >= 3)
1293be27425dSAndi Kleen break;
1294be27425dSAndi Kleen if (!isdigit(*rest) && *rest != '.')
1295be27425dSAndi Kleen break;
1296be27425dSAndi Kleen rest++;
1297be27425dSAndi Kleen }
129888a68672SSasha Levin v = LINUX_VERSION_PATCHLEVEL + 60;
129931fd84b9SKees Cook copy = clamp_t(size_t, len, 1, sizeof(buf));
13002702b152SKees Cook copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
13012702b152SKees Cook ret = copy_to_user(release, buf, copy + 1);
1302be27425dSAndi Kleen }
1303be27425dSAndi Kleen return ret;
1304be27425dSAndi Kleen }
1305be27425dSAndi Kleen
SYSCALL_DEFINE1(newuname,struct new_utsname __user *,name)1306e48fbb69SHeiko Carstens SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
13071da177e4SLinus Torvalds {
130842a0cc34SJann Horn struct new_utsname tmp;
13091da177e4SLinus Torvalds
13101da177e4SLinus Torvalds down_read(&uts_sem);
131142a0cc34SJann Horn memcpy(&tmp, utsname(), sizeof(tmp));
13121da177e4SLinus Torvalds up_read(&uts_sem);
131342a0cc34SJann Horn if (copy_to_user(name, &tmp, sizeof(tmp)))
131442a0cc34SJann Horn return -EFAULT;
1315e28cbf22SChristoph Hellwig
131642a0cc34SJann Horn if (override_release(name->release, sizeof(name->release)))
131742a0cc34SJann Horn return -EFAULT;
131842a0cc34SJann Horn if (override_architecture(name))
131942a0cc34SJann Horn return -EFAULT;
132042a0cc34SJann Horn return 0;
13211da177e4SLinus Torvalds }
13221da177e4SLinus Torvalds
13235cacdb4aSChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_UNAME
13245cacdb4aSChristoph Hellwig /*
13255cacdb4aSChristoph Hellwig * Old cruft
13265cacdb4aSChristoph Hellwig */
SYSCALL_DEFINE1(uname,struct old_utsname __user *,name)13275cacdb4aSChristoph Hellwig SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
13285cacdb4aSChristoph Hellwig {
132942a0cc34SJann Horn struct old_utsname tmp;
13305cacdb4aSChristoph Hellwig
13315cacdb4aSChristoph Hellwig if (!name)
13325cacdb4aSChristoph Hellwig return -EFAULT;
13335cacdb4aSChristoph Hellwig
13345cacdb4aSChristoph Hellwig down_read(&uts_sem);
133542a0cc34SJann Horn memcpy(&tmp, utsname(), sizeof(tmp));
13365cacdb4aSChristoph Hellwig up_read(&uts_sem);
133742a0cc34SJann Horn if (copy_to_user(name, &tmp, sizeof(tmp)))
133842a0cc34SJann Horn return -EFAULT;
13395cacdb4aSChristoph Hellwig
134042a0cc34SJann Horn if (override_release(name->release, sizeof(name->release)))
134142a0cc34SJann Horn return -EFAULT;
134242a0cc34SJann Horn if (override_architecture(name))
134342a0cc34SJann Horn return -EFAULT;
134442a0cc34SJann Horn return 0;
13455cacdb4aSChristoph Hellwig }
13465cacdb4aSChristoph Hellwig
SYSCALL_DEFINE1(olduname,struct oldold_utsname __user *,name)13475cacdb4aSChristoph Hellwig SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
13485cacdb4aSChristoph Hellwig {
13495e1aada0SJoe Perches struct oldold_utsname tmp;
13505cacdb4aSChristoph Hellwig
13515cacdb4aSChristoph Hellwig if (!name)
13525cacdb4aSChristoph Hellwig return -EFAULT;
13535cacdb4aSChristoph Hellwig
13545e1aada0SJoe Perches memset(&tmp, 0, sizeof(tmp));
13555e1aada0SJoe Perches
13565cacdb4aSChristoph Hellwig down_read(&uts_sem);
135742a0cc34SJann Horn memcpy(&tmp.sysname, &utsname()->sysname, __OLD_UTS_LEN);
135842a0cc34SJann Horn memcpy(&tmp.nodename, &utsname()->nodename, __OLD_UTS_LEN);
135942a0cc34SJann Horn memcpy(&tmp.release, &utsname()->release, __OLD_UTS_LEN);
136042a0cc34SJann Horn memcpy(&tmp.version, &utsname()->version, __OLD_UTS_LEN);
136142a0cc34SJann Horn memcpy(&tmp.machine, &utsname()->machine, __OLD_UTS_LEN);
13625cacdb4aSChristoph Hellwig up_read(&uts_sem);
136342a0cc34SJann Horn if (copy_to_user(name, &tmp, sizeof(tmp)))
136442a0cc34SJann Horn return -EFAULT;
13655cacdb4aSChristoph Hellwig
136642a0cc34SJann Horn if (override_architecture(name))
136742a0cc34SJann Horn return -EFAULT;
136842a0cc34SJann Horn if (override_release(name->release, sizeof(name->release)))
136942a0cc34SJann Horn return -EFAULT;
137042a0cc34SJann Horn return 0;
13715cacdb4aSChristoph Hellwig }
13725cacdb4aSChristoph Hellwig #endif
13735cacdb4aSChristoph Hellwig
SYSCALL_DEFINE2(sethostname,char __user *,name,int,len)13745a8a82b1SHeiko Carstens SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
13751da177e4SLinus Torvalds {
13761da177e4SLinus Torvalds int errno;
13771da177e4SLinus Torvalds char tmp[__NEW_UTS_LEN];
13781da177e4SLinus Torvalds
1379bb96a6f5SSerge E. Hallyn if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
13801da177e4SLinus Torvalds return -EPERM;
1381fc832ad3SSerge E. Hallyn
13821da177e4SLinus Torvalds if (len < 0 || len > __NEW_UTS_LEN)
13831da177e4SLinus Torvalds return -EINVAL;
13841da177e4SLinus Torvalds errno = -EFAULT;
13851da177e4SLinus Torvalds if (!copy_from_user(tmp, name, len)) {
138642a0cc34SJann Horn struct new_utsname *u;
13879679e4ddSAndrew Morton
138837608ba3SJason A. Donenfeld add_device_randomness(tmp, len);
138942a0cc34SJann Horn down_write(&uts_sem);
139042a0cc34SJann Horn u = utsname();
13919679e4ddSAndrew Morton memcpy(u->nodename, tmp, len);
13929679e4ddSAndrew Morton memset(u->nodename + len, 0, sizeof(u->nodename) - len);
13931da177e4SLinus Torvalds errno = 0;
1394f1ecf068SLucas De Marchi uts_proc_notify(UTS_PROC_HOSTNAME);
13951da177e4SLinus Torvalds up_write(&uts_sem);
139642a0cc34SJann Horn }
13971da177e4SLinus Torvalds return errno;
13981da177e4SLinus Torvalds }
13991da177e4SLinus Torvalds
14001da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_GETHOSTNAME
14011da177e4SLinus Torvalds
SYSCALL_DEFINE2(gethostname,char __user *,name,int,len)14025a8a82b1SHeiko Carstens SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
14031da177e4SLinus Torvalds {
140442a0cc34SJann Horn int i;
14059679e4ddSAndrew Morton struct new_utsname *u;
140642a0cc34SJann Horn char tmp[__NEW_UTS_LEN + 1];
14071da177e4SLinus Torvalds
14081da177e4SLinus Torvalds if (len < 0)
14091da177e4SLinus Torvalds return -EINVAL;
14101da177e4SLinus Torvalds down_read(&uts_sem);
14119679e4ddSAndrew Morton u = utsname();
14129679e4ddSAndrew Morton i = 1 + strlen(u->nodename);
14131da177e4SLinus Torvalds if (i > len)
14141da177e4SLinus Torvalds i = len;
141542a0cc34SJann Horn memcpy(tmp, u->nodename, i);
14161da177e4SLinus Torvalds up_read(&uts_sem);
141742a0cc34SJann Horn if (copy_to_user(name, tmp, i))
141842a0cc34SJann Horn return -EFAULT;
141942a0cc34SJann Horn return 0;
14201da177e4SLinus Torvalds }
14211da177e4SLinus Torvalds
14221da177e4SLinus Torvalds #endif
14231da177e4SLinus Torvalds
14241da177e4SLinus Torvalds /*
14251da177e4SLinus Torvalds * Only setdomainname; getdomainname can be implemented by calling
14261da177e4SLinus Torvalds * uname()
14271da177e4SLinus Torvalds */
SYSCALL_DEFINE2(setdomainname,char __user *,name,int,len)14285a8a82b1SHeiko Carstens SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
14291da177e4SLinus Torvalds {
14301da177e4SLinus Torvalds int errno;
14311da177e4SLinus Torvalds char tmp[__NEW_UTS_LEN];
14321da177e4SLinus Torvalds
1433fc832ad3SSerge E. Hallyn if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
14341da177e4SLinus Torvalds return -EPERM;
14351da177e4SLinus Torvalds if (len < 0 || len > __NEW_UTS_LEN)
14361da177e4SLinus Torvalds return -EINVAL;
14371da177e4SLinus Torvalds
14381da177e4SLinus Torvalds errno = -EFAULT;
14391da177e4SLinus Torvalds if (!copy_from_user(tmp, name, len)) {
144042a0cc34SJann Horn struct new_utsname *u;
14419679e4ddSAndrew Morton
144237608ba3SJason A. Donenfeld add_device_randomness(tmp, len);
144342a0cc34SJann Horn down_write(&uts_sem);
144442a0cc34SJann Horn u = utsname();
14459679e4ddSAndrew Morton memcpy(u->domainname, tmp, len);
14469679e4ddSAndrew Morton memset(u->domainname + len, 0, sizeof(u->domainname) - len);
14471da177e4SLinus Torvalds errno = 0;
1448f1ecf068SLucas De Marchi uts_proc_notify(UTS_PROC_DOMAINNAME);
14491da177e4SLinus Torvalds up_write(&uts_sem);
145042a0cc34SJann Horn }
14511da177e4SLinus Torvalds return errno;
14521da177e4SLinus Torvalds }
14531da177e4SLinus Torvalds
1454c57bef02SBarret Rhoden /* make sure you are allowed to change @tsk limits before calling this */
do_prlimit(struct task_struct * tsk,unsigned int resource,struct rlimit * new_rlim,struct rlimit * old_rlim)1455c57bef02SBarret Rhoden static int do_prlimit(struct task_struct *tsk, unsigned int resource,
1456c57bef02SBarret Rhoden struct rlimit *new_rlim, struct rlimit *old_rlim)
1457c57bef02SBarret Rhoden {
1458c57bef02SBarret Rhoden struct rlimit *rlim;
1459c57bef02SBarret Rhoden int retval = 0;
1460c57bef02SBarret Rhoden
1461c57bef02SBarret Rhoden if (resource >= RLIM_NLIMITS)
1462c57bef02SBarret Rhoden return -EINVAL;
146373979060SGreg Kroah-Hartman resource = array_index_nospec(resource, RLIM_NLIMITS);
146473979060SGreg Kroah-Hartman
1465c57bef02SBarret Rhoden if (new_rlim) {
1466c57bef02SBarret Rhoden if (new_rlim->rlim_cur > new_rlim->rlim_max)
1467c57bef02SBarret Rhoden return -EINVAL;
1468c57bef02SBarret Rhoden if (resource == RLIMIT_NOFILE &&
1469c57bef02SBarret Rhoden new_rlim->rlim_max > sysctl_nr_open)
1470c57bef02SBarret Rhoden return -EPERM;
1471c57bef02SBarret Rhoden }
1472c57bef02SBarret Rhoden
147318c91bb2SBarret Rhoden /* Holding a refcount on tsk protects tsk->signal from disappearing. */
1474c57bef02SBarret Rhoden rlim = tsk->signal->rlim + resource;
1475c57bef02SBarret Rhoden task_lock(tsk->group_leader);
1476c57bef02SBarret Rhoden if (new_rlim) {
1477c57bef02SBarret Rhoden /*
1478c57bef02SBarret Rhoden * Keep the capable check against init_user_ns until cgroups can
1479c57bef02SBarret Rhoden * contain all limits.
1480c57bef02SBarret Rhoden */
1481c57bef02SBarret Rhoden if (new_rlim->rlim_max > rlim->rlim_max &&
1482c57bef02SBarret Rhoden !capable(CAP_SYS_RESOURCE))
1483c57bef02SBarret Rhoden retval = -EPERM;
1484c57bef02SBarret Rhoden if (!retval)
1485c57bef02SBarret Rhoden retval = security_task_setrlimit(tsk, resource, new_rlim);
1486c57bef02SBarret Rhoden }
1487c57bef02SBarret Rhoden if (!retval) {
1488c57bef02SBarret Rhoden if (old_rlim)
1489c57bef02SBarret Rhoden *old_rlim = *rlim;
1490c57bef02SBarret Rhoden if (new_rlim)
1491c57bef02SBarret Rhoden *rlim = *new_rlim;
1492c57bef02SBarret Rhoden }
1493c57bef02SBarret Rhoden task_unlock(tsk->group_leader);
1494c57bef02SBarret Rhoden
1495c57bef02SBarret Rhoden /*
1496c57bef02SBarret Rhoden * RLIMIT_CPU handling. Arm the posix CPU timer if the limit is not
1497c57bef02SBarret Rhoden * infinite. In case of RLIM_INFINITY the posix CPU timer code
1498c57bef02SBarret Rhoden * ignores the rlimit.
1499c57bef02SBarret Rhoden */
1500c57bef02SBarret Rhoden if (!retval && new_rlim && resource == RLIMIT_CPU &&
1501c57bef02SBarret Rhoden new_rlim->rlim_cur != RLIM_INFINITY &&
150218c91bb2SBarret Rhoden IS_ENABLED(CONFIG_POSIX_TIMERS)) {
150318c91bb2SBarret Rhoden /*
150418c91bb2SBarret Rhoden * update_rlimit_cpu can fail if the task is exiting, but there
150518c91bb2SBarret Rhoden * may be other tasks in the thread group that are not exiting,
150618c91bb2SBarret Rhoden * and they need their cpu timers adjusted.
150718c91bb2SBarret Rhoden *
150818c91bb2SBarret Rhoden * The group_leader is the last task to be released, so if we
150918c91bb2SBarret Rhoden * cannot update_rlimit_cpu on it, then the entire process is
151018c91bb2SBarret Rhoden * exiting and we do not need to update at all.
151118c91bb2SBarret Rhoden */
151218c91bb2SBarret Rhoden update_rlimit_cpu(tsk->group_leader, new_rlim->rlim_cur);
151318c91bb2SBarret Rhoden }
151418c91bb2SBarret Rhoden
1515c57bef02SBarret Rhoden return retval;
1516c57bef02SBarret Rhoden }
1517c57bef02SBarret Rhoden
SYSCALL_DEFINE2(getrlimit,unsigned int,resource,struct rlimit __user *,rlim)1518e48fbb69SHeiko Carstens SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
15191da177e4SLinus Torvalds {
15201da177e4SLinus Torvalds struct rlimit value;
1521b9518345SJiri Slaby int ret;
1522b9518345SJiri Slaby
1523b9518345SJiri Slaby ret = do_prlimit(current, resource, NULL, &value);
1524b9518345SJiri Slaby if (!ret)
1525b9518345SJiri Slaby ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1526b9518345SJiri Slaby
1527b9518345SJiri Slaby return ret;
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds
1530d9e968cbSAl Viro #ifdef CONFIG_COMPAT
1531d9e968cbSAl Viro
COMPAT_SYSCALL_DEFINE2(setrlimit,unsigned int,resource,struct compat_rlimit __user *,rlim)1532d9e968cbSAl Viro COMPAT_SYSCALL_DEFINE2(setrlimit, unsigned int, resource,
1533d9e968cbSAl Viro struct compat_rlimit __user *, rlim)
1534d9e968cbSAl Viro {
1535d9e968cbSAl Viro struct rlimit r;
1536d9e968cbSAl Viro struct compat_rlimit r32;
1537d9e968cbSAl Viro
1538d9e968cbSAl Viro if (copy_from_user(&r32, rlim, sizeof(struct compat_rlimit)))
1539d9e968cbSAl Viro return -EFAULT;
1540d9e968cbSAl Viro
1541d9e968cbSAl Viro if (r32.rlim_cur == COMPAT_RLIM_INFINITY)
1542d9e968cbSAl Viro r.rlim_cur = RLIM_INFINITY;
1543d9e968cbSAl Viro else
1544d9e968cbSAl Viro r.rlim_cur = r32.rlim_cur;
1545d9e968cbSAl Viro if (r32.rlim_max == COMPAT_RLIM_INFINITY)
1546d9e968cbSAl Viro r.rlim_max = RLIM_INFINITY;
1547d9e968cbSAl Viro else
1548d9e968cbSAl Viro r.rlim_max = r32.rlim_max;
1549d9e968cbSAl Viro return do_prlimit(current, resource, &r, NULL);
1550d9e968cbSAl Viro }
1551d9e968cbSAl Viro
COMPAT_SYSCALL_DEFINE2(getrlimit,unsigned int,resource,struct compat_rlimit __user *,rlim)1552d9e968cbSAl Viro COMPAT_SYSCALL_DEFINE2(getrlimit, unsigned int, resource,
1553d9e968cbSAl Viro struct compat_rlimit __user *, rlim)
1554d9e968cbSAl Viro {
1555d9e968cbSAl Viro struct rlimit r;
1556d9e968cbSAl Viro int ret;
1557d9e968cbSAl Viro
1558d9e968cbSAl Viro ret = do_prlimit(current, resource, NULL, &r);
1559d9e968cbSAl Viro if (!ret) {
156058c7ffc0SAl Viro struct compat_rlimit r32;
1561d9e968cbSAl Viro if (r.rlim_cur > COMPAT_RLIM_INFINITY)
1562d9e968cbSAl Viro r32.rlim_cur = COMPAT_RLIM_INFINITY;
1563d9e968cbSAl Viro else
1564d9e968cbSAl Viro r32.rlim_cur = r.rlim_cur;
1565d9e968cbSAl Viro if (r.rlim_max > COMPAT_RLIM_INFINITY)
1566d9e968cbSAl Viro r32.rlim_max = COMPAT_RLIM_INFINITY;
1567d9e968cbSAl Viro else
1568d9e968cbSAl Viro r32.rlim_max = r.rlim_max;
1569d9e968cbSAl Viro
1570d9e968cbSAl Viro if (copy_to_user(rlim, &r32, sizeof(struct compat_rlimit)))
1571d9e968cbSAl Viro return -EFAULT;
1572d9e968cbSAl Viro }
1573d9e968cbSAl Viro return ret;
1574d9e968cbSAl Viro }
1575d9e968cbSAl Viro
1576d9e968cbSAl Viro #endif
1577d9e968cbSAl Viro
15781da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
15791da177e4SLinus Torvalds
15801da177e4SLinus Torvalds /*
15811da177e4SLinus Torvalds * Back compatibility for getrlimit. Needed for some apps.
15821da177e4SLinus Torvalds */
SYSCALL_DEFINE2(old_getrlimit,unsigned int,resource,struct rlimit __user *,rlim)1583e48fbb69SHeiko Carstens SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1584e48fbb69SHeiko Carstens struct rlimit __user *, rlim)
15851da177e4SLinus Torvalds {
15861da177e4SLinus Torvalds struct rlimit x;
15871da177e4SLinus Torvalds if (resource >= RLIM_NLIMITS)
15881da177e4SLinus Torvalds return -EINVAL;
15891da177e4SLinus Torvalds
159023d6aef7SGustavo A. R. Silva resource = array_index_nospec(resource, RLIM_NLIMITS);
15911da177e4SLinus Torvalds task_lock(current->group_leader);
15921da177e4SLinus Torvalds x = current->signal->rlim[resource];
15931da177e4SLinus Torvalds task_unlock(current->group_leader);
15941da177e4SLinus Torvalds if (x.rlim_cur > 0x7FFFFFFF)
15951da177e4SLinus Torvalds x.rlim_cur = 0x7FFFFFFF;
15961da177e4SLinus Torvalds if (x.rlim_max > 0x7FFFFFFF)
15971da177e4SLinus Torvalds x.rlim_max = 0x7FFFFFFF;
15981da177e4SLinus Torvalds return copy_to_user(rlim, &x, sizeof(x)) ? -EFAULT : 0;
15991da177e4SLinus Torvalds }
16001da177e4SLinus Torvalds
1601613763a1SAl Viro #ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE2(old_getrlimit,unsigned int,resource,struct compat_rlimit __user *,rlim)1602613763a1SAl Viro COMPAT_SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1603613763a1SAl Viro struct compat_rlimit __user *, rlim)
1604613763a1SAl Viro {
1605613763a1SAl Viro struct rlimit r;
1606613763a1SAl Viro
1607613763a1SAl Viro if (resource >= RLIM_NLIMITS)
1608613763a1SAl Viro return -EINVAL;
1609613763a1SAl Viro
161023d6aef7SGustavo A. R. Silva resource = array_index_nospec(resource, RLIM_NLIMITS);
1611613763a1SAl Viro task_lock(current->group_leader);
1612613763a1SAl Viro r = current->signal->rlim[resource];
1613613763a1SAl Viro task_unlock(current->group_leader);
1614613763a1SAl Viro if (r.rlim_cur > 0x7FFFFFFF)
1615613763a1SAl Viro r.rlim_cur = 0x7FFFFFFF;
1616613763a1SAl Viro if (r.rlim_max > 0x7FFFFFFF)
1617613763a1SAl Viro r.rlim_max = 0x7FFFFFFF;
1618613763a1SAl Viro
1619613763a1SAl Viro if (put_user(r.rlim_cur, &rlim->rlim_cur) ||
1620613763a1SAl Viro put_user(r.rlim_max, &rlim->rlim_max))
1621613763a1SAl Viro return -EFAULT;
1622613763a1SAl Viro return 0;
1623613763a1SAl Viro }
1624613763a1SAl Viro #endif
1625613763a1SAl Viro
16261da177e4SLinus Torvalds #endif
16271da177e4SLinus Torvalds
rlim64_is_infinity(__u64 rlim64)1628c022a0acSJiri Slaby static inline bool rlim64_is_infinity(__u64 rlim64)
1629c022a0acSJiri Slaby {
1630c022a0acSJiri Slaby #if BITS_PER_LONG < 64
1631c022a0acSJiri Slaby return rlim64 >= ULONG_MAX;
1632c022a0acSJiri Slaby #else
1633c022a0acSJiri Slaby return rlim64 == RLIM64_INFINITY;
1634c022a0acSJiri Slaby #endif
1635c022a0acSJiri Slaby }
1636c022a0acSJiri Slaby
rlim_to_rlim64(const struct rlimit * rlim,struct rlimit64 * rlim64)1637c022a0acSJiri Slaby static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1638c022a0acSJiri Slaby {
1639c022a0acSJiri Slaby if (rlim->rlim_cur == RLIM_INFINITY)
1640c022a0acSJiri Slaby rlim64->rlim_cur = RLIM64_INFINITY;
1641c022a0acSJiri Slaby else
1642c022a0acSJiri Slaby rlim64->rlim_cur = rlim->rlim_cur;
1643c022a0acSJiri Slaby if (rlim->rlim_max == RLIM_INFINITY)
1644c022a0acSJiri Slaby rlim64->rlim_max = RLIM64_INFINITY;
1645c022a0acSJiri Slaby else
1646c022a0acSJiri Slaby rlim64->rlim_max = rlim->rlim_max;
1647c022a0acSJiri Slaby }
1648c022a0acSJiri Slaby
rlim64_to_rlim(const struct rlimit64 * rlim64,struct rlimit * rlim)1649c022a0acSJiri Slaby static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1650c022a0acSJiri Slaby {
1651c022a0acSJiri Slaby if (rlim64_is_infinity(rlim64->rlim_cur))
1652c022a0acSJiri Slaby rlim->rlim_cur = RLIM_INFINITY;
1653c022a0acSJiri Slaby else
1654c022a0acSJiri Slaby rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1655c022a0acSJiri Slaby if (rlim64_is_infinity(rlim64->rlim_max))
1656c022a0acSJiri Slaby rlim->rlim_max = RLIM_INFINITY;
1657c022a0acSJiri Slaby else
1658c022a0acSJiri Slaby rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1659c022a0acSJiri Slaby }
1660c022a0acSJiri Slaby
1661c022a0acSJiri Slaby /* rcu lock must be held */
check_prlimit_permission(struct task_struct * task,unsigned int flags)1662791ec491SStephen Smalley static int check_prlimit_permission(struct task_struct *task,
1663791ec491SStephen Smalley unsigned int flags)
1664c022a0acSJiri Slaby {
1665c022a0acSJiri Slaby const struct cred *cred = current_cred(), *tcred;
1666791ec491SStephen Smalley bool id_match;
1667c022a0acSJiri Slaby
1668fc832ad3SSerge E. Hallyn if (current == task)
1669c022a0acSJiri Slaby return 0;
1670fc832ad3SSerge E. Hallyn
1671fc832ad3SSerge E. Hallyn tcred = __task_cred(task);
1672791ec491SStephen Smalley id_match = (uid_eq(cred->uid, tcred->euid) &&
16735af66203SEric W. Biederman uid_eq(cred->uid, tcred->suid) &&
16745af66203SEric W. Biederman uid_eq(cred->uid, tcred->uid) &&
16755af66203SEric W. Biederman gid_eq(cred->gid, tcred->egid) &&
16765af66203SEric W. Biederman gid_eq(cred->gid, tcred->sgid) &&
1677791ec491SStephen Smalley gid_eq(cred->gid, tcred->gid));
1678791ec491SStephen Smalley if (!id_match && !ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
1679fc832ad3SSerge E. Hallyn return -EPERM;
1680791ec491SStephen Smalley
1681791ec491SStephen Smalley return security_task_prlimit(cred, tcred, flags);
1682c022a0acSJiri Slaby }
1683c022a0acSJiri Slaby
SYSCALL_DEFINE4(prlimit64,pid_t,pid,unsigned int,resource,const struct rlimit64 __user *,new_rlim,struct rlimit64 __user *,old_rlim)1684c022a0acSJiri Slaby SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1685c022a0acSJiri Slaby const struct rlimit64 __user *, new_rlim,
1686c022a0acSJiri Slaby struct rlimit64 __user *, old_rlim)
1687c022a0acSJiri Slaby {
1688c022a0acSJiri Slaby struct rlimit64 old64, new64;
1689c022a0acSJiri Slaby struct rlimit old, new;
1690c022a0acSJiri Slaby struct task_struct *tsk;
1691791ec491SStephen Smalley unsigned int checkflags = 0;
1692c022a0acSJiri Slaby int ret;
1693c022a0acSJiri Slaby
1694791ec491SStephen Smalley if (old_rlim)
1695791ec491SStephen Smalley checkflags |= LSM_PRLIMIT_READ;
1696791ec491SStephen Smalley
1697c022a0acSJiri Slaby if (new_rlim) {
1698c022a0acSJiri Slaby if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1699c022a0acSJiri Slaby return -EFAULT;
1700c022a0acSJiri Slaby rlim64_to_rlim(&new64, &new);
1701791ec491SStephen Smalley checkflags |= LSM_PRLIMIT_WRITE;
1702c022a0acSJiri Slaby }
1703c022a0acSJiri Slaby
1704c022a0acSJiri Slaby rcu_read_lock();
1705c022a0acSJiri Slaby tsk = pid ? find_task_by_vpid(pid) : current;
1706c022a0acSJiri Slaby if (!tsk) {
1707c022a0acSJiri Slaby rcu_read_unlock();
1708c022a0acSJiri Slaby return -ESRCH;
1709c022a0acSJiri Slaby }
1710791ec491SStephen Smalley ret = check_prlimit_permission(tsk, checkflags);
1711c022a0acSJiri Slaby if (ret) {
1712c022a0acSJiri Slaby rcu_read_unlock();
1713c022a0acSJiri Slaby return ret;
1714c022a0acSJiri Slaby }
1715c022a0acSJiri Slaby get_task_struct(tsk);
1716c022a0acSJiri Slaby rcu_read_unlock();
1717c022a0acSJiri Slaby
1718c022a0acSJiri Slaby ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1719c022a0acSJiri Slaby old_rlim ? &old : NULL);
1720c022a0acSJiri Slaby
1721c022a0acSJiri Slaby if (!ret && old_rlim) {
1722c022a0acSJiri Slaby rlim_to_rlim64(&old, &old64);
1723c022a0acSJiri Slaby if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1724c022a0acSJiri Slaby ret = -EFAULT;
1725c022a0acSJiri Slaby }
1726c022a0acSJiri Slaby
1727c022a0acSJiri Slaby put_task_struct(tsk);
1728c022a0acSJiri Slaby return ret;
1729c022a0acSJiri Slaby }
1730c022a0acSJiri Slaby
SYSCALL_DEFINE2(setrlimit,unsigned int,resource,struct rlimit __user *,rlim)17317855c35dSJiri Slaby SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
17327855c35dSJiri Slaby {
17337855c35dSJiri Slaby struct rlimit new_rlim;
17347855c35dSJiri Slaby
17357855c35dSJiri Slaby if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
17367855c35dSJiri Slaby return -EFAULT;
17375b41535aSJiri Slaby return do_prlimit(current, resource, &new_rlim, NULL);
17387855c35dSJiri Slaby }
17397855c35dSJiri Slaby
17401da177e4SLinus Torvalds /*
17411da177e4SLinus Torvalds * It would make sense to put struct rusage in the task_struct,
17421da177e4SLinus Torvalds * except that would make the task_struct be *really big*. After
17431da177e4SLinus Torvalds * task_struct gets moved into malloc'ed memory, it would
17441da177e4SLinus Torvalds * make sense to do this. It will make moving the rest of the information
17451da177e4SLinus Torvalds * a lot simpler! (Which we're not doing right now because we're not
17461da177e4SLinus Torvalds * measuring them yet).
17471da177e4SLinus Torvalds *
17481da177e4SLinus Torvalds * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
17491da177e4SLinus Torvalds * races with threads incrementing their own counters. But since word
17501da177e4SLinus Torvalds * reads are atomic, we either get new values or old values and we don't
17511da177e4SLinus Torvalds * care which for the sums. We always take the siglock to protect reading
17521da177e4SLinus Torvalds * the c* fields from p->signal from races with exit.c updating those
17531da177e4SLinus Torvalds * fields when reaping, so a sample either gets all the additions of a
17541da177e4SLinus Torvalds * given child after it's reaped, or none so this sample is before reaping.
17552dd0ebcdSRavikiran G Thirumalai *
1756de047c1bSRavikiran G Thirumalai * Locking:
1757de047c1bSRavikiran G Thirumalai * We need to take the siglock for CHILDEREN, SELF and BOTH
1758de047c1bSRavikiran G Thirumalai * for the cases current multithreaded, non-current single threaded
1759de047c1bSRavikiran G Thirumalai * non-current multithreaded. Thread traversal is now safe with
1760de047c1bSRavikiran G Thirumalai * the siglock held.
1761de047c1bSRavikiran G Thirumalai * Strictly speaking, we donot need to take the siglock if we are current and
1762de047c1bSRavikiran G Thirumalai * single threaded, as no one else can take our signal_struct away, no one
1763de047c1bSRavikiran G Thirumalai * else can reap the children to update signal->c* counters, and no one else
1764de047c1bSRavikiran G Thirumalai * can race with the signal-> fields. If we do not take any lock, the
1765de047c1bSRavikiran G Thirumalai * signal-> fields could be read out of order while another thread was just
1766de047c1bSRavikiran G Thirumalai * exiting. So we should place a read memory barrier when we avoid the lock.
1767de047c1bSRavikiran G Thirumalai * On the writer side, write memory barrier is implied in __exit_signal
1768de047c1bSRavikiran G Thirumalai * as __exit_signal releases the siglock spinlock after updating the signal->
1769de047c1bSRavikiran G Thirumalai * fields. But we don't do this yet to keep things simple.
17702dd0ebcdSRavikiran G Thirumalai *
17711da177e4SLinus Torvalds */
17721da177e4SLinus Torvalds
accumulate_thread_rusage(struct task_struct * t,struct rusage * r)1773f06febc9SFrank Mayhar static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
1774679c9cd4SSripathi Kodi {
1775679c9cd4SSripathi Kodi r->ru_nvcsw += t->nvcsw;
1776679c9cd4SSripathi Kodi r->ru_nivcsw += t->nivcsw;
1777679c9cd4SSripathi Kodi r->ru_minflt += t->min_flt;
1778679c9cd4SSripathi Kodi r->ru_majflt += t->maj_flt;
1779679c9cd4SSripathi Kodi r->ru_inblock += task_io_get_inblock(t);
1780679c9cd4SSripathi Kodi r->ru_oublock += task_io_get_oublock(t);
1781679c9cd4SSripathi Kodi }
1782679c9cd4SSripathi Kodi
getrusage(struct task_struct * p,int who,struct rusage * r)1783ce72a16fSAl Viro void getrusage(struct task_struct *p, int who, struct rusage *r)
17841da177e4SLinus Torvalds {
17851da177e4SLinus Torvalds struct task_struct *t;
17861da177e4SLinus Torvalds unsigned long flags;
17875613fda9SFrederic Weisbecker u64 tgutime, tgstime, utime, stime;
17881f10206cSJiri Pirko unsigned long maxrss = 0;
17891da177e4SLinus Torvalds
1790ec94fc3dSvishnu.ps memset((char *)r, 0, sizeof (*r));
179164861634SMartin Schwidefsky utime = stime = 0;
17920f59cc4aSOleg Nesterov
1793679c9cd4SSripathi Kodi if (who == RUSAGE_THREAD) {
1794e80d0a1aSFrederic Weisbecker task_cputime_adjusted(current, &utime, &stime);
1795f06febc9SFrank Mayhar accumulate_thread_rusage(p, r);
17961f10206cSJiri Pirko maxrss = p->signal->maxrss;
1797679c9cd4SSripathi Kodi goto out;
1798679c9cd4SSripathi Kodi }
1799679c9cd4SSripathi Kodi
1800d6cf723aSOleg Nesterov if (!lock_task_sighand(p, &flags))
18012dd0ebcdSRavikiran G Thirumalai return;
18022dd0ebcdSRavikiran G Thirumalai
18031da177e4SLinus Torvalds switch (who) {
18040f59cc4aSOleg Nesterov case RUSAGE_BOTH:
18051da177e4SLinus Torvalds case RUSAGE_CHILDREN:
18061da177e4SLinus Torvalds utime = p->signal->cutime;
18071da177e4SLinus Torvalds stime = p->signal->cstime;
18081da177e4SLinus Torvalds r->ru_nvcsw = p->signal->cnvcsw;
18091da177e4SLinus Torvalds r->ru_nivcsw = p->signal->cnivcsw;
18101da177e4SLinus Torvalds r->ru_minflt = p->signal->cmin_flt;
18111da177e4SLinus Torvalds r->ru_majflt = p->signal->cmaj_flt;
18126eaeeabaSEric Dumazet r->ru_inblock = p->signal->cinblock;
18136eaeeabaSEric Dumazet r->ru_oublock = p->signal->coublock;
18141f10206cSJiri Pirko maxrss = p->signal->cmaxrss;
18150f59cc4aSOleg Nesterov
18160f59cc4aSOleg Nesterov if (who == RUSAGE_CHILDREN)
18171da177e4SLinus Torvalds break;
1818df561f66SGustavo A. R. Silva fallthrough;
18190f59cc4aSOleg Nesterov
18201da177e4SLinus Torvalds case RUSAGE_SELF:
1821e80d0a1aSFrederic Weisbecker thread_group_cputime_adjusted(p, &tgutime, &tgstime);
182264861634SMartin Schwidefsky utime += tgutime;
182364861634SMartin Schwidefsky stime += tgstime;
18241da177e4SLinus Torvalds r->ru_nvcsw += p->signal->nvcsw;
18251da177e4SLinus Torvalds r->ru_nivcsw += p->signal->nivcsw;
18261da177e4SLinus Torvalds r->ru_minflt += p->signal->min_flt;
18271da177e4SLinus Torvalds r->ru_majflt += p->signal->maj_flt;
18286eaeeabaSEric Dumazet r->ru_inblock += p->signal->inblock;
18296eaeeabaSEric Dumazet r->ru_oublock += p->signal->oublock;
18301f10206cSJiri Pirko if (maxrss < p->signal->maxrss)
18311f10206cSJiri Pirko maxrss = p->signal->maxrss;
18321da177e4SLinus Torvalds t = p;
18331da177e4SLinus Torvalds do {
1834f06febc9SFrank Mayhar accumulate_thread_rusage(t, r);
18352e1f3835SOleg Nesterov } while_each_thread(p, t);
18361da177e4SLinus Torvalds break;
18370f59cc4aSOleg Nesterov
18381da177e4SLinus Torvalds default:
18391da177e4SLinus Torvalds BUG();
18401da177e4SLinus Torvalds }
1841de047c1bSRavikiran G Thirumalai unlock_task_sighand(p, &flags);
1842de047c1bSRavikiran G Thirumalai
1843679c9cd4SSripathi Kodi out:
1844bdd565f8SArnd Bergmann r->ru_utime = ns_to_kernel_old_timeval(utime);
1845bdd565f8SArnd Bergmann r->ru_stime = ns_to_kernel_old_timeval(stime);
18461f10206cSJiri Pirko
18471f10206cSJiri Pirko if (who != RUSAGE_CHILDREN) {
18481f10206cSJiri Pirko struct mm_struct *mm = get_task_mm(p);
1849ec94fc3dSvishnu.ps
18501f10206cSJiri Pirko if (mm) {
18511f10206cSJiri Pirko setmax_mm_hiwater_rss(&maxrss, mm);
18521f10206cSJiri Pirko mmput(mm);
18531f10206cSJiri Pirko }
18541f10206cSJiri Pirko }
18551f10206cSJiri Pirko r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
18561da177e4SLinus Torvalds }
18571da177e4SLinus Torvalds
SYSCALL_DEFINE2(getrusage,int,who,struct rusage __user *,ru)1858ce72a16fSAl Viro SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
18591da177e4SLinus Torvalds {
18601da177e4SLinus Torvalds struct rusage r;
1861ec94fc3dSvishnu.ps
1862679c9cd4SSripathi Kodi if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1863679c9cd4SSripathi Kodi who != RUSAGE_THREAD)
18641da177e4SLinus Torvalds return -EINVAL;
1865ce72a16fSAl Viro
1866ce72a16fSAl Viro getrusage(current, who, &r);
1867ce72a16fSAl Viro return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
18681da177e4SLinus Torvalds }
18691da177e4SLinus Torvalds
18708d2d5c4aSAl Viro #ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE2(getrusage,int,who,struct compat_rusage __user *,ru)18718d2d5c4aSAl Viro COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru)
18728d2d5c4aSAl Viro {
18738d2d5c4aSAl Viro struct rusage r;
18748d2d5c4aSAl Viro
18758d2d5c4aSAl Viro if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
18768d2d5c4aSAl Viro who != RUSAGE_THREAD)
18778d2d5c4aSAl Viro return -EINVAL;
18788d2d5c4aSAl Viro
1879ce72a16fSAl Viro getrusage(current, who, &r);
18808d2d5c4aSAl Viro return put_compat_rusage(&r, ru);
18818d2d5c4aSAl Viro }
18828d2d5c4aSAl Viro #endif
18838d2d5c4aSAl Viro
SYSCALL_DEFINE1(umask,int,mask)1884e48fbb69SHeiko Carstens SYSCALL_DEFINE1(umask, int, mask)
18851da177e4SLinus Torvalds {
18861da177e4SLinus Torvalds mask = xchg(¤t->fs->umask, mask & S_IRWXUGO);
18871da177e4SLinus Torvalds return mask;
18881da177e4SLinus Torvalds }
18891da177e4SLinus Torvalds
prctl_set_mm_exe_file(struct mm_struct * mm,unsigned int fd)18906e399cd1SDavidlohr Bueso static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
1891b32dfe37SCyrill Gorcunov {
18922903ff01SAl Viro struct fd exe;
1893496ad9aaSAl Viro struct inode *inode;
18942903ff01SAl Viro int err;
1895b32dfe37SCyrill Gorcunov
18962903ff01SAl Viro exe = fdget(fd);
18972903ff01SAl Viro if (!exe.file)
1898b32dfe37SCyrill Gorcunov return -EBADF;
1899b32dfe37SCyrill Gorcunov
1900496ad9aaSAl Viro inode = file_inode(exe.file);
1901b32dfe37SCyrill Gorcunov
1902b32dfe37SCyrill Gorcunov /*
1903b32dfe37SCyrill Gorcunov * Because the original mm->exe_file points to executable file, make
1904b32dfe37SCyrill Gorcunov * sure that this one is executable as well, to avoid breaking an
1905b32dfe37SCyrill Gorcunov * overall picture.
1906b32dfe37SCyrill Gorcunov */
1907b32dfe37SCyrill Gorcunov err = -EACCES;
190890f8572bSEric W. Biederman if (!S_ISREG(inode->i_mode) || path_noexec(&exe.file->f_path))
1909b32dfe37SCyrill Gorcunov goto exit;
1910b32dfe37SCyrill Gorcunov
191102f92b38SChristian Brauner err = file_permission(exe.file, MAY_EXEC);
1912b32dfe37SCyrill Gorcunov if (err)
1913b32dfe37SCyrill Gorcunov goto exit;
1914b32dfe37SCyrill Gorcunov
191535d7bdc8SDavid Hildenbrand err = replace_mm_exe_file(mm, exe.file);
1916b32dfe37SCyrill Gorcunov exit:
19172903ff01SAl Viro fdput(exe);
1918b32dfe37SCyrill Gorcunov return err;
1919b32dfe37SCyrill Gorcunov }
1920b32dfe37SCyrill Gorcunov
1921f606b77fSCyrill Gorcunov /*
192211bbd8b4SMichal Koutný * Check arithmetic relations of passed addresses.
192311bbd8b4SMichal Koutný *
1924f606b77fSCyrill Gorcunov * WARNING: we don't require any capability here so be very careful
1925f606b77fSCyrill Gorcunov * in what is allowed for modification from userspace.
1926f606b77fSCyrill Gorcunov */
validate_prctl_map_addr(struct prctl_mm_map * prctl_map)192711bbd8b4SMichal Koutný static int validate_prctl_map_addr(struct prctl_mm_map *prctl_map)
1928f606b77fSCyrill Gorcunov {
1929f606b77fSCyrill Gorcunov unsigned long mmap_max_addr = TASK_SIZE;
1930f606b77fSCyrill Gorcunov int error = -EINVAL, i;
1931f606b77fSCyrill Gorcunov
1932f606b77fSCyrill Gorcunov static const unsigned char offsets[] = {
1933f606b77fSCyrill Gorcunov offsetof(struct prctl_mm_map, start_code),
1934f606b77fSCyrill Gorcunov offsetof(struct prctl_mm_map, end_code),
1935f606b77fSCyrill Gorcunov offsetof(struct prctl_mm_map, start_data),
1936f606b77fSCyrill Gorcunov offsetof(struct prctl_mm_map, end_data),
1937f606b77fSCyrill Gorcunov offsetof(struct prctl_mm_map, start_brk),
1938f606b77fSCyrill Gorcunov offsetof(struct prctl_mm_map, brk),
1939f606b77fSCyrill Gorcunov offsetof(struct prctl_mm_map, start_stack),
1940f606b77fSCyrill Gorcunov offsetof(struct prctl_mm_map, arg_start),
1941f606b77fSCyrill Gorcunov offsetof(struct prctl_mm_map, arg_end),
1942f606b77fSCyrill Gorcunov offsetof(struct prctl_mm_map, env_start),
1943f606b77fSCyrill Gorcunov offsetof(struct prctl_mm_map, env_end),
1944f606b77fSCyrill Gorcunov };
1945f606b77fSCyrill Gorcunov
1946f606b77fSCyrill Gorcunov /*
1947f606b77fSCyrill Gorcunov * Make sure the members are not somewhere outside
1948f606b77fSCyrill Gorcunov * of allowed address space.
1949f606b77fSCyrill Gorcunov */
1950f606b77fSCyrill Gorcunov for (i = 0; i < ARRAY_SIZE(offsets); i++) {
1951f606b77fSCyrill Gorcunov u64 val = *(u64 *)((char *)prctl_map + offsets[i]);
1952f606b77fSCyrill Gorcunov
1953f606b77fSCyrill Gorcunov if ((unsigned long)val >= mmap_max_addr ||
1954f606b77fSCyrill Gorcunov (unsigned long)val < mmap_min_addr)
1955f606b77fSCyrill Gorcunov goto out;
1956f606b77fSCyrill Gorcunov }
1957f606b77fSCyrill Gorcunov
1958f606b77fSCyrill Gorcunov /*
1959f606b77fSCyrill Gorcunov * Make sure the pairs are ordered.
1960f606b77fSCyrill Gorcunov */
1961f606b77fSCyrill Gorcunov #define __prctl_check_order(__m1, __op, __m2) \
1962f606b77fSCyrill Gorcunov ((unsigned long)prctl_map->__m1 __op \
1963f606b77fSCyrill Gorcunov (unsigned long)prctl_map->__m2) ? 0 : -EINVAL
1964f606b77fSCyrill Gorcunov error = __prctl_check_order(start_code, <, end_code);
1965a9e73998SCyrill Gorcunov error |= __prctl_check_order(start_data,<=, end_data);
1966f606b77fSCyrill Gorcunov error |= __prctl_check_order(start_brk, <=, brk);
1967f606b77fSCyrill Gorcunov error |= __prctl_check_order(arg_start, <=, arg_end);
1968f606b77fSCyrill Gorcunov error |= __prctl_check_order(env_start, <=, env_end);
1969f606b77fSCyrill Gorcunov if (error)
1970f606b77fSCyrill Gorcunov goto out;
1971f606b77fSCyrill Gorcunov #undef __prctl_check_order
1972f606b77fSCyrill Gorcunov
1973f606b77fSCyrill Gorcunov error = -EINVAL;
1974f606b77fSCyrill Gorcunov
1975f606b77fSCyrill Gorcunov /*
1976f606b77fSCyrill Gorcunov * Neither we should allow to override limits if they set.
1977f606b77fSCyrill Gorcunov */
1978f606b77fSCyrill Gorcunov if (check_data_rlimit(rlimit(RLIMIT_DATA), prctl_map->brk,
1979f606b77fSCyrill Gorcunov prctl_map->start_brk, prctl_map->end_data,
1980f606b77fSCyrill Gorcunov prctl_map->start_data))
1981f606b77fSCyrill Gorcunov goto out;
1982f606b77fSCyrill Gorcunov
1983f606b77fSCyrill Gorcunov error = 0;
1984f606b77fSCyrill Gorcunov out:
1985f606b77fSCyrill Gorcunov return error;
1986f606b77fSCyrill Gorcunov }
1987f606b77fSCyrill Gorcunov
19884a00e9dfSAlexey Dobriyan #ifdef CONFIG_CHECKPOINT_RESTORE
prctl_set_mm_map(int opt,const void __user * addr,unsigned long data_size)1989f606b77fSCyrill Gorcunov static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data_size)
1990f606b77fSCyrill Gorcunov {
1991f606b77fSCyrill Gorcunov struct prctl_mm_map prctl_map = { .exe_fd = (u32)-1, };
1992f606b77fSCyrill Gorcunov unsigned long user_auxv[AT_VECTOR_SIZE];
1993f606b77fSCyrill Gorcunov struct mm_struct *mm = current->mm;
1994f606b77fSCyrill Gorcunov int error;
1995f606b77fSCyrill Gorcunov
1996f606b77fSCyrill Gorcunov BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
1997f606b77fSCyrill Gorcunov BUILD_BUG_ON(sizeof(struct prctl_mm_map) > 256);
1998f606b77fSCyrill Gorcunov
1999f606b77fSCyrill Gorcunov if (opt == PR_SET_MM_MAP_SIZE)
2000f606b77fSCyrill Gorcunov return put_user((unsigned int)sizeof(prctl_map),
2001f606b77fSCyrill Gorcunov (unsigned int __user *)addr);
2002f606b77fSCyrill Gorcunov
2003f606b77fSCyrill Gorcunov if (data_size != sizeof(prctl_map))
2004f606b77fSCyrill Gorcunov return -EINVAL;
2005f606b77fSCyrill Gorcunov
2006f606b77fSCyrill Gorcunov if (copy_from_user(&prctl_map, addr, sizeof(prctl_map)))
2007f606b77fSCyrill Gorcunov return -EFAULT;
2008f606b77fSCyrill Gorcunov
200911bbd8b4SMichal Koutný error = validate_prctl_map_addr(&prctl_map);
2010f606b77fSCyrill Gorcunov if (error)
2011f606b77fSCyrill Gorcunov return error;
2012f606b77fSCyrill Gorcunov
2013f606b77fSCyrill Gorcunov if (prctl_map.auxv_size) {
201411bbd8b4SMichal Koutný /*
201511bbd8b4SMichal Koutný * Someone is trying to cheat the auxv vector.
201611bbd8b4SMichal Koutný */
201711bbd8b4SMichal Koutný if (!prctl_map.auxv ||
201811bbd8b4SMichal Koutný prctl_map.auxv_size > sizeof(mm->saved_auxv))
201911bbd8b4SMichal Koutný return -EINVAL;
202011bbd8b4SMichal Koutný
2021f606b77fSCyrill Gorcunov memset(user_auxv, 0, sizeof(user_auxv));
2022f606b77fSCyrill Gorcunov if (copy_from_user(user_auxv,
2023f606b77fSCyrill Gorcunov (const void __user *)prctl_map.auxv,
2024f606b77fSCyrill Gorcunov prctl_map.auxv_size))
2025f606b77fSCyrill Gorcunov return -EFAULT;
2026f606b77fSCyrill Gorcunov
2027f606b77fSCyrill Gorcunov /* Last entry must be AT_NULL as specification requires */
2028f606b77fSCyrill Gorcunov user_auxv[AT_VECTOR_SIZE - 2] = AT_NULL;
2029f606b77fSCyrill Gorcunov user_auxv[AT_VECTOR_SIZE - 1] = AT_NULL;
2030f606b77fSCyrill Gorcunov }
2031f606b77fSCyrill Gorcunov
2032ddf1d398SMateusz Guzik if (prctl_map.exe_fd != (u32)-1) {
203311bbd8b4SMichal Koutný /*
2034ebd6de68SNicolas Viennot * Check if the current user is checkpoint/restore capable.
2035ebd6de68SNicolas Viennot * At the time of this writing, it checks for CAP_SYS_ADMIN
2036ebd6de68SNicolas Viennot * or CAP_CHECKPOINT_RESTORE.
2037ebd6de68SNicolas Viennot * Note that a user with access to ptrace can masquerade an
2038ebd6de68SNicolas Viennot * arbitrary program as any executable, even setuid ones.
2039ebd6de68SNicolas Viennot * This may have implications in the tomoyo subsystem.
204011bbd8b4SMichal Koutný */
2041ebd6de68SNicolas Viennot if (!checkpoint_restore_ns_capable(current_user_ns()))
2042227175b2SNicolas Viennot return -EPERM;
204311bbd8b4SMichal Koutný
20446e399cd1SDavidlohr Bueso error = prctl_set_mm_exe_file(mm, prctl_map.exe_fd);
2045f606b77fSCyrill Gorcunov if (error)
2046ddf1d398SMateusz Guzik return error;
2047ddf1d398SMateusz Guzik }
2048ddf1d398SMateusz Guzik
204988aa7cc6SYang Shi /*
20505afe69c2SXiaofeng Cao * arg_lock protects concurrent updates but we still need mmap_lock for
205188aa7cc6SYang Shi * read to exclude races with sys_brk.
205288aa7cc6SYang Shi */
2053d8ed45c5SMichel Lespinasse mmap_read_lock(mm);
2054f606b77fSCyrill Gorcunov
2055f606b77fSCyrill Gorcunov /*
2056f606b77fSCyrill Gorcunov * We don't validate if these members are pointing to
2057f606b77fSCyrill Gorcunov * real present VMAs because application may have correspond
2058f606b77fSCyrill Gorcunov * VMAs already unmapped and kernel uses these members for statistics
2059f606b77fSCyrill Gorcunov * output in procfs mostly, except
2060f606b77fSCyrill Gorcunov *
206115ec0fcfSLiao Pingfang * - @start_brk/@brk which are used in do_brk_flags but kernel lookups
20625afe69c2SXiaofeng Cao * for VMAs when updating these members so anything wrong written
2063f606b77fSCyrill Gorcunov * here cause kernel to swear at userspace program but won't lead
2064f606b77fSCyrill Gorcunov * to any problem in kernel itself
2065f606b77fSCyrill Gorcunov */
2066f606b77fSCyrill Gorcunov
206788aa7cc6SYang Shi spin_lock(&mm->arg_lock);
2068f606b77fSCyrill Gorcunov mm->start_code = prctl_map.start_code;
2069f606b77fSCyrill Gorcunov mm->end_code = prctl_map.end_code;
2070f606b77fSCyrill Gorcunov mm->start_data = prctl_map.start_data;
2071f606b77fSCyrill Gorcunov mm->end_data = prctl_map.end_data;
2072f606b77fSCyrill Gorcunov mm->start_brk = prctl_map.start_brk;
2073f606b77fSCyrill Gorcunov mm->brk = prctl_map.brk;
2074f606b77fSCyrill Gorcunov mm->start_stack = prctl_map.start_stack;
2075f606b77fSCyrill Gorcunov mm->arg_start = prctl_map.arg_start;
2076f606b77fSCyrill Gorcunov mm->arg_end = prctl_map.arg_end;
2077f606b77fSCyrill Gorcunov mm->env_start = prctl_map.env_start;
2078f606b77fSCyrill Gorcunov mm->env_end = prctl_map.env_end;
207988aa7cc6SYang Shi spin_unlock(&mm->arg_lock);
2080f606b77fSCyrill Gorcunov
2081f606b77fSCyrill Gorcunov /*
2082f606b77fSCyrill Gorcunov * Note this update of @saved_auxv is lockless thus
2083f606b77fSCyrill Gorcunov * if someone reads this member in procfs while we're
2084f606b77fSCyrill Gorcunov * updating -- it may get partly updated results. It's
2085f606b77fSCyrill Gorcunov * known and acceptable trade off: we leave it as is to
2086f606b77fSCyrill Gorcunov * not introduce additional locks here making the kernel
2087f606b77fSCyrill Gorcunov * more complex.
2088f606b77fSCyrill Gorcunov */
2089f606b77fSCyrill Gorcunov if (prctl_map.auxv_size)
2090f606b77fSCyrill Gorcunov memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
2091f606b77fSCyrill Gorcunov
2092d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
2093ddf1d398SMateusz Guzik return 0;
2094f606b77fSCyrill Gorcunov }
2095f606b77fSCyrill Gorcunov #endif /* CONFIG_CHECKPOINT_RESTORE */
2096f606b77fSCyrill Gorcunov
prctl_set_auxv(struct mm_struct * mm,unsigned long addr,unsigned long len)20974a00e9dfSAlexey Dobriyan static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr,
20984a00e9dfSAlexey Dobriyan unsigned long len)
20994a00e9dfSAlexey Dobriyan {
21004a00e9dfSAlexey Dobriyan /*
21014a00e9dfSAlexey Dobriyan * This doesn't move the auxiliary vector itself since it's pinned to
21024a00e9dfSAlexey Dobriyan * mm_struct, but it permits filling the vector with new values. It's
21034a00e9dfSAlexey Dobriyan * up to the caller to provide sane values here, otherwise userspace
21044a00e9dfSAlexey Dobriyan * tools which use this vector might be unhappy.
21054a00e9dfSAlexey Dobriyan */
2106c995f12aSAlexey Dobriyan unsigned long user_auxv[AT_VECTOR_SIZE] = {};
21074a00e9dfSAlexey Dobriyan
21084a00e9dfSAlexey Dobriyan if (len > sizeof(user_auxv))
21094a00e9dfSAlexey Dobriyan return -EINVAL;
21104a00e9dfSAlexey Dobriyan
21114a00e9dfSAlexey Dobriyan if (copy_from_user(user_auxv, (const void __user *)addr, len))
21124a00e9dfSAlexey Dobriyan return -EFAULT;
21134a00e9dfSAlexey Dobriyan
21144a00e9dfSAlexey Dobriyan /* Make sure the last entry is always AT_NULL */
21154a00e9dfSAlexey Dobriyan user_auxv[AT_VECTOR_SIZE - 2] = 0;
21164a00e9dfSAlexey Dobriyan user_auxv[AT_VECTOR_SIZE - 1] = 0;
21174a00e9dfSAlexey Dobriyan
21184a00e9dfSAlexey Dobriyan BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
21194a00e9dfSAlexey Dobriyan
21204a00e9dfSAlexey Dobriyan task_lock(current);
21214a00e9dfSAlexey Dobriyan memcpy(mm->saved_auxv, user_auxv, len);
21224a00e9dfSAlexey Dobriyan task_unlock(current);
21234a00e9dfSAlexey Dobriyan
21244a00e9dfSAlexey Dobriyan return 0;
21254a00e9dfSAlexey Dobriyan }
21264a00e9dfSAlexey Dobriyan
prctl_set_mm(int opt,unsigned long addr,unsigned long arg4,unsigned long arg5)2127028ee4beSCyrill Gorcunov static int prctl_set_mm(int opt, unsigned long addr,
2128028ee4beSCyrill Gorcunov unsigned long arg4, unsigned long arg5)
2129028ee4beSCyrill Gorcunov {
2130028ee4beSCyrill Gorcunov struct mm_struct *mm = current->mm;
213111bbd8b4SMichal Koutný struct prctl_mm_map prctl_map = {
213211bbd8b4SMichal Koutný .auxv = NULL,
213311bbd8b4SMichal Koutný .auxv_size = 0,
213411bbd8b4SMichal Koutný .exe_fd = -1,
213511bbd8b4SMichal Koutný };
2136fe8c7f5cSCyrill Gorcunov struct vm_area_struct *vma;
2137fe8c7f5cSCyrill Gorcunov int error;
2138028ee4beSCyrill Gorcunov
2139f606b77fSCyrill Gorcunov if (arg5 || (arg4 && (opt != PR_SET_MM_AUXV &&
2140f606b77fSCyrill Gorcunov opt != PR_SET_MM_MAP &&
2141f606b77fSCyrill Gorcunov opt != PR_SET_MM_MAP_SIZE)))
2142028ee4beSCyrill Gorcunov return -EINVAL;
2143028ee4beSCyrill Gorcunov
2144f606b77fSCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
2145f606b77fSCyrill Gorcunov if (opt == PR_SET_MM_MAP || opt == PR_SET_MM_MAP_SIZE)
2146f606b77fSCyrill Gorcunov return prctl_set_mm_map(opt, (const void __user *)addr, arg4);
2147f606b77fSCyrill Gorcunov #endif
2148f606b77fSCyrill Gorcunov
214979f0713dSCyrill Gorcunov if (!capable(CAP_SYS_RESOURCE))
2150028ee4beSCyrill Gorcunov return -EPERM;
2151028ee4beSCyrill Gorcunov
21526e399cd1SDavidlohr Bueso if (opt == PR_SET_MM_EXE_FILE)
21536e399cd1SDavidlohr Bueso return prctl_set_mm_exe_file(mm, (unsigned int)addr);
2154b32dfe37SCyrill Gorcunov
21554a00e9dfSAlexey Dobriyan if (opt == PR_SET_MM_AUXV)
21564a00e9dfSAlexey Dobriyan return prctl_set_auxv(mm, addr, arg4);
21574a00e9dfSAlexey Dobriyan
21581ad75b9eSCyrill Gorcunov if (addr >= TASK_SIZE || addr < mmap_min_addr)
2159028ee4beSCyrill Gorcunov return -EINVAL;
2160028ee4beSCyrill Gorcunov
2161fe8c7f5cSCyrill Gorcunov error = -EINVAL;
2162fe8c7f5cSCyrill Gorcunov
2163bc81426fSMichal Koutný /*
21645afe69c2SXiaofeng Cao * arg_lock protects concurrent updates of arg boundaries, we need
2165c1e8d7c6SMichel Lespinasse * mmap_lock for a) concurrent sys_brk, b) finding VMA for addr
2166bc81426fSMichal Koutný * validation.
2167bc81426fSMichal Koutný */
2168d8ed45c5SMichel Lespinasse mmap_read_lock(mm);
2169028ee4beSCyrill Gorcunov vma = find_vma(mm, addr);
2170028ee4beSCyrill Gorcunov
2171bc81426fSMichal Koutný spin_lock(&mm->arg_lock);
21724a00e9dfSAlexey Dobriyan prctl_map.start_code = mm->start_code;
21734a00e9dfSAlexey Dobriyan prctl_map.end_code = mm->end_code;
21744a00e9dfSAlexey Dobriyan prctl_map.start_data = mm->start_data;
21754a00e9dfSAlexey Dobriyan prctl_map.end_data = mm->end_data;
21764a00e9dfSAlexey Dobriyan prctl_map.start_brk = mm->start_brk;
21774a00e9dfSAlexey Dobriyan prctl_map.brk = mm->brk;
21784a00e9dfSAlexey Dobriyan prctl_map.start_stack = mm->start_stack;
21794a00e9dfSAlexey Dobriyan prctl_map.arg_start = mm->arg_start;
21804a00e9dfSAlexey Dobriyan prctl_map.arg_end = mm->arg_end;
21814a00e9dfSAlexey Dobriyan prctl_map.env_start = mm->env_start;
21824a00e9dfSAlexey Dobriyan prctl_map.env_end = mm->env_end;
21834a00e9dfSAlexey Dobriyan
2184028ee4beSCyrill Gorcunov switch (opt) {
2185028ee4beSCyrill Gorcunov case PR_SET_MM_START_CODE:
21864a00e9dfSAlexey Dobriyan prctl_map.start_code = addr;
2187fe8c7f5cSCyrill Gorcunov break;
2188fe8c7f5cSCyrill Gorcunov case PR_SET_MM_END_CODE:
21894a00e9dfSAlexey Dobriyan prctl_map.end_code = addr;
2190028ee4beSCyrill Gorcunov break;
2191028ee4beSCyrill Gorcunov case PR_SET_MM_START_DATA:
21924a00e9dfSAlexey Dobriyan prctl_map.start_data = addr;
2193028ee4beSCyrill Gorcunov break;
2194fe8c7f5cSCyrill Gorcunov case PR_SET_MM_END_DATA:
21954a00e9dfSAlexey Dobriyan prctl_map.end_data = addr;
2196028ee4beSCyrill Gorcunov break;
21974a00e9dfSAlexey Dobriyan case PR_SET_MM_START_STACK:
21984a00e9dfSAlexey Dobriyan prctl_map.start_stack = addr;
21994a00e9dfSAlexey Dobriyan break;
2200028ee4beSCyrill Gorcunov case PR_SET_MM_START_BRK:
22014a00e9dfSAlexey Dobriyan prctl_map.start_brk = addr;
2202028ee4beSCyrill Gorcunov break;
2203028ee4beSCyrill Gorcunov case PR_SET_MM_BRK:
22044a00e9dfSAlexey Dobriyan prctl_map.brk = addr;
2205028ee4beSCyrill Gorcunov break;
22064a00e9dfSAlexey Dobriyan case PR_SET_MM_ARG_START:
22074a00e9dfSAlexey Dobriyan prctl_map.arg_start = addr;
22084a00e9dfSAlexey Dobriyan break;
22094a00e9dfSAlexey Dobriyan case PR_SET_MM_ARG_END:
22104a00e9dfSAlexey Dobriyan prctl_map.arg_end = addr;
22114a00e9dfSAlexey Dobriyan break;
22124a00e9dfSAlexey Dobriyan case PR_SET_MM_ENV_START:
22134a00e9dfSAlexey Dobriyan prctl_map.env_start = addr;
22144a00e9dfSAlexey Dobriyan break;
22154a00e9dfSAlexey Dobriyan case PR_SET_MM_ENV_END:
22164a00e9dfSAlexey Dobriyan prctl_map.env_end = addr;
22174a00e9dfSAlexey Dobriyan break;
22184a00e9dfSAlexey Dobriyan default:
22194a00e9dfSAlexey Dobriyan goto out;
22204a00e9dfSAlexey Dobriyan }
2221028ee4beSCyrill Gorcunov
222211bbd8b4SMichal Koutný error = validate_prctl_map_addr(&prctl_map);
22234a00e9dfSAlexey Dobriyan if (error)
22244a00e9dfSAlexey Dobriyan goto out;
22254a00e9dfSAlexey Dobriyan
22264a00e9dfSAlexey Dobriyan switch (opt) {
2227fe8c7f5cSCyrill Gorcunov /*
2228fe8c7f5cSCyrill Gorcunov * If command line arguments and environment
2229fe8c7f5cSCyrill Gorcunov * are placed somewhere else on stack, we can
2230fe8c7f5cSCyrill Gorcunov * set them up here, ARG_START/END to setup
22315afe69c2SXiaofeng Cao * command line arguments and ENV_START/END
2232fe8c7f5cSCyrill Gorcunov * for environment.
2233fe8c7f5cSCyrill Gorcunov */
2234fe8c7f5cSCyrill Gorcunov case PR_SET_MM_START_STACK:
2235fe8c7f5cSCyrill Gorcunov case PR_SET_MM_ARG_START:
2236fe8c7f5cSCyrill Gorcunov case PR_SET_MM_ARG_END:
2237fe8c7f5cSCyrill Gorcunov case PR_SET_MM_ENV_START:
2238fe8c7f5cSCyrill Gorcunov case PR_SET_MM_ENV_END:
2239fe8c7f5cSCyrill Gorcunov if (!vma) {
2240fe8c7f5cSCyrill Gorcunov error = -EFAULT;
2241fe8c7f5cSCyrill Gorcunov goto out;
2242fe8c7f5cSCyrill Gorcunov }
2243fe8c7f5cSCyrill Gorcunov }
22444a00e9dfSAlexey Dobriyan
22454a00e9dfSAlexey Dobriyan mm->start_code = prctl_map.start_code;
22464a00e9dfSAlexey Dobriyan mm->end_code = prctl_map.end_code;
22474a00e9dfSAlexey Dobriyan mm->start_data = prctl_map.start_data;
22484a00e9dfSAlexey Dobriyan mm->end_data = prctl_map.end_data;
22494a00e9dfSAlexey Dobriyan mm->start_brk = prctl_map.start_brk;
22504a00e9dfSAlexey Dobriyan mm->brk = prctl_map.brk;
22514a00e9dfSAlexey Dobriyan mm->start_stack = prctl_map.start_stack;
22524a00e9dfSAlexey Dobriyan mm->arg_start = prctl_map.arg_start;
22534a00e9dfSAlexey Dobriyan mm->arg_end = prctl_map.arg_end;
22544a00e9dfSAlexey Dobriyan mm->env_start = prctl_map.env_start;
22554a00e9dfSAlexey Dobriyan mm->env_end = prctl_map.env_end;
2256028ee4beSCyrill Gorcunov
2257028ee4beSCyrill Gorcunov error = 0;
2258028ee4beSCyrill Gorcunov out:
2259bc81426fSMichal Koutný spin_unlock(&mm->arg_lock);
2260d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
2261028ee4beSCyrill Gorcunov return error;
2262028ee4beSCyrill Gorcunov }
2263300f786bSCyrill Gorcunov
226452b36941SAmnon Shiloh #ifdef CONFIG_CHECKPOINT_RESTORE
prctl_get_tid_address(struct task_struct * me,int __user * __user * tid_addr)2265986b9eacSRasmus Villemoes static int prctl_get_tid_address(struct task_struct *me, int __user * __user *tid_addr)
2266300f786bSCyrill Gorcunov {
2267300f786bSCyrill Gorcunov return put_user(me->clear_child_tid, tid_addr);
2268300f786bSCyrill Gorcunov }
226952b36941SAmnon Shiloh #else
prctl_get_tid_address(struct task_struct * me,int __user * __user * tid_addr)2270986b9eacSRasmus Villemoes static int prctl_get_tid_address(struct task_struct *me, int __user * __user *tid_addr)
2271300f786bSCyrill Gorcunov {
2272300f786bSCyrill Gorcunov return -EINVAL;
2273300f786bSCyrill Gorcunov }
2274028ee4beSCyrill Gorcunov #endif
2275028ee4beSCyrill Gorcunov
propagate_has_child_subreaper(struct task_struct * p,void * data)2276749860ceSPavel Tikhomirov static int propagate_has_child_subreaper(struct task_struct *p, void *data)
2277749860ceSPavel Tikhomirov {
2278749860ceSPavel Tikhomirov /*
22795afe69c2SXiaofeng Cao * If task has has_child_subreaper - all its descendants
22805afe69c2SXiaofeng Cao * already have these flag too and new descendants will
2281749860ceSPavel Tikhomirov * inherit it on fork, skip them.
2282749860ceSPavel Tikhomirov *
2283749860ceSPavel Tikhomirov * If we've found child_reaper - skip descendants in
2284749860ceSPavel Tikhomirov * it's subtree as they will never get out pidns.
2285749860ceSPavel Tikhomirov */
2286749860ceSPavel Tikhomirov if (p->signal->has_child_subreaper ||
2287749860ceSPavel Tikhomirov is_child_reaper(task_pid(p)))
2288749860ceSPavel Tikhomirov return 0;
2289749860ceSPavel Tikhomirov
2290749860ceSPavel Tikhomirov p->signal->has_child_subreaper = 1;
2291749860ceSPavel Tikhomirov return 1;
2292749860ceSPavel Tikhomirov }
2293749860ceSPavel Tikhomirov
arch_prctl_spec_ctrl_get(struct task_struct * t,unsigned long which)22947bbf1373SKees Cook int __weak arch_prctl_spec_ctrl_get(struct task_struct *t, unsigned long which)
2295b617cfc8SThomas Gleixner {
2296b617cfc8SThomas Gleixner return -EINVAL;
2297b617cfc8SThomas Gleixner }
2298b617cfc8SThomas Gleixner
arch_prctl_spec_ctrl_set(struct task_struct * t,unsigned long which,unsigned long ctrl)22997bbf1373SKees Cook int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which,
23007bbf1373SKees Cook unsigned long ctrl)
2301b617cfc8SThomas Gleixner {
2302b617cfc8SThomas Gleixner return -EINVAL;
2303b617cfc8SThomas Gleixner }
2304b617cfc8SThomas Gleixner
2305a37b0715SNeilBrown #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE)
23068d19f1c8SMike Christie
23079a10064fSColin Cross #ifdef CONFIG_ANON_VMA_NAME
23089a10064fSColin Cross
23099a10064fSColin Cross #define ANON_VMA_NAME_MAX_LEN 80
23109a10064fSColin Cross #define ANON_VMA_NAME_INVALID_CHARS "\\`$[]"
23119a10064fSColin Cross
is_valid_name_char(char ch)23129a10064fSColin Cross static inline bool is_valid_name_char(char ch)
23139a10064fSColin Cross {
23149a10064fSColin Cross /* printable ascii characters, excluding ANON_VMA_NAME_INVALID_CHARS */
23159a10064fSColin Cross return ch > 0x1f && ch < 0x7f &&
23169a10064fSColin Cross !strchr(ANON_VMA_NAME_INVALID_CHARS, ch);
23179a10064fSColin Cross }
23189a10064fSColin Cross
prctl_set_vma(unsigned long opt,unsigned long addr,unsigned long size,unsigned long arg)23199a10064fSColin Cross static int prctl_set_vma(unsigned long opt, unsigned long addr,
23209a10064fSColin Cross unsigned long size, unsigned long arg)
23219a10064fSColin Cross {
23229a10064fSColin Cross struct mm_struct *mm = current->mm;
23239a10064fSColin Cross const char __user *uname;
23245c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name = NULL;
23259a10064fSColin Cross int error;
23269a10064fSColin Cross
23279a10064fSColin Cross switch (opt) {
23289a10064fSColin Cross case PR_SET_VMA_ANON_NAME:
23299a10064fSColin Cross uname = (const char __user *)arg;
23309a10064fSColin Cross if (uname) {
23315c26f6acSSuren Baghdasaryan char *name, *pch;
23329a10064fSColin Cross
23335c26f6acSSuren Baghdasaryan name = strndup_user(uname, ANON_VMA_NAME_MAX_LEN);
23349a10064fSColin Cross if (IS_ERR(name))
23359a10064fSColin Cross return PTR_ERR(name);
23369a10064fSColin Cross
23379a10064fSColin Cross for (pch = name; *pch != '\0'; pch++) {
23389a10064fSColin Cross if (!is_valid_name_char(*pch)) {
23399a10064fSColin Cross kfree(name);
23409a10064fSColin Cross return -EINVAL;
23419a10064fSColin Cross }
23429a10064fSColin Cross }
23435c26f6acSSuren Baghdasaryan /* anon_vma has its own copy */
23445c26f6acSSuren Baghdasaryan anon_name = anon_vma_name_alloc(name);
23455c26f6acSSuren Baghdasaryan kfree(name);
23465c26f6acSSuren Baghdasaryan if (!anon_name)
23475c26f6acSSuren Baghdasaryan return -ENOMEM;
23485c26f6acSSuren Baghdasaryan
23499a10064fSColin Cross }
23509a10064fSColin Cross
23519a10064fSColin Cross mmap_write_lock(mm);
23525c26f6acSSuren Baghdasaryan error = madvise_set_anon_name(mm, addr, size, anon_name);
23539a10064fSColin Cross mmap_write_unlock(mm);
23545c26f6acSSuren Baghdasaryan anon_vma_name_put(anon_name);
23559a10064fSColin Cross break;
23569a10064fSColin Cross default:
23579a10064fSColin Cross error = -EINVAL;
23589a10064fSColin Cross }
23599a10064fSColin Cross
23609a10064fSColin Cross return error;
23619a10064fSColin Cross }
23629a10064fSColin Cross
23639a10064fSColin Cross #else /* CONFIG_ANON_VMA_NAME */
prctl_set_vma(unsigned long opt,unsigned long start,unsigned long size,unsigned long arg)23649a10064fSColin Cross static int prctl_set_vma(unsigned long opt, unsigned long start,
23659a10064fSColin Cross unsigned long size, unsigned long arg)
23669a10064fSColin Cross {
23679a10064fSColin Cross return -EINVAL;
23689a10064fSColin Cross }
23699a10064fSColin Cross #endif /* CONFIG_ANON_VMA_NAME */
23709a10064fSColin Cross
get_current_mdwe(void)23712b00d1fdSFlorent Revest static inline unsigned long get_current_mdwe(void)
23722b00d1fdSFlorent Revest {
23732b00d1fdSFlorent Revest unsigned long ret = 0;
23742b00d1fdSFlorent Revest
23752b00d1fdSFlorent Revest if (test_bit(MMF_HAS_MDWE, ¤t->mm->flags))
23762b00d1fdSFlorent Revest ret |= PR_MDWE_REFUSE_EXEC_GAIN;
23772b00d1fdSFlorent Revest if (test_bit(MMF_HAS_MDWE_NO_INHERIT, ¤t->mm->flags))
23782b00d1fdSFlorent Revest ret |= PR_MDWE_NO_INHERIT;
23792b00d1fdSFlorent Revest
23802b00d1fdSFlorent Revest return ret;
23812b00d1fdSFlorent Revest }
23822b00d1fdSFlorent Revest
prctl_set_mdwe(unsigned long bits,unsigned long arg3,unsigned long arg4,unsigned long arg5)2383b507808eSJoey Gouly static inline int prctl_set_mdwe(unsigned long bits, unsigned long arg3,
2384b507808eSJoey Gouly unsigned long arg4, unsigned long arg5)
2385b507808eSJoey Gouly {
23862b00d1fdSFlorent Revest unsigned long current_bits;
23872b00d1fdSFlorent Revest
2388b507808eSJoey Gouly if (arg3 || arg4 || arg5)
2389b507808eSJoey Gouly return -EINVAL;
2390b507808eSJoey Gouly
23912b00d1fdSFlorent Revest if (bits & ~(PR_MDWE_REFUSE_EXEC_GAIN | PR_MDWE_NO_INHERIT))
2392b507808eSJoey Gouly return -EINVAL;
2393b507808eSJoey Gouly
23942b00d1fdSFlorent Revest /* NO_INHERIT only makes sense with REFUSE_EXEC_GAIN */
23952b00d1fdSFlorent Revest if (bits & PR_MDWE_NO_INHERIT && !(bits & PR_MDWE_REFUSE_EXEC_GAIN))
23962b00d1fdSFlorent Revest return -EINVAL;
23972b00d1fdSFlorent Revest
2398*a0071e3bSZev Weiss /*
2399*a0071e3bSZev Weiss * EOPNOTSUPP might be more appropriate here in principle, but
2400*a0071e3bSZev Weiss * existing userspace depends on EINVAL specifically.
2401*a0071e3bSZev Weiss */
2402*a0071e3bSZev Weiss if (!arch_memory_deny_write_exec_supported())
2403790afa13SHelge Deller return -EINVAL;
2404790afa13SHelge Deller
24052b00d1fdSFlorent Revest current_bits = get_current_mdwe();
24062b00d1fdSFlorent Revest if (current_bits && current_bits != bits)
24072b00d1fdSFlorent Revest return -EPERM; /* Cannot unset the flags */
24082b00d1fdSFlorent Revest
24092b00d1fdSFlorent Revest if (bits & PR_MDWE_NO_INHERIT)
24102b00d1fdSFlorent Revest set_bit(MMF_HAS_MDWE_NO_INHERIT, ¤t->mm->flags);
2411b507808eSJoey Gouly if (bits & PR_MDWE_REFUSE_EXEC_GAIN)
2412b507808eSJoey Gouly set_bit(MMF_HAS_MDWE, ¤t->mm->flags);
2413b507808eSJoey Gouly
2414b507808eSJoey Gouly return 0;
2415b507808eSJoey Gouly }
2416b507808eSJoey Gouly
prctl_get_mdwe(unsigned long arg2,unsigned long arg3,unsigned long arg4,unsigned long arg5)2417b507808eSJoey Gouly static inline int prctl_get_mdwe(unsigned long arg2, unsigned long arg3,
2418b507808eSJoey Gouly unsigned long arg4, unsigned long arg5)
2419b507808eSJoey Gouly {
2420b507808eSJoey Gouly if (arg2 || arg3 || arg4 || arg5)
2421b507808eSJoey Gouly return -EINVAL;
24222b00d1fdSFlorent Revest return get_current_mdwe();
2423b507808eSJoey Gouly }
2424b507808eSJoey Gouly
prctl_get_auxv(void __user * addr,unsigned long len)2425ddc65971SJosh Triplett static int prctl_get_auxv(void __user *addr, unsigned long len)
2426ddc65971SJosh Triplett {
2427ddc65971SJosh Triplett struct mm_struct *mm = current->mm;
2428ddc65971SJosh Triplett unsigned long size = min_t(unsigned long, sizeof(mm->saved_auxv), len);
2429ddc65971SJosh Triplett
2430ddc65971SJosh Triplett if (size && copy_to_user(addr, mm->saved_auxv, size))
2431ddc65971SJosh Triplett return -EFAULT;
2432ddc65971SJosh Triplett return sizeof(mm->saved_auxv);
2433ddc65971SJosh Triplett }
2434ddc65971SJosh Triplett
SYSCALL_DEFINE5(prctl,int,option,unsigned long,arg2,unsigned long,arg3,unsigned long,arg4,unsigned long,arg5)2435c4ea37c2SHeiko Carstens SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
2436c4ea37c2SHeiko Carstens unsigned long, arg4, unsigned long, arg5)
24371da177e4SLinus Torvalds {
2438b6dff3ecSDavid Howells struct task_struct *me = current;
2439b6dff3ecSDavid Howells unsigned char comm[sizeof(me->comm)];
2440b6dff3ecSDavid Howells long error;
24411da177e4SLinus Torvalds
2442d84f4f99SDavid Howells error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2443d84f4f99SDavid Howells if (error != -ENOSYS)
24441da177e4SLinus Torvalds return error;
24451da177e4SLinus Torvalds
2446d84f4f99SDavid Howells error = 0;
24471da177e4SLinus Torvalds switch (option) {
24481da177e4SLinus Torvalds case PR_SET_PDEATHSIG:
24490730ded5SJesper Juhl if (!valid_signal(arg2)) {
24501da177e4SLinus Torvalds error = -EINVAL;
24511da177e4SLinus Torvalds break;
24521da177e4SLinus Torvalds }
2453b6dff3ecSDavid Howells me->pdeath_signal = arg2;
24541da177e4SLinus Torvalds break;
24551da177e4SLinus Torvalds case PR_GET_PDEATHSIG:
2456b6dff3ecSDavid Howells error = put_user(me->pdeath_signal, (int __user *)arg2);
24571da177e4SLinus Torvalds break;
24581da177e4SLinus Torvalds case PR_GET_DUMPABLE:
2459b6dff3ecSDavid Howells error = get_dumpable(me->mm);
24601da177e4SLinus Torvalds break;
24611da177e4SLinus Torvalds case PR_SET_DUMPABLE:
2462f3cbd435SAndrew Morton if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) {
24631da177e4SLinus Torvalds error = -EINVAL;
24641da177e4SLinus Torvalds break;
24651da177e4SLinus Torvalds }
2466b6dff3ecSDavid Howells set_dumpable(me->mm, arg2);
24671da177e4SLinus Torvalds break;
24681da177e4SLinus Torvalds
24691da177e4SLinus Torvalds case PR_SET_UNALIGN:
2470b6dff3ecSDavid Howells error = SET_UNALIGN_CTL(me, arg2);
24711da177e4SLinus Torvalds break;
24721da177e4SLinus Torvalds case PR_GET_UNALIGN:
2473b6dff3ecSDavid Howells error = GET_UNALIGN_CTL(me, arg2);
24741da177e4SLinus Torvalds break;
24751da177e4SLinus Torvalds case PR_SET_FPEMU:
2476b6dff3ecSDavid Howells error = SET_FPEMU_CTL(me, arg2);
24771da177e4SLinus Torvalds break;
24781da177e4SLinus Torvalds case PR_GET_FPEMU:
2479b6dff3ecSDavid Howells error = GET_FPEMU_CTL(me, arg2);
24801da177e4SLinus Torvalds break;
24811da177e4SLinus Torvalds case PR_SET_FPEXC:
2482b6dff3ecSDavid Howells error = SET_FPEXC_CTL(me, arg2);
24831da177e4SLinus Torvalds break;
24841da177e4SLinus Torvalds case PR_GET_FPEXC:
2485b6dff3ecSDavid Howells error = GET_FPEXC_CTL(me, arg2);
24861da177e4SLinus Torvalds break;
24871da177e4SLinus Torvalds case PR_GET_TIMING:
24881da177e4SLinus Torvalds error = PR_TIMING_STATISTICAL;
24891da177e4SLinus Torvalds break;
24901da177e4SLinus Torvalds case PR_SET_TIMING:
24917b26655fSShi Weihua if (arg2 != PR_TIMING_STATISTICAL)
24921da177e4SLinus Torvalds error = -EINVAL;
24931da177e4SLinus Torvalds break;
2494b6dff3ecSDavid Howells case PR_SET_NAME:
2495b6dff3ecSDavid Howells comm[sizeof(me->comm) - 1] = 0;
2496b6dff3ecSDavid Howells if (strncpy_from_user(comm, (char __user *)arg2,
24971da177e4SLinus Torvalds sizeof(me->comm) - 1) < 0)
24981da177e4SLinus Torvalds return -EFAULT;
2499b6dff3ecSDavid Howells set_task_comm(me, comm);
2500f786ecbaSVladimir Zapolskiy proc_comm_connector(me);
2501f1fd75bfSSasikantha babu break;
2502b6dff3ecSDavid Howells case PR_GET_NAME:
2503b6dff3ecSDavid Howells get_task_comm(comm, me);
2504f3cbd435SAndrew Morton if (copy_to_user((char __user *)arg2, comm, sizeof(comm)))
25051da177e4SLinus Torvalds return -EFAULT;
2506f1fd75bfSSasikantha babu break;
2507651d765dSAnton Blanchard case PR_GET_ENDIAN:
2508b6dff3ecSDavid Howells error = GET_ENDIAN(me, arg2);
2509651d765dSAnton Blanchard break;
2510651d765dSAnton Blanchard case PR_SET_ENDIAN:
2511b6dff3ecSDavid Howells error = SET_ENDIAN(me, arg2);
2512651d765dSAnton Blanchard break;
25131d9d02feSAndrea Arcangeli case PR_GET_SECCOMP:
25141d9d02feSAndrea Arcangeli error = prctl_get_seccomp();
25151d9d02feSAndrea Arcangeli break;
25161d9d02feSAndrea Arcangeli case PR_SET_SECCOMP:
2517e2cfabdfSWill Drewry error = prctl_set_seccomp(arg2, (char __user *)arg3);
25181d9d02feSAndrea Arcangeli break;
25198fb402bcSErik Bosman case PR_GET_TSC:
25208fb402bcSErik Bosman error = GET_TSC_CTL(arg2);
25218fb402bcSErik Bosman break;
25228fb402bcSErik Bosman case PR_SET_TSC:
25238fb402bcSErik Bosman error = SET_TSC_CTL(arg2);
25248fb402bcSErik Bosman break;
2525cdd6c482SIngo Molnar case PR_TASK_PERF_EVENTS_DISABLE:
2526cdd6c482SIngo Molnar error = perf_event_task_disable();
25271d1c7ddbSIngo Molnar break;
2528cdd6c482SIngo Molnar case PR_TASK_PERF_EVENTS_ENABLE:
2529cdd6c482SIngo Molnar error = perf_event_task_enable();
25301d1c7ddbSIngo Molnar break;
25316976675dSArjan van de Ven case PR_GET_TIMERSLACK:
2532da8b44d5SJohn Stultz if (current->timer_slack_ns > ULONG_MAX)
2533da8b44d5SJohn Stultz error = ULONG_MAX;
2534da8b44d5SJohn Stultz else
25356976675dSArjan van de Ven error = current->timer_slack_ns;
25366976675dSArjan van de Ven break;
25376976675dSArjan van de Ven case PR_SET_TIMERSLACK:
25386976675dSArjan van de Ven if (arg2 <= 0)
25396976675dSArjan van de Ven current->timer_slack_ns =
25406976675dSArjan van de Ven current->default_timer_slack_ns;
25416976675dSArjan van de Ven else
25426976675dSArjan van de Ven current->timer_slack_ns = arg2;
25436976675dSArjan van de Ven break;
25444db96cf0SAndi Kleen case PR_MCE_KILL:
25454db96cf0SAndi Kleen if (arg4 | arg5)
25464db96cf0SAndi Kleen return -EINVAL;
25474db96cf0SAndi Kleen switch (arg2) {
25481087e9b4SAndi Kleen case PR_MCE_KILL_CLEAR:
25494db96cf0SAndi Kleen if (arg3 != 0)
25504db96cf0SAndi Kleen return -EINVAL;
25514db96cf0SAndi Kleen current->flags &= ~PF_MCE_PROCESS;
25524db96cf0SAndi Kleen break;
25531087e9b4SAndi Kleen case PR_MCE_KILL_SET:
25544db96cf0SAndi Kleen current->flags |= PF_MCE_PROCESS;
25551087e9b4SAndi Kleen if (arg3 == PR_MCE_KILL_EARLY)
25564db96cf0SAndi Kleen current->flags |= PF_MCE_EARLY;
25571087e9b4SAndi Kleen else if (arg3 == PR_MCE_KILL_LATE)
25584db96cf0SAndi Kleen current->flags &= ~PF_MCE_EARLY;
25591087e9b4SAndi Kleen else if (arg3 == PR_MCE_KILL_DEFAULT)
25601087e9b4SAndi Kleen current->flags &=
25611087e9b4SAndi Kleen ~(PF_MCE_EARLY|PF_MCE_PROCESS);
25621087e9b4SAndi Kleen else
25631087e9b4SAndi Kleen return -EINVAL;
25644db96cf0SAndi Kleen break;
25654db96cf0SAndi Kleen default:
25664db96cf0SAndi Kleen return -EINVAL;
25674db96cf0SAndi Kleen }
25684db96cf0SAndi Kleen break;
25691087e9b4SAndi Kleen case PR_MCE_KILL_GET:
25701087e9b4SAndi Kleen if (arg2 | arg3 | arg4 | arg5)
25711087e9b4SAndi Kleen return -EINVAL;
25721087e9b4SAndi Kleen if (current->flags & PF_MCE_PROCESS)
25731087e9b4SAndi Kleen error = (current->flags & PF_MCE_EARLY) ?
25741087e9b4SAndi Kleen PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
25751087e9b4SAndi Kleen else
25761087e9b4SAndi Kleen error = PR_MCE_KILL_DEFAULT;
25771087e9b4SAndi Kleen break;
2578028ee4beSCyrill Gorcunov case PR_SET_MM:
2579028ee4beSCyrill Gorcunov error = prctl_set_mm(arg2, arg3, arg4, arg5);
2580028ee4beSCyrill Gorcunov break;
25815702c5eeSCyrill Gorcunov case PR_GET_TID_ADDRESS:
2582986b9eacSRasmus Villemoes error = prctl_get_tid_address(me, (int __user * __user *)arg2);
25835702c5eeSCyrill Gorcunov break;
2584ebec18a6SLennart Poettering case PR_SET_CHILD_SUBREAPER:
2585ebec18a6SLennart Poettering me->signal->is_child_subreaper = !!arg2;
2586749860ceSPavel Tikhomirov if (!arg2)
2587749860ceSPavel Tikhomirov break;
2588749860ceSPavel Tikhomirov
2589749860ceSPavel Tikhomirov walk_process_tree(me, propagate_has_child_subreaper, NULL);
2590ebec18a6SLennart Poettering break;
2591ebec18a6SLennart Poettering case PR_GET_CHILD_SUBREAPER:
2592ebec18a6SLennart Poettering error = put_user(me->signal->is_child_subreaper,
2593ebec18a6SLennart Poettering (int __user *)arg2);
2594ebec18a6SLennart Poettering break;
2595259e5e6cSAndy Lutomirski case PR_SET_NO_NEW_PRIVS:
2596259e5e6cSAndy Lutomirski if (arg2 != 1 || arg3 || arg4 || arg5)
2597259e5e6cSAndy Lutomirski return -EINVAL;
2598259e5e6cSAndy Lutomirski
25991d4457f9SKees Cook task_set_no_new_privs(current);
2600259e5e6cSAndy Lutomirski break;
2601259e5e6cSAndy Lutomirski case PR_GET_NO_NEW_PRIVS:
2602259e5e6cSAndy Lutomirski if (arg2 || arg3 || arg4 || arg5)
2603259e5e6cSAndy Lutomirski return -EINVAL;
26041d4457f9SKees Cook return task_no_new_privs(current) ? 1 : 0;
2605a0715cc2SAlex Thorlton case PR_GET_THP_DISABLE:
2606a0715cc2SAlex Thorlton if (arg2 || arg3 || arg4 || arg5)
2607a0715cc2SAlex Thorlton return -EINVAL;
260818600332SMichal Hocko error = !!test_bit(MMF_DISABLE_THP, &me->mm->flags);
2609a0715cc2SAlex Thorlton break;
2610a0715cc2SAlex Thorlton case PR_SET_THP_DISABLE:
2611a0715cc2SAlex Thorlton if (arg3 || arg4 || arg5)
2612a0715cc2SAlex Thorlton return -EINVAL;
2613d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(me->mm))
261417b0573dSMichal Hocko return -EINTR;
2615a0715cc2SAlex Thorlton if (arg2)
261618600332SMichal Hocko set_bit(MMF_DISABLE_THP, &me->mm->flags);
2617a0715cc2SAlex Thorlton else
261818600332SMichal Hocko clear_bit(MMF_DISABLE_THP, &me->mm->flags);
2619d8ed45c5SMichel Lespinasse mmap_write_unlock(me->mm);
2620a0715cc2SAlex Thorlton break;
2621fe3d197fSDave Hansen case PR_MPX_ENABLE_MANAGEMENT:
2622fe3d197fSDave Hansen case PR_MPX_DISABLE_MANAGEMENT:
2623f240652bSDave Hansen /* No longer implemented: */
2624e9d1b4f3SDave Hansen return -EINVAL;
26259791554bSPaul Burton case PR_SET_FP_MODE:
26269791554bSPaul Burton error = SET_FP_MODE(me, arg2);
26279791554bSPaul Burton break;
26289791554bSPaul Burton case PR_GET_FP_MODE:
26299791554bSPaul Burton error = GET_FP_MODE(me);
26309791554bSPaul Burton break;
26312d2123bcSDave Martin case PR_SVE_SET_VL:
26322d2123bcSDave Martin error = SVE_SET_VL(arg2);
26332d2123bcSDave Martin break;
26342d2123bcSDave Martin case PR_SVE_GET_VL:
26352d2123bcSDave Martin error = SVE_GET_VL();
26362d2123bcSDave Martin break;
26379e4ab6c8SMark Brown case PR_SME_SET_VL:
26389e4ab6c8SMark Brown error = SME_SET_VL(arg2);
26399e4ab6c8SMark Brown break;
26409e4ab6c8SMark Brown case PR_SME_GET_VL:
26419e4ab6c8SMark Brown error = SME_GET_VL();
26429e4ab6c8SMark Brown break;
2643b617cfc8SThomas Gleixner case PR_GET_SPECULATION_CTRL:
2644b617cfc8SThomas Gleixner if (arg3 || arg4 || arg5)
2645b617cfc8SThomas Gleixner return -EINVAL;
26467bbf1373SKees Cook error = arch_prctl_spec_ctrl_get(me, arg2);
2647b617cfc8SThomas Gleixner break;
2648b617cfc8SThomas Gleixner case PR_SET_SPECULATION_CTRL:
2649b617cfc8SThomas Gleixner if (arg4 || arg5)
2650b617cfc8SThomas Gleixner return -EINVAL;
26517bbf1373SKees Cook error = arch_prctl_spec_ctrl_set(me, arg2, arg3);
2652b617cfc8SThomas Gleixner break;
2653ba830885SKristina Martsenko case PR_PAC_RESET_KEYS:
2654ba830885SKristina Martsenko if (arg3 || arg4 || arg5)
2655ba830885SKristina Martsenko return -EINVAL;
2656ba830885SKristina Martsenko error = PAC_RESET_KEYS(me, arg2);
2657ba830885SKristina Martsenko break;
265820169862SPeter Collingbourne case PR_PAC_SET_ENABLED_KEYS:
265920169862SPeter Collingbourne if (arg4 || arg5)
266020169862SPeter Collingbourne return -EINVAL;
266120169862SPeter Collingbourne error = PAC_SET_ENABLED_KEYS(me, arg2, arg3);
266220169862SPeter Collingbourne break;
266320169862SPeter Collingbourne case PR_PAC_GET_ENABLED_KEYS:
266420169862SPeter Collingbourne if (arg2 || arg3 || arg4 || arg5)
266520169862SPeter Collingbourne return -EINVAL;
266620169862SPeter Collingbourne error = PAC_GET_ENABLED_KEYS(me);
266720169862SPeter Collingbourne break;
266863f0c603SCatalin Marinas case PR_SET_TAGGED_ADDR_CTRL:
26693e91ec89SCatalin Marinas if (arg3 || arg4 || arg5)
26703e91ec89SCatalin Marinas return -EINVAL;
267163f0c603SCatalin Marinas error = SET_TAGGED_ADDR_CTRL(arg2);
267263f0c603SCatalin Marinas break;
267363f0c603SCatalin Marinas case PR_GET_TAGGED_ADDR_CTRL:
26743e91ec89SCatalin Marinas if (arg2 || arg3 || arg4 || arg5)
26753e91ec89SCatalin Marinas return -EINVAL;
267663f0c603SCatalin Marinas error = GET_TAGGED_ADDR_CTRL();
267763f0c603SCatalin Marinas break;
26788d19f1c8SMike Christie case PR_SET_IO_FLUSHER:
26798d19f1c8SMike Christie if (!capable(CAP_SYS_RESOURCE))
26808d19f1c8SMike Christie return -EPERM;
26818d19f1c8SMike Christie
26828d19f1c8SMike Christie if (arg3 || arg4 || arg5)
26838d19f1c8SMike Christie return -EINVAL;
26848d19f1c8SMike Christie
26858d19f1c8SMike Christie if (arg2 == 1)
26868d19f1c8SMike Christie current->flags |= PR_IO_FLUSHER;
26878d19f1c8SMike Christie else if (!arg2)
26888d19f1c8SMike Christie current->flags &= ~PR_IO_FLUSHER;
26898d19f1c8SMike Christie else
26908d19f1c8SMike Christie return -EINVAL;
26918d19f1c8SMike Christie break;
26928d19f1c8SMike Christie case PR_GET_IO_FLUSHER:
26938d19f1c8SMike Christie if (!capable(CAP_SYS_RESOURCE))
26948d19f1c8SMike Christie return -EPERM;
26958d19f1c8SMike Christie
26968d19f1c8SMike Christie if (arg2 || arg3 || arg4 || arg5)
26978d19f1c8SMike Christie return -EINVAL;
26988d19f1c8SMike Christie
26998d19f1c8SMike Christie error = (current->flags & PR_IO_FLUSHER) == PR_IO_FLUSHER;
27008d19f1c8SMike Christie break;
27011446e1dfSGabriel Krisman Bertazi case PR_SET_SYSCALL_USER_DISPATCH:
27021446e1dfSGabriel Krisman Bertazi error = set_syscall_user_dispatch(arg2, arg3, arg4,
27031446e1dfSGabriel Krisman Bertazi (char __user *) arg5);
27041446e1dfSGabriel Krisman Bertazi break;
27057ac592aaSChris Hyser #ifdef CONFIG_SCHED_CORE
27067ac592aaSChris Hyser case PR_SCHED_CORE:
27077ac592aaSChris Hyser error = sched_core_share_pid(arg2, arg3, arg4, arg5);
27087ac592aaSChris Hyser break;
27097ac592aaSChris Hyser #endif
2710b507808eSJoey Gouly case PR_SET_MDWE:
2711b507808eSJoey Gouly error = prctl_set_mdwe(arg2, arg3, arg4, arg5);
2712b507808eSJoey Gouly break;
2713b507808eSJoey Gouly case PR_GET_MDWE:
2714b507808eSJoey Gouly error = prctl_get_mdwe(arg2, arg3, arg4, arg5);
2715b507808eSJoey Gouly break;
27169a10064fSColin Cross case PR_SET_VMA:
27179a10064fSColin Cross error = prctl_set_vma(arg2, arg3, arg4, arg5);
27189a10064fSColin Cross break;
2719636e3483SMiguel Ojeda case PR_GET_AUXV:
2720636e3483SMiguel Ojeda if (arg4 || arg5)
2721636e3483SMiguel Ojeda return -EINVAL;
2722636e3483SMiguel Ojeda error = prctl_get_auxv((void __user *)arg2, arg3);
2723636e3483SMiguel Ojeda break;
2724d7597f59SStefan Roesch #ifdef CONFIG_KSM
2725d7597f59SStefan Roesch case PR_SET_MEMORY_MERGE:
2726d7597f59SStefan Roesch if (arg3 || arg4 || arg5)
2727d7597f59SStefan Roesch return -EINVAL;
2728d7597f59SStefan Roesch if (mmap_write_lock_killable(me->mm))
2729d7597f59SStefan Roesch return -EINTR;
2730d7597f59SStefan Roesch
273124139c07SDavid Hildenbrand if (arg2)
2732d7597f59SStefan Roesch error = ksm_enable_merge_any(me->mm);
273324139c07SDavid Hildenbrand else
273424139c07SDavid Hildenbrand error = ksm_disable_merge_any(me->mm);
2735d7597f59SStefan Roesch mmap_write_unlock(me->mm);
2736d7597f59SStefan Roesch break;
2737d7597f59SStefan Roesch case PR_GET_MEMORY_MERGE:
2738d7597f59SStefan Roesch if (arg2 || arg3 || arg4 || arg5)
2739d7597f59SStefan Roesch return -EINVAL;
2740d7597f59SStefan Roesch
2741d7597f59SStefan Roesch error = !!test_bit(MMF_VM_MERGE_ANY, &me->mm->flags);
2742d7597f59SStefan Roesch break;
2743d7597f59SStefan Roesch #endif
27441fd96a3eSAndy Chiu case PR_RISCV_V_SET_CONTROL:
27451fd96a3eSAndy Chiu error = RISCV_V_SET_CONTROL(arg2);
27461fd96a3eSAndy Chiu break;
27471fd96a3eSAndy Chiu case PR_RISCV_V_GET_CONTROL:
27481fd96a3eSAndy Chiu error = RISCV_V_GET_CONTROL();
27491fd96a3eSAndy Chiu break;
27501da177e4SLinus Torvalds default:
27511da177e4SLinus Torvalds error = -EINVAL;
27521da177e4SLinus Torvalds break;
27531da177e4SLinus Torvalds }
27541da177e4SLinus Torvalds return error;
27551da177e4SLinus Torvalds }
27563cfc348bSAndi Kleen
SYSCALL_DEFINE3(getcpu,unsigned __user *,cpup,unsigned __user *,nodep,struct getcpu_cache __user *,unused)2757836f92adSHeiko Carstens SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
2758836f92adSHeiko Carstens struct getcpu_cache __user *, unused)
27593cfc348bSAndi Kleen {
27603cfc348bSAndi Kleen int err = 0;
27613cfc348bSAndi Kleen int cpu = raw_smp_processor_id();
2762ec94fc3dSvishnu.ps
27633cfc348bSAndi Kleen if (cpup)
27643cfc348bSAndi Kleen err |= put_user(cpu, cpup);
27653cfc348bSAndi Kleen if (nodep)
27663cfc348bSAndi Kleen err |= put_user(cpu_to_node(cpu), nodep);
27673cfc348bSAndi Kleen return err ? -EFAULT : 0;
27683cfc348bSAndi Kleen }
276910a0a8d4SJeremy Fitzhardinge
27704a22f166SStephen Rothwell /**
27714a22f166SStephen Rothwell * do_sysinfo - fill in sysinfo struct
27724a22f166SStephen Rothwell * @info: pointer to buffer to fill
27734a22f166SStephen Rothwell */
do_sysinfo(struct sysinfo * info)27744a22f166SStephen Rothwell static int do_sysinfo(struct sysinfo *info)
27754a22f166SStephen Rothwell {
27764a22f166SStephen Rothwell unsigned long mem_total, sav_total;
27774a22f166SStephen Rothwell unsigned int mem_unit, bitcount;
2778dc1b7b6cSArnd Bergmann struct timespec64 tp;
27794a22f166SStephen Rothwell
27804a22f166SStephen Rothwell memset(info, 0, sizeof(struct sysinfo));
27814a22f166SStephen Rothwell
2782dc1b7b6cSArnd Bergmann ktime_get_boottime_ts64(&tp);
2783ecc421e0SCyril Hrubis timens_add_boottime(&tp);
27844a22f166SStephen Rothwell info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
27854a22f166SStephen Rothwell
27864a22f166SStephen Rothwell get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
27874a22f166SStephen Rothwell
27884a22f166SStephen Rothwell info->procs = nr_threads;
27894a22f166SStephen Rothwell
27904a22f166SStephen Rothwell si_meminfo(info);
27914a22f166SStephen Rothwell si_swapinfo(info);
27924a22f166SStephen Rothwell
27934a22f166SStephen Rothwell /*
27944a22f166SStephen Rothwell * If the sum of all the available memory (i.e. ram + swap)
27954a22f166SStephen Rothwell * is less than can be stored in a 32 bit unsigned long then
27964a22f166SStephen Rothwell * we can be binary compatible with 2.2.x kernels. If not,
27974a22f166SStephen Rothwell * well, in that case 2.2.x was broken anyways...
27984a22f166SStephen Rothwell *
27994a22f166SStephen Rothwell * -Erik Andersen <andersee@debian.org>
28004a22f166SStephen Rothwell */
28014a22f166SStephen Rothwell
28024a22f166SStephen Rothwell mem_total = info->totalram + info->totalswap;
28034a22f166SStephen Rothwell if (mem_total < info->totalram || mem_total < info->totalswap)
28044a22f166SStephen Rothwell goto out;
28054a22f166SStephen Rothwell bitcount = 0;
28064a22f166SStephen Rothwell mem_unit = info->mem_unit;
28074a22f166SStephen Rothwell while (mem_unit > 1) {
28084a22f166SStephen Rothwell bitcount++;
28094a22f166SStephen Rothwell mem_unit >>= 1;
28104a22f166SStephen Rothwell sav_total = mem_total;
28114a22f166SStephen Rothwell mem_total <<= 1;
28124a22f166SStephen Rothwell if (mem_total < sav_total)
28134a22f166SStephen Rothwell goto out;
28144a22f166SStephen Rothwell }
28154a22f166SStephen Rothwell
28164a22f166SStephen Rothwell /*
28174a22f166SStephen Rothwell * If mem_total did not overflow, multiply all memory values by
28184a22f166SStephen Rothwell * info->mem_unit and set it to 1. This leaves things compatible
28194a22f166SStephen Rothwell * with 2.2.x, and also retains compatibility with earlier 2.4.x
28204a22f166SStephen Rothwell * kernels...
28214a22f166SStephen Rothwell */
28224a22f166SStephen Rothwell
28234a22f166SStephen Rothwell info->mem_unit = 1;
28244a22f166SStephen Rothwell info->totalram <<= bitcount;
28254a22f166SStephen Rothwell info->freeram <<= bitcount;
28264a22f166SStephen Rothwell info->sharedram <<= bitcount;
28274a22f166SStephen Rothwell info->bufferram <<= bitcount;
28284a22f166SStephen Rothwell info->totalswap <<= bitcount;
28294a22f166SStephen Rothwell info->freeswap <<= bitcount;
28304a22f166SStephen Rothwell info->totalhigh <<= bitcount;
28314a22f166SStephen Rothwell info->freehigh <<= bitcount;
28324a22f166SStephen Rothwell
28334a22f166SStephen Rothwell out:
28344a22f166SStephen Rothwell return 0;
28354a22f166SStephen Rothwell }
28364a22f166SStephen Rothwell
SYSCALL_DEFINE1(sysinfo,struct sysinfo __user *,info)28374a22f166SStephen Rothwell SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
28384a22f166SStephen Rothwell {
28394a22f166SStephen Rothwell struct sysinfo val;
28404a22f166SStephen Rothwell
28414a22f166SStephen Rothwell do_sysinfo(&val);
28424a22f166SStephen Rothwell
28434a22f166SStephen Rothwell if (copy_to_user(info, &val, sizeof(struct sysinfo)))
28444a22f166SStephen Rothwell return -EFAULT;
28454a22f166SStephen Rothwell
28464a22f166SStephen Rothwell return 0;
28474a22f166SStephen Rothwell }
28484a22f166SStephen Rothwell
28494a22f166SStephen Rothwell #ifdef CONFIG_COMPAT
28504a22f166SStephen Rothwell struct compat_sysinfo {
28514a22f166SStephen Rothwell s32 uptime;
28524a22f166SStephen Rothwell u32 loads[3];
28534a22f166SStephen Rothwell u32 totalram;
28544a22f166SStephen Rothwell u32 freeram;
28554a22f166SStephen Rothwell u32 sharedram;
28564a22f166SStephen Rothwell u32 bufferram;
28574a22f166SStephen Rothwell u32 totalswap;
28584a22f166SStephen Rothwell u32 freeswap;
28594a22f166SStephen Rothwell u16 procs;
28604a22f166SStephen Rothwell u16 pad;
28614a22f166SStephen Rothwell u32 totalhigh;
28624a22f166SStephen Rothwell u32 freehigh;
28634a22f166SStephen Rothwell u32 mem_unit;
28644a22f166SStephen Rothwell char _f[20-2*sizeof(u32)-sizeof(int)];
28654a22f166SStephen Rothwell };
28664a22f166SStephen Rothwell
COMPAT_SYSCALL_DEFINE1(sysinfo,struct compat_sysinfo __user *,info)28674a22f166SStephen Rothwell COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
28684a22f166SStephen Rothwell {
28694a22f166SStephen Rothwell struct sysinfo s;
2870ce5155c4SAl Viro struct compat_sysinfo s_32;
28714a22f166SStephen Rothwell
28724a22f166SStephen Rothwell do_sysinfo(&s);
28734a22f166SStephen Rothwell
28744a22f166SStephen Rothwell /* Check to see if any memory value is too large for 32-bit and scale
28754a22f166SStephen Rothwell * down if needed
28764a22f166SStephen Rothwell */
28770baae41eSScotty Bauer if (upper_32_bits(s.totalram) || upper_32_bits(s.totalswap)) {
28784a22f166SStephen Rothwell int bitcount = 0;
28794a22f166SStephen Rothwell
28804a22f166SStephen Rothwell while (s.mem_unit < PAGE_SIZE) {
28814a22f166SStephen Rothwell s.mem_unit <<= 1;
28824a22f166SStephen Rothwell bitcount++;
28834a22f166SStephen Rothwell }
28844a22f166SStephen Rothwell
28854a22f166SStephen Rothwell s.totalram >>= bitcount;
28864a22f166SStephen Rothwell s.freeram >>= bitcount;
28874a22f166SStephen Rothwell s.sharedram >>= bitcount;
28884a22f166SStephen Rothwell s.bufferram >>= bitcount;
28894a22f166SStephen Rothwell s.totalswap >>= bitcount;
28904a22f166SStephen Rothwell s.freeswap >>= bitcount;
28914a22f166SStephen Rothwell s.totalhigh >>= bitcount;
28924a22f166SStephen Rothwell s.freehigh >>= bitcount;
28934a22f166SStephen Rothwell }
28944a22f166SStephen Rothwell
2895ce5155c4SAl Viro memset(&s_32, 0, sizeof(s_32));
2896ce5155c4SAl Viro s_32.uptime = s.uptime;
2897ce5155c4SAl Viro s_32.loads[0] = s.loads[0];
2898ce5155c4SAl Viro s_32.loads[1] = s.loads[1];
2899ce5155c4SAl Viro s_32.loads[2] = s.loads[2];
2900ce5155c4SAl Viro s_32.totalram = s.totalram;
2901ce5155c4SAl Viro s_32.freeram = s.freeram;
2902ce5155c4SAl Viro s_32.sharedram = s.sharedram;
2903ce5155c4SAl Viro s_32.bufferram = s.bufferram;
2904ce5155c4SAl Viro s_32.totalswap = s.totalswap;
2905ce5155c4SAl Viro s_32.freeswap = s.freeswap;
2906ce5155c4SAl Viro s_32.procs = s.procs;
2907ce5155c4SAl Viro s_32.totalhigh = s.totalhigh;
2908ce5155c4SAl Viro s_32.freehigh = s.freehigh;
2909ce5155c4SAl Viro s_32.mem_unit = s.mem_unit;
2910ce5155c4SAl Viro if (copy_to_user(info, &s_32, sizeof(s_32)))
29114a22f166SStephen Rothwell return -EFAULT;
29124a22f166SStephen Rothwell return 0;
29134a22f166SStephen Rothwell }
29144a22f166SStephen Rothwell #endif /* CONFIG_COMPAT */
2915