blob: dd948a1fca4c7e0b70ed7d5dc3902350c65d0c25 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
8#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>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020015#include <linux/perf_event.h>
Daniel Walker3e88c552007-05-10 22:22:53 -070016#include <linux/resource.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070017#include <linux/kernel.h>
18#include <linux/kexec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/workqueue.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080020#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/device.h>
22#include <linux/key.h>
23#include <linux/times.h>
24#include <linux/posix-timers.h>
25#include <linux/security.h>
26#include <linux/dcookies.h>
27#include <linux/suspend.h>
28#include <linux/tty.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070029#include <linux/signal.h>
Matt Helsley9f460802005-11-07 00:59:16 -080030#include <linux/cn_proc.h>
Andi Kleen3cfc3482006-09-26 10:52:28 +020031#include <linux/getcpu.h>
Eric Dumazet6eaeeab2007-05-10 22:22:37 -070032#include <linux/task_io_accounting_ops.h>
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -070033#include <linux/seccomp.h>
Mark Lord40477272007-10-01 01:20:10 -070034#include <linux/cpu.h>
Christoph Hellwige28cbf22010-03-10 15:21:19 -080035#include <linux/personality.h>
Paul Mackerrase3d5a272009-01-06 14:41:02 -080036#include <linux/ptrace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040037#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/gfp.h>
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010039#include <linux/syscore_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <linux/compat.h>
42#include <linux/syscalls.h>
Keshavamurthy Anil S00d7c052005-12-12 00:37:33 -080043#include <linux/kprobes.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070044#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Seiji Aguchi04c68622011-01-12 16:59:30 -080046#include <linux/kmsg_dump.h>
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/uaccess.h>
49#include <asm/io.h>
50#include <asm/unistd.h>
51
52#ifndef SET_UNALIGN_CTL
53# define SET_UNALIGN_CTL(a,b) (-EINVAL)
54#endif
55#ifndef GET_UNALIGN_CTL
56# define GET_UNALIGN_CTL(a,b) (-EINVAL)
57#endif
58#ifndef SET_FPEMU_CTL
59# define SET_FPEMU_CTL(a,b) (-EINVAL)
60#endif
61#ifndef GET_FPEMU_CTL
62# define GET_FPEMU_CTL(a,b) (-EINVAL)
63#endif
64#ifndef SET_FPEXC_CTL
65# define SET_FPEXC_CTL(a,b) (-EINVAL)
66#endif
67#ifndef GET_FPEXC_CTL
68# define GET_FPEXC_CTL(a,b) (-EINVAL)
69#endif
Anton Blanchard651d7652006-06-07 16:10:19 +100070#ifndef GET_ENDIAN
71# define GET_ENDIAN(a,b) (-EINVAL)
72#endif
73#ifndef SET_ENDIAN
74# define SET_ENDIAN(a,b) (-EINVAL)
75#endif
Erik Bosman8fb402b2008-04-11 18:54:17 +020076#ifndef GET_TSC_CTL
77# define GET_TSC_CTL(a) (-EINVAL)
78#endif
79#ifndef SET_TSC_CTL
80# define SET_TSC_CTL(a) (-EINVAL)
81#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/*
84 * this is where the system-wide overflow UID and GID are defined, for
85 * architectures that now have 32-bit UID/GID but didn't in the past
86 */
87
88int overflowuid = DEFAULT_OVERFLOWUID;
89int overflowgid = DEFAULT_OVERFLOWGID;
90
91#ifdef CONFIG_UID16
92EXPORT_SYMBOL(overflowuid);
93EXPORT_SYMBOL(overflowgid);
94#endif
95
96/*
97 * the same as above, but for filesystems which can only store a 16-bit
98 * UID and GID. as such, this is needed on all architectures
99 */
100
101int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
102int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
103
104EXPORT_SYMBOL(fs_overflowuid);
105EXPORT_SYMBOL(fs_overflowgid);
106
107/*
108 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
109 */
110
111int C_A_D = 1;
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700112struct pid *cad_pid;
113EXPORT_SYMBOL(cad_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115/*
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700116 * If set, this is used for preparing the system to power off.
117 */
118
119void (*pm_power_off_prepare)(void);
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700120
David Howellsc69e8d92008-11-14 10:39:19 +1100121/*
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700122 * Returns true if current's euid is same as p's uid or euid,
123 * or has CAP_SYS_NICE to p's user_ns.
124 *
125 * Called with rcu_read_lock, creds are safe
126 */
127static bool set_one_prio_perm(struct task_struct *p)
128{
129 const struct cred *cred = current_cred(), *pcred = __task_cred(p);
130
131 if (pcred->user->user_ns == cred->user->user_ns &&
132 (pcred->uid == cred->euid ||
133 pcred->euid == cred->euid))
134 return true;
135 if (ns_capable(pcred->user->user_ns, CAP_SYS_NICE))
136 return true;
137 return false;
138}
139
140/*
David Howellsc69e8d92008-11-14 10:39:19 +1100141 * set the priority of a task
142 * - the caller must hold the RCU read lock
143 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static int set_one_prio(struct task_struct *p, int niceval, int error)
145{
146 int no_nice;
147
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700148 if (!set_one_prio_perm(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 error = -EPERM;
150 goto out;
151 }
Matt Mackalle43379f2005-05-01 08:59:00 -0700152 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 error = -EACCES;
154 goto out;
155 }
156 no_nice = security_task_setnice(p, niceval);
157 if (no_nice) {
158 error = no_nice;
159 goto out;
160 }
161 if (error == -ESRCH)
162 error = 0;
163 set_user_nice(p, niceval);
164out:
165 return error;
166}
167
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100168SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 struct task_struct *g, *p;
171 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100172 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 int error = -EINVAL;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800174 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Daniel Walker3e88c552007-05-10 22:22:53 -0700176 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 goto out;
178
179 /* normalize: avoid signed division (rounding problems) */
180 error = -ESRCH;
181 if (niceval < -20)
182 niceval = -20;
183 if (niceval > 19)
184 niceval = 19;
185
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000186 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 read_lock(&tasklist_lock);
188 switch (which) {
189 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800190 if (who)
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700191 p = find_task_by_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800192 else
193 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (p)
195 error = set_one_prio(p, niceval, error);
196 break;
197 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800198 if (who)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700199 pgrp = find_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800200 else
201 pgrp = task_pgrp(current);
Ken Chen2d70b682008-08-20 14:09:17 -0700202 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 error = set_one_prio(p, niceval, error);
Ken Chen2d70b682008-08-20 14:09:17 -0700204 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 break;
206 case PRIO_USER:
David Howellsd84f4f92008-11-14 10:39:23 +1100207 user = (struct user_struct *) cred->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (!who)
David Howells86a264a2008-11-14 10:39:18 +1100209 who = cred->uid;
210 else if ((who != cred->uid) &&
211 !(user = find_user(who)))
212 goto out_unlock; /* No processes for this user */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
H Hartley Sweetendfc6a732009-12-14 18:00:22 -0800214 do_each_thread(g, p) {
David Howells86a264a2008-11-14 10:39:18 +1100215 if (__task_cred(p)->uid == who)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 error = set_one_prio(p, niceval, error);
H Hartley Sweetendfc6a732009-12-14 18:00:22 -0800217 } while_each_thread(g, p);
David Howells86a264a2008-11-14 10:39:18 +1100218 if (who != cred->uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 free_uid(user); /* For find_user() */
220 break;
221 }
222out_unlock:
223 read_unlock(&tasklist_lock);
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000224 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225out:
226 return error;
227}
228
229/*
230 * Ugh. To avoid negative return values, "getpriority()" will
231 * not return the normal nice-value, but a negated value that
232 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
233 * to stay compatible.
234 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100235SYSCALL_DEFINE2(getpriority, int, which, int, who)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 struct task_struct *g, *p;
238 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100239 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 long niceval, retval = -ESRCH;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800241 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Daniel Walker3e88c552007-05-10 22:22:53 -0700243 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return -EINVAL;
245
Tetsuo Handa70118832010-02-22 12:44:16 -0800246 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 read_lock(&tasklist_lock);
248 switch (which) {
249 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800250 if (who)
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700251 p = find_task_by_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800252 else
253 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (p) {
255 niceval = 20 - task_nice(p);
256 if (niceval > retval)
257 retval = niceval;
258 }
259 break;
260 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800261 if (who)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700262 pgrp = find_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800263 else
264 pgrp = task_pgrp(current);
Ken Chen2d70b682008-08-20 14:09:17 -0700265 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 niceval = 20 - task_nice(p);
267 if (niceval > retval)
268 retval = niceval;
Ken Chen2d70b682008-08-20 14:09:17 -0700269 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 break;
271 case PRIO_USER:
David Howells86a264a2008-11-14 10:39:18 +1100272 user = (struct user_struct *) cred->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (!who)
David Howells86a264a2008-11-14 10:39:18 +1100274 who = cred->uid;
275 else if ((who != cred->uid) &&
276 !(user = find_user(who)))
277 goto out_unlock; /* No processes for this user */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
H Hartley Sweetendfc6a732009-12-14 18:00:22 -0800279 do_each_thread(g, p) {
David Howells86a264a2008-11-14 10:39:18 +1100280 if (__task_cred(p)->uid == who) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 niceval = 20 - task_nice(p);
282 if (niceval > retval)
283 retval = niceval;
284 }
H Hartley Sweetendfc6a732009-12-14 18:00:22 -0800285 } while_each_thread(g, p);
David Howells86a264a2008-11-14 10:39:18 +1100286 if (who != cred->uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 free_uid(user); /* for find_user() */
288 break;
289 }
290out_unlock:
291 read_unlock(&tasklist_lock);
Tetsuo Handa70118832010-02-22 12:44:16 -0800292 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 return retval;
295}
296
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700297/**
298 * emergency_restart - reboot the system
299 *
300 * Without shutting down any hardware or taking any locks
301 * reboot the system. This is called when we know we are in
302 * trouble so this is our best effort to reboot. This is
303 * safe to call in interrupt context.
304 */
Eric W. Biederman7c903472005-07-26 11:29:55 -0600305void emergency_restart(void)
306{
Seiji Aguchi04c68622011-01-12 16:59:30 -0800307 kmsg_dump(KMSG_DUMP_EMERG);
Eric W. Biederman7c903472005-07-26 11:29:55 -0600308 machine_emergency_restart();
309}
310EXPORT_SYMBOL_GPL(emergency_restart);
311
Huang Yingca195b72008-08-15 00:40:24 -0700312void kernel_restart_prepare(char *cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600313{
Alan Sterne041c682006-03-27 01:16:30 -0800314 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600315 system_state = SYSTEM_RESTART;
Kay Sieversb50fa7c2011-05-05 13:32:05 +0200316 usermodehelper_disable();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600317 device_shutdown();
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100318 syscore_shutdown();
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700319}
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800320
321/**
Amerigo Wangc5f41752011-07-25 17:13:10 -0700322 * register_reboot_notifier - Register function to be called at reboot time
323 * @nb: Info about notifier function to be called
324 *
325 * Registers a function with the list of functions
326 * to be called at reboot time.
327 *
328 * Currently always returns zero, as blocking_notifier_chain_register()
329 * always returns zero.
330 */
331int register_reboot_notifier(struct notifier_block *nb)
332{
333 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
334}
335EXPORT_SYMBOL(register_reboot_notifier);
336
337/**
338 * unregister_reboot_notifier - Unregister previously registered reboot notifier
339 * @nb: Hook to be unregistered
340 *
341 * Unregisters a previously registered reboot
342 * notifier function.
343 *
344 * Returns zero on success, or %-ENOENT on failure.
345 */
346int unregister_reboot_notifier(struct notifier_block *nb)
347{
348 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
349}
350EXPORT_SYMBOL(unregister_reboot_notifier);
351
352/**
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800353 * kernel_restart - reboot the system
354 * @cmd: pointer to buffer containing command to execute for restart
Randy Dunlapb8887e62005-11-07 01:01:07 -0800355 * or %NULL
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800356 *
357 * Shutdown everything and perform a clean reboot.
358 * This is not safe to call in interrupt context.
359 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700360void kernel_restart(char *cmd)
361{
362 kernel_restart_prepare(cmd);
Cal Peake756184b2006-09-30 23:27:24 -0700363 if (!cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600364 printk(KERN_EMERG "Restarting system.\n");
Cal Peake756184b2006-09-30 23:27:24 -0700365 else
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600366 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
Seiji Aguchi04c68622011-01-12 16:59:30 -0800367 kmsg_dump(KMSG_DUMP_RESTART);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600368 machine_restart(cmd);
369}
370EXPORT_SYMBOL_GPL(kernel_restart);
371
Adrian Bunk4ef72292008-02-04 22:30:06 -0800372static void kernel_shutdown_prepare(enum system_states state)
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500373{
Alan Sterne041c682006-03-27 01:16:30 -0800374 blocking_notifier_call_chain(&reboot_notifier_list,
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500375 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
376 system_state = state;
Kay Sieversb50fa7c2011-05-05 13:32:05 +0200377 usermodehelper_disable();
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500378 device_shutdown();
379}
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700380/**
381 * kernel_halt - halt the system
382 *
383 * Shutdown everything and perform a clean system halt.
384 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700385void kernel_halt(void)
386{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500387 kernel_shutdown_prepare(SYSTEM_HALT);
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100388 syscore_shutdown();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600389 printk(KERN_EMERG "System halted.\n");
Seiji Aguchi04c68622011-01-12 16:59:30 -0800390 kmsg_dump(KMSG_DUMP_HALT);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600391 machine_halt();
392}
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500393
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600394EXPORT_SYMBOL_GPL(kernel_halt);
395
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700396/**
397 * kernel_power_off - power_off the system
398 *
399 * Shutdown everything and perform a clean system power_off.
400 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700401void kernel_power_off(void)
402{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500403 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700404 if (pm_power_off_prepare)
405 pm_power_off_prepare();
Mark Lord40477272007-10-01 01:20:10 -0700406 disable_nonboot_cpus();
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100407 syscore_shutdown();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600408 printk(KERN_EMERG "Power down.\n");
Seiji Aguchi04c68622011-01-12 16:59:30 -0800409 kmsg_dump(KMSG_DUMP_POWEROFF);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600410 machine_power_off();
411}
412EXPORT_SYMBOL_GPL(kernel_power_off);
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200413
414static DEFINE_MUTEX(reboot_mutex);
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/*
417 * Reboot system call: for obvious reasons only root may call it,
418 * and even root needs to set up some magic numbers in the registers
419 * so that some mistake won't make this reboot the whole machine.
420 * You can also set the meaning of the ctrl-alt-del-key here.
421 *
422 * reboot doesn't sync: do that yourself before calling this.
423 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100424SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
425 void __user *, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 char buffer[256];
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700428 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 /* We only trust the superuser with rebooting the system. */
431 if (!capable(CAP_SYS_BOOT))
432 return -EPERM;
433
434 /* For safety, we require "magic" arguments. */
435 if (magic1 != LINUX_REBOOT_MAGIC1 ||
436 (magic2 != LINUX_REBOOT_MAGIC2 &&
437 magic2 != LINUX_REBOOT_MAGIC2A &&
438 magic2 != LINUX_REBOOT_MAGIC2B &&
439 magic2 != LINUX_REBOOT_MAGIC2C))
440 return -EINVAL;
441
Eric W. Biederman5e382912006-01-08 01:03:46 -0800442 /* Instead of trying to make the power_off code look like
443 * halt when pm_power_off is not set do it the easy way.
444 */
445 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
446 cmd = LINUX_REBOOT_CMD_HALT;
447
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200448 mutex_lock(&reboot_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 switch (cmd) {
450 case LINUX_REBOOT_CMD_RESTART:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600451 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 break;
453
454 case LINUX_REBOOT_CMD_CAD_ON:
455 C_A_D = 1;
456 break;
457
458 case LINUX_REBOOT_CMD_CAD_OFF:
459 C_A_D = 0;
460 break;
461
462 case LINUX_REBOOT_CMD_HALT:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600463 kernel_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 do_exit(0);
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700465 panic("cannot halt");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 case LINUX_REBOOT_CMD_POWER_OFF:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600468 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 do_exit(0);
470 break;
471
472 case LINUX_REBOOT_CMD_RESTART2:
473 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200474 ret = -EFAULT;
475 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477 buffer[sizeof(buffer) - 1] = '\0';
478
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600479 kernel_restart(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 break;
481
Huang Ying3ab83522008-07-25 19:45:07 -0700482#ifdef CONFIG_KEXEC
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700483 case LINUX_REBOOT_CMD_KEXEC:
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700484 ret = kernel_kexec();
485 break;
Huang Ying3ab83522008-07-25 19:45:07 -0700486#endif
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600487
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200488#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 case LINUX_REBOOT_CMD_SW_SUSPEND:
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700490 ret = hibernate();
491 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492#endif
493
494 default:
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700495 ret = -EINVAL;
496 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200498 mutex_unlock(&reboot_mutex);
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700499 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
David Howells65f27f32006-11-22 14:55:48 +0000502static void deferred_cad(struct work_struct *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Eric W. Biedermanabcd9e52005-07-26 11:27:34 -0600504 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507/*
508 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
509 * As it's called within an interrupt, it may NOT sync: the only choice
510 * is whether to reboot at once, or just ignore the ctrl-alt-del.
511 */
512void ctrl_alt_del(void)
513{
David Howells65f27f32006-11-22 14:55:48 +0000514 static DECLARE_WORK(cad_work, deferred_cad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 if (C_A_D)
517 schedule_work(&cad_work);
518 else
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700519 kill_cad_pid(SIGINT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/*
523 * Unprivileged users may change the real gid to the effective gid
524 * or vice versa. (BSD-style)
525 *
526 * If you set the real gid at all, or set the effective gid to a value not
527 * equal to the real gid, then the saved gid is set to the new effective gid.
528 *
529 * This makes it possible for a setgid program to completely drop its
530 * privileges, which is often a useful assertion to make when you are doing
531 * a security audit over a program.
532 *
533 * The general idea is that a program which uses just setregid() will be
534 * 100% compatible with BSD. A program which uses just setgid() will be
535 * 100% compatible with POSIX with saved IDs.
536 *
537 * SMP: There are not races, the GIDs are checked only by filesystem
538 * operations (as far as semantic preservation is concerned).
539 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100540SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
David Howellsd84f4f92008-11-14 10:39:23 +1100542 const struct cred *old;
543 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 int retval;
545
David Howellsd84f4f92008-11-14 10:39:23 +1100546 new = prepare_creds();
547 if (!new)
548 return -ENOMEM;
549 old = current_cred();
550
David Howellsd84f4f92008-11-14 10:39:23 +1100551 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (rgid != (gid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100553 if (old->gid == rgid ||
554 old->egid == rgid ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700555 nsown_capable(CAP_SETGID))
David Howellsd84f4f92008-11-14 10:39:23 +1100556 new->gid = rgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 else
David Howellsd84f4f92008-11-14 10:39:23 +1100558 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560 if (egid != (gid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100561 if (old->gid == egid ||
562 old->egid == egid ||
563 old->sgid == egid ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700564 nsown_capable(CAP_SETGID))
David Howellsd84f4f92008-11-14 10:39:23 +1100565 new->egid = egid;
Cal Peake756184b2006-09-30 23:27:24 -0700566 else
David Howellsd84f4f92008-11-14 10:39:23 +1100567 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
David Howellsd84f4f92008-11-14 10:39:23 +1100569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (rgid != (gid_t) -1 ||
David Howellsd84f4f92008-11-14 10:39:23 +1100571 (egid != (gid_t) -1 && egid != old->gid))
572 new->sgid = new->egid;
573 new->fsgid = new->egid;
574
575 return commit_creds(new);
576
577error:
578 abort_creds(new);
579 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
582/*
583 * setgid() is implemented like SysV w/ SAVED_IDS
584 *
585 * SMP: Same implicit races as above.
586 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100587SYSCALL_DEFINE1(setgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
David Howellsd84f4f92008-11-14 10:39:23 +1100589 const struct cred *old;
590 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 int retval;
592
David Howellsd84f4f92008-11-14 10:39:23 +1100593 new = prepare_creds();
594 if (!new)
595 return -ENOMEM;
596 old = current_cred();
597
David Howellsd84f4f92008-11-14 10:39:23 +1100598 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700599 if (nsown_capable(CAP_SETGID))
David Howellsd84f4f92008-11-14 10:39:23 +1100600 new->gid = new->egid = new->sgid = new->fsgid = gid;
601 else if (gid == old->gid || gid == old->sgid)
602 new->egid = new->fsgid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 else
David Howellsd84f4f92008-11-14 10:39:23 +1100604 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
David Howellsd84f4f92008-11-14 10:39:23 +1100606 return commit_creds(new);
607
608error:
609 abort_creds(new);
610 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
Dhaval Giani54e99122009-02-27 15:13:54 +0530612
David Howellsd84f4f92008-11-14 10:39:23 +1100613/*
614 * change the user struct in a credentials set to match the new UID
615 */
616static int set_user(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 struct user_struct *new_user;
619
Serge Hallyn18b6e042008-10-15 16:38:45 -0500620 new_user = alloc_uid(current_user_ns(), new->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (!new_user)
622 return -EAGAIN;
623
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400624 /*
625 * We don't fail in case of NPROC limit excess here because too many
626 * poorly written programs don't check set*uid() return code, assuming
627 * it never fails if called by root. We may still enforce NPROC limit
628 * for programs doing set*uid()+execve() by harmlessly deferring the
629 * failure to the execve() stage.
630 */
Jiri Slaby78d7d402010-03-05 13:42:54 -0800631 if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400632 new_user != INIT_USER)
633 current->flags |= PF_NPROC_EXCEEDED;
634 else
635 current->flags &= ~PF_NPROC_EXCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
David Howellsd84f4f92008-11-14 10:39:23 +1100637 free_uid(new->user);
638 new->user = new_user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return 0;
640}
641
642/*
643 * Unprivileged users may change the real uid to the effective uid
644 * or vice versa. (BSD-style)
645 *
646 * If you set the real uid at all, or set the effective uid to a value not
647 * equal to the real uid, then the saved uid is set to the new effective uid.
648 *
649 * This makes it possible for a setuid program to completely drop its
650 * privileges, which is often a useful assertion to make when you are doing
651 * a security audit over a program.
652 *
653 * The general idea is that a program which uses just setreuid() will be
654 * 100% compatible with BSD. A program which uses just setuid() will be
655 * 100% compatible with POSIX with saved IDs.
656 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100657SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
David Howellsd84f4f92008-11-14 10:39:23 +1100659 const struct cred *old;
660 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 int retval;
662
David Howellsd84f4f92008-11-14 10:39:23 +1100663 new = prepare_creds();
664 if (!new)
665 return -ENOMEM;
666 old = current_cred();
667
David Howellsd84f4f92008-11-14 10:39:23 +1100668 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (ruid != (uid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100670 new->uid = ruid;
671 if (old->uid != ruid &&
672 old->euid != ruid &&
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700673 !nsown_capable(CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100674 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
677 if (euid != (uid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100678 new->euid = euid;
679 if (old->uid != euid &&
680 old->euid != euid &&
681 old->suid != euid &&
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700682 !nsown_capable(CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100683 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
Dhaval Giani54e99122009-02-27 15:13:54 +0530686 if (new->uid != old->uid) {
687 retval = set_user(new);
688 if (retval < 0)
689 goto error;
690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (ruid != (uid_t) -1 ||
David Howellsd84f4f92008-11-14 10:39:23 +1100692 (euid != (uid_t) -1 && euid != old->uid))
693 new->suid = new->euid;
694 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
David Howellsd84f4f92008-11-14 10:39:23 +1100696 retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
697 if (retval < 0)
698 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
David Howellsd84f4f92008-11-14 10:39:23 +1100700 return commit_creds(new);
701
702error:
703 abort_creds(new);
704 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707/*
708 * setuid() is implemented like SysV with SAVED_IDS
709 *
710 * Note that SAVED_ID's is deficient in that a setuid root program
711 * like sendmail, for example, cannot set its uid to be a normal
712 * user and then switch back, because if you're root, setuid() sets
713 * the saved uid too. If you don't like this, blame the bright people
714 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
715 * will allow a root program to temporarily drop privileges and be able to
716 * regain them by swapping the real and effective uid.
717 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100718SYSCALL_DEFINE1(setuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
David Howellsd84f4f92008-11-14 10:39:23 +1100720 const struct cred *old;
721 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 int retval;
723
David Howellsd84f4f92008-11-14 10:39:23 +1100724 new = prepare_creds();
725 if (!new)
726 return -ENOMEM;
727 old = current_cred();
728
David Howellsd84f4f92008-11-14 10:39:23 +1100729 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700730 if (nsown_capable(CAP_SETUID)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100731 new->suid = new->uid = uid;
Dhaval Giani54e99122009-02-27 15:13:54 +0530732 if (uid != old->uid) {
733 retval = set_user(new);
734 if (retval < 0)
735 goto error;
David Howellsd84f4f92008-11-14 10:39:23 +1100736 }
737 } else if (uid != old->uid && uid != new->suid) {
738 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
David Howellsd84f4f92008-11-14 10:39:23 +1100741 new->fsuid = new->euid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
David Howellsd84f4f92008-11-14 10:39:23 +1100743 retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
744 if (retval < 0)
745 goto error;
746
747 return commit_creds(new);
748
749error:
750 abort_creds(new);
751 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754
755/*
756 * This function implements a generic ability to update ruid, euid,
757 * and suid. This allows you to implement the 4.4 compatible seteuid().
758 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100759SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
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
David Howellsd84f4f92008-11-14 10:39:23 +1100769 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
David Howellsd84f4f92008-11-14 10:39:23 +1100771 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700772 if (!nsown_capable(CAP_SETUID)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100773 if (ruid != (uid_t) -1 && ruid != old->uid &&
774 ruid != old->euid && ruid != old->suid)
775 goto error;
776 if (euid != (uid_t) -1 && euid != old->uid &&
777 euid != old->euid && euid != old->suid)
778 goto error;
779 if (suid != (uid_t) -1 && suid != old->uid &&
780 suid != old->euid && suid != old->suid)
781 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
David Howellsd84f4f92008-11-14 10:39:23 +1100783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (ruid != (uid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100785 new->uid = ruid;
Dhaval Giani54e99122009-02-27 15:13:54 +0530786 if (ruid != old->uid) {
787 retval = set_user(new);
788 if (retval < 0)
789 goto error;
790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
David Howellsd84f4f92008-11-14 10:39:23 +1100792 if (euid != (uid_t) -1)
793 new->euid = euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (suid != (uid_t) -1)
David Howellsd84f4f92008-11-14 10:39:23 +1100795 new->suid = suid;
796 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
David Howellsd84f4f92008-11-14 10:39:23 +1100798 retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
799 if (retval < 0)
800 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
David Howellsd84f4f92008-11-14 10:39:23 +1100802 return commit_creds(new);
803
804error:
805 abort_creds(new);
806 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
Heiko Carstensdbf040d2009-01-14 14:14:04 +0100809SYSCALL_DEFINE3(getresuid, uid_t __user *, ruid, uid_t __user *, euid, uid_t __user *, suid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
David Howells86a264a2008-11-14 10:39:18 +1100811 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 int retval;
813
David Howells86a264a2008-11-14 10:39:18 +1100814 if (!(retval = put_user(cred->uid, ruid)) &&
815 !(retval = put_user(cred->euid, euid)))
David Howellsb6dff3e2008-11-14 10:39:16 +1100816 retval = put_user(cred->suid, suid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 return retval;
819}
820
821/*
822 * Same as above, but for rgid, egid, sgid.
823 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100824SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
David Howellsd84f4f92008-11-14 10:39:23 +1100826 const struct cred *old;
827 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 int retval;
829
David Howellsd84f4f92008-11-14 10:39:23 +1100830 new = prepare_creds();
831 if (!new)
832 return -ENOMEM;
833 old = current_cred();
834
David Howellsd84f4f92008-11-14 10:39:23 +1100835 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700836 if (!nsown_capable(CAP_SETGID)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100837 if (rgid != (gid_t) -1 && rgid != old->gid &&
838 rgid != old->egid && rgid != old->sgid)
839 goto error;
840 if (egid != (gid_t) -1 && egid != old->gid &&
841 egid != old->egid && egid != old->sgid)
842 goto error;
843 if (sgid != (gid_t) -1 && sgid != old->gid &&
844 sgid != old->egid && sgid != old->sgid)
845 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
David Howellsd84f4f92008-11-14 10:39:23 +1100848 if (rgid != (gid_t) -1)
849 new->gid = rgid;
850 if (egid != (gid_t) -1)
851 new->egid = egid;
852 if (sgid != (gid_t) -1)
853 new->sgid = sgid;
854 new->fsgid = new->egid;
855
856 return commit_creds(new);
857
858error:
859 abort_creds(new);
860 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
Heiko Carstensdbf040d2009-01-14 14:14:04 +0100863SYSCALL_DEFINE3(getresgid, gid_t __user *, rgid, gid_t __user *, egid, gid_t __user *, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
David Howells86a264a2008-11-14 10:39:18 +1100865 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 int retval;
867
David Howells86a264a2008-11-14 10:39:18 +1100868 if (!(retval = put_user(cred->gid, rgid)) &&
869 !(retval = put_user(cred->egid, egid)))
David Howellsb6dff3e2008-11-14 10:39:16 +1100870 retval = put_user(cred->sgid, sgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 return retval;
873}
874
875
876/*
877 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
878 * is used for "access()" and for the NFS daemon (letting nfsd stay at
879 * whatever uid it wants to). It normally shadows "euid", except when
880 * explicitly set by setfsuid() or for access..
881 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100882SYSCALL_DEFINE1(setfsuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
David Howellsd84f4f92008-11-14 10:39:23 +1100884 const struct cred *old;
885 struct cred *new;
886 uid_t old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
David Howellsd84f4f92008-11-14 10:39:23 +1100888 new = prepare_creds();
889 if (!new)
890 return current_fsuid();
891 old = current_cred();
892 old_fsuid = old->fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
David Howellsd84f4f92008-11-14 10:39:23 +1100894 if (uid == old->uid || uid == old->euid ||
895 uid == old->suid || uid == old->fsuid ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700896 nsown_capable(CAP_SETUID)) {
Cal Peake756184b2006-09-30 23:27:24 -0700897 if (uid != old_fsuid) {
David Howellsd84f4f92008-11-14 10:39:23 +1100898 new->fsuid = uid;
899 if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
900 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903
David Howellsd84f4f92008-11-14 10:39:23 +1100904 abort_creds(new);
905 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
David Howellsd84f4f92008-11-14 10:39:23 +1100907change_okay:
908 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return old_fsuid;
910}
911
912/*
John Anthony Kazos Jrf42df9e2007-05-09 08:23:08 +0200913 * Samma på svenska..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100915SYSCALL_DEFINE1(setfsgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
David Howellsd84f4f92008-11-14 10:39:23 +1100917 const struct cred *old;
918 struct cred *new;
919 gid_t old_fsgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
David Howellsd84f4f92008-11-14 10:39:23 +1100921 new = prepare_creds();
922 if (!new)
923 return current_fsgid();
924 old = current_cred();
925 old_fsgid = old->fsgid;
926
David Howellsd84f4f92008-11-14 10:39:23 +1100927 if (gid == old->gid || gid == old->egid ||
928 gid == old->sgid || gid == old->fsgid ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700929 nsown_capable(CAP_SETGID)) {
Cal Peake756184b2006-09-30 23:27:24 -0700930 if (gid != old_fsgid) {
David Howellsd84f4f92008-11-14 10:39:23 +1100931 new->fsgid = gid;
932 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
David Howellsd84f4f92008-11-14 10:39:23 +1100935
David Howellsd84f4f92008-11-14 10:39:23 +1100936 abort_creds(new);
937 return old_fsgid;
938
939change_okay:
940 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return old_fsgid;
942}
943
Frank Mayharf06febc2008-09-12 09:54:39 -0700944void do_sys_times(struct tms *tms)
945{
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +0900946 cputime_t tgutime, tgstime, cutime, cstime;
Frank Mayharf06febc2008-09-12 09:54:39 -0700947
Oleg Nesterov2b5fe6d2008-11-17 15:40:08 +0100948 spin_lock_irq(&current->sighand->siglock);
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +0900949 thread_group_times(current, &tgutime, &tgstime);
Frank Mayharf06febc2008-09-12 09:54:39 -0700950 cutime = current->signal->cutime;
951 cstime = current->signal->cstime;
952 spin_unlock_irq(&current->sighand->siglock);
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +0900953 tms->tms_utime = cputime_to_clock_t(tgutime);
954 tms->tms_stime = cputime_to_clock_t(tgstime);
Frank Mayharf06febc2008-09-12 09:54:39 -0700955 tms->tms_cutime = cputime_to_clock_t(cutime);
956 tms->tms_cstime = cputime_to_clock_t(cstime);
957}
958
Heiko Carstens58fd3aa2009-01-14 14:14:03 +0100959SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (tbuf) {
962 struct tms tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Frank Mayharf06febc2008-09-12 09:54:39 -0700964 do_sys_times(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
966 return -EFAULT;
967 }
Paul Mackerrase3d5a272009-01-06 14:41:02 -0800968 force_successful_syscall_return();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return (long) jiffies_64_to_clock_t(get_jiffies_64());
970}
971
972/*
973 * This needs some heavy checking ...
974 * I just haven't the stomach for it. I also don't fully
975 * understand sessions/pgrp etc. Let somebody who does explain it.
976 *
977 * OK, I think I have the protection semantics right.... this is really
978 * only important on a multi-user system anyway, to make sure one user
979 * can't send a signal to a process owned by another. -TYT, 12/12/91
980 *
981 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
982 * LBT 04.03.94
983 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +0100984SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 struct task_struct *p;
Oleg Nesterovee0acf92006-01-08 01:03:53 -0800987 struct task_struct *group_leader = current->group_leader;
Oleg Nesterov4e021302008-02-08 04:19:08 -0800988 struct pid *pgrp;
989 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 if (!pid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700992 pid = task_pid_vnr(group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (!pgid)
994 pgid = pid;
995 if (pgid < 0)
996 return -EINVAL;
Paul E. McKenney950eaac2010-08-31 17:00:18 -0700997 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 /* From this point forward we keep holding onto the tasklist lock
1000 * so that our parent does not change from under us. -DaveM
1001 */
1002 write_lock_irq(&tasklist_lock);
1003
1004 err = -ESRCH;
Oleg Nesterov4e021302008-02-08 04:19:08 -08001005 p = find_task_by_vpid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (!p)
1007 goto out;
1008
1009 err = -EINVAL;
1010 if (!thread_group_leader(p))
1011 goto out;
1012
Oleg Nesterov4e021302008-02-08 04:19:08 -08001013 if (same_thread_group(p->real_parent, group_leader)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 err = -EPERM;
Eric W. Biederman41487c62007-02-12 00:53:01 -08001015 if (task_session(p) != task_session(group_leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 goto out;
1017 err = -EACCES;
1018 if (p->did_exec)
1019 goto out;
1020 } else {
1021 err = -ESRCH;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001022 if (p != group_leader)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 goto out;
1024 }
1025
1026 err = -EPERM;
1027 if (p->signal->leader)
1028 goto out;
1029
Oleg Nesterov4e021302008-02-08 04:19:08 -08001030 pgrp = task_pid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (pgid != pid) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001032 struct task_struct *g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Oleg Nesterov4e021302008-02-08 04:19:08 -08001034 pgrp = find_vpid(pgid);
1035 g = pid_task(pgrp, PIDTYPE_PGID);
Eric W. Biederman41487c62007-02-12 00:53:01 -08001036 if (!g || task_session(g) != task_session(group_leader))
Oleg Nesterovf020bc42006-12-08 02:38:02 -08001037 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 err = security_task_setpgid(p, pgid);
1041 if (err)
1042 goto out;
1043
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07001044 if (task_pgrp(p) != pgrp)
Oleg Nesterov83beaf32008-04-30 00:54:27 -07001045 change_pid(p, PIDTYPE_PGID, pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 err = 0;
1048out:
1049 /* All paths lead to here, thus we are safe. -DaveM */
1050 write_unlock_irq(&tasklist_lock);
Paul E. McKenney950eaac2010-08-31 17:00:18 -07001051 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return err;
1053}
1054
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001055SYSCALL_DEFINE1(getpgid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001057 struct task_struct *p;
1058 struct pid *grp;
1059 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001061 rcu_read_lock();
1062 if (!pid)
1063 grp = task_pgrp(current);
1064 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 retval = -ESRCH;
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001066 p = find_task_by_vpid(pid);
1067 if (!p)
1068 goto out;
1069 grp = task_pgrp(p);
1070 if (!grp)
1071 goto out;
1072
1073 retval = security_task_getpgid(p);
1074 if (retval)
1075 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001077 retval = pid_vnr(grp);
1078out:
1079 rcu_read_unlock();
1080 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
1083#ifdef __ARCH_WANT_SYS_GETPGRP
1084
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001085SYSCALL_DEFINE0(getpgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001087 return sys_getpgid(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
1090#endif
1091
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001092SYSCALL_DEFINE1(getsid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001094 struct task_struct *p;
1095 struct pid *sid;
1096 int retval;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001097
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001098 rcu_read_lock();
1099 if (!pid)
1100 sid = task_session(current);
1101 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 retval = -ESRCH;
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001103 p = find_task_by_vpid(pid);
1104 if (!p)
1105 goto out;
1106 sid = task_session(p);
1107 if (!sid)
1108 goto out;
1109
1110 retval = security_task_getsid(p);
1111 if (retval)
1112 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001114 retval = pid_vnr(sid);
1115out:
1116 rcu_read_unlock();
1117 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
Heiko Carstensb290ebe2009-01-14 14:14:06 +01001120SYSCALL_DEFINE0(setsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Oren Laadane19f2472006-01-08 01:03:58 -08001122 struct task_struct *group_leader = current->group_leader;
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001123 struct pid *sid = task_pid(group_leader);
1124 pid_t session = pid_vnr(sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 int err = -EPERM;
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 write_lock_irq(&tasklist_lock);
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001128 /* Fail if I am already a session leader */
1129 if (group_leader->signal->leader)
1130 goto out;
1131
Oleg Nesterov430c6232008-02-08 04:19:11 -08001132 /* Fail if a process group id already exists that equals the
1133 * proposed session id.
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001134 */
Oleg Nesterov6806aac2008-02-08 04:19:12 -08001135 if (pid_task(sid, PIDTYPE_PGID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 goto out;
1137
Oren Laadane19f2472006-01-08 01:03:58 -08001138 group_leader->signal->leader = 1;
Oleg Nesterov8520d7c2008-02-08 04:19:09 -08001139 __set_special_pids(sid);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001140
Alan Cox9c9f4de2008-10-13 10:37:26 +01001141 proc_clear_tty(group_leader);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001142
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001143 err = session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144out:
1145 write_unlock_irq(&tasklist_lock);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001146 if (err > 0) {
Christian Borntraeger0d0df592009-10-26 16:49:34 -07001147 proc_sid_connector(group_leader);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001148 sched_autogroup_create_attach(group_leader);
1149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return err;
1151}
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153DECLARE_RWSEM(uts_sem);
1154
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001155#ifdef COMPAT_UTS_MACHINE
1156#define override_architecture(name) \
Andreas Schwab46da2762010-04-23 13:17:44 -04001157 (personality(current->personality) == PER_LINUX32 && \
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001158 copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1159 sizeof(COMPAT_UTS_MACHINE)))
1160#else
1161#define override_architecture(name) 0
1162#endif
1163
Heiko Carstense48fbb62009-01-14 14:14:26 +01001164SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 int errno = 0;
1167
1168 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001169 if (copy_to_user(name, utsname(), sizeof *name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 errno = -EFAULT;
1171 up_read(&uts_sem);
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001172
1173 if (!errno && override_architecture(name))
1174 errno = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return errno;
1176}
1177
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001178#ifdef __ARCH_WANT_SYS_OLD_UNAME
1179/*
1180 * Old cruft
1181 */
1182SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
1183{
1184 int error = 0;
1185
1186 if (!name)
1187 return -EFAULT;
1188
1189 down_read(&uts_sem);
1190 if (copy_to_user(name, utsname(), sizeof(*name)))
1191 error = -EFAULT;
1192 up_read(&uts_sem);
1193
1194 if (!error && override_architecture(name))
1195 error = -EFAULT;
1196 return error;
1197}
1198
1199SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
1200{
1201 int error;
1202
1203 if (!name)
1204 return -EFAULT;
1205 if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
1206 return -EFAULT;
1207
1208 down_read(&uts_sem);
1209 error = __copy_to_user(&name->sysname, &utsname()->sysname,
1210 __OLD_UTS_LEN);
1211 error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
1212 error |= __copy_to_user(&name->nodename, &utsname()->nodename,
1213 __OLD_UTS_LEN);
1214 error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
1215 error |= __copy_to_user(&name->release, &utsname()->release,
1216 __OLD_UTS_LEN);
1217 error |= __put_user(0, name->release + __OLD_UTS_LEN);
1218 error |= __copy_to_user(&name->version, &utsname()->version,
1219 __OLD_UTS_LEN);
1220 error |= __put_user(0, name->version + __OLD_UTS_LEN);
1221 error |= __copy_to_user(&name->machine, &utsname()->machine,
1222 __OLD_UTS_LEN);
1223 error |= __put_user(0, name->machine + __OLD_UTS_LEN);
1224 up_read(&uts_sem);
1225
1226 if (!error && override_architecture(name))
1227 error = -EFAULT;
1228 return error ? -EFAULT : 0;
1229}
1230#endif
1231
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001232SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234 int errno;
1235 char tmp[__NEW_UTS_LEN];
1236
Serge E. Hallynbb96a6f2011-03-23 16:43:18 -07001237 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (len < 0 || len > __NEW_UTS_LEN)
1241 return -EINVAL;
1242 down_write(&uts_sem);
1243 errno = -EFAULT;
1244 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001245 struct new_utsname *u = utsname();
1246
1247 memcpy(u->nodename, tmp, len);
1248 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 errno = 0;
1250 }
1251 up_write(&uts_sem);
1252 return errno;
1253}
1254
1255#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1256
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001257SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 int i, errno;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001260 struct new_utsname *u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 if (len < 0)
1263 return -EINVAL;
1264 down_read(&uts_sem);
Andrew Morton9679e4d2008-10-15 22:01:51 -07001265 u = utsname();
1266 i = 1 + strlen(u->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (i > len)
1268 i = len;
1269 errno = 0;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001270 if (copy_to_user(name, u->nodename, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 errno = -EFAULT;
1272 up_read(&uts_sem);
1273 return errno;
1274}
1275
1276#endif
1277
1278/*
1279 * Only setdomainname; getdomainname can be implemented by calling
1280 * uname()
1281 */
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001282SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 int errno;
1285 char tmp[__NEW_UTS_LEN];
1286
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001287 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return -EPERM;
1289 if (len < 0 || len > __NEW_UTS_LEN)
1290 return -EINVAL;
1291
1292 down_write(&uts_sem);
1293 errno = -EFAULT;
1294 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001295 struct new_utsname *u = utsname();
1296
1297 memcpy(u->domainname, tmp, len);
1298 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 errno = 0;
1300 }
1301 up_write(&uts_sem);
1302 return errno;
1303}
1304
Heiko Carstense48fbb62009-01-14 14:14:26 +01001305SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
Jiri Slabyb9518342010-05-04 11:28:25 +02001307 struct rlimit value;
1308 int ret;
1309
1310 ret = do_prlimit(current, resource, NULL, &value);
1311 if (!ret)
1312 ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1313
1314 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
1317#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1318
1319/*
1320 * Back compatibility for getrlimit. Needed for some apps.
1321 */
1322
Heiko Carstense48fbb62009-01-14 14:14:26 +01001323SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1324 struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
1326 struct rlimit x;
1327 if (resource >= RLIM_NLIMITS)
1328 return -EINVAL;
1329
1330 task_lock(current->group_leader);
1331 x = current->signal->rlim[resource];
1332 task_unlock(current->group_leader);
Cal Peake756184b2006-09-30 23:27:24 -07001333 if (x.rlim_cur > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 x.rlim_cur = 0x7FFFFFFF;
Cal Peake756184b2006-09-30 23:27:24 -07001335 if (x.rlim_max > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 x.rlim_max = 0x7FFFFFFF;
1337 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1338}
1339
1340#endif
1341
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001342static inline bool rlim64_is_infinity(__u64 rlim64)
1343{
1344#if BITS_PER_LONG < 64
1345 return rlim64 >= ULONG_MAX;
1346#else
1347 return rlim64 == RLIM64_INFINITY;
1348#endif
1349}
1350
1351static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1352{
1353 if (rlim->rlim_cur == RLIM_INFINITY)
1354 rlim64->rlim_cur = RLIM64_INFINITY;
1355 else
1356 rlim64->rlim_cur = rlim->rlim_cur;
1357 if (rlim->rlim_max == RLIM_INFINITY)
1358 rlim64->rlim_max = RLIM64_INFINITY;
1359 else
1360 rlim64->rlim_max = rlim->rlim_max;
1361}
1362
1363static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1364{
1365 if (rlim64_is_infinity(rlim64->rlim_cur))
1366 rlim->rlim_cur = RLIM_INFINITY;
1367 else
1368 rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1369 if (rlim64_is_infinity(rlim64->rlim_max))
1370 rlim->rlim_max = RLIM_INFINITY;
1371 else
1372 rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1373}
1374
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001375/* make sure you are allowed to change @tsk limits before calling this */
Jiri Slaby5b415352010-03-24 16:11:29 +01001376int do_prlimit(struct task_struct *tsk, unsigned int resource,
1377 struct rlimit *new_rlim, struct rlimit *old_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Jiri Slaby5b415352010-03-24 16:11:29 +01001379 struct rlimit *rlim;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001380 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 if (resource >= RLIM_NLIMITS)
1383 return -EINVAL;
Jiri Slaby5b415352010-03-24 16:11:29 +01001384 if (new_rlim) {
1385 if (new_rlim->rlim_cur > new_rlim->rlim_max)
1386 return -EINVAL;
1387 if (resource == RLIMIT_NOFILE &&
1388 new_rlim->rlim_max > sysctl_nr_open)
1389 return -EPERM;
1390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001392 /* protect tsk->signal and tsk->sighand from disappearing */
1393 read_lock(&tasklist_lock);
1394 if (!tsk->sighand) {
1395 retval = -ESRCH;
1396 goto out;
1397 }
1398
Jiri Slaby5b415352010-03-24 16:11:29 +01001399 rlim = tsk->signal->rlim + resource;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001400 task_lock(tsk->group_leader);
Jiri Slaby5b415352010-03-24 16:11:29 +01001401 if (new_rlim) {
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001402 /* Keep the capable check against init_user_ns until
1403 cgroups can contain all limits */
Jiri Slaby5b415352010-03-24 16:11:29 +01001404 if (new_rlim->rlim_max > rlim->rlim_max &&
1405 !capable(CAP_SYS_RESOURCE))
1406 retval = -EPERM;
1407 if (!retval)
1408 retval = security_task_setrlimit(tsk->group_leader,
1409 resource, new_rlim);
1410 if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
1411 /*
1412 * The caller is asking for an immediate RLIMIT_CPU
1413 * expiry. But we use the zero value to mean "it was
1414 * never set". So let's cheat and make it one second
1415 * instead
1416 */
1417 new_rlim->rlim_cur = 1;
1418 }
Tom Alsberg9926e4c2007-05-08 00:30:31 -07001419 }
Jiri Slaby5b415352010-03-24 16:11:29 +01001420 if (!retval) {
1421 if (old_rlim)
1422 *old_rlim = *rlim;
1423 if (new_rlim)
1424 *rlim = *new_rlim;
1425 }
Jiri Slaby7855c352009-08-26 23:45:34 +02001426 task_unlock(tsk->group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Andrew Mortond3561f72006-03-24 03:18:36 -08001428 /*
1429 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1430 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1431 * very long-standing error, and fixing it now risks breakage of
1432 * applications, so we live with it
1433 */
Jiri Slaby5b415352010-03-24 16:11:29 +01001434 if (!retval && new_rlim && resource == RLIMIT_CPU &&
1435 new_rlim->rlim_cur != RLIM_INFINITY)
1436 update_rlimit_cpu(tsk, new_rlim->rlim_cur);
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001437out:
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001438 read_unlock(&tasklist_lock);
Oleg Nesterov2fb9d262009-09-03 19:21:45 +02001439 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001442/* rcu lock must be held */
1443static int check_prlimit_permission(struct task_struct *task)
1444{
1445 const struct cred *cred = current_cred(), *tcred;
1446
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001447 if (current == task)
1448 return 0;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001449
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001450 tcred = __task_cred(task);
1451 if (cred->user->user_ns == tcred->user->user_ns &&
1452 (cred->uid == tcred->euid &&
1453 cred->uid == tcred->suid &&
1454 cred->uid == tcred->uid &&
1455 cred->gid == tcred->egid &&
1456 cred->gid == tcred->sgid &&
1457 cred->gid == tcred->gid))
1458 return 0;
1459 if (ns_capable(tcred->user->user_ns, CAP_SYS_RESOURCE))
1460 return 0;
1461
1462 return -EPERM;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001463}
1464
1465SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1466 const struct rlimit64 __user *, new_rlim,
1467 struct rlimit64 __user *, old_rlim)
1468{
1469 struct rlimit64 old64, new64;
1470 struct rlimit old, new;
1471 struct task_struct *tsk;
1472 int ret;
1473
1474 if (new_rlim) {
1475 if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1476 return -EFAULT;
1477 rlim64_to_rlim(&new64, &new);
1478 }
1479
1480 rcu_read_lock();
1481 tsk = pid ? find_task_by_vpid(pid) : current;
1482 if (!tsk) {
1483 rcu_read_unlock();
1484 return -ESRCH;
1485 }
1486 ret = check_prlimit_permission(tsk);
1487 if (ret) {
1488 rcu_read_unlock();
1489 return ret;
1490 }
1491 get_task_struct(tsk);
1492 rcu_read_unlock();
1493
1494 ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1495 old_rlim ? &old : NULL);
1496
1497 if (!ret && old_rlim) {
1498 rlim_to_rlim64(&old, &old64);
1499 if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1500 ret = -EFAULT;
1501 }
1502
1503 put_task_struct(tsk);
1504 return ret;
1505}
1506
Jiri Slaby7855c352009-08-26 23:45:34 +02001507SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1508{
1509 struct rlimit new_rlim;
1510
1511 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1512 return -EFAULT;
Jiri Slaby5b415352010-03-24 16:11:29 +01001513 return do_prlimit(current, resource, &new_rlim, NULL);
Jiri Slaby7855c352009-08-26 23:45:34 +02001514}
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516/*
1517 * It would make sense to put struct rusage in the task_struct,
1518 * except that would make the task_struct be *really big*. After
1519 * task_struct gets moved into malloc'ed memory, it would
1520 * make sense to do this. It will make moving the rest of the information
1521 * a lot simpler! (Which we're not doing right now because we're not
1522 * measuring them yet).
1523 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1525 * races with threads incrementing their own counters. But since word
1526 * reads are atomic, we either get new values or old values and we don't
1527 * care which for the sums. We always take the siglock to protect reading
1528 * the c* fields from p->signal from races with exit.c updating those
1529 * fields when reaping, so a sample either gets all the additions of a
1530 * given child after it's reaped, or none so this sample is before reaping.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001531 *
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001532 * Locking:
1533 * We need to take the siglock for CHILDEREN, SELF and BOTH
1534 * for the cases current multithreaded, non-current single threaded
1535 * non-current multithreaded. Thread traversal is now safe with
1536 * the siglock held.
1537 * Strictly speaking, we donot need to take the siglock if we are current and
1538 * single threaded, as no one else can take our signal_struct away, no one
1539 * else can reap the children to update signal->c* counters, and no one else
1540 * can race with the signal-> fields. If we do not take any lock, the
1541 * signal-> fields could be read out of order while another thread was just
1542 * exiting. So we should place a read memory barrier when we avoid the lock.
1543 * On the writer side, write memory barrier is implied in __exit_signal
1544 * as __exit_signal releases the siglock spinlock after updating the signal->
1545 * fields. But we don't do this yet to keep things simple.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001546 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 */
1548
Frank Mayharf06febc2008-09-12 09:54:39 -07001549static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001550{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001551 r->ru_nvcsw += t->nvcsw;
1552 r->ru_nivcsw += t->nivcsw;
1553 r->ru_minflt += t->min_flt;
1554 r->ru_majflt += t->maj_flt;
1555 r->ru_inblock += task_io_get_inblock(t);
1556 r->ru_oublock += task_io_get_oublock(t);
1557}
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1560{
1561 struct task_struct *t;
1562 unsigned long flags;
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09001563 cputime_t tgutime, tgstime, utime, stime;
Jiri Pirko1f102062009-09-22 16:44:10 -07001564 unsigned long maxrss = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 memset((char *) r, 0, sizeof *r);
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001567 utime = stime = cputime_zero;
1568
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001569 if (who == RUSAGE_THREAD) {
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09001570 task_times(current, &utime, &stime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001571 accumulate_thread_rusage(p, r);
Jiri Pirko1f102062009-09-22 16:44:10 -07001572 maxrss = p->signal->maxrss;
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001573 goto out;
1574 }
1575
Oleg Nesterovd6cf7232008-04-30 00:52:38 -07001576 if (!lock_task_sighand(p, &flags))
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001577 return;
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 switch (who) {
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001580 case RUSAGE_BOTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 case RUSAGE_CHILDREN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 utime = p->signal->cutime;
1583 stime = p->signal->cstime;
1584 r->ru_nvcsw = p->signal->cnvcsw;
1585 r->ru_nivcsw = p->signal->cnivcsw;
1586 r->ru_minflt = p->signal->cmin_flt;
1587 r->ru_majflt = p->signal->cmaj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07001588 r->ru_inblock = p->signal->cinblock;
1589 r->ru_oublock = p->signal->coublock;
Jiri Pirko1f102062009-09-22 16:44:10 -07001590 maxrss = p->signal->cmaxrss;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001591
1592 if (who == RUSAGE_CHILDREN)
1593 break;
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 case RUSAGE_SELF:
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09001596 thread_group_times(p, &tgutime, &tgstime);
1597 utime = cputime_add(utime, tgutime);
1598 stime = cputime_add(stime, tgstime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 r->ru_nvcsw += p->signal->nvcsw;
1600 r->ru_nivcsw += p->signal->nivcsw;
1601 r->ru_minflt += p->signal->min_flt;
1602 r->ru_majflt += p->signal->maj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07001603 r->ru_inblock += p->signal->inblock;
1604 r->ru_oublock += p->signal->oublock;
Jiri Pirko1f102062009-09-22 16:44:10 -07001605 if (maxrss < p->signal->maxrss)
1606 maxrss = p->signal->maxrss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 t = p;
1608 do {
Frank Mayharf06febc2008-09-12 09:54:39 -07001609 accumulate_thread_rusage(t, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 t = next_thread(t);
1611 } while (t != p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 break;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 default:
1615 BUG();
1616 }
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001617 unlock_task_sighand(p, &flags);
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001618
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001619out:
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001620 cputime_to_timeval(utime, &r->ru_utime);
1621 cputime_to_timeval(stime, &r->ru_stime);
Jiri Pirko1f102062009-09-22 16:44:10 -07001622
1623 if (who != RUSAGE_CHILDREN) {
1624 struct mm_struct *mm = get_task_mm(p);
1625 if (mm) {
1626 setmax_mm_hiwater_rss(&maxrss, mm);
1627 mmput(mm);
1628 }
1629 }
1630 r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
1633int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1634{
1635 struct rusage r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 k_getrusage(p, who, &r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1638}
1639
Heiko Carstense48fbb62009-01-14 14:14:26 +01001640SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001642 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1643 who != RUSAGE_THREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return -EINVAL;
1645 return getrusage(current, who, ru);
1646}
1647
Heiko Carstense48fbb62009-01-14 14:14:26 +01001648SYSCALL_DEFINE1(umask, int, mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
1650 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1651 return mask;
1652}
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -08001653
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001654SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
1655 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
David Howellsb6dff3e2008-11-14 10:39:16 +11001657 struct task_struct *me = current;
1658 unsigned char comm[sizeof(me->comm)];
1659 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
David Howellsd84f4f92008-11-14 10:39:23 +11001661 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1662 if (error != -ENOSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return error;
1664
David Howellsd84f4f92008-11-14 10:39:23 +11001665 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 switch (option) {
1667 case PR_SET_PDEATHSIG:
Jesper Juhl0730ded2005-09-06 15:17:37 -07001668 if (!valid_signal(arg2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 error = -EINVAL;
1670 break;
1671 }
David Howellsb6dff3e2008-11-14 10:39:16 +11001672 me->pdeath_signal = arg2;
1673 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 break;
1675 case PR_GET_PDEATHSIG:
David Howellsb6dff3e2008-11-14 10:39:16 +11001676 error = put_user(me->pdeath_signal, (int __user *)arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 break;
1678 case PR_GET_DUMPABLE:
David Howellsb6dff3e2008-11-14 10:39:16 +11001679 error = get_dumpable(me->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 break;
1681 case PR_SET_DUMPABLE:
Marcel Holtmannabf75a52006-07-12 13:12:00 +02001682 if (arg2 < 0 || arg2 > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 error = -EINVAL;
1684 break;
1685 }
David Howellsb6dff3e2008-11-14 10:39:16 +11001686 set_dumpable(me->mm, arg2);
1687 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 break;
1689
1690 case PR_SET_UNALIGN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001691 error = SET_UNALIGN_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 break;
1693 case PR_GET_UNALIGN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001694 error = GET_UNALIGN_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 break;
1696 case PR_SET_FPEMU:
David Howellsb6dff3e2008-11-14 10:39:16 +11001697 error = SET_FPEMU_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 break;
1699 case PR_GET_FPEMU:
David Howellsb6dff3e2008-11-14 10:39:16 +11001700 error = GET_FPEMU_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 break;
1702 case PR_SET_FPEXC:
David Howellsb6dff3e2008-11-14 10:39:16 +11001703 error = SET_FPEXC_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 break;
1705 case PR_GET_FPEXC:
David Howellsb6dff3e2008-11-14 10:39:16 +11001706 error = GET_FPEXC_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 break;
1708 case PR_GET_TIMING:
1709 error = PR_TIMING_STATISTICAL;
1710 break;
1711 case PR_SET_TIMING:
Shi Weihua7b266552008-05-23 13:04:59 -07001712 if (arg2 != PR_TIMING_STATISTICAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 error = -EINVAL;
David Howellsb6dff3e2008-11-14 10:39:16 +11001714 else
1715 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 break;
1717
David Howellsb6dff3e2008-11-14 10:39:16 +11001718 case PR_SET_NAME:
1719 comm[sizeof(me->comm)-1] = 0;
1720 if (strncpy_from_user(comm, (char __user *)arg2,
1721 sizeof(me->comm) - 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return -EFAULT;
David Howellsb6dff3e2008-11-14 10:39:16 +11001723 set_task_comm(me, comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return 0;
David Howellsb6dff3e2008-11-14 10:39:16 +11001725 case PR_GET_NAME:
1726 get_task_comm(comm, me);
1727 if (copy_to_user((char __user *)arg2, comm,
1728 sizeof(comm)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return -EFAULT;
1730 return 0;
Anton Blanchard651d7652006-06-07 16:10:19 +10001731 case PR_GET_ENDIAN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001732 error = GET_ENDIAN(me, arg2);
Anton Blanchard651d7652006-06-07 16:10:19 +10001733 break;
1734 case PR_SET_ENDIAN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001735 error = SET_ENDIAN(me, arg2);
Anton Blanchard651d7652006-06-07 16:10:19 +10001736 break;
1737
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -07001738 case PR_GET_SECCOMP:
1739 error = prctl_get_seccomp();
1740 break;
1741 case PR_SET_SECCOMP:
1742 error = prctl_set_seccomp(arg2);
1743 break;
Erik Bosman8fb402b2008-04-11 18:54:17 +02001744 case PR_GET_TSC:
1745 error = GET_TSC_CTL(arg2);
1746 break;
1747 case PR_SET_TSC:
1748 error = SET_TSC_CTL(arg2);
1749 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001750 case PR_TASK_PERF_EVENTS_DISABLE:
1751 error = perf_event_task_disable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001752 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001753 case PR_TASK_PERF_EVENTS_ENABLE:
1754 error = perf_event_task_enable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001755 break;
Arjan van de Ven69766752008-09-01 15:52:40 -07001756 case PR_GET_TIMERSLACK:
1757 error = current->timer_slack_ns;
1758 break;
1759 case PR_SET_TIMERSLACK:
1760 if (arg2 <= 0)
1761 current->timer_slack_ns =
1762 current->default_timer_slack_ns;
1763 else
1764 current->timer_slack_ns = arg2;
David Howellsb6dff3e2008-11-14 10:39:16 +11001765 error = 0;
Arjan van de Ven69766752008-09-01 15:52:40 -07001766 break;
Andi Kleen4db96cf2009-09-16 11:50:14 +02001767 case PR_MCE_KILL:
1768 if (arg4 | arg5)
1769 return -EINVAL;
1770 switch (arg2) {
Andi Kleen1087e9b2009-10-04 02:20:11 +02001771 case PR_MCE_KILL_CLEAR:
Andi Kleen4db96cf2009-09-16 11:50:14 +02001772 if (arg3 != 0)
1773 return -EINVAL;
1774 current->flags &= ~PF_MCE_PROCESS;
1775 break;
Andi Kleen1087e9b2009-10-04 02:20:11 +02001776 case PR_MCE_KILL_SET:
Andi Kleen4db96cf2009-09-16 11:50:14 +02001777 current->flags |= PF_MCE_PROCESS;
Andi Kleen1087e9b2009-10-04 02:20:11 +02001778 if (arg3 == PR_MCE_KILL_EARLY)
Andi Kleen4db96cf2009-09-16 11:50:14 +02001779 current->flags |= PF_MCE_EARLY;
Andi Kleen1087e9b2009-10-04 02:20:11 +02001780 else if (arg3 == PR_MCE_KILL_LATE)
Andi Kleen4db96cf2009-09-16 11:50:14 +02001781 current->flags &= ~PF_MCE_EARLY;
Andi Kleen1087e9b2009-10-04 02:20:11 +02001782 else if (arg3 == PR_MCE_KILL_DEFAULT)
1783 current->flags &=
1784 ~(PF_MCE_EARLY|PF_MCE_PROCESS);
1785 else
1786 return -EINVAL;
Andi Kleen4db96cf2009-09-16 11:50:14 +02001787 break;
1788 default:
1789 return -EINVAL;
1790 }
1791 error = 0;
1792 break;
Andi Kleen1087e9b2009-10-04 02:20:11 +02001793 case PR_MCE_KILL_GET:
1794 if (arg2 | arg3 | arg4 | arg5)
1795 return -EINVAL;
1796 if (current->flags & PF_MCE_PROCESS)
1797 error = (current->flags & PF_MCE_EARLY) ?
1798 PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
1799 else
1800 error = PR_MCE_KILL_DEFAULT;
1801 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 default:
1803 error = -EINVAL;
1804 break;
1805 }
1806 return error;
1807}
Andi Kleen3cfc3482006-09-26 10:52:28 +02001808
Heiko Carstens836f92a2009-01-14 14:14:33 +01001809SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
1810 struct getcpu_cache __user *, unused)
Andi Kleen3cfc3482006-09-26 10:52:28 +02001811{
1812 int err = 0;
1813 int cpu = raw_smp_processor_id();
1814 if (cpup)
1815 err |= put_user(cpu, cpup);
1816 if (nodep)
1817 err |= put_user(cpu_to_node(cpu), nodep);
Andi Kleen3cfc3482006-09-26 10:52:28 +02001818 return err ? -EFAULT : 0;
1819}
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07001820
1821char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
1822
Neil Hormana06a4dc2010-05-26 14:42:58 -07001823static void argv_cleanup(struct subprocess_info *info)
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07001824{
Neil Hormana06a4dc2010-05-26 14:42:58 -07001825 argv_free(info->argv);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07001826}
1827
1828/**
1829 * orderly_poweroff - Trigger an orderly system poweroff
1830 * @force: force poweroff if command execution fails
1831 *
1832 * This may be called from any context to trigger a system shutdown.
1833 * If the orderly shutdown fails, it will force an immediate shutdown.
1834 */
1835int orderly_poweroff(bool force)
1836{
1837 int argc;
1838 char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
1839 static char *envp[] = {
1840 "HOME=/",
1841 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
1842 NULL
1843 };
1844 int ret = -ENOMEM;
1845 struct subprocess_info *info;
1846
1847 if (argv == NULL) {
1848 printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
1849 __func__, poweroff_cmd);
1850 goto out;
1851 }
1852
KOSAKI Motohiroac331d12008-07-25 01:45:38 -07001853 info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07001854 if (info == NULL) {
1855 argv_free(argv);
1856 goto out;
1857 }
1858
Neil Hormana06a4dc2010-05-26 14:42:58 -07001859 call_usermodehelper_setfns(info, NULL, argv_cleanup, NULL);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07001860
Jeremy Fitzhardinge86313c42007-07-17 18:37:03 -07001861 ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07001862
1863 out:
1864 if (ret && force) {
1865 printk(KERN_WARNING "Failed to start orderly shutdown: "
1866 "forcing the issue\n");
1867
1868 /* I guess this should try to kick off some daemon to
1869 sync and poweroff asap. Or not even bother syncing
1870 if we're doing an emergency shutdown? */
1871 emergency_sync();
1872 kernel_power_off();
1873 }
1874
1875 return ret;
1876}
1877EXPORT_SYMBOL_GPL(orderly_poweroff);