blob: ccc9eb736d35273f96422b07577fd43f5172c714 [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>
Daniel Walker3e88c552007-05-10 22:22:53 -070017#include <linux/resource.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070018#include <linux/kernel.h>
19#include <linux/kexec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/workqueue.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080021#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/device.h>
23#include <linux/key.h>
24#include <linux/times.h>
25#include <linux/posix-timers.h>
26#include <linux/security.h>
27#include <linux/dcookies.h>
28#include <linux/suspend.h>
29#include <linux/tty.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070030#include <linux/signal.h>
Matt Helsley9f460802005-11-07 00:59:16 -080031#include <linux/cn_proc.h>
Andi Kleen3cfc3482006-09-26 10:52:28 +020032#include <linux/getcpu.h>
Eric Dumazet6eaeeab2007-05-10 22:22:37 -070033#include <linux/task_io_accounting_ops.h>
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -070034#include <linux/seccomp.h>
Mark Lord40477272007-10-01 01:20:10 -070035#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <linux/compat.h>
38#include <linux/syscalls.h>
Keshavamurthy Anil S00d7c052005-12-12 00:37:33 -080039#include <linux/kprobes.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070040#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <asm/uaccess.h>
43#include <asm/io.h>
44#include <asm/unistd.h>
45
46#ifndef SET_UNALIGN_CTL
47# define SET_UNALIGN_CTL(a,b) (-EINVAL)
48#endif
49#ifndef GET_UNALIGN_CTL
50# define GET_UNALIGN_CTL(a,b) (-EINVAL)
51#endif
52#ifndef SET_FPEMU_CTL
53# define SET_FPEMU_CTL(a,b) (-EINVAL)
54#endif
55#ifndef GET_FPEMU_CTL
56# define GET_FPEMU_CTL(a,b) (-EINVAL)
57#endif
58#ifndef SET_FPEXC_CTL
59# define SET_FPEXC_CTL(a,b) (-EINVAL)
60#endif
61#ifndef GET_FPEXC_CTL
62# define GET_FPEXC_CTL(a,b) (-EINVAL)
63#endif
Anton Blanchard651d7652006-06-07 16:10:19 +100064#ifndef GET_ENDIAN
65# define GET_ENDIAN(a,b) (-EINVAL)
66#endif
67#ifndef SET_ENDIAN
68# define SET_ENDIAN(a,b) (-EINVAL)
69#endif
Erik Bosman8fb402b2008-04-11 18:54:17 +020070#ifndef GET_TSC_CTL
71# define GET_TSC_CTL(a) (-EINVAL)
72#endif
73#ifndef SET_TSC_CTL
74# define SET_TSC_CTL(a) (-EINVAL)
75#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*
78 * this is where the system-wide overflow UID and GID are defined, for
79 * architectures that now have 32-bit UID/GID but didn't in the past
80 */
81
82int overflowuid = DEFAULT_OVERFLOWUID;
83int overflowgid = DEFAULT_OVERFLOWGID;
84
85#ifdef CONFIG_UID16
86EXPORT_SYMBOL(overflowuid);
87EXPORT_SYMBOL(overflowgid);
88#endif
89
90/*
91 * the same as above, but for filesystems which can only store a 16-bit
92 * UID and GID. as such, this is needed on all architectures
93 */
94
95int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
96int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
97
98EXPORT_SYMBOL(fs_overflowuid);
99EXPORT_SYMBOL(fs_overflowgid);
100
101/*
102 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
103 */
104
105int C_A_D = 1;
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700106struct pid *cad_pid;
107EXPORT_SYMBOL(cad_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/*
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700110 * If set, this is used for preparing the system to power off.
111 */
112
113void (*pm_power_off_prepare)(void);
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700114
David Howellsc69e8d92008-11-14 10:39:19 +1100115/*
116 * set the priority of a task
117 * - the caller must hold the RCU read lock
118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static int set_one_prio(struct task_struct *p, int niceval, int error)
120{
David Howellsc69e8d92008-11-14 10:39:19 +1100121 const struct cred *cred = current_cred(), *pcred = __task_cred(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 int no_nice;
123
David Howellsc69e8d92008-11-14 10:39:19 +1100124 if (pcred->uid != cred->euid &&
125 pcred->euid != cred->euid && !capable(CAP_SYS_NICE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 error = -EPERM;
127 goto out;
128 }
Matt Mackalle43379f2005-05-01 08:59:00 -0700129 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 error = -EACCES;
131 goto out;
132 }
133 no_nice = security_task_setnice(p, niceval);
134 if (no_nice) {
135 error = no_nice;
136 goto out;
137 }
138 if (error == -ESRCH)
139 error = 0;
140 set_user_nice(p, niceval);
141out:
142 return error;
143}
144
145asmlinkage long sys_setpriority(int which, int who, int niceval)
146{
147 struct task_struct *g, *p;
148 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100149 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 int error = -EINVAL;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800151 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Daniel Walker3e88c552007-05-10 22:22:53 -0700153 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 goto out;
155
156 /* normalize: avoid signed division (rounding problems) */
157 error = -ESRCH;
158 if (niceval < -20)
159 niceval = -20;
160 if (niceval > 19)
161 niceval = 19;
162
163 read_lock(&tasklist_lock);
164 switch (which) {
165 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800166 if (who)
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700167 p = find_task_by_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800168 else
169 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (p)
171 error = set_one_prio(p, niceval, error);
172 break;
173 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800174 if (who)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700175 pgrp = find_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800176 else
177 pgrp = task_pgrp(current);
Ken Chen2d70b682008-08-20 14:09:17 -0700178 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 error = set_one_prio(p, niceval, error);
Ken Chen2d70b682008-08-20 14:09:17 -0700180 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 break;
182 case PRIO_USER:
David Howells86a264a2008-11-14 10:39:18 +1100183 user = cred->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (!who)
David Howells86a264a2008-11-14 10:39:18 +1100185 who = cred->uid;
186 else if ((who != cred->uid) &&
187 !(user = find_user(who)))
188 goto out_unlock; /* No processes for this user */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 do_each_thread(g, p)
David Howells86a264a2008-11-14 10:39:18 +1100191 if (__task_cred(p)->uid == who)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 error = set_one_prio(p, niceval, error);
193 while_each_thread(g, p);
David Howells86a264a2008-11-14 10:39:18 +1100194 if (who != cred->uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 free_uid(user); /* For find_user() */
196 break;
197 }
198out_unlock:
199 read_unlock(&tasklist_lock);
200out:
201 return error;
202}
203
204/*
205 * Ugh. To avoid negative return values, "getpriority()" will
206 * not return the normal nice-value, but a negated value that
207 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
208 * to stay compatible.
209 */
210asmlinkage long sys_getpriority(int which, int who)
211{
212 struct task_struct *g, *p;
213 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100214 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 long niceval, retval = -ESRCH;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800216 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Daniel Walker3e88c552007-05-10 22:22:53 -0700218 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return -EINVAL;
220
221 read_lock(&tasklist_lock);
222 switch (which) {
223 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800224 if (who)
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700225 p = find_task_by_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800226 else
227 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (p) {
229 niceval = 20 - task_nice(p);
230 if (niceval > retval)
231 retval = niceval;
232 }
233 break;
234 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800235 if (who)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700236 pgrp = find_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800237 else
238 pgrp = task_pgrp(current);
Ken Chen2d70b682008-08-20 14:09:17 -0700239 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 niceval = 20 - task_nice(p);
241 if (niceval > retval)
242 retval = niceval;
Ken Chen2d70b682008-08-20 14:09:17 -0700243 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 break;
245 case PRIO_USER:
David Howells86a264a2008-11-14 10:39:18 +1100246 user = (struct user_struct *) cred->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (!who)
David Howells86a264a2008-11-14 10:39:18 +1100248 who = cred->uid;
249 else if ((who != cred->uid) &&
250 !(user = find_user(who)))
251 goto out_unlock; /* No processes for this user */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 do_each_thread(g, p)
David Howells86a264a2008-11-14 10:39:18 +1100254 if (__task_cred(p)->uid == who) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 niceval = 20 - task_nice(p);
256 if (niceval > retval)
257 retval = niceval;
258 }
259 while_each_thread(g, p);
David Howells86a264a2008-11-14 10:39:18 +1100260 if (who != cred->uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 free_uid(user); /* for find_user() */
262 break;
263 }
264out_unlock:
265 read_unlock(&tasklist_lock);
266
267 return retval;
268}
269
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700270/**
271 * emergency_restart - reboot the system
272 *
273 * Without shutting down any hardware or taking any locks
274 * reboot the system. This is called when we know we are in
275 * trouble so this is our best effort to reboot. This is
276 * safe to call in interrupt context.
277 */
Eric W. Biederman7c903472005-07-26 11:29:55 -0600278void emergency_restart(void)
279{
280 machine_emergency_restart();
281}
282EXPORT_SYMBOL_GPL(emergency_restart);
283
Huang Yingca195b72008-08-15 00:40:24 -0700284void kernel_restart_prepare(char *cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600285{
Alan Sterne041c682006-03-27 01:16:30 -0800286 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600287 system_state = SYSTEM_RESTART;
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600288 device_shutdown();
Rafael J. Wysocki58b3b712007-07-26 16:29:55 +0200289 sysdev_shutdown();
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700290}
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800291
292/**
293 * kernel_restart - reboot the system
294 * @cmd: pointer to buffer containing command to execute for restart
Randy Dunlapb8887e62005-11-07 01:01:07 -0800295 * or %NULL
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800296 *
297 * Shutdown everything and perform a clean reboot.
298 * This is not safe to call in interrupt context.
299 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700300void kernel_restart(char *cmd)
301{
302 kernel_restart_prepare(cmd);
Cal Peake756184b2006-09-30 23:27:24 -0700303 if (!cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600304 printk(KERN_EMERG "Restarting system.\n");
Cal Peake756184b2006-09-30 23:27:24 -0700305 else
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600306 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600307 machine_restart(cmd);
308}
309EXPORT_SYMBOL_GPL(kernel_restart);
310
Adrian Bunk4ef72292008-02-04 22:30:06 -0800311static void kernel_shutdown_prepare(enum system_states state)
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500312{
Alan Sterne041c682006-03-27 01:16:30 -0800313 blocking_notifier_call_chain(&reboot_notifier_list,
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500314 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
315 system_state = state;
316 device_shutdown();
317}
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700318/**
319 * kernel_halt - halt the system
320 *
321 * Shutdown everything and perform a clean system halt.
322 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700323void kernel_halt(void)
324{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500325 kernel_shutdown_prepare(SYSTEM_HALT);
Rafael J. Wysocki58b3b712007-07-26 16:29:55 +0200326 sysdev_shutdown();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600327 printk(KERN_EMERG "System halted.\n");
328 machine_halt();
329}
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500330
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600331EXPORT_SYMBOL_GPL(kernel_halt);
332
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700333/**
334 * kernel_power_off - power_off the system
335 *
336 * Shutdown everything and perform a clean system power_off.
337 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700338void kernel_power_off(void)
339{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500340 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700341 if (pm_power_off_prepare)
342 pm_power_off_prepare();
Mark Lord40477272007-10-01 01:20:10 -0700343 disable_nonboot_cpus();
Rafael J. Wysocki58b3b712007-07-26 16:29:55 +0200344 sysdev_shutdown();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600345 printk(KERN_EMERG "Power down.\n");
346 machine_power_off();
347}
348EXPORT_SYMBOL_GPL(kernel_power_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349/*
350 * Reboot system call: for obvious reasons only root may call it,
351 * and even root needs to set up some magic numbers in the registers
352 * so that some mistake won't make this reboot the whole machine.
353 * You can also set the meaning of the ctrl-alt-del-key here.
354 *
355 * reboot doesn't sync: do that yourself before calling this.
356 */
357asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
358{
359 char buffer[256];
360
361 /* We only trust the superuser with rebooting the system. */
362 if (!capable(CAP_SYS_BOOT))
363 return -EPERM;
364
365 /* For safety, we require "magic" arguments. */
366 if (magic1 != LINUX_REBOOT_MAGIC1 ||
367 (magic2 != LINUX_REBOOT_MAGIC2 &&
368 magic2 != LINUX_REBOOT_MAGIC2A &&
369 magic2 != LINUX_REBOOT_MAGIC2B &&
370 magic2 != LINUX_REBOOT_MAGIC2C))
371 return -EINVAL;
372
Eric W. Biederman5e382912006-01-08 01:03:46 -0800373 /* Instead of trying to make the power_off code look like
374 * halt when pm_power_off is not set do it the easy way.
375 */
376 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
377 cmd = LINUX_REBOOT_CMD_HALT;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 lock_kernel();
380 switch (cmd) {
381 case LINUX_REBOOT_CMD_RESTART:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600382 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 break;
384
385 case LINUX_REBOOT_CMD_CAD_ON:
386 C_A_D = 1;
387 break;
388
389 case LINUX_REBOOT_CMD_CAD_OFF:
390 C_A_D = 0;
391 break;
392
393 case LINUX_REBOOT_CMD_HALT:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600394 kernel_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 unlock_kernel();
396 do_exit(0);
397 break;
398
399 case LINUX_REBOOT_CMD_POWER_OFF:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600400 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 unlock_kernel();
402 do_exit(0);
403 break;
404
405 case LINUX_REBOOT_CMD_RESTART2:
406 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
407 unlock_kernel();
408 return -EFAULT;
409 }
410 buffer[sizeof(buffer) - 1] = '\0';
411
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600412 kernel_restart(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 break;
414
Huang Ying3ab83522008-07-25 19:45:07 -0700415#ifdef CONFIG_KEXEC
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700416 case LINUX_REBOOT_CMD_KEXEC:
Huang Ying3ab83522008-07-25 19:45:07 -0700417 {
418 int ret;
419 ret = kernel_kexec();
420 unlock_kernel();
421 return ret;
422 }
423#endif
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600424
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200425#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 case LINUX_REBOOT_CMD_SW_SUSPEND:
427 {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700428 int ret = hibernate();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 unlock_kernel();
430 return ret;
431 }
432#endif
433
434 default:
435 unlock_kernel();
436 return -EINVAL;
437 }
438 unlock_kernel();
439 return 0;
440}
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 */
480asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
481{
David Howellsb6dff3e2008-11-14 10:39:16 +1100482 struct cred *cred = current->cred;
483 int old_rgid = cred->gid;
484 int old_egid = cred->egid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int new_rgid = old_rgid;
486 int new_egid = old_egid;
487 int retval;
488
489 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
490 if (retval)
491 return retval;
492
493 if (rgid != (gid_t) -1) {
494 if ((old_rgid == rgid) ||
David Howellsb6dff3e2008-11-14 10:39:16 +1100495 (cred->egid == rgid) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 capable(CAP_SETGID))
497 new_rgid = rgid;
498 else
499 return -EPERM;
500 }
501 if (egid != (gid_t) -1) {
502 if ((old_rgid == egid) ||
David Howellsb6dff3e2008-11-14 10:39:16 +1100503 (cred->egid == egid) ||
504 (cred->sgid == egid) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 capable(CAP_SETGID))
506 new_egid = egid;
Cal Peake756184b2006-09-30 23:27:24 -0700507 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Cal Peake756184b2006-09-30 23:27:24 -0700510 if (new_egid != old_egid) {
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700511 set_dumpable(current->mm, suid_dumpable);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700512 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514 if (rgid != (gid_t) -1 ||
515 (egid != (gid_t) -1 && egid != old_rgid))
David Howellsb6dff3e2008-11-14 10:39:16 +1100516 cred->sgid = new_egid;
517 cred->fsgid = new_egid;
518 cred->egid = new_egid;
519 cred->gid = new_rgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800521 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return 0;
523}
524
525/*
526 * setgid() is implemented like SysV w/ SAVED_IDS
527 *
528 * SMP: Same implicit races as above.
529 */
530asmlinkage long sys_setgid(gid_t gid)
531{
David Howellsb6dff3e2008-11-14 10:39:16 +1100532 struct cred *cred = current->cred;
533 int old_egid = cred->egid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 int retval;
535
536 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
537 if (retval)
538 return retval;
539
Cal Peake756184b2006-09-30 23:27:24 -0700540 if (capable(CAP_SETGID)) {
541 if (old_egid != gid) {
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700542 set_dumpable(current->mm, suid_dumpable);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700543 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100545 cred->gid = cred->egid = cred->sgid = cred->fsgid = gid;
546 } else if ((gid == cred->gid) || (gid == cred->sgid)) {
Cal Peake756184b2006-09-30 23:27:24 -0700547 if (old_egid != gid) {
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700548 set_dumpable(current->mm, suid_dumpable);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700549 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100551 cred->egid = cred->fsgid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553 else
554 return -EPERM;
555
556 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800557 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return 0;
559}
560
561static int set_user(uid_t new_ruid, int dumpclear)
562{
563 struct user_struct *new_user;
564
Cedric Le Goateracce2922007-07-15 23:40:59 -0700565 new_user = alloc_uid(current->nsproxy->user_ns, new_ruid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (!new_user)
567 return -EAGAIN;
568
569 if (atomic_read(&new_user->processes) >=
570 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
Cedric Le Goateracce2922007-07-15 23:40:59 -0700571 new_user != current->nsproxy->user_ns->root_user) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 free_uid(new_user);
573 return -EAGAIN;
574 }
575
576 switch_uid(new_user);
577
Cal Peake756184b2006-09-30 23:27:24 -0700578 if (dumpclear) {
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700579 set_dumpable(current->mm, suid_dumpable);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700580 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100582 current->cred->uid = new_ruid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return 0;
584}
585
586/*
587 * Unprivileged users may change the real uid to the effective uid
588 * or vice versa. (BSD-style)
589 *
590 * If you set the real uid at all, or set the effective uid to a value not
591 * equal to the real uid, then the saved uid is set to the new effective uid.
592 *
593 * This makes it possible for a setuid program to completely drop its
594 * privileges, which is often a useful assertion to make when you are doing
595 * a security audit over a program.
596 *
597 * The general idea is that a program which uses just setreuid() will be
598 * 100% compatible with BSD. A program which uses just setuid() will be
599 * 100% compatible with POSIX with saved IDs.
600 */
601asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
602{
David Howellsb6dff3e2008-11-14 10:39:16 +1100603 struct cred *cred = current->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
605 int retval;
606
607 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
608 if (retval)
609 return retval;
610
David Howellsb6dff3e2008-11-14 10:39:16 +1100611 new_ruid = old_ruid = cred->uid;
612 new_euid = old_euid = cred->euid;
613 old_suid = cred->suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 if (ruid != (uid_t) -1) {
616 new_ruid = ruid;
617 if ((old_ruid != ruid) &&
David Howellsb6dff3e2008-11-14 10:39:16 +1100618 (cred->euid != ruid) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 !capable(CAP_SETUID))
620 return -EPERM;
621 }
622
623 if (euid != (uid_t) -1) {
624 new_euid = euid;
625 if ((old_ruid != euid) &&
David Howellsb6dff3e2008-11-14 10:39:16 +1100626 (cred->euid != euid) &&
627 (cred->suid != euid) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 !capable(CAP_SETUID))
629 return -EPERM;
630 }
631
632 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
633 return -EAGAIN;
634
Cal Peake756184b2006-09-30 23:27:24 -0700635 if (new_euid != old_euid) {
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700636 set_dumpable(current->mm, suid_dumpable);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700637 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100639 cred->fsuid = cred->euid = new_euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (ruid != (uid_t) -1 ||
641 (euid != (uid_t) -1 && euid != old_ruid))
David Howellsb6dff3e2008-11-14 10:39:16 +1100642 cred->suid = cred->euid;
643 cred->fsuid = cred->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800646 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
649}
650
651
652
653/*
654 * setuid() is implemented like SysV with SAVED_IDS
655 *
656 * Note that SAVED_ID's is deficient in that a setuid root program
657 * like sendmail, for example, cannot set its uid to be a normal
658 * user and then switch back, because if you're root, setuid() sets
659 * the saved uid too. If you don't like this, blame the bright people
660 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
661 * will allow a root program to temporarily drop privileges and be able to
662 * regain them by swapping the real and effective uid.
663 */
664asmlinkage long sys_setuid(uid_t uid)
665{
David Howellsb6dff3e2008-11-14 10:39:16 +1100666 struct cred *cred = current->cred;
667 int old_euid = cred->euid;
David Rientjesa09c17a2006-12-06 20:40:18 -0800668 int old_ruid, old_suid, new_suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 int retval;
670
671 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
672 if (retval)
673 return retval;
674
David Howellsb6dff3e2008-11-14 10:39:16 +1100675 old_ruid = cred->uid;
676 old_suid = cred->suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 new_suid = old_suid;
678
679 if (capable(CAP_SETUID)) {
680 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
681 return -EAGAIN;
682 new_suid = uid;
David Howellsb6dff3e2008-11-14 10:39:16 +1100683 } else if ((uid != cred->uid) && (uid != new_suid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return -EPERM;
685
Cal Peake756184b2006-09-30 23:27:24 -0700686 if (old_euid != uid) {
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700687 set_dumpable(current->mm, suid_dumpable);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700688 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100690 cred->fsuid = cred->euid = uid;
691 cred->suid = new_suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800694 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
697}
698
699
700/*
701 * This function implements a generic ability to update ruid, euid,
702 * and suid. This allows you to implement the 4.4 compatible seteuid().
703 */
704asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
705{
David Howellsb6dff3e2008-11-14 10:39:16 +1100706 struct cred *cred = current->cred;
707 int old_ruid = cred->uid;
708 int old_euid = cred->euid;
709 int old_suid = cred->suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 int retval;
711
712 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
713 if (retval)
714 return retval;
715
716 if (!capable(CAP_SETUID)) {
David Howellsb6dff3e2008-11-14 10:39:16 +1100717 if ((ruid != (uid_t) -1) && (ruid != cred->uid) &&
718 (ruid != cred->euid) && (ruid != cred->suid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return -EPERM;
David Howellsb6dff3e2008-11-14 10:39:16 +1100720 if ((euid != (uid_t) -1) && (euid != cred->uid) &&
721 (euid != cred->euid) && (euid != cred->suid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return -EPERM;
David Howellsb6dff3e2008-11-14 10:39:16 +1100723 if ((suid != (uid_t) -1) && (suid != cred->uid) &&
724 (suid != cred->euid) && (suid != cred->suid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return -EPERM;
726 }
727 if (ruid != (uid_t) -1) {
David Howellsb6dff3e2008-11-14 10:39:16 +1100728 if (ruid != cred->uid &&
729 set_user(ruid, euid != cred->euid) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return -EAGAIN;
731 }
732 if (euid != (uid_t) -1) {
David Howellsb6dff3e2008-11-14 10:39:16 +1100733 if (euid != cred->euid) {
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700734 set_dumpable(current->mm, suid_dumpable);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700735 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100737 cred->euid = euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100739 cred->fsuid = cred->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (suid != (uid_t) -1)
David Howellsb6dff3e2008-11-14 10:39:16 +1100741 cred->suid = suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800744 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
747}
748
749asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
750{
David Howells86a264a2008-11-14 10:39:18 +1100751 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 int retval;
753
David Howells86a264a2008-11-14 10:39:18 +1100754 if (!(retval = put_user(cred->uid, ruid)) &&
755 !(retval = put_user(cred->euid, euid)))
David Howellsb6dff3e2008-11-14 10:39:16 +1100756 retval = put_user(cred->suid, suid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 return retval;
759}
760
761/*
762 * Same as above, but for rgid, egid, sgid.
763 */
764asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
765{
David Howellsb6dff3e2008-11-14 10:39:16 +1100766 struct cred *cred = current->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 int retval;
768
769 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
770 if (retval)
771 return retval;
772
773 if (!capable(CAP_SETGID)) {
David Howellsb6dff3e2008-11-14 10:39:16 +1100774 if ((rgid != (gid_t) -1) && (rgid != cred->gid) &&
775 (rgid != cred->egid) && (rgid != cred->sgid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return -EPERM;
David Howellsb6dff3e2008-11-14 10:39:16 +1100777 if ((egid != (gid_t) -1) && (egid != cred->gid) &&
778 (egid != cred->egid) && (egid != cred->sgid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return -EPERM;
David Howellsb6dff3e2008-11-14 10:39:16 +1100780 if ((sgid != (gid_t) -1) && (sgid != cred->gid) &&
781 (sgid != cred->egid) && (sgid != cred->sgid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return -EPERM;
783 }
784 if (egid != (gid_t) -1) {
David Howellsb6dff3e2008-11-14 10:39:16 +1100785 if (egid != cred->egid) {
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700786 set_dumpable(current->mm, suid_dumpable);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700787 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100789 cred->egid = egid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100791 cred->fsgid = cred->egid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (rgid != (gid_t) -1)
David Howellsb6dff3e2008-11-14 10:39:16 +1100793 cred->gid = rgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (sgid != (gid_t) -1)
David Howellsb6dff3e2008-11-14 10:39:16 +1100795 cred->sgid = sgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800798 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return 0;
800}
801
802asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
803{
David Howells86a264a2008-11-14 10:39:18 +1100804 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 int retval;
806
David Howells86a264a2008-11-14 10:39:18 +1100807 if (!(retval = put_user(cred->gid, rgid)) &&
808 !(retval = put_user(cred->egid, egid)))
David Howellsb6dff3e2008-11-14 10:39:16 +1100809 retval = put_user(cred->sgid, sgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 return retval;
812}
813
814
815/*
816 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
817 * is used for "access()" and for the NFS daemon (letting nfsd stay at
818 * whatever uid it wants to). It normally shadows "euid", except when
819 * explicitly set by setfsuid() or for access..
820 */
821asmlinkage long sys_setfsuid(uid_t uid)
822{
David Howellsb6dff3e2008-11-14 10:39:16 +1100823 struct cred *cred = current->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 int old_fsuid;
825
David Howellsb6dff3e2008-11-14 10:39:16 +1100826 old_fsuid = cred->fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
828 return old_fsuid;
829
David Howellsb6dff3e2008-11-14 10:39:16 +1100830 if (uid == cred->uid || uid == cred->euid ||
831 uid == cred->suid || uid == cred->fsuid ||
Cal Peake756184b2006-09-30 23:27:24 -0700832 capable(CAP_SETUID)) {
833 if (uid != old_fsuid) {
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700834 set_dumpable(current->mm, suid_dumpable);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700835 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100837 cred->fsuid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839
840 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800841 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
844
845 return old_fsuid;
846}
847
848/*
John Anthony Kazos Jrf42df9e2007-05-09 08:23:08 +0200849 * Samma på svenska..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 */
851asmlinkage long sys_setfsgid(gid_t gid)
852{
David Howellsb6dff3e2008-11-14 10:39:16 +1100853 struct cred *cred = current->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 int old_fsgid;
855
David Howellsb6dff3e2008-11-14 10:39:16 +1100856 old_fsgid = cred->fsgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
858 return old_fsgid;
859
David Howellsb6dff3e2008-11-14 10:39:16 +1100860 if (gid == cred->gid || gid == cred->egid ||
861 gid == cred->sgid || gid == cred->fsgid ||
Cal Peake756184b2006-09-30 23:27:24 -0700862 capable(CAP_SETGID)) {
863 if (gid != old_fsgid) {
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700864 set_dumpable(current->mm, suid_dumpable);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700865 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100867 cred->fsgid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800869 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871 return old_fsgid;
872}
873
Frank Mayharf06febc2008-09-12 09:54:39 -0700874void do_sys_times(struct tms *tms)
875{
876 struct task_cputime cputime;
877 cputime_t cutime, cstime;
878
879 spin_lock_irq(&current->sighand->siglock);
880 thread_group_cputime(current, &cputime);
881 cutime = current->signal->cutime;
882 cstime = current->signal->cstime;
883 spin_unlock_irq(&current->sighand->siglock);
884 tms->tms_utime = cputime_to_clock_t(cputime.utime);
885 tms->tms_stime = cputime_to_clock_t(cputime.stime);
886 tms->tms_cutime = cputime_to_clock_t(cutime);
887 tms->tms_cstime = cputime_to_clock_t(cstime);
888}
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890asmlinkage long sys_times(struct tms __user * tbuf)
891{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (tbuf) {
893 struct tms tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Frank Mayharf06febc2008-09-12 09:54:39 -0700895 do_sys_times(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
897 return -EFAULT;
898 }
899 return (long) jiffies_64_to_clock_t(get_jiffies_64());
900}
901
902/*
903 * This needs some heavy checking ...
904 * I just haven't the stomach for it. I also don't fully
905 * understand sessions/pgrp etc. Let somebody who does explain it.
906 *
907 * OK, I think I have the protection semantics right.... this is really
908 * only important on a multi-user system anyway, to make sure one user
909 * can't send a signal to a process owned by another. -TYT, 12/12/91
910 *
911 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
912 * LBT 04.03.94
913 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
915{
916 struct task_struct *p;
Oleg Nesterovee0acf92006-01-08 01:03:53 -0800917 struct task_struct *group_leader = current->group_leader;
Oleg Nesterov4e021302008-02-08 04:19:08 -0800918 struct pid *pgrp;
919 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (!pid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700922 pid = task_pid_vnr(group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (!pgid)
924 pgid = pid;
925 if (pgid < 0)
926 return -EINVAL;
927
928 /* From this point forward we keep holding onto the tasklist lock
929 * so that our parent does not change from under us. -DaveM
930 */
931 write_lock_irq(&tasklist_lock);
932
933 err = -ESRCH;
Oleg Nesterov4e021302008-02-08 04:19:08 -0800934 p = find_task_by_vpid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (!p)
936 goto out;
937
938 err = -EINVAL;
939 if (!thread_group_leader(p))
940 goto out;
941
Oleg Nesterov4e021302008-02-08 04:19:08 -0800942 if (same_thread_group(p->real_parent, group_leader)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 err = -EPERM;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800944 if (task_session(p) != task_session(group_leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 goto out;
946 err = -EACCES;
947 if (p->did_exec)
948 goto out;
949 } else {
950 err = -ESRCH;
Oleg Nesterovee0acf92006-01-08 01:03:53 -0800951 if (p != group_leader)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 goto out;
953 }
954
955 err = -EPERM;
956 if (p->signal->leader)
957 goto out;
958
Oleg Nesterov4e021302008-02-08 04:19:08 -0800959 pgrp = task_pid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (pgid != pid) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700961 struct task_struct *g;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Oleg Nesterov4e021302008-02-08 04:19:08 -0800963 pgrp = find_vpid(pgid);
964 g = pid_task(pgrp, PIDTYPE_PGID);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800965 if (!g || task_session(g) != task_session(group_leader))
Oleg Nesterovf020bc42006-12-08 02:38:02 -0800966 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 err = security_task_setpgid(p, pgid);
970 if (err)
971 goto out;
972
Oleg Nesterov4e021302008-02-08 04:19:08 -0800973 if (task_pgrp(p) != pgrp) {
Oleg Nesterov83beaf32008-04-30 00:54:27 -0700974 change_pid(p, PIDTYPE_PGID, pgrp);
Oleg Nesterov4e021302008-02-08 04:19:08 -0800975 set_task_pgrp(p, pid_nr(pgrp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977
978 err = 0;
979out:
980 /* All paths lead to here, thus we are safe. -DaveM */
981 write_unlock_irq(&tasklist_lock);
982 return err;
983}
984
985asmlinkage long sys_getpgid(pid_t pid)
986{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -0700987 struct task_struct *p;
988 struct pid *grp;
989 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -0700991 rcu_read_lock();
992 if (!pid)
993 grp = task_pgrp(current);
994 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 retval = -ESRCH;
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -0700996 p = find_task_by_vpid(pid);
997 if (!p)
998 goto out;
999 grp = task_pgrp(p);
1000 if (!grp)
1001 goto out;
1002
1003 retval = security_task_getpgid(p);
1004 if (retval)
1005 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001007 retval = pid_vnr(grp);
1008out:
1009 rcu_read_unlock();
1010 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
1013#ifdef __ARCH_WANT_SYS_GETPGRP
1014
1015asmlinkage long sys_getpgrp(void)
1016{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001017 return sys_getpgid(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
1020#endif
1021
1022asmlinkage long sys_getsid(pid_t pid)
1023{
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001024 struct task_struct *p;
1025 struct pid *sid;
1026 int retval;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001027
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001028 rcu_read_lock();
1029 if (!pid)
1030 sid = task_session(current);
1031 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 retval = -ESRCH;
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001033 p = find_task_by_vpid(pid);
1034 if (!p)
1035 goto out;
1036 sid = task_session(p);
1037 if (!sid)
1038 goto out;
1039
1040 retval = security_task_getsid(p);
1041 if (retval)
1042 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001044 retval = pid_vnr(sid);
1045out:
1046 rcu_read_unlock();
1047 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
1050asmlinkage long sys_setsid(void)
1051{
Oren Laadane19f2472006-01-08 01:03:58 -08001052 struct task_struct *group_leader = current->group_leader;
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001053 struct pid *sid = task_pid(group_leader);
1054 pid_t session = pid_vnr(sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 int err = -EPERM;
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 write_lock_irq(&tasklist_lock);
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001058 /* Fail if I am already a session leader */
1059 if (group_leader->signal->leader)
1060 goto out;
1061
Oleg Nesterov430c6232008-02-08 04:19:11 -08001062 /* Fail if a process group id already exists that equals the
1063 * proposed session id.
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001064 */
Oleg Nesterov6806aac2008-02-08 04:19:12 -08001065 if (pid_task(sid, PIDTYPE_PGID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 goto out;
1067
Oren Laadane19f2472006-01-08 01:03:58 -08001068 group_leader->signal->leader = 1;
Oleg Nesterov8520d7c2008-02-08 04:19:09 -08001069 __set_special_pids(sid);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001070
Alan Cox9c9f4de2008-10-13 10:37:26 +01001071 proc_clear_tty(group_leader);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001072
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001073 err = session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074out:
1075 write_unlock_irq(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return err;
1077}
1078
1079/*
1080 * Supplementary group IDs
1081 */
1082
1083/* init to 2 - one for init_task, one to ensure it is never freed */
1084struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1085
1086struct group_info *groups_alloc(int gidsetsize)
1087{
1088 struct group_info *group_info;
1089 int nblocks;
1090 int i;
1091
1092 nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1093 /* Make sure we always allocate at least one indirect block pointer */
1094 nblocks = nblocks ? : 1;
1095 group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1096 if (!group_info)
1097 return NULL;
1098 group_info->ngroups = gidsetsize;
1099 group_info->nblocks = nblocks;
1100 atomic_set(&group_info->usage, 1);
1101
Cal Peake756184b2006-09-30 23:27:24 -07001102 if (gidsetsize <= NGROUPS_SMALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 group_info->blocks[0] = group_info->small_block;
Cal Peake756184b2006-09-30 23:27:24 -07001104 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 for (i = 0; i < nblocks; i++) {
1106 gid_t *b;
1107 b = (void *)__get_free_page(GFP_USER);
1108 if (!b)
1109 goto out_undo_partial_alloc;
1110 group_info->blocks[i] = b;
1111 }
1112 }
1113 return group_info;
1114
1115out_undo_partial_alloc:
1116 while (--i >= 0) {
1117 free_page((unsigned long)group_info->blocks[i]);
1118 }
1119 kfree(group_info);
1120 return NULL;
1121}
1122
1123EXPORT_SYMBOL(groups_alloc);
1124
1125void groups_free(struct group_info *group_info)
1126{
1127 if (group_info->blocks[0] != group_info->small_block) {
1128 int i;
1129 for (i = 0; i < group_info->nblocks; i++)
1130 free_page((unsigned long)group_info->blocks[i]);
1131 }
1132 kfree(group_info);
1133}
1134
1135EXPORT_SYMBOL(groups_free);
1136
1137/* export the group_info to a user-space array */
1138static int groups_to_user(gid_t __user *grouplist,
1139 struct group_info *group_info)
1140{
1141 int i;
Eric Dumazet1bf47342008-02-06 01:37:56 -08001142 unsigned int count = group_info->ngroups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 for (i = 0; i < group_info->nblocks; i++) {
Eric Dumazet1bf47342008-02-06 01:37:56 -08001145 unsigned int cp_count = min(NGROUPS_PER_BLOCK, count);
1146 unsigned int len = cp_count * sizeof(*grouplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Eric Dumazet1bf47342008-02-06 01:37:56 -08001148 if (copy_to_user(grouplist, group_info->blocks[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return -EFAULT;
1150
Eric Dumazet1bf47342008-02-06 01:37:56 -08001151 grouplist += NGROUPS_PER_BLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 count -= cp_count;
1153 }
1154 return 0;
1155}
1156
1157/* fill a group_info from a user-space array - it must be allocated already */
1158static int groups_from_user(struct group_info *group_info,
1159 gid_t __user *grouplist)
Cal Peake756184b2006-09-30 23:27:24 -07001160{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 int i;
Eric Dumazet1bf47342008-02-06 01:37:56 -08001162 unsigned int count = group_info->ngroups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 for (i = 0; i < group_info->nblocks; i++) {
Eric Dumazet1bf47342008-02-06 01:37:56 -08001165 unsigned int cp_count = min(NGROUPS_PER_BLOCK, count);
1166 unsigned int len = cp_count * sizeof(*grouplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Eric Dumazet1bf47342008-02-06 01:37:56 -08001168 if (copy_from_user(group_info->blocks[i], grouplist, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return -EFAULT;
1170
Eric Dumazet1bf47342008-02-06 01:37:56 -08001171 grouplist += NGROUPS_PER_BLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 count -= cp_count;
1173 }
1174 return 0;
1175}
1176
Domen Puncerebe8b542005-05-05 16:16:19 -07001177/* a simple Shell sort */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178static void groups_sort(struct group_info *group_info)
1179{
1180 int base, max, stride;
1181 int gidsetsize = group_info->ngroups;
1182
1183 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1184 ; /* nothing */
1185 stride /= 3;
1186
1187 while (stride) {
1188 max = gidsetsize - stride;
1189 for (base = 0; base < max; base++) {
1190 int left = base;
1191 int right = left + stride;
1192 gid_t tmp = GROUP_AT(group_info, right);
1193
1194 while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1195 GROUP_AT(group_info, right) =
1196 GROUP_AT(group_info, left);
1197 right = left;
1198 left -= stride;
1199 }
1200 GROUP_AT(group_info, right) = tmp;
1201 }
1202 stride /= 3;
1203 }
1204}
1205
1206/* a simple bsearch */
David Howells86a264a2008-11-14 10:39:18 +11001207int groups_search(const struct group_info *group_info, gid_t grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Eric Dumazetd74beb9f2006-03-25 03:08:19 -08001209 unsigned int left, right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 if (!group_info)
1212 return 0;
1213
1214 left = 0;
1215 right = group_info->ngroups;
1216 while (left < right) {
Eric Dumazetd74beb9f2006-03-25 03:08:19 -08001217 unsigned int mid = (left+right)/2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 int cmp = grp - GROUP_AT(group_info, mid);
1219 if (cmp > 0)
1220 left = mid + 1;
1221 else if (cmp < 0)
1222 right = mid;
1223 else
1224 return 1;
1225 }
1226 return 0;
1227}
1228
David Howellsb6dff3e2008-11-14 10:39:16 +11001229/**
1230 * set_groups - Change a group subscription in a security record
1231 * @sec: The security record to alter
1232 * @group_info: The group list to impose
1233 *
1234 * Validate a group subscription and, if valid, impose it upon a task security
1235 * record.
1236 */
1237int set_groups(struct cred *cred, struct group_info *group_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239 int retval;
1240 struct group_info *old_info;
1241
1242 retval = security_task_setgroups(group_info);
1243 if (retval)
1244 return retval;
1245
1246 groups_sort(group_info);
1247 get_group_info(group_info);
1248
David Howellsb6dff3e2008-11-14 10:39:16 +11001249 spin_lock(&cred->lock);
1250 old_info = cred->group_info;
1251 cred->group_info = group_info;
1252 spin_unlock(&cred->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 put_group_info(old_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return 0;
1256}
1257
David Howellsb6dff3e2008-11-14 10:39:16 +11001258EXPORT_SYMBOL(set_groups);
1259
1260/**
1261 * set_current_groups - Change current's group subscription
1262 * @group_info: The group list to impose
1263 *
1264 * Validate a group subscription and, if valid, impose it upon current's task
1265 * security record.
1266 */
1267int set_current_groups(struct group_info *group_info)
1268{
1269 return set_groups(current->cred, group_info);
1270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272EXPORT_SYMBOL(set_current_groups);
1273
1274asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1275{
David Howells86a264a2008-11-14 10:39:18 +11001276 const struct cred *cred = current_cred();
1277 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 if (gidsetsize < 0)
1280 return -EINVAL;
1281
1282 /* no need to grab task_lock here; it cannot change */
David Howellsb6dff3e2008-11-14 10:39:16 +11001283 i = cred->group_info->ngroups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (gidsetsize) {
1285 if (i > gidsetsize) {
1286 i = -EINVAL;
1287 goto out;
1288 }
David Howellsb6dff3e2008-11-14 10:39:16 +11001289 if (groups_to_user(grouplist, cred->group_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 i = -EFAULT;
1291 goto out;
1292 }
1293 }
1294out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return i;
1296}
1297
1298/*
1299 * SMP: Our groups are copy-on-write. We can set them safely
1300 * without another task interfering.
1301 */
1302
1303asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1304{
1305 struct group_info *group_info;
1306 int retval;
1307
1308 if (!capable(CAP_SETGID))
1309 return -EPERM;
1310 if ((unsigned)gidsetsize > NGROUPS_MAX)
1311 return -EINVAL;
1312
1313 group_info = groups_alloc(gidsetsize);
1314 if (!group_info)
1315 return -ENOMEM;
1316 retval = groups_from_user(group_info, grouplist);
1317 if (retval) {
1318 put_group_info(group_info);
1319 return retval;
1320 }
1321
1322 retval = set_current_groups(group_info);
1323 put_group_info(group_info);
1324
1325 return retval;
1326}
1327
1328/*
1329 * Check whether we're fsgid/egid or in the supplemental group..
1330 */
1331int in_group_p(gid_t grp)
1332{
David Howells86a264a2008-11-14 10:39:18 +11001333 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 int retval = 1;
David Howells86a264a2008-11-14 10:39:18 +11001335
David Howellsb6dff3e2008-11-14 10:39:16 +11001336 if (grp != cred->fsgid)
1337 retval = groups_search(cred->group_info, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return retval;
1339}
1340
1341EXPORT_SYMBOL(in_group_p);
1342
1343int in_egroup_p(gid_t grp)
1344{
David Howells86a264a2008-11-14 10:39:18 +11001345 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 int retval = 1;
David Howells86a264a2008-11-14 10:39:18 +11001347
David Howellsb6dff3e2008-11-14 10:39:16 +11001348 if (grp != cred->egid)
1349 retval = groups_search(cred->group_info, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return retval;
1351}
1352
1353EXPORT_SYMBOL(in_egroup_p);
1354
1355DECLARE_RWSEM(uts_sem);
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357asmlinkage long sys_newuname(struct new_utsname __user * name)
1358{
1359 int errno = 0;
1360
1361 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001362 if (copy_to_user(name, utsname(), sizeof *name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 errno = -EFAULT;
1364 up_read(&uts_sem);
1365 return errno;
1366}
1367
1368asmlinkage long sys_sethostname(char __user *name, int len)
1369{
1370 int errno;
1371 char tmp[__NEW_UTS_LEN];
1372
1373 if (!capable(CAP_SYS_ADMIN))
1374 return -EPERM;
1375 if (len < 0 || len > __NEW_UTS_LEN)
1376 return -EINVAL;
1377 down_write(&uts_sem);
1378 errno = -EFAULT;
1379 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001380 struct new_utsname *u = utsname();
1381
1382 memcpy(u->nodename, tmp, len);
1383 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 errno = 0;
1385 }
1386 up_write(&uts_sem);
1387 return errno;
1388}
1389
1390#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1391
1392asmlinkage long sys_gethostname(char __user *name, int len)
1393{
1394 int i, errno;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001395 struct new_utsname *u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 if (len < 0)
1398 return -EINVAL;
1399 down_read(&uts_sem);
Andrew Morton9679e4d2008-10-15 22:01:51 -07001400 u = utsname();
1401 i = 1 + strlen(u->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (i > len)
1403 i = len;
1404 errno = 0;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001405 if (copy_to_user(name, u->nodename, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 errno = -EFAULT;
1407 up_read(&uts_sem);
1408 return errno;
1409}
1410
1411#endif
1412
1413/*
1414 * Only setdomainname; getdomainname can be implemented by calling
1415 * uname()
1416 */
1417asmlinkage long sys_setdomainname(char __user *name, int len)
1418{
1419 int errno;
1420 char tmp[__NEW_UTS_LEN];
1421
1422 if (!capable(CAP_SYS_ADMIN))
1423 return -EPERM;
1424 if (len < 0 || len > __NEW_UTS_LEN)
1425 return -EINVAL;
1426
1427 down_write(&uts_sem);
1428 errno = -EFAULT;
1429 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001430 struct new_utsname *u = utsname();
1431
1432 memcpy(u->domainname, tmp, len);
1433 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 errno = 0;
1435 }
1436 up_write(&uts_sem);
1437 return errno;
1438}
1439
1440asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1441{
1442 if (resource >= RLIM_NLIMITS)
1443 return -EINVAL;
1444 else {
1445 struct rlimit value;
1446 task_lock(current->group_leader);
1447 value = current->signal->rlim[resource];
1448 task_unlock(current->group_leader);
1449 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1450 }
1451}
1452
1453#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1454
1455/*
1456 * Back compatibility for getrlimit. Needed for some apps.
1457 */
1458
1459asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1460{
1461 struct rlimit x;
1462 if (resource >= RLIM_NLIMITS)
1463 return -EINVAL;
1464
1465 task_lock(current->group_leader);
1466 x = current->signal->rlim[resource];
1467 task_unlock(current->group_leader);
Cal Peake756184b2006-09-30 23:27:24 -07001468 if (x.rlim_cur > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 x.rlim_cur = 0x7FFFFFFF;
Cal Peake756184b2006-09-30 23:27:24 -07001470 if (x.rlim_max > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 x.rlim_max = 0x7FFFFFFF;
1472 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1473}
1474
1475#endif
1476
1477asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1478{
1479 struct rlimit new_rlim, *old_rlim;
1480 int retval;
1481
1482 if (resource >= RLIM_NLIMITS)
1483 return -EINVAL;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001484 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 old_rlim = current->signal->rlim + resource;
1487 if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1488 !capable(CAP_SYS_RESOURCE))
1489 return -EPERM;
Adam Tkac0c2d64f2008-10-15 22:01:45 -07001490
1491 if (resource == RLIMIT_NOFILE) {
1492 if (new_rlim.rlim_max == RLIM_INFINITY)
1493 new_rlim.rlim_max = sysctl_nr_open;
1494 if (new_rlim.rlim_cur == RLIM_INFINITY)
1495 new_rlim.rlim_cur = sysctl_nr_open;
1496 if (new_rlim.rlim_max > sysctl_nr_open)
1497 return -EPERM;
1498 }
1499
1500 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1501 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 retval = security_task_setrlimit(resource, &new_rlim);
1504 if (retval)
1505 return retval;
1506
Tom Alsberg9926e4c2007-05-08 00:30:31 -07001507 if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
1508 /*
1509 * The caller is asking for an immediate RLIMIT_CPU
1510 * expiry. But we use the zero value to mean "it was
1511 * never set". So let's cheat and make it one second
1512 * instead
1513 */
1514 new_rlim.rlim_cur = 1;
1515 }
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 task_lock(current->group_leader);
1518 *old_rlim = new_rlim;
1519 task_unlock(current->group_leader);
1520
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001521 if (resource != RLIMIT_CPU)
1522 goto out;
Andrew Mortond3561f72006-03-24 03:18:36 -08001523
1524 /*
1525 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1526 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1527 * very long-standing error, and fixing it now risks breakage of
1528 * applications, so we live with it
1529 */
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001530 if (new_rlim.rlim_cur == RLIM_INFINITY)
1531 goto out;
1532
Frank Mayharf06febc2008-09-12 09:54:39 -07001533 update_rlimit_cpu(new_rlim.rlim_cur);
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001534out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return 0;
1536}
1537
1538/*
1539 * It would make sense to put struct rusage in the task_struct,
1540 * except that would make the task_struct be *really big*. After
1541 * task_struct gets moved into malloc'ed memory, it would
1542 * make sense to do this. It will make moving the rest of the information
1543 * a lot simpler! (Which we're not doing right now because we're not
1544 * measuring them yet).
1545 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1547 * races with threads incrementing their own counters. But since word
1548 * reads are atomic, we either get new values or old values and we don't
1549 * care which for the sums. We always take the siglock to protect reading
1550 * the c* fields from p->signal from races with exit.c updating those
1551 * fields when reaping, so a sample either gets all the additions of a
1552 * given child after it's reaped, or none so this sample is before reaping.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001553 *
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001554 * Locking:
1555 * We need to take the siglock for CHILDEREN, SELF and BOTH
1556 * for the cases current multithreaded, non-current single threaded
1557 * non-current multithreaded. Thread traversal is now safe with
1558 * the siglock held.
1559 * Strictly speaking, we donot need to take the siglock if we are current and
1560 * single threaded, as no one else can take our signal_struct away, no one
1561 * else can reap the children to update signal->c* counters, and no one else
1562 * can race with the signal-> fields. If we do not take any lock, the
1563 * signal-> fields could be read out of order while another thread was just
1564 * exiting. So we should place a read memory barrier when we avoid the lock.
1565 * On the writer side, write memory barrier is implied in __exit_signal
1566 * as __exit_signal releases the siglock spinlock after updating the signal->
1567 * fields. But we don't do this yet to keep things simple.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001568 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 */
1570
Frank Mayharf06febc2008-09-12 09:54:39 -07001571static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001572{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001573 r->ru_nvcsw += t->nvcsw;
1574 r->ru_nivcsw += t->nivcsw;
1575 r->ru_minflt += t->min_flt;
1576 r->ru_majflt += t->maj_flt;
1577 r->ru_inblock += task_io_get_inblock(t);
1578 r->ru_oublock += task_io_get_oublock(t);
1579}
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1582{
1583 struct task_struct *t;
1584 unsigned long flags;
1585 cputime_t utime, stime;
Frank Mayharf06febc2008-09-12 09:54:39 -07001586 struct task_cputime cputime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 memset((char *) r, 0, sizeof *r);
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001589 utime = stime = cputime_zero;
1590
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001591 if (who == RUSAGE_THREAD) {
Frank Mayharf06febc2008-09-12 09:54:39 -07001592 accumulate_thread_rusage(p, r);
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001593 goto out;
1594 }
1595
Oleg Nesterovd6cf7232008-04-30 00:52:38 -07001596 if (!lock_task_sighand(p, &flags))
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001597 return;
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 switch (who) {
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001600 case RUSAGE_BOTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 case RUSAGE_CHILDREN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 utime = p->signal->cutime;
1603 stime = p->signal->cstime;
1604 r->ru_nvcsw = p->signal->cnvcsw;
1605 r->ru_nivcsw = p->signal->cnivcsw;
1606 r->ru_minflt = p->signal->cmin_flt;
1607 r->ru_majflt = p->signal->cmaj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07001608 r->ru_inblock = p->signal->cinblock;
1609 r->ru_oublock = p->signal->coublock;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001610
1611 if (who == RUSAGE_CHILDREN)
1612 break;
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 case RUSAGE_SELF:
Frank Mayharf06febc2008-09-12 09:54:39 -07001615 thread_group_cputime(p, &cputime);
1616 utime = cputime_add(utime, cputime.utime);
1617 stime = cputime_add(stime, cputime.stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 r->ru_nvcsw += p->signal->nvcsw;
1619 r->ru_nivcsw += p->signal->nivcsw;
1620 r->ru_minflt += p->signal->min_flt;
1621 r->ru_majflt += p->signal->maj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07001622 r->ru_inblock += p->signal->inblock;
1623 r->ru_oublock += p->signal->oublock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 t = p;
1625 do {
Frank Mayharf06febc2008-09-12 09:54:39 -07001626 accumulate_thread_rusage(t, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 t = next_thread(t);
1628 } while (t != p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 break;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 default:
1632 BUG();
1633 }
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001634 unlock_task_sighand(p, &flags);
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001635
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001636out:
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001637 cputime_to_timeval(utime, &r->ru_utime);
1638 cputime_to_timeval(stime, &r->ru_stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
1641int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1642{
1643 struct rusage r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 k_getrusage(p, who, &r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1646}
1647
1648asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
1649{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001650 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1651 who != RUSAGE_THREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return -EINVAL;
1653 return getrusage(current, who, ru);
1654}
1655
1656asmlinkage long sys_umask(int mask)
1657{
1658 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1659 return mask;
1660}
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -08001661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
1663 unsigned long arg4, unsigned long arg5)
1664{
David Howellsb6dff3e2008-11-14 10:39:16 +11001665 struct task_struct *me = current;
1666 unsigned char comm[sizeof(me->comm)];
1667 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -07001669 if (security_task_prctl(option, arg2, arg3, arg4, arg5, &error))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return error;
1671
1672 switch (option) {
1673 case PR_SET_PDEATHSIG:
Jesper Juhl0730ded2005-09-06 15:17:37 -07001674 if (!valid_signal(arg2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 error = -EINVAL;
1676 break;
1677 }
David Howellsb6dff3e2008-11-14 10:39:16 +11001678 me->pdeath_signal = arg2;
1679 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 break;
1681 case PR_GET_PDEATHSIG:
David Howellsb6dff3e2008-11-14 10:39:16 +11001682 error = put_user(me->pdeath_signal, (int __user *)arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 break;
1684 case PR_GET_DUMPABLE:
David Howellsb6dff3e2008-11-14 10:39:16 +11001685 error = get_dumpable(me->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 break;
1687 case PR_SET_DUMPABLE:
Marcel Holtmannabf75a52006-07-12 13:12:00 +02001688 if (arg2 < 0 || arg2 > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 error = -EINVAL;
1690 break;
1691 }
David Howellsb6dff3e2008-11-14 10:39:16 +11001692 set_dumpable(me->mm, arg2);
1693 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 break;
1695
1696 case PR_SET_UNALIGN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001697 error = SET_UNALIGN_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 break;
1699 case PR_GET_UNALIGN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001700 error = GET_UNALIGN_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 break;
1702 case PR_SET_FPEMU:
David Howellsb6dff3e2008-11-14 10:39:16 +11001703 error = SET_FPEMU_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 break;
1705 case PR_GET_FPEMU:
David Howellsb6dff3e2008-11-14 10:39:16 +11001706 error = GET_FPEMU_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 break;
1708 case PR_SET_FPEXC:
David Howellsb6dff3e2008-11-14 10:39:16 +11001709 error = SET_FPEXC_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 break;
1711 case PR_GET_FPEXC:
David Howellsb6dff3e2008-11-14 10:39:16 +11001712 error = GET_FPEXC_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 break;
1714 case PR_GET_TIMING:
1715 error = PR_TIMING_STATISTICAL;
1716 break;
1717 case PR_SET_TIMING:
Shi Weihua7b266552008-05-23 13:04:59 -07001718 if (arg2 != PR_TIMING_STATISTICAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 error = -EINVAL;
David Howellsb6dff3e2008-11-14 10:39:16 +11001720 else
1721 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 break;
1723
David Howellsb6dff3e2008-11-14 10:39:16 +11001724 case PR_SET_NAME:
1725 comm[sizeof(me->comm)-1] = 0;
1726 if (strncpy_from_user(comm, (char __user *)arg2,
1727 sizeof(me->comm) - 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 return -EFAULT;
David Howellsb6dff3e2008-11-14 10:39:16 +11001729 set_task_comm(me, comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return 0;
David Howellsb6dff3e2008-11-14 10:39:16 +11001731 case PR_GET_NAME:
1732 get_task_comm(comm, me);
1733 if (copy_to_user((char __user *)arg2, comm,
1734 sizeof(comm)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return -EFAULT;
1736 return 0;
Anton Blanchard651d7652006-06-07 16:10:19 +10001737 case PR_GET_ENDIAN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001738 error = GET_ENDIAN(me, arg2);
Anton Blanchard651d7652006-06-07 16:10:19 +10001739 break;
1740 case PR_SET_ENDIAN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001741 error = SET_ENDIAN(me, arg2);
Anton Blanchard651d7652006-06-07 16:10:19 +10001742 break;
1743
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -07001744 case PR_GET_SECCOMP:
1745 error = prctl_get_seccomp();
1746 break;
1747 case PR_SET_SECCOMP:
1748 error = prctl_set_seccomp(arg2);
1749 break;
Erik Bosman8fb402b2008-04-11 18:54:17 +02001750 case PR_GET_TSC:
1751 error = GET_TSC_CTL(arg2);
1752 break;
1753 case PR_SET_TSC:
1754 error = SET_TSC_CTL(arg2);
1755 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 default:
1768 error = -EINVAL;
1769 break;
1770 }
1771 return error;
1772}
Andi Kleen3cfc3482006-09-26 10:52:28 +02001773
1774asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
Ingo Molnar4307d1e2007-11-07 18:37:48 +01001775 struct getcpu_cache __user *unused)
Andi Kleen3cfc3482006-09-26 10:52:28 +02001776{
1777 int err = 0;
1778 int cpu = raw_smp_processor_id();
1779 if (cpup)
1780 err |= put_user(cpu, cpup);
1781 if (nodep)
1782 err |= put_user(cpu_to_node(cpu), nodep);
Andi Kleen3cfc3482006-09-26 10:52:28 +02001783 return err ? -EFAULT : 0;
1784}
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07001785
1786char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
1787
1788static void argv_cleanup(char **argv, char **envp)
1789{
1790 argv_free(argv);
1791}
1792
1793/**
1794 * orderly_poweroff - Trigger an orderly system poweroff
1795 * @force: force poweroff if command execution fails
1796 *
1797 * This may be called from any context to trigger a system shutdown.
1798 * If the orderly shutdown fails, it will force an immediate shutdown.
1799 */
1800int orderly_poweroff(bool force)
1801{
1802 int argc;
1803 char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
1804 static char *envp[] = {
1805 "HOME=/",
1806 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
1807 NULL
1808 };
1809 int ret = -ENOMEM;
1810 struct subprocess_info *info;
1811
1812 if (argv == NULL) {
1813 printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
1814 __func__, poweroff_cmd);
1815 goto out;
1816 }
1817
KOSAKI Motohiroac331d12008-07-25 01:45:38 -07001818 info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07001819 if (info == NULL) {
1820 argv_free(argv);
1821 goto out;
1822 }
1823
1824 call_usermodehelper_setcleanup(info, argv_cleanup);
1825
Jeremy Fitzhardinge86313c42007-07-17 18:37:03 -07001826 ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07001827
1828 out:
1829 if (ret && force) {
1830 printk(KERN_WARNING "Failed to start orderly shutdown: "
1831 "forcing the issue\n");
1832
1833 /* I guess this should try to kick off some daemon to
1834 sync and poweroff asap. Or not even bother syncing
1835 if we're doing an emergency shutdown? */
1836 emergency_sync();
1837 kernel_power_off();
1838 }
1839
1840 return ret;
1841}
1842EXPORT_SYMBOL_GPL(orderly_poweroff);