blob: 43e557211d8da3a8821bb9b4d31989b9c376ff59 [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
7#include <linux/config.h>
8#include <linux/module.h>
9#include <linux/mm.h>
10#include <linux/utsname.h>
11#include <linux/mman.h>
12#include <linux/smp_lock.h>
13#include <linux/notifier.h>
14#include <linux/reboot.h>
15#include <linux/prctl.h>
16#include <linux/init.h>
17#include <linux/highuid.h>
18#include <linux/fs.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070019#include <linux/kernel.h>
20#include <linux/kexec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/workqueue.h>
22#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <linux/compat.h>
34#include <linux/syscalls.h>
Keshavamurthy Anil S00d7c052005-12-12 00:37:33 -080035#include <linux/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <asm/uaccess.h>
38#include <asm/io.h>
39#include <asm/unistd.h>
40
41#ifndef SET_UNALIGN_CTL
42# define SET_UNALIGN_CTL(a,b) (-EINVAL)
43#endif
44#ifndef GET_UNALIGN_CTL
45# define GET_UNALIGN_CTL(a,b) (-EINVAL)
46#endif
47#ifndef SET_FPEMU_CTL
48# define SET_FPEMU_CTL(a,b) (-EINVAL)
49#endif
50#ifndef GET_FPEMU_CTL
51# define GET_FPEMU_CTL(a,b) (-EINVAL)
52#endif
53#ifndef SET_FPEXC_CTL
54# define SET_FPEXC_CTL(a,b) (-EINVAL)
55#endif
56#ifndef GET_FPEXC_CTL
57# define GET_FPEXC_CTL(a,b) (-EINVAL)
58#endif
59
60/*
61 * this is where the system-wide overflow UID and GID are defined, for
62 * architectures that now have 32-bit UID/GID but didn't in the past
63 */
64
65int overflowuid = DEFAULT_OVERFLOWUID;
66int overflowgid = DEFAULT_OVERFLOWGID;
67
68#ifdef CONFIG_UID16
69EXPORT_SYMBOL(overflowuid);
70EXPORT_SYMBOL(overflowgid);
71#endif
72
73/*
74 * the same as above, but for filesystems which can only store a 16-bit
75 * UID and GID. as such, this is needed on all architectures
76 */
77
78int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
79int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
80
81EXPORT_SYMBOL(fs_overflowuid);
82EXPORT_SYMBOL(fs_overflowgid);
83
84/*
85 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
86 */
87
88int C_A_D = 1;
89int cad_pid = 1;
90
91/*
92 * Notifier list for kernel code which wants to be called
93 * at shutdown. This is used to stop any idling DMA operations
94 * and the like.
95 */
96
97static struct notifier_block *reboot_notifier_list;
98static DEFINE_RWLOCK(notifier_lock);
99
100/**
101 * notifier_chain_register - Add notifier to a notifier chain
102 * @list: Pointer to root list pointer
103 * @n: New entry in notifier chain
104 *
105 * Adds a notifier to a notifier chain.
106 *
107 * Currently always returns zero.
108 */
109
110int notifier_chain_register(struct notifier_block **list, struct notifier_block *n)
111{
112 write_lock(&notifier_lock);
113 while(*list)
114 {
115 if(n->priority > (*list)->priority)
116 break;
117 list= &((*list)->next);
118 }
119 n->next = *list;
120 *list=n;
121 write_unlock(&notifier_lock);
122 return 0;
123}
124
125EXPORT_SYMBOL(notifier_chain_register);
126
127/**
128 * notifier_chain_unregister - Remove notifier from a notifier chain
129 * @nl: Pointer to root list pointer
130 * @n: New entry in notifier chain
131 *
132 * Removes a notifier from a notifier chain.
133 *
134 * Returns zero on success, or %-ENOENT on failure.
135 */
136
137int notifier_chain_unregister(struct notifier_block **nl, struct notifier_block *n)
138{
139 write_lock(&notifier_lock);
140 while((*nl)!=NULL)
141 {
142 if((*nl)==n)
143 {
144 *nl=n->next;
145 write_unlock(&notifier_lock);
146 return 0;
147 }
148 nl=&((*nl)->next);
149 }
150 write_unlock(&notifier_lock);
151 return -ENOENT;
152}
153
154EXPORT_SYMBOL(notifier_chain_unregister);
155
156/**
157 * notifier_call_chain - Call functions in a notifier chain
158 * @n: Pointer to root pointer of notifier chain
159 * @val: Value passed unmodified to notifier function
160 * @v: Pointer passed unmodified to notifier function
161 *
162 * Calls each function in a notifier chain in turn.
163 *
164 * If the return value of the notifier can be and'd
165 * with %NOTIFY_STOP_MASK, then notifier_call_chain
166 * will return immediately, with the return value of
167 * the notifier function which halted execution.
168 * Otherwise, the return value is the return value
169 * of the last notifier function called.
170 */
171
Keshavamurthy Anil S00d7c052005-12-12 00:37:33 -0800172int __kprobes notifier_call_chain(struct notifier_block **n, unsigned long val, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 int ret=NOTIFY_DONE;
175 struct notifier_block *nb = *n;
176
177 while(nb)
178 {
179 ret=nb->notifier_call(nb,val,v);
180 if(ret&NOTIFY_STOP_MASK)
181 {
182 return ret;
183 }
184 nb=nb->next;
185 }
186 return ret;
187}
188
189EXPORT_SYMBOL(notifier_call_chain);
190
191/**
192 * register_reboot_notifier - Register function to be called at reboot time
193 * @nb: Info about notifier function to be called
194 *
195 * Registers a function with the list of functions
196 * to be called at reboot time.
197 *
198 * Currently always returns zero, as notifier_chain_register
199 * always returns zero.
200 */
201
202int register_reboot_notifier(struct notifier_block * nb)
203{
204 return notifier_chain_register(&reboot_notifier_list, nb);
205}
206
207EXPORT_SYMBOL(register_reboot_notifier);
208
209/**
210 * unregister_reboot_notifier - Unregister previously registered reboot notifier
211 * @nb: Hook to be unregistered
212 *
213 * Unregisters a previously registered reboot
214 * notifier function.
215 *
216 * Returns zero on success, or %-ENOENT on failure.
217 */
218
219int unregister_reboot_notifier(struct notifier_block * nb)
220{
221 return notifier_chain_unregister(&reboot_notifier_list, nb);
222}
223
224EXPORT_SYMBOL(unregister_reboot_notifier);
225
226static int set_one_prio(struct task_struct *p, int niceval, int error)
227{
228 int no_nice;
229
230 if (p->uid != current->euid &&
231 p->euid != current->euid && !capable(CAP_SYS_NICE)) {
232 error = -EPERM;
233 goto out;
234 }
Matt Mackalle43379f2005-05-01 08:59:00 -0700235 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 error = -EACCES;
237 goto out;
238 }
239 no_nice = security_task_setnice(p, niceval);
240 if (no_nice) {
241 error = no_nice;
242 goto out;
243 }
244 if (error == -ESRCH)
245 error = 0;
246 set_user_nice(p, niceval);
247out:
248 return error;
249}
250
251asmlinkage long sys_setpriority(int which, int who, int niceval)
252{
253 struct task_struct *g, *p;
254 struct user_struct *user;
255 int error = -EINVAL;
256
257 if (which > 2 || which < 0)
258 goto out;
259
260 /* normalize: avoid signed division (rounding problems) */
261 error = -ESRCH;
262 if (niceval < -20)
263 niceval = -20;
264 if (niceval > 19)
265 niceval = 19;
266
267 read_lock(&tasklist_lock);
268 switch (which) {
269 case PRIO_PROCESS:
270 if (!who)
271 who = current->pid;
272 p = find_task_by_pid(who);
273 if (p)
274 error = set_one_prio(p, niceval, error);
275 break;
276 case PRIO_PGRP:
277 if (!who)
278 who = process_group(current);
279 do_each_task_pid(who, PIDTYPE_PGID, p) {
280 error = set_one_prio(p, niceval, error);
281 } while_each_task_pid(who, PIDTYPE_PGID, p);
282 break;
283 case PRIO_USER:
284 user = current->user;
285 if (!who)
286 who = current->uid;
287 else
288 if ((who != current->uid) && !(user = find_user(who)))
289 goto out_unlock; /* No processes for this user */
290
291 do_each_thread(g, p)
292 if (p->uid == who)
293 error = set_one_prio(p, niceval, error);
294 while_each_thread(g, p);
295 if (who != current->uid)
296 free_uid(user); /* For find_user() */
297 break;
298 }
299out_unlock:
300 read_unlock(&tasklist_lock);
301out:
302 return error;
303}
304
305/*
306 * Ugh. To avoid negative return values, "getpriority()" will
307 * not return the normal nice-value, but a negated value that
308 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
309 * to stay compatible.
310 */
311asmlinkage long sys_getpriority(int which, int who)
312{
313 struct task_struct *g, *p;
314 struct user_struct *user;
315 long niceval, retval = -ESRCH;
316
317 if (which > 2 || which < 0)
318 return -EINVAL;
319
320 read_lock(&tasklist_lock);
321 switch (which) {
322 case PRIO_PROCESS:
323 if (!who)
324 who = current->pid;
325 p = find_task_by_pid(who);
326 if (p) {
327 niceval = 20 - task_nice(p);
328 if (niceval > retval)
329 retval = niceval;
330 }
331 break;
332 case PRIO_PGRP:
333 if (!who)
334 who = process_group(current);
335 do_each_task_pid(who, PIDTYPE_PGID, p) {
336 niceval = 20 - task_nice(p);
337 if (niceval > retval)
338 retval = niceval;
339 } while_each_task_pid(who, PIDTYPE_PGID, p);
340 break;
341 case PRIO_USER:
342 user = current->user;
343 if (!who)
344 who = current->uid;
345 else
346 if ((who != current->uid) && !(user = find_user(who)))
347 goto out_unlock; /* No processes for this user */
348
349 do_each_thread(g, p)
350 if (p->uid == who) {
351 niceval = 20 - task_nice(p);
352 if (niceval > retval)
353 retval = niceval;
354 }
355 while_each_thread(g, p);
356 if (who != current->uid)
357 free_uid(user); /* for find_user() */
358 break;
359 }
360out_unlock:
361 read_unlock(&tasklist_lock);
362
363 return retval;
364}
365
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700366/**
367 * emergency_restart - reboot the system
368 *
369 * Without shutting down any hardware or taking any locks
370 * reboot the system. This is called when we know we are in
371 * trouble so this is our best effort to reboot. This is
372 * safe to call in interrupt context.
373 */
Eric W. Biederman7c903472005-07-26 11:29:55 -0600374void emergency_restart(void)
375{
376 machine_emergency_restart();
377}
378EXPORT_SYMBOL_GPL(emergency_restart);
379
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700380void kernel_restart_prepare(char *cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600381{
382 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
383 system_state = SYSTEM_RESTART;
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600384 device_shutdown();
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700385}
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800386
387/**
388 * kernel_restart - reboot the system
389 * @cmd: pointer to buffer containing command to execute for restart
Randy Dunlapb8887e62005-11-07 01:01:07 -0800390 * or %NULL
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800391 *
392 * Shutdown everything and perform a clean reboot.
393 * This is not safe to call in interrupt context.
394 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700395void kernel_restart(char *cmd)
396{
397 kernel_restart_prepare(cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600398 if (!cmd) {
399 printk(KERN_EMERG "Restarting system.\n");
400 } else {
401 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
402 }
403 printk(".\n");
404 machine_restart(cmd);
405}
406EXPORT_SYMBOL_GPL(kernel_restart);
407
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700408/**
409 * kernel_kexec - reboot the system
410 *
411 * Move into place and start executing a preloaded standalone
412 * executable. If nothing was preloaded return an error.
413 */
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600414void kernel_kexec(void)
415{
416#ifdef CONFIG_KEXEC
417 struct kimage *image;
418 image = xchg(&kexec_image, 0);
419 if (!image) {
420 return;
421 }
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700422 kernel_restart_prepare(NULL);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600423 printk(KERN_EMERG "Starting new kernel\n");
424 machine_shutdown();
425 machine_kexec(image);
426#endif
427}
428EXPORT_SYMBOL_GPL(kernel_kexec);
429
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700430/**
431 * kernel_halt - halt the system
432 *
433 * Shutdown everything and perform a clean system halt.
434 */
435void kernel_halt_prepare(void)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600436{
437 notifier_call_chain(&reboot_notifier_list, SYS_HALT, NULL);
438 system_state = SYSTEM_HALT;
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600439 device_shutdown();
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700440}
441void kernel_halt(void)
442{
443 kernel_halt_prepare();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600444 printk(KERN_EMERG "System halted.\n");
445 machine_halt();
446}
447EXPORT_SYMBOL_GPL(kernel_halt);
448
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700449/**
450 * kernel_power_off - power_off the system
451 *
452 * Shutdown everything and perform a clean system power_off.
453 */
454void kernel_power_off_prepare(void)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600455{
456 notifier_call_chain(&reboot_notifier_list, SYS_POWER_OFF, NULL);
457 system_state = SYSTEM_POWER_OFF;
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600458 device_shutdown();
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700459}
460void kernel_power_off(void)
461{
462 kernel_power_off_prepare();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600463 printk(KERN_EMERG "Power down.\n");
464 machine_power_off();
465}
466EXPORT_SYMBOL_GPL(kernel_power_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468/*
469 * Reboot system call: for obvious reasons only root may call it,
470 * and even root needs to set up some magic numbers in the registers
471 * so that some mistake won't make this reboot the whole machine.
472 * You can also set the meaning of the ctrl-alt-del-key here.
473 *
474 * reboot doesn't sync: do that yourself before calling this.
475 */
476asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
477{
478 char buffer[256];
479
480 /* We only trust the superuser with rebooting the system. */
481 if (!capable(CAP_SYS_BOOT))
482 return -EPERM;
483
484 /* For safety, we require "magic" arguments. */
485 if (magic1 != LINUX_REBOOT_MAGIC1 ||
486 (magic2 != LINUX_REBOOT_MAGIC2 &&
487 magic2 != LINUX_REBOOT_MAGIC2A &&
488 magic2 != LINUX_REBOOT_MAGIC2B &&
489 magic2 != LINUX_REBOOT_MAGIC2C))
490 return -EINVAL;
491
Eric W. Biederman5e382912006-01-08 01:03:46 -0800492 /* Instead of trying to make the power_off code look like
493 * halt when pm_power_off is not set do it the easy way.
494 */
495 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
496 cmd = LINUX_REBOOT_CMD_HALT;
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 lock_kernel();
499 switch (cmd) {
500 case LINUX_REBOOT_CMD_RESTART:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600501 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 break;
503
504 case LINUX_REBOOT_CMD_CAD_ON:
505 C_A_D = 1;
506 break;
507
508 case LINUX_REBOOT_CMD_CAD_OFF:
509 C_A_D = 0;
510 break;
511
512 case LINUX_REBOOT_CMD_HALT:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600513 kernel_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 unlock_kernel();
515 do_exit(0);
516 break;
517
518 case LINUX_REBOOT_CMD_POWER_OFF:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600519 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 unlock_kernel();
521 do_exit(0);
522 break;
523
524 case LINUX_REBOOT_CMD_RESTART2:
525 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
526 unlock_kernel();
527 return -EFAULT;
528 }
529 buffer[sizeof(buffer) - 1] = '\0';
530
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600531 kernel_restart(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 break;
533
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700534 case LINUX_REBOOT_CMD_KEXEC:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600535 kernel_kexec();
536 unlock_kernel();
537 return -EINVAL;
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539#ifdef CONFIG_SOFTWARE_SUSPEND
540 case LINUX_REBOOT_CMD_SW_SUSPEND:
541 {
542 int ret = software_suspend();
543 unlock_kernel();
544 return ret;
545 }
546#endif
547
548 default:
549 unlock_kernel();
550 return -EINVAL;
551 }
552 unlock_kernel();
553 return 0;
554}
555
556static void deferred_cad(void *dummy)
557{
Eric W. Biedermanabcd9e52005-07-26 11:27:34 -0600558 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561/*
562 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
563 * As it's called within an interrupt, it may NOT sync: the only choice
564 * is whether to reboot at once, or just ignore the ctrl-alt-del.
565 */
566void ctrl_alt_del(void)
567{
568 static DECLARE_WORK(cad_work, deferred_cad, NULL);
569
570 if (C_A_D)
571 schedule_work(&cad_work);
572 else
573 kill_proc(cad_pid, SIGINT, 1);
574}
575
576
577/*
578 * Unprivileged users may change the real gid to the effective gid
579 * or vice versa. (BSD-style)
580 *
581 * If you set the real gid at all, or set the effective gid to a value not
582 * equal to the real gid, then the saved gid is set to the new effective gid.
583 *
584 * This makes it possible for a setgid program to completely drop its
585 * privileges, which is often a useful assertion to make when you are doing
586 * a security audit over a program.
587 *
588 * The general idea is that a program which uses just setregid() will be
589 * 100% compatible with BSD. A program which uses just setgid() will be
590 * 100% compatible with POSIX with saved IDs.
591 *
592 * SMP: There are not races, the GIDs are checked only by filesystem
593 * operations (as far as semantic preservation is concerned).
594 */
595asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
596{
597 int old_rgid = current->gid;
598 int old_egid = current->egid;
599 int new_rgid = old_rgid;
600 int new_egid = old_egid;
601 int retval;
602
603 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
604 if (retval)
605 return retval;
606
607 if (rgid != (gid_t) -1) {
608 if ((old_rgid == rgid) ||
609 (current->egid==rgid) ||
610 capable(CAP_SETGID))
611 new_rgid = rgid;
612 else
613 return -EPERM;
614 }
615 if (egid != (gid_t) -1) {
616 if ((old_rgid == egid) ||
617 (current->egid == egid) ||
618 (current->sgid == egid) ||
619 capable(CAP_SETGID))
620 new_egid = egid;
621 else {
622 return -EPERM;
623 }
624 }
625 if (new_egid != old_egid)
626 {
Alan Coxd6e71142005-06-23 00:09:43 -0700627 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700628 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630 if (rgid != (gid_t) -1 ||
631 (egid != (gid_t) -1 && egid != old_rgid))
632 current->sgid = new_egid;
633 current->fsgid = new_egid;
634 current->egid = new_egid;
635 current->gid = new_rgid;
636 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800637 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return 0;
639}
640
641/*
642 * setgid() is implemented like SysV w/ SAVED_IDS
643 *
644 * SMP: Same implicit races as above.
645 */
646asmlinkage long sys_setgid(gid_t gid)
647{
648 int old_egid = current->egid;
649 int retval;
650
651 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
652 if (retval)
653 return retval;
654
655 if (capable(CAP_SETGID))
656 {
657 if(old_egid != gid)
658 {
Alan Coxd6e71142005-06-23 00:09:43 -0700659 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700660 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662 current->gid = current->egid = current->sgid = current->fsgid = gid;
663 }
664 else if ((gid == current->gid) || (gid == current->sgid))
665 {
666 if(old_egid != gid)
667 {
Alan Coxd6e71142005-06-23 00:09:43 -0700668 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700669 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671 current->egid = current->fsgid = gid;
672 }
673 else
674 return -EPERM;
675
676 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800677 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return 0;
679}
680
681static int set_user(uid_t new_ruid, int dumpclear)
682{
683 struct user_struct *new_user;
684
685 new_user = alloc_uid(new_ruid);
686 if (!new_user)
687 return -EAGAIN;
688
689 if (atomic_read(&new_user->processes) >=
690 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
691 new_user != &root_user) {
692 free_uid(new_user);
693 return -EAGAIN;
694 }
695
696 switch_uid(new_user);
697
698 if(dumpclear)
699 {
Alan Coxd6e71142005-06-23 00:09:43 -0700700 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700701 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703 current->uid = new_ruid;
704 return 0;
705}
706
707/*
708 * Unprivileged users may change the real uid to the effective uid
709 * or vice versa. (BSD-style)
710 *
711 * If you set the real uid at all, or set the effective uid to a value not
712 * equal to the real uid, then the saved uid is set to the new effective uid.
713 *
714 * This makes it possible for a setuid program to completely drop its
715 * privileges, which is often a useful assertion to make when you are doing
716 * a security audit over a program.
717 *
718 * The general idea is that a program which uses just setreuid() will be
719 * 100% compatible with BSD. A program which uses just setuid() will be
720 * 100% compatible with POSIX with saved IDs.
721 */
722asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
723{
724 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
725 int retval;
726
727 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
728 if (retval)
729 return retval;
730
731 new_ruid = old_ruid = current->uid;
732 new_euid = old_euid = current->euid;
733 old_suid = current->suid;
734
735 if (ruid != (uid_t) -1) {
736 new_ruid = ruid;
737 if ((old_ruid != ruid) &&
738 (current->euid != ruid) &&
739 !capable(CAP_SETUID))
740 return -EPERM;
741 }
742
743 if (euid != (uid_t) -1) {
744 new_euid = euid;
745 if ((old_ruid != euid) &&
746 (current->euid != euid) &&
747 (current->suid != euid) &&
748 !capable(CAP_SETUID))
749 return -EPERM;
750 }
751
752 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
753 return -EAGAIN;
754
755 if (new_euid != old_euid)
756 {
Alan Coxd6e71142005-06-23 00:09:43 -0700757 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700758 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760 current->fsuid = current->euid = new_euid;
761 if (ruid != (uid_t) -1 ||
762 (euid != (uid_t) -1 && euid != old_ruid))
763 current->suid = current->euid;
764 current->fsuid = current->euid;
765
766 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800767 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
770}
771
772
773
774/*
775 * setuid() is implemented like SysV with SAVED_IDS
776 *
777 * Note that SAVED_ID's is deficient in that a setuid root program
778 * like sendmail, for example, cannot set its uid to be a normal
779 * user and then switch back, because if you're root, setuid() sets
780 * the saved uid too. If you don't like this, blame the bright people
781 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
782 * will allow a root program to temporarily drop privileges and be able to
783 * regain them by swapping the real and effective uid.
784 */
785asmlinkage long sys_setuid(uid_t uid)
786{
787 int old_euid = current->euid;
788 int old_ruid, old_suid, new_ruid, new_suid;
789 int retval;
790
791 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
792 if (retval)
793 return retval;
794
795 old_ruid = new_ruid = current->uid;
796 old_suid = current->suid;
797 new_suid = old_suid;
798
799 if (capable(CAP_SETUID)) {
800 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
801 return -EAGAIN;
802 new_suid = uid;
803 } else if ((uid != current->uid) && (uid != new_suid))
804 return -EPERM;
805
806 if (old_euid != uid)
807 {
Alan Coxd6e71142005-06-23 00:09:43 -0700808 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700809 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811 current->fsuid = current->euid = uid;
812 current->suid = new_suid;
813
814 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800815 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
818}
819
820
821/*
822 * This function implements a generic ability to update ruid, euid,
823 * and suid. This allows you to implement the 4.4 compatible seteuid().
824 */
825asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
826{
827 int old_ruid = current->uid;
828 int old_euid = current->euid;
829 int old_suid = current->suid;
830 int retval;
831
832 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
833 if (retval)
834 return retval;
835
836 if (!capable(CAP_SETUID)) {
837 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
838 (ruid != current->euid) && (ruid != current->suid))
839 return -EPERM;
840 if ((euid != (uid_t) -1) && (euid != current->uid) &&
841 (euid != current->euid) && (euid != current->suid))
842 return -EPERM;
843 if ((suid != (uid_t) -1) && (suid != current->uid) &&
844 (suid != current->euid) && (suid != current->suid))
845 return -EPERM;
846 }
847 if (ruid != (uid_t) -1) {
848 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
849 return -EAGAIN;
850 }
851 if (euid != (uid_t) -1) {
852 if (euid != current->euid)
853 {
Alan Coxd6e71142005-06-23 00:09:43 -0700854 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700855 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857 current->euid = euid;
858 }
859 current->fsuid = current->euid;
860 if (suid != (uid_t) -1)
861 current->suid = suid;
862
863 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800864 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
867}
868
869asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
870{
871 int retval;
872
873 if (!(retval = put_user(current->uid, ruid)) &&
874 !(retval = put_user(current->euid, euid)))
875 retval = put_user(current->suid, suid);
876
877 return retval;
878}
879
880/*
881 * Same as above, but for rgid, egid, sgid.
882 */
883asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
884{
885 int retval;
886
887 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
888 if (retval)
889 return retval;
890
891 if (!capable(CAP_SETGID)) {
892 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
893 (rgid != current->egid) && (rgid != current->sgid))
894 return -EPERM;
895 if ((egid != (gid_t) -1) && (egid != current->gid) &&
896 (egid != current->egid) && (egid != current->sgid))
897 return -EPERM;
898 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
899 (sgid != current->egid) && (sgid != current->sgid))
900 return -EPERM;
901 }
902 if (egid != (gid_t) -1) {
903 if (egid != current->egid)
904 {
Alan Coxd6e71142005-06-23 00:09:43 -0700905 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700906 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908 current->egid = egid;
909 }
910 current->fsgid = current->egid;
911 if (rgid != (gid_t) -1)
912 current->gid = rgid;
913 if (sgid != (gid_t) -1)
914 current->sgid = sgid;
915
916 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800917 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return 0;
919}
920
921asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
922{
923 int retval;
924
925 if (!(retval = put_user(current->gid, rgid)) &&
926 !(retval = put_user(current->egid, egid)))
927 retval = put_user(current->sgid, sgid);
928
929 return retval;
930}
931
932
933/*
934 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
935 * is used for "access()" and for the NFS daemon (letting nfsd stay at
936 * whatever uid it wants to). It normally shadows "euid", except when
937 * explicitly set by setfsuid() or for access..
938 */
939asmlinkage long sys_setfsuid(uid_t uid)
940{
941 int old_fsuid;
942
943 old_fsuid = current->fsuid;
944 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
945 return old_fsuid;
946
947 if (uid == current->uid || uid == current->euid ||
948 uid == current->suid || uid == current->fsuid ||
949 capable(CAP_SETUID))
950 {
951 if (uid != old_fsuid)
952 {
Alan Coxd6e71142005-06-23 00:09:43 -0700953 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700954 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956 current->fsuid = uid;
957 }
958
959 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800960 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
963
964 return old_fsuid;
965}
966
967/*
968 * Samma på svenska..
969 */
970asmlinkage long sys_setfsgid(gid_t gid)
971{
972 int old_fsgid;
973
974 old_fsgid = current->fsgid;
975 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
976 return old_fsgid;
977
978 if (gid == current->gid || gid == current->egid ||
979 gid == current->sgid || gid == current->fsgid ||
980 capable(CAP_SETGID))
981 {
982 if (gid != old_fsgid)
983 {
Alan Coxd6e71142005-06-23 00:09:43 -0700984 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700985 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987 current->fsgid = gid;
988 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800989 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991 return old_fsgid;
992}
993
994asmlinkage long sys_times(struct tms __user * tbuf)
995{
996 /*
997 * In the SMP world we might just be unlucky and have one of
998 * the times increment as we use it. Since the value is an
999 * atomically safe type this is just fine. Conceptually its
1000 * as if the syscall took an instant longer to occur.
1001 */
1002 if (tbuf) {
1003 struct tms tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 cputime_t utime, stime, cutime, cstime;
1005
Christoph Lameter71a22242005-06-23 00:10:05 -07001006#ifdef CONFIG_SMP
1007 if (thread_group_empty(current)) {
1008 /*
1009 * Single thread case without the use of any locks.
1010 *
1011 * We may race with release_task if two threads are
1012 * executing. However, release task first adds up the
1013 * counters (__exit_signal) before removing the task
1014 * from the process tasklist (__unhash_process).
1015 * __exit_signal also acquires and releases the
1016 * siglock which results in the proper memory ordering
1017 * so that the list modifications are always visible
1018 * after the counters have been updated.
1019 *
1020 * If the counters have been updated by the second thread
1021 * but the thread has not yet been removed from the list
1022 * then the other branch will be executing which will
1023 * block on tasklist_lock until the exit handling of the
1024 * other task is finished.
1025 *
1026 * This also implies that the sighand->siglock cannot
1027 * be held by another processor. So we can also
1028 * skip acquiring that lock.
1029 */
1030 utime = cputime_add(current->signal->utime, current->utime);
1031 stime = cputime_add(current->signal->utime, current->stime);
1032 cutime = current->signal->cutime;
1033 cstime = current->signal->cstime;
1034 } else
1035#endif
1036 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Christoph Lameter71a22242005-06-23 00:10:05 -07001038 /* Process with multiple threads */
1039 struct task_struct *tsk = current;
1040 struct task_struct *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Christoph Lameter71a22242005-06-23 00:10:05 -07001042 read_lock(&tasklist_lock);
1043 utime = tsk->signal->utime;
1044 stime = tsk->signal->stime;
1045 t = tsk;
1046 do {
1047 utime = cputime_add(utime, t->utime);
1048 stime = cputime_add(stime, t->stime);
1049 t = next_thread(t);
1050 } while (t != tsk);
1051
1052 /*
1053 * While we have tasklist_lock read-locked, no dying thread
1054 * can be updating current->signal->[us]time. Instead,
1055 * we got their counts included in the live thread loop.
1056 * However, another thread can come in right now and
1057 * do a wait call that updates current->signal->c[us]time.
1058 * To make sure we always see that pair updated atomically,
1059 * we take the siglock around fetching them.
1060 */
1061 spin_lock_irq(&tsk->sighand->siglock);
1062 cutime = tsk->signal->cutime;
1063 cstime = tsk->signal->cstime;
1064 spin_unlock_irq(&tsk->sighand->siglock);
1065 read_unlock(&tasklist_lock);
1066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 tmp.tms_utime = cputime_to_clock_t(utime);
1068 tmp.tms_stime = cputime_to_clock_t(stime);
1069 tmp.tms_cutime = cputime_to_clock_t(cutime);
1070 tmp.tms_cstime = cputime_to_clock_t(cstime);
1071 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1072 return -EFAULT;
1073 }
1074 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1075}
1076
1077/*
1078 * This needs some heavy checking ...
1079 * I just haven't the stomach for it. I also don't fully
1080 * understand sessions/pgrp etc. Let somebody who does explain it.
1081 *
1082 * OK, I think I have the protection semantics right.... this is really
1083 * only important on a multi-user system anyway, to make sure one user
1084 * can't send a signal to a process owned by another. -TYT, 12/12/91
1085 *
1086 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1087 * LBT 04.03.94
1088 */
1089
1090asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
1091{
1092 struct task_struct *p;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001093 struct task_struct *group_leader = current->group_leader;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 int err = -EINVAL;
1095
1096 if (!pid)
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001097 pid = group_leader->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (!pgid)
1099 pgid = pid;
1100 if (pgid < 0)
1101 return -EINVAL;
1102
1103 /* From this point forward we keep holding onto the tasklist lock
1104 * so that our parent does not change from under us. -DaveM
1105 */
1106 write_lock_irq(&tasklist_lock);
1107
1108 err = -ESRCH;
1109 p = find_task_by_pid(pid);
1110 if (!p)
1111 goto out;
1112
1113 err = -EINVAL;
1114 if (!thread_group_leader(p))
1115 goto out;
1116
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001117 if (p->parent == current || p->real_parent == group_leader) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 err = -EPERM;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001119 if (p->signal->session != group_leader->signal->session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 goto out;
1121 err = -EACCES;
1122 if (p->did_exec)
1123 goto out;
1124 } else {
1125 err = -ESRCH;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001126 if (p != group_leader)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 goto out;
1128 }
1129
1130 err = -EPERM;
1131 if (p->signal->leader)
1132 goto out;
1133
1134 if (pgid != pid) {
1135 struct task_struct *p;
1136
1137 do_each_task_pid(pgid, PIDTYPE_PGID, p) {
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001138 if (p->signal->session == group_leader->signal->session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 goto ok_pgid;
1140 } while_each_task_pid(pgid, PIDTYPE_PGID, p);
1141 goto out;
1142 }
1143
1144ok_pgid:
1145 err = security_task_setpgid(p, pgid);
1146 if (err)
1147 goto out;
1148
1149 if (process_group(p) != pgid) {
1150 detach_pid(p, PIDTYPE_PGID);
1151 p->signal->pgrp = pgid;
1152 attach_pid(p, PIDTYPE_PGID, pgid);
1153 }
1154
1155 err = 0;
1156out:
1157 /* All paths lead to here, thus we are safe. -DaveM */
1158 write_unlock_irq(&tasklist_lock);
1159 return err;
1160}
1161
1162asmlinkage long sys_getpgid(pid_t pid)
1163{
1164 if (!pid) {
1165 return process_group(current);
1166 } else {
1167 int retval;
1168 struct task_struct *p;
1169
1170 read_lock(&tasklist_lock);
1171 p = find_task_by_pid(pid);
1172
1173 retval = -ESRCH;
1174 if (p) {
1175 retval = security_task_getpgid(p);
1176 if (!retval)
1177 retval = process_group(p);
1178 }
1179 read_unlock(&tasklist_lock);
1180 return retval;
1181 }
1182}
1183
1184#ifdef __ARCH_WANT_SYS_GETPGRP
1185
1186asmlinkage long sys_getpgrp(void)
1187{
1188 /* SMP - assuming writes are word atomic this is fine */
1189 return process_group(current);
1190}
1191
1192#endif
1193
1194asmlinkage long sys_getsid(pid_t pid)
1195{
1196 if (!pid) {
1197 return current->signal->session;
1198 } else {
1199 int retval;
1200 struct task_struct *p;
1201
1202 read_lock(&tasklist_lock);
1203 p = find_task_by_pid(pid);
1204
1205 retval = -ESRCH;
1206 if(p) {
1207 retval = security_task_getsid(p);
1208 if (!retval)
1209 retval = p->signal->session;
1210 }
1211 read_unlock(&tasklist_lock);
1212 return retval;
1213 }
1214}
1215
1216asmlinkage long sys_setsid(void)
1217{
1218 struct pid *pid;
1219 int err = -EPERM;
1220
1221 if (!thread_group_leader(current))
1222 return -EINVAL;
1223
1224 down(&tty_sem);
1225 write_lock_irq(&tasklist_lock);
1226
1227 pid = find_pid(PIDTYPE_PGID, current->pid);
1228 if (pid)
1229 goto out;
1230
1231 current->signal->leader = 1;
1232 __set_special_pids(current->pid, current->pid);
1233 current->signal->tty = NULL;
1234 current->signal->tty_old_pgrp = 0;
1235 err = process_group(current);
1236out:
1237 write_unlock_irq(&tasklist_lock);
1238 up(&tty_sem);
1239 return err;
1240}
1241
1242/*
1243 * Supplementary group IDs
1244 */
1245
1246/* init to 2 - one for init_task, one to ensure it is never freed */
1247struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1248
1249struct group_info *groups_alloc(int gidsetsize)
1250{
1251 struct group_info *group_info;
1252 int nblocks;
1253 int i;
1254
1255 nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1256 /* Make sure we always allocate at least one indirect block pointer */
1257 nblocks = nblocks ? : 1;
1258 group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1259 if (!group_info)
1260 return NULL;
1261 group_info->ngroups = gidsetsize;
1262 group_info->nblocks = nblocks;
1263 atomic_set(&group_info->usage, 1);
1264
1265 if (gidsetsize <= NGROUPS_SMALL) {
1266 group_info->blocks[0] = group_info->small_block;
1267 } else {
1268 for (i = 0; i < nblocks; i++) {
1269 gid_t *b;
1270 b = (void *)__get_free_page(GFP_USER);
1271 if (!b)
1272 goto out_undo_partial_alloc;
1273 group_info->blocks[i] = b;
1274 }
1275 }
1276 return group_info;
1277
1278out_undo_partial_alloc:
1279 while (--i >= 0) {
1280 free_page((unsigned long)group_info->blocks[i]);
1281 }
1282 kfree(group_info);
1283 return NULL;
1284}
1285
1286EXPORT_SYMBOL(groups_alloc);
1287
1288void groups_free(struct group_info *group_info)
1289{
1290 if (group_info->blocks[0] != group_info->small_block) {
1291 int i;
1292 for (i = 0; i < group_info->nblocks; i++)
1293 free_page((unsigned long)group_info->blocks[i]);
1294 }
1295 kfree(group_info);
1296}
1297
1298EXPORT_SYMBOL(groups_free);
1299
1300/* export the group_info to a user-space array */
1301static int groups_to_user(gid_t __user *grouplist,
1302 struct group_info *group_info)
1303{
1304 int i;
1305 int count = group_info->ngroups;
1306
1307 for (i = 0; i < group_info->nblocks; i++) {
1308 int cp_count = min(NGROUPS_PER_BLOCK, count);
1309 int off = i * NGROUPS_PER_BLOCK;
1310 int len = cp_count * sizeof(*grouplist);
1311
1312 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1313 return -EFAULT;
1314
1315 count -= cp_count;
1316 }
1317 return 0;
1318}
1319
1320/* fill a group_info from a user-space array - it must be allocated already */
1321static int groups_from_user(struct group_info *group_info,
1322 gid_t __user *grouplist)
1323 {
1324 int i;
1325 int count = group_info->ngroups;
1326
1327 for (i = 0; i < group_info->nblocks; i++) {
1328 int cp_count = min(NGROUPS_PER_BLOCK, count);
1329 int off = i * NGROUPS_PER_BLOCK;
1330 int len = cp_count * sizeof(*grouplist);
1331
1332 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1333 return -EFAULT;
1334
1335 count -= cp_count;
1336 }
1337 return 0;
1338}
1339
Domen Puncerebe8b542005-05-05 16:16:19 -07001340/* a simple Shell sort */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341static void groups_sort(struct group_info *group_info)
1342{
1343 int base, max, stride;
1344 int gidsetsize = group_info->ngroups;
1345
1346 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1347 ; /* nothing */
1348 stride /= 3;
1349
1350 while (stride) {
1351 max = gidsetsize - stride;
1352 for (base = 0; base < max; base++) {
1353 int left = base;
1354 int right = left + stride;
1355 gid_t tmp = GROUP_AT(group_info, right);
1356
1357 while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1358 GROUP_AT(group_info, right) =
1359 GROUP_AT(group_info, left);
1360 right = left;
1361 left -= stride;
1362 }
1363 GROUP_AT(group_info, right) = tmp;
1364 }
1365 stride /= 3;
1366 }
1367}
1368
1369/* a simple bsearch */
David Howells3e301482005-06-23 22:00:56 -07001370int groups_search(struct group_info *group_info, gid_t grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 int left, right;
1373
1374 if (!group_info)
1375 return 0;
1376
1377 left = 0;
1378 right = group_info->ngroups;
1379 while (left < right) {
1380 int mid = (left+right)/2;
1381 int cmp = grp - GROUP_AT(group_info, mid);
1382 if (cmp > 0)
1383 left = mid + 1;
1384 else if (cmp < 0)
1385 right = mid;
1386 else
1387 return 1;
1388 }
1389 return 0;
1390}
1391
1392/* validate and set current->group_info */
1393int set_current_groups(struct group_info *group_info)
1394{
1395 int retval;
1396 struct group_info *old_info;
1397
1398 retval = security_task_setgroups(group_info);
1399 if (retval)
1400 return retval;
1401
1402 groups_sort(group_info);
1403 get_group_info(group_info);
1404
1405 task_lock(current);
1406 old_info = current->group_info;
1407 current->group_info = group_info;
1408 task_unlock(current);
1409
1410 put_group_info(old_info);
1411
1412 return 0;
1413}
1414
1415EXPORT_SYMBOL(set_current_groups);
1416
1417asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1418{
1419 int i = 0;
1420
1421 /*
1422 * SMP: Nobody else can change our grouplist. Thus we are
1423 * safe.
1424 */
1425
1426 if (gidsetsize < 0)
1427 return -EINVAL;
1428
1429 /* no need to grab task_lock here; it cannot change */
1430 get_group_info(current->group_info);
1431 i = current->group_info->ngroups;
1432 if (gidsetsize) {
1433 if (i > gidsetsize) {
1434 i = -EINVAL;
1435 goto out;
1436 }
1437 if (groups_to_user(grouplist, current->group_info)) {
1438 i = -EFAULT;
1439 goto out;
1440 }
1441 }
1442out:
1443 put_group_info(current->group_info);
1444 return i;
1445}
1446
1447/*
1448 * SMP: Our groups are copy-on-write. We can set them safely
1449 * without another task interfering.
1450 */
1451
1452asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1453{
1454 struct group_info *group_info;
1455 int retval;
1456
1457 if (!capable(CAP_SETGID))
1458 return -EPERM;
1459 if ((unsigned)gidsetsize > NGROUPS_MAX)
1460 return -EINVAL;
1461
1462 group_info = groups_alloc(gidsetsize);
1463 if (!group_info)
1464 return -ENOMEM;
1465 retval = groups_from_user(group_info, grouplist);
1466 if (retval) {
1467 put_group_info(group_info);
1468 return retval;
1469 }
1470
1471 retval = set_current_groups(group_info);
1472 put_group_info(group_info);
1473
1474 return retval;
1475}
1476
1477/*
1478 * Check whether we're fsgid/egid or in the supplemental group..
1479 */
1480int in_group_p(gid_t grp)
1481{
1482 int retval = 1;
1483 if (grp != current->fsgid) {
1484 get_group_info(current->group_info);
1485 retval = groups_search(current->group_info, grp);
1486 put_group_info(current->group_info);
1487 }
1488 return retval;
1489}
1490
1491EXPORT_SYMBOL(in_group_p);
1492
1493int in_egroup_p(gid_t grp)
1494{
1495 int retval = 1;
1496 if (grp != current->egid) {
1497 get_group_info(current->group_info);
1498 retval = groups_search(current->group_info, grp);
1499 put_group_info(current->group_info);
1500 }
1501 return retval;
1502}
1503
1504EXPORT_SYMBOL(in_egroup_p);
1505
1506DECLARE_RWSEM(uts_sem);
1507
David S. Miller393b0722005-11-10 12:47:50 -08001508EXPORT_SYMBOL(uts_sem);
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510asmlinkage long sys_newuname(struct new_utsname __user * name)
1511{
1512 int errno = 0;
1513
1514 down_read(&uts_sem);
1515 if (copy_to_user(name,&system_utsname,sizeof *name))
1516 errno = -EFAULT;
1517 up_read(&uts_sem);
1518 return errno;
1519}
1520
1521asmlinkage long sys_sethostname(char __user *name, int len)
1522{
1523 int errno;
1524 char tmp[__NEW_UTS_LEN];
1525
1526 if (!capable(CAP_SYS_ADMIN))
1527 return -EPERM;
1528 if (len < 0 || len > __NEW_UTS_LEN)
1529 return -EINVAL;
1530 down_write(&uts_sem);
1531 errno = -EFAULT;
1532 if (!copy_from_user(tmp, name, len)) {
1533 memcpy(system_utsname.nodename, tmp, len);
1534 system_utsname.nodename[len] = 0;
1535 errno = 0;
1536 }
1537 up_write(&uts_sem);
1538 return errno;
1539}
1540
1541#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1542
1543asmlinkage long sys_gethostname(char __user *name, int len)
1544{
1545 int i, errno;
1546
1547 if (len < 0)
1548 return -EINVAL;
1549 down_read(&uts_sem);
1550 i = 1 + strlen(system_utsname.nodename);
1551 if (i > len)
1552 i = len;
1553 errno = 0;
1554 if (copy_to_user(name, system_utsname.nodename, i))
1555 errno = -EFAULT;
1556 up_read(&uts_sem);
1557 return errno;
1558}
1559
1560#endif
1561
1562/*
1563 * Only setdomainname; getdomainname can be implemented by calling
1564 * uname()
1565 */
1566asmlinkage long sys_setdomainname(char __user *name, int len)
1567{
1568 int errno;
1569 char tmp[__NEW_UTS_LEN];
1570
1571 if (!capable(CAP_SYS_ADMIN))
1572 return -EPERM;
1573 if (len < 0 || len > __NEW_UTS_LEN)
1574 return -EINVAL;
1575
1576 down_write(&uts_sem);
1577 errno = -EFAULT;
1578 if (!copy_from_user(tmp, name, len)) {
1579 memcpy(system_utsname.domainname, tmp, len);
1580 system_utsname.domainname[len] = 0;
1581 errno = 0;
1582 }
1583 up_write(&uts_sem);
1584 return errno;
1585}
1586
1587asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1588{
1589 if (resource >= RLIM_NLIMITS)
1590 return -EINVAL;
1591 else {
1592 struct rlimit value;
1593 task_lock(current->group_leader);
1594 value = current->signal->rlim[resource];
1595 task_unlock(current->group_leader);
1596 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1597 }
1598}
1599
1600#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1601
1602/*
1603 * Back compatibility for getrlimit. Needed for some apps.
1604 */
1605
1606asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1607{
1608 struct rlimit x;
1609 if (resource >= RLIM_NLIMITS)
1610 return -EINVAL;
1611
1612 task_lock(current->group_leader);
1613 x = current->signal->rlim[resource];
1614 task_unlock(current->group_leader);
1615 if(x.rlim_cur > 0x7FFFFFFF)
1616 x.rlim_cur = 0x7FFFFFFF;
1617 if(x.rlim_max > 0x7FFFFFFF)
1618 x.rlim_max = 0x7FFFFFFF;
1619 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1620}
1621
1622#endif
1623
1624asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1625{
1626 struct rlimit new_rlim, *old_rlim;
1627 int retval;
1628
1629 if (resource >= RLIM_NLIMITS)
1630 return -EINVAL;
1631 if(copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1632 return -EFAULT;
1633 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1634 return -EINVAL;
1635 old_rlim = current->signal->rlim + resource;
1636 if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1637 !capable(CAP_SYS_RESOURCE))
1638 return -EPERM;
1639 if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
1640 return -EPERM;
1641
1642 retval = security_task_setrlimit(resource, &new_rlim);
1643 if (retval)
1644 return retval;
1645
1646 task_lock(current->group_leader);
1647 *old_rlim = new_rlim;
1648 task_unlock(current->group_leader);
1649
1650 if (resource == RLIMIT_CPU && new_rlim.rlim_cur != RLIM_INFINITY &&
1651 (cputime_eq(current->signal->it_prof_expires, cputime_zero) ||
1652 new_rlim.rlim_cur <= cputime_to_secs(
1653 current->signal->it_prof_expires))) {
1654 cputime_t cputime = secs_to_cputime(new_rlim.rlim_cur);
1655 read_lock(&tasklist_lock);
1656 spin_lock_irq(&current->sighand->siglock);
1657 set_process_cpu_timer(current, CPUCLOCK_PROF,
1658 &cputime, NULL);
1659 spin_unlock_irq(&current->sighand->siglock);
1660 read_unlock(&tasklist_lock);
1661 }
1662
1663 return 0;
1664}
1665
1666/*
1667 * It would make sense to put struct rusage in the task_struct,
1668 * except that would make the task_struct be *really big*. After
1669 * task_struct gets moved into malloc'ed memory, it would
1670 * make sense to do this. It will make moving the rest of the information
1671 * a lot simpler! (Which we're not doing right now because we're not
1672 * measuring them yet).
1673 *
1674 * This expects to be called with tasklist_lock read-locked or better,
1675 * and the siglock not locked. It may momentarily take the siglock.
1676 *
1677 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1678 * races with threads incrementing their own counters. But since word
1679 * reads are atomic, we either get new values or old values and we don't
1680 * care which for the sums. We always take the siglock to protect reading
1681 * the c* fields from p->signal from races with exit.c updating those
1682 * fields when reaping, so a sample either gets all the additions of a
1683 * given child after it's reaped, or none so this sample is before reaping.
1684 */
1685
1686static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1687{
1688 struct task_struct *t;
1689 unsigned long flags;
1690 cputime_t utime, stime;
1691
1692 memset((char *) r, 0, sizeof *r);
1693
1694 if (unlikely(!p->signal))
1695 return;
1696
1697 switch (who) {
1698 case RUSAGE_CHILDREN:
1699 spin_lock_irqsave(&p->sighand->siglock, flags);
1700 utime = p->signal->cutime;
1701 stime = p->signal->cstime;
1702 r->ru_nvcsw = p->signal->cnvcsw;
1703 r->ru_nivcsw = p->signal->cnivcsw;
1704 r->ru_minflt = p->signal->cmin_flt;
1705 r->ru_majflt = p->signal->cmaj_flt;
1706 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1707 cputime_to_timeval(utime, &r->ru_utime);
1708 cputime_to_timeval(stime, &r->ru_stime);
1709 break;
1710 case RUSAGE_SELF:
1711 spin_lock_irqsave(&p->sighand->siglock, flags);
1712 utime = stime = cputime_zero;
1713 goto sum_group;
1714 case RUSAGE_BOTH:
1715 spin_lock_irqsave(&p->sighand->siglock, flags);
1716 utime = p->signal->cutime;
1717 stime = p->signal->cstime;
1718 r->ru_nvcsw = p->signal->cnvcsw;
1719 r->ru_nivcsw = p->signal->cnivcsw;
1720 r->ru_minflt = p->signal->cmin_flt;
1721 r->ru_majflt = p->signal->cmaj_flt;
1722 sum_group:
1723 utime = cputime_add(utime, p->signal->utime);
1724 stime = cputime_add(stime, p->signal->stime);
1725 r->ru_nvcsw += p->signal->nvcsw;
1726 r->ru_nivcsw += p->signal->nivcsw;
1727 r->ru_minflt += p->signal->min_flt;
1728 r->ru_majflt += p->signal->maj_flt;
1729 t = p;
1730 do {
1731 utime = cputime_add(utime, t->utime);
1732 stime = cputime_add(stime, t->stime);
1733 r->ru_nvcsw += t->nvcsw;
1734 r->ru_nivcsw += t->nivcsw;
1735 r->ru_minflt += t->min_flt;
1736 r->ru_majflt += t->maj_flt;
1737 t = next_thread(t);
1738 } while (t != p);
1739 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1740 cputime_to_timeval(utime, &r->ru_utime);
1741 cputime_to_timeval(stime, &r->ru_stime);
1742 break;
1743 default:
1744 BUG();
1745 }
1746}
1747
1748int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1749{
1750 struct rusage r;
1751 read_lock(&tasklist_lock);
1752 k_getrusage(p, who, &r);
1753 read_unlock(&tasklist_lock);
1754 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1755}
1756
1757asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
1758{
1759 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
1760 return -EINVAL;
1761 return getrusage(current, who, ru);
1762}
1763
1764asmlinkage long sys_umask(int mask)
1765{
1766 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1767 return mask;
1768}
1769
1770asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
1771 unsigned long arg4, unsigned long arg5)
1772{
1773 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1776 if (error)
1777 return error;
1778
1779 switch (option) {
1780 case PR_SET_PDEATHSIG:
Jesper Juhl0730ded2005-09-06 15:17:37 -07001781 if (!valid_signal(arg2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 error = -EINVAL;
1783 break;
1784 }
Jesper Juhl0730ded2005-09-06 15:17:37 -07001785 current->pdeath_signal = arg2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 break;
1787 case PR_GET_PDEATHSIG:
1788 error = put_user(current->pdeath_signal, (int __user *)arg2);
1789 break;
1790 case PR_GET_DUMPABLE:
Michael Kerrisk2030c0f2005-09-16 19:28:02 -07001791 error = current->mm->dumpable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 break;
1793 case PR_SET_DUMPABLE:
Alan Coxd6e71142005-06-23 00:09:43 -07001794 if (arg2 < 0 || arg2 > 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 error = -EINVAL;
1796 break;
1797 }
1798 current->mm->dumpable = arg2;
1799 break;
1800
1801 case PR_SET_UNALIGN:
1802 error = SET_UNALIGN_CTL(current, arg2);
1803 break;
1804 case PR_GET_UNALIGN:
1805 error = GET_UNALIGN_CTL(current, arg2);
1806 break;
1807 case PR_SET_FPEMU:
1808 error = SET_FPEMU_CTL(current, arg2);
1809 break;
1810 case PR_GET_FPEMU:
1811 error = GET_FPEMU_CTL(current, arg2);
1812 break;
1813 case PR_SET_FPEXC:
1814 error = SET_FPEXC_CTL(current, arg2);
1815 break;
1816 case PR_GET_FPEXC:
1817 error = GET_FPEXC_CTL(current, arg2);
1818 break;
1819 case PR_GET_TIMING:
1820 error = PR_TIMING_STATISTICAL;
1821 break;
1822 case PR_SET_TIMING:
1823 if (arg2 == PR_TIMING_STATISTICAL)
1824 error = 0;
1825 else
1826 error = -EINVAL;
1827 break;
1828
1829 case PR_GET_KEEPCAPS:
1830 if (current->keep_capabilities)
1831 error = 1;
1832 break;
1833 case PR_SET_KEEPCAPS:
1834 if (arg2 != 0 && arg2 != 1) {
1835 error = -EINVAL;
1836 break;
1837 }
1838 current->keep_capabilities = arg2;
1839 break;
1840 case PR_SET_NAME: {
1841 struct task_struct *me = current;
1842 unsigned char ncomm[sizeof(me->comm)];
1843
1844 ncomm[sizeof(me->comm)-1] = 0;
1845 if (strncpy_from_user(ncomm, (char __user *)arg2,
1846 sizeof(me->comm)-1) < 0)
1847 return -EFAULT;
1848 set_task_comm(me, ncomm);
1849 return 0;
1850 }
1851 case PR_GET_NAME: {
1852 struct task_struct *me = current;
1853 unsigned char tcomm[sizeof(me->comm)];
1854
1855 get_task_comm(tcomm, me);
1856 if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
1857 return -EFAULT;
1858 return 0;
1859 }
1860 default:
1861 error = -EINVAL;
1862 break;
1863 }
1864 return error;
1865}