blob: 524a4cb9bbe25d02d39d82e145c78d0584569d60 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/kernel/sys.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
Paul Gortmaker9984de12011-05-23 14:51:41 -04008#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/mm.h>
10#include <linux/utsname.h>
11#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/reboot.h>
13#include <linux/prctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/highuid.h>
15#include <linux/fs.h>
Paul Gortmaker74da1ff2011-05-26 12:48:41 -040016#include <linux/kmod.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020017#include <linux/perf_event.h>
Daniel Walker3e88c552007-05-10 22:22:53 -070018#include <linux/resource.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070019#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/workqueue.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080021#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/device.h>
23#include <linux/key.h>
24#include <linux/times.h>
25#include <linux/posix-timers.h>
26#include <linux/security.h>
27#include <linux/dcookies.h>
28#include <linux/suspend.h>
29#include <linux/tty.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070030#include <linux/signal.h>
Matt Helsley9f460802005-11-07 00:59:16 -080031#include <linux/cn_proc.h>
Andi Kleen3cfc3482006-09-26 10:52:28 +020032#include <linux/getcpu.h>
Eric Dumazet6eaeeab2007-05-10 22:22:37 -070033#include <linux/task_io_accounting_ops.h>
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -070034#include <linux/seccomp.h>
Mark Lord40477272007-10-01 01:20:10 -070035#include <linux/cpu.h>
Christoph Hellwige28cbf22010-03-10 15:21:19 -080036#include <linux/personality.h>
Paul Mackerrase3d5a272009-01-06 14:41:02 -080037#include <linux/ptrace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040038#include <linux/fs_struct.h>
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -070039#include <linux/file.h>
40#include <linux/mount.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/gfp.h>
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010042#include <linux/syscore_ops.h>
Andi Kleenbe274252011-08-19 16:15:10 -070043#include <linux/version.h>
44#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <linux/compat.h>
47#include <linux/syscalls.h>
Keshavamurthy Anil S00d7c052005-12-12 00:37:33 -080048#include <linux/kprobes.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070049#include <linux/user_namespace.h>
Chen Gang7fe5e042013-02-21 16:43:06 -080050#include <linux/binfmts.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Stephen Rothwell4a22f162013-04-30 15:27:37 -070052#include <linux/sched.h>
Ingo Molnar4eb5aaa2017-02-08 18:51:29 +010053#include <linux/sched/autogroup.h>
Ingo Molnar4f177222017-02-08 08:45:17 +010054#include <linux/sched/loadavg.h>
Ingo Molnar03441a32017-02-08 18:51:35 +010055#include <linux/sched/stat.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010056#include <linux/sched/mm.h>
Ingo Molnarf7ccbae2017-02-08 18:51:30 +010057#include <linux/sched/coredump.h>
Ingo Molnar29930022017-02-08 18:51:36 +010058#include <linux/sched/task.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +010059#include <linux/sched/cputime.h>
Stephen Rothwell4a22f162013-04-30 15:27:37 -070060#include <linux/rcupdate.h>
61#include <linux/uidgid.h>
62#include <linux/cred.h>
63
Seiji Aguchi04c68622011-01-12 16:59:30 -080064#include <linux/kmsg_dump.h>
Andi Kleenbe274252011-08-19 16:15:10 -070065/* Move somewhere else to avoid recompiling? */
66#include <generated/utsrelease.h>
Seiji Aguchi04c68622011-01-12 16:59:30 -080067
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080068#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <asm/io.h>
70#include <asm/unistd.h>
71
72#ifndef SET_UNALIGN_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070073# define SET_UNALIGN_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#endif
75#ifndef GET_UNALIGN_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070076# define GET_UNALIGN_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#endif
78#ifndef SET_FPEMU_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070079# define SET_FPEMU_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#endif
81#ifndef GET_FPEMU_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070082# define GET_FPEMU_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#endif
84#ifndef SET_FPEXC_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070085# define SET_FPEXC_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#endif
87#ifndef GET_FPEXC_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070088# define GET_FPEXC_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#endif
Anton Blanchard651d7652006-06-07 16:10:19 +100090#ifndef GET_ENDIAN
vishnu.psec94fc32014-10-09 15:30:23 -070091# define GET_ENDIAN(a, b) (-EINVAL)
Anton Blanchard651d7652006-06-07 16:10:19 +100092#endif
93#ifndef SET_ENDIAN
vishnu.psec94fc32014-10-09 15:30:23 -070094# define SET_ENDIAN(a, b) (-EINVAL)
Anton Blanchard651d7652006-06-07 16:10:19 +100095#endif
Erik Bosman8fb402b2008-04-11 18:54:17 +020096#ifndef GET_TSC_CTL
97# define GET_TSC_CTL(a) (-EINVAL)
98#endif
99#ifndef SET_TSC_CTL
100# define SET_TSC_CTL(a) (-EINVAL)
101#endif
Dave Hansenfe3d1972014-11-14 07:18:29 -0800102#ifndef MPX_ENABLE_MANAGEMENT
Dave Hansen46a6e0c2015-06-07 11:37:02 -0700103# define MPX_ENABLE_MANAGEMENT() (-EINVAL)
Dave Hansenfe3d1972014-11-14 07:18:29 -0800104#endif
105#ifndef MPX_DISABLE_MANAGEMENT
Dave Hansen46a6e0c2015-06-07 11:37:02 -0700106# define MPX_DISABLE_MANAGEMENT() (-EINVAL)
Dave Hansenfe3d1972014-11-14 07:18:29 -0800107#endif
Paul Burton97915542015-01-08 12:17:37 +0000108#ifndef GET_FP_MODE
109# define GET_FP_MODE(a) (-EINVAL)
110#endif
111#ifndef SET_FP_MODE
112# define SET_FP_MODE(a,b) (-EINVAL)
113#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115/*
116 * this is where the system-wide overflow UID and GID are defined, for
117 * architectures that now have 32-bit UID/GID but didn't in the past
118 */
119
120int overflowuid = DEFAULT_OVERFLOWUID;
121int overflowgid = DEFAULT_OVERFLOWGID;
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123EXPORT_SYMBOL(overflowuid);
124EXPORT_SYMBOL(overflowgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/*
127 * the same as above, but for filesystems which can only store a 16-bit
128 * UID and GID. as such, this is needed on all architectures
129 */
130
131int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
132int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
133
134EXPORT_SYMBOL(fs_overflowuid);
135EXPORT_SYMBOL(fs_overflowgid);
136
137/*
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700138 * Returns true if current's euid is same as p's uid or euid,
139 * or has CAP_SYS_NICE to p's user_ns.
140 *
141 * Called with rcu_read_lock, creds are safe
142 */
143static bool set_one_prio_perm(struct task_struct *p)
144{
145 const struct cred *cred = current_cred(), *pcred = __task_cred(p);
146
Eric W. Biederman5af66202012-03-03 20:21:47 -0800147 if (uid_eq(pcred->uid, cred->euid) ||
148 uid_eq(pcred->euid, cred->euid))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700149 return true;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800150 if (ns_capable(pcred->user_ns, CAP_SYS_NICE))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700151 return true;
152 return false;
153}
154
155/*
David Howellsc69e8d92008-11-14 10:39:19 +1100156 * set the priority of a task
157 * - the caller must hold the RCU read lock
158 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static int set_one_prio(struct task_struct *p, int niceval, int error)
160{
161 int no_nice;
162
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700163 if (!set_one_prio_perm(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 error = -EPERM;
165 goto out;
166 }
Matt Mackalle43379f2005-05-01 08:59:00 -0700167 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 error = -EACCES;
169 goto out;
170 }
171 no_nice = security_task_setnice(p, niceval);
172 if (no_nice) {
173 error = no_nice;
174 goto out;
175 }
176 if (error == -ESRCH)
177 error = 0;
178 set_user_nice(p, niceval);
179out:
180 return error;
181}
182
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100183SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 struct task_struct *g, *p;
186 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100187 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 int error = -EINVAL;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800189 struct pid *pgrp;
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800190 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Daniel Walker3e88c552007-05-10 22:22:53 -0700192 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 goto out;
194
195 /* normalize: avoid signed division (rounding problems) */
196 error = -ESRCH;
Dongsheng Yangc4a4d2f2014-02-11 15:34:51 +0800197 if (niceval < MIN_NICE)
198 niceval = MIN_NICE;
199 if (niceval > MAX_NICE)
200 niceval = MAX_NICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000202 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 read_lock(&tasklist_lock);
204 switch (which) {
vishnu.psec94fc32014-10-09 15:30:23 -0700205 case PRIO_PROCESS:
206 if (who)
207 p = find_task_by_vpid(who);
208 else
209 p = current;
210 if (p)
211 error = set_one_prio(p, niceval, error);
212 break;
213 case PRIO_PGRP:
214 if (who)
215 pgrp = find_vpid(who);
216 else
217 pgrp = task_pgrp(current);
218 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
219 error = set_one_prio(p, niceval, error);
220 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
221 break;
222 case PRIO_USER:
223 uid = make_kuid(cred->user_ns, who);
224 user = cred->user;
225 if (!who)
226 uid = cred->uid;
227 else if (!uid_eq(uid, cred->uid)) {
228 user = find_user(uid);
229 if (!user)
David Howells86a264a2008-11-14 10:39:18 +1100230 goto out_unlock; /* No processes for this user */
vishnu.psec94fc32014-10-09 15:30:23 -0700231 }
232 do_each_thread(g, p) {
Ben Segall8639b462015-11-06 16:32:48 -0800233 if (uid_eq(task_uid(p), uid) && task_pid_vnr(p))
vishnu.psec94fc32014-10-09 15:30:23 -0700234 error = set_one_prio(p, niceval, error);
235 } while_each_thread(g, p);
236 if (!uid_eq(uid, cred->uid))
237 free_uid(user); /* For find_user() */
238 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240out_unlock:
241 read_unlock(&tasklist_lock);
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000242 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243out:
244 return error;
245}
246
247/*
248 * Ugh. To avoid negative return values, "getpriority()" will
249 * not return the normal nice-value, but a negated value that
250 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
251 * to stay compatible.
252 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100253SYSCALL_DEFINE2(getpriority, int, which, int, who)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct task_struct *g, *p;
256 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100257 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 long niceval, retval = -ESRCH;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800259 struct pid *pgrp;
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800260 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Daniel Walker3e88c552007-05-10 22:22:53 -0700262 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return -EINVAL;
264
Tetsuo Handa70118832010-02-22 12:44:16 -0800265 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 read_lock(&tasklist_lock);
267 switch (which) {
vishnu.psec94fc32014-10-09 15:30:23 -0700268 case PRIO_PROCESS:
269 if (who)
270 p = find_task_by_vpid(who);
271 else
272 p = current;
273 if (p) {
274 niceval = nice_to_rlimit(task_nice(p));
275 if (niceval > retval)
276 retval = niceval;
277 }
278 break;
279 case PRIO_PGRP:
280 if (who)
281 pgrp = find_vpid(who);
282 else
283 pgrp = task_pgrp(current);
284 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
285 niceval = nice_to_rlimit(task_nice(p));
286 if (niceval > retval)
287 retval = niceval;
288 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
289 break;
290 case PRIO_USER:
291 uid = make_kuid(cred->user_ns, who);
292 user = cred->user;
293 if (!who)
294 uid = cred->uid;
295 else if (!uid_eq(uid, cred->uid)) {
296 user = find_user(uid);
297 if (!user)
298 goto out_unlock; /* No processes for this user */
299 }
300 do_each_thread(g, p) {
Ben Segall8639b462015-11-06 16:32:48 -0800301 if (uid_eq(task_uid(p), uid) && task_pid_vnr(p)) {
Dongsheng Yang7aa2c012014-05-08 18:33:49 +0900302 niceval = nice_to_rlimit(task_nice(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (niceval > retval)
304 retval = niceval;
305 }
vishnu.psec94fc32014-10-09 15:30:23 -0700306 } while_each_thread(g, p);
307 if (!uid_eq(uid, cred->uid))
308 free_uid(user); /* for find_user() */
309 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311out_unlock:
312 read_unlock(&tasklist_lock);
Tetsuo Handa70118832010-02-22 12:44:16 -0800313 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 return retval;
316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/*
319 * Unprivileged users may change the real gid to the effective gid
320 * or vice versa. (BSD-style)
321 *
322 * If you set the real gid at all, or set the effective gid to a value not
323 * equal to the real gid, then the saved gid is set to the new effective gid.
324 *
325 * This makes it possible for a setgid program to completely drop its
326 * privileges, which is often a useful assertion to make when you are doing
327 * a security audit over a program.
328 *
329 * The general idea is that a program which uses just setregid() will be
330 * 100% compatible with BSD. A program which uses just setgid() will be
vishnu.psec94fc32014-10-09 15:30:23 -0700331 * 100% compatible with POSIX with saved IDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 *
333 * SMP: There are not races, the GIDs are checked only by filesystem
334 * operations (as far as semantic preservation is concerned).
335 */
Iulia Manda28138932015-04-15 16:16:41 -0700336#ifdef CONFIG_MULTIUSER
Heiko Carstensae1251a2009-01-14 14:14:05 +0100337SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800339 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100340 const struct cred *old;
341 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800343 kgid_t krgid, kegid;
344
345 krgid = make_kgid(ns, rgid);
346 kegid = make_kgid(ns, egid);
347
348 if ((rgid != (gid_t) -1) && !gid_valid(krgid))
349 return -EINVAL;
350 if ((egid != (gid_t) -1) && !gid_valid(kegid))
351 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
David Howellsd84f4f92008-11-14 10:39:23 +1100353 new = prepare_creds();
354 if (!new)
355 return -ENOMEM;
356 old = current_cred();
357
David Howellsd84f4f92008-11-14 10:39:23 +1100358 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (rgid != (gid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800360 if (gid_eq(old->gid, krgid) ||
361 gid_eq(old->egid, krgid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700362 ns_capable(old->user_ns, CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800363 new->gid = krgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 else
David Howellsd84f4f92008-11-14 10:39:23 +1100365 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367 if (egid != (gid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800368 if (gid_eq(old->gid, kegid) ||
369 gid_eq(old->egid, kegid) ||
370 gid_eq(old->sgid, kegid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700371 ns_capable(old->user_ns, CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800372 new->egid = kegid;
Cal Peake756184b2006-09-30 23:27:24 -0700373 else
David Howellsd84f4f92008-11-14 10:39:23 +1100374 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
David Howellsd84f4f92008-11-14 10:39:23 +1100376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (rgid != (gid_t) -1 ||
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800378 (egid != (gid_t) -1 && !gid_eq(kegid, old->gid)))
David Howellsd84f4f92008-11-14 10:39:23 +1100379 new->sgid = new->egid;
380 new->fsgid = new->egid;
381
382 return commit_creds(new);
383
384error:
385 abort_creds(new);
386 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389/*
vishnu.psec94fc32014-10-09 15:30:23 -0700390 * setgid() is implemented like SysV w/ SAVED_IDS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 *
392 * SMP: Same implicit races as above.
393 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100394SYSCALL_DEFINE1(setgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800396 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100397 const struct cred *old;
398 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800400 kgid_t kgid;
401
402 kgid = make_kgid(ns, gid);
403 if (!gid_valid(kgid))
404 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
David Howellsd84f4f92008-11-14 10:39:23 +1100406 new = prepare_creds();
407 if (!new)
408 return -ENOMEM;
409 old = current_cred();
410
David Howellsd84f4f92008-11-14 10:39:23 +1100411 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700412 if (ns_capable(old->user_ns, CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800413 new->gid = new->egid = new->sgid = new->fsgid = kgid;
414 else if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->sgid))
415 new->egid = new->fsgid = kgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 else
David Howellsd84f4f92008-11-14 10:39:23 +1100417 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
David Howellsd84f4f92008-11-14 10:39:23 +1100419 return commit_creds(new);
420
421error:
422 abort_creds(new);
423 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
Dhaval Giani54e99122009-02-27 15:13:54 +0530425
David Howellsd84f4f92008-11-14 10:39:23 +1100426/*
427 * change the user struct in a credentials set to match the new UID
428 */
429static int set_user(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct user_struct *new_user;
432
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800433 new_user = alloc_uid(new->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (!new_user)
435 return -EAGAIN;
436
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400437 /*
438 * We don't fail in case of NPROC limit excess here because too many
439 * poorly written programs don't check set*uid() return code, assuming
440 * it never fails if called by root. We may still enforce NPROC limit
441 * for programs doing set*uid()+execve() by harmlessly deferring the
442 * failure to the execve() stage.
443 */
Jiri Slaby78d7d402010-03-05 13:42:54 -0800444 if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400445 new_user != INIT_USER)
446 current->flags |= PF_NPROC_EXCEEDED;
447 else
448 current->flags &= ~PF_NPROC_EXCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
David Howellsd84f4f92008-11-14 10:39:23 +1100450 free_uid(new->user);
451 new->user = new_user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return 0;
453}
454
455/*
456 * Unprivileged users may change the real uid to the effective uid
457 * or vice versa. (BSD-style)
458 *
459 * If you set the real uid at all, or set the effective uid to a value not
460 * equal to the real uid, then the saved uid is set to the new effective uid.
461 *
462 * This makes it possible for a setuid program to completely drop its
463 * privileges, which is often a useful assertion to make when you are doing
464 * a security audit over a program.
465 *
466 * The general idea is that a program which uses just setreuid() will be
467 * 100% compatible with BSD. A program which uses just setuid() will be
vishnu.psec94fc32014-10-09 15:30:23 -0700468 * 100% compatible with POSIX with saved IDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100470SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800472 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100473 const struct cred *old;
474 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800476 kuid_t kruid, keuid;
477
478 kruid = make_kuid(ns, ruid);
479 keuid = make_kuid(ns, euid);
480
481 if ((ruid != (uid_t) -1) && !uid_valid(kruid))
482 return -EINVAL;
483 if ((euid != (uid_t) -1) && !uid_valid(keuid))
484 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
David Howellsd84f4f92008-11-14 10:39:23 +1100486 new = prepare_creds();
487 if (!new)
488 return -ENOMEM;
489 old = current_cred();
490
David Howellsd84f4f92008-11-14 10:39:23 +1100491 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (ruid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800493 new->uid = kruid;
494 if (!uid_eq(old->uid, kruid) &&
495 !uid_eq(old->euid, kruid) &&
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700496 !ns_capable(old->user_ns, CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100497 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499
500 if (euid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800501 new->euid = keuid;
502 if (!uid_eq(old->uid, keuid) &&
503 !uid_eq(old->euid, keuid) &&
504 !uid_eq(old->suid, keuid) &&
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700505 !ns_capable(old->user_ns, CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100506 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800509 if (!uid_eq(new->uid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530510 retval = set_user(new);
511 if (retval < 0)
512 goto error;
513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (ruid != (uid_t) -1 ||
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800515 (euid != (uid_t) -1 && !uid_eq(keuid, old->uid)))
David Howellsd84f4f92008-11-14 10:39:23 +1100516 new->suid = new->euid;
517 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
David Howellsd84f4f92008-11-14 10:39:23 +1100519 retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
520 if (retval < 0)
521 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
David Howellsd84f4f92008-11-14 10:39:23 +1100523 return commit_creds(new);
524
525error:
526 abort_creds(new);
527 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
vishnu.psec94fc32014-10-09 15:30:23 -0700529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530/*
vishnu.psec94fc32014-10-09 15:30:23 -0700531 * setuid() is implemented like SysV with SAVED_IDS
532 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 * Note that SAVED_ID's is deficient in that a setuid root program
vishnu.psec94fc32014-10-09 15:30:23 -0700534 * like sendmail, for example, cannot set its uid to be a normal
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * user and then switch back, because if you're root, setuid() sets
536 * the saved uid too. If you don't like this, blame the bright people
537 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
538 * will allow a root program to temporarily drop privileges and be able to
vishnu.psec94fc32014-10-09 15:30:23 -0700539 * regain them by swapping the real and effective uid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100541SYSCALL_DEFINE1(setuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800543 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100544 const struct cred *old;
545 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800547 kuid_t kuid;
548
549 kuid = make_kuid(ns, uid);
550 if (!uid_valid(kuid))
551 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
David Howellsd84f4f92008-11-14 10:39:23 +1100553 new = prepare_creds();
554 if (!new)
555 return -ENOMEM;
556 old = current_cred();
557
David Howellsd84f4f92008-11-14 10:39:23 +1100558 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700559 if (ns_capable(old->user_ns, CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800560 new->suid = new->uid = kuid;
561 if (!uid_eq(kuid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530562 retval = set_user(new);
563 if (retval < 0)
564 goto error;
David Howellsd84f4f92008-11-14 10:39:23 +1100565 }
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800566 } else if (!uid_eq(kuid, old->uid) && !uid_eq(kuid, new->suid)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100567 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800570 new->fsuid = new->euid = kuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
David Howellsd84f4f92008-11-14 10:39:23 +1100572 retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
573 if (retval < 0)
574 goto error;
575
576 return commit_creds(new);
577
578error:
579 abort_creds(new);
580 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
583
584/*
585 * This function implements a generic ability to update ruid, euid,
586 * and suid. This allows you to implement the 4.4 compatible seteuid().
587 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100588SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800590 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100591 const struct cred *old;
592 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800594 kuid_t kruid, keuid, ksuid;
595
596 kruid = make_kuid(ns, ruid);
597 keuid = make_kuid(ns, euid);
598 ksuid = make_kuid(ns, suid);
599
600 if ((ruid != (uid_t) -1) && !uid_valid(kruid))
601 return -EINVAL;
602
603 if ((euid != (uid_t) -1) && !uid_valid(keuid))
604 return -EINVAL;
605
606 if ((suid != (uid_t) -1) && !uid_valid(ksuid))
607 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
David Howellsd84f4f92008-11-14 10:39:23 +1100609 new = prepare_creds();
610 if (!new)
611 return -ENOMEM;
612
David Howellsd84f4f92008-11-14 10:39:23 +1100613 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
David Howellsd84f4f92008-11-14 10:39:23 +1100615 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700616 if (!ns_capable(old->user_ns, CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800617 if (ruid != (uid_t) -1 && !uid_eq(kruid, old->uid) &&
618 !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100619 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800620 if (euid != (uid_t) -1 && !uid_eq(keuid, old->uid) &&
621 !uid_eq(keuid, old->euid) && !uid_eq(keuid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100622 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800623 if (suid != (uid_t) -1 && !uid_eq(ksuid, old->uid) &&
624 !uid_eq(ksuid, old->euid) && !uid_eq(ksuid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100625 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
David Howellsd84f4f92008-11-14 10:39:23 +1100627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (ruid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800629 new->uid = kruid;
630 if (!uid_eq(kruid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530631 retval = set_user(new);
632 if (retval < 0)
633 goto error;
634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
David Howellsd84f4f92008-11-14 10:39:23 +1100636 if (euid != (uid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800637 new->euid = keuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (suid != (uid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800639 new->suid = ksuid;
David Howellsd84f4f92008-11-14 10:39:23 +1100640 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
David Howellsd84f4f92008-11-14 10:39:23 +1100642 retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
643 if (retval < 0)
644 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
David Howellsd84f4f92008-11-14 10:39:23 +1100646 return commit_creds(new);
647
648error:
649 abort_creds(new);
650 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800653SYSCALL_DEFINE3(getresuid, uid_t __user *, ruidp, uid_t __user *, euidp, uid_t __user *, suidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
David Howells86a264a2008-11-14 10:39:18 +1100655 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800657 uid_t ruid, euid, suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800659 ruid = from_kuid_munged(cred->user_ns, cred->uid);
660 euid = from_kuid_munged(cred->user_ns, cred->euid);
661 suid = from_kuid_munged(cred->user_ns, cred->suid);
662
vishnu.psec94fc32014-10-09 15:30:23 -0700663 retval = put_user(ruid, ruidp);
664 if (!retval) {
665 retval = put_user(euid, euidp);
666 if (!retval)
667 return put_user(suid, suidp);
668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return retval;
670}
671
672/*
673 * Same as above, but for rgid, egid, sgid.
674 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100675SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800677 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100678 const struct cred *old;
679 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800681 kgid_t krgid, kegid, ksgid;
682
683 krgid = make_kgid(ns, rgid);
684 kegid = make_kgid(ns, egid);
685 ksgid = make_kgid(ns, sgid);
686
687 if ((rgid != (gid_t) -1) && !gid_valid(krgid))
688 return -EINVAL;
689 if ((egid != (gid_t) -1) && !gid_valid(kegid))
690 return -EINVAL;
691 if ((sgid != (gid_t) -1) && !gid_valid(ksgid))
692 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
David Howellsd84f4f92008-11-14 10:39:23 +1100694 new = prepare_creds();
695 if (!new)
696 return -ENOMEM;
697 old = current_cred();
698
David Howellsd84f4f92008-11-14 10:39:23 +1100699 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700700 if (!ns_capable(old->user_ns, CAP_SETGID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800701 if (rgid != (gid_t) -1 && !gid_eq(krgid, old->gid) &&
702 !gid_eq(krgid, old->egid) && !gid_eq(krgid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100703 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800704 if (egid != (gid_t) -1 && !gid_eq(kegid, old->gid) &&
705 !gid_eq(kegid, old->egid) && !gid_eq(kegid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100706 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800707 if (sgid != (gid_t) -1 && !gid_eq(ksgid, old->gid) &&
708 !gid_eq(ksgid, old->egid) && !gid_eq(ksgid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100709 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
David Howellsd84f4f92008-11-14 10:39:23 +1100712 if (rgid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800713 new->gid = krgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100714 if (egid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800715 new->egid = kegid;
David Howellsd84f4f92008-11-14 10:39:23 +1100716 if (sgid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800717 new->sgid = ksgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100718 new->fsgid = new->egid;
719
720 return commit_creds(new);
721
722error:
723 abort_creds(new);
724 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800727SYSCALL_DEFINE3(getresgid, gid_t __user *, rgidp, gid_t __user *, egidp, gid_t __user *, sgidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
David Howells86a264a2008-11-14 10:39:18 +1100729 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800731 gid_t rgid, egid, sgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800733 rgid = from_kgid_munged(cred->user_ns, cred->gid);
734 egid = from_kgid_munged(cred->user_ns, cred->egid);
735 sgid = from_kgid_munged(cred->user_ns, cred->sgid);
736
vishnu.psec94fc32014-10-09 15:30:23 -0700737 retval = put_user(rgid, rgidp);
738 if (!retval) {
739 retval = put_user(egid, egidp);
740 if (!retval)
741 retval = put_user(sgid, sgidp);
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 return retval;
745}
746
747
748/*
749 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
750 * is used for "access()" and for the NFS daemon (letting nfsd stay at
751 * whatever uid it wants to). It normally shadows "euid", except when
752 * explicitly set by setfsuid() or for access..
753 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100754SYSCALL_DEFINE1(setfsuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
David Howellsd84f4f92008-11-14 10:39:23 +1100756 const struct cred *old;
757 struct cred *new;
758 uid_t old_fsuid;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800759 kuid_t kuid;
760
761 old = current_cred();
762 old_fsuid = from_kuid_munged(old->user_ns, old->fsuid);
763
764 kuid = make_kuid(old->user_ns, uid);
765 if (!uid_valid(kuid))
766 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
David Howellsd84f4f92008-11-14 10:39:23 +1100768 new = prepare_creds();
769 if (!new)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800770 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800772 if (uid_eq(kuid, old->uid) || uid_eq(kuid, old->euid) ||
773 uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700774 ns_capable(old->user_ns, CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800775 if (!uid_eq(kuid, old->fsuid)) {
776 new->fsuid = kuid;
David Howellsd84f4f92008-11-14 10:39:23 +1100777 if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
778 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
781
David Howellsd84f4f92008-11-14 10:39:23 +1100782 abort_creds(new);
783 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
David Howellsd84f4f92008-11-14 10:39:23 +1100785change_okay:
786 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return old_fsuid;
788}
789
790/*
John Anthony Kazos Jrf42df9e2007-05-09 08:23:08 +0200791 * Samma på svenska..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100793SYSCALL_DEFINE1(setfsgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
David Howellsd84f4f92008-11-14 10:39:23 +1100795 const struct cred *old;
796 struct cred *new;
797 gid_t old_fsgid;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800798 kgid_t kgid;
799
800 old = current_cred();
801 old_fsgid = from_kgid_munged(old->user_ns, old->fsgid);
802
803 kgid = make_kgid(old->user_ns, gid);
804 if (!gid_valid(kgid))
805 return old_fsgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
David Howellsd84f4f92008-11-14 10:39:23 +1100807 new = prepare_creds();
808 if (!new)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800809 return old_fsgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100810
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800811 if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->egid) ||
812 gid_eq(kgid, old->sgid) || gid_eq(kgid, old->fsgid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700813 ns_capable(old->user_ns, CAP_SETGID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800814 if (!gid_eq(kgid, old->fsgid)) {
815 new->fsgid = kgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100816 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
David Howellsd84f4f92008-11-14 10:39:23 +1100819
David Howellsd84f4f92008-11-14 10:39:23 +1100820 abort_creds(new);
821 return old_fsgid;
822
823change_okay:
824 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return old_fsgid;
826}
Iulia Manda28138932015-04-15 16:16:41 -0700827#endif /* CONFIG_MULTIUSER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Stephen Rothwell4a22f162013-04-30 15:27:37 -0700829/**
830 * sys_getpid - return the thread group id of the current process
831 *
832 * Note, despite the name, this returns the tgid not the pid. The tgid and
833 * the pid are identical unless CLONE_THREAD was specified on clone() in
834 * which case the tgid is the same in all threads of the same group.
835 *
836 * This is SMP safe as current->tgid does not change.
837 */
838SYSCALL_DEFINE0(getpid)
839{
840 return task_tgid_vnr(current);
841}
842
843/* Thread ID - the internal kernel "pid" */
844SYSCALL_DEFINE0(gettid)
845{
846 return task_pid_vnr(current);
847}
848
849/*
850 * Accessing ->real_parent is not SMP-safe, it could
851 * change from under us. However, we can use a stale
852 * value of ->real_parent under rcu_read_lock(), see
853 * release_task()->call_rcu(delayed_put_task_struct).
854 */
855SYSCALL_DEFINE0(getppid)
856{
857 int pid;
858
859 rcu_read_lock();
860 pid = task_tgid_vnr(rcu_dereference(current->real_parent));
861 rcu_read_unlock();
862
863 return pid;
864}
865
866SYSCALL_DEFINE0(getuid)
867{
868 /* Only we change this so SMP safe */
869 return from_kuid_munged(current_user_ns(), current_uid());
870}
871
872SYSCALL_DEFINE0(geteuid)
873{
874 /* Only we change this so SMP safe */
875 return from_kuid_munged(current_user_ns(), current_euid());
876}
877
878SYSCALL_DEFINE0(getgid)
879{
880 /* Only we change this so SMP safe */
881 return from_kgid_munged(current_user_ns(), current_gid());
882}
883
884SYSCALL_DEFINE0(getegid)
885{
886 /* Only we change this so SMP safe */
887 return from_kgid_munged(current_user_ns(), current_egid());
888}
889
Al Viroca2406e2017-05-31 04:22:44 -0400890static void do_sys_times(struct tms *tms)
Frank Mayharf06febc2008-09-12 09:54:39 -0700891{
Frederic Weisbecker5613fda2017-01-31 04:09:23 +0100892 u64 tgutime, tgstime, cutime, cstime;
Frank Mayharf06febc2008-09-12 09:54:39 -0700893
Frederic Weisbeckere80d0a1a2012-11-21 16:26:44 +0100894 thread_group_cputime_adjusted(current, &tgutime, &tgstime);
Frank Mayharf06febc2008-09-12 09:54:39 -0700895 cutime = current->signal->cutime;
896 cstime = current->signal->cstime;
Frederic Weisbecker5613fda2017-01-31 04:09:23 +0100897 tms->tms_utime = nsec_to_clock_t(tgutime);
898 tms->tms_stime = nsec_to_clock_t(tgstime);
899 tms->tms_cutime = nsec_to_clock_t(cutime);
900 tms->tms_cstime = nsec_to_clock_t(cstime);
Frank Mayharf06febc2008-09-12 09:54:39 -0700901}
902
Heiko Carstens58fd3aa2009-01-14 14:14:03 +0100903SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (tbuf) {
906 struct tms tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Frank Mayharf06febc2008-09-12 09:54:39 -0700908 do_sys_times(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
910 return -EFAULT;
911 }
Paul Mackerrase3d5a272009-01-06 14:41:02 -0800912 force_successful_syscall_return();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return (long) jiffies_64_to_clock_t(get_jiffies_64());
914}
915
Al Viroca2406e2017-05-31 04:22:44 -0400916#ifdef CONFIG_COMPAT
917static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
918{
919 return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
920}
921
922COMPAT_SYSCALL_DEFINE1(times, struct compat_tms __user *, tbuf)
923{
924 if (tbuf) {
925 struct tms tms;
926 struct compat_tms tmp;
927
928 do_sys_times(&tms);
929 /* Convert our struct tms to the compat version. */
930 tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
931 tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
932 tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
933 tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
934 if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
935 return -EFAULT;
936 }
937 force_successful_syscall_return();
938 return compat_jiffies_to_clock_t(jiffies);
939}
940#endif
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942/*
943 * This needs some heavy checking ...
944 * I just haven't the stomach for it. I also don't fully
945 * understand sessions/pgrp etc. Let somebody who does explain it.
946 *
947 * OK, I think I have the protection semantics right.... this is really
948 * only important on a multi-user system anyway, to make sure one user
949 * can't send a signal to a process owned by another. -TYT, 12/12/91
950 *
Oleg Nesterov98611e42014-01-23 15:55:52 -0800951 * !PF_FORKNOEXEC check to conform completely to POSIX.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +0100953SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 struct task_struct *p;
Oleg Nesterovee0acf92006-01-08 01:03:53 -0800956 struct task_struct *group_leader = current->group_leader;
Oleg Nesterov4e021302008-02-08 04:19:08 -0800957 struct pid *pgrp;
958 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 if (!pid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700961 pid = task_pid_vnr(group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (!pgid)
963 pgid = pid;
964 if (pgid < 0)
965 return -EINVAL;
Paul E. McKenney950eaac2010-08-31 17:00:18 -0700966 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 /* From this point forward we keep holding onto the tasklist lock
969 * so that our parent does not change from under us. -DaveM
970 */
971 write_lock_irq(&tasklist_lock);
972
973 err = -ESRCH;
Oleg Nesterov4e021302008-02-08 04:19:08 -0800974 p = find_task_by_vpid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (!p)
976 goto out;
977
978 err = -EINVAL;
979 if (!thread_group_leader(p))
980 goto out;
981
Oleg Nesterov4e021302008-02-08 04:19:08 -0800982 if (same_thread_group(p->real_parent, group_leader)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 err = -EPERM;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800984 if (task_session(p) != task_session(group_leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 goto out;
986 err = -EACCES;
Oleg Nesterov98611e42014-01-23 15:55:52 -0800987 if (!(p->flags & PF_FORKNOEXEC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 goto out;
989 } else {
990 err = -ESRCH;
Oleg Nesterovee0acf92006-01-08 01:03:53 -0800991 if (p != group_leader)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 goto out;
993 }
994
995 err = -EPERM;
996 if (p->signal->leader)
997 goto out;
998
Oleg Nesterov4e021302008-02-08 04:19:08 -0800999 pgrp = task_pid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (pgid != pid) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001001 struct task_struct *g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Oleg Nesterov4e021302008-02-08 04:19:08 -08001003 pgrp = find_vpid(pgid);
1004 g = pid_task(pgrp, PIDTYPE_PGID);
Eric W. Biederman41487c62007-02-12 00:53:01 -08001005 if (!g || task_session(g) != task_session(group_leader))
Oleg Nesterovf020bc42006-12-08 02:38:02 -08001006 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 err = security_task_setpgid(p, pgid);
1010 if (err)
1011 goto out;
1012
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07001013 if (task_pgrp(p) != pgrp)
Oleg Nesterov83beaf32008-04-30 00:54:27 -07001014 change_pid(p, PIDTYPE_PGID, pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 err = 0;
1017out:
1018 /* All paths lead to here, thus we are safe. -DaveM */
1019 write_unlock_irq(&tasklist_lock);
Paul E. McKenney950eaac2010-08-31 17:00:18 -07001020 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return err;
1022}
1023
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001024SYSCALL_DEFINE1(getpgid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001026 struct task_struct *p;
1027 struct pid *grp;
1028 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001030 rcu_read_lock();
1031 if (!pid)
1032 grp = task_pgrp(current);
1033 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 retval = -ESRCH;
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001035 p = find_task_by_vpid(pid);
1036 if (!p)
1037 goto out;
1038 grp = task_pgrp(p);
1039 if (!grp)
1040 goto out;
1041
1042 retval = security_task_getpgid(p);
1043 if (retval)
1044 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001046 retval = pid_vnr(grp);
1047out:
1048 rcu_read_unlock();
1049 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
1052#ifdef __ARCH_WANT_SYS_GETPGRP
1053
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001054SYSCALL_DEFINE0(getpgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001056 return sys_getpgid(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
1059#endif
1060
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001061SYSCALL_DEFINE1(getsid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001063 struct task_struct *p;
1064 struct pid *sid;
1065 int retval;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001066
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001067 rcu_read_lock();
1068 if (!pid)
1069 sid = task_session(current);
1070 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 retval = -ESRCH;
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001072 p = find_task_by_vpid(pid);
1073 if (!p)
1074 goto out;
1075 sid = task_session(p);
1076 if (!sid)
1077 goto out;
1078
1079 retval = security_task_getsid(p);
1080 if (retval)
1081 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001083 retval = pid_vnr(sid);
1084out:
1085 rcu_read_unlock();
1086 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
Oleg Nesterov81dabb42013-07-03 15:08:26 -07001089static void set_special_pids(struct pid *pid)
1090{
1091 struct task_struct *curr = current->group_leader;
1092
1093 if (task_session(curr) != pid)
1094 change_pid(curr, PIDTYPE_SID, pid);
1095
1096 if (task_pgrp(curr) != pid)
1097 change_pid(curr, PIDTYPE_PGID, pid);
1098}
1099
Heiko Carstensb290ebe2009-01-14 14:14:06 +01001100SYSCALL_DEFINE0(setsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Oren Laadane19f2472006-01-08 01:03:58 -08001102 struct task_struct *group_leader = current->group_leader;
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001103 struct pid *sid = task_pid(group_leader);
1104 pid_t session = pid_vnr(sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 int err = -EPERM;
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 write_lock_irq(&tasklist_lock);
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001108 /* Fail if I am already a session leader */
1109 if (group_leader->signal->leader)
1110 goto out;
1111
Oleg Nesterov430c6232008-02-08 04:19:11 -08001112 /* Fail if a process group id already exists that equals the
1113 * proposed session id.
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001114 */
Oleg Nesterov6806aac2008-02-08 04:19:12 -08001115 if (pid_task(sid, PIDTYPE_PGID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 goto out;
1117
Oren Laadane19f2472006-01-08 01:03:58 -08001118 group_leader->signal->leader = 1;
Oleg Nesterov81dabb42013-07-03 15:08:26 -07001119 set_special_pids(sid);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001120
Alan Cox9c9f4de2008-10-13 10:37:26 +01001121 proc_clear_tty(group_leader);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001122
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001123 err = session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124out:
1125 write_unlock_irq(&tasklist_lock);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001126 if (err > 0) {
Christian Borntraeger0d0df592009-10-26 16:49:34 -07001127 proc_sid_connector(group_leader);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001128 sched_autogroup_create_attach(group_leader);
1129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return err;
1131}
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133DECLARE_RWSEM(uts_sem);
1134
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001135#ifdef COMPAT_UTS_MACHINE
1136#define override_architecture(name) \
Andreas Schwab46da2762010-04-23 13:17:44 -04001137 (personality(current->personality) == PER_LINUX32 && \
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001138 copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1139 sizeof(COMPAT_UTS_MACHINE)))
1140#else
1141#define override_architecture(name) 0
1142#endif
1143
Andi Kleenbe274252011-08-19 16:15:10 -07001144/*
1145 * Work around broken programs that cannot handle "Linux 3.0".
1146 * 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 -08001147 * 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 -07001148 */
Kees Cook2702b152012-10-19 13:56:51 -07001149static int override_release(char __user *release, size_t len)
Andi Kleenbe274252011-08-19 16:15:10 -07001150{
1151 int ret = 0;
Andi Kleenbe274252011-08-19 16:15:10 -07001152
1153 if (current->personality & UNAME26) {
Kees Cook2702b152012-10-19 13:56:51 -07001154 const char *rest = UTS_RELEASE;
1155 char buf[65] = { 0 };
Andi Kleenbe274252011-08-19 16:15:10 -07001156 int ndots = 0;
1157 unsigned v;
Kees Cook2702b152012-10-19 13:56:51 -07001158 size_t copy;
Andi Kleenbe274252011-08-19 16:15:10 -07001159
1160 while (*rest) {
1161 if (*rest == '.' && ++ndots >= 3)
1162 break;
1163 if (!isdigit(*rest) && *rest != '.')
1164 break;
1165 rest++;
1166 }
Jon DeVree39afb5e2015-02-27 15:52:07 -08001167 v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 60;
Kees Cook31fd84b92012-10-19 18:45:53 -07001168 copy = clamp_t(size_t, len, 1, sizeof(buf));
Kees Cook2702b152012-10-19 13:56:51 -07001169 copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
1170 ret = copy_to_user(release, buf, copy + 1);
Andi Kleenbe274252011-08-19 16:15:10 -07001171 }
1172 return ret;
1173}
1174
Heiko Carstense48fbb62009-01-14 14:14:26 +01001175SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 int errno = 0;
1178
1179 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001180 if (copy_to_user(name, utsname(), sizeof *name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 errno = -EFAULT;
1182 up_read(&uts_sem);
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001183
Andi Kleenbe274252011-08-19 16:15:10 -07001184 if (!errno && override_release(name->release, sizeof(name->release)))
1185 errno = -EFAULT;
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001186 if (!errno && override_architecture(name))
1187 errno = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return errno;
1189}
1190
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001191#ifdef __ARCH_WANT_SYS_OLD_UNAME
1192/*
1193 * Old cruft
1194 */
1195SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
1196{
1197 int error = 0;
1198
1199 if (!name)
1200 return -EFAULT;
1201
1202 down_read(&uts_sem);
1203 if (copy_to_user(name, utsname(), sizeof(*name)))
1204 error = -EFAULT;
1205 up_read(&uts_sem);
1206
Andi Kleenbe274252011-08-19 16:15:10 -07001207 if (!error && override_release(name->release, sizeof(name->release)))
1208 error = -EFAULT;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001209 if (!error && override_architecture(name))
1210 error = -EFAULT;
1211 return error;
1212}
1213
1214SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
1215{
1216 int error;
1217
1218 if (!name)
1219 return -EFAULT;
1220 if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
1221 return -EFAULT;
1222
1223 down_read(&uts_sem);
1224 error = __copy_to_user(&name->sysname, &utsname()->sysname,
1225 __OLD_UTS_LEN);
1226 error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
1227 error |= __copy_to_user(&name->nodename, &utsname()->nodename,
1228 __OLD_UTS_LEN);
1229 error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
1230 error |= __copy_to_user(&name->release, &utsname()->release,
1231 __OLD_UTS_LEN);
1232 error |= __put_user(0, name->release + __OLD_UTS_LEN);
1233 error |= __copy_to_user(&name->version, &utsname()->version,
1234 __OLD_UTS_LEN);
1235 error |= __put_user(0, name->version + __OLD_UTS_LEN);
1236 error |= __copy_to_user(&name->machine, &utsname()->machine,
1237 __OLD_UTS_LEN);
1238 error |= __put_user(0, name->machine + __OLD_UTS_LEN);
1239 up_read(&uts_sem);
1240
1241 if (!error && override_architecture(name))
1242 error = -EFAULT;
Andi Kleenbe274252011-08-19 16:15:10 -07001243 if (!error && override_release(name->release, sizeof(name->release)))
1244 error = -EFAULT;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001245 return error ? -EFAULT : 0;
1246}
1247#endif
1248
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001249SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 int errno;
1252 char tmp[__NEW_UTS_LEN];
1253
Serge E. Hallynbb96a6f2011-03-23 16:43:18 -07001254 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (len < 0 || len > __NEW_UTS_LEN)
1258 return -EINVAL;
1259 down_write(&uts_sem);
1260 errno = -EFAULT;
1261 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001262 struct new_utsname *u = utsname();
1263
1264 memcpy(u->nodename, tmp, len);
1265 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 errno = 0;
Sasikantha babu499eea62012-05-31 16:26:07 -07001267 uts_proc_notify(UTS_PROC_HOSTNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269 up_write(&uts_sem);
1270 return errno;
1271}
1272
1273#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1274
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001275SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 int i, errno;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001278 struct new_utsname *u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 if (len < 0)
1281 return -EINVAL;
1282 down_read(&uts_sem);
Andrew Morton9679e4d2008-10-15 22:01:51 -07001283 u = utsname();
1284 i = 1 + strlen(u->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (i > len)
1286 i = len;
1287 errno = 0;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001288 if (copy_to_user(name, u->nodename, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 errno = -EFAULT;
1290 up_read(&uts_sem);
1291 return errno;
1292}
1293
1294#endif
1295
1296/*
1297 * Only setdomainname; getdomainname can be implemented by calling
1298 * uname()
1299 */
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001300SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 int errno;
1303 char tmp[__NEW_UTS_LEN];
1304
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001305 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return -EPERM;
1307 if (len < 0 || len > __NEW_UTS_LEN)
1308 return -EINVAL;
1309
1310 down_write(&uts_sem);
1311 errno = -EFAULT;
1312 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001313 struct new_utsname *u = utsname();
1314
1315 memcpy(u->domainname, tmp, len);
1316 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 errno = 0;
Sasikantha babu499eea62012-05-31 16:26:07 -07001318 uts_proc_notify(UTS_PROC_DOMAINNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320 up_write(&uts_sem);
1321 return errno;
1322}
1323
Heiko Carstense48fbb62009-01-14 14:14:26 +01001324SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Jiri Slabyb9518342010-05-04 11:28:25 +02001326 struct rlimit value;
1327 int ret;
1328
1329 ret = do_prlimit(current, resource, NULL, &value);
1330 if (!ret)
1331 ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1332
1333 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
Al Virod9e968c2017-05-31 04:33:51 -04001336#ifdef CONFIG_COMPAT
1337
1338COMPAT_SYSCALL_DEFINE2(setrlimit, unsigned int, resource,
1339 struct compat_rlimit __user *, rlim)
1340{
1341 struct rlimit r;
1342 struct compat_rlimit r32;
1343
1344 if (copy_from_user(&r32, rlim, sizeof(struct compat_rlimit)))
1345 return -EFAULT;
1346
1347 if (r32.rlim_cur == COMPAT_RLIM_INFINITY)
1348 r.rlim_cur = RLIM_INFINITY;
1349 else
1350 r.rlim_cur = r32.rlim_cur;
1351 if (r32.rlim_max == COMPAT_RLIM_INFINITY)
1352 r.rlim_max = RLIM_INFINITY;
1353 else
1354 r.rlim_max = r32.rlim_max;
1355 return do_prlimit(current, resource, &r, NULL);
1356}
1357
1358COMPAT_SYSCALL_DEFINE2(getrlimit, unsigned int, resource,
1359 struct compat_rlimit __user *, rlim)
1360{
1361 struct rlimit r;
1362 int ret;
1363
1364 ret = do_prlimit(current, resource, NULL, &r);
1365 if (!ret) {
Al Viro58c7ffc2017-07-12 04:59:45 +01001366 struct compat_rlimit r32;
Al Virod9e968c2017-05-31 04:33:51 -04001367 if (r.rlim_cur > COMPAT_RLIM_INFINITY)
1368 r32.rlim_cur = COMPAT_RLIM_INFINITY;
1369 else
1370 r32.rlim_cur = r.rlim_cur;
1371 if (r.rlim_max > COMPAT_RLIM_INFINITY)
1372 r32.rlim_max = COMPAT_RLIM_INFINITY;
1373 else
1374 r32.rlim_max = r.rlim_max;
1375
1376 if (copy_to_user(rlim, &r32, sizeof(struct compat_rlimit)))
1377 return -EFAULT;
1378 }
1379 return ret;
1380}
1381
1382#endif
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1385
1386/*
1387 * Back compatibility for getrlimit. Needed for some apps.
1388 */
Heiko Carstense48fbb62009-01-14 14:14:26 +01001389SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1390 struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 struct rlimit x;
1393 if (resource >= RLIM_NLIMITS)
1394 return -EINVAL;
1395
1396 task_lock(current->group_leader);
1397 x = current->signal->rlim[resource];
1398 task_unlock(current->group_leader);
Cal Peake756184b2006-09-30 23:27:24 -07001399 if (x.rlim_cur > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 x.rlim_cur = 0x7FFFFFFF;
Cal Peake756184b2006-09-30 23:27:24 -07001401 if (x.rlim_max > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 x.rlim_max = 0x7FFFFFFF;
vishnu.psec94fc32014-10-09 15:30:23 -07001403 return copy_to_user(rlim, &x, sizeof(x)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
Al Viro613763a2017-05-26 22:04:29 -04001406#ifdef CONFIG_COMPAT
1407COMPAT_SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1408 struct compat_rlimit __user *, rlim)
1409{
1410 struct rlimit r;
1411
1412 if (resource >= RLIM_NLIMITS)
1413 return -EINVAL;
1414
1415 task_lock(current->group_leader);
1416 r = current->signal->rlim[resource];
1417 task_unlock(current->group_leader);
1418 if (r.rlim_cur > 0x7FFFFFFF)
1419 r.rlim_cur = 0x7FFFFFFF;
1420 if (r.rlim_max > 0x7FFFFFFF)
1421 r.rlim_max = 0x7FFFFFFF;
1422
1423 if (put_user(r.rlim_cur, &rlim->rlim_cur) ||
1424 put_user(r.rlim_max, &rlim->rlim_max))
1425 return -EFAULT;
1426 return 0;
1427}
1428#endif
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430#endif
1431
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001432static inline bool rlim64_is_infinity(__u64 rlim64)
1433{
1434#if BITS_PER_LONG < 64
1435 return rlim64 >= ULONG_MAX;
1436#else
1437 return rlim64 == RLIM64_INFINITY;
1438#endif
1439}
1440
1441static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1442{
1443 if (rlim->rlim_cur == RLIM_INFINITY)
1444 rlim64->rlim_cur = RLIM64_INFINITY;
1445 else
1446 rlim64->rlim_cur = rlim->rlim_cur;
1447 if (rlim->rlim_max == RLIM_INFINITY)
1448 rlim64->rlim_max = RLIM64_INFINITY;
1449 else
1450 rlim64->rlim_max = rlim->rlim_max;
1451}
1452
1453static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1454{
1455 if (rlim64_is_infinity(rlim64->rlim_cur))
1456 rlim->rlim_cur = RLIM_INFINITY;
1457 else
1458 rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1459 if (rlim64_is_infinity(rlim64->rlim_max))
1460 rlim->rlim_max = RLIM_INFINITY;
1461 else
1462 rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1463}
1464
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001465/* make sure you are allowed to change @tsk limits before calling this */
Jiri Slaby5b415352010-03-24 16:11:29 +01001466int do_prlimit(struct task_struct *tsk, unsigned int resource,
1467 struct rlimit *new_rlim, struct rlimit *old_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Jiri Slaby5b415352010-03-24 16:11:29 +01001469 struct rlimit *rlim;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001470 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 if (resource >= RLIM_NLIMITS)
1473 return -EINVAL;
Jiri Slaby5b415352010-03-24 16:11:29 +01001474 if (new_rlim) {
1475 if (new_rlim->rlim_cur > new_rlim->rlim_max)
1476 return -EINVAL;
1477 if (resource == RLIMIT_NOFILE &&
1478 new_rlim->rlim_max > sysctl_nr_open)
1479 return -EPERM;
1480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001482 /* protect tsk->signal and tsk->sighand from disappearing */
1483 read_lock(&tasklist_lock);
1484 if (!tsk->sighand) {
1485 retval = -ESRCH;
1486 goto out;
1487 }
1488
Jiri Slaby5b415352010-03-24 16:11:29 +01001489 rlim = tsk->signal->rlim + resource;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001490 task_lock(tsk->group_leader);
Jiri Slaby5b415352010-03-24 16:11:29 +01001491 if (new_rlim) {
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001492 /* Keep the capable check against init_user_ns until
1493 cgroups can contain all limits */
Jiri Slaby5b415352010-03-24 16:11:29 +01001494 if (new_rlim->rlim_max > rlim->rlim_max &&
1495 !capable(CAP_SYS_RESOURCE))
1496 retval = -EPERM;
1497 if (!retval)
Eric W. Biedermancad4ea52017-04-12 17:22:14 -05001498 retval = security_task_setrlimit(tsk, resource, new_rlim);
Jiri Slaby5b415352010-03-24 16:11:29 +01001499 if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
1500 /*
1501 * The caller is asking for an immediate RLIMIT_CPU
1502 * expiry. But we use the zero value to mean "it was
1503 * never set". So let's cheat and make it one second
1504 * instead
1505 */
1506 new_rlim->rlim_cur = 1;
1507 }
Tom Alsberg9926e4c2007-05-08 00:30:31 -07001508 }
Jiri Slaby5b415352010-03-24 16:11:29 +01001509 if (!retval) {
1510 if (old_rlim)
1511 *old_rlim = *rlim;
1512 if (new_rlim)
1513 *rlim = *new_rlim;
1514 }
Jiri Slaby7855c352009-08-26 23:45:34 +02001515 task_unlock(tsk->group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Andrew Mortond3561f72006-03-24 03:18:36 -08001517 /*
1518 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1519 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1520 * very long-standing error, and fixing it now risks breakage of
1521 * applications, so we live with it
1522 */
Jiri Slaby5b415352010-03-24 16:11:29 +01001523 if (!retval && new_rlim && resource == RLIMIT_CPU &&
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05001524 new_rlim->rlim_cur != RLIM_INFINITY &&
1525 IS_ENABLED(CONFIG_POSIX_TIMERS))
Jiri Slaby5b415352010-03-24 16:11:29 +01001526 update_rlimit_cpu(tsk, new_rlim->rlim_cur);
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001527out:
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001528 read_unlock(&tasklist_lock);
Oleg Nesterov2fb9d262009-09-03 19:21:45 +02001529 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001532/* rcu lock must be held */
Stephen Smalley791ec492017-02-17 07:57:00 -05001533static int check_prlimit_permission(struct task_struct *task,
1534 unsigned int flags)
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001535{
1536 const struct cred *cred = current_cred(), *tcred;
Stephen Smalley791ec492017-02-17 07:57:00 -05001537 bool id_match;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001538
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001539 if (current == task)
1540 return 0;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001541
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001542 tcred = __task_cred(task);
Stephen Smalley791ec492017-02-17 07:57:00 -05001543 id_match = (uid_eq(cred->uid, tcred->euid) &&
1544 uid_eq(cred->uid, tcred->suid) &&
1545 uid_eq(cred->uid, tcred->uid) &&
1546 gid_eq(cred->gid, tcred->egid) &&
1547 gid_eq(cred->gid, tcred->sgid) &&
1548 gid_eq(cred->gid, tcred->gid));
1549 if (!id_match && !ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
1550 return -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001551
Stephen Smalley791ec492017-02-17 07:57:00 -05001552 return security_task_prlimit(cred, tcred, flags);
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001553}
1554
1555SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1556 const struct rlimit64 __user *, new_rlim,
1557 struct rlimit64 __user *, old_rlim)
1558{
1559 struct rlimit64 old64, new64;
1560 struct rlimit old, new;
1561 struct task_struct *tsk;
Stephen Smalley791ec492017-02-17 07:57:00 -05001562 unsigned int checkflags = 0;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001563 int ret;
1564
Stephen Smalley791ec492017-02-17 07:57:00 -05001565 if (old_rlim)
1566 checkflags |= LSM_PRLIMIT_READ;
1567
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001568 if (new_rlim) {
1569 if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1570 return -EFAULT;
1571 rlim64_to_rlim(&new64, &new);
Stephen Smalley791ec492017-02-17 07:57:00 -05001572 checkflags |= LSM_PRLIMIT_WRITE;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001573 }
1574
1575 rcu_read_lock();
1576 tsk = pid ? find_task_by_vpid(pid) : current;
1577 if (!tsk) {
1578 rcu_read_unlock();
1579 return -ESRCH;
1580 }
Stephen Smalley791ec492017-02-17 07:57:00 -05001581 ret = check_prlimit_permission(tsk, checkflags);
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001582 if (ret) {
1583 rcu_read_unlock();
1584 return ret;
1585 }
1586 get_task_struct(tsk);
1587 rcu_read_unlock();
1588
1589 ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1590 old_rlim ? &old : NULL);
1591
1592 if (!ret && old_rlim) {
1593 rlim_to_rlim64(&old, &old64);
1594 if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1595 ret = -EFAULT;
1596 }
1597
1598 put_task_struct(tsk);
1599 return ret;
1600}
1601
Jiri Slaby7855c352009-08-26 23:45:34 +02001602SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1603{
1604 struct rlimit new_rlim;
1605
1606 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1607 return -EFAULT;
Jiri Slaby5b415352010-03-24 16:11:29 +01001608 return do_prlimit(current, resource, &new_rlim, NULL);
Jiri Slaby7855c352009-08-26 23:45:34 +02001609}
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611/*
1612 * It would make sense to put struct rusage in the task_struct,
1613 * except that would make the task_struct be *really big*. After
1614 * task_struct gets moved into malloc'ed memory, it would
1615 * make sense to do this. It will make moving the rest of the information
1616 * a lot simpler! (Which we're not doing right now because we're not
1617 * measuring them yet).
1618 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1620 * races with threads incrementing their own counters. But since word
1621 * reads are atomic, we either get new values or old values and we don't
1622 * care which for the sums. We always take the siglock to protect reading
1623 * the c* fields from p->signal from races with exit.c updating those
1624 * fields when reaping, so a sample either gets all the additions of a
1625 * given child after it's reaped, or none so this sample is before reaping.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001626 *
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001627 * Locking:
1628 * We need to take the siglock for CHILDEREN, SELF and BOTH
1629 * for the cases current multithreaded, non-current single threaded
1630 * non-current multithreaded. Thread traversal is now safe with
1631 * the siglock held.
1632 * Strictly speaking, we donot need to take the siglock if we are current and
1633 * single threaded, as no one else can take our signal_struct away, no one
1634 * else can reap the children to update signal->c* counters, and no one else
1635 * can race with the signal-> fields. If we do not take any lock, the
1636 * signal-> fields could be read out of order while another thread was just
1637 * exiting. So we should place a read memory barrier when we avoid the lock.
1638 * On the writer side, write memory barrier is implied in __exit_signal
1639 * as __exit_signal releases the siglock spinlock after updating the signal->
1640 * fields. But we don't do this yet to keep things simple.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001641 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 */
1643
Frank Mayharf06febc2008-09-12 09:54:39 -07001644static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001645{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001646 r->ru_nvcsw += t->nvcsw;
1647 r->ru_nivcsw += t->nivcsw;
1648 r->ru_minflt += t->min_flt;
1649 r->ru_majflt += t->maj_flt;
1650 r->ru_inblock += task_io_get_inblock(t);
1651 r->ru_oublock += task_io_get_oublock(t);
1652}
1653
Al Viroce72a162017-05-14 20:25:02 -04001654void getrusage(struct task_struct *p, int who, struct rusage *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
1656 struct task_struct *t;
1657 unsigned long flags;
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001658 u64 tgutime, tgstime, utime, stime;
Jiri Pirko1f102062009-09-22 16:44:10 -07001659 unsigned long maxrss = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
vishnu.psec94fc32014-10-09 15:30:23 -07001661 memset((char *)r, 0, sizeof (*r));
Martin Schwidefsky64861632011-12-15 14:56:09 +01001662 utime = stime = 0;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001663
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001664 if (who == RUSAGE_THREAD) {
Frederic Weisbeckere80d0a1a2012-11-21 16:26:44 +01001665 task_cputime_adjusted(current, &utime, &stime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001666 accumulate_thread_rusage(p, r);
Jiri Pirko1f102062009-09-22 16:44:10 -07001667 maxrss = p->signal->maxrss;
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001668 goto out;
1669 }
1670
Oleg Nesterovd6cf7232008-04-30 00:52:38 -07001671 if (!lock_task_sighand(p, &flags))
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001672 return;
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 switch (who) {
vishnu.psec94fc32014-10-09 15:30:23 -07001675 case RUSAGE_BOTH:
1676 case RUSAGE_CHILDREN:
1677 utime = p->signal->cutime;
1678 stime = p->signal->cstime;
1679 r->ru_nvcsw = p->signal->cnvcsw;
1680 r->ru_nivcsw = p->signal->cnivcsw;
1681 r->ru_minflt = p->signal->cmin_flt;
1682 r->ru_majflt = p->signal->cmaj_flt;
1683 r->ru_inblock = p->signal->cinblock;
1684 r->ru_oublock = p->signal->coublock;
1685 maxrss = p->signal->cmaxrss;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001686
vishnu.psec94fc32014-10-09 15:30:23 -07001687 if (who == RUSAGE_CHILDREN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 break;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001689
vishnu.psec94fc32014-10-09 15:30:23 -07001690 case RUSAGE_SELF:
1691 thread_group_cputime_adjusted(p, &tgutime, &tgstime);
1692 utime += tgutime;
1693 stime += tgstime;
1694 r->ru_nvcsw += p->signal->nvcsw;
1695 r->ru_nivcsw += p->signal->nivcsw;
1696 r->ru_minflt += p->signal->min_flt;
1697 r->ru_majflt += p->signal->maj_flt;
1698 r->ru_inblock += p->signal->inblock;
1699 r->ru_oublock += p->signal->oublock;
1700 if (maxrss < p->signal->maxrss)
1701 maxrss = p->signal->maxrss;
1702 t = p;
1703 do {
1704 accumulate_thread_rusage(t, r);
1705 } while_each_thread(p, t);
1706 break;
1707
1708 default:
1709 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001711 unlock_task_sighand(p, &flags);
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001712
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001713out:
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001714 r->ru_utime = ns_to_timeval(utime);
1715 r->ru_stime = ns_to_timeval(stime);
Jiri Pirko1f102062009-09-22 16:44:10 -07001716
1717 if (who != RUSAGE_CHILDREN) {
1718 struct mm_struct *mm = get_task_mm(p);
vishnu.psec94fc32014-10-09 15:30:23 -07001719
Jiri Pirko1f102062009-09-22 16:44:10 -07001720 if (mm) {
1721 setmax_mm_hiwater_rss(&maxrss, mm);
1722 mmput(mm);
1723 }
1724 }
1725 r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
Al Viroce72a162017-05-14 20:25:02 -04001728SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
1730 struct rusage r;
vishnu.psec94fc32014-10-09 15:30:23 -07001731
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001732 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1733 who != RUSAGE_THREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return -EINVAL;
Al Viroce72a162017-05-14 20:25:02 -04001735
1736 getrusage(current, who, &r);
1737 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
1739
Al Viro8d2d5c42013-03-03 12:49:06 -05001740#ifdef CONFIG_COMPAT
1741COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru)
1742{
1743 struct rusage r;
1744
1745 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1746 who != RUSAGE_THREAD)
1747 return -EINVAL;
1748
Al Viroce72a162017-05-14 20:25:02 -04001749 getrusage(current, who, &r);
Al Viro8d2d5c42013-03-03 12:49:06 -05001750 return put_compat_rusage(&r, ru);
1751}
1752#endif
1753
Heiko Carstense48fbb62009-01-14 14:14:26 +01001754SYSCALL_DEFINE1(umask, int, mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
1756 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1757 return mask;
1758}
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -08001759
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001760static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001761{
Al Viro2903ff02012-08-28 12:52:22 -04001762 struct fd exe;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001763 struct file *old_exe, *exe_file;
Al Viro496ad9a2013-01-23 17:07:38 -05001764 struct inode *inode;
Al Viro2903ff02012-08-28 12:52:22 -04001765 int err;
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001766
Al Viro2903ff02012-08-28 12:52:22 -04001767 exe = fdget(fd);
1768 if (!exe.file)
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001769 return -EBADF;
1770
Al Viro496ad9a2013-01-23 17:07:38 -05001771 inode = file_inode(exe.file);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001772
1773 /*
1774 * Because the original mm->exe_file points to executable file, make
1775 * sure that this one is executable as well, to avoid breaking an
1776 * overall picture.
1777 */
1778 err = -EACCES;
Eric W. Biederman90f85722015-06-29 14:42:03 -05001779 if (!S_ISREG(inode->i_mode) || path_noexec(&exe.file->f_path))
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001780 goto exit;
1781
Al Viro496ad9a2013-01-23 17:07:38 -05001782 err = inode_permission(inode, MAY_EXEC);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001783 if (err)
1784 goto exit;
1785
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001786 /*
Konstantin Khlebnikov4229fb1d2012-07-11 14:02:11 -07001787 * Forbid mm->exe_file change if old file still mapped.
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001788 */
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001789 exe_file = get_mm_exe_file(mm);
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001790 err = -EBUSY;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001791 if (exe_file) {
Konstantin Khlebnikov4229fb1d2012-07-11 14:02:11 -07001792 struct vm_area_struct *vma;
1793
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001794 down_read(&mm->mmap_sem);
1795 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1796 if (!vma->vm_file)
1797 continue;
1798 if (path_equal(&vma->vm_file->f_path,
1799 &exe_file->f_path))
1800 goto exit_err;
1801 }
1802
1803 up_read(&mm->mmap_sem);
1804 fput(exe_file);
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001805 }
1806
Konstantin Khlebnikov4229fb1d2012-07-11 14:02:11 -07001807 err = 0;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001808 /* set the new file, lockless */
1809 get_file(exe.file);
1810 old_exe = xchg(&mm->exe_file, exe.file);
1811 if (old_exe)
1812 fput(old_exe);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001813exit:
Al Viro2903ff02012-08-28 12:52:22 -04001814 fdput(exe);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001815 return err;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001816exit_err:
1817 up_read(&mm->mmap_sem);
1818 fput(exe_file);
1819 goto exit;
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001820}
1821
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001822/*
1823 * WARNING: we don't require any capability here so be very careful
1824 * in what is allowed for modification from userspace.
1825 */
1826static int validate_prctl_map(struct prctl_mm_map *prctl_map)
1827{
1828 unsigned long mmap_max_addr = TASK_SIZE;
1829 struct mm_struct *mm = current->mm;
1830 int error = -EINVAL, i;
1831
1832 static const unsigned char offsets[] = {
1833 offsetof(struct prctl_mm_map, start_code),
1834 offsetof(struct prctl_mm_map, end_code),
1835 offsetof(struct prctl_mm_map, start_data),
1836 offsetof(struct prctl_mm_map, end_data),
1837 offsetof(struct prctl_mm_map, start_brk),
1838 offsetof(struct prctl_mm_map, brk),
1839 offsetof(struct prctl_mm_map, start_stack),
1840 offsetof(struct prctl_mm_map, arg_start),
1841 offsetof(struct prctl_mm_map, arg_end),
1842 offsetof(struct prctl_mm_map, env_start),
1843 offsetof(struct prctl_mm_map, env_end),
1844 };
1845
1846 /*
1847 * Make sure the members are not somewhere outside
1848 * of allowed address space.
1849 */
1850 for (i = 0; i < ARRAY_SIZE(offsets); i++) {
1851 u64 val = *(u64 *)((char *)prctl_map + offsets[i]);
1852
1853 if ((unsigned long)val >= mmap_max_addr ||
1854 (unsigned long)val < mmap_min_addr)
1855 goto out;
1856 }
1857
1858 /*
1859 * Make sure the pairs are ordered.
1860 */
1861#define __prctl_check_order(__m1, __op, __m2) \
1862 ((unsigned long)prctl_map->__m1 __op \
1863 (unsigned long)prctl_map->__m2) ? 0 : -EINVAL
1864 error = __prctl_check_order(start_code, <, end_code);
1865 error |= __prctl_check_order(start_data, <, end_data);
1866 error |= __prctl_check_order(start_brk, <=, brk);
1867 error |= __prctl_check_order(arg_start, <=, arg_end);
1868 error |= __prctl_check_order(env_start, <=, env_end);
1869 if (error)
1870 goto out;
1871#undef __prctl_check_order
1872
1873 error = -EINVAL;
1874
1875 /*
1876 * @brk should be after @end_data in traditional maps.
1877 */
1878 if (prctl_map->start_brk <= prctl_map->end_data ||
1879 prctl_map->brk <= prctl_map->end_data)
1880 goto out;
1881
1882 /*
1883 * Neither we should allow to override limits if they set.
1884 */
1885 if (check_data_rlimit(rlimit(RLIMIT_DATA), prctl_map->brk,
1886 prctl_map->start_brk, prctl_map->end_data,
1887 prctl_map->start_data))
1888 goto out;
1889
1890 /*
1891 * Someone is trying to cheat the auxv vector.
1892 */
1893 if (prctl_map->auxv_size) {
1894 if (!prctl_map->auxv || prctl_map->auxv_size > sizeof(mm->saved_auxv))
1895 goto out;
1896 }
1897
1898 /*
1899 * Finally, make sure the caller has the rights to
Kirill Tkhai4d28df62017-05-12 17:33:36 +03001900 * change /proc/pid/exe link: only local sys admin should
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001901 * be allowed to.
1902 */
1903 if (prctl_map->exe_fd != (u32)-1) {
Kirill Tkhai4d28df62017-05-12 17:33:36 +03001904 if (!ns_capable(current_user_ns(), CAP_SYS_ADMIN))
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001905 goto out;
1906 }
1907
1908 error = 0;
1909out:
1910 return error;
1911}
1912
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07001913#ifdef CONFIG_CHECKPOINT_RESTORE
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001914static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data_size)
1915{
1916 struct prctl_mm_map prctl_map = { .exe_fd = (u32)-1, };
1917 unsigned long user_auxv[AT_VECTOR_SIZE];
1918 struct mm_struct *mm = current->mm;
1919 int error;
1920
1921 BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
1922 BUILD_BUG_ON(sizeof(struct prctl_mm_map) > 256);
1923
1924 if (opt == PR_SET_MM_MAP_SIZE)
1925 return put_user((unsigned int)sizeof(prctl_map),
1926 (unsigned int __user *)addr);
1927
1928 if (data_size != sizeof(prctl_map))
1929 return -EINVAL;
1930
1931 if (copy_from_user(&prctl_map, addr, sizeof(prctl_map)))
1932 return -EFAULT;
1933
1934 error = validate_prctl_map(&prctl_map);
1935 if (error)
1936 return error;
1937
1938 if (prctl_map.auxv_size) {
1939 memset(user_auxv, 0, sizeof(user_auxv));
1940 if (copy_from_user(user_auxv,
1941 (const void __user *)prctl_map.auxv,
1942 prctl_map.auxv_size))
1943 return -EFAULT;
1944
1945 /* Last entry must be AT_NULL as specification requires */
1946 user_auxv[AT_VECTOR_SIZE - 2] = AT_NULL;
1947 user_auxv[AT_VECTOR_SIZE - 1] = AT_NULL;
1948 }
1949
Mateusz Guzikddf1d392016-01-20 15:01:02 -08001950 if (prctl_map.exe_fd != (u32)-1) {
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001951 error = prctl_set_mm_exe_file(mm, prctl_map.exe_fd);
Mateusz Guzikddf1d392016-01-20 15:01:02 -08001952 if (error)
1953 return error;
1954 }
1955
1956 down_write(&mm->mmap_sem);
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001957
1958 /*
1959 * We don't validate if these members are pointing to
1960 * real present VMAs because application may have correspond
1961 * VMAs already unmapped and kernel uses these members for statistics
1962 * output in procfs mostly, except
1963 *
1964 * - @start_brk/@brk which are used in do_brk but kernel lookups
1965 * for VMAs when updating these memvers so anything wrong written
1966 * here cause kernel to swear at userspace program but won't lead
1967 * to any problem in kernel itself
1968 */
1969
1970 mm->start_code = prctl_map.start_code;
1971 mm->end_code = prctl_map.end_code;
1972 mm->start_data = prctl_map.start_data;
1973 mm->end_data = prctl_map.end_data;
1974 mm->start_brk = prctl_map.start_brk;
1975 mm->brk = prctl_map.brk;
1976 mm->start_stack = prctl_map.start_stack;
1977 mm->arg_start = prctl_map.arg_start;
1978 mm->arg_end = prctl_map.arg_end;
1979 mm->env_start = prctl_map.env_start;
1980 mm->env_end = prctl_map.env_end;
1981
1982 /*
1983 * Note this update of @saved_auxv is lockless thus
1984 * if someone reads this member in procfs while we're
1985 * updating -- it may get partly updated results. It's
1986 * known and acceptable trade off: we leave it as is to
1987 * not introduce additional locks here making the kernel
1988 * more complex.
1989 */
1990 if (prctl_map.auxv_size)
1991 memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
1992
Mateusz Guzikddf1d392016-01-20 15:01:02 -08001993 up_write(&mm->mmap_sem);
1994 return 0;
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001995}
1996#endif /* CONFIG_CHECKPOINT_RESTORE */
1997
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07001998static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr,
1999 unsigned long len)
2000{
2001 /*
2002 * This doesn't move the auxiliary vector itself since it's pinned to
2003 * mm_struct, but it permits filling the vector with new values. It's
2004 * up to the caller to provide sane values here, otherwise userspace
2005 * tools which use this vector might be unhappy.
2006 */
2007 unsigned long user_auxv[AT_VECTOR_SIZE];
2008
2009 if (len > sizeof(user_auxv))
2010 return -EINVAL;
2011
2012 if (copy_from_user(user_auxv, (const void __user *)addr, len))
2013 return -EFAULT;
2014
2015 /* Make sure the last entry is always AT_NULL */
2016 user_auxv[AT_VECTOR_SIZE - 2] = 0;
2017 user_auxv[AT_VECTOR_SIZE - 1] = 0;
2018
2019 BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
2020
2021 task_lock(current);
2022 memcpy(mm->saved_auxv, user_auxv, len);
2023 task_unlock(current);
2024
2025 return 0;
2026}
2027
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002028static int prctl_set_mm(int opt, unsigned long addr,
2029 unsigned long arg4, unsigned long arg5)
2030{
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002031 struct mm_struct *mm = current->mm;
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002032 struct prctl_mm_map prctl_map;
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002033 struct vm_area_struct *vma;
2034 int error;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002035
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07002036 if (arg5 || (arg4 && (opt != PR_SET_MM_AUXV &&
2037 opt != PR_SET_MM_MAP &&
2038 opt != PR_SET_MM_MAP_SIZE)))
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002039 return -EINVAL;
2040
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07002041#ifdef CONFIG_CHECKPOINT_RESTORE
2042 if (opt == PR_SET_MM_MAP || opt == PR_SET_MM_MAP_SIZE)
2043 return prctl_set_mm_map(opt, (const void __user *)addr, arg4);
2044#endif
2045
Cyrill Gorcunov79f07132012-03-15 15:17:10 -07002046 if (!capable(CAP_SYS_RESOURCE))
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002047 return -EPERM;
2048
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07002049 if (opt == PR_SET_MM_EXE_FILE)
2050 return prctl_set_mm_exe_file(mm, (unsigned int)addr);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07002051
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002052 if (opt == PR_SET_MM_AUXV)
2053 return prctl_set_auxv(mm, addr, arg4);
2054
Cyrill Gorcunov1ad75b92012-06-07 14:21:11 -07002055 if (addr >= TASK_SIZE || addr < mmap_min_addr)
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002056 return -EINVAL;
2057
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002058 error = -EINVAL;
2059
Mateusz Guzikddf1d392016-01-20 15:01:02 -08002060 down_write(&mm->mmap_sem);
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002061 vma = find_vma(mm, addr);
2062
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002063 prctl_map.start_code = mm->start_code;
2064 prctl_map.end_code = mm->end_code;
2065 prctl_map.start_data = mm->start_data;
2066 prctl_map.end_data = mm->end_data;
2067 prctl_map.start_brk = mm->start_brk;
2068 prctl_map.brk = mm->brk;
2069 prctl_map.start_stack = mm->start_stack;
2070 prctl_map.arg_start = mm->arg_start;
2071 prctl_map.arg_end = mm->arg_end;
2072 prctl_map.env_start = mm->env_start;
2073 prctl_map.env_end = mm->env_end;
2074 prctl_map.auxv = NULL;
2075 prctl_map.auxv_size = 0;
2076 prctl_map.exe_fd = -1;
2077
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002078 switch (opt) {
2079 case PR_SET_MM_START_CODE:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002080 prctl_map.start_code = addr;
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002081 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002082 case PR_SET_MM_END_CODE:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002083 prctl_map.end_code = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002084 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002085 case PR_SET_MM_START_DATA:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002086 prctl_map.start_data = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002087 break;
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002088 case PR_SET_MM_END_DATA:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002089 prctl_map.end_data = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002090 break;
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002091 case PR_SET_MM_START_STACK:
2092 prctl_map.start_stack = addr;
2093 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002094 case PR_SET_MM_START_BRK:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002095 prctl_map.start_brk = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002096 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002097 case PR_SET_MM_BRK:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002098 prctl_map.brk = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002099 break;
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002100 case PR_SET_MM_ARG_START:
2101 prctl_map.arg_start = addr;
2102 break;
2103 case PR_SET_MM_ARG_END:
2104 prctl_map.arg_end = addr;
2105 break;
2106 case PR_SET_MM_ENV_START:
2107 prctl_map.env_start = addr;
2108 break;
2109 case PR_SET_MM_ENV_END:
2110 prctl_map.env_end = addr;
2111 break;
2112 default:
2113 goto out;
2114 }
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002115
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002116 error = validate_prctl_map(&prctl_map);
2117 if (error)
2118 goto out;
2119
2120 switch (opt) {
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002121 /*
2122 * If command line arguments and environment
2123 * are placed somewhere else on stack, we can
2124 * set them up here, ARG_START/END to setup
2125 * command line argumets and ENV_START/END
2126 * for environment.
2127 */
2128 case PR_SET_MM_START_STACK:
2129 case PR_SET_MM_ARG_START:
2130 case PR_SET_MM_ARG_END:
2131 case PR_SET_MM_ENV_START:
2132 case PR_SET_MM_ENV_END:
2133 if (!vma) {
2134 error = -EFAULT;
2135 goto out;
2136 }
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002137 }
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002138
2139 mm->start_code = prctl_map.start_code;
2140 mm->end_code = prctl_map.end_code;
2141 mm->start_data = prctl_map.start_data;
2142 mm->end_data = prctl_map.end_data;
2143 mm->start_brk = prctl_map.start_brk;
2144 mm->brk = prctl_map.brk;
2145 mm->start_stack = prctl_map.start_stack;
2146 mm->arg_start = prctl_map.arg_start;
2147 mm->arg_end = prctl_map.arg_end;
2148 mm->env_start = prctl_map.env_start;
2149 mm->env_end = prctl_map.env_end;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002150
2151 error = 0;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002152out:
Mateusz Guzikddf1d392016-01-20 15:01:02 -08002153 up_write(&mm->mmap_sem);
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002154 return error;
2155}
Cyrill Gorcunov300f7862012-06-07 14:21:12 -07002156
Amnon Shiloh52b36942013-04-30 15:28:48 -07002157#ifdef CONFIG_CHECKPOINT_RESTORE
Cyrill Gorcunov300f7862012-06-07 14:21:12 -07002158static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
2159{
2160 return put_user(me->clear_child_tid, tid_addr);
2161}
Amnon Shiloh52b36942013-04-30 15:28:48 -07002162#else
Cyrill Gorcunov300f7862012-06-07 14:21:12 -07002163static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
2164{
2165 return -EINVAL;
2166}
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002167#endif
2168
Pavel Tikhomirov749860c2017-01-30 18:06:12 +03002169static int propagate_has_child_subreaper(struct task_struct *p, void *data)
2170{
2171 /*
2172 * If task has has_child_subreaper - all its decendants
2173 * already have these flag too and new decendants will
2174 * inherit it on fork, skip them.
2175 *
2176 * If we've found child_reaper - skip descendants in
2177 * it's subtree as they will never get out pidns.
2178 */
2179 if (p->signal->has_child_subreaper ||
2180 is_child_reaper(task_pid(p)))
2181 return 0;
2182
2183 p->signal->has_child_subreaper = 1;
2184 return 1;
2185}
2186
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01002187SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
2188 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
David Howellsb6dff3e2008-11-14 10:39:16 +11002190 struct task_struct *me = current;
2191 unsigned char comm[sizeof(me->comm)];
2192 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
David Howellsd84f4f92008-11-14 10:39:23 +11002194 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2195 if (error != -ENOSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 return error;
2197
David Howellsd84f4f92008-11-14 10:39:23 +11002198 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 switch (option) {
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002200 case PR_SET_PDEATHSIG:
2201 if (!valid_signal(arg2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 error = -EINVAL;
2203 break;
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002204 }
2205 me->pdeath_signal = arg2;
2206 break;
2207 case PR_GET_PDEATHSIG:
2208 error = put_user(me->pdeath_signal, (int __user *)arg2);
2209 break;
2210 case PR_GET_DUMPABLE:
2211 error = get_dumpable(me->mm);
2212 break;
2213 case PR_SET_DUMPABLE:
2214 if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) {
2215 error = -EINVAL;
2216 break;
2217 }
2218 set_dumpable(me->mm, arg2);
2219 break;
2220
2221 case PR_SET_UNALIGN:
2222 error = SET_UNALIGN_CTL(me, arg2);
2223 break;
2224 case PR_GET_UNALIGN:
2225 error = GET_UNALIGN_CTL(me, arg2);
2226 break;
2227 case PR_SET_FPEMU:
2228 error = SET_FPEMU_CTL(me, arg2);
2229 break;
2230 case PR_GET_FPEMU:
2231 error = GET_FPEMU_CTL(me, arg2);
2232 break;
2233 case PR_SET_FPEXC:
2234 error = SET_FPEXC_CTL(me, arg2);
2235 break;
2236 case PR_GET_FPEXC:
2237 error = GET_FPEXC_CTL(me, arg2);
2238 break;
2239 case PR_GET_TIMING:
2240 error = PR_TIMING_STATISTICAL;
2241 break;
2242 case PR_SET_TIMING:
2243 if (arg2 != PR_TIMING_STATISTICAL)
2244 error = -EINVAL;
2245 break;
2246 case PR_SET_NAME:
2247 comm[sizeof(me->comm) - 1] = 0;
2248 if (strncpy_from_user(comm, (char __user *)arg2,
2249 sizeof(me->comm) - 1) < 0)
2250 return -EFAULT;
2251 set_task_comm(me, comm);
2252 proc_comm_connector(me);
2253 break;
2254 case PR_GET_NAME:
2255 get_task_comm(comm, me);
2256 if (copy_to_user((char __user *)arg2, comm, sizeof(comm)))
2257 return -EFAULT;
2258 break;
2259 case PR_GET_ENDIAN:
2260 error = GET_ENDIAN(me, arg2);
2261 break;
2262 case PR_SET_ENDIAN:
2263 error = SET_ENDIAN(me, arg2);
2264 break;
2265 case PR_GET_SECCOMP:
2266 error = prctl_get_seccomp();
2267 break;
2268 case PR_SET_SECCOMP:
2269 error = prctl_set_seccomp(arg2, (char __user *)arg3);
2270 break;
2271 case PR_GET_TSC:
2272 error = GET_TSC_CTL(arg2);
2273 break;
2274 case PR_SET_TSC:
2275 error = SET_TSC_CTL(arg2);
2276 break;
2277 case PR_TASK_PERF_EVENTS_DISABLE:
2278 error = perf_event_task_disable();
2279 break;
2280 case PR_TASK_PERF_EVENTS_ENABLE:
2281 error = perf_event_task_enable();
2282 break;
2283 case PR_GET_TIMERSLACK:
John Stultzda8b44d2016-03-17 14:20:51 -07002284 if (current->timer_slack_ns > ULONG_MAX)
2285 error = ULONG_MAX;
2286 else
2287 error = current->timer_slack_ns;
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002288 break;
2289 case PR_SET_TIMERSLACK:
2290 if (arg2 <= 0)
2291 current->timer_slack_ns =
2292 current->default_timer_slack_ns;
2293 else
2294 current->timer_slack_ns = arg2;
2295 break;
2296 case PR_MCE_KILL:
2297 if (arg4 | arg5)
2298 return -EINVAL;
2299 switch (arg2) {
2300 case PR_MCE_KILL_CLEAR:
2301 if (arg3 != 0)
2302 return -EINVAL;
2303 current->flags &= ~PF_MCE_PROCESS;
2304 break;
2305 case PR_MCE_KILL_SET:
2306 current->flags |= PF_MCE_PROCESS;
2307 if (arg3 == PR_MCE_KILL_EARLY)
2308 current->flags |= PF_MCE_EARLY;
2309 else if (arg3 == PR_MCE_KILL_LATE)
2310 current->flags &= ~PF_MCE_EARLY;
2311 else if (arg3 == PR_MCE_KILL_DEFAULT)
2312 current->flags &=
2313 ~(PF_MCE_EARLY|PF_MCE_PROCESS);
2314 else
2315 return -EINVAL;
2316 break;
2317 default:
2318 return -EINVAL;
2319 }
2320 break;
2321 case PR_MCE_KILL_GET:
2322 if (arg2 | arg3 | arg4 | arg5)
2323 return -EINVAL;
2324 if (current->flags & PF_MCE_PROCESS)
2325 error = (current->flags & PF_MCE_EARLY) ?
2326 PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
2327 else
2328 error = PR_MCE_KILL_DEFAULT;
2329 break;
2330 case PR_SET_MM:
2331 error = prctl_set_mm(arg2, arg3, arg4, arg5);
2332 break;
2333 case PR_GET_TID_ADDRESS:
2334 error = prctl_get_tid_address(me, (int __user **)arg2);
2335 break;
2336 case PR_SET_CHILD_SUBREAPER:
2337 me->signal->is_child_subreaper = !!arg2;
Pavel Tikhomirov749860c2017-01-30 18:06:12 +03002338 if (!arg2)
2339 break;
2340
2341 walk_process_tree(me, propagate_has_child_subreaper, NULL);
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002342 break;
2343 case PR_GET_CHILD_SUBREAPER:
2344 error = put_user(me->signal->is_child_subreaper,
2345 (int __user *)arg2);
2346 break;
2347 case PR_SET_NO_NEW_PRIVS:
2348 if (arg2 != 1 || arg3 || arg4 || arg5)
2349 return -EINVAL;
2350
Kees Cook1d4457f2014-05-21 15:23:46 -07002351 task_set_no_new_privs(current);
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002352 break;
2353 case PR_GET_NO_NEW_PRIVS:
2354 if (arg2 || arg3 || arg4 || arg5)
2355 return -EINVAL;
Kees Cook1d4457f2014-05-21 15:23:46 -07002356 return task_no_new_privs(current) ? 1 : 0;
Alex Thorltona0715cc2014-04-07 15:37:10 -07002357 case PR_GET_THP_DISABLE:
2358 if (arg2 || arg3 || arg4 || arg5)
2359 return -EINVAL;
Michal Hocko18600332017-07-10 15:48:02 -07002360 error = !!test_bit(MMF_DISABLE_THP, &me->mm->flags);
Alex Thorltona0715cc2014-04-07 15:37:10 -07002361 break;
2362 case PR_SET_THP_DISABLE:
2363 if (arg3 || arg4 || arg5)
2364 return -EINVAL;
Michal Hocko17b05732016-05-23 16:26:05 -07002365 if (down_write_killable(&me->mm->mmap_sem))
2366 return -EINTR;
Alex Thorltona0715cc2014-04-07 15:37:10 -07002367 if (arg2)
Michal Hocko18600332017-07-10 15:48:02 -07002368 set_bit(MMF_DISABLE_THP, &me->mm->flags);
Alex Thorltona0715cc2014-04-07 15:37:10 -07002369 else
Michal Hocko18600332017-07-10 15:48:02 -07002370 clear_bit(MMF_DISABLE_THP, &me->mm->flags);
Alex Thorltona0715cc2014-04-07 15:37:10 -07002371 up_write(&me->mm->mmap_sem);
2372 break;
Dave Hansenfe3d1972014-11-14 07:18:29 -08002373 case PR_MPX_ENABLE_MANAGEMENT:
Dave Hansene9d1b4f2015-01-08 14:30:22 -08002374 if (arg2 || arg3 || arg4 || arg5)
2375 return -EINVAL;
Dave Hansen46a6e0c2015-06-07 11:37:02 -07002376 error = MPX_ENABLE_MANAGEMENT();
Dave Hansenfe3d1972014-11-14 07:18:29 -08002377 break;
2378 case PR_MPX_DISABLE_MANAGEMENT:
Dave Hansene9d1b4f2015-01-08 14:30:22 -08002379 if (arg2 || arg3 || arg4 || arg5)
2380 return -EINVAL;
Dave Hansen46a6e0c2015-06-07 11:37:02 -07002381 error = MPX_DISABLE_MANAGEMENT();
Dave Hansenfe3d1972014-11-14 07:18:29 -08002382 break;
Paul Burton97915542015-01-08 12:17:37 +00002383 case PR_SET_FP_MODE:
2384 error = SET_FP_MODE(me, arg2);
2385 break;
2386 case PR_GET_FP_MODE:
2387 error = GET_FP_MODE(me);
2388 break;
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002389 default:
2390 error = -EINVAL;
2391 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393 return error;
2394}
Andi Kleen3cfc3482006-09-26 10:52:28 +02002395
Heiko Carstens836f92a2009-01-14 14:14:33 +01002396SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
2397 struct getcpu_cache __user *, unused)
Andi Kleen3cfc3482006-09-26 10:52:28 +02002398{
2399 int err = 0;
2400 int cpu = raw_smp_processor_id();
vishnu.psec94fc32014-10-09 15:30:23 -07002401
Andi Kleen3cfc3482006-09-26 10:52:28 +02002402 if (cpup)
2403 err |= put_user(cpu, cpup);
2404 if (nodep)
2405 err |= put_user(cpu_to_node(cpu), nodep);
Andi Kleen3cfc3482006-09-26 10:52:28 +02002406 return err ? -EFAULT : 0;
2407}
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002408
Stephen Rothwell4a22f162013-04-30 15:27:37 -07002409/**
2410 * do_sysinfo - fill in sysinfo struct
2411 * @info: pointer to buffer to fill
2412 */
2413static int do_sysinfo(struct sysinfo *info)
2414{
2415 unsigned long mem_total, sav_total;
2416 unsigned int mem_unit, bitcount;
2417 struct timespec tp;
2418
2419 memset(info, 0, sizeof(struct sysinfo));
2420
Oleg Nesterov45c64942013-07-03 15:05:01 -07002421 get_monotonic_boottime(&tp);
Stephen Rothwell4a22f162013-04-30 15:27:37 -07002422 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
2423
2424 get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
2425
2426 info->procs = nr_threads;
2427
2428 si_meminfo(info);
2429 si_swapinfo(info);
2430
2431 /*
2432 * If the sum of all the available memory (i.e. ram + swap)
2433 * is less than can be stored in a 32 bit unsigned long then
2434 * we can be binary compatible with 2.2.x kernels. If not,
2435 * well, in that case 2.2.x was broken anyways...
2436 *
2437 * -Erik Andersen <andersee@debian.org>
2438 */
2439
2440 mem_total = info->totalram + info->totalswap;
2441 if (mem_total < info->totalram || mem_total < info->totalswap)
2442 goto out;
2443 bitcount = 0;
2444 mem_unit = info->mem_unit;
2445 while (mem_unit > 1) {
2446 bitcount++;
2447 mem_unit >>= 1;
2448 sav_total = mem_total;
2449 mem_total <<= 1;
2450 if (mem_total < sav_total)
2451 goto out;
2452 }
2453
2454 /*
2455 * If mem_total did not overflow, multiply all memory values by
2456 * info->mem_unit and set it to 1. This leaves things compatible
2457 * with 2.2.x, and also retains compatibility with earlier 2.4.x
2458 * kernels...
2459 */
2460
2461 info->mem_unit = 1;
2462 info->totalram <<= bitcount;
2463 info->freeram <<= bitcount;
2464 info->sharedram <<= bitcount;
2465 info->bufferram <<= bitcount;
2466 info->totalswap <<= bitcount;
2467 info->freeswap <<= bitcount;
2468 info->totalhigh <<= bitcount;
2469 info->freehigh <<= bitcount;
2470
2471out:
2472 return 0;
2473}
2474
2475SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
2476{
2477 struct sysinfo val;
2478
2479 do_sysinfo(&val);
2480
2481 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
2482 return -EFAULT;
2483
2484 return 0;
2485}
2486
2487#ifdef CONFIG_COMPAT
2488struct compat_sysinfo {
2489 s32 uptime;
2490 u32 loads[3];
2491 u32 totalram;
2492 u32 freeram;
2493 u32 sharedram;
2494 u32 bufferram;
2495 u32 totalswap;
2496 u32 freeswap;
2497 u16 procs;
2498 u16 pad;
2499 u32 totalhigh;
2500 u32 freehigh;
2501 u32 mem_unit;
2502 char _f[20-2*sizeof(u32)-sizeof(int)];
2503};
2504
2505COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
2506{
2507 struct sysinfo s;
2508
2509 do_sysinfo(&s);
2510
2511 /* Check to see if any memory value is too large for 32-bit and scale
2512 * down if needed
2513 */
Scotty Bauer0baae412014-10-09 15:30:26 -07002514 if (upper_32_bits(s.totalram) || upper_32_bits(s.totalswap)) {
Stephen Rothwell4a22f162013-04-30 15:27:37 -07002515 int bitcount = 0;
2516
2517 while (s.mem_unit < PAGE_SIZE) {
2518 s.mem_unit <<= 1;
2519 bitcount++;
2520 }
2521
2522 s.totalram >>= bitcount;
2523 s.freeram >>= bitcount;
2524 s.sharedram >>= bitcount;
2525 s.bufferram >>= bitcount;
2526 s.totalswap >>= bitcount;
2527 s.freeswap >>= bitcount;
2528 s.totalhigh >>= bitcount;
2529 s.freehigh >>= bitcount;
2530 }
2531
2532 if (!access_ok(VERIFY_WRITE, info, sizeof(struct compat_sysinfo)) ||
2533 __put_user(s.uptime, &info->uptime) ||
2534 __put_user(s.loads[0], &info->loads[0]) ||
2535 __put_user(s.loads[1], &info->loads[1]) ||
2536 __put_user(s.loads[2], &info->loads[2]) ||
2537 __put_user(s.totalram, &info->totalram) ||
2538 __put_user(s.freeram, &info->freeram) ||
2539 __put_user(s.sharedram, &info->sharedram) ||
2540 __put_user(s.bufferram, &info->bufferram) ||
2541 __put_user(s.totalswap, &info->totalswap) ||
2542 __put_user(s.freeswap, &info->freeswap) ||
2543 __put_user(s.procs, &info->procs) ||
2544 __put_user(s.totalhigh, &info->totalhigh) ||
2545 __put_user(s.freehigh, &info->freehigh) ||
2546 __put_user(s.mem_unit, &info->mem_unit))
2547 return -EFAULT;
2548
2549 return 0;
2550}
2551#endif /* CONFIG_COMPAT */