blob: ab735040468a74a6c81611df46fd97f9fc081fa2 [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 Howellsd84f4f92008-11-14 10:39:23 +1100183 user = (struct user_struct *) 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 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 */
531asmlinkage long sys_setgid(gid_t gid)
532{
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}
560
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
David Howellsd84f4f92008-11-14 10:39:23 +1100568 new_user = alloc_uid(current->nsproxy->user_ns, new->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (!new_user)
570 return -EAGAIN;
571
572 if (atomic_read(&new_user->processes) >=
573 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
Cedric Le Goateracce2922007-07-15 23:40:59 -0700574 new_user != current->nsproxy->user_ns->root_user) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 free_uid(new_user);
576 return -EAGAIN;
577 }
578
David Howellsd84f4f92008-11-14 10:39:23 +1100579 free_uid(new->user);
580 new->user = new_user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return 0;
582}
583
584/*
585 * Unprivileged users may change the real uid to the effective uid
586 * or vice versa. (BSD-style)
587 *
588 * If you set the real uid at all, or set the effective uid to a value not
589 * equal to the real uid, then the saved uid is set to the new effective uid.
590 *
591 * This makes it possible for a setuid program to completely drop its
592 * privileges, which is often a useful assertion to make when you are doing
593 * a security audit over a program.
594 *
595 * The general idea is that a program which uses just setreuid() will be
596 * 100% compatible with BSD. A program which uses just setuid() will be
597 * 100% compatible with POSIX with saved IDs.
598 */
599asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
600{
David Howellsd84f4f92008-11-14 10:39:23 +1100601 const struct cred *old;
602 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 int retval;
604
David Howellsd84f4f92008-11-14 10:39:23 +1100605 new = prepare_creds();
606 if (!new)
607 return -ENOMEM;
608 old = current_cred();
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
611 if (retval)
David Howellsd84f4f92008-11-14 10:39:23 +1100612 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
David Howellsd84f4f92008-11-14 10:39:23 +1100614 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (ruid != (uid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100616 new->uid = ruid;
617 if (old->uid != ruid &&
618 old->euid != ruid &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 !capable(CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100620 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
623 if (euid != (uid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100624 new->euid = euid;
625 if (old->uid != euid &&
626 old->euid != euid &&
627 old->suid != euid &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 !capable(CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100629 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631
David Howellsd84f4f92008-11-14 10:39:23 +1100632 retval = -EAGAIN;
633 if (new->uid != old->uid && set_user(new) < 0)
634 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (ruid != (uid_t) -1 ||
David Howellsd84f4f92008-11-14 10:39:23 +1100637 (euid != (uid_t) -1 && euid != old->uid))
638 new->suid = new->euid;
639 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
David Howellsd84f4f92008-11-14 10:39:23 +1100641 retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
642 if (retval < 0)
643 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
David Howellsd84f4f92008-11-14 10:39:23 +1100645 return commit_creds(new);
646
647error:
648 abort_creds(new);
649 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652/*
653 * setuid() is implemented like SysV with SAVED_IDS
654 *
655 * Note that SAVED_ID's is deficient in that a setuid root program
656 * like sendmail, for example, cannot set its uid to be a normal
657 * user and then switch back, because if you're root, setuid() sets
658 * the saved uid too. If you don't like this, blame the bright people
659 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
660 * will allow a root program to temporarily drop privileges and be able to
661 * regain them by swapping the real and effective uid.
662 */
663asmlinkage long sys_setuid(uid_t uid)
664{
David Howellsd84f4f92008-11-14 10:39:23 +1100665 const struct cred *old;
666 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 int retval;
668
David Howellsd84f4f92008-11-14 10:39:23 +1100669 new = prepare_creds();
670 if (!new)
671 return -ENOMEM;
672 old = current_cred();
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
675 if (retval)
David Howellsd84f4f92008-11-14 10:39:23 +1100676 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
David Howellsd84f4f92008-11-14 10:39:23 +1100678 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (capable(CAP_SETUID)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100680 new->suid = new->uid = uid;
681 if (uid != old->uid && set_user(new) < 0) {
682 retval = -EAGAIN;
683 goto error;
684 }
685 } else if (uid != old->uid && uid != new->suid) {
686 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
David Howellsd84f4f92008-11-14 10:39:23 +1100689 new->fsuid = new->euid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
David Howellsd84f4f92008-11-14 10:39:23 +1100691 retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
692 if (retval < 0)
693 goto error;
694
695 return commit_creds(new);
696
697error:
698 abort_creds(new);
699 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702
703/*
704 * This function implements a generic ability to update ruid, euid,
705 * and suid. This allows you to implement the 4.4 compatible seteuid().
706 */
707asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
708{
David Howellsd84f4f92008-11-14 10:39:23 +1100709 const struct cred *old;
710 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 int retval;
712
David Howellsd84f4f92008-11-14 10:39:23 +1100713 new = prepare_creds();
714 if (!new)
715 return -ENOMEM;
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
718 if (retval)
David Howellsd84f4f92008-11-14 10:39:23 +1100719 goto error;
720 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
David Howellsd84f4f92008-11-14 10:39:23 +1100722 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (!capable(CAP_SETUID)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100724 if (ruid != (uid_t) -1 && ruid != old->uid &&
725 ruid != old->euid && ruid != old->suid)
726 goto error;
727 if (euid != (uid_t) -1 && euid != old->uid &&
728 euid != old->euid && euid != old->suid)
729 goto error;
730 if (suid != (uid_t) -1 && suid != old->uid &&
731 suid != old->euid && suid != old->suid)
732 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
David Howellsd84f4f92008-11-14 10:39:23 +1100734
735 retval = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (ruid != (uid_t) -1) {
David Howellsd84f4f92008-11-14 10:39:23 +1100737 new->uid = ruid;
738 if (ruid != old->uid && set_user(new) < 0)
739 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
David Howellsd84f4f92008-11-14 10:39:23 +1100741 if (euid != (uid_t) -1)
742 new->euid = euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (suid != (uid_t) -1)
David Howellsd84f4f92008-11-14 10:39:23 +1100744 new->suid = suid;
745 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
David Howellsd84f4f92008-11-14 10:39:23 +1100747 retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
748 if (retval < 0)
749 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
David Howellsd84f4f92008-11-14 10:39:23 +1100751 return commit_creds(new);
752
753error:
754 abort_creds(new);
755 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
758asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
759{
David Howells86a264a2008-11-14 10:39:18 +1100760 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 int retval;
762
David Howells86a264a2008-11-14 10:39:18 +1100763 if (!(retval = put_user(cred->uid, ruid)) &&
764 !(retval = put_user(cred->euid, euid)))
David Howellsb6dff3e2008-11-14 10:39:16 +1100765 retval = put_user(cred->suid, suid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 return retval;
768}
769
770/*
771 * Same as above, but for rgid, egid, sgid.
772 */
773asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
774{
David Howellsd84f4f92008-11-14 10:39:23 +1100775 const struct cred *old;
776 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int retval;
778
David Howellsd84f4f92008-11-14 10:39:23 +1100779 new = prepare_creds();
780 if (!new)
781 return -ENOMEM;
782 old = current_cred();
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
785 if (retval)
David Howellsd84f4f92008-11-14 10:39:23 +1100786 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
David Howellsd84f4f92008-11-14 10:39:23 +1100788 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (!capable(CAP_SETGID)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100790 if (rgid != (gid_t) -1 && rgid != old->gid &&
791 rgid != old->egid && rgid != old->sgid)
792 goto error;
793 if (egid != (gid_t) -1 && egid != old->gid &&
794 egid != old->egid && egid != old->sgid)
795 goto error;
796 if (sgid != (gid_t) -1 && sgid != old->gid &&
797 sgid != old->egid && sgid != old->sgid)
798 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
David Howellsd84f4f92008-11-14 10:39:23 +1100801 if (rgid != (gid_t) -1)
802 new->gid = rgid;
803 if (egid != (gid_t) -1)
804 new->egid = egid;
805 if (sgid != (gid_t) -1)
806 new->sgid = sgid;
807 new->fsgid = new->egid;
808
809 return commit_creds(new);
810
811error:
812 abort_creds(new);
813 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
816asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
817{
David Howells86a264a2008-11-14 10:39:18 +1100818 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 int retval;
820
David Howells86a264a2008-11-14 10:39:18 +1100821 if (!(retval = put_user(cred->gid, rgid)) &&
822 !(retval = put_user(cred->egid, egid)))
David Howellsb6dff3e2008-11-14 10:39:16 +1100823 retval = put_user(cred->sgid, sgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 return retval;
826}
827
828
829/*
830 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
831 * is used for "access()" and for the NFS daemon (letting nfsd stay at
832 * whatever uid it wants to). It normally shadows "euid", except when
833 * explicitly set by setfsuid() or for access..
834 */
835asmlinkage long sys_setfsuid(uid_t uid)
836{
David Howellsd84f4f92008-11-14 10:39:23 +1100837 const struct cred *old;
838 struct cred *new;
839 uid_t old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
David Howellsd84f4f92008-11-14 10:39:23 +1100841 new = prepare_creds();
842 if (!new)
843 return current_fsuid();
844 old = current_cred();
845 old_fsuid = old->fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
David Howellsd84f4f92008-11-14 10:39:23 +1100847 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS) < 0)
848 goto error;
849
850 if (uid == old->uid || uid == old->euid ||
851 uid == old->suid || uid == old->fsuid ||
Cal Peake756184b2006-09-30 23:27:24 -0700852 capable(CAP_SETUID)) {
853 if (uid != old_fsuid) {
David Howellsd84f4f92008-11-14 10:39:23 +1100854 new->fsuid = uid;
855 if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
856 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
859
David Howellsd84f4f92008-11-14 10:39:23 +1100860error:
861 abort_creds(new);
862 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
David Howellsd84f4f92008-11-14 10:39:23 +1100864change_okay:
865 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return old_fsuid;
867}
868
869/*
John Anthony Kazos Jrf42df9e2007-05-09 08:23:08 +0200870 * Samma på svenska..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 */
872asmlinkage long sys_setfsgid(gid_t gid)
873{
David Howellsd84f4f92008-11-14 10:39:23 +1100874 const struct cred *old;
875 struct cred *new;
876 gid_t old_fsgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
David Howellsd84f4f92008-11-14 10:39:23 +1100878 new = prepare_creds();
879 if (!new)
880 return current_fsgid();
881 old = current_cred();
882 old_fsgid = old->fsgid;
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
David Howellsd84f4f92008-11-14 10:39:23 +1100885 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
David Howellsd84f4f92008-11-14 10:39:23 +1100887 if (gid == old->gid || gid == old->egid ||
888 gid == old->sgid || gid == old->fsgid ||
Cal Peake756184b2006-09-30 23:27:24 -0700889 capable(CAP_SETGID)) {
890 if (gid != old_fsgid) {
David Howellsd84f4f92008-11-14 10:39:23 +1100891 new->fsgid = gid;
892 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
David Howellsd84f4f92008-11-14 10:39:23 +1100895
896error:
897 abort_creds(new);
898 return old_fsgid;
899
900change_okay:
901 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return old_fsgid;
903}
904
Frank Mayharf06febc2008-09-12 09:54:39 -0700905void do_sys_times(struct tms *tms)
906{
907 struct task_cputime cputime;
908 cputime_t cutime, cstime;
909
910 spin_lock_irq(&current->sighand->siglock);
911 thread_group_cputime(current, &cputime);
912 cutime = current->signal->cutime;
913 cstime = current->signal->cstime;
914 spin_unlock_irq(&current->sighand->siglock);
915 tms->tms_utime = cputime_to_clock_t(cputime.utime);
916 tms->tms_stime = cputime_to_clock_t(cputime.stime);
917 tms->tms_cutime = cputime_to_clock_t(cutime);
918 tms->tms_cstime = cputime_to_clock_t(cstime);
919}
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921asmlinkage long sys_times(struct tms __user * tbuf)
922{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (tbuf) {
924 struct tms tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Frank Mayharf06febc2008-09-12 09:54:39 -0700926 do_sys_times(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
928 return -EFAULT;
929 }
930 return (long) jiffies_64_to_clock_t(get_jiffies_64());
931}
932
933/*
934 * This needs some heavy checking ...
935 * I just haven't the stomach for it. I also don't fully
936 * understand sessions/pgrp etc. Let somebody who does explain it.
937 *
938 * OK, I think I have the protection semantics right.... this is really
939 * only important on a multi-user system anyway, to make sure one user
940 * can't send a signal to a process owned by another. -TYT, 12/12/91
941 *
942 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
943 * LBT 04.03.94
944 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
946{
947 struct task_struct *p;
Oleg Nesterovee0acf92006-01-08 01:03:53 -0800948 struct task_struct *group_leader = current->group_leader;
Oleg Nesterov4e021302008-02-08 04:19:08 -0800949 struct pid *pgrp;
950 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 if (!pid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700953 pid = task_pid_vnr(group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (!pgid)
955 pgid = pid;
956 if (pgid < 0)
957 return -EINVAL;
958
959 /* From this point forward we keep holding onto the tasklist lock
960 * so that our parent does not change from under us. -DaveM
961 */
962 write_lock_irq(&tasklist_lock);
963
964 err = -ESRCH;
Oleg Nesterov4e021302008-02-08 04:19:08 -0800965 p = find_task_by_vpid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (!p)
967 goto out;
968
969 err = -EINVAL;
970 if (!thread_group_leader(p))
971 goto out;
972
Oleg Nesterov4e021302008-02-08 04:19:08 -0800973 if (same_thread_group(p->real_parent, group_leader)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 err = -EPERM;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800975 if (task_session(p) != task_session(group_leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 goto out;
977 err = -EACCES;
978 if (p->did_exec)
979 goto out;
980 } else {
981 err = -ESRCH;
Oleg Nesterovee0acf92006-01-08 01:03:53 -0800982 if (p != group_leader)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 goto out;
984 }
985
986 err = -EPERM;
987 if (p->signal->leader)
988 goto out;
989
Oleg Nesterov4e021302008-02-08 04:19:08 -0800990 pgrp = task_pid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (pgid != pid) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700992 struct task_struct *g;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Oleg Nesterov4e021302008-02-08 04:19:08 -0800994 pgrp = find_vpid(pgid);
995 g = pid_task(pgrp, PIDTYPE_PGID);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800996 if (!g || task_session(g) != task_session(group_leader))
Oleg Nesterovf020bc42006-12-08 02:38:02 -0800997 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 err = security_task_setpgid(p, pgid);
1001 if (err)
1002 goto out;
1003
Oleg Nesterov4e021302008-02-08 04:19:08 -08001004 if (task_pgrp(p) != pgrp) {
Oleg Nesterov83beaf32008-04-30 00:54:27 -07001005 change_pid(p, PIDTYPE_PGID, pgrp);
Oleg Nesterov4e021302008-02-08 04:19:08 -08001006 set_task_pgrp(p, pid_nr(pgrp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008
1009 err = 0;
1010out:
1011 /* All paths lead to here, thus we are safe. -DaveM */
1012 write_unlock_irq(&tasklist_lock);
1013 return err;
1014}
1015
1016asmlinkage long sys_getpgid(pid_t pid)
1017{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001018 struct task_struct *p;
1019 struct pid *grp;
1020 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001022 rcu_read_lock();
1023 if (!pid)
1024 grp = task_pgrp(current);
1025 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 retval = -ESRCH;
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001027 p = find_task_by_vpid(pid);
1028 if (!p)
1029 goto out;
1030 grp = task_pgrp(p);
1031 if (!grp)
1032 goto out;
1033
1034 retval = security_task_getpgid(p);
1035 if (retval)
1036 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001038 retval = pid_vnr(grp);
1039out:
1040 rcu_read_unlock();
1041 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044#ifdef __ARCH_WANT_SYS_GETPGRP
1045
1046asmlinkage long sys_getpgrp(void)
1047{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001048 return sys_getpgid(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051#endif
1052
1053asmlinkage long sys_getsid(pid_t pid)
1054{
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001055 struct task_struct *p;
1056 struct pid *sid;
1057 int retval;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001058
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001059 rcu_read_lock();
1060 if (!pid)
1061 sid = task_session(current);
1062 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 retval = -ESRCH;
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001064 p = find_task_by_vpid(pid);
1065 if (!p)
1066 goto out;
1067 sid = task_session(p);
1068 if (!sid)
1069 goto out;
1070
1071 retval = security_task_getsid(p);
1072 if (retval)
1073 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001075 retval = pid_vnr(sid);
1076out:
1077 rcu_read_unlock();
1078 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
1081asmlinkage long sys_setsid(void)
1082{
Oren Laadane19f2472006-01-08 01:03:58 -08001083 struct task_struct *group_leader = current->group_leader;
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001084 struct pid *sid = task_pid(group_leader);
1085 pid_t session = pid_vnr(sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 int err = -EPERM;
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 write_lock_irq(&tasklist_lock);
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001089 /* Fail if I am already a session leader */
1090 if (group_leader->signal->leader)
1091 goto out;
1092
Oleg Nesterov430c6232008-02-08 04:19:11 -08001093 /* Fail if a process group id already exists that equals the
1094 * proposed session id.
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001095 */
Oleg Nesterov6806aac2008-02-08 04:19:12 -08001096 if (pid_task(sid, PIDTYPE_PGID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 goto out;
1098
Oren Laadane19f2472006-01-08 01:03:58 -08001099 group_leader->signal->leader = 1;
Oleg Nesterov8520d7c2008-02-08 04:19:09 -08001100 __set_special_pids(sid);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001101
Alan Cox9c9f4de2008-10-13 10:37:26 +01001102 proc_clear_tty(group_leader);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001103
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001104 err = session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105out:
1106 write_unlock_irq(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return err;
1108}
1109
1110/*
1111 * Supplementary group IDs
1112 */
1113
1114/* init to 2 - one for init_task, one to ensure it is never freed */
1115struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1116
1117struct group_info *groups_alloc(int gidsetsize)
1118{
1119 struct group_info *group_info;
1120 int nblocks;
1121 int i;
1122
1123 nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1124 /* Make sure we always allocate at least one indirect block pointer */
1125 nblocks = nblocks ? : 1;
1126 group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1127 if (!group_info)
1128 return NULL;
1129 group_info->ngroups = gidsetsize;
1130 group_info->nblocks = nblocks;
1131 atomic_set(&group_info->usage, 1);
1132
Cal Peake756184b2006-09-30 23:27:24 -07001133 if (gidsetsize <= NGROUPS_SMALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 group_info->blocks[0] = group_info->small_block;
Cal Peake756184b2006-09-30 23:27:24 -07001135 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 for (i = 0; i < nblocks; i++) {
1137 gid_t *b;
1138 b = (void *)__get_free_page(GFP_USER);
1139 if (!b)
1140 goto out_undo_partial_alloc;
1141 group_info->blocks[i] = b;
1142 }
1143 }
1144 return group_info;
1145
1146out_undo_partial_alloc:
1147 while (--i >= 0) {
1148 free_page((unsigned long)group_info->blocks[i]);
1149 }
1150 kfree(group_info);
1151 return NULL;
1152}
1153
1154EXPORT_SYMBOL(groups_alloc);
1155
1156void groups_free(struct group_info *group_info)
1157{
1158 if (group_info->blocks[0] != group_info->small_block) {
1159 int i;
1160 for (i = 0; i < group_info->nblocks; i++)
1161 free_page((unsigned long)group_info->blocks[i]);
1162 }
1163 kfree(group_info);
1164}
1165
1166EXPORT_SYMBOL(groups_free);
1167
1168/* export the group_info to a user-space array */
1169static int groups_to_user(gid_t __user *grouplist,
David Howellsd84f4f92008-11-14 10:39:23 +11001170 const struct group_info *group_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 int i;
Eric Dumazet1bf47342008-02-06 01:37:56 -08001173 unsigned int count = group_info->ngroups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 for (i = 0; i < group_info->nblocks; i++) {
Eric Dumazet1bf47342008-02-06 01:37:56 -08001176 unsigned int cp_count = min(NGROUPS_PER_BLOCK, count);
1177 unsigned int len = cp_count * sizeof(*grouplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Eric Dumazet1bf47342008-02-06 01:37:56 -08001179 if (copy_to_user(grouplist, group_info->blocks[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return -EFAULT;
1181
Eric Dumazet1bf47342008-02-06 01:37:56 -08001182 grouplist += NGROUPS_PER_BLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 count -= cp_count;
1184 }
1185 return 0;
1186}
1187
1188/* fill a group_info from a user-space array - it must be allocated already */
1189static int groups_from_user(struct group_info *group_info,
1190 gid_t __user *grouplist)
Cal Peake756184b2006-09-30 23:27:24 -07001191{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 int i;
Eric Dumazet1bf47342008-02-06 01:37:56 -08001193 unsigned int count = group_info->ngroups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 for (i = 0; i < group_info->nblocks; i++) {
Eric Dumazet1bf47342008-02-06 01:37:56 -08001196 unsigned int cp_count = min(NGROUPS_PER_BLOCK, count);
1197 unsigned int len = cp_count * sizeof(*grouplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Eric Dumazet1bf47342008-02-06 01:37:56 -08001199 if (copy_from_user(group_info->blocks[i], grouplist, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return -EFAULT;
1201
Eric Dumazet1bf47342008-02-06 01:37:56 -08001202 grouplist += NGROUPS_PER_BLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 count -= cp_count;
1204 }
1205 return 0;
1206}
1207
Domen Puncerebe8b542005-05-05 16:16:19 -07001208/* a simple Shell sort */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209static void groups_sort(struct group_info *group_info)
1210{
1211 int base, max, stride;
1212 int gidsetsize = group_info->ngroups;
1213
1214 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1215 ; /* nothing */
1216 stride /= 3;
1217
1218 while (stride) {
1219 max = gidsetsize - stride;
1220 for (base = 0; base < max; base++) {
1221 int left = base;
1222 int right = left + stride;
1223 gid_t tmp = GROUP_AT(group_info, right);
1224
1225 while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1226 GROUP_AT(group_info, right) =
1227 GROUP_AT(group_info, left);
1228 right = left;
1229 left -= stride;
1230 }
1231 GROUP_AT(group_info, right) = tmp;
1232 }
1233 stride /= 3;
1234 }
1235}
1236
1237/* a simple bsearch */
David Howells86a264a2008-11-14 10:39:18 +11001238int groups_search(const struct group_info *group_info, gid_t grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Eric Dumazetd74beb9f2006-03-25 03:08:19 -08001240 unsigned int left, right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 if (!group_info)
1243 return 0;
1244
1245 left = 0;
1246 right = group_info->ngroups;
1247 while (left < right) {
Eric Dumazetd74beb9f2006-03-25 03:08:19 -08001248 unsigned int mid = (left+right)/2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 int cmp = grp - GROUP_AT(group_info, mid);
1250 if (cmp > 0)
1251 left = mid + 1;
1252 else if (cmp < 0)
1253 right = mid;
1254 else
1255 return 1;
1256 }
1257 return 0;
1258}
1259
David Howellsb6dff3e2008-11-14 10:39:16 +11001260/**
David Howellsd84f4f92008-11-14 10:39:23 +11001261 * set_groups - Change a group subscription in a set of credentials
1262 * @new: The newly prepared set of credentials to alter
1263 * @group_info: The group list to install
David Howellsb6dff3e2008-11-14 10:39:16 +11001264 *
David Howellsd84f4f92008-11-14 10:39:23 +11001265 * Validate a group subscription and, if valid, insert it into a set
1266 * of credentials.
David Howellsb6dff3e2008-11-14 10:39:16 +11001267 */
David Howellsd84f4f92008-11-14 10:39:23 +11001268int set_groups(struct cred *new, struct group_info *group_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 retval = security_task_setgroups(group_info);
1273 if (retval)
1274 return retval;
1275
David Howellsd84f4f92008-11-14 10:39:23 +11001276 put_group_info(new->group_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 groups_sort(group_info);
1278 get_group_info(group_info);
David Howellsd84f4f92008-11-14 10:39:23 +11001279 new->group_info = group_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return 0;
1281}
1282
David Howellsb6dff3e2008-11-14 10:39:16 +11001283EXPORT_SYMBOL(set_groups);
1284
1285/**
1286 * set_current_groups - Change current's group subscription
1287 * @group_info: The group list to impose
1288 *
1289 * Validate a group subscription and, if valid, impose it upon current's task
1290 * security record.
1291 */
1292int set_current_groups(struct group_info *group_info)
1293{
David Howellsd84f4f92008-11-14 10:39:23 +11001294 struct cred *new;
1295 int ret;
1296
1297 new = prepare_creds();
1298 if (!new)
1299 return -ENOMEM;
1300
1301 ret = set_groups(new, group_info);
1302 if (ret < 0) {
1303 abort_creds(new);
1304 return ret;
1305 }
1306
1307 return commit_creds(new);
David Howellsb6dff3e2008-11-14 10:39:16 +11001308}
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310EXPORT_SYMBOL(set_current_groups);
1311
1312asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1313{
David Howells86a264a2008-11-14 10:39:18 +11001314 const struct cred *cred = current_cred();
1315 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 if (gidsetsize < 0)
1318 return -EINVAL;
1319
1320 /* no need to grab task_lock here; it cannot change */
David Howellsb6dff3e2008-11-14 10:39:16 +11001321 i = cred->group_info->ngroups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (gidsetsize) {
1323 if (i > gidsetsize) {
1324 i = -EINVAL;
1325 goto out;
1326 }
David Howellsb6dff3e2008-11-14 10:39:16 +11001327 if (groups_to_user(grouplist, cred->group_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 i = -EFAULT;
1329 goto out;
1330 }
1331 }
1332out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return i;
1334}
1335
1336/*
1337 * SMP: Our groups are copy-on-write. We can set them safely
1338 * without another task interfering.
1339 */
1340
1341asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1342{
1343 struct group_info *group_info;
1344 int retval;
1345
1346 if (!capable(CAP_SETGID))
1347 return -EPERM;
1348 if ((unsigned)gidsetsize > NGROUPS_MAX)
1349 return -EINVAL;
1350
1351 group_info = groups_alloc(gidsetsize);
1352 if (!group_info)
1353 return -ENOMEM;
1354 retval = groups_from_user(group_info, grouplist);
1355 if (retval) {
1356 put_group_info(group_info);
1357 return retval;
1358 }
1359
1360 retval = set_current_groups(group_info);
1361 put_group_info(group_info);
1362
1363 return retval;
1364}
1365
1366/*
1367 * Check whether we're fsgid/egid or in the supplemental group..
1368 */
1369int in_group_p(gid_t grp)
1370{
David Howells86a264a2008-11-14 10:39:18 +11001371 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 int retval = 1;
David Howells86a264a2008-11-14 10:39:18 +11001373
David Howellsb6dff3e2008-11-14 10:39:16 +11001374 if (grp != cred->fsgid)
1375 retval = groups_search(cred->group_info, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return retval;
1377}
1378
1379EXPORT_SYMBOL(in_group_p);
1380
1381int in_egroup_p(gid_t grp)
1382{
David Howells86a264a2008-11-14 10:39:18 +11001383 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 int retval = 1;
David Howells86a264a2008-11-14 10:39:18 +11001385
David Howellsb6dff3e2008-11-14 10:39:16 +11001386 if (grp != cred->egid)
1387 retval = groups_search(cred->group_info, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return retval;
1389}
1390
1391EXPORT_SYMBOL(in_egroup_p);
1392
1393DECLARE_RWSEM(uts_sem);
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395asmlinkage long sys_newuname(struct new_utsname __user * name)
1396{
1397 int errno = 0;
1398
1399 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001400 if (copy_to_user(name, utsname(), sizeof *name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 errno = -EFAULT;
1402 up_read(&uts_sem);
1403 return errno;
1404}
1405
1406asmlinkage long sys_sethostname(char __user *name, int len)
1407{
1408 int errno;
1409 char tmp[__NEW_UTS_LEN];
1410
1411 if (!capable(CAP_SYS_ADMIN))
1412 return -EPERM;
1413 if (len < 0 || len > __NEW_UTS_LEN)
1414 return -EINVAL;
1415 down_write(&uts_sem);
1416 errno = -EFAULT;
1417 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001418 struct new_utsname *u = utsname();
1419
1420 memcpy(u->nodename, tmp, len);
1421 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 errno = 0;
1423 }
1424 up_write(&uts_sem);
1425 return errno;
1426}
1427
1428#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1429
1430asmlinkage long sys_gethostname(char __user *name, int len)
1431{
1432 int i, errno;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001433 struct new_utsname *u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 if (len < 0)
1436 return -EINVAL;
1437 down_read(&uts_sem);
Andrew Morton9679e4d2008-10-15 22:01:51 -07001438 u = utsname();
1439 i = 1 + strlen(u->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (i > len)
1441 i = len;
1442 errno = 0;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001443 if (copy_to_user(name, u->nodename, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 errno = -EFAULT;
1445 up_read(&uts_sem);
1446 return errno;
1447}
1448
1449#endif
1450
1451/*
1452 * Only setdomainname; getdomainname can be implemented by calling
1453 * uname()
1454 */
1455asmlinkage long sys_setdomainname(char __user *name, int len)
1456{
1457 int errno;
1458 char tmp[__NEW_UTS_LEN];
1459
1460 if (!capable(CAP_SYS_ADMIN))
1461 return -EPERM;
1462 if (len < 0 || len > __NEW_UTS_LEN)
1463 return -EINVAL;
1464
1465 down_write(&uts_sem);
1466 errno = -EFAULT;
1467 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001468 struct new_utsname *u = utsname();
1469
1470 memcpy(u->domainname, tmp, len);
1471 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 errno = 0;
1473 }
1474 up_write(&uts_sem);
1475 return errno;
1476}
1477
1478asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1479{
1480 if (resource >= RLIM_NLIMITS)
1481 return -EINVAL;
1482 else {
1483 struct rlimit value;
1484 task_lock(current->group_leader);
1485 value = current->signal->rlim[resource];
1486 task_unlock(current->group_leader);
1487 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1488 }
1489}
1490
1491#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1492
1493/*
1494 * Back compatibility for getrlimit. Needed for some apps.
1495 */
1496
1497asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1498{
1499 struct rlimit x;
1500 if (resource >= RLIM_NLIMITS)
1501 return -EINVAL;
1502
1503 task_lock(current->group_leader);
1504 x = current->signal->rlim[resource];
1505 task_unlock(current->group_leader);
Cal Peake756184b2006-09-30 23:27:24 -07001506 if (x.rlim_cur > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 x.rlim_cur = 0x7FFFFFFF;
Cal Peake756184b2006-09-30 23:27:24 -07001508 if (x.rlim_max > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 x.rlim_max = 0x7FFFFFFF;
1510 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1511}
1512
1513#endif
1514
1515asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1516{
1517 struct rlimit new_rlim, *old_rlim;
1518 int retval;
1519
1520 if (resource >= RLIM_NLIMITS)
1521 return -EINVAL;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001522 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 old_rlim = current->signal->rlim + resource;
1525 if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1526 !capable(CAP_SYS_RESOURCE))
1527 return -EPERM;
Adam Tkac0c2d64f2008-10-15 22:01:45 -07001528
1529 if (resource == RLIMIT_NOFILE) {
1530 if (new_rlim.rlim_max == RLIM_INFINITY)
1531 new_rlim.rlim_max = sysctl_nr_open;
1532 if (new_rlim.rlim_cur == RLIM_INFINITY)
1533 new_rlim.rlim_cur = sysctl_nr_open;
1534 if (new_rlim.rlim_max > sysctl_nr_open)
1535 return -EPERM;
1536 }
1537
1538 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1539 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 retval = security_task_setrlimit(resource, &new_rlim);
1542 if (retval)
1543 return retval;
1544
Tom Alsberg9926e4c2007-05-08 00:30:31 -07001545 if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
1546 /*
1547 * The caller is asking for an immediate RLIMIT_CPU
1548 * expiry. But we use the zero value to mean "it was
1549 * never set". So let's cheat and make it one second
1550 * instead
1551 */
1552 new_rlim.rlim_cur = 1;
1553 }
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 task_lock(current->group_leader);
1556 *old_rlim = new_rlim;
1557 task_unlock(current->group_leader);
1558
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001559 if (resource != RLIMIT_CPU)
1560 goto out;
Andrew Mortond3561f72006-03-24 03:18:36 -08001561
1562 /*
1563 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1564 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1565 * very long-standing error, and fixing it now risks breakage of
1566 * applications, so we live with it
1567 */
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001568 if (new_rlim.rlim_cur == RLIM_INFINITY)
1569 goto out;
1570
Frank Mayharf06febc2008-09-12 09:54:39 -07001571 update_rlimit_cpu(new_rlim.rlim_cur);
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001572out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return 0;
1574}
1575
1576/*
1577 * It would make sense to put struct rusage in the task_struct,
1578 * except that would make the task_struct be *really big*. After
1579 * task_struct gets moved into malloc'ed memory, it would
1580 * make sense to do this. It will make moving the rest of the information
1581 * a lot simpler! (Which we're not doing right now because we're not
1582 * measuring them yet).
1583 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1585 * races with threads incrementing their own counters. But since word
1586 * reads are atomic, we either get new values or old values and we don't
1587 * care which for the sums. We always take the siglock to protect reading
1588 * the c* fields from p->signal from races with exit.c updating those
1589 * fields when reaping, so a sample either gets all the additions of a
1590 * given child after it's reaped, or none so this sample is before reaping.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001591 *
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001592 * Locking:
1593 * We need to take the siglock for CHILDEREN, SELF and BOTH
1594 * for the cases current multithreaded, non-current single threaded
1595 * non-current multithreaded. Thread traversal is now safe with
1596 * the siglock held.
1597 * Strictly speaking, we donot need to take the siglock if we are current and
1598 * single threaded, as no one else can take our signal_struct away, no one
1599 * else can reap the children to update signal->c* counters, and no one else
1600 * can race with the signal-> fields. If we do not take any lock, the
1601 * signal-> fields could be read out of order while another thread was just
1602 * exiting. So we should place a read memory barrier when we avoid the lock.
1603 * On the writer side, write memory barrier is implied in __exit_signal
1604 * as __exit_signal releases the siglock spinlock after updating the signal->
1605 * fields. But we don't do this yet to keep things simple.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001606 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 */
1608
Frank Mayharf06febc2008-09-12 09:54:39 -07001609static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001610{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001611 r->ru_nvcsw += t->nvcsw;
1612 r->ru_nivcsw += t->nivcsw;
1613 r->ru_minflt += t->min_flt;
1614 r->ru_majflt += t->maj_flt;
1615 r->ru_inblock += task_io_get_inblock(t);
1616 r->ru_oublock += task_io_get_oublock(t);
1617}
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1620{
1621 struct task_struct *t;
1622 unsigned long flags;
1623 cputime_t utime, stime;
Frank Mayharf06febc2008-09-12 09:54:39 -07001624 struct task_cputime cputime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 memset((char *) r, 0, sizeof *r);
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001627 utime = stime = cputime_zero;
1628
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001629 if (who == RUSAGE_THREAD) {
Frank Mayharf06febc2008-09-12 09:54:39 -07001630 accumulate_thread_rusage(p, r);
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001631 goto out;
1632 }
1633
Oleg Nesterovd6cf7232008-04-30 00:52:38 -07001634 if (!lock_task_sighand(p, &flags))
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001635 return;
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 switch (who) {
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001638 case RUSAGE_BOTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 case RUSAGE_CHILDREN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 utime = p->signal->cutime;
1641 stime = p->signal->cstime;
1642 r->ru_nvcsw = p->signal->cnvcsw;
1643 r->ru_nivcsw = p->signal->cnivcsw;
1644 r->ru_minflt = p->signal->cmin_flt;
1645 r->ru_majflt = p->signal->cmaj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07001646 r->ru_inblock = p->signal->cinblock;
1647 r->ru_oublock = p->signal->coublock;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001648
1649 if (who == RUSAGE_CHILDREN)
1650 break;
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 case RUSAGE_SELF:
Frank Mayharf06febc2008-09-12 09:54:39 -07001653 thread_group_cputime(p, &cputime);
1654 utime = cputime_add(utime, cputime.utime);
1655 stime = cputime_add(stime, cputime.stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 r->ru_nvcsw += p->signal->nvcsw;
1657 r->ru_nivcsw += p->signal->nivcsw;
1658 r->ru_minflt += p->signal->min_flt;
1659 r->ru_majflt += p->signal->maj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07001660 r->ru_inblock += p->signal->inblock;
1661 r->ru_oublock += p->signal->oublock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 t = p;
1663 do {
Frank Mayharf06febc2008-09-12 09:54:39 -07001664 accumulate_thread_rusage(t, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 t = next_thread(t);
1666 } while (t != p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 break;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 default:
1670 BUG();
1671 }
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001672 unlock_task_sighand(p, &flags);
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001673
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001674out:
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001675 cputime_to_timeval(utime, &r->ru_utime);
1676 cputime_to_timeval(stime, &r->ru_stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
1678
1679int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1680{
1681 struct rusage r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 k_getrusage(p, who, &r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1684}
1685
1686asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
1687{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001688 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1689 who != RUSAGE_THREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return -EINVAL;
1691 return getrusage(current, who, ru);
1692}
1693
1694asmlinkage long sys_umask(int mask)
1695{
1696 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1697 return mask;
1698}
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -08001699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
1701 unsigned long arg4, unsigned long arg5)
1702{
David Howellsb6dff3e2008-11-14 10:39:16 +11001703 struct task_struct *me = current;
1704 unsigned char comm[sizeof(me->comm)];
1705 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
David Howellsd84f4f92008-11-14 10:39:23 +11001707 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1708 if (error != -ENOSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return error;
1710
David Howellsd84f4f92008-11-14 10:39:23 +11001711 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 switch (option) {
1713 case PR_SET_PDEATHSIG:
Jesper Juhl0730ded2005-09-06 15:17:37 -07001714 if (!valid_signal(arg2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 error = -EINVAL;
1716 break;
1717 }
David Howellsb6dff3e2008-11-14 10:39:16 +11001718 me->pdeath_signal = arg2;
1719 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 break;
1721 case PR_GET_PDEATHSIG:
David Howellsb6dff3e2008-11-14 10:39:16 +11001722 error = put_user(me->pdeath_signal, (int __user *)arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 break;
1724 case PR_GET_DUMPABLE:
David Howellsb6dff3e2008-11-14 10:39:16 +11001725 error = get_dumpable(me->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 break;
1727 case PR_SET_DUMPABLE:
Marcel Holtmannabf75a52006-07-12 13:12:00 +02001728 if (arg2 < 0 || arg2 > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 error = -EINVAL;
1730 break;
1731 }
David Howellsb6dff3e2008-11-14 10:39:16 +11001732 set_dumpable(me->mm, arg2);
1733 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 break;
1735
1736 case PR_SET_UNALIGN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001737 error = SET_UNALIGN_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 break;
1739 case PR_GET_UNALIGN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001740 error = GET_UNALIGN_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 break;
1742 case PR_SET_FPEMU:
David Howellsb6dff3e2008-11-14 10:39:16 +11001743 error = SET_FPEMU_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 break;
1745 case PR_GET_FPEMU:
David Howellsb6dff3e2008-11-14 10:39:16 +11001746 error = GET_FPEMU_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 break;
1748 case PR_SET_FPEXC:
David Howellsb6dff3e2008-11-14 10:39:16 +11001749 error = SET_FPEXC_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 break;
1751 case PR_GET_FPEXC:
David Howellsb6dff3e2008-11-14 10:39:16 +11001752 error = GET_FPEXC_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 break;
1754 case PR_GET_TIMING:
1755 error = PR_TIMING_STATISTICAL;
1756 break;
1757 case PR_SET_TIMING:
Shi Weihua7b266552008-05-23 13:04:59 -07001758 if (arg2 != PR_TIMING_STATISTICAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 error = -EINVAL;
David Howellsb6dff3e2008-11-14 10:39:16 +11001760 else
1761 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 break;
1763
David Howellsb6dff3e2008-11-14 10:39:16 +11001764 case PR_SET_NAME:
1765 comm[sizeof(me->comm)-1] = 0;
1766 if (strncpy_from_user(comm, (char __user *)arg2,
1767 sizeof(me->comm) - 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return -EFAULT;
David Howellsb6dff3e2008-11-14 10:39:16 +11001769 set_task_comm(me, comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 return 0;
David Howellsb6dff3e2008-11-14 10:39:16 +11001771 case PR_GET_NAME:
1772 get_task_comm(comm, me);
1773 if (copy_to_user((char __user *)arg2, comm,
1774 sizeof(comm)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return -EFAULT;
1776 return 0;
Anton Blanchard651d7652006-06-07 16:10:19 +10001777 case PR_GET_ENDIAN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001778 error = GET_ENDIAN(me, arg2);
Anton Blanchard651d7652006-06-07 16:10:19 +10001779 break;
1780 case PR_SET_ENDIAN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001781 error = SET_ENDIAN(me, arg2);
Anton Blanchard651d7652006-06-07 16:10:19 +10001782 break;
1783
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -07001784 case PR_GET_SECCOMP:
1785 error = prctl_get_seccomp();
1786 break;
1787 case PR_SET_SECCOMP:
1788 error = prctl_set_seccomp(arg2);
1789 break;
Erik Bosman8fb402b2008-04-11 18:54:17 +02001790 case PR_GET_TSC:
1791 error = GET_TSC_CTL(arg2);
1792 break;
1793 case PR_SET_TSC:
1794 error = SET_TSC_CTL(arg2);
1795 break;
Arjan van de Ven69766752008-09-01 15:52:40 -07001796 case PR_GET_TIMERSLACK:
1797 error = current->timer_slack_ns;
1798 break;
1799 case PR_SET_TIMERSLACK:
1800 if (arg2 <= 0)
1801 current->timer_slack_ns =
1802 current->default_timer_slack_ns;
1803 else
1804 current->timer_slack_ns = arg2;
David Howellsb6dff3e2008-11-14 10:39:16 +11001805 error = 0;
Arjan van de Ven69766752008-09-01 15:52:40 -07001806 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 default:
1808 error = -EINVAL;
1809 break;
1810 }
1811 return error;
1812}
Andi Kleen3cfc3482006-09-26 10:52:28 +02001813
1814asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
Ingo Molnar4307d1e2007-11-07 18:37:48 +01001815 struct getcpu_cache __user *unused)
Andi Kleen3cfc3482006-09-26 10:52:28 +02001816{
1817 int err = 0;
1818 int cpu = raw_smp_processor_id();
1819 if (cpup)
1820 err |= put_user(cpu, cpup);
1821 if (nodep)
1822 err |= put_user(cpu_to_node(cpu), nodep);
Andi Kleen3cfc3482006-09-26 10:52:28 +02001823 return err ? -EFAULT : 0;
1824}
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07001825
1826char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
1827
1828static void argv_cleanup(char **argv, char **envp)
1829{
1830 argv_free(argv);
1831}
1832
1833/**
1834 * orderly_poweroff - Trigger an orderly system poweroff
1835 * @force: force poweroff if command execution fails
1836 *
1837 * This may be called from any context to trigger a system shutdown.
1838 * If the orderly shutdown fails, it will force an immediate shutdown.
1839 */
1840int orderly_poweroff(bool force)
1841{
1842 int argc;
1843 char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
1844 static char *envp[] = {
1845 "HOME=/",
1846 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
1847 NULL
1848 };
1849 int ret = -ENOMEM;
1850 struct subprocess_info *info;
1851
1852 if (argv == NULL) {
1853 printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
1854 __func__, poweroff_cmd);
1855 goto out;
1856 }
1857
KOSAKI Motohiroac331d12008-07-25 01:45:38 -07001858 info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07001859 if (info == NULL) {
1860 argv_free(argv);
1861 goto out;
1862 }
1863
1864 call_usermodehelper_setcleanup(info, argv_cleanup);
1865
Jeremy Fitzhardinge86313c42007-07-17 18:37:03 -07001866 ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07001867
1868 out:
1869 if (ret && force) {
1870 printk(KERN_WARNING "Failed to start orderly shutdown: "
1871 "forcing the issue\n");
1872
1873 /* I guess this should try to kick off some daemon to
1874 sync and poweroff asap. Or not even bother syncing
1875 if we're doing an emergency shutdown? */
1876 emergency_sync();
1877 kernel_power_off();
1878 }
1879
1880 return ret;
1881}
1882EXPORT_SYMBOL_GPL(orderly_poweroff);