]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/sys.c
[SCTP]: Don't do CRC32C checksum over loopback.
[net-next-2.6.git] / kernel / sys.c
CommitLineData
1da177e4
LT
1/*
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>
dc009d92
EB
19#include <linux/kernel.h>
20#include <linux/kexec.h>
1da177e4 21#include <linux/workqueue.h>
c59ede7b 22#include <linux/capability.h>
1da177e4
LT
23#include <linux/device.h>
24#include <linux/key.h>
25#include <linux/times.h>
26#include <linux/posix-timers.h>
27#include <linux/security.h>
28#include <linux/dcookies.h>
29#include <linux/suspend.h>
30#include <linux/tty.h>
7ed20e1a 31#include <linux/signal.h>
9f46080c 32#include <linux/cn_proc.h>
1da177e4
LT
33
34#include <linux/compat.h>
35#include <linux/syscalls.h>
00d7c05a 36#include <linux/kprobes.h>
1da177e4
LT
37
38#include <asm/uaccess.h>
39#include <asm/io.h>
40#include <asm/unistd.h>
41
42#ifndef SET_UNALIGN_CTL
43# define SET_UNALIGN_CTL(a,b) (-EINVAL)
44#endif
45#ifndef GET_UNALIGN_CTL
46# define GET_UNALIGN_CTL(a,b) (-EINVAL)
47#endif
48#ifndef SET_FPEMU_CTL
49# define SET_FPEMU_CTL(a,b) (-EINVAL)
50#endif
51#ifndef GET_FPEMU_CTL
52# define GET_FPEMU_CTL(a,b) (-EINVAL)
53#endif
54#ifndef SET_FPEXC_CTL
55# define SET_FPEXC_CTL(a,b) (-EINVAL)
56#endif
57#ifndef GET_FPEXC_CTL
58# define GET_FPEXC_CTL(a,b) (-EINVAL)
59#endif
60
61/*
62 * this is where the system-wide overflow UID and GID are defined, for
63 * architectures that now have 32-bit UID/GID but didn't in the past
64 */
65
66int overflowuid = DEFAULT_OVERFLOWUID;
67int overflowgid = DEFAULT_OVERFLOWGID;
68
69#ifdef CONFIG_UID16
70EXPORT_SYMBOL(overflowuid);
71EXPORT_SYMBOL(overflowgid);
72#endif
73
74/*
75 * the same as above, but for filesystems which can only store a 16-bit
76 * UID and GID. as such, this is needed on all architectures
77 */
78
79int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
80int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
81
82EXPORT_SYMBOL(fs_overflowuid);
83EXPORT_SYMBOL(fs_overflowgid);
84
85/*
86 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
87 */
88
89int C_A_D = 1;
90int cad_pid = 1;
91
92/*
93 * Notifier list for kernel code which wants to be called
94 * at shutdown. This is used to stop any idling DMA operations
95 * and the like.
96 */
97
e041c683
AS
98static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
99
100/*
101 * Notifier chain core routines. The exported routines below
102 * are layered on top of these, with appropriate locking added.
103 */
104
105static int notifier_chain_register(struct notifier_block **nl,
106 struct notifier_block *n)
107{
108 while ((*nl) != NULL) {
109 if (n->priority > (*nl)->priority)
110 break;
111 nl = &((*nl)->next);
112 }
113 n->next = *nl;
114 rcu_assign_pointer(*nl, n);
115 return 0;
116}
117
118static int notifier_chain_unregister(struct notifier_block **nl,
119 struct notifier_block *n)
120{
121 while ((*nl) != NULL) {
122 if ((*nl) == n) {
123 rcu_assign_pointer(*nl, n->next);
124 return 0;
125 }
126 nl = &((*nl)->next);
127 }
128 return -ENOENT;
129}
130
131static int __kprobes notifier_call_chain(struct notifier_block **nl,
132 unsigned long val, void *v)
133{
134 int ret = NOTIFY_DONE;
135 struct notifier_block *nb;
136
137 nb = rcu_dereference(*nl);
138 while (nb) {
139 ret = nb->notifier_call(nb, val, v);
140 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
141 break;
142 nb = rcu_dereference(nb->next);
143 }
144 return ret;
145}
146
147/*
148 * Atomic notifier chain routines. Registration and unregistration
149 * use a mutex, and call_chain is synchronized by RCU (no locks).
150 */
1da177e4
LT
151
152/**
e041c683
AS
153 * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
154 * @nh: Pointer to head of the atomic notifier chain
1da177e4
LT
155 * @n: New entry in notifier chain
156 *
e041c683 157 * Adds a notifier to an atomic notifier chain.
1da177e4
LT
158 *
159 * Currently always returns zero.
160 */
e041c683
AS
161
162int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
163 struct notifier_block *n)
164{
165 unsigned long flags;
166 int ret;
167
168 spin_lock_irqsave(&nh->lock, flags);
169 ret = notifier_chain_register(&nh->head, n);
170 spin_unlock_irqrestore(&nh->lock, flags);
171 return ret;
172}
173
174EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
175
176/**
177 * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
178 * @nh: Pointer to head of the atomic notifier chain
179 * @n: Entry to remove from notifier chain
180 *
181 * Removes a notifier from an atomic notifier chain.
182 *
183 * Returns zero on success or %-ENOENT on failure.
184 */
185int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
186 struct notifier_block *n)
187{
188 unsigned long flags;
189 int ret;
190
191 spin_lock_irqsave(&nh->lock, flags);
192 ret = notifier_chain_unregister(&nh->head, n);
193 spin_unlock_irqrestore(&nh->lock, flags);
194 synchronize_rcu();
195 return ret;
196}
197
198EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
199
200/**
201 * atomic_notifier_call_chain - Call functions in an atomic notifier chain
202 * @nh: Pointer to head of the atomic notifier chain
203 * @val: Value passed unmodified to notifier function
204 * @v: Pointer passed unmodified to notifier function
205 *
206 * Calls each function in a notifier chain in turn. The functions
207 * run in an atomic context, so they must not block.
208 * This routine uses RCU to synchronize with changes to the chain.
209 *
210 * If the return value of the notifier can be and'ed
211 * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain
212 * will return immediately, with the return value of
213 * the notifier function which halted execution.
214 * Otherwise the return value is the return value
215 * of the last notifier function called.
216 */
1da177e4 217
e041c683
AS
218int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
219 unsigned long val, void *v)
1da177e4 220{
e041c683
AS
221 int ret;
222
223 rcu_read_lock();
224 ret = notifier_call_chain(&nh->head, val, v);
225 rcu_read_unlock();
226 return ret;
1da177e4
LT
227}
228
e041c683
AS
229EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
230
231/*
232 * Blocking notifier chain routines. All access to the chain is
233 * synchronized by an rwsem.
234 */
1da177e4
LT
235
236/**
e041c683
AS
237 * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
238 * @nh: Pointer to head of the blocking notifier chain
1da177e4
LT
239 * @n: New entry in notifier chain
240 *
e041c683
AS
241 * Adds a notifier to a blocking notifier chain.
242 * Must be called in process context.
1da177e4 243 *
e041c683 244 * Currently always returns zero.
1da177e4
LT
245 */
246
e041c683
AS
247int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
248 struct notifier_block *n)
1da177e4 249{
e041c683
AS
250 int ret;
251
252 /*
253 * This code gets used during boot-up, when task switching is
254 * not yet working and interrupts must remain disabled. At
255 * such times we must not call down_write().
256 */
257 if (unlikely(system_state == SYSTEM_BOOTING))
258 return notifier_chain_register(&nh->head, n);
259
260 down_write(&nh->rwsem);
261 ret = notifier_chain_register(&nh->head, n);
262 up_write(&nh->rwsem);
263 return ret;
1da177e4
LT
264}
265
e041c683 266EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
1da177e4
LT
267
268/**
e041c683
AS
269 * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
270 * @nh: Pointer to head of the blocking notifier chain
271 * @n: Entry to remove from notifier chain
272 *
273 * Removes a notifier from a blocking notifier chain.
274 * Must be called from process context.
275 *
276 * Returns zero on success or %-ENOENT on failure.
277 */
278int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
279 struct notifier_block *n)
280{
281 int ret;
282
283 /*
284 * This code gets used during boot-up, when task switching is
285 * not yet working and interrupts must remain disabled. At
286 * such times we must not call down_write().
287 */
288 if (unlikely(system_state == SYSTEM_BOOTING))
289 return notifier_chain_unregister(&nh->head, n);
290
291 down_write(&nh->rwsem);
292 ret = notifier_chain_unregister(&nh->head, n);
293 up_write(&nh->rwsem);
294 return ret;
295}
296
297EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
298
299/**
300 * blocking_notifier_call_chain - Call functions in a blocking notifier chain
301 * @nh: Pointer to head of the blocking notifier chain
1da177e4
LT
302 * @val: Value passed unmodified to notifier function
303 * @v: Pointer passed unmodified to notifier function
304 *
e041c683
AS
305 * Calls each function in a notifier chain in turn. The functions
306 * run in a process context, so they are allowed to block.
1da177e4 307 *
e041c683
AS
308 * If the return value of the notifier can be and'ed
309 * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain
1da177e4
LT
310 * will return immediately, with the return value of
311 * the notifier function which halted execution.
e041c683 312 * Otherwise the return value is the return value
1da177e4
LT
313 * of the last notifier function called.
314 */
315
e041c683
AS
316int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
317 unsigned long val, void *v)
1da177e4 318{
e041c683
AS
319 int ret;
320
321 down_read(&nh->rwsem);
322 ret = notifier_call_chain(&nh->head, val, v);
323 up_read(&nh->rwsem);
1da177e4
LT
324 return ret;
325}
326
e041c683
AS
327EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
328
329/*
330 * Raw notifier chain routines. There is no protection;
331 * the caller must provide it. Use at your own risk!
332 */
333
334/**
335 * raw_notifier_chain_register - Add notifier to a raw notifier chain
336 * @nh: Pointer to head of the raw notifier chain
337 * @n: New entry in notifier chain
338 *
339 * Adds a notifier to a raw notifier chain.
340 * All locking must be provided by the caller.
341 *
342 * Currently always returns zero.
343 */
344
345int raw_notifier_chain_register(struct raw_notifier_head *nh,
346 struct notifier_block *n)
347{
348 return notifier_chain_register(&nh->head, n);
349}
350
351EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
352
353/**
354 * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
355 * @nh: Pointer to head of the raw notifier chain
356 * @n: Entry to remove from notifier chain
357 *
358 * Removes a notifier from a raw notifier chain.
359 * All locking must be provided by the caller.
360 *
361 * Returns zero on success or %-ENOENT on failure.
362 */
363int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
364 struct notifier_block *n)
365{
366 return notifier_chain_unregister(&nh->head, n);
367}
368
369EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
370
371/**
372 * raw_notifier_call_chain - Call functions in a raw notifier chain
373 * @nh: Pointer to head of the raw notifier chain
374 * @val: Value passed unmodified to notifier function
375 * @v: Pointer passed unmodified to notifier function
376 *
377 * Calls each function in a notifier chain in turn. The functions
378 * run in an undefined context.
379 * All locking must be provided by the caller.
380 *
381 * If the return value of the notifier can be and'ed
382 * with %NOTIFY_STOP_MASK then raw_notifier_call_chain
383 * will return immediately, with the return value of
384 * the notifier function which halted execution.
385 * Otherwise the return value is the return value
386 * of the last notifier function called.
387 */
388
389int raw_notifier_call_chain(struct raw_notifier_head *nh,
390 unsigned long val, void *v)
391{
392 return notifier_call_chain(&nh->head, val, v);
393}
394
395EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
1da177e4
LT
396
397/**
398 * register_reboot_notifier - Register function to be called at reboot time
399 * @nb: Info about notifier function to be called
400 *
401 * Registers a function with the list of functions
402 * to be called at reboot time.
403 *
e041c683 404 * Currently always returns zero, as blocking_notifier_chain_register
1da177e4
LT
405 * always returns zero.
406 */
407
408int register_reboot_notifier(struct notifier_block * nb)
409{
e041c683 410 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
1da177e4
LT
411}
412
413EXPORT_SYMBOL(register_reboot_notifier);
414
415/**
416 * unregister_reboot_notifier - Unregister previously registered reboot notifier
417 * @nb: Hook to be unregistered
418 *
419 * Unregisters a previously registered reboot
420 * notifier function.
421 *
422 * Returns zero on success, or %-ENOENT on failure.
423 */
424
425int unregister_reboot_notifier(struct notifier_block * nb)
426{
e041c683 427 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
1da177e4
LT
428}
429
430EXPORT_SYMBOL(unregister_reboot_notifier);
431
432static int set_one_prio(struct task_struct *p, int niceval, int error)
433{
434 int no_nice;
435
436 if (p->uid != current->euid &&
437 p->euid != current->euid && !capable(CAP_SYS_NICE)) {
438 error = -EPERM;
439 goto out;
440 }
e43379f1 441 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
1da177e4
LT
442 error = -EACCES;
443 goto out;
444 }
445 no_nice = security_task_setnice(p, niceval);
446 if (no_nice) {
447 error = no_nice;
448 goto out;
449 }
450 if (error == -ESRCH)
451 error = 0;
452 set_user_nice(p, niceval);
453out:
454 return error;
455}
456
457asmlinkage long sys_setpriority(int which, int who, int niceval)
458{
459 struct task_struct *g, *p;
460 struct user_struct *user;
461 int error = -EINVAL;
462
463 if (which > 2 || which < 0)
464 goto out;
465
466 /* normalize: avoid signed division (rounding problems) */
467 error = -ESRCH;
468 if (niceval < -20)
469 niceval = -20;
470 if (niceval > 19)
471 niceval = 19;
472
473 read_lock(&tasklist_lock);
474 switch (which) {
475 case PRIO_PROCESS:
476 if (!who)
477 who = current->pid;
478 p = find_task_by_pid(who);
479 if (p)
480 error = set_one_prio(p, niceval, error);
481 break;
482 case PRIO_PGRP:
483 if (!who)
484 who = process_group(current);
485 do_each_task_pid(who, PIDTYPE_PGID, p) {
486 error = set_one_prio(p, niceval, error);
487 } while_each_task_pid(who, PIDTYPE_PGID, p);
488 break;
489 case PRIO_USER:
490 user = current->user;
491 if (!who)
492 who = current->uid;
493 else
494 if ((who != current->uid) && !(user = find_user(who)))
495 goto out_unlock; /* No processes for this user */
496
497 do_each_thread(g, p)
498 if (p->uid == who)
499 error = set_one_prio(p, niceval, error);
500 while_each_thread(g, p);
501 if (who != current->uid)
502 free_uid(user); /* For find_user() */
503 break;
504 }
505out_unlock:
506 read_unlock(&tasklist_lock);
507out:
508 return error;
509}
510
511/*
512 * Ugh. To avoid negative return values, "getpriority()" will
513 * not return the normal nice-value, but a negated value that
514 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
515 * to stay compatible.
516 */
517asmlinkage long sys_getpriority(int which, int who)
518{
519 struct task_struct *g, *p;
520 struct user_struct *user;
521 long niceval, retval = -ESRCH;
522
523 if (which > 2 || which < 0)
524 return -EINVAL;
525
526 read_lock(&tasklist_lock);
527 switch (which) {
528 case PRIO_PROCESS:
529 if (!who)
530 who = current->pid;
531 p = find_task_by_pid(who);
532 if (p) {
533 niceval = 20 - task_nice(p);
534 if (niceval > retval)
535 retval = niceval;
536 }
537 break;
538 case PRIO_PGRP:
539 if (!who)
540 who = process_group(current);
541 do_each_task_pid(who, PIDTYPE_PGID, p) {
542 niceval = 20 - task_nice(p);
543 if (niceval > retval)
544 retval = niceval;
545 } while_each_task_pid(who, PIDTYPE_PGID, p);
546 break;
547 case PRIO_USER:
548 user = current->user;
549 if (!who)
550 who = current->uid;
551 else
552 if ((who != current->uid) && !(user = find_user(who)))
553 goto out_unlock; /* No processes for this user */
554
555 do_each_thread(g, p)
556 if (p->uid == who) {
557 niceval = 20 - task_nice(p);
558 if (niceval > retval)
559 retval = niceval;
560 }
561 while_each_thread(g, p);
562 if (who != current->uid)
563 free_uid(user); /* for find_user() */
564 break;
565 }
566out_unlock:
567 read_unlock(&tasklist_lock);
568
569 return retval;
570}
571
e4c94330
EB
572/**
573 * emergency_restart - reboot the system
574 *
575 * Without shutting down any hardware or taking any locks
576 * reboot the system. This is called when we know we are in
577 * trouble so this is our best effort to reboot. This is
578 * safe to call in interrupt context.
579 */
7c903473
EB
580void emergency_restart(void)
581{
582 machine_emergency_restart();
583}
584EXPORT_SYMBOL_GPL(emergency_restart);
585
e4c94330 586void kernel_restart_prepare(char *cmd)
4a00ea1e 587{
e041c683 588 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
4a00ea1e 589 system_state = SYSTEM_RESTART;
4a00ea1e 590 device_shutdown();
e4c94330 591}
1e5d5331
RD
592
593/**
594 * kernel_restart - reboot the system
595 * @cmd: pointer to buffer containing command to execute for restart
b8887e6e 596 * or %NULL
1e5d5331
RD
597 *
598 * Shutdown everything and perform a clean reboot.
599 * This is not safe to call in interrupt context.
600 */
e4c94330
EB
601void kernel_restart(char *cmd)
602{
603 kernel_restart_prepare(cmd);
4a00ea1e
EB
604 if (!cmd) {
605 printk(KERN_EMERG "Restarting system.\n");
606 } else {
607 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
608 }
609 printk(".\n");
610 machine_restart(cmd);
611}
612EXPORT_SYMBOL_GPL(kernel_restart);
613
e4c94330
EB
614/**
615 * kernel_kexec - reboot the system
616 *
617 * Move into place and start executing a preloaded standalone
618 * executable. If nothing was preloaded return an error.
619 */
4a00ea1e
EB
620void kernel_kexec(void)
621{
622#ifdef CONFIG_KEXEC
623 struct kimage *image;
4bb8089c 624 image = xchg(&kexec_image, NULL);
4a00ea1e
EB
625 if (!image) {
626 return;
627 }
e4c94330 628 kernel_restart_prepare(NULL);
4a00ea1e
EB
629 printk(KERN_EMERG "Starting new kernel\n");
630 machine_shutdown();
631 machine_kexec(image);
632#endif
633}
634EXPORT_SYMBOL_GPL(kernel_kexec);
635
729b4d4c
AS
636void kernel_shutdown_prepare(enum system_states state)
637{
e041c683 638 blocking_notifier_call_chain(&reboot_notifier_list,
729b4d4c
AS
639 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
640 system_state = state;
641 device_shutdown();
642}
e4c94330
EB
643/**
644 * kernel_halt - halt the system
645 *
646 * Shutdown everything and perform a clean system halt.
647 */
e4c94330
EB
648void kernel_halt(void)
649{
729b4d4c 650 kernel_shutdown_prepare(SYSTEM_HALT);
4a00ea1e
EB
651 printk(KERN_EMERG "System halted.\n");
652 machine_halt();
653}
729b4d4c 654
4a00ea1e
EB
655EXPORT_SYMBOL_GPL(kernel_halt);
656
e4c94330
EB
657/**
658 * kernel_power_off - power_off the system
659 *
660 * Shutdown everything and perform a clean system power_off.
661 */
e4c94330
EB
662void kernel_power_off(void)
663{
729b4d4c 664 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
4a00ea1e
EB
665 printk(KERN_EMERG "Power down.\n");
666 machine_power_off();
667}
668EXPORT_SYMBOL_GPL(kernel_power_off);
1da177e4
LT
669/*
670 * Reboot system call: for obvious reasons only root may call it,
671 * and even root needs to set up some magic numbers in the registers
672 * so that some mistake won't make this reboot the whole machine.
673 * You can also set the meaning of the ctrl-alt-del-key here.
674 *
675 * reboot doesn't sync: do that yourself before calling this.
676 */
677asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
678{
679 char buffer[256];
680
681 /* We only trust the superuser with rebooting the system. */
682 if (!capable(CAP_SYS_BOOT))
683 return -EPERM;
684
685 /* For safety, we require "magic" arguments. */
686 if (magic1 != LINUX_REBOOT_MAGIC1 ||
687 (magic2 != LINUX_REBOOT_MAGIC2 &&
688 magic2 != LINUX_REBOOT_MAGIC2A &&
689 magic2 != LINUX_REBOOT_MAGIC2B &&
690 magic2 != LINUX_REBOOT_MAGIC2C))
691 return -EINVAL;
692
5e38291d
EB
693 /* Instead of trying to make the power_off code look like
694 * halt when pm_power_off is not set do it the easy way.
695 */
696 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
697 cmd = LINUX_REBOOT_CMD_HALT;
698
1da177e4
LT
699 lock_kernel();
700 switch (cmd) {
701 case LINUX_REBOOT_CMD_RESTART:
4a00ea1e 702 kernel_restart(NULL);
1da177e4
LT
703 break;
704
705 case LINUX_REBOOT_CMD_CAD_ON:
706 C_A_D = 1;
707 break;
708
709 case LINUX_REBOOT_CMD_CAD_OFF:
710 C_A_D = 0;
711 break;
712
713 case LINUX_REBOOT_CMD_HALT:
4a00ea1e 714 kernel_halt();
1da177e4
LT
715 unlock_kernel();
716 do_exit(0);
717 break;
718
719 case LINUX_REBOOT_CMD_POWER_OFF:
4a00ea1e 720 kernel_power_off();
1da177e4
LT
721 unlock_kernel();
722 do_exit(0);
723 break;
724
725 case LINUX_REBOOT_CMD_RESTART2:
726 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
727 unlock_kernel();
728 return -EFAULT;
729 }
730 buffer[sizeof(buffer) - 1] = '\0';
731
4a00ea1e 732 kernel_restart(buffer);
1da177e4
LT
733 break;
734
dc009d92 735 case LINUX_REBOOT_CMD_KEXEC:
4a00ea1e
EB
736 kernel_kexec();
737 unlock_kernel();
738 return -EINVAL;
739
1da177e4
LT
740#ifdef CONFIG_SOFTWARE_SUSPEND
741 case LINUX_REBOOT_CMD_SW_SUSPEND:
742 {
743 int ret = software_suspend();
744 unlock_kernel();
745 return ret;
746 }
747#endif
748
749 default:
750 unlock_kernel();
751 return -EINVAL;
752 }
753 unlock_kernel();
754 return 0;
755}
756
757static void deferred_cad(void *dummy)
758{
abcd9e51 759 kernel_restart(NULL);
1da177e4
LT
760}
761
762/*
763 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
764 * As it's called within an interrupt, it may NOT sync: the only choice
765 * is whether to reboot at once, or just ignore the ctrl-alt-del.
766 */
767void ctrl_alt_del(void)
768{
769 static DECLARE_WORK(cad_work, deferred_cad, NULL);
770
771 if (C_A_D)
772 schedule_work(&cad_work);
773 else
774 kill_proc(cad_pid, SIGINT, 1);
775}
776
777
778/*
779 * Unprivileged users may change the real gid to the effective gid
780 * or vice versa. (BSD-style)
781 *
782 * If you set the real gid at all, or set the effective gid to a value not
783 * equal to the real gid, then the saved gid is set to the new effective gid.
784 *
785 * This makes it possible for a setgid program to completely drop its
786 * privileges, which is often a useful assertion to make when you are doing
787 * a security audit over a program.
788 *
789 * The general idea is that a program which uses just setregid() will be
790 * 100% compatible with BSD. A program which uses just setgid() will be
791 * 100% compatible with POSIX with saved IDs.
792 *
793 * SMP: There are not races, the GIDs are checked only by filesystem
794 * operations (as far as semantic preservation is concerned).
795 */
796asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
797{
798 int old_rgid = current->gid;
799 int old_egid = current->egid;
800 int new_rgid = old_rgid;
801 int new_egid = old_egid;
802 int retval;
803
804 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
805 if (retval)
806 return retval;
807
808 if (rgid != (gid_t) -1) {
809 if ((old_rgid == rgid) ||
810 (current->egid==rgid) ||
811 capable(CAP_SETGID))
812 new_rgid = rgid;
813 else
814 return -EPERM;
815 }
816 if (egid != (gid_t) -1) {
817 if ((old_rgid == egid) ||
818 (current->egid == egid) ||
819 (current->sgid == egid) ||
820 capable(CAP_SETGID))
821 new_egid = egid;
822 else {
823 return -EPERM;
824 }
825 }
826 if (new_egid != old_egid)
827 {
d6e71144 828 current->mm->dumpable = suid_dumpable;
d59dd462 829 smp_wmb();
1da177e4
LT
830 }
831 if (rgid != (gid_t) -1 ||
832 (egid != (gid_t) -1 && egid != old_rgid))
833 current->sgid = new_egid;
834 current->fsgid = new_egid;
835 current->egid = new_egid;
836 current->gid = new_rgid;
837 key_fsgid_changed(current);
9f46080c 838 proc_id_connector(current, PROC_EVENT_GID);
1da177e4
LT
839 return 0;
840}
841
842/*
843 * setgid() is implemented like SysV w/ SAVED_IDS
844 *
845 * SMP: Same implicit races as above.
846 */
847asmlinkage long sys_setgid(gid_t gid)
848{
849 int old_egid = current->egid;
850 int retval;
851
852 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
853 if (retval)
854 return retval;
855
856 if (capable(CAP_SETGID))
857 {
858 if(old_egid != gid)
859 {
d6e71144 860 current->mm->dumpable = suid_dumpable;
d59dd462 861 smp_wmb();
1da177e4
LT
862 }
863 current->gid = current->egid = current->sgid = current->fsgid = gid;
864 }
865 else if ((gid == current->gid) || (gid == current->sgid))
866 {
867 if(old_egid != gid)
868 {
d6e71144 869 current->mm->dumpable = suid_dumpable;
d59dd462 870 smp_wmb();
1da177e4
LT
871 }
872 current->egid = current->fsgid = gid;
873 }
874 else
875 return -EPERM;
876
877 key_fsgid_changed(current);
9f46080c 878 proc_id_connector(current, PROC_EVENT_GID);
1da177e4
LT
879 return 0;
880}
881
882static int set_user(uid_t new_ruid, int dumpclear)
883{
884 struct user_struct *new_user;
885
886 new_user = alloc_uid(new_ruid);
887 if (!new_user)
888 return -EAGAIN;
889
890 if (atomic_read(&new_user->processes) >=
891 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
892 new_user != &root_user) {
893 free_uid(new_user);
894 return -EAGAIN;
895 }
896
897 switch_uid(new_user);
898
899 if(dumpclear)
900 {
d6e71144 901 current->mm->dumpable = suid_dumpable;
d59dd462 902 smp_wmb();
1da177e4
LT
903 }
904 current->uid = new_ruid;
905 return 0;
906}
907
908/*
909 * Unprivileged users may change the real uid to the effective uid
910 * or vice versa. (BSD-style)
911 *
912 * If you set the real uid at all, or set the effective uid to a value not
913 * equal to the real uid, then the saved uid is set to the new effective uid.
914 *
915 * This makes it possible for a setuid program to completely drop its
916 * privileges, which is often a useful assertion to make when you are doing
917 * a security audit over a program.
918 *
919 * The general idea is that a program which uses just setreuid() will be
920 * 100% compatible with BSD. A program which uses just setuid() will be
921 * 100% compatible with POSIX with saved IDs.
922 */
923asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
924{
925 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
926 int retval;
927
928 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
929 if (retval)
930 return retval;
931
932 new_ruid = old_ruid = current->uid;
933 new_euid = old_euid = current->euid;
934 old_suid = current->suid;
935
936 if (ruid != (uid_t) -1) {
937 new_ruid = ruid;
938 if ((old_ruid != ruid) &&
939 (current->euid != ruid) &&
940 !capable(CAP_SETUID))
941 return -EPERM;
942 }
943
944 if (euid != (uid_t) -1) {
945 new_euid = euid;
946 if ((old_ruid != euid) &&
947 (current->euid != euid) &&
948 (current->suid != euid) &&
949 !capable(CAP_SETUID))
950 return -EPERM;
951 }
952
953 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
954 return -EAGAIN;
955
956 if (new_euid != old_euid)
957 {
d6e71144 958 current->mm->dumpable = suid_dumpable;
d59dd462 959 smp_wmb();
1da177e4
LT
960 }
961 current->fsuid = current->euid = new_euid;
962 if (ruid != (uid_t) -1 ||
963 (euid != (uid_t) -1 && euid != old_ruid))
964 current->suid = current->euid;
965 current->fsuid = current->euid;
966
967 key_fsuid_changed(current);
9f46080c 968 proc_id_connector(current, PROC_EVENT_UID);
1da177e4
LT
969
970 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
971}
972
973
974
975/*
976 * setuid() is implemented like SysV with SAVED_IDS
977 *
978 * Note that SAVED_ID's is deficient in that a setuid root program
979 * like sendmail, for example, cannot set its uid to be a normal
980 * user and then switch back, because if you're root, setuid() sets
981 * the saved uid too. If you don't like this, blame the bright people
982 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
983 * will allow a root program to temporarily drop privileges and be able to
984 * regain them by swapping the real and effective uid.
985 */
986asmlinkage long sys_setuid(uid_t uid)
987{
988 int old_euid = current->euid;
989 int old_ruid, old_suid, new_ruid, new_suid;
990 int retval;
991
992 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
993 if (retval)
994 return retval;
995
996 old_ruid = new_ruid = current->uid;
997 old_suid = current->suid;
998 new_suid = old_suid;
999
1000 if (capable(CAP_SETUID)) {
1001 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
1002 return -EAGAIN;
1003 new_suid = uid;
1004 } else if ((uid != current->uid) && (uid != new_suid))
1005 return -EPERM;
1006
1007 if (old_euid != uid)
1008 {
d6e71144 1009 current->mm->dumpable = suid_dumpable;
d59dd462 1010 smp_wmb();
1da177e4
LT
1011 }
1012 current->fsuid = current->euid = uid;
1013 current->suid = new_suid;
1014
1015 key_fsuid_changed(current);
9f46080c 1016 proc_id_connector(current, PROC_EVENT_UID);
1da177e4
LT
1017
1018 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
1019}
1020
1021
1022/*
1023 * This function implements a generic ability to update ruid, euid,
1024 * and suid. This allows you to implement the 4.4 compatible seteuid().
1025 */
1026asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
1027{
1028 int old_ruid = current->uid;
1029 int old_euid = current->euid;
1030 int old_suid = current->suid;
1031 int retval;
1032
1033 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
1034 if (retval)
1035 return retval;
1036
1037 if (!capable(CAP_SETUID)) {
1038 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
1039 (ruid != current->euid) && (ruid != current->suid))
1040 return -EPERM;
1041 if ((euid != (uid_t) -1) && (euid != current->uid) &&
1042 (euid != current->euid) && (euid != current->suid))
1043 return -EPERM;
1044 if ((suid != (uid_t) -1) && (suid != current->uid) &&
1045 (suid != current->euid) && (suid != current->suid))
1046 return -EPERM;
1047 }
1048 if (ruid != (uid_t) -1) {
1049 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
1050 return -EAGAIN;
1051 }
1052 if (euid != (uid_t) -1) {
1053 if (euid != current->euid)
1054 {
d6e71144 1055 current->mm->dumpable = suid_dumpable;
d59dd462 1056 smp_wmb();
1da177e4
LT
1057 }
1058 current->euid = euid;
1059 }
1060 current->fsuid = current->euid;
1061 if (suid != (uid_t) -1)
1062 current->suid = suid;
1063
1064 key_fsuid_changed(current);
9f46080c 1065 proc_id_connector(current, PROC_EVENT_UID);
1da177e4
LT
1066
1067 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
1068}
1069
1070asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
1071{
1072 int retval;
1073
1074 if (!(retval = put_user(current->uid, ruid)) &&
1075 !(retval = put_user(current->euid, euid)))
1076 retval = put_user(current->suid, suid);
1077
1078 return retval;
1079}
1080
1081/*
1082 * Same as above, but for rgid, egid, sgid.
1083 */
1084asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1085{
1086 int retval;
1087
1088 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
1089 if (retval)
1090 return retval;
1091
1092 if (!capable(CAP_SETGID)) {
1093 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
1094 (rgid != current->egid) && (rgid != current->sgid))
1095 return -EPERM;
1096 if ((egid != (gid_t) -1) && (egid != current->gid) &&
1097 (egid != current->egid) && (egid != current->sgid))
1098 return -EPERM;
1099 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
1100 (sgid != current->egid) && (sgid != current->sgid))
1101 return -EPERM;
1102 }
1103 if (egid != (gid_t) -1) {
1104 if (egid != current->egid)
1105 {
d6e71144 1106 current->mm->dumpable = suid_dumpable;
d59dd462 1107 smp_wmb();
1da177e4
LT
1108 }
1109 current->egid = egid;
1110 }
1111 current->fsgid = current->egid;
1112 if (rgid != (gid_t) -1)
1113 current->gid = rgid;
1114 if (sgid != (gid_t) -1)
1115 current->sgid = sgid;
1116
1117 key_fsgid_changed(current);
9f46080c 1118 proc_id_connector(current, PROC_EVENT_GID);
1da177e4
LT
1119 return 0;
1120}
1121
1122asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
1123{
1124 int retval;
1125
1126 if (!(retval = put_user(current->gid, rgid)) &&
1127 !(retval = put_user(current->egid, egid)))
1128 retval = put_user(current->sgid, sgid);
1129
1130 return retval;
1131}
1132
1133
1134/*
1135 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
1136 * is used for "access()" and for the NFS daemon (letting nfsd stay at
1137 * whatever uid it wants to). It normally shadows "euid", except when
1138 * explicitly set by setfsuid() or for access..
1139 */
1140asmlinkage long sys_setfsuid(uid_t uid)
1141{
1142 int old_fsuid;
1143
1144 old_fsuid = current->fsuid;
1145 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
1146 return old_fsuid;
1147
1148 if (uid == current->uid || uid == current->euid ||
1149 uid == current->suid || uid == current->fsuid ||
1150 capable(CAP_SETUID))
1151 {
1152 if (uid != old_fsuid)
1153 {
d6e71144 1154 current->mm->dumpable = suid_dumpable;
d59dd462 1155 smp_wmb();
1da177e4
LT
1156 }
1157 current->fsuid = uid;
1158 }
1159
1160 key_fsuid_changed(current);
9f46080c 1161 proc_id_connector(current, PROC_EVENT_UID);
1da177e4
LT
1162
1163 security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
1164
1165 return old_fsuid;
1166}
1167
1168/*
1169