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>
32 #include <linux/compat.h>
33 #include <linux/syscalls.h>
34 #include <linux/kprobes.h>
36 #include <asm/uaccess.h>
38 #include <asm/unistd.h>
40 #ifndef SET_UNALIGN_CTL
41 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
43 #ifndef GET_UNALIGN_CTL
44 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
47 # define SET_FPEMU_CTL(a,b) (-EINVAL)
50 # define GET_FPEMU_CTL(a,b) (-EINVAL)
53 # define SET_FPEXC_CTL(a,b) (-EINVAL)
56 # define GET_FPEXC_CTL(a,b) (-EINVAL)
59 # define GET_ENDIAN(a,b) (-EINVAL)
62 # define SET_ENDIAN(a,b) (-EINVAL)
66 * this is where the system-wide overflow UID and GID are defined, for
67 * architectures that now have 32-bit UID/GID but didn't in the past
70 int overflowuid
= DEFAULT_OVERFLOWUID
;
71 int overflowgid
= DEFAULT_OVERFLOWGID
;
74 EXPORT_SYMBOL(overflowuid
);
75 EXPORT_SYMBOL(overflowgid
);
79 * the same as above, but for filesystems which can only store a 16-bit
80 * UID and GID. as such, this is needed on all architectures
83 int fs_overflowuid
= DEFAULT_FS_OVERFLOWUID
;
84 int fs_overflowgid
= DEFAULT_FS_OVERFLOWUID
;
86 EXPORT_SYMBOL(fs_overflowuid
);
87 EXPORT_SYMBOL(fs_overflowgid
);
90 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
97 * Notifier list for kernel code which wants to be called
98 * at shutdown. This is used to stop any idling DMA operations
102 static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list
);
105 * Notifier chain core routines. The exported routines below
106 * are layered on top of these, with appropriate locking added.
109 static int notifier_chain_register(struct notifier_block
**nl
,
110 struct notifier_block
*n
)
112 while ((*nl
) != NULL
) {
113 if (n
->priority
> (*nl
)->priority
)
118 rcu_assign_pointer(*nl
, n
);
122 static int notifier_chain_unregister(struct notifier_block
**nl
,
123 struct notifier_block
*n
)
125 while ((*nl
) != NULL
) {
127 rcu_assign_pointer(*nl
, n
->next
);
135 static int __kprobes
notifier_call_chain(struct notifier_block
**nl
,
136 unsigned long val
, void *v
)
138 int ret
= NOTIFY_DONE
;
139 struct notifier_block
*nb
, *next_nb
;
141 nb
= rcu_dereference(*nl
);
143 next_nb
= rcu_dereference(nb
->next
);
144 ret
= nb
->notifier_call(nb
, val
, v
);
145 if ((ret
& NOTIFY_STOP_MASK
) == NOTIFY_STOP_MASK
)
153 * Atomic notifier chain routines. Registration and unregistration
154 * use a mutex, and call_chain is synchronized by RCU (no locks).
158 * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
159 * @nh: Pointer to head of the atomic notifier chain
160 * @n: New entry in notifier chain
162 * Adds a notifier to an atomic notifier chain.
164 * Currently always returns zero.
167 int atomic_notifier_chain_register(struct atomic_notifier_head
*nh
,
168 struct notifier_block
*n
)
173 spin_lock_irqsave(&nh
->lock
, flags
);
174 ret
= notifier_chain_register(&nh
->head
, n
);
175 spin_unlock_irqrestore(&nh
->lock
, flags
);
179 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register
);
182 * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
183 * @nh: Pointer to head of the atomic notifier chain
184 * @n: Entry to remove from notifier chain
186 * Removes a notifier from an atomic notifier chain.
188 * Returns zero on success or %-ENOENT on failure.
190 int atomic_notifier_chain_unregister(struct atomic_notifier_head
*nh
,
191 struct notifier_block
*n
)
196 spin_lock_irqsave(&nh
->lock
, flags
);
197 ret
= notifier_chain_unregister(&nh
->head
, n
);
198 spin_unlock_irqrestore(&nh
->lock
, flags
);
203 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister
);
206 * atomic_notifier_call_chain - Call functions in an atomic notifier chain
207 * @nh: Pointer to head of the atomic notifier chain
208 * @val: Value passed unmodified to notifier function
209 * @v: Pointer passed unmodified to notifier function
211 * Calls each function in a notifier chain in turn. The functions
212 * run in an atomic context, so they must not block.
213 * This routine uses RCU to synchronize with changes to the chain.
215 * If the return value of the notifier can be and'ed
216 * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain
217 * will return immediately, with the return value of
218 * the notifier function which halted execution.
219 * Otherwise the return value is the return value
220 * of the last notifier function called.
223 int atomic_notifier_call_chain(struct atomic_notifier_head
*nh
,
224 unsigned long val
, void *v
)
229 ret
= notifier_call_chain(&nh
->head
, val
, v
);
234 EXPORT_SYMBOL_GPL(atomic_notifier_call_chain
);
237 * Blocking notifier chain routines. All access to the chain is
238 * synchronized by an rwsem.
242 * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
243 * @nh: Pointer to head of the blocking notifier chain
244 * @n: New entry in notifier chain
246 * Adds a notifier to a blocking notifier chain.
247 * Must be called in process context.
249 * Currently always returns zero.
252 int blocking_notifier_chain_register(struct blocking_notifier_head
*nh
,
253 struct notifier_block
*n
)
258 * This code gets used during boot-up, when task switching is
259 * not yet working and interrupts must remain disabled. At
260 * such times we must not call down_write().
262 if (unlikely(system_state
== SYSTEM_BOOTING
))
263 return notifier_chain_register(&nh
->head
, n
);
265 down_write(&nh
->rwsem
);
266 ret
= notifier_chain_register(&nh
->head
, n
);
267 up_write(&nh
->rwsem
);
271 EXPORT_SYMBOL_GPL(blocking_notifier_chain_register
);
274 * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
275 * @nh: Pointer to head of the blocking notifier chain
276 * @n: Entry to remove from notifier chain
278 * Removes a notifier from a blocking notifier chain.
279 * Must be called from process context.
281 * Returns zero on success or %-ENOENT on failure.
283 int blocking_notifier_chain_unregister(struct blocking_notifier_head
*nh
,
284 struct notifier_block
*n
)
289 * This code gets used during boot-up, when task switching is
290 * not yet working and interrupts must remain disabled. At
291 * such times we must not call down_write().
293 if (unlikely(system_state
== SYSTEM_BOOTING
))
294 return notifier_chain_unregister(&nh
->head
, n
);
296 down_write(&nh
->rwsem
);
297 ret
= notifier_chain_unregister(&nh
->head
, n
);
298 up_write(&nh
->rwsem
);
302 EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister
);
305 * blocking_notifier_call_chain - Call functions in a blocking notifier chain
306 * @nh: Pointer to head of the blocking notifier chain
307 * @val: Value passed unmodified to notifier function
308 * @v: Pointer passed unmodified to notifier function
310 * Calls each function in a notifier chain in turn. The functions
311 * run in a process context, so they are allowed to block.
313 * If the return value of the notifier can be and'ed
314 * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain
315 * will return immediately, with the return value of
316 * the notifier function which halted execution.
317 * Otherwise the return value is the return value
318 * of the last notifier function called.
321 int blocking_notifier_call_chain(struct blocking_notifier_head
*nh
,
322 unsigned long val
, void *v
)
326 down_read(&nh
->rwsem
);
327 ret
= notifier_call_chain(&nh
->head
, val
, v
);
332 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain
);
335 * Raw notifier chain routines. There is no protection;
336 * the caller must provide it. Use at your own risk!
340 * raw_notifier_chain_register - Add notifier to a raw notifier chain
341 * @nh: Pointer to head of the raw notifier chain
342 * @n: New entry in notifier chain
344 * Adds a notifier to a raw notifier chain.
345 * All locking must be provided by the caller.
347 * Currently always returns zero.
350 int raw_notifier_chain_register(struct raw_notifier_head
*nh
,
351 struct notifier_block
*n
)
353 return notifier_chain_register(&nh
->head
, n
);
356 EXPORT_SYMBOL_GPL(raw_notifier_chain_register
);
359 * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
360 * @nh: Pointer to head of the raw notifier chain
361 * @n: Entry to remove from notifier chain
363 * Removes a notifier from a raw notifier chain.
364 * All locking must be provided by the caller.
366 * Returns zero on success or %-ENOENT on failure.
368 int raw_notifier_chain_unregister(struct raw_notifier_head
*nh
,
369 struct notifier_block
*n
)
371 return notifier_chain_unregister(&nh
->head
, n
);
374 EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister
);
377 * raw_notifier_call_chain - Call functions in a raw notifier chain
378 * @nh: Pointer to head of the raw notifier chain
379 * @val: Value passed unmodified to notifier function
380 * @v: Pointer passed unmodified to notifier function
382 * Calls each function in a notifier chain in turn. The functions
383 * run in an undefined context.
384 * All locking must be provided by the caller.
386 * If the return value of the notifier can be and'ed
387 * with %NOTIFY_STOP_MASK then raw_notifier_call_chain
388 * will return immediately, with the return value of
389 * the notifier function which halted execution.
390 * Otherwise the return value is the return value
391 * of the last notifier function called.
394 int raw_notifier_call_chain(struct raw_notifier_head
*nh
,
395 unsigned long val
, void *v
)
397 return notifier_call_chain(&nh
->head
, val
, v
);
400 EXPORT_SYMBOL_GPL(raw_notifier_call_chain
);
403 * register_reboot_notifier - Register function to be called at reboot time
404 * @nb: Info about notifier function to be called
406 * Registers a function with the list of functions
407 * to be called at reboot time.
409 * Currently always returns zero, as blocking_notifier_chain_register
410 * always returns zero.
413 int register_reboot_notifier(struct notifier_block
* nb
)
415 return blocking_notifier_chain_register(&reboot_notifier_list
, nb
);
418 EXPORT_SYMBOL(register_reboot_notifier
);
421 * unregister_reboot_notifier - Unregister previously registered reboot notifier
422 * @nb: Hook to be unregistered
424 * Unregisters a previously registered reboot
427 * Returns zero on success, or %-ENOENT on failure.
430 int unregister_reboot_notifier(struct notifier_block
* nb
)
432 return blocking_notifier_chain_unregister(&reboot_notifier_list
, nb
);
435 EXPORT_SYMBOL(unregister_reboot_notifier
);
437 static int set_one_prio(struct task_struct
*p
, int niceval
, int error
)
441 if (p
->uid
!= current
->euid
&&
442 p
->euid
!= current
->euid
&& !capable(CAP_SYS_NICE
)) {
446 if (niceval
< task_nice(p
) && !can_nice(p
, niceval
)) {
450 no_nice
= security_task_setnice(p
, niceval
);
457 set_user_nice(p
, niceval
);
462 asmlinkage
long sys_setpriority(int which
, int who
, int niceval
)
464 struct task_struct
*g
, *p
;
465 struct user_struct
*user
;
468 if (which
> 2 || which
< 0)
471 /* normalize: avoid signed division (rounding problems) */
478 read_lock(&tasklist_lock
);
483 p
= find_task_by_pid(who
);
485 error
= set_one_prio(p
, niceval
, error
);
489 who
= process_group(current
);
490 do_each_task_pid(who
, PIDTYPE_PGID
, p
) {
491 error
= set_one_prio(p
, niceval
, error
);
492 } while_each_task_pid(who
, PIDTYPE_PGID
, p
);
495 user
= current
->user
;
499 if ((who
!= current
->uid
) && !(user
= find_user(who
)))
500 goto out_unlock
; /* No processes for this user */
504 error
= set_one_prio(p
, niceval
, error
);
505 while_each_thread(g
, p
);
506 if (who
!= current
->uid
)
507 free_uid(user
); /* For find_user() */
511 read_unlock(&tasklist_lock
);
517 * Ugh. To avoid negative return values, "getpriority()" will
518 * not return the normal nice-value, but a negated value that
519 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
520 * to stay compatible.
522 asmlinkage
long sys_getpriority(int which
, int who
)
524 struct task_struct
*g
, *p
;
525 struct user_struct
*user
;
526 long niceval
, retval
= -ESRCH
;
528 if (which
> 2 || which
< 0)
531 read_lock(&tasklist_lock
);
536 p
= find_task_by_pid(who
);
538 niceval
= 20 - task_nice(p
);
539 if (niceval
> retval
)
545 who
= process_group(current
);
546 do_each_task_pid(who
, PIDTYPE_PGID
, p
) {
547 niceval
= 20 - task_nice(p
);
548 if (niceval
> retval
)
550 } while_each_task_pid(who
, PIDTYPE_PGID
, p
);
553 user
= current
->user
;
557 if ((who
!= current
->uid
) && !(user
= find_user(who
)))
558 goto out_unlock
; /* No processes for this user */
562 niceval
= 20 - task_nice(p
);
563 if (niceval
> retval
)
566 while_each_thread(g
, p
);
567 if (who
!= current
->uid
)
568 free_uid(user
); /* for find_user() */
572 read_unlock(&tasklist_lock
);
578 * emergency_restart - reboot the system
580 * Without shutting down any hardware or taking any locks
581 * reboot the system. This is called when we know we are in
582 * trouble so this is our best effort to reboot. This is
583 * safe to call in interrupt context.
585 void emergency_restart(void)
587 machine_emergency_restart();
589 EXPORT_SYMBOL_GPL(emergency_restart
);
591 static void kernel_restart_prepare(char *cmd
)
593 blocking_notifier_call_chain(&reboot_notifier_list
, SYS_RESTART
, cmd
);
594 system_state
= SYSTEM_RESTART
;
599 * kernel_restart - reboot the system
600 * @cmd: pointer to buffer containing command to execute for restart
603 * Shutdown everything and perform a clean reboot.
604 * This is not safe to call in interrupt context.
606 void kernel_restart(char *cmd
)
608 kernel_restart_prepare(cmd
);
610 printk(KERN_EMERG
"Restarting system.\n");
612 printk(KERN_EMERG
"Restarting system with command '%s'.\n", cmd
);
615 machine_restart(cmd
);
617 EXPORT_SYMBOL_GPL(kernel_restart
);
620 * kernel_kexec - reboot the system
622 * Move into place and start executing a preloaded standalone
623 * executable. If nothing was preloaded return an error.
625 static void kernel_kexec(void)
628 struct kimage
*image
;
629 image
= xchg(&kexec_image
, NULL
);
633 kernel_restart_prepare(NULL
);
634 printk(KERN_EMERG
"Starting new kernel\n");
636 machine_kexec(image
);
640 void kernel_shutdown_prepare(enum system_states state
)
642 blocking_notifier_call_chain(&reboot_notifier_list
,
643 (state
== SYSTEM_HALT
)?SYS_HALT
:SYS_POWER_OFF
, NULL
);
644 system_state
= state
;
648 * kernel_halt - halt the system
650 * Shutdown everything and perform a clean system halt.
652 void kernel_halt(void)
654 kernel_shutdown_prepare(SYSTEM_HALT
);
655 printk(KERN_EMERG
"System halted.\n");
659 EXPORT_SYMBOL_GPL(kernel_halt
);
662 * kernel_power_off - power_off the system
664 * Shutdown everything and perform a clean system power_off.
666 void kernel_power_off(void)
668 kernel_shutdown_prepare(SYSTEM_POWER_OFF
);
669 printk(KERN_EMERG
"Power down.\n");
672 EXPORT_SYMBOL_GPL(kernel_power_off
);
674 * Reboot system call: for obvious reasons only root may call it,
675 * and even root needs to set up some magic numbers in the registers
676 * so that some mistake won't make this reboot the whole machine.
677 * You can also set the meaning of the ctrl-alt-del-key here.
679 * reboot doesn't sync: do that yourself before calling this.
681 asmlinkage
long sys_reboot(int magic1
, int magic2
, unsigned int cmd
, void __user
* arg
)
685 /* We only trust the superuser with rebooting the system. */
686 if (!capable(CAP_SYS_BOOT
))
689 /* For safety, we require "magic" arguments. */
690 if (magic1
!= LINUX_REBOOT_MAGIC1
||
691 (magic2
!= LINUX_REBOOT_MAGIC2
&&
692 magic2
!= LINUX_REBOOT_MAGIC2A
&&
693 magic2
!= LINUX_REBOOT_MAGIC2B
&&
694 magic2
!= LINUX_REBOOT_MAGIC2C
))
697 /* Instead of trying to make the power_off code look like
698 * halt when pm_power_off is not set do it the easy way.
700 if ((cmd
== LINUX_REBOOT_CMD_POWER_OFF
) && !pm_power_off
)
701 cmd
= LINUX_REBOOT_CMD_HALT
;
705 case LINUX_REBOOT_CMD_RESTART
:
706 kernel_restart(NULL
);
709 case LINUX_REBOOT_CMD_CAD_ON
:
713 case LINUX_REBOOT_CMD_CAD_OFF
:
717 case LINUX_REBOOT_CMD_HALT
:
723 case LINUX_REBOOT_CMD_POWER_OFF
:
729 case LINUX_REBOOT_CMD_RESTART2
:
730 if (strncpy_from_user(&buffer
[0], arg
, sizeof(buffer
) - 1) < 0) {
734 buffer
[sizeof(buffer
) - 1] = '\0';
736 kernel_restart(buffer
);
739 case LINUX_REBOOT_CMD_KEXEC
:
744 #ifdef CONFIG_SOFTWARE_SUSPEND
745 case LINUX_REBOOT_CMD_SW_SUSPEND
:
747 int ret
= software_suspend();
761 static void deferred_cad(void *dummy
)
763 kernel_restart(NULL
);
767 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
768 * As it's called within an interrupt, it may NOT sync: the only choice
769 * is whether to reboot at once, or just ignore the ctrl-alt-del.
771 void ctrl_alt_del(void)
773 static DECLARE_WORK(cad_work
, deferred_cad
, NULL
);
776 schedule_work(&cad_work
);
778 kill_proc(cad_pid
, SIGINT
, 1);
783 * Unprivileged users may change the real gid to the effective gid
784 * or vice versa. (BSD-style)
786 * If you set the real gid at all, or set the effective gid to a value not
787 * equal to the real gid, then the saved gid is set to the new effective gid.
789 * This makes it possible for a setgid program to completely drop its
790 * privileges, which is often a useful assertion to make when you are doing
791 * a security audit over a program.
793 * The general idea is that a program which uses just setregid() will be
794 * 100% compatible with BSD. A program which uses just setgid() will be
795 * 100% compatible with POSIX with saved IDs.
797 * SMP: There are not races, the GIDs are checked only by filesystem
798 * operations (as far as semantic preservation is concerned).
800 asmlinkage
long sys_setregid(gid_t rgid
, gid_t egid
)
802 int old_rgid
= current
->gid
;
803 int old_egid
= current
->egid
;
804 int new_rgid
= old_rgid
;
805 int new_egid
= old_egid
;
808 retval
= security_task_setgid(rgid
, egid
, (gid_t
)-1, LSM_SETID_RE
);
812 if (rgid
!= (gid_t
) -1) {
813 if ((old_rgid
== rgid
) ||
814 (current
->egid
==rgid
) ||
820 if (egid
!= (gid_t
) -1) {
821 if ((old_rgid
== egid
) ||
822 (current
->egid
== egid
) ||
823 (current
->sgid
== egid
) ||
830 if (new_egid
!= old_egid
)
832 current
->mm
->dumpable
= suid_dumpable
;
835 if (rgid
!= (gid_t
) -1 ||
836 (egid
!= (gid_t
) -1 && egid
!= old_rgid
))
837 current
->sgid
= new_egid
;
838 current
->fsgid
= new_egid
;
839 current
->egid
= new_egid
;
840 current
->gid
= new_rgid
;
841 key_fsgid_changed(current
);
842 proc_id_connector(current
, PROC_EVENT_GID
);
847 * setgid() is implemented like SysV w/ SAVED_IDS
849 * SMP: Same implicit races as above.
851 asmlinkage
long sys_setgid(gid_t gid
)
853 int old_egid
= current
->egid
;
856 retval
= security_task_setgid(gid
, (gid_t
)-1, (gid_t
)-1, LSM_SETID_ID
);
860 if (capable(CAP_SETGID
))
864 current
->mm
->dumpable
= suid_dumpable
;
867 current
->gid
= current
->egid
= current
->sgid
= current
->fsgid
= gid
;
869 else if ((gid
== current
->gid
) || (gid
== current
->sgid
))
873 current
->mm
->dumpable
= suid_dumpable
;
876 current
->egid
= current
->fsgid
= gid
;
881 key_fsgid_changed(current
);
882 proc_id_connector(current
, PROC_EVENT_GID
);
886 static int set_user(uid_t new_ruid
, int dumpclear
)
888 struct user_struct
*new_user
;
890 new_user
= alloc_uid(new_ruid
);
894 if (atomic_read(&new_user
->processes
) >=
895 current
->signal
->rlim
[RLIMIT_NPROC
].rlim_cur
&&
896 new_user
!= &root_user
) {
901 switch_uid(new_user
);
905 current
->mm
->dumpable
= suid_dumpable
;
908 current
->uid
= new_ruid
;
913 * Unprivileged users may change the real uid to the effective uid
914 * or vice versa. (BSD-style)
916 * If you set the real uid at all, or set the effective uid to a value not
917 * equal to the real uid, then the saved uid is set to the new effective uid.
919 * This makes it possible for a setuid program to completely drop its
920 * privileges, which is often a useful assertion to make when you are doing
921 * a security audit over a program.
923 * The general idea is that a program which uses just setreuid() will be
924 * 100% compatible with BSD. A program which uses just setuid() will be
925 * 100% compatible with POSIX with saved IDs.
927 asmlinkage
long sys_setreuid(uid_t ruid
, uid_t euid
)
929 int old_ruid
, old_euid
, old_suid
, new_ruid
, new_euid
;
932 retval
= security_task_setuid(ruid
, euid
, (uid_t
)-1, LSM_SETID_RE
);
936 new_ruid
= old_ruid
= current
->uid
;
937 new_euid
= old_euid
= current
->euid
;
938 old_suid
= current
->suid
;
940 if (ruid
!= (uid_t
) -1) {
942 if ((old_ruid
!= ruid
) &&
943 (current
->euid
!= ruid
) &&
944 !capable(CAP_SETUID
))
948 if (euid
!= (uid_t
) -1) {
950 if ((old_ruid
!= euid
) &&
951 (current
->euid
!= euid
) &&
952 (current
->suid
!= euid
) &&
953 !capable(CAP_SETUID
))
957 if (new_ruid
!= old_ruid
&& set_user(new_ruid
, new_euid
!= old_euid
) < 0)
960 if (new_euid
!= old_euid
)
962 current
->mm
->dumpable
= suid_dumpable
;
965 current
->fsuid
= current
->euid
= new_euid
;
966 if (ruid
!= (uid_t
) -1 ||
967 (euid
!= (uid_t
) -1 && euid
!= old_ruid
))
968 current
->suid
= current
->euid
;
969 current
->fsuid
= current
->euid
;
971 key_fsuid_changed(current
);
972 proc_id_connector(current
, PROC_EVENT_UID
);
974 return security_task_post_setuid(old_ruid
, old_euid
, old_suid
, LSM_SETID_RE
);
980 * setuid() is implemented like SysV with SAVED_IDS
982 * Note that SAVED_ID's is deficient in that a setuid root program
983 * like sendmail, for example, cannot set its uid to be a normal
984 * user and then switch back, because if you're root, setuid() sets
985 * the saved uid too. If you don't like this, blame the bright people
986 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
987 * will allow a root program to temporarily drop privileges and be able to
988 * regain them by swapping the real and effective uid.
990 asmlinkage
long sys_setuid(uid_t uid
)
992 int old_euid
= current
->euid
;
993 int old_ruid
, old_suid
, new_ruid
, new_suid
;
996 retval
= security_task_setuid(uid
, (uid_t
)-1, (uid_t
)-1, LSM_SETID_ID
);
1000 old_ruid
= new_ruid
= current
->uid
;
1001 old_suid
= current
->suid
;
1002 new_suid
= old_suid
;
1004 if (capable(CAP_SETUID
)) {
1005 if (uid
!= old_ruid
&& set_user(uid
, old_euid
!= uid
) < 0)
1008 } else if ((uid
!= current
->uid
) && (uid
!= new_suid
))
1011 if (old_euid
!= uid
)
1013 current
->mm
->dumpable
= suid_dumpable
;
1016 current
->fsuid
= current
->euid
= uid
;
1017 current
->suid
= new_suid
;
1019 key_fsuid_changed(current
);
1020 proc_id_connector(current
, PROC_EVENT_UID
);
1022 return security_task_post_setuid(old_ruid
, old_euid
, old_suid
, LSM_SETID_ID
);
1027 * This function implements a generic ability to update ruid, euid,
1028 * and suid. This allows you to implement the 4.4 compatible seteuid().
1030 asmlinkage
long sys_setresuid(uid_t ruid
, uid_t euid
, uid_t suid
)
1032 int old_ruid
= current
->uid
;
1033 int old_euid
= current
->euid
;
1034 int old_suid
= current
->suid
;
1037 retval
= security_task_setuid(ruid
, euid
, suid
, LSM_SETID_RES
);
1041 if (!capable(CAP_SETUID
)) {
1042 if ((ruid
!= (uid_t
) -1) && (ruid
!= current
->uid
) &&
1043 (ruid
!= current
->euid
) && (ruid
!= current
->suid
))
1045 if ((euid
!= (uid_t
) -1) && (euid
!= current
->uid
) &&
1046 (euid
!= current
->euid
) && (euid
!= current
->suid
))
1048 if ((suid
!= (uid_t
) -1) && (suid
!= current
->uid
) &&
1049 (suid
!= current
->euid
) && (suid
!= current
->suid
))
1052 if (ruid
!= (uid_t
) -1) {
1053 if (ruid
!= current
->uid
&& set_user(ruid
, euid
!= current
->euid
) < 0)
1056 if (euid
!= (uid_t
) -1) {
1057 if (euid
!= current
->euid
)
1059 current
->mm
->dumpable
= suid_dumpable
;
1062 current
->euid
= euid
;
1064 current
->fsuid
= current
->euid
;
1065 if (suid
!= (uid_t
) -1)
1066 current
->suid
= suid
;
1068 key_fsuid_changed(current
);
1069 proc_id_connector(current
, PROC_EVENT_UID
);
1071 return security_task_post_setuid(old_ruid
, old_euid
, old_suid
, LSM_SETID_RES
);
1074 asmlinkage
long sys_getresuid(uid_t __user
*ruid
, uid_t __user
*euid
, uid_t __user
*suid
)
1078 if (!(retval
= put_user(current
->uid
, ruid
)) &&
1079 !(retval
= put_user(current
->euid
, euid
)))
1080 retval
= put_user(current
->suid
, suid
);
1086 * Same as above, but for rgid, egid, sgid.
1088 asmlinkage
long sys_setresgid(gid_t rgid
, gid_t egid
, gid_t sgid
)
1092 retval
= security_task_setgid(rgid
, egid
, sgid
, LSM_SETID_RES
);
1096 if (!capable(CAP_SETGID
)) {
1097 if ((rgid
!= (gid_t
) -1) && (rgid
!= current
->gid
) &&
1098 (rgid
!= current
->egid
) && (rgid
!= current
->sgid
))
1100 if ((egid
!= (gid_t
) -1) && (egid
!= current
->gid
) &&
1101 (egid
!= current
->egid
) && (egid
!= current
->sgid
))
1103 if ((sgid
!= (gid_t
) -1) && (sgid
!= current
->gid
) &&
1104 (sgid
!= current
->egid
) && (sgid
!= current
->sgid
))
1107 if (egid
!= (gid_t
) -1) {
1108 if (egid
!= current
->egid
)
1110 current
->mm
->dumpable
= suid_dumpable
;
1113 current
->egid
= egid
;
1115 current
->fsgid
= current
->egid
;
1116 if (rgid
!= (gid_t
) -1)
1117 current
->gid
= rgid
;
1118 if (sgid
!= (gid_t
) -1)
1119 current
->sgid
= sgid
;
1121 key_fsgid_changed(current
);
1122 proc_id_connector(current
, PROC_EVENT_GID
);
1126 asmlinkage
long sys_getresgid(gid_t __user
*rgid
, gid_t __user
*egid
, gid_t __user
*sgid
)
1130 if (!(retval
= put_user(current
->gid
, rgid
)) &&
1131 !(retval
= put_user(current
->egid
, egid
)))
1132 retval
= put_user(current
->sgid
, sgid
);
1139 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
1140 * is used for "access()" and for the NFS daemon (letting nfsd stay at
1141 * whatever uid it wants to). It normally shadows "euid", except when
1142 * explicitly set by setfsuid() or for access..
1144 asmlinkage
long sys_setfsuid(uid_t uid
)
1148 old_fsuid
= current
->fsuid
;
1149 if (security_task_setuid(uid
, (uid_t
)-1, (uid_t
)-1, LSM_SETID_FS
))
1152 if (uid
== current
->uid
|| uid
== current
->euid
||
1153 uid
== current
->suid
|| uid
== current
->fsuid
||
1154 capable(CAP_SETUID
))
1156 if (uid
!= old_fsuid
)
1158 current
->mm
->dumpable
= suid_dumpable
;
1161 current
->fsuid
= uid
;
1164 key_fsuid_changed(current
);
1165 proc_id_connector(current
, PROC_EVENT_UID
);
1167 security_task_post_setuid(old_fsuid
, (uid_t
)-1, (uid_t
)-1, LSM_SETID_FS
);
1173 * Samma på svenska..
1175 asmlinkage
long sys_setfsgid(gid_t gid
)
1179 old_fsgid
= current
->fsgid
;
1180 if (security_task_setgid(gid
, (gid_t
)-1, (gid_t
)-1, LSM_SETID_FS
))
1183 if (gid
== current
->gid
|| gid
== current
->egid
||
1184 gid
== current
->sgid
|| gid
== current
->fsgid
||
1185 capable(CAP_SETGID
))
1187 if (gid
!= old_fsgid
)
1189 current
->mm
->dumpable
= suid_dumpable
;
1192 current
->fsgid
= gid
;
1193 key_fsgid_changed(current
);
1194 proc_id_connector(current
, PROC_EVENT_GID
);
1199 asmlinkage
long sys_times(struct tms __user
* tbuf
)
1202 * In the SMP world we might just be unlucky and have one of
1203 * the times increment as we use it. Since the value is an
1204 * atomically safe type this is just fine. Conceptually its
1205 * as if the syscall took an instant longer to occur.
1209 struct task_struct
*tsk
= current
;
1210 struct task_struct
*t
;
1211 cputime_t utime
, stime
, cutime
, cstime
;
1213 spin_lock_irq(&tsk
->sighand
->siglock
);
1214 utime
= tsk
->signal
->utime
;
1215 stime
= tsk
->signal
->stime
;
1218 utime
= cputime_add(utime
, t
->utime
);
1219 stime
= cputime_add(stime
, t
->stime
);
1223 cutime
= tsk
->signal
->cutime
;
1224 cstime
= tsk
->signal
->cstime
;
1225 spin_unlock_irq(&tsk
->sighand
->siglock
);
1227 tmp
.tms_utime
= cputime_to_clock_t(utime
);
1228 tmp
.tms_stime
= cputime_to_clock_t(stime
);
1229 tmp
.tms_cutime
= cputime_to_clock_t(cutime
);
1230 tmp
.tms_cstime
= cputime_to_clock_t(cstime
);
1231 if (copy_to_user(tbuf
, &tmp
, sizeof(struct tms
)))
1234 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1238 * This needs some heavy checking ...
1239 * I just haven't the stomach for it. I also don't fully
1240 * understand sessions/pgrp etc. Let somebody who does explain it.
1242 * OK, I think I have the protection semantics right.... this is really
1243 * only important on a multi-user system anyway, to make sure one user
1244 * can't send a signal to a process owned by another. -TYT, 12/12/91
1246 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1250 asmlinkage
long sys_setpgid(pid_t pid
, pid_t pgid
)
1252 struct task_struct
*p
;
1253 struct task_struct
*group_leader
= current
->group_leader
;
1257 pid
= group_leader
->pid
;
1263 /* From this point forward we keep holding onto the tasklist lock
1264 * so that our parent does not change from under us. -DaveM
1266 write_lock_irq(&tasklist_lock
);
1269 p
= find_task_by_pid(pid
);
1274 if (!thread_group_leader(p
))
1277 if (p
->real_parent
== group_leader
) {
1279 if (p
->signal
->session
!= group_leader
->signal
->session
)
1286 if (p
!= group_leader
)
1291 if (p
->signal
->leader
)
1295 struct task_struct
*p
;
1297 do_each_task_pid(pgid
, PIDTYPE_PGID
, p
) {
1298 if (p
->signal
->session
== group_leader
->signal
->session
)
1300 } while_each_task_pid(pgid
, PIDTYPE_PGID
, p
);
1305 err
= security_task_setpgid(p
, pgid
);
1309 if (process_group(p
) != pgid
) {
1310 detach_pid(p
, PIDTYPE_PGID
);
1311 p
->signal
->pgrp
= pgid
;
1312 attach_pid(p
, PIDTYPE_PGID
, pgid
);
1317 /* All paths lead to here, thus we are safe. -DaveM */
1318 write_unlock_irq(&tasklist_lock
);
1322 asmlinkage
long sys_getpgid(pid_t pid
)
1325 return process_group(current
);
1328 struct task_struct
*p
;
1330 read_lock(&tasklist_lock
);
1331 p
= find_task_by_pid(pid
);
1335 retval
= security_task_getpgid(p
);
1337 retval
= process_group(p
);
1339 read_unlock(&tasklist_lock
);
1344 #ifdef __ARCH_WANT_SYS_GETPGRP
1346 asmlinkage
long sys_getpgrp(void)
1348 /* SMP - assuming writes are word atomic this is fine */
1349 return process_group(current
);
1354 asmlinkage
long sys_getsid(pid_t pid
)
1357 return current
->signal
->session
;
1360 struct task_struct
*p
;
1362 read_lock(&tasklist_lock
);
1363 p
= find_task_by_pid(pid
);
1367 retval
= security_task_getsid(p
);
1369 retval
= p
->signal
->session
;
1371 read_unlock(&tasklist_lock
);
1376 asmlinkage
long sys_setsid(void)
1378 struct task_struct
*group_leader
= current
->group_leader
;
1382 mutex_lock(&tty_mutex
);
1383 write_lock_irq(&tasklist_lock
);
1385 /* Fail if I am already a session leader */
1386 if (group_leader
->signal
->leader
)
1389 session
= group_leader
->pid
;
1390 /* Fail if a process group id already exists that equals the
1391 * proposed session id.
1393 * Don't check if session id == 1 because kernel threads use this
1394 * session id and so the check will always fail and make it so
1395 * init cannot successfully call setsid.
1397 if (session
> 1 && find_task_by_pid_type(PIDTYPE_PGID
, session
))
1400 group_leader
->signal
->leader
= 1;
1401 __set_special_pids(session
, session
);
1402 group_leader
->signal
->tty
= NULL
;
1403 group_leader
->signal
->tty_old_pgrp
= 0;
1404 err
= process_group(group_leader
);
1406 write_unlock_irq(&tasklist_lock
);
1407 mutex_unlock(&tty_mutex
);
1412 * Supplementary group IDs
1415 /* init to 2 - one for init_task, one to ensure it is never freed */
1416 struct group_info init_groups
= { .usage
= ATOMIC_INIT(2) };
1418 struct group_info
*groups_alloc(int gidsetsize
)
1420 struct group_info
*group_info
;
1424 nblocks
= (gidsetsize
+ NGROUPS_PER_BLOCK
- 1) / NGROUPS_PER_BLOCK
;
1425 /* Make sure we always allocate at least one indirect block pointer */
1426 nblocks
= nblocks
? : 1;
1427 group_info
= kmalloc(sizeof(*group_info
) + nblocks
*sizeof(gid_t
*), GFP_USER
);
1430 group_info
->ngroups
= gidsetsize
;
1431 group_info
->nblocks
= nblocks
;
1432 atomic_set(&group_info
->usage
, 1);
1434 if (gidsetsize
<= NGROUPS_SMALL
) {
1435 group_info
->blocks
[0] = group_info
->small_block
;
1437 for (i
= 0; i
< nblocks
; i
++) {
1439 b
= (void *)__get_free_page(GFP_USER
);
1441 goto out_undo_partial_alloc
;
1442 group_info
->blocks
[i
] = b
;
1447 out_undo_partial_alloc
:
1449 free_page((unsigned long)group_info
->blocks
[i
]);
1455 EXPORT_SYMBOL(groups_alloc
);
1457 void groups_free(struct group_info
*group_info
)
1459 if (group_info
->blocks
[0] != group_info
->small_block
) {
1461 for (i
= 0; i
< group_info
->nblocks
; i
++)
1462 free_page((unsigned long)group_info
->blocks
[i
]);
1467 EXPORT_SYMBOL(groups_free
);
1469 /* export the group_info to a user-space array */
1470 static int groups_to_user(gid_t __user
*grouplist
,
1471 struct group_info
*group_info
)
1474 int count
= group_info
->ngroups
;
1476 for (i
= 0; i
< group_info
->nblocks
; i
++) {
1477 int cp_count
= min(NGROUPS_PER_BLOCK
, count
);
1478 int off
= i
* NGROUPS_PER_BLOCK
;
1479 int len
= cp_count
* sizeof(*grouplist
);
1481 if (copy_to_user(grouplist
+off
, group_info
->blocks
[i
], len
))
1489 /* fill a group_info from a user-space array - it must be allocated already */
1490 static int groups_from_user(struct group_info
*group_info
,
1491 gid_t __user
*grouplist
)
1494 int count
= group_info
->ngroups
;
1496 for (i
= 0; i
< group_info
->nblocks
; i
++) {
1497 int cp_count
= min(NGROUPS_PER_BLOCK
, count
);
1498 int off
= i
* NGROUPS_PER_BLOCK
;
1499 int len
= cp_count
* sizeof(*grouplist
);
1501 if (copy_from_user(group_info
->blocks
[i
], grouplist
+off
, len
))
1509 /* a simple Shell sort */
1510 static void groups_sort(struct group_info
*group_info
)
1512 int base
, max
, stride
;
1513 int gidsetsize
= group_info
->ngroups
;
1515 for (stride
= 1; stride
< gidsetsize
; stride
= 3 * stride
+ 1)
1520 max
= gidsetsize
- stride
;
1521 for (base
= 0; base
< max
; base
++) {
1523 int right
= left
+ stride
;
1524 gid_t tmp
= GROUP_AT(group_info
, right
);
1526 while (left
>= 0 && GROUP_AT(group_info
, left
) > tmp
) {
1527 GROUP_AT(group_info
, right
) =
1528 GROUP_AT(group_info
, left
);
1532 GROUP_AT(group_info
, right
) = tmp
;
1538 /* a simple bsearch */
1539 int groups_search(struct group_info
*group_info
, gid_t grp
)
1541 unsigned int left
, right
;
1547 right
= group_info
->ngroups
;
1548 while (left
< right
) {
1549 unsigned int mid
= (left
+right
)/2;
1550 int cmp
= grp
- GROUP_AT(group_info
, mid
);
1561 /* validate and set current->group_info */
1562 int set_current_groups(struct group_info
*group_info
)
1565 struct group_info
*old_info
;
1567 retval
= security_task_setgroups(group_info
);
1571 groups_sort(group_info
);
1572 get_group_info(group_info
);
1575 old_info
= current
->group_info
;
1576 current
->group_info
= group_info
;
1577 task_unlock(current
);
1579 put_group_info(old_info
);
1584 EXPORT_SYMBOL(set_current_groups
);
1586 asmlinkage
long sys_getgroups(int gidsetsize
, gid_t __user
*grouplist
)
1591 * SMP: Nobody else can change our grouplist. Thus we are
1598 /* no need to grab task_lock here; it cannot change */
1599 i
= current
->group_info
->ngroups
;
1601 if (i
> gidsetsize
) {
1605 if (groups_to_user(grouplist
, current
->group_info
)) {
1615 * SMP: Our groups are copy-on-write. We can set them safely
1616 * without another task interfering.
1619 asmlinkage
long sys_setgroups(int gidsetsize
, gid_t __user
*grouplist
)
1621 struct group_info
*group_info
;
1624 if (!capable(CAP_SETGID
))
1626 if ((unsigned)gidsetsize
> NGROUPS_MAX
)
1629 group_info
= groups_alloc(gidsetsize
);
1632 retval
= groups_from_user(group_info
, grouplist
);
1634 put_group_info(group_info
);
1638 retval
= set_current_groups(group_info
);
1639 put_group_info(group_info
);
1645 * Check whether we're fsgid/egid or in the supplemental group..
1647 int in_group_p(gid_t grp
)
1650 if (grp
!= current
->fsgid
) {
1651 retval
= groups_search(current
->group_info
, grp
);
1656 EXPORT_SYMBOL(in_group_p
);
1658 int in_egroup_p(gid_t grp
)
1661 if (grp
!= current
->egid
) {
1662 retval
= groups_search(current
->group_info
, grp
);
1667 EXPORT_SYMBOL(in_egroup_p
);
1669 DECLARE_RWSEM(uts_sem
);
1671 EXPORT_SYMBOL(uts_sem
);
1673 asmlinkage
long sys_newuname(struct new_utsname __user
* name
)
1677 down_read(&uts_sem
);
1678 if (copy_to_user(name
,&system_utsname
,sizeof *name
))
1684 asmlinkage
long sys_sethostname(char __user
*name
, int len
)
1687 char tmp
[__NEW_UTS_LEN
];
1689 if (!capable(CAP_SYS_ADMIN
))
1691 if (len
< 0 || len
> __NEW_UTS_LEN
)
1693 down_write(&uts_sem
);
1695 if (!copy_from_user(tmp
, name
, len
)) {
1696 memcpy(system_utsname
.nodename
, tmp
, len
);
1697 system_utsname
.nodename
[len
] = 0;
1704 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1706 asmlinkage
long sys_gethostname(char __user
*name
, int len
)
1712 down_read(&uts_sem
);
1713 i
= 1 + strlen(system_utsname
.nodename
);
1717 if (copy_to_user(name
, system_utsname
.nodename
, i
))
1726 * Only setdomainname; getdomainname can be implemented by calling
1729 asmlinkage
long sys_setdomainname(char __user
*name
, int len
)
1732 char tmp
[__NEW_UTS_LEN
];
1734 if (!capable(CAP_SYS_ADMIN
))
1736 if (len
< 0 || len
> __NEW_UTS_LEN
)
1739 down_write(&uts_sem
);
1741 if (!copy_from_user(tmp
, name
, len
)) {
1742 memcpy(system_utsname
.domainname
, tmp
, len
);
1743 system_utsname
.domainname
[len
] = 0;
1750 asmlinkage
long sys_getrlimit(unsigned int resource
, struct rlimit __user
*rlim
)
1752 if (resource
>= RLIM_NLIMITS
)
1755 struct rlimit value
;
1756 task_lock(current
->group_leader
);
1757 value
= current
->signal
->rlim
[resource
];
1758 task_unlock(current
->group_leader
);
1759 return copy_to_user(rlim
, &value
, sizeof(*rlim
)) ? -EFAULT
: 0;
1763 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1766 * Back compatibility for getrlimit. Needed for some apps.
1769 asmlinkage
long sys_old_getrlimit(unsigned int resource
, struct rlimit __user
*rlim
)
1772 if (resource
>= RLIM_NLIMITS
)
1775 task_lock(current
->group_leader
);
1776 x
= current
->signal
->rlim
[resource
];
1777 task_unlock(current
->group_leader
);
1778 if(x
.rlim_cur
> 0x7FFFFFFF)
1779 x
.rlim_cur
= 0x7FFFFFFF;
1780 if(x
.rlim_max
> 0x7FFFFFFF)
1781 x
.rlim_max
= 0x7FFFFFFF;
1782 return copy_to_user(rlim
, &x
, sizeof(x
))?-EFAULT
:0;
1787 asmlinkage
long sys_setrlimit(unsigned int resource
, struct rlimit __user
*rlim
)
1789 struct rlimit new_rlim
, *old_rlim
;
1790 unsigned long it_prof_secs
;
1793 if (resource
>= RLIM_NLIMITS
)
1795 if (copy_from_user(&new_rlim
, rlim
, sizeof(*rlim
)))
1797 if (new_rlim
.rlim_cur
> new_rlim
.rlim_max
)
1799 old_rlim
= current
->signal
->rlim
+ resource
;
1800 if ((new_rlim
.rlim_max
> old_rlim
->rlim_max
) &&
1801 !capable(CAP_SYS_RESOURCE
))
1803 if (resource
== RLIMIT_NOFILE
&& new_rlim
.rlim_max
> NR_OPEN
)
1806 retval
= security_task_setrlimit(resource
, &new_rlim
);
1810 task_lock(current
->group_leader
);
1811 *old_rlim
= new_rlim
;
1812 task_unlock(current
->group_leader
);
1814 if (resource
!= RLIMIT_CPU
)
1818 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1819 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1820 * very long-standing error, and fixing it now risks breakage of
1821 * applications, so we live with it
1823 if (new_rlim
.rlim_cur
== RLIM_INFINITY
)
1826 it_prof_secs
= cputime_to_secs(current
->signal
->it_prof_expires
);
1827 if (it_prof_secs
== 0 || new_rlim
.rlim_cur
<= it_prof_secs
) {
1828 unsigned long rlim_cur
= new_rlim
.rlim_cur
;
1831 if (rlim_cur
== 0) {
1833 * The caller is asking for an immediate RLIMIT_CPU
1834 * expiry. But we use the zero value to mean "it was
1835 * never set". So let's cheat and make it one second
1840 cputime
= secs_to_cputime(rlim_cur
);
1841 read_lock(&tasklist_lock
);
1842 spin_lock_irq(¤t
->sighand
->siglock
);
1843 set_process_cpu_timer(current
, CPUCLOCK_PROF
, &cputime
, NULL
);
1844 spin_unlock_irq(¤t
->sighand
->siglock
);
1845 read_unlock(&tasklist_lock
);
1852 * It would make sense to put struct rusage in the task_struct,
1853 * except that would make the task_struct be *really big*. After
1854 * task_struct gets moved into malloc'ed memory, it would
1855 * make sense to do this. It will make moving the rest of the information
1856 * a lot simpler! (Which we're not doing right now because we're not
1857 * measuring them yet).
1859 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1860 * races with threads incrementing their own counters. But since word
1861 * reads are atomic, we either get new values or old values and we don't
1862 * care which for the sums. We always take the siglock to protect reading
1863 * the c* fields from p->signal from races with exit.c updating those
1864 * fields when reaping, so a sample either gets all the additions of a
1865 * given child after it's reaped, or none so this sample is before reaping.
1868 * We need to take the siglock for CHILDEREN, SELF and BOTH
1869 * for the cases current multithreaded, non-current single threaded
1870 * non-current multithreaded. Thread traversal is now safe with
1872 * Strictly speaking, we donot need to take the siglock if we are current and
1873 * single threaded, as no one else can take our signal_struct away, no one
1874 * else can reap the children to update signal->c* counters, and no one else
1875 * can race with the signal-> fields. If we do not take any lock, the
1876 * signal-> fields could be read out of order while another thread was just
1877 * exiting. So we should place a read memory barrier when we avoid the lock.
1878 * On the writer side, write memory barrier is implied in __exit_signal
1879 * as __exit_signal releases the siglock spinlock after updating the signal->
1880 * fields. But we don't do this yet to keep things simple.
1884 static void k_getrusage(struct task_struct
*p
, int who
, struct rusage
*r
)
1886 struct task_struct
*t
;
1887 unsigned long flags
;
1888 cputime_t utime
, stime
;
1890 memset((char *) r
, 0, sizeof *r
);
1891 utime
= stime
= cputime_zero
;
1894 if (!lock_task_sighand(p
, &flags
)) {
1901 case RUSAGE_CHILDREN
:
1902 utime
= p
->signal
->cutime
;
1903 stime
= p
->signal
->cstime
;
1904 r
->ru_nvcsw
= p
->signal
->cnvcsw
;
1905 r
->ru_nivcsw
= p
->signal
->cnivcsw
;
1906 r
->ru_minflt
= p
->signal
->cmin_flt
;
1907 r
->ru_majflt
= p
->signal
->cmaj_flt
;
1909 if (who
== RUSAGE_CHILDREN
)
1913 utime
= cputime_add(utime
, p
->signal
->utime
);
1914 stime
= cputime_add(stime
, p
->signal
->stime
);
1915 r
->ru_nvcsw
+= p
->signal
->nvcsw
;
1916 r
->ru_nivcsw
+= p
->signal
->nivcsw
;
1917 r
->ru_minflt
+= p
->signal
->min_flt
;
1918 r
->ru_majflt
+= p
->signal
->maj_flt
;
1921 utime
= cputime_add(utime
, t
->utime
);
1922 stime
= cputime_add(stime
, t
->stime
);
1923 r
->ru_nvcsw
+= t
->nvcsw
;
1924 r
->ru_nivcsw
+= t
->nivcsw
;
1925 r
->ru_minflt
+= t
->min_flt
;
1926 r
->ru_majflt
+= t
->maj_flt
;
1935 unlock_task_sighand(p
, &flags
);
1938 cputime_to_timeval(utime
, &r
->ru_utime
);
1939 cputime_to_timeval(stime
, &r
->ru_stime
);
1942 int getrusage(struct task_struct
*p
, int who
, struct rusage __user
*ru
)
1945 k_getrusage(p
, who
, &r
);
1946 return copy_to_user(ru
, &r
, sizeof(r
)) ? -EFAULT
: 0;
1949 asmlinkage
long sys_getrusage(int who
, struct rusage __user
*ru
)
1951 if (who
!= RUSAGE_SELF
&& who
!= RUSAGE_CHILDREN
)
1953 return getrusage(current
, who
, ru
);
1956 asmlinkage
long sys_umask(int mask
)
1958 mask
= xchg(¤t
->fs
->umask
, mask
& S_IRWXUGO
);
1962 asmlinkage
long sys_prctl(int option
, unsigned long arg2
, unsigned long arg3
,
1963 unsigned long arg4
, unsigned long arg5
)
1967 error
= security_task_prctl(option
, arg2
, arg3
, arg4
, arg5
);
1972 case PR_SET_PDEATHSIG
:
1973 if (!valid_signal(arg2
)) {
1977 current
->pdeath_signal
= arg2
;
1979 case PR_GET_PDEATHSIG
:
1980 error
= put_user(current
->pdeath_signal
, (int __user
*)arg2
);
1982 case PR_GET_DUMPABLE
:
1983 error
= current
->mm
->dumpable
;
1985 case PR_SET_DUMPABLE
:
1986 if (arg2
< 0 || arg2
> 1) {
1990 current
->mm
->dumpable
= arg2
;
1993 case PR_SET_UNALIGN
:
1994 error
= SET_UNALIGN_CTL(current
, arg2
);
1996 case PR_GET_UNALIGN
:
1997 error
= GET_UNALIGN_CTL(current
, arg2
);
2000 error
= SET_FPEMU_CTL(current
, arg2
);
2003 error
= GET_FPEMU_CTL(current
, arg2
);
2006 error
= SET_FPEXC_CTL(current
, arg2
);
2009 error
= GET_FPEXC_CTL(current
, arg2
);
2012 error
= PR_TIMING_STATISTICAL
;
2015 if (arg2
== PR_TIMING_STATISTICAL
)
2021 case PR_GET_KEEPCAPS
:
2022 if (current
->keep_capabilities
)
2025 case PR_SET_KEEPCAPS
:
2026 if (arg2
!= 0 && arg2
!= 1) {
2030 current
->keep_capabilities
= arg2
;
2033 struct task_struct
*me
= current
;
2034 unsigned char ncomm
[sizeof(me
->comm
)];
2036 ncomm
[sizeof(me
->comm
)-1] = 0;
2037 if (strncpy_from_user(ncomm
, (char __user
*)arg2
,
2038 sizeof(me
->comm
)-1) < 0)
2040 set_task_comm(me
, ncomm
);
2044 struct task_struct
*me
= current
;
2045 unsigned char tcomm
[sizeof(me
->comm
)];
2047 get_task_comm(tcomm
, me
);
2048 if (copy_to_user((char __user
*)arg2
, tcomm
, sizeof(tcomm
)))
2053 error
= GET_ENDIAN(current
, arg2
);
2056 error
= SET_ENDIAN(current
, arg2
);