blob: 864611c7b1a3d670671748041f93d506dec43c90 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/gfp.h>
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010040#include <linux/syscore_ops.h>
Andi Kleenbe274252011-08-19 16:15:10 -070041#include <linux/version.h>
42#include <linux/ctype.h>
Colin Cross8ad62c22013-06-26 17:26:01 -070043#include <linux/mm.h>
44#include <linux/mempolicy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <linux/compat.h>
47#include <linux/syscalls.h>
Keshavamurthy Anil S00d7c052005-12-12 00:37:33 -080048#include <linux/kprobes.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070049#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Seiji Aguchi04c68622011-01-12 16:59:30 -080051#include <linux/kmsg_dump.h>
Andi Kleenbe274252011-08-19 16:15:10 -070052/* Move somewhere else to avoid recompiling? */
53#include <generated/utsrelease.h>
Seiji Aguchi04c68622011-01-12 16:59:30 -080054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/uaccess.h>
56#include <asm/io.h>
57#include <asm/unistd.h>
58
59#ifndef SET_UNALIGN_CTL
60# define SET_UNALIGN_CTL(a,b) (-EINVAL)
61#endif
62#ifndef GET_UNALIGN_CTL
63# define GET_UNALIGN_CTL(a,b) (-EINVAL)
64#endif
65#ifndef SET_FPEMU_CTL
66# define SET_FPEMU_CTL(a,b) (-EINVAL)
67#endif
68#ifndef GET_FPEMU_CTL
69# define GET_FPEMU_CTL(a,b) (-EINVAL)
70#endif
71#ifndef SET_FPEXC_CTL
72# define SET_FPEXC_CTL(a,b) (-EINVAL)
73#endif
74#ifndef GET_FPEXC_CTL
75# define GET_FPEXC_CTL(a,b) (-EINVAL)
76#endif
Anton Blanchard651d7652006-06-07 16:10:19 +100077#ifndef GET_ENDIAN
78# define GET_ENDIAN(a,b) (-EINVAL)
79#endif
80#ifndef SET_ENDIAN
81# define SET_ENDIAN(a,b) (-EINVAL)
82#endif
Erik Bosman8fb402b2008-04-11 18:54:17 +020083#ifndef GET_TSC_CTL
84# define GET_TSC_CTL(a) (-EINVAL)
85#endif
86#ifndef SET_TSC_CTL
87# define SET_TSC_CTL(a) (-EINVAL)
88#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/*
91 * this is where the system-wide overflow UID and GID are defined, for
92 * architectures that now have 32-bit UID/GID but didn't in the past
93 */
94
95int overflowuid = DEFAULT_OVERFLOWUID;
96int overflowgid = DEFAULT_OVERFLOWGID;
97
98#ifdef CONFIG_UID16
99EXPORT_SYMBOL(overflowuid);
100EXPORT_SYMBOL(overflowgid);
101#endif
102
103/*
104 * the same as above, but for filesystems which can only store a 16-bit
105 * UID and GID. as such, this is needed on all architectures
106 */
107
108int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
109int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
110
111EXPORT_SYMBOL(fs_overflowuid);
112EXPORT_SYMBOL(fs_overflowgid);
113
114/*
115 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
116 */
117
118int C_A_D = 1;
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700119struct pid *cad_pid;
120EXPORT_SYMBOL(cad_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122/*
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700123 * If set, this is used for preparing the system to power off.
124 */
125
126void (*pm_power_off_prepare)(void);
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700127
David Howellsc69e8d92008-11-14 10:39:19 +1100128/*
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700129 * Returns true if current's euid is same as p's uid or euid,
130 * or has CAP_SYS_NICE to p's user_ns.
131 *
132 * Called with rcu_read_lock, creds are safe
133 */
134static bool set_one_prio_perm(struct task_struct *p)
135{
136 const struct cred *cred = current_cred(), *pcred = __task_cred(p);
137
138 if (pcred->user->user_ns == cred->user->user_ns &&
139 (pcred->uid == cred->euid ||
140 pcred->euid == cred->euid))
141 return true;
142 if (ns_capable(pcred->user->user_ns, CAP_SYS_NICE))
143 return true;
144 return false;
145}
146
147/*
David Howellsc69e8d92008-11-14 10:39:19 +1100148 * set the priority of a task
149 * - the caller must hold the RCU read lock
150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static int set_one_prio(struct task_struct *p, int niceval, int error)
152{
153 int no_nice;
154
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700155 if (!set_one_prio_perm(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 error = -EPERM;
157 goto out;
158 }
Matt Mackalle43379f2005-05-01 08:59:00 -0700159 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 error = -EACCES;
161 goto out;
162 }
163 no_nice = security_task_setnice(p, niceval);
164 if (no_nice) {
165 error = no_nice;
166 goto out;
167 }
168 if (error == -ESRCH)
169 error = 0;
170 set_user_nice(p, niceval);
171out:
172 return error;
173}
174
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100175SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct task_struct *g, *p;
178 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100179 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 int error = -EINVAL;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800181 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Daniel Walker3e88c552007-05-10 22:22:53 -0700183 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 goto out;
185
186 /* normalize: avoid signed division (rounding problems) */
187 error = -ESRCH;
188 if (niceval < -20)
189 niceval = -20;
190 if (niceval > 19)
191 niceval = 19;
192
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000193 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 read_lock(&tasklist_lock);
195 switch (which) {
196 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800197 if (who)
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700198 p = find_task_by_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800199 else
200 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (p)
202 error = set_one_prio(p, niceval, error);
203 break;
204 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800205 if (who)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700206 pgrp = find_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800207 else
208 pgrp = task_pgrp(current);
Ken Chen2d70b682008-08-20 14:09:17 -0700209 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 error = set_one_prio(p, niceval, error);
Ken Chen2d70b682008-08-20 14:09:17 -0700211 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 break;
213 case PRIO_USER:
David Howellsd84f4f92008-11-14 10:39:23 +1100214 user = (struct user_struct *) cred->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (!who)
David Howells86a264a2008-11-14 10:39:18 +1100216 who = cred->uid;
217 else if ((who != cred->uid) &&
218 !(user = find_user(who)))
219 goto out_unlock; /* No processes for this user */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
H Hartley Sweetendfc6a732009-12-14 18:00:22 -0800221 do_each_thread(g, p) {
David Howells86a264a2008-11-14 10:39:18 +1100222 if (__task_cred(p)->uid == who)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 error = set_one_prio(p, niceval, error);
H Hartley Sweetendfc6a732009-12-14 18:00:22 -0800224 } while_each_thread(g, p);
David Howells86a264a2008-11-14 10:39:18 +1100225 if (who != cred->uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 free_uid(user); /* For find_user() */
227 break;
228 }
229out_unlock:
230 read_unlock(&tasklist_lock);
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000231 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232out:
233 return error;
234}
235
236/*
237 * Ugh. To avoid negative return values, "getpriority()" will
238 * not return the normal nice-value, but a negated value that
239 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
240 * to stay compatible.
241 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100242SYSCALL_DEFINE2(getpriority, int, which, int, who)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 struct task_struct *g, *p;
245 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100246 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 long niceval, retval = -ESRCH;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800248 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Daniel Walker3e88c552007-05-10 22:22:53 -0700250 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return -EINVAL;
252
Tetsuo Handa70118832010-02-22 12:44:16 -0800253 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 read_lock(&tasklist_lock);
255 switch (which) {
256 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800257 if (who)
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700258 p = find_task_by_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800259 else
260 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (p) {
262 niceval = 20 - task_nice(p);
263 if (niceval > retval)
264 retval = niceval;
265 }
266 break;
267 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800268 if (who)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700269 pgrp = find_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800270 else
271 pgrp = task_pgrp(current);
Ken Chen2d70b682008-08-20 14:09:17 -0700272 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 niceval = 20 - task_nice(p);
274 if (niceval > retval)
275 retval = niceval;
Ken Chen2d70b682008-08-20 14:09:17 -0700276 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 break;
278 case PRIO_USER:
David Howells86a264a2008-11-14 10:39:18 +1100279 user = (struct user_struct *) cred->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (!who)
David Howells86a264a2008-11-14 10:39:18 +1100281 who = cred->uid;
282 else if ((who != cred->uid) &&
283 !(user = find_user(who)))
284 goto out_unlock; /* No processes for this user */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
H Hartley Sweetendfc6a732009-12-14 18:00:22 -0800286 do_each_thread(g, p) {
David Howells86a264a2008-11-14 10:39:18 +1100287 if (__task_cred(p)->uid == who) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 niceval = 20 - task_nice(p);
289 if (niceval > retval)
290 retval = niceval;
291 }
H Hartley Sweetendfc6a732009-12-14 18:00:22 -0800292 } while_each_thread(g, p);
David Howells86a264a2008-11-14 10:39:18 +1100293 if (who != cred->uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 free_uid(user); /* for find_user() */
295 break;
296 }
297out_unlock:
298 read_unlock(&tasklist_lock);
Tetsuo Handa70118832010-02-22 12:44:16 -0800299 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 return retval;
302}
303
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700304/**
305 * emergency_restart - reboot the system
306 *
307 * Without shutting down any hardware or taking any locks
308 * reboot the system. This is called when we know we are in
309 * trouble so this is our best effort to reboot. This is
310 * safe to call in interrupt context.
311 */
Eric W. Biederman7c903472005-07-26 11:29:55 -0600312void emergency_restart(void)
313{
Seiji Aguchi04c68622011-01-12 16:59:30 -0800314 kmsg_dump(KMSG_DUMP_EMERG);
Eric W. Biederman7c903472005-07-26 11:29:55 -0600315 machine_emergency_restart();
316}
317EXPORT_SYMBOL_GPL(emergency_restart);
318
Huang Yingca195b72008-08-15 00:40:24 -0700319void kernel_restart_prepare(char *cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600320{
Alan Sterne041c682006-03-27 01:16:30 -0800321 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600322 system_state = SYSTEM_RESTART;
Kay Sieversb50fa7c2011-05-05 13:32:05 +0200323 usermodehelper_disable();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600324 device_shutdown();
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700325}
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800326
327/**
Amerigo Wangc5f41752011-07-25 17:13:10 -0700328 * register_reboot_notifier - Register function to be called at reboot time
329 * @nb: Info about notifier function to be called
330 *
331 * Registers a function with the list of functions
332 * to be called at reboot time.
333 *
334 * Currently always returns zero, as blocking_notifier_chain_register()
335 * always returns zero.
336 */
337int register_reboot_notifier(struct notifier_block *nb)
338{
339 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
340}
341EXPORT_SYMBOL(register_reboot_notifier);
342
343/**
344 * unregister_reboot_notifier - Unregister previously registered reboot notifier
345 * @nb: Hook to be unregistered
346 *
347 * Unregisters a previously registered reboot
348 * notifier function.
349 *
350 * Returns zero on success, or %-ENOENT on failure.
351 */
352int unregister_reboot_notifier(struct notifier_block *nb)
353{
354 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
355}
356EXPORT_SYMBOL(unregister_reboot_notifier);
357
Robin Holtfc1cbc72013-06-12 14:04:37 -0700358/* Add backwards compatibility for stable trees. */
359#ifndef PF_NO_SETAFFINITY
360#define PF_NO_SETAFFINITY PF_THREAD_BOUND
361#endif
362
363static void migrate_to_reboot_cpu(void)
364{
365 /* The boot cpu is always logical cpu 0 */
366 int cpu = 0;
367
368 cpu_hotplug_disable();
369
370 /* Make certain the cpu I'm about to reboot on is online */
371 if (!cpu_online(cpu))
372 cpu = cpumask_first(cpu_online_mask);
373
374 /* Prevent races with other tasks migrating this task */
375 current->flags |= PF_NO_SETAFFINITY;
376
377 /* Make certain I only run on the appropriate processor */
378 set_cpus_allowed_ptr(current, cpumask_of(cpu));
379}
380
Amerigo Wangc5f41752011-07-25 17:13:10 -0700381/**
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800382 * kernel_restart - reboot the system
383 * @cmd: pointer to buffer containing command to execute for restart
Randy Dunlapb8887e62005-11-07 01:01:07 -0800384 * or %NULL
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800385 *
386 * Shutdown everything and perform a clean reboot.
387 * This is not safe to call in interrupt context.
388 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700389void kernel_restart(char *cmd)
390{
391 kernel_restart_prepare(cmd);
Robin Holtfc1cbc72013-06-12 14:04:37 -0700392 migrate_to_reboot_cpu();
Huacai Chene3573b22013-04-07 02:14:14 +0000393 syscore_shutdown();
Cal Peake756184b2006-09-30 23:27:24 -0700394 if (!cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600395 printk(KERN_EMERG "Restarting system.\n");
Cal Peake756184b2006-09-30 23:27:24 -0700396 else
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600397 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
Seiji Aguchi04c68622011-01-12 16:59:30 -0800398 kmsg_dump(KMSG_DUMP_RESTART);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600399 machine_restart(cmd);
400}
401EXPORT_SYMBOL_GPL(kernel_restart);
402
Adrian Bunk4ef72292008-02-04 22:30:06 -0800403static void kernel_shutdown_prepare(enum system_states state)
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500404{
Alan Sterne041c682006-03-27 01:16:30 -0800405 blocking_notifier_call_chain(&reboot_notifier_list,
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500406 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
407 system_state = state;
Kay Sieversb50fa7c2011-05-05 13:32:05 +0200408 usermodehelper_disable();
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500409 device_shutdown();
410}
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700411/**
412 * kernel_halt - halt the system
413 *
414 * Shutdown everything and perform a clean system halt.
415 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700416void kernel_halt(void)
417{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500418 kernel_shutdown_prepare(SYSTEM_HALT);
Robin Holtfc1cbc72013-06-12 14:04:37 -0700419 migrate_to_reboot_cpu();
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100420 syscore_shutdown();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600421 printk(KERN_EMERG "System halted.\n");
Seiji Aguchi04c68622011-01-12 16:59:30 -0800422 kmsg_dump(KMSG_DUMP_HALT);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600423 machine_halt();
424}
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500425
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600426EXPORT_SYMBOL_GPL(kernel_halt);
427
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700428/**
429 * kernel_power_off - power_off the system
430 *
431 * Shutdown everything and perform a clean system power_off.
432 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700433void kernel_power_off(void)
434{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500435 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700436 if (pm_power_off_prepare)
437 pm_power_off_prepare();
Robin Holtfc1cbc72013-06-12 14:04:37 -0700438 migrate_to_reboot_cpu();
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100439 syscore_shutdown();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600440 printk(KERN_EMERG "Power down.\n");
Seiji Aguchi04c68622011-01-12 16:59:30 -0800441 kmsg_dump(KMSG_DUMP_POWEROFF);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600442 machine_power_off();
443}
444EXPORT_SYMBOL_GPL(kernel_power_off);
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200445
446static DEFINE_MUTEX(reboot_mutex);
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448/*
449 * Reboot system call: for obvious reasons only root may call it,
450 * and even root needs to set up some magic numbers in the registers
451 * so that some mistake won't make this reboot the whole machine.
452 * You can also set the meaning of the ctrl-alt-del-key here.
453 *
454 * reboot doesn't sync: do that yourself before calling this.
455 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100456SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
457 void __user *, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 char buffer[256];
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700460 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 /* We only trust the superuser with rebooting the system. */
463 if (!capable(CAP_SYS_BOOT))
464 return -EPERM;
465
466 /* For safety, we require "magic" arguments. */
467 if (magic1 != LINUX_REBOOT_MAGIC1 ||
468 (magic2 != LINUX_REBOOT_MAGIC2 &&
469 magic2 != LINUX_REBOOT_MAGIC2A &&
470 magic2 != LINUX_REBOOT_MAGIC2B &&
471 magic2 != LINUX_REBOOT_MAGIC2C))
472 return -EINVAL;
473
Daniel Lezcanocf3f8922012-03-28 14:42:51 -0700474 /*
475 * If pid namespaces are enabled and the current task is in a child
476 * pid_namespace, the command is handled by reboot_pid_ns() which will
477 * call do_exit().
478 */
479 ret = reboot_pid_ns(task_active_pid_ns(current), cmd);
480 if (ret)
481 return ret;
482
Eric W. Biederman5e382912006-01-08 01:03:46 -0800483 /* Instead of trying to make the power_off code look like
484 * halt when pm_power_off is not set do it the easy way.
485 */
486 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
487 cmd = LINUX_REBOOT_CMD_HALT;
488
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200489 mutex_lock(&reboot_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 switch (cmd) {
491 case LINUX_REBOOT_CMD_RESTART:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600492 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 break;
494
495 case LINUX_REBOOT_CMD_CAD_ON:
496 C_A_D = 1;
497 break;
498
499 case LINUX_REBOOT_CMD_CAD_OFF:
500 C_A_D = 0;
501 break;
502
503 case LINUX_REBOOT_CMD_HALT:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600504 kernel_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 do_exit(0);
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700506 panic("cannot halt");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 case LINUX_REBOOT_CMD_POWER_OFF:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600509 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 do_exit(0);
511 break;
512
513 case LINUX_REBOOT_CMD_RESTART2:
514 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200515 ret = -EFAULT;
516 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518 buffer[sizeof(buffer) - 1] = '\0';
519
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600520 kernel_restart(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 break;
522
Huang Ying3ab83522008-07-25 19:45:07 -0700523#ifdef CONFIG_KEXEC
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700524 case LINUX_REBOOT_CMD_KEXEC:
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700525 ret = kernel_kexec();
526 break;
Huang Ying3ab83522008-07-25 19:45:07 -0700527#endif
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600528
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200529#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 case LINUX_REBOOT_CMD_SW_SUSPEND:
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700531 ret = hibernate();
532 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533#endif
534
535 default:
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700536 ret = -EINVAL;
537 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200539 mutex_unlock(&reboot_mutex);
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700540 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
David Howells65f27f32006-11-22 14:55:48 +0000543static void deferred_cad(struct work_struct *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Eric W. Biedermanabcd9e52005-07-26 11:27:34 -0600545 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
548/*
549 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
550 * As it's called within an interrupt, it may NOT sync: the only choice
551 * is whether to reboot at once, or just ignore the ctrl-alt-del.
552 */
553void ctrl_alt_del(void)
554{
David Howells65f27f32006-11-22 14:55:48 +0000555 static DECLARE_WORK(cad_work, deferred_cad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 if (C_A_D)
558 schedule_work(&cad_work);
559 else
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700560 kill_cad_pid(SIGINT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563/*
564 * Unprivileged users may change the real gid to the effective gid
565 * or vice versa. (BSD-style)
566 *
567 * If you set the real gid at all, or set the effective gid to a value not
568 * equal to the real gid, then the saved gid is set to the new effective gid.
569 *
570 * This makes it possible for a setgid program to completely drop its
571 * privileges, which is often a useful assertion to make when you are doing
572 * a security audit over a program.
573 *
574 * The general idea is that a program which uses just setregid() will be
575 * 100% compatible with BSD. A program which uses just setgid() will be
576 * 100% compatible with POSIX with saved IDs.
577 *
578 * SMP: There are not races, the GIDs are checked only by filesystem
579 * operations (as far as semantic preservation is concerned).
580 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100581SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
David Howellsd84f4f92008-11-14 10:39:23 +1100583 const struct cred *old;
584 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 int retval;
586
David Howellsd84f4f92008-11-14 10:39:23 +1100587 new = prepare_creds();
588 if (!new)
589 return -ENOMEM;
590 old = current_cred();
591
David Howellsd84f4f92008-11-14 10:39:23 +1100592 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (rgid != (gid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100594 if (old->gid == rgid ||
595 old->egid == rgid ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700596 nsown_capable(CAP_SETGID))
David Howellsd84f4f92008-11-14 10:39:23 +1100597 new->gid = rgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 else
David Howellsd84f4f92008-11-14 10:39:23 +1100599 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601 if (egid != (gid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100602 if (old->gid == egid ||
603 old->egid == egid ||
604 old->sgid == egid ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700605 nsown_capable(CAP_SETGID))
David Howellsd84f4f92008-11-14 10:39:23 +1100606 new->egid = egid;
Cal Peake756184b2006-09-30 23:27:24 -0700607 else
David Howellsd84f4f92008-11-14 10:39:23 +1100608 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
David Howellsd84f4f92008-11-14 10:39:23 +1100610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (rgid != (gid_t) -1 ||
David Howellsd84f4f92008-11-14 10:39:23 +1100612 (egid != (gid_t) -1 && egid != old->gid))
613 new->sgid = new->egid;
614 new->fsgid = new->egid;
615
616 return commit_creds(new);
617
618error:
619 abort_creds(new);
620 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623/*
624 * setgid() is implemented like SysV w/ SAVED_IDS
625 *
626 * SMP: Same implicit races as above.
627 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100628SYSCALL_DEFINE1(setgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
David Howellsd84f4f92008-11-14 10:39:23 +1100630 const struct cred *old;
631 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int retval;
633
David Howellsd84f4f92008-11-14 10:39:23 +1100634 new = prepare_creds();
635 if (!new)
636 return -ENOMEM;
637 old = current_cred();
638
David Howellsd84f4f92008-11-14 10:39:23 +1100639 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700640 if (nsown_capable(CAP_SETGID))
David Howellsd84f4f92008-11-14 10:39:23 +1100641 new->gid = new->egid = new->sgid = new->fsgid = gid;
642 else if (gid == old->gid || gid == old->sgid)
643 new->egid = new->fsgid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 else
David Howellsd84f4f92008-11-14 10:39:23 +1100645 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
David Howellsd84f4f92008-11-14 10:39:23 +1100647 return commit_creds(new);
648
649error:
650 abort_creds(new);
651 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
Dhaval Giani54e99122009-02-27 15:13:54 +0530653
David Howellsd84f4f92008-11-14 10:39:23 +1100654/*
655 * change the user struct in a credentials set to match the new UID
656 */
657static int set_user(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 struct user_struct *new_user;
660
Serge Hallyn18b6e042008-10-15 16:38:45 -0500661 new_user = alloc_uid(current_user_ns(), new->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (!new_user)
663 return -EAGAIN;
664
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400665 /*
666 * We don't fail in case of NPROC limit excess here because too many
667 * poorly written programs don't check set*uid() return code, assuming
668 * it never fails if called by root. We may still enforce NPROC limit
669 * for programs doing set*uid()+execve() by harmlessly deferring the
670 * failure to the execve() stage.
671 */
Jiri Slaby78d7d402010-03-05 13:42:54 -0800672 if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400673 new_user != INIT_USER)
674 current->flags |= PF_NPROC_EXCEEDED;
675 else
676 current->flags &= ~PF_NPROC_EXCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
David Howellsd84f4f92008-11-14 10:39:23 +1100678 free_uid(new->user);
679 new->user = new_user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return 0;
681}
682
683/*
684 * Unprivileged users may change the real uid to the effective uid
685 * or vice versa. (BSD-style)
686 *
687 * If you set the real uid at all, or set the effective uid to a value not
688 * equal to the real uid, then the saved uid is set to the new effective uid.
689 *
690 * This makes it possible for a setuid program to completely drop its
691 * privileges, which is often a useful assertion to make when you are doing
692 * a security audit over a program.
693 *
694 * The general idea is that a program which uses just setreuid() will be
695 * 100% compatible with BSD. A program which uses just setuid() will be
696 * 100% compatible with POSIX with saved IDs.
697 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100698SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
David Howellsd84f4f92008-11-14 10:39:23 +1100700 const struct cred *old;
701 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 int retval;
703
David Howellsd84f4f92008-11-14 10:39:23 +1100704 new = prepare_creds();
705 if (!new)
706 return -ENOMEM;
707 old = current_cred();
708
David Howellsd84f4f92008-11-14 10:39:23 +1100709 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (ruid != (uid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100711 new->uid = ruid;
712 if (old->uid != ruid &&
713 old->euid != ruid &&
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700714 !nsown_capable(CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100715 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717
718 if (euid != (uid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100719 new->euid = euid;
720 if (old->uid != euid &&
721 old->euid != euid &&
722 old->suid != euid &&
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700723 !nsown_capable(CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100724 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
Dhaval Giani54e99122009-02-27 15:13:54 +0530727 if (new->uid != old->uid) {
728 retval = set_user(new);
729 if (retval < 0)
730 goto error;
731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (ruid != (uid_t) -1 ||
David Howellsd84f4f92008-11-14 10:39:23 +1100733 (euid != (uid_t) -1 && euid != old->uid))
734 new->suid = new->euid;
735 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
David Howellsd84f4f92008-11-14 10:39:23 +1100737 retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
738 if (retval < 0)
739 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
David Howellsd84f4f92008-11-14 10:39:23 +1100741 return commit_creds(new);
742
743error:
744 abort_creds(new);
745 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748/*
749 * setuid() is implemented like SysV with SAVED_IDS
750 *
751 * Note that SAVED_ID's is deficient in that a setuid root program
752 * like sendmail, for example, cannot set its uid to be a normal
753 * user and then switch back, because if you're root, setuid() sets
754 * the saved uid too. If you don't like this, blame the bright people
755 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
756 * will allow a root program to temporarily drop privileges and be able to
757 * regain them by swapping the real and effective uid.
758 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100759SYSCALL_DEFINE1(setuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
David Howellsd84f4f92008-11-14 10:39:23 +1100761 const struct cred *old;
762 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 int retval;
764
David Howellsd84f4f92008-11-14 10:39:23 +1100765 new = prepare_creds();
766 if (!new)
767 return -ENOMEM;
768 old = current_cred();
769
David Howellsd84f4f92008-11-14 10:39:23 +1100770 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700771 if (nsown_capable(CAP_SETUID)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100772 new->suid = new->uid = uid;
Dhaval Giani54e99122009-02-27 15:13:54 +0530773 if (uid != old->uid) {
774 retval = set_user(new);
775 if (retval < 0)
776 goto error;
David Howellsd84f4f92008-11-14 10:39:23 +1100777 }
778 } else if (uid != old->uid && uid != new->suid) {
779 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
David Howellsd84f4f92008-11-14 10:39:23 +1100782 new->fsuid = new->euid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
David Howellsd84f4f92008-11-14 10:39:23 +1100784 retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
785 if (retval < 0)
786 goto error;
787
788 return commit_creds(new);
789
790error:
791 abort_creds(new);
792 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
795
796/*
797 * This function implements a generic ability to update ruid, euid,
798 * and suid. This allows you to implement the 4.4 compatible seteuid().
799 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100800SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
David Howellsd84f4f92008-11-14 10:39:23 +1100802 const struct cred *old;
803 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 int retval;
805
David Howellsd84f4f92008-11-14 10:39:23 +1100806 new = prepare_creds();
807 if (!new)
808 return -ENOMEM;
809
David Howellsd84f4f92008-11-14 10:39:23 +1100810 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
David Howellsd84f4f92008-11-14 10:39:23 +1100812 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700813 if (!nsown_capable(CAP_SETUID)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100814 if (ruid != (uid_t) -1 && ruid != old->uid &&
815 ruid != old->euid && ruid != old->suid)
816 goto error;
817 if (euid != (uid_t) -1 && euid != old->uid &&
818 euid != old->euid && euid != old->suid)
819 goto error;
820 if (suid != (uid_t) -1 && suid != old->uid &&
821 suid != old->euid && suid != old->suid)
822 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
David Howellsd84f4f92008-11-14 10:39:23 +1100824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (ruid != (uid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100826 new->uid = ruid;
Dhaval Giani54e99122009-02-27 15:13:54 +0530827 if (ruid != old->uid) {
828 retval = set_user(new);
829 if (retval < 0)
830 goto error;
831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
David Howellsd84f4f92008-11-14 10:39:23 +1100833 if (euid != (uid_t) -1)
834 new->euid = euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (suid != (uid_t) -1)
David Howellsd84f4f92008-11-14 10:39:23 +1100836 new->suid = suid;
837 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
David Howellsd84f4f92008-11-14 10:39:23 +1100839 retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
840 if (retval < 0)
841 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
David Howellsd84f4f92008-11-14 10:39:23 +1100843 return commit_creds(new);
844
845error:
846 abort_creds(new);
847 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Heiko Carstensdbf040d2009-01-14 14:14:04 +0100850SYSCALL_DEFINE3(getresuid, uid_t __user *, ruid, uid_t __user *, euid, uid_t __user *, suid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
David Howells86a264a2008-11-14 10:39:18 +1100852 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 int retval;
854
David Howells86a264a2008-11-14 10:39:18 +1100855 if (!(retval = put_user(cred->uid, ruid)) &&
856 !(retval = put_user(cred->euid, euid)))
David Howellsb6dff3e2008-11-14 10:39:16 +1100857 retval = put_user(cred->suid, suid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 return retval;
860}
861
862/*
863 * Same as above, but for rgid, egid, sgid.
864 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100865SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
David Howellsd84f4f92008-11-14 10:39:23 +1100867 const struct cred *old;
868 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 int retval;
870
David Howellsd84f4f92008-11-14 10:39:23 +1100871 new = prepare_creds();
872 if (!new)
873 return -ENOMEM;
874 old = current_cred();
875
David Howellsd84f4f92008-11-14 10:39:23 +1100876 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700877 if (!nsown_capable(CAP_SETGID)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100878 if (rgid != (gid_t) -1 && rgid != old->gid &&
879 rgid != old->egid && rgid != old->sgid)
880 goto error;
881 if (egid != (gid_t) -1 && egid != old->gid &&
882 egid != old->egid && egid != old->sgid)
883 goto error;
884 if (sgid != (gid_t) -1 && sgid != old->gid &&
885 sgid != old->egid && sgid != old->sgid)
886 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
David Howellsd84f4f92008-11-14 10:39:23 +1100889 if (rgid != (gid_t) -1)
890 new->gid = rgid;
891 if (egid != (gid_t) -1)
892 new->egid = egid;
893 if (sgid != (gid_t) -1)
894 new->sgid = sgid;
895 new->fsgid = new->egid;
896
897 return commit_creds(new);
898
899error:
900 abort_creds(new);
901 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Heiko Carstensdbf040d2009-01-14 14:14:04 +0100904SYSCALL_DEFINE3(getresgid, gid_t __user *, rgid, gid_t __user *, egid, gid_t __user *, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
David Howells86a264a2008-11-14 10:39:18 +1100906 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 int retval;
908
David Howells86a264a2008-11-14 10:39:18 +1100909 if (!(retval = put_user(cred->gid, rgid)) &&
910 !(retval = put_user(cred->egid, egid)))
David Howellsb6dff3e2008-11-14 10:39:16 +1100911 retval = put_user(cred->sgid, sgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 return retval;
914}
915
916
917/*
918 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
919 * is used for "access()" and for the NFS daemon (letting nfsd stay at
920 * whatever uid it wants to). It normally shadows "euid", except when
921 * explicitly set by setfsuid() or for access..
922 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100923SYSCALL_DEFINE1(setfsuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
David Howellsd84f4f92008-11-14 10:39:23 +1100925 const struct cred *old;
926 struct cred *new;
927 uid_t old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
David Howellsd84f4f92008-11-14 10:39:23 +1100929 new = prepare_creds();
930 if (!new)
931 return current_fsuid();
932 old = current_cred();
933 old_fsuid = old->fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
David Howellsd84f4f92008-11-14 10:39:23 +1100935 if (uid == old->uid || uid == old->euid ||
936 uid == old->suid || uid == old->fsuid ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700937 nsown_capable(CAP_SETUID)) {
Cal Peake756184b2006-09-30 23:27:24 -0700938 if (uid != old_fsuid) {
David Howellsd84f4f92008-11-14 10:39:23 +1100939 new->fsuid = uid;
940 if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
941 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944
David Howellsd84f4f92008-11-14 10:39:23 +1100945 abort_creds(new);
946 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
David Howellsd84f4f92008-11-14 10:39:23 +1100948change_okay:
949 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return old_fsuid;
951}
952
953/*
John Anthony Kazos Jrf42df9e2007-05-09 08:23:08 +0200954 * Samma på svenska..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100956SYSCALL_DEFINE1(setfsgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
David Howellsd84f4f92008-11-14 10:39:23 +1100958 const struct cred *old;
959 struct cred *new;
960 gid_t old_fsgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
David Howellsd84f4f92008-11-14 10:39:23 +1100962 new = prepare_creds();
963 if (!new)
964 return current_fsgid();
965 old = current_cred();
966 old_fsgid = old->fsgid;
967
David Howellsd84f4f92008-11-14 10:39:23 +1100968 if (gid == old->gid || gid == old->egid ||
969 gid == old->sgid || gid == old->fsgid ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700970 nsown_capable(CAP_SETGID)) {
Cal Peake756184b2006-09-30 23:27:24 -0700971 if (gid != old_fsgid) {
David Howellsd84f4f92008-11-14 10:39:23 +1100972 new->fsgid = gid;
973 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
David Howellsd84f4f92008-11-14 10:39:23 +1100976
David Howellsd84f4f92008-11-14 10:39:23 +1100977 abort_creds(new);
978 return old_fsgid;
979
980change_okay:
981 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return old_fsgid;
983}
984
Frank Mayharf06febc2008-09-12 09:54:39 -0700985void do_sys_times(struct tms *tms)
986{
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +0900987 cputime_t tgutime, tgstime, cutime, cstime;
Frank Mayharf06febc2008-09-12 09:54:39 -0700988
Oleg Nesterov2b5fe6d2008-11-17 15:40:08 +0100989 spin_lock_irq(&current->sighand->siglock);
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +0900990 thread_group_times(current, &tgutime, &tgstime);
Frank Mayharf06febc2008-09-12 09:54:39 -0700991 cutime = current->signal->cutime;
992 cstime = current->signal->cstime;
993 spin_unlock_irq(&current->sighand->siglock);
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +0900994 tms->tms_utime = cputime_to_clock_t(tgutime);
995 tms->tms_stime = cputime_to_clock_t(tgstime);
Frank Mayharf06febc2008-09-12 09:54:39 -0700996 tms->tms_cutime = cputime_to_clock_t(cutime);
997 tms->tms_cstime = cputime_to_clock_t(cstime);
998}
999
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001000SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (tbuf) {
1003 struct tms tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Frank Mayharf06febc2008-09-12 09:54:39 -07001005 do_sys_times(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1007 return -EFAULT;
1008 }
Paul Mackerrase3d5a272009-01-06 14:41:02 -08001009 force_successful_syscall_return();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1011}
1012
1013/*
1014 * This needs some heavy checking ...
1015 * I just haven't the stomach for it. I also don't fully
1016 * understand sessions/pgrp etc. Let somebody who does explain it.
1017 *
1018 * OK, I think I have the protection semantics right.... this is really
1019 * only important on a multi-user system anyway, to make sure one user
1020 * can't send a signal to a process owned by another. -TYT, 12/12/91
1021 *
1022 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1023 * LBT 04.03.94
1024 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +01001025SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 struct task_struct *p;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001028 struct task_struct *group_leader = current->group_leader;
Oleg Nesterov4e021302008-02-08 04:19:08 -08001029 struct pid *pgrp;
1030 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 if (!pid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001033 pid = task_pid_vnr(group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (!pgid)
1035 pgid = pid;
1036 if (pgid < 0)
1037 return -EINVAL;
Paul E. McKenney950eaac2010-08-31 17:00:18 -07001038 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 /* From this point forward we keep holding onto the tasklist lock
1041 * so that our parent does not change from under us. -DaveM
1042 */
1043 write_lock_irq(&tasklist_lock);
1044
1045 err = -ESRCH;
Oleg Nesterov4e021302008-02-08 04:19:08 -08001046 p = find_task_by_vpid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (!p)
1048 goto out;
1049
1050 err = -EINVAL;
1051 if (!thread_group_leader(p))
1052 goto out;
1053
Oleg Nesterov4e021302008-02-08 04:19:08 -08001054 if (same_thread_group(p->real_parent, group_leader)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 err = -EPERM;
Eric W. Biederman41487c62007-02-12 00:53:01 -08001056 if (task_session(p) != task_session(group_leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 goto out;
1058 err = -EACCES;
1059 if (p->did_exec)
1060 goto out;
1061 } else {
1062 err = -ESRCH;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001063 if (p != group_leader)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 goto out;
1065 }
1066
1067 err = -EPERM;
1068 if (p->signal->leader)
1069 goto out;
1070
Oleg Nesterov4e021302008-02-08 04:19:08 -08001071 pgrp = task_pid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (pgid != pid) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001073 struct task_struct *g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Oleg Nesterov4e021302008-02-08 04:19:08 -08001075 pgrp = find_vpid(pgid);
1076 g = pid_task(pgrp, PIDTYPE_PGID);
Eric W. Biederman41487c62007-02-12 00:53:01 -08001077 if (!g || task_session(g) != task_session(group_leader))
Oleg Nesterovf020bc42006-12-08 02:38:02 -08001078 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 err = security_task_setpgid(p, pgid);
1082 if (err)
1083 goto out;
1084
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07001085 if (task_pgrp(p) != pgrp)
Oleg Nesterov83beaf32008-04-30 00:54:27 -07001086 change_pid(p, PIDTYPE_PGID, pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 err = 0;
1089out:
1090 /* All paths lead to here, thus we are safe. -DaveM */
1091 write_unlock_irq(&tasklist_lock);
Paul E. McKenney950eaac2010-08-31 17:00:18 -07001092 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return err;
1094}
1095
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001096SYSCALL_DEFINE1(getpgid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Oleg Nesterov12a3de02008-04-30 00:54:29 -07001098 struct task_struct *p;
1099 struct pid *grp;
1100 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Oleg Nesterov12a3de02008-04-30 00:54:29 -07001102 rcu_read_lock();
1103 if (!pid)
1104 grp = task_pgrp(current);
1105 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 retval = -ESRCH;
Oleg Nesterov12a3de02008-04-30 00:54:29 -07001107 p = find_task_by_vpid(pid);
1108 if (!p)
1109 goto out;
1110 grp = task_pgrp(p);
1111 if (!grp)
1112 goto out;
1113
1114 retval = security_task_getpgid(p);
1115 if (retval)
1116 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
Oleg Nesterov12a3de02008-04-30 00:54:29 -07001118 retval = pid_vnr(grp);
1119out:
1120 rcu_read_unlock();
1121 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
1124#ifdef __ARCH_WANT_SYS_GETPGRP
1125
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001126SYSCALL_DEFINE0(getpgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Oleg Nesterov12a3de02008-04-30 00:54:29 -07001128 return sys_getpgid(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
1131#endif
1132
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001133SYSCALL_DEFINE1(getsid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Oleg Nesterov1dd768c2008-04-30 00:54:28 -07001135 struct task_struct *p;
1136 struct pid *sid;
1137 int retval;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001138
Oleg Nesterov1dd768c2008-04-30 00:54:28 -07001139 rcu_read_lock();
1140 if (!pid)
1141 sid = task_session(current);
1142 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 retval = -ESRCH;
Oleg Nesterov1dd768c2008-04-30 00:54:28 -07001144 p = find_task_by_vpid(pid);
1145 if (!p)
1146 goto out;
1147 sid = task_session(p);
1148 if (!sid)
1149 goto out;
1150
1151 retval = security_task_getsid(p);
1152 if (retval)
1153 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
Oleg Nesterov1dd768c2008-04-30 00:54:28 -07001155 retval = pid_vnr(sid);
1156out:
1157 rcu_read_unlock();
1158 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
Heiko Carstensb290ebe2009-01-14 14:14:06 +01001161SYSCALL_DEFINE0(setsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Oren Laadane19f2472006-01-08 01:03:58 -08001163 struct task_struct *group_leader = current->group_leader;
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001164 struct pid *sid = task_pid(group_leader);
1165 pid_t session = pid_vnr(sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 int err = -EPERM;
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 write_lock_irq(&tasklist_lock);
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001169 /* Fail if I am already a session leader */
1170 if (group_leader->signal->leader)
1171 goto out;
1172
Oleg Nesterov430c6232008-02-08 04:19:11 -08001173 /* Fail if a process group id already exists that equals the
1174 * proposed session id.
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001175 */
Oleg Nesterov6806aac2008-02-08 04:19:12 -08001176 if (pid_task(sid, PIDTYPE_PGID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 goto out;
1178
Oren Laadane19f2472006-01-08 01:03:58 -08001179 group_leader->signal->leader = 1;
Oleg Nesterov8520d7c2008-02-08 04:19:09 -08001180 __set_special_pids(sid);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001181
Alan Cox9c9f4de2008-10-13 10:37:26 +01001182 proc_clear_tty(group_leader);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001183
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001184 err = session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185out:
1186 write_unlock_irq(&tasklist_lock);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001187 if (err > 0) {
Christian Borntraeger0d0df592009-10-26 16:49:34 -07001188 proc_sid_connector(group_leader);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001189 sched_autogroup_create_attach(group_leader);
1190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return err;
1192}
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194DECLARE_RWSEM(uts_sem);
1195
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001196#ifdef COMPAT_UTS_MACHINE
1197#define override_architecture(name) \
Andreas Schwab46da2762010-04-23 13:17:44 -04001198 (personality(current->personality) == PER_LINUX32 && \
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001199 copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1200 sizeof(COMPAT_UTS_MACHINE)))
1201#else
1202#define override_architecture(name) 0
1203#endif
1204
Andi Kleenbe274252011-08-19 16:15:10 -07001205/*
1206 * Work around broken programs that cannot handle "Linux 3.0".
1207 * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40
1208 */
Kees Cook643fde82012-10-19 13:56:51 -07001209static int override_release(char __user *release, size_t len)
Andi Kleenbe274252011-08-19 16:15:10 -07001210{
1211 int ret = 0;
Andi Kleenbe274252011-08-19 16:15:10 -07001212
1213 if (current->personality & UNAME26) {
Kees Cook643fde82012-10-19 13:56:51 -07001214 const char *rest = UTS_RELEASE;
1215 char buf[65] = { 0 };
Andi Kleenbe274252011-08-19 16:15:10 -07001216 int ndots = 0;
1217 unsigned v;
Kees Cook643fde82012-10-19 13:56:51 -07001218 size_t copy;
Andi Kleenbe274252011-08-19 16:15:10 -07001219
1220 while (*rest) {
1221 if (*rest == '.' && ++ndots >= 3)
1222 break;
1223 if (!isdigit(*rest) && *rest != '.')
1224 break;
1225 rest++;
1226 }
1227 v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 40;
Kees Cook36800302012-10-19 18:45:53 -07001228 copy = clamp_t(size_t, len, 1, sizeof(buf));
Kees Cook643fde82012-10-19 13:56:51 -07001229 copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
1230 ret = copy_to_user(release, buf, copy + 1);
Andi Kleenbe274252011-08-19 16:15:10 -07001231 }
1232 return ret;
1233}
1234
Heiko Carstense48fbb62009-01-14 14:14:26 +01001235SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 int errno = 0;
1238
1239 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001240 if (copy_to_user(name, utsname(), sizeof *name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 errno = -EFAULT;
1242 up_read(&uts_sem);
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001243
Andi Kleenbe274252011-08-19 16:15:10 -07001244 if (!errno && override_release(name->release, sizeof(name->release)))
1245 errno = -EFAULT;
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001246 if (!errno && override_architecture(name))
1247 errno = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return errno;
1249}
1250
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001251#ifdef __ARCH_WANT_SYS_OLD_UNAME
1252/*
1253 * Old cruft
1254 */
1255SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
1256{
1257 int error = 0;
1258
1259 if (!name)
1260 return -EFAULT;
1261
1262 down_read(&uts_sem);
1263 if (copy_to_user(name, utsname(), sizeof(*name)))
1264 error = -EFAULT;
1265 up_read(&uts_sem);
1266
Andi Kleenbe274252011-08-19 16:15:10 -07001267 if (!error && override_release(name->release, sizeof(name->release)))
1268 error = -EFAULT;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001269 if (!error && override_architecture(name))
1270 error = -EFAULT;
1271 return error;
1272}
1273
1274SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
1275{
1276 int error;
1277
1278 if (!name)
1279 return -EFAULT;
1280 if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
1281 return -EFAULT;
1282
1283 down_read(&uts_sem);
1284 error = __copy_to_user(&name->sysname, &utsname()->sysname,
1285 __OLD_UTS_LEN);
1286 error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
1287 error |= __copy_to_user(&name->nodename, &utsname()->nodename,
1288 __OLD_UTS_LEN);
1289 error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
1290 error |= __copy_to_user(&name->release, &utsname()->release,
1291 __OLD_UTS_LEN);
1292 error |= __put_user(0, name->release + __OLD_UTS_LEN);
1293 error |= __copy_to_user(&name->version, &utsname()->version,
1294 __OLD_UTS_LEN);
1295 error |= __put_user(0, name->version + __OLD_UTS_LEN);
1296 error |= __copy_to_user(&name->machine, &utsname()->machine,
1297 __OLD_UTS_LEN);
1298 error |= __put_user(0, name->machine + __OLD_UTS_LEN);
1299 up_read(&uts_sem);
1300
1301 if (!error && override_architecture(name))
1302 error = -EFAULT;
Andi Kleenbe274252011-08-19 16:15:10 -07001303 if (!error && override_release(name->release, sizeof(name->release)))
1304 error = -EFAULT;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001305 return error ? -EFAULT : 0;
1306}
1307#endif
1308
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001309SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 int errno;
1312 char tmp[__NEW_UTS_LEN];
1313
Serge E. Hallynbb96a6f2011-03-23 16:43:18 -07001314 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (len < 0 || len > __NEW_UTS_LEN)
1318 return -EINVAL;
1319 down_write(&uts_sem);
1320 errno = -EFAULT;
1321 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001322 struct new_utsname *u = utsname();
1323
1324 memcpy(u->nodename, tmp, len);
1325 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 errno = 0;
1327 }
Lucas De Marchif1ecf062011-11-02 13:39:22 -07001328 uts_proc_notify(UTS_PROC_HOSTNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 up_write(&uts_sem);
1330 return errno;
1331}
1332
1333#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1334
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001335SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 int i, errno;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001338 struct new_utsname *u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 if (len < 0)
1341 return -EINVAL;
1342 down_read(&uts_sem);
Andrew Morton9679e4d2008-10-15 22:01:51 -07001343 u = utsname();
1344 i = 1 + strlen(u->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (i > len)
1346 i = len;
1347 errno = 0;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001348 if (copy_to_user(name, u->nodename, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 errno = -EFAULT;
1350 up_read(&uts_sem);
1351 return errno;
1352}
1353
1354#endif
1355
1356/*
1357 * Only setdomainname; getdomainname can be implemented by calling
1358 * uname()
1359 */
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001360SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 int errno;
1363 char tmp[__NEW_UTS_LEN];
1364
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001365 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return -EPERM;
1367 if (len < 0 || len > __NEW_UTS_LEN)
1368 return -EINVAL;
1369
1370 down_write(&uts_sem);
1371 errno = -EFAULT;
1372 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001373 struct new_utsname *u = utsname();
1374
1375 memcpy(u->domainname, tmp, len);
1376 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 errno = 0;
1378 }
Lucas De Marchif1ecf062011-11-02 13:39:22 -07001379 uts_proc_notify(UTS_PROC_DOMAINNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 up_write(&uts_sem);
1381 return errno;
1382}
1383
Heiko Carstense48fbb62009-01-14 14:14:26 +01001384SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Jiri Slabyb9518342010-05-04 11:28:25 +02001386 struct rlimit value;
1387 int ret;
1388
1389 ret = do_prlimit(current, resource, NULL, &value);
1390 if (!ret)
1391 ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1392
1393 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
1396#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1397
1398/*
1399 * Back compatibility for getrlimit. Needed for some apps.
1400 */
1401
Heiko Carstense48fbb62009-01-14 14:14:26 +01001402SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1403 struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 struct rlimit x;
1406 if (resource >= RLIM_NLIMITS)
1407 return -EINVAL;
1408
1409 task_lock(current->group_leader);
1410 x = current->signal->rlim[resource];
1411 task_unlock(current->group_leader);
Cal Peake756184b2006-09-30 23:27:24 -07001412 if (x.rlim_cur > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 x.rlim_cur = 0x7FFFFFFF;
Cal Peake756184b2006-09-30 23:27:24 -07001414 if (x.rlim_max > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 x.rlim_max = 0x7FFFFFFF;
1416 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1417}
1418
1419#endif
1420
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001421static inline bool rlim64_is_infinity(__u64 rlim64)
1422{
1423#if BITS_PER_LONG < 64
1424 return rlim64 >= ULONG_MAX;
1425#else
1426 return rlim64 == RLIM64_INFINITY;
1427#endif
1428}
1429
1430static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1431{
1432 if (rlim->rlim_cur == RLIM_INFINITY)
1433 rlim64->rlim_cur = RLIM64_INFINITY;
1434 else
1435 rlim64->rlim_cur = rlim->rlim_cur;
1436 if (rlim->rlim_max == RLIM_INFINITY)
1437 rlim64->rlim_max = RLIM64_INFINITY;
1438 else
1439 rlim64->rlim_max = rlim->rlim_max;
1440}
1441
1442static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1443{
1444 if (rlim64_is_infinity(rlim64->rlim_cur))
1445 rlim->rlim_cur = RLIM_INFINITY;
1446 else
1447 rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1448 if (rlim64_is_infinity(rlim64->rlim_max))
1449 rlim->rlim_max = RLIM_INFINITY;
1450 else
1451 rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1452}
1453
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001454/* make sure you are allowed to change @tsk limits before calling this */
Jiri Slaby5b415352010-03-24 16:11:29 +01001455int do_prlimit(struct task_struct *tsk, unsigned int resource,
1456 struct rlimit *new_rlim, struct rlimit *old_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Jiri Slaby5b415352010-03-24 16:11:29 +01001458 struct rlimit *rlim;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001459 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 if (resource >= RLIM_NLIMITS)
1462 return -EINVAL;
Jiri Slaby5b415352010-03-24 16:11:29 +01001463 if (new_rlim) {
1464 if (new_rlim->rlim_cur > new_rlim->rlim_max)
1465 return -EINVAL;
1466 if (resource == RLIMIT_NOFILE &&
1467 new_rlim->rlim_max > sysctl_nr_open)
1468 return -EPERM;
1469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001471 /* protect tsk->signal and tsk->sighand from disappearing */
1472 read_lock(&tasklist_lock);
1473 if (!tsk->sighand) {
1474 retval = -ESRCH;
1475 goto out;
1476 }
1477
Jiri Slaby5b415352010-03-24 16:11:29 +01001478 rlim = tsk->signal->rlim + resource;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001479 task_lock(tsk->group_leader);
Jiri Slaby5b415352010-03-24 16:11:29 +01001480 if (new_rlim) {
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001481 /* Keep the capable check against init_user_ns until
1482 cgroups can contain all limits */
Jiri Slaby5b415352010-03-24 16:11:29 +01001483 if (new_rlim->rlim_max > rlim->rlim_max &&
1484 !capable(CAP_SYS_RESOURCE))
1485 retval = -EPERM;
1486 if (!retval)
1487 retval = security_task_setrlimit(tsk->group_leader,
1488 resource, new_rlim);
1489 if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
1490 /*
1491 * The caller is asking for an immediate RLIMIT_CPU
1492 * expiry. But we use the zero value to mean "it was
1493 * never set". So let's cheat and make it one second
1494 * instead
1495 */
1496 new_rlim->rlim_cur = 1;
1497 }
Tom Alsberg9926e4c2007-05-08 00:30:31 -07001498 }
Jiri Slaby5b415352010-03-24 16:11:29 +01001499 if (!retval) {
1500 if (old_rlim)
1501 *old_rlim = *rlim;
1502 if (new_rlim)
1503 *rlim = *new_rlim;
1504 }
Jiri Slaby7855c352009-08-26 23:45:34 +02001505 task_unlock(tsk->group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Andrew Mortond3561f72006-03-24 03:18:36 -08001507 /*
1508 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1509 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1510 * very long-standing error, and fixing it now risks breakage of
1511 * applications, so we live with it
1512 */
Jiri Slaby5b415352010-03-24 16:11:29 +01001513 if (!retval && new_rlim && resource == RLIMIT_CPU &&
1514 new_rlim->rlim_cur != RLIM_INFINITY)
1515 update_rlimit_cpu(tsk, new_rlim->rlim_cur);
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001516out:
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001517 read_unlock(&tasklist_lock);
Oleg Nesterov2fb9d262009-09-03 19:21:45 +02001518 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001521/* rcu lock must be held */
1522static int check_prlimit_permission(struct task_struct *task)
1523{
1524 const struct cred *cred = current_cred(), *tcred;
1525
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001526 if (current == task)
1527 return 0;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001528
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001529 tcred = __task_cred(task);
1530 if (cred->user->user_ns == tcred->user->user_ns &&
1531 (cred->uid == tcred->euid &&
1532 cred->uid == tcred->suid &&
1533 cred->uid == tcred->uid &&
1534 cred->gid == tcred->egid &&
1535 cred->gid == tcred->sgid &&
1536 cred->gid == tcred->gid))
1537 return 0;
1538 if (ns_capable(tcred->user->user_ns, CAP_SYS_RESOURCE))
1539 return 0;
1540
1541 return -EPERM;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001542}
1543
1544SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1545 const struct rlimit64 __user *, new_rlim,
1546 struct rlimit64 __user *, old_rlim)
1547{
1548 struct rlimit64 old64, new64;
1549 struct rlimit old, new;
1550 struct task_struct *tsk;
1551 int ret;
1552
1553 if (new_rlim) {
1554 if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1555 return -EFAULT;
1556 rlim64_to_rlim(&new64, &new);
1557 }
1558
1559 rcu_read_lock();
1560 tsk = pid ? find_task_by_vpid(pid) : current;
1561 if (!tsk) {
1562 rcu_read_unlock();
1563 return -ESRCH;
1564 }
1565 ret = check_prlimit_permission(tsk);
1566 if (ret) {
1567 rcu_read_unlock();
1568 return ret;
1569 }
1570 get_task_struct(tsk);
1571 rcu_read_unlock();
1572
1573 ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1574 old_rlim ? &old : NULL);
1575
1576 if (!ret && old_rlim) {
1577 rlim_to_rlim64(&old, &old64);
1578 if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1579 ret = -EFAULT;
1580 }
1581
1582 put_task_struct(tsk);
1583 return ret;
1584}
1585
Jiri Slaby7855c352009-08-26 23:45:34 +02001586SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1587{
1588 struct rlimit new_rlim;
1589
1590 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1591 return -EFAULT;
Jiri Slaby5b415352010-03-24 16:11:29 +01001592 return do_prlimit(current, resource, &new_rlim, NULL);
Jiri Slaby7855c352009-08-26 23:45:34 +02001593}
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595/*
1596 * It would make sense to put struct rusage in the task_struct,
1597 * except that would make the task_struct be *really big*. After
1598 * task_struct gets moved into malloc'ed memory, it would
1599 * make sense to do this. It will make moving the rest of the information
1600 * a lot simpler! (Which we're not doing right now because we're not
1601 * measuring them yet).
1602 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1604 * races with threads incrementing their own counters. But since word
1605 * reads are atomic, we either get new values or old values and we don't
1606 * care which for the sums. We always take the siglock to protect reading
1607 * the c* fields from p->signal from races with exit.c updating those
1608 * fields when reaping, so a sample either gets all the additions of a
1609 * given child after it's reaped, or none so this sample is before reaping.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001610 *
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001611 * Locking:
1612 * We need to take the siglock for CHILDEREN, SELF and BOTH
1613 * for the cases current multithreaded, non-current single threaded
1614 * non-current multithreaded. Thread traversal is now safe with
1615 * the siglock held.
1616 * Strictly speaking, we donot need to take the siglock if we are current and
1617 * single threaded, as no one else can take our signal_struct away, no one
1618 * else can reap the children to update signal->c* counters, and no one else
1619 * can race with the signal-> fields. If we do not take any lock, the
1620 * signal-> fields could be read out of order while another thread was just
1621 * exiting. So we should place a read memory barrier when we avoid the lock.
1622 * On the writer side, write memory barrier is implied in __exit_signal
1623 * as __exit_signal releases the siglock spinlock after updating the signal->
1624 * fields. But we don't do this yet to keep things simple.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001625 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 */
1627
Frank Mayharf06febc2008-09-12 09:54:39 -07001628static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001629{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001630 r->ru_nvcsw += t->nvcsw;
1631 r->ru_nivcsw += t->nivcsw;
1632 r->ru_minflt += t->min_flt;
1633 r->ru_majflt += t->maj_flt;
1634 r->ru_inblock += task_io_get_inblock(t);
1635 r->ru_oublock += task_io_get_oublock(t);
1636}
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1639{
1640 struct task_struct *t;
1641 unsigned long flags;
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09001642 cputime_t tgutime, tgstime, utime, stime;
Jiri Pirko1f102062009-09-22 16:44:10 -07001643 unsigned long maxrss = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 memset((char *) r, 0, sizeof *r);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001646 utime = stime = 0;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001647
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001648 if (who == RUSAGE_THREAD) {
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09001649 task_times(current, &utime, &stime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001650 accumulate_thread_rusage(p, r);
Jiri Pirko1f102062009-09-22 16:44:10 -07001651 maxrss = p->signal->maxrss;
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001652 goto out;
1653 }
1654
Oleg Nesterovd6cf7232008-04-30 00:52:38 -07001655 if (!lock_task_sighand(p, &flags))
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001656 return;
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 switch (who) {
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001659 case RUSAGE_BOTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 case RUSAGE_CHILDREN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 utime = p->signal->cutime;
1662 stime = p->signal->cstime;
1663 r->ru_nvcsw = p->signal->cnvcsw;
1664 r->ru_nivcsw = p->signal->cnivcsw;
1665 r->ru_minflt = p->signal->cmin_flt;
1666 r->ru_majflt = p->signal->cmaj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07001667 r->ru_inblock = p->signal->cinblock;
1668 r->ru_oublock = p->signal->coublock;
Jiri Pirko1f102062009-09-22 16:44:10 -07001669 maxrss = p->signal->cmaxrss;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001670
1671 if (who == RUSAGE_CHILDREN)
1672 break;
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 case RUSAGE_SELF:
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09001675 thread_group_times(p, &tgutime, &tgstime);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001676 utime += tgutime;
1677 stime += tgstime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 r->ru_nvcsw += p->signal->nvcsw;
1679 r->ru_nivcsw += p->signal->nivcsw;
1680 r->ru_minflt += p->signal->min_flt;
1681 r->ru_majflt += p->signal->maj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07001682 r->ru_inblock += p->signal->inblock;
1683 r->ru_oublock += p->signal->oublock;
Jiri Pirko1f102062009-09-22 16:44:10 -07001684 if (maxrss < p->signal->maxrss)
1685 maxrss = p->signal->maxrss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 t = p;
1687 do {
Frank Mayharf06febc2008-09-12 09:54:39 -07001688 accumulate_thread_rusage(t, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 t = next_thread(t);
1690 } while (t != p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 break;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 default:
1694 BUG();
1695 }
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001696 unlock_task_sighand(p, &flags);
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001697
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001698out:
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001699 cputime_to_timeval(utime, &r->ru_utime);
1700 cputime_to_timeval(stime, &r->ru_stime);
Jiri Pirko1f102062009-09-22 16:44:10 -07001701
1702 if (who != RUSAGE_CHILDREN) {
1703 struct mm_struct *mm = get_task_mm(p);
1704 if (mm) {
1705 setmax_mm_hiwater_rss(&maxrss, mm);
1706 mmput(mm);
1707 }
1708 }
1709 r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
1711
1712int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1713{
1714 struct rusage r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 k_getrusage(p, who, &r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1717}
1718
Heiko Carstense48fbb62009-01-14 14:14:26 +01001719SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001721 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1722 who != RUSAGE_THREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return -EINVAL;
1724 return getrusage(current, who, ru);
1725}
1726
Heiko Carstense48fbb62009-01-14 14:14:26 +01001727SYSCALL_DEFINE1(umask, int, mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1730 return mask;
1731}
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -08001732
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001733#ifdef CONFIG_CHECKPOINT_RESTORE
1734static int prctl_set_mm(int opt, unsigned long addr,
1735 unsigned long arg4, unsigned long arg5)
1736{
1737 unsigned long rlim = rlimit(RLIMIT_DATA);
1738 unsigned long vm_req_flags;
1739 unsigned long vm_bad_flags;
1740 struct vm_area_struct *vma;
1741 int error = 0;
1742 struct mm_struct *mm = current->mm;
1743
1744 if (arg4 | arg5)
1745 return -EINVAL;
1746
Cyrill Gorcunov79f07132012-03-15 15:17:10 -07001747 if (!capable(CAP_SYS_RESOURCE))
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001748 return -EPERM;
1749
1750 if (addr >= TASK_SIZE)
1751 return -EINVAL;
1752
1753 down_read(&mm->mmap_sem);
1754 vma = find_vma(mm, addr);
1755
1756 if (opt != PR_SET_MM_START_BRK && opt != PR_SET_MM_BRK) {
1757 /* It must be existing VMA */
1758 if (!vma || vma->vm_start > addr)
1759 goto out;
1760 }
1761
1762 error = -EINVAL;
1763 switch (opt) {
1764 case PR_SET_MM_START_CODE:
1765 case PR_SET_MM_END_CODE:
1766 vm_req_flags = VM_READ | VM_EXEC;
1767 vm_bad_flags = VM_WRITE | VM_MAYSHARE;
1768
1769 if ((vma->vm_flags & vm_req_flags) != vm_req_flags ||
1770 (vma->vm_flags & vm_bad_flags))
1771 goto out;
1772
1773 if (opt == PR_SET_MM_START_CODE)
1774 mm->start_code = addr;
1775 else
1776 mm->end_code = addr;
1777 break;
1778
1779 case PR_SET_MM_START_DATA:
1780 case PR_SET_MM_END_DATA:
1781 vm_req_flags = VM_READ | VM_WRITE;
1782 vm_bad_flags = VM_EXEC | VM_MAYSHARE;
1783
1784 if ((vma->vm_flags & vm_req_flags) != vm_req_flags ||
1785 (vma->vm_flags & vm_bad_flags))
1786 goto out;
1787
1788 if (opt == PR_SET_MM_START_DATA)
1789 mm->start_data = addr;
1790 else
1791 mm->end_data = addr;
1792 break;
1793
1794 case PR_SET_MM_START_STACK:
1795
1796#ifdef CONFIG_STACK_GROWSUP
1797 vm_req_flags = VM_READ | VM_WRITE | VM_GROWSUP;
1798#else
1799 vm_req_flags = VM_READ | VM_WRITE | VM_GROWSDOWN;
1800#endif
1801 if ((vma->vm_flags & vm_req_flags) != vm_req_flags)
1802 goto out;
1803
1804 mm->start_stack = addr;
1805 break;
1806
1807 case PR_SET_MM_START_BRK:
1808 if (addr <= mm->end_data)
1809 goto out;
1810
1811 if (rlim < RLIM_INFINITY &&
1812 (mm->brk - addr) +
1813 (mm->end_data - mm->start_data) > rlim)
1814 goto out;
1815
1816 mm->start_brk = addr;
1817 break;
1818
1819 case PR_SET_MM_BRK:
1820 if (addr <= mm->end_data)
1821 goto out;
1822
1823 if (rlim < RLIM_INFINITY &&
1824 (addr - mm->start_brk) +
1825 (mm->end_data - mm->start_data) > rlim)
1826 goto out;
1827
1828 mm->brk = addr;
1829 break;
1830
1831 default:
1832 error = -EINVAL;
1833 goto out;
1834 }
1835
1836 error = 0;
1837
1838out:
1839 up_read(&mm->mmap_sem);
1840
1841 return error;
1842}
1843#else /* CONFIG_CHECKPOINT_RESTORE */
1844static int prctl_set_mm(int opt, unsigned long addr,
1845 unsigned long arg4, unsigned long arg5)
1846{
1847 return -EINVAL;
1848}
1849#endif
1850
Colin Cross8ad62c22013-06-26 17:26:01 -07001851
1852static int prctl_update_vma_anon_name(struct vm_area_struct *vma,
1853 struct vm_area_struct **prev,
1854 unsigned long start, unsigned long end,
1855 const char __user *name_addr)
1856{
1857 struct mm_struct * mm = vma->vm_mm;
1858 int error = 0;
1859 pgoff_t pgoff;
1860
1861 if (name_addr == vma_get_anon_name(vma)) {
1862 *prev = vma;
1863 goto out;
1864 }
1865
1866 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
1867 *prev = vma_merge(mm, *prev, start, end, vma->vm_flags, vma->anon_vma,
1868 vma->vm_file, pgoff, vma_policy(vma),
1869 name_addr);
1870 if (*prev) {
1871 vma = *prev;
1872 goto success;
1873 }
1874
1875 *prev = vma;
1876
1877 if (start != vma->vm_start) {
1878 error = split_vma(mm, vma, start, 1);
1879 if (error)
1880 goto out;
1881 }
1882
1883 if (end != vma->vm_end) {
1884 error = split_vma(mm, vma, end, 0);
1885 if (error)
1886 goto out;
1887 }
1888
1889success:
1890 if (!vma->vm_file)
1891 vma->shared.anon_name = name_addr;
1892
1893out:
1894 if (error == -ENOMEM)
1895 error = -EAGAIN;
1896 return error;
1897}
1898
1899static int prctl_set_vma_anon_name(unsigned long start, unsigned long end,
1900 unsigned long arg)
1901{
1902 unsigned long tmp;
1903 struct vm_area_struct * vma, *prev;
1904 int unmapped_error = 0;
1905 int error = -EINVAL;
1906
1907 /*
1908 * If the interval [start,end) covers some unmapped address
1909 * ranges, just ignore them, but return -ENOMEM at the end.
1910 * - this matches the handling in madvise.
1911 */
1912 vma = find_vma_prev(current->mm, start, &prev);
1913 if (vma && start > vma->vm_start)
1914 prev = vma;
1915
1916 for (;;) {
1917 /* Still start < end. */
1918 error = -ENOMEM;
1919 if (!vma)
1920 return error;
1921
1922 /* Here start < (end|vma->vm_end). */
1923 if (start < vma->vm_start) {
1924 unmapped_error = -ENOMEM;
1925 start = vma->vm_start;
1926 if (start >= end)
1927 return error;
1928 }
1929
1930 /* Here vma->vm_start <= start < (end|vma->vm_end) */
1931 tmp = vma->vm_end;
1932 if (end < tmp)
1933 tmp = end;
1934
1935 /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
1936 error = prctl_update_vma_anon_name(vma, &prev, start, end,
1937 (const char __user *)arg);
1938 if (error)
1939 return error;
1940 start = tmp;
1941 if (prev && start < prev->vm_end)
1942 start = prev->vm_end;
1943 error = unmapped_error;
1944 if (start >= end)
1945 return error;
1946 if (prev)
1947 vma = prev->vm_next;
1948 else /* madvise_remove dropped mmap_sem */
1949 vma = find_vma(current->mm, start);
1950 }
1951}
1952
1953static int prctl_set_vma(unsigned long opt, unsigned long start,
1954 unsigned long len_in, unsigned long arg)
1955{
1956 struct mm_struct *mm = current->mm;
1957 int error;
1958 unsigned long len;
1959 unsigned long end;
1960
1961 if (start & ~PAGE_MASK)
1962 return -EINVAL;
1963 len = (len_in + ~PAGE_MASK) & PAGE_MASK;
1964
1965 /* Check to see whether len was rounded up from small -ve to zero */
1966 if (len_in && !len)
1967 return -EINVAL;
1968
1969 end = start + len;
1970 if (end < start)
1971 return -EINVAL;
1972
1973 if (end == start)
1974 return 0;
1975
1976 down_write(&mm->mmap_sem);
1977
1978 switch (opt) {
1979 case PR_SET_VMA_ANON_NAME:
1980 error = prctl_set_vma_anon_name(start, end, arg);
1981 break;
1982 default:
1983 error = -EINVAL;
1984 }
1985
1986 up_write(&mm->mmap_sem);
1987
1988 return error;
1989}
1990
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001991SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
1992 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
David Howellsb6dff3e2008-11-14 10:39:16 +11001994 struct task_struct *me = current;
1995 unsigned char comm[sizeof(me->comm)];
1996 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
David Howellsd84f4f92008-11-14 10:39:23 +11001998 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1999 if (error != -ENOSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return error;
2001
David Howellsd84f4f92008-11-14 10:39:23 +11002002 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 switch (option) {
2004 case PR_SET_PDEATHSIG:
Jesper Juhl0730ded2005-09-06 15:17:37 -07002005 if (!valid_signal(arg2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 error = -EINVAL;
2007 break;
2008 }
David Howellsb6dff3e2008-11-14 10:39:16 +11002009 me->pdeath_signal = arg2;
2010 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 break;
2012 case PR_GET_PDEATHSIG:
David Howellsb6dff3e2008-11-14 10:39:16 +11002013 error = put_user(me->pdeath_signal, (int __user *)arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 break;
2015 case PR_GET_DUMPABLE:
David Howellsb6dff3e2008-11-14 10:39:16 +11002016 error = get_dumpable(me->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 break;
2018 case PR_SET_DUMPABLE:
Marcel Holtmannabf75a52006-07-12 13:12:00 +02002019 if (arg2 < 0 || arg2 > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 error = -EINVAL;
2021 break;
2022 }
David Howellsb6dff3e2008-11-14 10:39:16 +11002023 set_dumpable(me->mm, arg2);
2024 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 break;
2026
2027 case PR_SET_UNALIGN:
David Howellsb6dff3e2008-11-14 10:39:16 +11002028 error = SET_UNALIGN_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 break;
2030 case PR_GET_UNALIGN:
David Howellsb6dff3e2008-11-14 10:39:16 +11002031 error = GET_UNALIGN_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 break;
2033 case PR_SET_FPEMU:
David Howellsb6dff3e2008-11-14 10:39:16 +11002034 error = SET_FPEMU_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 break;
2036 case PR_GET_FPEMU:
David Howellsb6dff3e2008-11-14 10:39:16 +11002037 error = GET_FPEMU_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 break;
2039 case PR_SET_FPEXC:
David Howellsb6dff3e2008-11-14 10:39:16 +11002040 error = SET_FPEXC_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 break;
2042 case PR_GET_FPEXC:
David Howellsb6dff3e2008-11-14 10:39:16 +11002043 error = GET_FPEXC_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 break;
2045 case PR_GET_TIMING:
2046 error = PR_TIMING_STATISTICAL;
2047 break;
2048 case PR_SET_TIMING:
Shi Weihua7b266552008-05-23 13:04:59 -07002049 if (arg2 != PR_TIMING_STATISTICAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 error = -EINVAL;
David Howellsb6dff3e2008-11-14 10:39:16 +11002051 else
2052 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 break;
2054
David Howellsb6dff3e2008-11-14 10:39:16 +11002055 case PR_SET_NAME:
2056 comm[sizeof(me->comm)-1] = 0;
2057 if (strncpy_from_user(comm, (char __user *)arg2,
2058 sizeof(me->comm) - 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 return -EFAULT;
David Howellsb6dff3e2008-11-14 10:39:16 +11002060 set_task_comm(me, comm);
Vladimir Zapolskiyf786ecb2011-09-21 09:26:44 +00002061 proc_comm_connector(me);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return 0;
David Howellsb6dff3e2008-11-14 10:39:16 +11002063 case PR_GET_NAME:
2064 get_task_comm(comm, me);
2065 if (copy_to_user((char __user *)arg2, comm,
2066 sizeof(comm)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return -EFAULT;
2068 return 0;
Anton Blanchard651d7652006-06-07 16:10:19 +10002069 case PR_GET_ENDIAN:
David Howellsb6dff3e2008-11-14 10:39:16 +11002070 error = GET_ENDIAN(me, arg2);
Anton Blanchard651d7652006-06-07 16:10:19 +10002071 break;
2072 case PR_SET_ENDIAN:
David Howellsb6dff3e2008-11-14 10:39:16 +11002073 error = SET_ENDIAN(me, arg2);
Anton Blanchard651d7652006-06-07 16:10:19 +10002074 break;
2075
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -07002076 case PR_GET_SECCOMP:
2077 error = prctl_get_seccomp();
2078 break;
2079 case PR_SET_SECCOMP:
2080 error = prctl_set_seccomp(arg2);
2081 break;
Erik Bosman8fb402b2008-04-11 18:54:17 +02002082 case PR_GET_TSC:
2083 error = GET_TSC_CTL(arg2);
2084 break;
2085 case PR_SET_TSC:
2086 error = SET_TSC_CTL(arg2);
2087 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002088 case PR_TASK_PERF_EVENTS_DISABLE:
2089 error = perf_event_task_disable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01002090 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002091 case PR_TASK_PERF_EVENTS_ENABLE:
2092 error = perf_event_task_enable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01002093 break;
Arjan van de Ven69766752008-09-01 15:52:40 -07002094 case PR_GET_TIMERSLACK:
2095 error = current->timer_slack_ns;
2096 break;
2097 case PR_SET_TIMERSLACK:
2098 if (arg2 <= 0)
2099 current->timer_slack_ns =
2100 current->default_timer_slack_ns;
2101 else
2102 current->timer_slack_ns = arg2;
David Howellsb6dff3e2008-11-14 10:39:16 +11002103 error = 0;
Arjan van de Ven69766752008-09-01 15:52:40 -07002104 break;
Andi Kleen4db96cf2009-09-16 11:50:14 +02002105 case PR_MCE_KILL:
2106 if (arg4 | arg5)
2107 return -EINVAL;
2108 switch (arg2) {
Andi Kleen1087e9b2009-10-04 02:20:11 +02002109 case PR_MCE_KILL_CLEAR:
Andi Kleen4db96cf2009-09-16 11:50:14 +02002110 if (arg3 != 0)
2111 return -EINVAL;
2112 current->flags &= ~PF_MCE_PROCESS;
2113 break;
Andi Kleen1087e9b2009-10-04 02:20:11 +02002114 case PR_MCE_KILL_SET:
Andi Kleen4db96cf2009-09-16 11:50:14 +02002115 current->flags |= PF_MCE_PROCESS;
Andi Kleen1087e9b2009-10-04 02:20:11 +02002116 if (arg3 == PR_MCE_KILL_EARLY)
Andi Kleen4db96cf2009-09-16 11:50:14 +02002117 current->flags |= PF_MCE_EARLY;
Andi Kleen1087e9b2009-10-04 02:20:11 +02002118 else if (arg3 == PR_MCE_KILL_LATE)
Andi Kleen4db96cf2009-09-16 11:50:14 +02002119 current->flags &= ~PF_MCE_EARLY;
Andi Kleen1087e9b2009-10-04 02:20:11 +02002120 else if (arg3 == PR_MCE_KILL_DEFAULT)
2121 current->flags &=
2122 ~(PF_MCE_EARLY|PF_MCE_PROCESS);
2123 else
2124 return -EINVAL;
Andi Kleen4db96cf2009-09-16 11:50:14 +02002125 break;
2126 default:
2127 return -EINVAL;
2128 }
2129 error = 0;
2130 break;
Andi Kleen1087e9b2009-10-04 02:20:11 +02002131 case PR_MCE_KILL_GET:
2132 if (arg2 | arg3 | arg4 | arg5)
2133 return -EINVAL;
2134 if (current->flags & PF_MCE_PROCESS)
2135 error = (current->flags & PF_MCE_EARLY) ?
2136 PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
2137 else
2138 error = PR_MCE_KILL_DEFAULT;
2139 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002140 case PR_SET_MM:
2141 error = prctl_set_mm(arg2, arg3, arg4, arg5);
2142 break;
Lennart Poetteringebec18a2012-03-23 15:01:54 -07002143 case PR_SET_CHILD_SUBREAPER:
2144 me->signal->is_child_subreaper = !!arg2;
2145 error = 0;
2146 break;
2147 case PR_GET_CHILD_SUBREAPER:
2148 error = put_user(me->signal->is_child_subreaper,
2149 (int __user *) arg2);
2150 break;
Colin Cross8ad62c22013-06-26 17:26:01 -07002151 case PR_SET_VMA:
2152 error = prctl_set_vma(arg2, arg3, arg4, arg5);
2153 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 default:
2155 error = -EINVAL;
2156 break;
2157 }
2158 return error;
2159}
Andi Kleen3cfc3482006-09-26 10:52:28 +02002160
Heiko Carstens836f92a2009-01-14 14:14:33 +01002161SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
2162 struct getcpu_cache __user *, unused)
Andi Kleen3cfc3482006-09-26 10:52:28 +02002163{
2164 int err = 0;
2165 int cpu = raw_smp_processor_id();
2166 if (cpup)
2167 err |= put_user(cpu, cpup);
2168 if (nodep)
2169 err |= put_user(cpu_to_node(cpu), nodep);
Andi Kleen3cfc3482006-09-26 10:52:28 +02002170 return err ? -EFAULT : 0;
2171}
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002172
2173char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
2174
Neil Hormana06a4dc2010-05-26 14:42:58 -07002175static void argv_cleanup(struct subprocess_info *info)
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002176{
Neil Hormana06a4dc2010-05-26 14:42:58 -07002177 argv_free(info->argv);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002178}
2179
2180/**
2181 * orderly_poweroff - Trigger an orderly system poweroff
2182 * @force: force poweroff if command execution fails
2183 *
2184 * This may be called from any context to trigger a system shutdown.
2185 * If the orderly shutdown fails, it will force an immediate shutdown.
2186 */
2187int orderly_poweroff(bool force)
2188{
2189 int argc;
2190 char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
2191 static char *envp[] = {
2192 "HOME=/",
2193 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
2194 NULL
2195 };
2196 int ret = -ENOMEM;
2197 struct subprocess_info *info;
2198
2199 if (argv == NULL) {
2200 printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
2201 __func__, poweroff_cmd);
2202 goto out;
2203 }
2204
KOSAKI Motohiroac331d12008-07-25 01:45:38 -07002205 info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002206 if (info == NULL) {
2207 argv_free(argv);
2208 goto out;
2209 }
2210
Neil Hormana06a4dc2010-05-26 14:42:58 -07002211 call_usermodehelper_setfns(info, NULL, argv_cleanup, NULL);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002212
Jeremy Fitzhardinge86313c42007-07-17 18:37:03 -07002213 ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002214
2215 out:
2216 if (ret && force) {
2217 printk(KERN_WARNING "Failed to start orderly shutdown: "
2218 "forcing the issue\n");
2219
2220 /* I guess this should try to kick off some daemon to
2221 sync and poweroff asap. Or not even bother syncing
2222 if we're doing an emergency shutdown? */
2223 emergency_sync();
2224 kernel_power_off();
2225 }
2226
2227 return ret;
2228}
2229EXPORT_SYMBOL_GPL(orderly_poweroff);