blob: 28b8a4c1bc7ea4e9e75e849b784e68069af706bb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/sys.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
Paul Gortmaker9984de12011-05-23 14:51:41 -04007#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/mm.h>
9#include <linux/utsname.h>
10#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/reboot.h>
12#include <linux/prctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/highuid.h>
14#include <linux/fs.h>
Paul Gortmaker74da1ff2011-05-26 12:48:41 -040015#include <linux/kmod.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020016#include <linux/perf_event.h>
Daniel Walker3e88c552007-05-10 22:22:53 -070017#include <linux/resource.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070018#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/workqueue.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080020#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/device.h>
22#include <linux/key.h>
23#include <linux/times.h>
24#include <linux/posix-timers.h>
25#include <linux/security.h>
26#include <linux/dcookies.h>
27#include <linux/suspend.h>
28#include <linux/tty.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070029#include <linux/signal.h>
Matt Helsley9f460802005-11-07 00:59:16 -080030#include <linux/cn_proc.h>
Andi Kleen3cfc3482006-09-26 10:52:28 +020031#include <linux/getcpu.h>
Eric Dumazet6eaeeab2007-05-10 22:22:37 -070032#include <linux/task_io_accounting_ops.h>
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -070033#include <linux/seccomp.h>
Mark Lord40477272007-10-01 01:20:10 -070034#include <linux/cpu.h>
Christoph Hellwige28cbf22010-03-10 15:21:19 -080035#include <linux/personality.h>
Paul Mackerrase3d5a272009-01-06 14:41:02 -080036#include <linux/ptrace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040037#include <linux/fs_struct.h>
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -070038#include <linux/file.h>
39#include <linux/mount.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/gfp.h>
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010041#include <linux/syscore_ops.h>
Andi Kleenbe274252011-08-19 16:15:10 -070042#include <linux/version.h>
43#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <linux/compat.h>
46#include <linux/syscalls.h>
Keshavamurthy Anil S00d7c052005-12-12 00:37:33 -080047#include <linux/kprobes.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070048#include <linux/user_namespace.h>
Chen Gang7fe5e042013-02-21 16:43:06 -080049#include <linux/binfmts.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Stephen Rothwell4a22f162013-04-30 15:27:37 -070051#include <linux/sched.h>
Ingo Molnar4f177222017-02-08 08:45:17 +010052#include <linux/sched/loadavg.h>
Stephen Rothwell4a22f162013-04-30 15:27:37 -070053#include <linux/rcupdate.h>
54#include <linux/uidgid.h>
55#include <linux/cred.h>
56
Seiji Aguchi04c68622011-01-12 16:59:30 -080057#include <linux/kmsg_dump.h>
Andi Kleenbe274252011-08-19 16:15:10 -070058/* Move somewhere else to avoid recompiling? */
59#include <generated/utsrelease.h>
Seiji Aguchi04c68622011-01-12 16:59:30 -080060
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080061#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <asm/io.h>
63#include <asm/unistd.h>
64
65#ifndef SET_UNALIGN_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070066# define SET_UNALIGN_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#endif
68#ifndef GET_UNALIGN_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070069# define GET_UNALIGN_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#endif
71#ifndef SET_FPEMU_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070072# define SET_FPEMU_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#endif
74#ifndef GET_FPEMU_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070075# define GET_FPEMU_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#endif
77#ifndef SET_FPEXC_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070078# define SET_FPEXC_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#endif
80#ifndef GET_FPEXC_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070081# define GET_FPEXC_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#endif
Anton Blanchard651d7652006-06-07 16:10:19 +100083#ifndef GET_ENDIAN
vishnu.psec94fc32014-10-09 15:30:23 -070084# define GET_ENDIAN(a, b) (-EINVAL)
Anton Blanchard651d7652006-06-07 16:10:19 +100085#endif
86#ifndef SET_ENDIAN
vishnu.psec94fc32014-10-09 15:30:23 -070087# define SET_ENDIAN(a, b) (-EINVAL)
Anton Blanchard651d7652006-06-07 16:10:19 +100088#endif
Erik Bosman8fb402b2008-04-11 18:54:17 +020089#ifndef GET_TSC_CTL
90# define GET_TSC_CTL(a) (-EINVAL)
91#endif
92#ifndef SET_TSC_CTL
93# define SET_TSC_CTL(a) (-EINVAL)
94#endif
Dave Hansenfe3d1972014-11-14 07:18:29 -080095#ifndef MPX_ENABLE_MANAGEMENT
Dave Hansen46a6e0c2015-06-07 11:37:02 -070096# define MPX_ENABLE_MANAGEMENT() (-EINVAL)
Dave Hansenfe3d1972014-11-14 07:18:29 -080097#endif
98#ifndef MPX_DISABLE_MANAGEMENT
Dave Hansen46a6e0c2015-06-07 11:37:02 -070099# define MPX_DISABLE_MANAGEMENT() (-EINVAL)
Dave Hansenfe3d1972014-11-14 07:18:29 -0800100#endif
Paul Burton97915542015-01-08 12:17:37 +0000101#ifndef GET_FP_MODE
102# define GET_FP_MODE(a) (-EINVAL)
103#endif
104#ifndef SET_FP_MODE
105# define SET_FP_MODE(a,b) (-EINVAL)
106#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/*
109 * this is where the system-wide overflow UID and GID are defined, for
110 * architectures that now have 32-bit UID/GID but didn't in the past
111 */
112
113int overflowuid = DEFAULT_OVERFLOWUID;
114int overflowgid = DEFAULT_OVERFLOWGID;
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116EXPORT_SYMBOL(overflowuid);
117EXPORT_SYMBOL(overflowgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119/*
120 * the same as above, but for filesystems which can only store a 16-bit
121 * UID and GID. as such, this is needed on all architectures
122 */
123
124int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
125int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
126
127EXPORT_SYMBOL(fs_overflowuid);
128EXPORT_SYMBOL(fs_overflowgid);
129
130/*
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700131 * Returns true if current's euid is same as p's uid or euid,
132 * or has CAP_SYS_NICE to p's user_ns.
133 *
134 * Called with rcu_read_lock, creds are safe
135 */
136static bool set_one_prio_perm(struct task_struct *p)
137{
138 const struct cred *cred = current_cred(), *pcred = __task_cred(p);
139
Eric W. Biederman5af66202012-03-03 20:21:47 -0800140 if (uid_eq(pcred->uid, cred->euid) ||
141 uid_eq(pcred->euid, cred->euid))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700142 return true;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800143 if (ns_capable(pcred->user_ns, CAP_SYS_NICE))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700144 return true;
145 return false;
146}
147
148/*
David Howellsc69e8d92008-11-14 10:39:19 +1100149 * set the priority of a task
150 * - the caller must hold the RCU read lock
151 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static int set_one_prio(struct task_struct *p, int niceval, int error)
153{
154 int no_nice;
155
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700156 if (!set_one_prio_perm(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 error = -EPERM;
158 goto out;
159 }
Matt Mackalle43379f2005-05-01 08:59:00 -0700160 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 error = -EACCES;
162 goto out;
163 }
164 no_nice = security_task_setnice(p, niceval);
165 if (no_nice) {
166 error = no_nice;
167 goto out;
168 }
169 if (error == -ESRCH)
170 error = 0;
171 set_user_nice(p, niceval);
172out:
173 return error;
174}
175
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100176SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 struct task_struct *g, *p;
179 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100180 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 int error = -EINVAL;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800182 struct pid *pgrp;
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800183 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Daniel Walker3e88c552007-05-10 22:22:53 -0700185 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 goto out;
187
188 /* normalize: avoid signed division (rounding problems) */
189 error = -ESRCH;
Dongsheng Yangc4a4d2f2014-02-11 15:34:51 +0800190 if (niceval < MIN_NICE)
191 niceval = MIN_NICE;
192 if (niceval > MAX_NICE)
193 niceval = MAX_NICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000195 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 read_lock(&tasklist_lock);
197 switch (which) {
vishnu.psec94fc32014-10-09 15:30:23 -0700198 case PRIO_PROCESS:
199 if (who)
200 p = find_task_by_vpid(who);
201 else
202 p = current;
203 if (p)
204 error = set_one_prio(p, niceval, error);
205 break;
206 case PRIO_PGRP:
207 if (who)
208 pgrp = find_vpid(who);
209 else
210 pgrp = task_pgrp(current);
211 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
212 error = set_one_prio(p, niceval, error);
213 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
214 break;
215 case PRIO_USER:
216 uid = make_kuid(cred->user_ns, who);
217 user = cred->user;
218 if (!who)
219 uid = cred->uid;
220 else if (!uid_eq(uid, cred->uid)) {
221 user = find_user(uid);
222 if (!user)
David Howells86a264a2008-11-14 10:39:18 +1100223 goto out_unlock; /* No processes for this user */
vishnu.psec94fc32014-10-09 15:30:23 -0700224 }
225 do_each_thread(g, p) {
Ben Segall8639b462015-11-06 16:32:48 -0800226 if (uid_eq(task_uid(p), uid) && task_pid_vnr(p))
vishnu.psec94fc32014-10-09 15:30:23 -0700227 error = set_one_prio(p, niceval, error);
228 } while_each_thread(g, p);
229 if (!uid_eq(uid, cred->uid))
230 free_uid(user); /* For find_user() */
231 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233out_unlock:
234 read_unlock(&tasklist_lock);
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000235 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236out:
237 return error;
238}
239
240/*
241 * Ugh. To avoid negative return values, "getpriority()" will
242 * not return the normal nice-value, but a negated value that
243 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
244 * to stay compatible.
245 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100246SYSCALL_DEFINE2(getpriority, int, which, int, who)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct task_struct *g, *p;
249 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100250 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 long niceval, retval = -ESRCH;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800252 struct pid *pgrp;
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800253 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Daniel Walker3e88c552007-05-10 22:22:53 -0700255 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return -EINVAL;
257
Tetsuo Handa70118832010-02-22 12:44:16 -0800258 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 read_lock(&tasklist_lock);
260 switch (which) {
vishnu.psec94fc32014-10-09 15:30:23 -0700261 case PRIO_PROCESS:
262 if (who)
263 p = find_task_by_vpid(who);
264 else
265 p = current;
266 if (p) {
267 niceval = nice_to_rlimit(task_nice(p));
268 if (niceval > retval)
269 retval = niceval;
270 }
271 break;
272 case PRIO_PGRP:
273 if (who)
274 pgrp = find_vpid(who);
275 else
276 pgrp = task_pgrp(current);
277 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
278 niceval = nice_to_rlimit(task_nice(p));
279 if (niceval > retval)
280 retval = niceval;
281 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
282 break;
283 case PRIO_USER:
284 uid = make_kuid(cred->user_ns, who);
285 user = cred->user;
286 if (!who)
287 uid = cred->uid;
288 else if (!uid_eq(uid, cred->uid)) {
289 user = find_user(uid);
290 if (!user)
291 goto out_unlock; /* No processes for this user */
292 }
293 do_each_thread(g, p) {
Ben Segall8639b462015-11-06 16:32:48 -0800294 if (uid_eq(task_uid(p), uid) && task_pid_vnr(p)) {
Dongsheng Yang7aa2c012014-05-08 18:33:49 +0900295 niceval = nice_to_rlimit(task_nice(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (niceval > retval)
297 retval = niceval;
298 }
vishnu.psec94fc32014-10-09 15:30:23 -0700299 } while_each_thread(g, p);
300 if (!uid_eq(uid, cred->uid))
301 free_uid(user); /* for find_user() */
302 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304out_unlock:
305 read_unlock(&tasklist_lock);
Tetsuo Handa70118832010-02-22 12:44:16 -0800306 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 return retval;
309}
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311/*
312 * Unprivileged users may change the real gid to the effective gid
313 * or vice versa. (BSD-style)
314 *
315 * If you set the real gid at all, or set the effective gid to a value not
316 * equal to the real gid, then the saved gid is set to the new effective gid.
317 *
318 * This makes it possible for a setgid program to completely drop its
319 * privileges, which is often a useful assertion to make when you are doing
320 * a security audit over a program.
321 *
322 * The general idea is that a program which uses just setregid() will be
323 * 100% compatible with BSD. A program which uses just setgid() will be
vishnu.psec94fc32014-10-09 15:30:23 -0700324 * 100% compatible with POSIX with saved IDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 *
326 * SMP: There are not races, the GIDs are checked only by filesystem
327 * operations (as far as semantic preservation is concerned).
328 */
Iulia Manda28138932015-04-15 16:16:41 -0700329#ifdef CONFIG_MULTIUSER
Heiko Carstensae1251a2009-01-14 14:14:05 +0100330SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800332 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100333 const struct cred *old;
334 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800336 kgid_t krgid, kegid;
337
338 krgid = make_kgid(ns, rgid);
339 kegid = make_kgid(ns, egid);
340
341 if ((rgid != (gid_t) -1) && !gid_valid(krgid))
342 return -EINVAL;
343 if ((egid != (gid_t) -1) && !gid_valid(kegid))
344 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
David Howellsd84f4f92008-11-14 10:39:23 +1100346 new = prepare_creds();
347 if (!new)
348 return -ENOMEM;
349 old = current_cred();
350
David Howellsd84f4f92008-11-14 10:39:23 +1100351 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (rgid != (gid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800353 if (gid_eq(old->gid, krgid) ||
354 gid_eq(old->egid, krgid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700355 ns_capable(old->user_ns, CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800356 new->gid = krgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 else
David Howellsd84f4f92008-11-14 10:39:23 +1100358 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360 if (egid != (gid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800361 if (gid_eq(old->gid, kegid) ||
362 gid_eq(old->egid, kegid) ||
363 gid_eq(old->sgid, kegid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700364 ns_capable(old->user_ns, CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800365 new->egid = kegid;
Cal Peake756184b2006-09-30 23:27:24 -0700366 else
David Howellsd84f4f92008-11-14 10:39:23 +1100367 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
David Howellsd84f4f92008-11-14 10:39:23 +1100369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (rgid != (gid_t) -1 ||
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800371 (egid != (gid_t) -1 && !gid_eq(kegid, old->gid)))
David Howellsd84f4f92008-11-14 10:39:23 +1100372 new->sgid = new->egid;
373 new->fsgid = new->egid;
374
375 return commit_creds(new);
376
377error:
378 abort_creds(new);
379 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
382/*
vishnu.psec94fc32014-10-09 15:30:23 -0700383 * setgid() is implemented like SysV w/ SAVED_IDS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 *
385 * SMP: Same implicit races as above.
386 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100387SYSCALL_DEFINE1(setgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800389 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100390 const struct cred *old;
391 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800393 kgid_t kgid;
394
395 kgid = make_kgid(ns, gid);
396 if (!gid_valid(kgid))
397 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
David Howellsd84f4f92008-11-14 10:39:23 +1100399 new = prepare_creds();
400 if (!new)
401 return -ENOMEM;
402 old = current_cred();
403
David Howellsd84f4f92008-11-14 10:39:23 +1100404 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700405 if (ns_capable(old->user_ns, CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800406 new->gid = new->egid = new->sgid = new->fsgid = kgid;
407 else if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->sgid))
408 new->egid = new->fsgid = kgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 else
David Howellsd84f4f92008-11-14 10:39:23 +1100410 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
David Howellsd84f4f92008-11-14 10:39:23 +1100412 return commit_creds(new);
413
414error:
415 abort_creds(new);
416 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
Dhaval Giani54e99122009-02-27 15:13:54 +0530418
David Howellsd84f4f92008-11-14 10:39:23 +1100419/*
420 * change the user struct in a credentials set to match the new UID
421 */
422static int set_user(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct user_struct *new_user;
425
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800426 new_user = alloc_uid(new->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (!new_user)
428 return -EAGAIN;
429
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400430 /*
431 * We don't fail in case of NPROC limit excess here because too many
432 * poorly written programs don't check set*uid() return code, assuming
433 * it never fails if called by root. We may still enforce NPROC limit
434 * for programs doing set*uid()+execve() by harmlessly deferring the
435 * failure to the execve() stage.
436 */
Jiri Slaby78d7d402010-03-05 13:42:54 -0800437 if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400438 new_user != INIT_USER)
439 current->flags |= PF_NPROC_EXCEEDED;
440 else
441 current->flags &= ~PF_NPROC_EXCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
David Howellsd84f4f92008-11-14 10:39:23 +1100443 free_uid(new->user);
444 new->user = new_user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return 0;
446}
447
448/*
449 * Unprivileged users may change the real uid to the effective uid
450 * or vice versa. (BSD-style)
451 *
452 * If you set the real uid at all, or set the effective uid to a value not
453 * equal to the real uid, then the saved uid is set to the new effective uid.
454 *
455 * This makes it possible for a setuid program to completely drop its
456 * privileges, which is often a useful assertion to make when you are doing
457 * a security audit over a program.
458 *
459 * The general idea is that a program which uses just setreuid() will be
460 * 100% compatible with BSD. A program which uses just setuid() will be
vishnu.psec94fc32014-10-09 15:30:23 -0700461 * 100% compatible with POSIX with saved IDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100463SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800465 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100466 const struct cred *old;
467 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800469 kuid_t kruid, keuid;
470
471 kruid = make_kuid(ns, ruid);
472 keuid = make_kuid(ns, euid);
473
474 if ((ruid != (uid_t) -1) && !uid_valid(kruid))
475 return -EINVAL;
476 if ((euid != (uid_t) -1) && !uid_valid(keuid))
477 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
David Howellsd84f4f92008-11-14 10:39:23 +1100479 new = prepare_creds();
480 if (!new)
481 return -ENOMEM;
482 old = current_cred();
483
David Howellsd84f4f92008-11-14 10:39:23 +1100484 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (ruid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800486 new->uid = kruid;
487 if (!uid_eq(old->uid, kruid) &&
488 !uid_eq(old->euid, kruid) &&
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700489 !ns_capable(old->user_ns, CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100490 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492
493 if (euid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800494 new->euid = keuid;
495 if (!uid_eq(old->uid, keuid) &&
496 !uid_eq(old->euid, keuid) &&
497 !uid_eq(old->suid, keuid) &&
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700498 !ns_capable(old->user_ns, CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100499 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800502 if (!uid_eq(new->uid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530503 retval = set_user(new);
504 if (retval < 0)
505 goto error;
506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (ruid != (uid_t) -1 ||
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800508 (euid != (uid_t) -1 && !uid_eq(keuid, old->uid)))
David Howellsd84f4f92008-11-14 10:39:23 +1100509 new->suid = new->euid;
510 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
David Howellsd84f4f92008-11-14 10:39:23 +1100512 retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
513 if (retval < 0)
514 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
David Howellsd84f4f92008-11-14 10:39:23 +1100516 return commit_creds(new);
517
518error:
519 abort_creds(new);
520 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
vishnu.psec94fc32014-10-09 15:30:23 -0700522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/*
vishnu.psec94fc32014-10-09 15:30:23 -0700524 * setuid() is implemented like SysV with SAVED_IDS
525 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 * Note that SAVED_ID's is deficient in that a setuid root program
vishnu.psec94fc32014-10-09 15:30:23 -0700527 * like sendmail, for example, cannot set its uid to be a normal
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 * user and then switch back, because if you're root, setuid() sets
529 * the saved uid too. If you don't like this, blame the bright people
530 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
531 * will allow a root program to temporarily drop privileges and be able to
vishnu.psec94fc32014-10-09 15:30:23 -0700532 * regain them by swapping the real and effective uid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100534SYSCALL_DEFINE1(setuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800536 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100537 const struct cred *old;
538 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800540 kuid_t kuid;
541
542 kuid = make_kuid(ns, uid);
543 if (!uid_valid(kuid))
544 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
David Howellsd84f4f92008-11-14 10:39:23 +1100546 new = prepare_creds();
547 if (!new)
548 return -ENOMEM;
549 old = current_cred();
550
David Howellsd84f4f92008-11-14 10:39:23 +1100551 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700552 if (ns_capable(old->user_ns, CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800553 new->suid = new->uid = kuid;
554 if (!uid_eq(kuid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530555 retval = set_user(new);
556 if (retval < 0)
557 goto error;
David Howellsd84f4f92008-11-14 10:39:23 +1100558 }
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800559 } else if (!uid_eq(kuid, old->uid) && !uid_eq(kuid, new->suid)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100560 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800563 new->fsuid = new->euid = kuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
David Howellsd84f4f92008-11-14 10:39:23 +1100565 retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
566 if (retval < 0)
567 goto error;
568
569 return commit_creds(new);
570
571error:
572 abort_creds(new);
573 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576
577/*
578 * This function implements a generic ability to update ruid, euid,
579 * and suid. This allows you to implement the 4.4 compatible seteuid().
580 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100581SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800583 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100584 const struct cred *old;
585 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800587 kuid_t kruid, keuid, ksuid;
588
589 kruid = make_kuid(ns, ruid);
590 keuid = make_kuid(ns, euid);
591 ksuid = make_kuid(ns, suid);
592
593 if ((ruid != (uid_t) -1) && !uid_valid(kruid))
594 return -EINVAL;
595
596 if ((euid != (uid_t) -1) && !uid_valid(keuid))
597 return -EINVAL;
598
599 if ((suid != (uid_t) -1) && !uid_valid(ksuid))
600 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
David Howellsd84f4f92008-11-14 10:39:23 +1100602 new = prepare_creds();
603 if (!new)
604 return -ENOMEM;
605
David Howellsd84f4f92008-11-14 10:39:23 +1100606 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
David Howellsd84f4f92008-11-14 10:39:23 +1100608 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700609 if (!ns_capable(old->user_ns, CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800610 if (ruid != (uid_t) -1 && !uid_eq(kruid, old->uid) &&
611 !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100612 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800613 if (euid != (uid_t) -1 && !uid_eq(keuid, old->uid) &&
614 !uid_eq(keuid, old->euid) && !uid_eq(keuid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100615 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800616 if (suid != (uid_t) -1 && !uid_eq(ksuid, old->uid) &&
617 !uid_eq(ksuid, old->euid) && !uid_eq(ksuid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100618 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
David Howellsd84f4f92008-11-14 10:39:23 +1100620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (ruid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800622 new->uid = kruid;
623 if (!uid_eq(kruid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530624 retval = set_user(new);
625 if (retval < 0)
626 goto error;
627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
David Howellsd84f4f92008-11-14 10:39:23 +1100629 if (euid != (uid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800630 new->euid = keuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (suid != (uid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800632 new->suid = ksuid;
David Howellsd84f4f92008-11-14 10:39:23 +1100633 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
David Howellsd84f4f92008-11-14 10:39:23 +1100635 retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
636 if (retval < 0)
637 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
David Howellsd84f4f92008-11-14 10:39:23 +1100639 return commit_creds(new);
640
641error:
642 abort_creds(new);
643 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800646SYSCALL_DEFINE3(getresuid, uid_t __user *, ruidp, uid_t __user *, euidp, uid_t __user *, suidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
David Howells86a264a2008-11-14 10:39:18 +1100648 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800650 uid_t ruid, euid, suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800652 ruid = from_kuid_munged(cred->user_ns, cred->uid);
653 euid = from_kuid_munged(cred->user_ns, cred->euid);
654 suid = from_kuid_munged(cred->user_ns, cred->suid);
655
vishnu.psec94fc32014-10-09 15:30:23 -0700656 retval = put_user(ruid, ruidp);
657 if (!retval) {
658 retval = put_user(euid, euidp);
659 if (!retval)
660 return put_user(suid, suidp);
661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return retval;
663}
664
665/*
666 * Same as above, but for rgid, egid, sgid.
667 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100668SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800670 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100671 const struct cred *old;
672 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800674 kgid_t krgid, kegid, ksgid;
675
676 krgid = make_kgid(ns, rgid);
677 kegid = make_kgid(ns, egid);
678 ksgid = make_kgid(ns, sgid);
679
680 if ((rgid != (gid_t) -1) && !gid_valid(krgid))
681 return -EINVAL;
682 if ((egid != (gid_t) -1) && !gid_valid(kegid))
683 return -EINVAL;
684 if ((sgid != (gid_t) -1) && !gid_valid(ksgid))
685 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
David Howellsd84f4f92008-11-14 10:39:23 +1100687 new = prepare_creds();
688 if (!new)
689 return -ENOMEM;
690 old = current_cred();
691
David Howellsd84f4f92008-11-14 10:39:23 +1100692 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700693 if (!ns_capable(old->user_ns, CAP_SETGID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800694 if (rgid != (gid_t) -1 && !gid_eq(krgid, old->gid) &&
695 !gid_eq(krgid, old->egid) && !gid_eq(krgid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100696 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800697 if (egid != (gid_t) -1 && !gid_eq(kegid, old->gid) &&
698 !gid_eq(kegid, old->egid) && !gid_eq(kegid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100699 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800700 if (sgid != (gid_t) -1 && !gid_eq(ksgid, old->gid) &&
701 !gid_eq(ksgid, old->egid) && !gid_eq(ksgid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100702 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
David Howellsd84f4f92008-11-14 10:39:23 +1100705 if (rgid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800706 new->gid = krgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100707 if (egid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800708 new->egid = kegid;
David Howellsd84f4f92008-11-14 10:39:23 +1100709 if (sgid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800710 new->sgid = ksgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100711 new->fsgid = new->egid;
712
713 return commit_creds(new);
714
715error:
716 abort_creds(new);
717 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800720SYSCALL_DEFINE3(getresgid, gid_t __user *, rgidp, gid_t __user *, egidp, gid_t __user *, sgidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
David Howells86a264a2008-11-14 10:39:18 +1100722 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800724 gid_t rgid, egid, sgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800726 rgid = from_kgid_munged(cred->user_ns, cred->gid);
727 egid = from_kgid_munged(cred->user_ns, cred->egid);
728 sgid = from_kgid_munged(cred->user_ns, cred->sgid);
729
vishnu.psec94fc32014-10-09 15:30:23 -0700730 retval = put_user(rgid, rgidp);
731 if (!retval) {
732 retval = put_user(egid, egidp);
733 if (!retval)
734 retval = put_user(sgid, sgidp);
735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 return retval;
738}
739
740
741/*
742 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
743 * is used for "access()" and for the NFS daemon (letting nfsd stay at
744 * whatever uid it wants to). It normally shadows "euid", except when
745 * explicitly set by setfsuid() or for access..
746 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100747SYSCALL_DEFINE1(setfsuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
David Howellsd84f4f92008-11-14 10:39:23 +1100749 const struct cred *old;
750 struct cred *new;
751 uid_t old_fsuid;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800752 kuid_t kuid;
753
754 old = current_cred();
755 old_fsuid = from_kuid_munged(old->user_ns, old->fsuid);
756
757 kuid = make_kuid(old->user_ns, uid);
758 if (!uid_valid(kuid))
759 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
David Howellsd84f4f92008-11-14 10:39:23 +1100761 new = prepare_creds();
762 if (!new)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800763 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800765 if (uid_eq(kuid, old->uid) || uid_eq(kuid, old->euid) ||
766 uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700767 ns_capable(old->user_ns, CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800768 if (!uid_eq(kuid, old->fsuid)) {
769 new->fsuid = kuid;
David Howellsd84f4f92008-11-14 10:39:23 +1100770 if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
771 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774
David Howellsd84f4f92008-11-14 10:39:23 +1100775 abort_creds(new);
776 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
David Howellsd84f4f92008-11-14 10:39:23 +1100778change_okay:
779 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return old_fsuid;
781}
782
783/*
John Anthony Kazos Jrf42df9e2007-05-09 08:23:08 +0200784 * Samma på svenska..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100786SYSCALL_DEFINE1(setfsgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
David Howellsd84f4f92008-11-14 10:39:23 +1100788 const struct cred *old;
789 struct cred *new;
790 gid_t old_fsgid;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800791 kgid_t kgid;
792
793 old = current_cred();
794 old_fsgid = from_kgid_munged(old->user_ns, old->fsgid);
795
796 kgid = make_kgid(old->user_ns, gid);
797 if (!gid_valid(kgid))
798 return old_fsgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
David Howellsd84f4f92008-11-14 10:39:23 +1100800 new = prepare_creds();
801 if (!new)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800802 return old_fsgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100803
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800804 if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->egid) ||
805 gid_eq(kgid, old->sgid) || gid_eq(kgid, old->fsgid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700806 ns_capable(old->user_ns, CAP_SETGID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800807 if (!gid_eq(kgid, old->fsgid)) {
808 new->fsgid = kgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100809 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
David Howellsd84f4f92008-11-14 10:39:23 +1100812
David Howellsd84f4f92008-11-14 10:39:23 +1100813 abort_creds(new);
814 return old_fsgid;
815
816change_okay:
817 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return old_fsgid;
819}
Iulia Manda28138932015-04-15 16:16:41 -0700820#endif /* CONFIG_MULTIUSER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Stephen Rothwell4a22f162013-04-30 15:27:37 -0700822/**
823 * sys_getpid - return the thread group id of the current process
824 *
825 * Note, despite the name, this returns the tgid not the pid. The tgid and
826 * the pid are identical unless CLONE_THREAD was specified on clone() in
827 * which case the tgid is the same in all threads of the same group.
828 *
829 * This is SMP safe as current->tgid does not change.
830 */
831SYSCALL_DEFINE0(getpid)
832{
833 return task_tgid_vnr(current);
834}
835
836/* Thread ID - the internal kernel "pid" */
837SYSCALL_DEFINE0(gettid)
838{
839 return task_pid_vnr(current);
840}
841
842/*
843 * Accessing ->real_parent is not SMP-safe, it could
844 * change from under us. However, we can use a stale
845 * value of ->real_parent under rcu_read_lock(), see
846 * release_task()->call_rcu(delayed_put_task_struct).
847 */
848SYSCALL_DEFINE0(getppid)
849{
850 int pid;
851
852 rcu_read_lock();
853 pid = task_tgid_vnr(rcu_dereference(current->real_parent));
854 rcu_read_unlock();
855
856 return pid;
857}
858
859SYSCALL_DEFINE0(getuid)
860{
861 /* Only we change this so SMP safe */
862 return from_kuid_munged(current_user_ns(), current_uid());
863}
864
865SYSCALL_DEFINE0(geteuid)
866{
867 /* Only we change this so SMP safe */
868 return from_kuid_munged(current_user_ns(), current_euid());
869}
870
871SYSCALL_DEFINE0(getgid)
872{
873 /* Only we change this so SMP safe */
874 return from_kgid_munged(current_user_ns(), current_gid());
875}
876
877SYSCALL_DEFINE0(getegid)
878{
879 /* Only we change this so SMP safe */
880 return from_kgid_munged(current_user_ns(), current_egid());
881}
882
Frank Mayharf06febc2008-09-12 09:54:39 -0700883void do_sys_times(struct tms *tms)
884{
Frederic Weisbecker5613fda2017-01-31 04:09:23 +0100885 u64 tgutime, tgstime, cutime, cstime;
Frank Mayharf06febc2008-09-12 09:54:39 -0700886
Frederic Weisbeckere80d0a1a2012-11-21 16:26:44 +0100887 thread_group_cputime_adjusted(current, &tgutime, &tgstime);
Frank Mayharf06febc2008-09-12 09:54:39 -0700888 cutime = current->signal->cutime;
889 cstime = current->signal->cstime;
Frederic Weisbecker5613fda2017-01-31 04:09:23 +0100890 tms->tms_utime = nsec_to_clock_t(tgutime);
891 tms->tms_stime = nsec_to_clock_t(tgstime);
892 tms->tms_cutime = nsec_to_clock_t(cutime);
893 tms->tms_cstime = nsec_to_clock_t(cstime);
Frank Mayharf06febc2008-09-12 09:54:39 -0700894}
895
Heiko Carstens58fd3aa2009-01-14 14:14:03 +0100896SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (tbuf) {
899 struct tms tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Frank Mayharf06febc2008-09-12 09:54:39 -0700901 do_sys_times(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
903 return -EFAULT;
904 }
Paul Mackerrase3d5a272009-01-06 14:41:02 -0800905 force_successful_syscall_return();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return (long) jiffies_64_to_clock_t(get_jiffies_64());
907}
908
909/*
910 * This needs some heavy checking ...
911 * I just haven't the stomach for it. I also don't fully
912 * understand sessions/pgrp etc. Let somebody who does explain it.
913 *
914 * OK, I think I have the protection semantics right.... this is really
915 * only important on a multi-user system anyway, to make sure one user
916 * can't send a signal to a process owned by another. -TYT, 12/12/91
917 *
Oleg Nesterov98611e42014-01-23 15:55:52 -0800918 * !PF_FORKNOEXEC check to conform completely to POSIX.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +0100920SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 struct task_struct *p;
Oleg Nesterovee0acf92006-01-08 01:03:53 -0800923 struct task_struct *group_leader = current->group_leader;
Oleg Nesterov4e021302008-02-08 04:19:08 -0800924 struct pid *pgrp;
925 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 if (!pid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700928 pid = task_pid_vnr(group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (!pgid)
930 pgid = pid;
931 if (pgid < 0)
932 return -EINVAL;
Paul E. McKenney950eaac2010-08-31 17:00:18 -0700933 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 /* From this point forward we keep holding onto the tasklist lock
936 * so that our parent does not change from under us. -DaveM
937 */
938 write_lock_irq(&tasklist_lock);
939
940 err = -ESRCH;
Oleg Nesterov4e021302008-02-08 04:19:08 -0800941 p = find_task_by_vpid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (!p)
943 goto out;
944
945 err = -EINVAL;
946 if (!thread_group_leader(p))
947 goto out;
948
Oleg Nesterov4e021302008-02-08 04:19:08 -0800949 if (same_thread_group(p->real_parent, group_leader)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 err = -EPERM;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800951 if (task_session(p) != task_session(group_leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 goto out;
953 err = -EACCES;
Oleg Nesterov98611e42014-01-23 15:55:52 -0800954 if (!(p->flags & PF_FORKNOEXEC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 goto out;
956 } else {
957 err = -ESRCH;
Oleg Nesterovee0acf92006-01-08 01:03:53 -0800958 if (p != group_leader)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 goto out;
960 }
961
962 err = -EPERM;
963 if (p->signal->leader)
964 goto out;
965
Oleg Nesterov4e021302008-02-08 04:19:08 -0800966 pgrp = task_pid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (pgid != pid) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700968 struct task_struct *g;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Oleg Nesterov4e021302008-02-08 04:19:08 -0800970 pgrp = find_vpid(pgid);
971 g = pid_task(pgrp, PIDTYPE_PGID);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800972 if (!g || task_session(g) != task_session(group_leader))
Oleg Nesterovf020bc42006-12-08 02:38:02 -0800973 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 err = security_task_setpgid(p, pgid);
977 if (err)
978 goto out;
979
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -0700980 if (task_pgrp(p) != pgrp)
Oleg Nesterov83beaf32008-04-30 00:54:27 -0700981 change_pid(p, PIDTYPE_PGID, pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 err = 0;
984out:
985 /* All paths lead to here, thus we are safe. -DaveM */
986 write_unlock_irq(&tasklist_lock);
Paul E. McKenney950eaac2010-08-31 17:00:18 -0700987 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return err;
989}
990
Heiko Carstensdbf040d2009-01-14 14:14:04 +0100991SYSCALL_DEFINE1(getpgid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -0700993 struct task_struct *p;
994 struct pid *grp;
995 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -0700997 rcu_read_lock();
998 if (!pid)
999 grp = task_pgrp(current);
1000 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 retval = -ESRCH;
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001002 p = find_task_by_vpid(pid);
1003 if (!p)
1004 goto out;
1005 grp = task_pgrp(p);
1006 if (!grp)
1007 goto out;
1008
1009 retval = security_task_getpgid(p);
1010 if (retval)
1011 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001013 retval = pid_vnr(grp);
1014out:
1015 rcu_read_unlock();
1016 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019#ifdef __ARCH_WANT_SYS_GETPGRP
1020
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001021SYSCALL_DEFINE0(getpgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001023 return sys_getpgid(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
1026#endif
1027
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001028SYSCALL_DEFINE1(getsid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001030 struct task_struct *p;
1031 struct pid *sid;
1032 int retval;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001033
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001034 rcu_read_lock();
1035 if (!pid)
1036 sid = task_session(current);
1037 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 retval = -ESRCH;
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001039 p = find_task_by_vpid(pid);
1040 if (!p)
1041 goto out;
1042 sid = task_session(p);
1043 if (!sid)
1044 goto out;
1045
1046 retval = security_task_getsid(p);
1047 if (retval)
1048 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001050 retval = pid_vnr(sid);
1051out:
1052 rcu_read_unlock();
1053 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
Oleg Nesterov81dabb42013-07-03 15:08:26 -07001056static void set_special_pids(struct pid *pid)
1057{
1058 struct task_struct *curr = current->group_leader;
1059
1060 if (task_session(curr) != pid)
1061 change_pid(curr, PIDTYPE_SID, pid);
1062
1063 if (task_pgrp(curr) != pid)
1064 change_pid(curr, PIDTYPE_PGID, pid);
1065}
1066
Heiko Carstensb290ebe2009-01-14 14:14:06 +01001067SYSCALL_DEFINE0(setsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Oren Laadane19f2472006-01-08 01:03:58 -08001069 struct task_struct *group_leader = current->group_leader;
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001070 struct pid *sid = task_pid(group_leader);
1071 pid_t session = pid_vnr(sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 int err = -EPERM;
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 write_lock_irq(&tasklist_lock);
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001075 /* Fail if I am already a session leader */
1076 if (group_leader->signal->leader)
1077 goto out;
1078
Oleg Nesterov430c6232008-02-08 04:19:11 -08001079 /* Fail if a process group id already exists that equals the
1080 * proposed session id.
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001081 */
Oleg Nesterov6806aac2008-02-08 04:19:12 -08001082 if (pid_task(sid, PIDTYPE_PGID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 goto out;
1084
Oren Laadane19f2472006-01-08 01:03:58 -08001085 group_leader->signal->leader = 1;
Oleg Nesterov81dabb42013-07-03 15:08:26 -07001086 set_special_pids(sid);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001087
Alan Cox9c9f4de2008-10-13 10:37:26 +01001088 proc_clear_tty(group_leader);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001089
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001090 err = session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091out:
1092 write_unlock_irq(&tasklist_lock);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001093 if (err > 0) {
Christian Borntraeger0d0df592009-10-26 16:49:34 -07001094 proc_sid_connector(group_leader);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001095 sched_autogroup_create_attach(group_leader);
1096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return err;
1098}
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100DECLARE_RWSEM(uts_sem);
1101
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001102#ifdef COMPAT_UTS_MACHINE
1103#define override_architecture(name) \
Andreas Schwab46da2762010-04-23 13:17:44 -04001104 (personality(current->personality) == PER_LINUX32 && \
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001105 copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1106 sizeof(COMPAT_UTS_MACHINE)))
1107#else
1108#define override_architecture(name) 0
1109#endif
1110
Andi Kleenbe274252011-08-19 16:15:10 -07001111/*
1112 * Work around broken programs that cannot handle "Linux 3.0".
1113 * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40
Jon DeVree39afb5e2015-02-27 15:52:07 -08001114 * And we map 4.x to 2.6.60+x, so 4.0 would be 2.6.60.
Andi Kleenbe274252011-08-19 16:15:10 -07001115 */
Kees Cook2702b152012-10-19 13:56:51 -07001116static int override_release(char __user *release, size_t len)
Andi Kleenbe274252011-08-19 16:15:10 -07001117{
1118 int ret = 0;
Andi Kleenbe274252011-08-19 16:15:10 -07001119
1120 if (current->personality & UNAME26) {
Kees Cook2702b152012-10-19 13:56:51 -07001121 const char *rest = UTS_RELEASE;
1122 char buf[65] = { 0 };
Andi Kleenbe274252011-08-19 16:15:10 -07001123 int ndots = 0;
1124 unsigned v;
Kees Cook2702b152012-10-19 13:56:51 -07001125 size_t copy;
Andi Kleenbe274252011-08-19 16:15:10 -07001126
1127 while (*rest) {
1128 if (*rest == '.' && ++ndots >= 3)
1129 break;
1130 if (!isdigit(*rest) && *rest != '.')
1131 break;
1132 rest++;
1133 }
Jon DeVree39afb5e2015-02-27 15:52:07 -08001134 v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 60;
Kees Cook31fd84b92012-10-19 18:45:53 -07001135 copy = clamp_t(size_t, len, 1, sizeof(buf));
Kees Cook2702b152012-10-19 13:56:51 -07001136 copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
1137 ret = copy_to_user(release, buf, copy + 1);
Andi Kleenbe274252011-08-19 16:15:10 -07001138 }
1139 return ret;
1140}
1141
Heiko Carstense48fbb62009-01-14 14:14:26 +01001142SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
1144 int errno = 0;
1145
1146 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001147 if (copy_to_user(name, utsname(), sizeof *name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 errno = -EFAULT;
1149 up_read(&uts_sem);
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001150
Andi Kleenbe274252011-08-19 16:15:10 -07001151 if (!errno && override_release(name->release, sizeof(name->release)))
1152 errno = -EFAULT;
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001153 if (!errno && override_architecture(name))
1154 errno = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return errno;
1156}
1157
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001158#ifdef __ARCH_WANT_SYS_OLD_UNAME
1159/*
1160 * Old cruft
1161 */
1162SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
1163{
1164 int error = 0;
1165
1166 if (!name)
1167 return -EFAULT;
1168
1169 down_read(&uts_sem);
1170 if (copy_to_user(name, utsname(), sizeof(*name)))
1171 error = -EFAULT;
1172 up_read(&uts_sem);
1173
Andi Kleenbe274252011-08-19 16:15:10 -07001174 if (!error && override_release(name->release, sizeof(name->release)))
1175 error = -EFAULT;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001176 if (!error && override_architecture(name))
1177 error = -EFAULT;
1178 return error;
1179}
1180
1181SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
1182{
1183 int error;
1184
1185 if (!name)
1186 return -EFAULT;
1187 if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
1188 return -EFAULT;
1189
1190 down_read(&uts_sem);
1191 error = __copy_to_user(&name->sysname, &utsname()->sysname,
1192 __OLD_UTS_LEN);
1193 error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
1194 error |= __copy_to_user(&name->nodename, &utsname()->nodename,
1195 __OLD_UTS_LEN);
1196 error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
1197 error |= __copy_to_user(&name->release, &utsname()->release,
1198 __OLD_UTS_LEN);
1199 error |= __put_user(0, name->release + __OLD_UTS_LEN);
1200 error |= __copy_to_user(&name->version, &utsname()->version,
1201 __OLD_UTS_LEN);
1202 error |= __put_user(0, name->version + __OLD_UTS_LEN);
1203 error |= __copy_to_user(&name->machine, &utsname()->machine,
1204 __OLD_UTS_LEN);
1205 error |= __put_user(0, name->machine + __OLD_UTS_LEN);
1206 up_read(&uts_sem);
1207
1208 if (!error && override_architecture(name))
1209 error = -EFAULT;
Andi Kleenbe274252011-08-19 16:15:10 -07001210 if (!error && override_release(name->release, sizeof(name->release)))
1211 error = -EFAULT;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001212 return error ? -EFAULT : 0;
1213}
1214#endif
1215
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001216SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 int errno;
1219 char tmp[__NEW_UTS_LEN];
1220
Serge E. Hallynbb96a6f52011-03-23 16:43:18 -07001221 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (len < 0 || len > __NEW_UTS_LEN)
1225 return -EINVAL;
1226 down_write(&uts_sem);
1227 errno = -EFAULT;
1228 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001229 struct new_utsname *u = utsname();
1230
1231 memcpy(u->nodename, tmp, len);
1232 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 errno = 0;
Sasikantha babu499eea62012-05-31 16:26:07 -07001234 uts_proc_notify(UTS_PROC_HOSTNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236 up_write(&uts_sem);
1237 return errno;
1238}
1239
1240#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1241
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001242SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 int i, errno;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001245 struct new_utsname *u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 if (len < 0)
1248 return -EINVAL;
1249 down_read(&uts_sem);
Andrew Morton9679e4d2008-10-15 22:01:51 -07001250 u = utsname();
1251 i = 1 + strlen(u->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (i > len)
1253 i = len;
1254 errno = 0;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001255 if (copy_to_user(name, u->nodename, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 errno = -EFAULT;
1257 up_read(&uts_sem);
1258 return errno;
1259}
1260
1261#endif
1262
1263/*
1264 * Only setdomainname; getdomainname can be implemented by calling
1265 * uname()
1266 */
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001267SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 int errno;
1270 char tmp[__NEW_UTS_LEN];
1271
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001272 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return -EPERM;
1274 if (len < 0 || len > __NEW_UTS_LEN)
1275 return -EINVAL;
1276
1277 down_write(&uts_sem);
1278 errno = -EFAULT;
1279 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001280 struct new_utsname *u = utsname();
1281
1282 memcpy(u->domainname, tmp, len);
1283 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 errno = 0;
Sasikantha babu499eea62012-05-31 16:26:07 -07001285 uts_proc_notify(UTS_PROC_DOMAINNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
1287 up_write(&uts_sem);
1288 return errno;
1289}
1290
Heiko Carstense48fbb62009-01-14 14:14:26 +01001291SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Jiri Slabyb9518342010-05-04 11:28:25 +02001293 struct rlimit value;
1294 int ret;
1295
1296 ret = do_prlimit(current, resource, NULL, &value);
1297 if (!ret)
1298 ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1299
1300 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
1303#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1304
1305/*
1306 * Back compatibility for getrlimit. Needed for some apps.
1307 */
Heiko Carstense48fbb62009-01-14 14:14:26 +01001308SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1309 struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 struct rlimit x;
1312 if (resource >= RLIM_NLIMITS)
1313 return -EINVAL;
1314
1315 task_lock(current->group_leader);
1316 x = current->signal->rlim[resource];
1317 task_unlock(current->group_leader);
Cal Peake756184b2006-09-30 23:27:24 -07001318 if (x.rlim_cur > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 x.rlim_cur = 0x7FFFFFFF;
Cal Peake756184b2006-09-30 23:27:24 -07001320 if (x.rlim_max > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 x.rlim_max = 0x7FFFFFFF;
vishnu.psec94fc32014-10-09 15:30:23 -07001322 return copy_to_user(rlim, &x, sizeof(x)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
1325#endif
1326
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001327static inline bool rlim64_is_infinity(__u64 rlim64)
1328{
1329#if BITS_PER_LONG < 64
1330 return rlim64 >= ULONG_MAX;
1331#else
1332 return rlim64 == RLIM64_INFINITY;
1333#endif
1334}
1335
1336static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1337{
1338 if (rlim->rlim_cur == RLIM_INFINITY)
1339 rlim64->rlim_cur = RLIM64_INFINITY;
1340 else
1341 rlim64->rlim_cur = rlim->rlim_cur;
1342 if (rlim->rlim_max == RLIM_INFINITY)
1343 rlim64->rlim_max = RLIM64_INFINITY;
1344 else
1345 rlim64->rlim_max = rlim->rlim_max;
1346}
1347
1348static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1349{
1350 if (rlim64_is_infinity(rlim64->rlim_cur))
1351 rlim->rlim_cur = RLIM_INFINITY;
1352 else
1353 rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1354 if (rlim64_is_infinity(rlim64->rlim_max))
1355 rlim->rlim_max = RLIM_INFINITY;
1356 else
1357 rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1358}
1359
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001360/* make sure you are allowed to change @tsk limits before calling this */
Jiri Slaby5b415352010-03-24 16:11:29 +01001361int do_prlimit(struct task_struct *tsk, unsigned int resource,
1362 struct rlimit *new_rlim, struct rlimit *old_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Jiri Slaby5b415352010-03-24 16:11:29 +01001364 struct rlimit *rlim;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001365 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 if (resource >= RLIM_NLIMITS)
1368 return -EINVAL;
Jiri Slaby5b415352010-03-24 16:11:29 +01001369 if (new_rlim) {
1370 if (new_rlim->rlim_cur > new_rlim->rlim_max)
1371 return -EINVAL;
1372 if (resource == RLIMIT_NOFILE &&
1373 new_rlim->rlim_max > sysctl_nr_open)
1374 return -EPERM;
1375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001377 /* protect tsk->signal and tsk->sighand from disappearing */
1378 read_lock(&tasklist_lock);
1379 if (!tsk->sighand) {
1380 retval = -ESRCH;
1381 goto out;
1382 }
1383
Jiri Slaby5b415352010-03-24 16:11:29 +01001384 rlim = tsk->signal->rlim + resource;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001385 task_lock(tsk->group_leader);
Jiri Slaby5b415352010-03-24 16:11:29 +01001386 if (new_rlim) {
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001387 /* Keep the capable check against init_user_ns until
1388 cgroups can contain all limits */
Jiri Slaby5b415352010-03-24 16:11:29 +01001389 if (new_rlim->rlim_max > rlim->rlim_max &&
1390 !capable(CAP_SYS_RESOURCE))
1391 retval = -EPERM;
1392 if (!retval)
1393 retval = security_task_setrlimit(tsk->group_leader,
1394 resource, new_rlim);
1395 if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
1396 /*
1397 * The caller is asking for an immediate RLIMIT_CPU
1398 * expiry. But we use the zero value to mean "it was
1399 * never set". So let's cheat and make it one second
1400 * instead
1401 */
1402 new_rlim->rlim_cur = 1;
1403 }
Tom Alsberg9926e4c2007-05-08 00:30:31 -07001404 }
Jiri Slaby5b415352010-03-24 16:11:29 +01001405 if (!retval) {
1406 if (old_rlim)
1407 *old_rlim = *rlim;
1408 if (new_rlim)
1409 *rlim = *new_rlim;
1410 }
Jiri Slaby7855c352009-08-26 23:45:34 +02001411 task_unlock(tsk->group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Andrew Mortond3561f72006-03-24 03:18:36 -08001413 /*
1414 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1415 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1416 * very long-standing error, and fixing it now risks breakage of
1417 * applications, so we live with it
1418 */
Jiri Slaby5b415352010-03-24 16:11:29 +01001419 if (!retval && new_rlim && resource == RLIMIT_CPU &&
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05001420 new_rlim->rlim_cur != RLIM_INFINITY &&
1421 IS_ENABLED(CONFIG_POSIX_TIMERS))
Jiri Slaby5b415352010-03-24 16:11:29 +01001422 update_rlimit_cpu(tsk, new_rlim->rlim_cur);
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001423out:
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001424 read_unlock(&tasklist_lock);
Oleg Nesterov2fb9d262009-09-03 19:21:45 +02001425 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001428/* rcu lock must be held */
1429static int check_prlimit_permission(struct task_struct *task)
1430{
1431 const struct cred *cred = current_cred(), *tcred;
1432
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001433 if (current == task)
1434 return 0;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001435
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001436 tcred = __task_cred(task);
Eric W. Biederman5af66202012-03-03 20:21:47 -08001437 if (uid_eq(cred->uid, tcred->euid) &&
1438 uid_eq(cred->uid, tcred->suid) &&
1439 uid_eq(cred->uid, tcred->uid) &&
1440 gid_eq(cred->gid, tcred->egid) &&
1441 gid_eq(cred->gid, tcred->sgid) &&
1442 gid_eq(cred->gid, tcred->gid))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001443 return 0;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -08001444 if (ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001445 return 0;
1446
1447 return -EPERM;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001448}
1449
1450SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1451 const struct rlimit64 __user *, new_rlim,
1452 struct rlimit64 __user *, old_rlim)
1453{
1454 struct rlimit64 old64, new64;
1455 struct rlimit old, new;
1456 struct task_struct *tsk;
1457 int ret;
1458
1459 if (new_rlim) {
1460 if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1461 return -EFAULT;
1462 rlim64_to_rlim(&new64, &new);
1463 }
1464
1465 rcu_read_lock();
1466 tsk = pid ? find_task_by_vpid(pid) : current;
1467 if (!tsk) {
1468 rcu_read_unlock();
1469 return -ESRCH;
1470 }
1471 ret = check_prlimit_permission(tsk);
1472 if (ret) {
1473 rcu_read_unlock();
1474 return ret;
1475 }
1476 get_task_struct(tsk);
1477 rcu_read_unlock();
1478
1479 ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1480 old_rlim ? &old : NULL);
1481
1482 if (!ret && old_rlim) {
1483 rlim_to_rlim64(&old, &old64);
1484 if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1485 ret = -EFAULT;
1486 }
1487
1488 put_task_struct(tsk);
1489 return ret;
1490}
1491
Jiri Slaby7855c352009-08-26 23:45:34 +02001492SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1493{
1494 struct rlimit new_rlim;
1495
1496 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1497 return -EFAULT;
Jiri Slaby5b415352010-03-24 16:11:29 +01001498 return do_prlimit(current, resource, &new_rlim, NULL);
Jiri Slaby7855c352009-08-26 23:45:34 +02001499}
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501/*
1502 * It would make sense to put struct rusage in the task_struct,
1503 * except that would make the task_struct be *really big*. After
1504 * task_struct gets moved into malloc'ed memory, it would
1505 * make sense to do this. It will make moving the rest of the information
1506 * a lot simpler! (Which we're not doing right now because we're not
1507 * measuring them yet).
1508 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1510 * races with threads incrementing their own counters. But since word
1511 * reads are atomic, we either get new values or old values and we don't
1512 * care which for the sums. We always take the siglock to protect reading
1513 * the c* fields from p->signal from races with exit.c updating those
1514 * fields when reaping, so a sample either gets all the additions of a
1515 * given child after it's reaped, or none so this sample is before reaping.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001516 *
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001517 * Locking:
1518 * We need to take the siglock for CHILDEREN, SELF and BOTH
1519 * for the cases current multithreaded, non-current single threaded
1520 * non-current multithreaded. Thread traversal is now safe with
1521 * the siglock held.
1522 * Strictly speaking, we donot need to take the siglock if we are current and
1523 * single threaded, as no one else can take our signal_struct away, no one
1524 * else can reap the children to update signal->c* counters, and no one else
1525 * can race with the signal-> fields. If we do not take any lock, the
1526 * signal-> fields could be read out of order while another thread was just
1527 * exiting. So we should place a read memory barrier when we avoid the lock.
1528 * On the writer side, write memory barrier is implied in __exit_signal
1529 * as __exit_signal releases the siglock spinlock after updating the signal->
1530 * fields. But we don't do this yet to keep things simple.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001531 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 */
1533
Frank Mayharf06febc2008-09-12 09:54:39 -07001534static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001535{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001536 r->ru_nvcsw += t->nvcsw;
1537 r->ru_nivcsw += t->nivcsw;
1538 r->ru_minflt += t->min_flt;
1539 r->ru_majflt += t->maj_flt;
1540 r->ru_inblock += task_io_get_inblock(t);
1541 r->ru_oublock += task_io_get_oublock(t);
1542}
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1545{
1546 struct task_struct *t;
1547 unsigned long flags;
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001548 u64 tgutime, tgstime, utime, stime;
Jiri Pirko1f102062009-09-22 16:44:10 -07001549 unsigned long maxrss = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
vishnu.psec94fc32014-10-09 15:30:23 -07001551 memset((char *)r, 0, sizeof (*r));
Martin Schwidefsky64861632011-12-15 14:56:09 +01001552 utime = stime = 0;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001553
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001554 if (who == RUSAGE_THREAD) {
Frederic Weisbeckere80d0a1a2012-11-21 16:26:44 +01001555 task_cputime_adjusted(current, &utime, &stime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001556 accumulate_thread_rusage(p, r);
Jiri Pirko1f102062009-09-22 16:44:10 -07001557 maxrss = p->signal->maxrss;
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001558 goto out;
1559 }
1560
Oleg Nesterovd6cf7232008-04-30 00:52:38 -07001561 if (!lock_task_sighand(p, &flags))
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001562 return;
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 switch (who) {
vishnu.psec94fc32014-10-09 15:30:23 -07001565 case RUSAGE_BOTH:
1566 case RUSAGE_CHILDREN:
1567 utime = p->signal->cutime;
1568 stime = p->signal->cstime;
1569 r->ru_nvcsw = p->signal->cnvcsw;
1570 r->ru_nivcsw = p->signal->cnivcsw;
1571 r->ru_minflt = p->signal->cmin_flt;
1572 r->ru_majflt = p->signal->cmaj_flt;
1573 r->ru_inblock = p->signal->cinblock;
1574 r->ru_oublock = p->signal->coublock;
1575 maxrss = p->signal->cmaxrss;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001576
vishnu.psec94fc32014-10-09 15:30:23 -07001577 if (who == RUSAGE_CHILDREN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 break;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001579
vishnu.psec94fc32014-10-09 15:30:23 -07001580 case RUSAGE_SELF:
1581 thread_group_cputime_adjusted(p, &tgutime, &tgstime);
1582 utime += tgutime;
1583 stime += tgstime;
1584 r->ru_nvcsw += p->signal->nvcsw;
1585 r->ru_nivcsw += p->signal->nivcsw;
1586 r->ru_minflt += p->signal->min_flt;
1587 r->ru_majflt += p->signal->maj_flt;
1588 r->ru_inblock += p->signal->inblock;
1589 r->ru_oublock += p->signal->oublock;
1590 if (maxrss < p->signal->maxrss)
1591 maxrss = p->signal->maxrss;
1592 t = p;
1593 do {
1594 accumulate_thread_rusage(t, r);
1595 } while_each_thread(p, t);
1596 break;
1597
1598 default:
1599 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001601 unlock_task_sighand(p, &flags);
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001602
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001603out:
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001604 r->ru_utime = ns_to_timeval(utime);
1605 r->ru_stime = ns_to_timeval(stime);
Jiri Pirko1f102062009-09-22 16:44:10 -07001606
1607 if (who != RUSAGE_CHILDREN) {
1608 struct mm_struct *mm = get_task_mm(p);
vishnu.psec94fc32014-10-09 15:30:23 -07001609
Jiri Pirko1f102062009-09-22 16:44:10 -07001610 if (mm) {
1611 setmax_mm_hiwater_rss(&maxrss, mm);
1612 mmput(mm);
1613 }
1614 }
1615 r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
1618int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1619{
1620 struct rusage r;
vishnu.psec94fc32014-10-09 15:30:23 -07001621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 k_getrusage(p, who, &r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1624}
1625
Heiko Carstense48fbb62009-01-14 14:14:26 +01001626SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001628 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1629 who != RUSAGE_THREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return -EINVAL;
1631 return getrusage(current, who, ru);
1632}
1633
Al Viro8d2d5c42013-03-03 12:49:06 -05001634#ifdef CONFIG_COMPAT
1635COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru)
1636{
1637 struct rusage r;
1638
1639 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1640 who != RUSAGE_THREAD)
1641 return -EINVAL;
1642
1643 k_getrusage(current, who, &r);
1644 return put_compat_rusage(&r, ru);
1645}
1646#endif
1647
Heiko Carstense48fbb62009-01-14 14:14:26 +01001648SYSCALL_DEFINE1(umask, int, mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
1650 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1651 return mask;
1652}
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -08001653
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001654static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001655{
Al Viro2903ff02012-08-28 12:52:22 -04001656 struct fd exe;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001657 struct file *old_exe, *exe_file;
Al Viro496ad9a2013-01-23 17:07:38 -05001658 struct inode *inode;
Al Viro2903ff02012-08-28 12:52:22 -04001659 int err;
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001660
Al Viro2903ff02012-08-28 12:52:22 -04001661 exe = fdget(fd);
1662 if (!exe.file)
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001663 return -EBADF;
1664
Al Viro496ad9a2013-01-23 17:07:38 -05001665 inode = file_inode(exe.file);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001666
1667 /*
1668 * Because the original mm->exe_file points to executable file, make
1669 * sure that this one is executable as well, to avoid breaking an
1670 * overall picture.
1671 */
1672 err = -EACCES;
Eric W. Biederman90f85722015-06-29 14:42:03 -05001673 if (!S_ISREG(inode->i_mode) || path_noexec(&exe.file->f_path))
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001674 goto exit;
1675
Al Viro496ad9a2013-01-23 17:07:38 -05001676 err = inode_permission(inode, MAY_EXEC);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001677 if (err)
1678 goto exit;
1679
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001680 /*
Konstantin Khlebnikov4229fb1d2012-07-11 14:02:11 -07001681 * Forbid mm->exe_file change if old file still mapped.
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001682 */
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001683 exe_file = get_mm_exe_file(mm);
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001684 err = -EBUSY;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001685 if (exe_file) {
Konstantin Khlebnikov4229fb1d2012-07-11 14:02:11 -07001686 struct vm_area_struct *vma;
1687
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001688 down_read(&mm->mmap_sem);
1689 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1690 if (!vma->vm_file)
1691 continue;
1692 if (path_equal(&vma->vm_file->f_path,
1693 &exe_file->f_path))
1694 goto exit_err;
1695 }
1696
1697 up_read(&mm->mmap_sem);
1698 fput(exe_file);
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001699 }
1700
Konstantin Khlebnikov4229fb1d2012-07-11 14:02:11 -07001701 err = 0;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001702 /* set the new file, lockless */
1703 get_file(exe.file);
1704 old_exe = xchg(&mm->exe_file, exe.file);
1705 if (old_exe)
1706 fput(old_exe);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001707exit:
Al Viro2903ff02012-08-28 12:52:22 -04001708 fdput(exe);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001709 return err;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001710exit_err:
1711 up_read(&mm->mmap_sem);
1712 fput(exe_file);
1713 goto exit;
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001714}
1715
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001716/*
1717 * WARNING: we don't require any capability here so be very careful
1718 * in what is allowed for modification from userspace.
1719 */
1720static int validate_prctl_map(struct prctl_mm_map *prctl_map)
1721{
1722 unsigned long mmap_max_addr = TASK_SIZE;
1723 struct mm_struct *mm = current->mm;
1724 int error = -EINVAL, i;
1725
1726 static const unsigned char offsets[] = {
1727 offsetof(struct prctl_mm_map, start_code),
1728 offsetof(struct prctl_mm_map, end_code),
1729 offsetof(struct prctl_mm_map, start_data),
1730 offsetof(struct prctl_mm_map, end_data),
1731 offsetof(struct prctl_mm_map, start_brk),
1732 offsetof(struct prctl_mm_map, brk),
1733 offsetof(struct prctl_mm_map, start_stack),
1734 offsetof(struct prctl_mm_map, arg_start),
1735 offsetof(struct prctl_mm_map, arg_end),
1736 offsetof(struct prctl_mm_map, env_start),
1737 offsetof(struct prctl_mm_map, env_end),
1738 };
1739
1740 /*
1741 * Make sure the members are not somewhere outside
1742 * of allowed address space.
1743 */
1744 for (i = 0; i < ARRAY_SIZE(offsets); i++) {
1745 u64 val = *(u64 *)((char *)prctl_map + offsets[i]);
1746
1747 if ((unsigned long)val >= mmap_max_addr ||
1748 (unsigned long)val < mmap_min_addr)
1749 goto out;
1750 }
1751
1752 /*
1753 * Make sure the pairs are ordered.
1754 */
1755#define __prctl_check_order(__m1, __op, __m2) \
1756 ((unsigned long)prctl_map->__m1 __op \
1757 (unsigned long)prctl_map->__m2) ? 0 : -EINVAL
1758 error = __prctl_check_order(start_code, <, end_code);
1759 error |= __prctl_check_order(start_data, <, end_data);
1760 error |= __prctl_check_order(start_brk, <=, brk);
1761 error |= __prctl_check_order(arg_start, <=, arg_end);
1762 error |= __prctl_check_order(env_start, <=, env_end);
1763 if (error)
1764 goto out;
1765#undef __prctl_check_order
1766
1767 error = -EINVAL;
1768
1769 /*
1770 * @brk should be after @end_data in traditional maps.
1771 */
1772 if (prctl_map->start_brk <= prctl_map->end_data ||
1773 prctl_map->brk <= prctl_map->end_data)
1774 goto out;
1775
1776 /*
1777 * Neither we should allow to override limits if they set.
1778 */
1779 if (check_data_rlimit(rlimit(RLIMIT_DATA), prctl_map->brk,
1780 prctl_map->start_brk, prctl_map->end_data,
1781 prctl_map->start_data))
1782 goto out;
1783
1784 /*
1785 * Someone is trying to cheat the auxv vector.
1786 */
1787 if (prctl_map->auxv_size) {
1788 if (!prctl_map->auxv || prctl_map->auxv_size > sizeof(mm->saved_auxv))
1789 goto out;
1790 }
1791
1792 /*
1793 * Finally, make sure the caller has the rights to
1794 * change /proc/pid/exe link: only local root should
1795 * be allowed to.
1796 */
1797 if (prctl_map->exe_fd != (u32)-1) {
1798 struct user_namespace *ns = current_user_ns();
1799 const struct cred *cred = current_cred();
1800
1801 if (!uid_eq(cred->uid, make_kuid(ns, 0)) ||
1802 !gid_eq(cred->gid, make_kgid(ns, 0)))
1803 goto out;
1804 }
1805
1806 error = 0;
1807out:
1808 return error;
1809}
1810
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07001811#ifdef CONFIG_CHECKPOINT_RESTORE
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001812static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data_size)
1813{
1814 struct prctl_mm_map prctl_map = { .exe_fd = (u32)-1, };
1815 unsigned long user_auxv[AT_VECTOR_SIZE];
1816 struct mm_struct *mm = current->mm;
1817 int error;
1818
1819 BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
1820 BUILD_BUG_ON(sizeof(struct prctl_mm_map) > 256);
1821
1822 if (opt == PR_SET_MM_MAP_SIZE)
1823 return put_user((unsigned int)sizeof(prctl_map),
1824 (unsigned int __user *)addr);
1825
1826 if (data_size != sizeof(prctl_map))
1827 return -EINVAL;
1828
1829 if (copy_from_user(&prctl_map, addr, sizeof(prctl_map)))
1830 return -EFAULT;
1831
1832 error = validate_prctl_map(&prctl_map);
1833 if (error)
1834 return error;
1835
1836 if (prctl_map.auxv_size) {
1837 memset(user_auxv, 0, sizeof(user_auxv));
1838 if (copy_from_user(user_auxv,
1839 (const void __user *)prctl_map.auxv,
1840 prctl_map.auxv_size))
1841 return -EFAULT;
1842
1843 /* Last entry must be AT_NULL as specification requires */
1844 user_auxv[AT_VECTOR_SIZE - 2] = AT_NULL;
1845 user_auxv[AT_VECTOR_SIZE - 1] = AT_NULL;
1846 }
1847
Mateusz Guzikddf1d392016-01-20 15:01:02 -08001848 if (prctl_map.exe_fd != (u32)-1) {
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001849 error = prctl_set_mm_exe_file(mm, prctl_map.exe_fd);
Mateusz Guzikddf1d392016-01-20 15:01:02 -08001850 if (error)
1851 return error;
1852 }
1853
1854 down_write(&mm->mmap_sem);
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001855
1856 /*
1857 * We don't validate if these members are pointing to
1858 * real present VMAs because application may have correspond
1859 * VMAs already unmapped and kernel uses these members for statistics
1860 * output in procfs mostly, except
1861 *
1862 * - @start_brk/@brk which are used in do_brk but kernel lookups
1863 * for VMAs when updating these memvers so anything wrong written
1864 * here cause kernel to swear at userspace program but won't lead
1865 * to any problem in kernel itself
1866 */
1867
1868 mm->start_code = prctl_map.start_code;
1869 mm->end_code = prctl_map.end_code;
1870 mm->start_data = prctl_map.start_data;
1871 mm->end_data = prctl_map.end_data;
1872 mm->start_brk = prctl_map.start_brk;
1873 mm->brk = prctl_map.brk;
1874 mm->start_stack = prctl_map.start_stack;
1875 mm->arg_start = prctl_map.arg_start;
1876 mm->arg_end = prctl_map.arg_end;
1877 mm->env_start = prctl_map.env_start;
1878 mm->env_end = prctl_map.env_end;
1879
1880 /*
1881 * Note this update of @saved_auxv is lockless thus
1882 * if someone reads this member in procfs while we're
1883 * updating -- it may get partly updated results. It's
1884 * known and acceptable trade off: we leave it as is to
1885 * not introduce additional locks here making the kernel
1886 * more complex.
1887 */
1888 if (prctl_map.auxv_size)
1889 memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
1890
Mateusz Guzikddf1d392016-01-20 15:01:02 -08001891 up_write(&mm->mmap_sem);
1892 return 0;
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001893}
1894#endif /* CONFIG_CHECKPOINT_RESTORE */
1895
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07001896static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr,
1897 unsigned long len)
1898{
1899 /*
1900 * This doesn't move the auxiliary vector itself since it's pinned to
1901 * mm_struct, but it permits filling the vector with new values. It's
1902 * up to the caller to provide sane values here, otherwise userspace
1903 * tools which use this vector might be unhappy.
1904 */
1905 unsigned long user_auxv[AT_VECTOR_SIZE];
1906
1907 if (len > sizeof(user_auxv))
1908 return -EINVAL;
1909
1910 if (copy_from_user(user_auxv, (const void __user *)addr, len))
1911 return -EFAULT;
1912
1913 /* Make sure the last entry is always AT_NULL */
1914 user_auxv[AT_VECTOR_SIZE - 2] = 0;
1915 user_auxv[AT_VECTOR_SIZE - 1] = 0;
1916
1917 BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
1918
1919 task_lock(current);
1920 memcpy(mm->saved_auxv, user_auxv, len);
1921 task_unlock(current);
1922
1923 return 0;
1924}
1925
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001926static int prctl_set_mm(int opt, unsigned long addr,
1927 unsigned long arg4, unsigned long arg5)
1928{
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001929 struct mm_struct *mm = current->mm;
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07001930 struct prctl_mm_map prctl_map;
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07001931 struct vm_area_struct *vma;
1932 int error;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001933
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001934 if (arg5 || (arg4 && (opt != PR_SET_MM_AUXV &&
1935 opt != PR_SET_MM_MAP &&
1936 opt != PR_SET_MM_MAP_SIZE)))
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001937 return -EINVAL;
1938
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001939#ifdef CONFIG_CHECKPOINT_RESTORE
1940 if (opt == PR_SET_MM_MAP || opt == PR_SET_MM_MAP_SIZE)
1941 return prctl_set_mm_map(opt, (const void __user *)addr, arg4);
1942#endif
1943
Cyrill Gorcunov79f07132012-03-15 15:17:10 -07001944 if (!capable(CAP_SYS_RESOURCE))
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001945 return -EPERM;
1946
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001947 if (opt == PR_SET_MM_EXE_FILE)
1948 return prctl_set_mm_exe_file(mm, (unsigned int)addr);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001949
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07001950 if (opt == PR_SET_MM_AUXV)
1951 return prctl_set_auxv(mm, addr, arg4);
1952
Cyrill Gorcunov1ad75b92012-06-07 14:21:11 -07001953 if (addr >= TASK_SIZE || addr < mmap_min_addr)
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001954 return -EINVAL;
1955
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07001956 error = -EINVAL;
1957
Mateusz Guzikddf1d392016-01-20 15:01:02 -08001958 down_write(&mm->mmap_sem);
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001959 vma = find_vma(mm, addr);
1960
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07001961 prctl_map.start_code = mm->start_code;
1962 prctl_map.end_code = mm->end_code;
1963 prctl_map.start_data = mm->start_data;
1964 prctl_map.end_data = mm->end_data;
1965 prctl_map.start_brk = mm->start_brk;
1966 prctl_map.brk = mm->brk;
1967 prctl_map.start_stack = mm->start_stack;
1968 prctl_map.arg_start = mm->arg_start;
1969 prctl_map.arg_end = mm->arg_end;
1970 prctl_map.env_start = mm->env_start;
1971 prctl_map.env_end = mm->env_end;
1972 prctl_map.auxv = NULL;
1973 prctl_map.auxv_size = 0;
1974 prctl_map.exe_fd = -1;
1975
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001976 switch (opt) {
1977 case PR_SET_MM_START_CODE:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07001978 prctl_map.start_code = addr;
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07001979 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001980 case PR_SET_MM_END_CODE:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07001981 prctl_map.end_code = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001982 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001983 case PR_SET_MM_START_DATA:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07001984 prctl_map.start_data = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001985 break;
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07001986 case PR_SET_MM_END_DATA:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07001987 prctl_map.end_data = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001988 break;
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07001989 case PR_SET_MM_START_STACK:
1990 prctl_map.start_stack = addr;
1991 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001992 case PR_SET_MM_START_BRK:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07001993 prctl_map.start_brk = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001994 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001995 case PR_SET_MM_BRK:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07001996 prctl_map.brk = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001997 break;
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07001998 case PR_SET_MM_ARG_START:
1999 prctl_map.arg_start = addr;
2000 break;
2001 case PR_SET_MM_ARG_END:
2002 prctl_map.arg_end = addr;
2003 break;
2004 case PR_SET_MM_ENV_START:
2005 prctl_map.env_start = addr;
2006 break;
2007 case PR_SET_MM_ENV_END:
2008 prctl_map.env_end = addr;
2009 break;
2010 default:
2011 goto out;
2012 }
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002013
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002014 error = validate_prctl_map(&prctl_map);
2015 if (error)
2016 goto out;
2017
2018 switch (opt) {
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002019 /*
2020 * If command line arguments and environment
2021 * are placed somewhere else on stack, we can
2022 * set them up here, ARG_START/END to setup
2023 * command line argumets and ENV_START/END
2024 * for environment.
2025 */
2026 case PR_SET_MM_START_STACK:
2027 case PR_SET_MM_ARG_START:
2028 case PR_SET_MM_ARG_END:
2029 case PR_SET_MM_ENV_START:
2030 case PR_SET_MM_ENV_END:
2031 if (!vma) {
2032 error = -EFAULT;
2033 goto out;
2034 }
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002035 }
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002036
2037 mm->start_code = prctl_map.start_code;
2038 mm->end_code = prctl_map.end_code;
2039 mm->start_data = prctl_map.start_data;
2040 mm->end_data = prctl_map.end_data;
2041 mm->start_brk = prctl_map.start_brk;
2042 mm->brk = prctl_map.brk;
2043 mm->start_stack = prctl_map.start_stack;
2044 mm->arg_start = prctl_map.arg_start;
2045 mm->arg_end = prctl_map.arg_end;
2046 mm->env_start = prctl_map.env_start;
2047 mm->env_end = prctl_map.env_end;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002048
2049 error = 0;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002050out:
Mateusz Guzikddf1d392016-01-20 15:01:02 -08002051 up_write(&mm->mmap_sem);
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002052 return error;
2053}
Cyrill Gorcunov300f7862012-06-07 14:21:12 -07002054
Amnon Shiloh52b36942013-04-30 15:28:48 -07002055#ifdef CONFIG_CHECKPOINT_RESTORE
Cyrill Gorcunov300f7862012-06-07 14:21:12 -07002056static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
2057{
2058 return put_user(me->clear_child_tid, tid_addr);
2059}
Amnon Shiloh52b36942013-04-30 15:28:48 -07002060#else
Cyrill Gorcunov300f7862012-06-07 14:21:12 -07002061static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
2062{
2063 return -EINVAL;
2064}
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002065#endif
2066
Pavel Tikhomirov749860c2017-01-30 18:06:12 +03002067static int propagate_has_child_subreaper(struct task_struct *p, void *data)
2068{
2069 /*
2070 * If task has has_child_subreaper - all its decendants
2071 * already have these flag too and new decendants will
2072 * inherit it on fork, skip them.
2073 *
2074 * If we've found child_reaper - skip descendants in
2075 * it's subtree as they will never get out pidns.
2076 */
2077 if (p->signal->has_child_subreaper ||
2078 is_child_reaper(task_pid(p)))
2079 return 0;
2080
2081 p->signal->has_child_subreaper = 1;
2082 return 1;
2083}
2084
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01002085SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
2086 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
David Howellsb6dff3e2008-11-14 10:39:16 +11002088 struct task_struct *me = current;
2089 unsigned char comm[sizeof(me->comm)];
2090 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
David Howellsd84f4f92008-11-14 10:39:23 +11002092 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2093 if (error != -ENOSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 return error;
2095
David Howellsd84f4f92008-11-14 10:39:23 +11002096 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 switch (option) {
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002098 case PR_SET_PDEATHSIG:
2099 if (!valid_signal(arg2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 error = -EINVAL;
2101 break;
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002102 }
2103 me->pdeath_signal = arg2;
2104 break;
2105 case PR_GET_PDEATHSIG:
2106 error = put_user(me->pdeath_signal, (int __user *)arg2);
2107 break;
2108 case PR_GET_DUMPABLE:
2109 error = get_dumpable(me->mm);
2110 break;
2111 case PR_SET_DUMPABLE:
2112 if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) {
2113 error = -EINVAL;
2114 break;
2115 }
2116 set_dumpable(me->mm, arg2);
2117 break;
2118
2119 case PR_SET_UNALIGN:
2120 error = SET_UNALIGN_CTL(me, arg2);
2121 break;
2122 case PR_GET_UNALIGN:
2123 error = GET_UNALIGN_CTL(me, arg2);
2124 break;
2125 case PR_SET_FPEMU:
2126 error = SET_FPEMU_CTL(me, arg2);
2127 break;
2128 case PR_GET_FPEMU:
2129 error = GET_FPEMU_CTL(me, arg2);
2130 break;
2131 case PR_SET_FPEXC:
2132 error = SET_FPEXC_CTL(me, arg2);
2133 break;
2134 case PR_GET_FPEXC:
2135 error = GET_FPEXC_CTL(me, arg2);
2136 break;
2137 case PR_GET_TIMING:
2138 error = PR_TIMING_STATISTICAL;
2139 break;
2140 case PR_SET_TIMING:
2141 if (arg2 != PR_TIMING_STATISTICAL)
2142 error = -EINVAL;
2143 break;
2144 case PR_SET_NAME:
2145 comm[sizeof(me->comm) - 1] = 0;
2146 if (strncpy_from_user(comm, (char __user *)arg2,
2147 sizeof(me->comm) - 1) < 0)
2148 return -EFAULT;
2149 set_task_comm(me, comm);
2150 proc_comm_connector(me);
2151 break;
2152 case PR_GET_NAME:
2153 get_task_comm(comm, me);
2154 if (copy_to_user((char __user *)arg2, comm, sizeof(comm)))
2155 return -EFAULT;
2156 break;
2157 case PR_GET_ENDIAN:
2158 error = GET_ENDIAN(me, arg2);
2159 break;
2160 case PR_SET_ENDIAN:
2161 error = SET_ENDIAN(me, arg2);
2162 break;
2163 case PR_GET_SECCOMP:
2164 error = prctl_get_seccomp();
2165 break;
2166 case PR_SET_SECCOMP:
2167 error = prctl_set_seccomp(arg2, (char __user *)arg3);
2168 break;
2169 case PR_GET_TSC:
2170 error = GET_TSC_CTL(arg2);
2171 break;
2172 case PR_SET_TSC:
2173 error = SET_TSC_CTL(arg2);
2174 break;
2175 case PR_TASK_PERF_EVENTS_DISABLE:
2176 error = perf_event_task_disable();
2177 break;
2178 case PR_TASK_PERF_EVENTS_ENABLE:
2179 error = perf_event_task_enable();
2180 break;
2181 case PR_GET_TIMERSLACK:
John Stultzda8b44d2016-03-17 14:20:51 -07002182 if (current->timer_slack_ns > ULONG_MAX)
2183 error = ULONG_MAX;
2184 else
2185 error = current->timer_slack_ns;
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002186 break;
2187 case PR_SET_TIMERSLACK:
2188 if (arg2 <= 0)
2189 current->timer_slack_ns =
2190 current->default_timer_slack_ns;
2191 else
2192 current->timer_slack_ns = arg2;
2193 break;
2194 case PR_MCE_KILL:
2195 if (arg4 | arg5)
2196 return -EINVAL;
2197 switch (arg2) {
2198 case PR_MCE_KILL_CLEAR:
2199 if (arg3 != 0)
2200 return -EINVAL;
2201 current->flags &= ~PF_MCE_PROCESS;
2202 break;
2203 case PR_MCE_KILL_SET:
2204 current->flags |= PF_MCE_PROCESS;
2205 if (arg3 == PR_MCE_KILL_EARLY)
2206 current->flags |= PF_MCE_EARLY;
2207 else if (arg3 == PR_MCE_KILL_LATE)
2208 current->flags &= ~PF_MCE_EARLY;
2209 else if (arg3 == PR_MCE_KILL_DEFAULT)
2210 current->flags &=
2211 ~(PF_MCE_EARLY|PF_MCE_PROCESS);
2212 else
2213 return -EINVAL;
2214 break;
2215 default:
2216 return -EINVAL;
2217 }
2218 break;
2219 case PR_MCE_KILL_GET:
2220 if (arg2 | arg3 | arg4 | arg5)
2221 return -EINVAL;
2222 if (current->flags & PF_MCE_PROCESS)
2223 error = (current->flags & PF_MCE_EARLY) ?
2224 PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
2225 else
2226 error = PR_MCE_KILL_DEFAULT;
2227 break;
2228 case PR_SET_MM:
2229 error = prctl_set_mm(arg2, arg3, arg4, arg5);
2230 break;
2231 case PR_GET_TID_ADDRESS:
2232 error = prctl_get_tid_address(me, (int __user **)arg2);
2233 break;
2234 case PR_SET_CHILD_SUBREAPER:
2235 me->signal->is_child_subreaper = !!arg2;
Pavel Tikhomirov749860c2017-01-30 18:06:12 +03002236 if (!arg2)
2237 break;
2238
2239 walk_process_tree(me, propagate_has_child_subreaper, NULL);
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002240 break;
2241 case PR_GET_CHILD_SUBREAPER:
2242 error = put_user(me->signal->is_child_subreaper,
2243 (int __user *)arg2);
2244 break;
2245 case PR_SET_NO_NEW_PRIVS:
2246 if (arg2 != 1 || arg3 || arg4 || arg5)
2247 return -EINVAL;
2248
Kees Cook1d4457f2014-05-21 15:23:46 -07002249 task_set_no_new_privs(current);
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002250 break;
2251 case PR_GET_NO_NEW_PRIVS:
2252 if (arg2 || arg3 || arg4 || arg5)
2253 return -EINVAL;
Kees Cook1d4457f2014-05-21 15:23:46 -07002254 return task_no_new_privs(current) ? 1 : 0;
Alex Thorltona0715cc2014-04-07 15:37:10 -07002255 case PR_GET_THP_DISABLE:
2256 if (arg2 || arg3 || arg4 || arg5)
2257 return -EINVAL;
2258 error = !!(me->mm->def_flags & VM_NOHUGEPAGE);
2259 break;
2260 case PR_SET_THP_DISABLE:
2261 if (arg3 || arg4 || arg5)
2262 return -EINVAL;
Michal Hocko17b05732016-05-23 16:26:05 -07002263 if (down_write_killable(&me->mm->mmap_sem))
2264 return -EINTR;
Alex Thorltona0715cc2014-04-07 15:37:10 -07002265 if (arg2)
2266 me->mm->def_flags |= VM_NOHUGEPAGE;
2267 else
2268 me->mm->def_flags &= ~VM_NOHUGEPAGE;
2269 up_write(&me->mm->mmap_sem);
2270 break;
Dave Hansenfe3d1972014-11-14 07:18:29 -08002271 case PR_MPX_ENABLE_MANAGEMENT:
Dave Hansene9d1b4f2015-01-08 14:30:22 -08002272 if (arg2 || arg3 || arg4 || arg5)
2273 return -EINVAL;
Dave Hansen46a6e0c2015-06-07 11:37:02 -07002274 error = MPX_ENABLE_MANAGEMENT();
Dave Hansenfe3d1972014-11-14 07:18:29 -08002275 break;
2276 case PR_MPX_DISABLE_MANAGEMENT:
Dave Hansene9d1b4f2015-01-08 14:30:22 -08002277 if (arg2 || arg3 || arg4 || arg5)
2278 return -EINVAL;
Dave Hansen46a6e0c2015-06-07 11:37:02 -07002279 error = MPX_DISABLE_MANAGEMENT();
Dave Hansenfe3d1972014-11-14 07:18:29 -08002280 break;
Paul Burton97915542015-01-08 12:17:37 +00002281 case PR_SET_FP_MODE:
2282 error = SET_FP_MODE(me, arg2);
2283 break;
2284 case PR_GET_FP_MODE:
2285 error = GET_FP_MODE(me);
2286 break;
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002287 default:
2288 error = -EINVAL;
2289 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 }
2291 return error;
2292}
Andi Kleen3cfc3482006-09-26 10:52:28 +02002293
Heiko Carstens836f92a2009-01-14 14:14:33 +01002294SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
2295 struct getcpu_cache __user *, unused)
Andi Kleen3cfc3482006-09-26 10:52:28 +02002296{
2297 int err = 0;
2298 int cpu = raw_smp_processor_id();
vishnu.psec94fc32014-10-09 15:30:23 -07002299
Andi Kleen3cfc3482006-09-26 10:52:28 +02002300 if (cpup)
2301 err |= put_user(cpu, cpup);
2302 if (nodep)
2303 err |= put_user(cpu_to_node(cpu), nodep);
Andi Kleen3cfc3482006-09-26 10:52:28 +02002304 return err ? -EFAULT : 0;
2305}
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002306
Stephen Rothwell4a22f162013-04-30 15:27:37 -07002307/**
2308 * do_sysinfo - fill in sysinfo struct
2309 * @info: pointer to buffer to fill
2310 */
2311static int do_sysinfo(struct sysinfo *info)
2312{
2313 unsigned long mem_total, sav_total;
2314 unsigned int mem_unit, bitcount;
2315 struct timespec tp;
2316
2317 memset(info, 0, sizeof(struct sysinfo));
2318
Oleg Nesterov45c64942013-07-03 15:05:01 -07002319 get_monotonic_boottime(&tp);
Stephen Rothwell4a22f162013-04-30 15:27:37 -07002320 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
2321
2322 get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
2323
2324 info->procs = nr_threads;
2325
2326 si_meminfo(info);
2327 si_swapinfo(info);
2328
2329 /*
2330 * If the sum of all the available memory (i.e. ram + swap)
2331 * is less than can be stored in a 32 bit unsigned long then
2332 * we can be binary compatible with 2.2.x kernels. If not,
2333 * well, in that case 2.2.x was broken anyways...
2334 *
2335 * -Erik Andersen <andersee@debian.org>
2336 */
2337
2338 mem_total = info->totalram + info->totalswap;
2339 if (mem_total < info->totalram || mem_total < info->totalswap)
2340 goto out;
2341 bitcount = 0;
2342 mem_unit = info->mem_unit;
2343 while (mem_unit > 1) {
2344 bitcount++;
2345 mem_unit >>= 1;
2346 sav_total = mem_total;
2347 mem_total <<= 1;
2348 if (mem_total < sav_total)
2349 goto out;
2350 }
2351
2352 /*
2353 * If mem_total did not overflow, multiply all memory values by
2354 * info->mem_unit and set it to 1. This leaves things compatible
2355 * with 2.2.x, and also retains compatibility with earlier 2.4.x
2356 * kernels...
2357 */
2358
2359 info->mem_unit = 1;
2360 info->totalram <<= bitcount;
2361 info->freeram <<= bitcount;
2362 info->sharedram <<= bitcount;
2363 info->bufferram <<= bitcount;
2364 info->totalswap <<= bitcount;
2365 info->freeswap <<= bitcount;
2366 info->totalhigh <<= bitcount;
2367 info->freehigh <<= bitcount;
2368
2369out:
2370 return 0;
2371}
2372
2373SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
2374{
2375 struct sysinfo val;
2376
2377 do_sysinfo(&val);
2378
2379 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
2380 return -EFAULT;
2381
2382 return 0;
2383}
2384
2385#ifdef CONFIG_COMPAT
2386struct compat_sysinfo {
2387 s32 uptime;
2388 u32 loads[3];
2389 u32 totalram;
2390 u32 freeram;
2391 u32 sharedram;
2392 u32 bufferram;
2393 u32 totalswap;
2394 u32 freeswap;
2395 u16 procs;
2396 u16 pad;
2397 u32 totalhigh;
2398 u32 freehigh;
2399 u32 mem_unit;
2400 char _f[20-2*sizeof(u32)-sizeof(int)];
2401};
2402
2403COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
2404{
2405 struct sysinfo s;
2406
2407 do_sysinfo(&s);
2408
2409 /* Check to see if any memory value is too large for 32-bit and scale
2410 * down if needed
2411 */
Scotty Bauer0baae412014-10-09 15:30:26 -07002412 if (upper_32_bits(s.totalram) || upper_32_bits(s.totalswap)) {
Stephen Rothwell4a22f162013-04-30 15:27:37 -07002413 int bitcount = 0;
2414
2415 while (s.mem_unit < PAGE_SIZE) {
2416 s.mem_unit <<= 1;
2417 bitcount++;
2418 }
2419
2420 s.totalram >>= bitcount;
2421 s.freeram >>= bitcount;
2422 s.sharedram >>= bitcount;
2423 s.bufferram >>= bitcount;
2424 s.totalswap >>= bitcount;
2425 s.freeswap >>= bitcount;
2426 s.totalhigh >>= bitcount;
2427 s.freehigh >>= bitcount;
2428 }
2429
2430 if (!access_ok(VERIFY_WRITE, info, sizeof(struct compat_sysinfo)) ||
2431 __put_user(s.uptime, &info->uptime) ||
2432 __put_user(s.loads[0], &info->loads[0]) ||
2433 __put_user(s.loads[1], &info->loads[1]) ||
2434 __put_user(s.loads[2], &info->loads[2]) ||
2435 __put_user(s.totalram, &info->totalram) ||
2436 __put_user(s.freeram, &info->freeram) ||
2437 __put_user(s.sharedram, &info->sharedram) ||
2438 __put_user(s.bufferram, &info->bufferram) ||
2439 __put_user(s.totalswap, &info->totalswap) ||
2440 __put_user(s.freeswap, &info->freeswap) ||
2441 __put_user(s.procs, &info->procs) ||
2442 __put_user(s.totalhigh, &info->totalhigh) ||
2443 __put_user(s.freehigh, &info->freehigh) ||
2444 __put_user(s.mem_unit, &info->mem_unit))
2445 return -EFAULT;
2446
2447 return 0;
2448}
2449#endif /* CONFIG_COMPAT */