blob: c94b9f74a92324fc029ade04a27c0369869860ed [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/kernel/sys.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
Paul Gortmaker9984de12011-05-23 14:51:41 -04008#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/mm.h>
10#include <linux/utsname.h>
11#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/reboot.h>
13#include <linux/prctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/highuid.h>
15#include <linux/fs.h>
Paul Gortmaker74da1ff2011-05-26 12:48:41 -040016#include <linux/kmod.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020017#include <linux/perf_event.h>
Daniel Walker3e88c552007-05-10 22:22:53 -070018#include <linux/resource.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070019#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/workqueue.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080021#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/device.h>
23#include <linux/key.h>
24#include <linux/times.h>
25#include <linux/posix-timers.h>
26#include <linux/security.h>
27#include <linux/dcookies.h>
28#include <linux/suspend.h>
29#include <linux/tty.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070030#include <linux/signal.h>
Matt Helsley9f460802005-11-07 00:59:16 -080031#include <linux/cn_proc.h>
Andi Kleen3cfc3482006-09-26 10:52:28 +020032#include <linux/getcpu.h>
Eric Dumazet6eaeeab2007-05-10 22:22:37 -070033#include <linux/task_io_accounting_ops.h>
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -070034#include <linux/seccomp.h>
Mark Lord40477272007-10-01 01:20:10 -070035#include <linux/cpu.h>
Christoph Hellwige28cbf22010-03-10 15:21:19 -080036#include <linux/personality.h>
Paul Mackerrase3d5a272009-01-06 14:41:02 -080037#include <linux/ptrace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040038#include <linux/fs_struct.h>
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -070039#include <linux/file.h>
40#include <linux/mount.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/gfp.h>
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010042#include <linux/syscore_ops.h>
Andi Kleenbe274252011-08-19 16:15:10 -070043#include <linux/version.h>
44#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <linux/compat.h>
47#include <linux/syscalls.h>
Keshavamurthy Anil S00d7c052005-12-12 00:37:33 -080048#include <linux/kprobes.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070049#include <linux/user_namespace.h>
Chen Gang7fe5e042013-02-21 16:43:06 -080050#include <linux/binfmts.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Stephen Rothwell4a22f162013-04-30 15:27:37 -070052#include <linux/sched.h>
Ingo Molnar4eb5aaa2017-02-08 18:51:29 +010053#include <linux/sched/autogroup.h>
Ingo Molnar4f177222017-02-08 08:45:17 +010054#include <linux/sched/loadavg.h>
Ingo Molnar03441a32017-02-08 18:51:35 +010055#include <linux/sched/stat.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010056#include <linux/sched/mm.h>
Ingo Molnarf7ccbae2017-02-08 18:51:30 +010057#include <linux/sched/coredump.h>
Ingo Molnar29930022017-02-08 18:51:36 +010058#include <linux/sched/task.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +010059#include <linux/sched/cputime.h>
Stephen Rothwell4a22f162013-04-30 15:27:37 -070060#include <linux/rcupdate.h>
61#include <linux/uidgid.h>
62#include <linux/cred.h>
63
Thomas Gleixner33f6a062018-04-29 15:20:11 +020064#include <linux/nospec.h>
65
Seiji Aguchi04c68622011-01-12 16:59:30 -080066#include <linux/kmsg_dump.h>
Andi Kleenbe274252011-08-19 16:15:10 -070067/* Move somewhere else to avoid recompiling? */
68#include <generated/utsrelease.h>
Seiji Aguchi04c68622011-01-12 16:59:30 -080069
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080070#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <asm/io.h>
72#include <asm/unistd.h>
73
74#ifndef SET_UNALIGN_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070075# define SET_UNALIGN_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#endif
77#ifndef GET_UNALIGN_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070078# define GET_UNALIGN_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#endif
80#ifndef SET_FPEMU_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070081# define SET_FPEMU_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#endif
83#ifndef GET_FPEMU_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070084# define GET_FPEMU_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#endif
86#ifndef SET_FPEXC_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070087# define SET_FPEXC_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#endif
89#ifndef GET_FPEXC_CTL
vishnu.psec94fc32014-10-09 15:30:23 -070090# define GET_FPEXC_CTL(a, b) (-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#endif
Anton Blanchard651d7652006-06-07 16:10:19 +100092#ifndef GET_ENDIAN
vishnu.psec94fc32014-10-09 15:30:23 -070093# define GET_ENDIAN(a, b) (-EINVAL)
Anton Blanchard651d7652006-06-07 16:10:19 +100094#endif
95#ifndef SET_ENDIAN
vishnu.psec94fc32014-10-09 15:30:23 -070096# define SET_ENDIAN(a, b) (-EINVAL)
Anton Blanchard651d7652006-06-07 16:10:19 +100097#endif
Erik Bosman8fb402b2008-04-11 18:54:17 +020098#ifndef GET_TSC_CTL
99# define GET_TSC_CTL(a) (-EINVAL)
100#endif
101#ifndef SET_TSC_CTL
102# define SET_TSC_CTL(a) (-EINVAL)
103#endif
Dave Hansenfe3d1972014-11-14 07:18:29 -0800104#ifndef MPX_ENABLE_MANAGEMENT
Dave Hansen46a6e0c2015-06-07 11:37:02 -0700105# define MPX_ENABLE_MANAGEMENT() (-EINVAL)
Dave Hansenfe3d1972014-11-14 07:18:29 -0800106#endif
107#ifndef MPX_DISABLE_MANAGEMENT
Dave Hansen46a6e0c2015-06-07 11:37:02 -0700108# define MPX_DISABLE_MANAGEMENT() (-EINVAL)
Dave Hansenfe3d1972014-11-14 07:18:29 -0800109#endif
Paul Burton97915542015-01-08 12:17:37 +0000110#ifndef GET_FP_MODE
111# define GET_FP_MODE(a) (-EINVAL)
112#endif
113#ifndef SET_FP_MODE
114# define SET_FP_MODE(a,b) (-EINVAL)
115#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117/*
118 * this is where the system-wide overflow UID and GID are defined, for
119 * architectures that now have 32-bit UID/GID but didn't in the past
120 */
121
122int overflowuid = DEFAULT_OVERFLOWUID;
123int overflowgid = DEFAULT_OVERFLOWGID;
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125EXPORT_SYMBOL(overflowuid);
126EXPORT_SYMBOL(overflowgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/*
129 * the same as above, but for filesystems which can only store a 16-bit
130 * UID and GID. as such, this is needed on all architectures
131 */
132
133int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
134int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
135
136EXPORT_SYMBOL(fs_overflowuid);
137EXPORT_SYMBOL(fs_overflowgid);
138
139/*
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700140 * Returns true if current's euid is same as p's uid or euid,
141 * or has CAP_SYS_NICE to p's user_ns.
142 *
143 * Called with rcu_read_lock, creds are safe
144 */
145static bool set_one_prio_perm(struct task_struct *p)
146{
147 const struct cred *cred = current_cred(), *pcred = __task_cred(p);
148
Eric W. Biederman5af66202012-03-03 20:21:47 -0800149 if (uid_eq(pcred->uid, cred->euid) ||
150 uid_eq(pcred->euid, cred->euid))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700151 return true;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800152 if (ns_capable(pcred->user_ns, CAP_SYS_NICE))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700153 return true;
154 return false;
155}
156
157/*
David Howellsc69e8d92008-11-14 10:39:19 +1100158 * set the priority of a task
159 * - the caller must hold the RCU read lock
160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static int set_one_prio(struct task_struct *p, int niceval, int error)
162{
163 int no_nice;
164
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700165 if (!set_one_prio_perm(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 error = -EPERM;
167 goto out;
168 }
Matt Mackalle43379f2005-05-01 08:59:00 -0700169 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 error = -EACCES;
171 goto out;
172 }
173 no_nice = security_task_setnice(p, niceval);
174 if (no_nice) {
175 error = no_nice;
176 goto out;
177 }
178 if (error == -ESRCH)
179 error = 0;
180 set_user_nice(p, niceval);
181out:
182 return error;
183}
184
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100185SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 struct task_struct *g, *p;
188 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100189 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 int error = -EINVAL;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800191 struct pid *pgrp;
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800192 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Daniel Walker3e88c552007-05-10 22:22:53 -0700194 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 goto out;
196
197 /* normalize: avoid signed division (rounding problems) */
198 error = -ESRCH;
Dongsheng Yangc4a4d2f2014-02-11 15:34:51 +0800199 if (niceval < MIN_NICE)
200 niceval = MIN_NICE;
201 if (niceval > MAX_NICE)
202 niceval = MAX_NICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000204 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 read_lock(&tasklist_lock);
206 switch (which) {
vishnu.psec94fc32014-10-09 15:30:23 -0700207 case PRIO_PROCESS:
208 if (who)
209 p = find_task_by_vpid(who);
210 else
211 p = current;
212 if (p)
213 error = set_one_prio(p, niceval, error);
214 break;
215 case PRIO_PGRP:
216 if (who)
217 pgrp = find_vpid(who);
218 else
219 pgrp = task_pgrp(current);
220 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
221 error = set_one_prio(p, niceval, error);
222 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
223 break;
224 case PRIO_USER:
225 uid = make_kuid(cred->user_ns, who);
226 user = cred->user;
227 if (!who)
228 uid = cred->uid;
229 else if (!uid_eq(uid, cred->uid)) {
230 user = find_user(uid);
231 if (!user)
David Howells86a264a2008-11-14 10:39:18 +1100232 goto out_unlock; /* No processes for this user */
vishnu.psec94fc32014-10-09 15:30:23 -0700233 }
234 do_each_thread(g, p) {
Ben Segall8639b462015-11-06 16:32:48 -0800235 if (uid_eq(task_uid(p), uid) && task_pid_vnr(p))
vishnu.psec94fc32014-10-09 15:30:23 -0700236 error = set_one_prio(p, niceval, error);
237 } while_each_thread(g, p);
238 if (!uid_eq(uid, cred->uid))
239 free_uid(user); /* For find_user() */
240 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242out_unlock:
243 read_unlock(&tasklist_lock);
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000244 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245out:
246 return error;
247}
248
249/*
250 * Ugh. To avoid negative return values, "getpriority()" will
251 * not return the normal nice-value, but a negated value that
252 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
253 * to stay compatible.
254 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100255SYSCALL_DEFINE2(getpriority, int, which, int, who)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 struct task_struct *g, *p;
258 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100259 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 long niceval, retval = -ESRCH;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800261 struct pid *pgrp;
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800262 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Daniel Walker3e88c552007-05-10 22:22:53 -0700264 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return -EINVAL;
266
Tetsuo Handa70118832010-02-22 12:44:16 -0800267 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 read_lock(&tasklist_lock);
269 switch (which) {
vishnu.psec94fc32014-10-09 15:30:23 -0700270 case PRIO_PROCESS:
271 if (who)
272 p = find_task_by_vpid(who);
273 else
274 p = current;
275 if (p) {
276 niceval = nice_to_rlimit(task_nice(p));
277 if (niceval > retval)
278 retval = niceval;
279 }
280 break;
281 case PRIO_PGRP:
282 if (who)
283 pgrp = find_vpid(who);
284 else
285 pgrp = task_pgrp(current);
286 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
287 niceval = nice_to_rlimit(task_nice(p));
288 if (niceval > retval)
289 retval = niceval;
290 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
291 break;
292 case PRIO_USER:
293 uid = make_kuid(cred->user_ns, who);
294 user = cred->user;
295 if (!who)
296 uid = cred->uid;
297 else if (!uid_eq(uid, cred->uid)) {
298 user = find_user(uid);
299 if (!user)
300 goto out_unlock; /* No processes for this user */
301 }
302 do_each_thread(g, p) {
Ben Segall8639b462015-11-06 16:32:48 -0800303 if (uid_eq(task_uid(p), uid) && task_pid_vnr(p)) {
Dongsheng Yang7aa2c012014-05-08 18:33:49 +0900304 niceval = nice_to_rlimit(task_nice(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (niceval > retval)
306 retval = niceval;
307 }
vishnu.psec94fc32014-10-09 15:30:23 -0700308 } while_each_thread(g, p);
309 if (!uid_eq(uid, cred->uid))
310 free_uid(user); /* for find_user() */
311 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313out_unlock:
314 read_unlock(&tasklist_lock);
Tetsuo Handa70118832010-02-22 12:44:16 -0800315 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 return retval;
318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320/*
321 * Unprivileged users may change the real gid to the effective gid
322 * or vice versa. (BSD-style)
323 *
324 * If you set the real gid at all, or set the effective gid to a value not
325 * equal to the real gid, then the saved gid is set to the new effective gid.
326 *
327 * This makes it possible for a setgid program to completely drop its
328 * privileges, which is often a useful assertion to make when you are doing
329 * a security audit over a program.
330 *
331 * The general idea is that a program which uses just setregid() will be
332 * 100% compatible with BSD. A program which uses just setgid() will be
vishnu.psec94fc32014-10-09 15:30:23 -0700333 * 100% compatible with POSIX with saved IDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 *
335 * SMP: There are not races, the GIDs are checked only by filesystem
336 * operations (as far as semantic preservation is concerned).
337 */
Iulia Manda28138932015-04-15 16:16:41 -0700338#ifdef CONFIG_MULTIUSER
Heiko Carstensae1251a2009-01-14 14:14:05 +0100339SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800341 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100342 const struct cred *old;
343 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800345 kgid_t krgid, kegid;
346
347 krgid = make_kgid(ns, rgid);
348 kegid = make_kgid(ns, egid);
349
350 if ((rgid != (gid_t) -1) && !gid_valid(krgid))
351 return -EINVAL;
352 if ((egid != (gid_t) -1) && !gid_valid(kegid))
353 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
David Howellsd84f4f92008-11-14 10:39:23 +1100355 new = prepare_creds();
356 if (!new)
357 return -ENOMEM;
358 old = current_cred();
359
David Howellsd84f4f92008-11-14 10:39:23 +1100360 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (rgid != (gid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800362 if (gid_eq(old->gid, krgid) ||
363 gid_eq(old->egid, krgid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700364 ns_capable(old->user_ns, CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800365 new->gid = krgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 else
David Howellsd84f4f92008-11-14 10:39:23 +1100367 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369 if (egid != (gid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800370 if (gid_eq(old->gid, kegid) ||
371 gid_eq(old->egid, kegid) ||
372 gid_eq(old->sgid, kegid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700373 ns_capable(old->user_ns, CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800374 new->egid = kegid;
Cal Peake756184b2006-09-30 23:27:24 -0700375 else
David Howellsd84f4f92008-11-14 10:39:23 +1100376 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
David Howellsd84f4f92008-11-14 10:39:23 +1100378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (rgid != (gid_t) -1 ||
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800380 (egid != (gid_t) -1 && !gid_eq(kegid, old->gid)))
David Howellsd84f4f92008-11-14 10:39:23 +1100381 new->sgid = new->egid;
382 new->fsgid = new->egid;
383
384 return commit_creds(new);
385
386error:
387 abort_creds(new);
388 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391/*
vishnu.psec94fc32014-10-09 15:30:23 -0700392 * setgid() is implemented like SysV w/ SAVED_IDS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 *
394 * SMP: Same implicit races as above.
395 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100396SYSCALL_DEFINE1(setgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800398 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100399 const struct cred *old;
400 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800402 kgid_t kgid;
403
404 kgid = make_kgid(ns, gid);
405 if (!gid_valid(kgid))
406 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
David Howellsd84f4f92008-11-14 10:39:23 +1100408 new = prepare_creds();
409 if (!new)
410 return -ENOMEM;
411 old = current_cred();
412
David Howellsd84f4f92008-11-14 10:39:23 +1100413 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700414 if (ns_capable(old->user_ns, CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800415 new->gid = new->egid = new->sgid = new->fsgid = kgid;
416 else if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->sgid))
417 new->egid = new->fsgid = kgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 else
David Howellsd84f4f92008-11-14 10:39:23 +1100419 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
David Howellsd84f4f92008-11-14 10:39:23 +1100421 return commit_creds(new);
422
423error:
424 abort_creds(new);
425 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
Dhaval Giani54e99122009-02-27 15:13:54 +0530427
David Howellsd84f4f92008-11-14 10:39:23 +1100428/*
429 * change the user struct in a credentials set to match the new UID
430 */
431static int set_user(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 struct user_struct *new_user;
434
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800435 new_user = alloc_uid(new->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (!new_user)
437 return -EAGAIN;
438
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400439 /*
440 * We don't fail in case of NPROC limit excess here because too many
441 * poorly written programs don't check set*uid() return code, assuming
442 * it never fails if called by root. We may still enforce NPROC limit
443 * for programs doing set*uid()+execve() by harmlessly deferring the
444 * failure to the execve() stage.
445 */
Jiri Slaby78d7d402010-03-05 13:42:54 -0800446 if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400447 new_user != INIT_USER)
448 current->flags |= PF_NPROC_EXCEEDED;
449 else
450 current->flags &= ~PF_NPROC_EXCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
David Howellsd84f4f92008-11-14 10:39:23 +1100452 free_uid(new->user);
453 new->user = new_user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return 0;
455}
456
457/*
458 * Unprivileged users may change the real uid to the effective uid
459 * or vice versa. (BSD-style)
460 *
461 * If you set the real uid at all, or set the effective uid to a value not
462 * equal to the real uid, then the saved uid is set to the new effective uid.
463 *
464 * This makes it possible for a setuid program to completely drop its
465 * privileges, which is often a useful assertion to make when you are doing
466 * a security audit over a program.
467 *
468 * The general idea is that a program which uses just setreuid() will be
469 * 100% compatible with BSD. A program which uses just setuid() will be
vishnu.psec94fc32014-10-09 15:30:23 -0700470 * 100% compatible with POSIX with saved IDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100472SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800474 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100475 const struct cred *old;
476 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800478 kuid_t kruid, keuid;
479
480 kruid = make_kuid(ns, ruid);
481 keuid = make_kuid(ns, euid);
482
483 if ((ruid != (uid_t) -1) && !uid_valid(kruid))
484 return -EINVAL;
485 if ((euid != (uid_t) -1) && !uid_valid(keuid))
486 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
David Howellsd84f4f92008-11-14 10:39:23 +1100488 new = prepare_creds();
489 if (!new)
490 return -ENOMEM;
491 old = current_cred();
492
David Howellsd84f4f92008-11-14 10:39:23 +1100493 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (ruid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800495 new->uid = kruid;
496 if (!uid_eq(old->uid, kruid) &&
497 !uid_eq(old->euid, kruid) &&
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700498 !ns_capable(old->user_ns, CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100499 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501
502 if (euid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800503 new->euid = keuid;
504 if (!uid_eq(old->uid, keuid) &&
505 !uid_eq(old->euid, keuid) &&
506 !uid_eq(old->suid, keuid) &&
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700507 !ns_capable(old->user_ns, CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100508 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800511 if (!uid_eq(new->uid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530512 retval = set_user(new);
513 if (retval < 0)
514 goto error;
515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (ruid != (uid_t) -1 ||
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800517 (euid != (uid_t) -1 && !uid_eq(keuid, old->uid)))
David Howellsd84f4f92008-11-14 10:39:23 +1100518 new->suid = new->euid;
519 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
David Howellsd84f4f92008-11-14 10:39:23 +1100521 retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
522 if (retval < 0)
523 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
David Howellsd84f4f92008-11-14 10:39:23 +1100525 return commit_creds(new);
526
527error:
528 abort_creds(new);
529 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
vishnu.psec94fc32014-10-09 15:30:23 -0700531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/*
vishnu.psec94fc32014-10-09 15:30:23 -0700533 * setuid() is implemented like SysV with SAVED_IDS
534 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * Note that SAVED_ID's is deficient in that a setuid root program
vishnu.psec94fc32014-10-09 15:30:23 -0700536 * like sendmail, for example, cannot set its uid to be a normal
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 * user and then switch back, because if you're root, setuid() sets
538 * the saved uid too. If you don't like this, blame the bright people
539 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
540 * will allow a root program to temporarily drop privileges and be able to
vishnu.psec94fc32014-10-09 15:30:23 -0700541 * regain them by swapping the real and effective uid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100543SYSCALL_DEFINE1(setuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800545 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100546 const struct cred *old;
547 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800549 kuid_t kuid;
550
551 kuid = make_kuid(ns, uid);
552 if (!uid_valid(kuid))
553 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
David Howellsd84f4f92008-11-14 10:39:23 +1100555 new = prepare_creds();
556 if (!new)
557 return -ENOMEM;
558 old = current_cred();
559
David Howellsd84f4f92008-11-14 10:39:23 +1100560 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700561 if (ns_capable(old->user_ns, CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800562 new->suid = new->uid = kuid;
563 if (!uid_eq(kuid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530564 retval = set_user(new);
565 if (retval < 0)
566 goto error;
David Howellsd84f4f92008-11-14 10:39:23 +1100567 }
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800568 } else if (!uid_eq(kuid, old->uid) && !uid_eq(kuid, new->suid)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100569 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800572 new->fsuid = new->euid = kuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
David Howellsd84f4f92008-11-14 10:39:23 +1100574 retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
575 if (retval < 0)
576 goto error;
577
578 return commit_creds(new);
579
580error:
581 abort_creds(new);
582 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
585
586/*
587 * This function implements a generic ability to update ruid, euid,
588 * and suid. This allows you to implement the 4.4 compatible seteuid().
589 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100590SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800592 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100593 const struct cred *old;
594 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800596 kuid_t kruid, keuid, ksuid;
597
598 kruid = make_kuid(ns, ruid);
599 keuid = make_kuid(ns, euid);
600 ksuid = make_kuid(ns, suid);
601
602 if ((ruid != (uid_t) -1) && !uid_valid(kruid))
603 return -EINVAL;
604
605 if ((euid != (uid_t) -1) && !uid_valid(keuid))
606 return -EINVAL;
607
608 if ((suid != (uid_t) -1) && !uid_valid(ksuid))
609 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
David Howellsd84f4f92008-11-14 10:39:23 +1100611 new = prepare_creds();
612 if (!new)
613 return -ENOMEM;
614
David Howellsd84f4f92008-11-14 10:39:23 +1100615 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
David Howellsd84f4f92008-11-14 10:39:23 +1100617 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700618 if (!ns_capable(old->user_ns, CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800619 if (ruid != (uid_t) -1 && !uid_eq(kruid, old->uid) &&
620 !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100621 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800622 if (euid != (uid_t) -1 && !uid_eq(keuid, old->uid) &&
623 !uid_eq(keuid, old->euid) && !uid_eq(keuid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100624 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800625 if (suid != (uid_t) -1 && !uid_eq(ksuid, old->uid) &&
626 !uid_eq(ksuid, old->euid) && !uid_eq(ksuid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100627 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
David Howellsd84f4f92008-11-14 10:39:23 +1100629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (ruid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800631 new->uid = kruid;
632 if (!uid_eq(kruid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530633 retval = set_user(new);
634 if (retval < 0)
635 goto error;
636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
David Howellsd84f4f92008-11-14 10:39:23 +1100638 if (euid != (uid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800639 new->euid = keuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (suid != (uid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800641 new->suid = ksuid;
David Howellsd84f4f92008-11-14 10:39:23 +1100642 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
David Howellsd84f4f92008-11-14 10:39:23 +1100644 retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
645 if (retval < 0)
646 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
David Howellsd84f4f92008-11-14 10:39:23 +1100648 return commit_creds(new);
649
650error:
651 abort_creds(new);
652 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800655SYSCALL_DEFINE3(getresuid, uid_t __user *, ruidp, uid_t __user *, euidp, uid_t __user *, suidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
David Howells86a264a2008-11-14 10:39:18 +1100657 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800659 uid_t ruid, euid, suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800661 ruid = from_kuid_munged(cred->user_ns, cred->uid);
662 euid = from_kuid_munged(cred->user_ns, cred->euid);
663 suid = from_kuid_munged(cred->user_ns, cred->suid);
664
vishnu.psec94fc32014-10-09 15:30:23 -0700665 retval = put_user(ruid, ruidp);
666 if (!retval) {
667 retval = put_user(euid, euidp);
668 if (!retval)
669 return put_user(suid, suidp);
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return retval;
672}
673
674/*
675 * Same as above, but for rgid, egid, sgid.
676 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100677SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800679 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100680 const struct cred *old;
681 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800683 kgid_t krgid, kegid, ksgid;
684
685 krgid = make_kgid(ns, rgid);
686 kegid = make_kgid(ns, egid);
687 ksgid = make_kgid(ns, sgid);
688
689 if ((rgid != (gid_t) -1) && !gid_valid(krgid))
690 return -EINVAL;
691 if ((egid != (gid_t) -1) && !gid_valid(kegid))
692 return -EINVAL;
693 if ((sgid != (gid_t) -1) && !gid_valid(ksgid))
694 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
David Howellsd84f4f92008-11-14 10:39:23 +1100696 new = prepare_creds();
697 if (!new)
698 return -ENOMEM;
699 old = current_cred();
700
David Howellsd84f4f92008-11-14 10:39:23 +1100701 retval = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700702 if (!ns_capable(old->user_ns, CAP_SETGID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800703 if (rgid != (gid_t) -1 && !gid_eq(krgid, old->gid) &&
704 !gid_eq(krgid, old->egid) && !gid_eq(krgid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100705 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800706 if (egid != (gid_t) -1 && !gid_eq(kegid, old->gid) &&
707 !gid_eq(kegid, old->egid) && !gid_eq(kegid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100708 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800709 if (sgid != (gid_t) -1 && !gid_eq(ksgid, old->gid) &&
710 !gid_eq(ksgid, old->egid) && !gid_eq(ksgid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100711 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
David Howellsd84f4f92008-11-14 10:39:23 +1100714 if (rgid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800715 new->gid = krgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100716 if (egid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800717 new->egid = kegid;
David Howellsd84f4f92008-11-14 10:39:23 +1100718 if (sgid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800719 new->sgid = ksgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100720 new->fsgid = new->egid;
721
722 return commit_creds(new);
723
724error:
725 abort_creds(new);
726 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800729SYSCALL_DEFINE3(getresgid, gid_t __user *, rgidp, gid_t __user *, egidp, gid_t __user *, sgidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
David Howells86a264a2008-11-14 10:39:18 +1100731 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800733 gid_t rgid, egid, sgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800735 rgid = from_kgid_munged(cred->user_ns, cred->gid);
736 egid = from_kgid_munged(cred->user_ns, cred->egid);
737 sgid = from_kgid_munged(cred->user_ns, cred->sgid);
738
vishnu.psec94fc32014-10-09 15:30:23 -0700739 retval = put_user(rgid, rgidp);
740 if (!retval) {
741 retval = put_user(egid, egidp);
742 if (!retval)
743 retval = put_user(sgid, sgidp);
744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 return retval;
747}
748
749
750/*
751 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
752 * is used for "access()" and for the NFS daemon (letting nfsd stay at
753 * whatever uid it wants to). It normally shadows "euid", except when
754 * explicitly set by setfsuid() or for access..
755 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100756SYSCALL_DEFINE1(setfsuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
David Howellsd84f4f92008-11-14 10:39:23 +1100758 const struct cred *old;
759 struct cred *new;
760 uid_t old_fsuid;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800761 kuid_t kuid;
762
763 old = current_cred();
764 old_fsuid = from_kuid_munged(old->user_ns, old->fsuid);
765
766 kuid = make_kuid(old->user_ns, uid);
767 if (!uid_valid(kuid))
768 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
David Howellsd84f4f92008-11-14 10:39:23 +1100770 new = prepare_creds();
771 if (!new)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800772 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800774 if (uid_eq(kuid, old->uid) || uid_eq(kuid, old->euid) ||
775 uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700776 ns_capable(old->user_ns, CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800777 if (!uid_eq(kuid, old->fsuid)) {
778 new->fsuid = kuid;
David Howellsd84f4f92008-11-14 10:39:23 +1100779 if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
780 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783
David Howellsd84f4f92008-11-14 10:39:23 +1100784 abort_creds(new);
785 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
David Howellsd84f4f92008-11-14 10:39:23 +1100787change_okay:
788 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return old_fsuid;
790}
791
792/*
John Anthony Kazos Jrf42df9e2007-05-09 08:23:08 +0200793 * Samma på svenska..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100795SYSCALL_DEFINE1(setfsgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
David Howellsd84f4f92008-11-14 10:39:23 +1100797 const struct cred *old;
798 struct cred *new;
799 gid_t old_fsgid;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800800 kgid_t kgid;
801
802 old = current_cred();
803 old_fsgid = from_kgid_munged(old->user_ns, old->fsgid);
804
805 kgid = make_kgid(old->user_ns, gid);
806 if (!gid_valid(kgid))
807 return old_fsgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
David Howellsd84f4f92008-11-14 10:39:23 +1100809 new = prepare_creds();
810 if (!new)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800811 return old_fsgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100812
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800813 if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->egid) ||
814 gid_eq(kgid, old->sgid) || gid_eq(kgid, old->fsgid) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700815 ns_capable(old->user_ns, CAP_SETGID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800816 if (!gid_eq(kgid, old->fsgid)) {
817 new->fsgid = kgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100818 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
David Howellsd84f4f92008-11-14 10:39:23 +1100821
David Howellsd84f4f92008-11-14 10:39:23 +1100822 abort_creds(new);
823 return old_fsgid;
824
825change_okay:
826 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return old_fsgid;
828}
Iulia Manda28138932015-04-15 16:16:41 -0700829#endif /* CONFIG_MULTIUSER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Stephen Rothwell4a22f162013-04-30 15:27:37 -0700831/**
832 * sys_getpid - return the thread group id of the current process
833 *
834 * Note, despite the name, this returns the tgid not the pid. The tgid and
835 * the pid are identical unless CLONE_THREAD was specified on clone() in
836 * which case the tgid is the same in all threads of the same group.
837 *
838 * This is SMP safe as current->tgid does not change.
839 */
840SYSCALL_DEFINE0(getpid)
841{
842 return task_tgid_vnr(current);
843}
844
845/* Thread ID - the internal kernel "pid" */
846SYSCALL_DEFINE0(gettid)
847{
848 return task_pid_vnr(current);
849}
850
851/*
852 * Accessing ->real_parent is not SMP-safe, it could
853 * change from under us. However, we can use a stale
854 * value of ->real_parent under rcu_read_lock(), see
855 * release_task()->call_rcu(delayed_put_task_struct).
856 */
857SYSCALL_DEFINE0(getppid)
858{
859 int pid;
860
861 rcu_read_lock();
862 pid = task_tgid_vnr(rcu_dereference(current->real_parent));
863 rcu_read_unlock();
864
865 return pid;
866}
867
868SYSCALL_DEFINE0(getuid)
869{
870 /* Only we change this so SMP safe */
871 return from_kuid_munged(current_user_ns(), current_uid());
872}
873
874SYSCALL_DEFINE0(geteuid)
875{
876 /* Only we change this so SMP safe */
877 return from_kuid_munged(current_user_ns(), current_euid());
878}
879
880SYSCALL_DEFINE0(getgid)
881{
882 /* Only we change this so SMP safe */
883 return from_kgid_munged(current_user_ns(), current_gid());
884}
885
886SYSCALL_DEFINE0(getegid)
887{
888 /* Only we change this so SMP safe */
889 return from_kgid_munged(current_user_ns(), current_egid());
890}
891
Al Viroca2406e2017-05-31 04:22:44 -0400892static void do_sys_times(struct tms *tms)
Frank Mayharf06febc2008-09-12 09:54:39 -0700893{
Frederic Weisbecker5613fda2017-01-31 04:09:23 +0100894 u64 tgutime, tgstime, cutime, cstime;
Frank Mayharf06febc2008-09-12 09:54:39 -0700895
Frederic Weisbeckere80d0a1a2012-11-21 16:26:44 +0100896 thread_group_cputime_adjusted(current, &tgutime, &tgstime);
Frank Mayharf06febc2008-09-12 09:54:39 -0700897 cutime = current->signal->cutime;
898 cstime = current->signal->cstime;
Frederic Weisbecker5613fda2017-01-31 04:09:23 +0100899 tms->tms_utime = nsec_to_clock_t(tgutime);
900 tms->tms_stime = nsec_to_clock_t(tgstime);
901 tms->tms_cutime = nsec_to_clock_t(cutime);
902 tms->tms_cstime = nsec_to_clock_t(cstime);
Frank Mayharf06febc2008-09-12 09:54:39 -0700903}
904
Heiko Carstens58fd3aa2009-01-14 14:14:03 +0100905SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (tbuf) {
908 struct tms tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Frank Mayharf06febc2008-09-12 09:54:39 -0700910 do_sys_times(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
912 return -EFAULT;
913 }
Paul Mackerrase3d5a272009-01-06 14:41:02 -0800914 force_successful_syscall_return();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return (long) jiffies_64_to_clock_t(get_jiffies_64());
916}
917
Al Viroca2406e2017-05-31 04:22:44 -0400918#ifdef CONFIG_COMPAT
919static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
920{
921 return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
922}
923
924COMPAT_SYSCALL_DEFINE1(times, struct compat_tms __user *, tbuf)
925{
926 if (tbuf) {
927 struct tms tms;
928 struct compat_tms tmp;
929
930 do_sys_times(&tms);
931 /* Convert our struct tms to the compat version. */
932 tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
933 tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
934 tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
935 tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
936 if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
937 return -EFAULT;
938 }
939 force_successful_syscall_return();
940 return compat_jiffies_to_clock_t(jiffies);
941}
942#endif
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944/*
945 * This needs some heavy checking ...
946 * I just haven't the stomach for it. I also don't fully
947 * understand sessions/pgrp etc. Let somebody who does explain it.
948 *
949 * OK, I think I have the protection semantics right.... this is really
950 * only important on a multi-user system anyway, to make sure one user
951 * can't send a signal to a process owned by another. -TYT, 12/12/91
952 *
Oleg Nesterov98611e42014-01-23 15:55:52 -0800953 * !PF_FORKNOEXEC check to conform completely to POSIX.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +0100955SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 struct task_struct *p;
Oleg Nesterovee0acf92006-01-08 01:03:53 -0800958 struct task_struct *group_leader = current->group_leader;
Oleg Nesterov4e021302008-02-08 04:19:08 -0800959 struct pid *pgrp;
960 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 if (!pid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700963 pid = task_pid_vnr(group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (!pgid)
965 pgid = pid;
966 if (pgid < 0)
967 return -EINVAL;
Paul E. McKenney950eaac2010-08-31 17:00:18 -0700968 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 /* From this point forward we keep holding onto the tasklist lock
971 * so that our parent does not change from under us. -DaveM
972 */
973 write_lock_irq(&tasklist_lock);
974
975 err = -ESRCH;
Oleg Nesterov4e021302008-02-08 04:19:08 -0800976 p = find_task_by_vpid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (!p)
978 goto out;
979
980 err = -EINVAL;
981 if (!thread_group_leader(p))
982 goto out;
983
Oleg Nesterov4e021302008-02-08 04:19:08 -0800984 if (same_thread_group(p->real_parent, group_leader)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 err = -EPERM;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800986 if (task_session(p) != task_session(group_leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 goto out;
988 err = -EACCES;
Oleg Nesterov98611e42014-01-23 15:55:52 -0800989 if (!(p->flags & PF_FORKNOEXEC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 goto out;
991 } else {
992 err = -ESRCH;
Oleg Nesterovee0acf92006-01-08 01:03:53 -0800993 if (p != group_leader)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 goto out;
995 }
996
997 err = -EPERM;
998 if (p->signal->leader)
999 goto out;
1000
Oleg Nesterov4e021302008-02-08 04:19:08 -08001001 pgrp = task_pid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (pgid != pid) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001003 struct task_struct *g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Oleg Nesterov4e021302008-02-08 04:19:08 -08001005 pgrp = find_vpid(pgid);
1006 g = pid_task(pgrp, PIDTYPE_PGID);
Eric W. Biederman41487c62007-02-12 00:53:01 -08001007 if (!g || task_session(g) != task_session(group_leader))
Oleg Nesterovf020bc42006-12-08 02:38:02 -08001008 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 err = security_task_setpgid(p, pgid);
1012 if (err)
1013 goto out;
1014
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07001015 if (task_pgrp(p) != pgrp)
Oleg Nesterov83beaf32008-04-30 00:54:27 -07001016 change_pid(p, PIDTYPE_PGID, pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 err = 0;
1019out:
1020 /* All paths lead to here, thus we are safe. -DaveM */
1021 write_unlock_irq(&tasklist_lock);
Paul E. McKenney950eaac2010-08-31 17:00:18 -07001022 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return err;
1024}
1025
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001026SYSCALL_DEFINE1(getpgid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001028 struct task_struct *p;
1029 struct pid *grp;
1030 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001032 rcu_read_lock();
1033 if (!pid)
1034 grp = task_pgrp(current);
1035 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 retval = -ESRCH;
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001037 p = find_task_by_vpid(pid);
1038 if (!p)
1039 goto out;
1040 grp = task_pgrp(p);
1041 if (!grp)
1042 goto out;
1043
1044 retval = security_task_getpgid(p);
1045 if (retval)
1046 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001048 retval = pid_vnr(grp);
1049out:
1050 rcu_read_unlock();
1051 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
1054#ifdef __ARCH_WANT_SYS_GETPGRP
1055
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001056SYSCALL_DEFINE0(getpgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001058 return sys_getpgid(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061#endif
1062
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001063SYSCALL_DEFINE1(getsid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001065 struct task_struct *p;
1066 struct pid *sid;
1067 int retval;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001068
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001069 rcu_read_lock();
1070 if (!pid)
1071 sid = task_session(current);
1072 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 retval = -ESRCH;
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001074 p = find_task_by_vpid(pid);
1075 if (!p)
1076 goto out;
1077 sid = task_session(p);
1078 if (!sid)
1079 goto out;
1080
1081 retval = security_task_getsid(p);
1082 if (retval)
1083 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001085 retval = pid_vnr(sid);
1086out:
1087 rcu_read_unlock();
1088 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
Oleg Nesterov81dabb42013-07-03 15:08:26 -07001091static void set_special_pids(struct pid *pid)
1092{
1093 struct task_struct *curr = current->group_leader;
1094
1095 if (task_session(curr) != pid)
1096 change_pid(curr, PIDTYPE_SID, pid);
1097
1098 if (task_pgrp(curr) != pid)
1099 change_pid(curr, PIDTYPE_PGID, pid);
1100}
1101
Heiko Carstensb290ebe2009-01-14 14:14:06 +01001102SYSCALL_DEFINE0(setsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Oren Laadane19f2472006-01-08 01:03:58 -08001104 struct task_struct *group_leader = current->group_leader;
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001105 struct pid *sid = task_pid(group_leader);
1106 pid_t session = pid_vnr(sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 int err = -EPERM;
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 write_lock_irq(&tasklist_lock);
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001110 /* Fail if I am already a session leader */
1111 if (group_leader->signal->leader)
1112 goto out;
1113
Oleg Nesterov430c6232008-02-08 04:19:11 -08001114 /* Fail if a process group id already exists that equals the
1115 * proposed session id.
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001116 */
Oleg Nesterov6806aac2008-02-08 04:19:12 -08001117 if (pid_task(sid, PIDTYPE_PGID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 goto out;
1119
Oren Laadane19f2472006-01-08 01:03:58 -08001120 group_leader->signal->leader = 1;
Oleg Nesterov81dabb42013-07-03 15:08:26 -07001121 set_special_pids(sid);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001122
Alan Cox9c9f4de2008-10-13 10:37:26 +01001123 proc_clear_tty(group_leader);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001124
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001125 err = session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126out:
1127 write_unlock_irq(&tasklist_lock);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001128 if (err > 0) {
Christian Borntraeger0d0df592009-10-26 16:49:34 -07001129 proc_sid_connector(group_leader);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001130 sched_autogroup_create_attach(group_leader);
1131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return err;
1133}
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135DECLARE_RWSEM(uts_sem);
1136
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001137#ifdef COMPAT_UTS_MACHINE
1138#define override_architecture(name) \
Andreas Schwab46da2762010-04-23 13:17:44 -04001139 (personality(current->personality) == PER_LINUX32 && \
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001140 copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1141 sizeof(COMPAT_UTS_MACHINE)))
1142#else
1143#define override_architecture(name) 0
1144#endif
1145
Andi Kleenbe274252011-08-19 16:15:10 -07001146/*
1147 * Work around broken programs that cannot handle "Linux 3.0".
1148 * 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 -08001149 * 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 -07001150 */
Kees Cook2702b152012-10-19 13:56:51 -07001151static int override_release(char __user *release, size_t len)
Andi Kleenbe274252011-08-19 16:15:10 -07001152{
1153 int ret = 0;
Andi Kleenbe274252011-08-19 16:15:10 -07001154
1155 if (current->personality & UNAME26) {
Kees Cook2702b152012-10-19 13:56:51 -07001156 const char *rest = UTS_RELEASE;
1157 char buf[65] = { 0 };
Andi Kleenbe274252011-08-19 16:15:10 -07001158 int ndots = 0;
1159 unsigned v;
Kees Cook2702b152012-10-19 13:56:51 -07001160 size_t copy;
Andi Kleenbe274252011-08-19 16:15:10 -07001161
1162 while (*rest) {
1163 if (*rest == '.' && ++ndots >= 3)
1164 break;
1165 if (!isdigit(*rest) && *rest != '.')
1166 break;
1167 rest++;
1168 }
Jon DeVree39afb5e2015-02-27 15:52:07 -08001169 v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 60;
Kees Cook31fd84b92012-10-19 18:45:53 -07001170 copy = clamp_t(size_t, len, 1, sizeof(buf));
Kees Cook2702b152012-10-19 13:56:51 -07001171 copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
1172 ret = copy_to_user(release, buf, copy + 1);
Andi Kleenbe274252011-08-19 16:15:10 -07001173 }
1174 return ret;
1175}
1176
Heiko Carstense48fbb62009-01-14 14:14:26 +01001177SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Jann Hornb692c402018-06-25 18:34:10 +02001179 struct new_utsname tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 down_read(&uts_sem);
Jann Hornb692c402018-06-25 18:34:10 +02001182 memcpy(&tmp, utsname(), sizeof(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 up_read(&uts_sem);
Jann Hornb692c402018-06-25 18:34:10 +02001184 if (copy_to_user(name, &tmp, sizeof(tmp)))
1185 return -EFAULT;
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001186
Jann Hornb692c402018-06-25 18:34:10 +02001187 if (override_release(name->release, sizeof(name->release)))
1188 return -EFAULT;
1189 if (override_architecture(name))
1190 return -EFAULT;
1191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001194#ifdef __ARCH_WANT_SYS_OLD_UNAME
1195/*
1196 * Old cruft
1197 */
1198SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
1199{
Jann Hornb692c402018-06-25 18:34:10 +02001200 struct old_utsname tmp;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001201
1202 if (!name)
1203 return -EFAULT;
1204
1205 down_read(&uts_sem);
Jann Hornb692c402018-06-25 18:34:10 +02001206 memcpy(&tmp, utsname(), sizeof(tmp));
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001207 up_read(&uts_sem);
Jann Hornb692c402018-06-25 18:34:10 +02001208 if (copy_to_user(name, &tmp, sizeof(tmp)))
1209 return -EFAULT;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001210
Jann Hornb692c402018-06-25 18:34:10 +02001211 if (override_release(name->release, sizeof(name->release)))
1212 return -EFAULT;
1213 if (override_architecture(name))
1214 return -EFAULT;
1215 return 0;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001216}
1217
1218SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
1219{
Joe Perches62fd2fc2019-12-04 16:50:53 -08001220 struct oldold_utsname tmp;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001221
1222 if (!name)
1223 return -EFAULT;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001224
Joe Perches62fd2fc2019-12-04 16:50:53 -08001225 memset(&tmp, 0, sizeof(tmp));
1226
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001227 down_read(&uts_sem);
Jann Hornb692c402018-06-25 18:34:10 +02001228 memcpy(&tmp.sysname, &utsname()->sysname, __OLD_UTS_LEN);
1229 memcpy(&tmp.nodename, &utsname()->nodename, __OLD_UTS_LEN);
1230 memcpy(&tmp.release, &utsname()->release, __OLD_UTS_LEN);
1231 memcpy(&tmp.version, &utsname()->version, __OLD_UTS_LEN);
1232 memcpy(&tmp.machine, &utsname()->machine, __OLD_UTS_LEN);
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001233 up_read(&uts_sem);
Jann Hornb692c402018-06-25 18:34:10 +02001234 if (copy_to_user(name, &tmp, sizeof(tmp)))
1235 return -EFAULT;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001236
Jann Hornb692c402018-06-25 18:34:10 +02001237 if (override_architecture(name))
1238 return -EFAULT;
1239 if (override_release(name->release, sizeof(name->release)))
1240 return -EFAULT;
1241 return 0;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001242}
1243#endif
1244
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001245SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
1247 int errno;
1248 char tmp[__NEW_UTS_LEN];
1249
Serge E. Hallynbb96a6f2011-03-23 16:43:18 -07001250 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (len < 0 || len > __NEW_UTS_LEN)
1254 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 errno = -EFAULT;
1256 if (!copy_from_user(tmp, name, len)) {
Jann Hornb692c402018-06-25 18:34:10 +02001257 struct new_utsname *u;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001258
Jann Hornb692c402018-06-25 18:34:10 +02001259 down_write(&uts_sem);
1260 u = utsname();
Andrew Morton9679e4d2008-10-15 22:01:51 -07001261 memcpy(u->nodename, tmp, len);
1262 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 errno = 0;
Sasikantha babu499eea62012-05-31 16:26:07 -07001264 uts_proc_notify(UTS_PROC_HOSTNAME);
Jann Hornb692c402018-06-25 18:34:10 +02001265 up_write(&uts_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return errno;
1268}
1269
1270#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1271
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001272SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Jann Hornb692c402018-06-25 18:34:10 +02001274 int i;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001275 struct new_utsname *u;
Jann Hornb692c402018-06-25 18:34:10 +02001276 char tmp[__NEW_UTS_LEN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 if (len < 0)
1279 return -EINVAL;
1280 down_read(&uts_sem);
Andrew Morton9679e4d2008-10-15 22:01:51 -07001281 u = utsname();
1282 i = 1 + strlen(u->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (i > len)
1284 i = len;
Jann Hornb692c402018-06-25 18:34:10 +02001285 memcpy(tmp, u->nodename, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 up_read(&uts_sem);
Jann Hornb692c402018-06-25 18:34:10 +02001287 if (copy_to_user(name, tmp, i))
1288 return -EFAULT;
1289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
1292#endif
1293
1294/*
1295 * Only setdomainname; getdomainname can be implemented by calling
1296 * uname()
1297 */
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001298SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 int errno;
1301 char tmp[__NEW_UTS_LEN];
1302
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001303 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return -EPERM;
1305 if (len < 0 || len > __NEW_UTS_LEN)
1306 return -EINVAL;
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 errno = -EFAULT;
1309 if (!copy_from_user(tmp, name, len)) {
Jann Hornb692c402018-06-25 18:34:10 +02001310 struct new_utsname *u;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001311
Jann Hornb692c402018-06-25 18:34:10 +02001312 down_write(&uts_sem);
1313 u = utsname();
Andrew Morton9679e4d2008-10-15 22:01:51 -07001314 memcpy(u->domainname, tmp, len);
1315 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 errno = 0;
Sasikantha babu499eea62012-05-31 16:26:07 -07001317 uts_proc_notify(UTS_PROC_DOMAINNAME);
Jann Hornb692c402018-06-25 18:34:10 +02001318 up_write(&uts_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return errno;
1321}
1322
Heiko Carstense48fbb62009-01-14 14:14:26 +01001323SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Jiri Slabyb9518342010-05-04 11:28:25 +02001325 struct rlimit value;
1326 int ret;
1327
1328 ret = do_prlimit(current, resource, NULL, &value);
1329 if (!ret)
1330 ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1331
1332 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
Al Virod9e968c2017-05-31 04:33:51 -04001335#ifdef CONFIG_COMPAT
1336
1337COMPAT_SYSCALL_DEFINE2(setrlimit, unsigned int, resource,
1338 struct compat_rlimit __user *, rlim)
1339{
1340 struct rlimit r;
1341 struct compat_rlimit r32;
1342
1343 if (copy_from_user(&r32, rlim, sizeof(struct compat_rlimit)))
1344 return -EFAULT;
1345
1346 if (r32.rlim_cur == COMPAT_RLIM_INFINITY)
1347 r.rlim_cur = RLIM_INFINITY;
1348 else
1349 r.rlim_cur = r32.rlim_cur;
1350 if (r32.rlim_max == COMPAT_RLIM_INFINITY)
1351 r.rlim_max = RLIM_INFINITY;
1352 else
1353 r.rlim_max = r32.rlim_max;
1354 return do_prlimit(current, resource, &r, NULL);
1355}
1356
1357COMPAT_SYSCALL_DEFINE2(getrlimit, unsigned int, resource,
1358 struct compat_rlimit __user *, rlim)
1359{
1360 struct rlimit r;
1361 int ret;
1362
1363 ret = do_prlimit(current, resource, NULL, &r);
1364 if (!ret) {
Al Viro58c7ffc2017-07-12 04:59:45 +01001365 struct compat_rlimit r32;
Al Virod9e968c2017-05-31 04:33:51 -04001366 if (r.rlim_cur > COMPAT_RLIM_INFINITY)
1367 r32.rlim_cur = COMPAT_RLIM_INFINITY;
1368 else
1369 r32.rlim_cur = r.rlim_cur;
1370 if (r.rlim_max > COMPAT_RLIM_INFINITY)
1371 r32.rlim_max = COMPAT_RLIM_INFINITY;
1372 else
1373 r32.rlim_max = r.rlim_max;
1374
1375 if (copy_to_user(rlim, &r32, sizeof(struct compat_rlimit)))
1376 return -EFAULT;
1377 }
1378 return ret;
1379}
1380
1381#endif
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1384
1385/*
1386 * Back compatibility for getrlimit. Needed for some apps.
1387 */
Heiko Carstense48fbb62009-01-14 14:14:26 +01001388SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1389 struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 struct rlimit x;
1392 if (resource >= RLIM_NLIMITS)
1393 return -EINVAL;
1394
Gustavo A. R. Silva058dfcf2018-05-25 14:47:57 -07001395 resource = array_index_nospec(resource, RLIM_NLIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 task_lock(current->group_leader);
1397 x = current->signal->rlim[resource];
1398 task_unlock(current->group_leader);
Cal Peake756184b2006-09-30 23:27:24 -07001399 if (x.rlim_cur > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 x.rlim_cur = 0x7FFFFFFF;
Cal Peake756184b2006-09-30 23:27:24 -07001401 if (x.rlim_max > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 x.rlim_max = 0x7FFFFFFF;
vishnu.psec94fc32014-10-09 15:30:23 -07001403 return copy_to_user(rlim, &x, sizeof(x)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
Al Viro613763a2017-05-26 22:04:29 -04001406#ifdef CONFIG_COMPAT
1407COMPAT_SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1408 struct compat_rlimit __user *, rlim)
1409{
1410 struct rlimit r;
1411
1412 if (resource >= RLIM_NLIMITS)
1413 return -EINVAL;
1414
Gustavo A. R. Silva058dfcf2018-05-25 14:47:57 -07001415 resource = array_index_nospec(resource, RLIM_NLIMITS);
Al Viro613763a2017-05-26 22:04:29 -04001416 task_lock(current->group_leader);
1417 r = current->signal->rlim[resource];
1418 task_unlock(current->group_leader);
1419 if (r.rlim_cur > 0x7FFFFFFF)
1420 r.rlim_cur = 0x7FFFFFFF;
1421 if (r.rlim_max > 0x7FFFFFFF)
1422 r.rlim_max = 0x7FFFFFFF;
1423
1424 if (put_user(r.rlim_cur, &rlim->rlim_cur) ||
1425 put_user(r.rlim_max, &rlim->rlim_max))
1426 return -EFAULT;
1427 return 0;
1428}
1429#endif
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431#endif
1432
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001433static inline bool rlim64_is_infinity(__u64 rlim64)
1434{
1435#if BITS_PER_LONG < 64
1436 return rlim64 >= ULONG_MAX;
1437#else
1438 return rlim64 == RLIM64_INFINITY;
1439#endif
1440}
1441
1442static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1443{
1444 if (rlim->rlim_cur == RLIM_INFINITY)
1445 rlim64->rlim_cur = RLIM64_INFINITY;
1446 else
1447 rlim64->rlim_cur = rlim->rlim_cur;
1448 if (rlim->rlim_max == RLIM_INFINITY)
1449 rlim64->rlim_max = RLIM64_INFINITY;
1450 else
1451 rlim64->rlim_max = rlim->rlim_max;
1452}
1453
1454static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1455{
1456 if (rlim64_is_infinity(rlim64->rlim_cur))
1457 rlim->rlim_cur = RLIM_INFINITY;
1458 else
1459 rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1460 if (rlim64_is_infinity(rlim64->rlim_max))
1461 rlim->rlim_max = RLIM_INFINITY;
1462 else
1463 rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1464}
1465
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001466/* make sure you are allowed to change @tsk limits before calling this */
Jiri Slaby5b415352010-03-24 16:11:29 +01001467int do_prlimit(struct task_struct *tsk, unsigned int resource,
1468 struct rlimit *new_rlim, struct rlimit *old_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Jiri Slaby5b415352010-03-24 16:11:29 +01001470 struct rlimit *rlim;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001471 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 if (resource >= RLIM_NLIMITS)
1474 return -EINVAL;
Greg Kroah-Hartman291a0392023-01-20 11:03:20 +01001475 resource = array_index_nospec(resource, RLIM_NLIMITS);
1476
Jiri Slaby5b415352010-03-24 16:11:29 +01001477 if (new_rlim) {
1478 if (new_rlim->rlim_cur > new_rlim->rlim_max)
1479 return -EINVAL;
1480 if (resource == RLIMIT_NOFILE &&
1481 new_rlim->rlim_max > sysctl_nr_open)
1482 return -EPERM;
1483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001485 /* protect tsk->signal and tsk->sighand from disappearing */
1486 read_lock(&tasklist_lock);
1487 if (!tsk->sighand) {
1488 retval = -ESRCH;
1489 goto out;
1490 }
1491
Jiri Slaby5b415352010-03-24 16:11:29 +01001492 rlim = tsk->signal->rlim + resource;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001493 task_lock(tsk->group_leader);
Jiri Slaby5b415352010-03-24 16:11:29 +01001494 if (new_rlim) {
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001495 /* Keep the capable check against init_user_ns until
1496 cgroups can contain all limits */
Jiri Slaby5b415352010-03-24 16:11:29 +01001497 if (new_rlim->rlim_max > rlim->rlim_max &&
1498 !capable(CAP_SYS_RESOURCE))
1499 retval = -EPERM;
1500 if (!retval)
Eric W. Biedermancad4ea52017-04-12 17:22:14 -05001501 retval = security_task_setrlimit(tsk, resource, new_rlim);
Jiri Slaby5b415352010-03-24 16:11:29 +01001502 if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
1503 /*
1504 * The caller is asking for an immediate RLIMIT_CPU
1505 * expiry. But we use the zero value to mean "it was
1506 * never set". So let's cheat and make it one second
1507 * instead
1508 */
1509 new_rlim->rlim_cur = 1;
1510 }
Tom Alsberg9926e4c2007-05-08 00:30:31 -07001511 }
Jiri Slaby5b415352010-03-24 16:11:29 +01001512 if (!retval) {
1513 if (old_rlim)
1514 *old_rlim = *rlim;
1515 if (new_rlim)
1516 *rlim = *new_rlim;
1517 }
Jiri Slaby7855c352009-08-26 23:45:34 +02001518 task_unlock(tsk->group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Andrew Mortond3561f72006-03-24 03:18:36 -08001520 /*
1521 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1522 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1523 * very long-standing error, and fixing it now risks breakage of
1524 * applications, so we live with it
1525 */
Jiri Slaby5b415352010-03-24 16:11:29 +01001526 if (!retval && new_rlim && resource == RLIMIT_CPU &&
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05001527 new_rlim->rlim_cur != RLIM_INFINITY &&
1528 IS_ENABLED(CONFIG_POSIX_TIMERS))
Jiri Slaby5b415352010-03-24 16:11:29 +01001529 update_rlimit_cpu(tsk, new_rlim->rlim_cur);
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001530out:
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001531 read_unlock(&tasklist_lock);
Oleg Nesterov2fb9d262009-09-03 19:21:45 +02001532 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001535/* rcu lock must be held */
Stephen Smalley791ec492017-02-17 07:57:00 -05001536static int check_prlimit_permission(struct task_struct *task,
1537 unsigned int flags)
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001538{
1539 const struct cred *cred = current_cred(), *tcred;
Stephen Smalley791ec492017-02-17 07:57:00 -05001540 bool id_match;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001541
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001542 if (current == task)
1543 return 0;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001544
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001545 tcred = __task_cred(task);
Stephen Smalley791ec492017-02-17 07:57:00 -05001546 id_match = (uid_eq(cred->uid, tcred->euid) &&
1547 uid_eq(cred->uid, tcred->suid) &&
1548 uid_eq(cred->uid, tcred->uid) &&
1549 gid_eq(cred->gid, tcred->egid) &&
1550 gid_eq(cred->gid, tcred->sgid) &&
1551 gid_eq(cred->gid, tcred->gid));
1552 if (!id_match && !ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
1553 return -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001554
Stephen Smalley791ec492017-02-17 07:57:00 -05001555 return security_task_prlimit(cred, tcred, flags);
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001556}
1557
1558SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1559 const struct rlimit64 __user *, new_rlim,
1560 struct rlimit64 __user *, old_rlim)
1561{
1562 struct rlimit64 old64, new64;
1563 struct rlimit old, new;
1564 struct task_struct *tsk;
Stephen Smalley791ec492017-02-17 07:57:00 -05001565 unsigned int checkflags = 0;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001566 int ret;
1567
Stephen Smalley791ec492017-02-17 07:57:00 -05001568 if (old_rlim)
1569 checkflags |= LSM_PRLIMIT_READ;
1570
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001571 if (new_rlim) {
1572 if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1573 return -EFAULT;
1574 rlim64_to_rlim(&new64, &new);
Stephen Smalley791ec492017-02-17 07:57:00 -05001575 checkflags |= LSM_PRLIMIT_WRITE;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001576 }
1577
1578 rcu_read_lock();
1579 tsk = pid ? find_task_by_vpid(pid) : current;
1580 if (!tsk) {
1581 rcu_read_unlock();
1582 return -ESRCH;
1583 }
Stephen Smalley791ec492017-02-17 07:57:00 -05001584 ret = check_prlimit_permission(tsk, checkflags);
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001585 if (ret) {
1586 rcu_read_unlock();
1587 return ret;
1588 }
1589 get_task_struct(tsk);
1590 rcu_read_unlock();
1591
1592 ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1593 old_rlim ? &old : NULL);
1594
1595 if (!ret && old_rlim) {
1596 rlim_to_rlim64(&old, &old64);
1597 if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1598 ret = -EFAULT;
1599 }
1600
1601 put_task_struct(tsk);
1602 return ret;
1603}
1604
Jiri Slaby7855c352009-08-26 23:45:34 +02001605SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1606{
1607 struct rlimit new_rlim;
1608
1609 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1610 return -EFAULT;
Jiri Slaby5b415352010-03-24 16:11:29 +01001611 return do_prlimit(current, resource, &new_rlim, NULL);
Jiri Slaby7855c352009-08-26 23:45:34 +02001612}
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614/*
1615 * It would make sense to put struct rusage in the task_struct,
1616 * except that would make the task_struct be *really big*. After
1617 * task_struct gets moved into malloc'ed memory, it would
1618 * make sense to do this. It will make moving the rest of the information
1619 * a lot simpler! (Which we're not doing right now because we're not
1620 * measuring them yet).
1621 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1623 * races with threads incrementing their own counters. But since word
1624 * reads are atomic, we either get new values or old values and we don't
1625 * care which for the sums. We always take the siglock to protect reading
1626 * the c* fields from p->signal from races with exit.c updating those
1627 * fields when reaping, so a sample either gets all the additions of a
1628 * given child after it's reaped, or none so this sample is before reaping.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001629 *
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001630 * Locking:
1631 * We need to take the siglock for CHILDEREN, SELF and BOTH
1632 * for the cases current multithreaded, non-current single threaded
1633 * non-current multithreaded. Thread traversal is now safe with
1634 * the siglock held.
1635 * Strictly speaking, we donot need to take the siglock if we are current and
1636 * single threaded, as no one else can take our signal_struct away, no one
1637 * else can reap the children to update signal->c* counters, and no one else
1638 * can race with the signal-> fields. If we do not take any lock, the
1639 * signal-> fields could be read out of order while another thread was just
1640 * exiting. So we should place a read memory barrier when we avoid the lock.
1641 * On the writer side, write memory barrier is implied in __exit_signal
1642 * as __exit_signal releases the siglock spinlock after updating the signal->
1643 * fields. But we don't do this yet to keep things simple.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001644 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 */
1646
Frank Mayharf06febc2008-09-12 09:54:39 -07001647static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001648{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001649 r->ru_nvcsw += t->nvcsw;
1650 r->ru_nivcsw += t->nivcsw;
1651 r->ru_minflt += t->min_flt;
1652 r->ru_majflt += t->maj_flt;
1653 r->ru_inblock += task_io_get_inblock(t);
1654 r->ru_oublock += task_io_get_oublock(t);
1655}
1656
Al Viroce72a162017-05-14 20:25:02 -04001657void getrusage(struct task_struct *p, int who, struct rusage *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 struct task_struct *t;
1660 unsigned long flags;
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001661 u64 tgutime, tgstime, utime, stime;
Oleg Nesterovc048d822024-01-22 16:50:50 +01001662 unsigned long maxrss;
1663 struct mm_struct *mm;
Oleg Nesterovf2348ee2023-09-09 19:25:54 +02001664 struct signal_struct *sig = p->signal;
Oleg Nesterov8d119492024-01-22 16:50:53 +01001665 unsigned int seq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Oleg Nesterov8d119492024-01-22 16:50:53 +01001667retry:
Oleg Nesterovc048d822024-01-22 16:50:50 +01001668 memset(r, 0, sizeof(*r));
Martin Schwidefsky64861632011-12-15 14:56:09 +01001669 utime = stime = 0;
Oleg Nesterovc048d822024-01-22 16:50:50 +01001670 maxrss = 0;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001671
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001672 if (who == RUSAGE_THREAD) {
Frederic Weisbeckere80d0a1a2012-11-21 16:26:44 +01001673 task_cputime_adjusted(current, &utime, &stime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001674 accumulate_thread_rusage(p, r);
Oleg Nesterovf2348ee2023-09-09 19:25:54 +02001675 maxrss = sig->maxrss;
Oleg Nesterovc048d822024-01-22 16:50:50 +01001676 goto out_thread;
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001677 }
1678
Oleg Nesterov8d119492024-01-22 16:50:53 +01001679 flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 switch (who) {
vishnu.psec94fc32014-10-09 15:30:23 -07001682 case RUSAGE_BOTH:
1683 case RUSAGE_CHILDREN:
Oleg Nesterovf2348ee2023-09-09 19:25:54 +02001684 utime = sig->cutime;
1685 stime = sig->cstime;
1686 r->ru_nvcsw = sig->cnvcsw;
1687 r->ru_nivcsw = sig->cnivcsw;
1688 r->ru_minflt = sig->cmin_flt;
1689 r->ru_majflt = sig->cmaj_flt;
1690 r->ru_inblock = sig->cinblock;
1691 r->ru_oublock = sig->coublock;
1692 maxrss = sig->cmaxrss;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001693
vishnu.psec94fc32014-10-09 15:30:23 -07001694 if (who == RUSAGE_CHILDREN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 break;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001696
vishnu.psec94fc32014-10-09 15:30:23 -07001697 case RUSAGE_SELF:
Oleg Nesterovf2348ee2023-09-09 19:25:54 +02001698 r->ru_nvcsw += sig->nvcsw;
1699 r->ru_nivcsw += sig->nivcsw;
1700 r->ru_minflt += sig->min_flt;
1701 r->ru_majflt += sig->maj_flt;
1702 r->ru_inblock += sig->inblock;
1703 r->ru_oublock += sig->oublock;
1704 if (maxrss < sig->maxrss)
1705 maxrss = sig->maxrss;
Oleg Nesterov8d119492024-01-22 16:50:53 +01001706
1707 rcu_read_lock();
Oleg Nesterov82f1e0d2023-09-09 19:26:29 +02001708 __for_each_thread(sig, t)
vishnu.psec94fc32014-10-09 15:30:23 -07001709 accumulate_thread_rusage(t, r);
Oleg Nesterov8d119492024-01-22 16:50:53 +01001710 rcu_read_unlock();
1711
vishnu.psec94fc32014-10-09 15:30:23 -07001712 break;
1713
1714 default:
1715 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
Oleg Nesterov8d119492024-01-22 16:50:53 +01001717
1718 if (need_seqretry(&sig->stats_lock, seq)) {
1719 seq = 1;
1720 goto retry;
1721 }
1722 done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001723
Oleg Nesterovc048d822024-01-22 16:50:50 +01001724 if (who == RUSAGE_CHILDREN)
1725 goto out_children;
1726
1727 thread_group_cputime_adjusted(p, &tgutime, &tgstime);
1728 utime += tgutime;
1729 stime += tgstime;
1730
1731out_thread:
1732 mm = get_task_mm(p);
1733 if (mm) {
1734 setmax_mm_hiwater_rss(&maxrss, mm);
1735 mmput(mm);
1736 }
1737
1738out_children:
1739 r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001740 r->ru_utime = ns_to_timeval(utime);
1741 r->ru_stime = ns_to_timeval(stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742}
1743
Al Viroce72a162017-05-14 20:25:02 -04001744SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
1746 struct rusage r;
vishnu.psec94fc32014-10-09 15:30:23 -07001747
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001748 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1749 who != RUSAGE_THREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return -EINVAL;
Al Viroce72a162017-05-14 20:25:02 -04001751
1752 getrusage(current, who, &r);
1753 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
Al Viro8d2d5c42013-03-03 12:49:06 -05001756#ifdef CONFIG_COMPAT
1757COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru)
1758{
1759 struct rusage r;
1760
1761 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1762 who != RUSAGE_THREAD)
1763 return -EINVAL;
1764
Al Viroce72a162017-05-14 20:25:02 -04001765 getrusage(current, who, &r);
Al Viro8d2d5c42013-03-03 12:49:06 -05001766 return put_compat_rusage(&r, ru);
1767}
1768#endif
1769
Heiko Carstense48fbb62009-01-14 14:14:26 +01001770SYSCALL_DEFINE1(umask, int, mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
1772 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1773 return mask;
1774}
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -08001775
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001776static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001777{
Al Viro2903ff02012-08-28 12:52:22 -04001778 struct fd exe;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001779 struct file *old_exe, *exe_file;
Al Viro496ad9a2013-01-23 17:07:38 -05001780 struct inode *inode;
Al Viro2903ff02012-08-28 12:52:22 -04001781 int err;
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001782
Al Viro2903ff02012-08-28 12:52:22 -04001783 exe = fdget(fd);
1784 if (!exe.file)
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001785 return -EBADF;
1786
Al Viro496ad9a2013-01-23 17:07:38 -05001787 inode = file_inode(exe.file);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001788
1789 /*
1790 * Because the original mm->exe_file points to executable file, make
1791 * sure that this one is executable as well, to avoid breaking an
1792 * overall picture.
1793 */
1794 err = -EACCES;
Eric W. Biederman90f85722015-06-29 14:42:03 -05001795 if (!S_ISREG(inode->i_mode) || path_noexec(&exe.file->f_path))
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001796 goto exit;
1797
Al Viro496ad9a2013-01-23 17:07:38 -05001798 err = inode_permission(inode, MAY_EXEC);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001799 if (err)
1800 goto exit;
1801
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001802 /*
Konstantin Khlebnikov4229fb1d2012-07-11 14:02:11 -07001803 * Forbid mm->exe_file change if old file still mapped.
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001804 */
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001805 exe_file = get_mm_exe_file(mm);
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001806 err = -EBUSY;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001807 if (exe_file) {
Konstantin Khlebnikov4229fb1d2012-07-11 14:02:11 -07001808 struct vm_area_struct *vma;
1809
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001810 down_read(&mm->mmap_sem);
1811 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1812 if (!vma->vm_file)
1813 continue;
1814 if (path_equal(&vma->vm_file->f_path,
1815 &exe_file->f_path))
1816 goto exit_err;
1817 }
1818
1819 up_read(&mm->mmap_sem);
1820 fput(exe_file);
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001821 }
1822
Konstantin Khlebnikov4229fb1d2012-07-11 14:02:11 -07001823 err = 0;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001824 /* set the new file, lockless */
1825 get_file(exe.file);
1826 old_exe = xchg(&mm->exe_file, exe.file);
1827 if (old_exe)
1828 fput(old_exe);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001829exit:
Al Viro2903ff02012-08-28 12:52:22 -04001830 fdput(exe);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001831 return err;
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001832exit_err:
1833 up_read(&mm->mmap_sem);
1834 fput(exe_file);
1835 goto exit;
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001836}
1837
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001838/*
1839 * WARNING: we don't require any capability here so be very careful
1840 * in what is allowed for modification from userspace.
1841 */
1842static int validate_prctl_map(struct prctl_mm_map *prctl_map)
1843{
1844 unsigned long mmap_max_addr = TASK_SIZE;
1845 struct mm_struct *mm = current->mm;
1846 int error = -EINVAL, i;
1847
1848 static const unsigned char offsets[] = {
1849 offsetof(struct prctl_mm_map, start_code),
1850 offsetof(struct prctl_mm_map, end_code),
1851 offsetof(struct prctl_mm_map, start_data),
1852 offsetof(struct prctl_mm_map, end_data),
1853 offsetof(struct prctl_mm_map, start_brk),
1854 offsetof(struct prctl_mm_map, brk),
1855 offsetof(struct prctl_mm_map, start_stack),
1856 offsetof(struct prctl_mm_map, arg_start),
1857 offsetof(struct prctl_mm_map, arg_end),
1858 offsetof(struct prctl_mm_map, env_start),
1859 offsetof(struct prctl_mm_map, env_end),
1860 };
1861
1862 /*
1863 * Make sure the members are not somewhere outside
1864 * of allowed address space.
1865 */
1866 for (i = 0; i < ARRAY_SIZE(offsets); i++) {
1867 u64 val = *(u64 *)((char *)prctl_map + offsets[i]);
1868
1869 if ((unsigned long)val >= mmap_max_addr ||
1870 (unsigned long)val < mmap_min_addr)
1871 goto out;
1872 }
1873
1874 /*
1875 * Make sure the pairs are ordered.
1876 */
1877#define __prctl_check_order(__m1, __op, __m2) \
1878 ((unsigned long)prctl_map->__m1 __op \
1879 (unsigned long)prctl_map->__m2) ? 0 : -EINVAL
1880 error = __prctl_check_order(start_code, <, end_code);
Cyrill Gorcunova84bd982019-05-13 17:15:40 -07001881 error |= __prctl_check_order(start_data,<=, end_data);
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001882 error |= __prctl_check_order(start_brk, <=, brk);
1883 error |= __prctl_check_order(arg_start, <=, arg_end);
1884 error |= __prctl_check_order(env_start, <=, env_end);
1885 if (error)
1886 goto out;
1887#undef __prctl_check_order
1888
1889 error = -EINVAL;
1890
1891 /*
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001892 * Neither we should allow to override limits if they set.
1893 */
1894 if (check_data_rlimit(rlimit(RLIMIT_DATA), prctl_map->brk,
1895 prctl_map->start_brk, prctl_map->end_data,
1896 prctl_map->start_data))
1897 goto out;
1898
1899 /*
1900 * Someone is trying to cheat the auxv vector.
1901 */
1902 if (prctl_map->auxv_size) {
1903 if (!prctl_map->auxv || prctl_map->auxv_size > sizeof(mm->saved_auxv))
1904 goto out;
1905 }
1906
1907 /*
1908 * Finally, make sure the caller has the rights to
Kirill Tkhai4d28df62017-05-12 17:33:36 +03001909 * change /proc/pid/exe link: only local sys admin should
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001910 * be allowed to.
1911 */
1912 if (prctl_map->exe_fd != (u32)-1) {
Kirill Tkhai4d28df62017-05-12 17:33:36 +03001913 if (!ns_capable(current_user_ns(), CAP_SYS_ADMIN))
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001914 goto out;
1915 }
1916
1917 error = 0;
1918out:
1919 return error;
1920}
1921
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07001922#ifdef CONFIG_CHECKPOINT_RESTORE
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001923static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data_size)
1924{
1925 struct prctl_mm_map prctl_map = { .exe_fd = (u32)-1, };
1926 unsigned long user_auxv[AT_VECTOR_SIZE];
1927 struct mm_struct *mm = current->mm;
1928 int error;
1929
1930 BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
1931 BUILD_BUG_ON(sizeof(struct prctl_mm_map) > 256);
1932
1933 if (opt == PR_SET_MM_MAP_SIZE)
1934 return put_user((unsigned int)sizeof(prctl_map),
1935 (unsigned int __user *)addr);
1936
1937 if (data_size != sizeof(prctl_map))
1938 return -EINVAL;
1939
1940 if (copy_from_user(&prctl_map, addr, sizeof(prctl_map)))
1941 return -EFAULT;
1942
1943 error = validate_prctl_map(&prctl_map);
1944 if (error)
1945 return error;
1946
1947 if (prctl_map.auxv_size) {
1948 memset(user_auxv, 0, sizeof(user_auxv));
1949 if (copy_from_user(user_auxv,
1950 (const void __user *)prctl_map.auxv,
1951 prctl_map.auxv_size))
1952 return -EFAULT;
1953
1954 /* Last entry must be AT_NULL as specification requires */
1955 user_auxv[AT_VECTOR_SIZE - 2] = AT_NULL;
1956 user_auxv[AT_VECTOR_SIZE - 1] = AT_NULL;
1957 }
1958
Mateusz Guzikddf1d392016-01-20 15:01:02 -08001959 if (prctl_map.exe_fd != (u32)-1) {
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001960 error = prctl_set_mm_exe_file(mm, prctl_map.exe_fd);
Mateusz Guzikddf1d392016-01-20 15:01:02 -08001961 if (error)
1962 return error;
1963 }
1964
1965 down_write(&mm->mmap_sem);
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07001966
1967 /*
1968 * We don't validate if these members are pointing to
1969 * real present VMAs because application may have correspond
1970 * VMAs already unmapped and kernel uses these members for statistics
1971 * output in procfs mostly, except
1972 *
1973 * - @start_brk/@brk which are used in do_brk but kernel lookups
1974 * for VMAs when updating these memvers so anything wrong written
1975 * here cause kernel to swear at userspace program but won't lead
1976 * to any problem in kernel itself
1977 */
1978
1979 mm->start_code = prctl_map.start_code;
1980 mm->end_code = prctl_map.end_code;
1981 mm->start_data = prctl_map.start_data;
1982 mm->end_data = prctl_map.end_data;
1983 mm->start_brk = prctl_map.start_brk;
1984 mm->brk = prctl_map.brk;
1985 mm->start_stack = prctl_map.start_stack;
1986 mm->arg_start = prctl_map.arg_start;
1987 mm->arg_end = prctl_map.arg_end;
1988 mm->env_start = prctl_map.env_start;
1989 mm->env_end = prctl_map.env_end;
1990
1991 /*
1992 * Note this update of @saved_auxv is lockless thus
1993 * if someone reads this member in procfs while we're
1994 * updating -- it may get partly updated results. It's
1995 * known and acceptable trade off: we leave it as is to
1996 * not introduce additional locks here making the kernel
1997 * more complex.
1998 */
1999 if (prctl_map.auxv_size)
2000 memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
2001
Mateusz Guzikddf1d392016-01-20 15:01:02 -08002002 up_write(&mm->mmap_sem);
2003 return 0;
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07002004}
2005#endif /* CONFIG_CHECKPOINT_RESTORE */
2006
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002007static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr,
2008 unsigned long len)
2009{
2010 /*
2011 * This doesn't move the auxiliary vector itself since it's pinned to
2012 * mm_struct, but it permits filling the vector with new values. It's
2013 * up to the caller to provide sane values here, otherwise userspace
2014 * tools which use this vector might be unhappy.
2015 */
2016 unsigned long user_auxv[AT_VECTOR_SIZE];
2017
2018 if (len > sizeof(user_auxv))
2019 return -EINVAL;
2020
2021 if (copy_from_user(user_auxv, (const void __user *)addr, len))
2022 return -EFAULT;
2023
2024 /* Make sure the last entry is always AT_NULL */
2025 user_auxv[AT_VECTOR_SIZE - 2] = 0;
2026 user_auxv[AT_VECTOR_SIZE - 1] = 0;
2027
2028 BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
2029
2030 task_lock(current);
2031 memcpy(mm->saved_auxv, user_auxv, len);
2032 task_unlock(current);
2033
2034 return 0;
2035}
2036
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002037static int prctl_set_mm(int opt, unsigned long addr,
2038 unsigned long arg4, unsigned long arg5)
2039{
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002040 struct mm_struct *mm = current->mm;
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002041 struct prctl_mm_map prctl_map;
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002042 struct vm_area_struct *vma;
2043 int error;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002044
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07002045 if (arg5 || (arg4 && (opt != PR_SET_MM_AUXV &&
2046 opt != PR_SET_MM_MAP &&
2047 opt != PR_SET_MM_MAP_SIZE)))
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002048 return -EINVAL;
2049
Cyrill Gorcunovf606b772014-10-09 15:27:37 -07002050#ifdef CONFIG_CHECKPOINT_RESTORE
2051 if (opt == PR_SET_MM_MAP || opt == PR_SET_MM_MAP_SIZE)
2052 return prctl_set_mm_map(opt, (const void __user *)addr, arg4);
2053#endif
2054
Cyrill Gorcunov79f07132012-03-15 15:17:10 -07002055 if (!capable(CAP_SYS_RESOURCE))
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002056 return -EPERM;
2057
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07002058 if (opt == PR_SET_MM_EXE_FILE)
2059 return prctl_set_mm_exe_file(mm, (unsigned int)addr);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07002060
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002061 if (opt == PR_SET_MM_AUXV)
2062 return prctl_set_auxv(mm, addr, arg4);
2063
Cyrill Gorcunov1ad75b92012-06-07 14:21:11 -07002064 if (addr >= TASK_SIZE || addr < mmap_min_addr)
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002065 return -EINVAL;
2066
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002067 error = -EINVAL;
2068
Mateusz Guzikddf1d392016-01-20 15:01:02 -08002069 down_write(&mm->mmap_sem);
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002070 vma = find_vma(mm, addr);
2071
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002072 prctl_map.start_code = mm->start_code;
2073 prctl_map.end_code = mm->end_code;
2074 prctl_map.start_data = mm->start_data;
2075 prctl_map.end_data = mm->end_data;
2076 prctl_map.start_brk = mm->start_brk;
2077 prctl_map.brk = mm->brk;
2078 prctl_map.start_stack = mm->start_stack;
2079 prctl_map.arg_start = mm->arg_start;
2080 prctl_map.arg_end = mm->arg_end;
2081 prctl_map.env_start = mm->env_start;
2082 prctl_map.env_end = mm->env_end;
2083 prctl_map.auxv = NULL;
2084 prctl_map.auxv_size = 0;
2085 prctl_map.exe_fd = -1;
2086
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002087 switch (opt) {
2088 case PR_SET_MM_START_CODE:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002089 prctl_map.start_code = addr;
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002090 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002091 case PR_SET_MM_END_CODE:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002092 prctl_map.end_code = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002093 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002094 case PR_SET_MM_START_DATA:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002095 prctl_map.start_data = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002096 break;
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002097 case PR_SET_MM_END_DATA:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002098 prctl_map.end_data = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002099 break;
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002100 case PR_SET_MM_START_STACK:
2101 prctl_map.start_stack = addr;
2102 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002103 case PR_SET_MM_START_BRK:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002104 prctl_map.start_brk = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002105 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002106 case PR_SET_MM_BRK:
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002107 prctl_map.brk = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002108 break;
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002109 case PR_SET_MM_ARG_START:
2110 prctl_map.arg_start = addr;
2111 break;
2112 case PR_SET_MM_ARG_END:
2113 prctl_map.arg_end = addr;
2114 break;
2115 case PR_SET_MM_ENV_START:
2116 prctl_map.env_start = addr;
2117 break;
2118 case PR_SET_MM_ENV_END:
2119 prctl_map.env_end = addr;
2120 break;
2121 default:
2122 goto out;
2123 }
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002124
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002125 error = validate_prctl_map(&prctl_map);
2126 if (error)
2127 goto out;
2128
2129 switch (opt) {
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002130 /*
2131 * If command line arguments and environment
2132 * are placed somewhere else on stack, we can
2133 * set them up here, ARG_START/END to setup
2134 * command line argumets and ENV_START/END
2135 * for environment.
2136 */
2137 case PR_SET_MM_START_STACK:
2138 case PR_SET_MM_ARG_START:
2139 case PR_SET_MM_ARG_END:
2140 case PR_SET_MM_ENV_START:
2141 case PR_SET_MM_ENV_END:
2142 if (!vma) {
2143 error = -EFAULT;
2144 goto out;
2145 }
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002146 }
Alexey Dobriyan4a00e9d2015-06-25 15:00:51 -07002147
2148 mm->start_code = prctl_map.start_code;
2149 mm->end_code = prctl_map.end_code;
2150 mm->start_data = prctl_map.start_data;
2151 mm->end_data = prctl_map.end_data;
2152 mm->start_brk = prctl_map.start_brk;
2153 mm->brk = prctl_map.brk;
2154 mm->start_stack = prctl_map.start_stack;
2155 mm->arg_start = prctl_map.arg_start;
2156 mm->arg_end = prctl_map.arg_end;
2157 mm->env_start = prctl_map.env_start;
2158 mm->env_end = prctl_map.env_end;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002159
2160 error = 0;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002161out:
Mateusz Guzikddf1d392016-01-20 15:01:02 -08002162 up_write(&mm->mmap_sem);
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002163 return error;
2164}
Cyrill Gorcunov300f7862012-06-07 14:21:12 -07002165
Amnon Shiloh52b36942013-04-30 15:28:48 -07002166#ifdef CONFIG_CHECKPOINT_RESTORE
Cyrill Gorcunov300f7862012-06-07 14:21:12 -07002167static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
2168{
2169 return put_user(me->clear_child_tid, tid_addr);
2170}
Amnon Shiloh52b36942013-04-30 15:28:48 -07002171#else
Cyrill Gorcunov300f7862012-06-07 14:21:12 -07002172static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
2173{
2174 return -EINVAL;
2175}
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002176#endif
2177
Pavel Tikhomirov749860c2017-01-30 18:06:12 +03002178static int propagate_has_child_subreaper(struct task_struct *p, void *data)
2179{
2180 /*
2181 * If task has has_child_subreaper - all its decendants
2182 * already have these flag too and new decendants will
2183 * inherit it on fork, skip them.
2184 *
2185 * If we've found child_reaper - skip descendants in
2186 * it's subtree as they will never get out pidns.
2187 */
2188 if (p->signal->has_child_subreaper ||
2189 is_child_reaper(task_pid(p)))
2190 return 0;
2191
2192 p->signal->has_child_subreaper = 1;
2193 return 1;
2194}
2195
Kees Cook7d1254a2018-05-01 15:19:04 -07002196int __weak arch_prctl_spec_ctrl_get(struct task_struct *t, unsigned long which)
Thomas Gleixner33f6a062018-04-29 15:20:11 +02002197{
2198 return -EINVAL;
2199}
2200
Kees Cook7d1254a2018-05-01 15:19:04 -07002201int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which,
2202 unsigned long ctrl)
Thomas Gleixner33f6a062018-04-29 15:20:11 +02002203{
2204 return -EINVAL;
2205}
2206
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01002207SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
2208 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
David Howellsb6dff3e2008-11-14 10:39:16 +11002210 struct task_struct *me = current;
2211 unsigned char comm[sizeof(me->comm)];
2212 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
David Howellsd84f4f92008-11-14 10:39:23 +11002214 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2215 if (error != -ENOSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 return error;
2217
David Howellsd84f4f92008-11-14 10:39:23 +11002218 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 switch (option) {
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002220 case PR_SET_PDEATHSIG:
2221 if (!valid_signal(arg2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 error = -EINVAL;
2223 break;
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002224 }
2225 me->pdeath_signal = arg2;
2226 break;
2227 case PR_GET_PDEATHSIG:
2228 error = put_user(me->pdeath_signal, (int __user *)arg2);
2229 break;
2230 case PR_GET_DUMPABLE:
2231 error = get_dumpable(me->mm);
2232 break;
2233 case PR_SET_DUMPABLE:
2234 if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) {
2235 error = -EINVAL;
2236 break;
2237 }
2238 set_dumpable(me->mm, arg2);
2239 break;
2240
2241 case PR_SET_UNALIGN:
2242 error = SET_UNALIGN_CTL(me, arg2);
2243 break;
2244 case PR_GET_UNALIGN:
2245 error = GET_UNALIGN_CTL(me, arg2);
2246 break;
2247 case PR_SET_FPEMU:
2248 error = SET_FPEMU_CTL(me, arg2);
2249 break;
2250 case PR_GET_FPEMU:
2251 error = GET_FPEMU_CTL(me, arg2);
2252 break;
2253 case PR_SET_FPEXC:
2254 error = SET_FPEXC_CTL(me, arg2);
2255 break;
2256 case PR_GET_FPEXC:
2257 error = GET_FPEXC_CTL(me, arg2);
2258 break;
2259 case PR_GET_TIMING:
2260 error = PR_TIMING_STATISTICAL;
2261 break;
2262 case PR_SET_TIMING:
2263 if (arg2 != PR_TIMING_STATISTICAL)
2264 error = -EINVAL;
2265 break;
2266 case PR_SET_NAME:
2267 comm[sizeof(me->comm) - 1] = 0;
2268 if (strncpy_from_user(comm, (char __user *)arg2,
2269 sizeof(me->comm) - 1) < 0)
2270 return -EFAULT;
2271 set_task_comm(me, comm);
2272 proc_comm_connector(me);
2273 break;
2274 case PR_GET_NAME:
2275 get_task_comm(comm, me);
2276 if (copy_to_user((char __user *)arg2, comm, sizeof(comm)))
2277 return -EFAULT;
2278 break;
2279 case PR_GET_ENDIAN:
2280 error = GET_ENDIAN(me, arg2);
2281 break;
2282 case PR_SET_ENDIAN:
2283 error = SET_ENDIAN(me, arg2);
2284 break;
2285 case PR_GET_SECCOMP:
2286 error = prctl_get_seccomp();
2287 break;
2288 case PR_SET_SECCOMP:
2289 error = prctl_set_seccomp(arg2, (char __user *)arg3);
2290 break;
2291 case PR_GET_TSC:
2292 error = GET_TSC_CTL(arg2);
2293 break;
2294 case PR_SET_TSC:
2295 error = SET_TSC_CTL(arg2);
2296 break;
2297 case PR_TASK_PERF_EVENTS_DISABLE:
2298 error = perf_event_task_disable();
2299 break;
2300 case PR_TASK_PERF_EVENTS_ENABLE:
2301 error = perf_event_task_enable();
2302 break;
2303 case PR_GET_TIMERSLACK:
John Stultzda8b44d2016-03-17 14:20:51 -07002304 if (current->timer_slack_ns > ULONG_MAX)
2305 error = ULONG_MAX;
2306 else
2307 error = current->timer_slack_ns;
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002308 break;
2309 case PR_SET_TIMERSLACK:
2310 if (arg2 <= 0)
2311 current->timer_slack_ns =
2312 current->default_timer_slack_ns;
2313 else
2314 current->timer_slack_ns = arg2;
2315 break;
2316 case PR_MCE_KILL:
2317 if (arg4 | arg5)
2318 return -EINVAL;
2319 switch (arg2) {
2320 case PR_MCE_KILL_CLEAR:
2321 if (arg3 != 0)
2322 return -EINVAL;
2323 current->flags &= ~PF_MCE_PROCESS;
2324 break;
2325 case PR_MCE_KILL_SET:
2326 current->flags |= PF_MCE_PROCESS;
2327 if (arg3 == PR_MCE_KILL_EARLY)
2328 current->flags |= PF_MCE_EARLY;
2329 else if (arg3 == PR_MCE_KILL_LATE)
2330 current->flags &= ~PF_MCE_EARLY;
2331 else if (arg3 == PR_MCE_KILL_DEFAULT)
2332 current->flags &=
2333 ~(PF_MCE_EARLY|PF_MCE_PROCESS);
2334 else
2335 return -EINVAL;
2336 break;
2337 default:
2338 return -EINVAL;
2339 }
2340 break;
2341 case PR_MCE_KILL_GET:
2342 if (arg2 | arg3 | arg4 | arg5)
2343 return -EINVAL;
2344 if (current->flags & PF_MCE_PROCESS)
2345 error = (current->flags & PF_MCE_EARLY) ?
2346 PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
2347 else
2348 error = PR_MCE_KILL_DEFAULT;
2349 break;
2350 case PR_SET_MM:
2351 error = prctl_set_mm(arg2, arg3, arg4, arg5);
2352 break;
2353 case PR_GET_TID_ADDRESS:
2354 error = prctl_get_tid_address(me, (int __user **)arg2);
2355 break;
2356 case PR_SET_CHILD_SUBREAPER:
2357 me->signal->is_child_subreaper = !!arg2;
Pavel Tikhomirov749860c2017-01-30 18:06:12 +03002358 if (!arg2)
2359 break;
2360
2361 walk_process_tree(me, propagate_has_child_subreaper, NULL);
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002362 break;
2363 case PR_GET_CHILD_SUBREAPER:
2364 error = put_user(me->signal->is_child_subreaper,
2365 (int __user *)arg2);
2366 break;
2367 case PR_SET_NO_NEW_PRIVS:
2368 if (arg2 != 1 || arg3 || arg4 || arg5)
2369 return -EINVAL;
2370
Kees Cook1d4457f2014-05-21 15:23:46 -07002371 task_set_no_new_privs(current);
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002372 break;
2373 case PR_GET_NO_NEW_PRIVS:
2374 if (arg2 || arg3 || arg4 || arg5)
2375 return -EINVAL;
Kees Cook1d4457f2014-05-21 15:23:46 -07002376 return task_no_new_privs(current) ? 1 : 0;
Alex Thorltona0715cc2014-04-07 15:37:10 -07002377 case PR_GET_THP_DISABLE:
2378 if (arg2 || arg3 || arg4 || arg5)
2379 return -EINVAL;
Michal Hocko18600332017-07-10 15:48:02 -07002380 error = !!test_bit(MMF_DISABLE_THP, &me->mm->flags);
Alex Thorltona0715cc2014-04-07 15:37:10 -07002381 break;
2382 case PR_SET_THP_DISABLE:
2383 if (arg3 || arg4 || arg5)
2384 return -EINVAL;
Michal Hocko17b05732016-05-23 16:26:05 -07002385 if (down_write_killable(&me->mm->mmap_sem))
2386 return -EINTR;
Alex Thorltona0715cc2014-04-07 15:37:10 -07002387 if (arg2)
Michal Hocko18600332017-07-10 15:48:02 -07002388 set_bit(MMF_DISABLE_THP, &me->mm->flags);
Alex Thorltona0715cc2014-04-07 15:37:10 -07002389 else
Michal Hocko18600332017-07-10 15:48:02 -07002390 clear_bit(MMF_DISABLE_THP, &me->mm->flags);
Alex Thorltona0715cc2014-04-07 15:37:10 -07002391 up_write(&me->mm->mmap_sem);
2392 break;
Dave Hansenfe3d1972014-11-14 07:18:29 -08002393 case PR_MPX_ENABLE_MANAGEMENT:
Dave Hansene9d1b4f2015-01-08 14:30:22 -08002394 if (arg2 || arg3 || arg4 || arg5)
2395 return -EINVAL;
Dave Hansen46a6e0c2015-06-07 11:37:02 -07002396 error = MPX_ENABLE_MANAGEMENT();
Dave Hansenfe3d1972014-11-14 07:18:29 -08002397 break;
2398 case PR_MPX_DISABLE_MANAGEMENT:
Dave Hansene9d1b4f2015-01-08 14:30:22 -08002399 if (arg2 || arg3 || arg4 || arg5)
2400 return -EINVAL;
Dave Hansen46a6e0c2015-06-07 11:37:02 -07002401 error = MPX_DISABLE_MANAGEMENT();
Dave Hansenfe3d1972014-11-14 07:18:29 -08002402 break;
Paul Burton97915542015-01-08 12:17:37 +00002403 case PR_SET_FP_MODE:
2404 error = SET_FP_MODE(me, arg2);
2405 break;
2406 case PR_GET_FP_MODE:
2407 error = GET_FP_MODE(me);
2408 break;
Thomas Gleixner33f6a062018-04-29 15:20:11 +02002409 case PR_GET_SPECULATION_CTRL:
2410 if (arg3 || arg4 || arg5)
2411 return -EINVAL;
Kees Cook7d1254a2018-05-01 15:19:04 -07002412 error = arch_prctl_spec_ctrl_get(me, arg2);
Thomas Gleixner33f6a062018-04-29 15:20:11 +02002413 break;
2414 case PR_SET_SPECULATION_CTRL:
2415 if (arg4 || arg5)
2416 return -EINVAL;
Kees Cook7d1254a2018-05-01 15:19:04 -07002417 error = arch_prctl_spec_ctrl_set(me, arg2, arg3);
Thomas Gleixner33f6a062018-04-29 15:20:11 +02002418 break;
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002419 default:
2420 error = -EINVAL;
2421 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 }
2423 return error;
2424}
Andi Kleen3cfc3482006-09-26 10:52:28 +02002425
Heiko Carstens836f92a2009-01-14 14:14:33 +01002426SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
2427 struct getcpu_cache __user *, unused)
Andi Kleen3cfc3482006-09-26 10:52:28 +02002428{
2429 int err = 0;
2430 int cpu = raw_smp_processor_id();
vishnu.psec94fc32014-10-09 15:30:23 -07002431
Andi Kleen3cfc3482006-09-26 10:52:28 +02002432 if (cpup)
2433 err |= put_user(cpu, cpup);
2434 if (nodep)
2435 err |= put_user(cpu_to_node(cpu), nodep);
Andi Kleen3cfc3482006-09-26 10:52:28 +02002436 return err ? -EFAULT : 0;
2437}
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002438
Stephen Rothwell4a22f162013-04-30 15:27:37 -07002439/**
2440 * do_sysinfo - fill in sysinfo struct
2441 * @info: pointer to buffer to fill
2442 */
2443static int do_sysinfo(struct sysinfo *info)
2444{
2445 unsigned long mem_total, sav_total;
2446 unsigned int mem_unit, bitcount;
2447 struct timespec tp;
2448
2449 memset(info, 0, sizeof(struct sysinfo));
2450
Oleg Nesterov45c64942013-07-03 15:05:01 -07002451 get_monotonic_boottime(&tp);
Stephen Rothwell4a22f162013-04-30 15:27:37 -07002452 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
2453
2454 get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
2455
2456 info->procs = nr_threads;
2457
2458 si_meminfo(info);
2459 si_swapinfo(info);
2460
2461 /*
2462 * If the sum of all the available memory (i.e. ram + swap)
2463 * is less than can be stored in a 32 bit unsigned long then
2464 * we can be binary compatible with 2.2.x kernels. If not,
2465 * well, in that case 2.2.x was broken anyways...
2466 *
2467 * -Erik Andersen <andersee@debian.org>
2468 */
2469
2470 mem_total = info->totalram + info->totalswap;
2471 if (mem_total < info->totalram || mem_total < info->totalswap)
2472 goto out;
2473 bitcount = 0;
2474 mem_unit = info->mem_unit;
2475 while (mem_unit > 1) {
2476 bitcount++;
2477 mem_unit >>= 1;
2478 sav_total = mem_total;
2479 mem_total <<= 1;
2480 if (mem_total < sav_total)
2481 goto out;
2482 }
2483
2484 /*
2485 * If mem_total did not overflow, multiply all memory values by
2486 * info->mem_unit and set it to 1. This leaves things compatible
2487 * with 2.2.x, and also retains compatibility with earlier 2.4.x
2488 * kernels...
2489 */
2490
2491 info->mem_unit = 1;
2492 info->totalram <<= bitcount;
2493 info->freeram <<= bitcount;
2494 info->sharedram <<= bitcount;
2495 info->bufferram <<= bitcount;
2496 info->totalswap <<= bitcount;
2497 info->freeswap <<= bitcount;
2498 info->totalhigh <<= bitcount;
2499 info->freehigh <<= bitcount;
2500
2501out:
2502 return 0;
2503}
2504
2505SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
2506{
2507 struct sysinfo val;
2508
2509 do_sysinfo(&val);
2510
2511 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
2512 return -EFAULT;
2513
2514 return 0;
2515}
2516
2517#ifdef CONFIG_COMPAT
2518struct compat_sysinfo {
2519 s32 uptime;
2520 u32 loads[3];
2521 u32 totalram;
2522 u32 freeram;
2523 u32 sharedram;
2524 u32 bufferram;
2525 u32 totalswap;
2526 u32 freeswap;
2527 u16 procs;
2528 u16 pad;
2529 u32 totalhigh;
2530 u32 freehigh;
2531 u32 mem_unit;
2532 char _f[20-2*sizeof(u32)-sizeof(int)];
2533};
2534
2535COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
2536{
2537 struct sysinfo s;
2538
2539 do_sysinfo(&s);
2540
2541 /* Check to see if any memory value is too large for 32-bit and scale
2542 * down if needed
2543 */
Scotty Bauer0baae412014-10-09 15:30:26 -07002544 if (upper_32_bits(s.totalram) || upper_32_bits(s.totalswap)) {
Stephen Rothwell4a22f162013-04-30 15:27:37 -07002545 int bitcount = 0;
2546
2547 while (s.mem_unit < PAGE_SIZE) {
2548 s.mem_unit <<= 1;
2549 bitcount++;
2550 }
2551
2552 s.totalram >>= bitcount;
2553 s.freeram >>= bitcount;
2554 s.sharedram >>= bitcount;
2555 s.bufferram >>= bitcount;
2556 s.totalswap >>= bitcount;
2557 s.freeswap >>= bitcount;
2558 s.totalhigh >>= bitcount;
2559 s.freehigh >>= bitcount;
2560 }
2561
2562 if (!access_ok(VERIFY_WRITE, info, sizeof(struct compat_sysinfo)) ||
2563 __put_user(s.uptime, &info->uptime) ||
2564 __put_user(s.loads[0], &info->loads[0]) ||
2565 __put_user(s.loads[1], &info->loads[1]) ||
2566 __put_user(s.loads[2], &info->loads[2]) ||
2567 __put_user(s.totalram, &info->totalram) ||
2568 __put_user(s.freeram, &info->freeram) ||
2569 __put_user(s.sharedram, &info->sharedram) ||
2570 __put_user(s.bufferram, &info->bufferram) ||
2571 __put_user(s.totalswap, &info->totalswap) ||
2572 __put_user(s.freeswap, &info->freeswap) ||
2573 __put_user(s.procs, &info->procs) ||
2574 __put_user(s.totalhigh, &info->totalhigh) ||
2575 __put_user(s.freehigh, &info->freehigh) ||
2576 __put_user(s.mem_unit, &info->mem_unit))
2577 return -EFAULT;
2578
2579 return 0;
2580}
2581#endif /* CONFIG_COMPAT */