blob: 126b7c939d1f76fdb4c6fe60a79f71da873ab307 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/sys.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
Paul Gortmaker9984de12011-05-23 14:51:41 -04007#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/mm.h>
9#include <linux/utsname.h>
10#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/reboot.h>
12#include <linux/prctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/highuid.h>
14#include <linux/fs.h>
Paul Gortmaker74da1ff2011-05-26 12:48:41 -040015#include <linux/kmod.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020016#include <linux/perf_event.h>
Daniel Walker3e88c552007-05-10 22:22:53 -070017#include <linux/resource.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070018#include <linux/kernel.h>
19#include <linux/kexec.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>
Colin Cross6ebfe582013-06-26 17:26:01 -070045#include <linux/mm.h>
46#include <linux/mempolicy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <linux/compat.h>
49#include <linux/syscalls.h>
Keshavamurthy Anil S00d7c052005-12-12 00:37:33 -080050#include <linux/kprobes.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070051#include <linux/user_namespace.h>
Chen Gang7fe5e042013-02-21 16:43:06 -080052#include <linux/binfmts.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Stephen Rothwell4a22f162013-04-30 15:27:37 -070054#include <linux/sched.h>
55#include <linux/rcupdate.h>
56#include <linux/uidgid.h>
57#include <linux/cred.h>
58
Seiji Aguchi04c68622011-01-12 16:59:30 -080059#include <linux/kmsg_dump.h>
Andi Kleenbe274252011-08-19 16:15:10 -070060/* Move somewhere else to avoid recompiling? */
61#include <generated/utsrelease.h>
Seiji Aguchi04c68622011-01-12 16:59:30 -080062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <asm/uaccess.h>
64#include <asm/io.h>
65#include <asm/unistd.h>
66
67#ifndef SET_UNALIGN_CTL
68# define SET_UNALIGN_CTL(a,b) (-EINVAL)
69#endif
70#ifndef GET_UNALIGN_CTL
71# define GET_UNALIGN_CTL(a,b) (-EINVAL)
72#endif
73#ifndef SET_FPEMU_CTL
74# define SET_FPEMU_CTL(a,b) (-EINVAL)
75#endif
76#ifndef GET_FPEMU_CTL
77# define GET_FPEMU_CTL(a,b) (-EINVAL)
78#endif
79#ifndef SET_FPEXC_CTL
80# define SET_FPEXC_CTL(a,b) (-EINVAL)
81#endif
82#ifndef GET_FPEXC_CTL
83# define GET_FPEXC_CTL(a,b) (-EINVAL)
84#endif
Anton Blanchard651d7652006-06-07 16:10:19 +100085#ifndef GET_ENDIAN
86# define GET_ENDIAN(a,b) (-EINVAL)
87#endif
88#ifndef SET_ENDIAN
89# define SET_ENDIAN(a,b) (-EINVAL)
90#endif
Erik Bosman8fb402b2008-04-11 18:54:17 +020091#ifndef GET_TSC_CTL
92# define GET_TSC_CTL(a) (-EINVAL)
93#endif
94#ifndef SET_TSC_CTL
95# define SET_TSC_CTL(a) (-EINVAL)
96#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/*
99 * this is where the system-wide overflow UID and GID are defined, for
100 * architectures that now have 32-bit UID/GID but didn't in the past
101 */
102
103int overflowuid = DEFAULT_OVERFLOWUID;
104int overflowgid = DEFAULT_OVERFLOWGID;
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106EXPORT_SYMBOL(overflowuid);
107EXPORT_SYMBOL(overflowgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/*
110 * the same as above, but for filesystems which can only store a 16-bit
111 * UID and GID. as such, this is needed on all architectures
112 */
113
114int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
115int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
116
117EXPORT_SYMBOL(fs_overflowuid);
118EXPORT_SYMBOL(fs_overflowgid);
119
120/*
121 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
122 */
123
124int C_A_D = 1;
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700125struct pid *cad_pid;
126EXPORT_SYMBOL(cad_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/*
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700129 * If set, this is used for preparing the system to power off.
130 */
131
132void (*pm_power_off_prepare)(void);
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700133
David Howellsc69e8d92008-11-14 10:39:19 +1100134/*
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700135 * Returns true if current's euid is same as p's uid or euid,
136 * or has CAP_SYS_NICE to p's user_ns.
137 *
138 * Called with rcu_read_lock, creds are safe
139 */
140static bool set_one_prio_perm(struct task_struct *p)
141{
142 const struct cred *cred = current_cred(), *pcred = __task_cred(p);
143
Eric W. Biederman5af66202012-03-03 20:21:47 -0800144 if (uid_eq(pcred->uid, cred->euid) ||
145 uid_eq(pcred->euid, cred->euid))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700146 return true;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800147 if (ns_capable(pcred->user_ns, CAP_SYS_NICE))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700148 return true;
149 return false;
150}
151
152/*
David Howellsc69e8d92008-11-14 10:39:19 +1100153 * set the priority of a task
154 * - the caller must hold the RCU read lock
155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static int set_one_prio(struct task_struct *p, int niceval, int error)
157{
158 int no_nice;
159
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700160 if (!set_one_prio_perm(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 error = -EPERM;
162 goto out;
163 }
Matt Mackalle43379f2005-05-01 08:59:00 -0700164 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 error = -EACCES;
166 goto out;
167 }
168 no_nice = security_task_setnice(p, niceval);
169 if (no_nice) {
170 error = no_nice;
171 goto out;
172 }
173 if (error == -ESRCH)
174 error = 0;
175 set_user_nice(p, niceval);
176out:
177 return error;
178}
179
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100180SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 struct task_struct *g, *p;
183 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100184 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int error = -EINVAL;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800186 struct pid *pgrp;
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800187 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Daniel Walker3e88c552007-05-10 22:22:53 -0700189 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 goto out;
191
192 /* normalize: avoid signed division (rounding problems) */
193 error = -ESRCH;
194 if (niceval < -20)
195 niceval = -20;
196 if (niceval > 19)
197 niceval = 19;
198
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000199 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 read_lock(&tasklist_lock);
201 switch (which) {
202 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800203 if (who)
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700204 p = find_task_by_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800205 else
206 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (p)
208 error = set_one_prio(p, niceval, error);
209 break;
210 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800211 if (who)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700212 pgrp = find_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800213 else
214 pgrp = task_pgrp(current);
Ken Chen2d70b682008-08-20 14:09:17 -0700215 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 error = set_one_prio(p, niceval, error);
Ken Chen2d70b682008-08-20 14:09:17 -0700217 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 break;
219 case PRIO_USER:
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800220 uid = make_kuid(cred->user_ns, who);
Eric W. Biederman74ba5082012-03-03 18:58:11 -0800221 user = cred->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (!who)
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800223 uid = cred->uid;
224 else if (!uid_eq(uid, cred->uid) &&
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800225 !(user = find_user(uid)))
David Howells86a264a2008-11-14 10:39:18 +1100226 goto out_unlock; /* No processes for this user */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
H Hartley Sweetendfc6a732009-12-14 18:00:22 -0800228 do_each_thread(g, p) {
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800229 if (uid_eq(task_uid(p), uid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 error = set_one_prio(p, niceval, error);
H Hartley Sweetendfc6a732009-12-14 18:00:22 -0800231 } while_each_thread(g, p);
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800232 if (!uid_eq(uid, cred->uid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 free_uid(user); /* For find_user() */
234 break;
235 }
236out_unlock:
237 read_unlock(&tasklist_lock);
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000238 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239out:
240 return error;
241}
242
243/*
244 * Ugh. To avoid negative return values, "getpriority()" will
245 * not return the normal nice-value, but a negated value that
246 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
247 * to stay compatible.
248 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100249SYSCALL_DEFINE2(getpriority, int, which, int, who)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 struct task_struct *g, *p;
252 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100253 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 long niceval, retval = -ESRCH;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800255 struct pid *pgrp;
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800256 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Daniel Walker3e88c552007-05-10 22:22:53 -0700258 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return -EINVAL;
260
Tetsuo Handa70118832010-02-22 12:44:16 -0800261 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 read_lock(&tasklist_lock);
263 switch (which) {
264 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800265 if (who)
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700266 p = find_task_by_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800267 else
268 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (p) {
270 niceval = 20 - task_nice(p);
271 if (niceval > retval)
272 retval = niceval;
273 }
274 break;
275 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800276 if (who)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700277 pgrp = find_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800278 else
279 pgrp = task_pgrp(current);
Ken Chen2d70b682008-08-20 14:09:17 -0700280 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 niceval = 20 - task_nice(p);
282 if (niceval > retval)
283 retval = niceval;
Ken Chen2d70b682008-08-20 14:09:17 -0700284 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 break;
286 case PRIO_USER:
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800287 uid = make_kuid(cred->user_ns, who);
Eric W. Biederman74ba5082012-03-03 18:58:11 -0800288 user = cred->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (!who)
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800290 uid = cred->uid;
291 else if (!uid_eq(uid, cred->uid) &&
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800292 !(user = find_user(uid)))
David Howells86a264a2008-11-14 10:39:18 +1100293 goto out_unlock; /* No processes for this user */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
H Hartley Sweetendfc6a732009-12-14 18:00:22 -0800295 do_each_thread(g, p) {
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800296 if (uid_eq(task_uid(p), uid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 niceval = 20 - task_nice(p);
298 if (niceval > retval)
299 retval = niceval;
300 }
H Hartley Sweetendfc6a732009-12-14 18:00:22 -0800301 } while_each_thread(g, p);
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800302 if (!uid_eq(uid, cred->uid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 free_uid(user); /* for find_user() */
304 break;
305 }
306out_unlock:
307 read_unlock(&tasklist_lock);
Tetsuo Handa70118832010-02-22 12:44:16 -0800308 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 return retval;
311}
312
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700313/**
314 * emergency_restart - reboot the system
315 *
316 * Without shutting down any hardware or taking any locks
317 * reboot the system. This is called when we know we are in
318 * trouble so this is our best effort to reboot. This is
319 * safe to call in interrupt context.
320 */
Eric W. Biederman7c903472005-07-26 11:29:55 -0600321void emergency_restart(void)
322{
Seiji Aguchi04c68622011-01-12 16:59:30 -0800323 kmsg_dump(KMSG_DUMP_EMERG);
Eric W. Biederman7c903472005-07-26 11:29:55 -0600324 machine_emergency_restart();
325}
326EXPORT_SYMBOL_GPL(emergency_restart);
327
Huang Yingca195b72008-08-15 00:40:24 -0700328void kernel_restart_prepare(char *cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600329{
Alan Sterne041c682006-03-27 01:16:30 -0800330 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600331 system_state = SYSTEM_RESTART;
Kay Sieversb50fa7c2011-05-05 13:32:05 +0200332 usermodehelper_disable();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600333 device_shutdown();
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700334}
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800335
336/**
Amerigo Wangc5f41752011-07-25 17:13:10 -0700337 * register_reboot_notifier - Register function to be called at reboot time
338 * @nb: Info about notifier function to be called
339 *
340 * Registers a function with the list of functions
341 * to be called at reboot time.
342 *
343 * Currently always returns zero, as blocking_notifier_chain_register()
344 * always returns zero.
345 */
346int register_reboot_notifier(struct notifier_block *nb)
347{
348 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
349}
350EXPORT_SYMBOL(register_reboot_notifier);
351
352/**
353 * unregister_reboot_notifier - Unregister previously registered reboot notifier
354 * @nb: Hook to be unregistered
355 *
356 * Unregisters a previously registered reboot
357 * notifier function.
358 *
359 * Returns zero on success, or %-ENOENT on failure.
360 */
361int unregister_reboot_notifier(struct notifier_block *nb)
362{
363 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
364}
365EXPORT_SYMBOL(unregister_reboot_notifier);
366
Robin Holtcf7df372013-06-12 14:04:37 -0700367/* Add backwards compatibility for stable trees. */
368#ifndef PF_NO_SETAFFINITY
369#define PF_NO_SETAFFINITY PF_THREAD_BOUND
370#endif
371
372static void migrate_to_reboot_cpu(void)
373{
374 /* The boot cpu is always logical cpu 0 */
375 int cpu = 0;
376
377 cpu_hotplug_disable();
378
379 /* Make certain the cpu I'm about to reboot on is online */
380 if (!cpu_online(cpu))
381 cpu = cpumask_first(cpu_online_mask);
382
383 /* Prevent races with other tasks migrating this task */
384 current->flags |= PF_NO_SETAFFINITY;
385
386 /* Make certain I only run on the appropriate processor */
387 set_cpus_allowed_ptr(current, cpumask_of(cpu));
388}
389
Amerigo Wangc5f41752011-07-25 17:13:10 -0700390/**
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800391 * kernel_restart - reboot the system
392 * @cmd: pointer to buffer containing command to execute for restart
Randy Dunlapb8887e62005-11-07 01:01:07 -0800393 * or %NULL
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800394 *
395 * Shutdown everything and perform a clean reboot.
396 * This is not safe to call in interrupt context.
397 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700398void kernel_restart(char *cmd)
399{
400 kernel_restart_prepare(cmd);
Robin Holtcf7df372013-06-12 14:04:37 -0700401 migrate_to_reboot_cpu();
Huacai Chen6f389a82013-04-07 02:14:14 +0000402 syscore_shutdown();
Cal Peake756184b2006-09-30 23:27:24 -0700403 if (!cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600404 printk(KERN_EMERG "Restarting system.\n");
Cal Peake756184b2006-09-30 23:27:24 -0700405 else
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600406 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
Seiji Aguchi04c68622011-01-12 16:59:30 -0800407 kmsg_dump(KMSG_DUMP_RESTART);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600408 machine_restart(cmd);
409}
410EXPORT_SYMBOL_GPL(kernel_restart);
411
Adrian Bunk4ef72292008-02-04 22:30:06 -0800412static void kernel_shutdown_prepare(enum system_states state)
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500413{
Alan Sterne041c682006-03-27 01:16:30 -0800414 blocking_notifier_call_chain(&reboot_notifier_list,
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500415 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
416 system_state = state;
Kay Sieversb50fa7c2011-05-05 13:32:05 +0200417 usermodehelper_disable();
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500418 device_shutdown();
419}
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700420/**
421 * kernel_halt - halt the system
422 *
423 * Shutdown everything and perform a clean system halt.
424 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700425void kernel_halt(void)
426{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500427 kernel_shutdown_prepare(SYSTEM_HALT);
Robin Holtcf7df372013-06-12 14:04:37 -0700428 migrate_to_reboot_cpu();
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100429 syscore_shutdown();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600430 printk(KERN_EMERG "System halted.\n");
Seiji Aguchi04c68622011-01-12 16:59:30 -0800431 kmsg_dump(KMSG_DUMP_HALT);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600432 machine_halt();
433}
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500434
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600435EXPORT_SYMBOL_GPL(kernel_halt);
436
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700437/**
438 * kernel_power_off - power_off the system
439 *
440 * Shutdown everything and perform a clean system power_off.
441 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700442void kernel_power_off(void)
443{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500444 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700445 if (pm_power_off_prepare)
446 pm_power_off_prepare();
Robin Holtcf7df372013-06-12 14:04:37 -0700447 migrate_to_reboot_cpu();
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100448 syscore_shutdown();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600449 printk(KERN_EMERG "Power down.\n");
Seiji Aguchi04c68622011-01-12 16:59:30 -0800450 kmsg_dump(KMSG_DUMP_POWEROFF);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600451 machine_power_off();
452}
453EXPORT_SYMBOL_GPL(kernel_power_off);
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200454
455static DEFINE_MUTEX(reboot_mutex);
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*
458 * Reboot system call: for obvious reasons only root may call it,
459 * and even root needs to set up some magic numbers in the registers
460 * so that some mistake won't make this reboot the whole machine.
461 * You can also set the meaning of the ctrl-alt-del-key here.
462 *
463 * reboot doesn't sync: do that yourself before calling this.
464 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100465SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
466 void __user *, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Li Zefan923c7532012-12-27 11:39:12 +0800468 struct pid_namespace *pid_ns = task_active_pid_ns(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 char buffer[256];
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700470 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 /* We only trust the superuser with rebooting the system. */
Li Zefan923c7532012-12-27 11:39:12 +0800473 if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return -EPERM;
475
476 /* For safety, we require "magic" arguments. */
477 if (magic1 != LINUX_REBOOT_MAGIC1 ||
478 (magic2 != LINUX_REBOOT_MAGIC2 &&
479 magic2 != LINUX_REBOOT_MAGIC2A &&
480 magic2 != LINUX_REBOOT_MAGIC2B &&
481 magic2 != LINUX_REBOOT_MAGIC2C))
482 return -EINVAL;
483
Daniel Lezcanocf3f8922012-03-28 14:42:51 -0700484 /*
485 * If pid namespaces are enabled and the current task is in a child
486 * pid_namespace, the command is handled by reboot_pid_ns() which will
487 * call do_exit().
488 */
Li Zefan923c7532012-12-27 11:39:12 +0800489 ret = reboot_pid_ns(pid_ns, cmd);
Daniel Lezcanocf3f8922012-03-28 14:42:51 -0700490 if (ret)
491 return ret;
492
Eric W. Biederman5e382912006-01-08 01:03:46 -0800493 /* Instead of trying to make the power_off code look like
494 * halt when pm_power_off is not set do it the easy way.
495 */
496 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
497 cmd = LINUX_REBOOT_CMD_HALT;
498
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200499 mutex_lock(&reboot_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 switch (cmd) {
501 case LINUX_REBOOT_CMD_RESTART:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600502 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 break;
504
505 case LINUX_REBOOT_CMD_CAD_ON:
506 C_A_D = 1;
507 break;
508
509 case LINUX_REBOOT_CMD_CAD_OFF:
510 C_A_D = 0;
511 break;
512
513 case LINUX_REBOOT_CMD_HALT:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600514 kernel_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 do_exit(0);
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700516 panic("cannot halt");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 case LINUX_REBOOT_CMD_POWER_OFF:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600519 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 do_exit(0);
521 break;
522
523 case LINUX_REBOOT_CMD_RESTART2:
524 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200525 ret = -EFAULT;
526 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528 buffer[sizeof(buffer) - 1] = '\0';
529
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600530 kernel_restart(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 break;
532
Huang Ying3ab83522008-07-25 19:45:07 -0700533#ifdef CONFIG_KEXEC
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700534 case LINUX_REBOOT_CMD_KEXEC:
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700535 ret = kernel_kexec();
536 break;
Huang Ying3ab83522008-07-25 19:45:07 -0700537#endif
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600538
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200539#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 case LINUX_REBOOT_CMD_SW_SUSPEND:
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700541 ret = hibernate();
542 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543#endif
544
545 default:
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700546 ret = -EINVAL;
547 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200549 mutex_unlock(&reboot_mutex);
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700550 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
David Howells65f27f32006-11-22 14:55:48 +0000553static void deferred_cad(struct work_struct *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Eric W. Biedermanabcd9e52005-07-26 11:27:34 -0600555 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
558/*
559 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
560 * As it's called within an interrupt, it may NOT sync: the only choice
561 * is whether to reboot at once, or just ignore the ctrl-alt-del.
562 */
563void ctrl_alt_del(void)
564{
David Howells65f27f32006-11-22 14:55:48 +0000565 static DECLARE_WORK(cad_work, deferred_cad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 if (C_A_D)
568 schedule_work(&cad_work);
569 else
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700570 kill_cad_pid(SIGINT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/*
574 * Unprivileged users may change the real gid to the effective gid
575 * or vice versa. (BSD-style)
576 *
577 * If you set the real gid at all, or set the effective gid to a value not
578 * equal to the real gid, then the saved gid is set to the new effective gid.
579 *
580 * This makes it possible for a setgid program to completely drop its
581 * privileges, which is often a useful assertion to make when you are doing
582 * a security audit over a program.
583 *
584 * The general idea is that a program which uses just setregid() will be
585 * 100% compatible with BSD. A program which uses just setgid() will be
586 * 100% compatible with POSIX with saved IDs.
587 *
588 * SMP: There are not races, the GIDs are checked only by filesystem
589 * operations (as far as semantic preservation is concerned).
590 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100591SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800593 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100594 const struct cred *old;
595 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800597 kgid_t krgid, kegid;
598
599 krgid = make_kgid(ns, rgid);
600 kegid = make_kgid(ns, egid);
601
602 if ((rgid != (gid_t) -1) && !gid_valid(krgid))
603 return -EINVAL;
604 if ((egid != (gid_t) -1) && !gid_valid(kegid))
605 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
David Howellsd84f4f92008-11-14 10:39:23 +1100607 new = prepare_creds();
608 if (!new)
609 return -ENOMEM;
610 old = current_cred();
611
David Howellsd84f4f92008-11-14 10:39:23 +1100612 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (rgid != (gid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800614 if (gid_eq(old->gid, krgid) ||
615 gid_eq(old->egid, krgid) ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700616 nsown_capable(CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800617 new->gid = krgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 else
David Howellsd84f4f92008-11-14 10:39:23 +1100619 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621 if (egid != (gid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800622 if (gid_eq(old->gid, kegid) ||
623 gid_eq(old->egid, kegid) ||
624 gid_eq(old->sgid, kegid) ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700625 nsown_capable(CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800626 new->egid = kegid;
Cal Peake756184b2006-09-30 23:27:24 -0700627 else
David Howellsd84f4f92008-11-14 10:39:23 +1100628 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
David Howellsd84f4f92008-11-14 10:39:23 +1100630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (rgid != (gid_t) -1 ||
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800632 (egid != (gid_t) -1 && !gid_eq(kegid, old->gid)))
David Howellsd84f4f92008-11-14 10:39:23 +1100633 new->sgid = new->egid;
634 new->fsgid = new->egid;
635
636 return commit_creds(new);
637
638error:
639 abort_creds(new);
640 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643/*
644 * setgid() is implemented like SysV w/ SAVED_IDS
645 *
646 * SMP: Same implicit races as above.
647 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100648SYSCALL_DEFINE1(setgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800650 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100651 const struct cred *old;
652 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800654 kgid_t kgid;
655
656 kgid = make_kgid(ns, gid);
657 if (!gid_valid(kgid))
658 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
David Howellsd84f4f92008-11-14 10:39:23 +1100660 new = prepare_creds();
661 if (!new)
662 return -ENOMEM;
663 old = current_cred();
664
David Howellsd84f4f92008-11-14 10:39:23 +1100665 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700666 if (nsown_capable(CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800667 new->gid = new->egid = new->sgid = new->fsgid = kgid;
668 else if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->sgid))
669 new->egid = new->fsgid = kgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 else
David Howellsd84f4f92008-11-14 10:39:23 +1100671 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
David Howellsd84f4f92008-11-14 10:39:23 +1100673 return commit_creds(new);
674
675error:
676 abort_creds(new);
677 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
Dhaval Giani54e99122009-02-27 15:13:54 +0530679
David Howellsd84f4f92008-11-14 10:39:23 +1100680/*
681 * change the user struct in a credentials set to match the new UID
682 */
683static int set_user(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 struct user_struct *new_user;
686
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800687 new_user = alloc_uid(new->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (!new_user)
689 return -EAGAIN;
690
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400691 /*
692 * We don't fail in case of NPROC limit excess here because too many
693 * poorly written programs don't check set*uid() return code, assuming
694 * it never fails if called by root. We may still enforce NPROC limit
695 * for programs doing set*uid()+execve() by harmlessly deferring the
696 * failure to the execve() stage.
697 */
Jiri Slaby78d7d402010-03-05 13:42:54 -0800698 if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400699 new_user != INIT_USER)
700 current->flags |= PF_NPROC_EXCEEDED;
701 else
702 current->flags &= ~PF_NPROC_EXCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
David Howellsd84f4f92008-11-14 10:39:23 +1100704 free_uid(new->user);
705 new->user = new_user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return 0;
707}
708
709/*
710 * Unprivileged users may change the real uid to the effective uid
711 * or vice versa. (BSD-style)
712 *
713 * If you set the real uid at all, or set the effective uid to a value not
714 * equal to the real uid, then the saved uid is set to the new effective uid.
715 *
716 * This makes it possible for a setuid program to completely drop its
717 * privileges, which is often a useful assertion to make when you are doing
718 * a security audit over a program.
719 *
720 * The general idea is that a program which uses just setreuid() will be
721 * 100% compatible with BSD. A program which uses just setuid() will be
722 * 100% compatible with POSIX with saved IDs.
723 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100724SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800726 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100727 const struct cred *old;
728 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800730 kuid_t kruid, keuid;
731
732 kruid = make_kuid(ns, ruid);
733 keuid = make_kuid(ns, euid);
734
735 if ((ruid != (uid_t) -1) && !uid_valid(kruid))
736 return -EINVAL;
737 if ((euid != (uid_t) -1) && !uid_valid(keuid))
738 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
David Howellsd84f4f92008-11-14 10:39:23 +1100740 new = prepare_creds();
741 if (!new)
742 return -ENOMEM;
743 old = current_cred();
744
David Howellsd84f4f92008-11-14 10:39:23 +1100745 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (ruid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800747 new->uid = kruid;
748 if (!uid_eq(old->uid, kruid) &&
749 !uid_eq(old->euid, kruid) &&
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700750 !nsown_capable(CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100751 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753
754 if (euid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800755 new->euid = keuid;
756 if (!uid_eq(old->uid, keuid) &&
757 !uid_eq(old->euid, keuid) &&
758 !uid_eq(old->suid, keuid) &&
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700759 !nsown_capable(CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100760 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800763 if (!uid_eq(new->uid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530764 retval = set_user(new);
765 if (retval < 0)
766 goto error;
767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (ruid != (uid_t) -1 ||
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800769 (euid != (uid_t) -1 && !uid_eq(keuid, old->uid)))
David Howellsd84f4f92008-11-14 10:39:23 +1100770 new->suid = new->euid;
771 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
David Howellsd84f4f92008-11-14 10:39:23 +1100773 retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
774 if (retval < 0)
775 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
David Howellsd84f4f92008-11-14 10:39:23 +1100777 return commit_creds(new);
778
779error:
780 abort_creds(new);
781 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784/*
785 * setuid() is implemented like SysV with SAVED_IDS
786 *
787 * Note that SAVED_ID's is deficient in that a setuid root program
788 * like sendmail, for example, cannot set its uid to be a normal
789 * user and then switch back, because if you're root, setuid() sets
790 * the saved uid too. If you don't like this, blame the bright people
791 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
792 * will allow a root program to temporarily drop privileges and be able to
793 * regain them by swapping the real and effective uid.
794 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100795SYSCALL_DEFINE1(setuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800797 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100798 const struct cred *old;
799 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800801 kuid_t kuid;
802
803 kuid = make_kuid(ns, uid);
804 if (!uid_valid(kuid))
805 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
David Howellsd84f4f92008-11-14 10:39:23 +1100807 new = prepare_creds();
808 if (!new)
809 return -ENOMEM;
810 old = current_cred();
811
David Howellsd84f4f92008-11-14 10:39:23 +1100812 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700813 if (nsown_capable(CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800814 new->suid = new->uid = kuid;
815 if (!uid_eq(kuid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530816 retval = set_user(new);
817 if (retval < 0)
818 goto error;
David Howellsd84f4f92008-11-14 10:39:23 +1100819 }
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800820 } else if (!uid_eq(kuid, old->uid) && !uid_eq(kuid, new->suid)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100821 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800824 new->fsuid = new->euid = kuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
David Howellsd84f4f92008-11-14 10:39:23 +1100826 retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
827 if (retval < 0)
828 goto error;
829
830 return commit_creds(new);
831
832error:
833 abort_creds(new);
834 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
837
838/*
839 * This function implements a generic ability to update ruid, euid,
840 * and suid. This allows you to implement the 4.4 compatible seteuid().
841 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100842SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800844 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100845 const struct cred *old;
846 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800848 kuid_t kruid, keuid, ksuid;
849
850 kruid = make_kuid(ns, ruid);
851 keuid = make_kuid(ns, euid);
852 ksuid = make_kuid(ns, suid);
853
854 if ((ruid != (uid_t) -1) && !uid_valid(kruid))
855 return -EINVAL;
856
857 if ((euid != (uid_t) -1) && !uid_valid(keuid))
858 return -EINVAL;
859
860 if ((suid != (uid_t) -1) && !uid_valid(ksuid))
861 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
David Howellsd84f4f92008-11-14 10:39:23 +1100863 new = prepare_creds();
864 if (!new)
865 return -ENOMEM;
866
David Howellsd84f4f92008-11-14 10:39:23 +1100867 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
David Howellsd84f4f92008-11-14 10:39:23 +1100869 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700870 if (!nsown_capable(CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800871 if (ruid != (uid_t) -1 && !uid_eq(kruid, old->uid) &&
872 !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100873 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800874 if (euid != (uid_t) -1 && !uid_eq(keuid, old->uid) &&
875 !uid_eq(keuid, old->euid) && !uid_eq(keuid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100876 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800877 if (suid != (uid_t) -1 && !uid_eq(ksuid, old->uid) &&
878 !uid_eq(ksuid, old->euid) && !uid_eq(ksuid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100879 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
David Howellsd84f4f92008-11-14 10:39:23 +1100881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (ruid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800883 new->uid = kruid;
884 if (!uid_eq(kruid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530885 retval = set_user(new);
886 if (retval < 0)
887 goto error;
888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
David Howellsd84f4f92008-11-14 10:39:23 +1100890 if (euid != (uid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800891 new->euid = keuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (suid != (uid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800893 new->suid = ksuid;
David Howellsd84f4f92008-11-14 10:39:23 +1100894 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
David Howellsd84f4f92008-11-14 10:39:23 +1100896 retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
897 if (retval < 0)
898 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
David Howellsd84f4f92008-11-14 10:39:23 +1100900 return commit_creds(new);
901
902error:
903 abort_creds(new);
904 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800907SYSCALL_DEFINE3(getresuid, uid_t __user *, ruidp, uid_t __user *, euidp, uid_t __user *, suidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
David Howells86a264a2008-11-14 10:39:18 +1100909 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800911 uid_t ruid, euid, suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800913 ruid = from_kuid_munged(cred->user_ns, cred->uid);
914 euid = from_kuid_munged(cred->user_ns, cred->euid);
915 suid = from_kuid_munged(cred->user_ns, cred->suid);
916
917 if (!(retval = put_user(ruid, ruidp)) &&
918 !(retval = put_user(euid, euidp)))
919 retval = put_user(suid, suidp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 return retval;
922}
923
924/*
925 * Same as above, but for rgid, egid, sgid.
926 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100927SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800929 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100930 const struct cred *old;
931 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800933 kgid_t krgid, kegid, ksgid;
934
935 krgid = make_kgid(ns, rgid);
936 kegid = make_kgid(ns, egid);
937 ksgid = make_kgid(ns, sgid);
938
939 if ((rgid != (gid_t) -1) && !gid_valid(krgid))
940 return -EINVAL;
941 if ((egid != (gid_t) -1) && !gid_valid(kegid))
942 return -EINVAL;
943 if ((sgid != (gid_t) -1) && !gid_valid(ksgid))
944 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
David Howellsd84f4f92008-11-14 10:39:23 +1100946 new = prepare_creds();
947 if (!new)
948 return -ENOMEM;
949 old = current_cred();
950
David Howellsd84f4f92008-11-14 10:39:23 +1100951 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700952 if (!nsown_capable(CAP_SETGID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800953 if (rgid != (gid_t) -1 && !gid_eq(krgid, old->gid) &&
954 !gid_eq(krgid, old->egid) && !gid_eq(krgid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100955 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800956 if (egid != (gid_t) -1 && !gid_eq(kegid, old->gid) &&
957 !gid_eq(kegid, old->egid) && !gid_eq(kegid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100958 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800959 if (sgid != (gid_t) -1 && !gid_eq(ksgid, old->gid) &&
960 !gid_eq(ksgid, old->egid) && !gid_eq(ksgid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100961 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
David Howellsd84f4f92008-11-14 10:39:23 +1100964 if (rgid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800965 new->gid = krgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100966 if (egid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800967 new->egid = kegid;
David Howellsd84f4f92008-11-14 10:39:23 +1100968 if (sgid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800969 new->sgid = ksgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100970 new->fsgid = new->egid;
971
972 return commit_creds(new);
973
974error:
975 abort_creds(new);
976 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800979SYSCALL_DEFINE3(getresgid, gid_t __user *, rgidp, gid_t __user *, egidp, gid_t __user *, sgidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
David Howells86a264a2008-11-14 10:39:18 +1100981 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800983 gid_t rgid, egid, sgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800985 rgid = from_kgid_munged(cred->user_ns, cred->gid);
986 egid = from_kgid_munged(cred->user_ns, cred->egid);
987 sgid = from_kgid_munged(cred->user_ns, cred->sgid);
988
989 if (!(retval = put_user(rgid, rgidp)) &&
990 !(retval = put_user(egid, egidp)))
991 retval = put_user(sgid, sgidp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 return retval;
994}
995
996
997/*
998 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
999 * is used for "access()" and for the NFS daemon (letting nfsd stay at
1000 * whatever uid it wants to). It normally shadows "euid", except when
1001 * explicitly set by setfsuid() or for access..
1002 */
Heiko Carstensae1251a2009-01-14 14:14:05 +01001003SYSCALL_DEFINE1(setfsuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
David Howellsd84f4f92008-11-14 10:39:23 +11001005 const struct cred *old;
1006 struct cred *new;
1007 uid_t old_fsuid;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -08001008 kuid_t kuid;
1009
1010 old = current_cred();
1011 old_fsuid = from_kuid_munged(old->user_ns, old->fsuid);
1012
1013 kuid = make_kuid(old->user_ns, uid);
1014 if (!uid_valid(kuid))
1015 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
David Howellsd84f4f92008-11-14 10:39:23 +11001017 new = prepare_creds();
1018 if (!new)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -08001019 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Eric W. Biedermana29c33f2012-02-07 18:51:01 -08001021 if (uid_eq(kuid, old->uid) || uid_eq(kuid, old->euid) ||
1022 uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001023 nsown_capable(CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -08001024 if (!uid_eq(kuid, old->fsuid)) {
1025 new->fsuid = kuid;
David Howellsd84f4f92008-11-14 10:39:23 +11001026 if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
1027 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030
David Howellsd84f4f92008-11-14 10:39:23 +11001031 abort_creds(new);
1032 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
David Howellsd84f4f92008-11-14 10:39:23 +11001034change_okay:
1035 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return old_fsuid;
1037}
1038
1039/*
John Anthony Kazos Jrf42df9e2007-05-09 08:23:08 +02001040 * Samma på svenska..
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 */
Heiko Carstensae1251a2009-01-14 14:14:05 +01001042SYSCALL_DEFINE1(setfsgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
David Howellsd84f4f92008-11-14 10:39:23 +11001044 const struct cred *old;
1045 struct cred *new;
1046 gid_t old_fsgid;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -08001047 kgid_t kgid;
1048
1049 old = current_cred();
1050 old_fsgid = from_kgid_munged(old->user_ns, old->fsgid);
1051
1052 kgid = make_kgid(old->user_ns, gid);
1053 if (!gid_valid(kgid))
1054 return old_fsgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
David Howellsd84f4f92008-11-14 10:39:23 +11001056 new = prepare_creds();
1057 if (!new)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -08001058 return old_fsgid;
David Howellsd84f4f92008-11-14 10:39:23 +11001059
Eric W. Biedermana29c33f2012-02-07 18:51:01 -08001060 if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->egid) ||
1061 gid_eq(kgid, old->sgid) || gid_eq(kgid, old->fsgid) ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001062 nsown_capable(CAP_SETGID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -08001063 if (!gid_eq(kgid, old->fsgid)) {
1064 new->fsgid = kgid;
David Howellsd84f4f92008-11-14 10:39:23 +11001065 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
David Howellsd84f4f92008-11-14 10:39:23 +11001068
David Howellsd84f4f92008-11-14 10:39:23 +11001069 abort_creds(new);
1070 return old_fsgid;
1071
1072change_okay:
1073 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return old_fsgid;
1075}
1076
Stephen Rothwell4a22f162013-04-30 15:27:37 -07001077/**
1078 * sys_getpid - return the thread group id of the current process
1079 *
1080 * Note, despite the name, this returns the tgid not the pid. The tgid and
1081 * the pid are identical unless CLONE_THREAD was specified on clone() in
1082 * which case the tgid is the same in all threads of the same group.
1083 *
1084 * This is SMP safe as current->tgid does not change.
1085 */
1086SYSCALL_DEFINE0(getpid)
1087{
1088 return task_tgid_vnr(current);
1089}
1090
1091/* Thread ID - the internal kernel "pid" */
1092SYSCALL_DEFINE0(gettid)
1093{
1094 return task_pid_vnr(current);
1095}
1096
1097/*
1098 * Accessing ->real_parent is not SMP-safe, it could
1099 * change from under us. However, we can use a stale
1100 * value of ->real_parent under rcu_read_lock(), see
1101 * release_task()->call_rcu(delayed_put_task_struct).
1102 */
1103SYSCALL_DEFINE0(getppid)
1104{
1105 int pid;
1106
1107 rcu_read_lock();
1108 pid = task_tgid_vnr(rcu_dereference(current->real_parent));
1109 rcu_read_unlock();
1110
1111 return pid;
1112}
1113
1114SYSCALL_DEFINE0(getuid)
1115{
1116 /* Only we change this so SMP safe */
1117 return from_kuid_munged(current_user_ns(), current_uid());
1118}
1119
1120SYSCALL_DEFINE0(geteuid)
1121{
1122 /* Only we change this so SMP safe */
1123 return from_kuid_munged(current_user_ns(), current_euid());
1124}
1125
1126SYSCALL_DEFINE0(getgid)
1127{
1128 /* Only we change this so SMP safe */
1129 return from_kgid_munged(current_user_ns(), current_gid());
1130}
1131
1132SYSCALL_DEFINE0(getegid)
1133{
1134 /* Only we change this so SMP safe */
1135 return from_kgid_munged(current_user_ns(), current_egid());
1136}
1137
Frank Mayharf06febc2008-09-12 09:54:39 -07001138void do_sys_times(struct tms *tms)
1139{
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09001140 cputime_t tgutime, tgstime, cutime, cstime;
Frank Mayharf06febc2008-09-12 09:54:39 -07001141
Oleg Nesterov2b5fe6d2008-11-17 15:40:08 +01001142 spin_lock_irq(&current->sighand->siglock);
Frederic Weisbeckere80d0a12012-11-21 16:26:44 +01001143 thread_group_cputime_adjusted(current, &tgutime, &tgstime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001144 cutime = current->signal->cutime;
1145 cstime = current->signal->cstime;
1146 spin_unlock_irq(&current->sighand->siglock);
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09001147 tms->tms_utime = cputime_to_clock_t(tgutime);
1148 tms->tms_stime = cputime_to_clock_t(tgstime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001149 tms->tms_cutime = cputime_to_clock_t(cutime);
1150 tms->tms_cstime = cputime_to_clock_t(cstime);
1151}
1152
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001153SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (tbuf) {
1156 struct tms tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Frank Mayharf06febc2008-09-12 09:54:39 -07001158 do_sys_times(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1160 return -EFAULT;
1161 }
Paul Mackerrase3d5a272009-01-06 14:41:02 -08001162 force_successful_syscall_return();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1164}
1165
1166/*
1167 * This needs some heavy checking ...
1168 * I just haven't the stomach for it. I also don't fully
1169 * understand sessions/pgrp etc. Let somebody who does explain it.
1170 *
1171 * OK, I think I have the protection semantics right.... this is really
1172 * only important on a multi-user system anyway, to make sure one user
1173 * can't send a signal to a process owned by another. -TYT, 12/12/91
1174 *
1175 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1176 * LBT 04.03.94
1177 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +01001178SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 struct task_struct *p;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001181 struct task_struct *group_leader = current->group_leader;
Oleg Nesterov4e021302008-02-08 04:19:08 -08001182 struct pid *pgrp;
1183 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 if (!pid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001186 pid = task_pid_vnr(group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (!pgid)
1188 pgid = pid;
1189 if (pgid < 0)
1190 return -EINVAL;
Paul E. McKenney950eaac2010-08-31 17:00:18 -07001191 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 /* From this point forward we keep holding onto the tasklist lock
1194 * so that our parent does not change from under us. -DaveM
1195 */
1196 write_lock_irq(&tasklist_lock);
1197
1198 err = -ESRCH;
Oleg Nesterov4e021302008-02-08 04:19:08 -08001199 p = find_task_by_vpid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (!p)
1201 goto out;
1202
1203 err = -EINVAL;
1204 if (!thread_group_leader(p))
1205 goto out;
1206
Oleg Nesterov4e021302008-02-08 04:19:08 -08001207 if (same_thread_group(p->real_parent, group_leader)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 err = -EPERM;
Eric W. Biederman41487c62007-02-12 00:53:01 -08001209 if (task_session(p) != task_session(group_leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 goto out;
1211 err = -EACCES;
1212 if (p->did_exec)
1213 goto out;
1214 } else {
1215 err = -ESRCH;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001216 if (p != group_leader)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 goto out;
1218 }
1219
1220 err = -EPERM;
1221 if (p->signal->leader)
1222 goto out;
1223
Oleg Nesterov4e021302008-02-08 04:19:08 -08001224 pgrp = task_pid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (pgid != pid) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001226 struct task_struct *g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Oleg Nesterov4e021302008-02-08 04:19:08 -08001228 pgrp = find_vpid(pgid);
1229 g = pid_task(pgrp, PIDTYPE_PGID);
Eric W. Biederman41487c62007-02-12 00:53:01 -08001230 if (!g || task_session(g) != task_session(group_leader))
Oleg Nesterovf020bc42006-12-08 02:38:02 -08001231 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 err = security_task_setpgid(p, pgid);
1235 if (err)
1236 goto out;
1237
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07001238 if (task_pgrp(p) != pgrp)
Oleg Nesterov83beaf32008-04-30 00:54:27 -07001239 change_pid(p, PIDTYPE_PGID, pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 err = 0;
1242out:
1243 /* All paths lead to here, thus we are safe. -DaveM */
1244 write_unlock_irq(&tasklist_lock);
Paul E. McKenney950eaac2010-08-31 17:00:18 -07001245 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return err;
1247}
1248
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001249SYSCALL_DEFINE1(getpgid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001251 struct task_struct *p;
1252 struct pid *grp;
1253 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001255 rcu_read_lock();
1256 if (!pid)
1257 grp = task_pgrp(current);
1258 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 retval = -ESRCH;
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001260 p = find_task_by_vpid(pid);
1261 if (!p)
1262 goto out;
1263 grp = task_pgrp(p);
1264 if (!grp)
1265 goto out;
1266
1267 retval = security_task_getpgid(p);
1268 if (retval)
1269 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001271 retval = pid_vnr(grp);
1272out:
1273 rcu_read_unlock();
1274 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
1277#ifdef __ARCH_WANT_SYS_GETPGRP
1278
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001279SYSCALL_DEFINE0(getpgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001281 return sys_getpgid(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
1284#endif
1285
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001286SYSCALL_DEFINE1(getsid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001288 struct task_struct *p;
1289 struct pid *sid;
1290 int retval;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001291
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001292 rcu_read_lock();
1293 if (!pid)
1294 sid = task_session(current);
1295 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 retval = -ESRCH;
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001297 p = find_task_by_vpid(pid);
1298 if (!p)
1299 goto out;
1300 sid = task_session(p);
1301 if (!sid)
1302 goto out;
1303
1304 retval = security_task_getsid(p);
1305 if (retval)
1306 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001308 retval = pid_vnr(sid);
1309out:
1310 rcu_read_unlock();
1311 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
Heiko Carstensb290ebe2009-01-14 14:14:06 +01001314SYSCALL_DEFINE0(setsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Oren Laadane19f2472006-01-08 01:03:58 -08001316 struct task_struct *group_leader = current->group_leader;
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001317 struct pid *sid = task_pid(group_leader);
1318 pid_t session = pid_vnr(sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 int err = -EPERM;
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 write_lock_irq(&tasklist_lock);
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001322 /* Fail if I am already a session leader */
1323 if (group_leader->signal->leader)
1324 goto out;
1325
Oleg Nesterov430c6232008-02-08 04:19:11 -08001326 /* Fail if a process group id already exists that equals the
1327 * proposed session id.
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001328 */
Oleg Nesterov6806aac2008-02-08 04:19:12 -08001329 if (pid_task(sid, PIDTYPE_PGID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 goto out;
1331
Oren Laadane19f2472006-01-08 01:03:58 -08001332 group_leader->signal->leader = 1;
Oleg Nesterov8520d7c2008-02-08 04:19:09 -08001333 __set_special_pids(sid);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001334
Alan Cox9c9f4de2008-10-13 10:37:26 +01001335 proc_clear_tty(group_leader);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001336
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001337 err = session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338out:
1339 write_unlock_irq(&tasklist_lock);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001340 if (err > 0) {
Christian Borntraeger0d0df592009-10-26 16:49:34 -07001341 proc_sid_connector(group_leader);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001342 sched_autogroup_create_attach(group_leader);
1343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return err;
1345}
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347DECLARE_RWSEM(uts_sem);
1348
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001349#ifdef COMPAT_UTS_MACHINE
1350#define override_architecture(name) \
Andreas Schwab46da2762010-04-23 13:17:44 -04001351 (personality(current->personality) == PER_LINUX32 && \
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001352 copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1353 sizeof(COMPAT_UTS_MACHINE)))
1354#else
1355#define override_architecture(name) 0
1356#endif
1357
Andi Kleenbe274252011-08-19 16:15:10 -07001358/*
1359 * Work around broken programs that cannot handle "Linux 3.0".
1360 * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40
1361 */
Kees Cook2702b152012-10-19 13:56:51 -07001362static int override_release(char __user *release, size_t len)
Andi Kleenbe274252011-08-19 16:15:10 -07001363{
1364 int ret = 0;
Andi Kleenbe274252011-08-19 16:15:10 -07001365
1366 if (current->personality & UNAME26) {
Kees Cook2702b152012-10-19 13:56:51 -07001367 const char *rest = UTS_RELEASE;
1368 char buf[65] = { 0 };
Andi Kleenbe274252011-08-19 16:15:10 -07001369 int ndots = 0;
1370 unsigned v;
Kees Cook2702b152012-10-19 13:56:51 -07001371 size_t copy;
Andi Kleenbe274252011-08-19 16:15:10 -07001372
1373 while (*rest) {
1374 if (*rest == '.' && ++ndots >= 3)
1375 break;
1376 if (!isdigit(*rest) && *rest != '.')
1377 break;
1378 rest++;
1379 }
1380 v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 40;
Kees Cook31fd84b2012-10-19 18:45:53 -07001381 copy = clamp_t(size_t, len, 1, sizeof(buf));
Kees Cook2702b152012-10-19 13:56:51 -07001382 copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
1383 ret = copy_to_user(release, buf, copy + 1);
Andi Kleenbe274252011-08-19 16:15:10 -07001384 }
1385 return ret;
1386}
1387
Heiko Carstense48fbb62009-01-14 14:14:26 +01001388SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 int errno = 0;
1391
1392 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001393 if (copy_to_user(name, utsname(), sizeof *name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 errno = -EFAULT;
1395 up_read(&uts_sem);
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001396
Andi Kleenbe274252011-08-19 16:15:10 -07001397 if (!errno && override_release(name->release, sizeof(name->release)))
1398 errno = -EFAULT;
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001399 if (!errno && override_architecture(name))
1400 errno = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return errno;
1402}
1403
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001404#ifdef __ARCH_WANT_SYS_OLD_UNAME
1405/*
1406 * Old cruft
1407 */
1408SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
1409{
1410 int error = 0;
1411
1412 if (!name)
1413 return -EFAULT;
1414
1415 down_read(&uts_sem);
1416 if (copy_to_user(name, utsname(), sizeof(*name)))
1417 error = -EFAULT;
1418 up_read(&uts_sem);
1419
Andi Kleenbe274252011-08-19 16:15:10 -07001420 if (!error && override_release(name->release, sizeof(name->release)))
1421 error = -EFAULT;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001422 if (!error && override_architecture(name))
1423 error = -EFAULT;
1424 return error;
1425}
1426
1427SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
1428{
1429 int error;
1430
1431 if (!name)
1432 return -EFAULT;
1433 if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
1434 return -EFAULT;
1435
1436 down_read(&uts_sem);
1437 error = __copy_to_user(&name->sysname, &utsname()->sysname,
1438 __OLD_UTS_LEN);
1439 error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
1440 error |= __copy_to_user(&name->nodename, &utsname()->nodename,
1441 __OLD_UTS_LEN);
1442 error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
1443 error |= __copy_to_user(&name->release, &utsname()->release,
1444 __OLD_UTS_LEN);
1445 error |= __put_user(0, name->release + __OLD_UTS_LEN);
1446 error |= __copy_to_user(&name->version, &utsname()->version,
1447 __OLD_UTS_LEN);
1448 error |= __put_user(0, name->version + __OLD_UTS_LEN);
1449 error |= __copy_to_user(&name->machine, &utsname()->machine,
1450 __OLD_UTS_LEN);
1451 error |= __put_user(0, name->machine + __OLD_UTS_LEN);
1452 up_read(&uts_sem);
1453
1454 if (!error && override_architecture(name))
1455 error = -EFAULT;
Andi Kleenbe274252011-08-19 16:15:10 -07001456 if (!error && override_release(name->release, sizeof(name->release)))
1457 error = -EFAULT;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001458 return error ? -EFAULT : 0;
1459}
1460#endif
1461
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001462SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 int errno;
1465 char tmp[__NEW_UTS_LEN];
1466
Serge E. Hallynbb96a6f2011-03-23 16:43:18 -07001467 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (len < 0 || len > __NEW_UTS_LEN)
1471 return -EINVAL;
1472 down_write(&uts_sem);
1473 errno = -EFAULT;
1474 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001475 struct new_utsname *u = utsname();
1476
1477 memcpy(u->nodename, tmp, len);
1478 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 errno = 0;
Sasikantha babu499eea62012-05-31 16:26:07 -07001480 uts_proc_notify(UTS_PROC_HOSTNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482 up_write(&uts_sem);
1483 return errno;
1484}
1485
1486#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1487
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001488SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
1490 int i, errno;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001491 struct new_utsname *u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 if (len < 0)
1494 return -EINVAL;
1495 down_read(&uts_sem);
Andrew Morton9679e4d2008-10-15 22:01:51 -07001496 u = utsname();
1497 i = 1 + strlen(u->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (i > len)
1499 i = len;
1500 errno = 0;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001501 if (copy_to_user(name, u->nodename, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 errno = -EFAULT;
1503 up_read(&uts_sem);
1504 return errno;
1505}
1506
1507#endif
1508
1509/*
1510 * Only setdomainname; getdomainname can be implemented by calling
1511 * uname()
1512 */
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001513SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 int errno;
1516 char tmp[__NEW_UTS_LEN];
1517
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001518 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return -EPERM;
1520 if (len < 0 || len > __NEW_UTS_LEN)
1521 return -EINVAL;
1522
1523 down_write(&uts_sem);
1524 errno = -EFAULT;
1525 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001526 struct new_utsname *u = utsname();
1527
1528 memcpy(u->domainname, tmp, len);
1529 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 errno = 0;
Sasikantha babu499eea62012-05-31 16:26:07 -07001531 uts_proc_notify(UTS_PROC_DOMAINNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533 up_write(&uts_sem);
1534 return errno;
1535}
1536
Heiko Carstense48fbb62009-01-14 14:14:26 +01001537SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Jiri Slabyb9518342010-05-04 11:28:25 +02001539 struct rlimit value;
1540 int ret;
1541
1542 ret = do_prlimit(current, resource, NULL, &value);
1543 if (!ret)
1544 ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1545
1546 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
1549#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1550
1551/*
1552 * Back compatibility for getrlimit. Needed for some apps.
1553 */
1554
Heiko Carstense48fbb62009-01-14 14:14:26 +01001555SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1556 struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
1558 struct rlimit x;
1559 if (resource >= RLIM_NLIMITS)
1560 return -EINVAL;
1561
1562 task_lock(current->group_leader);
1563 x = current->signal->rlim[resource];
1564 task_unlock(current->group_leader);
Cal Peake756184b2006-09-30 23:27:24 -07001565 if (x.rlim_cur > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 x.rlim_cur = 0x7FFFFFFF;
Cal Peake756184b2006-09-30 23:27:24 -07001567 if (x.rlim_max > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 x.rlim_max = 0x7FFFFFFF;
1569 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1570}
1571
1572#endif
1573
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001574static inline bool rlim64_is_infinity(__u64 rlim64)
1575{
1576#if BITS_PER_LONG < 64
1577 return rlim64 >= ULONG_MAX;
1578#else
1579 return rlim64 == RLIM64_INFINITY;
1580#endif
1581}
1582
1583static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1584{
1585 if (rlim->rlim_cur == RLIM_INFINITY)
1586 rlim64->rlim_cur = RLIM64_INFINITY;
1587 else
1588 rlim64->rlim_cur = rlim->rlim_cur;
1589 if (rlim->rlim_max == RLIM_INFINITY)
1590 rlim64->rlim_max = RLIM64_INFINITY;
1591 else
1592 rlim64->rlim_max = rlim->rlim_max;
1593}
1594
1595static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1596{
1597 if (rlim64_is_infinity(rlim64->rlim_cur))
1598 rlim->rlim_cur = RLIM_INFINITY;
1599 else
1600 rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1601 if (rlim64_is_infinity(rlim64->rlim_max))
1602 rlim->rlim_max = RLIM_INFINITY;
1603 else
1604 rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1605}
1606
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001607/* make sure you are allowed to change @tsk limits before calling this */
Jiri Slaby5b415352010-03-24 16:11:29 +01001608int do_prlimit(struct task_struct *tsk, unsigned int resource,
1609 struct rlimit *new_rlim, struct rlimit *old_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Jiri Slaby5b415352010-03-24 16:11:29 +01001611 struct rlimit *rlim;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001612 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 if (resource >= RLIM_NLIMITS)
1615 return -EINVAL;
Jiri Slaby5b415352010-03-24 16:11:29 +01001616 if (new_rlim) {
1617 if (new_rlim->rlim_cur > new_rlim->rlim_max)
1618 return -EINVAL;
1619 if (resource == RLIMIT_NOFILE &&
1620 new_rlim->rlim_max > sysctl_nr_open)
1621 return -EPERM;
1622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001624 /* protect tsk->signal and tsk->sighand from disappearing */
1625 read_lock(&tasklist_lock);
1626 if (!tsk->sighand) {
1627 retval = -ESRCH;
1628 goto out;
1629 }
1630
Jiri Slaby5b415352010-03-24 16:11:29 +01001631 rlim = tsk->signal->rlim + resource;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001632 task_lock(tsk->group_leader);
Jiri Slaby5b415352010-03-24 16:11:29 +01001633 if (new_rlim) {
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001634 /* Keep the capable check against init_user_ns until
1635 cgroups can contain all limits */
Jiri Slaby5b415352010-03-24 16:11:29 +01001636 if (new_rlim->rlim_max > rlim->rlim_max &&
1637 !capable(CAP_SYS_RESOURCE))
1638 retval = -EPERM;
1639 if (!retval)
1640 retval = security_task_setrlimit(tsk->group_leader,
1641 resource, new_rlim);
1642 if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
1643 /*
1644 * The caller is asking for an immediate RLIMIT_CPU
1645 * expiry. But we use the zero value to mean "it was
1646 * never set". So let's cheat and make it one second
1647 * instead
1648 */
1649 new_rlim->rlim_cur = 1;
1650 }
Tom Alsberg9926e4c2007-05-08 00:30:31 -07001651 }
Jiri Slaby5b415352010-03-24 16:11:29 +01001652 if (!retval) {
1653 if (old_rlim)
1654 *old_rlim = *rlim;
1655 if (new_rlim)
1656 *rlim = *new_rlim;
1657 }
Jiri Slaby7855c352009-08-26 23:45:34 +02001658 task_unlock(tsk->group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Andrew Mortond3561f72006-03-24 03:18:36 -08001660 /*
1661 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1662 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1663 * very long-standing error, and fixing it now risks breakage of
1664 * applications, so we live with it
1665 */
Jiri Slaby5b415352010-03-24 16:11:29 +01001666 if (!retval && new_rlim && resource == RLIMIT_CPU &&
1667 new_rlim->rlim_cur != RLIM_INFINITY)
1668 update_rlimit_cpu(tsk, new_rlim->rlim_cur);
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001669out:
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001670 read_unlock(&tasklist_lock);
Oleg Nesterov2fb9d262009-09-03 19:21:45 +02001671 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001674/* rcu lock must be held */
1675static int check_prlimit_permission(struct task_struct *task)
1676{
1677 const struct cred *cred = current_cred(), *tcred;
1678
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001679 if (current == task)
1680 return 0;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001681
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001682 tcred = __task_cred(task);
Eric W. Biederman5af66202012-03-03 20:21:47 -08001683 if (uid_eq(cred->uid, tcred->euid) &&
1684 uid_eq(cred->uid, tcred->suid) &&
1685 uid_eq(cred->uid, tcred->uid) &&
1686 gid_eq(cred->gid, tcred->egid) &&
1687 gid_eq(cred->gid, tcred->sgid) &&
1688 gid_eq(cred->gid, tcred->gid))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001689 return 0;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -08001690 if (ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001691 return 0;
1692
1693 return -EPERM;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001694}
1695
1696SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1697 const struct rlimit64 __user *, new_rlim,
1698 struct rlimit64 __user *, old_rlim)
1699{
1700 struct rlimit64 old64, new64;
1701 struct rlimit old, new;
1702 struct task_struct *tsk;
1703 int ret;
1704
1705 if (new_rlim) {
1706 if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1707 return -EFAULT;
1708 rlim64_to_rlim(&new64, &new);
1709 }
1710
1711 rcu_read_lock();
1712 tsk = pid ? find_task_by_vpid(pid) : current;
1713 if (!tsk) {
1714 rcu_read_unlock();
1715 return -ESRCH;
1716 }
1717 ret = check_prlimit_permission(tsk);
1718 if (ret) {
1719 rcu_read_unlock();
1720 return ret;
1721 }
1722 get_task_struct(tsk);
1723 rcu_read_unlock();
1724
1725 ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1726 old_rlim ? &old : NULL);
1727
1728 if (!ret && old_rlim) {
1729 rlim_to_rlim64(&old, &old64);
1730 if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1731 ret = -EFAULT;
1732 }
1733
1734 put_task_struct(tsk);
1735 return ret;
1736}
1737
Jiri Slaby7855c352009-08-26 23:45:34 +02001738SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1739{
1740 struct rlimit new_rlim;
1741
1742 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1743 return -EFAULT;
Jiri Slaby5b415352010-03-24 16:11:29 +01001744 return do_prlimit(current, resource, &new_rlim, NULL);
Jiri Slaby7855c352009-08-26 23:45:34 +02001745}
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747/*
1748 * It would make sense to put struct rusage in the task_struct,
1749 * except that would make the task_struct be *really big*. After
1750 * task_struct gets moved into malloc'ed memory, it would
1751 * make sense to do this. It will make moving the rest of the information
1752 * a lot simpler! (Which we're not doing right now because we're not
1753 * measuring them yet).
1754 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1756 * races with threads incrementing their own counters. But since word
1757 * reads are atomic, we either get new values or old values and we don't
1758 * care which for the sums. We always take the siglock to protect reading
1759 * the c* fields from p->signal from races with exit.c updating those
1760 * fields when reaping, so a sample either gets all the additions of a
1761 * given child after it's reaped, or none so this sample is before reaping.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001762 *
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001763 * Locking:
1764 * We need to take the siglock for CHILDEREN, SELF and BOTH
1765 * for the cases current multithreaded, non-current single threaded
1766 * non-current multithreaded. Thread traversal is now safe with
1767 * the siglock held.
1768 * Strictly speaking, we donot need to take the siglock if we are current and
1769 * single threaded, as no one else can take our signal_struct away, no one
1770 * else can reap the children to update signal->c* counters, and no one else
1771 * can race with the signal-> fields. If we do not take any lock, the
1772 * signal-> fields could be read out of order while another thread was just
1773 * exiting. So we should place a read memory barrier when we avoid the lock.
1774 * On the writer side, write memory barrier is implied in __exit_signal
1775 * as __exit_signal releases the siglock spinlock after updating the signal->
1776 * fields. But we don't do this yet to keep things simple.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001777 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 */
1779
Frank Mayharf06febc2008-09-12 09:54:39 -07001780static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001781{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001782 r->ru_nvcsw += t->nvcsw;
1783 r->ru_nivcsw += t->nivcsw;
1784 r->ru_minflt += t->min_flt;
1785 r->ru_majflt += t->maj_flt;
1786 r->ru_inblock += task_io_get_inblock(t);
1787 r->ru_oublock += task_io_get_oublock(t);
1788}
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1791{
1792 struct task_struct *t;
1793 unsigned long flags;
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09001794 cputime_t tgutime, tgstime, utime, stime;
Jiri Pirko1f102062009-09-22 16:44:10 -07001795 unsigned long maxrss = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 memset((char *) r, 0, sizeof *r);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001798 utime = stime = 0;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001799
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001800 if (who == RUSAGE_THREAD) {
Frederic Weisbeckere80d0a12012-11-21 16:26:44 +01001801 task_cputime_adjusted(current, &utime, &stime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001802 accumulate_thread_rusage(p, r);
Jiri Pirko1f102062009-09-22 16:44:10 -07001803 maxrss = p->signal->maxrss;
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001804 goto out;
1805 }
1806
Oleg Nesterovd6cf7232008-04-30 00:52:38 -07001807 if (!lock_task_sighand(p, &flags))
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001808 return;
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 switch (who) {
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001811 case RUSAGE_BOTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 case RUSAGE_CHILDREN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 utime = p->signal->cutime;
1814 stime = p->signal->cstime;
1815 r->ru_nvcsw = p->signal->cnvcsw;
1816 r->ru_nivcsw = p->signal->cnivcsw;
1817 r->ru_minflt = p->signal->cmin_flt;
1818 r->ru_majflt = p->signal->cmaj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07001819 r->ru_inblock = p->signal->cinblock;
1820 r->ru_oublock = p->signal->coublock;
Jiri Pirko1f102062009-09-22 16:44:10 -07001821 maxrss = p->signal->cmaxrss;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001822
1823 if (who == RUSAGE_CHILDREN)
1824 break;
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 case RUSAGE_SELF:
Frederic Weisbeckere80d0a12012-11-21 16:26:44 +01001827 thread_group_cputime_adjusted(p, &tgutime, &tgstime);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001828 utime += tgutime;
1829 stime += tgstime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 r->ru_nvcsw += p->signal->nvcsw;
1831 r->ru_nivcsw += p->signal->nivcsw;
1832 r->ru_minflt += p->signal->min_flt;
1833 r->ru_majflt += p->signal->maj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07001834 r->ru_inblock += p->signal->inblock;
1835 r->ru_oublock += p->signal->oublock;
Jiri Pirko1f102062009-09-22 16:44:10 -07001836 if (maxrss < p->signal->maxrss)
1837 maxrss = p->signal->maxrss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 t = p;
1839 do {
Frank Mayharf06febc2008-09-12 09:54:39 -07001840 accumulate_thread_rusage(t, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 t = next_thread(t);
1842 } while (t != p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 break;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 default:
1846 BUG();
1847 }
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001848 unlock_task_sighand(p, &flags);
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001849
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001850out:
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001851 cputime_to_timeval(utime, &r->ru_utime);
1852 cputime_to_timeval(stime, &r->ru_stime);
Jiri Pirko1f102062009-09-22 16:44:10 -07001853
1854 if (who != RUSAGE_CHILDREN) {
1855 struct mm_struct *mm = get_task_mm(p);
1856 if (mm) {
1857 setmax_mm_hiwater_rss(&maxrss, mm);
1858 mmput(mm);
1859 }
1860 }
1861 r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
1864int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1865{
1866 struct rusage r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 k_getrusage(p, who, &r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1869}
1870
Heiko Carstense48fbb62009-01-14 14:14:26 +01001871SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001873 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1874 who != RUSAGE_THREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return -EINVAL;
1876 return getrusage(current, who, ru);
1877}
1878
Al Viro8d2d5c42013-03-03 12:49:06 -05001879#ifdef CONFIG_COMPAT
1880COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru)
1881{
1882 struct rusage r;
1883
1884 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1885 who != RUSAGE_THREAD)
1886 return -EINVAL;
1887
1888 k_getrusage(current, who, &r);
1889 return put_compat_rusage(&r, ru);
1890}
1891#endif
1892
Heiko Carstense48fbb62009-01-14 14:14:26 +01001893SYSCALL_DEFINE1(umask, int, mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1896 return mask;
1897}
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -08001898
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001899static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
1900{
Al Viro2903ff02012-08-28 12:52:22 -04001901 struct fd exe;
Al Viro496ad9a2013-01-23 17:07:38 -05001902 struct inode *inode;
Al Viro2903ff02012-08-28 12:52:22 -04001903 int err;
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001904
Al Viro2903ff02012-08-28 12:52:22 -04001905 exe = fdget(fd);
1906 if (!exe.file)
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001907 return -EBADF;
1908
Al Viro496ad9a2013-01-23 17:07:38 -05001909 inode = file_inode(exe.file);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001910
1911 /*
1912 * Because the original mm->exe_file points to executable file, make
1913 * sure that this one is executable as well, to avoid breaking an
1914 * overall picture.
1915 */
1916 err = -EACCES;
Al Viro496ad9a2013-01-23 17:07:38 -05001917 if (!S_ISREG(inode->i_mode) ||
Al Viro2903ff02012-08-28 12:52:22 -04001918 exe.file->f_path.mnt->mnt_flags & MNT_NOEXEC)
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001919 goto exit;
1920
Al Viro496ad9a2013-01-23 17:07:38 -05001921 err = inode_permission(inode, MAY_EXEC);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001922 if (err)
1923 goto exit;
1924
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001925 down_write(&mm->mmap_sem);
1926
1927 /*
Konstantin Khlebnikov4229fb12012-07-11 14:02:11 -07001928 * Forbid mm->exe_file change if old file still mapped.
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001929 */
1930 err = -EBUSY;
Konstantin Khlebnikov4229fb12012-07-11 14:02:11 -07001931 if (mm->exe_file) {
1932 struct vm_area_struct *vma;
1933
1934 for (vma = mm->mmap; vma; vma = vma->vm_next)
1935 if (vma->vm_file &&
1936 path_equal(&vma->vm_file->f_path,
1937 &mm->exe_file->f_path))
1938 goto exit_unlock;
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001939 }
1940
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001941 /*
1942 * The symlink can be changed only once, just to disallow arbitrary
1943 * transitions malicious software might bring in. This means one
1944 * could make a snapshot over all processes running and monitor
1945 * /proc/pid/exe changes to notice unusual activity if needed.
1946 */
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001947 err = -EPERM;
1948 if (test_and_set_bit(MMF_EXE_FILE_CHANGED, &mm->flags))
1949 goto exit_unlock;
1950
Konstantin Khlebnikov4229fb12012-07-11 14:02:11 -07001951 err = 0;
Al Viro2903ff02012-08-28 12:52:22 -04001952 set_mm_exe_file(mm, exe.file); /* this grabs a reference to exe.file */
Konstantin Khlebnikovbafb2822012-06-07 14:21:11 -07001953exit_unlock:
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001954 up_write(&mm->mmap_sem);
1955
1956exit:
Al Viro2903ff02012-08-28 12:52:22 -04001957 fdput(exe);
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001958 return err;
1959}
1960
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001961static int prctl_set_mm(int opt, unsigned long addr,
1962 unsigned long arg4, unsigned long arg5)
1963{
1964 unsigned long rlim = rlimit(RLIMIT_DATA);
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001965 struct mm_struct *mm = current->mm;
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07001966 struct vm_area_struct *vma;
1967 int error;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001968
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07001969 if (arg5 || (arg4 && opt != PR_SET_MM_AUXV))
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001970 return -EINVAL;
1971
Cyrill Gorcunov79f07132012-03-15 15:17:10 -07001972 if (!capable(CAP_SYS_RESOURCE))
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001973 return -EPERM;
1974
Cyrill Gorcunovb32dfe32012-05-31 16:26:46 -07001975 if (opt == PR_SET_MM_EXE_FILE)
1976 return prctl_set_mm_exe_file(mm, (unsigned int)addr);
1977
Cyrill Gorcunov1ad75b92012-06-07 14:21:11 -07001978 if (addr >= TASK_SIZE || addr < mmap_min_addr)
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001979 return -EINVAL;
1980
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07001981 error = -EINVAL;
1982
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001983 down_read(&mm->mmap_sem);
1984 vma = find_vma(mm, addr);
1985
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001986 switch (opt) {
1987 case PR_SET_MM_START_CODE:
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07001988 mm->start_code = addr;
1989 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001990 case PR_SET_MM_END_CODE:
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07001991 mm->end_code = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001992 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001993 case PR_SET_MM_START_DATA:
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07001994 mm->start_data = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001995 break;
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07001996 case PR_SET_MM_END_DATA:
1997 mm->end_data = addr;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001998 break;
1999
2000 case PR_SET_MM_START_BRK:
2001 if (addr <= mm->end_data)
2002 goto out;
2003
2004 if (rlim < RLIM_INFINITY &&
2005 (mm->brk - addr) +
2006 (mm->end_data - mm->start_data) > rlim)
2007 goto out;
2008
2009 mm->start_brk = addr;
2010 break;
2011
2012 case PR_SET_MM_BRK:
2013 if (addr <= mm->end_data)
2014 goto out;
2015
2016 if (rlim < RLIM_INFINITY &&
2017 (addr - mm->start_brk) +
2018 (mm->end_data - mm->start_data) > rlim)
2019 goto out;
2020
2021 mm->brk = addr;
2022 break;
2023
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002024 /*
2025 * If command line arguments and environment
2026 * are placed somewhere else on stack, we can
2027 * set them up here, ARG_START/END to setup
2028 * command line argumets and ENV_START/END
2029 * for environment.
2030 */
2031 case PR_SET_MM_START_STACK:
2032 case PR_SET_MM_ARG_START:
2033 case PR_SET_MM_ARG_END:
2034 case PR_SET_MM_ENV_START:
2035 case PR_SET_MM_ENV_END:
2036 if (!vma) {
2037 error = -EFAULT;
2038 goto out;
2039 }
Cyrill Gorcunovfe8c7f52012-05-31 16:26:45 -07002040 if (opt == PR_SET_MM_START_STACK)
2041 mm->start_stack = addr;
2042 else if (opt == PR_SET_MM_ARG_START)
2043 mm->arg_start = addr;
2044 else if (opt == PR_SET_MM_ARG_END)
2045 mm->arg_end = addr;
2046 else if (opt == PR_SET_MM_ENV_START)
2047 mm->env_start = addr;
2048 else if (opt == PR_SET_MM_ENV_END)
2049 mm->env_end = addr;
2050 break;
2051
2052 /*
2053 * This doesn't move auxiliary vector itself
2054 * since it's pinned to mm_struct, but allow
2055 * to fill vector with new values. It's up
2056 * to a caller to provide sane values here
2057 * otherwise user space tools which use this
2058 * vector might be unhappy.
2059 */
2060 case PR_SET_MM_AUXV: {
2061 unsigned long user_auxv[AT_VECTOR_SIZE];
2062
2063 if (arg4 > sizeof(user_auxv))
2064 goto out;
2065 up_read(&mm->mmap_sem);
2066
2067 if (copy_from_user(user_auxv, (const void __user *)addr, arg4))
2068 return -EFAULT;
2069
2070 /* Make sure the last entry is always AT_NULL */
2071 user_auxv[AT_VECTOR_SIZE - 2] = 0;
2072 user_auxv[AT_VECTOR_SIZE - 1] = 0;
2073
2074 BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
2075
2076 task_lock(current);
2077 memcpy(mm->saved_auxv, user_auxv, arg4);
2078 task_unlock(current);
2079
2080 return 0;
2081 }
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002082 default:
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002083 goto out;
2084 }
2085
2086 error = 0;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002087out:
2088 up_read(&mm->mmap_sem);
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002089 return error;
2090}
Cyrill Gorcunov300f7862012-06-07 14:21:12 -07002091
Amnon Shiloh52b36942013-04-30 15:28:48 -07002092#ifdef CONFIG_CHECKPOINT_RESTORE
Cyrill Gorcunov300f7862012-06-07 14:21:12 -07002093static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
2094{
2095 return put_user(me->clear_child_tid, tid_addr);
2096}
Amnon Shiloh52b36942013-04-30 15:28:48 -07002097#else
Cyrill Gorcunov300f7862012-06-07 14:21:12 -07002098static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
2099{
2100 return -EINVAL;
2101}
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002102#endif
2103
Colin Crossd67a07b2013-10-30 13:17:34 -07002104#ifdef CONFIG_MMU
Colin Cross6ebfe582013-06-26 17:26:01 -07002105static int prctl_update_vma_anon_name(struct vm_area_struct *vma,
2106 struct vm_area_struct **prev,
2107 unsigned long start, unsigned long end,
2108 const char __user *name_addr)
2109{
2110 struct mm_struct * mm = vma->vm_mm;
2111 int error = 0;
2112 pgoff_t pgoff;
2113
2114 if (name_addr == vma_get_anon_name(vma)) {
2115 *prev = vma;
2116 goto out;
2117 }
2118
2119 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
2120 *prev = vma_merge(mm, *prev, start, end, vma->vm_flags, vma->anon_vma,
2121 vma->vm_file, pgoff, vma_policy(vma),
2122 name_addr);
2123 if (*prev) {
2124 vma = *prev;
2125 goto success;
2126 }
2127
2128 *prev = vma;
2129
2130 if (start != vma->vm_start) {
2131 error = split_vma(mm, vma, start, 1);
2132 if (error)
2133 goto out;
2134 }
2135
2136 if (end != vma->vm_end) {
2137 error = split_vma(mm, vma, end, 0);
2138 if (error)
2139 goto out;
2140 }
2141
2142success:
2143 if (!vma->vm_file)
2144 vma->shared.anon_name = name_addr;
2145
2146out:
2147 if (error == -ENOMEM)
2148 error = -EAGAIN;
2149 return error;
2150}
2151
2152static int prctl_set_vma_anon_name(unsigned long start, unsigned long end,
2153 unsigned long arg)
2154{
2155 unsigned long tmp;
2156 struct vm_area_struct * vma, *prev;
2157 int unmapped_error = 0;
2158 int error = -EINVAL;
2159
2160 /*
2161 * If the interval [start,end) covers some unmapped address
2162 * ranges, just ignore them, but return -ENOMEM at the end.
2163 * - this matches the handling in madvise.
2164 */
2165 vma = find_vma_prev(current->mm, start, &prev);
2166 if (vma && start > vma->vm_start)
2167 prev = vma;
2168
2169 for (;;) {
2170 /* Still start < end. */
2171 error = -ENOMEM;
2172 if (!vma)
2173 return error;
2174
2175 /* Here start < (end|vma->vm_end). */
2176 if (start < vma->vm_start) {
2177 unmapped_error = -ENOMEM;
2178 start = vma->vm_start;
2179 if (start >= end)
2180 return error;
2181 }
2182
2183 /* Here vma->vm_start <= start < (end|vma->vm_end) */
2184 tmp = vma->vm_end;
2185 if (end < tmp)
2186 tmp = end;
2187
2188 /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
2189 error = prctl_update_vma_anon_name(vma, &prev, start, end,
2190 (const char __user *)arg);
2191 if (error)
2192 return error;
2193 start = tmp;
2194 if (prev && start < prev->vm_end)
2195 start = prev->vm_end;
2196 error = unmapped_error;
2197 if (start >= end)
2198 return error;
2199 if (prev)
2200 vma = prev->vm_next;
2201 else /* madvise_remove dropped mmap_sem */
2202 vma = find_vma(current->mm, start);
2203 }
2204}
2205
2206static int prctl_set_vma(unsigned long opt, unsigned long start,
2207 unsigned long len_in, unsigned long arg)
2208{
2209 struct mm_struct *mm = current->mm;
2210 int error;
2211 unsigned long len;
2212 unsigned long end;
2213
2214 if (start & ~PAGE_MASK)
2215 return -EINVAL;
2216 len = (len_in + ~PAGE_MASK) & PAGE_MASK;
2217
2218 /* Check to see whether len was rounded up from small -ve to zero */
2219 if (len_in && !len)
2220 return -EINVAL;
2221
2222 end = start + len;
2223 if (end < start)
2224 return -EINVAL;
2225
2226 if (end == start)
2227 return 0;
2228
2229 down_write(&mm->mmap_sem);
2230
2231 switch (opt) {
2232 case PR_SET_VMA_ANON_NAME:
2233 error = prctl_set_vma_anon_name(start, end, arg);
2234 break;
2235 default:
2236 error = -EINVAL;
2237 }
2238
2239 up_write(&mm->mmap_sem);
2240
2241 return error;
2242}
Colin Crossd67a07b2013-10-30 13:17:34 -07002243#else /* CONFIG_MMU */
2244static int prctl_set_vma(unsigned long opt, unsigned long start,
2245 unsigned long len_in, unsigned long arg)
2246{
2247 return -EINVAL;
2248}
2249#endif
Colin Cross6ebfe582013-06-26 17:26:01 -07002250
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01002251SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
2252 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
David Howellsb6dff3e2008-11-14 10:39:16 +11002254 struct task_struct *me = current;
2255 unsigned char comm[sizeof(me->comm)];
2256 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
David Howellsd84f4f92008-11-14 10:39:23 +11002258 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2259 if (error != -ENOSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 return error;
2261
David Howellsd84f4f92008-11-14 10:39:23 +11002262 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 switch (option) {
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002264 case PR_SET_PDEATHSIG:
2265 if (!valid_signal(arg2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 error = -EINVAL;
2267 break;
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002268 }
2269 me->pdeath_signal = arg2;
2270 break;
2271 case PR_GET_PDEATHSIG:
2272 error = put_user(me->pdeath_signal, (int __user *)arg2);
2273 break;
2274 case PR_GET_DUMPABLE:
2275 error = get_dumpable(me->mm);
2276 break;
2277 case PR_SET_DUMPABLE:
2278 if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) {
2279 error = -EINVAL;
2280 break;
2281 }
2282 set_dumpable(me->mm, arg2);
2283 break;
2284
2285 case PR_SET_UNALIGN:
2286 error = SET_UNALIGN_CTL(me, arg2);
2287 break;
2288 case PR_GET_UNALIGN:
2289 error = GET_UNALIGN_CTL(me, arg2);
2290 break;
2291 case PR_SET_FPEMU:
2292 error = SET_FPEMU_CTL(me, arg2);
2293 break;
2294 case PR_GET_FPEMU:
2295 error = GET_FPEMU_CTL(me, arg2);
2296 break;
2297 case PR_SET_FPEXC:
2298 error = SET_FPEXC_CTL(me, arg2);
2299 break;
2300 case PR_GET_FPEXC:
2301 error = GET_FPEXC_CTL(me, arg2);
2302 break;
2303 case PR_GET_TIMING:
2304 error = PR_TIMING_STATISTICAL;
2305 break;
2306 case PR_SET_TIMING:
2307 if (arg2 != PR_TIMING_STATISTICAL)
2308 error = -EINVAL;
2309 break;
2310 case PR_SET_NAME:
2311 comm[sizeof(me->comm) - 1] = 0;
2312 if (strncpy_from_user(comm, (char __user *)arg2,
2313 sizeof(me->comm) - 1) < 0)
2314 return -EFAULT;
2315 set_task_comm(me, comm);
2316 proc_comm_connector(me);
2317 break;
2318 case PR_GET_NAME:
2319 get_task_comm(comm, me);
2320 if (copy_to_user((char __user *)arg2, comm, sizeof(comm)))
2321 return -EFAULT;
2322 break;
2323 case PR_GET_ENDIAN:
2324 error = GET_ENDIAN(me, arg2);
2325 break;
2326 case PR_SET_ENDIAN:
2327 error = SET_ENDIAN(me, arg2);
2328 break;
2329 case PR_GET_SECCOMP:
2330 error = prctl_get_seccomp();
2331 break;
2332 case PR_SET_SECCOMP:
2333 error = prctl_set_seccomp(arg2, (char __user *)arg3);
2334 break;
2335 case PR_GET_TSC:
2336 error = GET_TSC_CTL(arg2);
2337 break;
2338 case PR_SET_TSC:
2339 error = SET_TSC_CTL(arg2);
2340 break;
2341 case PR_TASK_PERF_EVENTS_DISABLE:
2342 error = perf_event_task_disable();
2343 break;
2344 case PR_TASK_PERF_EVENTS_ENABLE:
2345 error = perf_event_task_enable();
2346 break;
2347 case PR_GET_TIMERSLACK:
2348 error = current->timer_slack_ns;
2349 break;
2350 case PR_SET_TIMERSLACK:
2351 if (arg2 <= 0)
2352 current->timer_slack_ns =
2353 current->default_timer_slack_ns;
2354 else
2355 current->timer_slack_ns = arg2;
2356 break;
2357 case PR_MCE_KILL:
2358 if (arg4 | arg5)
2359 return -EINVAL;
2360 switch (arg2) {
2361 case PR_MCE_KILL_CLEAR:
2362 if (arg3 != 0)
2363 return -EINVAL;
2364 current->flags &= ~PF_MCE_PROCESS;
2365 break;
2366 case PR_MCE_KILL_SET:
2367 current->flags |= PF_MCE_PROCESS;
2368 if (arg3 == PR_MCE_KILL_EARLY)
2369 current->flags |= PF_MCE_EARLY;
2370 else if (arg3 == PR_MCE_KILL_LATE)
2371 current->flags &= ~PF_MCE_EARLY;
2372 else if (arg3 == PR_MCE_KILL_DEFAULT)
2373 current->flags &=
2374 ~(PF_MCE_EARLY|PF_MCE_PROCESS);
2375 else
2376 return -EINVAL;
2377 break;
2378 default:
2379 return -EINVAL;
2380 }
2381 break;
2382 case PR_MCE_KILL_GET:
2383 if (arg2 | arg3 | arg4 | arg5)
2384 return -EINVAL;
2385 if (current->flags & PF_MCE_PROCESS)
2386 error = (current->flags & PF_MCE_EARLY) ?
2387 PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
2388 else
2389 error = PR_MCE_KILL_DEFAULT;
2390 break;
2391 case PR_SET_MM:
2392 error = prctl_set_mm(arg2, arg3, arg4, arg5);
2393 break;
2394 case PR_GET_TID_ADDRESS:
2395 error = prctl_get_tid_address(me, (int __user **)arg2);
2396 break;
2397 case PR_SET_CHILD_SUBREAPER:
2398 me->signal->is_child_subreaper = !!arg2;
2399 break;
2400 case PR_GET_CHILD_SUBREAPER:
2401 error = put_user(me->signal->is_child_subreaper,
2402 (int __user *)arg2);
2403 break;
2404 case PR_SET_NO_NEW_PRIVS:
2405 if (arg2 != 1 || arg3 || arg4 || arg5)
2406 return -EINVAL;
2407
2408 current->no_new_privs = 1;
2409 break;
2410 case PR_GET_NO_NEW_PRIVS:
2411 if (arg2 || arg3 || arg4 || arg5)
2412 return -EINVAL;
2413 return current->no_new_privs ? 1 : 0;
Colin Cross527aae52013-10-30 13:14:12 -07002414 case PR_SET_VMA:
2415 error = prctl_set_vma(arg2, arg3, arg4, arg5);
2416 break;
Andrew Mortonf3cbd432013-02-21 16:43:07 -08002417 default:
2418 error = -EINVAL;
2419 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
2421 return error;
2422}
Andi Kleen3cfc3482006-09-26 10:52:28 +02002423
Heiko Carstens836f92a2009-01-14 14:14:33 +01002424SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
2425 struct getcpu_cache __user *, unused)
Andi Kleen3cfc3482006-09-26 10:52:28 +02002426{
2427 int err = 0;
2428 int cpu = raw_smp_processor_id();
2429 if (cpup)
2430 err |= put_user(cpu, cpup);
2431 if (nodep)
2432 err |= put_user(cpu_to_node(cpu), nodep);
Andi Kleen3cfc3482006-09-26 10:52:28 +02002433 return err ? -EFAULT : 0;
2434}
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002435
2436char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
2437
Oleg Nesterov2ca067e2013-03-22 15:04:41 -07002438static int __orderly_poweroff(bool force)
Andrew Mortonb57b44a2012-07-30 14:40:03 -07002439{
Andrew Mortonb57b44a2012-07-30 14:40:03 -07002440 char **argv;
2441 static char *envp[] = {
2442 "HOME=/",
2443 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
2444 NULL
2445 };
2446 int ret;
2447
Oleg Nesterov2ca067e2013-03-22 15:04:41 -07002448 argv = argv_split(GFP_KERNEL, poweroff_cmd, NULL);
2449 if (argv) {
2450 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
2451 argv_free(argv);
2452 } else {
Andrew Mortonb57b44a2012-07-30 14:40:03 -07002453 printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
Oleg Nesterov2ca067e2013-03-22 15:04:41 -07002454 __func__, poweroff_cmd);
2455 ret = -ENOMEM;
Andrew Mortonb57b44a2012-07-30 14:40:03 -07002456 }
2457
Andrew Mortonb57b44a2012-07-30 14:40:03 -07002458 if (ret && force) {
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002459 printk(KERN_WARNING "Failed to start orderly shutdown: "
Oleg Nesterov2ca067e2013-03-22 15:04:41 -07002460 "forcing the issue\n");
Andrew Mortonb57b44a2012-07-30 14:40:03 -07002461 /*
2462 * I guess this should try to kick off some daemon to sync and
2463 * poweroff asap. Or not even bother syncing if we're doing an
2464 * emergency shutdown?
2465 */
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002466 emergency_sync();
2467 kernel_power_off();
2468 }
2469
2470 return ret;
2471}
Oleg Nesterov2ca067e2013-03-22 15:04:41 -07002472
2473static bool poweroff_force;
2474
2475static void poweroff_work_func(struct work_struct *work)
2476{
2477 __orderly_poweroff(poweroff_force);
2478}
2479
2480static DECLARE_WORK(poweroff_work, poweroff_work_func);
2481
2482/**
2483 * orderly_poweroff - Trigger an orderly system poweroff
2484 * @force: force poweroff if command execution fails
2485 *
2486 * This may be called from any context to trigger a system shutdown.
2487 * If the orderly shutdown fails, it will force an immediate shutdown.
2488 */
2489int orderly_poweroff(bool force)
2490{
2491 if (force) /* do not override the pending "true" */
2492 poweroff_force = true;
2493 schedule_work(&poweroff_work);
2494 return 0;
2495}
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002496EXPORT_SYMBOL_GPL(orderly_poweroff);
Stephen Rothwell4a22f162013-04-30 15:27:37 -07002497
2498/**
2499 * do_sysinfo - fill in sysinfo struct
2500 * @info: pointer to buffer to fill
2501 */
2502static int do_sysinfo(struct sysinfo *info)
2503{
2504 unsigned long mem_total, sav_total;
2505 unsigned int mem_unit, bitcount;
2506 struct timespec tp;
2507
2508 memset(info, 0, sizeof(struct sysinfo));
2509
2510 ktime_get_ts(&tp);
2511 monotonic_to_bootbased(&tp);
2512 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
2513
2514 get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
2515
2516 info->procs = nr_threads;
2517
2518 si_meminfo(info);
2519 si_swapinfo(info);
2520
2521 /*
2522 * If the sum of all the available memory (i.e. ram + swap)
2523 * is less than can be stored in a 32 bit unsigned long then
2524 * we can be binary compatible with 2.2.x kernels. If not,
2525 * well, in that case 2.2.x was broken anyways...
2526 *
2527 * -Erik Andersen <andersee@debian.org>
2528 */
2529
2530 mem_total = info->totalram + info->totalswap;
2531 if (mem_total < info->totalram || mem_total < info->totalswap)
2532 goto out;
2533 bitcount = 0;
2534 mem_unit = info->mem_unit;
2535 while (mem_unit > 1) {
2536 bitcount++;
2537 mem_unit >>= 1;
2538 sav_total = mem_total;
2539 mem_total <<= 1;
2540 if (mem_total < sav_total)
2541 goto out;
2542 }
2543
2544 /*
2545 * If mem_total did not overflow, multiply all memory values by
2546 * info->mem_unit and set it to 1. This leaves things compatible
2547 * with 2.2.x, and also retains compatibility with earlier 2.4.x
2548 * kernels...
2549 */
2550
2551 info->mem_unit = 1;
2552 info->totalram <<= bitcount;
2553 info->freeram <<= bitcount;
2554 info->sharedram <<= bitcount;
2555 info->bufferram <<= bitcount;
2556 info->totalswap <<= bitcount;
2557 info->freeswap <<= bitcount;
2558 info->totalhigh <<= bitcount;
2559 info->freehigh <<= bitcount;
2560
2561out:
2562 return 0;
2563}
2564
2565SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
2566{
2567 struct sysinfo val;
2568
2569 do_sysinfo(&val);
2570
2571 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
2572 return -EFAULT;
2573
2574 return 0;
2575}
2576
2577#ifdef CONFIG_COMPAT
2578struct compat_sysinfo {
2579 s32 uptime;
2580 u32 loads[3];
2581 u32 totalram;
2582 u32 freeram;
2583 u32 sharedram;
2584 u32 bufferram;
2585 u32 totalswap;
2586 u32 freeswap;
2587 u16 procs;
2588 u16 pad;
2589 u32 totalhigh;
2590 u32 freehigh;
2591 u32 mem_unit;
2592 char _f[20-2*sizeof(u32)-sizeof(int)];
2593};
2594
2595COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
2596{
2597 struct sysinfo s;
2598
2599 do_sysinfo(&s);
2600
2601 /* Check to see if any memory value is too large for 32-bit and scale
2602 * down if needed
2603 */
2604 if ((s.totalram >> 32) || (s.totalswap >> 32)) {
2605 int bitcount = 0;
2606
2607 while (s.mem_unit < PAGE_SIZE) {
2608 s.mem_unit <<= 1;
2609 bitcount++;
2610 }
2611
2612 s.totalram >>= bitcount;
2613 s.freeram >>= bitcount;
2614 s.sharedram >>= bitcount;
2615 s.bufferram >>= bitcount;
2616 s.totalswap >>= bitcount;
2617 s.freeswap >>= bitcount;
2618 s.totalhigh >>= bitcount;
2619 s.freehigh >>= bitcount;
2620 }
2621
2622 if (!access_ok(VERIFY_WRITE, info, sizeof(struct compat_sysinfo)) ||
2623 __put_user(s.uptime, &info->uptime) ||
2624 __put_user(s.loads[0], &info->loads[0]) ||
2625 __put_user(s.loads[1], &info->loads[1]) ||
2626 __put_user(s.loads[2], &info->loads[2]) ||
2627 __put_user(s.totalram, &info->totalram) ||
2628 __put_user(s.freeram, &info->freeram) ||
2629 __put_user(s.sharedram, &info->sharedram) ||
2630 __put_user(s.bufferram, &info->bufferram) ||
2631 __put_user(s.totalswap, &info->totalswap) ||
2632 __put_user(s.freeswap, &info->freeswap) ||
2633 __put_user(s.procs, &info->procs) ||
2634 __put_user(s.totalhigh, &info->totalhigh) ||
2635 __put_user(s.freehigh, &info->freehigh) ||
2636 __put_user(s.mem_unit, &info->mem_unit))
2637 return -EFAULT;
2638
2639 return 0;
2640}
2641#endif /* CONFIG_COMPAT */