blob: f0c43b4b6657eeb0ddc11b93fc3795f110e84f73 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <linux/compat.h>
45#include <linux/syscalls.h>
Keshavamurthy Anil S00d7c052005-12-12 00:37:33 -080046#include <linux/kprobes.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070047#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Seiji Aguchi04c68622011-01-12 16:59:30 -080049#include <linux/kmsg_dump.h>
Andi Kleenbe274252011-08-19 16:15:10 -070050/* Move somewhere else to avoid recompiling? */
51#include <generated/utsrelease.h>
Seiji Aguchi04c68622011-01-12 16:59:30 -080052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/uaccess.h>
54#include <asm/io.h>
55#include <asm/unistd.h>
56
57#ifndef SET_UNALIGN_CTL
58# define SET_UNALIGN_CTL(a,b) (-EINVAL)
59#endif
60#ifndef GET_UNALIGN_CTL
61# define GET_UNALIGN_CTL(a,b) (-EINVAL)
62#endif
63#ifndef SET_FPEMU_CTL
64# define SET_FPEMU_CTL(a,b) (-EINVAL)
65#endif
66#ifndef GET_FPEMU_CTL
67# define GET_FPEMU_CTL(a,b) (-EINVAL)
68#endif
69#ifndef SET_FPEXC_CTL
70# define SET_FPEXC_CTL(a,b) (-EINVAL)
71#endif
72#ifndef GET_FPEXC_CTL
73# define GET_FPEXC_CTL(a,b) (-EINVAL)
74#endif
Anton Blanchard651d7652006-06-07 16:10:19 +100075#ifndef GET_ENDIAN
76# define GET_ENDIAN(a,b) (-EINVAL)
77#endif
78#ifndef SET_ENDIAN
79# define SET_ENDIAN(a,b) (-EINVAL)
80#endif
Erik Bosman8fb402b2008-04-11 18:54:17 +020081#ifndef GET_TSC_CTL
82# define GET_TSC_CTL(a) (-EINVAL)
83#endif
84#ifndef SET_TSC_CTL
85# define SET_TSC_CTL(a) (-EINVAL)
86#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88/*
89 * this is where the system-wide overflow UID and GID are defined, for
90 * architectures that now have 32-bit UID/GID but didn't in the past
91 */
92
93int overflowuid = DEFAULT_OVERFLOWUID;
94int overflowgid = DEFAULT_OVERFLOWGID;
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096EXPORT_SYMBOL(overflowuid);
97EXPORT_SYMBOL(overflowgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/*
100 * the same as above, but for filesystems which can only store a 16-bit
101 * UID and GID. as such, this is needed on all architectures
102 */
103
104int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
105int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
106
107EXPORT_SYMBOL(fs_overflowuid);
108EXPORT_SYMBOL(fs_overflowgid);
109
110/*
111 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
112 */
113
114int C_A_D = 1;
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700115struct pid *cad_pid;
116EXPORT_SYMBOL(cad_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/*
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700119 * If set, this is used for preparing the system to power off.
120 */
121
122void (*pm_power_off_prepare)(void);
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700123
David Howellsc69e8d92008-11-14 10:39:19 +1100124/*
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700125 * Returns true if current's euid is same as p's uid or euid,
126 * or has CAP_SYS_NICE to p's user_ns.
127 *
128 * Called with rcu_read_lock, creds are safe
129 */
130static bool set_one_prio_perm(struct task_struct *p)
131{
132 const struct cred *cred = current_cred(), *pcred = __task_cred(p);
133
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800134 if (pcred->user_ns == cred->user_ns &&
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700135 (pcred->uid == cred->euid ||
136 pcred->euid == cred->euid))
137 return true;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800138 if (ns_capable(pcred->user_ns, CAP_SYS_NICE))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700139 return true;
140 return false;
141}
142
143/*
David Howellsc69e8d92008-11-14 10:39:19 +1100144 * set the priority of a task
145 * - the caller must hold the RCU read lock
146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static int set_one_prio(struct task_struct *p, int niceval, int error)
148{
149 int no_nice;
150
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700151 if (!set_one_prio_perm(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 error = -EPERM;
153 goto out;
154 }
Matt Mackalle43379f2005-05-01 08:59:00 -0700155 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 error = -EACCES;
157 goto out;
158 }
159 no_nice = security_task_setnice(p, niceval);
160 if (no_nice) {
161 error = no_nice;
162 goto out;
163 }
164 if (error == -ESRCH)
165 error = 0;
166 set_user_nice(p, niceval);
167out:
168 return error;
169}
170
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100171SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 struct task_struct *g, *p;
174 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100175 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 int error = -EINVAL;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800177 struct pid *pgrp;
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800178 kuid_t cred_uid;
179 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Daniel Walker3e88c552007-05-10 22:22:53 -0700181 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 goto out;
183
184 /* normalize: avoid signed division (rounding problems) */
185 error = -ESRCH;
186 if (niceval < -20)
187 niceval = -20;
188 if (niceval > 19)
189 niceval = 19;
190
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000191 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 read_lock(&tasklist_lock);
193 switch (which) {
194 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800195 if (who)
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700196 p = find_task_by_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800197 else
198 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (p)
200 error = set_one_prio(p, niceval, error);
201 break;
202 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800203 if (who)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700204 pgrp = find_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800205 else
206 pgrp = task_pgrp(current);
Ken Chen2d70b682008-08-20 14:09:17 -0700207 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 error = set_one_prio(p, niceval, error);
Ken Chen2d70b682008-08-20 14:09:17 -0700209 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 break;
211 case PRIO_USER:
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800212 cred_uid = make_kuid(cred->user_ns, cred->uid);
213 uid = make_kuid(cred->user_ns, who);
Eric W. Biederman74ba5082012-03-03 18:58:11 -0800214 user = cred->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (!who)
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800216 uid = cred_uid;
217 else if (!uid_eq(uid, cred_uid) &&
218 !(user = find_user(uid)))
David Howells86a264a2008-11-14 10:39:18 +1100219 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) {
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800222 const struct cred *tcred = __task_cred(p);
223 kuid_t tcred_uid = make_kuid(tcred->user_ns, tcred->uid);
224 if (uid_eq(tcred_uid, uid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 error = set_one_prio(p, niceval, error);
H Hartley Sweetendfc6a732009-12-14 18:00:22 -0800226 } while_each_thread(g, p);
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800227 if (!uid_eq(uid, cred_uid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 free_uid(user); /* For find_user() */
229 break;
230 }
231out_unlock:
232 read_unlock(&tasklist_lock);
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000233 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234out:
235 return error;
236}
237
238/*
239 * Ugh. To avoid negative return values, "getpriority()" will
240 * not return the normal nice-value, but a negated value that
241 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
242 * to stay compatible.
243 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100244SYSCALL_DEFINE2(getpriority, int, which, int, who)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 struct task_struct *g, *p;
247 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100248 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 long niceval, retval = -ESRCH;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800250 struct pid *pgrp;
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800251 kuid_t cred_uid;
252 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Daniel Walker3e88c552007-05-10 22:22:53 -0700254 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -EINVAL;
256
Tetsuo Handa70118832010-02-22 12:44:16 -0800257 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 read_lock(&tasklist_lock);
259 switch (which) {
260 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800261 if (who)
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700262 p = find_task_by_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800263 else
264 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (p) {
266 niceval = 20 - task_nice(p);
267 if (niceval > retval)
268 retval = niceval;
269 }
270 break;
271 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800272 if (who)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700273 pgrp = find_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800274 else
275 pgrp = task_pgrp(current);
Ken Chen2d70b682008-08-20 14:09:17 -0700276 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 niceval = 20 - task_nice(p);
278 if (niceval > retval)
279 retval = niceval;
Ken Chen2d70b682008-08-20 14:09:17 -0700280 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 break;
282 case PRIO_USER:
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800283 cred_uid = make_kuid(cred->user_ns, cred->uid);
284 uid = make_kuid(cred->user_ns, who);
Eric W. Biederman74ba5082012-03-03 18:58:11 -0800285 user = cred->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (!who)
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800287 uid = cred_uid;
288 else if (!uid_eq(uid, cred_uid) &&
289 !(user = find_user(uid)))
David Howells86a264a2008-11-14 10:39:18 +1100290 goto out_unlock; /* No processes for this user */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
H Hartley Sweetendfc6a732009-12-14 18:00:22 -0800292 do_each_thread(g, p) {
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800293 const struct cred *tcred = __task_cred(p);
294 kuid_t tcred_uid = make_kuid(tcred->user_ns, tcred->uid);
295 if (uid_eq(tcred_uid, uid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 niceval = 20 - task_nice(p);
297 if (niceval > retval)
298 retval = niceval;
299 }
H Hartley Sweetendfc6a732009-12-14 18:00:22 -0800300 } while_each_thread(g, p);
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800301 if (!uid_eq(uid, cred_uid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 free_uid(user); /* for find_user() */
303 break;
304 }
305out_unlock:
306 read_unlock(&tasklist_lock);
Tetsuo Handa70118832010-02-22 12:44:16 -0800307 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 return retval;
310}
311
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700312/**
313 * emergency_restart - reboot the system
314 *
315 * Without shutting down any hardware or taking any locks
316 * reboot the system. This is called when we know we are in
317 * trouble so this is our best effort to reboot. This is
318 * safe to call in interrupt context.
319 */
Eric W. Biederman7c903472005-07-26 11:29:55 -0600320void emergency_restart(void)
321{
Seiji Aguchi04c68622011-01-12 16:59:30 -0800322 kmsg_dump(KMSG_DUMP_EMERG);
Eric W. Biederman7c903472005-07-26 11:29:55 -0600323 machine_emergency_restart();
324}
325EXPORT_SYMBOL_GPL(emergency_restart);
326
Huang Yingca195b72008-08-15 00:40:24 -0700327void kernel_restart_prepare(char *cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600328{
Alan Sterne041c682006-03-27 01:16:30 -0800329 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600330 system_state = SYSTEM_RESTART;
Kay Sieversb50fa7c2011-05-05 13:32:05 +0200331 usermodehelper_disable();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600332 device_shutdown();
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100333 syscore_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
367/**
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800368 * kernel_restart - reboot the system
369 * @cmd: pointer to buffer containing command to execute for restart
Randy Dunlapb8887e62005-11-07 01:01:07 -0800370 * or %NULL
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800371 *
372 * Shutdown everything and perform a clean reboot.
373 * This is not safe to call in interrupt context.
374 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700375void kernel_restart(char *cmd)
376{
377 kernel_restart_prepare(cmd);
Cal Peake756184b2006-09-30 23:27:24 -0700378 if (!cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600379 printk(KERN_EMERG "Restarting system.\n");
Cal Peake756184b2006-09-30 23:27:24 -0700380 else
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600381 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
Seiji Aguchi04c68622011-01-12 16:59:30 -0800382 kmsg_dump(KMSG_DUMP_RESTART);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600383 machine_restart(cmd);
384}
385EXPORT_SYMBOL_GPL(kernel_restart);
386
Adrian Bunk4ef72292008-02-04 22:30:06 -0800387static void kernel_shutdown_prepare(enum system_states state)
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500388{
Alan Sterne041c682006-03-27 01:16:30 -0800389 blocking_notifier_call_chain(&reboot_notifier_list,
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500390 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
391 system_state = state;
Kay Sieversb50fa7c2011-05-05 13:32:05 +0200392 usermodehelper_disable();
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500393 device_shutdown();
394}
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700395/**
396 * kernel_halt - halt the system
397 *
398 * Shutdown everything and perform a clean system halt.
399 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700400void kernel_halt(void)
401{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500402 kernel_shutdown_prepare(SYSTEM_HALT);
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100403 syscore_shutdown();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600404 printk(KERN_EMERG "System halted.\n");
Seiji Aguchi04c68622011-01-12 16:59:30 -0800405 kmsg_dump(KMSG_DUMP_HALT);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600406 machine_halt();
407}
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500408
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600409EXPORT_SYMBOL_GPL(kernel_halt);
410
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700411/**
412 * kernel_power_off - power_off the system
413 *
414 * Shutdown everything and perform a clean system power_off.
415 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700416void kernel_power_off(void)
417{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500418 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700419 if (pm_power_off_prepare)
420 pm_power_off_prepare();
Mark Lord40477272007-10-01 01:20:10 -0700421 disable_nonboot_cpus();
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100422 syscore_shutdown();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600423 printk(KERN_EMERG "Power down.\n");
Seiji Aguchi04c68622011-01-12 16:59:30 -0800424 kmsg_dump(KMSG_DUMP_POWEROFF);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600425 machine_power_off();
426}
427EXPORT_SYMBOL_GPL(kernel_power_off);
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200428
429static DEFINE_MUTEX(reboot_mutex);
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/*
432 * Reboot system call: for obvious reasons only root may call it,
433 * and even root needs to set up some magic numbers in the registers
434 * so that some mistake won't make this reboot the whole machine.
435 * You can also set the meaning of the ctrl-alt-del-key here.
436 *
437 * reboot doesn't sync: do that yourself before calling this.
438 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100439SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
440 void __user *, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 char buffer[256];
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700443 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /* We only trust the superuser with rebooting the system. */
446 if (!capable(CAP_SYS_BOOT))
447 return -EPERM;
448
449 /* For safety, we require "magic" arguments. */
450 if (magic1 != LINUX_REBOOT_MAGIC1 ||
451 (magic2 != LINUX_REBOOT_MAGIC2 &&
452 magic2 != LINUX_REBOOT_MAGIC2A &&
453 magic2 != LINUX_REBOOT_MAGIC2B &&
454 magic2 != LINUX_REBOOT_MAGIC2C))
455 return -EINVAL;
456
Daniel Lezcanocf3f8922012-03-28 14:42:51 -0700457 /*
458 * If pid namespaces are enabled and the current task is in a child
459 * pid_namespace, the command is handled by reboot_pid_ns() which will
460 * call do_exit().
461 */
462 ret = reboot_pid_ns(task_active_pid_ns(current), cmd);
463 if (ret)
464 return ret;
465
Eric W. Biederman5e382912006-01-08 01:03:46 -0800466 /* Instead of trying to make the power_off code look like
467 * halt when pm_power_off is not set do it the easy way.
468 */
469 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
470 cmd = LINUX_REBOOT_CMD_HALT;
471
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200472 mutex_lock(&reboot_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 switch (cmd) {
474 case LINUX_REBOOT_CMD_RESTART:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600475 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 break;
477
478 case LINUX_REBOOT_CMD_CAD_ON:
479 C_A_D = 1;
480 break;
481
482 case LINUX_REBOOT_CMD_CAD_OFF:
483 C_A_D = 0;
484 break;
485
486 case LINUX_REBOOT_CMD_HALT:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600487 kernel_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 do_exit(0);
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700489 panic("cannot halt");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 case LINUX_REBOOT_CMD_POWER_OFF:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600492 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 do_exit(0);
494 break;
495
496 case LINUX_REBOOT_CMD_RESTART2:
497 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200498 ret = -EFAULT;
499 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501 buffer[sizeof(buffer) - 1] = '\0';
502
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600503 kernel_restart(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 break;
505
Huang Ying3ab83522008-07-25 19:45:07 -0700506#ifdef CONFIG_KEXEC
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700507 case LINUX_REBOOT_CMD_KEXEC:
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700508 ret = kernel_kexec();
509 break;
Huang Ying3ab83522008-07-25 19:45:07 -0700510#endif
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600511
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200512#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 case LINUX_REBOOT_CMD_SW_SUSPEND:
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700514 ret = hibernate();
515 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516#endif
517
518 default:
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700519 ret = -EINVAL;
520 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200522 mutex_unlock(&reboot_mutex);
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700523 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
David Howells65f27f32006-11-22 14:55:48 +0000526static void deferred_cad(struct work_struct *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Eric W. Biedermanabcd9e52005-07-26 11:27:34 -0600528 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531/*
532 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
533 * As it's called within an interrupt, it may NOT sync: the only choice
534 * is whether to reboot at once, or just ignore the ctrl-alt-del.
535 */
536void ctrl_alt_del(void)
537{
David Howells65f27f32006-11-22 14:55:48 +0000538 static DECLARE_WORK(cad_work, deferred_cad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 if (C_A_D)
541 schedule_work(&cad_work);
542 else
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700543 kill_cad_pid(SIGINT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546/*
547 * Unprivileged users may change the real gid to the effective gid
548 * or vice versa. (BSD-style)
549 *
550 * If you set the real gid at all, or set the effective gid to a value not
551 * equal to the real gid, then the saved gid is set to the new effective gid.
552 *
553 * This makes it possible for a setgid program to completely drop its
554 * privileges, which is often a useful assertion to make when you are doing
555 * a security audit over a program.
556 *
557 * The general idea is that a program which uses just setregid() will be
558 * 100% compatible with BSD. A program which uses just setgid() will be
559 * 100% compatible with POSIX with saved IDs.
560 *
561 * SMP: There are not races, the GIDs are checked only by filesystem
562 * operations (as far as semantic preservation is concerned).
563 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100564SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
David Howellsd84f4f92008-11-14 10:39:23 +1100566 const struct cred *old;
567 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 int retval;
569
David Howellsd84f4f92008-11-14 10:39:23 +1100570 new = prepare_creds();
571 if (!new)
572 return -ENOMEM;
573 old = current_cred();
574
David Howellsd84f4f92008-11-14 10:39:23 +1100575 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (rgid != (gid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100577 if (old->gid == rgid ||
578 old->egid == rgid ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700579 nsown_capable(CAP_SETGID))
David Howellsd84f4f92008-11-14 10:39:23 +1100580 new->gid = rgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 else
David Howellsd84f4f92008-11-14 10:39:23 +1100582 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584 if (egid != (gid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100585 if (old->gid == egid ||
586 old->egid == egid ||
587 old->sgid == egid ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700588 nsown_capable(CAP_SETGID))
David Howellsd84f4f92008-11-14 10:39:23 +1100589 new->egid = egid;
Cal Peake756184b2006-09-30 23:27:24 -0700590 else
David Howellsd84f4f92008-11-14 10:39:23 +1100591 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
David Howellsd84f4f92008-11-14 10:39:23 +1100593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (rgid != (gid_t) -1 ||
David Howellsd84f4f92008-11-14 10:39:23 +1100595 (egid != (gid_t) -1 && egid != old->gid))
596 new->sgid = new->egid;
597 new->fsgid = new->egid;
598
599 return commit_creds(new);
600
601error:
602 abort_creds(new);
603 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606/*
607 * setgid() is implemented like SysV w/ SAVED_IDS
608 *
609 * SMP: Same implicit races as above.
610 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100611SYSCALL_DEFINE1(setgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
David Howellsd84f4f92008-11-14 10:39:23 +1100613 const struct cred *old;
614 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 int retval;
616
David Howellsd84f4f92008-11-14 10:39:23 +1100617 new = prepare_creds();
618 if (!new)
619 return -ENOMEM;
620 old = current_cred();
621
David Howellsd84f4f92008-11-14 10:39:23 +1100622 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700623 if (nsown_capable(CAP_SETGID))
David Howellsd84f4f92008-11-14 10:39:23 +1100624 new->gid = new->egid = new->sgid = new->fsgid = gid;
625 else if (gid == old->gid || gid == old->sgid)
626 new->egid = new->fsgid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -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 return commit_creds(new);
631
632error:
633 abort_creds(new);
634 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
Dhaval Giani54e99122009-02-27 15:13:54 +0530636
David Howellsd84f4f92008-11-14 10:39:23 +1100637/*
638 * change the user struct in a credentials set to match the new UID
639 */
640static int set_user(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 struct user_struct *new_user;
643
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800644 new_user = alloc_uid(make_kuid(new->user_ns, new->uid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (!new_user)
646 return -EAGAIN;
647
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400648 /*
649 * We don't fail in case of NPROC limit excess here because too many
650 * poorly written programs don't check set*uid() return code, assuming
651 * it never fails if called by root. We may still enforce NPROC limit
652 * for programs doing set*uid()+execve() by harmlessly deferring the
653 * failure to the execve() stage.
654 */
Jiri Slaby78d7d402010-03-05 13:42:54 -0800655 if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400656 new_user != INIT_USER)
657 current->flags |= PF_NPROC_EXCEEDED;
658 else
659 current->flags &= ~PF_NPROC_EXCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
David Howellsd84f4f92008-11-14 10:39:23 +1100661 free_uid(new->user);
662 new->user = new_user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return 0;
664}
665
666/*
667 * Unprivileged users may change the real uid to the effective uid
668 * or vice versa. (BSD-style)
669 *
670 * If you set the real uid at all, or set the effective uid to a value not
671 * equal to the real uid, then the saved uid is set to the new effective uid.
672 *
673 * This makes it possible for a setuid program to completely drop its
674 * privileges, which is often a useful assertion to make when you are doing
675 * a security audit over a program.
676 *
677 * The general idea is that a program which uses just setreuid() will be
678 * 100% compatible with BSD. A program which uses just setuid() will be
679 * 100% compatible with POSIX with saved IDs.
680 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100681SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
David Howellsd84f4f92008-11-14 10:39:23 +1100683 const struct cred *old;
684 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 int retval;
686
David Howellsd84f4f92008-11-14 10:39:23 +1100687 new = prepare_creds();
688 if (!new)
689 return -ENOMEM;
690 old = current_cred();
691
David Howellsd84f4f92008-11-14 10:39:23 +1100692 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (ruid != (uid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100694 new->uid = ruid;
695 if (old->uid != ruid &&
696 old->euid != ruid &&
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700697 !nsown_capable(CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100698 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700
701 if (euid != (uid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100702 new->euid = euid;
703 if (old->uid != euid &&
704 old->euid != euid &&
705 old->suid != euid &&
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700706 !nsown_capable(CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100707 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
Dhaval Giani54e99122009-02-27 15:13:54 +0530710 if (new->uid != old->uid) {
711 retval = set_user(new);
712 if (retval < 0)
713 goto error;
714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (ruid != (uid_t) -1 ||
David Howellsd84f4f92008-11-14 10:39:23 +1100716 (euid != (uid_t) -1 && euid != old->uid))
717 new->suid = new->euid;
718 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
David Howellsd84f4f92008-11-14 10:39:23 +1100720 retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
721 if (retval < 0)
722 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
David Howellsd84f4f92008-11-14 10:39:23 +1100724 return commit_creds(new);
725
726error:
727 abort_creds(new);
728 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731/*
732 * setuid() is implemented like SysV with SAVED_IDS
733 *
734 * Note that SAVED_ID's is deficient in that a setuid root program
735 * like sendmail, for example, cannot set its uid to be a normal
736 * user and then switch back, because if you're root, setuid() sets
737 * the saved uid too. If you don't like this, blame the bright people
738 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
739 * will allow a root program to temporarily drop privileges and be able to
740 * regain them by swapping the real and effective uid.
741 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100742SYSCALL_DEFINE1(setuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
David Howellsd84f4f92008-11-14 10:39:23 +1100744 const struct cred *old;
745 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int retval;
747
David Howellsd84f4f92008-11-14 10:39:23 +1100748 new = prepare_creds();
749 if (!new)
750 return -ENOMEM;
751 old = current_cred();
752
David Howellsd84f4f92008-11-14 10:39:23 +1100753 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700754 if (nsown_capable(CAP_SETUID)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100755 new->suid = new->uid = uid;
Dhaval Giani54e99122009-02-27 15:13:54 +0530756 if (uid != old->uid) {
757 retval = set_user(new);
758 if (retval < 0)
759 goto error;
David Howellsd84f4f92008-11-14 10:39:23 +1100760 }
761 } else if (uid != old->uid && uid != new->suid) {
762 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
David Howellsd84f4f92008-11-14 10:39:23 +1100765 new->fsuid = new->euid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
David Howellsd84f4f92008-11-14 10:39:23 +1100767 retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
768 if (retval < 0)
769 goto error;
770
771 return commit_creds(new);
772
773error:
774 abort_creds(new);
775 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
778
779/*
780 * This function implements a generic ability to update ruid, euid,
781 * and suid. This allows you to implement the 4.4 compatible seteuid().
782 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100783SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
David Howellsd84f4f92008-11-14 10:39:23 +1100785 const struct cred *old;
786 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 int retval;
788
David Howellsd84f4f92008-11-14 10:39:23 +1100789 new = prepare_creds();
790 if (!new)
791 return -ENOMEM;
792
David Howellsd84f4f92008-11-14 10:39:23 +1100793 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
David Howellsd84f4f92008-11-14 10:39:23 +1100795 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700796 if (!nsown_capable(CAP_SETUID)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100797 if (ruid != (uid_t) -1 && ruid != old->uid &&
798 ruid != old->euid && ruid != old->suid)
799 goto error;
800 if (euid != (uid_t) -1 && euid != old->uid &&
801 euid != old->euid && euid != old->suid)
802 goto error;
803 if (suid != (uid_t) -1 && suid != old->uid &&
804 suid != old->euid && suid != old->suid)
805 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
David Howellsd84f4f92008-11-14 10:39:23 +1100807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (ruid != (uid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100809 new->uid = ruid;
Dhaval Giani54e99122009-02-27 15:13:54 +0530810 if (ruid != old->uid) {
811 retval = set_user(new);
812 if (retval < 0)
813 goto error;
814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
David Howellsd84f4f92008-11-14 10:39:23 +1100816 if (euid != (uid_t) -1)
817 new->euid = euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (suid != (uid_t) -1)
David Howellsd84f4f92008-11-14 10:39:23 +1100819 new->suid = suid;
820 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
David Howellsd84f4f92008-11-14 10:39:23 +1100822 retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
823 if (retval < 0)
824 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
David Howellsd84f4f92008-11-14 10:39:23 +1100826 return commit_creds(new);
827
828error:
829 abort_creds(new);
830 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Heiko Carstensdbf040d2009-01-14 14:14:04 +0100833SYSCALL_DEFINE3(getresuid, uid_t __user *, ruid, uid_t __user *, euid, uid_t __user *, suid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
David Howells86a264a2008-11-14 10:39:18 +1100835 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 int retval;
837
David Howells86a264a2008-11-14 10:39:18 +1100838 if (!(retval = put_user(cred->uid, ruid)) &&
839 !(retval = put_user(cred->euid, euid)))
David Howellsb6dff3e2008-11-14 10:39:16 +1100840 retval = put_user(cred->suid, suid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 return retval;
843}
844
845/*
846 * Same as above, but for rgid, egid, sgid.
847 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100848SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
David Howellsd84f4f92008-11-14 10:39:23 +1100850 const struct cred *old;
851 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int retval;
853
David Howellsd84f4f92008-11-14 10:39:23 +1100854 new = prepare_creds();
855 if (!new)
856 return -ENOMEM;
857 old = current_cred();
858
David Howellsd84f4f92008-11-14 10:39:23 +1100859 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700860 if (!nsown_capable(CAP_SETGID)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100861 if (rgid != (gid_t) -1 && rgid != old->gid &&
862 rgid != old->egid && rgid != old->sgid)
863 goto error;
864 if (egid != (gid_t) -1 && egid != old->gid &&
865 egid != old->egid && egid != old->sgid)
866 goto error;
867 if (sgid != (gid_t) -1 && sgid != old->gid &&
868 sgid != old->egid && sgid != old->sgid)
869 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
David Howellsd84f4f92008-11-14 10:39:23 +1100872 if (rgid != (gid_t) -1)
873 new->gid = rgid;
874 if (egid != (gid_t) -1)
875 new->egid = egid;
876 if (sgid != (gid_t) -1)
877 new->sgid = sgid;
878 new->fsgid = new->egid;
879
880 return commit_creds(new);
881
882error:
883 abort_creds(new);
884 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
Heiko Carstensdbf040d2009-01-14 14:14:04 +0100887SYSCALL_DEFINE3(getresgid, gid_t __user *, rgid, gid_t __user *, egid, gid_t __user *, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
David Howells86a264a2008-11-14 10:39:18 +1100889 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 int retval;
891
David Howells86a264a2008-11-14 10:39:18 +1100892 if (!(retval = put_user(cred->gid, rgid)) &&
893 !(retval = put_user(cred->egid, egid)))
David Howellsb6dff3e2008-11-14 10:39:16 +1100894 retval = put_user(cred->sgid, sgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 return retval;
897}
898
899
900/*
901 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
902 * is used for "access()" and for the NFS daemon (letting nfsd stay at
903 * whatever uid it wants to). It normally shadows "euid", except when
904 * explicitly set by setfsuid() or for access..
905 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100906SYSCALL_DEFINE1(setfsuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
David Howellsd84f4f92008-11-14 10:39:23 +1100908 const struct cred *old;
909 struct cred *new;
910 uid_t old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
David Howellsd84f4f92008-11-14 10:39:23 +1100912 new = prepare_creds();
913 if (!new)
914 return current_fsuid();
915 old = current_cred();
916 old_fsuid = old->fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
David Howellsd84f4f92008-11-14 10:39:23 +1100918 if (uid == old->uid || uid == old->euid ||
919 uid == old->suid || uid == old->fsuid ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700920 nsown_capable(CAP_SETUID)) {
Cal Peake756184b2006-09-30 23:27:24 -0700921 if (uid != old_fsuid) {
David Howellsd84f4f92008-11-14 10:39:23 +1100922 new->fsuid = uid;
923 if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
924 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927
David Howellsd84f4f92008-11-14 10:39:23 +1100928 abort_creds(new);
929 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
David Howellsd84f4f92008-11-14 10:39:23 +1100931change_okay:
932 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return old_fsuid;
934}
935
936/*
John Anthony Kazos Jrf42df9e2007-05-09 08:23:08 +0200937 * Samma på svenska..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100939SYSCALL_DEFINE1(setfsgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
David Howellsd84f4f92008-11-14 10:39:23 +1100941 const struct cred *old;
942 struct cred *new;
943 gid_t old_fsgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
David Howellsd84f4f92008-11-14 10:39:23 +1100945 new = prepare_creds();
946 if (!new)
947 return current_fsgid();
948 old = current_cred();
949 old_fsgid = old->fsgid;
950
David Howellsd84f4f92008-11-14 10:39:23 +1100951 if (gid == old->gid || gid == old->egid ||
952 gid == old->sgid || gid == old->fsgid ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700953 nsown_capable(CAP_SETGID)) {
Cal Peake756184b2006-09-30 23:27:24 -0700954 if (gid != old_fsgid) {
David Howellsd84f4f92008-11-14 10:39:23 +1100955 new->fsgid = gid;
956 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
David Howellsd84f4f92008-11-14 10:39:23 +1100959
David Howellsd84f4f92008-11-14 10:39:23 +1100960 abort_creds(new);
961 return old_fsgid;
962
963change_okay:
964 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return old_fsgid;
966}
967
Frank Mayharf06febc2008-09-12 09:54:39 -0700968void do_sys_times(struct tms *tms)
969{
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +0900970 cputime_t tgutime, tgstime, cutime, cstime;
Frank Mayharf06febc2008-09-12 09:54:39 -0700971
Oleg Nesterov2b5fe6d2008-11-17 15:40:08 +0100972 spin_lock_irq(&current->sighand->siglock);
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +0900973 thread_group_times(current, &tgutime, &tgstime);
Frank Mayharf06febc2008-09-12 09:54:39 -0700974 cutime = current->signal->cutime;
975 cstime = current->signal->cstime;
976 spin_unlock_irq(&current->sighand->siglock);
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +0900977 tms->tms_utime = cputime_to_clock_t(tgutime);
978 tms->tms_stime = cputime_to_clock_t(tgstime);
Frank Mayharf06febc2008-09-12 09:54:39 -0700979 tms->tms_cutime = cputime_to_clock_t(cutime);
980 tms->tms_cstime = cputime_to_clock_t(cstime);
981}
982
Heiko Carstens58fd3aa2009-01-14 14:14:03 +0100983SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (tbuf) {
986 struct tms tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Frank Mayharf06febc2008-09-12 09:54:39 -0700988 do_sys_times(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
990 return -EFAULT;
991 }
Paul Mackerrase3d5a272009-01-06 14:41:02 -0800992 force_successful_syscall_return();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return (long) jiffies_64_to_clock_t(get_jiffies_64());
994}
995
996/*
997 * This needs some heavy checking ...
998 * I just haven't the stomach for it. I also don't fully
999 * understand sessions/pgrp etc. Let somebody who does explain it.
1000 *
1001 * OK, I think I have the protection semantics right.... this is really
1002 * only important on a multi-user system anyway, to make sure one user
1003 * can't send a signal to a process owned by another. -TYT, 12/12/91
1004 *
1005 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1006 * LBT 04.03.94
1007 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +01001008SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 struct task_struct *p;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001011 struct task_struct *group_leader = current->group_leader;
Oleg Nesterov4e021302008-02-08 04:19:08 -08001012 struct pid *pgrp;
1013 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 if (!pid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001016 pid = task_pid_vnr(group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (!pgid)
1018 pgid = pid;
1019 if (pgid < 0)
1020 return -EINVAL;
Paul E. McKenney950eaac2010-08-31 17:00:18 -07001021 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 /* From this point forward we keep holding onto the tasklist lock
1024 * so that our parent does not change from under us. -DaveM
1025 */
1026 write_lock_irq(&tasklist_lock);
1027
1028 err = -ESRCH;
Oleg Nesterov4e021302008-02-08 04:19:08 -08001029 p = find_task_by_vpid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (!p)
1031 goto out;
1032
1033 err = -EINVAL;
1034 if (!thread_group_leader(p))
1035 goto out;
1036
Oleg Nesterov4e021302008-02-08 04:19:08 -08001037 if (same_thread_group(p->real_parent, group_leader)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 err = -EPERM;
Eric W. Biederman41487c62007-02-12 00:53:01 -08001039 if (task_session(p) != task_session(group_leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 goto out;
1041 err = -EACCES;
1042 if (p->did_exec)
1043 goto out;
1044 } else {
1045 err = -ESRCH;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001046 if (p != group_leader)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 goto out;
1048 }
1049
1050 err = -EPERM;
1051 if (p->signal->leader)
1052 goto out;
1053
Oleg Nesterov4e021302008-02-08 04:19:08 -08001054 pgrp = task_pid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (pgid != pid) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001056 struct task_struct *g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Oleg Nesterov4e021302008-02-08 04:19:08 -08001058 pgrp = find_vpid(pgid);
1059 g = pid_task(pgrp, PIDTYPE_PGID);
Eric W. Biederman41487c62007-02-12 00:53:01 -08001060 if (!g || task_session(g) != task_session(group_leader))
Oleg Nesterovf020bc42006-12-08 02:38:02 -08001061 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 err = security_task_setpgid(p, pgid);
1065 if (err)
1066 goto out;
1067
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07001068 if (task_pgrp(p) != pgrp)
Oleg Nesterov83beaf32008-04-30 00:54:27 -07001069 change_pid(p, PIDTYPE_PGID, pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 err = 0;
1072out:
1073 /* All paths lead to here, thus we are safe. -DaveM */
1074 write_unlock_irq(&tasklist_lock);
Paul E. McKenney950eaac2010-08-31 17:00:18 -07001075 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return err;
1077}
1078
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001079SYSCALL_DEFINE1(getpgid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001081 struct task_struct *p;
1082 struct pid *grp;
1083 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001085 rcu_read_lock();
1086 if (!pid)
1087 grp = task_pgrp(current);
1088 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 retval = -ESRCH;
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001090 p = find_task_by_vpid(pid);
1091 if (!p)
1092 goto out;
1093 grp = task_pgrp(p);
1094 if (!grp)
1095 goto out;
1096
1097 retval = security_task_getpgid(p);
1098 if (retval)
1099 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001101 retval = pid_vnr(grp);
1102out:
1103 rcu_read_unlock();
1104 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107#ifdef __ARCH_WANT_SYS_GETPGRP
1108
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001109SYSCALL_DEFINE0(getpgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001111 return sys_getpgid(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
1114#endif
1115
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001116SYSCALL_DEFINE1(getsid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001118 struct task_struct *p;
1119 struct pid *sid;
1120 int retval;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001121
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001122 rcu_read_lock();
1123 if (!pid)
1124 sid = task_session(current);
1125 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 retval = -ESRCH;
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001127 p = find_task_by_vpid(pid);
1128 if (!p)
1129 goto out;
1130 sid = task_session(p);
1131 if (!sid)
1132 goto out;
1133
1134 retval = security_task_getsid(p);
1135 if (retval)
1136 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001138 retval = pid_vnr(sid);
1139out:
1140 rcu_read_unlock();
1141 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
Heiko Carstensb290ebe2009-01-14 14:14:06 +01001144SYSCALL_DEFINE0(setsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Oren Laadane19f2472006-01-08 01:03:58 -08001146 struct task_struct *group_leader = current->group_leader;
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001147 struct pid *sid = task_pid(group_leader);
1148 pid_t session = pid_vnr(sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 int err = -EPERM;
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 write_lock_irq(&tasklist_lock);
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001152 /* Fail if I am already a session leader */
1153 if (group_leader->signal->leader)
1154 goto out;
1155
Oleg Nesterov430c6232008-02-08 04:19:11 -08001156 /* Fail if a process group id already exists that equals the
1157 * proposed session id.
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001158 */
Oleg Nesterov6806aac2008-02-08 04:19:12 -08001159 if (pid_task(sid, PIDTYPE_PGID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 goto out;
1161
Oren Laadane19f2472006-01-08 01:03:58 -08001162 group_leader->signal->leader = 1;
Oleg Nesterov8520d7c2008-02-08 04:19:09 -08001163 __set_special_pids(sid);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001164
Alan Cox9c9f4de2008-10-13 10:37:26 +01001165 proc_clear_tty(group_leader);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001166
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001167 err = session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168out:
1169 write_unlock_irq(&tasklist_lock);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001170 if (err > 0) {
Christian Borntraeger0d0df592009-10-26 16:49:34 -07001171 proc_sid_connector(group_leader);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001172 sched_autogroup_create_attach(group_leader);
1173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return err;
1175}
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177DECLARE_RWSEM(uts_sem);
1178
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001179#ifdef COMPAT_UTS_MACHINE
1180#define override_architecture(name) \
Andreas Schwab46da2762010-04-23 13:17:44 -04001181 (personality(current->personality) == PER_LINUX32 && \
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001182 copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1183 sizeof(COMPAT_UTS_MACHINE)))
1184#else
1185#define override_architecture(name) 0
1186#endif
1187
Andi Kleenbe274252011-08-19 16:15:10 -07001188/*
1189 * Work around broken programs that cannot handle "Linux 3.0".
1190 * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40
1191 */
1192static int override_release(char __user *release, int len)
1193{
1194 int ret = 0;
Linus Torvaldsa84a79e2011-10-17 08:24:24 -07001195 char buf[65];
Andi Kleenbe274252011-08-19 16:15:10 -07001196
1197 if (current->personality & UNAME26) {
1198 char *rest = UTS_RELEASE;
1199 int ndots = 0;
1200 unsigned v;
1201
1202 while (*rest) {
1203 if (*rest == '.' && ++ndots >= 3)
1204 break;
1205 if (!isdigit(*rest) && *rest != '.')
1206 break;
1207 rest++;
1208 }
1209 v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 40;
1210 snprintf(buf, len, "2.6.%u%s", v, rest);
1211 ret = copy_to_user(release, buf, len);
1212 }
1213 return ret;
1214}
1215
Heiko Carstense48fbb62009-01-14 14:14:26 +01001216SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 int errno = 0;
1219
1220 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001221 if (copy_to_user(name, utsname(), sizeof *name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 errno = -EFAULT;
1223 up_read(&uts_sem);
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001224
Andi Kleenbe274252011-08-19 16:15:10 -07001225 if (!errno && override_release(name->release, sizeof(name->release)))
1226 errno = -EFAULT;
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001227 if (!errno && override_architecture(name))
1228 errno = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return errno;
1230}
1231
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001232#ifdef __ARCH_WANT_SYS_OLD_UNAME
1233/*
1234 * Old cruft
1235 */
1236SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
1237{
1238 int error = 0;
1239
1240 if (!name)
1241 return -EFAULT;
1242
1243 down_read(&uts_sem);
1244 if (copy_to_user(name, utsname(), sizeof(*name)))
1245 error = -EFAULT;
1246 up_read(&uts_sem);
1247
Andi Kleenbe274252011-08-19 16:15:10 -07001248 if (!error && override_release(name->release, sizeof(name->release)))
1249 error = -EFAULT;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001250 if (!error && override_architecture(name))
1251 error = -EFAULT;
1252 return error;
1253}
1254
1255SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
1256{
1257 int error;
1258
1259 if (!name)
1260 return -EFAULT;
1261 if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
1262 return -EFAULT;
1263
1264 down_read(&uts_sem);
1265 error = __copy_to_user(&name->sysname, &utsname()->sysname,
1266 __OLD_UTS_LEN);
1267 error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
1268 error |= __copy_to_user(&name->nodename, &utsname()->nodename,
1269 __OLD_UTS_LEN);
1270 error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
1271 error |= __copy_to_user(&name->release, &utsname()->release,
1272 __OLD_UTS_LEN);
1273 error |= __put_user(0, name->release + __OLD_UTS_LEN);
1274 error |= __copy_to_user(&name->version, &utsname()->version,
1275 __OLD_UTS_LEN);
1276 error |= __put_user(0, name->version + __OLD_UTS_LEN);
1277 error |= __copy_to_user(&name->machine, &utsname()->machine,
1278 __OLD_UTS_LEN);
1279 error |= __put_user(0, name->machine + __OLD_UTS_LEN);
1280 up_read(&uts_sem);
1281
1282 if (!error && override_architecture(name))
1283 error = -EFAULT;
Andi Kleenbe274252011-08-19 16:15:10 -07001284 if (!error && override_release(name->release, sizeof(name->release)))
1285 error = -EFAULT;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001286 return error ? -EFAULT : 0;
1287}
1288#endif
1289
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001290SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 int errno;
1293 char tmp[__NEW_UTS_LEN];
1294
Serge E. Hallynbb96a6f2011-03-23 16:43:18 -07001295 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (len < 0 || len > __NEW_UTS_LEN)
1299 return -EINVAL;
1300 down_write(&uts_sem);
1301 errno = -EFAULT;
1302 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001303 struct new_utsname *u = utsname();
1304
1305 memcpy(u->nodename, tmp, len);
1306 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 errno = 0;
1308 }
Lucas De Marchif1ecf062011-11-02 13:39:22 -07001309 uts_proc_notify(UTS_PROC_HOSTNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 up_write(&uts_sem);
1311 return errno;
1312}
1313
1314#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1315
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001316SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 int i, errno;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001319 struct new_utsname *u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 if (len < 0)
1322 return -EINVAL;
1323 down_read(&uts_sem);
Andrew Morton9679e4d2008-10-15 22:01:51 -07001324 u = utsname();
1325 i = 1 + strlen(u->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (i > len)
1327 i = len;
1328 errno = 0;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001329 if (copy_to_user(name, u->nodename, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 errno = -EFAULT;
1331 up_read(&uts_sem);
1332 return errno;
1333}
1334
1335#endif
1336
1337/*
1338 * Only setdomainname; getdomainname can be implemented by calling
1339 * uname()
1340 */
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001341SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 int errno;
1344 char tmp[__NEW_UTS_LEN];
1345
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001346 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return -EPERM;
1348 if (len < 0 || len > __NEW_UTS_LEN)
1349 return -EINVAL;
1350
1351 down_write(&uts_sem);
1352 errno = -EFAULT;
1353 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001354 struct new_utsname *u = utsname();
1355
1356 memcpy(u->domainname, tmp, len);
1357 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 errno = 0;
1359 }
Lucas De Marchif1ecf062011-11-02 13:39:22 -07001360 uts_proc_notify(UTS_PROC_DOMAINNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 up_write(&uts_sem);
1362 return errno;
1363}
1364
Heiko Carstense48fbb62009-01-14 14:14:26 +01001365SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
Jiri Slabyb9518342010-05-04 11:28:25 +02001367 struct rlimit value;
1368 int ret;
1369
1370 ret = do_prlimit(current, resource, NULL, &value);
1371 if (!ret)
1372 ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1373
1374 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
1377#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1378
1379/*
1380 * Back compatibility for getrlimit. Needed for some apps.
1381 */
1382
Heiko Carstense48fbb62009-01-14 14:14:26 +01001383SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1384 struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 struct rlimit x;
1387 if (resource >= RLIM_NLIMITS)
1388 return -EINVAL;
1389
1390 task_lock(current->group_leader);
1391 x = current->signal->rlim[resource];
1392 task_unlock(current->group_leader);
Cal Peake756184b2006-09-30 23:27:24 -07001393 if (x.rlim_cur > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 x.rlim_cur = 0x7FFFFFFF;
Cal Peake756184b2006-09-30 23:27:24 -07001395 if (x.rlim_max > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 x.rlim_max = 0x7FFFFFFF;
1397 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1398}
1399
1400#endif
1401
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001402static inline bool rlim64_is_infinity(__u64 rlim64)
1403{
1404#if BITS_PER_LONG < 64
1405 return rlim64 >= ULONG_MAX;
1406#else
1407 return rlim64 == RLIM64_INFINITY;
1408#endif
1409}
1410
1411static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1412{
1413 if (rlim->rlim_cur == RLIM_INFINITY)
1414 rlim64->rlim_cur = RLIM64_INFINITY;
1415 else
1416 rlim64->rlim_cur = rlim->rlim_cur;
1417 if (rlim->rlim_max == RLIM_INFINITY)
1418 rlim64->rlim_max = RLIM64_INFINITY;
1419 else
1420 rlim64->rlim_max = rlim->rlim_max;
1421}
1422
1423static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1424{
1425 if (rlim64_is_infinity(rlim64->rlim_cur))
1426 rlim->rlim_cur = RLIM_INFINITY;
1427 else
1428 rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1429 if (rlim64_is_infinity(rlim64->rlim_max))
1430 rlim->rlim_max = RLIM_INFINITY;
1431 else
1432 rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1433}
1434
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001435/* make sure you are allowed to change @tsk limits before calling this */
Jiri Slaby5b415352010-03-24 16:11:29 +01001436int do_prlimit(struct task_struct *tsk, unsigned int resource,
1437 struct rlimit *new_rlim, struct rlimit *old_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Jiri Slaby5b415352010-03-24 16:11:29 +01001439 struct rlimit *rlim;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001440 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 if (resource >= RLIM_NLIMITS)
1443 return -EINVAL;
Jiri Slaby5b415352010-03-24 16:11:29 +01001444 if (new_rlim) {
1445 if (new_rlim->rlim_cur > new_rlim->rlim_max)
1446 return -EINVAL;
1447 if (resource == RLIMIT_NOFILE &&
1448 new_rlim->rlim_max > sysctl_nr_open)
1449 return -EPERM;
1450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001452 /* protect tsk->signal and tsk->sighand from disappearing */
1453 read_lock(&tasklist_lock);
1454 if (!tsk->sighand) {
1455 retval = -ESRCH;
1456 goto out;
1457 }
1458
Jiri Slaby5b415352010-03-24 16:11:29 +01001459 rlim = tsk->signal->rlim + resource;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001460 task_lock(tsk->group_leader);
Jiri Slaby5b415352010-03-24 16:11:29 +01001461 if (new_rlim) {
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001462 /* Keep the capable check against init_user_ns until
1463 cgroups can contain all limits */
Jiri Slaby5b415352010-03-24 16:11:29 +01001464 if (new_rlim->rlim_max > rlim->rlim_max &&
1465 !capable(CAP_SYS_RESOURCE))
1466 retval = -EPERM;
1467 if (!retval)
1468 retval = security_task_setrlimit(tsk->group_leader,
1469 resource, new_rlim);
1470 if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
1471 /*
1472 * The caller is asking for an immediate RLIMIT_CPU
1473 * expiry. But we use the zero value to mean "it was
1474 * never set". So let's cheat and make it one second
1475 * instead
1476 */
1477 new_rlim->rlim_cur = 1;
1478 }
Tom Alsberg9926e4c2007-05-08 00:30:31 -07001479 }
Jiri Slaby5b415352010-03-24 16:11:29 +01001480 if (!retval) {
1481 if (old_rlim)
1482 *old_rlim = *rlim;
1483 if (new_rlim)
1484 *rlim = *new_rlim;
1485 }
Jiri Slaby7855c352009-08-26 23:45:34 +02001486 task_unlock(tsk->group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Andrew Mortond3561f72006-03-24 03:18:36 -08001488 /*
1489 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1490 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1491 * very long-standing error, and fixing it now risks breakage of
1492 * applications, so we live with it
1493 */
Jiri Slaby5b415352010-03-24 16:11:29 +01001494 if (!retval && new_rlim && resource == RLIMIT_CPU &&
1495 new_rlim->rlim_cur != RLIM_INFINITY)
1496 update_rlimit_cpu(tsk, new_rlim->rlim_cur);
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001497out:
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001498 read_unlock(&tasklist_lock);
Oleg Nesterov2fb9d262009-09-03 19:21:45 +02001499 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001502/* rcu lock must be held */
1503static int check_prlimit_permission(struct task_struct *task)
1504{
1505 const struct cred *cred = current_cred(), *tcred;
1506
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001507 if (current == task)
1508 return 0;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001509
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001510 tcred = __task_cred(task);
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -08001511 if (cred->user_ns == tcred->user_ns &&
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001512 (cred->uid == tcred->euid &&
1513 cred->uid == tcred->suid &&
1514 cred->uid == tcred->uid &&
1515 cred->gid == tcred->egid &&
1516 cred->gid == tcred->sgid &&
1517 cred->gid == tcred->gid))
1518 return 0;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -08001519 if (ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001520 return 0;
1521
1522 return -EPERM;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001523}
1524
1525SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1526 const struct rlimit64 __user *, new_rlim,
1527 struct rlimit64 __user *, old_rlim)
1528{
1529 struct rlimit64 old64, new64;
1530 struct rlimit old, new;
1531 struct task_struct *tsk;
1532 int ret;
1533
1534 if (new_rlim) {
1535 if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1536 return -EFAULT;
1537 rlim64_to_rlim(&new64, &new);
1538 }
1539
1540 rcu_read_lock();
1541 tsk = pid ? find_task_by_vpid(pid) : current;
1542 if (!tsk) {
1543 rcu_read_unlock();
1544 return -ESRCH;
1545 }
1546 ret = check_prlimit_permission(tsk);
1547 if (ret) {
1548 rcu_read_unlock();
1549 return ret;
1550 }
1551 get_task_struct(tsk);
1552 rcu_read_unlock();
1553
1554 ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1555 old_rlim ? &old : NULL);
1556
1557 if (!ret && old_rlim) {
1558 rlim_to_rlim64(&old, &old64);
1559 if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1560 ret = -EFAULT;
1561 }
1562
1563 put_task_struct(tsk);
1564 return ret;
1565}
1566
Jiri Slaby7855c352009-08-26 23:45:34 +02001567SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1568{
1569 struct rlimit new_rlim;
1570
1571 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1572 return -EFAULT;
Jiri Slaby5b415352010-03-24 16:11:29 +01001573 return do_prlimit(current, resource, &new_rlim, NULL);
Jiri Slaby7855c352009-08-26 23:45:34 +02001574}
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576/*
1577 * It would make sense to put struct rusage in the task_struct,
1578 * except that would make the task_struct be *really big*. After
1579 * task_struct gets moved into malloc'ed memory, it would
1580 * make sense to do this. It will make moving the rest of the information
1581 * a lot simpler! (Which we're not doing right now because we're not
1582 * measuring them yet).
1583 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1585 * races with threads incrementing their own counters. But since word
1586 * reads are atomic, we either get new values or old values and we don't
1587 * care which for the sums. We always take the siglock to protect reading
1588 * the c* fields from p->signal from races with exit.c updating those
1589 * fields when reaping, so a sample either gets all the additions of a
1590 * given child after it's reaped, or none so this sample is before reaping.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001591 *
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001592 * Locking:
1593 * We need to take the siglock for CHILDEREN, SELF and BOTH
1594 * for the cases current multithreaded, non-current single threaded
1595 * non-current multithreaded. Thread traversal is now safe with
1596 * the siglock held.
1597 * Strictly speaking, we donot need to take the siglock if we are current and
1598 * single threaded, as no one else can take our signal_struct away, no one
1599 * else can reap the children to update signal->c* counters, and no one else
1600 * can race with the signal-> fields. If we do not take any lock, the
1601 * signal-> fields could be read out of order while another thread was just
1602 * exiting. So we should place a read memory barrier when we avoid the lock.
1603 * On the writer side, write memory barrier is implied in __exit_signal
1604 * as __exit_signal releases the siglock spinlock after updating the signal->
1605 * fields. But we don't do this yet to keep things simple.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001606 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 */
1608
Frank Mayharf06febc2008-09-12 09:54:39 -07001609static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001610{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001611 r->ru_nvcsw += t->nvcsw;
1612 r->ru_nivcsw += t->nivcsw;
1613 r->ru_minflt += t->min_flt;
1614 r->ru_majflt += t->maj_flt;
1615 r->ru_inblock += task_io_get_inblock(t);
1616 r->ru_oublock += task_io_get_oublock(t);
1617}
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1620{
1621 struct task_struct *t;
1622 unsigned long flags;
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09001623 cputime_t tgutime, tgstime, utime, stime;
Jiri Pirko1f102062009-09-22 16:44:10 -07001624 unsigned long maxrss = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 memset((char *) r, 0, sizeof *r);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001627 utime = stime = 0;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001628
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001629 if (who == RUSAGE_THREAD) {
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09001630 task_times(current, &utime, &stime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001631 accumulate_thread_rusage(p, r);
Jiri Pirko1f102062009-09-22 16:44:10 -07001632 maxrss = p->signal->maxrss;
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001633 goto out;
1634 }
1635
Oleg Nesterovd6cf7232008-04-30 00:52:38 -07001636 if (!lock_task_sighand(p, &flags))
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001637 return;
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 switch (who) {
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001640 case RUSAGE_BOTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 case RUSAGE_CHILDREN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 utime = p->signal->cutime;
1643 stime = p->signal->cstime;
1644 r->ru_nvcsw = p->signal->cnvcsw;
1645 r->ru_nivcsw = p->signal->cnivcsw;
1646 r->ru_minflt = p->signal->cmin_flt;
1647 r->ru_majflt = p->signal->cmaj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07001648 r->ru_inblock = p->signal->cinblock;
1649 r->ru_oublock = p->signal->coublock;
Jiri Pirko1f102062009-09-22 16:44:10 -07001650 maxrss = p->signal->cmaxrss;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001651
1652 if (who == RUSAGE_CHILDREN)
1653 break;
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 case RUSAGE_SELF:
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09001656 thread_group_times(p, &tgutime, &tgstime);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001657 utime += tgutime;
1658 stime += tgstime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 r->ru_nvcsw += p->signal->nvcsw;
1660 r->ru_nivcsw += p->signal->nivcsw;
1661 r->ru_minflt += p->signal->min_flt;
1662 r->ru_majflt += p->signal->maj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07001663 r->ru_inblock += p->signal->inblock;
1664 r->ru_oublock += p->signal->oublock;
Jiri Pirko1f102062009-09-22 16:44:10 -07001665 if (maxrss < p->signal->maxrss)
1666 maxrss = p->signal->maxrss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 t = p;
1668 do {
Frank Mayharf06febc2008-09-12 09:54:39 -07001669 accumulate_thread_rusage(t, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 t = next_thread(t);
1671 } while (t != p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 break;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 default:
1675 BUG();
1676 }
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001677 unlock_task_sighand(p, &flags);
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001678
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001679out:
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001680 cputime_to_timeval(utime, &r->ru_utime);
1681 cputime_to_timeval(stime, &r->ru_stime);
Jiri Pirko1f102062009-09-22 16:44:10 -07001682
1683 if (who != RUSAGE_CHILDREN) {
1684 struct mm_struct *mm = get_task_mm(p);
1685 if (mm) {
1686 setmax_mm_hiwater_rss(&maxrss, mm);
1687 mmput(mm);
1688 }
1689 }
1690 r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
1692
1693int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1694{
1695 struct rusage r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 k_getrusage(p, who, &r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1698}
1699
Heiko Carstense48fbb62009-01-14 14:14:26 +01001700SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001702 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1703 who != RUSAGE_THREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return -EINVAL;
1705 return getrusage(current, who, ru);
1706}
1707
Heiko Carstense48fbb62009-01-14 14:14:26 +01001708SYSCALL_DEFINE1(umask, int, mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
1710 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1711 return mask;
1712}
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -08001713
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001714#ifdef CONFIG_CHECKPOINT_RESTORE
1715static int prctl_set_mm(int opt, unsigned long addr,
1716 unsigned long arg4, unsigned long arg5)
1717{
1718 unsigned long rlim = rlimit(RLIMIT_DATA);
1719 unsigned long vm_req_flags;
1720 unsigned long vm_bad_flags;
1721 struct vm_area_struct *vma;
1722 int error = 0;
1723 struct mm_struct *mm = current->mm;
1724
1725 if (arg4 | arg5)
1726 return -EINVAL;
1727
Cyrill Gorcunov79f07132012-03-15 15:17:10 -07001728 if (!capable(CAP_SYS_RESOURCE))
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001729 return -EPERM;
1730
1731 if (addr >= TASK_SIZE)
1732 return -EINVAL;
1733
1734 down_read(&mm->mmap_sem);
1735 vma = find_vma(mm, addr);
1736
1737 if (opt != PR_SET_MM_START_BRK && opt != PR_SET_MM_BRK) {
1738 /* It must be existing VMA */
1739 if (!vma || vma->vm_start > addr)
1740 goto out;
1741 }
1742
1743 error = -EINVAL;
1744 switch (opt) {
1745 case PR_SET_MM_START_CODE:
1746 case PR_SET_MM_END_CODE:
1747 vm_req_flags = VM_READ | VM_EXEC;
1748 vm_bad_flags = VM_WRITE | VM_MAYSHARE;
1749
1750 if ((vma->vm_flags & vm_req_flags) != vm_req_flags ||
1751 (vma->vm_flags & vm_bad_flags))
1752 goto out;
1753
1754 if (opt == PR_SET_MM_START_CODE)
1755 mm->start_code = addr;
1756 else
1757 mm->end_code = addr;
1758 break;
1759
1760 case PR_SET_MM_START_DATA:
1761 case PR_SET_MM_END_DATA:
1762 vm_req_flags = VM_READ | VM_WRITE;
1763 vm_bad_flags = VM_EXEC | VM_MAYSHARE;
1764
1765 if ((vma->vm_flags & vm_req_flags) != vm_req_flags ||
1766 (vma->vm_flags & vm_bad_flags))
1767 goto out;
1768
1769 if (opt == PR_SET_MM_START_DATA)
1770 mm->start_data = addr;
1771 else
1772 mm->end_data = addr;
1773 break;
1774
1775 case PR_SET_MM_START_STACK:
1776
1777#ifdef CONFIG_STACK_GROWSUP
1778 vm_req_flags = VM_READ | VM_WRITE | VM_GROWSUP;
1779#else
1780 vm_req_flags = VM_READ | VM_WRITE | VM_GROWSDOWN;
1781#endif
1782 if ((vma->vm_flags & vm_req_flags) != vm_req_flags)
1783 goto out;
1784
1785 mm->start_stack = addr;
1786 break;
1787
1788 case PR_SET_MM_START_BRK:
1789 if (addr <= mm->end_data)
1790 goto out;
1791
1792 if (rlim < RLIM_INFINITY &&
1793 (mm->brk - addr) +
1794 (mm->end_data - mm->start_data) > rlim)
1795 goto out;
1796
1797 mm->start_brk = addr;
1798 break;
1799
1800 case PR_SET_MM_BRK:
1801 if (addr <= mm->end_data)
1802 goto out;
1803
1804 if (rlim < RLIM_INFINITY &&
1805 (addr - mm->start_brk) +
1806 (mm->end_data - mm->start_data) > rlim)
1807 goto out;
1808
1809 mm->brk = addr;
1810 break;
1811
1812 default:
1813 error = -EINVAL;
1814 goto out;
1815 }
1816
1817 error = 0;
1818
1819out:
1820 up_read(&mm->mmap_sem);
1821
1822 return error;
1823}
1824#else /* CONFIG_CHECKPOINT_RESTORE */
1825static int prctl_set_mm(int opt, unsigned long addr,
1826 unsigned long arg4, unsigned long arg5)
1827{
1828 return -EINVAL;
1829}
1830#endif
1831
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001832SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
1833 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
David Howellsb6dff3e2008-11-14 10:39:16 +11001835 struct task_struct *me = current;
1836 unsigned char comm[sizeof(me->comm)];
1837 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
David Howellsd84f4f92008-11-14 10:39:23 +11001839 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1840 if (error != -ENOSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 return error;
1842
David Howellsd84f4f92008-11-14 10:39:23 +11001843 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 switch (option) {
1845 case PR_SET_PDEATHSIG:
Jesper Juhl0730ded2005-09-06 15:17:37 -07001846 if (!valid_signal(arg2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 error = -EINVAL;
1848 break;
1849 }
David Howellsb6dff3e2008-11-14 10:39:16 +11001850 me->pdeath_signal = arg2;
1851 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 break;
1853 case PR_GET_PDEATHSIG:
David Howellsb6dff3e2008-11-14 10:39:16 +11001854 error = put_user(me->pdeath_signal, (int __user *)arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 break;
1856 case PR_GET_DUMPABLE:
David Howellsb6dff3e2008-11-14 10:39:16 +11001857 error = get_dumpable(me->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 break;
1859 case PR_SET_DUMPABLE:
Marcel Holtmannabf75a52006-07-12 13:12:00 +02001860 if (arg2 < 0 || arg2 > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 error = -EINVAL;
1862 break;
1863 }
David Howellsb6dff3e2008-11-14 10:39:16 +11001864 set_dumpable(me->mm, arg2);
1865 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 break;
1867
1868 case PR_SET_UNALIGN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001869 error = SET_UNALIGN_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 break;
1871 case PR_GET_UNALIGN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001872 error = GET_UNALIGN_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 break;
1874 case PR_SET_FPEMU:
David Howellsb6dff3e2008-11-14 10:39:16 +11001875 error = SET_FPEMU_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 break;
1877 case PR_GET_FPEMU:
David Howellsb6dff3e2008-11-14 10:39:16 +11001878 error = GET_FPEMU_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 break;
1880 case PR_SET_FPEXC:
David Howellsb6dff3e2008-11-14 10:39:16 +11001881 error = SET_FPEXC_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 break;
1883 case PR_GET_FPEXC:
David Howellsb6dff3e2008-11-14 10:39:16 +11001884 error = GET_FPEXC_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 break;
1886 case PR_GET_TIMING:
1887 error = PR_TIMING_STATISTICAL;
1888 break;
1889 case PR_SET_TIMING:
Shi Weihua7b266552008-05-23 13:04:59 -07001890 if (arg2 != PR_TIMING_STATISTICAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 error = -EINVAL;
David Howellsb6dff3e2008-11-14 10:39:16 +11001892 else
1893 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 break;
1895
David Howellsb6dff3e2008-11-14 10:39:16 +11001896 case PR_SET_NAME:
1897 comm[sizeof(me->comm)-1] = 0;
1898 if (strncpy_from_user(comm, (char __user *)arg2,
1899 sizeof(me->comm) - 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return -EFAULT;
David Howellsb6dff3e2008-11-14 10:39:16 +11001901 set_task_comm(me, comm);
Vladimir Zapolskiyf786ecb2011-09-21 09:26:44 +00001902 proc_comm_connector(me);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return 0;
David Howellsb6dff3e2008-11-14 10:39:16 +11001904 case PR_GET_NAME:
1905 get_task_comm(comm, me);
1906 if (copy_to_user((char __user *)arg2, comm,
1907 sizeof(comm)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 return -EFAULT;
1909 return 0;
Anton Blanchard651d7652006-06-07 16:10:19 +10001910 case PR_GET_ENDIAN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001911 error = GET_ENDIAN(me, arg2);
Anton Blanchard651d7652006-06-07 16:10:19 +10001912 break;
1913 case PR_SET_ENDIAN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001914 error = SET_ENDIAN(me, arg2);
Anton Blanchard651d7652006-06-07 16:10:19 +10001915 break;
1916
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -07001917 case PR_GET_SECCOMP:
1918 error = prctl_get_seccomp();
1919 break;
1920 case PR_SET_SECCOMP:
1921 error = prctl_set_seccomp(arg2);
1922 break;
Erik Bosman8fb402b2008-04-11 18:54:17 +02001923 case PR_GET_TSC:
1924 error = GET_TSC_CTL(arg2);
1925 break;
1926 case PR_SET_TSC:
1927 error = SET_TSC_CTL(arg2);
1928 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001929 case PR_TASK_PERF_EVENTS_DISABLE:
1930 error = perf_event_task_disable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001931 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001932 case PR_TASK_PERF_EVENTS_ENABLE:
1933 error = perf_event_task_enable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001934 break;
Arjan van de Ven69766752008-09-01 15:52:40 -07001935 case PR_GET_TIMERSLACK:
1936 error = current->timer_slack_ns;
1937 break;
1938 case PR_SET_TIMERSLACK:
1939 if (arg2 <= 0)
1940 current->timer_slack_ns =
1941 current->default_timer_slack_ns;
1942 else
1943 current->timer_slack_ns = arg2;
David Howellsb6dff3e2008-11-14 10:39:16 +11001944 error = 0;
Arjan van de Ven69766752008-09-01 15:52:40 -07001945 break;
Andi Kleen4db96cf2009-09-16 11:50:14 +02001946 case PR_MCE_KILL:
1947 if (arg4 | arg5)
1948 return -EINVAL;
1949 switch (arg2) {
Andi Kleen1087e9b2009-10-04 02:20:11 +02001950 case PR_MCE_KILL_CLEAR:
Andi Kleen4db96cf2009-09-16 11:50:14 +02001951 if (arg3 != 0)
1952 return -EINVAL;
1953 current->flags &= ~PF_MCE_PROCESS;
1954 break;
Andi Kleen1087e9b2009-10-04 02:20:11 +02001955 case PR_MCE_KILL_SET:
Andi Kleen4db96cf2009-09-16 11:50:14 +02001956 current->flags |= PF_MCE_PROCESS;
Andi Kleen1087e9b2009-10-04 02:20:11 +02001957 if (arg3 == PR_MCE_KILL_EARLY)
Andi Kleen4db96cf2009-09-16 11:50:14 +02001958 current->flags |= PF_MCE_EARLY;
Andi Kleen1087e9b2009-10-04 02:20:11 +02001959 else if (arg3 == PR_MCE_KILL_LATE)
Andi Kleen4db96cf2009-09-16 11:50:14 +02001960 current->flags &= ~PF_MCE_EARLY;
Andi Kleen1087e9b2009-10-04 02:20:11 +02001961 else if (arg3 == PR_MCE_KILL_DEFAULT)
1962 current->flags &=
1963 ~(PF_MCE_EARLY|PF_MCE_PROCESS);
1964 else
1965 return -EINVAL;
Andi Kleen4db96cf2009-09-16 11:50:14 +02001966 break;
1967 default:
1968 return -EINVAL;
1969 }
1970 error = 0;
1971 break;
Andi Kleen1087e9b2009-10-04 02:20:11 +02001972 case PR_MCE_KILL_GET:
1973 if (arg2 | arg3 | arg4 | arg5)
1974 return -EINVAL;
1975 if (current->flags & PF_MCE_PROCESS)
1976 error = (current->flags & PF_MCE_EARLY) ?
1977 PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
1978 else
1979 error = PR_MCE_KILL_DEFAULT;
1980 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001981 case PR_SET_MM:
1982 error = prctl_set_mm(arg2, arg3, arg4, arg5);
1983 break;
Lennart Poetteringebec18a2012-03-23 15:01:54 -07001984 case PR_SET_CHILD_SUBREAPER:
1985 me->signal->is_child_subreaper = !!arg2;
1986 error = 0;
1987 break;
1988 case PR_GET_CHILD_SUBREAPER:
1989 error = put_user(me->signal->is_child_subreaper,
1990 (int __user *) arg2);
1991 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 default:
1993 error = -EINVAL;
1994 break;
1995 }
1996 return error;
1997}
Andi Kleen3cfc3482006-09-26 10:52:28 +02001998
Heiko Carstens836f92a2009-01-14 14:14:33 +01001999SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
2000 struct getcpu_cache __user *, unused)
Andi Kleen3cfc3482006-09-26 10:52:28 +02002001{
2002 int err = 0;
2003 int cpu = raw_smp_processor_id();
2004 if (cpup)
2005 err |= put_user(cpu, cpup);
2006 if (nodep)
2007 err |= put_user(cpu_to_node(cpu), nodep);
Andi Kleen3cfc3482006-09-26 10:52:28 +02002008 return err ? -EFAULT : 0;
2009}
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002010
2011char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
2012
Neil Hormana06a4dc2010-05-26 14:42:58 -07002013static void argv_cleanup(struct subprocess_info *info)
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002014{
Neil Hormana06a4dc2010-05-26 14:42:58 -07002015 argv_free(info->argv);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002016}
2017
2018/**
2019 * orderly_poweroff - Trigger an orderly system poweroff
2020 * @force: force poweroff if command execution fails
2021 *
2022 * This may be called from any context to trigger a system shutdown.
2023 * If the orderly shutdown fails, it will force an immediate shutdown.
2024 */
2025int orderly_poweroff(bool force)
2026{
2027 int argc;
2028 char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
2029 static char *envp[] = {
2030 "HOME=/",
2031 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
2032 NULL
2033 };
2034 int ret = -ENOMEM;
2035 struct subprocess_info *info;
2036
2037 if (argv == NULL) {
2038 printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
2039 __func__, poweroff_cmd);
2040 goto out;
2041 }
2042
KOSAKI Motohiroac331d12008-07-25 01:45:38 -07002043 info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002044 if (info == NULL) {
2045 argv_free(argv);
2046 goto out;
2047 }
2048
Neil Hormana06a4dc2010-05-26 14:42:58 -07002049 call_usermodehelper_setfns(info, NULL, argv_cleanup, NULL);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002050
Jeremy Fitzhardinge86313c42007-07-17 18:37:03 -07002051 ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002052
2053 out:
2054 if (ret && force) {
2055 printk(KERN_WARNING "Failed to start orderly shutdown: "
2056 "forcing the issue\n");
2057
2058 /* I guess this should try to kick off some daemon to
2059 sync and poweroff asap. Or not even bother syncing
2060 if we're doing an emergency shutdown? */
2061 emergency_sync();
2062 kernel_power_off();
2063 }
2064
2065 return ret;
2066}
2067EXPORT_SYMBOL_GPL(orderly_poweroff);