xref: /linux-6.15/kernel/sys.c (revision ec2d0c04)
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 
78c38904ebSMarco Elver #include <trace/events/task.h>
79c38904ebSMarco Elver 
80e530dca5SDominik Brodowski #include "uid16.h"
81e530dca5SDominik Brodowski 
821da177e4SLinus Torvalds #ifndef SET_UNALIGN_CTL
831da177e4SLinus Torvalds # define SET_UNALIGN_CTL(a, b)	(-EINVAL)
841da177e4SLinus Torvalds #endif
851da177e4SLinus Torvalds #ifndef GET_UNALIGN_CTL
861da177e4SLinus Torvalds # define GET_UNALIGN_CTL(a, b)	(-EINVAL)
871da177e4SLinus Torvalds #endif
881da177e4SLinus Torvalds #ifndef SET_FPEMU_CTL
891da177e4SLinus Torvalds # define SET_FPEMU_CTL(a, b)	(-EINVAL)
901da177e4SLinus Torvalds #endif
911da177e4SLinus Torvalds #ifndef GET_FPEMU_CTL
921da177e4SLinus Torvalds # define GET_FPEMU_CTL(a, b)	(-EINVAL)
931da177e4SLinus Torvalds #endif
941da177e4SLinus Torvalds #ifndef SET_FPEXC_CTL
951da177e4SLinus Torvalds # define SET_FPEXC_CTL(a, b)	(-EINVAL)
961da177e4SLinus Torvalds #endif
971da177e4SLinus Torvalds #ifndef GET_FPEXC_CTL
981da177e4SLinus Torvalds # define GET_FPEXC_CTL(a, b)	(-EINVAL)
991da177e4SLinus Torvalds #endif
100651d765dSAnton Blanchard #ifndef GET_ENDIAN
101651d765dSAnton Blanchard # define GET_ENDIAN(a, b)	(-EINVAL)
102651d765dSAnton Blanchard #endif
103651d765dSAnton Blanchard #ifndef SET_ENDIAN
104651d765dSAnton Blanchard # define SET_ENDIAN(a, b)	(-EINVAL)
105651d765dSAnton Blanchard #endif
1068fb402bcSErik Bosman #ifndef GET_TSC_CTL
1078fb402bcSErik Bosman # define GET_TSC_CTL(a)		(-EINVAL)
1088fb402bcSErik Bosman #endif
1098fb402bcSErik Bosman #ifndef SET_TSC_CTL
1108fb402bcSErik Bosman # define SET_TSC_CTL(a)		(-EINVAL)
1118fb402bcSErik Bosman #endif
1129791554bSPaul Burton #ifndef GET_FP_MODE
1139791554bSPaul Burton # define GET_FP_MODE(a)		(-EINVAL)
1149791554bSPaul Burton #endif
1159791554bSPaul Burton #ifndef SET_FP_MODE
1169791554bSPaul Burton # define SET_FP_MODE(a,b)	(-EINVAL)
1179791554bSPaul Burton #endif
1182d2123bcSDave Martin #ifndef SVE_SET_VL
1192d2123bcSDave Martin # define SVE_SET_VL(a)		(-EINVAL)
1202d2123bcSDave Martin #endif
1212d2123bcSDave Martin #ifndef SVE_GET_VL
1222d2123bcSDave Martin # define SVE_GET_VL()		(-EINVAL)
1232d2123bcSDave Martin #endif
1249e4ab6c8SMark Brown #ifndef SME_SET_VL
1259e4ab6c8SMark Brown # define SME_SET_VL(a)		(-EINVAL)
1269e4ab6c8SMark Brown #endif
1279e4ab6c8SMark Brown #ifndef SME_GET_VL
1289e4ab6c8SMark Brown # define SME_GET_VL()		(-EINVAL)
1299e4ab6c8SMark Brown #endif
130ba830885SKristina Martsenko #ifndef PAC_RESET_KEYS
131ba830885SKristina Martsenko # define PAC_RESET_KEYS(a, b)	(-EINVAL)
132ba830885SKristina Martsenko #endif
13320169862SPeter Collingbourne #ifndef PAC_SET_ENABLED_KEYS
13420169862SPeter Collingbourne # define PAC_SET_ENABLED_KEYS(a, b, c)	(-EINVAL)
13520169862SPeter Collingbourne #endif
13620169862SPeter Collingbourne #ifndef PAC_GET_ENABLED_KEYS
13720169862SPeter Collingbourne # define PAC_GET_ENABLED_KEYS(a)	(-EINVAL)
13820169862SPeter Collingbourne #endif
13963f0c603SCatalin Marinas #ifndef SET_TAGGED_ADDR_CTRL
14063f0c603SCatalin Marinas # define SET_TAGGED_ADDR_CTRL(a)	(-EINVAL)
14163f0c603SCatalin Marinas #endif
14263f0c603SCatalin Marinas #ifndef GET_TAGGED_ADDR_CTRL
14363f0c603SCatalin Marinas # define GET_TAGGED_ADDR_CTRL()		(-EINVAL)
14463f0c603SCatalin Marinas #endif
1451fd96a3eSAndy Chiu #ifndef RISCV_V_SET_CONTROL
1461fd96a3eSAndy Chiu # define RISCV_V_SET_CONTROL(a)		(-EINVAL)
1471fd96a3eSAndy Chiu #endif
1481fd96a3eSAndy Chiu #ifndef RISCV_V_GET_CONTROL
1491fd96a3eSAndy Chiu # define RISCV_V_GET_CONTROL()		(-EINVAL)
1501fd96a3eSAndy Chiu #endif
1516b9391b5SCharlie Jenkins #ifndef RISCV_SET_ICACHE_FLUSH_CTX
1526b9391b5SCharlie Jenkins # define RISCV_SET_ICACHE_FLUSH_CTX(a, b)	(-EINVAL)
1536b9391b5SCharlie Jenkins #endif
154628d701fSBenjamin Gray #ifndef PPC_GET_DEXCR_ASPECT
155628d701fSBenjamin Gray # define PPC_GET_DEXCR_ASPECT(a, b)	(-EINVAL)
156628d701fSBenjamin Gray #endif
157628d701fSBenjamin Gray #ifndef PPC_SET_DEXCR_ASPECT
158628d701fSBenjamin Gray # define PPC_SET_DEXCR_ASPECT(a, b, c)	(-EINVAL)
159628d701fSBenjamin Gray #endif
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds /*
1621da177e4SLinus Torvalds  * this is where the system-wide overflow UID and GID are defined, for
1631da177e4SLinus Torvalds  * architectures that now have 32-bit UID/GID but didn't in the past
1641da177e4SLinus Torvalds  */
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds int overflowuid = DEFAULT_OVERFLOWUID;
1671da177e4SLinus Torvalds int overflowgid = DEFAULT_OVERFLOWGID;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds EXPORT_SYMBOL(overflowuid);
1701da177e4SLinus Torvalds EXPORT_SYMBOL(overflowgid);
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds /*
1731da177e4SLinus Torvalds  * the same as above, but for filesystems which can only store a 16-bit
1741da177e4SLinus Torvalds  * UID and GID. as such, this is needed on all architectures
1751da177e4SLinus Torvalds  */
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
1788b2770a4SWolffhardt Schwabe int fs_overflowgid = DEFAULT_FS_OVERFLOWGID;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds EXPORT_SYMBOL(fs_overflowuid);
1811da177e4SLinus Torvalds EXPORT_SYMBOL(fs_overflowgid);
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds /*
184fc832ad3SSerge E. Hallyn  * Returns true if current's euid is same as p's uid or euid,
185fc832ad3SSerge E. Hallyn  * or has CAP_SYS_NICE to p's user_ns.
186fc832ad3SSerge E. Hallyn  *
187fc832ad3SSerge E. Hallyn  * Called with rcu_read_lock, creds are safe
188fc832ad3SSerge E. Hallyn  */
set_one_prio_perm(struct task_struct * p)189fc832ad3SSerge E. Hallyn static bool set_one_prio_perm(struct task_struct *p)
190fc832ad3SSerge E. Hallyn {
191fc832ad3SSerge E. Hallyn 	const struct cred *cred = current_cred(), *pcred = __task_cred(p);
192fc832ad3SSerge E. Hallyn 
1935af66203SEric W. Biederman 	if (uid_eq(pcred->uid,  cred->euid) ||
1945af66203SEric W. Biederman 	    uid_eq(pcred->euid, cred->euid))
195fc832ad3SSerge E. Hallyn 		return true;
196c4a4d603SEric W. Biederman 	if (ns_capable(pcred->user_ns, CAP_SYS_NICE))
197fc832ad3SSerge E. Hallyn 		return true;
198fc832ad3SSerge E. Hallyn 	return false;
199fc832ad3SSerge E. Hallyn }
200fc832ad3SSerge E. Hallyn 
201fc832ad3SSerge E. Hallyn /*
202c69e8d9cSDavid Howells  * set the priority of a task
203c69e8d9cSDavid Howells  * - the caller must hold the RCU read lock
204c69e8d9cSDavid Howells  */
set_one_prio(struct task_struct * p,int niceval,int error)2051da177e4SLinus Torvalds static int set_one_prio(struct task_struct *p, int niceval, int error)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds 	int no_nice;
2081da177e4SLinus Torvalds 
209fc832ad3SSerge E. Hallyn 	if (!set_one_prio_perm(p)) {
2101da177e4SLinus Torvalds 		error = -EPERM;
2111da177e4SLinus Torvalds 		goto out;
2121da177e4SLinus Torvalds 	}
213e43379f1SMatt Mackall 	if (niceval < task_nice(p) && !can_nice(p, niceval)) {
2141da177e4SLinus Torvalds 		error = -EACCES;
2151da177e4SLinus Torvalds 		goto out;
2161da177e4SLinus Torvalds 	}
2171da177e4SLinus Torvalds 	no_nice = security_task_setnice(p, niceval);
2181da177e4SLinus Torvalds 	if (no_nice) {
2191da177e4SLinus Torvalds 		error = no_nice;
2201da177e4SLinus Torvalds 		goto out;
2211da177e4SLinus Torvalds 	}
2221da177e4SLinus Torvalds 	if (error == -ESRCH)
2231da177e4SLinus Torvalds 		error = 0;
2241da177e4SLinus Torvalds 	set_user_nice(p, niceval);
2251da177e4SLinus Torvalds out:
2261da177e4SLinus Torvalds 	return error;
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds 
SYSCALL_DEFINE3(setpriority,int,which,int,who,int,niceval)229754fe8d2SHeiko Carstens SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
2301da177e4SLinus Torvalds {
2311da177e4SLinus Torvalds 	struct task_struct *g, *p;
2321da177e4SLinus Torvalds 	struct user_struct *user;
23386a264abSDavid Howells 	const struct cred *cred = current_cred();
2341da177e4SLinus Torvalds 	int error = -EINVAL;
23541487c65SEric W. Biederman 	struct pid *pgrp;
2367b44ab97SEric W. Biederman 	kuid_t uid;
2371da177e4SLinus Torvalds 
2383e88c553SDaniel Walker 	if (which > PRIO_USER || which < PRIO_PROCESS)
2391da177e4SLinus Torvalds 		goto out;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	/* normalize: avoid signed division (rounding problems) */
2421da177e4SLinus Torvalds 	error = -ESRCH;
243c4a4d2f4SDongsheng Yang 	if (niceval < MIN_NICE)
244c4a4d2f4SDongsheng Yang 		niceval = MIN_NICE;
245c4a4d2f4SDongsheng Yang 	if (niceval > MAX_NICE)
246c4a4d2f4SDongsheng Yang 		niceval = MAX_NICE;
2471da177e4SLinus Torvalds 
248d4581a23SThomas Gleixner 	rcu_read_lock();
2491da177e4SLinus Torvalds 	switch (which) {
2501da177e4SLinus Torvalds 	case PRIO_PROCESS:
25141487c65SEric W. Biederman 		if (who)
252228ebcbeSPavel Emelyanov 			p = find_task_by_vpid(who);
25341487c65SEric W. Biederman 		else
25441487c65SEric W. Biederman 			p = current;
2551da177e4SLinus Torvalds 		if (p)
2561da177e4SLinus Torvalds 			error = set_one_prio(p, niceval, error);
2571da177e4SLinus Torvalds 		break;
2581da177e4SLinus Torvalds 	case PRIO_PGRP:
25941487c65SEric W. Biederman 		if (who)
260b488893aSPavel Emelyanov 			pgrp = find_vpid(who);
26141487c65SEric W. Biederman 		else
26241487c65SEric W. Biederman 			pgrp = task_pgrp(current);
2637f8ca0edSDavidlohr Bueso 		read_lock(&tasklist_lock);
2642d70b68dSKen Chen 		do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
2651da177e4SLinus Torvalds 			error = set_one_prio(p, niceval, error);
2662d70b68dSKen Chen 		} while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
2677f8ca0edSDavidlohr Bueso 		read_unlock(&tasklist_lock);
2681da177e4SLinus Torvalds 		break;
2691da177e4SLinus Torvalds 	case PRIO_USER:
2707b44ab97SEric W. Biederman 		uid = make_kuid(cred->user_ns, who);
27174ba508fSEric W. Biederman 		user = cred->user;
2721da177e4SLinus Torvalds 		if (!who)
273078de5f7SEric W. Biederman 			uid = cred->uid;
274ec94fc3dSvishnu.ps 		else if (!uid_eq(uid, cred->uid)) {
275ec94fc3dSvishnu.ps 			user = find_user(uid);
276ec94fc3dSvishnu.ps 			if (!user)
2771da177e4SLinus Torvalds 				goto out_unlock;	/* No processes for this user */
278ec94fc3dSvishnu.ps 		}
2797f8ca0edSDavidlohr Bueso 		for_each_process_thread(g, p) {
2808639b461SBen Segall 			if (uid_eq(task_uid(p), uid) && task_pid_vnr(p))
2811da177e4SLinus Torvalds 				error = set_one_prio(p, niceval, error);
2827f8ca0edSDavidlohr Bueso 		}
283078de5f7SEric W. Biederman 		if (!uid_eq(uid, cred->uid))
2841da177e4SLinus Torvalds 			free_uid(user);		/* For find_user() */
2851da177e4SLinus Torvalds 		break;
2861da177e4SLinus Torvalds 	}
2871da177e4SLinus Torvalds out_unlock:
288d4581a23SThomas Gleixner 	rcu_read_unlock();
2891da177e4SLinus Torvalds out:
2901da177e4SLinus Torvalds 	return error;
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds /*
2941da177e4SLinus Torvalds  * Ugh. To avoid negative return values, "getpriority()" will
2951da177e4SLinus Torvalds  * not return the normal nice-value, but a negated value that
2961da177e4SLinus Torvalds  * has been offset by 20 (ie it returns 40..1 instead of -20..19)
2971da177e4SLinus Torvalds  * to stay compatible.
2981da177e4SLinus Torvalds  */
SYSCALL_DEFINE2(getpriority,int,which,int,who)299754fe8d2SHeiko Carstens SYSCALL_DEFINE2(getpriority, int, which, int, who)
3001da177e4SLinus Torvalds {
3011da177e4SLinus Torvalds 	struct task_struct *g, *p;
3021da177e4SLinus Torvalds 	struct user_struct *user;
30386a264abSDavid Howells 	const struct cred *cred = current_cred();
3041da177e4SLinus Torvalds 	long niceval, retval = -ESRCH;
30541487c65SEric W. Biederman 	struct pid *pgrp;
3067b44ab97SEric W. Biederman 	kuid_t uid;
3071da177e4SLinus Torvalds 
3083e88c553SDaniel Walker 	if (which > PRIO_USER || which < PRIO_PROCESS)
3091da177e4SLinus Torvalds 		return -EINVAL;
3101da177e4SLinus Torvalds 
31170118837STetsuo Handa 	rcu_read_lock();
3121da177e4SLinus Torvalds 	switch (which) {
3131da177e4SLinus Torvalds 	case PRIO_PROCESS:
31441487c65SEric W. Biederman 		if (who)
315228ebcbeSPavel Emelyanov 			p = find_task_by_vpid(who);
31641487c65SEric W. Biederman 		else
31741487c65SEric W. Biederman 			p = current;
3181da177e4SLinus Torvalds 		if (p) {
3197aa2c016SDongsheng Yang 			niceval = nice_to_rlimit(task_nice(p));
3201da177e4SLinus Torvalds 			if (niceval > retval)
3211da177e4SLinus Torvalds 				retval = niceval;
3221da177e4SLinus Torvalds 		}
3231da177e4SLinus Torvalds 		break;
3241da177e4SLinus Torvalds 	case PRIO_PGRP:
32541487c65SEric W. Biederman 		if (who)
326b488893aSPavel Emelyanov 			pgrp = find_vpid(who);
32741487c65SEric W. Biederman 		else
32841487c65SEric W. Biederman 			pgrp = task_pgrp(current);
3297f8ca0edSDavidlohr Bueso 		read_lock(&tasklist_lock);
3302d70b68dSKen Chen 		do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
3317aa2c016SDongsheng Yang 			niceval = nice_to_rlimit(task_nice(p));
3321da177e4SLinus Torvalds 			if (niceval > retval)
3331da177e4SLinus Torvalds 				retval = niceval;
3342d70b68dSKen Chen 		} while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
3357f8ca0edSDavidlohr Bueso 		read_unlock(&tasklist_lock);
3361da177e4SLinus Torvalds 		break;
3371da177e4SLinus Torvalds 	case PRIO_USER:
3387b44ab97SEric W. Biederman 		uid = make_kuid(cred->user_ns, who);
33974ba508fSEric W. Biederman 		user = cred->user;
3401da177e4SLinus Torvalds 		if (!who)
341078de5f7SEric W. Biederman 			uid = cred->uid;
342ec94fc3dSvishnu.ps 		else if (!uid_eq(uid, cred->uid)) {
343ec94fc3dSvishnu.ps 			user = find_user(uid);
344ec94fc3dSvishnu.ps 			if (!user)
3451da177e4SLinus Torvalds 				goto out_unlock;	/* No processes for this user */
346ec94fc3dSvishnu.ps 		}
3477f8ca0edSDavidlohr Bueso 		for_each_process_thread(g, p) {
3488639b461SBen Segall 			if (uid_eq(task_uid(p), uid) && task_pid_vnr(p)) {
3497aa2c016SDongsheng Yang 				niceval = nice_to_rlimit(task_nice(p));
3501da177e4SLinus Torvalds 				if (niceval > retval)
3511da177e4SLinus Torvalds 					retval = niceval;
3521da177e4SLinus Torvalds 			}
3537f8ca0edSDavidlohr Bueso 		}
354078de5f7SEric W. Biederman 		if (!uid_eq(uid, cred->uid))
3551da177e4SLinus Torvalds 			free_uid(user);		/* for find_user() */
3561da177e4SLinus Torvalds 		break;
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds out_unlock:
35970118837STetsuo Handa 	rcu_read_unlock();
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	return retval;
3621da177e4SLinus Torvalds }
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds /*
3651da177e4SLinus Torvalds  * Unprivileged users may change the real gid to the effective gid
3661da177e4SLinus Torvalds  * or vice versa.  (BSD-style)
3671da177e4SLinus Torvalds  *
3681da177e4SLinus Torvalds  * If you set the real gid at all, or set the effective gid to a value not
3691da177e4SLinus Torvalds  * equal to the real gid, then the saved gid is set to the new effective gid.
3701da177e4SLinus Torvalds  *
3711da177e4SLinus Torvalds  * This makes it possible for a setgid program to completely drop its
3721da177e4SLinus Torvalds  * privileges, which is often a useful assertion to make when you are doing
3731da177e4SLinus Torvalds  * a security audit over a program.
3741da177e4SLinus Torvalds  *
3751da177e4SLinus Torvalds  * The general idea is that a program which uses just setregid() will be
3761da177e4SLinus Torvalds  * 100% compatible with BSD.  A program which uses just setgid() will be
3771da177e4SLinus Torvalds  * 100% compatible with POSIX with saved IDs.
3781da177e4SLinus Torvalds  *
3791da177e4SLinus Torvalds  * SMP: There are not races, the GIDs are checked only by filesystem
3801da177e4SLinus Torvalds  *      operations (as far as semantic preservation is concerned).
3811da177e4SLinus Torvalds  */
3822813893fSIulia Manda #ifdef CONFIG_MULTIUSER
__sys_setregid(gid_t rgid,gid_t egid)383e530dca5SDominik Brodowski long __sys_setregid(gid_t rgid, gid_t egid)
3841da177e4SLinus Torvalds {
385a29c33f4SEric W. Biederman 	struct user_namespace *ns = current_user_ns();
386d84f4f99SDavid Howells 	const struct cred *old;
387d84f4f99SDavid Howells 	struct cred *new;
3881da177e4SLinus Torvalds 	int retval;
389a29c33f4SEric W. Biederman 	kgid_t krgid, kegid;
390a29c33f4SEric W. Biederman 
391a29c33f4SEric W. Biederman 	krgid = make_kgid(ns, rgid);
392a29c33f4SEric W. Biederman 	kegid = make_kgid(ns, egid);
393a29c33f4SEric W. Biederman 
394a29c33f4SEric W. Biederman 	if ((rgid != (gid_t) -1) && !gid_valid(krgid))
395a29c33f4SEric W. Biederman 		return -EINVAL;
396a29c33f4SEric W. Biederman 	if ((egid != (gid_t) -1) && !gid_valid(kegid))
397a29c33f4SEric W. Biederman 		return -EINVAL;
3981da177e4SLinus Torvalds 
399d84f4f99SDavid Howells 	new = prepare_creds();
400d84f4f99SDavid Howells 	if (!new)
401d84f4f99SDavid Howells 		return -ENOMEM;
402d84f4f99SDavid Howells 	old = current_cred();
403d84f4f99SDavid Howells 
404d84f4f99SDavid Howells 	retval = -EPERM;
4051da177e4SLinus Torvalds 	if (rgid != (gid_t) -1) {
406a29c33f4SEric W. Biederman 		if (gid_eq(old->gid, krgid) ||
407a29c33f4SEric W. Biederman 		    gid_eq(old->egid, krgid) ||
408111767c1SThomas Cedeno 		    ns_capable_setid(old->user_ns, CAP_SETGID))
409a29c33f4SEric W. Biederman 			new->gid = krgid;
4101da177e4SLinus Torvalds 		else
411d84f4f99SDavid Howells 			goto error;
4121da177e4SLinus Torvalds 	}
4131da177e4SLinus Torvalds 	if (egid != (gid_t) -1) {
414a29c33f4SEric W. Biederman 		if (gid_eq(old->gid, kegid) ||
415a29c33f4SEric W. Biederman 		    gid_eq(old->egid, kegid) ||
416a29c33f4SEric W. Biederman 		    gid_eq(old->sgid, kegid) ||
417111767c1SThomas Cedeno 		    ns_capable_setid(old->user_ns, CAP_SETGID))
418a29c33f4SEric W. Biederman 			new->egid = kegid;
419756184b7SCal Peake 		else
420d84f4f99SDavid Howells 			goto error;
4211da177e4SLinus Torvalds 	}
422d84f4f99SDavid Howells 
4231da177e4SLinus Torvalds 	if (rgid != (gid_t) -1 ||
424a29c33f4SEric W. Biederman 	    (egid != (gid_t) -1 && !gid_eq(kegid, old->gid)))
425d84f4f99SDavid Howells 		new->sgid = new->egid;
426d84f4f99SDavid Howells 	new->fsgid = new->egid;
427d84f4f99SDavid Howells 
42839030e13SThomas Cedeno 	retval = security_task_fix_setgid(new, old, LSM_SETID_RE);
42939030e13SThomas Cedeno 	if (retval < 0)
43039030e13SThomas Cedeno 		goto error;
43139030e13SThomas Cedeno 
432d84f4f99SDavid Howells 	return commit_creds(new);
433d84f4f99SDavid Howells 
434d84f4f99SDavid Howells error:
435d84f4f99SDavid Howells 	abort_creds(new);
436d84f4f99SDavid Howells 	return retval;
4371da177e4SLinus Torvalds }
4381da177e4SLinus Torvalds 
SYSCALL_DEFINE2(setregid,gid_t,rgid,gid_t,egid)439e530dca5SDominik Brodowski SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
440e530dca5SDominik Brodowski {
441e530dca5SDominik Brodowski 	return __sys_setregid(rgid, egid);
442e530dca5SDominik Brodowski }
443e530dca5SDominik Brodowski 
4441da177e4SLinus Torvalds /*
4451da177e4SLinus Torvalds  * setgid() is implemented like SysV w/ SAVED_IDS
4461da177e4SLinus Torvalds  *
4471da177e4SLinus Torvalds  * SMP: Same implicit races as above.
4481da177e4SLinus Torvalds  */
__sys_setgid(gid_t gid)449e530dca5SDominik Brodowski long __sys_setgid(gid_t gid)
4501da177e4SLinus Torvalds {
451a29c33f4SEric W. Biederman 	struct user_namespace *ns = current_user_ns();
452d84f4f99SDavid Howells 	const struct cred *old;
453d84f4f99SDavid Howells 	struct cred *new;
4541da177e4SLinus Torvalds 	int retval;
455a29c33f4SEric W. Biederman 	kgid_t kgid;
456a29c33f4SEric W. Biederman 
457a29c33f4SEric W. Biederman 	kgid = make_kgid(ns, gid);
458a29c33f4SEric W. Biederman 	if (!gid_valid(kgid))
459a29c33f4SEric W. Biederman 		return -EINVAL;
4601da177e4SLinus Torvalds 
461d84f4f99SDavid Howells 	new = prepare_creds();
462d84f4f99SDavid Howells 	if (!new)
463d84f4f99SDavid Howells 		return -ENOMEM;
464d84f4f99SDavid Howells 	old = current_cred();
465d84f4f99SDavid Howells 
466d84f4f99SDavid Howells 	retval = -EPERM;
467111767c1SThomas Cedeno 	if (ns_capable_setid(old->user_ns, CAP_SETGID))
468a29c33f4SEric W. Biederman 		new->gid = new->egid = new->sgid = new->fsgid = kgid;
469a29c33f4SEric W. Biederman 	else if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->sgid))
470a29c33f4SEric W. Biederman 		new->egid = new->fsgid = kgid;
4711da177e4SLinus Torvalds 	else
472d84f4f99SDavid Howells 		goto error;
4731da177e4SLinus Torvalds 
47439030e13SThomas Cedeno 	retval = security_task_fix_setgid(new, old, LSM_SETID_ID);
47539030e13SThomas Cedeno 	if (retval < 0)
47639030e13SThomas Cedeno 		goto error;
47739030e13SThomas Cedeno 
478d84f4f99SDavid Howells 	return commit_creds(new);
479d84f4f99SDavid Howells 
480d84f4f99SDavid Howells error:
481d84f4f99SDavid Howells 	abort_creds(new);
482d84f4f99SDavid Howells 	return retval;
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
SYSCALL_DEFINE1(setgid,gid_t,gid)485e530dca5SDominik Brodowski SYSCALL_DEFINE1(setgid, gid_t, gid)
486e530dca5SDominik Brodowski {
487e530dca5SDominik Brodowski 	return __sys_setgid(gid);
488e530dca5SDominik Brodowski }
489e530dca5SDominik Brodowski 
490d84f4f99SDavid Howells /*
491d84f4f99SDavid Howells  * change the user struct in a credentials set to match the new UID
492d84f4f99SDavid Howells  */
set_user(struct cred * new)493d84f4f99SDavid Howells static int set_user(struct cred *new)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	struct user_struct *new_user;
4961da177e4SLinus Torvalds 
497078de5f7SEric W. Biederman 	new_user = alloc_uid(new->uid);
4981da177e4SLinus Torvalds 	if (!new_user)
4991da177e4SLinus Torvalds 		return -EAGAIN;
5001da177e4SLinus Torvalds 
501c923a8e7SEric W. Biederman 	free_uid(new->user);
502c923a8e7SEric W. Biederman 	new->user = new_user;
503c923a8e7SEric W. Biederman 	return 0;
504c923a8e7SEric W. Biederman }
505c923a8e7SEric W. Biederman 
flag_nproc_exceeded(struct cred * new)506c923a8e7SEric W. Biederman static void flag_nproc_exceeded(struct cred *new)
507c923a8e7SEric W. Biederman {
508c923a8e7SEric W. Biederman 	if (new->ucounts == current_ucounts())
509c923a8e7SEric W. Biederman 		return;
510c923a8e7SEric W. Biederman 
51172fa5997SVasiliy Kulikov 	/*
51272fa5997SVasiliy Kulikov 	 * We don't fail in case of NPROC limit excess here because too many
51372fa5997SVasiliy Kulikov 	 * poorly written programs don't check set*uid() return code, assuming
51472fa5997SVasiliy Kulikov 	 * it never fails if called by root.  We may still enforce NPROC limit
51572fa5997SVasiliy Kulikov 	 * for programs doing set*uid()+execve() by harmlessly deferring the
51672fa5997SVasiliy Kulikov 	 * failure to the execve() stage.
51772fa5997SVasiliy Kulikov 	 */
518de399236SAlexey Gladkov 	if (is_rlimit_overlimit(new->ucounts, UCOUNT_RLIMIT_NPROC, rlimit(RLIMIT_NPROC)) &&
519c923a8e7SEric W. Biederman 			new->user != INIT_USER)
52072fa5997SVasiliy Kulikov 		current->flags |= PF_NPROC_EXCEEDED;
52172fa5997SVasiliy Kulikov 	else
52272fa5997SVasiliy Kulikov 		current->flags &= ~PF_NPROC_EXCEEDED;
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds /*
5261da177e4SLinus Torvalds  * Unprivileged users may change the real uid to the effective uid
5271da177e4SLinus Torvalds  * or vice versa.  (BSD-style)
5281da177e4SLinus Torvalds  *
5291da177e4SLinus Torvalds  * If you set the real uid at all, or set the effective uid to a value not
5301da177e4SLinus Torvalds  * equal to the real uid, then the saved uid is set to the new effective uid.
5311da177e4SLinus Torvalds  *
5321da177e4SLinus Torvalds  * This makes it possible for a setuid program to completely drop its
5331da177e4SLinus Torvalds  * privileges, which is often a useful assertion to make when you are doing
5341da177e4SLinus Torvalds  * a security audit over a program.
5351da177e4SLinus Torvalds  *
5361da177e4SLinus Torvalds  * The general idea is that a program which uses just setreuid() will be
5371da177e4SLinus Torvalds  * 100% compatible with BSD.  A program which uses just setuid() will be
5381da177e4SLinus Torvalds  * 100% compatible with POSIX with saved IDs.
5391da177e4SLinus Torvalds  */
__sys_setreuid(uid_t ruid,uid_t euid)540e530dca5SDominik Brodowski long __sys_setreuid(uid_t ruid, uid_t euid)
5411da177e4SLinus Torvalds {
542a29c33f4SEric W. Biederman 	struct user_namespace *ns = current_user_ns();
543d84f4f99SDavid Howells 	const struct cred *old;
544d84f4f99SDavid Howells 	struct cred *new;
5451da177e4SLinus Torvalds 	int retval;
546a29c33f4SEric W. Biederman 	kuid_t kruid, keuid;
547a29c33f4SEric W. Biederman 
548a29c33f4SEric W. Biederman 	kruid = make_kuid(ns, ruid);
549a29c33f4SEric W. Biederman 	keuid = make_kuid(ns, euid);
550a29c33f4SEric W. Biederman 
551a29c33f4SEric W. Biederman 	if ((ruid != (uid_t) -1) && !uid_valid(kruid))
552a29c33f4SEric W. Biederman 		return -EINVAL;
553a29c33f4SEric W. Biederman 	if ((euid != (uid_t) -1) && !uid_valid(keuid))
554a29c33f4SEric W. Biederman 		return -EINVAL;
5551da177e4SLinus Torvalds 
556d84f4f99SDavid Howells 	new = prepare_creds();
557d84f4f99SDavid Howells 	if (!new)
558d84f4f99SDavid Howells 		return -ENOMEM;
559d84f4f99SDavid Howells 	old = current_cred();
560d84f4f99SDavid Howells 
561d84f4f99SDavid Howells 	retval = -EPERM;
5621da177e4SLinus Torvalds 	if (ruid != (uid_t) -1) {
563a29c33f4SEric W. Biederman 		new->uid = kruid;
564a29c33f4SEric W. Biederman 		if (!uid_eq(old->uid, kruid) &&
565a29c33f4SEric W. Biederman 		    !uid_eq(old->euid, kruid) &&
56640852275SMicah Morton 		    !ns_capable_setid(old->user_ns, CAP_SETUID))
567d84f4f99SDavid Howells 			goto error;
5681da177e4SLinus Torvalds 	}
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	if (euid != (uid_t) -1) {
571a29c33f4SEric W. Biederman 		new->euid = keuid;
572a29c33f4SEric W. Biederman 		if (!uid_eq(old->uid, keuid) &&
573a29c33f4SEric W. Biederman 		    !uid_eq(old->euid, keuid) &&
574a29c33f4SEric W. Biederman 		    !uid_eq(old->suid, keuid) &&
57540852275SMicah Morton 		    !ns_capable_setid(old->user_ns, CAP_SETUID))
576d84f4f99SDavid Howells 			goto error;
5771da177e4SLinus Torvalds 	}
5781da177e4SLinus Torvalds 
579a29c33f4SEric W. Biederman 	if (!uid_eq(new->uid, old->uid)) {
58054e99124SDhaval Giani 		retval = set_user(new);
58154e99124SDhaval Giani 		if (retval < 0)
582d84f4f99SDavid Howells 			goto error;
58354e99124SDhaval Giani 	}
5841da177e4SLinus Torvalds 	if (ruid != (uid_t) -1 ||
585a29c33f4SEric W. Biederman 	    (euid != (uid_t) -1 && !uid_eq(keuid, old->uid)))
586d84f4f99SDavid Howells 		new->suid = new->euid;
587d84f4f99SDavid Howells 	new->fsuid = new->euid;
5881da177e4SLinus Torvalds 
589d84f4f99SDavid Howells 	retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
590d84f4f99SDavid Howells 	if (retval < 0)
591d84f4f99SDavid Howells 		goto error;
5921da177e4SLinus Torvalds 
593905ae01cSAlexey Gladkov 	retval = set_cred_ucounts(new);
594905ae01cSAlexey Gladkov 	if (retval < 0)
595905ae01cSAlexey Gladkov 		goto error;
596905ae01cSAlexey Gladkov 
597c923a8e7SEric W. Biederman 	flag_nproc_exceeded(new);
598d84f4f99SDavid Howells 	return commit_creds(new);
599d84f4f99SDavid Howells 
600d84f4f99SDavid Howells error:
601d84f4f99SDavid Howells 	abort_creds(new);
602d84f4f99SDavid Howells 	return retval;
6031da177e4SLinus Torvalds }
6041da177e4SLinus Torvalds 
SYSCALL_DEFINE2(setreuid,uid_t,ruid,uid_t,euid)605e530dca5SDominik Brodowski SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
606e530dca5SDominik Brodowski {
607e530dca5SDominik Brodowski 	return __sys_setreuid(ruid, euid);
608e530dca5SDominik Brodowski }
609e530dca5SDominik Brodowski 
6101da177e4SLinus Torvalds /*
6111da177e4SLinus Torvalds  * setuid() is implemented like SysV with SAVED_IDS
6121da177e4SLinus Torvalds  *
6131da177e4SLinus Torvalds  * Note that SAVED_ID's is deficient in that a setuid root program
6141da177e4SLinus Torvalds  * like sendmail, for example, cannot set its uid to be a normal
6151da177e4SLinus Torvalds  * user and then switch back, because if you're root, setuid() sets
6161da177e4SLinus Torvalds  * the saved uid too.  If you don't like this, blame the bright people
6171da177e4SLinus Torvalds  * in the POSIX committee and/or USG.  Note that the BSD-style setreuid()
6181da177e4SLinus Torvalds  * will allow a root program to temporarily drop privileges and be able to
6191da177e4SLinus Torvalds  * regain them by swapping the real and effective uid.
6201da177e4SLinus Torvalds  */
__sys_setuid(uid_t uid)621e530dca5SDominik Brodowski long __sys_setuid(uid_t uid)
6221da177e4SLinus Torvalds {
623a29c33f4SEric W. Biederman 	struct user_namespace *ns = current_user_ns();
624d84f4f99SDavid Howells 	const struct cred *old;
625d84f4f99SDavid Howells 	struct cred *new;
6261da177e4SLinus Torvalds 	int retval;
627a29c33f4SEric W. Biederman 	kuid_t kuid;
628a29c33f4SEric W. Biederman 
629a29c33f4SEric W. Biederman 	kuid = make_kuid(ns, uid);
630a29c33f4SEric W. Biederman 	if (!uid_valid(kuid))
631a29c33f4SEric W. Biederman 		return -EINVAL;
6321da177e4SLinus Torvalds 
633d84f4f99SDavid Howells 	new = prepare_creds();
634d84f4f99SDavid Howells 	if (!new)
635d84f4f99SDavid Howells 		return -ENOMEM;
636d84f4f99SDavid Howells 	old = current_cred();
637d84f4f99SDavid Howells 
638d84f4f99SDavid Howells 	retval = -EPERM;
63940852275SMicah Morton 	if (ns_capable_setid(old->user_ns, CAP_SETUID)) {
640a29c33f4SEric W. Biederman 		new->suid = new->uid = kuid;
641a29c33f4SEric W. Biederman 		if (!uid_eq(kuid, old->uid)) {
64254e99124SDhaval Giani 			retval = set_user(new);
64354e99124SDhaval Giani 			if (retval < 0)
644d84f4f99SDavid Howells 				goto error;
6451da177e4SLinus Torvalds 		}
646a29c33f4SEric W. Biederman 	} else if (!uid_eq(kuid, old->uid) && !uid_eq(kuid, new->suid)) {
647d84f4f99SDavid Howells 		goto error;
648d84f4f99SDavid Howells 	}
6491da177e4SLinus Torvalds 
650a29c33f4SEric W. Biederman 	new->fsuid = new->euid = kuid;
6511da177e4SLinus Torvalds 
652d84f4f99SDavid Howells 	retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
653d84f4f99SDavid Howells 	if (retval < 0)
654d84f4f99SDavid Howells 		goto error;
655d84f4f99SDavid Howells 
656905ae01cSAlexey Gladkov 	retval = set_cred_ucounts(new);
657905ae01cSAlexey Gladkov 	if (retval < 0)
658905ae01cSAlexey Gladkov 		goto error;
659905ae01cSAlexey Gladkov 
660c923a8e7SEric W. Biederman 	flag_nproc_exceeded(new);
661d84f4f99SDavid Howells 	return commit_creds(new);
662d84f4f99SDavid Howells 
663d84f4f99SDavid Howells error:
664d84f4f99SDavid Howells 	abort_creds(new);
665d84f4f99SDavid Howells 	return retval;
6661da177e4SLinus Torvalds }
6671da177e4SLinus Torvalds 
SYSCALL_DEFINE1(setuid,uid_t,uid)668e530dca5SDominik Brodowski SYSCALL_DEFINE1(setuid, uid_t, uid)
669e530dca5SDominik Brodowski {
670e530dca5SDominik Brodowski 	return __sys_setuid(uid);
671e530dca5SDominik Brodowski }
672e530dca5SDominik Brodowski 
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds /*
6751da177e4SLinus Torvalds  * This function implements a generic ability to update ruid, euid,
6761da177e4SLinus Torvalds  * and suid.  This allows you to implement the 4.4 compatible seteuid().
6771da177e4SLinus Torvalds  */
__sys_setresuid(uid_t ruid,uid_t euid,uid_t suid)678e530dca5SDominik Brodowski long __sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
6791da177e4SLinus Torvalds {
680a29c33f4SEric W. Biederman 	struct user_namespace *ns = current_user_ns();
681d84f4f99SDavid Howells 	const struct cred *old;
682d84f4f99SDavid Howells 	struct cred *new;
6831da177e4SLinus Torvalds 	int retval;
684a29c33f4SEric W. Biederman 	kuid_t kruid, keuid, ksuid;
685659c0ce1SOndrej Mosnacek 	bool ruid_new, euid_new, suid_new;
686a29c33f4SEric W. Biederman 
687a29c33f4SEric W. Biederman 	kruid = make_kuid(ns, ruid);
688a29c33f4SEric W. Biederman 	keuid = make_kuid(ns, euid);
689a29c33f4SEric W. Biederman 	ksuid = make_kuid(ns, suid);
690a29c33f4SEric W. Biederman 
691a29c33f4SEric W. Biederman 	if ((ruid != (uid_t) -1) && !uid_valid(kruid))
692a29c33f4SEric W. Biederman 		return -EINVAL;
693a29c33f4SEric W. Biederman 
694a29c33f4SEric W. Biederman 	if ((euid != (uid_t) -1) && !uid_valid(keuid))
695a29c33f4SEric W. Biederman 		return -EINVAL;
696a29c33f4SEric W. Biederman 
697a29c33f4SEric W. Biederman 	if ((suid != (uid_t) -1) && !uid_valid(ksuid))
698a29c33f4SEric W. Biederman 		return -EINVAL;
6991da177e4SLinus Torvalds 
700659c0ce1SOndrej Mosnacek 	old = current_cred();
701659c0ce1SOndrej Mosnacek 
702659c0ce1SOndrej Mosnacek 	/* check for no-op */
703659c0ce1SOndrej Mosnacek 	if ((ruid == (uid_t) -1 || uid_eq(kruid, old->uid)) &&
704659c0ce1SOndrej Mosnacek 	    (euid == (uid_t) -1 || (uid_eq(keuid, old->euid) &&
705659c0ce1SOndrej Mosnacek 				    uid_eq(keuid, old->fsuid))) &&
706659c0ce1SOndrej Mosnacek 	    (suid == (uid_t) -1 || uid_eq(ksuid, old->suid)))
707659c0ce1SOndrej Mosnacek 		return 0;
708659c0ce1SOndrej Mosnacek 
709659c0ce1SOndrej Mosnacek 	ruid_new = ruid != (uid_t) -1        && !uid_eq(kruid, old->uid) &&
710659c0ce1SOndrej Mosnacek 		   !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid);
711659c0ce1SOndrej Mosnacek 	euid_new = euid != (uid_t) -1        && !uid_eq(keuid, old->uid) &&
712659c0ce1SOndrej Mosnacek 		   !uid_eq(keuid, old->euid) && !uid_eq(keuid, old->suid);
713659c0ce1SOndrej Mosnacek 	suid_new = suid != (uid_t) -1        && !uid_eq(ksuid, old->uid) &&
714659c0ce1SOndrej Mosnacek 		   !uid_eq(ksuid, old->euid) && !uid_eq(ksuid, old->suid);
715659c0ce1SOndrej Mosnacek 	if ((ruid_new || euid_new || suid_new) &&
716659c0ce1SOndrej Mosnacek 	    !ns_capable_setid(old->user_ns, CAP_SETUID))
717659c0ce1SOndrej Mosnacek 		return -EPERM;
718659c0ce1SOndrej Mosnacek 
719d84f4f99SDavid Howells 	new = prepare_creds();
720d84f4f99SDavid Howells 	if (!new)
721d84f4f99SDavid Howells 		return -ENOMEM;
722d84f4f99SDavid Howells 
7231da177e4SLinus Torvalds 	if (ruid != (uid_t) -1) {
724a29c33f4SEric W. Biederman 		new->uid = kruid;
725a29c33f4SEric W. Biederman 		if (!uid_eq(kruid, old->uid)) {
72654e99124SDhaval Giani 			retval = set_user(new);
72754e99124SDhaval Giani 			if (retval < 0)
728d84f4f99SDavid Howells 				goto error;
7291da177e4SLinus Torvalds 		}
73054e99124SDhaval Giani 	}
731d84f4f99SDavid Howells 	if (euid != (uid_t) -1)
732a29c33f4SEric W. Biederman 		new->euid = keuid;
7331da177e4SLinus Torvalds 	if (suid != (uid_t) -1)
734a29c33f4SEric W. Biederman 		new->suid = ksuid;
735d84f4f99SDavid Howells 	new->fsuid = new->euid;
7361da177e4SLinus Torvalds 
737d84f4f99SDavid Howells 	retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
738d84f4f99SDavid Howells 	if (retval < 0)
739d84f4f99SDavid Howells 		goto error;
7401da177e4SLinus Torvalds 
741905ae01cSAlexey Gladkov 	retval = set_cred_ucounts(new);
742905ae01cSAlexey Gladkov 	if (retval < 0)
743905ae01cSAlexey Gladkov 		goto error;
744905ae01cSAlexey Gladkov 
745c923a8e7SEric W. Biederman 	flag_nproc_exceeded(new);
746d84f4f99SDavid Howells 	return commit_creds(new);
747d84f4f99SDavid Howells 
748d84f4f99SDavid Howells error:
749d84f4f99SDavid Howells 	abort_creds(new);
750d84f4f99SDavid Howells 	return retval;
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds 
SYSCALL_DEFINE3(setresuid,uid_t,ruid,uid_t,euid,uid_t,suid)753e530dca5SDominik Brodowski SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
754e530dca5SDominik Brodowski {
755e530dca5SDominik Brodowski 	return __sys_setresuid(ruid, euid, suid);
756e530dca5SDominik Brodowski }
757e530dca5SDominik Brodowski 
SYSCALL_DEFINE3(getresuid,uid_t __user *,ruidp,uid_t __user *,euidp,uid_t __user *,suidp)758a29c33f4SEric W. Biederman SYSCALL_DEFINE3(getresuid, uid_t __user *, ruidp, uid_t __user *, euidp, uid_t __user *, suidp)
7591da177e4SLinus Torvalds {
76086a264abSDavid Howells 	const struct cred *cred = current_cred();
7611da177e4SLinus Torvalds 	int retval;
762a29c33f4SEric W. Biederman 	uid_t ruid, euid, suid;
7631da177e4SLinus Torvalds 
764a29c33f4SEric W. Biederman 	ruid = from_kuid_munged(cred->user_ns, cred->uid);
765a29c33f4SEric W. Biederman 	euid = from_kuid_munged(cred->user_ns, cred->euid);
766a29c33f4SEric W. Biederman 	suid = from_kuid_munged(cred->user_ns, cred->suid);
767a29c33f4SEric W. Biederman 
768ec94fc3dSvishnu.ps 	retval = put_user(ruid, ruidp);
769ec94fc3dSvishnu.ps 	if (!retval) {
770ec94fc3dSvishnu.ps 		retval = put_user(euid, euidp);
771ec94fc3dSvishnu.ps 		if (!retval)
772ec94fc3dSvishnu.ps 			return put_user(suid, suidp);
773ec94fc3dSvishnu.ps 	}
7741da177e4SLinus Torvalds 	return retval;
7751da177e4SLinus Torvalds }
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds /*
7781da177e4SLinus Torvalds  * Same as above, but for rgid, egid, sgid.
7791da177e4SLinus Torvalds  */
__sys_setresgid(gid_t rgid,gid_t egid,gid_t sgid)780e530dca5SDominik Brodowski long __sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
7811da177e4SLinus Torvalds {
782a29c33f4SEric W. Biederman 	struct user_namespace *ns = current_user_ns();
783d84f4f99SDavid Howells 	const struct cred *old;
784d84f4f99SDavid Howells 	struct cred *new;
7851da177e4SLinus Torvalds 	int retval;
786a29c33f4SEric W. Biederman 	kgid_t krgid, kegid, ksgid;
787659c0ce1SOndrej Mosnacek 	bool rgid_new, egid_new, sgid_new;
788a29c33f4SEric W. Biederman 
789a29c33f4SEric W. Biederman 	krgid = make_kgid(ns, rgid);
790a29c33f4SEric W. Biederman 	kegid = make_kgid(ns, egid);
791a29c33f4SEric W. Biederman 	ksgid = make_kgid(ns, sgid);
792a29c33f4SEric W. Biederman 
793a29c33f4SEric W. Biederman 	if ((rgid != (gid_t) -1) && !gid_valid(krgid))
794a29c33f4SEric W. Biederman 		return -EINVAL;
795a29c33f4SEric W. Biederman 	if ((egid != (gid_t) -1) && !gid_valid(kegid))
796a29c33f4SEric W. Biederman 		return -EINVAL;
797a29c33f4SEric W. Biederman 	if ((sgid != (gid_t) -1) && !gid_valid(ksgid))
798a29c33f4SEric W. Biederman 		return -EINVAL;
7991da177e4SLinus Torvalds 
800659c0ce1SOndrej Mosnacek 	old = current_cred();
801659c0ce1SOndrej Mosnacek 
802659c0ce1SOndrej Mosnacek 	/* check for no-op */
803659c0ce1SOndrej Mosnacek 	if ((rgid == (gid_t) -1 || gid_eq(krgid, old->gid)) &&
804659c0ce1SOndrej Mosnacek 	    (egid == (gid_t) -1 || (gid_eq(kegid, old->egid) &&
805659c0ce1SOndrej Mosnacek 				    gid_eq(kegid, old->fsgid))) &&
806659c0ce1SOndrej Mosnacek 	    (sgid == (gid_t) -1 || gid_eq(ksgid, old->sgid)))
807659c0ce1SOndrej Mosnacek 		return 0;
808659c0ce1SOndrej Mosnacek 
809659c0ce1SOndrej Mosnacek 	rgid_new = rgid != (gid_t) -1        && !gid_eq(krgid, old->gid) &&
810659c0ce1SOndrej Mosnacek 		   !gid_eq(krgid, old->egid) && !gid_eq(krgid, old->sgid);
811659c0ce1SOndrej Mosnacek 	egid_new = egid != (gid_t) -1        && !gid_eq(kegid, old->gid) &&
812659c0ce1SOndrej Mosnacek 		   !gid_eq(kegid, old->egid) && !gid_eq(kegid, old->sgid);
813659c0ce1SOndrej Mosnacek 	sgid_new = sgid != (gid_t) -1        && !gid_eq(ksgid, old->gid) &&
814659c0ce1SOndrej Mosnacek 		   !gid_eq(ksgid, old->egid) && !gid_eq(ksgid, old->sgid);
815659c0ce1SOndrej Mosnacek 	if ((rgid_new || egid_new || sgid_new) &&
816659c0ce1SOndrej Mosnacek 	    !ns_capable_setid(old->user_ns, CAP_SETGID))
817659c0ce1SOndrej Mosnacek 		return -EPERM;
818659c0ce1SOndrej Mosnacek 
819d84f4f99SDavid Howells 	new = prepare_creds();
820d84f4f99SDavid Howells 	if (!new)
821d84f4f99SDavid Howells 		return -ENOMEM;
8221da177e4SLinus Torvalds 
823d84f4f99SDavid Howells 	if (rgid != (gid_t) -1)
824a29c33f4SEric W. Biederman 		new->gid = krgid;
825d84f4f99SDavid Howells 	if (egid != (gid_t) -1)
826a29c33f4SEric W. Biederman 		new->egid = kegid;
827d84f4f99SDavid Howells 	if (sgid != (gid_t) -1)
828a29c33f4SEric W. Biederman 		new->sgid = ksgid;
829d84f4f99SDavid Howells 	new->fsgid = new->egid;
830d84f4f99SDavid Howells 
83139030e13SThomas Cedeno 	retval = security_task_fix_setgid(new, old, LSM_SETID_RES);
83239030e13SThomas Cedeno 	if (retval < 0)
83339030e13SThomas Cedeno 		goto error;
83439030e13SThomas Cedeno 
835d84f4f99SDavid Howells 	return commit_creds(new);
836d84f4f99SDavid Howells 
837d84f4f99SDavid Howells error:
838d84f4f99SDavid Howells 	abort_creds(new);
839d84f4f99SDavid Howells 	return retval;
8401da177e4SLinus Torvalds }
8411da177e4SLinus Torvalds 
SYSCALL_DEFINE3(setresgid,gid_t,rgid,gid_t,egid,gid_t,sgid)842e530dca5SDominik Brodowski SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
843e530dca5SDominik Brodowski {
844e530dca5SDominik Brodowski 	return __sys_setresgid(rgid, egid, sgid);
845e530dca5SDominik Brodowski }
846e530dca5SDominik Brodowski 
SYSCALL_DEFINE3(getresgid,gid_t __user *,rgidp,gid_t __user *,egidp,gid_t __user *,sgidp)847a29c33f4SEric W. Biederman SYSCALL_DEFINE3(getresgid, gid_t __user *, rgidp, gid_t __user *, egidp, gid_t __user *, sgidp)
8481da177e4SLinus Torvalds {
84986a264abSDavid Howells 	const struct cred *cred = current_cred();
8501da177e4SLinus Torvalds 	int retval;
851a29c33f4SEric W. Biederman 	gid_t rgid, egid, sgid;
8521da177e4SLinus Torvalds 
853a29c33f4SEric W. Biederman 	rgid = from_kgid_munged(cred->user_ns, cred->gid);
854a29c33f4SEric W. Biederman 	egid = from_kgid_munged(cred->user_ns, cred->egid);
855a29c33f4SEric W. Biederman 	sgid = from_kgid_munged(cred->user_ns, cred->sgid);
856a29c33f4SEric W. Biederman 
857ec94fc3dSvishnu.ps 	retval = put_user(rgid, rgidp);
858ec94fc3dSvishnu.ps 	if (!retval) {
859ec94fc3dSvishnu.ps 		retval = put_user(egid, egidp);
860ec94fc3dSvishnu.ps 		if (!retval)
861a29c33f4SEric W. Biederman 			retval = put_user(sgid, sgidp);
862ec94fc3dSvishnu.ps 	}
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	return retval;
8651da177e4SLinus Torvalds }
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds /*
8691da177e4SLinus Torvalds  * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
8701da177e4SLinus Torvalds  * is used for "access()" and for the NFS daemon (letting nfsd stay at
8711da177e4SLinus Torvalds  * whatever uid it wants to). It normally shadows "euid", except when
8721da177e4SLinus Torvalds  * explicitly set by setfsuid() or for access..
8731da177e4SLinus Torvalds  */
__sys_setfsuid(uid_t uid)874e530dca5SDominik Brodowski long __sys_setfsuid(uid_t uid)
8751da177e4SLinus Torvalds {
876d84f4f99SDavid Howells 	const struct cred *old;
877d84f4f99SDavid Howells 	struct cred *new;
878d84f4f99SDavid Howells 	uid_t old_fsuid;
879a29c33f4SEric W. Biederman 	kuid_t kuid;
880a29c33f4SEric W. Biederman 
881a29c33f4SEric W. Biederman 	old = current_cred();
882a29c33f4SEric W. Biederman 	old_fsuid = from_kuid_munged(old->user_ns, old->fsuid);
883a29c33f4SEric W. Biederman 
884a29c33f4SEric W. Biederman 	kuid = make_kuid(old->user_ns, uid);
885a29c33f4SEric W. Biederman 	if (!uid_valid(kuid))
886a29c33f4SEric W. Biederman 		return old_fsuid;
8871da177e4SLinus Torvalds 
888d84f4f99SDavid Howells 	new = prepare_creds();
889d84f4f99SDavid Howells 	if (!new)
890a29c33f4SEric W. Biederman 		return old_fsuid;
8911da177e4SLinus Torvalds 
892a29c33f4SEric W. Biederman 	if (uid_eq(kuid, old->uid)  || uid_eq(kuid, old->euid)  ||
893a29c33f4SEric W. Biederman 	    uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) ||
89440852275SMicah Morton 	    ns_capable_setid(old->user_ns, CAP_SETUID)) {
895a29c33f4SEric W. Biederman 		if (!uid_eq(kuid, old->fsuid)) {
896a29c33f4SEric W. Biederman 			new->fsuid = kuid;
897d84f4f99SDavid Howells 			if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
898d84f4f99SDavid Howells 				goto change_okay;
8991da177e4SLinus Torvalds 		}
9001da177e4SLinus Torvalds 	}
9011da177e4SLinus Torvalds 
902d84f4f99SDavid Howells 	abort_creds(new);
903d84f4f99SDavid Howells 	return old_fsuid;
9041da177e4SLinus Torvalds 
905d84f4f99SDavid Howells change_okay:
906d84f4f99SDavid Howells 	commit_creds(new);
9071da177e4SLinus Torvalds 	return old_fsuid;
9081da177e4SLinus Torvalds }
9091da177e4SLinus Torvalds 
SYSCALL_DEFINE1(setfsuid,uid_t,uid)910e530dca5SDominik Brodowski SYSCALL_DEFINE1(setfsuid, uid_t, uid)
911e530dca5SDominik Brodowski {
912e530dca5SDominik Brodowski 	return __sys_setfsuid(uid);
913e530dca5SDominik Brodowski }
914e530dca5SDominik Brodowski 
9151da177e4SLinus Torvalds /*
916f42df9e6SJohn Anthony Kazos Jr  * Samma på svenska..
9171da177e4SLinus Torvalds  */
__sys_setfsgid(gid_t gid)918e530dca5SDominik Brodowski long __sys_setfsgid(gid_t gid)
9191da177e4SLinus Torvalds {
920d84f4f99SDavid Howells 	const struct cred *old;
921d84f4f99SDavid Howells 	struct cred *new;
922d84f4f99SDavid Howells 	gid_t old_fsgid;
923a29c33f4SEric W. Biederman 	kgid_t kgid;
924a29c33f4SEric W. Biederman 
925a29c33f4SEric W. Biederman 	old = current_cred();
926a29c33f4SEric W. Biederman 	old_fsgid = from_kgid_munged(old->user_ns, old->fsgid);
927a29c33f4SEric W. Biederman 
928a29c33f4SEric W. Biederman 	kgid = make_kgid(old->user_ns, gid);
929a29c33f4SEric W. Biederman 	if (!gid_valid(kgid))
930a29c33f4SEric W. Biederman 		return old_fsgid;
9311da177e4SLinus Torvalds 
932d84f4f99SDavid Howells 	new = prepare_creds();
933d84f4f99SDavid Howells 	if (!new)
934a29c33f4SEric W. Biederman 		return old_fsgid;
935d84f4f99SDavid Howells 
936a29c33f4SEric W. Biederman 	if (gid_eq(kgid, old->gid)  || gid_eq(kgid, old->egid)  ||
937a29c33f4SEric W. Biederman 	    gid_eq(kgid, old->sgid) || gid_eq(kgid, old->fsgid) ||
938111767c1SThomas Cedeno 	    ns_capable_setid(old->user_ns, CAP_SETGID)) {
939a29c33f4SEric W. Biederman 		if (!gid_eq(kgid, old->fsgid)) {
940a29c33f4SEric W. Biederman 			new->fsgid = kgid;
94139030e13SThomas Cedeno 			if (security_task_fix_setgid(new,old,LSM_SETID_FS) == 0)
942d84f4f99SDavid Howells 				goto change_okay;
9431da177e4SLinus Torvalds 		}
9441da177e4SLinus Torvalds 	}
945d84f4f99SDavid Howells 
946d84f4f99SDavid Howells 	abort_creds(new);
947d84f4f99SDavid Howells 	return old_fsgid;
948d84f4f99SDavid Howells 
949d84f4f99SDavid Howells change_okay:
950d84f4f99SDavid Howells 	commit_creds(new);
9511da177e4SLinus Torvalds 	return old_fsgid;
9521da177e4SLinus Torvalds }
953e530dca5SDominik Brodowski 
SYSCALL_DEFINE1(setfsgid,gid_t,gid)954e530dca5SDominik Brodowski SYSCALL_DEFINE1(setfsgid, gid_t, gid)
955e530dca5SDominik Brodowski {
956e530dca5SDominik Brodowski 	return __sys_setfsgid(gid);
957e530dca5SDominik Brodowski }
9582813893fSIulia Manda #endif /* CONFIG_MULTIUSER */
9591da177e4SLinus Torvalds 
9604a22f166SStephen Rothwell /**
9614a22f166SStephen Rothwell  * sys_getpid - return the thread group id of the current process
9624a22f166SStephen Rothwell  *
9634a22f166SStephen Rothwell  * Note, despite the name, this returns the tgid not the pid.  The tgid and
9644a22f166SStephen Rothwell  * the pid are identical unless CLONE_THREAD was specified on clone() in
9654a22f166SStephen Rothwell  * which case the tgid is the same in all threads of the same group.
9664a22f166SStephen Rothwell  *
9674a22f166SStephen Rothwell  * This is SMP safe as current->tgid does not change.
9684a22f166SStephen Rothwell  */
SYSCALL_DEFINE0(getpid)9694a22f166SStephen Rothwell SYSCALL_DEFINE0(getpid)
9704a22f166SStephen Rothwell {
9714a22f166SStephen Rothwell 	return task_tgid_vnr(current);
9724a22f166SStephen Rothwell }
9734a22f166SStephen Rothwell 
9744a22f166SStephen Rothwell /* Thread ID - the internal kernel "pid" */
SYSCALL_DEFINE0(gettid)9754a22f166SStephen Rothwell SYSCALL_DEFINE0(gettid)
9764a22f166SStephen Rothwell {
9774a22f166SStephen Rothwell 	return task_pid_vnr(current);
9784a22f166SStephen Rothwell }
9794a22f166SStephen Rothwell 
9804a22f166SStephen Rothwell /*
9814a22f166SStephen Rothwell  * Accessing ->real_parent is not SMP-safe, it could
9824a22f166SStephen Rothwell  * change from under us. However, we can use a stale
9834a22f166SStephen Rothwell  * value of ->real_parent under rcu_read_lock(), see
9844a22f166SStephen Rothwell  * release_task()->call_rcu(delayed_put_task_struct).
9854a22f166SStephen Rothwell  */
SYSCALL_DEFINE0(getppid)9864a22f166SStephen Rothwell SYSCALL_DEFINE0(getppid)
9874a22f166SStephen Rothwell {
9884a22f166SStephen Rothwell 	int pid;
9894a22f166SStephen Rothwell 
9904a22f166SStephen Rothwell 	rcu_read_lock();
9914a22f166SStephen Rothwell 	pid = task_tgid_vnr(rcu_dereference(current->real_parent));
9924a22f166SStephen Rothwell 	rcu_read_unlock();
9934a22f166SStephen Rothwell 
9944a22f166SStephen Rothwell 	return pid;
9954a22f166SStephen Rothwell }
9964a22f166SStephen Rothwell 
SYSCALL_DEFINE0(getuid)9974a22f166SStephen Rothwell SYSCALL_DEFINE0(getuid)
9984a22f166SStephen Rothwell {
9994a22f166SStephen Rothwell 	/* Only we change this so SMP safe */
10004a22f166SStephen Rothwell 	return from_kuid_munged(current_user_ns(), current_uid());
10014a22f166SStephen Rothwell }
10024a22f166SStephen Rothwell 
SYSCALL_DEFINE0(geteuid)10034a22f166SStephen Rothwell SYSCALL_DEFINE0(geteuid)
10044a22f166SStephen Rothwell {
10054a22f166SStephen Rothwell 	/* Only we change this so SMP safe */
10064a22f166SStephen Rothwell 	return from_kuid_munged(current_user_ns(), current_euid());
10074a22f166SStephen Rothwell }
10084a22f166SStephen Rothwell 
SYSCALL_DEFINE0(getgid)10094a22f166SStephen Rothwell SYSCALL_DEFINE0(getgid)
10104a22f166SStephen Rothwell {
10114a22f166SStephen Rothwell 	/* Only we change this so SMP safe */
10124a22f166SStephen Rothwell 	return from_kgid_munged(current_user_ns(), current_gid());
10134a22f166SStephen Rothwell }
10144a22f166SStephen Rothwell 
SYSCALL_DEFINE0(getegid)10154a22f166SStephen Rothwell SYSCALL_DEFINE0(getegid)
10164a22f166SStephen Rothwell {
10174a22f166SStephen Rothwell 	/* Only we change this so SMP safe */
10184a22f166SStephen Rothwell 	return from_kgid_munged(current_user_ns(), current_egid());
10194a22f166SStephen Rothwell }
10204a22f166SStephen Rothwell 
do_sys_times(struct tms * tms)1021ca2406edSAl Viro static void do_sys_times(struct tms *tms)
1022f06febc9SFrank Mayhar {
10235613fda9SFrederic Weisbecker 	u64 tgutime, tgstime, cutime, cstime;
1024f06febc9SFrank Mayhar 
1025e80d0a1aSFrederic Weisbecker 	thread_group_cputime_adjusted(current, &tgutime, &tgstime);
1026f06febc9SFrank Mayhar 	cutime = current->signal->cutime;
1027f06febc9SFrank Mayhar 	cstime = current->signal->cstime;
10285613fda9SFrederic Weisbecker 	tms->tms_utime = nsec_to_clock_t(tgutime);
10295613fda9SFrederic Weisbecker 	tms->tms_stime = nsec_to_clock_t(tgstime);
10305613fda9SFrederic Weisbecker 	tms->tms_cutime = nsec_to_clock_t(cutime);
10315613fda9SFrederic Weisbecker 	tms->tms_cstime = nsec_to_clock_t(cstime);
1032f06febc9SFrank Mayhar }
1033f06febc9SFrank Mayhar 
SYSCALL_DEFINE1(times,struct tms __user *,tbuf)103458fd3aa2SHeiko Carstens SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
10351da177e4SLinus Torvalds {
10361da177e4SLinus Torvalds 	if (tbuf) {
10371da177e4SLinus Torvalds 		struct tms tmp;
10381da177e4SLinus Torvalds 
1039f06febc9SFrank Mayhar 		do_sys_times(&tmp);
10401da177e4SLinus Torvalds 		if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
10411da177e4SLinus Torvalds 			return -EFAULT;
10421da177e4SLinus Torvalds 	}
1043e3d5a27dSPaul Mackerras 	force_successful_syscall_return();
10441da177e4SLinus Torvalds 	return (long) jiffies_64_to_clock_t(get_jiffies_64());
10451da177e4SLinus Torvalds }
10461da177e4SLinus Torvalds 
1047ca2406edSAl Viro #ifdef CONFIG_COMPAT
clock_t_to_compat_clock_t(clock_t x)1048ca2406edSAl Viro static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
1049ca2406edSAl Viro {
1050ca2406edSAl Viro 	return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
1051ca2406edSAl Viro }
1052ca2406edSAl Viro 
COMPAT_SYSCALL_DEFINE1(times,struct compat_tms __user *,tbuf)1053ca2406edSAl Viro COMPAT_SYSCALL_DEFINE1(times, struct compat_tms __user *, tbuf)
1054ca2406edSAl Viro {
1055ca2406edSAl Viro 	if (tbuf) {
1056ca2406edSAl Viro 		struct tms tms;
1057ca2406edSAl Viro 		struct compat_tms tmp;
1058ca2406edSAl Viro 
1059ca2406edSAl Viro 		do_sys_times(&tms);
1060ca2406edSAl Viro 		/* Convert our struct tms to the compat version. */
1061ca2406edSAl Viro 		tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
1062ca2406edSAl Viro 		tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
1063ca2406edSAl Viro 		tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
1064ca2406edSAl Viro 		tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
1065ca2406edSAl Viro 		if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
1066ca2406edSAl Viro 			return -EFAULT;
1067ca2406edSAl Viro 	}
1068ca2406edSAl Viro 	force_successful_syscall_return();
1069ca2406edSAl Viro 	return compat_jiffies_to_clock_t(jiffies);
1070ca2406edSAl Viro }
1071ca2406edSAl Viro #endif
1072ca2406edSAl Viro 
10731da177e4SLinus Torvalds /*
10741da177e4SLinus Torvalds  * This needs some heavy checking ...
10751da177e4SLinus Torvalds  * I just haven't the stomach for it. I also don't fully
10761da177e4SLinus Torvalds  * understand sessions/pgrp etc. Let somebody who does explain it.
10771da177e4SLinus Torvalds  *
10781da177e4SLinus Torvalds  * OK, I think I have the protection semantics right.... this is really
10791da177e4SLinus Torvalds  * only important on a multi-user system anyway, to make sure one user
10801da177e4SLinus Torvalds  * can't send a signal to a process owned by another.  -TYT, 12/12/91
10811da177e4SLinus Torvalds  *
108298611e4eSOleg Nesterov  * !PF_FORKNOEXEC check to conform completely to POSIX.
10831da177e4SLinus Torvalds  */
SYSCALL_DEFINE2(setpgid,pid_t,pid,pid_t,pgid)1084b290ebe2SHeiko Carstens SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
10851da177e4SLinus Torvalds {
10861da177e4SLinus Torvalds 	struct task_struct *p;
1087ee0acf90SOleg Nesterov 	struct task_struct *group_leader = current->group_leader;
10884e021306SOleg Nesterov 	struct pid *pids[PIDTYPE_MAX] = { 0 };
10894e021306SOleg Nesterov 	struct pid *pgrp;
10901da177e4SLinus Torvalds 	int err;
10911da177e4SLinus Torvalds 
1092b488893aSPavel Emelyanov 	if (!pid)
10931da177e4SLinus Torvalds 		pid = task_pid_vnr(group_leader);
10941da177e4SLinus Torvalds 	if (!pgid)
10951da177e4SLinus Torvalds 		pgid = pid;
10961da177e4SLinus Torvalds 	if (pgid < 0)
1097950eaacaSPaul E. McKenney 		return -EINVAL;
10981da177e4SLinus Torvalds 	rcu_read_lock();
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	/* From this point forward we keep holding onto the tasklist lock
11011da177e4SLinus Torvalds 	 * so that our parent does not change from under us. -DaveM
11021da177e4SLinus Torvalds 	 */
11031da177e4SLinus Torvalds 	write_lock_irq(&tasklist_lock);
11041da177e4SLinus Torvalds 
11054e021306SOleg Nesterov 	err = -ESRCH;
11061da177e4SLinus Torvalds 	p = find_task_by_vpid(pid);
11071da177e4SLinus Torvalds 	if (!p)
11081da177e4SLinus Torvalds 		goto out;
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 	err = -EINVAL;
11111da177e4SLinus Torvalds 	if (!thread_group_leader(p))
11121da177e4SLinus Torvalds 		goto out;
11134e021306SOleg Nesterov 
11141da177e4SLinus Torvalds 	if (same_thread_group(p->real_parent, group_leader)) {
111541487c65SEric W. Biederman 		err = -EPERM;
11161da177e4SLinus Torvalds 		if (task_session(p) != task_session(group_leader))
11171da177e4SLinus Torvalds 			goto out;
111898611e4eSOleg Nesterov 		err = -EACCES;
11191da177e4SLinus Torvalds 		if (!(p->flags & PF_FORKNOEXEC))
11201da177e4SLinus Torvalds 			goto out;
11211da177e4SLinus Torvalds 	} else {
1122ee0acf90SOleg Nesterov 		err = -ESRCH;
11231da177e4SLinus Torvalds 		if (p != group_leader)
11241da177e4SLinus Torvalds 			goto out;
11251da177e4SLinus Torvalds 	}
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds 	err = -EPERM;
11281da177e4SLinus Torvalds 	if (p->signal->leader)
11291da177e4SLinus Torvalds 		goto out;
11304e021306SOleg Nesterov 
11311da177e4SLinus Torvalds 	pgrp = task_pid(p);
1132b488893aSPavel Emelyanov 	if (pgid != pid) {
11331da177e4SLinus Torvalds 		struct task_struct *g;
11344e021306SOleg Nesterov 
11354e021306SOleg Nesterov 		pgrp = find_vpid(pgid);
113641487c65SEric W. Biederman 		g = pid_task(pgrp, PIDTYPE_PGID);
11371da177e4SLinus Torvalds 		if (!g || task_session(g) != task_session(group_leader))
11381da177e4SLinus Torvalds 			goto out;
11391da177e4SLinus Torvalds 	}
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 	err = security_task_setpgid(p, pgid);
11421da177e4SLinus Torvalds 	if (err)
11431da177e4SLinus Torvalds 		goto out;
11441b0f7ffdSOleg Nesterov 
114583beaf3cSOleg Nesterov 	if (task_pgrp(p) != pgrp)
11461da177e4SLinus Torvalds 		change_pid(pids, p, PIDTYPE_PGID, pgrp);
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 	err = 0;
11491da177e4SLinus Torvalds out:
11501da177e4SLinus Torvalds 	/* All paths lead to here, thus we are safe. -DaveM */
1151950eaacaSPaul E. McKenney 	write_unlock_irq(&tasklist_lock);
11521da177e4SLinus Torvalds 	rcu_read_unlock();
11531da177e4SLinus Torvalds 	free_pids(pids);
11541da177e4SLinus Torvalds 	return err;
1155192c5807SDominik Brodowski }
11561da177e4SLinus Torvalds 
do_getpgid(pid_t pid)11571da177e4SLinus Torvalds static int do_getpgid(pid_t pid)
115812a3de0aSOleg Nesterov {
115912a3de0aSOleg Nesterov 	struct task_struct *p;
11601da177e4SLinus Torvalds 	struct pid *grp;
116112a3de0aSOleg Nesterov 	int retval;
116212a3de0aSOleg Nesterov 
116312a3de0aSOleg Nesterov 	rcu_read_lock();
116412a3de0aSOleg Nesterov 	if (!pid)
11651da177e4SLinus Torvalds 		grp = task_pgrp(current);
116612a3de0aSOleg Nesterov 	else {
116712a3de0aSOleg Nesterov 		retval = -ESRCH;
116812a3de0aSOleg Nesterov 		p = find_task_by_vpid(pid);
116912a3de0aSOleg Nesterov 		if (!p)
117012a3de0aSOleg Nesterov 			goto out;
117112a3de0aSOleg Nesterov 		grp = task_pgrp(p);
117212a3de0aSOleg Nesterov 		if (!grp)
11731da177e4SLinus Torvalds 			goto out;
117412a3de0aSOleg Nesterov 
117512a3de0aSOleg Nesterov 		retval = security_task_getpgid(p);
11761da177e4SLinus Torvalds 		if (retval)
117712a3de0aSOleg Nesterov 			goto out;
117812a3de0aSOleg Nesterov 	}
117912a3de0aSOleg Nesterov 	retval = pid_vnr(grp);
11801da177e4SLinus Torvalds out:
11811da177e4SLinus Torvalds 	rcu_read_unlock();
11821da177e4SLinus Torvalds 	return retval;
1183192c5807SDominik Brodowski }
1184192c5807SDominik Brodowski 
SYSCALL_DEFINE1(getpgid,pid_t,pid)1185192c5807SDominik Brodowski SYSCALL_DEFINE1(getpgid, pid_t, pid)
1186192c5807SDominik Brodowski {
1187192c5807SDominik Brodowski 	return do_getpgid(pid);
11881da177e4SLinus Torvalds }
11891da177e4SLinus Torvalds 
1190dbf040d9SHeiko Carstens #ifdef __ARCH_WANT_SYS_GETPGRP
11911da177e4SLinus Torvalds 
SYSCALL_DEFINE0(getpgrp)1192192c5807SDominik Brodowski SYSCALL_DEFINE0(getpgrp)
11931da177e4SLinus Torvalds {
11941da177e4SLinus Torvalds 	return do_getpgid(0);
11951da177e4SLinus Torvalds }
11961da177e4SLinus Torvalds 
1197dbf040d9SHeiko Carstens #endif
11981da177e4SLinus Torvalds 
SYSCALL_DEFINE1(getsid,pid_t,pid)11991da177e4SLinus Torvalds SYSCALL_DEFINE1(getsid, pid_t, pid)
12001dd768c0SOleg Nesterov {
12011dd768c0SOleg Nesterov 	struct task_struct *p;
1202b488893aSPavel Emelyanov 	struct pid *sid;
1203ac9a8e3fSOleg Nesterov 	int retval;
12041dd768c0SOleg Nesterov 
12051dd768c0SOleg Nesterov 	rcu_read_lock();
12061dd768c0SOleg Nesterov 	if (!pid)
12071da177e4SLinus Torvalds 		sid = task_session(current);
12081dd768c0SOleg Nesterov 	else {
12091dd768c0SOleg Nesterov 		retval = -ESRCH;
12101dd768c0SOleg Nesterov 		p = find_task_by_vpid(pid);
12111dd768c0SOleg Nesterov 		if (!p)
12121dd768c0SOleg Nesterov 			goto out;
12131dd768c0SOleg Nesterov 		sid = task_session(p);
12141dd768c0SOleg Nesterov 		if (!sid)
12151da177e4SLinus Torvalds 			goto out;
12161dd768c0SOleg Nesterov 
12171dd768c0SOleg Nesterov 		retval = security_task_getsid(p);
12181da177e4SLinus Torvalds 		if (retval)
12191dd768c0SOleg Nesterov 			goto out;
12201dd768c0SOleg Nesterov 	}
1221ac9a8e3fSOleg Nesterov 	retval = pid_vnr(sid);
12221da177e4SLinus Torvalds out:
12231da177e4SLinus Torvalds 	rcu_read_unlock();
12241da177e4SLinus Torvalds 	return retval;
122581dabb46SOleg Nesterov }
122681dabb46SOleg Nesterov 
set_special_pids(struct pid ** pids,struct pid * pid)122781dabb46SOleg Nesterov static void set_special_pids(struct pid **pids, struct pid *pid)
122881dabb46SOleg Nesterov {
122981dabb46SOleg Nesterov 	struct task_struct *curr = current->group_leader;
123081dabb46SOleg Nesterov 
123181dabb46SOleg Nesterov 	if (task_session(curr) != pid)
123281dabb46SOleg Nesterov 		change_pid(pids, curr, PIDTYPE_SID, pid);
123381dabb46SOleg Nesterov 
123481dabb46SOleg Nesterov 	if (task_pgrp(curr) != pid)
123581dabb46SOleg Nesterov 		change_pid(pids, curr, PIDTYPE_PGID, pid);
1236e2aaa9f4SDominik Brodowski }
12371da177e4SLinus Torvalds 
ksys_setsid(void)1238e19f247aSOren Laadan int ksys_setsid(void)
1239e4cc0a9cSOleg Nesterov {
1240e4cc0a9cSOleg Nesterov 	struct task_struct *group_leader = current->group_leader;
12411da177e4SLinus Torvalds 	struct pid *sid = task_pid(group_leader);
12421da177e4SLinus Torvalds 	struct pid *pids[PIDTYPE_MAX] = { 0 };
12431da177e4SLinus Torvalds 	pid_t session = pid_vnr(sid);
1244390e2ff0SEric W. Biederman 	int err = -EPERM;
1245390e2ff0SEric W. Biederman 
1246390e2ff0SEric W. Biederman 	write_lock_irq(&tasklist_lock);
1247390e2ff0SEric W. Biederman 	/* Fail if I am already a session leader */
1248430c6231SOleg Nesterov 	if (group_leader->signal->leader)
1249430c6231SOleg Nesterov 		goto out;
1250390e2ff0SEric W. Biederman 
12516806aac6SOleg Nesterov 	/* Fail if a process group id already exists that equals the
12521da177e4SLinus Torvalds 	 * proposed session id.
12531da177e4SLinus Torvalds 	 */
1254e19f247aSOren Laadan 	if (pid_task(sid, PIDTYPE_PGID))
125581dabb46SOleg Nesterov 		goto out;
125624ec839cSPeter Zijlstra 
12579c9f4dedSAlan Cox 	group_leader->signal->leader = 1;
125824ec839cSPeter Zijlstra 	set_special_pids(pids, sid);
1259e4cc0a9cSOleg Nesterov 
12601da177e4SLinus Torvalds 	proc_clear_tty(group_leader);
12611da177e4SLinus Torvalds 
12625091faa4SMike Galbraith 	err = session;
12630d0df599SChristian Borntraeger out:
12645091faa4SMike Galbraith 	write_unlock_irq(&tasklist_lock);
12655091faa4SMike Galbraith 	free_pids(pids);
12661da177e4SLinus Torvalds 	if (err > 0) {
12671da177e4SLinus Torvalds 		proc_sid_connector(group_leader);
12681da177e4SLinus Torvalds 		sched_autogroup_create_attach(group_leader);
1269e2aaa9f4SDominik Brodowski 	}
1270e2aaa9f4SDominik Brodowski 	return err;
1271e2aaa9f4SDominik Brodowski }
1272e2aaa9f4SDominik Brodowski 
SYSCALL_DEFINE0(setsid)1273e2aaa9f4SDominik Brodowski SYSCALL_DEFINE0(setsid)
12741da177e4SLinus Torvalds {
12751da177e4SLinus Torvalds 	return ksys_setsid();
1276e28cbf22SChristoph Hellwig }
1277e28cbf22SChristoph Hellwig 
127846da2766SAndreas Schwab DECLARE_RWSEM(uts_sem);
1279e28cbf22SChristoph Hellwig 
1280e28cbf22SChristoph Hellwig #ifdef COMPAT_UTS_MACHINE
1281e28cbf22SChristoph Hellwig #define override_architecture(name) \
1282e28cbf22SChristoph Hellwig 	(personality(current->personality) == PER_LINUX32 && \
1283e28cbf22SChristoph Hellwig 	 copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1284e28cbf22SChristoph Hellwig 		      sizeof(COMPAT_UTS_MACHINE)))
1285be27425dSAndi Kleen #else
1286be27425dSAndi Kleen #define override_architecture(name)	0
1287be27425dSAndi Kleen #endif
1288b7285b42SJonathan Neuschäfer 
1289b7285b42SJonathan Neuschäfer /*
1290be27425dSAndi Kleen  * Work around broken programs that cannot handle "Linux 3.0".
12912702b152SKees Cook  * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40
1292be27425dSAndi Kleen  * And we map 4.x and later versions to 2.6.60+x, so 4.0/5.0/6.0/... would be
1293be27425dSAndi Kleen  * 2.6.60.
1294be27425dSAndi Kleen  */
override_release(char __user * release,size_t len)1295be27425dSAndi Kleen static int override_release(char __user *release, size_t len)
12962702b152SKees Cook {
12972702b152SKees Cook 	int ret = 0;
1298be27425dSAndi Kleen 
1299be27425dSAndi Kleen 	if (current->personality & UNAME26) {
13002702b152SKees Cook 		const char *rest = UTS_RELEASE;
1301be27425dSAndi Kleen 		char buf[65] = { 0 };
1302be27425dSAndi Kleen 		int ndots = 0;
1303be27425dSAndi Kleen 		unsigned v;
1304be27425dSAndi Kleen 		size_t copy;
1305be27425dSAndi Kleen 
1306be27425dSAndi Kleen 		while (*rest) {
1307be27425dSAndi Kleen 			if (*rest == '.' && ++ndots >= 3)
1308be27425dSAndi Kleen 				break;
130988a68672SSasha Levin 			if (!isdigit(*rest) && *rest != '.')
131031fd84b9SKees Cook 				break;
13112702b152SKees Cook 			rest++;
13122702b152SKees Cook 		}
1313be27425dSAndi Kleen 		v = LINUX_VERSION_PATCHLEVEL + 60;
1314be27425dSAndi Kleen 		copy = clamp_t(size_t, len, 1, sizeof(buf));
1315be27425dSAndi Kleen 		copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
1316be27425dSAndi Kleen 		ret = copy_to_user(release, buf, copy + 1);
1317e48fbb69SHeiko Carstens 	}
13181da177e4SLinus Torvalds 	return ret;
131942a0cc34SJann Horn }
13201da177e4SLinus Torvalds 
SYSCALL_DEFINE1(newuname,struct new_utsname __user *,name)13211da177e4SLinus Torvalds SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
132242a0cc34SJann Horn {
13231da177e4SLinus Torvalds 	struct new_utsname tmp;
132442a0cc34SJann Horn 
132542a0cc34SJann Horn 	down_read(&uts_sem);
1326e28cbf22SChristoph Hellwig 	memcpy(&tmp, utsname(), sizeof(tmp));
132742a0cc34SJann Horn 	up_read(&uts_sem);
132842a0cc34SJann Horn 	if (copy_to_user(name, &tmp, sizeof(tmp)))
132942a0cc34SJann Horn 		return -EFAULT;
133042a0cc34SJann Horn 
133142a0cc34SJann Horn 	if (override_release(name->release, sizeof(name->release)))
13321da177e4SLinus Torvalds 		return -EFAULT;
13331da177e4SLinus Torvalds 	if (override_architecture(name))
13345cacdb4aSChristoph Hellwig 		return -EFAULT;
13355cacdb4aSChristoph Hellwig 	return 0;
13365cacdb4aSChristoph Hellwig }
13375cacdb4aSChristoph Hellwig 
13385cacdb4aSChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_UNAME
13395cacdb4aSChristoph Hellwig /*
134042a0cc34SJann Horn  * Old cruft
13415cacdb4aSChristoph Hellwig  */
SYSCALL_DEFINE1(uname,struct old_utsname __user *,name)13425cacdb4aSChristoph Hellwig SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
13435cacdb4aSChristoph Hellwig {
13445cacdb4aSChristoph Hellwig 	struct old_utsname tmp;
13455cacdb4aSChristoph Hellwig 
134642a0cc34SJann Horn 	if (!name)
13475cacdb4aSChristoph Hellwig 		return -EFAULT;
134842a0cc34SJann Horn 
134942a0cc34SJann Horn 	down_read(&uts_sem);
13505cacdb4aSChristoph Hellwig 	memcpy(&tmp, utsname(), sizeof(tmp));
135142a0cc34SJann Horn 	up_read(&uts_sem);
135242a0cc34SJann Horn 	if (copy_to_user(name, &tmp, sizeof(tmp)))
135342a0cc34SJann Horn 		return -EFAULT;
135442a0cc34SJann Horn 
135542a0cc34SJann Horn 	if (override_release(name->release, sizeof(name->release)))
13565cacdb4aSChristoph Hellwig 		return -EFAULT;
13575cacdb4aSChristoph Hellwig 	if (override_architecture(name))
13585cacdb4aSChristoph Hellwig 		return -EFAULT;
13595cacdb4aSChristoph Hellwig 	return 0;
13605e1aada0SJoe Perches }
13615cacdb4aSChristoph Hellwig 
SYSCALL_DEFINE1(olduname,struct oldold_utsname __user *,name)13625cacdb4aSChristoph Hellwig SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
13635cacdb4aSChristoph Hellwig {
13645cacdb4aSChristoph Hellwig 	struct oldold_utsname tmp;
13655e1aada0SJoe Perches 
13665e1aada0SJoe Perches 	if (!name)
13675cacdb4aSChristoph Hellwig 		return -EFAULT;
136842a0cc34SJann Horn 
136942a0cc34SJann Horn 	memset(&tmp, 0, sizeof(tmp));
137042a0cc34SJann Horn 
137142a0cc34SJann Horn 	down_read(&uts_sem);
137242a0cc34SJann Horn 	memcpy(&tmp.sysname, &utsname()->sysname, __OLD_UTS_LEN);
13735cacdb4aSChristoph Hellwig 	memcpy(&tmp.nodename, &utsname()->nodename, __OLD_UTS_LEN);
137442a0cc34SJann Horn 	memcpy(&tmp.release, &utsname()->release, __OLD_UTS_LEN);
137542a0cc34SJann Horn 	memcpy(&tmp.version, &utsname()->version, __OLD_UTS_LEN);
13765cacdb4aSChristoph Hellwig 	memcpy(&tmp.machine, &utsname()->machine, __OLD_UTS_LEN);
137742a0cc34SJann Horn 	up_read(&uts_sem);
137842a0cc34SJann Horn 	if (copy_to_user(name, &tmp, sizeof(tmp)))
137942a0cc34SJann Horn 		return -EFAULT;
138042a0cc34SJann Horn 
138142a0cc34SJann Horn 	if (override_architecture(name))
13825cacdb4aSChristoph Hellwig 		return -EFAULT;
13835cacdb4aSChristoph Hellwig 	if (override_release(name->release, sizeof(name->release)))
13845cacdb4aSChristoph Hellwig 		return -EFAULT;
13855a8a82b1SHeiko Carstens 	return 0;
13861da177e4SLinus Torvalds }
13871da177e4SLinus Torvalds #endif
13881da177e4SLinus Torvalds 
SYSCALL_DEFINE2(sethostname,char __user *,name,int,len)13891da177e4SLinus Torvalds SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
1390bb96a6f5SSerge E. Hallyn {
13911da177e4SLinus Torvalds 	int errno;
1392fc832ad3SSerge E. Hallyn 	char tmp[__NEW_UTS_LEN];
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds 	if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
13951da177e4SLinus Torvalds 		return -EPERM;
13961da177e4SLinus Torvalds 
139742a0cc34SJann Horn 	if (len < 0 || len > __NEW_UTS_LEN)
13989679e4ddSAndrew Morton 		return -EINVAL;
139937608ba3SJason A. Donenfeld 	errno = -EFAULT;
140042a0cc34SJann Horn 	if (!copy_from_user(tmp, name, len)) {
140142a0cc34SJann Horn 		struct new_utsname *u;
14029679e4ddSAndrew Morton 
14039679e4ddSAndrew Morton 		add_device_randomness(tmp, len);
14041da177e4SLinus Torvalds 		down_write(&uts_sem);
1405f1ecf068SLucas De Marchi 		u = utsname();
14061da177e4SLinus Torvalds 		memcpy(u->nodename, tmp, len);
140742a0cc34SJann Horn 		memset(u->nodename + len, 0, sizeof(u->nodename) - len);
14081da177e4SLinus Torvalds 		errno = 0;
14091da177e4SLinus Torvalds 		uts_proc_notify(UTS_PROC_HOSTNAME);
14101da177e4SLinus Torvalds 		up_write(&uts_sem);
14111da177e4SLinus Torvalds 	}
14121da177e4SLinus Torvalds 	return errno;
14135a8a82b1SHeiko Carstens }
14141da177e4SLinus Torvalds 
141542a0cc34SJann Horn #ifdef __ARCH_WANT_SYS_GETHOSTNAME
14169679e4ddSAndrew Morton 
SYSCALL_DEFINE2(gethostname,char __user *,name,int,len)141742a0cc34SJann Horn SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
14181da177e4SLinus Torvalds {
14191da177e4SLinus Torvalds 	int i;
14201da177e4SLinus Torvalds 	struct new_utsname *u;
14211da177e4SLinus Torvalds 	char tmp[__NEW_UTS_LEN + 1];
14229679e4ddSAndrew Morton 
14239679e4ddSAndrew Morton 	if (len < 0)
14241da177e4SLinus Torvalds 		return -EINVAL;
14251da177e4SLinus Torvalds 	down_read(&uts_sem);
142642a0cc34SJann Horn 	u = utsname();
14271da177e4SLinus Torvalds 	i = 1 + strlen(u->nodename);
142842a0cc34SJann Horn 	if (i > len)
142942a0cc34SJann Horn 		i = len;
143042a0cc34SJann Horn 	memcpy(tmp, u->nodename, i);
14311da177e4SLinus Torvalds 	up_read(&uts_sem);
14321da177e4SLinus Torvalds 	if (copy_to_user(name, tmp, i))
14331da177e4SLinus Torvalds 		return -EFAULT;
14341da177e4SLinus Torvalds 	return 0;
14351da177e4SLinus Torvalds }
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds #endif
14381da177e4SLinus Torvalds 
14395a8a82b1SHeiko Carstens /*
14401da177e4SLinus Torvalds  * Only setdomainname; getdomainname can be implemented by calling
14411da177e4SLinus Torvalds  * uname()
14421da177e4SLinus Torvalds  */
SYSCALL_DEFINE2(setdomainname,char __user *,name,int,len)14431da177e4SLinus Torvalds SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
1444fc832ad3SSerge E. Hallyn {
14451da177e4SLinus Torvalds 	int errno;
14461da177e4SLinus Torvalds 	char tmp[__NEW_UTS_LEN];
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds 	if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
14491da177e4SLinus Torvalds 		return -EPERM;
14501da177e4SLinus Torvalds 	if (len < 0 || len > __NEW_UTS_LEN)
145142a0cc34SJann Horn 		return -EINVAL;
14529679e4ddSAndrew Morton 
145337608ba3SJason A. Donenfeld 	errno = -EFAULT;
145442a0cc34SJann Horn 	if (!copy_from_user(tmp, name, len)) {
145542a0cc34SJann Horn 		struct new_utsname *u;
14569679e4ddSAndrew Morton 
14579679e4ddSAndrew Morton 		add_device_randomness(tmp, len);
14581da177e4SLinus Torvalds 		down_write(&uts_sem);
1459f1ecf068SLucas De Marchi 		u = utsname();
14601da177e4SLinus Torvalds 		memcpy(u->domainname, tmp, len);
146142a0cc34SJann Horn 		memset(u->domainname + len, 0, sizeof(u->domainname) - len);
14621da177e4SLinus Torvalds 		errno = 0;
14631da177e4SLinus Torvalds 		uts_proc_notify(UTS_PROC_DOMAINNAME);
14641da177e4SLinus Torvalds 		up_write(&uts_sem);
1465c57bef02SBarret Rhoden 	}
1466c57bef02SBarret Rhoden 	return errno;
1467c57bef02SBarret Rhoden }
1468c57bef02SBarret Rhoden 
1469c57bef02SBarret 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)1470c57bef02SBarret Rhoden static int do_prlimit(struct task_struct *tsk, unsigned int resource,
1471c57bef02SBarret Rhoden 		      struct rlimit *new_rlim, struct rlimit *old_rlim)
1472c57bef02SBarret Rhoden {
1473c57bef02SBarret Rhoden 	struct rlimit *rlim;
147473979060SGreg Kroah-Hartman 	int retval = 0;
147573979060SGreg Kroah-Hartman 
1476c57bef02SBarret Rhoden 	if (resource >= RLIM_NLIMITS)
1477c57bef02SBarret Rhoden 		return -EINVAL;
1478c57bef02SBarret Rhoden 	resource = array_index_nospec(resource, RLIM_NLIMITS);
1479c57bef02SBarret Rhoden 
1480c57bef02SBarret Rhoden 	if (new_rlim) {
1481c57bef02SBarret Rhoden 		if (new_rlim->rlim_cur > new_rlim->rlim_max)
1482c57bef02SBarret Rhoden 			return -EINVAL;
1483c57bef02SBarret Rhoden 		if (resource == RLIMIT_NOFILE &&
148418c91bb2SBarret Rhoden 				new_rlim->rlim_max > sysctl_nr_open)
1485c57bef02SBarret Rhoden 			return -EPERM;
1486c57bef02SBarret Rhoden 	}
1487c57bef02SBarret Rhoden 
1488c57bef02SBarret Rhoden 	/* Holding a refcount on tsk protects tsk->signal from disappearing. */
1489c57bef02SBarret Rhoden 	rlim = tsk->signal->rlim + resource;
1490c57bef02SBarret Rhoden 	task_lock(tsk->group_leader);
1491c57bef02SBarret Rhoden 	if (new_rlim) {
1492c57bef02SBarret Rhoden 		/*
1493c57bef02SBarret Rhoden 		 * Keep the capable check against init_user_ns until cgroups can
1494c57bef02SBarret Rhoden 		 * contain all limits.
1495c57bef02SBarret Rhoden 		 */
1496c57bef02SBarret Rhoden 		if (new_rlim->rlim_max > rlim->rlim_max &&
1497c57bef02SBarret Rhoden 				!capable(CAP_SYS_RESOURCE))
1498c57bef02SBarret Rhoden 			retval = -EPERM;
1499c57bef02SBarret Rhoden 		if (!retval)
1500c57bef02SBarret Rhoden 			retval = security_task_setrlimit(tsk, resource, new_rlim);
1501c57bef02SBarret Rhoden 	}
1502c57bef02SBarret Rhoden 	if (!retval) {
1503c57bef02SBarret Rhoden 		if (old_rlim)
1504c57bef02SBarret Rhoden 			*old_rlim = *rlim;
1505c57bef02SBarret Rhoden 		if (new_rlim)
1506c57bef02SBarret Rhoden 			*rlim = *new_rlim;
1507c57bef02SBarret Rhoden 	}
1508c57bef02SBarret Rhoden 	task_unlock(tsk->group_leader);
1509c57bef02SBarret Rhoden 
1510c57bef02SBarret Rhoden 	/*
1511c57bef02SBarret Rhoden 	 * RLIMIT_CPU handling. Arm the posix CPU timer if the limit is not
1512c57bef02SBarret Rhoden 	 * infinite. In case of RLIM_INFINITY the posix CPU timer code
151318c91bb2SBarret Rhoden 	 * ignores the rlimit.
151418c91bb2SBarret Rhoden 	 */
151518c91bb2SBarret Rhoden 	if (!retval && new_rlim && resource == RLIMIT_CPU &&
151618c91bb2SBarret Rhoden 	    new_rlim->rlim_cur != RLIM_INFINITY &&
151718c91bb2SBarret Rhoden 	    IS_ENABLED(CONFIG_POSIX_TIMERS)) {
151818c91bb2SBarret Rhoden 		/*
151918c91bb2SBarret Rhoden 		 * update_rlimit_cpu can fail if the task is exiting, but there
152018c91bb2SBarret Rhoden 		 * may be other tasks in the thread group that are not exiting,
152118c91bb2SBarret Rhoden 		 * and they need their cpu timers adjusted.
152218c91bb2SBarret Rhoden 		 *
152318c91bb2SBarret Rhoden 		 * The group_leader is the last task to be released, so if we
152418c91bb2SBarret Rhoden 		 * cannot update_rlimit_cpu on it, then the entire process is
152518c91bb2SBarret Rhoden 		 * exiting and we do not need to update at all.
1526c57bef02SBarret Rhoden 		 */
1527c57bef02SBarret Rhoden 		update_rlimit_cpu(tsk->group_leader, new_rlim->rlim_cur);
1528c57bef02SBarret Rhoden 	}
1529e48fbb69SHeiko Carstens 
15301da177e4SLinus Torvalds 	return retval;
15311da177e4SLinus Torvalds }
1532b9518345SJiri Slaby 
SYSCALL_DEFINE2(getrlimit,unsigned int,resource,struct rlimit __user *,rlim)1533b9518345SJiri Slaby SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1534b9518345SJiri Slaby {
1535b9518345SJiri Slaby 	struct rlimit value;
1536b9518345SJiri Slaby 	int ret;
1537b9518345SJiri Slaby 
1538b9518345SJiri Slaby 	ret = do_prlimit(current, resource, NULL, &value);
15391da177e4SLinus Torvalds 	if (!ret)
15401da177e4SLinus Torvalds 		ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1541d9e968cbSAl Viro 
1542d9e968cbSAl Viro 	return ret;
1543d9e968cbSAl Viro }
1544d9e968cbSAl Viro 
1545d9e968cbSAl Viro #ifdef CONFIG_COMPAT
1546d9e968cbSAl Viro 
COMPAT_SYSCALL_DEFINE2(setrlimit,unsigned int,resource,struct compat_rlimit __user *,rlim)1547d9e968cbSAl Viro COMPAT_SYSCALL_DEFINE2(setrlimit, unsigned int, resource,
1548d9e968cbSAl Viro 		       struct compat_rlimit __user *, rlim)
1549d9e968cbSAl Viro {
1550d9e968cbSAl Viro 	struct rlimit r;
1551d9e968cbSAl Viro 	struct compat_rlimit r32;
1552d9e968cbSAl Viro 
1553d9e968cbSAl Viro 	if (copy_from_user(&r32, rlim, sizeof(struct compat_rlimit)))
1554d9e968cbSAl Viro 		return -EFAULT;
1555d9e968cbSAl Viro 
1556d9e968cbSAl Viro 	if (r32.rlim_cur == COMPAT_RLIM_INFINITY)
1557d9e968cbSAl Viro 		r.rlim_cur = RLIM_INFINITY;
1558d9e968cbSAl Viro 	else
1559d9e968cbSAl Viro 		r.rlim_cur = r32.rlim_cur;
1560d9e968cbSAl Viro 	if (r32.rlim_max == COMPAT_RLIM_INFINITY)
1561d9e968cbSAl Viro 		r.rlim_max = RLIM_INFINITY;
1562d9e968cbSAl Viro 	else
1563d9e968cbSAl Viro 		r.rlim_max = r32.rlim_max;
1564d9e968cbSAl Viro 	return do_prlimit(current, resource, &r, NULL);
1565d9e968cbSAl Viro }
1566d9e968cbSAl Viro 
COMPAT_SYSCALL_DEFINE2(getrlimit,unsigned int,resource,struct compat_rlimit __user *,rlim)1567d9e968cbSAl Viro COMPAT_SYSCALL_DEFINE2(getrlimit, unsigned int, resource,
1568d9e968cbSAl Viro 		       struct compat_rlimit __user *, rlim)
1569d9e968cbSAl Viro {
1570d9e968cbSAl Viro 	struct rlimit r;
157158c7ffc0SAl Viro 	int ret;
1572d9e968cbSAl Viro 
1573d9e968cbSAl Viro 	ret = do_prlimit(current, resource, NULL, &r);
1574d9e968cbSAl Viro 	if (!ret) {
1575d9e968cbSAl Viro 		struct compat_rlimit r32;
1576d9e968cbSAl Viro 		if (r.rlim_cur > COMPAT_RLIM_INFINITY)
1577d9e968cbSAl Viro 			r32.rlim_cur = COMPAT_RLIM_INFINITY;
1578d9e968cbSAl Viro 		else
1579d9e968cbSAl Viro 			r32.rlim_cur = r.rlim_cur;
1580d9e968cbSAl Viro 		if (r.rlim_max > COMPAT_RLIM_INFINITY)
1581d9e968cbSAl Viro 			r32.rlim_max = COMPAT_RLIM_INFINITY;
1582d9e968cbSAl Viro 		else
1583d9e968cbSAl Viro 			r32.rlim_max = r.rlim_max;
1584d9e968cbSAl Viro 
1585d9e968cbSAl Viro 		if (copy_to_user(rlim, &r32, sizeof(struct compat_rlimit)))
1586d9e968cbSAl Viro 			return -EFAULT;
1587d9e968cbSAl Viro 	}
1588d9e968cbSAl Viro 	return ret;
15891da177e4SLinus Torvalds }
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds #endif
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1594e48fbb69SHeiko Carstens 
1595e48fbb69SHeiko Carstens /*
15961da177e4SLinus Torvalds  *	Back compatibility for getrlimit. Needed for some apps.
15971da177e4SLinus Torvalds  */
SYSCALL_DEFINE2(old_getrlimit,unsigned int,resource,struct rlimit __user *,rlim)15981da177e4SLinus Torvalds SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
15991da177e4SLinus Torvalds 		struct rlimit __user *, rlim)
16001da177e4SLinus Torvalds {
160123d6aef7SGustavo A. R. Silva 	struct rlimit x;
16021da177e4SLinus Torvalds 	if (resource >= RLIM_NLIMITS)
16031da177e4SLinus Torvalds 		return -EINVAL;
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 	resource = array_index_nospec(resource, RLIM_NLIMITS);
16061da177e4SLinus Torvalds 	task_lock(current->group_leader);
16071da177e4SLinus Torvalds 	x = current->signal->rlim[resource];
16081da177e4SLinus Torvalds 	task_unlock(current->group_leader);
16091da177e4SLinus Torvalds 	if (x.rlim_cur > 0x7FFFFFFF)
16101da177e4SLinus Torvalds 		x.rlim_cur = 0x7FFFFFFF;
16111da177e4SLinus Torvalds 	if (x.rlim_max > 0x7FFFFFFF)
1612613763a1SAl Viro 		x.rlim_max = 0x7FFFFFFF;
1613613763a1SAl Viro 	return copy_to_user(rlim, &x, sizeof(x)) ? -EFAULT : 0;
1614613763a1SAl Viro }
1615613763a1SAl Viro 
1616613763a1SAl Viro #ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE2(old_getrlimit,unsigned int,resource,struct compat_rlimit __user *,rlim)1617613763a1SAl Viro COMPAT_SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1618613763a1SAl Viro 		       struct compat_rlimit __user *, rlim)
1619613763a1SAl Viro {
1620613763a1SAl Viro 	struct rlimit r;
162123d6aef7SGustavo A. R. Silva 
1622613763a1SAl Viro 	if (resource >= RLIM_NLIMITS)
1623613763a1SAl Viro 		return -EINVAL;
1624613763a1SAl Viro 
1625613763a1SAl Viro 	resource = array_index_nospec(resource, RLIM_NLIMITS);
1626613763a1SAl Viro 	task_lock(current->group_leader);
1627613763a1SAl Viro 	r = current->signal->rlim[resource];
1628613763a1SAl Viro 	task_unlock(current->group_leader);
1629613763a1SAl Viro 	if (r.rlim_cur > 0x7FFFFFFF)
1630613763a1SAl Viro 		r.rlim_cur = 0x7FFFFFFF;
1631613763a1SAl Viro 	if (r.rlim_max > 0x7FFFFFFF)
1632613763a1SAl Viro 		r.rlim_max = 0x7FFFFFFF;
1633613763a1SAl Viro 
1634613763a1SAl Viro 	if (put_user(r.rlim_cur, &rlim->rlim_cur) ||
1635613763a1SAl Viro 	    put_user(r.rlim_max, &rlim->rlim_max))
1636613763a1SAl Viro 		return -EFAULT;
16371da177e4SLinus Torvalds 	return 0;
16381da177e4SLinus Torvalds }
1639c022a0acSJiri Slaby #endif
1640c022a0acSJiri Slaby 
1641c022a0acSJiri Slaby #endif
1642c022a0acSJiri Slaby 
rlim64_is_infinity(__u64 rlim64)1643c022a0acSJiri Slaby static inline bool rlim64_is_infinity(__u64 rlim64)
1644c022a0acSJiri Slaby {
1645c022a0acSJiri Slaby #if BITS_PER_LONG < 64
1646c022a0acSJiri Slaby 	return rlim64 >= ULONG_MAX;
1647c022a0acSJiri Slaby #else
1648c022a0acSJiri Slaby 	return rlim64 == RLIM64_INFINITY;
1649c022a0acSJiri Slaby #endif
1650c022a0acSJiri Slaby }
1651c022a0acSJiri Slaby 
rlim_to_rlim64(const struct rlimit * rlim,struct rlimit64 * rlim64)1652c022a0acSJiri Slaby static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1653c022a0acSJiri Slaby {
1654c022a0acSJiri Slaby 	if (rlim->rlim_cur == RLIM_INFINITY)
1655c022a0acSJiri Slaby 		rlim64->rlim_cur = RLIM64_INFINITY;
1656c022a0acSJiri Slaby 	else
1657c022a0acSJiri Slaby 		rlim64->rlim_cur = rlim->rlim_cur;
1658c022a0acSJiri Slaby 	if (rlim->rlim_max == RLIM_INFINITY)
1659c022a0acSJiri Slaby 		rlim64->rlim_max = RLIM64_INFINITY;
1660c022a0acSJiri Slaby 	else
1661c022a0acSJiri Slaby 		rlim64->rlim_max = rlim->rlim_max;
1662c022a0acSJiri Slaby }
1663c022a0acSJiri Slaby 
rlim64_to_rlim(const struct rlimit64 * rlim64,struct rlimit * rlim)1664c022a0acSJiri Slaby static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1665c022a0acSJiri Slaby {
1666c022a0acSJiri Slaby 	if (rlim64_is_infinity(rlim64->rlim_cur))
1667c022a0acSJiri Slaby 		rlim->rlim_cur = RLIM_INFINITY;
1668c022a0acSJiri Slaby 	else
1669c022a0acSJiri Slaby 		rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1670c022a0acSJiri Slaby 	if (rlim64_is_infinity(rlim64->rlim_max))
1671c022a0acSJiri Slaby 		rlim->rlim_max = RLIM_INFINITY;
1672c022a0acSJiri Slaby 	else
1673791ec491SStephen Smalley 		rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1674791ec491SStephen Smalley }
1675c022a0acSJiri Slaby 
1676c022a0acSJiri Slaby /* rcu lock must be held */
check_prlimit_permission(struct task_struct * task,unsigned int flags)1677791ec491SStephen Smalley static int check_prlimit_permission(struct task_struct *task,
1678c022a0acSJiri Slaby 				    unsigned int flags)
1679fc832ad3SSerge E. Hallyn {
1680c022a0acSJiri Slaby 	const struct cred *cred = current_cred(), *tcred;
1681fc832ad3SSerge E. Hallyn 	bool id_match;
1682fc832ad3SSerge E. Hallyn 
1683791ec491SStephen Smalley 	if (current == task)
16845af66203SEric W. Biederman 		return 0;
16855af66203SEric W. Biederman 
16865af66203SEric W. Biederman 	tcred = __task_cred(task);
16875af66203SEric W. Biederman 	id_match = (uid_eq(cred->uid, tcred->euid) &&
1688791ec491SStephen Smalley 		    uid_eq(cred->uid, tcred->suid) &&
1689791ec491SStephen Smalley 		    uid_eq(cred->uid, tcred->uid)  &&
1690fc832ad3SSerge E. Hallyn 		    gid_eq(cred->gid, tcred->egid) &&
1691791ec491SStephen Smalley 		    gid_eq(cred->gid, tcred->sgid) &&
1692791ec491SStephen Smalley 		    gid_eq(cred->gid, tcred->gid));
1693c022a0acSJiri Slaby 	if (!id_match && !ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
1694c022a0acSJiri Slaby 		return -EPERM;
1695c022a0acSJiri Slaby 
1696c022a0acSJiri Slaby 	return security_task_prlimit(cred, tcred, flags);
1697c022a0acSJiri Slaby }
1698c022a0acSJiri Slaby 
SYSCALL_DEFINE4(prlimit64,pid_t,pid,unsigned int,resource,const struct rlimit64 __user *,new_rlim,struct rlimit64 __user *,old_rlim)1699c022a0acSJiri Slaby SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1700c022a0acSJiri Slaby 		const struct rlimit64 __user *, new_rlim,
1701c022a0acSJiri Slaby 		struct rlimit64 __user *, old_rlim)
1702791ec491SStephen Smalley {
1703c022a0acSJiri Slaby 	struct rlimit64 old64, new64;
1704c022a0acSJiri Slaby 	struct rlimit old, new;
1705791ec491SStephen Smalley 	struct task_struct *tsk;
1706791ec491SStephen Smalley 	unsigned int checkflags = 0;
1707791ec491SStephen Smalley 	int ret;
1708c022a0acSJiri Slaby 
1709c022a0acSJiri Slaby 	if (old_rlim)
1710c022a0acSJiri Slaby 		checkflags |= LSM_PRLIMIT_READ;
1711c022a0acSJiri Slaby 
1712791ec491SStephen Smalley 	if (new_rlim) {
1713c022a0acSJiri Slaby 		if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1714c022a0acSJiri Slaby 			return -EFAULT;
1715c022a0acSJiri Slaby 		rlim64_to_rlim(&new64, &new);
1716c022a0acSJiri Slaby 		checkflags |= LSM_PRLIMIT_WRITE;
1717c022a0acSJiri Slaby 	}
1718c022a0acSJiri Slaby 
1719c022a0acSJiri Slaby 	rcu_read_lock();
1720c022a0acSJiri Slaby 	tsk = pid ? find_task_by_vpid(pid) : current;
1721791ec491SStephen Smalley 	if (!tsk) {
1722c022a0acSJiri Slaby 		rcu_read_unlock();
1723c022a0acSJiri Slaby 		return -ESRCH;
1724c022a0acSJiri Slaby 	}
1725c022a0acSJiri Slaby 	ret = check_prlimit_permission(tsk, checkflags);
1726c022a0acSJiri Slaby 	if (ret) {
1727c022a0acSJiri Slaby 		rcu_read_unlock();
1728c022a0acSJiri Slaby 		return ret;
1729c022a0acSJiri Slaby 	}
1730c022a0acSJiri Slaby 	get_task_struct(tsk);
1731c022a0acSJiri Slaby 	rcu_read_unlock();
1732c022a0acSJiri Slaby 
1733c022a0acSJiri Slaby 	ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1734c022a0acSJiri Slaby 			old_rlim ? &old : NULL);
1735c022a0acSJiri Slaby 
1736c022a0acSJiri Slaby 	if (!ret && old_rlim) {
1737c022a0acSJiri Slaby 		rlim_to_rlim64(&old, &old64);
1738c022a0acSJiri Slaby 		if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1739c022a0acSJiri Slaby 			ret = -EFAULT;
1740c022a0acSJiri Slaby 	}
1741c022a0acSJiri Slaby 
17427855c35dSJiri Slaby 	put_task_struct(tsk);
17437855c35dSJiri Slaby 	return ret;
17447855c35dSJiri Slaby }
17457855c35dSJiri Slaby 
SYSCALL_DEFINE2(setrlimit,unsigned int,resource,struct rlimit __user *,rlim)17467855c35dSJiri Slaby SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
17477855c35dSJiri Slaby {
17485b41535aSJiri Slaby 	struct rlimit new_rlim;
17497855c35dSJiri Slaby 
17507855c35dSJiri Slaby 	if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
17511da177e4SLinus Torvalds 		return -EFAULT;
17521da177e4SLinus Torvalds 	return do_prlimit(current, resource, &new_rlim, NULL);
17531da177e4SLinus Torvalds }
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds /*
17561da177e4SLinus Torvalds  * It would make sense to put struct rusage in the task_struct,
17571da177e4SLinus Torvalds  * except that would make the task_struct be *really big*.  After
17581da177e4SLinus Torvalds  * task_struct gets moved into malloc'ed memory, it would
17591da177e4SLinus Torvalds  * make sense to do this.  It will make moving the rest of the information
17601da177e4SLinus Torvalds  * a lot simpler!  (Which we're not doing right now because we're not
17611da177e4SLinus Torvalds  * measuring them yet).
17621da177e4SLinus Torvalds  *
17631da177e4SLinus Torvalds  * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
17641da177e4SLinus Torvalds  * races with threads incrementing their own counters.  But since word
17651da177e4SLinus Torvalds  * reads are atomic, we either get new values or old values and we don't
17662dd0ebcdSRavikiran G Thirumalai  * care which for the sums.  We always take the siglock to protect reading
1767de047c1bSRavikiran G Thirumalai  * the c* fields from p->signal from races with exit.c updating those
1768de047c1bSRavikiran G Thirumalai  * fields when reaping, so a sample either gets all the additions of a
1769de047c1bSRavikiran G Thirumalai  * given child after it's reaped, or none so this sample is before reaping.
1770de047c1bSRavikiran G Thirumalai  *
1771de047c1bSRavikiran G Thirumalai  * Locking:
1772de047c1bSRavikiran G Thirumalai  * We need to take the siglock for CHILDEREN, SELF and BOTH
1773de047c1bSRavikiran G Thirumalai  * for  the cases current multithreaded, non-current single threaded
1774de047c1bSRavikiran G Thirumalai  * non-current multithreaded.  Thread traversal is now safe with
1775de047c1bSRavikiran G Thirumalai  * the siglock held.
1776de047c1bSRavikiran G Thirumalai  * Strictly speaking, we donot need to take the siglock if we are current and
1777de047c1bSRavikiran G Thirumalai  * single threaded,  as no one else can take our signal_struct away, no one
1778de047c1bSRavikiran G Thirumalai  * else can  reap the  children to update signal->c* counters, and no one else
1779de047c1bSRavikiran G Thirumalai  * can race with the signal-> fields. If we do not take any lock, the
1780de047c1bSRavikiran G Thirumalai  * signal-> fields could be read out of order while another thread was just
17812dd0ebcdSRavikiran G Thirumalai  * exiting. So we should  place a read memory barrier when we avoid the lock.
17821da177e4SLinus Torvalds  * On the writer side,  write memory barrier is implied in  __exit_signal
17831da177e4SLinus Torvalds  * as __exit_signal releases  the siglock spinlock after updating the signal->
1784f06febc9SFrank Mayhar  * fields. But we don't do this yet to keep things simple.
1785679c9cd4SSripathi Kodi  *
1786679c9cd4SSripathi Kodi  */
1787679c9cd4SSripathi Kodi 
accumulate_thread_rusage(struct task_struct * t,struct rusage * r)1788679c9cd4SSripathi Kodi static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
1789679c9cd4SSripathi Kodi {
1790679c9cd4SSripathi Kodi 	r->ru_nvcsw += t->nvcsw;
1791679c9cd4SSripathi Kodi 	r->ru_nivcsw += t->nivcsw;
1792679c9cd4SSripathi Kodi 	r->ru_minflt += t->min_flt;
1793679c9cd4SSripathi Kodi 	r->ru_majflt += t->maj_flt;
1794ce72a16fSAl Viro 	r->ru_inblock += task_io_get_inblock(t);
17951da177e4SLinus Torvalds 	r->ru_oublock += task_io_get_oublock(t);
17961da177e4SLinus Torvalds }
17971da177e4SLinus Torvalds 
getrusage(struct task_struct * p,int who,struct rusage * r)17985613fda9SFrederic Weisbecker void getrusage(struct task_struct *p, int who, struct rusage *r)
1799daa694e4SOleg Nesterov {
1800daa694e4SOleg Nesterov 	struct task_struct *t;
1801c7ac8231SOleg Nesterov 	unsigned long flags;
1802f7ec1cd5SOleg Nesterov 	u64 tgutime, tgstime, utime, stime;
18031da177e4SLinus Torvalds 	unsigned long maxrss;
1804f7ec1cd5SOleg Nesterov 	struct mm_struct *mm;
1805daa694e4SOleg Nesterov 	struct signal_struct *sig = p->signal;
180664861634SMartin Schwidefsky 	unsigned int seq = 0;
1807daa694e4SOleg Nesterov 
18080f59cc4aSOleg Nesterov retry:
1809679c9cd4SSripathi Kodi 	memset(r, 0, sizeof(*r));
1810e80d0a1aSFrederic Weisbecker 	utime = stime = 0;
1811f06febc9SFrank Mayhar 	maxrss = 0;
1812c7ac8231SOleg Nesterov 
1813daa694e4SOleg Nesterov 	if (who == RUSAGE_THREAD) {
1814679c9cd4SSripathi Kodi 		task_cputime_adjusted(current, &utime, &stime);
1815679c9cd4SSripathi Kodi 		accumulate_thread_rusage(p, r);
1816f7ec1cd5SOleg Nesterov 		maxrss = sig->maxrss;
18172dd0ebcdSRavikiran G Thirumalai 		goto out_thread;
18181da177e4SLinus Torvalds 	}
18190f59cc4aSOleg Nesterov 
18201da177e4SLinus Torvalds 	flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
1821c7ac8231SOleg Nesterov 
1822c7ac8231SOleg Nesterov 	switch (who) {
1823c7ac8231SOleg Nesterov 	case RUSAGE_BOTH:
1824c7ac8231SOleg Nesterov 	case RUSAGE_CHILDREN:
1825c7ac8231SOleg Nesterov 		utime = sig->cutime;
1826c7ac8231SOleg Nesterov 		stime = sig->cstime;
1827c7ac8231SOleg Nesterov 		r->ru_nvcsw = sig->cnvcsw;
1828c7ac8231SOleg Nesterov 		r->ru_nivcsw = sig->cnivcsw;
1829c7ac8231SOleg Nesterov 		r->ru_minflt = sig->cmin_flt;
18300f59cc4aSOleg Nesterov 		r->ru_majflt = sig->cmaj_flt;
18310f59cc4aSOleg Nesterov 		r->ru_inblock = sig->cinblock;
18321da177e4SLinus Torvalds 		r->ru_oublock = sig->coublock;
1833df561f66SGustavo A. R. Silva 		maxrss = sig->cmaxrss;
18340f59cc4aSOleg Nesterov 
18351da177e4SLinus Torvalds 		if (who == RUSAGE_CHILDREN)
1836c7ac8231SOleg Nesterov 			break;
1837c7ac8231SOleg Nesterov 		fallthrough;
1838c7ac8231SOleg Nesterov 
1839c7ac8231SOleg Nesterov 	case RUSAGE_SELF:
1840c7ac8231SOleg Nesterov 		r->ru_nvcsw += sig->nvcsw;
1841c7ac8231SOleg Nesterov 		r->ru_nivcsw += sig->nivcsw;
1842c7ac8231SOleg Nesterov 		r->ru_minflt += sig->min_flt;
1843c7ac8231SOleg Nesterov 		r->ru_majflt += sig->maj_flt;
1844f7ec1cd5SOleg Nesterov 		r->ru_inblock += sig->inblock;
1845f7ec1cd5SOleg Nesterov 		r->ru_oublock += sig->oublock;
184613b7bc60SOleg Nesterov 		if (maxrss < sig->maxrss)
1847f06febc9SFrank Mayhar 			maxrss = sig->maxrss;
1848f7ec1cd5SOleg Nesterov 
1849f7ec1cd5SOleg Nesterov 		rcu_read_lock();
18501da177e4SLinus Torvalds 		__for_each_thread(sig, t)
18510f59cc4aSOleg Nesterov 			accumulate_thread_rusage(t, r);
18521da177e4SLinus Torvalds 		rcu_read_unlock();
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds 		break;
1855f7ec1cd5SOleg Nesterov 
1856f7ec1cd5SOleg Nesterov 	default:
1857f7ec1cd5SOleg Nesterov 		BUG();
1858f7ec1cd5SOleg Nesterov 	}
1859f7ec1cd5SOleg Nesterov 
1860f7ec1cd5SOleg Nesterov 	if (need_seqretry(&sig->stats_lock, seq)) {
1861de047c1bSRavikiran G Thirumalai 		seq = 1;
1862daa694e4SOleg Nesterov 		goto retry;
1863daa694e4SOleg Nesterov 	}
18641f10206cSJiri Pirko 	done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
1865daa694e4SOleg Nesterov 
1866daa694e4SOleg Nesterov 	if (who == RUSAGE_CHILDREN)
1867daa694e4SOleg Nesterov 		goto out_children;
1868ec94fc3dSvishnu.ps 
1869daa694e4SOleg Nesterov 	thread_group_cputime_adjusted(p, &tgutime, &tgstime);
1870daa694e4SOleg Nesterov 	utime += tgutime;
18711f10206cSJiri Pirko 	stime += tgstime;
18721f10206cSJiri Pirko 
18731f10206cSJiri Pirko out_thread:
18741f10206cSJiri Pirko 	mm = get_task_mm(p);
1875daa694e4SOleg Nesterov 	if (mm) {
1876daa694e4SOleg Nesterov 		setmax_mm_hiwater_rss(&maxrss, mm);
18771f10206cSJiri Pirko 		mmput(mm);
1878daa694e4SOleg Nesterov 	}
1879daa694e4SOleg Nesterov 
18801da177e4SLinus Torvalds out_children:
18811da177e4SLinus Torvalds 	r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
1882ce72a16fSAl Viro 	r->ru_utime = ns_to_kernel_old_timeval(utime);
18831da177e4SLinus Torvalds 	r->ru_stime = ns_to_kernel_old_timeval(stime);
18841da177e4SLinus Torvalds }
1885ec94fc3dSvishnu.ps 
SYSCALL_DEFINE2(getrusage,int,who,struct rusage __user *,ru)1886679c9cd4SSripathi Kodi SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
1887679c9cd4SSripathi Kodi {
18881da177e4SLinus Torvalds 	struct rusage r;
1889ce72a16fSAl Viro 
1890ce72a16fSAl Viro 	if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1891ce72a16fSAl Viro 	    who != RUSAGE_THREAD)
18921da177e4SLinus Torvalds 		return -EINVAL;
18931da177e4SLinus Torvalds 
18948d2d5c4aSAl Viro 	getrusage(current, who, &r);
18958d2d5c4aSAl Viro 	return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
18968d2d5c4aSAl Viro }
18978d2d5c4aSAl Viro 
18988d2d5c4aSAl Viro #ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE2(getrusage,int,who,struct compat_rusage __user *,ru)18998d2d5c4aSAl Viro COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru)
19008d2d5c4aSAl Viro {
19018d2d5c4aSAl Viro 	struct rusage r;
19028d2d5c4aSAl Viro 
1903ce72a16fSAl Viro 	if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
19048d2d5c4aSAl Viro 	    who != RUSAGE_THREAD)
19058d2d5c4aSAl Viro 		return -EINVAL;
19068d2d5c4aSAl Viro 
19078d2d5c4aSAl Viro 	getrusage(current, who, &r);
1908e48fbb69SHeiko Carstens 	return put_compat_rusage(&r, ru);
19091da177e4SLinus Torvalds }
19101da177e4SLinus Torvalds #endif
19111da177e4SLinus Torvalds 
SYSCALL_DEFINE1(umask,int,mask)19121da177e4SLinus Torvalds SYSCALL_DEFINE1(umask, int, mask)
19131da177e4SLinus Torvalds {
19146e399cd1SDavidlohr Bueso 	mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1915b32dfe37SCyrill Gorcunov 	return mask;
19166348be02SAl Viro }
1917496ad9aaSAl Viro 
prctl_set_mm_exe_file(struct mm_struct * mm,unsigned int fd)19182903ff01SAl Viro static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
1919b32dfe37SCyrill Gorcunov {
19206348be02SAl Viro 	CLASS(fd, exe)(fd);
1921b32dfe37SCyrill Gorcunov 	struct inode *inode;
1922b32dfe37SCyrill Gorcunov 	int err;
19231da91ea8SAl Viro 
1924b32dfe37SCyrill Gorcunov 	if (fd_empty(exe))
1925b32dfe37SCyrill Gorcunov 		return -EBADF;
1926b32dfe37SCyrill Gorcunov 
1927b32dfe37SCyrill Gorcunov 	inode = file_inode(fd_file(exe));
1928b32dfe37SCyrill Gorcunov 
1929b32dfe37SCyrill Gorcunov 	/*
19301da91ea8SAl Viro 	 * Because the original mm->exe_file points to executable file, make
19316348be02SAl Viro 	 * sure that this one is executable as well, to avoid breaking an
1932b32dfe37SCyrill Gorcunov 	 * overall picture.
19331da91ea8SAl Viro 	 */
1934b32dfe37SCyrill Gorcunov 	if (!S_ISREG(inode->i_mode) || path_noexec(&fd_file(exe)->f_path))
1935b32dfe37SCyrill Gorcunov 		return -EACCES;
19366348be02SAl Viro 
19376348be02SAl Viro 	err = file_permission(fd_file(exe), MAY_EXEC);
1938b32dfe37SCyrill Gorcunov 	if (err)
1939b32dfe37SCyrill Gorcunov 		return err;
1940f606b77fSCyrill Gorcunov 
194111bbd8b4SMichal Koutný 	return replace_mm_exe_file(mm, fd_file(exe));
194211bbd8b4SMichal Koutný }
1943f606b77fSCyrill Gorcunov 
1944f606b77fSCyrill Gorcunov /*
1945f606b77fSCyrill Gorcunov  * Check arithmetic relations of passed addresses.
194611bbd8b4SMichal Koutný  *
1947f606b77fSCyrill Gorcunov  * WARNING: we don't require any capability here so be very careful
1948f606b77fSCyrill Gorcunov  * in what is allowed for modification from userspace.
1949f606b77fSCyrill Gorcunov  */
validate_prctl_map_addr(struct prctl_mm_map * prctl_map)1950f606b77fSCyrill Gorcunov static int validate_prctl_map_addr(struct prctl_mm_map *prctl_map)
1951f606b77fSCyrill Gorcunov {
1952f606b77fSCyrill Gorcunov 	unsigned long mmap_max_addr = TASK_SIZE;
1953f606b77fSCyrill Gorcunov 	int error = -EINVAL, i;
1954f606b77fSCyrill Gorcunov 
1955f606b77fSCyrill Gorcunov 	static const unsigned char offsets[] = {
1956f606b77fSCyrill Gorcunov 		offsetof(struct prctl_mm_map, start_code),
1957f606b77fSCyrill Gorcunov 		offsetof(struct prctl_mm_map, end_code),
1958f606b77fSCyrill Gorcunov 		offsetof(struct prctl_mm_map, start_data),
1959f606b77fSCyrill Gorcunov 		offsetof(struct prctl_mm_map, end_data),
1960f606b77fSCyrill Gorcunov 		offsetof(struct prctl_mm_map, start_brk),
1961f606b77fSCyrill Gorcunov 		offsetof(struct prctl_mm_map, brk),
1962f606b77fSCyrill Gorcunov 		offsetof(struct prctl_mm_map, start_stack),
1963f606b77fSCyrill Gorcunov 		offsetof(struct prctl_mm_map, arg_start),
1964f606b77fSCyrill Gorcunov 		offsetof(struct prctl_mm_map, arg_end),
1965f606b77fSCyrill Gorcunov 		offsetof(struct prctl_mm_map, env_start),
1966f606b77fSCyrill Gorcunov 		offsetof(struct prctl_mm_map, env_end),
1967f606b77fSCyrill Gorcunov 	};
1968f606b77fSCyrill Gorcunov 
1969f606b77fSCyrill Gorcunov 	/*
1970f606b77fSCyrill Gorcunov 	 * Make sure the members are not somewhere outside
1971f606b77fSCyrill Gorcunov 	 * of allowed address space.
1972f606b77fSCyrill Gorcunov 	 */
1973f606b77fSCyrill Gorcunov 	for (i = 0; i < ARRAY_SIZE(offsets); i++) {
1974f606b77fSCyrill Gorcunov 		u64 val = *(u64 *)((char *)prctl_map + offsets[i]);
1975f606b77fSCyrill Gorcunov 
1976f606b77fSCyrill Gorcunov 		if ((unsigned long)val >= mmap_max_addr ||
1977f606b77fSCyrill Gorcunov 		    (unsigned long)val < mmap_min_addr)
1978f606b77fSCyrill Gorcunov 			goto out;
1979f606b77fSCyrill Gorcunov 	}
1980f606b77fSCyrill Gorcunov 
1981f606b77fSCyrill Gorcunov 	/*
1982f606b77fSCyrill Gorcunov 	 * Make sure the pairs are ordered.
1983f606b77fSCyrill Gorcunov 	 */
1984a9e73998SCyrill Gorcunov #define __prctl_check_order(__m1, __op, __m2)				\
1985f606b77fSCyrill Gorcunov 	((unsigned long)prctl_map->__m1 __op				\
1986f606b77fSCyrill Gorcunov 	 (unsigned long)prctl_map->__m2) ? 0 : -EINVAL
1987f606b77fSCyrill Gorcunov 	error  = __prctl_check_order(start_code, <, end_code);
1988f606b77fSCyrill Gorcunov 	error |= __prctl_check_order(start_data,<=, end_data);
1989f606b77fSCyrill Gorcunov 	error |= __prctl_check_order(start_brk, <=, brk);
1990f606b77fSCyrill Gorcunov 	error |= __prctl_check_order(arg_start, <=, arg_end);
1991f606b77fSCyrill Gorcunov 	error |= __prctl_check_order(env_start, <=, env_end);
1992f606b77fSCyrill Gorcunov 	if (error)
1993f606b77fSCyrill Gorcunov 		goto out;
1994f606b77fSCyrill Gorcunov #undef __prctl_check_order
1995f606b77fSCyrill Gorcunov 
1996f606b77fSCyrill Gorcunov 	error = -EINVAL;
1997f606b77fSCyrill Gorcunov 
1998f606b77fSCyrill Gorcunov 	/*
1999f606b77fSCyrill Gorcunov 	 * Neither we should allow to override limits if they set.
2000f606b77fSCyrill Gorcunov 	 */
2001f606b77fSCyrill Gorcunov 	if (check_data_rlimit(rlimit(RLIMIT_DATA), prctl_map->brk,
2002f606b77fSCyrill Gorcunov 			      prctl_map->start_brk, prctl_map->end_data,
2003f606b77fSCyrill Gorcunov 			      prctl_map->start_data))
2004f606b77fSCyrill Gorcunov 			goto out;
2005f606b77fSCyrill Gorcunov 
2006f606b77fSCyrill Gorcunov 	error = 0;
20074a00e9dfSAlexey Dobriyan out:
2008f606b77fSCyrill Gorcunov 	return error;
2009f606b77fSCyrill Gorcunov }
2010f606b77fSCyrill Gorcunov 
2011f606b77fSCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
prctl_set_mm_map(int opt,const void __user * addr,unsigned long data_size)2012f606b77fSCyrill Gorcunov static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data_size)
2013f606b77fSCyrill Gorcunov {
2014f606b77fSCyrill Gorcunov 	struct prctl_mm_map prctl_map = { .exe_fd = (u32)-1, };
2015f606b77fSCyrill Gorcunov 	unsigned long user_auxv[AT_VECTOR_SIZE];
2016f606b77fSCyrill Gorcunov 	struct mm_struct *mm = current->mm;
2017f606b77fSCyrill Gorcunov 	int error;
2018f606b77fSCyrill Gorcunov 
2019f606b77fSCyrill Gorcunov 	BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
2020f606b77fSCyrill Gorcunov 	BUILD_BUG_ON(sizeof(struct prctl_mm_map) > 256);
2021f606b77fSCyrill Gorcunov 
2022f606b77fSCyrill Gorcunov 	if (opt == PR_SET_MM_MAP_SIZE)
2023f606b77fSCyrill Gorcunov 		return put_user((unsigned int)sizeof(prctl_map),
2024f606b77fSCyrill Gorcunov 				(unsigned int __user *)addr);
2025f606b77fSCyrill Gorcunov 
2026f606b77fSCyrill Gorcunov 	if (data_size != sizeof(prctl_map))
2027f606b77fSCyrill Gorcunov 		return -EINVAL;
202811bbd8b4SMichal Koutný 
2029f606b77fSCyrill Gorcunov 	if (copy_from_user(&prctl_map, addr, sizeof(prctl_map)))
2030f606b77fSCyrill Gorcunov 		return -EFAULT;
2031f606b77fSCyrill Gorcunov 
2032f606b77fSCyrill Gorcunov 	error = validate_prctl_map_addr(&prctl_map);
203311bbd8b4SMichal Koutný 	if (error)
203411bbd8b4SMichal Koutný 		return error;
203511bbd8b4SMichal Koutný 
203611bbd8b4SMichal Koutný 	if (prctl_map.auxv_size) {
203711bbd8b4SMichal Koutný 		/*
203811bbd8b4SMichal Koutný 		 * Someone is trying to cheat the auxv vector.
203911bbd8b4SMichal Koutný 		 */
2040f606b77fSCyrill Gorcunov 		if (!prctl_map.auxv ||
2041f606b77fSCyrill Gorcunov 				prctl_map.auxv_size > sizeof(mm->saved_auxv))
2042f606b77fSCyrill Gorcunov 			return -EINVAL;
2043f606b77fSCyrill Gorcunov 
2044f606b77fSCyrill Gorcunov 		memset(user_auxv, 0, sizeof(user_auxv));
2045f606b77fSCyrill Gorcunov 		if (copy_from_user(user_auxv,
2046f606b77fSCyrill Gorcunov 				   (const void __user *)prctl_map.auxv,
2047f606b77fSCyrill Gorcunov 				   prctl_map.auxv_size))
2048f606b77fSCyrill Gorcunov 			return -EFAULT;
2049f606b77fSCyrill Gorcunov 
2050f606b77fSCyrill Gorcunov 		/* Last entry must be AT_NULL as specification requires */
2051ddf1d398SMateusz Guzik 		user_auxv[AT_VECTOR_SIZE - 2] = AT_NULL;
205211bbd8b4SMichal Koutný 		user_auxv[AT_VECTOR_SIZE - 1] = AT_NULL;
2053ebd6de68SNicolas Viennot 	}
2054ebd6de68SNicolas Viennot 
2055ebd6de68SNicolas Viennot 	if (prctl_map.exe_fd != (u32)-1) {
2056ebd6de68SNicolas Viennot 		/*
2057ebd6de68SNicolas Viennot 		 * Check if the current user is checkpoint/restore capable.
2058ebd6de68SNicolas Viennot 		 * At the time of this writing, it checks for CAP_SYS_ADMIN
205911bbd8b4SMichal Koutný 		 * or CAP_CHECKPOINT_RESTORE.
2060ebd6de68SNicolas Viennot 		 * Note that a user with access to ptrace can masquerade an
2061227175b2SNicolas Viennot 		 * arbitrary program as any executable, even setuid ones.
206211bbd8b4SMichal Koutný 		 * This may have implications in the tomoyo subsystem.
20636e399cd1SDavidlohr Bueso 		 */
2064f606b77fSCyrill Gorcunov 		if (!checkpoint_restore_ns_capable(current_user_ns()))
2065ddf1d398SMateusz Guzik 			return -EPERM;
2066ddf1d398SMateusz Guzik 
2067ddf1d398SMateusz Guzik 		error = prctl_set_mm_exe_file(mm, prctl_map.exe_fd);
206888aa7cc6SYang Shi 		if (error)
20695afe69c2SXiaofeng Cao 			return error;
207088aa7cc6SYang Shi 	}
207188aa7cc6SYang Shi 
2072d8ed45c5SMichel Lespinasse 	/*
2073f606b77fSCyrill Gorcunov 	 * arg_lock protects concurrent updates but we still need mmap_lock for
2074f606b77fSCyrill Gorcunov 	 * read to exclude races with sys_brk.
2075f606b77fSCyrill Gorcunov 	 */
2076f606b77fSCyrill Gorcunov 	mmap_read_lock(mm);
2077f606b77fSCyrill Gorcunov 
2078f606b77fSCyrill Gorcunov 	/*
2079f606b77fSCyrill Gorcunov 	 * We don't validate if these members are pointing to
208015ec0fcfSLiao Pingfang 	 * real present VMAs because application may have correspond
20815afe69c2SXiaofeng Cao 	 * VMAs already unmapped and kernel uses these members for statistics
2082f606b77fSCyrill Gorcunov 	 * output in procfs mostly, except
2083f606b77fSCyrill Gorcunov 	 *
2084f606b77fSCyrill Gorcunov 	 *  - @start_brk/@brk which are used in do_brk_flags but kernel lookups
2085f606b77fSCyrill Gorcunov 	 *    for VMAs when updating these members so anything wrong written
208688aa7cc6SYang Shi 	 *    here cause kernel to swear at userspace program but won't lead
2087f606b77fSCyrill Gorcunov 	 *    to any problem in kernel itself
2088f606b77fSCyrill Gorcunov 	 */
2089f606b77fSCyrill Gorcunov 
2090f606b77fSCyrill Gorcunov 	spin_lock(&mm->arg_lock);
2091f606b77fSCyrill Gorcunov 	mm->start_code	= prctl_map.start_code;
2092f606b77fSCyrill Gorcunov 	mm->end_code	= prctl_map.end_code;
2093f606b77fSCyrill Gorcunov 	mm->start_data	= prctl_map.start_data;
2094f606b77fSCyrill Gorcunov 	mm->end_data	= prctl_map.end_data;
2095f606b77fSCyrill Gorcunov 	mm->start_brk	= prctl_map.start_brk;
2096f606b77fSCyrill Gorcunov 	mm->brk		= prctl_map.brk;
2097f606b77fSCyrill Gorcunov 	mm->start_stack	= prctl_map.start_stack;
209888aa7cc6SYang Shi 	mm->arg_start	= prctl_map.arg_start;
2099f606b77fSCyrill Gorcunov 	mm->arg_end	= prctl_map.arg_end;
2100f606b77fSCyrill Gorcunov 	mm->env_start	= prctl_map.env_start;
2101f606b77fSCyrill Gorcunov 	mm->env_end	= prctl_map.env_end;
2102f606b77fSCyrill Gorcunov 	spin_unlock(&mm->arg_lock);
2103f606b77fSCyrill Gorcunov 
2104f606b77fSCyrill Gorcunov 	/*
2105f606b77fSCyrill Gorcunov 	 * Note this update of @saved_auxv is lockless thus
2106f606b77fSCyrill Gorcunov 	 * if someone reads this member in procfs while we're
2107f606b77fSCyrill Gorcunov 	 * updating -- it may get partly updated results. It's
2108f606b77fSCyrill Gorcunov 	 * known and acceptable trade off: we leave it as is to
2109f606b77fSCyrill Gorcunov 	 * not introduce additional locks here making the kernel
2110f606b77fSCyrill Gorcunov 	 * more complex.
2111d8ed45c5SMichel Lespinasse 	 */
2112ddf1d398SMateusz Guzik 	if (prctl_map.auxv_size)
2113f606b77fSCyrill Gorcunov 		memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
2114f606b77fSCyrill Gorcunov 
2115f606b77fSCyrill Gorcunov 	mmap_read_unlock(mm);
21164a00e9dfSAlexey Dobriyan 	return 0;
21174a00e9dfSAlexey Dobriyan }
21184a00e9dfSAlexey Dobriyan #endif /* CONFIG_CHECKPOINT_RESTORE */
21194a00e9dfSAlexey Dobriyan 
prctl_set_auxv(struct mm_struct * mm,unsigned long addr,unsigned long len)21204a00e9dfSAlexey Dobriyan static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr,
21214a00e9dfSAlexey Dobriyan 			  unsigned long len)
21224a00e9dfSAlexey Dobriyan {
21234a00e9dfSAlexey Dobriyan 	/*
21244a00e9dfSAlexey Dobriyan 	 * This doesn't move the auxiliary vector itself since it's pinned to
2125c995f12aSAlexey Dobriyan 	 * mm_struct, but it permits filling the vector with new values.  It's
21264a00e9dfSAlexey Dobriyan 	 * up to the caller to provide sane values here, otherwise userspace
21274a00e9dfSAlexey Dobriyan 	 * tools which use this vector might be unhappy.
21284a00e9dfSAlexey Dobriyan 	 */
21294a00e9dfSAlexey Dobriyan 	unsigned long user_auxv[AT_VECTOR_SIZE] = {};
21304a00e9dfSAlexey Dobriyan 
21314a00e9dfSAlexey Dobriyan 	if (len > sizeof(user_auxv))
21324a00e9dfSAlexey Dobriyan 		return -EINVAL;
21334a00e9dfSAlexey Dobriyan 
21344a00e9dfSAlexey Dobriyan 	if (copy_from_user(user_auxv, (const void __user *)addr, len))
21354a00e9dfSAlexey Dobriyan 		return -EFAULT;
21364a00e9dfSAlexey Dobriyan 
21374a00e9dfSAlexey Dobriyan 	/* Make sure the last entry is always AT_NULL */
21384a00e9dfSAlexey Dobriyan 	user_auxv[AT_VECTOR_SIZE - 2] = 0;
21394a00e9dfSAlexey Dobriyan 	user_auxv[AT_VECTOR_SIZE - 1] = 0;
21404a00e9dfSAlexey Dobriyan 
21414a00e9dfSAlexey Dobriyan 	BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
21424a00e9dfSAlexey Dobriyan 
21434a00e9dfSAlexey Dobriyan 	task_lock(current);
21444a00e9dfSAlexey Dobriyan 	memcpy(mm->saved_auxv, user_auxv, len);
21454a00e9dfSAlexey Dobriyan 	task_unlock(current);
2146028ee4beSCyrill Gorcunov 
2147028ee4beSCyrill Gorcunov 	return 0;
2148028ee4beSCyrill Gorcunov }
2149028ee4beSCyrill Gorcunov 
prctl_set_mm(int opt,unsigned long addr,unsigned long arg4,unsigned long arg5)215011bbd8b4SMichal Koutný static int prctl_set_mm(int opt, unsigned long addr,
215111bbd8b4SMichal Koutný 			unsigned long arg4, unsigned long arg5)
215211bbd8b4SMichal Koutný {
215311bbd8b4SMichal Koutný 	struct mm_struct *mm = current->mm;
215411bbd8b4SMichal Koutný 	struct prctl_mm_map prctl_map = {
2155fe8c7f5cSCyrill Gorcunov 		.auxv = NULL,
2156fe8c7f5cSCyrill Gorcunov 		.auxv_size = 0,
2157028ee4beSCyrill Gorcunov 		.exe_fd = -1,
2158f606b77fSCyrill Gorcunov 	};
2159f606b77fSCyrill Gorcunov 	struct vm_area_struct *vma;
2160f606b77fSCyrill Gorcunov 	int error;
2161028ee4beSCyrill Gorcunov 
2162028ee4beSCyrill Gorcunov 	if (arg5 || (arg4 && (opt != PR_SET_MM_AUXV &&
2163f606b77fSCyrill Gorcunov 			      opt != PR_SET_MM_MAP &&
2164f606b77fSCyrill Gorcunov 			      opt != PR_SET_MM_MAP_SIZE)))
2165f606b77fSCyrill Gorcunov 		return -EINVAL;
2166f606b77fSCyrill Gorcunov 
2167f606b77fSCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
216879f0713dSCyrill Gorcunov 	if (opt == PR_SET_MM_MAP || opt == PR_SET_MM_MAP_SIZE)
2169028ee4beSCyrill Gorcunov 		return prctl_set_mm_map(opt, (const void __user *)addr, arg4);
2170028ee4beSCyrill Gorcunov #endif
21716e399cd1SDavidlohr Bueso 
21726e399cd1SDavidlohr Bueso 	if (!capable(CAP_SYS_RESOURCE))
2173b32dfe37SCyrill Gorcunov 		return -EPERM;
21744a00e9dfSAlexey Dobriyan 
21754a00e9dfSAlexey Dobriyan 	if (opt == PR_SET_MM_EXE_FILE)
21764a00e9dfSAlexey Dobriyan 		return prctl_set_mm_exe_file(mm, (unsigned int)addr);
21771ad75b9eSCyrill Gorcunov 
2178028ee4beSCyrill Gorcunov 	if (opt == PR_SET_MM_AUXV)
2179028ee4beSCyrill Gorcunov 		return prctl_set_auxv(mm, addr, arg4);
2180fe8c7f5cSCyrill Gorcunov 
2181fe8c7f5cSCyrill Gorcunov 	if (addr >= TASK_SIZE || addr < mmap_min_addr)
2182bc81426fSMichal Koutný 		return -EINVAL;
21835afe69c2SXiaofeng Cao 
2184c1e8d7c6SMichel Lespinasse 	error = -EINVAL;
2185bc81426fSMichal Koutný 
2186bc81426fSMichal Koutný 	/*
2187d8ed45c5SMichel Lespinasse 	 * arg_lock protects concurrent updates of arg boundaries, we need
2188028ee4beSCyrill Gorcunov 	 * mmap_lock for a) concurrent sys_brk, b) finding VMA for addr
2189028ee4beSCyrill Gorcunov 	 * validation.
2190bc81426fSMichal Koutný 	 */
21914a00e9dfSAlexey Dobriyan 	mmap_read_lock(mm);
21924a00e9dfSAlexey Dobriyan 	vma = find_vma(mm, addr);
21934a00e9dfSAlexey Dobriyan 
21944a00e9dfSAlexey Dobriyan 	spin_lock(&mm->arg_lock);
21954a00e9dfSAlexey Dobriyan 	prctl_map.start_code	= mm->start_code;
21964a00e9dfSAlexey Dobriyan 	prctl_map.end_code	= mm->end_code;
21974a00e9dfSAlexey Dobriyan 	prctl_map.start_data	= mm->start_data;
21984a00e9dfSAlexey Dobriyan 	prctl_map.end_data	= mm->end_data;
21994a00e9dfSAlexey Dobriyan 	prctl_map.start_brk	= mm->start_brk;
22004a00e9dfSAlexey Dobriyan 	prctl_map.brk		= mm->brk;
22014a00e9dfSAlexey Dobriyan 	prctl_map.start_stack	= mm->start_stack;
22024a00e9dfSAlexey Dobriyan 	prctl_map.arg_start	= mm->arg_start;
2203028ee4beSCyrill Gorcunov 	prctl_map.arg_end	= mm->arg_end;
2204028ee4beSCyrill Gorcunov 	prctl_map.env_start	= mm->env_start;
22054a00e9dfSAlexey Dobriyan 	prctl_map.env_end	= mm->env_end;
2206fe8c7f5cSCyrill Gorcunov 
2207fe8c7f5cSCyrill Gorcunov 	switch (opt) {
22084a00e9dfSAlexey Dobriyan 	case PR_SET_MM_START_CODE:
2209028ee4beSCyrill Gorcunov 		prctl_map.start_code = addr;
2210028ee4beSCyrill Gorcunov 		break;
22114a00e9dfSAlexey Dobriyan 	case PR_SET_MM_END_CODE:
2212028ee4beSCyrill Gorcunov 		prctl_map.end_code = addr;
2213fe8c7f5cSCyrill Gorcunov 		break;
22144a00e9dfSAlexey Dobriyan 	case PR_SET_MM_START_DATA:
2215028ee4beSCyrill Gorcunov 		prctl_map.start_data = addr;
22164a00e9dfSAlexey Dobriyan 		break;
22174a00e9dfSAlexey Dobriyan 	case PR_SET_MM_END_DATA:
22184a00e9dfSAlexey Dobriyan 		prctl_map.end_data = addr;
2219028ee4beSCyrill Gorcunov 		break;
22204a00e9dfSAlexey Dobriyan 	case PR_SET_MM_START_STACK:
2221028ee4beSCyrill Gorcunov 		prctl_map.start_stack = addr;
2222028ee4beSCyrill Gorcunov 		break;
22234a00e9dfSAlexey Dobriyan 	case PR_SET_MM_START_BRK:
2224028ee4beSCyrill Gorcunov 		prctl_map.start_brk = addr;
22254a00e9dfSAlexey Dobriyan 		break;
22264a00e9dfSAlexey Dobriyan 	case PR_SET_MM_BRK:
22274a00e9dfSAlexey Dobriyan 		prctl_map.brk = addr;
22284a00e9dfSAlexey Dobriyan 		break;
22294a00e9dfSAlexey Dobriyan 	case PR_SET_MM_ARG_START:
22304a00e9dfSAlexey Dobriyan 		prctl_map.arg_start = addr;
22314a00e9dfSAlexey Dobriyan 		break;
22324a00e9dfSAlexey Dobriyan 	case PR_SET_MM_ARG_END:
22334a00e9dfSAlexey Dobriyan 		prctl_map.arg_end = addr;
22344a00e9dfSAlexey Dobriyan 		break;
22354a00e9dfSAlexey Dobriyan 	case PR_SET_MM_ENV_START:
22364a00e9dfSAlexey Dobriyan 		prctl_map.env_start = addr;
22374a00e9dfSAlexey Dobriyan 		break;
22384a00e9dfSAlexey Dobriyan 	case PR_SET_MM_ENV_END:
22394a00e9dfSAlexey Dobriyan 		prctl_map.env_end = addr;
2240028ee4beSCyrill Gorcunov 		break;
224111bbd8b4SMichal Koutný 	default:
22424a00e9dfSAlexey Dobriyan 		goto out;
22434a00e9dfSAlexey Dobriyan 	}
22444a00e9dfSAlexey Dobriyan 
22454a00e9dfSAlexey Dobriyan 	error = validate_prctl_map_addr(&prctl_map);
2246fe8c7f5cSCyrill Gorcunov 	if (error)
2247fe8c7f5cSCyrill Gorcunov 		goto out;
2248fe8c7f5cSCyrill Gorcunov 
2249fe8c7f5cSCyrill Gorcunov 	switch (opt) {
22505afe69c2SXiaofeng Cao 	/*
2251fe8c7f5cSCyrill Gorcunov 	 * If command line arguments and environment
2252fe8c7f5cSCyrill Gorcunov 	 * are placed somewhere else on stack, we can
2253fe8c7f5cSCyrill Gorcunov 	 * set them up here, ARG_START/END to setup
2254fe8c7f5cSCyrill Gorcunov 	 * command line arguments and ENV_START/END
2255fe8c7f5cSCyrill Gorcunov 	 * for environment.
2256fe8c7f5cSCyrill Gorcunov 	 */
2257fe8c7f5cSCyrill Gorcunov 	case PR_SET_MM_START_STACK:
2258fe8c7f5cSCyrill Gorcunov 	case PR_SET_MM_ARG_START:
2259fe8c7f5cSCyrill Gorcunov 	case PR_SET_MM_ARG_END:
2260fe8c7f5cSCyrill Gorcunov 	case PR_SET_MM_ENV_START:
2261fe8c7f5cSCyrill Gorcunov 	case PR_SET_MM_ENV_END:
2262fe8c7f5cSCyrill Gorcunov 		if (!vma) {
22634a00e9dfSAlexey Dobriyan 			error = -EFAULT;
22644a00e9dfSAlexey Dobriyan 			goto out;
22654a00e9dfSAlexey Dobriyan 		}
22664a00e9dfSAlexey Dobriyan 	}
22674a00e9dfSAlexey Dobriyan 
22684a00e9dfSAlexey Dobriyan 	mm->start_code	= prctl_map.start_code;
22694a00e9dfSAlexey Dobriyan 	mm->end_code	= prctl_map.end_code;
22704a00e9dfSAlexey Dobriyan 	mm->start_data	= prctl_map.start_data;
22714a00e9dfSAlexey Dobriyan 	mm->end_data	= prctl_map.end_data;
22724a00e9dfSAlexey Dobriyan 	mm->start_brk	= prctl_map.start_brk;
22734a00e9dfSAlexey Dobriyan 	mm->brk		= prctl_map.brk;
22744a00e9dfSAlexey Dobriyan 	mm->start_stack	= prctl_map.start_stack;
2275028ee4beSCyrill Gorcunov 	mm->arg_start	= prctl_map.arg_start;
2276028ee4beSCyrill Gorcunov 	mm->arg_end	= prctl_map.arg_end;
2277028ee4beSCyrill Gorcunov 	mm->env_start	= prctl_map.env_start;
2278bc81426fSMichal Koutný 	mm->env_end	= prctl_map.env_end;
2279d8ed45c5SMichel Lespinasse 
2280028ee4beSCyrill Gorcunov 	error = 0;
2281028ee4beSCyrill Gorcunov out:
2282300f786bSCyrill Gorcunov 	spin_unlock(&mm->arg_lock);
228352b36941SAmnon Shiloh 	mmap_read_unlock(mm);
2284986b9eacSRasmus Villemoes 	return error;
2285300f786bSCyrill Gorcunov }
2286300f786bSCyrill Gorcunov 
2287300f786bSCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
prctl_get_tid_address(struct task_struct * me,int __user * __user * tid_addr)228852b36941SAmnon Shiloh static int prctl_get_tid_address(struct task_struct *me, int __user * __user *tid_addr)
2289986b9eacSRasmus Villemoes {
2290300f786bSCyrill Gorcunov 	return put_user(me->clear_child_tid, tid_addr);
2291300f786bSCyrill Gorcunov }
2292300f786bSCyrill Gorcunov #else
prctl_get_tid_address(struct task_struct * me,int __user * __user * tid_addr)2293028ee4beSCyrill Gorcunov static int prctl_get_tid_address(struct task_struct *me, int __user * __user *tid_addr)
2294028ee4beSCyrill Gorcunov {
2295749860ceSPavel Tikhomirov 	return -EINVAL;
2296749860ceSPavel Tikhomirov }
2297749860ceSPavel Tikhomirov #endif
22985afe69c2SXiaofeng Cao 
propagate_has_child_subreaper(struct task_struct * p,void * data)22995afe69c2SXiaofeng Cao static int propagate_has_child_subreaper(struct task_struct *p, void *data)
2300749860ceSPavel Tikhomirov {
2301749860ceSPavel Tikhomirov 	/*
2302749860ceSPavel Tikhomirov 	 * If task has has_child_subreaper - all its descendants
2303749860ceSPavel Tikhomirov 	 * already have these flag too and new descendants will
2304749860ceSPavel Tikhomirov 	 * inherit it on fork, skip them.
2305749860ceSPavel Tikhomirov 	 *
2306749860ceSPavel Tikhomirov 	 * If we've found child_reaper - skip descendants in
2307749860ceSPavel Tikhomirov 	 * it's subtree as they will never get out pidns.
2308749860ceSPavel Tikhomirov 	 */
2309749860ceSPavel Tikhomirov 	if (p->signal->has_child_subreaper ||
2310749860ceSPavel Tikhomirov 	    is_child_reaper(task_pid(p)))
2311749860ceSPavel Tikhomirov 		return 0;
2312749860ceSPavel Tikhomirov 
23137bbf1373SKees Cook 	p->signal->has_child_subreaper = 1;
2314b617cfc8SThomas Gleixner 	return 1;
2315b617cfc8SThomas Gleixner }
2316b617cfc8SThomas Gleixner 
arch_prctl_spec_ctrl_get(struct task_struct * t,unsigned long which)2317b617cfc8SThomas Gleixner int __weak arch_prctl_spec_ctrl_get(struct task_struct *t, unsigned long which)
23187bbf1373SKees Cook {
23197bbf1373SKees Cook 	return -EINVAL;
2320b617cfc8SThomas Gleixner }
2321b617cfc8SThomas Gleixner 
arch_prctl_spec_ctrl_set(struct task_struct * t,unsigned long which,unsigned long ctrl)2322b617cfc8SThomas Gleixner int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which,
2323b617cfc8SThomas Gleixner 				    unsigned long ctrl)
232491e102e7SMark Brown {
232591e102e7SMark Brown 	return -EINVAL;
232691e102e7SMark Brown }
232791e102e7SMark Brown 
arch_get_shadow_stack_status(struct task_struct * t,unsigned long __user * status)232891e102e7SMark Brown int __weak arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status)
232991e102e7SMark Brown {
233091e102e7SMark Brown 	return -EINVAL;
233191e102e7SMark Brown }
233291e102e7SMark Brown 
arch_set_shadow_stack_status(struct task_struct * t,unsigned long status)233391e102e7SMark Brown int __weak arch_set_shadow_stack_status(struct task_struct *t, unsigned long status)
233491e102e7SMark Brown {
233591e102e7SMark Brown 	return -EINVAL;
233691e102e7SMark Brown }
233791e102e7SMark Brown 
arch_lock_shadow_stack_status(struct task_struct * t,unsigned long status)233891e102e7SMark Brown int __weak arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status)
2339a37b0715SNeilBrown {
23408d19f1c8SMike Christie 	return -EINVAL;
23419a10064fSColin Cross }
23429a10064fSColin Cross 
23439a10064fSColin Cross #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE)
23449a10064fSColin Cross 
23459a10064fSColin Cross #ifdef CONFIG_ANON_VMA_NAME
23469a10064fSColin Cross 
23479a10064fSColin Cross #define ANON_VMA_NAME_MAX_LEN		80
23489a10064fSColin Cross #define ANON_VMA_NAME_INVALID_CHARS	"\\`$[]"
23499a10064fSColin Cross 
is_valid_name_char(char ch)23509a10064fSColin Cross static inline bool is_valid_name_char(char ch)
23519a10064fSColin Cross {
23529a10064fSColin Cross 	/* printable ascii characters, excluding ANON_VMA_NAME_INVALID_CHARS */
23539a10064fSColin Cross 	return ch > 0x1f && ch < 0x7f &&
23549a10064fSColin Cross 		!strchr(ANON_VMA_NAME_INVALID_CHARS, ch);
23559a10064fSColin Cross }
23569a10064fSColin Cross 
prctl_set_vma(unsigned long opt,unsigned long addr,unsigned long size,unsigned long arg)23579a10064fSColin Cross static int prctl_set_vma(unsigned long opt, unsigned long addr,
23585c26f6acSSuren Baghdasaryan 			 unsigned long size, unsigned long arg)
23599a10064fSColin Cross {
23609a10064fSColin Cross 	struct mm_struct *mm = current->mm;
23619a10064fSColin Cross 	const char __user *uname;
23629a10064fSColin Cross 	struct anon_vma_name *anon_name = NULL;
23639a10064fSColin Cross 	int error;
23649a10064fSColin Cross 
23655c26f6acSSuren Baghdasaryan 	switch (opt) {
23669a10064fSColin Cross 	case PR_SET_VMA_ANON_NAME:
23675c26f6acSSuren Baghdasaryan 		uname = (const char __user *)arg;
23689a10064fSColin Cross 		if (uname) {
23699a10064fSColin Cross 			char *name, *pch;
23709a10064fSColin Cross 
23719a10064fSColin Cross 			name = strndup_user(uname, ANON_VMA_NAME_MAX_LEN);
23729a10064fSColin Cross 			if (IS_ERR(name))
23739a10064fSColin Cross 				return PTR_ERR(name);
23749a10064fSColin Cross 
23759a10064fSColin Cross 			for (pch = name; *pch != '\0'; pch++) {
23769a10064fSColin Cross 				if (!is_valid_name_char(*pch)) {
23775c26f6acSSuren Baghdasaryan 					kfree(name);
23785c26f6acSSuren Baghdasaryan 					return -EINVAL;
23795c26f6acSSuren Baghdasaryan 				}
23805c26f6acSSuren Baghdasaryan 			}
23815c26f6acSSuren Baghdasaryan 			/* anon_vma has its own copy */
23825c26f6acSSuren Baghdasaryan 			anon_name = anon_vma_name_alloc(name);
23839a10064fSColin Cross 			kfree(name);
23849a10064fSColin Cross 			if (!anon_name)
23859a10064fSColin Cross 				return -ENOMEM;
23865c26f6acSSuren Baghdasaryan 
23879a10064fSColin Cross 		}
23885c26f6acSSuren Baghdasaryan 
23899a10064fSColin Cross 		mmap_write_lock(mm);
23909a10064fSColin Cross 		error = madvise_set_anon_name(mm, addr, size, anon_name);
23919a10064fSColin Cross 		mmap_write_unlock(mm);
23929a10064fSColin Cross 		anon_vma_name_put(anon_name);
23939a10064fSColin Cross 		break;
23949a10064fSColin Cross 	default:
23959a10064fSColin Cross 		error = -EINVAL;
23969a10064fSColin Cross 	}
23979a10064fSColin Cross 
23989a10064fSColin Cross 	return error;
23999a10064fSColin Cross }
24009a10064fSColin Cross 
24019a10064fSColin Cross #else /* CONFIG_ANON_VMA_NAME */
prctl_set_vma(unsigned long opt,unsigned long start,unsigned long size,unsigned long arg)24029a10064fSColin Cross static int prctl_set_vma(unsigned long opt, unsigned long start,
24039a10064fSColin Cross 			 unsigned long size, unsigned long arg)
24049a10064fSColin Cross {
240524e41bf8SFlorent Revest 	return -EINVAL;
240624e41bf8SFlorent Revest }
240724e41bf8SFlorent Revest #endif /* CONFIG_ANON_VMA_NAME */
240824e41bf8SFlorent Revest 
get_current_mdwe(void)240924e41bf8SFlorent Revest static inline unsigned long get_current_mdwe(void)
241024e41bf8SFlorent Revest {
241124e41bf8SFlorent Revest 	unsigned long ret = 0;
241224e41bf8SFlorent Revest 
241324e41bf8SFlorent Revest 	if (test_bit(MMF_HAS_MDWE, &current->mm->flags))
241424e41bf8SFlorent Revest 		ret |= PR_MDWE_REFUSE_EXEC_GAIN;
241524e41bf8SFlorent Revest 	if (test_bit(MMF_HAS_MDWE_NO_INHERIT, &current->mm->flags))
241624e41bf8SFlorent Revest 		ret |= PR_MDWE_NO_INHERIT;
2417b507808eSJoey Gouly 
2418b507808eSJoey Gouly 	return ret;
2419b507808eSJoey Gouly }
242024e41bf8SFlorent Revest 
prctl_set_mdwe(unsigned long bits,unsigned long arg3,unsigned long arg4,unsigned long arg5)242124e41bf8SFlorent Revest static inline int prctl_set_mdwe(unsigned long bits, unsigned long arg3,
2422b507808eSJoey Gouly 				 unsigned long arg4, unsigned long arg5)
2423b507808eSJoey Gouly {
2424b507808eSJoey Gouly 	unsigned long current_bits;
242524e41bf8SFlorent Revest 
2426b507808eSJoey Gouly 	if (arg3 || arg4 || arg5)
2427b507808eSJoey Gouly 		return -EINVAL;
242824e41bf8SFlorent Revest 
242924e41bf8SFlorent Revest 	if (bits & ~(PR_MDWE_REFUSE_EXEC_GAIN | PR_MDWE_NO_INHERIT))
243024e41bf8SFlorent Revest 		return -EINVAL;
243124e41bf8SFlorent Revest 
2432d5aad4c2SZev Weiss 	/* NO_INHERIT only makes sense with REFUSE_EXEC_GAIN */
2433d5aad4c2SZev Weiss 	if (bits & PR_MDWE_NO_INHERIT && !(bits & PR_MDWE_REFUSE_EXEC_GAIN))
2434d5aad4c2SZev Weiss 		return -EINVAL;
2435d5aad4c2SZev Weiss 
2436d5aad4c2SZev Weiss 	/*
243779383813SHelge Deller 	 * EOPNOTSUPP might be more appropriate here in principle, but
243879383813SHelge Deller 	 * existing userspace depends on EINVAL specifically.
243924e41bf8SFlorent Revest 	 */
244024e41bf8SFlorent Revest 	if (!arch_memory_deny_write_exec_supported())
244124e41bf8SFlorent Revest 		return -EINVAL;
244224e41bf8SFlorent Revest 
244324e41bf8SFlorent Revest 	current_bits = get_current_mdwe();
244424e41bf8SFlorent Revest 	if (current_bits && current_bits != bits)
2445b507808eSJoey Gouly 		return -EPERM; /* Cannot unset the flags */
2446b507808eSJoey Gouly 
2447b507808eSJoey Gouly 	if (bits & PR_MDWE_NO_INHERIT)
2448b507808eSJoey Gouly 		set_bit(MMF_HAS_MDWE_NO_INHERIT, &current->mm->flags);
2449b507808eSJoey Gouly 	if (bits & PR_MDWE_REFUSE_EXEC_GAIN)
2450b507808eSJoey Gouly 		set_bit(MMF_HAS_MDWE, &current->mm->flags);
2451b507808eSJoey Gouly 
2452b507808eSJoey Gouly 	return 0;
2453b507808eSJoey Gouly }
2454b507808eSJoey Gouly 
prctl_get_mdwe(unsigned long arg2,unsigned long arg3,unsigned long arg4,unsigned long arg5)2455b507808eSJoey Gouly static inline int prctl_get_mdwe(unsigned long arg2, unsigned long arg3,
245624e41bf8SFlorent Revest 				 unsigned long arg4, unsigned long arg5)
2457b507808eSJoey Gouly {
2458b507808eSJoey Gouly 	if (arg2 || arg3 || arg4 || arg5)
2459ddc65971SJosh Triplett 		return -EINVAL;
2460ddc65971SJosh Triplett 	return get_current_mdwe();
2461ddc65971SJosh Triplett }
2462ddc65971SJosh Triplett 
prctl_get_auxv(void __user * addr,unsigned long len)2463ddc65971SJosh Triplett static int prctl_get_auxv(void __user *addr, unsigned long len)
2464ddc65971SJosh Triplett {
2465ddc65971SJosh Triplett 	struct mm_struct *mm = current->mm;
2466ddc65971SJosh Triplett 	unsigned long size = min_t(unsigned long, sizeof(mm->saved_auxv), len);
2467ddc65971SJosh Triplett 
2468ddc65971SJosh Triplett 	if (size && copy_to_user(addr, mm->saved_auxv, size))
2469c4ea37c2SHeiko Carstens 		return -EFAULT;
2470c4ea37c2SHeiko Carstens 	return sizeof(mm->saved_auxv);
24711da177e4SLinus Torvalds }
2472b6dff3ecSDavid Howells 
SYSCALL_DEFINE5(prctl,int,option,unsigned long,arg2,unsigned long,arg3,unsigned long,arg4,unsigned long,arg5)2473b6dff3ecSDavid Howells SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
2474b6dff3ecSDavid Howells 		unsigned long, arg4, unsigned long, arg5)
24751da177e4SLinus Torvalds {
2476d84f4f99SDavid Howells 	struct task_struct *me = current;
2477d84f4f99SDavid Howells 	unsigned char comm[sizeof(me->comm)];
24781da177e4SLinus Torvalds 	long error;
24791da177e4SLinus Torvalds 
2480d84f4f99SDavid Howells 	error = security_task_prctl(option, arg2, arg3, arg4, arg5);
24811da177e4SLinus Torvalds 	if (error != -ENOSYS)
24821da177e4SLinus Torvalds 		return error;
24830730ded5SJesper Juhl 
24841da177e4SLinus Torvalds 	error = 0;
24851da177e4SLinus Torvalds 	switch (option) {
24861da177e4SLinus Torvalds 	case PR_SET_PDEATHSIG:
2487b6dff3ecSDavid Howells 		if (!valid_signal(arg2)) {
24881da177e4SLinus Torvalds 			error = -EINVAL;
24891da177e4SLinus Torvalds 			break;
2490b6dff3ecSDavid Howells 		}
24911da177e4SLinus Torvalds 		me->pdeath_signal = arg2;
24921da177e4SLinus Torvalds 		break;
2493b6dff3ecSDavid Howells 	case PR_GET_PDEATHSIG:
24941da177e4SLinus Torvalds 		error = put_user(me->pdeath_signal, (int __user *)arg2);
24951da177e4SLinus Torvalds 		break;
2496f3cbd435SAndrew Morton 	case PR_GET_DUMPABLE:
24971da177e4SLinus Torvalds 		error = get_dumpable(me->mm);
24981da177e4SLinus Torvalds 		break;
24991da177e4SLinus Torvalds 	case PR_SET_DUMPABLE:
2500b6dff3ecSDavid Howells 		if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) {
25011da177e4SLinus Torvalds 			error = -EINVAL;
25021da177e4SLinus Torvalds 			break;
25031da177e4SLinus Torvalds 		}
2504b6dff3ecSDavid Howells 		set_dumpable(me->mm, arg2);
25051da177e4SLinus Torvalds 		break;
25061da177e4SLinus Torvalds 
2507b6dff3ecSDavid Howells 	case PR_SET_UNALIGN:
25081da177e4SLinus Torvalds 		error = SET_UNALIGN_CTL(me, arg2);
25091da177e4SLinus Torvalds 		break;
2510b6dff3ecSDavid Howells 	case PR_GET_UNALIGN:
25111da177e4SLinus Torvalds 		error = GET_UNALIGN_CTL(me, arg2);
25121da177e4SLinus Torvalds 		break;
2513b6dff3ecSDavid Howells 	case PR_SET_FPEMU:
25141da177e4SLinus Torvalds 		error = SET_FPEMU_CTL(me, arg2);
25151da177e4SLinus Torvalds 		break;
2516b6dff3ecSDavid Howells 	case PR_GET_FPEMU:
25171da177e4SLinus Torvalds 		error = GET_FPEMU_CTL(me, arg2);
25181da177e4SLinus Torvalds 		break;
2519b6dff3ecSDavid Howells 	case PR_SET_FPEXC:
25201da177e4SLinus Torvalds 		error = SET_FPEXC_CTL(me, arg2);
25211da177e4SLinus Torvalds 		break;
25221da177e4SLinus Torvalds 	case PR_GET_FPEXC:
25231da177e4SLinus Torvalds 		error = GET_FPEXC_CTL(me, arg2);
25241da177e4SLinus Torvalds 		break;
25257b26655fSShi Weihua 	case PR_GET_TIMING:
25261da177e4SLinus Torvalds 		error = PR_TIMING_STATISTICAL;
25271da177e4SLinus Torvalds 		break;
2528b6dff3ecSDavid Howells 	case PR_SET_TIMING:
2529b6dff3ecSDavid Howells 		if (arg2 != PR_TIMING_STATISTICAL)
2530b6dff3ecSDavid Howells 			error = -EINVAL;
25311da177e4SLinus Torvalds 		break;
25321da177e4SLinus Torvalds 	case PR_SET_NAME:
2533b6dff3ecSDavid Howells 		comm[sizeof(me->comm) - 1] = 0;
2534f786ecbaSVladimir Zapolskiy 		if (strncpy_from_user(comm, (char __user *)arg2,
2535f1fd75bfSSasikantha babu 				      sizeof(me->comm) - 1) < 0)
2536b6dff3ecSDavid Howells 			return -EFAULT;
2537b6dff3ecSDavid Howells 		set_task_comm(me, comm);
2538f3cbd435SAndrew Morton 		proc_comm_connector(me);
25391da177e4SLinus Torvalds 		break;
2540f1fd75bfSSasikantha babu 	case PR_GET_NAME:
2541651d765dSAnton Blanchard 		get_task_comm(comm, me);
2542b6dff3ecSDavid Howells 		if (copy_to_user((char __user *)arg2, comm, sizeof(comm)))
2543651d765dSAnton Blanchard 			return -EFAULT;
2544651d765dSAnton Blanchard 		break;
2545b6dff3ecSDavid Howells 	case PR_GET_ENDIAN:
2546651d765dSAnton Blanchard 		error = GET_ENDIAN(me, arg2);
25471d9d02feSAndrea Arcangeli 		break;
25481d9d02feSAndrea Arcangeli 	case PR_SET_ENDIAN:
25491d9d02feSAndrea Arcangeli 		error = SET_ENDIAN(me, arg2);
25501d9d02feSAndrea Arcangeli 		break;
2551e2cfabdfSWill Drewry 	case PR_GET_SECCOMP:
25521d9d02feSAndrea Arcangeli 		error = prctl_get_seccomp();
25538fb402bcSErik Bosman 		break;
25548fb402bcSErik Bosman 	case PR_SET_SECCOMP:
25558fb402bcSErik Bosman 		error = prctl_set_seccomp(arg2, (char __user *)arg3);
25568fb402bcSErik Bosman 		break;
25578fb402bcSErik Bosman 	case PR_GET_TSC:
25588fb402bcSErik Bosman 		error = GET_TSC_CTL(arg2);
2559cdd6c482SIngo Molnar 		break;
2560cdd6c482SIngo Molnar 	case PR_SET_TSC:
25611d1c7ddbSIngo Molnar 		error = SET_TSC_CTL(arg2);
2562cdd6c482SIngo Molnar 		break;
2563cdd6c482SIngo Molnar 	case PR_TASK_PERF_EVENTS_DISABLE:
25641d1c7ddbSIngo Molnar 		error = perf_event_task_disable();
25656976675dSArjan van de Ven 		break;
2566da8b44d5SJohn Stultz 	case PR_TASK_PERF_EVENTS_ENABLE:
2567da8b44d5SJohn Stultz 		error = perf_event_task_enable();
2568da8b44d5SJohn Stultz 		break;
25696976675dSArjan van de Ven 	case PR_GET_TIMERSLACK:
25706976675dSArjan van de Ven 		if (current->timer_slack_ns > ULONG_MAX)
25716976675dSArjan van de Ven 			error = ULONG_MAX;
25722004cef1SLinus Torvalds 		else
2573ed4fb6d7SFelix Moessbauer 			error = current->timer_slack_ns;
25746976675dSArjan van de Ven 		break;
25756976675dSArjan van de Ven 	case PR_SET_TIMERSLACK:
25766976675dSArjan van de Ven 		if (rt_or_dl_task_policy(current))
25776976675dSArjan van de Ven 			break;
25786976675dSArjan van de Ven 		if (arg2 <= 0)
25796976675dSArjan van de Ven 			current->timer_slack_ns =
25804db96cf0SAndi Kleen 					current->default_timer_slack_ns;
25814db96cf0SAndi Kleen 		else
25824db96cf0SAndi Kleen 			current->timer_slack_ns = arg2;
25834db96cf0SAndi Kleen 		break;
25841087e9b4SAndi Kleen 	case PR_MCE_KILL:
25854db96cf0SAndi Kleen 		if (arg4 | arg5)
25864db96cf0SAndi Kleen 			return -EINVAL;
25874db96cf0SAndi Kleen 		switch (arg2) {
25884db96cf0SAndi Kleen 		case PR_MCE_KILL_CLEAR:
25891087e9b4SAndi Kleen 			if (arg3 != 0)
25904db96cf0SAndi Kleen 				return -EINVAL;
25911087e9b4SAndi Kleen 			current->flags &= ~PF_MCE_PROCESS;
25924db96cf0SAndi Kleen 			break;
25931087e9b4SAndi Kleen 		case PR_MCE_KILL_SET:
25944db96cf0SAndi Kleen 			current->flags |= PF_MCE_PROCESS;
25951087e9b4SAndi Kleen 			if (arg3 == PR_MCE_KILL_EARLY)
25961087e9b4SAndi Kleen 				current->flags |= PF_MCE_EARLY;
25971087e9b4SAndi Kleen 			else if (arg3 == PR_MCE_KILL_LATE)
25981087e9b4SAndi Kleen 				current->flags &= ~PF_MCE_EARLY;
25991087e9b4SAndi Kleen 			else if (arg3 == PR_MCE_KILL_DEFAULT)
26004db96cf0SAndi Kleen 				current->flags &=
26014db96cf0SAndi Kleen 						~(PF_MCE_EARLY|PF_MCE_PROCESS);
26024db96cf0SAndi Kleen 			else
26034db96cf0SAndi Kleen 				return -EINVAL;
26044db96cf0SAndi Kleen 			break;
26051087e9b4SAndi Kleen 		default:
26061087e9b4SAndi Kleen 			return -EINVAL;
26071087e9b4SAndi Kleen 		}
26081087e9b4SAndi Kleen 		break;
26091087e9b4SAndi Kleen 	case PR_MCE_KILL_GET:
26101087e9b4SAndi Kleen 		if (arg2 | arg3 | arg4 | arg5)
26111087e9b4SAndi Kleen 			return -EINVAL;
26121087e9b4SAndi Kleen 		if (current->flags & PF_MCE_PROCESS)
26131087e9b4SAndi Kleen 			error = (current->flags & PF_MCE_EARLY) ?
2614028ee4beSCyrill Gorcunov 				PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
2615028ee4beSCyrill Gorcunov 		else
2616028ee4beSCyrill Gorcunov 			error = PR_MCE_KILL_DEFAULT;
26175702c5eeSCyrill Gorcunov 		break;
2618986b9eacSRasmus Villemoes 	case PR_SET_MM:
26195702c5eeSCyrill Gorcunov 		error = prctl_set_mm(arg2, arg3, arg4, arg5);
2620ebec18a6SLennart Poettering 		break;
2621ebec18a6SLennart Poettering 	case PR_GET_TID_ADDRESS:
2622749860ceSPavel Tikhomirov 		error = prctl_get_tid_address(me, (int __user * __user *)arg2);
2623749860ceSPavel Tikhomirov 		break;
2624749860ceSPavel Tikhomirov 	case PR_SET_CHILD_SUBREAPER:
2625749860ceSPavel Tikhomirov 		me->signal->is_child_subreaper = !!arg2;
2626ebec18a6SLennart Poettering 		if (!arg2)
2627ebec18a6SLennart Poettering 			break;
2628ebec18a6SLennart Poettering 
2629ebec18a6SLennart Poettering 		walk_process_tree(me, propagate_has_child_subreaper, NULL);
2630ebec18a6SLennart Poettering 		break;
2631259e5e6cSAndy Lutomirski 	case PR_GET_CHILD_SUBREAPER:
2632259e5e6cSAndy Lutomirski 		error = put_user(me->signal->is_child_subreaper,
2633259e5e6cSAndy Lutomirski 				 (int __user *)arg2);
2634259e5e6cSAndy Lutomirski 		break;
26351d4457f9SKees Cook 	case PR_SET_NO_NEW_PRIVS:
2636259e5e6cSAndy Lutomirski 		if (arg2 != 1 || arg3 || arg4 || arg5)
2637259e5e6cSAndy Lutomirski 			return -EINVAL;
2638259e5e6cSAndy Lutomirski 
2639259e5e6cSAndy Lutomirski 		task_set_no_new_privs(current);
26401d4457f9SKees Cook 		break;
2641a0715cc2SAlex Thorlton 	case PR_GET_NO_NEW_PRIVS:
2642a0715cc2SAlex Thorlton 		if (arg2 || arg3 || arg4 || arg5)
2643a0715cc2SAlex Thorlton 			return -EINVAL;
264418600332SMichal Hocko 		return task_no_new_privs(current) ? 1 : 0;
2645a0715cc2SAlex Thorlton 	case PR_GET_THP_DISABLE:
2646a0715cc2SAlex Thorlton 		if (arg2 || arg3 || arg4 || arg5)
2647a0715cc2SAlex Thorlton 			return -EINVAL;
2648a0715cc2SAlex Thorlton 		error = !!test_bit(MMF_DISABLE_THP, &me->mm->flags);
2649d8ed45c5SMichel Lespinasse 		break;
265017b0573dSMichal Hocko 	case PR_SET_THP_DISABLE:
2651a0715cc2SAlex Thorlton 		if (arg3 || arg4 || arg5)
265218600332SMichal Hocko 			return -EINVAL;
2653a0715cc2SAlex Thorlton 		if (mmap_write_lock_killable(me->mm))
265418600332SMichal Hocko 			return -EINTR;
2655d8ed45c5SMichel Lespinasse 		if (arg2)
2656a0715cc2SAlex Thorlton 			set_bit(MMF_DISABLE_THP, &me->mm->flags);
2657fe3d197fSDave Hansen 		else
2658fe3d197fSDave Hansen 			clear_bit(MMF_DISABLE_THP, &me->mm->flags);
2659f240652bSDave Hansen 		mmap_write_unlock(me->mm);
2660e9d1b4f3SDave Hansen 		break;
26619791554bSPaul Burton 	case PR_MPX_ENABLE_MANAGEMENT:
26629791554bSPaul Burton 	case PR_MPX_DISABLE_MANAGEMENT:
26639791554bSPaul Burton 		/* No longer implemented: */
26649791554bSPaul Burton 		return -EINVAL;
26659791554bSPaul Burton 	case PR_SET_FP_MODE:
26669791554bSPaul Burton 		error = SET_FP_MODE(me, arg2);
26672d2123bcSDave Martin 		break;
26682d2123bcSDave Martin 	case PR_GET_FP_MODE:
26692d2123bcSDave Martin 		error = GET_FP_MODE(me);
26702d2123bcSDave Martin 		break;
26712d2123bcSDave Martin 	case PR_SVE_SET_VL:
26722d2123bcSDave Martin 		error = SVE_SET_VL(arg2);
26739e4ab6c8SMark Brown 		break;
26749e4ab6c8SMark Brown 	case PR_SVE_GET_VL:
26759e4ab6c8SMark Brown 		error = SVE_GET_VL();
26769e4ab6c8SMark Brown 		break;
26779e4ab6c8SMark Brown 	case PR_SME_SET_VL:
26789e4ab6c8SMark Brown 		error = SME_SET_VL(arg2);
2679b617cfc8SThomas Gleixner 		break;
2680b617cfc8SThomas Gleixner 	case PR_SME_GET_VL:
2681b617cfc8SThomas Gleixner 		error = SME_GET_VL();
26827bbf1373SKees Cook 		break;
2683b617cfc8SThomas Gleixner 	case PR_GET_SPECULATION_CTRL:
2684b617cfc8SThomas Gleixner 		if (arg3 || arg4 || arg5)
2685b617cfc8SThomas Gleixner 			return -EINVAL;
2686b617cfc8SThomas Gleixner 		error = arch_prctl_spec_ctrl_get(me, arg2);
26877bbf1373SKees Cook 		break;
2688b617cfc8SThomas Gleixner 	case PR_SET_SPECULATION_CTRL:
2689ba830885SKristina Martsenko 		if (arg4 || arg5)
2690ba830885SKristina Martsenko 			return -EINVAL;
2691ba830885SKristina Martsenko 		error = arch_prctl_spec_ctrl_set(me, arg2, arg3);
2692ba830885SKristina Martsenko 		break;
2693ba830885SKristina Martsenko 	case PR_PAC_RESET_KEYS:
269420169862SPeter Collingbourne 		if (arg3 || arg4 || arg5)
269520169862SPeter Collingbourne 			return -EINVAL;
269620169862SPeter Collingbourne 		error = PAC_RESET_KEYS(me, arg2);
269720169862SPeter Collingbourne 		break;
269820169862SPeter Collingbourne 	case PR_PAC_SET_ENABLED_KEYS:
269920169862SPeter Collingbourne 		if (arg4 || arg5)
270020169862SPeter Collingbourne 			return -EINVAL;
270120169862SPeter Collingbourne 		error = PAC_SET_ENABLED_KEYS(me, arg2, arg3);
270220169862SPeter Collingbourne 		break;
270320169862SPeter Collingbourne 	case PR_PAC_GET_ENABLED_KEYS:
270463f0c603SCatalin Marinas 		if (arg2 || arg3 || arg4 || arg5)
27053e91ec89SCatalin Marinas 			return -EINVAL;
27063e91ec89SCatalin Marinas 		error = PAC_GET_ENABLED_KEYS(me);
270763f0c603SCatalin Marinas 		break;
270863f0c603SCatalin Marinas 	case PR_SET_TAGGED_ADDR_CTRL:
270963f0c603SCatalin Marinas 		if (arg3 || arg4 || arg5)
27103e91ec89SCatalin Marinas 			return -EINVAL;
27113e91ec89SCatalin Marinas 		error = SET_TAGGED_ADDR_CTRL(arg2);
271263f0c603SCatalin Marinas 		break;
271363f0c603SCatalin Marinas 	case PR_GET_TAGGED_ADDR_CTRL:
27148d19f1c8SMike Christie 		if (arg2 || arg3 || arg4 || arg5)
27158d19f1c8SMike Christie 			return -EINVAL;
27168d19f1c8SMike Christie 		error = GET_TAGGED_ADDR_CTRL();
27178d19f1c8SMike Christie 		break;
27188d19f1c8SMike Christie 	case PR_SET_IO_FLUSHER:
27198d19f1c8SMike Christie 		if (!capable(CAP_SYS_RESOURCE))
27208d19f1c8SMike Christie 			return -EPERM;
27218d19f1c8SMike Christie 
27228d19f1c8SMike Christie 		if (arg3 || arg4 || arg5)
27238d19f1c8SMike Christie 			return -EINVAL;
27248d19f1c8SMike Christie 
27258d19f1c8SMike Christie 		if (arg2 == 1)
27268d19f1c8SMike Christie 			current->flags |= PR_IO_FLUSHER;
27278d19f1c8SMike Christie 		else if (!arg2)
27288d19f1c8SMike Christie 			current->flags &= ~PR_IO_FLUSHER;
27298d19f1c8SMike Christie 		else
27308d19f1c8SMike Christie 			return -EINVAL;
27318d19f1c8SMike Christie 		break;
27328d19f1c8SMike Christie 	case PR_GET_IO_FLUSHER:
27338d19f1c8SMike Christie 		if (!capable(CAP_SYS_RESOURCE))
27348d19f1c8SMike Christie 			return -EPERM;
27358d19f1c8SMike Christie 
27368d19f1c8SMike Christie 		if (arg2 || arg3 || arg4 || arg5)
27371446e1dfSGabriel Krisman Bertazi 			return -EINVAL;
27381446e1dfSGabriel Krisman Bertazi 
27391446e1dfSGabriel Krisman Bertazi 		error = (current->flags & PR_IO_FLUSHER) == PR_IO_FLUSHER;
27401446e1dfSGabriel Krisman Bertazi 		break;
27417ac592aaSChris Hyser 	case PR_SET_SYSCALL_USER_DISPATCH:
27427ac592aaSChris Hyser 		error = set_syscall_user_dispatch(arg2, arg3, arg4,
27437ac592aaSChris Hyser 						  (char __user *) arg5);
27447ac592aaSChris Hyser 		break;
27457ac592aaSChris Hyser #ifdef CONFIG_SCHED_CORE
2746b507808eSJoey Gouly 	case PR_SCHED_CORE:
2747b507808eSJoey Gouly 		error = sched_core_share_pid(arg2, arg3, arg4, arg5);
2748b507808eSJoey Gouly 		break;
2749b507808eSJoey Gouly #endif
2750b507808eSJoey Gouly 	case PR_SET_MDWE:
2751b507808eSJoey Gouly 		error = prctl_set_mdwe(arg2, arg3, arg4, arg5);
2752628d701fSBenjamin Gray 		break;
2753628d701fSBenjamin Gray 	case PR_GET_MDWE:
2754628d701fSBenjamin Gray 		error = prctl_get_mdwe(arg2, arg3, arg4, arg5);
2755628d701fSBenjamin Gray 		break;
2756628d701fSBenjamin Gray 	case PR_PPC_GET_DEXCR:
2757628d701fSBenjamin Gray 		if (arg3 || arg4 || arg5)
2758628d701fSBenjamin Gray 			return -EINVAL;
2759628d701fSBenjamin Gray 		error = PPC_GET_DEXCR_ASPECT(me, arg2);
2760628d701fSBenjamin Gray 		break;
2761628d701fSBenjamin Gray 	case PR_PPC_SET_DEXCR:
27629a10064fSColin Cross 		if (arg4 || arg5)
27639a10064fSColin Cross 			return -EINVAL;
27649a10064fSColin Cross 		error = PPC_SET_DEXCR_ASPECT(me, arg2, arg3);
2765636e3483SMiguel Ojeda 		break;
2766636e3483SMiguel Ojeda 	case PR_SET_VMA:
2767636e3483SMiguel Ojeda 		error = prctl_set_vma(arg2, arg3, arg4, arg5);
2768636e3483SMiguel Ojeda 		break;
2769636e3483SMiguel Ojeda 	case PR_GET_AUXV:
2770d7597f59SStefan Roesch 		if (arg4 || arg5)
2771d7597f59SStefan Roesch 			return -EINVAL;
2772d7597f59SStefan Roesch 		error = prctl_get_auxv((void __user *)arg2, arg3);
2773d7597f59SStefan Roesch 		break;
2774d7597f59SStefan Roesch #ifdef CONFIG_KSM
2775d7597f59SStefan Roesch 	case PR_SET_MEMORY_MERGE:
2776d7597f59SStefan Roesch 		if (arg3 || arg4 || arg5)
277724139c07SDavid Hildenbrand 			return -EINVAL;
2778d7597f59SStefan Roesch 		if (mmap_write_lock_killable(me->mm))
277924139c07SDavid Hildenbrand 			return -EINTR;
278024139c07SDavid Hildenbrand 
2781d7597f59SStefan Roesch 		if (arg2)
2782d7597f59SStefan Roesch 			error = ksm_enable_merge_any(me->mm);
2783d7597f59SStefan Roesch 		else
2784d7597f59SStefan Roesch 			error = ksm_disable_merge_any(me->mm);
2785d7597f59SStefan Roesch 		mmap_write_unlock(me->mm);
2786d7597f59SStefan Roesch 		break;
2787d7597f59SStefan Roesch 	case PR_GET_MEMORY_MERGE:
2788d7597f59SStefan Roesch 		if (arg2 || arg3 || arg4 || arg5)
2789d7597f59SStefan Roesch 			return -EINVAL;
27901fd96a3eSAndy Chiu 
27911fd96a3eSAndy Chiu 		error = !!test_bit(MMF_VM_MERGE_ANY, &me->mm->flags);
27921fd96a3eSAndy Chiu 		break;
27931fd96a3eSAndy Chiu #endif
27941fd96a3eSAndy Chiu 	case PR_RISCV_V_SET_CONTROL:
27951fd96a3eSAndy Chiu 		error = RISCV_V_SET_CONTROL(arg2);
27966b9391b5SCharlie Jenkins 		break;
27976b9391b5SCharlie Jenkins 	case PR_RISCV_V_GET_CONTROL:
27986b9391b5SCharlie Jenkins 		error = RISCV_V_GET_CONTROL();
279991e102e7SMark Brown 		break;
280091e102e7SMark Brown 	case PR_RISCV_SET_ICACHE_FLUSH_CTX:
280191e102e7SMark Brown 		error = RISCV_SET_ICACHE_FLUSH_CTX(arg2, arg3);
280291e102e7SMark Brown 		break;
280391e102e7SMark Brown 	case PR_GET_SHADOW_STACK_STATUS:
280491e102e7SMark Brown 		if (arg3 || arg4 || arg5)
280591e102e7SMark Brown 			return -EINVAL;
280691e102e7SMark Brown 		error = arch_get_shadow_stack_status(me, (unsigned long __user *) arg2);
280791e102e7SMark Brown 		break;
280891e102e7SMark Brown 	case PR_SET_SHADOW_STACK_STATUS:
280991e102e7SMark Brown 		if (arg3 || arg4 || arg5)
281091e102e7SMark Brown 			return -EINVAL;
281191e102e7SMark Brown 		error = arch_set_shadow_stack_status(me, arg2);
281291e102e7SMark Brown 		break;
281391e102e7SMark Brown 	case PR_LOCK_SHADOW_STACK_STATUS:
2814*ec2d0c04SThomas Gleixner 		if (arg3 || arg4 || arg5)
2815*ec2d0c04SThomas Gleixner 			return -EINVAL;
2816*ec2d0c04SThomas Gleixner 		error = arch_lock_shadow_stack_status(me, arg2);
2817*ec2d0c04SThomas Gleixner 		break;
2818*ec2d0c04SThomas Gleixner 	case PR_TIMER_CREATE_RESTORE_IDS:
28191da177e4SLinus Torvalds 		if (arg3 || arg4 || arg5)
2820c38904ebSMarco Elver 			return -EINVAL;
28211da177e4SLinus Torvalds 		error = posixtimer_create_prctl(arg2);
28221da177e4SLinus Torvalds 		break;
28231da177e4SLinus Torvalds 	default:
28241da177e4SLinus Torvalds 		trace_task_prctl_unknown(option, arg2, arg3, arg4, arg5);
28251da177e4SLinus Torvalds 		error = -EINVAL;
28263cfc348bSAndi Kleen 		break;
2827836f92adSHeiko Carstens 	}
2828836f92adSHeiko Carstens 	return error;
28293cfc348bSAndi Kleen }
28303cfc348bSAndi Kleen 
SYSCALL_DEFINE3(getcpu,unsigned __user *,cpup,unsigned __user *,nodep,struct getcpu_cache __user *,unused)28313cfc348bSAndi Kleen SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
2832ec94fc3dSvishnu.ps 		struct getcpu_cache __user *, unused)
28333cfc348bSAndi Kleen {
28343cfc348bSAndi Kleen 	int err = 0;
28353cfc348bSAndi Kleen 	int cpu = raw_smp_processor_id();
28363cfc348bSAndi Kleen 
28373cfc348bSAndi Kleen 	if (cpup)
28383cfc348bSAndi Kleen 		err |= put_user(cpu, cpup);
283910a0a8d4SJeremy Fitzhardinge 	if (nodep)
28404a22f166SStephen Rothwell 		err |= put_user(cpu_to_node(cpu), nodep);
28414a22f166SStephen Rothwell 	return err ? -EFAULT : 0;
28424a22f166SStephen Rothwell }
28434a22f166SStephen Rothwell 
28444a22f166SStephen Rothwell /**
28454a22f166SStephen Rothwell  * do_sysinfo - fill in sysinfo struct
28464a22f166SStephen Rothwell  * @info: pointer to buffer to fill
28474a22f166SStephen Rothwell  */
do_sysinfo(struct sysinfo * info)2848dc1b7b6cSArnd Bergmann static int do_sysinfo(struct sysinfo *info)
28494a22f166SStephen Rothwell {
28504a22f166SStephen Rothwell 	unsigned long mem_total, sav_total;
28514a22f166SStephen Rothwell 	unsigned int mem_unit, bitcount;
2852dc1b7b6cSArnd Bergmann 	struct timespec64 tp;
2853ecc421e0SCyril Hrubis 
28544a22f166SStephen Rothwell 	memset(info, 0, sizeof(struct sysinfo));
28554a22f166SStephen Rothwell 
28564a22f166SStephen Rothwell 	ktime_get_boottime_ts64(&tp);
28574a22f166SStephen Rothwell 	timens_add_boottime(&tp);
28584a22f166SStephen Rothwell 	info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
28594a22f166SStephen Rothwell 
28604a22f166SStephen Rothwell 	get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
28614a22f166SStephen Rothwell 
28624a22f166SStephen Rothwell 	info->procs = nr_threads;
28634a22f166SStephen Rothwell 
28644a22f166SStephen Rothwell 	si_meminfo(info);
28654a22f166SStephen Rothwell 	si_swapinfo(info);
28664a22f166SStephen Rothwell 
28674a22f166SStephen Rothwell 	/*
28684a22f166SStephen Rothwell 	 * If the sum of all the available memory (i.e. ram + swap)
28694a22f166SStephen Rothwell 	 * is less than can be stored in a 32 bit unsigned long then
28704a22f166SStephen Rothwell 	 * we can be binary compatible with 2.2.x kernels.  If not,
28714a22f166SStephen Rothwell 	 * well, in that case 2.2.x was broken anyways...
28724a22f166SStephen Rothwell 	 *
28734a22f166SStephen Rothwell 	 *  -Erik Andersen <[email protected]>
28744a22f166SStephen Rothwell 	 */
28754a22f166SStephen Rothwell 
28764a22f166SStephen Rothwell 	mem_total = info->totalram + info->totalswap;
28774a22f166SStephen Rothwell 	if (mem_total < info->totalram || mem_total < info->totalswap)
28784a22f166SStephen Rothwell 		goto out;
28794a22f166SStephen Rothwell 	bitcount = 0;
28804a22f166SStephen Rothwell 	mem_unit = info->mem_unit;
28814a22f166SStephen Rothwell 	while (mem_unit > 1) {
28824a22f166SStephen Rothwell 		bitcount++;
28834a22f166SStephen Rothwell 		mem_unit >>= 1;
28844a22f166SStephen Rothwell 		sav_total = mem_total;
28854a22f166SStephen Rothwell 		mem_total <<= 1;
28864a22f166SStephen Rothwell 		if (mem_total < sav_total)
28874a22f166SStephen Rothwell 			goto out;
28884a22f166SStephen Rothwell 	}
28894a22f166SStephen Rothwell 
28904a22f166SStephen Rothwell 	/*
28914a22f166SStephen Rothwell 	 * If mem_total did not overflow, multiply all memory values by
28924a22f166SStephen Rothwell 	 * info->mem_unit and set it to 1.  This leaves things compatible
28934a22f166SStephen Rothwell 	 * with 2.2.x, and also retains compatibility with earlier 2.4.x
28944a22f166SStephen Rothwell 	 * kernels...
28954a22f166SStephen Rothwell 	 */
28964a22f166SStephen Rothwell 
28974a22f166SStephen Rothwell 	info->mem_unit = 1;
28984a22f166SStephen Rothwell 	info->totalram <<= bitcount;
28994a22f166SStephen Rothwell 	info->freeram <<= bitcount;
29004a22f166SStephen Rothwell 	info->sharedram <<= bitcount;
29014a22f166SStephen Rothwell 	info->bufferram <<= bitcount;
29024a22f166SStephen Rothwell 	info->totalswap <<= bitcount;
29034a22f166SStephen Rothwell 	info->freeswap <<= bitcount;
29044a22f166SStephen Rothwell 	info->totalhigh <<= bitcount;
29054a22f166SStephen Rothwell 	info->freehigh <<= bitcount;
29064a22f166SStephen Rothwell 
29074a22f166SStephen Rothwell out:
29084a22f166SStephen Rothwell 	return 0;
29094a22f166SStephen Rothwell }
29104a22f166SStephen Rothwell 
SYSCALL_DEFINE1(sysinfo,struct sysinfo __user *,info)29114a22f166SStephen Rothwell SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
29124a22f166SStephen Rothwell {
29134a22f166SStephen Rothwell 	struct sysinfo val;
29144a22f166SStephen Rothwell 
29154a22f166SStephen Rothwell 	do_sysinfo(&val);
29164a22f166SStephen Rothwell 
29174a22f166SStephen Rothwell 	if (copy_to_user(info, &val, sizeof(struct sysinfo)))
29184a22f166SStephen Rothwell 		return -EFAULT;
29194a22f166SStephen Rothwell 
29204a22f166SStephen Rothwell 	return 0;
29214a22f166SStephen Rothwell }
29224a22f166SStephen Rothwell 
29234a22f166SStephen Rothwell #ifdef CONFIG_COMPAT
29244a22f166SStephen Rothwell struct compat_sysinfo {
29254a22f166SStephen Rothwell 	s32 uptime;
29264a22f166SStephen Rothwell 	u32 loads[3];
29274a22f166SStephen Rothwell 	u32 totalram;
29284a22f166SStephen Rothwell 	u32 freeram;
29294a22f166SStephen Rothwell 	u32 sharedram;
29304a22f166SStephen Rothwell 	u32 bufferram;
29314a22f166SStephen Rothwell 	u32 totalswap;
29324a22f166SStephen Rothwell 	u32 freeswap;
29334a22f166SStephen Rothwell 	u16 procs;
29344a22f166SStephen Rothwell 	u16 pad;
29354a22f166SStephen Rothwell 	u32 totalhigh;
29364a22f166SStephen Rothwell 	u32 freehigh;
29374a22f166SStephen Rothwell 	u32 mem_unit;
29384a22f166SStephen Rothwell 	char _f[20-2*sizeof(u32)-sizeof(int)];
29394a22f166SStephen Rothwell };
2940ce5155c4SAl Viro 
COMPAT_SYSCALL_DEFINE1(sysinfo,struct compat_sysinfo __user *,info)29414a22f166SStephen Rothwell COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
29424a22f166SStephen Rothwell {
29434a22f166SStephen Rothwell 	struct sysinfo s;
29444a22f166SStephen Rothwell 	struct compat_sysinfo s_32;
29454a22f166SStephen Rothwell 
29464a22f166SStephen Rothwell 	do_sysinfo(&s);
29470baae41eSScotty Bauer 
29484a22f166SStephen Rothwell 	/* Check to see if any memory value is too large for 32-bit and scale
29494a22f166SStephen Rothwell 	 *  down if needed
29504a22f166SStephen Rothwell 	 */
29514a22f166SStephen Rothwell 	if (upper_32_bits(s.totalram) || upper_32_bits(s.totalswap)) {
29524a22f166SStephen Rothwell 		int bitcount = 0;
29534a22f166SStephen Rothwell 
29544a22f166SStephen Rothwell 		while (s.mem_unit < PAGE_SIZE) {
29554a22f166SStephen Rothwell 			s.mem_unit <<= 1;
29564a22f166SStephen Rothwell 			bitcount++;
29574a22f166SStephen Rothwell 		}
29584a22f166SStephen Rothwell 
29594a22f166SStephen Rothwell 		s.totalram >>= bitcount;
29604a22f166SStephen Rothwell 		s.freeram >>= bitcount;
29614a22f166SStephen Rothwell 		s.sharedram >>= bitcount;
29624a22f166SStephen Rothwell 		s.bufferram >>= bitcount;
29634a22f166SStephen Rothwell 		s.totalswap >>= bitcount;
29644a22f166SStephen Rothwell 		s.freeswap >>= bitcount;
2965ce5155c4SAl Viro 		s.totalhigh >>= bitcount;
2966ce5155c4SAl Viro 		s.freehigh >>= bitcount;
2967ce5155c4SAl Viro 	}
2968ce5155c4SAl Viro 
2969ce5155c4SAl Viro 	memset(&s_32, 0, sizeof(s_32));
2970ce5155c4SAl Viro 	s_32.uptime = s.uptime;
2971ce5155c4SAl Viro 	s_32.loads[0] = s.loads[0];
2972ce5155c4SAl Viro 	s_32.loads[1] = s.loads[1];
2973ce5155c4SAl Viro 	s_32.loads[2] = s.loads[2];
2974ce5155c4SAl Viro 	s_32.totalram = s.totalram;
2975ce5155c4SAl Viro 	s_32.freeram = s.freeram;
2976ce5155c4SAl Viro 	s_32.sharedram = s.sharedram;
2977ce5155c4SAl Viro 	s_32.bufferram = s.bufferram;
2978ce5155c4SAl Viro 	s_32.totalswap = s.totalswap;
2979ce5155c4SAl Viro 	s_32.freeswap = s.freeswap;
2980ce5155c4SAl Viro 	s_32.procs = s.procs;
29814a22f166SStephen Rothwell 	s_32.totalhigh = s.totalhigh;
29824a22f166SStephen Rothwell 	s_32.freehigh = s.freehigh;
29834a22f166SStephen Rothwell 	s_32.mem_unit = s.mem_unit;
29844a22f166SStephen Rothwell 	if (copy_to_user(info, &s_32, sizeof(s_32)))
2985 		return -EFAULT;
2986 	return 0;
2987 }
2988 #endif /* CONFIG_COMPAT */
2989