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