4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/module.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>
15 #include <linux/highuid.h>
17 #include <linux/kernel.h>
18 #include <linux/kexec.h>
19 #include <linux/workqueue.h>
20 #include <linux/capability.h>
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>
29 #include <linux/signal.h>
30 #include <linux/cn_proc.h>
31 #include <linux/getcpu.h>
33 #include <linux/compat.h>
34 #include <linux/syscalls.h>
35 #include <linux/kprobes.h>
37 #include <asm/uaccess.h>
39 #include <asm/unistd.h>
41 #ifndef SET_UNALIGN_CTL
42 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
44 #ifndef GET_UNALIGN_CTL
45 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
48 # define SET_FPEMU_CTL(a,b) (-EINVAL)
51 # define GET_FPEMU_CTL(a,b) (-EINVAL)
54 # define SET_FPEXC_CTL(a,b) (-EINVAL)
57 # define GET_FPEXC_CTL(a,b) (-EINVAL)
60 # define GET_ENDIAN(a,b) (-EINVAL)
63 # define SET_ENDIAN(a,b) (-EINVAL)
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
71 int overflowuid = DEFAULT_OVERFLOWUID;
72 int overflowgid = DEFAULT_OVERFLOWGID;
75 EXPORT_SYMBOL(overflowuid);
76 EXPORT_SYMBOL(overflowgid);
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
84 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
85 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
87 EXPORT_SYMBOL(fs_overflowuid);
88 EXPORT_SYMBOL(fs_overflowgid);
91 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
96 EXPORT_SYMBOL(cad_pid);
99 * Notifier list for kernel code which wants to be called
100 * at shutdown. This is used to stop any idling DMA operations
104 static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
107 * Notifier chain core routines. The exported routines below
108 * are layered on top of these, with appropriate locking added.
111 static int notifier_chain_register(struct notifier_block **nl,
112 struct notifier_block *n)
114 while ((*nl) != NULL) {
115 if (n->priority > (*nl)->priority)
120 rcu_assign_pointer(*nl, n);
124 static int notifier_chain_unregister(struct notifier_block **nl,
125 struct notifier_block *n)
127 while ((*nl) != NULL) {
129 rcu_assign_pointer(*nl, n->next);
137 static int __kprobes notifier_call_chain(struct notifier_block **nl,
138 unsigned long val, void *v)
140 int ret = NOTIFY_DONE;
141 struct notifier_block *nb, *next_nb;
143 nb = rcu_dereference(*nl);
145 next_nb = rcu_dereference(nb->next);
146 ret = nb->notifier_call(nb, val, v);
147 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
155 * Atomic notifier chain routines. Registration and unregistration
156 * use a spinlock, and call_chain is synchronized by RCU (no locks).
160 * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
161 * @nh: Pointer to head of the atomic notifier chain
162 * @n: New entry in notifier chain
164 * Adds a notifier to an atomic notifier chain.
166 * Currently always returns zero.
169 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
170 struct notifier_block *n)
175 spin_lock_irqsave(&nh->lock, flags);
176 ret = notifier_chain_register(&nh->head, n);
177 spin_unlock_irqrestore(&nh->lock, flags);
181 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
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
188 * Removes a notifier from an atomic notifier chain.
190 * Returns zero on success or %-ENOENT on failure.
192 int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
193 struct notifier_block *n)
198 spin_lock_irqsave(&nh->lock, flags);
199 ret = notifier_chain_unregister(&nh->head, n);
200 spin_unlock_irqrestore(&nh->lock, flags);
205 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
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
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.
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.
225 int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
226 unsigned long val, void *v)
231 ret = notifier_call_chain(&nh->head, val, v);
236 EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
239 * Blocking notifier chain routines. All access to the chain is
240 * synchronized by an rwsem.
244 * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
245 * @nh: Pointer to head of the blocking notifier chain
246 * @n: New entry in notifier chain
248 * Adds a notifier to a blocking notifier chain.
249 * Must be called in process context.
251 * Currently always returns zero.
254 int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
255 struct notifier_block *n)
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().
264 if (unlikely(system_state == SYSTEM_BOOTING))
265 return notifier_chain_register(&nh->head, n);
267 down_write(&nh->rwsem);
268 ret = notifier_chain_register(&nh->head, n);
269 up_write(&nh->rwsem);
273 EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
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
280 * Removes a notifier from a blocking notifier chain.
281 * Must be called from process context.
283 * Returns zero on success or %-ENOENT on failure.
285 int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
286 struct notifier_block *n)
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().
295 if (unlikely(system_state == SYSTEM_BOOTING))
296 return notifier_chain_unregister(&nh->head, n);
298 down_write(&nh->rwsem);
299 ret = notifier_chain_unregister(&nh->head, n);
300 up_write(&nh->rwsem);
304 EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
307 * blocking_notifier_call_chain - Call functions in a blocking notifier chain
308 * @nh: Pointer to head of the blocking notifier chain
309 * @val: Value passed unmodified to notifier function
310 * @v: Pointer passed unmodified to notifier function
312 * Calls each function in a notifier chain in turn. The functions
313 * run in a process context, so they are allowed to block.
315 * If the return value of the notifier can be and'ed
316 * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain
317 * will return immediately, with the return value of
318 * the notifier function which halted execution.
319 * Otherwise the return value is the return value
320 * of the last notifier function called.
323 int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
324 unsigned long val, void *v)
328 down_read(&nh->rwsem);
329 ret = notifier_call_chain(&nh->head, val, v);
334 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
337 * Raw notifier chain routines. There is no protection;
338 * the caller must provide it. Use at your own risk!
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
346 * Adds a notifier to a raw notifier chain.
347 * All locking must be provided by the caller.
349 * Currently always returns zero.
352 int raw_notifier_chain_register(struct raw_notifier_head *nh,
353 struct notifier_block *n)
355 return notifier_chain_register(&nh->head, n);
358 EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
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
365 * Removes a notifier from a raw notifier chain.
366 * All locking must be provided by the caller.
368 * Returns zero on success or %-ENOENT on failure.
370 int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
371 struct notifier_block *n)
373 return notifier_chain_unregister(&nh->head, n);
376 EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
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
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.
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.
396 int raw_notifier_call_chain(struct raw_notifier_head *nh,
397 unsigned long val, void *v)
399 return notifier_call_chain(&nh->head, val, v);
402 EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
405 * SRCU notifier chain routines. Registration and unregistration
406 * use a mutex, and call_chain is synchronized by SRCU (no locks).
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
414 * Adds a notifier to an SRCU notifier chain.
415 * Must be called in process context.
417 * Currently always returns zero.
420 int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
421 struct notifier_block *n)
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().
430 if (unlikely(system_state == SYSTEM_BOOTING))
431 return notifier_chain_register(&nh->head, n);
433 mutex_lock(&nh->mutex);
434 ret = notifier_chain_register(&nh->head, n);
435 mutex_unlock(&nh->mutex);
439 EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
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
446 * Removes a notifier from an SRCU notifier chain.
447 * Must be called from process context.
449 * Returns zero on success or %-ENOENT on failure.
451 int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
452 struct notifier_block *n)
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().
461 if (unlikely(system_state == SYSTEM_BOOTING))
462 return notifier_chain_unregister(&nh->head, n);
464 mutex_lock(&nh->mutex);
465 ret = notifier_chain_unregister(&nh->head, n);
466 mutex_unlock(&nh->mutex);
467 synchronize_srcu(&nh->srcu);
471 EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
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
479 * Calls each function in a notifier chain in turn. The functions
480 * run in a process context, so they are allowed to block.
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.
490 int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
491 unsigned long val, void *v)
496 idx = srcu_read_lock(&nh->srcu);
497 ret = notifier_call_chain(&nh->head, val, v);
498 srcu_read_unlock(&nh->srcu, idx);
502 EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
505 * srcu_init_notifier_head - Initialize an SRCU notifier head
506 * @nh: Pointer to head of the srcu notifier chain
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.
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.
517 void srcu_init_notifier_head(struct srcu_notifier_head *nh)
519 mutex_init(&nh->mutex);
520 if (init_srcu_struct(&nh->srcu) < 0)
525 EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
528 * register_reboot_notifier - Register function to be called at reboot time
529 * @nb: Info about notifier function to be called
531 * Registers a function with the list of functions
532 * to be called at reboot time.
534 * Currently always returns zero, as blocking_notifier_chain_register
535 * always returns zero.
538 int register_reboot_notifier(struct notifier_block * nb)
540 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
543 EXPORT_SYMBOL(register_reboot_notifier);
546 * unregister_reboot_notifier - Unregister previously registered reboot notifier
547 * @nb: Hook to be unregistered
549 * Unregisters a previously registered reboot
552 * Returns zero on success, or %-ENOENT on failure.
555 int unregister_reboot_notifier(struct notifier_block * nb)
557 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
560 EXPORT_SYMBOL(unregister_reboot_notifier);
562 static int set_one_prio(struct task_struct *p, int niceval, int error)
566 if (p->uid != current->euid &&
567 p->euid != current->euid && !capable(CAP_SYS_NICE)) {
571 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
575 no_nice = security_task_setnice(p, niceval);
582 set_user_nice(p, niceval);
587 asmlinkage long sys_setpriority(int which, int who, int niceval)
589 struct task_struct *g, *p;
590 struct user_struct *user;
593 if (which > 2 || which < 0)
596 /* normalize: avoid signed division (rounding problems) */
603 read_lock(&tasklist_lock);
608 p = find_task_by_pid(who);
610 error = set_one_prio(p, niceval, error);
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);
620 user = current->user;
624 if ((who != current->uid) && !(user = find_user(who)))
625 goto out_unlock; /* No processes for this user */
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() */
636 read_unlock(&tasklist_lock);
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.
647 asmlinkage long sys_getpriority(int which, int who)
649 struct task_struct *g, *p;
650 struct user_struct *user;
651 long niceval, retval = -ESRCH;
653 if (which > 2 || which < 0)
656 read_lock(&tasklist_lock);
661 p = find_task_by_pid(who);
663 niceval = 20 - task_nice(p);
664 if (niceval > retval)
670 who = process_group(current);
671 do_each_task_pid(who, PIDTYPE_PGID, p) {
672 niceval = 20 - task_nice(p);
673 if (niceval > retval)
675 } while_each_task_pid(who, PIDTYPE_PGID, p);
678 user = current->user;
682 if ((who != current->uid) && !(user = find_user(who)))
683 goto out_unlock; /* No processes for this user */
687 niceval = 20 - task_nice(p);
688 if (niceval > retval)
691 while_each_thread(g, p);
692 if (who != current->uid)
693 free_uid(user); /* for find_user() */
697 read_unlock(&tasklist_lock);
703 * emergency_restart - reboot the system
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.
710 void emergency_restart(void)
712 machine_emergency_restart();
714 EXPORT_SYMBOL_GPL(emergency_restart);
716 static void kernel_restart_prepare(char *cmd)
718 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
719 system_state = SYSTEM_RESTART;
724 * kernel_restart - reboot the system
725 * @cmd: pointer to buffer containing command to execute for restart
728 * Shutdown everything and perform a clean reboot.
729 * This is not safe to call in interrupt context.
731 void kernel_restart(char *cmd)
733 kernel_restart_prepare(cmd);
735 printk(KERN_EMERG "Restarting system.\n");
737 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
738 machine_restart(cmd);
740 EXPORT_SYMBOL_GPL(kernel_restart);
743 * kernel_kexec - reboot the system
745 * Move into place and start executing a preloaded standalone
746 * executable. If nothing was preloaded return an error.
748 static void kernel_kexec(void)
751 struct kimage *image;
752 image = xchg(&kexec_image, NULL);
755 kernel_restart_prepare(NULL);
756 printk(KERN_EMERG "Starting new kernel\n");
758 machine_kexec(image);
762 void kernel_shutdown_prepare(enum system_states state)
764 blocking_notifier_call_chain(&reboot_notifier_list,
765 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
766 system_state = state;
770 * kernel_halt - halt the system
772 * Shutdown everything and perform a clean system halt.
774 void kernel_halt(void)
776 kernel_shutdown_prepare(SYSTEM_HALT);
777 printk(KERN_EMERG "System halted.\n");
781 EXPORT_SYMBOL_GPL(kernel_halt);
784 * kernel_power_off - power_off the system
786 * Shutdown everything and perform a clean system power_off.
788 void kernel_power_off(void)
790 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
791 printk(KERN_EMERG "Power down.\n");
794 EXPORT_SYMBOL_GPL(kernel_power_off);
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.
801 * reboot doesn't sync: do that yourself before calling this.
803 asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
807 /* We only trust the superuser with rebooting the system. */
808 if (!capable(CAP_SYS_BOOT))
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))
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.
822 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
823 cmd = LINUX_REBOOT_CMD_HALT;
827 case LINUX_REBOOT_CMD_RESTART:
828 kernel_restart(NULL);
831 case LINUX_REBOOT_CMD_CAD_ON:
835 case LINUX_REBOOT_CMD_CAD_OFF:
839 case LINUX_REBOOT_CMD_HALT:
845 case LINUX_REBOOT_CMD_POWER_OFF:
851 case LINUX_REBOOT_CMD_RESTART2:
852 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
856 buffer[sizeof(buffer) - 1] = '\0';
858 kernel_restart(buffer);
861 case LINUX_REBOOT_CMD_KEXEC:
866 #ifdef CONFIG_SOFTWARE_SUSPEND
867 case LINUX_REBOOT_CMD_SW_SUSPEND:
869 int ret = software_suspend();
883 static void deferred_cad(struct work_struct *dummy)
885 kernel_restart(NULL);
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.
893 void ctrl_alt_del(void)
895 static DECLARE_WORK(cad_work, deferred_cad);
898 schedule_work(&cad_work);
900 kill_cad_pid(SIGINT, 1);
904 * Unprivileged users may change the real gid to the effective gid
905 * or vice versa. (BSD-style)
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.
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.
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.
918 * SMP: There are not races, the GIDs are checked only by filesystem
919 * operations (as far as semantic preservation is concerned).
921 asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
923 int old_rgid = current->gid;
924 int old_egid = current->egid;
925 int new_rgid = old_rgid;
926 int new_egid = old_egid;
929 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
933 if (rgid != (gid_t) -1) {
934 if ((old_rgid == rgid) ||
935 (current->egid==rgid) ||
941 if (egid != (gid_t) -1) {
942 if ((old_rgid == egid) ||
943 (current->egid == egid) ||
944 (current->sgid == egid) ||
950 if (new_egid != old_egid) {
951 current->mm->dumpable = suid_dumpable;
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);
961 proc_id_connector(current, PROC_EVENT_GID);
966 * setgid() is implemented like SysV w/ SAVED_IDS
968 * SMP: Same implicit races as above.
970 asmlinkage long sys_setgid(gid_t gid)
972 int old_egid = current->egid;
975 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
979 if (capable(CAP_SETGID)) {
980 if (old_egid != gid) {
981 current->mm->dumpable = suid_dumpable;
984 current->gid = current->egid = current->sgid = current->fsgid = gid;
985 } else if ((gid == current->gid) || (gid == current->sgid)) {
986 if (old_egid != gid) {
987 current->mm->dumpable = suid_dumpable;
990 current->egid = current->fsgid = gid;
995 key_fsgid_changed(current);
996 proc_id_connector(current, PROC_EVENT_GID);
1000 static int set_user(uid_t new_ruid, int dumpclear)
1002 struct user_struct *new_user;
1004 new_user = alloc_uid(new_ruid);
1008 if (atomic_read(&new_user->processes) >=
1009 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
1010 new_user != &root_user) {
1015 switch_uid(new_user);
1018 current->mm->dumpable = suid_dumpable;
1021 current->uid = new_ruid;
1026 * Unprivileged users may change the real uid to the effective uid
1027 * or vice versa. (BSD-style)
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.
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.
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.
1040 asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
1042 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
1045 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
1049 new_ruid = old_ruid = current->uid;
1050 new_euid = old_euid = current->euid;
1051 old_suid = current->suid;
1053 if (ruid != (uid_t) -1) {
1055 if ((old_ruid != ruid) &&
1056 (current->euid != ruid) &&
1057 !capable(CAP_SETUID))
1061 if (euid != (uid_t) -1) {
1063 if ((old_ruid != euid) &&
1064 (current->euid != euid) &&
1065 (current->suid != euid) &&
1066 !capable(CAP_SETUID))
1070 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
1073 if (new_euid != old_euid) {
1074 current->mm->dumpable = suid_dumpable;
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;
1083 key_fsuid_changed(current);
1084 proc_id_connector(current, PROC_EVENT_UID);
1086 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
1092 * setuid() is implemented like SysV with SAVED_IDS
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.
1102 asmlinkage long sys_setuid(uid_t uid)
1104 int old_euid = current->euid;
1105 int old_ruid, old_suid, new_ruid, new_suid;
1108 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
1112 old_ruid = new_ruid = current->uid;
1113 old_suid = current->suid;
1114 new_suid = old_suid;
1116 if (capable(CAP_SETUID)) {
1117 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
1120 } else if ((uid != current->uid) && (uid != new_suid))
1123 if (old_euid != uid) {
1124 current->mm->dumpable = suid_dumpable;
1127 current->fsuid = current->euid = uid;
1128 current->suid = new_suid;
1130 key_fsuid_changed(current);
1131 proc_id_connector(current, PROC_EVENT_UID);
1133 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
1138 * This function implements a generic ability to update ruid, euid,
1139 * and suid. This allows you to implement the 4.4 compatible seteuid().
1141 asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
1143 int old_ruid = current->uid;
1144 int old_euid = current->euid;
1145 int old_suid = current->suid;
1148 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
1152 if (!capable(CAP_SETUID)) {
1153 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
1154 (ruid != current->euid) && (ruid != current->suid))
1156 if ((euid != (uid_t) -1) && (euid != current->uid) &&
1157 (euid != current->euid) && (euid != current->suid))
1159 if ((suid != (uid_t) -1) && (suid != current->uid) &&
1160 (suid != current->euid) && (suid != current->suid))
1163 if (ruid != (uid_t) -1) {
1164 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
1167 if (euid != (uid_t) -1) {
1168 if (euid != current->euid) {
1169 current->mm->dumpable = suid_dumpable;
1172 current->euid = euid;
1174 current->fsuid = current->euid;
1175 if (suid != (uid_t) -1)
1176 current->suid = suid;
1178 key_fsuid_changed(current);
1179 proc_id_connector(current, PROC_EVENT_UID);
1181 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
1184 asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
1188 if (!(retval = put_user(current->uid, ruid)) &&
1189 !(retval = put_user(current->euid, euid)))
1190 retval = put_user(current->suid, suid);
1196 * Same as above, but for rgid, egid, sgid.
1198 asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1202 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
1206 if (!capable(CAP_SETGID)) {
1207 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
1208 (rgid != current->egid) && (rgid != current->sgid))
1210 if ((egid != (gid_t) -1) && (egid != current->gid) &&
1211 (egid != current->egid) && (egid != current->sgid))
1213 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
1214 (sgid != current->egid) && (sgid != current->sgid))
1217 if (egid != (gid_t) -1) {
1218 if (egid != current->egid) {
1219 current->mm->dumpable = suid_dumpable;
1222 current->egid = egid;
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;
1230 key_fsgid_changed(current);
1231 proc_id_connector(current, PROC_EVENT_GID);
1235 asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
1239 if (!(retval = put_user(current->gid, rgid)) &&
1240 !(retval = put_user(current->egid, egid)))
1241 retval = put_user(current->sgid, sgid);
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..
1253 asmlinkage long sys_setfsuid(uid_t uid)
1257 old_fsuid = current->fsuid;
1258 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
1261 if (uid == current->uid || uid == current->euid ||
1262 uid == current->suid || uid == current->fsuid ||
1263 capable(CAP_SETUID)) {
1264 if (uid != old_fsuid) {
1265 current->mm->dumpable = suid_dumpable;
1268 current->fsuid = uid;
1271 key_fsuid_changed(current);
1272 proc_id_connector(current, PROC_EVENT_UID);
1274 security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
1280 * Samma på svenska..
1282 asmlinkage long sys_setfsgid(gid_t gid)
1286 old_fsgid = current->fsgid;
1287 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
1290 if (gid == current->gid || gid == current->egid ||
1291 gid == current->sgid || gid == current->fsgid ||
1292 capable(CAP_SETGID)) {
1293 if (gid != old_fsgid) {
1294 current->mm->dumpable = suid_dumpable;
1297 current->fsgid = gid;
1298 key_fsgid_changed(current);
1299 proc_id_connector(current, PROC_EVENT_GID);
1304 asmlinkage long sys_times(struct tms __user * tbuf)
1307 * In the SMP world we might just be unlucky and have one of
1308 * the times increment as we use it. Since the value is an
1309 * atomically safe type this is just fine. Conceptually its
1310 * as if the syscall took an instant longer to occur.
1314 struct task_struct *tsk = current;
1315 struct task_struct *t;
1316 cputime_t utime, stime, cutime, cstime;
1318 spin_lock_irq(&tsk->sighand->siglock);
1319 utime = tsk->signal->utime;
1320 stime = tsk->signal->stime;
1323 utime = cputime_add(utime, t->utime);
1324 stime = cputime_add(stime, t->stime);
1328 cutime = tsk->signal->cutime;
1329 cstime = tsk->signal->cstime;
1330 spin_unlock_irq(&tsk->sighand->siglock);
1332 tmp.tms_utime = cputime_to_clock_t(utime);
1333 tmp.tms_stime = cputime_to_clock_t(stime);
1334 tmp.tms_cutime = cputime_to_clock_t(cutime);
1335 tmp.tms_cstime = cputime_to_clock_t(cstime);
1336 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1339 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1343 * This needs some heavy checking ...
1344 * I just haven't the stomach for it. I also don't fully
1345 * understand sessions/pgrp etc. Let somebody who does explain it.
1347 * OK, I think I have the protection semantics right.... this is really
1348 * only important on a multi-user system anyway, to make sure one user
1349 * can't send a signal to a process owned by another. -TYT, 12/12/91
1351 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1355 asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
1357 struct task_struct *p;
1358 struct task_struct *group_leader = current->group_leader;
1362 pid = group_leader->pid;
1368 /* From this point forward we keep holding onto the tasklist lock
1369 * so that our parent does not change from under us. -DaveM
1371 write_lock_irq(&tasklist_lock);
1374 p = find_task_by_pid(pid);
1379 if (!thread_group_leader(p))
1382 if (p->real_parent == group_leader) {
1384 if (p->signal->session != group_leader->signal->session)
1391 if (p != group_leader)
1396 if (p->signal->leader)
1400 struct task_struct *p;
1402 do_each_task_pid(pgid, PIDTYPE_PGID, p) {
1403 if (p->signal->session == group_leader->signal->session)
1405 } while_each_task_pid(pgid, PIDTYPE_PGID, p);
1410 err = security_task_setpgid(p, pgid);
1414 if (process_group(p) != pgid) {
1415 detach_pid(p, PIDTYPE_PGID);
1416 p->signal->pgrp = pgid;
1417 attach_pid(p, PIDTYPE_PGID, pgid);
1422 /* All paths lead to here, thus we are safe. -DaveM */
1423 write_unlock_irq(&tasklist_lock);
1427 asmlinkage long sys_getpgid(pid_t pid)
1430 return process_group(current);
1433 struct task_struct *p;
1435 read_lock(&tasklist_lock);
1436 p = find_task_by_pid(pid);
1440 retval = security_task_getpgid(p);
1442 retval = process_group(p);
1444 read_unlock(&tasklist_lock);
1449 #ifdef __ARCH_WANT_SYS_GETPGRP
1451 asmlinkage long sys_getpgrp(void)
1453 /* SMP - assuming writes are word atomic this is fine */
1454 return process_group(current);
1459 asmlinkage long sys_getsid(pid_t pid)
1462 return current->signal->session;
1465 struct task_struct *p;
1467 read_lock(&tasklist_lock);
1468 p = find_task_by_pid(pid);
1472 retval = security_task_getsid(p);
1474 retval = p->signal->session;
1476 read_unlock(&tasklist_lock);
1481 asmlinkage long sys_setsid(void)
1483 struct task_struct *group_leader = current->group_leader;
1487 mutex_lock(&tty_mutex);
1488 write_lock_irq(&tasklist_lock);
1490 /* Fail if I am already a session leader */
1491 if (group_leader->signal->leader)
1494 session = group_leader->pid;
1495 /* Fail if a process group id already exists that equals the
1496 * proposed session id.
1498 * Don't check if session id == 1 because kernel threads use this
1499 * session id and so the check will always fail and make it so
1500 * init cannot successfully call setsid.
1502 if (session > 1 && find_task_by_pid_type(PIDTYPE_PGID, session))
1505 group_leader->signal->leader = 1;
1506 __set_special_pids(session, session);
1507 group_leader->signal->tty = NULL;
1508 group_leader->signal->tty_old_pgrp = 0;
1509 err = process_group(group_leader);
1511 write_unlock_irq(&tasklist_lock);
1512 mutex_unlock(&tty_mutex);
1517 * Supplementary group IDs
1520 /* init to 2 - one for init_task, one to ensure it is never freed */
1521 struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1523 struct group_info *groups_alloc(int gidsetsize)
1525 struct group_info *group_info;
1529 nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1530 /* Make sure we always allocate at least one indirect block pointer */
1531 nblocks = nblocks ? : 1;
1532 group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1535 group_info->ngroups = gidsetsize;
1536 group_info->nblocks = nblocks;
1537 atomic_set(&group_info->usage, 1);
1539 if (gidsetsize <= NGROUPS_SMALL)
1540 group_info->blocks[0] = group_info->small_block;
1542 for (i = 0; i < nblocks; i++) {
1544 b = (void *)__get_free_page(GFP_USER);
1546 goto out_undo_partial_alloc;
1547 group_info->blocks[i] = b;
1552 out_undo_partial_alloc:
1554 free_page((unsigned long)group_info->blocks[i]);
1560 EXPORT_SYMBOL(groups_alloc);
1562 void groups_free(struct group_info *group_info)
1564 if (group_info->blocks[0] != group_info->small_block) {
1566 for (i = 0; i < group_info->nblocks; i++)
1567 free_page((unsigned long)group_info->blocks[i]);
1572 EXPORT_SYMBOL(groups_free);
1574 /* export the group_info to a user-space array */
1575 static int groups_to_user(gid_t __user *grouplist,
1576 struct group_info *group_info)
1579 int count = group_info->ngroups;
1581 for (i = 0; i < group_info->nblocks; i++) {
1582 int cp_count = min(NGROUPS_PER_BLOCK, count);
1583 int off = i * NGROUPS_PER_BLOCK;
1584 int len = cp_count * sizeof(*grouplist);
1586 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1594 /* fill a group_info from a user-space array - it must be allocated already */
1595 static int groups_from_user(struct group_info *group_info,
1596 gid_t __user *grouplist)
1599 int count = group_info->ngroups;
1601 for (i = 0; i < group_info->nblocks; i++) {
1602 int cp_count = min(NGROUPS_PER_BLOCK, count);
1603 int off = i * NGROUPS_PER_BLOCK;
1604 int len = cp_count * sizeof(*grouplist);
1606 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1614 /* a simple Shell sort */
1615 static void groups_sort(struct group_info *group_info)
1617 int base, max, stride;
1618 int gidsetsize = group_info->ngroups;
1620 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1625 max = gidsetsize - stride;
1626 for (base = 0; base < max; base++) {
1628 int right = left + stride;
1629 gid_t tmp = GROUP_AT(group_info, right);
1631 while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1632 GROUP_AT(group_info, right) =
1633 GROUP_AT(group_info, left);
1637 GROUP_AT(group_info, right) = tmp;
1643 /* a simple bsearch */
1644 int groups_search(struct group_info *group_info, gid_t grp)
1646 unsigned int left, right;
1652 right = group_info->ngroups;
1653 while (left < right) {
1654 unsigned int mid = (left+right)/2;
1655 int cmp = grp - GROUP_AT(group_info, mid);
1666 /* validate and set current->group_info */
1667 int set_current_groups(struct group_info *group_info)
1670 struct group_info *old_info;
1672 retval = security_task_setgroups(group_info);
1676 groups_sort(group_info);
1677 get_group_info(group_info);
1680 old_info = current->group_info;
1681 current->group_info = group_info;
1682 task_unlock(current);
1684 put_group_info(old_info);
1689 EXPORT_SYMBOL(set_current_groups);
1691 asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1696 * SMP: Nobody else can change our grouplist. Thus we are
1703 /* no need to grab task_lock here; it cannot change */
1704 i = current->group_info->ngroups;
1706 if (i > gidsetsize) {
1710 if (groups_to_user(grouplist, current->group_info)) {
1720 * SMP: Our groups are copy-on-write. We can set them safely
1721 * without another task interfering.
1724 asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1726 struct group_info *group_info;
1729 if (!capable(CAP_SETGID))
1731 if ((unsigned)gidsetsize > NGROUPS_MAX)
1734 group_info = groups_alloc(gidsetsize);
1737 retval = groups_from_user(group_info, grouplist);
1739 put_group_info(group_info);
1743 retval = set_current_groups(group_info);
1744 put_group_info(group_info);
1750 * Check whether we're fsgid/egid or in the supplemental group..
1752 int in_group_p(gid_t grp)
1755 if (grp != current->fsgid)
1756 retval = groups_search(current->group_info, grp);
1760 EXPORT_SYMBOL(in_group_p);
1762 int in_egroup_p(gid_t grp)
1765 if (grp != current->egid)
1766 retval = groups_search(current->group_info, grp);
1770 EXPORT_SYMBOL(in_egroup_p);
1772 DECLARE_RWSEM(uts_sem);
1774 EXPORT_SYMBOL(uts_sem);
1776 asmlinkage long sys_newuname(struct new_utsname __user * name)
1780 down_read(&uts_sem);
1781 if (copy_to_user(name, utsname(), sizeof *name))
1787 asmlinkage long sys_sethostname(char __user *name, int len)
1790 char tmp[__NEW_UTS_LEN];
1792 if (!capable(CAP_SYS_ADMIN))
1794 if (len < 0 || len > __NEW_UTS_LEN)
1796 down_write(&uts_sem);
1798 if (!copy_from_user(tmp, name, len)) {
1799 memcpy(utsname()->nodename, tmp, len);
1800 utsname()->nodename[len] = 0;
1807 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1809 asmlinkage long sys_gethostname(char __user *name, int len)
1815 down_read(&uts_sem);
1816 i = 1 + strlen(utsname()->nodename);
1820 if (copy_to_user(name, utsname()->nodename, i))
1829 * Only setdomainname; getdomainname can be implemented by calling
1832 asmlinkage long sys_setdomainname(char __user *name, int len)
1835 char tmp[__NEW_UTS_LEN];
1837 if (!capable(CAP_SYS_ADMIN))
1839 if (len < 0 || len > __NEW_UTS_LEN)
1842 down_write(&uts_sem);
1844 if (!copy_from_user(tmp, name, len)) {
1845 memcpy(utsname()->domainname, tmp, len);
1846 utsname()->domainname[len] = 0;
1853 asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1855 if (resource >= RLIM_NLIMITS)
1858 struct rlimit value;
1859 task_lock(current->group_leader);
1860 value = current->signal->rlim[resource];
1861 task_unlock(current->group_leader);
1862 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1866 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1869 * Back compatibility for getrlimit. Needed for some apps.
1872 asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1875 if (resource >= RLIM_NLIMITS)
1878 task_lock(current->group_leader);
1879 x = current->signal->rlim[resource];
1880 task_unlock(current->group_leader);
1881 if (x.rlim_cur > 0x7FFFFFFF)
1882 x.rlim_cur = 0x7FFFFFFF;
1883 if (x.rlim_max > 0x7FFFFFFF)
1884 x.rlim_max = 0x7FFFFFFF;
1885 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1890 asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1892 struct rlimit new_rlim, *old_rlim;
1893 unsigned long it_prof_secs;
1896 if (resource >= RLIM_NLIMITS)
1898 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1900 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1902 old_rlim = current->signal->rlim + resource;
1903 if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1904 !capable(CAP_SYS_RESOURCE))
1906 if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
1909 retval = security_task_setrlimit(resource, &new_rlim);
1913 task_lock(current->group_leader);
1914 *old_rlim = new_rlim;
1915 task_unlock(current->group_leader);
1917 if (resource != RLIMIT_CPU)
1921 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1922 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1923 * very long-standing error, and fixing it now risks breakage of
1924 * applications, so we live with it
1926 if (new_rlim.rlim_cur == RLIM_INFINITY)
1929 it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
1930 if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
1931 unsigned long rlim_cur = new_rlim.rlim_cur;
1934 if (rlim_cur == 0) {
1936 * The caller is asking for an immediate RLIMIT_CPU
1937 * expiry. But we use the zero value to mean "it was
1938 * never set". So let's cheat and make it one second
1943 cputime = secs_to_cputime(rlim_cur);
1944 read_lock(&tasklist_lock);
1945 spin_lock_irq(¤t->sighand->siglock);
1946 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
1947 spin_unlock_irq(¤t->sighand->siglock);
1948 read_unlock(&tasklist_lock);
1955 * It would make sense to put struct rusage in the task_struct,
1956 * except that would make the task_struct be *really big*. After
1957 * task_struct gets moved into malloc'ed memory, it would
1958 * make sense to do this. It will make moving the rest of the information
1959 * a lot simpler! (Which we're not doing right now because we're not
1960 * measuring them yet).
1962 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1963 * races with threads incrementing their own counters. But since word
1964 * reads are atomic, we either get new values or old values and we don't
1965 * care which for the sums. We always take the siglock to protect reading
1966 * the c* fields from p->signal from races with exit.c updating those
1967 * fields when reaping, so a sample either gets all the additions of a
1968 * given child after it's reaped, or none so this sample is before reaping.
1971 * We need to take the siglock for CHILDEREN, SELF and BOTH
1972 * for the cases current multithreaded, non-current single threaded
1973 * non-current multithreaded. Thread traversal is now safe with
1975 * Strictly speaking, we donot need to take the siglock if we are current and
1976 * single threaded, as no one else can take our signal_struct away, no one
1977 * else can reap the children to update signal->c* counters, and no one else
1978 * can race with the signal-> fields. If we do not take any lock, the
1979 * signal-> fields could be read out of order while another thread was just
1980 * exiting. So we should place a read memory barrier when we avoid the lock.
1981 * On the writer side, write memory barrier is implied in __exit_signal
1982 * as __exit_signal releases the siglock spinlock after updating the signal->
1983 * fields. But we don't do this yet to keep things simple.
1987 static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1989 struct task_struct *t;
1990 unsigned long flags;
1991 cputime_t utime, stime;
1993 memset((char *) r, 0, sizeof *r);
1994 utime = stime = cputime_zero;
1997 if (!lock_task_sighand(p, &flags)) {
2004 case RUSAGE_CHILDREN:
2005 utime = p->signal->cutime;
2006 stime = p->signal->cstime;
2007 r->ru_nvcsw = p->signal->cnvcsw;
2008 r->ru_nivcsw = p->signal->cnivcsw;
2009 r->ru_minflt = p->signal->cmin_flt;
2010 r->ru_majflt = p->signal->cmaj_flt;
2012 if (who == RUSAGE_CHILDREN)
2016 utime = cputime_add(utime, p->signal->utime);
2017 stime = cputime_add(stime, p->signal->stime);
2018 r->ru_nvcsw += p->signal->nvcsw;
2019 r->ru_nivcsw += p->signal->nivcsw;
2020 r->ru_minflt += p->signal->min_flt;
2021 r->ru_majflt += p->signal->maj_flt;
2024 utime = cputime_add(utime, t->utime);
2025 stime = cputime_add(stime, t->stime);
2026 r->ru_nvcsw += t->nvcsw;
2027 r->ru_nivcsw += t->nivcsw;
2028 r->ru_minflt += t->min_flt;
2029 r->ru_majflt += t->maj_flt;
2038 unlock_task_sighand(p, &flags);
2041 cputime_to_timeval(utime, &r->ru_utime);
2042 cputime_to_timeval(stime, &r->ru_stime);
2045 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
2048 k_getrusage(p, who, &r);
2049 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
2052 asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
2054 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
2056 return getrusage(current, who, ru);
2059 asmlinkage long sys_umask(int mask)
2061 mask = xchg(¤t->fs->umask, mask & S_IRWXUGO);
2065 asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
2066 unsigned long arg4, unsigned long arg5)
2070 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2075 case PR_SET_PDEATHSIG:
2076 if (!valid_signal(arg2)) {
2080 current->pdeath_signal = arg2;
2082 case PR_GET_PDEATHSIG:
2083 error = put_user(current->pdeath_signal, (int __user *)arg2);
2085 case PR_GET_DUMPABLE:
2086 error = current->mm->dumpable;
2088 case PR_SET_DUMPABLE:
2089 if (arg2 < 0 || arg2 > 1) {
2093 current->mm->dumpable = arg2;
2096 case PR_SET_UNALIGN:
2097 error = SET_UNALIGN_CTL(current, arg2);
2099 case PR_GET_UNALIGN:
2100 error = GET_UNALIGN_CTL(current, arg2);
2103 error = SET_FPEMU_CTL(current, arg2);
2106 error = GET_FPEMU_CTL(current, arg2);
2109 error = SET_FPEXC_CTL(current, arg2);
2112 error = GET_FPEXC_CTL(current, arg2);
2115 error = PR_TIMING_STATISTICAL;
2118 if (arg2 == PR_TIMING_STATISTICAL)
2124 case PR_GET_KEEPCAPS:
2125 if (current->keep_capabilities)
2128 case PR_SET_KEEPCAPS:
2129 if (arg2 != 0 && arg2 != 1) {
2133 current->keep_capabilities = arg2;
2136 struct task_struct *me = current;
2137 unsigned char ncomm[sizeof(me->comm)];
2139 ncomm[sizeof(me->comm)-1] = 0;
2140 if (strncpy_from_user(ncomm, (char __user *)arg2,
2141 sizeof(me->comm)-1) < 0)
2143 set_task_comm(me, ncomm);
2147 struct task_struct *me = current;
2148 unsigned char tcomm[sizeof(me->comm)];
2150 get_task_comm(tcomm, me);
2151 if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
2156 error = GET_ENDIAN(current, arg2);
2159 error = SET_ENDIAN(current, arg2);
2169 asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
2170 struct getcpu_cache __user *cache)
2173 int cpu = raw_smp_processor_id();
2175 err |= put_user(cpu, cpup);
2177 err |= put_user(cpu_to_node(cpu), nodep);
2180 * The cache is not needed for this implementation,
2181 * but make sure user programs pass something
2182 * valid. vsyscall implementations can instead make
2183 * good use of the cache. Only use t0 and t1 because
2184 * these are available in both 32bit and 64bit ABI (no
2185 * need for a compat_getcpu). 32bit has enough
2188 unsigned long t0, t1;
2189 get_user(t0, &cache->blob[0]);
2190 get_user(t1, &cache->blob[1]);
2193 put_user(t0, &cache->blob[0]);
2194 put_user(t1, &cache->blob[1]);
2196 return err ? -EFAULT : 0;