blob: f2289de20e19a9ff285c1b88f9b62bd08a830a2e [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
Dave Martin2d2123b2017-10-31 15:51:14 +0000114#ifndef SVE_SET_VL
115# define SVE_SET_VL(a) (-EINVAL)
116#endif
117#ifndef SVE_GET_VL
118# define SVE_GET_VL() (-EINVAL)
119#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121/*
122 * this is where the system-wide overflow UID and GID are defined, for
123 * architectures that now have 32-bit UID/GID but didn't in the past
124 */
125
126int overflowuid = DEFAULT_OVERFLOWUID;
127int overflowgid = DEFAULT_OVERFLOWGID;
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129EXPORT_SYMBOL(overflowuid);
130EXPORT_SYMBOL(overflowgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132/*
133 * the same as above, but for filesystems which can only store a 16-bit
134 * UID and GID. as such, this is needed on all architectures
135 */
136
137int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
Wolffhardt Schwabe8b2770a2017-12-14 19:13:57 +0100138int fs_overflowgid = DEFAULT_FS_OVERFLOWGID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140EXPORT_SYMBOL(fs_overflowuid);
141EXPORT_SYMBOL(fs_overflowgid);
142
143/*
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700144 * Returns true if current's euid is same as p's uid or euid,
145 * or has CAP_SYS_NICE to p's user_ns.
146 *
147 * Called with rcu_read_lock, creds are safe
148 */
149static bool set_one_prio_perm(struct task_struct *p)
150{
151 const struct cred *cred = current_cred(), *pcred = __task_cred(p);
152
Eric W. Biederman5af66202012-03-03 20:21:47 -0800153 if (uid_eq(pcred->uid, cred->euid) ||
154 uid_eq(pcred->euid, cred->euid))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700155 return true;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800156 if (ns_capable(pcred->user_ns, CAP_SYS_NICE))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700157 return true;
158 return false;
159}
160
161/*
David Howellsc69e8d92008-11-14 10:39:19 +1100162 * set the priority of a task
163 * - the caller must hold the RCU read lock
164 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static int set_one_prio(struct task_struct *p, int niceval, int error)
166{
167 int no_nice;
168
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700169 if (!set_one_prio_perm(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 error = -EPERM;
171 goto out;
172 }
Matt Mackalle43379f2005-05-01 08:59:00 -0700173 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 error = -EACCES;
175 goto out;
176 }
177 no_nice = security_task_setnice(p, niceval);
178 if (no_nice) {
179 error = no_nice;
180 goto out;
181 }
182 if (error == -ESRCH)
183 error = 0;
184 set_user_nice(p, niceval);
185out:
186 return error;
187}
188
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100189SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct task_struct *g, *p;
192 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100193 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int error = -EINVAL;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800195 struct pid *pgrp;
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800196 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Daniel Walker3e88c552007-05-10 22:22:53 -0700198 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 goto out;
200
201 /* normalize: avoid signed division (rounding problems) */
202 error = -ESRCH;
Dongsheng Yangc4a4d2f2014-02-11 15:34:51 +0800203 if (niceval < MIN_NICE)
204 niceval = MIN_NICE;
205 if (niceval > MAX_NICE)
206 niceval = MAX_NICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000208 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 read_lock(&tasklist_lock);
210 switch (which) {
vishnu.psec94fc32014-10-09 15:30:23 -0700211 case PRIO_PROCESS:
212 if (who)
213 p = find_task_by_vpid(who);
214 else
215 p = current;
216 if (p)
217 error = set_one_prio(p, niceval, error);
218 break;
219 case PRIO_PGRP:
220 if (who)
221 pgrp = find_vpid(who);
222 else
223 pgrp = task_pgrp(current);
224 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
225 error = set_one_prio(p, niceval, error);
226 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
227 break;
228 case PRIO_USER:
229 uid = make_kuid(cred->user_ns, who);
230 user = cred->user;
231 if (!who)
232 uid = cred->uid;
233 else if (!uid_eq(uid, cred->uid)) {
234 user = find_user(uid);
235 if (!user)
David Howells86a264a2008-11-14 10:39:18 +1100236 goto out_unlock; /* No processes for this user */
vishnu.psec94fc32014-10-09 15:30:23 -0700237 }
238 do_each_thread(g, p) {
Ben Segall8639b462015-11-06 16:32:48 -0800239 if (uid_eq(task_uid(p), uid) && task_pid_vnr(p))
vishnu.psec94fc32014-10-09 15:30:23 -0700240 error = set_one_prio(p, niceval, error);
241 } while_each_thread(g, p);
242 if (!uid_eq(uid, cred->uid))
243 free_uid(user); /* For find_user() */
244 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246out_unlock:
247 read_unlock(&tasklist_lock);
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000248 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249out:
250 return error;
251}
252
253/*
254 * Ugh. To avoid negative return values, "getpriority()" will
255 * not return the normal nice-value, but a negated value that
256 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
257 * to stay compatible.
258 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100259SYSCALL_DEFINE2(getpriority, int, which, int, who)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct task_struct *g, *p;
262 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100263 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 long niceval, retval = -ESRCH;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800265 struct pid *pgrp;
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800266 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Daniel Walker3e88c552007-05-10 22:22:53 -0700268 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return -EINVAL;
270
Tetsuo Handa70118832010-02-22 12:44:16 -0800271 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 read_lock(&tasklist_lock);
273 switch (which) {
vishnu.psec94fc32014-10-09 15:30:23 -0700274 case PRIO_PROCESS:
275 if (who)
276 p = find_task_by_vpid(who);
277 else
278 p = current;
279 if (p) {
280 niceval = nice_to_rlimit(task_nice(p));
281 if (niceval > retval)
282 retval = niceval;
283 }
284 break;
285 case PRIO_PGRP:
286 if (who)
287 pgrp = find_vpid(who);
288 else
289 pgrp = task_pgrp(current);
290 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
291 niceval = nice_to_rlimit(task_nice(p));
292 if (niceval > retval)
293 retval = niceval;
294 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
295 break;
296 case PRIO_USER:
297 uid = make_kuid(cred->user_ns, who);
298 user = cred->user;
299 if (!who)
300 uid = cred->uid;
301 else if (!uid_eq(uid, cred->uid)) {
302 user = find_user(uid);
303 if (!user)
304 goto out_unlock; /* No processes for this user */
305 }
306 do_each_thread(g, p) {
Ben Segall8639b462015-11-06 16:32:48 -0800307 if (uid_eq(task_uid(p), uid) && task_pid_vnr(p)) {
Dongsheng Yang7aa2c012014-05-08 18:33:49 +0900308 niceval = nice_to_rlimit(task_nice(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (niceval > retval)
310 retval = niceval;
311 }
vishnu.psec94fc32014-10-09 15:30:23 -0700312 } while_each_thread(g, p);
313 if (!uid_eq(uid, cred->uid))
314 free_uid(user); /* for find_user() */
315 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317out_unlock:
318 read_unlock(&tasklist_lock);
Tetsuo Handa70118832010-02-22 12:44:16 -0800319 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 return retval;
322}
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/*
325 * Unprivileged users may change the real gid to the effective gid
326 * or vice versa. (BSD-style)
327 *
328 * If you set the real gid at all, or set the effective gid to a value not
329 * equal to the real gid, then the saved gid is set to the new effective gid.
330 *
331 * This makes it possible for a setgid program to completely drop its
332 * privileges, which is often a useful assertion to make when you are doing
333 * a security audit over a program.
334 *
335 * The general idea is that a program which uses just setregid() will be
336 * 100% compatible with BSD. A program which uses just setgid() will be
vishnu.psec94fc32014-10-09 15:30:23 -0700337 * 100% compatible with POSIX with saved IDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 *
339 * SMP: There are not races, the GIDs are checked only by filesystem
340 * operations (as far as semantic preservation is concerned).
341 */
Iulia Manda28138932015-04-15 16:16:41 -0700342#ifdef CONFIG_MULTIUSER
Heiko Carstensae1251a2009-01-14 14:14:05 +0100343SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800345 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100346 const struct cred *old;
347 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800349 kgid_t krgid, kegid;
350
351 krgid = make_kgid(ns, rgid);
352 kegid = make_kgid(ns, egid);
353
354 if ((rgid != (gid_t) -1) && !gid_valid(krgid))
355 return -EINVAL;
356 if ((egid != (gid_t) -1) && !gid_valid(kegid))
357 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
David Howellsd84f4f92008-11-14 10:39:23 +1100359 new = prepare_creds();
360 if (!new)
361 return -ENOMEM;
362 old = current_cred();
363
David Howellsd84f4f92008-11-14 10:39:23 +1100364 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (rgid != (gid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800366 if (gid_eq(old->gid, krgid) ||
367 gid_eq(old->egid, krgid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700368 ns_capable(old->user_ns, CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800369 new->gid = krgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 else
David Howellsd84f4f92008-11-14 10:39:23 +1100371 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373 if (egid != (gid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800374 if (gid_eq(old->gid, kegid) ||
375 gid_eq(old->egid, kegid) ||
376 gid_eq(old->sgid, kegid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700377 ns_capable(old->user_ns, CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800378 new->egid = kegid;
Cal Peake756184b2006-09-30 23:27:24 -0700379 else
David Howellsd84f4f92008-11-14 10:39:23 +1100380 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
David Howellsd84f4f92008-11-14 10:39:23 +1100382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (rgid != (gid_t) -1 ||
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800384 (egid != (gid_t) -1 && !gid_eq(kegid, old->gid)))
David Howellsd84f4f92008-11-14 10:39:23 +1100385 new->sgid = new->egid;
386 new->fsgid = new->egid;
387
388 return commit_creds(new);
389
390error:
391 abort_creds(new);
392 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395/*
vishnu.psec94fc32014-10-09 15:30:23 -0700396 * setgid() is implemented like SysV w/ SAVED_IDS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 *
398 * SMP: Same implicit races as above.
399 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100400SYSCALL_DEFINE1(setgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800402 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100403 const struct cred *old;
404 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800406 kgid_t kgid;
407
408 kgid = make_kgid(ns, gid);
409 if (!gid_valid(kgid))
410 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
David Howellsd84f4f92008-11-14 10:39:23 +1100412 new = prepare_creds();
413 if (!new)
414 return -ENOMEM;
415 old = current_cred();
416
David Howellsd84f4f92008-11-14 10:39:23 +1100417 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700418 if (ns_capable(old->user_ns, CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800419 new->gid = new->egid = new->sgid = new->fsgid = kgid;
420 else if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->sgid))
421 new->egid = new->fsgid = kgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 else
David Howellsd84f4f92008-11-14 10:39:23 +1100423 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
David Howellsd84f4f92008-11-14 10:39:23 +1100425 return commit_creds(new);
426
427error:
428 abort_creds(new);
429 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
Dhaval Giani54e99122009-02-27 15:13:54 +0530431
David Howellsd84f4f92008-11-14 10:39:23 +1100432/*
433 * change the user struct in a credentials set to match the new UID
434 */
435static int set_user(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 struct user_struct *new_user;
438
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800439 new_user = alloc_uid(new->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (!new_user)
441 return -EAGAIN;
442
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400443 /*
444 * We don't fail in case of NPROC limit excess here because too many
445 * poorly written programs don't check set*uid() return code, assuming
446 * it never fails if called by root. We may still enforce NPROC limit
447 * for programs doing set*uid()+execve() by harmlessly deferring the
448 * failure to the execve() stage.
449 */
Jiri Slaby78d7d402010-03-05 13:42:54 -0800450 if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400451 new_user != INIT_USER)
452 current->flags |= PF_NPROC_EXCEEDED;
453 else
454 current->flags &= ~PF_NPROC_EXCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
David Howellsd84f4f92008-11-14 10:39:23 +1100456 free_uid(new->user);
457 new->user = new_user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return 0;
459}
460
461/*
462 * Unprivileged users may change the real uid to the effective uid
463 * or vice versa. (BSD-style)
464 *
465 * If you set the real uid at all, or set the effective uid to a value not
466 * equal to the real uid, then the saved uid is set to the new effective uid.
467 *
468 * This makes it possible for a setuid program to completely drop its
469 * privileges, which is often a useful assertion to make when you are doing
470 * a security audit over a program.
471 *
472 * The general idea is that a program which uses just setreuid() will be
473 * 100% compatible with BSD. A program which uses just setuid() will be
vishnu.psec94fc32014-10-09 15:30:23 -0700474 * 100% compatible with POSIX with saved IDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100476SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800478 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100479 const struct cred *old;
480 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800482 kuid_t kruid, keuid;
483
484 kruid = make_kuid(ns, ruid);
485 keuid = make_kuid(ns, euid);
486
487 if ((ruid != (uid_t) -1) && !uid_valid(kruid))
488 return -EINVAL;
489 if ((euid != (uid_t) -1) && !uid_valid(keuid))
490 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
David Howellsd84f4f92008-11-14 10:39:23 +1100492 new = prepare_creds();
493 if (!new)
494 return -ENOMEM;
495 old = current_cred();
496
David Howellsd84f4f92008-11-14 10:39:23 +1100497 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (ruid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800499 new->uid = kruid;
500 if (!uid_eq(old->uid, kruid) &&
501 !uid_eq(old->euid, kruid) &&
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700502 !ns_capable(old->user_ns, CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100503 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505
506 if (euid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800507 new->euid = keuid;
508 if (!uid_eq(old->uid, keuid) &&
509 !uid_eq(old->euid, keuid) &&
510 !uid_eq(old->suid, keuid) &&
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700511 !ns_capable(old->user_ns, CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100512 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800515 if (!uid_eq(new->uid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530516 retval = set_user(new);
517 if (retval < 0)
518 goto error;
519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (ruid != (uid_t) -1 ||
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800521 (euid != (uid_t) -1 && !uid_eq(keuid, old->uid)))
David Howellsd84f4f92008-11-14 10:39:23 +1100522 new->suid = new->euid;
523 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
David Howellsd84f4f92008-11-14 10:39:23 +1100525 retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
526 if (retval < 0)
527 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
David Howellsd84f4f92008-11-14 10:39:23 +1100529 return commit_creds(new);
530
531error:
532 abort_creds(new);
533 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
vishnu.psec94fc32014-10-09 15:30:23 -0700535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536/*
vishnu.psec94fc32014-10-09 15:30:23 -0700537 * setuid() is implemented like SysV with SAVED_IDS
538 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 * Note that SAVED_ID's is deficient in that a setuid root program
vishnu.psec94fc32014-10-09 15:30:23 -0700540 * like sendmail, for example, cannot set its uid to be a normal
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 * user and then switch back, because if you're root, setuid() sets
542 * the saved uid too. If you don't like this, blame the bright people
543 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
544 * will allow a root program to temporarily drop privileges and be able to
vishnu.psec94fc32014-10-09 15:30:23 -0700545 * regain them by swapping the real and effective uid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100547SYSCALL_DEFINE1(setuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800549 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100550 const struct cred *old;
551 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800553 kuid_t kuid;
554
555 kuid = make_kuid(ns, uid);
556 if (!uid_valid(kuid))
557 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
David Howellsd84f4f92008-11-14 10:39:23 +1100559 new = prepare_creds();
560 if (!new)
561 return -ENOMEM;
562 old = current_cred();
563
David Howellsd84f4f92008-11-14 10:39:23 +1100564 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700565 if (ns_capable(old->user_ns, CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800566 new->suid = new->uid = kuid;
567 if (!uid_eq(kuid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530568 retval = set_user(new);
569 if (retval < 0)
570 goto error;
David Howellsd84f4f92008-11-14 10:39:23 +1100571 }
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800572 } else if (!uid_eq(kuid, old->uid) && !uid_eq(kuid, new->suid)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100573 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800576 new->fsuid = new->euid = kuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
David Howellsd84f4f92008-11-14 10:39:23 +1100578 retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
579 if (retval < 0)
580 goto error;
581
582 return commit_creds(new);
583
584error:
585 abort_creds(new);
586 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
589
590/*
591 * This function implements a generic ability to update ruid, euid,
592 * and suid. This allows you to implement the 4.4 compatible seteuid().
593 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100594SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800596 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100597 const struct cred *old;
598 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800600 kuid_t kruid, keuid, ksuid;
601
602 kruid = make_kuid(ns, ruid);
603 keuid = make_kuid(ns, euid);
604 ksuid = make_kuid(ns, suid);
605
606 if ((ruid != (uid_t) -1) && !uid_valid(kruid))
607 return -EINVAL;
608
609 if ((euid != (uid_t) -1) && !uid_valid(keuid))
610 return -EINVAL;
611
612 if ((suid != (uid_t) -1) && !uid_valid(ksuid))
613 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
David Howellsd84f4f92008-11-14 10:39:23 +1100615 new = prepare_creds();
616 if (!new)
617 return -ENOMEM;
618
David Howellsd84f4f92008-11-14 10:39:23 +1100619 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
David Howellsd84f4f92008-11-14 10:39:23 +1100621 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700622 if (!ns_capable(old->user_ns, CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800623 if (ruid != (uid_t) -1 && !uid_eq(kruid, old->uid) &&
624 !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100625 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800626 if (euid != (uid_t) -1 && !uid_eq(keuid, old->uid) &&
627 !uid_eq(keuid, old->euid) && !uid_eq(keuid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100628 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800629 if (suid != (uid_t) -1 && !uid_eq(ksuid, old->uid) &&
630 !uid_eq(ksuid, old->euid) && !uid_eq(ksuid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100631 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
David Howellsd84f4f92008-11-14 10:39:23 +1100633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (ruid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800635 new->uid = kruid;
636 if (!uid_eq(kruid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530637 retval = set_user(new);
638 if (retval < 0)
639 goto error;
640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
David Howellsd84f4f92008-11-14 10:39:23 +1100642 if (euid != (uid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800643 new->euid = keuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (suid != (uid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800645 new->suid = ksuid;
David Howellsd84f4f92008-11-14 10:39:23 +1100646 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
David Howellsd84f4f92008-11-14 10:39:23 +1100648 retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
649 if (retval < 0)
650 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
David Howellsd84f4f92008-11-14 10:39:23 +1100652 return commit_creds(new);
653
654error:
655 abort_creds(new);
656 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800659SYSCALL_DEFINE3(getresuid, uid_t __user *, ruidp, uid_t __user *, euidp, uid_t __user *, suidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
David Howells86a264a2008-11-14 10:39:18 +1100661 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800663 uid_t ruid, euid, suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800665 ruid = from_kuid_munged(cred->user_ns, cred->uid);
666 euid = from_kuid_munged(cred->user_ns, cred->euid);
667 suid = from_kuid_munged(cred->user_ns, cred->suid);
668
vishnu.psec94fc32014-10-09 15:30:23 -0700669 retval = put_user(ruid, ruidp);
670 if (!retval) {
671 retval = put_user(euid, euidp);
672 if (!retval)
673 return put_user(suid, suidp);
674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return retval;
676}
677
678/*
679 * Same as above, but for rgid, egid, sgid.
680 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100681SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800683 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100684 const struct cred *old;
685 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800687 kgid_t krgid, kegid, ksgid;
688
689 krgid = make_kgid(ns, rgid);
690 kegid = make_kgid(ns, egid);
691 ksgid = make_kgid(ns, sgid);
692
693 if ((rgid != (gid_t) -1) && !gid_valid(krgid))
694 return -EINVAL;
695 if ((egid != (gid_t) -1) && !gid_valid(kegid))
696 return -EINVAL;
697 if ((sgid != (gid_t) -1) && !gid_valid(ksgid))
698 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
David Howellsd84f4f92008-11-14 10:39:23 +1100700 new = prepare_creds();
701 if (!new)
702 return -ENOMEM;
703 old = current_cred();
704
David Howellsd84f4f92008-11-14 10:39:23 +1100705 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700706 if (!ns_capable(old->user_ns, CAP_SETGID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800707 if (rgid != (gid_t) -1 && !gid_eq(krgid, old->gid) &&
708 !gid_eq(krgid, old->egid) && !gid_eq(krgid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100709 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800710 if (egid != (gid_t) -1 && !gid_eq(kegid, old->gid) &&
711 !gid_eq(kegid, old->egid) && !gid_eq(kegid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100712 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800713 if (sgid != (gid_t) -1 && !gid_eq(ksgid, old->gid) &&
714 !gid_eq(ksgid, old->egid) && !gid_eq(ksgid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100715 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
David Howellsd84f4f92008-11-14 10:39:23 +1100718 if (rgid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800719 new->gid = krgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100720 if (egid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800721 new->egid = kegid;
David Howellsd84f4f92008-11-14 10:39:23 +1100722 if (sgid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800723 new->sgid = ksgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100724 new->fsgid = new->egid;
725
726 return commit_creds(new);
727
728error:
729 abort_creds(new);
730 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800733SYSCALL_DEFINE3(getresgid, gid_t __user *, rgidp, gid_t __user *, egidp, gid_t __user *, sgidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
David Howells86a264a2008-11-14 10:39:18 +1100735 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800737 gid_t rgid, egid, sgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800739 rgid = from_kgid_munged(cred->user_ns, cred->gid);
740 egid = from_kgid_munged(cred->user_ns, cred->egid);
741 sgid = from_kgid_munged(cred->user_ns, cred->sgid);
742
vishnu.psec94fc32014-10-09 15:30:23 -0700743 retval = put_user(rgid, rgidp);
744 if (!retval) {
745 retval = put_user(egid, egidp);
746 if (!retval)
747 retval = put_user(sgid, sgidp);
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 return retval;
751}
752
753
754/*
755 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
756 * is used for "access()" and for the NFS daemon (letting nfsd stay at
757 * whatever uid it wants to). It normally shadows "euid", except when
758 * explicitly set by setfsuid() or for access..
759 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100760SYSCALL_DEFINE1(setfsuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
David Howellsd84f4f92008-11-14 10:39:23 +1100762 const struct cred *old;
763 struct cred *new;
764 uid_t old_fsuid;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800765 kuid_t kuid;
766
767 old = current_cred();
768 old_fsuid = from_kuid_munged(old->user_ns, old->fsuid);
769
770 kuid = make_kuid(old->user_ns, uid);
771 if (!uid_valid(kuid))
772 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
David Howellsd84f4f92008-11-14 10:39:23 +1100774 new = prepare_creds();
775 if (!new)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800776 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800778 if (uid_eq(kuid, old->uid) || uid_eq(kuid, old->euid) ||
779 uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700780 ns_capable(old->user_ns, CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800781 if (!uid_eq(kuid, old->fsuid)) {
782 new->fsuid = kuid;
David Howellsd84f4f92008-11-14 10:39:23 +1100783 if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
784 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
David Howellsd84f4f92008-11-14 10:39:23 +1100788 abort_creds(new);
789 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
David Howellsd84f4f92008-11-14 10:39:23 +1100791change_okay:
792 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return old_fsuid;
794}
795
796/*
John Anthony Kazos Jrf42df9e2007-05-09 08:23:08 +0200797 * Samma på svenska..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100799SYSCALL_DEFINE1(setfsgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
David Howellsd84f4f92008-11-14 10:39:23 +1100801 const struct cred *old;
802 struct cred *new;
803 gid_t old_fsgid;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800804 kgid_t kgid;
805
806 old = current_cred();
807 old_fsgid = from_kgid_munged(old->user_ns, old->fsgid);
808
809 kgid = make_kgid(old->user_ns, gid);
810 if (!gid_valid(kgid))
811 return old_fsgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
David Howellsd84f4f92008-11-14 10:39:23 +1100813 new = prepare_creds();
814 if (!new)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800815 return old_fsgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100816
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800817 if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->egid) ||
818 gid_eq(kgid, old->sgid) || gid_eq(kgid, old->fsgid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700819 ns_capable(old->user_ns, CAP_SETGID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800820 if (!gid_eq(kgid, old->fsgid)) {
821 new->fsgid = kgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100822 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
David Howellsd84f4f92008-11-14 10:39:23 +1100825
David Howellsd84f4f92008-11-14 10:39:23 +1100826 abort_creds(new);
827 return old_fsgid;
828
829change_okay:
830 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return old_fsgid;
832}
Iulia Manda28138932015-04-15 16:16:41 -0700833#endif /* CONFIG_MULTIUSER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Stephen Rothwell4a22f162013-04-30 15:27:37 -0700835/**
836 * sys_getpid - return the thread group id of the current process
837 *
838 * Note, despite the name, this returns the tgid not the pid. The tgid and
839 * the pid are identical unless CLONE_THREAD was specified on clone() in
840 * which case the tgid is the same in all threads of the same group.
841 *
842 * This is SMP safe as current->tgid does not change.
843 */
844SYSCALL_DEFINE0(getpid)
845{
846 return task_tgid_vnr(current);
847}
848
849/* Thread ID - the internal kernel "pid" */
850SYSCALL_DEFINE0(gettid)
851{
852 return task_pid_vnr(current);
853}
854
855/*
856 * Accessing ->real_parent is not SMP-safe, it could
857 * change from under us. However, we can use a stale
858 * value of ->real_parent under rcu_read_lock(), see
859 * release_task()->call_rcu(delayed_put_task_struct).
860 */
861SYSCALL_DEFINE0(getppid)
862{
863 int pid;
864
865 rcu_read_lock();
866 pid = task_tgid_vnr(rcu_dereference(current->real_parent));
867 rcu_read_unlock();
868
869 return pid;
870}
871
872SYSCALL_DEFINE0(getuid)
873{
874 /* Only we change this so SMP safe */
875 return from_kuid_munged(current_user_ns(), current_uid());
876}
877
878SYSCALL_DEFINE0(geteuid)
879{
880 /* Only we change this so SMP safe */
881 return from_kuid_munged(current_user_ns(), current_euid());
882}
883
884SYSCALL_DEFINE0(getgid)
885{
886 /* Only we change this so SMP safe */
887 return from_kgid_munged(current_user_ns(), current_gid());
888}
889
890SYSCALL_DEFINE0(getegid)
891{
892 /* Only we change this so SMP safe */
893 return from_kgid_munged(current_user_ns(), current_egid());
894}
895
Al Viroca2406e2017-05-31 04:22:44 -0400896static void do_sys_times(struct tms *tms)
Frank Mayharf06febc2008-09-12 09:54:39 -0700897{
Frederic Weisbecker5613fda2017-01-31 04:09:23 +0100898 u64 tgutime, tgstime, cutime, cstime;
Frank Mayharf06febc2008-09-12 09:54:39 -0700899
Frederic Weisbeckere80d0a1a2012-11-21 16:26:44 +0100900 thread_group_cputime_adjusted(current, &tgutime, &tgstime);
Frank Mayharf06febc2008-09-12 09:54:39 -0700901 cutime = current->signal->cutime;
902 cstime = current->signal->cstime;
Frederic Weisbecker5613fda2017-01-31 04:09:23 +0100903 tms->tms_utime = nsec_to_clock_t(tgutime);
904 tms->tms_stime = nsec_to_clock_t(tgstime);
905 tms->tms_cutime = nsec_to_clock_t(cutime);
906 tms->tms_cstime = nsec_to_clock_t(cstime);
Frank Mayharf06febc2008-09-12 09:54:39 -0700907}
908
Heiko Carstens58fd3aa2009-01-14 14:14:03 +0100909SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (tbuf) {
912 struct tms tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Frank Mayharf06febc2008-09-12 09:54:39 -0700914 do_sys_times(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
916 return -EFAULT;
917 }
Paul Mackerrase3d5a272009-01-06 14:41:02 -0800918 force_successful_syscall_return();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return (long) jiffies_64_to_clock_t(get_jiffies_64());
920}
921
Al Viroca2406e2017-05-31 04:22:44 -0400922#ifdef CONFIG_COMPAT
923static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
924{
925 return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
926}
927
928COMPAT_SYSCALL_DEFINE1(times, struct compat_tms __user *, tbuf)
929{
930 if (tbuf) {
931 struct tms tms;
932 struct compat_tms tmp;
933
934 do_sys_times(&tms);
935 /* Convert our struct tms to the compat version. */
936 tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
937 tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
938 tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
939 tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
940 if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
941 return -EFAULT;
942 }
943 force_successful_syscall_return();
944 return compat_jiffies_to_clock_t(jiffies);
945}
946#endif
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948/*
949 * This needs some heavy checking ...
950 * I just haven't the stomach for it. I also don't fully
951 * understand sessions/pgrp etc. Let somebody who does explain it.
952 *
953 * OK, I think I have the protection semantics right.... this is really
954 * only important on a multi-user system anyway, to make sure one user
955 * can't send a signal to a process owned by another. -TYT, 12/12/91
956 *
Oleg Nesterov98611e42014-01-23 15:55:52 -0800957 * !PF_FORKNOEXEC check to conform completely to POSIX.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +0100959SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 struct task_struct *p;
Oleg Nesterovee0acf92006-01-08 01:03:53 -0800962 struct task_struct *group_leader = current->group_leader;
Oleg Nesterov4e021302008-02-08 04:19:08 -0800963 struct pid *pgrp;
964 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 if (!pid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700967 pid = task_pid_vnr(group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (!pgid)
969 pgid = pid;
970 if (pgid < 0)
971 return -EINVAL;
Paul E. McKenney950eaac2010-08-31 17:00:18 -0700972 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 /* From this point forward we keep holding onto the tasklist lock
975 * so that our parent does not change from under us. -DaveM
976 */
977 write_lock_irq(&tasklist_lock);
978
979 err = -ESRCH;
Oleg Nesterov4e021302008-02-08 04:19:08 -0800980 p = find_task_by_vpid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (!p)
982 goto out;
983
984 err = -EINVAL;
985 if (!thread_group_leader(p))
986 goto out;
987
Oleg Nesterov4e021302008-02-08 04:19:08 -0800988 if (same_thread_group(p->real_parent, group_leader)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 err = -EPERM;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800990 if (task_session(p) != task_session(group_leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 goto out;
992 err = -EACCES;
Oleg Nesterov98611e42014-01-23 15:55:52 -0800993 if (!(p->flags & PF_FORKNOEXEC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 goto out;
995 } else {
996 err = -ESRCH;
Oleg Nesterovee0acf92006-01-08 01:03:53 -0800997 if (p != group_leader)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 goto out;
999 }
1000
1001 err = -EPERM;
1002 if (p->signal->leader)
1003 goto out;
1004
Oleg Nesterov4e021302008-02-08 04:19:08 -08001005 pgrp = task_pid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (pgid != pid) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001007 struct task_struct *g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Oleg Nesterov4e021302008-02-08 04:19:08 -08001009 pgrp = find_vpid(pgid);
1010 g = pid_task(pgrp, PIDTYPE_PGID);
Eric W. Biederman41487c62007-02-12 00:53:01 -08001011 if (!g || task_session(g) != task_session(group_leader))
Oleg Nesterovf020bc42006-12-08 02:38:02 -08001012 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 err = security_task_setpgid(p, pgid);
1016 if (err)
1017 goto out;
1018
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07001019 if (task_pgrp(p) != pgrp)
Oleg Nesterov83beaf32008-04-30 00:54:27 -07001020 change_pid(p, PIDTYPE_PGID, pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 err = 0;
1023out:
1024 /* All paths lead to here, thus we are safe. -DaveM */
1025 write_unlock_irq(&tasklist_lock);
Paul E. McKenney950eaac2010-08-31 17:00:18 -07001026 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return err;
1028}
1029
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001030SYSCALL_DEFINE1(getpgid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001032 struct task_struct *p;
1033 struct pid *grp;
1034 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001036 rcu_read_lock();
1037 if (!pid)
1038 grp = task_pgrp(current);
1039 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 retval = -ESRCH;
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001041 p = find_task_by_vpid(pid);
1042 if (!p)
1043 goto out;
1044 grp = task_pgrp(p);
1045 if (!grp)
1046 goto out;
1047
1048 retval = security_task_getpgid(p);
1049 if (retval)
1050 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001052 retval = pid_vnr(grp);
1053out:
1054 rcu_read_unlock();
1055 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
1058#ifdef __ARCH_WANT_SYS_GETPGRP
1059
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001060SYSCALL_DEFINE0(getpgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001062 return sys_getpgid(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
1065#endif
1066
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001067SYSCALL_DEFINE1(getsid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001069 struct task_struct *p;
1070 struct pid *sid;
1071 int retval;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001072
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001073 rcu_read_lock();
1074 if (!pid)
1075 sid = task_session(current);
1076 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 retval = -ESRCH;
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001078 p = find_task_by_vpid(pid);
1079 if (!p)
1080 goto out;
1081 sid = task_session(p);
1082 if (!sid)
1083 goto out;
1084
1085 retval = security_task_getsid(p);
1086 if (retval)
1087 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001089 retval = pid_vnr(sid);
1090out:
1091 rcu_read_unlock();
1092 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
Oleg Nesterov81dabb42013-07-03 15:08:26 -07001095static void set_special_pids(struct pid *pid)
1096{
1097 struct task_struct *curr = current->group_leader;
1098
1099 if (task_session(curr) != pid)
1100 change_pid(curr, PIDTYPE_SID, pid);
1101
1102 if (task_pgrp(curr) != pid)
1103 change_pid(curr, PIDTYPE_PGID, pid);
1104}
1105
Heiko Carstensb290ebe2009-01-14 14:14:06 +01001106SYSCALL_DEFINE0(setsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
Oren Laadane19f2472006-01-08 01:03:58 -08001108 struct task_struct *group_leader = current->group_leader;
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001109 struct pid *sid = task_pid(group_leader);
1110 pid_t session = pid_vnr(sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 int err = -EPERM;
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 write_lock_irq(&tasklist_lock);
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001114 /* Fail if I am already a session leader */
1115 if (group_leader->signal->leader)
1116 goto out;
1117
Oleg Nesterov430c6232008-02-08 04:19:11 -08001118 /* Fail if a process group id already exists that equals the
1119 * proposed session id.
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001120 */
Oleg Nesterov6806aac2008-02-08 04:19:12 -08001121 if (pid_task(sid, PIDTYPE_PGID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 goto out;
1123
Oren Laadane19f2472006-01-08 01:03:58 -08001124 group_leader->signal->leader = 1;
Oleg Nesterov81dabb42013-07-03 15:08:26 -07001125 set_special_pids(sid);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001126
Alan Cox9c9f4de2008-10-13 10:37:26 +01001127 proc_clear_tty(group_leader);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001128
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001129 err = session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130out:
1131 write_unlock_irq(&tasklist_lock);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001132 if (err > 0) {
Christian Borntraeger0d0df592009-10-26 16:49:34 -07001133 proc_sid_connector(group_leader);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001134 sched_autogroup_create_attach(group_leader);
1135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return err;
1137}
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139DECLARE_RWSEM(uts_sem);
1140
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001141#ifdef COMPAT_UTS_MACHINE
1142#define override_architecture(name) \
Andreas Schwab46da2762010-04-23 13:17:44 -04001143 (personality(current->personality) == PER_LINUX32 && \
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001144 copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1145 sizeof(COMPAT_UTS_MACHINE)))
1146#else
1147#define override_architecture(name) 0
1148#endif
1149
Andi Kleenbe274252011-08-19 16:15:10 -07001150/*
1151 * Work around broken programs that cannot handle "Linux 3.0".
1152 * 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 -08001153 * 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 -07001154 */
Kees Cook2702b152012-10-19 13:56:51 -07001155static int override_release(char __user *release, size_t len)
Andi Kleenbe274252011-08-19 16:15:10 -07001156{
1157 int ret = 0;
Andi Kleenbe274252011-08-19 16:15:10 -07001158
1159 if (current->personality & UNAME26) {
Kees Cook2702b152012-10-19 13:56:51 -07001160 const char *rest = UTS_RELEASE;
1161 char buf[65] = { 0 };
Andi Kleenbe274252011-08-19 16:15:10 -07001162 int ndots = 0;
1163 unsigned v;
Kees Cook2702b152012-10-19 13:56:51 -07001164 size_t copy;
Andi Kleenbe274252011-08-19 16:15:10 -07001165
1166 while (*rest) {
1167 if (*rest == '.' && ++ndots >= 3)
1168 break;
1169 if (!isdigit(*rest) && *rest != '.')
1170 break;
1171 rest++;
1172 }
Jon DeVree39afb5e2015-02-27 15:52:07 -08001173 v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 60;
Kees Cook31fd84b92012-10-19 18:45:53 -07001174 copy = clamp_t(size_t, len, 1, sizeof(buf));
Kees Cook2702b152012-10-19 13:56:51 -07001175 copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
1176 ret = copy_to_user(release, buf, copy + 1);
Andi Kleenbe274252011-08-19 16:15:10 -07001177 }
1178 return ret;
1179}
1180
Heiko Carstense48fbb62009-01-14 14:14:26 +01001181SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
1183 int errno = 0;
1184
1185 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001186 if (copy_to_user(name, utsname(), sizeof *name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 errno = -EFAULT;
1188 up_read(&uts_sem);
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001189
Andi Kleenbe274252011-08-19 16:15:10 -07001190 if (!errno && override_release(name->release, sizeof(name->release)))
1191 errno = -EFAULT;
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001192 if (!errno && override_architecture(name))
1193 errno = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return errno;
1195}
1196
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001197#ifdef __ARCH_WANT_SYS_OLD_UNAME
1198/*
1199 * Old cruft
1200 */
1201SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
1202{
1203 int error = 0;
1204
1205 if (!name)
1206 return -EFAULT;
1207
1208 down_read(&uts_sem);
1209 if (copy_to_user(name, utsname(), sizeof(*name)))
1210 error = -EFAULT;
1211 up_read(&uts_sem);
1212
Andi Kleenbe274252011-08-19 16:15:10 -07001213 if (!error && override_release(name->release, sizeof(name->release)))
1214 error = -EFAULT;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001215 if (!error && override_architecture(name))
1216 error = -EFAULT;
1217 return error;
1218}
1219
1220SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
1221{
1222 int error;
1223
1224 if (!name)
1225 return -EFAULT;
1226 if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
1227 return -EFAULT;
1228
1229 down_read(&uts_sem);
1230 error = __copy_to_user(&name->sysname, &utsname()->sysname,
1231 __OLD_UTS_LEN);
1232 error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
1233 error |= __copy_to_user(&name->nodename, &utsname()->nodename,
1234 __OLD_UTS_LEN);
1235 error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
1236 error |= __copy_to_user(&name->release, &utsname()->release,
1237 __OLD_UTS_LEN);
1238 error |= __put_user(0, name->release + __OLD_UTS_LEN);
1239 error |= __copy_to_user(&name->version, &utsname()->version,
1240 __OLD_UTS_LEN);
1241 error |= __put_user(0, name->version + __OLD_UTS_LEN);
1242 error |= __copy_to_user(&name->machine, &utsname()->machine,
1243 __OLD_UTS_LEN);
1244 error |= __put_user(0, name->machine + __OLD_UTS_LEN);
1245 up_read(&uts_sem);
1246
1247 if (!error && override_architecture(name))
1248 error = -EFAULT;
Andi Kleenbe274252011-08-19 16:15:10 -07001249 if (!error && override_release(name->release, sizeof(name->release)))
1250 error = -EFAULT;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001251 return error ? -EFAULT : 0;
1252}
1253#endif
1254
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001255SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
1257 int errno;
1258 char tmp[__NEW_UTS_LEN];
1259
Serge E. Hallynbb96a6f2011-03-23 16:43:18 -07001260 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (len < 0 || len > __NEW_UTS_LEN)
1264 return -EINVAL;
1265 down_write(&uts_sem);
1266 errno = -EFAULT;
1267 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001268 struct new_utsname *u = utsname();
1269
1270 memcpy(u->nodename, tmp, len);
1271 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 errno = 0;
Sasikantha babu499eea62012-05-31 16:26:07 -07001273 uts_proc_notify(UTS_PROC_HOSTNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275 up_write(&uts_sem);
1276 return errno;
1277}
1278
1279#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1280
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001281SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 int i, errno;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001284 struct new_utsname *u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 if (len < 0)
1287 return -EINVAL;
1288 down_read(&uts_sem);
Andrew Morton9679e4d2008-10-15 22:01:51 -07001289 u = utsname();
1290 i = 1 + strlen(u->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (i > len)
1292 i = len;
1293 errno = 0;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001294 if (copy_to_user(name, u->nodename, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 errno = -EFAULT;
1296 up_read(&uts_sem);
1297 return errno;
1298}
1299
1300#endif
1301
1302/*
1303 * Only setdomainname; getdomainname can be implemented by calling
1304 * uname()
1305 */
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001306SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 int errno;
1309 char tmp[__NEW_UTS_LEN];
1310
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001311 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return -EPERM;
1313 if (len < 0 || len > __NEW_UTS_LEN)
1314 return -EINVAL;
1315
1316 down_write(&uts_sem);
1317 errno = -EFAULT;
1318 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001319 struct new_utsname *u = utsname();
1320
1321 memcpy(u->domainname, tmp, len);
1322 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 errno = 0;
Sasikantha babu499eea62012-05-31 16:26:07 -07001324 uts_proc_notify(UTS_PROC_DOMAINNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326 up_write(&uts_sem);
1327 return errno;
1328}
1329
Heiko Carstense48fbb62009-01-14 14:14:26 +01001330SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Jiri Slabyb9518342010-05-04 11:28:25 +02001332 struct rlimit value;
1333 int ret;
1334
1335 ret = do_prlimit(current, resource, NULL, &value);
1336 if (!ret)
1337 ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1338
1339 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
Al Virod9e968c2017-05-31 04:33:51 -04001342#ifdef CONFIG_COMPAT
1343
1344COMPAT_SYSCALL_DEFINE2(setrlimit, unsigned int, resource,
1345 struct compat_rlimit __user *, rlim)
1346{
1347 struct rlimit r;
1348 struct compat_rlimit r32;
1349
1350 if (copy_from_user(&r32, rlim, sizeof(struct compat_rlimit)))
1351 return -EFAULT;
1352
1353 if (r32.rlim_cur == COMPAT_RLIM_INFINITY)
1354 r.rlim_cur = RLIM_INFINITY;
1355 else
1356 r.rlim_cur = r32.rlim_cur;
1357 if (r32.rlim_max == COMPAT_RLIM_INFINITY)
1358 r.rlim_max = RLIM_INFINITY;
1359 else
1360 r.rlim_max = r32.rlim_max;
1361 return do_prlimit(current, resource, &r, NULL);
1362}
1363
1364COMPAT_SYSCALL_DEFINE2(getrlimit, unsigned int, resource,
1365 struct compat_rlimit __user *, rlim)
1366{
1367 struct rlimit r;
1368 int ret;
1369
1370 ret = do_prlimit(current, resource, NULL, &r);
1371 if (!ret) {
Al Viro58c7ffc2017-07-12 04:59:45 +01001372 struct compat_rlimit r32;
Al Virod9e968c2017-05-31 04:33:51 -04001373 if (r.rlim_cur > COMPAT_RLIM_INFINITY)
1374 r32.rlim_cur = COMPAT_RLIM_INFINITY;
1375 else
1376 r32.rlim_cur = r.rlim_cur;
1377 if (r.rlim_max > COMPAT_RLIM_INFINITY)
1378 r32.rlim_max = COMPAT_RLIM_INFINITY;
1379 else
1380 r32.rlim_max = r.rlim_max;
1381
1382 if (copy_to_user(rlim, &r32, sizeof(struct compat_rlimit)))
1383 return -EFAULT;
1384 }
1385 return ret;
1386}
1387
1388#endif
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1391
1392/*
1393 * Back compatibility for getrlimit. Needed for some apps.
1394 */
Heiko Carstense48fbb62009-01-14 14:14:26 +01001395SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1396 struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
1398 struct rlimit x;
1399 if (resource >= RLIM_NLIMITS)
1400 return -EINVAL;
1401
1402 task_lock(current->group_leader);
1403 x = current->signal->rlim[resource];
1404 task_unlock(current->group_leader);
Cal Peake756184b2006-09-30 23:27:24 -07001405 if (x.rlim_cur > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 x.rlim_cur = 0x7FFFFFFF;
Cal Peake756184b2006-09-30 23:27:24 -07001407 if (x.rlim_max > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 x.rlim_max = 0x7FFFFFFF;
vishnu.psec94fc32014-10-09 15:30:23 -07001409 return copy_to_user(rlim, &x, sizeof(x)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
Al Viro613763a2017-05-26 22:04:29 -04001412#ifdef CONFIG_COMPAT
1413COMPAT_SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1414 struct compat_rlimit __user *, rlim)
1415{
1416 struct rlimit r;
1417
1418 if (resource >= RLIM_NLIMITS)
1419 return -EINVAL;
1420
1421 task_lock(current->group_leader);
1422 r = current->signal->rlim[resource];
1423 task_unlock(current->group_leader);
1424 if (r.rlim_cur > 0x7FFFFFFF)
1425 r.rlim_cur = 0x7FFFFFFF;
1426 if (r.rlim_max > 0x7FFFFFFF)
1427 r.rlim_max = 0x7FFFFFFF;
1428
1429 if (put_user(r.rlim_cur, &rlim->rlim_cur) ||
1430 put_user(r.rlim_max, &rlim->rlim_max))
1431 return -EFAULT;
1432 return 0;
1433}
1434#endif
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436#endif
1437
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001438static inline bool rlim64_is_infinity(__u64 rlim64)
1439{
1440#if BITS_PER_LONG < 64
1441 return rlim64 >= ULONG_MAX;
1442#else
1443 return rlim64 == RLIM64_INFINITY;
1444#endif
1445}
1446
1447static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1448{
1449 if (rlim->rlim_cur == RLIM_INFINITY)
1450 rlim64->rlim_cur = RLIM64_INFINITY;
1451 else
1452 rlim64->rlim_cur = rlim->rlim_cur;
1453 if (rlim->rlim_max == RLIM_INFINITY)
1454 rlim64->rlim_max = RLIM64_INFINITY;
1455 else
1456 rlim64->rlim_max = rlim->rlim_max;
1457}
1458
1459static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1460{
1461 if (rlim64_is_infinity(rlim64->rlim_cur))
1462 rlim->rlim_cur = RLIM_INFINITY;
1463 else
1464 rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1465 if (rlim64_is_infinity(rlim64->rlim_max))
1466 rlim->rlim_max = RLIM_INFINITY;
1467 else
1468 rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1469}
1470
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001471/* make sure you are allowed to change @tsk limits before calling this */
Jiri Slaby5b415352010-03-24 16:11:29 +01001472int do_prlimit(struct task_struct *tsk, unsigned int resource,
1473 struct rlimit *new_rlim, struct rlimit *old_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Jiri Slaby5b415352010-03-24 16:11:29 +01001475 struct rlimit *rlim;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001476 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 if (resource >= RLIM_NLIMITS)
1479 return -EINVAL;
Jiri Slaby5b415352010-03-24 16:11:29 +01001480 if (new_rlim) {
1481 if (new_rlim->rlim_cur > new_rlim->rlim_max)
1482 return -EINVAL;
1483 if (resource == RLIMIT_NOFILE &&
1484 new_rlim->rlim_max > sysctl_nr_open)
1485 return -EPERM;
1486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001488 /* protect tsk->signal and tsk->sighand from disappearing */
1489 read_lock(&tasklist_lock);
1490 if (!tsk->sighand) {
1491 retval = -ESRCH;
1492 goto out;
1493 }
1494
Jiri Slaby5b415352010-03-24 16:11:29 +01001495 rlim = tsk->signal->rlim + resource;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001496 task_lock(tsk->group_leader);
Jiri Slaby5b415352010-03-24 16:11:29 +01001497 if (new_rlim) {
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001498 /* Keep the capable check against init_user_ns until
1499 cgroups can contain all limits */
Jiri Slaby5b415352010-03-24 16:11:29 +01001500 if (new_rlim->rlim_max > rlim->rlim_max &&
1501 !capable(CAP_SYS_RESOURCE))
1502 retval = -EPERM;
1503 if (!retval)
Eric W. Biedermancad4ea52017-04-12 17:22:14 -05001504 retval = security_task_setrlimit(tsk, resource, new_rlim);
Jiri Slaby5b415352010-03-24 16:11:29 +01001505 if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
1506 /*
1507 * The caller is asking for an immediate RLIMIT_CPU
1508 * expiry. But we use the zero value to mean "it was
1509 * never set". So let's cheat and make it one second
1510 * instead
1511 */
1512 new_rlim->rlim_cur = 1;
1513 }
Tom Alsberg9926e4c2007-05-08 00:30:31 -07001514 }
Jiri Slaby5b415352010-03-24 16:11:29 +01001515 if (!retval) {
1516 if (old_rlim)
1517 *old_rlim = *rlim;
1518 if (new_rlim)
1519 *rlim = *new_rlim;
1520 }
Jiri Slaby7855c352009-08-26 23:45:34 +02001521 task_unlock(tsk->group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Andrew Mortond3561f72006-03-24 03:18:36 -08001523 /*
1524 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1525 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1526 * very long-standing error, and fixing it now risks breakage of
1527 * applications, so we live with it
1528 */
Jiri Slaby5b415352010-03-24 16:11:29 +01001529 if (!retval && new_rlim && resource == RLIMIT_CPU &&
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05001530 new_rlim->rlim_cur != RLIM_INFINITY &&
1531 IS_ENABLED(CONFIG_POSIX_TIMERS))
Jiri Slaby5b415352010-03-24 16:11:29 +01001532 update_rlimit_cpu(tsk, new_rlim->rlim_cur);
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001533out:
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001534 read_unlock(&tasklist_lock);
Oleg Nesterov2fb9d262009-09-03 19:21:45 +02001535 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001538/* rcu lock must be held */
Stephen Smalley791ec492017-02-17 07:57:00 -05001539static int check_prlimit_permission(struct task_struct *task,
1540 unsigned int flags)
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001541{
1542 const struct cred *cred = current_cred(), *tcred;
Stephen Smalley791ec492017-02-17 07:57:00 -05001543 bool id_match;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001544
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001545 if (current == task)
1546 return 0;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001547
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001548 tcred = __task_cred(task);
Stephen Smalley791ec492017-02-17 07:57:00 -05001549 id_match = (uid_eq(cred->uid, tcred->euid) &&
1550 uid_eq(cred->uid, tcred->suid) &&
1551 uid_eq(cred->uid, tcred->uid) &&
1552 gid_eq(cred->gid, tcred->egid) &&
1553 gid_eq(cred->gid, tcred->sgid) &&
1554 gid_eq(cred->gid, tcred->gid));
1555 if (!id_match && !ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
1556 return -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001557
Stephen Smalley791ec492017-02-17 07:57:00 -05001558 return security_task_prlimit(cred, tcred, flags);
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001559}
1560
1561SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1562 const struct rlimit64 __user *, new_rlim,
1563 struct rlimit64 __user *, old_rlim)
1564{
1565 struct rlimit64 old64, new64;
1566 struct rlimit old, new;
1567 struct task_struct *tsk;
Stephen Smalley791ec492017-02-17 07:57:00 -05001568 unsigned int checkflags = 0;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001569 int ret;
1570
Stephen Smalley791ec492017-02-17 07:57:00 -05001571 if (old_rlim)
1572 checkflags |= LSM_PRLIMIT_READ;
1573
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001574 if (new_rlim) {
1575 if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1576 return -EFAULT;
1577 rlim64_to_rlim(&new64, &new);
Stephen Smalley791ec492017-02-17 07:57:00 -05001578 checkflags |= LSM_PRLIMIT_WRITE;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001579 }
1580
1581 rcu_read_lock();
1582 tsk = pid ? find_task_by_vpid(pid) : current;
1583 if (!tsk) {
1584 rcu_read_unlock();
1585 return -ESRCH;
1586 }
Stephen Smalley791ec492017-02-17 07:57:00 -05001587 ret = check_prlimit_permission(tsk, checkflags);
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001588 if (ret) {
1589 rcu_read_unlock();
1590 return ret;
1591 }
1592 get_task_struct(tsk);
1593 rcu_read_unlock();
1594
1595 ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1596 old_rlim ? &old : NULL);
1597
1598 if (!ret && old_rlim) {
1599 rlim_to_rlim64(&old, &old64);
1600 if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1601 ret = -EFAULT;
1602 }
1603
1604 put_task_struct(tsk);
1605 return ret;
1606}
1607
Jiri Slaby7855c352009-08-26 23:45:34 +02001608SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1609{
1610 struct rlimit new_rlim;
1611
1612 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1613 return -EFAULT;
Jiri Slaby5b415352010-03-24 16:11:29 +01001614 return do_prlimit(current, resource, &new_rlim, NULL);
Jiri Slaby7855c352009-08-26 23:45:34 +02001615}
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617/*
1618 * It would make sense to put struct rusage in the task_struct,
1619 * except that would make the task_struct be *really big*. After
1620 * task_struct gets moved into malloc'ed memory, it would
1621 * make sense to do this. It will make moving the rest of the information
1622 * a lot simpler! (Which we're not doing right now because we're not
1623 * measuring them yet).
1624 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1626 * races with threads incrementing their own counters. But since word
1627 * reads are atomic, we either get new values or old values and we don't
1628 * care which for the sums. We always take the siglock to protect reading
1629 * the c* fields from p->signal from races with exit.c updating those
1630 * fields when reaping, so a sample either gets all the additions of a
1631 * given child after it's reaped, or none so this sample is before reaping.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001632 *
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001633 * Locking:
1634 * We need to take the siglock for CHILDEREN, SELF and BOTH
1635 * for the cases current multithreaded, non-current single threaded
1636 * non-current multithreaded. Thread traversal is now safe with
1637 * the siglock held.
1638 * Strictly speaking, we donot need to take the siglock if we are current and
1639 * single threaded, as no one else can take our signal_struct away, no one
1640 * else can reap the children to update signal->c* counters, and no one else
1641 * can race with the signal-> fields. If we do not take any lock, the
1642 * signal-> fields could be read out of order while another thread was just
1643 * exiting. So we should place a read memory barrier when we avoid the lock.
1644 * On the writer side, write memory barrier is implied in __exit_signal
1645 * as __exit_signal releases the siglock spinlock after updating the signal->
1646 * fields. But we don't do this yet to keep things simple.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001647 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 */
1649
Frank Mayharf06febc2008-09-12 09:54:39 -07001650static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001651{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001652 r->ru_nvcsw += t->nvcsw;
1653 r->ru_nivcsw += t->nivcsw;
1654 r->ru_minflt += t->min_flt;
1655 r->ru_majflt += t->maj_flt;
1656 r->ru_inblock += task_io_get_inblock(t);
1657 r->ru_oublock += task_io_get_oublock(t);
1658}
1659
Al Viroce72a162017-05-14 20:25:02 -04001660void getrusage(struct task_struct *p, int who, struct rusage *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
1662 struct task_struct *t;
1663 unsigned long flags;
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001664 u64 tgutime, tgstime, utime, stime;
Jiri Pirko1f102062009-09-22 16:44:10 -07001665 unsigned long maxrss = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
vishnu.psec94fc32014-10-09 15:30:23 -07001667 memset((char *)r, 0, sizeof (*r));
Martin Schwidefsky64861632011-12-15 14:56:09 +01001668 utime = stime = 0;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001669
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001670 if (who == RUSAGE_THREAD) {
Frederic Weisbeckere80d0a1a2012-11-21 16:26:44 +01001671 task_cputime_adjusted(current, &utime, &stime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001672 accumulate_thread_rusage(p, r);
Jiri Pirko1f102062009-09-22 16:44:10 -07001673 maxrss = p->signal->maxrss;
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001674 goto out;
1675 }
1676
Oleg Nesterovd6cf7232008-04-30 00:52:38 -07001677 if (!lock_task_sighand(p, &flags))
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001678 return;
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 switch (who) {
vishnu.psec94fc32014-10-09 15:30:23 -07001681 case RUSAGE_BOTH:
1682 case RUSAGE_CHILDREN:
1683 utime = p->signal->cutime;
1684 stime = p->signal->cstime;
1685 r->ru_nvcsw = p->signal->cnvcsw;
1686 r->ru_nivcsw = p->signal->cnivcsw;
1687 r->ru_minflt = p->signal->cmin_flt;
1688 r->ru_majflt = p->signal->cmaj_flt;
1689 r->ru_inblock = p->signal->cinblock;
1690 r->ru_oublock = p->signal->coublock;
1691 maxrss = p->signal->cmaxrss;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001692
vishnu.psec94fc32014-10-09 15:30:23 -07001693 if (who == RUSAGE_CHILDREN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 break;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001695
vishnu.psec94fc32014-10-09 15:30:23 -07001696 case RUSAGE_SELF:
1697 thread_group_cputime_adjusted(p, &tgutime, &tgstime);
1698 utime += tgutime;
1699 stime += tgstime;
1700 r->ru_nvcsw += p->signal->nvcsw;
1701 r->ru_nivcsw += p->signal->nivcsw;
1702 r->ru_minflt += p->signal->min_flt;
1703 r->ru_majflt += p->signal->maj_flt;
1704 r->ru_inblock += p->signal->inblock;
1705 r->ru_oublock += p->signal->oublock;
1706 if (maxrss < p->signal->maxrss)
1707 maxrss = p->signal->maxrss;
1708 t = p;
1709 do {
1710 accumulate_thread_rusage(t, r);
1711 } while_each_thread(p, t);
1712 break;
1713
1714 default:
1715 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001717 unlock_task_sighand(p, &flags);
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001718
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001719out:
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001720 r->ru_utime = ns_to_timeval(utime);
1721 r->ru_stime = ns_to_timeval(stime);
Jiri Pirko1f102062009-09-22 16:44:10 -07001722
1723 if (who != RUSAGE_CHILDREN) {
1724 struct mm_struct *mm = get_task_mm(p);
vishnu.psec94fc32014-10-09 15:30:23 -07001725
Jiri Pirko1f102062009-09-22 16:44:10 -07001726 if (mm) {
1727 setmax_mm_hiwater_rss(&maxrss, mm);
1728 mmput(mm);
1729 }
1730 }
1731 r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733
Al Viroce72a162017-05-14 20:25:02 -04001734SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 struct rusage r;
vishnu.psec94fc32014-10-09 15:30:23 -07001737
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001738 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1739 who != RUSAGE_THREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return -EINVAL;
Al Viroce72a162017-05-14 20:25:02 -04001741
1742 getrusage(current, who, &r);
1743 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
1745
Al Viro8d2d5c42013-03-03 12:49:06 -05001746#ifdef CONFIG_COMPAT
1747COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru)
1748{
1749 struct rusage r;
1750
1751 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1752 who != RUSAGE_THREAD)
1753 return -EINVAL;
1754
Al Viroce72a162017-05-14 20:25:02 -04001755 getrusage(current, who, &r);
Al Viro8d2d5c42013-03-03 12:49:06 -05001756 return put_compat_rusage(&r, ru);
1757}
1758#endif
1759
Heiko Carstense48fbb62009-01-14 14:14:26 +01001760SYSCALL_DEFINE1(umask, int, mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1763 return mask;
1764}
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -08001765
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001766static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001767{
Al Viro2903ff02012-08-28 12:52:22 -04001768 struct fd exe;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001769 struct file *old_exe, *exe_file;
Al Viro496ad9a2013-01-23 17:07:38 -05001770 struct inode *inode;
Al Viro2903ff02012-08-28 12:52:22 -04001771 int err;
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001772
Al Viro2903ff02012-08-28 12:52:22 -04001773 exe = fdget(fd);
1774 if (!exe.file)
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001775 return -EBADF;
1776
Al Viro496ad9a2013-01-23 17:07:38 -05001777 inode = file_inode(exe.file);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001778
1779 /*
1780 * Because the original mm->exe_file points to executable file, make
1781 * sure that this one is executable as well, to avoid breaking an
1782 * overall picture.
1783 */
1784 err = -EACCES;
Eric W. Biederman90f85722015-06-29 14:42:03 -05001785 if (!S_ISREG(inode->i_mode) || path_noexec(&exe.file->f_path))
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001786 goto exit;
1787
Al Viro496ad9a2013-01-23 17:07:38 -05001788 err = inode_permission(inode, MAY_EXEC);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001789 if (err)
1790 goto exit;
1791
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001792 /*
Konstantin Khlebnikov4229fb1d2012-07-11 14:02:11 -07001793 * Forbid mm->exe_file change if old file still mapped.
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001794 */
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001795 exe_file = get_mm_exe_file(mm);
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001796 err = -EBUSY;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001797 if (exe_file) {
Konstantin Khlebnikov4229fb1d2012-07-11 14:02:11 -07001798 struct vm_area_struct *vma;
1799
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001800 down_read(&mm->mmap_sem);
1801 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1802 if (!vma->vm_file)
1803 continue;
1804 if (path_equal(&vma->vm_file->f_path,
1805 &exe_file->f_path))
1806 goto exit_err;
1807 }
1808
1809 up_read(&mm->mmap_sem);
1810 fput(exe_file);
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001811 }
1812
Konstantin Khlebnikov4229fb1d2012-07-11 14:02:11 -07001813 err = 0;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001814 /* set the new file, lockless */
1815 get_file(exe.file);
1816 old_exe = xchg(&mm->exe_file, exe.file);
1817 if (old_exe)
1818 fput(old_exe);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001819exit:
Al Viro2903ff02012-08-28 12:52:22 -04001820 fdput(exe);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001821 return err;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001822exit_err:
1823 up_read(&mm->mmap_sem);
1824 fput(exe_file);
1825 goto exit;
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001826}
1827
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001828/*
1829 * WARNING: we don't require any capability here so be very careful
1830 * in what is allowed for modification from userspace.
1831 */
1832static int validate_prctl_map(struct prctl_mm_map *prctl_map)
1833{
1834 unsigned long mmap_max_addr = TASK_SIZE;
1835 struct mm_struct *mm = current->mm;
1836 int error = -EINVAL, i;
1837
1838 static const unsigned char offsets[] = {
1839 offsetof(struct prctl_mm_map, start_code),
1840 offsetof(struct prctl_mm_map, end_code),
1841 offsetof(struct prctl_mm_map, start_data),
1842 offsetof(struct prctl_mm_map, end_data),
1843 offsetof(struct prctl_mm_map, start_brk),
1844 offsetof(struct prctl_mm_map, brk),
1845 offsetof(struct prctl_mm_map, start_stack),
1846 offsetof(struct prctl_mm_map, arg_start),
1847 offsetof(struct prctl_mm_map, arg_end),
1848 offsetof(struct prctl_mm_map, env_start),
1849 offsetof(struct prctl_mm_map, env_end),
1850 };
1851
1852 /*
1853 * Make sure the members are not somewhere outside
1854 * of allowed address space.
1855 */
1856 for (i = 0; i < ARRAY_SIZE(offsets); i++) {
1857 u64 val = *(u64 *)((char *)prctl_map + offsets[i]);
1858
1859 if ((unsigned long)val >= mmap_max_addr ||
1860 (unsigned long)val < mmap_min_addr)
1861 goto out;
1862 }
1863
1864 /*
1865 * Make sure the pairs are ordered.
1866 */
1867#define __prctl_check_order(__m1, __op, __m2) \
1868 ((unsigned long)prctl_map->__m1 __op \
1869 (unsigned long)prctl_map->__m2) ? 0 : -EINVAL
1870 error = __prctl_check_order(start_code, <, end_code);
1871 error |= __prctl_check_order(start_data, <, end_data);
1872 error |= __prctl_check_order(start_brk, <=, brk);
1873 error |= __prctl_check_order(arg_start, <=, arg_end);
1874 error |= __prctl_check_order(env_start, <=, env_end);
1875 if (error)
1876 goto out;
1877#undef __prctl_check_order
1878
1879 error = -EINVAL;
1880
1881 /*
1882 * @brk should be after @end_data in traditional maps.
1883 */
1884 if (prctl_map->start_brk <= prctl_map->end_data ||
1885 prctl_map->brk <= prctl_map->end_data)
1886 goto out;
1887
1888 /*
1889 * Neither we should allow to override limits if they set.
1890 */
1891 if (check_data_rlimit(rlimit(RLIMIT_DATA), prctl_map->brk,
1892 prctl_map->start_brk, prctl_map->end_data,
1893 prctl_map->start_data))
1894 goto out;
1895
1896 /*
1897 * Someone is trying to cheat the auxv vector.
1898 */
1899 if (prctl_map->auxv_size) {
1900 if (!prctl_map->auxv || prctl_map->auxv_size > sizeof(mm->saved_auxv))
1901 goto out;
1902 }
1903
1904 /*
1905 * Finally, make sure the caller has the rights to
Kirill Tkhai4d28df62017-05-12 17:33:36 +03001906 * change /proc/pid/exe link: only local sys admin should
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001907 * be allowed to.
1908 */
1909 if (prctl_map->exe_fd != (u32)-1) {
Kirill Tkhai4d28df62017-05-12 17:33:36 +03001910 if (!ns_capable(current_user_ns(), CAP_SYS_ADMIN))
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001911 goto out;
1912 }
1913
1914 error = 0;
1915out:
1916 return error;
1917}
1918
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07001919#ifdef CONFIG_CHECKPOINT_RESTORE
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001920static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data_size)
1921{
1922 struct prctl_mm_map prctl_map = { .exe_fd = (u32)-1, };
1923 unsigned long user_auxv[AT_VECTOR_SIZE];
1924 struct mm_struct *mm = current->mm;
1925 int error;
1926
1927 BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
1928 BUILD_BUG_ON(sizeof(struct prctl_mm_map) > 256);
1929
1930 if (opt == PR_SET_MM_MAP_SIZE)
1931 return put_user((unsigned int)sizeof(prctl_map),
1932 (unsigned int __user *)addr);
1933
1934 if (data_size != sizeof(prctl_map))
1935 return -EINVAL;
1936
1937 if (copy_from_user(&prctl_map, addr, sizeof(prctl_map)))
1938 return -EFAULT;
1939
1940 error = validate_prctl_map(&prctl_map);
1941 if (error)
1942 return error;
1943
1944 if (prctl_map.auxv_size) {
1945 memset(user_auxv, 0, sizeof(user_auxv));
1946 if (copy_from_user(user_auxv,
1947 (const void __user *)prctl_map.auxv,
1948 prctl_map.auxv_size))
1949 return -EFAULT;
1950
1951 /* Last entry must be AT_NULL as specification requires */
1952 user_auxv[AT_VECTOR_SIZE - 2] = AT_NULL;
1953 user_auxv[AT_VECTOR_SIZE - 1] = AT_NULL;
1954 }
1955
Mateusz Guzikddf1d392016-01-20 15:01:02 -08001956 if (prctl_map.exe_fd != (u32)-1) {
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001957 error = prctl_set_mm_exe_file(mm, prctl_map.exe_fd);
Mateusz Guzikddf1d392016-01-20 15:01:02 -08001958 if (error)
1959 return error;
1960 }
1961
1962 down_write(&mm->mmap_sem);
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001963
1964 /*
1965 * We don't validate if these members are pointing to
1966 * real present VMAs because application may have correspond
1967 * VMAs already unmapped and kernel uses these members for statistics
1968 * output in procfs mostly, except
1969 *
1970 * - @start_brk/@brk which are used in do_brk but kernel lookups
1971 * for VMAs when updating these memvers so anything wrong written
1972 * here cause kernel to swear at userspace program but won't lead
1973 * to any problem in kernel itself
1974 */
1975
1976 mm->start_code = prctl_map.start_code;
1977 mm->end_code = prctl_map.end_code;
1978 mm->start_data = prctl_map.start_data;
1979 mm->end_data = prctl_map.end_data;
1980 mm->start_brk = prctl_map.start_brk;
1981 mm->brk = prctl_map.brk;
1982 mm->start_stack = prctl_map.start_stack;
1983 mm->arg_start = prctl_map.arg_start;
1984 mm->arg_end = prctl_map.arg_end;
1985 mm->env_start = prctl_map.env_start;
1986 mm->env_end = prctl_map.env_end;
1987
1988 /*
1989 * Note this update of @saved_auxv is lockless thus
1990 * if someone reads this member in procfs while we're
1991 * updating -- it may get partly updated results. It's
1992 * known and acceptable trade off: we leave it as is to
1993 * not introduce additional locks here making the kernel
1994 * more complex.
1995 */
1996 if (prctl_map.auxv_size)
1997 memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
1998
Mateusz Guzikddf1d392016-01-20 15:01:02 -08001999 up_write(&mm->mmap_sem);
2000 return 0;
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07002001}
2002#endif /* CONFIG_CHECKPOINT_RESTORE */
2003
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002004static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr,
2005 unsigned long len)
2006{
2007 /*
2008 * This doesn't move the auxiliary vector itself since it's pinned to
2009 * mm_struct, but it permits filling the vector with new values. It's
2010 * up to the caller to provide sane values here, otherwise userspace
2011 * tools which use this vector might be unhappy.
2012 */
2013 unsigned long user_auxv[AT_VECTOR_SIZE];
2014
2015 if (len > sizeof(user_auxv))
2016 return -EINVAL;
2017
2018 if (copy_from_user(user_auxv, (const void __user *)addr, len))
2019 return -EFAULT;
2020
2021 /* Make sure the last entry is always AT_NULL */
2022 user_auxv[AT_VECTOR_SIZE - 2] = 0;
2023 user_auxv[AT_VECTOR_SIZE - 1] = 0;
2024
2025 BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
2026
2027 task_lock(current);
2028 memcpy(mm->saved_auxv, user_auxv, len);
2029 task_unlock(current);
2030
2031 return 0;
2032}
2033
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002034static int prctl_set_mm(int opt, unsigned long addr,
2035 unsigned long arg4, unsigned long arg5)
2036{
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002037 struct mm_struct *mm = current->mm;
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002038 struct prctl_mm_map prctl_map;
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002039 struct vm_area_struct *vma;
2040 int error;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002041
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07002042 if (arg5 || (arg4 && (opt != PR_SET_MM_AUXV &&
2043 opt != PR_SET_MM_MAP &&
2044 opt != PR_SET_MM_MAP_SIZE)))
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002045 return -EINVAL;
2046
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07002047#ifdef CONFIG_CHECKPOINT_RESTORE
2048 if (opt == PR_SET_MM_MAP || opt == PR_SET_MM_MAP_SIZE)
2049 return prctl_set_mm_map(opt, (const void __user *)addr, arg4);
2050#endif
2051
Cyrill Gorcunov79f07132012-03-15 15:17:10 -07002052 if (!capable(CAP_SYS_RESOURCE))
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002053 return -EPERM;
2054
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07002055 if (opt == PR_SET_MM_EXE_FILE)
2056 return prctl_set_mm_exe_file(mm, (unsigned int)addr);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07002057
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002058 if (opt == PR_SET_MM_AUXV)
2059 return prctl_set_auxv(mm, addr, arg4);
2060
Cyrill Gorcunov1ad75b92012-06-07 14:21:11 -07002061 if (addr >= TASK_SIZE || addr < mmap_min_addr)
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002062 return -EINVAL;
2063
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002064 error = -EINVAL;
2065
Mateusz Guzikddf1d392016-01-20 15:01:02 -08002066 down_write(&mm->mmap_sem);
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002067 vma = find_vma(mm, addr);
2068
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002069 prctl_map.start_code = mm->start_code;
2070 prctl_map.end_code = mm->end_code;
2071 prctl_map.start_data = mm->start_data;
2072 prctl_map.end_data = mm->end_data;
2073 prctl_map.start_brk = mm->start_brk;
2074 prctl_map.brk = mm->brk;
2075 prctl_map.start_stack = mm->start_stack;
2076 prctl_map.arg_start = mm->arg_start;
2077 prctl_map.arg_end = mm->arg_end;
2078 prctl_map.env_start = mm->env_start;
2079 prctl_map.env_end = mm->env_end;
2080 prctl_map.auxv = NULL;
2081 prctl_map.auxv_size = 0;
2082 prctl_map.exe_fd = -1;
2083
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002084 switch (opt) {
2085 case PR_SET_MM_START_CODE:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002086 prctl_map.start_code = addr;
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002087 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002088 case PR_SET_MM_END_CODE:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002089 prctl_map.end_code = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002090 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002091 case PR_SET_MM_START_DATA:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002092 prctl_map.start_data = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002093 break;
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002094 case PR_SET_MM_END_DATA:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002095 prctl_map.end_data = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002096 break;
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002097 case PR_SET_MM_START_STACK:
2098 prctl_map.start_stack = addr;
2099 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002100 case PR_SET_MM_START_BRK:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002101 prctl_map.start_brk = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002102 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002103 case PR_SET_MM_BRK:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002104 prctl_map.brk = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002105 break;
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002106 case PR_SET_MM_ARG_START:
2107 prctl_map.arg_start = addr;
2108 break;
2109 case PR_SET_MM_ARG_END:
2110 prctl_map.arg_end = addr;
2111 break;
2112 case PR_SET_MM_ENV_START:
2113 prctl_map.env_start = addr;
2114 break;
2115 case PR_SET_MM_ENV_END:
2116 prctl_map.env_end = addr;
2117 break;
2118 default:
2119 goto out;
2120 }
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002121
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002122 error = validate_prctl_map(&prctl_map);
2123 if (error)
2124 goto out;
2125
2126 switch (opt) {
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002127 /*
2128 * If command line arguments and environment
2129 * are placed somewhere else on stack, we can
2130 * set them up here, ARG_START/END to setup
2131 * command line argumets and ENV_START/END
2132 * for environment.
2133 */
2134 case PR_SET_MM_START_STACK:
2135 case PR_SET_MM_ARG_START:
2136 case PR_SET_MM_ARG_END:
2137 case PR_SET_MM_ENV_START:
2138 case PR_SET_MM_ENV_END:
2139 if (!vma) {
2140 error = -EFAULT;
2141 goto out;
2142 }
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002143 }
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002144
2145 mm->start_code = prctl_map.start_code;
2146 mm->end_code = prctl_map.end_code;
2147 mm->start_data = prctl_map.start_data;
2148 mm->end_data = prctl_map.end_data;
2149 mm->start_brk = prctl_map.start_brk;
2150 mm->brk = prctl_map.brk;
2151 mm->start_stack = prctl_map.start_stack;
2152 mm->arg_start = prctl_map.arg_start;
2153 mm->arg_end = prctl_map.arg_end;
2154 mm->env_start = prctl_map.env_start;
2155 mm->env_end = prctl_map.env_end;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002156
2157 error = 0;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002158out:
Mateusz Guzikddf1d392016-01-20 15:01:02 -08002159 up_write(&mm->mmap_sem);
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002160 return error;
2161}
Cyrill Gorcunov300f7862012-06-07 14:21:12 -07002162
Amnon Shiloh52b36942013-04-30 15:28:48 -07002163#ifdef CONFIG_CHECKPOINT_RESTORE
Cyrill Gorcunov300f7862012-06-07 14:21:12 -07002164static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
2165{
2166 return put_user(me->clear_child_tid, tid_addr);
2167}
Amnon Shiloh52b36942013-04-30 15:28:48 -07002168#else
Cyrill Gorcunov300f7862012-06-07 14:21:12 -07002169static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
2170{
2171 return -EINVAL;
2172}
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002173#endif
2174
Pavel Tikhomirov749860c2017-01-30 18:06:12 +03002175static int propagate_has_child_subreaper(struct task_struct *p, void *data)
2176{
2177 /*
2178 * If task has has_child_subreaper - all its decendants
2179 * already have these flag too and new decendants will
2180 * inherit it on fork, skip them.
2181 *
2182 * If we've found child_reaper - skip descendants in
2183 * it's subtree as they will never get out pidns.
2184 */
2185 if (p->signal->has_child_subreaper ||
2186 is_child_reaper(task_pid(p)))
2187 return 0;
2188
2189 p->signal->has_child_subreaper = 1;
2190 return 1;
2191}
2192
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01002193SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
2194 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
David Howellsb6dff3e2008-11-14 10:39:16 +11002196 struct task_struct *me = current;
2197 unsigned char comm[sizeof(me->comm)];
2198 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
David Howellsd84f4f92008-11-14 10:39:23 +11002200 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2201 if (error != -ENOSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 return error;
2203
David Howellsd84f4f92008-11-14 10:39:23 +11002204 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 switch (option) {
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002206 case PR_SET_PDEATHSIG:
2207 if (!valid_signal(arg2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 error = -EINVAL;
2209 break;
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002210 }
2211 me->pdeath_signal = arg2;
2212 break;
2213 case PR_GET_PDEATHSIG:
2214 error = put_user(me->pdeath_signal, (int __user *)arg2);
2215 break;
2216 case PR_GET_DUMPABLE:
2217 error = get_dumpable(me->mm);
2218 break;
2219 case PR_SET_DUMPABLE:
2220 if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) {
2221 error = -EINVAL;
2222 break;
2223 }
2224 set_dumpable(me->mm, arg2);
2225 break;
2226
2227 case PR_SET_UNALIGN:
2228 error = SET_UNALIGN_CTL(me, arg2);
2229 break;
2230 case PR_GET_UNALIGN:
2231 error = GET_UNALIGN_CTL(me, arg2);
2232 break;
2233 case PR_SET_FPEMU:
2234 error = SET_FPEMU_CTL(me, arg2);
2235 break;
2236 case PR_GET_FPEMU:
2237 error = GET_FPEMU_CTL(me, arg2);
2238 break;
2239 case PR_SET_FPEXC:
2240 error = SET_FPEXC_CTL(me, arg2);
2241 break;
2242 case PR_GET_FPEXC:
2243 error = GET_FPEXC_CTL(me, arg2);
2244 break;
2245 case PR_GET_TIMING:
2246 error = PR_TIMING_STATISTICAL;
2247 break;
2248 case PR_SET_TIMING:
2249 if (arg2 != PR_TIMING_STATISTICAL)
2250 error = -EINVAL;
2251 break;
2252 case PR_SET_NAME:
2253 comm[sizeof(me->comm) - 1] = 0;
2254 if (strncpy_from_user(comm, (char __user *)arg2,
2255 sizeof(me->comm) - 1) < 0)
2256 return -EFAULT;
2257 set_task_comm(me, comm);
2258 proc_comm_connector(me);
2259 break;
2260 case PR_GET_NAME:
2261 get_task_comm(comm, me);
2262 if (copy_to_user((char __user *)arg2, comm, sizeof(comm)))
2263 return -EFAULT;
2264 break;
2265 case PR_GET_ENDIAN:
2266 error = GET_ENDIAN(me, arg2);
2267 break;
2268 case PR_SET_ENDIAN:
2269 error = SET_ENDIAN(me, arg2);
2270 break;
2271 case PR_GET_SECCOMP:
2272 error = prctl_get_seccomp();
2273 break;
2274 case PR_SET_SECCOMP:
2275 error = prctl_set_seccomp(arg2, (char __user *)arg3);
2276 break;
2277 case PR_GET_TSC:
2278 error = GET_TSC_CTL(arg2);
2279 break;
2280 case PR_SET_TSC:
2281 error = SET_TSC_CTL(arg2);
2282 break;
2283 case PR_TASK_PERF_EVENTS_DISABLE:
2284 error = perf_event_task_disable();
2285 break;
2286 case PR_TASK_PERF_EVENTS_ENABLE:
2287 error = perf_event_task_enable();
2288 break;
2289 case PR_GET_TIMERSLACK:
John Stultzda8b44d2016-03-17 14:20:51 -07002290 if (current->timer_slack_ns > ULONG_MAX)
2291 error = ULONG_MAX;
2292 else
2293 error = current->timer_slack_ns;
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002294 break;
2295 case PR_SET_TIMERSLACK:
2296 if (arg2 <= 0)
2297 current->timer_slack_ns =
2298 current->default_timer_slack_ns;
2299 else
2300 current->timer_slack_ns = arg2;
2301 break;
2302 case PR_MCE_KILL:
2303 if (arg4 | arg5)
2304 return -EINVAL;
2305 switch (arg2) {
2306 case PR_MCE_KILL_CLEAR:
2307 if (arg3 != 0)
2308 return -EINVAL;
2309 current->flags &= ~PF_MCE_PROCESS;
2310 break;
2311 case PR_MCE_KILL_SET:
2312 current->flags |= PF_MCE_PROCESS;
2313 if (arg3 == PR_MCE_KILL_EARLY)
2314 current->flags |= PF_MCE_EARLY;
2315 else if (arg3 == PR_MCE_KILL_LATE)
2316 current->flags &= ~PF_MCE_EARLY;
2317 else if (arg3 == PR_MCE_KILL_DEFAULT)
2318 current->flags &=
2319 ~(PF_MCE_EARLY|PF_MCE_PROCESS);
2320 else
2321 return -EINVAL;
2322 break;
2323 default:
2324 return -EINVAL;
2325 }
2326 break;
2327 case PR_MCE_KILL_GET:
2328 if (arg2 | arg3 | arg4 | arg5)
2329 return -EINVAL;
2330 if (current->flags & PF_MCE_PROCESS)
2331 error = (current->flags & PF_MCE_EARLY) ?
2332 PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
2333 else
2334 error = PR_MCE_KILL_DEFAULT;
2335 break;
2336 case PR_SET_MM:
2337 error = prctl_set_mm(arg2, arg3, arg4, arg5);
2338 break;
2339 case PR_GET_TID_ADDRESS:
2340 error = prctl_get_tid_address(me, (int __user **)arg2);
2341 break;
2342 case PR_SET_CHILD_SUBREAPER:
2343 me->signal->is_child_subreaper = !!arg2;
Pavel Tikhomirov749860c2017-01-30 18:06:12 +03002344 if (!arg2)
2345 break;
2346
2347 walk_process_tree(me, propagate_has_child_subreaper, NULL);
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002348 break;
2349 case PR_GET_CHILD_SUBREAPER:
2350 error = put_user(me->signal->is_child_subreaper,
2351 (int __user *)arg2);
2352 break;
2353 case PR_SET_NO_NEW_PRIVS:
2354 if (arg2 != 1 || arg3 || arg4 || arg5)
2355 return -EINVAL;
2356
Kees Cook1d4457f2014-05-21 15:23:46 -07002357 task_set_no_new_privs(current);
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002358 break;
2359 case PR_GET_NO_NEW_PRIVS:
2360 if (arg2 || arg3 || arg4 || arg5)
2361 return -EINVAL;
Kees Cook1d4457f2014-05-21 15:23:46 -07002362 return task_no_new_privs(current) ? 1 : 0;
Alex Thorltona0715cc2014-04-07 15:37:10 -07002363 case PR_GET_THP_DISABLE:
2364 if (arg2 || arg3 || arg4 || arg5)
2365 return -EINVAL;
Michal Hocko18600332017-07-10 15:48:02 -07002366 error = !!test_bit(MMF_DISABLE_THP, &me->mm->flags);
Alex Thorltona0715cc2014-04-07 15:37:10 -07002367 break;
2368 case PR_SET_THP_DISABLE:
2369 if (arg3 || arg4 || arg5)
2370 return -EINVAL;
Michal Hocko17b05732016-05-23 16:26:05 -07002371 if (down_write_killable(&me->mm->mmap_sem))
2372 return -EINTR;
Alex Thorltona0715cc2014-04-07 15:37:10 -07002373 if (arg2)
Michal Hocko18600332017-07-10 15:48:02 -07002374 set_bit(MMF_DISABLE_THP, &me->mm->flags);
Alex Thorltona0715cc2014-04-07 15:37:10 -07002375 else
Michal Hocko18600332017-07-10 15:48:02 -07002376 clear_bit(MMF_DISABLE_THP, &me->mm->flags);
Alex Thorltona0715cc2014-04-07 15:37:10 -07002377 up_write(&me->mm->mmap_sem);
2378 break;
Dave Hansenfe3d1972014-11-14 07:18:29 -08002379 case PR_MPX_ENABLE_MANAGEMENT:
Dave Hansene9d1b4f2015-01-08 14:30:22 -08002380 if (arg2 || arg3 || arg4 || arg5)
2381 return -EINVAL;
Dave Hansen46a6e0c2015-06-07 11:37:02 -07002382 error = MPX_ENABLE_MANAGEMENT();
Dave Hansenfe3d1972014-11-14 07:18:29 -08002383 break;
2384 case PR_MPX_DISABLE_MANAGEMENT:
Dave Hansene9d1b4f2015-01-08 14:30:22 -08002385 if (arg2 || arg3 || arg4 || arg5)
2386 return -EINVAL;
Dave Hansen46a6e0c2015-06-07 11:37:02 -07002387 error = MPX_DISABLE_MANAGEMENT();
Dave Hansenfe3d1972014-11-14 07:18:29 -08002388 break;
Paul Burton97915542015-01-08 12:17:37 +00002389 case PR_SET_FP_MODE:
2390 error = SET_FP_MODE(me, arg2);
2391 break;
2392 case PR_GET_FP_MODE:
2393 error = GET_FP_MODE(me);
2394 break;
Dave Martin2d2123b2017-10-31 15:51:14 +00002395 case PR_SVE_SET_VL:
2396 error = SVE_SET_VL(arg2);
2397 break;
2398 case PR_SVE_GET_VL:
2399 error = SVE_GET_VL();
2400 break;
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002401 default:
2402 error = -EINVAL;
2403 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 }
2405 return error;
2406}
Andi Kleen3cfc3482006-09-26 10:52:28 +02002407
Heiko Carstens836f92a2009-01-14 14:14:33 +01002408SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
2409 struct getcpu_cache __user *, unused)
Andi Kleen3cfc3482006-09-26 10:52:28 +02002410{
2411 int err = 0;
2412 int cpu = raw_smp_processor_id();
vishnu.psec94fc32014-10-09 15:30:23 -07002413
Andi Kleen3cfc3482006-09-26 10:52:28 +02002414 if (cpup)
2415 err |= put_user(cpu, cpup);
2416 if (nodep)
2417 err |= put_user(cpu_to_node(cpu), nodep);
Andi Kleen3cfc3482006-09-26 10:52:28 +02002418 return err ? -EFAULT : 0;
2419}
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002420
Stephen Rothwell4a22f162013-04-30 15:27:37 -07002421/**
2422 * do_sysinfo - fill in sysinfo struct
2423 * @info: pointer to buffer to fill
2424 */
2425static int do_sysinfo(struct sysinfo *info)
2426{
2427 unsigned long mem_total, sav_total;
2428 unsigned int mem_unit, bitcount;
2429 struct timespec tp;
2430
2431 memset(info, 0, sizeof(struct sysinfo));
2432
Oleg Nesterov45c64942013-07-03 15:05:01 -07002433 get_monotonic_boottime(&tp);
Stephen Rothwell4a22f162013-04-30 15:27:37 -07002434 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
2435
2436 get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
2437
2438 info->procs = nr_threads;
2439
2440 si_meminfo(info);
2441 si_swapinfo(info);
2442
2443 /*
2444 * If the sum of all the available memory (i.e. ram + swap)
2445 * is less than can be stored in a 32 bit unsigned long then
2446 * we can be binary compatible with 2.2.x kernels. If not,
2447 * well, in that case 2.2.x was broken anyways...
2448 *
2449 * -Erik Andersen <andersee@debian.org>
2450 */
2451
2452 mem_total = info->totalram + info->totalswap;
2453 if (mem_total < info->totalram || mem_total < info->totalswap)
2454 goto out;
2455 bitcount = 0;
2456 mem_unit = info->mem_unit;
2457 while (mem_unit > 1) {
2458 bitcount++;
2459 mem_unit >>= 1;
2460 sav_total = mem_total;
2461 mem_total <<= 1;
2462 if (mem_total < sav_total)
2463 goto out;
2464 }
2465
2466 /*
2467 * If mem_total did not overflow, multiply all memory values by
2468 * info->mem_unit and set it to 1. This leaves things compatible
2469 * with 2.2.x, and also retains compatibility with earlier 2.4.x
2470 * kernels...
2471 */
2472
2473 info->mem_unit = 1;
2474 info->totalram <<= bitcount;
2475 info->freeram <<= bitcount;
2476 info->sharedram <<= bitcount;
2477 info->bufferram <<= bitcount;
2478 info->totalswap <<= bitcount;
2479 info->freeswap <<= bitcount;
2480 info->totalhigh <<= bitcount;
2481 info->freehigh <<= bitcount;
2482
2483out:
2484 return 0;
2485}
2486
2487SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
2488{
2489 struct sysinfo val;
2490
2491 do_sysinfo(&val);
2492
2493 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
2494 return -EFAULT;
2495
2496 return 0;
2497}
2498
2499#ifdef CONFIG_COMPAT
2500struct compat_sysinfo {
2501 s32 uptime;
2502 u32 loads[3];
2503 u32 totalram;
2504 u32 freeram;
2505 u32 sharedram;
2506 u32 bufferram;
2507 u32 totalswap;
2508 u32 freeswap;
2509 u16 procs;
2510 u16 pad;
2511 u32 totalhigh;
2512 u32 freehigh;
2513 u32 mem_unit;
2514 char _f[20-2*sizeof(u32)-sizeof(int)];
2515};
2516
2517COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
2518{
2519 struct sysinfo s;
2520
2521 do_sysinfo(&s);
2522
2523 /* Check to see if any memory value is too large for 32-bit and scale
2524 * down if needed
2525 */
Scotty Bauer0baae412014-10-09 15:30:26 -07002526 if (upper_32_bits(s.totalram) || upper_32_bits(s.totalswap)) {
Stephen Rothwell4a22f162013-04-30 15:27:37 -07002527 int bitcount = 0;
2528
2529 while (s.mem_unit < PAGE_SIZE) {
2530 s.mem_unit <<= 1;
2531 bitcount++;
2532 }
2533
2534 s.totalram >>= bitcount;
2535 s.freeram >>= bitcount;
2536 s.sharedram >>= bitcount;
2537 s.bufferram >>= bitcount;
2538 s.totalswap >>= bitcount;
2539 s.freeswap >>= bitcount;
2540 s.totalhigh >>= bitcount;
2541 s.freehigh >>= bitcount;
2542 }
2543
2544 if (!access_ok(VERIFY_WRITE, info, sizeof(struct compat_sysinfo)) ||
2545 __put_user(s.uptime, &info->uptime) ||
2546 __put_user(s.loads[0], &info->loads[0]) ||
2547 __put_user(s.loads[1], &info->loads[1]) ||
2548 __put_user(s.loads[2], &info->loads[2]) ||
2549 __put_user(s.totalram, &info->totalram) ||
2550 __put_user(s.freeram, &info->freeram) ||
2551 __put_user(s.sharedram, &info->sharedram) ||
2552 __put_user(s.bufferram, &info->bufferram) ||
2553 __put_user(s.totalswap, &info->totalswap) ||
2554 __put_user(s.freeswap, &info->freeswap) ||
2555 __put_user(s.procs, &info->procs) ||
2556 __put_user(s.totalhigh, &info->totalhigh) ||
2557 __put_user(s.freehigh, &info->freehigh) ||
2558 __put_user(s.mem_unit, &info->mem_unit))
2559 return -EFAULT;
2560
2561 return 0;
2562}
2563#endif /* CONFIG_COMPAT */