blob: c541916b38c66d8763eebebad37a8293d1e4a60b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/sys.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
Paul Gortmaker9984de12011-05-23 14:51:41 -04007#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/mm.h>
9#include <linux/utsname.h>
10#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/reboot.h>
12#include <linux/prctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/highuid.h>
14#include <linux/fs.h>
Paul Gortmaker74da1ff2011-05-26 12:48:41 -040015#include <linux/kmod.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020016#include <linux/perf_event.h>
Daniel Walker3e88c552007-05-10 22:22:53 -070017#include <linux/resource.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070018#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/workqueue.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080020#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/device.h>
22#include <linux/key.h>
23#include <linux/times.h>
24#include <linux/posix-timers.h>
25#include <linux/security.h>
26#include <linux/dcookies.h>
27#include <linux/suspend.h>
28#include <linux/tty.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070029#include <linux/signal.h>
Matt Helsley9f460802005-11-07 00:59:16 -080030#include <linux/cn_proc.h>
Andi Kleen3cfc3482006-09-26 10:52:28 +020031#include <linux/getcpu.h>
Eric Dumazet6eaeeab2007-05-10 22:22:37 -070032#include <linux/task_io_accounting_ops.h>
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -070033#include <linux/seccomp.h>
Mark Lord40477272007-10-01 01:20:10 -070034#include <linux/cpu.h>
Christoph Hellwige28cbf22010-03-10 15:21:19 -080035#include <linux/personality.h>
Paul Mackerrase3d5a272009-01-06 14:41:02 -080036#include <linux/ptrace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040037#include <linux/fs_struct.h>
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -070038#include <linux/file.h>
39#include <linux/mount.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/gfp.h>
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010041#include <linux/syscore_ops.h>
Andi Kleenbe274252011-08-19 16:15:10 -070042#include <linux/version.h>
43#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <linux/compat.h>
46#include <linux/syscalls.h>
Keshavamurthy Anil S00d7c052005-12-12 00:37:33 -080047#include <linux/kprobes.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070048#include <linux/user_namespace.h>
Chen Gang7fe5e042013-02-21 16:43:06 -080049#include <linux/binfmts.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Stephen Rothwell4a22f162013-04-30 15:27:37 -070051#include <linux/sched.h>
Ingo Molnar4eb5aaa2017-02-08 18:51:29 +010052#include <linux/sched/autogroup.h>
Ingo Molnar4f177222017-02-08 08:45:17 +010053#include <linux/sched/loadavg.h>
Ingo Molnar03441a32017-02-08 18:51:35 +010054#include <linux/sched/stat.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010055#include <linux/sched/mm.h>
Ingo Molnarf7ccbae2017-02-08 18:51:30 +010056#include <linux/sched/coredump.h>
Ingo Molnar29930022017-02-08 18:51:36 +010057#include <linux/sched/task.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +010058#include <linux/sched/cputime.h>
Stephen Rothwell4a22f162013-04-30 15:27:37 -070059#include <linux/rcupdate.h>
60#include <linux/uidgid.h>
61#include <linux/cred.h>
62
Seiji Aguchi04c68622011-01-12 16:59:30 -080063#include <linux/kmsg_dump.h>
Andi Kleenbe274252011-08-19 16:15:10 -070064/* Move somewhere else to avoid recompiling? */
65#include <generated/utsrelease.h>
Seiji Aguchi04c68622011-01-12 16:59:30 -080066
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080067#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <asm/io.h>
69#include <asm/unistd.h>
70
71#ifndef SET_UNALIGN_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070072# define SET_UNALIGN_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#endif
74#ifndef GET_UNALIGN_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070075# define GET_UNALIGN_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#endif
77#ifndef SET_FPEMU_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070078# define SET_FPEMU_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#endif
80#ifndef GET_FPEMU_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070081# define GET_FPEMU_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#endif
83#ifndef SET_FPEXC_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070084# define SET_FPEXC_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#endif
86#ifndef GET_FPEXC_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070087# define GET_FPEXC_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#endif
Anton Blanchard651d7652006-06-07 16:10:19 +100089#ifndef GET_ENDIAN
vishnu.psec94fc32014-10-09 15:30:23 -070090# define GET_ENDIAN(a, b) (-EINVAL)
Anton Blanchard651d7652006-06-07 16:10:19 +100091#endif
92#ifndef SET_ENDIAN
vishnu.psec94fc32014-10-09 15:30:23 -070093# define SET_ENDIAN(a, b) (-EINVAL)
Anton Blanchard651d7652006-06-07 16:10:19 +100094#endif
Erik Bosman8fb402b2008-04-11 18:54:17 +020095#ifndef GET_TSC_CTL
96# define GET_TSC_CTL(a) (-EINVAL)
97#endif
98#ifndef SET_TSC_CTL
99# define SET_TSC_CTL(a) (-EINVAL)
100#endif
Dave Hansenfe3d1972014-11-14 07:18:29 -0800101#ifndef MPX_ENABLE_MANAGEMENT
Dave Hansen46a6e0c2015-06-07 11:37:02 -0700102# define MPX_ENABLE_MANAGEMENT() (-EINVAL)
Dave Hansenfe3d1972014-11-14 07:18:29 -0800103#endif
104#ifndef MPX_DISABLE_MANAGEMENT
Dave Hansen46a6e0c2015-06-07 11:37:02 -0700105# define MPX_DISABLE_MANAGEMENT() (-EINVAL)
Dave Hansenfe3d1972014-11-14 07:18:29 -0800106#endif
Paul Burton97915542015-01-08 12:17:37 +0000107#ifndef GET_FP_MODE
108# define GET_FP_MODE(a) (-EINVAL)
109#endif
110#ifndef SET_FP_MODE
111# define SET_FP_MODE(a,b) (-EINVAL)
112#endif
Dave Martin2d2123b2017-10-31 15:51:14 +0000113#ifndef SVE_SET_VL
114# define SVE_SET_VL(a) (-EINVAL)
115#endif
116#ifndef SVE_GET_VL
117# define SVE_GET_VL() (-EINVAL)
118#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/*
121 * this is where the system-wide overflow UID and GID are defined, for
122 * architectures that now have 32-bit UID/GID but didn't in the past
123 */
124
125int overflowuid = DEFAULT_OVERFLOWUID;
126int overflowgid = DEFAULT_OVERFLOWGID;
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128EXPORT_SYMBOL(overflowuid);
129EXPORT_SYMBOL(overflowgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/*
132 * the same as above, but for filesystems which can only store a 16-bit
133 * UID and GID. as such, this is needed on all architectures
134 */
135
136int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
137int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
138
139EXPORT_SYMBOL(fs_overflowuid);
140EXPORT_SYMBOL(fs_overflowgid);
141
142/*
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700143 * Returns true if current's euid is same as p's uid or euid,
144 * or has CAP_SYS_NICE to p's user_ns.
145 *
146 * Called with rcu_read_lock, creds are safe
147 */
148static bool set_one_prio_perm(struct task_struct *p)
149{
150 const struct cred *cred = current_cred(), *pcred = __task_cred(p);
151
Eric W. Biederman5af66202012-03-03 20:21:47 -0800152 if (uid_eq(pcred->uid, cred->euid) ||
153 uid_eq(pcred->euid, cred->euid))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700154 return true;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800155 if (ns_capable(pcred->user_ns, CAP_SYS_NICE))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700156 return true;
157 return false;
158}
159
160/*
David Howellsc69e8d92008-11-14 10:39:19 +1100161 * set the priority of a task
162 * - the caller must hold the RCU read lock
163 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static int set_one_prio(struct task_struct *p, int niceval, int error)
165{
166 int no_nice;
167
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700168 if (!set_one_prio_perm(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 error = -EPERM;
170 goto out;
171 }
Matt Mackalle43379f2005-05-01 08:59:00 -0700172 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 error = -EACCES;
174 goto out;
175 }
176 no_nice = security_task_setnice(p, niceval);
177 if (no_nice) {
178 error = no_nice;
179 goto out;
180 }
181 if (error == -ESRCH)
182 error = 0;
183 set_user_nice(p, niceval);
184out:
185 return error;
186}
187
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100188SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 struct task_struct *g, *p;
191 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100192 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 int error = -EINVAL;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800194 struct pid *pgrp;
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800195 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Daniel Walker3e88c552007-05-10 22:22:53 -0700197 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 goto out;
199
200 /* normalize: avoid signed division (rounding problems) */
201 error = -ESRCH;
Dongsheng Yangc4a4d2f2014-02-11 15:34:51 +0800202 if (niceval < MIN_NICE)
203 niceval = MIN_NICE;
204 if (niceval > MAX_NICE)
205 niceval = MAX_NICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000207 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 read_lock(&tasklist_lock);
209 switch (which) {
vishnu.psec94fc32014-10-09 15:30:23 -0700210 case PRIO_PROCESS:
211 if (who)
212 p = find_task_by_vpid(who);
213 else
214 p = current;
215 if (p)
216 error = set_one_prio(p, niceval, error);
217 break;
218 case PRIO_PGRP:
219 if (who)
220 pgrp = find_vpid(who);
221 else
222 pgrp = task_pgrp(current);
223 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
224 error = set_one_prio(p, niceval, error);
225 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
226 break;
227 case PRIO_USER:
228 uid = make_kuid(cred->user_ns, who);
229 user = cred->user;
230 if (!who)
231 uid = cred->uid;
232 else if (!uid_eq(uid, cred->uid)) {
233 user = find_user(uid);
234 if (!user)
David Howells86a264a2008-11-14 10:39:18 +1100235 goto out_unlock; /* No processes for this user */
vishnu.psec94fc32014-10-09 15:30:23 -0700236 }
237 do_each_thread(g, p) {
Ben Segall8639b462015-11-06 16:32:48 -0800238 if (uid_eq(task_uid(p), uid) && task_pid_vnr(p))
vishnu.psec94fc32014-10-09 15:30:23 -0700239 error = set_one_prio(p, niceval, error);
240 } while_each_thread(g, p);
241 if (!uid_eq(uid, cred->uid))
242 free_uid(user); /* For find_user() */
243 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245out_unlock:
246 read_unlock(&tasklist_lock);
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000247 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248out:
249 return error;
250}
251
252/*
253 * Ugh. To avoid negative return values, "getpriority()" will
254 * not return the normal nice-value, but a negated value that
255 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
256 * to stay compatible.
257 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100258SYSCALL_DEFINE2(getpriority, int, which, int, who)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 struct task_struct *g, *p;
261 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100262 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 long niceval, retval = -ESRCH;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800264 struct pid *pgrp;
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800265 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Daniel Walker3e88c552007-05-10 22:22:53 -0700267 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return -EINVAL;
269
Tetsuo Handa70118832010-02-22 12:44:16 -0800270 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 read_lock(&tasklist_lock);
272 switch (which) {
vishnu.psec94fc32014-10-09 15:30:23 -0700273 case PRIO_PROCESS:
274 if (who)
275 p = find_task_by_vpid(who);
276 else
277 p = current;
278 if (p) {
279 niceval = nice_to_rlimit(task_nice(p));
280 if (niceval > retval)
281 retval = niceval;
282 }
283 break;
284 case PRIO_PGRP:
285 if (who)
286 pgrp = find_vpid(who);
287 else
288 pgrp = task_pgrp(current);
289 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
290 niceval = nice_to_rlimit(task_nice(p));
291 if (niceval > retval)
292 retval = niceval;
293 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
294 break;
295 case PRIO_USER:
296 uid = make_kuid(cred->user_ns, who);
297 user = cred->user;
298 if (!who)
299 uid = cred->uid;
300 else if (!uid_eq(uid, cred->uid)) {
301 user = find_user(uid);
302 if (!user)
303 goto out_unlock; /* No processes for this user */
304 }
305 do_each_thread(g, p) {
Ben Segall8639b462015-11-06 16:32:48 -0800306 if (uid_eq(task_uid(p), uid) && task_pid_vnr(p)) {
Dongsheng Yang7aa2c012014-05-08 18:33:49 +0900307 niceval = nice_to_rlimit(task_nice(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (niceval > retval)
309 retval = niceval;
310 }
vishnu.psec94fc32014-10-09 15:30:23 -0700311 } while_each_thread(g, p);
312 if (!uid_eq(uid, cred->uid))
313 free_uid(user); /* for find_user() */
314 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316out_unlock:
317 read_unlock(&tasklist_lock);
Tetsuo Handa70118832010-02-22 12:44:16 -0800318 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 return retval;
321}
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323/*
324 * Unprivileged users may change the real gid to the effective gid
325 * or vice versa. (BSD-style)
326 *
327 * If you set the real gid at all, or set the effective gid to a value not
328 * equal to the real gid, then the saved gid is set to the new effective gid.
329 *
330 * This makes it possible for a setgid program to completely drop its
331 * privileges, which is often a useful assertion to make when you are doing
332 * a security audit over a program.
333 *
334 * The general idea is that a program which uses just setregid() will be
335 * 100% compatible with BSD. A program which uses just setgid() will be
vishnu.psec94fc32014-10-09 15:30:23 -0700336 * 100% compatible with POSIX with saved IDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 *
338 * SMP: There are not races, the GIDs are checked only by filesystem
339 * operations (as far as semantic preservation is concerned).
340 */
Iulia Manda28138932015-04-15 16:16:41 -0700341#ifdef CONFIG_MULTIUSER
Heiko Carstensae1251a2009-01-14 14:14:05 +0100342SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800344 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100345 const struct cred *old;
346 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800348 kgid_t krgid, kegid;
349
350 krgid = make_kgid(ns, rgid);
351 kegid = make_kgid(ns, egid);
352
353 if ((rgid != (gid_t) -1) && !gid_valid(krgid))
354 return -EINVAL;
355 if ((egid != (gid_t) -1) && !gid_valid(kegid))
356 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
David Howellsd84f4f92008-11-14 10:39:23 +1100358 new = prepare_creds();
359 if (!new)
360 return -ENOMEM;
361 old = current_cred();
362
David Howellsd84f4f92008-11-14 10:39:23 +1100363 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (rgid != (gid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800365 if (gid_eq(old->gid, krgid) ||
366 gid_eq(old->egid, krgid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700367 ns_capable(old->user_ns, CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800368 new->gid = krgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 else
David Howellsd84f4f92008-11-14 10:39:23 +1100370 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372 if (egid != (gid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800373 if (gid_eq(old->gid, kegid) ||
374 gid_eq(old->egid, kegid) ||
375 gid_eq(old->sgid, kegid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700376 ns_capable(old->user_ns, CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800377 new->egid = kegid;
Cal Peake756184b2006-09-30 23:27:24 -0700378 else
David Howellsd84f4f92008-11-14 10:39:23 +1100379 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
David Howellsd84f4f92008-11-14 10:39:23 +1100381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (rgid != (gid_t) -1 ||
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800383 (egid != (gid_t) -1 && !gid_eq(kegid, old->gid)))
David Howellsd84f4f92008-11-14 10:39:23 +1100384 new->sgid = new->egid;
385 new->fsgid = new->egid;
386
387 return commit_creds(new);
388
389error:
390 abort_creds(new);
391 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394/*
vishnu.psec94fc32014-10-09 15:30:23 -0700395 * setgid() is implemented like SysV w/ SAVED_IDS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 *
397 * SMP: Same implicit races as above.
398 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100399SYSCALL_DEFINE1(setgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800401 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100402 const struct cred *old;
403 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800405 kgid_t kgid;
406
407 kgid = make_kgid(ns, gid);
408 if (!gid_valid(kgid))
409 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
David Howellsd84f4f92008-11-14 10:39:23 +1100411 new = prepare_creds();
412 if (!new)
413 return -ENOMEM;
414 old = current_cred();
415
David Howellsd84f4f92008-11-14 10:39:23 +1100416 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700417 if (ns_capable(old->user_ns, CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800418 new->gid = new->egid = new->sgid = new->fsgid = kgid;
419 else if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->sgid))
420 new->egid = new->fsgid = kgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 else
David Howellsd84f4f92008-11-14 10:39:23 +1100422 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
David Howellsd84f4f92008-11-14 10:39:23 +1100424 return commit_creds(new);
425
426error:
427 abort_creds(new);
428 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
Dhaval Giani54e99122009-02-27 15:13:54 +0530430
David Howellsd84f4f92008-11-14 10:39:23 +1100431/*
432 * change the user struct in a credentials set to match the new UID
433 */
434static int set_user(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 struct user_struct *new_user;
437
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800438 new_user = alloc_uid(new->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (!new_user)
440 return -EAGAIN;
441
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400442 /*
443 * We don't fail in case of NPROC limit excess here because too many
444 * poorly written programs don't check set*uid() return code, assuming
445 * it never fails if called by root. We may still enforce NPROC limit
446 * for programs doing set*uid()+execve() by harmlessly deferring the
447 * failure to the execve() stage.
448 */
Jiri Slaby78d7d402010-03-05 13:42:54 -0800449 if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400450 new_user != INIT_USER)
451 current->flags |= PF_NPROC_EXCEEDED;
452 else
453 current->flags &= ~PF_NPROC_EXCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
David Howellsd84f4f92008-11-14 10:39:23 +1100455 free_uid(new->user);
456 new->user = new_user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
458}
459
460/*
461 * Unprivileged users may change the real uid to the effective uid
462 * or vice versa. (BSD-style)
463 *
464 * If you set the real uid at all, or set the effective uid to a value not
465 * equal to the real uid, then the saved uid is set to the new effective uid.
466 *
467 * This makes it possible for a setuid program to completely drop its
468 * privileges, which is often a useful assertion to make when you are doing
469 * a security audit over a program.
470 *
471 * The general idea is that a program which uses just setreuid() will be
472 * 100% compatible with BSD. A program which uses just setuid() will be
vishnu.psec94fc32014-10-09 15:30:23 -0700473 * 100% compatible with POSIX with saved IDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100475SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800477 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100478 const struct cred *old;
479 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800481 kuid_t kruid, keuid;
482
483 kruid = make_kuid(ns, ruid);
484 keuid = make_kuid(ns, euid);
485
486 if ((ruid != (uid_t) -1) && !uid_valid(kruid))
487 return -EINVAL;
488 if ((euid != (uid_t) -1) && !uid_valid(keuid))
489 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
David Howellsd84f4f92008-11-14 10:39:23 +1100491 new = prepare_creds();
492 if (!new)
493 return -ENOMEM;
494 old = current_cred();
495
David Howellsd84f4f92008-11-14 10:39:23 +1100496 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (ruid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800498 new->uid = kruid;
499 if (!uid_eq(old->uid, kruid) &&
500 !uid_eq(old->euid, kruid) &&
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700501 !ns_capable(old->user_ns, CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100502 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504
505 if (euid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800506 new->euid = keuid;
507 if (!uid_eq(old->uid, keuid) &&
508 !uid_eq(old->euid, keuid) &&
509 !uid_eq(old->suid, keuid) &&
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700510 !ns_capable(old->user_ns, CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100511 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800514 if (!uid_eq(new->uid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530515 retval = set_user(new);
516 if (retval < 0)
517 goto error;
518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (ruid != (uid_t) -1 ||
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800520 (euid != (uid_t) -1 && !uid_eq(keuid, old->uid)))
David Howellsd84f4f92008-11-14 10:39:23 +1100521 new->suid = new->euid;
522 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
David Howellsd84f4f92008-11-14 10:39:23 +1100524 retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
525 if (retval < 0)
526 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
David Howellsd84f4f92008-11-14 10:39:23 +1100528 return commit_creds(new);
529
530error:
531 abort_creds(new);
532 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
vishnu.psec94fc32014-10-09 15:30:23 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535/*
vishnu.psec94fc32014-10-09 15:30:23 -0700536 * setuid() is implemented like SysV with SAVED_IDS
537 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * Note that SAVED_ID's is deficient in that a setuid root program
vishnu.psec94fc32014-10-09 15:30:23 -0700539 * like sendmail, for example, cannot set its uid to be a normal
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 * user and then switch back, because if you're root, setuid() sets
541 * the saved uid too. If you don't like this, blame the bright people
542 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
543 * will allow a root program to temporarily drop privileges and be able to
vishnu.psec94fc32014-10-09 15:30:23 -0700544 * regain them by swapping the real and effective uid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100546SYSCALL_DEFINE1(setuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800548 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100549 const struct cred *old;
550 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800552 kuid_t kuid;
553
554 kuid = make_kuid(ns, uid);
555 if (!uid_valid(kuid))
556 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
David Howellsd84f4f92008-11-14 10:39:23 +1100558 new = prepare_creds();
559 if (!new)
560 return -ENOMEM;
561 old = current_cred();
562
David Howellsd84f4f92008-11-14 10:39:23 +1100563 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700564 if (ns_capable(old->user_ns, CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800565 new->suid = new->uid = kuid;
566 if (!uid_eq(kuid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530567 retval = set_user(new);
568 if (retval < 0)
569 goto error;
David Howellsd84f4f92008-11-14 10:39:23 +1100570 }
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800571 } else if (!uid_eq(kuid, old->uid) && !uid_eq(kuid, new->suid)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100572 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800575 new->fsuid = new->euid = kuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
David Howellsd84f4f92008-11-14 10:39:23 +1100577 retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
578 if (retval < 0)
579 goto error;
580
581 return commit_creds(new);
582
583error:
584 abort_creds(new);
585 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
588
589/*
590 * This function implements a generic ability to update ruid, euid,
591 * and suid. This allows you to implement the 4.4 compatible seteuid().
592 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100593SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800595 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100596 const struct cred *old;
597 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800599 kuid_t kruid, keuid, ksuid;
600
601 kruid = make_kuid(ns, ruid);
602 keuid = make_kuid(ns, euid);
603 ksuid = make_kuid(ns, suid);
604
605 if ((ruid != (uid_t) -1) && !uid_valid(kruid))
606 return -EINVAL;
607
608 if ((euid != (uid_t) -1) && !uid_valid(keuid))
609 return -EINVAL;
610
611 if ((suid != (uid_t) -1) && !uid_valid(ksuid))
612 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
David Howellsd84f4f92008-11-14 10:39:23 +1100614 new = prepare_creds();
615 if (!new)
616 return -ENOMEM;
617
David Howellsd84f4f92008-11-14 10:39:23 +1100618 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
David Howellsd84f4f92008-11-14 10:39:23 +1100620 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700621 if (!ns_capable(old->user_ns, CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800622 if (ruid != (uid_t) -1 && !uid_eq(kruid, old->uid) &&
623 !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100624 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800625 if (euid != (uid_t) -1 && !uid_eq(keuid, old->uid) &&
626 !uid_eq(keuid, old->euid) && !uid_eq(keuid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100627 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800628 if (suid != (uid_t) -1 && !uid_eq(ksuid, old->uid) &&
629 !uid_eq(ksuid, old->euid) && !uid_eq(ksuid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100630 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
David Howellsd84f4f92008-11-14 10:39:23 +1100632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (ruid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800634 new->uid = kruid;
635 if (!uid_eq(kruid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530636 retval = set_user(new);
637 if (retval < 0)
638 goto error;
639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
David Howellsd84f4f92008-11-14 10:39:23 +1100641 if (euid != (uid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800642 new->euid = keuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (suid != (uid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800644 new->suid = ksuid;
David Howellsd84f4f92008-11-14 10:39:23 +1100645 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
David Howellsd84f4f92008-11-14 10:39:23 +1100647 retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
648 if (retval < 0)
649 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
David Howellsd84f4f92008-11-14 10:39:23 +1100651 return commit_creds(new);
652
653error:
654 abort_creds(new);
655 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800658SYSCALL_DEFINE3(getresuid, uid_t __user *, ruidp, uid_t __user *, euidp, uid_t __user *, suidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
David Howells86a264a2008-11-14 10:39:18 +1100660 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800662 uid_t ruid, euid, suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800664 ruid = from_kuid_munged(cred->user_ns, cred->uid);
665 euid = from_kuid_munged(cred->user_ns, cred->euid);
666 suid = from_kuid_munged(cred->user_ns, cred->suid);
667
vishnu.psec94fc32014-10-09 15:30:23 -0700668 retval = put_user(ruid, ruidp);
669 if (!retval) {
670 retval = put_user(euid, euidp);
671 if (!retval)
672 return put_user(suid, suidp);
673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return retval;
675}
676
677/*
678 * Same as above, but for rgid, egid, sgid.
679 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100680SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800682 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100683 const struct cred *old;
684 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800686 kgid_t krgid, kegid, ksgid;
687
688 krgid = make_kgid(ns, rgid);
689 kegid = make_kgid(ns, egid);
690 ksgid = make_kgid(ns, sgid);
691
692 if ((rgid != (gid_t) -1) && !gid_valid(krgid))
693 return -EINVAL;
694 if ((egid != (gid_t) -1) && !gid_valid(kegid))
695 return -EINVAL;
696 if ((sgid != (gid_t) -1) && !gid_valid(ksgid))
697 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
David Howellsd84f4f92008-11-14 10:39:23 +1100699 new = prepare_creds();
700 if (!new)
701 return -ENOMEM;
702 old = current_cred();
703
David Howellsd84f4f92008-11-14 10:39:23 +1100704 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700705 if (!ns_capable(old->user_ns, CAP_SETGID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800706 if (rgid != (gid_t) -1 && !gid_eq(krgid, old->gid) &&
707 !gid_eq(krgid, old->egid) && !gid_eq(krgid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100708 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800709 if (egid != (gid_t) -1 && !gid_eq(kegid, old->gid) &&
710 !gid_eq(kegid, old->egid) && !gid_eq(kegid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100711 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800712 if (sgid != (gid_t) -1 && !gid_eq(ksgid, old->gid) &&
713 !gid_eq(ksgid, old->egid) && !gid_eq(ksgid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100714 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
David Howellsd84f4f92008-11-14 10:39:23 +1100717 if (rgid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800718 new->gid = krgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100719 if (egid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800720 new->egid = kegid;
David Howellsd84f4f92008-11-14 10:39:23 +1100721 if (sgid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800722 new->sgid = ksgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100723 new->fsgid = new->egid;
724
725 return commit_creds(new);
726
727error:
728 abort_creds(new);
729 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800732SYSCALL_DEFINE3(getresgid, gid_t __user *, rgidp, gid_t __user *, egidp, gid_t __user *, sgidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
David Howells86a264a2008-11-14 10:39:18 +1100734 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800736 gid_t rgid, egid, sgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800738 rgid = from_kgid_munged(cred->user_ns, cred->gid);
739 egid = from_kgid_munged(cred->user_ns, cred->egid);
740 sgid = from_kgid_munged(cred->user_ns, cred->sgid);
741
vishnu.psec94fc32014-10-09 15:30:23 -0700742 retval = put_user(rgid, rgidp);
743 if (!retval) {
744 retval = put_user(egid, egidp);
745 if (!retval)
746 retval = put_user(sgid, sgidp);
747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 return retval;
750}
751
752
753/*
754 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
755 * is used for "access()" and for the NFS daemon (letting nfsd stay at
756 * whatever uid it wants to). It normally shadows "euid", except when
757 * explicitly set by setfsuid() or for access..
758 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100759SYSCALL_DEFINE1(setfsuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
David Howellsd84f4f92008-11-14 10:39:23 +1100761 const struct cred *old;
762 struct cred *new;
763 uid_t old_fsuid;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800764 kuid_t kuid;
765
766 old = current_cred();
767 old_fsuid = from_kuid_munged(old->user_ns, old->fsuid);
768
769 kuid = make_kuid(old->user_ns, uid);
770 if (!uid_valid(kuid))
771 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
David Howellsd84f4f92008-11-14 10:39:23 +1100773 new = prepare_creds();
774 if (!new)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800775 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800777 if (uid_eq(kuid, old->uid) || uid_eq(kuid, old->euid) ||
778 uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700779 ns_capable(old->user_ns, CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800780 if (!uid_eq(kuid, old->fsuid)) {
781 new->fsuid = kuid;
David Howellsd84f4f92008-11-14 10:39:23 +1100782 if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
783 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786
David Howellsd84f4f92008-11-14 10:39:23 +1100787 abort_creds(new);
788 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
David Howellsd84f4f92008-11-14 10:39:23 +1100790change_okay:
791 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return old_fsuid;
793}
794
795/*
John Anthony Kazos Jrf42df9e2007-05-09 08:23:08 +0200796 * Samma på svenska..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100798SYSCALL_DEFINE1(setfsgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
David Howellsd84f4f92008-11-14 10:39:23 +1100800 const struct cred *old;
801 struct cred *new;
802 gid_t old_fsgid;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800803 kgid_t kgid;
804
805 old = current_cred();
806 old_fsgid = from_kgid_munged(old->user_ns, old->fsgid);
807
808 kgid = make_kgid(old->user_ns, gid);
809 if (!gid_valid(kgid))
810 return old_fsgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
David Howellsd84f4f92008-11-14 10:39:23 +1100812 new = prepare_creds();
813 if (!new)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800814 return old_fsgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100815
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800816 if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->egid) ||
817 gid_eq(kgid, old->sgid) || gid_eq(kgid, old->fsgid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700818 ns_capable(old->user_ns, CAP_SETGID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800819 if (!gid_eq(kgid, old->fsgid)) {
820 new->fsgid = kgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100821 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
David Howellsd84f4f92008-11-14 10:39:23 +1100824
David Howellsd84f4f92008-11-14 10:39:23 +1100825 abort_creds(new);
826 return old_fsgid;
827
828change_okay:
829 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return old_fsgid;
831}
Iulia Manda28138932015-04-15 16:16:41 -0700832#endif /* CONFIG_MULTIUSER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Stephen Rothwell4a22f162013-04-30 15:27:37 -0700834/**
835 * sys_getpid - return the thread group id of the current process
836 *
837 * Note, despite the name, this returns the tgid not the pid. The tgid and
838 * the pid are identical unless CLONE_THREAD was specified on clone() in
839 * which case the tgid is the same in all threads of the same group.
840 *
841 * This is SMP safe as current->tgid does not change.
842 */
843SYSCALL_DEFINE0(getpid)
844{
845 return task_tgid_vnr(current);
846}
847
848/* Thread ID - the internal kernel "pid" */
849SYSCALL_DEFINE0(gettid)
850{
851 return task_pid_vnr(current);
852}
853
854/*
855 * Accessing ->real_parent is not SMP-safe, it could
856 * change from under us. However, we can use a stale
857 * value of ->real_parent under rcu_read_lock(), see
858 * release_task()->call_rcu(delayed_put_task_struct).
859 */
860SYSCALL_DEFINE0(getppid)
861{
862 int pid;
863
864 rcu_read_lock();
865 pid = task_tgid_vnr(rcu_dereference(current->real_parent));
866 rcu_read_unlock();
867
868 return pid;
869}
870
871SYSCALL_DEFINE0(getuid)
872{
873 /* Only we change this so SMP safe */
874 return from_kuid_munged(current_user_ns(), current_uid());
875}
876
877SYSCALL_DEFINE0(geteuid)
878{
879 /* Only we change this so SMP safe */
880 return from_kuid_munged(current_user_ns(), current_euid());
881}
882
883SYSCALL_DEFINE0(getgid)
884{
885 /* Only we change this so SMP safe */
886 return from_kgid_munged(current_user_ns(), current_gid());
887}
888
889SYSCALL_DEFINE0(getegid)
890{
891 /* Only we change this so SMP safe */
892 return from_kgid_munged(current_user_ns(), current_egid());
893}
894
Al Viroca2406e2017-05-31 04:22:44 -0400895static void do_sys_times(struct tms *tms)
Frank Mayharf06febc2008-09-12 09:54:39 -0700896{
Frederic Weisbecker5613fda2017-01-31 04:09:23 +0100897 u64 tgutime, tgstime, cutime, cstime;
Frank Mayharf06febc2008-09-12 09:54:39 -0700898
Frederic Weisbeckere80d0a1a2012-11-21 16:26:44 +0100899 thread_group_cputime_adjusted(current, &tgutime, &tgstime);
Frank Mayharf06febc2008-09-12 09:54:39 -0700900 cutime = current->signal->cutime;
901 cstime = current->signal->cstime;
Frederic Weisbecker5613fda2017-01-31 04:09:23 +0100902 tms->tms_utime = nsec_to_clock_t(tgutime);
903 tms->tms_stime = nsec_to_clock_t(tgstime);
904 tms->tms_cutime = nsec_to_clock_t(cutime);
905 tms->tms_cstime = nsec_to_clock_t(cstime);
Frank Mayharf06febc2008-09-12 09:54:39 -0700906}
907
Heiko Carstens58fd3aa2009-01-14 14:14:03 +0100908SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (tbuf) {
911 struct tms tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Frank Mayharf06febc2008-09-12 09:54:39 -0700913 do_sys_times(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
915 return -EFAULT;
916 }
Paul Mackerrase3d5a272009-01-06 14:41:02 -0800917 force_successful_syscall_return();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return (long) jiffies_64_to_clock_t(get_jiffies_64());
919}
920
Al Viroca2406e2017-05-31 04:22:44 -0400921#ifdef CONFIG_COMPAT
922static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
923{
924 return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
925}
926
927COMPAT_SYSCALL_DEFINE1(times, struct compat_tms __user *, tbuf)
928{
929 if (tbuf) {
930 struct tms tms;
931 struct compat_tms tmp;
932
933 do_sys_times(&tms);
934 /* Convert our struct tms to the compat version. */
935 tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
936 tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
937 tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
938 tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
939 if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
940 return -EFAULT;
941 }
942 force_successful_syscall_return();
943 return compat_jiffies_to_clock_t(jiffies);
944}
945#endif
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947/*
948 * This needs some heavy checking ...
949 * I just haven't the stomach for it. I also don't fully
950 * understand sessions/pgrp etc. Let somebody who does explain it.
951 *
952 * OK, I think I have the protection semantics right.... this is really
953 * only important on a multi-user system anyway, to make sure one user
954 * can't send a signal to a process owned by another. -TYT, 12/12/91
955 *
Oleg Nesterov98611e42014-01-23 15:55:52 -0800956 * !PF_FORKNOEXEC check to conform completely to POSIX.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +0100958SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 struct task_struct *p;
Oleg Nesterovee0acf92006-01-08 01:03:53 -0800961 struct task_struct *group_leader = current->group_leader;
Oleg Nesterov4e021302008-02-08 04:19:08 -0800962 struct pid *pgrp;
963 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 if (!pid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700966 pid = task_pid_vnr(group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (!pgid)
968 pgid = pid;
969 if (pgid < 0)
970 return -EINVAL;
Paul E. McKenney950eaac2010-08-31 17:00:18 -0700971 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 /* From this point forward we keep holding onto the tasklist lock
974 * so that our parent does not change from under us. -DaveM
975 */
976 write_lock_irq(&tasklist_lock);
977
978 err = -ESRCH;
Oleg Nesterov4e021302008-02-08 04:19:08 -0800979 p = find_task_by_vpid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (!p)
981 goto out;
982
983 err = -EINVAL;
984 if (!thread_group_leader(p))
985 goto out;
986
Oleg Nesterov4e021302008-02-08 04:19:08 -0800987 if (same_thread_group(p->real_parent, group_leader)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 err = -EPERM;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800989 if (task_session(p) != task_session(group_leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 goto out;
991 err = -EACCES;
Oleg Nesterov98611e42014-01-23 15:55:52 -0800992 if (!(p->flags & PF_FORKNOEXEC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 goto out;
994 } else {
995 err = -ESRCH;
Oleg Nesterovee0acf92006-01-08 01:03:53 -0800996 if (p != group_leader)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 goto out;
998 }
999
1000 err = -EPERM;
1001 if (p->signal->leader)
1002 goto out;
1003
Oleg Nesterov4e021302008-02-08 04:19:08 -08001004 pgrp = task_pid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (pgid != pid) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001006 struct task_struct *g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Oleg Nesterov4e021302008-02-08 04:19:08 -08001008 pgrp = find_vpid(pgid);
1009 g = pid_task(pgrp, PIDTYPE_PGID);
Eric W. Biederman41487c62007-02-12 00:53:01 -08001010 if (!g || task_session(g) != task_session(group_leader))
Oleg Nesterovf020bc42006-12-08 02:38:02 -08001011 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 err = security_task_setpgid(p, pgid);
1015 if (err)
1016 goto out;
1017
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07001018 if (task_pgrp(p) != pgrp)
Oleg Nesterov83beaf32008-04-30 00:54:27 -07001019 change_pid(p, PIDTYPE_PGID, pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 err = 0;
1022out:
1023 /* All paths lead to here, thus we are safe. -DaveM */
1024 write_unlock_irq(&tasklist_lock);
Paul E. McKenney950eaac2010-08-31 17:00:18 -07001025 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return err;
1027}
1028
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001029SYSCALL_DEFINE1(getpgid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001031 struct task_struct *p;
1032 struct pid *grp;
1033 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001035 rcu_read_lock();
1036 if (!pid)
1037 grp = task_pgrp(current);
1038 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 retval = -ESRCH;
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001040 p = find_task_by_vpid(pid);
1041 if (!p)
1042 goto out;
1043 grp = task_pgrp(p);
1044 if (!grp)
1045 goto out;
1046
1047 retval = security_task_getpgid(p);
1048 if (retval)
1049 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001051 retval = pid_vnr(grp);
1052out:
1053 rcu_read_unlock();
1054 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
1057#ifdef __ARCH_WANT_SYS_GETPGRP
1058
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001059SYSCALL_DEFINE0(getpgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001061 return sys_getpgid(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
1064#endif
1065
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001066SYSCALL_DEFINE1(getsid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001068 struct task_struct *p;
1069 struct pid *sid;
1070 int retval;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001071
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001072 rcu_read_lock();
1073 if (!pid)
1074 sid = task_session(current);
1075 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 retval = -ESRCH;
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001077 p = find_task_by_vpid(pid);
1078 if (!p)
1079 goto out;
1080 sid = task_session(p);
1081 if (!sid)
1082 goto out;
1083
1084 retval = security_task_getsid(p);
1085 if (retval)
1086 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001088 retval = pid_vnr(sid);
1089out:
1090 rcu_read_unlock();
1091 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
Oleg Nesterov81dabb42013-07-03 15:08:26 -07001094static void set_special_pids(struct pid *pid)
1095{
1096 struct task_struct *curr = current->group_leader;
1097
1098 if (task_session(curr) != pid)
1099 change_pid(curr, PIDTYPE_SID, pid);
1100
1101 if (task_pgrp(curr) != pid)
1102 change_pid(curr, PIDTYPE_PGID, pid);
1103}
1104
Heiko Carstensb290ebe2009-01-14 14:14:06 +01001105SYSCALL_DEFINE0(setsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Oren Laadane19f2472006-01-08 01:03:58 -08001107 struct task_struct *group_leader = current->group_leader;
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001108 struct pid *sid = task_pid(group_leader);
1109 pid_t session = pid_vnr(sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 int err = -EPERM;
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 write_lock_irq(&tasklist_lock);
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001113 /* Fail if I am already a session leader */
1114 if (group_leader->signal->leader)
1115 goto out;
1116
Oleg Nesterov430c6232008-02-08 04:19:11 -08001117 /* Fail if a process group id already exists that equals the
1118 * proposed session id.
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001119 */
Oleg Nesterov6806aac2008-02-08 04:19:12 -08001120 if (pid_task(sid, PIDTYPE_PGID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 goto out;
1122
Oren Laadane19f2472006-01-08 01:03:58 -08001123 group_leader->signal->leader = 1;
Oleg Nesterov81dabb42013-07-03 15:08:26 -07001124 set_special_pids(sid);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001125
Alan Cox9c9f4de2008-10-13 10:37:26 +01001126 proc_clear_tty(group_leader);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001127
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001128 err = session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129out:
1130 write_unlock_irq(&tasklist_lock);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001131 if (err > 0) {
Christian Borntraeger0d0df592009-10-26 16:49:34 -07001132 proc_sid_connector(group_leader);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001133 sched_autogroup_create_attach(group_leader);
1134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return err;
1136}
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138DECLARE_RWSEM(uts_sem);
1139
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001140#ifdef COMPAT_UTS_MACHINE
1141#define override_architecture(name) \
Andreas Schwab46da2762010-04-23 13:17:44 -04001142 (personality(current->personality) == PER_LINUX32 && \
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001143 copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1144 sizeof(COMPAT_UTS_MACHINE)))
1145#else
1146#define override_architecture(name) 0
1147#endif
1148
Andi Kleenbe274252011-08-19 16:15:10 -07001149/*
1150 * Work around broken programs that cannot handle "Linux 3.0".
1151 * 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 -08001152 * 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 -07001153 */
Kees Cook2702b152012-10-19 13:56:51 -07001154static int override_release(char __user *release, size_t len)
Andi Kleenbe274252011-08-19 16:15:10 -07001155{
1156 int ret = 0;
Andi Kleenbe274252011-08-19 16:15:10 -07001157
1158 if (current->personality & UNAME26) {
Kees Cook2702b152012-10-19 13:56:51 -07001159 const char *rest = UTS_RELEASE;
1160 char buf[65] = { 0 };
Andi Kleenbe274252011-08-19 16:15:10 -07001161 int ndots = 0;
1162 unsigned v;
Kees Cook2702b152012-10-19 13:56:51 -07001163 size_t copy;
Andi Kleenbe274252011-08-19 16:15:10 -07001164
1165 while (*rest) {
1166 if (*rest == '.' && ++ndots >= 3)
1167 break;
1168 if (!isdigit(*rest) && *rest != '.')
1169 break;
1170 rest++;
1171 }
Jon DeVree39afb5e2015-02-27 15:52:07 -08001172 v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 60;
Kees Cook31fd84b92012-10-19 18:45:53 -07001173 copy = clamp_t(size_t, len, 1, sizeof(buf));
Kees Cook2702b152012-10-19 13:56:51 -07001174 copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
1175 ret = copy_to_user(release, buf, copy + 1);
Andi Kleenbe274252011-08-19 16:15:10 -07001176 }
1177 return ret;
1178}
1179
Heiko Carstense48fbb62009-01-14 14:14:26 +01001180SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 int errno = 0;
1183
1184 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001185 if (copy_to_user(name, utsname(), sizeof *name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 errno = -EFAULT;
1187 up_read(&uts_sem);
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001188
Andi Kleenbe274252011-08-19 16:15:10 -07001189 if (!errno && override_release(name->release, sizeof(name->release)))
1190 errno = -EFAULT;
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001191 if (!errno && override_architecture(name))
1192 errno = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return errno;
1194}
1195
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001196#ifdef __ARCH_WANT_SYS_OLD_UNAME
1197/*
1198 * Old cruft
1199 */
1200SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
1201{
1202 int error = 0;
1203
1204 if (!name)
1205 return -EFAULT;
1206
1207 down_read(&uts_sem);
1208 if (copy_to_user(name, utsname(), sizeof(*name)))
1209 error = -EFAULT;
1210 up_read(&uts_sem);
1211
Andi Kleenbe274252011-08-19 16:15:10 -07001212 if (!error && override_release(name->release, sizeof(name->release)))
1213 error = -EFAULT;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001214 if (!error && override_architecture(name))
1215 error = -EFAULT;
1216 return error;
1217}
1218
1219SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
1220{
1221 int error;
1222
1223 if (!name)
1224 return -EFAULT;
1225 if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
1226 return -EFAULT;
1227
1228 down_read(&uts_sem);
1229 error = __copy_to_user(&name->sysname, &utsname()->sysname,
1230 __OLD_UTS_LEN);
1231 error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
1232 error |= __copy_to_user(&name->nodename, &utsname()->nodename,
1233 __OLD_UTS_LEN);
1234 error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
1235 error |= __copy_to_user(&name->release, &utsname()->release,
1236 __OLD_UTS_LEN);
1237 error |= __put_user(0, name->release + __OLD_UTS_LEN);
1238 error |= __copy_to_user(&name->version, &utsname()->version,
1239 __OLD_UTS_LEN);
1240 error |= __put_user(0, name->version + __OLD_UTS_LEN);
1241 error |= __copy_to_user(&name->machine, &utsname()->machine,
1242 __OLD_UTS_LEN);
1243 error |= __put_user(0, name->machine + __OLD_UTS_LEN);
1244 up_read(&uts_sem);
1245
1246 if (!error && override_architecture(name))
1247 error = -EFAULT;
Andi Kleenbe274252011-08-19 16:15:10 -07001248 if (!error && override_release(name->release, sizeof(name->release)))
1249 error = -EFAULT;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001250 return error ? -EFAULT : 0;
1251}
1252#endif
1253
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001254SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 int errno;
1257 char tmp[__NEW_UTS_LEN];
1258
Serge E. Hallynbb96a6f52011-03-23 16:43:18 -07001259 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (len < 0 || len > __NEW_UTS_LEN)
1263 return -EINVAL;
1264 down_write(&uts_sem);
1265 errno = -EFAULT;
1266 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001267 struct new_utsname *u = utsname();
1268
1269 memcpy(u->nodename, tmp, len);
1270 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 errno = 0;
Sasikantha babu499eea62012-05-31 16:26:07 -07001272 uts_proc_notify(UTS_PROC_HOSTNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274 up_write(&uts_sem);
1275 return errno;
1276}
1277
1278#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1279
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001280SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 int i, errno;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001283 struct new_utsname *u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 if (len < 0)
1286 return -EINVAL;
1287 down_read(&uts_sem);
Andrew Morton9679e4d2008-10-15 22:01:51 -07001288 u = utsname();
1289 i = 1 + strlen(u->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (i > len)
1291 i = len;
1292 errno = 0;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001293 if (copy_to_user(name, u->nodename, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 errno = -EFAULT;
1295 up_read(&uts_sem);
1296 return errno;
1297}
1298
1299#endif
1300
1301/*
1302 * Only setdomainname; getdomainname can be implemented by calling
1303 * uname()
1304 */
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001305SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
1307 int errno;
1308 char tmp[__NEW_UTS_LEN];
1309
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001310 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return -EPERM;
1312 if (len < 0 || len > __NEW_UTS_LEN)
1313 return -EINVAL;
1314
1315 down_write(&uts_sem);
1316 errno = -EFAULT;
1317 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001318 struct new_utsname *u = utsname();
1319
1320 memcpy(u->domainname, tmp, len);
1321 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 errno = 0;
Sasikantha babu499eea62012-05-31 16:26:07 -07001323 uts_proc_notify(UTS_PROC_DOMAINNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325 up_write(&uts_sem);
1326 return errno;
1327}
1328
Heiko Carstense48fbb62009-01-14 14:14:26 +01001329SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Jiri Slabyb9518342010-05-04 11:28:25 +02001331 struct rlimit value;
1332 int ret;
1333
1334 ret = do_prlimit(current, resource, NULL, &value);
1335 if (!ret)
1336 ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1337
1338 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Al Virod9e968c2017-05-31 04:33:51 -04001341#ifdef CONFIG_COMPAT
1342
1343COMPAT_SYSCALL_DEFINE2(setrlimit, unsigned int, resource,
1344 struct compat_rlimit __user *, rlim)
1345{
1346 struct rlimit r;
1347 struct compat_rlimit r32;
1348
1349 if (copy_from_user(&r32, rlim, sizeof(struct compat_rlimit)))
1350 return -EFAULT;
1351
1352 if (r32.rlim_cur == COMPAT_RLIM_INFINITY)
1353 r.rlim_cur = RLIM_INFINITY;
1354 else
1355 r.rlim_cur = r32.rlim_cur;
1356 if (r32.rlim_max == COMPAT_RLIM_INFINITY)
1357 r.rlim_max = RLIM_INFINITY;
1358 else
1359 r.rlim_max = r32.rlim_max;
1360 return do_prlimit(current, resource, &r, NULL);
1361}
1362
1363COMPAT_SYSCALL_DEFINE2(getrlimit, unsigned int, resource,
1364 struct compat_rlimit __user *, rlim)
1365{
1366 struct rlimit r;
1367 int ret;
1368
1369 ret = do_prlimit(current, resource, NULL, &r);
1370 if (!ret) {
Al Viro58c7ffc2017-07-12 04:59:45 +01001371 struct compat_rlimit r32;
Al Virod9e968c2017-05-31 04:33:51 -04001372 if (r.rlim_cur > COMPAT_RLIM_INFINITY)
1373 r32.rlim_cur = COMPAT_RLIM_INFINITY;
1374 else
1375 r32.rlim_cur = r.rlim_cur;
1376 if (r.rlim_max > COMPAT_RLIM_INFINITY)
1377 r32.rlim_max = COMPAT_RLIM_INFINITY;
1378 else
1379 r32.rlim_max = r.rlim_max;
1380
1381 if (copy_to_user(rlim, &r32, sizeof(struct compat_rlimit)))
1382 return -EFAULT;
1383 }
1384 return ret;
1385}
1386
1387#endif
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1390
1391/*
1392 * Back compatibility for getrlimit. Needed for some apps.
1393 */
Heiko Carstense48fbb62009-01-14 14:14:26 +01001394SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1395 struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
1397 struct rlimit x;
1398 if (resource >= RLIM_NLIMITS)
1399 return -EINVAL;
1400
1401 task_lock(current->group_leader);
1402 x = current->signal->rlim[resource];
1403 task_unlock(current->group_leader);
Cal Peake756184b2006-09-30 23:27:24 -07001404 if (x.rlim_cur > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 x.rlim_cur = 0x7FFFFFFF;
Cal Peake756184b2006-09-30 23:27:24 -07001406 if (x.rlim_max > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 x.rlim_max = 0x7FFFFFFF;
vishnu.psec94fc32014-10-09 15:30:23 -07001408 return copy_to_user(rlim, &x, sizeof(x)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
Al Viro613763a2017-05-26 22:04:29 -04001411#ifdef CONFIG_COMPAT
1412COMPAT_SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1413 struct compat_rlimit __user *, rlim)
1414{
1415 struct rlimit r;
1416
1417 if (resource >= RLIM_NLIMITS)
1418 return -EINVAL;
1419
1420 task_lock(current->group_leader);
1421 r = current->signal->rlim[resource];
1422 task_unlock(current->group_leader);
1423 if (r.rlim_cur > 0x7FFFFFFF)
1424 r.rlim_cur = 0x7FFFFFFF;
1425 if (r.rlim_max > 0x7FFFFFFF)
1426 r.rlim_max = 0x7FFFFFFF;
1427
1428 if (put_user(r.rlim_cur, &rlim->rlim_cur) ||
1429 put_user(r.rlim_max, &rlim->rlim_max))
1430 return -EFAULT;
1431 return 0;
1432}
1433#endif
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435#endif
1436
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001437static inline bool rlim64_is_infinity(__u64 rlim64)
1438{
1439#if BITS_PER_LONG < 64
1440 return rlim64 >= ULONG_MAX;
1441#else
1442 return rlim64 == RLIM64_INFINITY;
1443#endif
1444}
1445
1446static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1447{
1448 if (rlim->rlim_cur == RLIM_INFINITY)
1449 rlim64->rlim_cur = RLIM64_INFINITY;
1450 else
1451 rlim64->rlim_cur = rlim->rlim_cur;
1452 if (rlim->rlim_max == RLIM_INFINITY)
1453 rlim64->rlim_max = RLIM64_INFINITY;
1454 else
1455 rlim64->rlim_max = rlim->rlim_max;
1456}
1457
1458static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1459{
1460 if (rlim64_is_infinity(rlim64->rlim_cur))
1461 rlim->rlim_cur = RLIM_INFINITY;
1462 else
1463 rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1464 if (rlim64_is_infinity(rlim64->rlim_max))
1465 rlim->rlim_max = RLIM_INFINITY;
1466 else
1467 rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1468}
1469
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001470/* make sure you are allowed to change @tsk limits before calling this */
Jiri Slaby5b415352010-03-24 16:11:29 +01001471int do_prlimit(struct task_struct *tsk, unsigned int resource,
1472 struct rlimit *new_rlim, struct rlimit *old_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Jiri Slaby5b415352010-03-24 16:11:29 +01001474 struct rlimit *rlim;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001475 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 if (resource >= RLIM_NLIMITS)
1478 return -EINVAL;
Jiri Slaby5b415352010-03-24 16:11:29 +01001479 if (new_rlim) {
1480 if (new_rlim->rlim_cur > new_rlim->rlim_max)
1481 return -EINVAL;
1482 if (resource == RLIMIT_NOFILE &&
1483 new_rlim->rlim_max > sysctl_nr_open)
1484 return -EPERM;
1485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001487 /* protect tsk->signal and tsk->sighand from disappearing */
1488 read_lock(&tasklist_lock);
1489 if (!tsk->sighand) {
1490 retval = -ESRCH;
1491 goto out;
1492 }
1493
Jiri Slaby5b415352010-03-24 16:11:29 +01001494 rlim = tsk->signal->rlim + resource;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001495 task_lock(tsk->group_leader);
Jiri Slaby5b415352010-03-24 16:11:29 +01001496 if (new_rlim) {
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001497 /* Keep the capable check against init_user_ns until
1498 cgroups can contain all limits */
Jiri Slaby5b415352010-03-24 16:11:29 +01001499 if (new_rlim->rlim_max > rlim->rlim_max &&
1500 !capable(CAP_SYS_RESOURCE))
1501 retval = -EPERM;
1502 if (!retval)
Eric W. Biedermancad4ea52017-04-12 17:22:14 -05001503 retval = security_task_setrlimit(tsk, resource, new_rlim);
Jiri Slaby5b415352010-03-24 16:11:29 +01001504 if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
1505 /*
1506 * The caller is asking for an immediate RLIMIT_CPU
1507 * expiry. But we use the zero value to mean "it was
1508 * never set". So let's cheat and make it one second
1509 * instead
1510 */
1511 new_rlim->rlim_cur = 1;
1512 }
Tom Alsberg9926e4c2007-05-08 00:30:31 -07001513 }
Jiri Slaby5b415352010-03-24 16:11:29 +01001514 if (!retval) {
1515 if (old_rlim)
1516 *old_rlim = *rlim;
1517 if (new_rlim)
1518 *rlim = *new_rlim;
1519 }
Jiri Slaby7855c352009-08-26 23:45:34 +02001520 task_unlock(tsk->group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Andrew Mortond3561f72006-03-24 03:18:36 -08001522 /*
1523 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1524 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1525 * very long-standing error, and fixing it now risks breakage of
1526 * applications, so we live with it
1527 */
Jiri Slaby5b415352010-03-24 16:11:29 +01001528 if (!retval && new_rlim && resource == RLIMIT_CPU &&
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05001529 new_rlim->rlim_cur != RLIM_INFINITY &&
1530 IS_ENABLED(CONFIG_POSIX_TIMERS))
Jiri Slaby5b415352010-03-24 16:11:29 +01001531 update_rlimit_cpu(tsk, new_rlim->rlim_cur);
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001532out:
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001533 read_unlock(&tasklist_lock);
Oleg Nesterov2fb9d262009-09-03 19:21:45 +02001534 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001537/* rcu lock must be held */
Stephen Smalley791ec492017-02-17 07:57:00 -05001538static int check_prlimit_permission(struct task_struct *task,
1539 unsigned int flags)
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001540{
1541 const struct cred *cred = current_cred(), *tcred;
Stephen Smalley791ec492017-02-17 07:57:00 -05001542 bool id_match;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001543
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001544 if (current == task)
1545 return 0;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001546
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001547 tcred = __task_cred(task);
Stephen Smalley791ec492017-02-17 07:57:00 -05001548 id_match = (uid_eq(cred->uid, tcred->euid) &&
1549 uid_eq(cred->uid, tcred->suid) &&
1550 uid_eq(cred->uid, tcred->uid) &&
1551 gid_eq(cred->gid, tcred->egid) &&
1552 gid_eq(cred->gid, tcred->sgid) &&
1553 gid_eq(cred->gid, tcred->gid));
1554 if (!id_match && !ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
1555 return -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001556
Stephen Smalley791ec492017-02-17 07:57:00 -05001557 return security_task_prlimit(cred, tcred, flags);
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001558}
1559
1560SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1561 const struct rlimit64 __user *, new_rlim,
1562 struct rlimit64 __user *, old_rlim)
1563{
1564 struct rlimit64 old64, new64;
1565 struct rlimit old, new;
1566 struct task_struct *tsk;
Stephen Smalley791ec492017-02-17 07:57:00 -05001567 unsigned int checkflags = 0;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001568 int ret;
1569
Stephen Smalley791ec492017-02-17 07:57:00 -05001570 if (old_rlim)
1571 checkflags |= LSM_PRLIMIT_READ;
1572
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001573 if (new_rlim) {
1574 if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1575 return -EFAULT;
1576 rlim64_to_rlim(&new64, &new);
Stephen Smalley791ec492017-02-17 07:57:00 -05001577 checkflags |= LSM_PRLIMIT_WRITE;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001578 }
1579
1580 rcu_read_lock();
1581 tsk = pid ? find_task_by_vpid(pid) : current;
1582 if (!tsk) {
1583 rcu_read_unlock();
1584 return -ESRCH;
1585 }
Stephen Smalley791ec492017-02-17 07:57:00 -05001586 ret = check_prlimit_permission(tsk, checkflags);
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001587 if (ret) {
1588 rcu_read_unlock();
1589 return ret;
1590 }
1591 get_task_struct(tsk);
1592 rcu_read_unlock();
1593
1594 ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1595 old_rlim ? &old : NULL);
1596
1597 if (!ret && old_rlim) {
1598 rlim_to_rlim64(&old, &old64);
1599 if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1600 ret = -EFAULT;
1601 }
1602
1603 put_task_struct(tsk);
1604 return ret;
1605}
1606
Jiri Slaby7855c352009-08-26 23:45:34 +02001607SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1608{
1609 struct rlimit new_rlim;
1610
1611 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1612 return -EFAULT;
Jiri Slaby5b415352010-03-24 16:11:29 +01001613 return do_prlimit(current, resource, &new_rlim, NULL);
Jiri Slaby7855c352009-08-26 23:45:34 +02001614}
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616/*
1617 * It would make sense to put struct rusage in the task_struct,
1618 * except that would make the task_struct be *really big*. After
1619 * task_struct gets moved into malloc'ed memory, it would
1620 * make sense to do this. It will make moving the rest of the information
1621 * a lot simpler! (Which we're not doing right now because we're not
1622 * measuring them yet).
1623 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1625 * races with threads incrementing their own counters. But since word
1626 * reads are atomic, we either get new values or old values and we don't
1627 * care which for the sums. We always take the siglock to protect reading
1628 * the c* fields from p->signal from races with exit.c updating those
1629 * fields when reaping, so a sample either gets all the additions of a
1630 * given child after it's reaped, or none so this sample is before reaping.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001631 *
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001632 * Locking:
1633 * We need to take the siglock for CHILDEREN, SELF and BOTH
1634 * for the cases current multithreaded, non-current single threaded
1635 * non-current multithreaded. Thread traversal is now safe with
1636 * the siglock held.
1637 * Strictly speaking, we donot need to take the siglock if we are current and
1638 * single threaded, as no one else can take our signal_struct away, no one
1639 * else can reap the children to update signal->c* counters, and no one else
1640 * can race with the signal-> fields. If we do not take any lock, the
1641 * signal-> fields could be read out of order while another thread was just
1642 * exiting. So we should place a read memory barrier when we avoid the lock.
1643 * On the writer side, write memory barrier is implied in __exit_signal
1644 * as __exit_signal releases the siglock spinlock after updating the signal->
1645 * fields. But we don't do this yet to keep things simple.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001646 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 */
1648
Frank Mayharf06febc2008-09-12 09:54:39 -07001649static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001650{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001651 r->ru_nvcsw += t->nvcsw;
1652 r->ru_nivcsw += t->nivcsw;
1653 r->ru_minflt += t->min_flt;
1654 r->ru_majflt += t->maj_flt;
1655 r->ru_inblock += task_io_get_inblock(t);
1656 r->ru_oublock += task_io_get_oublock(t);
1657}
1658
Al Viroce72a162017-05-14 20:25:02 -04001659void getrusage(struct task_struct *p, int who, struct rusage *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
1661 struct task_struct *t;
1662 unsigned long flags;
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001663 u64 tgutime, tgstime, utime, stime;
Jiri Pirko1f102062009-09-22 16:44:10 -07001664 unsigned long maxrss = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
vishnu.psec94fc32014-10-09 15:30:23 -07001666 memset((char *)r, 0, sizeof (*r));
Martin Schwidefsky64861632011-12-15 14:56:09 +01001667 utime = stime = 0;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001668
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001669 if (who == RUSAGE_THREAD) {
Frederic Weisbeckere80d0a1a2012-11-21 16:26:44 +01001670 task_cputime_adjusted(current, &utime, &stime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001671 accumulate_thread_rusage(p, r);
Jiri Pirko1f102062009-09-22 16:44:10 -07001672 maxrss = p->signal->maxrss;
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001673 goto out;
1674 }
1675
Oleg Nesterovd6cf7232008-04-30 00:52:38 -07001676 if (!lock_task_sighand(p, &flags))
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001677 return;
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 switch (who) {
vishnu.psec94fc32014-10-09 15:30:23 -07001680 case RUSAGE_BOTH:
1681 case RUSAGE_CHILDREN:
1682 utime = p->signal->cutime;
1683 stime = p->signal->cstime;
1684 r->ru_nvcsw = p->signal->cnvcsw;
1685 r->ru_nivcsw = p->signal->cnivcsw;
1686 r->ru_minflt = p->signal->cmin_flt;
1687 r->ru_majflt = p->signal->cmaj_flt;
1688 r->ru_inblock = p->signal->cinblock;
1689 r->ru_oublock = p->signal->coublock;
1690 maxrss = p->signal->cmaxrss;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001691
vishnu.psec94fc32014-10-09 15:30:23 -07001692 if (who == RUSAGE_CHILDREN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 break;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001694
vishnu.psec94fc32014-10-09 15:30:23 -07001695 case RUSAGE_SELF:
1696 thread_group_cputime_adjusted(p, &tgutime, &tgstime);
1697 utime += tgutime;
1698 stime += tgstime;
1699 r->ru_nvcsw += p->signal->nvcsw;
1700 r->ru_nivcsw += p->signal->nivcsw;
1701 r->ru_minflt += p->signal->min_flt;
1702 r->ru_majflt += p->signal->maj_flt;
1703 r->ru_inblock += p->signal->inblock;
1704 r->ru_oublock += p->signal->oublock;
1705 if (maxrss < p->signal->maxrss)
1706 maxrss = p->signal->maxrss;
1707 t = p;
1708 do {
1709 accumulate_thread_rusage(t, r);
1710 } while_each_thread(p, t);
1711 break;
1712
1713 default:
1714 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001716 unlock_task_sighand(p, &flags);
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001717
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001718out:
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001719 r->ru_utime = ns_to_timeval(utime);
1720 r->ru_stime = ns_to_timeval(stime);
Jiri Pirko1f102062009-09-22 16:44:10 -07001721
1722 if (who != RUSAGE_CHILDREN) {
1723 struct mm_struct *mm = get_task_mm(p);
vishnu.psec94fc32014-10-09 15:30:23 -07001724
Jiri Pirko1f102062009-09-22 16:44:10 -07001725 if (mm) {
1726 setmax_mm_hiwater_rss(&maxrss, mm);
1727 mmput(mm);
1728 }
1729 }
1730 r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Al Viroce72a162017-05-14 20:25:02 -04001733SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
1735 struct rusage r;
vishnu.psec94fc32014-10-09 15:30:23 -07001736
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001737 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1738 who != RUSAGE_THREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return -EINVAL;
Al Viroce72a162017-05-14 20:25:02 -04001740
1741 getrusage(current, who, &r);
1742 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
Al Viro8d2d5c42013-03-03 12:49:06 -05001745#ifdef CONFIG_COMPAT
1746COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru)
1747{
1748 struct rusage r;
1749
1750 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1751 who != RUSAGE_THREAD)
1752 return -EINVAL;
1753
Al Viroce72a162017-05-14 20:25:02 -04001754 getrusage(current, who, &r);
Al Viro8d2d5c42013-03-03 12:49:06 -05001755 return put_compat_rusage(&r, ru);
1756}
1757#endif
1758
Heiko Carstense48fbb62009-01-14 14:14:26 +01001759SYSCALL_DEFINE1(umask, int, mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1762 return mask;
1763}
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -08001764
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001765static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001766{
Al Viro2903ff02012-08-28 12:52:22 -04001767 struct fd exe;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001768 struct file *old_exe, *exe_file;
Al Viro496ad9a2013-01-23 17:07:38 -05001769 struct inode *inode;
Al Viro2903ff02012-08-28 12:52:22 -04001770 int err;
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001771
Al Viro2903ff02012-08-28 12:52:22 -04001772 exe = fdget(fd);
1773 if (!exe.file)
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001774 return -EBADF;
1775
Al Viro496ad9a2013-01-23 17:07:38 -05001776 inode = file_inode(exe.file);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001777
1778 /*
1779 * Because the original mm->exe_file points to executable file, make
1780 * sure that this one is executable as well, to avoid breaking an
1781 * overall picture.
1782 */
1783 err = -EACCES;
Eric W. Biederman90f85722015-06-29 14:42:03 -05001784 if (!S_ISREG(inode->i_mode) || path_noexec(&exe.file->f_path))
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001785 goto exit;
1786
Al Viro496ad9a2013-01-23 17:07:38 -05001787 err = inode_permission(inode, MAY_EXEC);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001788 if (err)
1789 goto exit;
1790
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001791 /*
Konstantin Khlebnikov4229fb1d2012-07-11 14:02:11 -07001792 * Forbid mm->exe_file change if old file still mapped.
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001793 */
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001794 exe_file = get_mm_exe_file(mm);
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001795 err = -EBUSY;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001796 if (exe_file) {
Konstantin Khlebnikov4229fb1d2012-07-11 14:02:11 -07001797 struct vm_area_struct *vma;
1798
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001799 down_read(&mm->mmap_sem);
1800 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1801 if (!vma->vm_file)
1802 continue;
1803 if (path_equal(&vma->vm_file->f_path,
1804 &exe_file->f_path))
1805 goto exit_err;
1806 }
1807
1808 up_read(&mm->mmap_sem);
1809 fput(exe_file);
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001810 }
1811
Konstantin Khlebnikov4229fb1d2012-07-11 14:02:11 -07001812 err = 0;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001813 /* set the new file, lockless */
1814 get_file(exe.file);
1815 old_exe = xchg(&mm->exe_file, exe.file);
1816 if (old_exe)
1817 fput(old_exe);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001818exit:
Al Viro2903ff02012-08-28 12:52:22 -04001819 fdput(exe);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001820 return err;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001821exit_err:
1822 up_read(&mm->mmap_sem);
1823 fput(exe_file);
1824 goto exit;
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001825}
1826
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001827/*
1828 * WARNING: we don't require any capability here so be very careful
1829 * in what is allowed for modification from userspace.
1830 */
1831static int validate_prctl_map(struct prctl_mm_map *prctl_map)
1832{
1833 unsigned long mmap_max_addr = TASK_SIZE;
1834 struct mm_struct *mm = current->mm;
1835 int error = -EINVAL, i;
1836
1837 static const unsigned char offsets[] = {
1838 offsetof(struct prctl_mm_map, start_code),
1839 offsetof(struct prctl_mm_map, end_code),
1840 offsetof(struct prctl_mm_map, start_data),
1841 offsetof(struct prctl_mm_map, end_data),
1842 offsetof(struct prctl_mm_map, start_brk),
1843 offsetof(struct prctl_mm_map, brk),
1844 offsetof(struct prctl_mm_map, start_stack),
1845 offsetof(struct prctl_mm_map, arg_start),
1846 offsetof(struct prctl_mm_map, arg_end),
1847 offsetof(struct prctl_mm_map, env_start),
1848 offsetof(struct prctl_mm_map, env_end),
1849 };
1850
1851 /*
1852 * Make sure the members are not somewhere outside
1853 * of allowed address space.
1854 */
1855 for (i = 0; i < ARRAY_SIZE(offsets); i++) {
1856 u64 val = *(u64 *)((char *)prctl_map + offsets[i]);
1857
1858 if ((unsigned long)val >= mmap_max_addr ||
1859 (unsigned long)val < mmap_min_addr)
1860 goto out;
1861 }
1862
1863 /*
1864 * Make sure the pairs are ordered.
1865 */
1866#define __prctl_check_order(__m1, __op, __m2) \
1867 ((unsigned long)prctl_map->__m1 __op \
1868 (unsigned long)prctl_map->__m2) ? 0 : -EINVAL
1869 error = __prctl_check_order(start_code, <, end_code);
1870 error |= __prctl_check_order(start_data, <, end_data);
1871 error |= __prctl_check_order(start_brk, <=, brk);
1872 error |= __prctl_check_order(arg_start, <=, arg_end);
1873 error |= __prctl_check_order(env_start, <=, env_end);
1874 if (error)
1875 goto out;
1876#undef __prctl_check_order
1877
1878 error = -EINVAL;
1879
1880 /*
1881 * @brk should be after @end_data in traditional maps.
1882 */
1883 if (prctl_map->start_brk <= prctl_map->end_data ||
1884 prctl_map->brk <= prctl_map->end_data)
1885 goto out;
1886
1887 /*
1888 * Neither we should allow to override limits if they set.
1889 */
1890 if (check_data_rlimit(rlimit(RLIMIT_DATA), prctl_map->brk,
1891 prctl_map->start_brk, prctl_map->end_data,
1892 prctl_map->start_data))
1893 goto out;
1894
1895 /*
1896 * Someone is trying to cheat the auxv vector.
1897 */
1898 if (prctl_map->auxv_size) {
1899 if (!prctl_map->auxv || prctl_map->auxv_size > sizeof(mm->saved_auxv))
1900 goto out;
1901 }
1902
1903 /*
1904 * Finally, make sure the caller has the rights to
Kirill Tkhai4d28df62017-05-12 17:33:36 +03001905 * change /proc/pid/exe link: only local sys admin should
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001906 * be allowed to.
1907 */
1908 if (prctl_map->exe_fd != (u32)-1) {
Kirill Tkhai4d28df62017-05-12 17:33:36 +03001909 if (!ns_capable(current_user_ns(), CAP_SYS_ADMIN))
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001910 goto out;
1911 }
1912
1913 error = 0;
1914out:
1915 return error;
1916}
1917
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07001918#ifdef CONFIG_CHECKPOINT_RESTORE
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001919static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data_size)
1920{
1921 struct prctl_mm_map prctl_map = { .exe_fd = (u32)-1, };
1922 unsigned long user_auxv[AT_VECTOR_SIZE];
1923 struct mm_struct *mm = current->mm;
1924 int error;
1925
1926 BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
1927 BUILD_BUG_ON(sizeof(struct prctl_mm_map) > 256);
1928
1929 if (opt == PR_SET_MM_MAP_SIZE)
1930 return put_user((unsigned int)sizeof(prctl_map),
1931 (unsigned int __user *)addr);
1932
1933 if (data_size != sizeof(prctl_map))
1934 return -EINVAL;
1935
1936 if (copy_from_user(&prctl_map, addr, sizeof(prctl_map)))
1937 return -EFAULT;
1938
1939 error = validate_prctl_map(&prctl_map);
1940 if (error)
1941 return error;
1942
1943 if (prctl_map.auxv_size) {
1944 memset(user_auxv, 0, sizeof(user_auxv));
1945 if (copy_from_user(user_auxv,
1946 (const void __user *)prctl_map.auxv,
1947 prctl_map.auxv_size))
1948 return -EFAULT;
1949
1950 /* Last entry must be AT_NULL as specification requires */
1951 user_auxv[AT_VECTOR_SIZE - 2] = AT_NULL;
1952 user_auxv[AT_VECTOR_SIZE - 1] = AT_NULL;
1953 }
1954
Mateusz Guzikddf1d392016-01-20 15:01:02 -08001955 if (prctl_map.exe_fd != (u32)-1) {
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001956 error = prctl_set_mm_exe_file(mm, prctl_map.exe_fd);
Mateusz Guzikddf1d392016-01-20 15:01:02 -08001957 if (error)
1958 return error;
1959 }
1960
1961 down_write(&mm->mmap_sem);
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001962
1963 /*
1964 * We don't validate if these members are pointing to
1965 * real present VMAs because application may have correspond
1966 * VMAs already unmapped and kernel uses these members for statistics
1967 * output in procfs mostly, except
1968 *
1969 * - @start_brk/@brk which are used in do_brk but kernel lookups
1970 * for VMAs when updating these memvers so anything wrong written
1971 * here cause kernel to swear at userspace program but won't lead
1972 * to any problem in kernel itself
1973 */
1974
1975 mm->start_code = prctl_map.start_code;
1976 mm->end_code = prctl_map.end_code;
1977 mm->start_data = prctl_map.start_data;
1978 mm->end_data = prctl_map.end_data;
1979 mm->start_brk = prctl_map.start_brk;
1980 mm->brk = prctl_map.brk;
1981 mm->start_stack = prctl_map.start_stack;
1982 mm->arg_start = prctl_map.arg_start;
1983 mm->arg_end = prctl_map.arg_end;
1984 mm->env_start = prctl_map.env_start;
1985 mm->env_end = prctl_map.env_end;
1986
1987 /*
1988 * Note this update of @saved_auxv is lockless thus
1989 * if someone reads this member in procfs while we're
1990 * updating -- it may get partly updated results. It's
1991 * known and acceptable trade off: we leave it as is to
1992 * not introduce additional locks here making the kernel
1993 * more complex.
1994 */
1995 if (prctl_map.auxv_size)
1996 memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
1997
Mateusz Guzikddf1d392016-01-20 15:01:02 -08001998 up_write(&mm->mmap_sem);
1999 return 0;
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07002000}
2001#endif /* CONFIG_CHECKPOINT_RESTORE */
2002
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002003static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr,
2004 unsigned long len)
2005{
2006 /*
2007 * This doesn't move the auxiliary vector itself since it's pinned to
2008 * mm_struct, but it permits filling the vector with new values. It's
2009 * up to the caller to provide sane values here, otherwise userspace
2010 * tools which use this vector might be unhappy.
2011 */
2012 unsigned long user_auxv[AT_VECTOR_SIZE];
2013
2014 if (len > sizeof(user_auxv))
2015 return -EINVAL;
2016
2017 if (copy_from_user(user_auxv, (const void __user *)addr, len))
2018 return -EFAULT;
2019
2020 /* Make sure the last entry is always AT_NULL */
2021 user_auxv[AT_VECTOR_SIZE - 2] = 0;
2022 user_auxv[AT_VECTOR_SIZE - 1] = 0;
2023
2024 BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
2025
2026 task_lock(current);
2027 memcpy(mm->saved_auxv, user_auxv, len);
2028 task_unlock(current);
2029
2030 return 0;
2031}
2032
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002033static int prctl_set_mm(int opt, unsigned long addr,
2034 unsigned long arg4, unsigned long arg5)
2035{
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002036 struct mm_struct *mm = current->mm;
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002037 struct prctl_mm_map prctl_map;
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002038 struct vm_area_struct *vma;
2039 int error;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002040
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07002041 if (arg5 || (arg4 && (opt != PR_SET_MM_AUXV &&
2042 opt != PR_SET_MM_MAP &&
2043 opt != PR_SET_MM_MAP_SIZE)))
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002044 return -EINVAL;
2045
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07002046#ifdef CONFIG_CHECKPOINT_RESTORE
2047 if (opt == PR_SET_MM_MAP || opt == PR_SET_MM_MAP_SIZE)
2048 return prctl_set_mm_map(opt, (const void __user *)addr, arg4);
2049#endif
2050
Cyrill Gorcunov79f07132012-03-15 15:17:10 -07002051 if (!capable(CAP_SYS_RESOURCE))
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002052 return -EPERM;
2053
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07002054 if (opt == PR_SET_MM_EXE_FILE)
2055 return prctl_set_mm_exe_file(mm, (unsigned int)addr);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07002056
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002057 if (opt == PR_SET_MM_AUXV)
2058 return prctl_set_auxv(mm, addr, arg4);
2059
Cyrill Gorcunov1ad75b92012-06-07 14:21:11 -07002060 if (addr >= TASK_SIZE || addr < mmap_min_addr)
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002061 return -EINVAL;
2062
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002063 error = -EINVAL;
2064
Mateusz Guzikddf1d392016-01-20 15:01:02 -08002065 down_write(&mm->mmap_sem);
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002066 vma = find_vma(mm, addr);
2067
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002068 prctl_map.start_code = mm->start_code;
2069 prctl_map.end_code = mm->end_code;
2070 prctl_map.start_data = mm->start_data;
2071 prctl_map.end_data = mm->end_data;
2072 prctl_map.start_brk = mm->start_brk;
2073 prctl_map.brk = mm->brk;
2074 prctl_map.start_stack = mm->start_stack;
2075 prctl_map.arg_start = mm->arg_start;
2076 prctl_map.arg_end = mm->arg_end;
2077 prctl_map.env_start = mm->env_start;
2078 prctl_map.env_end = mm->env_end;
2079 prctl_map.auxv = NULL;
2080 prctl_map.auxv_size = 0;
2081 prctl_map.exe_fd = -1;
2082
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002083 switch (opt) {
2084 case PR_SET_MM_START_CODE:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002085 prctl_map.start_code = addr;
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002086 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002087 case PR_SET_MM_END_CODE:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002088 prctl_map.end_code = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002089 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002090 case PR_SET_MM_START_DATA:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002091 prctl_map.start_data = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002092 break;
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002093 case PR_SET_MM_END_DATA:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002094 prctl_map.end_data = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002095 break;
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002096 case PR_SET_MM_START_STACK:
2097 prctl_map.start_stack = addr;
2098 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002099 case PR_SET_MM_START_BRK:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002100 prctl_map.start_brk = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002101 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002102 case PR_SET_MM_BRK:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002103 prctl_map.brk = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002104 break;
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002105 case PR_SET_MM_ARG_START:
2106 prctl_map.arg_start = addr;
2107 break;
2108 case PR_SET_MM_ARG_END:
2109 prctl_map.arg_end = addr;
2110 break;
2111 case PR_SET_MM_ENV_START:
2112 prctl_map.env_start = addr;
2113 break;
2114 case PR_SET_MM_ENV_END:
2115 prctl_map.env_end = addr;
2116 break;
2117 default:
2118 goto out;
2119 }
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002120
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002121 error = validate_prctl_map(&prctl_map);
2122 if (error)
2123 goto out;
2124
2125 switch (opt) {
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002126 /*
2127 * If command line arguments and environment
2128 * are placed somewhere else on stack, we can
2129 * set them up here, ARG_START/END to setup
2130 * command line argumets and ENV_START/END
2131 * for environment.
2132 */
2133 case PR_SET_MM_START_STACK:
2134 case PR_SET_MM_ARG_START:
2135 case PR_SET_MM_ARG_END:
2136 case PR_SET_MM_ENV_START:
2137 case PR_SET_MM_ENV_END:
2138 if (!vma) {
2139 error = -EFAULT;
2140 goto out;
2141 }
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002142 }
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002143
2144 mm->start_code = prctl_map.start_code;
2145 mm->end_code = prctl_map.end_code;
2146 mm->start_data = prctl_map.start_data;
2147 mm->end_data = prctl_map.end_data;
2148 mm->start_brk = prctl_map.start_brk;
2149 mm->brk = prctl_map.brk;
2150 mm->start_stack = prctl_map.start_stack;
2151 mm->arg_start = prctl_map.arg_start;
2152 mm->arg_end = prctl_map.arg_end;
2153 mm->env_start = prctl_map.env_start;
2154 mm->env_end = prctl_map.env_end;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002155
2156 error = 0;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002157out:
Mateusz Guzikddf1d392016-01-20 15:01:02 -08002158 up_write(&mm->mmap_sem);
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002159 return error;
2160}
Cyrill Gorcunov300f7862012-06-07 14:21:12 -07002161
Amnon Shiloh52b36942013-04-30 15:28:48 -07002162#ifdef CONFIG_CHECKPOINT_RESTORE
Cyrill Gorcunov300f7862012-06-07 14:21:12 -07002163static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
2164{
2165 return put_user(me->clear_child_tid, tid_addr);
2166}
Amnon Shiloh52b36942013-04-30 15:28:48 -07002167#else
Cyrill Gorcunov300f7862012-06-07 14:21:12 -07002168static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
2169{
2170 return -EINVAL;
2171}
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002172#endif
2173
Pavel Tikhomirov749860c2017-01-30 18:06:12 +03002174static int propagate_has_child_subreaper(struct task_struct *p, void *data)
2175{
2176 /*
2177 * If task has has_child_subreaper - all its decendants
2178 * already have these flag too and new decendants will
2179 * inherit it on fork, skip them.
2180 *
2181 * If we've found child_reaper - skip descendants in
2182 * it's subtree as they will never get out pidns.
2183 */
2184 if (p->signal->has_child_subreaper ||
2185 is_child_reaper(task_pid(p)))
2186 return 0;
2187
2188 p->signal->has_child_subreaper = 1;
2189 return 1;
2190}
2191
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01002192SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
2193 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
David Howellsb6dff3e2008-11-14 10:39:16 +11002195 struct task_struct *me = current;
2196 unsigned char comm[sizeof(me->comm)];
2197 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
David Howellsd84f4f92008-11-14 10:39:23 +11002199 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2200 if (error != -ENOSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return error;
2202
David Howellsd84f4f92008-11-14 10:39:23 +11002203 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 switch (option) {
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002205 case PR_SET_PDEATHSIG:
2206 if (!valid_signal(arg2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 error = -EINVAL;
2208 break;
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002209 }
2210 me->pdeath_signal = arg2;
2211 break;
2212 case PR_GET_PDEATHSIG:
2213 error = put_user(me->pdeath_signal, (int __user *)arg2);
2214 break;
2215 case PR_GET_DUMPABLE:
2216 error = get_dumpable(me->mm);
2217 break;
2218 case PR_SET_DUMPABLE:
2219 if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) {
2220 error = -EINVAL;
2221 break;
2222 }
2223 set_dumpable(me->mm, arg2);
2224 break;
2225
2226 case PR_SET_UNALIGN:
2227 error = SET_UNALIGN_CTL(me, arg2);
2228 break;
2229 case PR_GET_UNALIGN:
2230 error = GET_UNALIGN_CTL(me, arg2);
2231 break;
2232 case PR_SET_FPEMU:
2233 error = SET_FPEMU_CTL(me, arg2);
2234 break;
2235 case PR_GET_FPEMU:
2236 error = GET_FPEMU_CTL(me, arg2);
2237 break;
2238 case PR_SET_FPEXC:
2239 error = SET_FPEXC_CTL(me, arg2);
2240 break;
2241 case PR_GET_FPEXC:
2242 error = GET_FPEXC_CTL(me, arg2);
2243 break;
2244 case PR_GET_TIMING:
2245 error = PR_TIMING_STATISTICAL;
2246 break;
2247 case PR_SET_TIMING:
2248 if (arg2 != PR_TIMING_STATISTICAL)
2249 error = -EINVAL;
2250 break;
2251 case PR_SET_NAME:
2252 comm[sizeof(me->comm) - 1] = 0;
2253 if (strncpy_from_user(comm, (char __user *)arg2,
2254 sizeof(me->comm) - 1) < 0)
2255 return -EFAULT;
2256 set_task_comm(me, comm);
2257 proc_comm_connector(me);
2258 break;
2259 case PR_GET_NAME:
2260 get_task_comm(comm, me);
2261 if (copy_to_user((char __user *)arg2, comm, sizeof(comm)))
2262 return -EFAULT;
2263 break;
2264 case PR_GET_ENDIAN:
2265 error = GET_ENDIAN(me, arg2);
2266 break;
2267 case PR_SET_ENDIAN:
2268 error = SET_ENDIAN(me, arg2);
2269 break;
2270 case PR_GET_SECCOMP:
2271 error = prctl_get_seccomp();
2272 break;
2273 case PR_SET_SECCOMP:
2274 error = prctl_set_seccomp(arg2, (char __user *)arg3);
2275 break;
2276 case PR_GET_TSC:
2277 error = GET_TSC_CTL(arg2);
2278 break;
2279 case PR_SET_TSC:
2280 error = SET_TSC_CTL(arg2);
2281 break;
2282 case PR_TASK_PERF_EVENTS_DISABLE:
2283 error = perf_event_task_disable();
2284 break;
2285 case PR_TASK_PERF_EVENTS_ENABLE:
2286 error = perf_event_task_enable();
2287 break;
2288 case PR_GET_TIMERSLACK:
John Stultzda8b44d2016-03-17 14:20:51 -07002289 if (current->timer_slack_ns > ULONG_MAX)
2290 error = ULONG_MAX;
2291 else
2292 error = current->timer_slack_ns;
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002293 break;
2294 case PR_SET_TIMERSLACK:
2295 if (arg2 <= 0)
2296 current->timer_slack_ns =
2297 current->default_timer_slack_ns;
2298 else
2299 current->timer_slack_ns = arg2;
2300 break;
2301 case PR_MCE_KILL:
2302 if (arg4 | arg5)
2303 return -EINVAL;
2304 switch (arg2) {
2305 case PR_MCE_KILL_CLEAR:
2306 if (arg3 != 0)
2307 return -EINVAL;
2308 current->flags &= ~PF_MCE_PROCESS;
2309 break;
2310 case PR_MCE_KILL_SET:
2311 current->flags |= PF_MCE_PROCESS;
2312 if (arg3 == PR_MCE_KILL_EARLY)
2313 current->flags |= PF_MCE_EARLY;
2314 else if (arg3 == PR_MCE_KILL_LATE)
2315 current->flags &= ~PF_MCE_EARLY;
2316 else if (arg3 == PR_MCE_KILL_DEFAULT)
2317 current->flags &=
2318 ~(PF_MCE_EARLY|PF_MCE_PROCESS);
2319 else
2320 return -EINVAL;
2321 break;
2322 default:
2323 return -EINVAL;
2324 }
2325 break;
2326 case PR_MCE_KILL_GET:
2327 if (arg2 | arg3 | arg4 | arg5)
2328 return -EINVAL;
2329 if (current->flags & PF_MCE_PROCESS)
2330 error = (current->flags & PF_MCE_EARLY) ?
2331 PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
2332 else
2333 error = PR_MCE_KILL_DEFAULT;
2334 break;
2335 case PR_SET_MM:
2336 error = prctl_set_mm(arg2, arg3, arg4, arg5);
2337 break;
2338 case PR_GET_TID_ADDRESS:
2339 error = prctl_get_tid_address(me, (int __user **)arg2);
2340 break;
2341 case PR_SET_CHILD_SUBREAPER:
2342 me->signal->is_child_subreaper = !!arg2;
Pavel Tikhomirov749860c2017-01-30 18:06:12 +03002343 if (!arg2)
2344 break;
2345
2346 walk_process_tree(me, propagate_has_child_subreaper, NULL);
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002347 break;
2348 case PR_GET_CHILD_SUBREAPER:
2349 error = put_user(me->signal->is_child_subreaper,
2350 (int __user *)arg2);
2351 break;
2352 case PR_SET_NO_NEW_PRIVS:
2353 if (arg2 != 1 || arg3 || arg4 || arg5)
2354 return -EINVAL;
2355
Kees Cook1d4457f2014-05-21 15:23:46 -07002356 task_set_no_new_privs(current);
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002357 break;
2358 case PR_GET_NO_NEW_PRIVS:
2359 if (arg2 || arg3 || arg4 || arg5)
2360 return -EINVAL;
Kees Cook1d4457f2014-05-21 15:23:46 -07002361 return task_no_new_privs(current) ? 1 : 0;
Alex Thorltona0715cc2014-04-07 15:37:10 -07002362 case PR_GET_THP_DISABLE:
2363 if (arg2 || arg3 || arg4 || arg5)
2364 return -EINVAL;
Michal Hocko18600332017-07-10 15:48:02 -07002365 error = !!test_bit(MMF_DISABLE_THP, &me->mm->flags);
Alex Thorltona0715cc2014-04-07 15:37:10 -07002366 break;
2367 case PR_SET_THP_DISABLE:
2368 if (arg3 || arg4 || arg5)
2369 return -EINVAL;
Michal Hocko17b05732016-05-23 16:26:05 -07002370 if (down_write_killable(&me->mm->mmap_sem))
2371 return -EINTR;
Alex Thorltona0715cc2014-04-07 15:37:10 -07002372 if (arg2)
Michal Hocko18600332017-07-10 15:48:02 -07002373 set_bit(MMF_DISABLE_THP, &me->mm->flags);
Alex Thorltona0715cc2014-04-07 15:37:10 -07002374 else
Michal Hocko18600332017-07-10 15:48:02 -07002375 clear_bit(MMF_DISABLE_THP, &me->mm->flags);
Alex Thorltona0715cc2014-04-07 15:37:10 -07002376 up_write(&me->mm->mmap_sem);
2377 break;
Dave Hansenfe3d1972014-11-14 07:18:29 -08002378 case PR_MPX_ENABLE_MANAGEMENT:
Dave Hansene9d1b4f2015-01-08 14:30:22 -08002379 if (arg2 || arg3 || arg4 || arg5)
2380 return -EINVAL;
Dave Hansen46a6e0c2015-06-07 11:37:02 -07002381 error = MPX_ENABLE_MANAGEMENT();
Dave Hansenfe3d1972014-11-14 07:18:29 -08002382 break;
2383 case PR_MPX_DISABLE_MANAGEMENT:
Dave Hansene9d1b4f2015-01-08 14:30:22 -08002384 if (arg2 || arg3 || arg4 || arg5)
2385 return -EINVAL;
Dave Hansen46a6e0c2015-06-07 11:37:02 -07002386 error = MPX_DISABLE_MANAGEMENT();
Dave Hansenfe3d1972014-11-14 07:18:29 -08002387 break;
Paul Burton97915542015-01-08 12:17:37 +00002388 case PR_SET_FP_MODE:
2389 error = SET_FP_MODE(me, arg2);
2390 break;
2391 case PR_GET_FP_MODE:
2392 error = GET_FP_MODE(me);
2393 break;
Dave Martin2d2123b2017-10-31 15:51:14 +00002394 case PR_SVE_SET_VL:
2395 error = SVE_SET_VL(arg2);
2396 break;
2397 case PR_SVE_GET_VL:
2398 error = SVE_GET_VL();
2399 break;
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002400 default:
2401 error = -EINVAL;
2402 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 }
2404 return error;
2405}
Andi Kleen3cfc3482006-09-26 10:52:28 +02002406
Heiko Carstens836f92a2009-01-14 14:14:33 +01002407SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
2408 struct getcpu_cache __user *, unused)
Andi Kleen3cfc3482006-09-26 10:52:28 +02002409{
2410 int err = 0;
2411 int cpu = raw_smp_processor_id();
vishnu.psec94fc32014-10-09 15:30:23 -07002412
Andi Kleen3cfc3482006-09-26 10:52:28 +02002413 if (cpup)
2414 err |= put_user(cpu, cpup);
2415 if (nodep)
2416 err |= put_user(cpu_to_node(cpu), nodep);
Andi Kleen3cfc3482006-09-26 10:52:28 +02002417 return err ? -EFAULT : 0;
2418}
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002419
Stephen Rothwell4a22f162013-04-30 15:27:37 -07002420/**
2421 * do_sysinfo - fill in sysinfo struct
2422 * @info: pointer to buffer to fill
2423 */
2424static int do_sysinfo(struct sysinfo *info)
2425{
2426 unsigned long mem_total, sav_total;
2427 unsigned int mem_unit, bitcount;
2428 struct timespec tp;
2429
2430 memset(info, 0, sizeof(struct sysinfo));
2431
Oleg Nesterov45c64942013-07-03 15:05:01 -07002432 get_monotonic_boottime(&tp);
Stephen Rothwell4a22f162013-04-30 15:27:37 -07002433 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
2434
2435 get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
2436
2437 info->procs = nr_threads;
2438
2439 si_meminfo(info);
2440 si_swapinfo(info);
2441
2442 /*
2443 * If the sum of all the available memory (i.e. ram + swap)
2444 * is less than can be stored in a 32 bit unsigned long then
2445 * we can be binary compatible with 2.2.x kernels. If not,
2446 * well, in that case 2.2.x was broken anyways...
2447 *
2448 * -Erik Andersen <andersee@debian.org>
2449 */
2450
2451 mem_total = info->totalram + info->totalswap;
2452 if (mem_total < info->totalram || mem_total < info->totalswap)
2453 goto out;
2454 bitcount = 0;
2455 mem_unit = info->mem_unit;
2456 while (mem_unit > 1) {
2457 bitcount++;
2458 mem_unit >>= 1;
2459 sav_total = mem_total;
2460 mem_total <<= 1;
2461 if (mem_total < sav_total)
2462 goto out;
2463 }
2464
2465 /*
2466 * If mem_total did not overflow, multiply all memory values by
2467 * info->mem_unit and set it to 1. This leaves things compatible
2468 * with 2.2.x, and also retains compatibility with earlier 2.4.x
2469 * kernels...
2470 */
2471
2472 info->mem_unit = 1;
2473 info->totalram <<= bitcount;
2474 info->freeram <<= bitcount;
2475 info->sharedram <<= bitcount;
2476 info->bufferram <<= bitcount;
2477 info->totalswap <<= bitcount;
2478 info->freeswap <<= bitcount;
2479 info->totalhigh <<= bitcount;
2480 info->freehigh <<= bitcount;
2481
2482out:
2483 return 0;
2484}
2485
2486SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
2487{
2488 struct sysinfo val;
2489
2490 do_sysinfo(&val);
2491
2492 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
2493 return -EFAULT;
2494
2495 return 0;
2496}
2497
2498#ifdef CONFIG_COMPAT
2499struct compat_sysinfo {
2500 s32 uptime;
2501 u32 loads[3];
2502 u32 totalram;
2503 u32 freeram;
2504 u32 sharedram;
2505 u32 bufferram;
2506 u32 totalswap;
2507 u32 freeswap;
2508 u16 procs;
2509 u16 pad;
2510 u32 totalhigh;
2511 u32 freehigh;
2512 u32 mem_unit;
2513 char _f[20-2*sizeof(u32)-sizeof(int)];
2514};
2515
2516COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
2517{
2518 struct sysinfo s;
2519
2520 do_sysinfo(&s);
2521
2522 /* Check to see if any memory value is too large for 32-bit and scale
2523 * down if needed
2524 */
Scotty Bauer0baae412014-10-09 15:30:26 -07002525 if (upper_32_bits(s.totalram) || upper_32_bits(s.totalswap)) {
Stephen Rothwell4a22f162013-04-30 15:27:37 -07002526 int bitcount = 0;
2527
2528 while (s.mem_unit < PAGE_SIZE) {
2529 s.mem_unit <<= 1;
2530 bitcount++;
2531 }
2532
2533 s.totalram >>= bitcount;
2534 s.freeram >>= bitcount;
2535 s.sharedram >>= bitcount;
2536 s.bufferram >>= bitcount;
2537 s.totalswap >>= bitcount;
2538 s.freeswap >>= bitcount;
2539 s.totalhigh >>= bitcount;
2540 s.freehigh >>= bitcount;
2541 }
2542
2543 if (!access_ok(VERIFY_WRITE, info, sizeof(struct compat_sysinfo)) ||
2544 __put_user(s.uptime, &info->uptime) ||
2545 __put_user(s.loads[0], &info->loads[0]) ||
2546 __put_user(s.loads[1], &info->loads[1]) ||
2547 __put_user(s.loads[2], &info->loads[2]) ||
2548 __put_user(s.totalram, &info->totalram) ||
2549 __put_user(s.freeram, &info->freeram) ||
2550 __put_user(s.sharedram, &info->sharedram) ||
2551 __put_user(s.bufferram, &info->bufferram) ||
2552 __put_user(s.totalswap, &info->totalswap) ||
2553 __put_user(s.freeswap, &info->freeswap) ||
2554 __put_user(s.procs, &info->procs) ||
2555 __put_user(s.totalhigh, &info->totalhigh) ||
2556 __put_user(s.freehigh, &info->freehigh) ||
2557 __put_user(s.mem_unit, &info->mem_unit))
2558 return -EFAULT;
2559
2560 return 0;
2561}
2562#endif /* CONFIG_COMPAT */