blob: bc1dc61c31edfba9d7e38cfac6cdc553c01715e4 [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>
11#include <linux/smp_lock.h>
12#include <linux/notifier.h>
13#include <linux/reboot.h>
14#include <linux/prctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/highuid.h>
16#include <linux/fs.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020017#include <linux/perf_event.h>
Daniel Walker3e88c552007-05-10 22:22:53 -070018#include <linux/resource.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070019#include <linux/kernel.h>
20#include <linux/kexec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/workqueue.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080022#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/device.h>
24#include <linux/key.h>
25#include <linux/times.h>
26#include <linux/posix-timers.h>
27#include <linux/security.h>
28#include <linux/dcookies.h>
29#include <linux/suspend.h>
30#include <linux/tty.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070031#include <linux/signal.h>
Matt Helsley9f460802005-11-07 00:59:16 -080032#include <linux/cn_proc.h>
Andi Kleen3cfc3482006-09-26 10:52:28 +020033#include <linux/getcpu.h>
Eric Dumazet6eaeeab2007-05-10 22:22:37 -070034#include <linux/task_io_accounting_ops.h>
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -070035#include <linux/seccomp.h>
Mark Lord40477272007-10-01 01:20:10 -070036#include <linux/cpu.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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <linux/compat.h>
41#include <linux/syscalls.h>
Keshavamurthy Anil S00d7c052005-12-12 00:37:33 -080042#include <linux/kprobes.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070043#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <asm/uaccess.h>
46#include <asm/io.h>
47#include <asm/unistd.h>
48
49#ifndef SET_UNALIGN_CTL
50# define SET_UNALIGN_CTL(a,b) (-EINVAL)
51#endif
52#ifndef GET_UNALIGN_CTL
53# define GET_UNALIGN_CTL(a,b) (-EINVAL)
54#endif
55#ifndef SET_FPEMU_CTL
56# define SET_FPEMU_CTL(a,b) (-EINVAL)
57#endif
58#ifndef GET_FPEMU_CTL
59# define GET_FPEMU_CTL(a,b) (-EINVAL)
60#endif
61#ifndef SET_FPEXC_CTL
62# define SET_FPEXC_CTL(a,b) (-EINVAL)
63#endif
64#ifndef GET_FPEXC_CTL
65# define GET_FPEXC_CTL(a,b) (-EINVAL)
66#endif
Anton Blanchard651d7652006-06-07 16:10:19 +100067#ifndef GET_ENDIAN
68# define GET_ENDIAN(a,b) (-EINVAL)
69#endif
70#ifndef SET_ENDIAN
71# define SET_ENDIAN(a,b) (-EINVAL)
72#endif
Erik Bosman8fb402b2008-04-11 18:54:17 +020073#ifndef GET_TSC_CTL
74# define GET_TSC_CTL(a) (-EINVAL)
75#endif
76#ifndef SET_TSC_CTL
77# define SET_TSC_CTL(a) (-EINVAL)
78#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/*
81 * this is where the system-wide overflow UID and GID are defined, for
82 * architectures that now have 32-bit UID/GID but didn't in the past
83 */
84
85int overflowuid = DEFAULT_OVERFLOWUID;
86int overflowgid = DEFAULT_OVERFLOWGID;
87
88#ifdef CONFIG_UID16
89EXPORT_SYMBOL(overflowuid);
90EXPORT_SYMBOL(overflowgid);
91#endif
92
93/*
94 * the same as above, but for filesystems which can only store a 16-bit
95 * UID and GID. as such, this is needed on all architectures
96 */
97
98int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
99int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
100
101EXPORT_SYMBOL(fs_overflowuid);
102EXPORT_SYMBOL(fs_overflowgid);
103
104/*
105 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
106 */
107
108int C_A_D = 1;
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700109struct pid *cad_pid;
110EXPORT_SYMBOL(cad_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/*
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700113 * If set, this is used for preparing the system to power off.
114 */
115
116void (*pm_power_off_prepare)(void);
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700117
David Howellsc69e8d92008-11-14 10:39:19 +1100118/*
119 * set the priority of a task
120 * - the caller must hold the RCU read lock
121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static int set_one_prio(struct task_struct *p, int niceval, int error)
123{
David Howellsc69e8d92008-11-14 10:39:19 +1100124 const struct cred *cred = current_cred(), *pcred = __task_cred(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 int no_nice;
126
David Howellsc69e8d92008-11-14 10:39:19 +1100127 if (pcred->uid != cred->euid &&
128 pcred->euid != cred->euid && !capable(CAP_SYS_NICE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 error = -EPERM;
130 goto out;
131 }
Matt Mackalle43379f2005-05-01 08:59:00 -0700132 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 error = -EACCES;
134 goto out;
135 }
136 no_nice = security_task_setnice(p, niceval);
137 if (no_nice) {
138 error = no_nice;
139 goto out;
140 }
141 if (error == -ESRCH)
142 error = 0;
143 set_user_nice(p, niceval);
144out:
145 return error;
146}
147
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100148SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 struct task_struct *g, *p;
151 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100152 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int error = -EINVAL;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800154 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Daniel Walker3e88c552007-05-10 22:22:53 -0700156 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 goto out;
158
159 /* normalize: avoid signed division (rounding problems) */
160 error = -ESRCH;
161 if (niceval < -20)
162 niceval = -20;
163 if (niceval > 19)
164 niceval = 19;
165
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000166 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 read_lock(&tasklist_lock);
168 switch (which) {
169 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800170 if (who)
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700171 p = find_task_by_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800172 else
173 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (p)
175 error = set_one_prio(p, niceval, error);
176 break;
177 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800178 if (who)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700179 pgrp = find_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800180 else
181 pgrp = task_pgrp(current);
Ken Chen2d70b682008-08-20 14:09:17 -0700182 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 error = set_one_prio(p, niceval, error);
Ken Chen2d70b682008-08-20 14:09:17 -0700184 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 break;
186 case PRIO_USER:
David Howellsd84f4f92008-11-14 10:39:23 +1100187 user = (struct user_struct *) cred->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (!who)
David Howells86a264a2008-11-14 10:39:18 +1100189 who = cred->uid;
190 else if ((who != cred->uid) &&
191 !(user = find_user(who)))
192 goto out_unlock; /* No processes for this user */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 do_each_thread(g, p)
David Howells86a264a2008-11-14 10:39:18 +1100195 if (__task_cred(p)->uid == who)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 error = set_one_prio(p, niceval, error);
197 while_each_thread(g, p);
David Howells86a264a2008-11-14 10:39:18 +1100198 if (who != cred->uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 free_uid(user); /* For find_user() */
200 break;
201 }
202out_unlock:
203 read_unlock(&tasklist_lock);
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000204 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205out:
206 return error;
207}
208
209/*
210 * Ugh. To avoid negative return values, "getpriority()" will
211 * not return the normal nice-value, but a negated value that
212 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
213 * to stay compatible.
214 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100215SYSCALL_DEFINE2(getpriority, int, which, int, who)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 struct task_struct *g, *p;
218 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100219 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 long niceval, retval = -ESRCH;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800221 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Daniel Walker3e88c552007-05-10 22:22:53 -0700223 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -EINVAL;
225
226 read_lock(&tasklist_lock);
227 switch (which) {
228 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800229 if (who)
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700230 p = find_task_by_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800231 else
232 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (p) {
234 niceval = 20 - task_nice(p);
235 if (niceval > retval)
236 retval = niceval;
237 }
238 break;
239 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800240 if (who)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700241 pgrp = find_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800242 else
243 pgrp = task_pgrp(current);
Ken Chen2d70b682008-08-20 14:09:17 -0700244 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 niceval = 20 - task_nice(p);
246 if (niceval > retval)
247 retval = niceval;
Ken Chen2d70b682008-08-20 14:09:17 -0700248 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 break;
250 case PRIO_USER:
David Howells86a264a2008-11-14 10:39:18 +1100251 user = (struct user_struct *) cred->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (!who)
David Howells86a264a2008-11-14 10:39:18 +1100253 who = cred->uid;
254 else if ((who != cred->uid) &&
255 !(user = find_user(who)))
256 goto out_unlock; /* No processes for this user */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 do_each_thread(g, p)
David Howells86a264a2008-11-14 10:39:18 +1100259 if (__task_cred(p)->uid == who) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 niceval = 20 - task_nice(p);
261 if (niceval > retval)
262 retval = niceval;
263 }
264 while_each_thread(g, p);
David Howells86a264a2008-11-14 10:39:18 +1100265 if (who != cred->uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 free_uid(user); /* for find_user() */
267 break;
268 }
269out_unlock:
270 read_unlock(&tasklist_lock);
271
272 return retval;
273}
274
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700275/**
276 * emergency_restart - reboot the system
277 *
278 * Without shutting down any hardware or taking any locks
279 * reboot the system. This is called when we know we are in
280 * trouble so this is our best effort to reboot. This is
281 * safe to call in interrupt context.
282 */
Eric W. Biederman7c903472005-07-26 11:29:55 -0600283void emergency_restart(void)
284{
285 machine_emergency_restart();
286}
287EXPORT_SYMBOL_GPL(emergency_restart);
288
Huang Yingca195b72008-08-15 00:40:24 -0700289void kernel_restart_prepare(char *cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600290{
Alan Sterne041c682006-03-27 01:16:30 -0800291 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600292 system_state = SYSTEM_RESTART;
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600293 device_shutdown();
Rafael J. Wysocki58b3b712007-07-26 16:29:55 +0200294 sysdev_shutdown();
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700295}
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800296
297/**
298 * kernel_restart - reboot the system
299 * @cmd: pointer to buffer containing command to execute for restart
Randy Dunlapb8887e62005-11-07 01:01:07 -0800300 * or %NULL
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800301 *
302 * Shutdown everything and perform a clean reboot.
303 * This is not safe to call in interrupt context.
304 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700305void kernel_restart(char *cmd)
306{
307 kernel_restart_prepare(cmd);
Cal Peake756184b2006-09-30 23:27:24 -0700308 if (!cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600309 printk(KERN_EMERG "Restarting system.\n");
Cal Peake756184b2006-09-30 23:27:24 -0700310 else
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600311 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600312 machine_restart(cmd);
313}
314EXPORT_SYMBOL_GPL(kernel_restart);
315
Adrian Bunk4ef72292008-02-04 22:30:06 -0800316static void kernel_shutdown_prepare(enum system_states state)
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500317{
Alan Sterne041c682006-03-27 01:16:30 -0800318 blocking_notifier_call_chain(&reboot_notifier_list,
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500319 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
320 system_state = state;
321 device_shutdown();
322}
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700323/**
324 * kernel_halt - halt the system
325 *
326 * Shutdown everything and perform a clean system halt.
327 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700328void kernel_halt(void)
329{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500330 kernel_shutdown_prepare(SYSTEM_HALT);
Rafael J. Wysocki58b3b712007-07-26 16:29:55 +0200331 sysdev_shutdown();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600332 printk(KERN_EMERG "System halted.\n");
333 machine_halt();
334}
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500335
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600336EXPORT_SYMBOL_GPL(kernel_halt);
337
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700338/**
339 * kernel_power_off - power_off the system
340 *
341 * Shutdown everything and perform a clean system power_off.
342 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700343void kernel_power_off(void)
344{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500345 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700346 if (pm_power_off_prepare)
347 pm_power_off_prepare();
Mark Lord40477272007-10-01 01:20:10 -0700348 disable_nonboot_cpus();
Rafael J. Wysocki58b3b712007-07-26 16:29:55 +0200349 sysdev_shutdown();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600350 printk(KERN_EMERG "Power down.\n");
351 machine_power_off();
352}
353EXPORT_SYMBOL_GPL(kernel_power_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354/*
355 * Reboot system call: for obvious reasons only root may call it,
356 * and even root needs to set up some magic numbers in the registers
357 * so that some mistake won't make this reboot the whole machine.
358 * You can also set the meaning of the ctrl-alt-del-key here.
359 *
360 * reboot doesn't sync: do that yourself before calling this.
361 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100362SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
363 void __user *, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 char buffer[256];
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700366 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 /* We only trust the superuser with rebooting the system. */
369 if (!capable(CAP_SYS_BOOT))
370 return -EPERM;
371
372 /* For safety, we require "magic" arguments. */
373 if (magic1 != LINUX_REBOOT_MAGIC1 ||
374 (magic2 != LINUX_REBOOT_MAGIC2 &&
375 magic2 != LINUX_REBOOT_MAGIC2A &&
376 magic2 != LINUX_REBOOT_MAGIC2B &&
377 magic2 != LINUX_REBOOT_MAGIC2C))
378 return -EINVAL;
379
Eric W. Biederman5e382912006-01-08 01:03:46 -0800380 /* Instead of trying to make the power_off code look like
381 * halt when pm_power_off is not set do it the easy way.
382 */
383 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
384 cmd = LINUX_REBOOT_CMD_HALT;
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 lock_kernel();
387 switch (cmd) {
388 case LINUX_REBOOT_CMD_RESTART:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600389 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 break;
391
392 case LINUX_REBOOT_CMD_CAD_ON:
393 C_A_D = 1;
394 break;
395
396 case LINUX_REBOOT_CMD_CAD_OFF:
397 C_A_D = 0;
398 break;
399
400 case LINUX_REBOOT_CMD_HALT:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600401 kernel_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 unlock_kernel();
403 do_exit(0);
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700404 panic("cannot halt");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 case LINUX_REBOOT_CMD_POWER_OFF:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600407 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 unlock_kernel();
409 do_exit(0);
410 break;
411
412 case LINUX_REBOOT_CMD_RESTART2:
413 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
414 unlock_kernel();
415 return -EFAULT;
416 }
417 buffer[sizeof(buffer) - 1] = '\0';
418
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600419 kernel_restart(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 break;
421
Huang Ying3ab83522008-07-25 19:45:07 -0700422#ifdef CONFIG_KEXEC
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700423 case LINUX_REBOOT_CMD_KEXEC:
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700424 ret = kernel_kexec();
425 break;
Huang Ying3ab83522008-07-25 19:45:07 -0700426#endif
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600427
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200428#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 case LINUX_REBOOT_CMD_SW_SUSPEND:
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700430 ret = hibernate();
431 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432#endif
433
434 default:
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700435 ret = -EINVAL;
436 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438 unlock_kernel();
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700439 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
David Howells65f27f32006-11-22 14:55:48 +0000442static void deferred_cad(struct work_struct *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Eric W. Biedermanabcd9e52005-07-26 11:27:34 -0600444 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447/*
448 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
449 * As it's called within an interrupt, it may NOT sync: the only choice
450 * is whether to reboot at once, or just ignore the ctrl-alt-del.
451 */
452void ctrl_alt_del(void)
453{
David Howells65f27f32006-11-22 14:55:48 +0000454 static DECLARE_WORK(cad_work, deferred_cad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 if (C_A_D)
457 schedule_work(&cad_work);
458 else
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700459 kill_cad_pid(SIGINT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462/*
463 * Unprivileged users may change the real gid to the effective gid
464 * or vice versa. (BSD-style)
465 *
466 * If you set the real gid at all, or set the effective gid to a value not
467 * equal to the real gid, then the saved gid is set to the new effective gid.
468 *
469 * This makes it possible for a setgid program to completely drop its
470 * privileges, which is often a useful assertion to make when you are doing
471 * a security audit over a program.
472 *
473 * The general idea is that a program which uses just setregid() will be
474 * 100% compatible with BSD. A program which uses just setgid() will be
475 * 100% compatible with POSIX with saved IDs.
476 *
477 * SMP: There are not races, the GIDs are checked only by filesystem
478 * operations (as far as semantic preservation is concerned).
479 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100480SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
David Howellsd84f4f92008-11-14 10:39:23 +1100482 const struct cred *old;
483 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 int retval;
485
David Howellsd84f4f92008-11-14 10:39:23 +1100486 new = prepare_creds();
487 if (!new)
488 return -ENOMEM;
489 old = current_cred();
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
492 if (retval)
David Howellsd84f4f92008-11-14 10:39:23 +1100493 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
David Howellsd84f4f92008-11-14 10:39:23 +1100495 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (rgid != (gid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100497 if (old->gid == rgid ||
498 old->egid == rgid ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 capable(CAP_SETGID))
David Howellsd84f4f92008-11-14 10:39:23 +1100500 new->gid = rgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 else
David Howellsd84f4f92008-11-14 10:39:23 +1100502 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 if (egid != (gid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100505 if (old->gid == egid ||
506 old->egid == egid ||
507 old->sgid == egid ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 capable(CAP_SETGID))
David Howellsd84f4f92008-11-14 10:39:23 +1100509 new->egid = egid;
Cal Peake756184b2006-09-30 23:27:24 -0700510 else
David Howellsd84f4f92008-11-14 10:39:23 +1100511 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
David Howellsd84f4f92008-11-14 10:39:23 +1100513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (rgid != (gid_t) -1 ||
David Howellsd84f4f92008-11-14 10:39:23 +1100515 (egid != (gid_t) -1 && egid != old->gid))
516 new->sgid = new->egid;
517 new->fsgid = new->egid;
518
519 return commit_creds(new);
520
521error:
522 abort_creds(new);
523 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
526/*
527 * setgid() is implemented like SysV w/ SAVED_IDS
528 *
529 * SMP: Same implicit races as above.
530 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100531SYSCALL_DEFINE1(setgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
David Howellsd84f4f92008-11-14 10:39:23 +1100533 const struct cred *old;
534 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 int retval;
536
David Howellsd84f4f92008-11-14 10:39:23 +1100537 new = prepare_creds();
538 if (!new)
539 return -ENOMEM;
540 old = current_cred();
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
543 if (retval)
David Howellsd84f4f92008-11-14 10:39:23 +1100544 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
David Howellsd84f4f92008-11-14 10:39:23 +1100546 retval = -EPERM;
547 if (capable(CAP_SETGID))
548 new->gid = new->egid = new->sgid = new->fsgid = gid;
549 else if (gid == old->gid || gid == old->sgid)
550 new->egid = new->fsgid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 else
David Howellsd84f4f92008-11-14 10:39:23 +1100552 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
David Howellsd84f4f92008-11-14 10:39:23 +1100554 return commit_creds(new);
555
556error:
557 abort_creds(new);
558 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
Dhaval Giani54e99122009-02-27 15:13:54 +0530560
David Howellsd84f4f92008-11-14 10:39:23 +1100561/*
562 * change the user struct in a credentials set to match the new UID
563 */
564static int set_user(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 struct user_struct *new_user;
567
Serge Hallyn18b6e042008-10-15 16:38:45 -0500568 new_user = alloc_uid(current_user_ns(), new->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (!new_user)
570 return -EAGAIN;
571
Dhaval Giani54e99122009-02-27 15:13:54 +0530572 if (!task_can_switch_user(new_user, current)) {
573 free_uid(new_user);
574 return -EINVAL;
575 }
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (atomic_read(&new_user->processes) >=
578 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
Serge Hallyn18b6e042008-10-15 16:38:45 -0500579 new_user != INIT_USER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 free_uid(new_user);
581 return -EAGAIN;
582 }
583
David Howellsd84f4f92008-11-14 10:39:23 +1100584 free_uid(new->user);
585 new->user = new_user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return 0;
587}
588
589/*
590 * Unprivileged users may change the real uid to the effective uid
591 * or vice versa. (BSD-style)
592 *
593 * If you set the real uid at all, or set the effective uid to a value not
594 * equal to the real uid, then the saved uid is set to the new effective uid.
595 *
596 * This makes it possible for a setuid program to completely drop its
597 * privileges, which is often a useful assertion to make when you are doing
598 * a security audit over a program.
599 *
600 * The general idea is that a program which uses just setreuid() will be
601 * 100% compatible with BSD. A program which uses just setuid() will be
602 * 100% compatible with POSIX with saved IDs.
603 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100604SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
David Howellsd84f4f92008-11-14 10:39:23 +1100606 const struct cred *old;
607 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 int retval;
609
David Howellsd84f4f92008-11-14 10:39:23 +1100610 new = prepare_creds();
611 if (!new)
612 return -ENOMEM;
613 old = current_cred();
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
616 if (retval)
David Howellsd84f4f92008-11-14 10:39:23 +1100617 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
David Howellsd84f4f92008-11-14 10:39:23 +1100619 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (ruid != (uid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100621 new->uid = ruid;
622 if (old->uid != ruid &&
623 old->euid != ruid &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 !capable(CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100625 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
628 if (euid != (uid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100629 new->euid = euid;
630 if (old->uid != euid &&
631 old->euid != euid &&
632 old->suid != euid &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 !capable(CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100634 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636
Dhaval Giani54e99122009-02-27 15:13:54 +0530637 if (new->uid != old->uid) {
638 retval = set_user(new);
639 if (retval < 0)
640 goto error;
641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (ruid != (uid_t) -1 ||
David Howellsd84f4f92008-11-14 10:39:23 +1100643 (euid != (uid_t) -1 && euid != old->uid))
644 new->suid = new->euid;
645 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
David Howellsd84f4f92008-11-14 10:39:23 +1100647 retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
648 if (retval < 0)
649 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
David Howellsd84f4f92008-11-14 10:39:23 +1100651 return commit_creds(new);
652
653error:
654 abort_creds(new);
655 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658/*
659 * setuid() is implemented like SysV with SAVED_IDS
660 *
661 * Note that SAVED_ID's is deficient in that a setuid root program
662 * like sendmail, for example, cannot set its uid to be a normal
663 * user and then switch back, because if you're root, setuid() sets
664 * the saved uid too. If you don't like this, blame the bright people
665 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
666 * will allow a root program to temporarily drop privileges and be able to
667 * regain them by swapping the real and effective uid.
668 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100669SYSCALL_DEFINE1(setuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
David Howellsd84f4f92008-11-14 10:39:23 +1100671 const struct cred *old;
672 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 int retval;
674
David Howellsd84f4f92008-11-14 10:39:23 +1100675 new = prepare_creds();
676 if (!new)
677 return -ENOMEM;
678 old = current_cred();
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
681 if (retval)
David Howellsd84f4f92008-11-14 10:39:23 +1100682 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
David Howellsd84f4f92008-11-14 10:39:23 +1100684 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (capable(CAP_SETUID)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100686 new->suid = new->uid = uid;
Dhaval Giani54e99122009-02-27 15:13:54 +0530687 if (uid != old->uid) {
688 retval = set_user(new);
689 if (retval < 0)
690 goto error;
David Howellsd84f4f92008-11-14 10:39:23 +1100691 }
692 } else if (uid != old->uid && uid != new->suid) {
693 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
David Howellsd84f4f92008-11-14 10:39:23 +1100696 new->fsuid = new->euid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
David Howellsd84f4f92008-11-14 10:39:23 +1100698 retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
699 if (retval < 0)
700 goto error;
701
702 return commit_creds(new);
703
704error:
705 abort_creds(new);
706 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
709
710/*
711 * This function implements a generic ability to update ruid, euid,
712 * and suid. This allows you to implement the 4.4 compatible seteuid().
713 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100714SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
David Howellsd84f4f92008-11-14 10:39:23 +1100716 const struct cred *old;
717 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 int retval;
719
David Howellsd84f4f92008-11-14 10:39:23 +1100720 new = prepare_creds();
721 if (!new)
722 return -ENOMEM;
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
725 if (retval)
David Howellsd84f4f92008-11-14 10:39:23 +1100726 goto error;
727 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
David Howellsd84f4f92008-11-14 10:39:23 +1100729 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (!capable(CAP_SETUID)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100731 if (ruid != (uid_t) -1 && ruid != old->uid &&
732 ruid != old->euid && ruid != old->suid)
733 goto error;
734 if (euid != (uid_t) -1 && euid != old->uid &&
735 euid != old->euid && euid != old->suid)
736 goto error;
737 if (suid != (uid_t) -1 && suid != old->uid &&
738 suid != old->euid && suid != old->suid)
739 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
David Howellsd84f4f92008-11-14 10:39:23 +1100741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (ruid != (uid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100743 new->uid = ruid;
Dhaval Giani54e99122009-02-27 15:13:54 +0530744 if (ruid != old->uid) {
745 retval = set_user(new);
746 if (retval < 0)
747 goto error;
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
David Howellsd84f4f92008-11-14 10:39:23 +1100750 if (euid != (uid_t) -1)
751 new->euid = euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (suid != (uid_t) -1)
David Howellsd84f4f92008-11-14 10:39:23 +1100753 new->suid = suid;
754 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
David Howellsd84f4f92008-11-14 10:39:23 +1100756 retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
757 if (retval < 0)
758 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
David Howellsd84f4f92008-11-14 10:39:23 +1100760 return commit_creds(new);
761
762error:
763 abort_creds(new);
764 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Heiko Carstensdbf040d2009-01-14 14:14:04 +0100767SYSCALL_DEFINE3(getresuid, uid_t __user *, ruid, uid_t __user *, euid, uid_t __user *, suid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
David Howells86a264a2008-11-14 10:39:18 +1100769 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 int retval;
771
David Howells86a264a2008-11-14 10:39:18 +1100772 if (!(retval = put_user(cred->uid, ruid)) &&
773 !(retval = put_user(cred->euid, euid)))
David Howellsb6dff3e2008-11-14 10:39:16 +1100774 retval = put_user(cred->suid, suid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 return retval;
777}
778
779/*
780 * Same as above, but for rgid, egid, sgid.
781 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100782SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
David Howellsd84f4f92008-11-14 10:39:23 +1100784 const struct cred *old;
785 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 int retval;
787
David Howellsd84f4f92008-11-14 10:39:23 +1100788 new = prepare_creds();
789 if (!new)
790 return -ENOMEM;
791 old = current_cred();
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
794 if (retval)
David Howellsd84f4f92008-11-14 10:39:23 +1100795 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
David Howellsd84f4f92008-11-14 10:39:23 +1100797 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (!capable(CAP_SETGID)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100799 if (rgid != (gid_t) -1 && rgid != old->gid &&
800 rgid != old->egid && rgid != old->sgid)
801 goto error;
802 if (egid != (gid_t) -1 && egid != old->gid &&
803 egid != old->egid && egid != old->sgid)
804 goto error;
805 if (sgid != (gid_t) -1 && sgid != old->gid &&
806 sgid != old->egid && sgid != old->sgid)
807 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
David Howellsd84f4f92008-11-14 10:39:23 +1100810 if (rgid != (gid_t) -1)
811 new->gid = rgid;
812 if (egid != (gid_t) -1)
813 new->egid = egid;
814 if (sgid != (gid_t) -1)
815 new->sgid = sgid;
816 new->fsgid = new->egid;
817
818 return commit_creds(new);
819
820error:
821 abort_creds(new);
822 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
Heiko Carstensdbf040d2009-01-14 14:14:04 +0100825SYSCALL_DEFINE3(getresgid, gid_t __user *, rgid, gid_t __user *, egid, gid_t __user *, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
David Howells86a264a2008-11-14 10:39:18 +1100827 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 int retval;
829
David Howells86a264a2008-11-14 10:39:18 +1100830 if (!(retval = put_user(cred->gid, rgid)) &&
831 !(retval = put_user(cred->egid, egid)))
David Howellsb6dff3e2008-11-14 10:39:16 +1100832 retval = put_user(cred->sgid, sgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 return retval;
835}
836
837
838/*
839 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
840 * is used for "access()" and for the NFS daemon (letting nfsd stay at
841 * whatever uid it wants to). It normally shadows "euid", except when
842 * explicitly set by setfsuid() or for access..
843 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100844SYSCALL_DEFINE1(setfsuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
David Howellsd84f4f92008-11-14 10:39:23 +1100846 const struct cred *old;
847 struct cred *new;
848 uid_t old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
David Howellsd84f4f92008-11-14 10:39:23 +1100850 new = prepare_creds();
851 if (!new)
852 return current_fsuid();
853 old = current_cred();
854 old_fsuid = old->fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
David Howellsd84f4f92008-11-14 10:39:23 +1100856 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS) < 0)
857 goto error;
858
859 if (uid == old->uid || uid == old->euid ||
860 uid == old->suid || uid == old->fsuid ||
Cal Peake756184b2006-09-30 23:27:24 -0700861 capable(CAP_SETUID)) {
862 if (uid != old_fsuid) {
David Howellsd84f4f92008-11-14 10:39:23 +1100863 new->fsuid = uid;
864 if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
865 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868
David Howellsd84f4f92008-11-14 10:39:23 +1100869error:
870 abort_creds(new);
871 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
David Howellsd84f4f92008-11-14 10:39:23 +1100873change_okay:
874 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return old_fsuid;
876}
877
878/*
John Anthony Kazos Jrf42df9e2007-05-09 08:23:08 +0200879 * Samma på svenska..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100881SYSCALL_DEFINE1(setfsgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
David Howellsd84f4f92008-11-14 10:39:23 +1100883 const struct cred *old;
884 struct cred *new;
885 gid_t old_fsgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
David Howellsd84f4f92008-11-14 10:39:23 +1100887 new = prepare_creds();
888 if (!new)
889 return current_fsgid();
890 old = current_cred();
891 old_fsgid = old->fsgid;
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
David Howellsd84f4f92008-11-14 10:39:23 +1100894 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
David Howellsd84f4f92008-11-14 10:39:23 +1100896 if (gid == old->gid || gid == old->egid ||
897 gid == old->sgid || gid == old->fsgid ||
Cal Peake756184b2006-09-30 23:27:24 -0700898 capable(CAP_SETGID)) {
899 if (gid != old_fsgid) {
David Howellsd84f4f92008-11-14 10:39:23 +1100900 new->fsgid = gid;
901 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
David Howellsd84f4f92008-11-14 10:39:23 +1100904
905error:
906 abort_creds(new);
907 return old_fsgid;
908
909change_okay:
910 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return old_fsgid;
912}
913
Frank Mayharf06febc2008-09-12 09:54:39 -0700914void do_sys_times(struct tms *tms)
915{
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +0900916 cputime_t tgutime, tgstime, cutime, cstime;
Frank Mayharf06febc2008-09-12 09:54:39 -0700917
Oleg Nesterov2b5fe6d2008-11-17 15:40:08 +0100918 spin_lock_irq(&current->sighand->siglock);
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +0900919 thread_group_times(current, &tgutime, &tgstime);
Frank Mayharf06febc2008-09-12 09:54:39 -0700920 cutime = current->signal->cutime;
921 cstime = current->signal->cstime;
922 spin_unlock_irq(&current->sighand->siglock);
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +0900923 tms->tms_utime = cputime_to_clock_t(tgutime);
924 tms->tms_stime = cputime_to_clock_t(tgstime);
Frank Mayharf06febc2008-09-12 09:54:39 -0700925 tms->tms_cutime = cputime_to_clock_t(cutime);
926 tms->tms_cstime = cputime_to_clock_t(cstime);
927}
928
Heiko Carstens58fd3aa2009-01-14 14:14:03 +0100929SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (tbuf) {
932 struct tms tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Frank Mayharf06febc2008-09-12 09:54:39 -0700934 do_sys_times(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
936 return -EFAULT;
937 }
Paul Mackerrase3d5a272009-01-06 14:41:02 -0800938 force_successful_syscall_return();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return (long) jiffies_64_to_clock_t(get_jiffies_64());
940}
941
942/*
943 * This needs some heavy checking ...
944 * I just haven't the stomach for it. I also don't fully
945 * understand sessions/pgrp etc. Let somebody who does explain it.
946 *
947 * OK, I think I have the protection semantics right.... this is really
948 * only important on a multi-user system anyway, to make sure one user
949 * can't send a signal to a process owned by another. -TYT, 12/12/91
950 *
951 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
952 * LBT 04.03.94
953 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +0100954SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 struct task_struct *p;
Oleg Nesterovee0acf92006-01-08 01:03:53 -0800957 struct task_struct *group_leader = current->group_leader;
Oleg Nesterov4e021302008-02-08 04:19:08 -0800958 struct pid *pgrp;
959 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 if (!pid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700962 pid = task_pid_vnr(group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (!pgid)
964 pgid = pid;
965 if (pgid < 0)
966 return -EINVAL;
967
968 /* From this point forward we keep holding onto the tasklist lock
969 * so that our parent does not change from under us. -DaveM
970 */
971 write_lock_irq(&tasklist_lock);
972
973 err = -ESRCH;
Oleg Nesterov4e021302008-02-08 04:19:08 -0800974 p = find_task_by_vpid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (!p)
976 goto out;
977
978 err = -EINVAL;
979 if (!thread_group_leader(p))
980 goto out;
981
Oleg Nesterov4e021302008-02-08 04:19:08 -0800982 if (same_thread_group(p->real_parent, group_leader)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 err = -EPERM;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800984 if (task_session(p) != task_session(group_leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 goto out;
986 err = -EACCES;
987 if (p->did_exec)
988 goto out;
989 } else {
990 err = -ESRCH;
Oleg Nesterovee0acf92006-01-08 01:03:53 -0800991 if (p != group_leader)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 goto out;
993 }
994
995 err = -EPERM;
996 if (p->signal->leader)
997 goto out;
998
Oleg Nesterov4e021302008-02-08 04:19:08 -0800999 pgrp = task_pid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (pgid != pid) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001001 struct task_struct *g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Oleg Nesterov4e021302008-02-08 04:19:08 -08001003 pgrp = find_vpid(pgid);
1004 g = pid_task(pgrp, PIDTYPE_PGID);
Eric W. Biederman41487c62007-02-12 00:53:01 -08001005 if (!g || task_session(g) != task_session(group_leader))
Oleg Nesterovf020bc42006-12-08 02:38:02 -08001006 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 err = security_task_setpgid(p, pgid);
1010 if (err)
1011 goto out;
1012
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07001013 if (task_pgrp(p) != pgrp)
Oleg Nesterov83beaf32008-04-30 00:54:27 -07001014 change_pid(p, PIDTYPE_PGID, pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 err = 0;
1017out:
1018 /* All paths lead to here, thus we are safe. -DaveM */
1019 write_unlock_irq(&tasklist_lock);
1020 return err;
1021}
1022
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001023SYSCALL_DEFINE1(getpgid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001025 struct task_struct *p;
1026 struct pid *grp;
1027 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001029 rcu_read_lock();
1030 if (!pid)
1031 grp = task_pgrp(current);
1032 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 retval = -ESRCH;
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001034 p = find_task_by_vpid(pid);
1035 if (!p)
1036 goto out;
1037 grp = task_pgrp(p);
1038 if (!grp)
1039 goto out;
1040
1041 retval = security_task_getpgid(p);
1042 if (retval)
1043 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001045 retval = pid_vnr(grp);
1046out:
1047 rcu_read_unlock();
1048 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051#ifdef __ARCH_WANT_SYS_GETPGRP
1052
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001053SYSCALL_DEFINE0(getpgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001055 return sys_getpgid(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
1058#endif
1059
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001060SYSCALL_DEFINE1(getsid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001062 struct task_struct *p;
1063 struct pid *sid;
1064 int retval;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001065
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001066 rcu_read_lock();
1067 if (!pid)
1068 sid = task_session(current);
1069 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 retval = -ESRCH;
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001071 p = find_task_by_vpid(pid);
1072 if (!p)
1073 goto out;
1074 sid = task_session(p);
1075 if (!sid)
1076 goto out;
1077
1078 retval = security_task_getsid(p);
1079 if (retval)
1080 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001082 retval = pid_vnr(sid);
1083out:
1084 rcu_read_unlock();
1085 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
Heiko Carstensb290ebe2009-01-14 14:14:06 +01001088SYSCALL_DEFINE0(setsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Oren Laadane19f2472006-01-08 01:03:58 -08001090 struct task_struct *group_leader = current->group_leader;
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001091 struct pid *sid = task_pid(group_leader);
1092 pid_t session = pid_vnr(sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 int err = -EPERM;
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 write_lock_irq(&tasklist_lock);
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001096 /* Fail if I am already a session leader */
1097 if (group_leader->signal->leader)
1098 goto out;
1099
Oleg Nesterov430c6232008-02-08 04:19:11 -08001100 /* Fail if a process group id already exists that equals the
1101 * proposed session id.
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001102 */
Oleg Nesterov6806aac2008-02-08 04:19:12 -08001103 if (pid_task(sid, PIDTYPE_PGID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 goto out;
1105
Oren Laadane19f2472006-01-08 01:03:58 -08001106 group_leader->signal->leader = 1;
Oleg Nesterov8520d7c2008-02-08 04:19:09 -08001107 __set_special_pids(sid);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001108
Alan Cox9c9f4de2008-10-13 10:37:26 +01001109 proc_clear_tty(group_leader);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001110
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001111 err = session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112out:
1113 write_unlock_irq(&tasklist_lock);
Christian Borntraeger0d0df592009-10-26 16:49:34 -07001114 if (err > 0)
1115 proc_sid_connector(group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return err;
1117}
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119DECLARE_RWSEM(uts_sem);
1120
Heiko Carstense48fbb62009-01-14 14:14:26 +01001121SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 int errno = 0;
1124
1125 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001126 if (copy_to_user(name, utsname(), sizeof *name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 errno = -EFAULT;
1128 up_read(&uts_sem);
1129 return errno;
1130}
1131
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001132SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
1134 int errno;
1135 char tmp[__NEW_UTS_LEN];
1136
1137 if (!capable(CAP_SYS_ADMIN))
1138 return -EPERM;
1139 if (len < 0 || len > __NEW_UTS_LEN)
1140 return -EINVAL;
1141 down_write(&uts_sem);
1142 errno = -EFAULT;
1143 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001144 struct new_utsname *u = utsname();
1145
1146 memcpy(u->nodename, tmp, len);
1147 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 errno = 0;
1149 }
1150 up_write(&uts_sem);
1151 return errno;
1152}
1153
1154#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1155
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001156SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 int i, errno;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001159 struct new_utsname *u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 if (len < 0)
1162 return -EINVAL;
1163 down_read(&uts_sem);
Andrew Morton9679e4d2008-10-15 22:01:51 -07001164 u = utsname();
1165 i = 1 + strlen(u->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (i > len)
1167 i = len;
1168 errno = 0;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001169 if (copy_to_user(name, u->nodename, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 errno = -EFAULT;
1171 up_read(&uts_sem);
1172 return errno;
1173}
1174
1175#endif
1176
1177/*
1178 * Only setdomainname; getdomainname can be implemented by calling
1179 * uname()
1180 */
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001181SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
1183 int errno;
1184 char tmp[__NEW_UTS_LEN];
1185
1186 if (!capable(CAP_SYS_ADMIN))
1187 return -EPERM;
1188 if (len < 0 || len > __NEW_UTS_LEN)
1189 return -EINVAL;
1190
1191 down_write(&uts_sem);
1192 errno = -EFAULT;
1193 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001194 struct new_utsname *u = utsname();
1195
1196 memcpy(u->domainname, tmp, len);
1197 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 errno = 0;
1199 }
1200 up_write(&uts_sem);
1201 return errno;
1202}
1203
Heiko Carstense48fbb62009-01-14 14:14:26 +01001204SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
1206 if (resource >= RLIM_NLIMITS)
1207 return -EINVAL;
1208 else {
1209 struct rlimit value;
1210 task_lock(current->group_leader);
1211 value = current->signal->rlim[resource];
1212 task_unlock(current->group_leader);
1213 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1214 }
1215}
1216
1217#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1218
1219/*
1220 * Back compatibility for getrlimit. Needed for some apps.
1221 */
1222
Heiko Carstense48fbb62009-01-14 14:14:26 +01001223SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1224 struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 struct rlimit x;
1227 if (resource >= RLIM_NLIMITS)
1228 return -EINVAL;
1229
1230 task_lock(current->group_leader);
1231 x = current->signal->rlim[resource];
1232 task_unlock(current->group_leader);
Cal Peake756184b2006-09-30 23:27:24 -07001233 if (x.rlim_cur > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 x.rlim_cur = 0x7FFFFFFF;
Cal Peake756184b2006-09-30 23:27:24 -07001235 if (x.rlim_max > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 x.rlim_max = 0x7FFFFFFF;
1237 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1238}
1239
1240#endif
1241
Heiko Carstense48fbb62009-01-14 14:14:26 +01001242SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 struct rlimit new_rlim, *old_rlim;
1245 int retval;
1246
1247 if (resource >= RLIM_NLIMITS)
1248 return -EINVAL;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001249 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return -EFAULT;
Andrew Morton60fd7602009-02-04 15:12:06 -08001251 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1252 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 old_rlim = current->signal->rlim + resource;
1254 if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1255 !capable(CAP_SYS_RESOURCE))
1256 return -EPERM;
Andrew Morton60fd7602009-02-04 15:12:06 -08001257 if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > sysctl_nr_open)
1258 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 retval = security_task_setrlimit(resource, &new_rlim);
1261 if (retval)
1262 return retval;
1263
Tom Alsberg9926e4c2007-05-08 00:30:31 -07001264 if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
1265 /*
1266 * The caller is asking for an immediate RLIMIT_CPU
1267 * expiry. But we use the zero value to mean "it was
1268 * never set". So let's cheat and make it one second
1269 * instead
1270 */
1271 new_rlim.rlim_cur = 1;
1272 }
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 task_lock(current->group_leader);
1275 *old_rlim = new_rlim;
1276 task_unlock(current->group_leader);
1277
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001278 if (resource != RLIMIT_CPU)
1279 goto out;
Andrew Mortond3561f72006-03-24 03:18:36 -08001280
1281 /*
1282 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1283 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1284 * very long-standing error, and fixing it now risks breakage of
1285 * applications, so we live with it
1286 */
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001287 if (new_rlim.rlim_cur == RLIM_INFINITY)
1288 goto out;
1289
Frank Mayharf06febc2008-09-12 09:54:39 -07001290 update_rlimit_cpu(new_rlim.rlim_cur);
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001291out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return 0;
1293}
1294
1295/*
1296 * It would make sense to put struct rusage in the task_struct,
1297 * except that would make the task_struct be *really big*. After
1298 * task_struct gets moved into malloc'ed memory, it would
1299 * make sense to do this. It will make moving the rest of the information
1300 * a lot simpler! (Which we're not doing right now because we're not
1301 * measuring them yet).
1302 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1304 * races with threads incrementing their own counters. But since word
1305 * reads are atomic, we either get new values or old values and we don't
1306 * care which for the sums. We always take the siglock to protect reading
1307 * the c* fields from p->signal from races with exit.c updating those
1308 * fields when reaping, so a sample either gets all the additions of a
1309 * given child after it's reaped, or none so this sample is before reaping.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001310 *
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001311 * Locking:
1312 * We need to take the siglock for CHILDEREN, SELF and BOTH
1313 * for the cases current multithreaded, non-current single threaded
1314 * non-current multithreaded. Thread traversal is now safe with
1315 * the siglock held.
1316 * Strictly speaking, we donot need to take the siglock if we are current and
1317 * single threaded, as no one else can take our signal_struct away, no one
1318 * else can reap the children to update signal->c* counters, and no one else
1319 * can race with the signal-> fields. If we do not take any lock, the
1320 * signal-> fields could be read out of order while another thread was just
1321 * exiting. So we should place a read memory barrier when we avoid the lock.
1322 * On the writer side, write memory barrier is implied in __exit_signal
1323 * as __exit_signal releases the siglock spinlock after updating the signal->
1324 * fields. But we don't do this yet to keep things simple.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001325 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 */
1327
Frank Mayharf06febc2008-09-12 09:54:39 -07001328static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001329{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001330 r->ru_nvcsw += t->nvcsw;
1331 r->ru_nivcsw += t->nivcsw;
1332 r->ru_minflt += t->min_flt;
1333 r->ru_majflt += t->maj_flt;
1334 r->ru_inblock += task_io_get_inblock(t);
1335 r->ru_oublock += task_io_get_oublock(t);
1336}
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1339{
1340 struct task_struct *t;
1341 unsigned long flags;
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09001342 cputime_t tgutime, tgstime, utime, stime;
Jiri Pirko1f102062009-09-22 16:44:10 -07001343 unsigned long maxrss = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 memset((char *) r, 0, sizeof *r);
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001346 utime = stime = cputime_zero;
1347
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001348 if (who == RUSAGE_THREAD) {
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09001349 task_times(current, &utime, &stime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001350 accumulate_thread_rusage(p, r);
Jiri Pirko1f102062009-09-22 16:44:10 -07001351 maxrss = p->signal->maxrss;
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001352 goto out;
1353 }
1354
Oleg Nesterovd6cf7232008-04-30 00:52:38 -07001355 if (!lock_task_sighand(p, &flags))
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001356 return;
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 switch (who) {
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001359 case RUSAGE_BOTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 case RUSAGE_CHILDREN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 utime = p->signal->cutime;
1362 stime = p->signal->cstime;
1363 r->ru_nvcsw = p->signal->cnvcsw;
1364 r->ru_nivcsw = p->signal->cnivcsw;
1365 r->ru_minflt = p->signal->cmin_flt;
1366 r->ru_majflt = p->signal->cmaj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07001367 r->ru_inblock = p->signal->cinblock;
1368 r->ru_oublock = p->signal->coublock;
Jiri Pirko1f102062009-09-22 16:44:10 -07001369 maxrss = p->signal->cmaxrss;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001370
1371 if (who == RUSAGE_CHILDREN)
1372 break;
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 case RUSAGE_SELF:
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09001375 thread_group_times(p, &tgutime, &tgstime);
1376 utime = cputime_add(utime, tgutime);
1377 stime = cputime_add(stime, tgstime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 r->ru_nvcsw += p->signal->nvcsw;
1379 r->ru_nivcsw += p->signal->nivcsw;
1380 r->ru_minflt += p->signal->min_flt;
1381 r->ru_majflt += p->signal->maj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07001382 r->ru_inblock += p->signal->inblock;
1383 r->ru_oublock += p->signal->oublock;
Jiri Pirko1f102062009-09-22 16:44:10 -07001384 if (maxrss < p->signal->maxrss)
1385 maxrss = p->signal->maxrss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 t = p;
1387 do {
Frank Mayharf06febc2008-09-12 09:54:39 -07001388 accumulate_thread_rusage(t, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 t = next_thread(t);
1390 } while (t != p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 break;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 default:
1394 BUG();
1395 }
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001396 unlock_task_sighand(p, &flags);
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001397
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001398out:
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001399 cputime_to_timeval(utime, &r->ru_utime);
1400 cputime_to_timeval(stime, &r->ru_stime);
Jiri Pirko1f102062009-09-22 16:44:10 -07001401
1402 if (who != RUSAGE_CHILDREN) {
1403 struct mm_struct *mm = get_task_mm(p);
1404 if (mm) {
1405 setmax_mm_hiwater_rss(&maxrss, mm);
1406 mmput(mm);
1407 }
1408 }
1409 r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
1412int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1413{
1414 struct rusage r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 k_getrusage(p, who, &r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1417}
1418
Heiko Carstense48fbb62009-01-14 14:14:26 +01001419SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001421 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1422 who != RUSAGE_THREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return -EINVAL;
1424 return getrusage(current, who, ru);
1425}
1426
Heiko Carstense48fbb62009-01-14 14:14:26 +01001427SYSCALL_DEFINE1(umask, int, mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
1429 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1430 return mask;
1431}
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -08001432
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001433SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
1434 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
David Howellsb6dff3e2008-11-14 10:39:16 +11001436 struct task_struct *me = current;
1437 unsigned char comm[sizeof(me->comm)];
1438 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
David Howellsd84f4f92008-11-14 10:39:23 +11001440 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1441 if (error != -ENOSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return error;
1443
David Howellsd84f4f92008-11-14 10:39:23 +11001444 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 switch (option) {
1446 case PR_SET_PDEATHSIG:
Jesper Juhl0730ded2005-09-06 15:17:37 -07001447 if (!valid_signal(arg2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 error = -EINVAL;
1449 break;
1450 }
David Howellsb6dff3e2008-11-14 10:39:16 +11001451 me->pdeath_signal = arg2;
1452 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 break;
1454 case PR_GET_PDEATHSIG:
David Howellsb6dff3e2008-11-14 10:39:16 +11001455 error = put_user(me->pdeath_signal, (int __user *)arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 break;
1457 case PR_GET_DUMPABLE:
David Howellsb6dff3e2008-11-14 10:39:16 +11001458 error = get_dumpable(me->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 break;
1460 case PR_SET_DUMPABLE:
Marcel Holtmannabf75a52006-07-12 13:12:00 +02001461 if (arg2 < 0 || arg2 > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 error = -EINVAL;
1463 break;
1464 }
David Howellsb6dff3e2008-11-14 10:39:16 +11001465 set_dumpable(me->mm, arg2);
1466 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 break;
1468
1469 case PR_SET_UNALIGN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001470 error = SET_UNALIGN_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 break;
1472 case PR_GET_UNALIGN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001473 error = GET_UNALIGN_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 break;
1475 case PR_SET_FPEMU:
David Howellsb6dff3e2008-11-14 10:39:16 +11001476 error = SET_FPEMU_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 break;
1478 case PR_GET_FPEMU:
David Howellsb6dff3e2008-11-14 10:39:16 +11001479 error = GET_FPEMU_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 break;
1481 case PR_SET_FPEXC:
David Howellsb6dff3e2008-11-14 10:39:16 +11001482 error = SET_FPEXC_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 break;
1484 case PR_GET_FPEXC:
David Howellsb6dff3e2008-11-14 10:39:16 +11001485 error = GET_FPEXC_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 break;
1487 case PR_GET_TIMING:
1488 error = PR_TIMING_STATISTICAL;
1489 break;
1490 case PR_SET_TIMING:
Shi Weihua7b266552008-05-23 13:04:59 -07001491 if (arg2 != PR_TIMING_STATISTICAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 error = -EINVAL;
David Howellsb6dff3e2008-11-14 10:39:16 +11001493 else
1494 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 break;
1496
David Howellsb6dff3e2008-11-14 10:39:16 +11001497 case PR_SET_NAME:
1498 comm[sizeof(me->comm)-1] = 0;
1499 if (strncpy_from_user(comm, (char __user *)arg2,
1500 sizeof(me->comm) - 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return -EFAULT;
David Howellsb6dff3e2008-11-14 10:39:16 +11001502 set_task_comm(me, comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return 0;
David Howellsb6dff3e2008-11-14 10:39:16 +11001504 case PR_GET_NAME:
1505 get_task_comm(comm, me);
1506 if (copy_to_user((char __user *)arg2, comm,
1507 sizeof(comm)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return -EFAULT;
1509 return 0;
Anton Blanchard651d7652006-06-07 16:10:19 +10001510 case PR_GET_ENDIAN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001511 error = GET_ENDIAN(me, arg2);
Anton Blanchard651d7652006-06-07 16:10:19 +10001512 break;
1513 case PR_SET_ENDIAN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001514 error = SET_ENDIAN(me, arg2);
Anton Blanchard651d7652006-06-07 16:10:19 +10001515 break;
1516
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -07001517 case PR_GET_SECCOMP:
1518 error = prctl_get_seccomp();
1519 break;
1520 case PR_SET_SECCOMP:
1521 error = prctl_set_seccomp(arg2);
1522 break;
Erik Bosman8fb402b2008-04-11 18:54:17 +02001523 case PR_GET_TSC:
1524 error = GET_TSC_CTL(arg2);
1525 break;
1526 case PR_SET_TSC:
1527 error = SET_TSC_CTL(arg2);
1528 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001529 case PR_TASK_PERF_EVENTS_DISABLE:
1530 error = perf_event_task_disable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001531 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001532 case PR_TASK_PERF_EVENTS_ENABLE:
1533 error = perf_event_task_enable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001534 break;
Arjan van de Ven69766752008-09-01 15:52:40 -07001535 case PR_GET_TIMERSLACK:
1536 error = current->timer_slack_ns;
1537 break;
1538 case PR_SET_TIMERSLACK:
1539 if (arg2 <= 0)
1540 current->timer_slack_ns =
1541 current->default_timer_slack_ns;
1542 else
1543 current->timer_slack_ns = arg2;
David Howellsb6dff3e2008-11-14 10:39:16 +11001544 error = 0;
Arjan van de Ven69766752008-09-01 15:52:40 -07001545 break;
Andi Kleen4db96cf2009-09-16 11:50:14 +02001546 case PR_MCE_KILL:
1547 if (arg4 | arg5)
1548 return -EINVAL;
1549 switch (arg2) {
Andi Kleen1087e9b2009-10-04 02:20:11 +02001550 case PR_MCE_KILL_CLEAR:
Andi Kleen4db96cf2009-09-16 11:50:14 +02001551 if (arg3 != 0)
1552 return -EINVAL;
1553 current->flags &= ~PF_MCE_PROCESS;
1554 break;
Andi Kleen1087e9b2009-10-04 02:20:11 +02001555 case PR_MCE_KILL_SET:
Andi Kleen4db96cf2009-09-16 11:50:14 +02001556 current->flags |= PF_MCE_PROCESS;
Andi Kleen1087e9b2009-10-04 02:20:11 +02001557 if (arg3 == PR_MCE_KILL_EARLY)
Andi Kleen4db96cf2009-09-16 11:50:14 +02001558 current->flags |= PF_MCE_EARLY;
Andi Kleen1087e9b2009-10-04 02:20:11 +02001559 else if (arg3 == PR_MCE_KILL_LATE)
Andi Kleen4db96cf2009-09-16 11:50:14 +02001560 current->flags &= ~PF_MCE_EARLY;
Andi Kleen1087e9b2009-10-04 02:20:11 +02001561 else if (arg3 == PR_MCE_KILL_DEFAULT)
1562 current->flags &=
1563 ~(PF_MCE_EARLY|PF_MCE_PROCESS);
1564 else
1565 return -EINVAL;
Andi Kleen4db96cf2009-09-16 11:50:14 +02001566 break;
1567 default:
1568 return -EINVAL;
1569 }
1570 error = 0;
1571 break;
Andi Kleen1087e9b2009-10-04 02:20:11 +02001572 case PR_MCE_KILL_GET:
1573 if (arg2 | arg3 | arg4 | arg5)
1574 return -EINVAL;
1575 if (current->flags & PF_MCE_PROCESS)
1576 error = (current->flags & PF_MCE_EARLY) ?
1577 PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
1578 else
1579 error = PR_MCE_KILL_DEFAULT;
1580 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 default:
1582 error = -EINVAL;
1583 break;
1584 }
1585 return error;
1586}
Andi Kleen3cfc3482006-09-26 10:52:28 +02001587
Heiko Carstens836f92a2009-01-14 14:14:33 +01001588SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
1589 struct getcpu_cache __user *, unused)
Andi Kleen3cfc3482006-09-26 10:52:28 +02001590{
1591 int err = 0;
1592 int cpu = raw_smp_processor_id();
1593 if (cpup)
1594 err |= put_user(cpu, cpup);
1595 if (nodep)
1596 err |= put_user(cpu_to_node(cpu), nodep);
Andi Kleen3cfc3482006-09-26 10:52:28 +02001597 return err ? -EFAULT : 0;
1598}
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07001599
1600char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
1601
1602static void argv_cleanup(char **argv, char **envp)
1603{
1604 argv_free(argv);
1605}
1606
1607/**
1608 * orderly_poweroff - Trigger an orderly system poweroff
1609 * @force: force poweroff if command execution fails
1610 *
1611 * This may be called from any context to trigger a system shutdown.
1612 * If the orderly shutdown fails, it will force an immediate shutdown.
1613 */
1614int orderly_poweroff(bool force)
1615{
1616 int argc;
1617 char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
1618 static char *envp[] = {
1619 "HOME=/",
1620 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
1621 NULL
1622 };
1623 int ret = -ENOMEM;
1624 struct subprocess_info *info;
1625
1626 if (argv == NULL) {
1627 printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
1628 __func__, poweroff_cmd);
1629 goto out;
1630 }
1631
KOSAKI Motohiroac331d12008-07-25 01:45:38 -07001632 info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07001633 if (info == NULL) {
1634 argv_free(argv);
1635 goto out;
1636 }
1637
1638 call_usermodehelper_setcleanup(info, argv_cleanup);
1639
Jeremy Fitzhardinge86313c42007-07-17 18:37:03 -07001640 ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07001641
1642 out:
1643 if (ret && force) {
1644 printk(KERN_WARNING "Failed to start orderly shutdown: "
1645 "forcing the issue\n");
1646
1647 /* I guess this should try to kick off some daemon to
1648 sync and poweroff asap. Or not even bother syncing
1649 if we're doing an emergency shutdown? */
1650 emergency_sync();
1651 kernel_power_off();
1652 }
1653
1654 return ret;
1655}
1656EXPORT_SYMBOL_GPL(orderly_poweroff);