Cleanup syscall code to look more like it's mips64 equivalent.
[linux-2.6/linux-mips.git] / kernel / sys.c
blob4c3f48c93148528de18fad5c4d08173665b7995a
1 /*
2 * linux/kernel/sys.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/config.h>
8 #include <linux/compat.h>
9 #include <linux/module.h>
10 #include <linux/mm.h>
11 #include <linux/utsname.h>
12 #include <linux/mman.h>
13 #include <linux/smp_lock.h>
14 #include <linux/notifier.h>
15 #include <linux/reboot.h>
16 #include <linux/prctl.h>
17 #include <linux/init.h>
18 #include <linux/highuid.h>
19 #include <linux/fs.h>
20 #include <linux/workqueue.h>
21 #include <linux/device.h>
22 #include <linux/times.h>
23 #include <linux/security.h>
24 #include <linux/dcookies.h>
25 #include <linux/suspend.h>
27 #include <asm/uaccess.h>
28 #include <asm/io.h>
29 #include <asm/unistd.h>
31 #ifndef SET_UNALIGN_CTL
32 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
33 #endif
34 #ifndef GET_UNALIGN_CTL
35 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
36 #endif
37 #ifndef SET_FPEMU_CTL
38 # define SET_FPEMU_CTL(a,b) (-EINVAL)
39 #endif
40 #ifndef GET_FPEMU_CTL
41 # define GET_FPEMU_CTL(a,b) (-EINVAL)
42 #endif
43 #ifndef SET_FPEXC_CTL
44 # define SET_FPEXC_CTL(a,b) (-EINVAL)
45 #endif
46 #ifndef GET_FPEXC_CTL
47 # define GET_FPEXC_CTL(a,b) (-EINVAL)
48 #endif
51 * this is where the system-wide overflow UID and GID are defined, for
52 * architectures that now have 32-bit UID/GID but didn't in the past
55 int overflowuid = DEFAULT_OVERFLOWUID;
56 int overflowgid = DEFAULT_OVERFLOWGID;
59 * the same as above, but for filesystems which can only store a 16-bit
60 * UID and GID. as such, this is needed on all architectures
63 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
64 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
67 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
70 int C_A_D = 1;
71 int cad_pid = 1;
73 extern int system_running;
76 * Notifier list for kernel code which wants to be called
77 * at shutdown. This is used to stop any idling DMA operations
78 * and the like.
81 static struct notifier_block *reboot_notifier_list;
82 rwlock_t notifier_lock = RW_LOCK_UNLOCKED;
84 /**
85 * notifier_chain_register - Add notifier to a notifier chain
86 * @list: Pointer to root list pointer
87 * @n: New entry in notifier chain
89 * Adds a notifier to a notifier chain.
91 * Currently always returns zero.
94 int notifier_chain_register(struct notifier_block **list, struct notifier_block *n)
96 write_lock(&notifier_lock);
97 while(*list)
99 if(n->priority > (*list)->priority)
100 break;
101 list= &((*list)->next);
103 n->next = *list;
104 *list=n;
105 write_unlock(&notifier_lock);
106 return 0;
110 * notifier_chain_unregister - Remove notifier from a notifier chain
111 * @nl: Pointer to root list pointer
112 * @n: New entry in notifier chain
114 * Removes a notifier from a notifier chain.
116 * Returns zero on success, or %-ENOENT on failure.
119 int notifier_chain_unregister(struct notifier_block **nl, struct notifier_block *n)
121 write_lock(&notifier_lock);
122 while((*nl)!=NULL)
124 if((*nl)==n)
126 *nl=n->next;
127 write_unlock(&notifier_lock);
128 return 0;
130 nl=&((*nl)->next);
132 write_unlock(&notifier_lock);
133 return -ENOENT;
137 * notifier_call_chain - Call functions in a notifier chain
138 * @n: Pointer to root pointer of notifier chain
139 * @val: Value passed unmodified to notifier function
140 * @v: Pointer passed unmodified to notifier function
142 * Calls each function in a notifier chain in turn.
144 * If the return value of the notifier can be and'd
145 * with %NOTIFY_STOP_MASK, then notifier_call_chain
146 * will return immediately, with the return value of
147 * the notifier function which halted execution.
148 * Otherwise, the return value is the return value
149 * of the last notifier function called.
152 int notifier_call_chain(struct notifier_block **n, unsigned long val, void *v)
154 int ret=NOTIFY_DONE;
155 struct notifier_block *nb = *n;
157 while(nb)
159 ret=nb->notifier_call(nb,val,v);
160 if(ret&NOTIFY_STOP_MASK)
162 return ret;
164 nb=nb->next;
166 return ret;
170 * register_reboot_notifier - Register function to be called at reboot time
171 * @nb: Info about notifier function to be called
173 * Registers a function with the list of functions
174 * to be called at reboot time.
176 * Currently always returns zero, as notifier_chain_register
177 * always returns zero.
180 int register_reboot_notifier(struct notifier_block * nb)
182 return notifier_chain_register(&reboot_notifier_list, nb);
186 * unregister_reboot_notifier - Unregister previously registered reboot notifier
187 * @nb: Hook to be unregistered
189 * Unregisters a previously registered reboot
190 * notifier function.
192 * Returns zero on success, or %-ENOENT on failure.
195 int unregister_reboot_notifier(struct notifier_block * nb)
197 return notifier_chain_unregister(&reboot_notifier_list, nb);
200 asmlinkage long sys_ni_syscall(void)
202 return -ENOSYS;
205 cond_syscall(sys_nfsservctl)
206 cond_syscall(sys_quotactl)
207 cond_syscall(sys_acct)
208 cond_syscall(sys_lookup_dcookie)
209 cond_syscall(sys_swapon)
210 cond_syscall(sys_swapoff)
211 cond_syscall(sys_init_module)
212 cond_syscall(sys_delete_module)
213 cond_syscall(sys_socketpair)
214 cond_syscall(sys_bind)
215 cond_syscall(sys_listen)
216 cond_syscall(sys_accept)
217 cond_syscall(sys_connect)
218 cond_syscall(sys_getsockname)
219 cond_syscall(sys_getpeername)
220 cond_syscall(sys_sendto)
221 cond_syscall(sys_send)
222 cond_syscall(sys_recvfrom)
223 cond_syscall(sys_recv)
224 cond_syscall(sys_socket)
225 cond_syscall(sys_setsockopt)
226 cond_syscall(sys_getsockopt)
227 cond_syscall(sys_shutdown)
228 cond_syscall(sys_sendmsg)
229 cond_syscall(sys_recvmsg)
230 cond_syscall(sys_socketcall)
231 cond_syscall(sys_futex)
232 cond_syscall(compat_sys_futex)
233 cond_syscall(sys_epoll_create)
234 cond_syscall(sys_epoll_ctl)
235 cond_syscall(sys_epoll_wait)
236 cond_syscall(sys_pciconfig_read)
237 cond_syscall(sys_pciconfig_write)
239 static int set_one_prio(struct task_struct *p, int niceval, int error)
241 int no_nice;
243 if (p->uid != current->euid &&
244 p->uid != current->uid && !capable(CAP_SYS_NICE)) {
245 error = -EPERM;
246 goto out;
248 if (niceval < task_nice(p) && !capable(CAP_SYS_NICE)) {
249 error = -EACCES;
250 goto out;
252 no_nice = security_task_setnice(p, niceval);
253 if (no_nice) {
254 error = no_nice;
255 goto out;
257 if (error == -ESRCH)
258 error = 0;
259 set_user_nice(p, niceval);
260 out:
261 return error;
264 asmlinkage long sys_setpriority(int which, int who, int niceval)
266 struct task_struct *g, *p;
267 struct user_struct *user;
268 struct pid *pid;
269 struct list_head *l;
270 int error = -EINVAL;
272 if (which > 2 || which < 0)
273 goto out;
275 /* normalize: avoid signed division (rounding problems) */
276 error = -ESRCH;
277 if (niceval < -20)
278 niceval = -20;
279 if (niceval > 19)
280 niceval = 19;
282 read_lock(&tasklist_lock);
283 switch (which) {
284 case PRIO_PROCESS:
285 if (!who)
286 who = current->pid;
287 p = find_task_by_pid(who);
288 if (p)
289 error = set_one_prio(p, niceval, error);
290 break;
291 case PRIO_PGRP:
292 if (!who)
293 who = current->pgrp;
294 for_each_task_pid(who, PIDTYPE_PGID, p, l, pid)
295 error = set_one_prio(p, niceval, error);
296 break;
297 case PRIO_USER:
298 if (!who)
299 user = current->user;
300 else
301 user = find_user(who);
303 if (!user)
304 goto out_unlock;
306 do_each_thread(g, p)
307 if (p->uid == who)
308 error = set_one_prio(p, niceval, error);
309 while_each_thread(g, p);
310 break;
312 out_unlock:
313 read_unlock(&tasklist_lock);
314 out:
315 return error;
319 * Ugh. To avoid negative return values, "getpriority()" will
320 * not return the normal nice-value, but a negated value that
321 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
322 * to stay compatible.
324 asmlinkage long sys_getpriority(int which, int who)
326 struct task_struct *g, *p;
327 struct list_head *l;
328 struct pid *pid;
329 struct user_struct *user;
330 long niceval, retval = -ESRCH;
332 if (which > 2 || which < 0)
333 return -EINVAL;
335 read_lock(&tasklist_lock);
336 switch (which) {
337 case PRIO_PROCESS:
338 if (!who)
339 who = current->pid;
340 p = find_task_by_pid(who);
341 if (p) {
342 niceval = 20 - task_nice(p);
343 if (niceval > retval)
344 retval = niceval;
346 break;
347 case PRIO_PGRP:
348 if (!who)
349 who = current->pgrp;
350 for_each_task_pid(who, PIDTYPE_PGID, p, l, pid) {
351 niceval = 20 - task_nice(p);
352 if (niceval > retval)
353 retval = niceval;
355 break;
356 case PRIO_USER:
357 if (!who)
358 user = current->user;
359 else
360 user = find_user(who);
362 if (!user)
363 goto out_unlock;
365 do_each_thread(g, p)
366 if (p->uid == who) {
367 niceval = 20 - task_nice(p);
368 if (niceval > retval)
369 retval = niceval;
371 while_each_thread(g, p);
372 break;
374 out_unlock:
375 read_unlock(&tasklist_lock);
377 return retval;
382 * Reboot system call: for obvious reasons only root may call it,
383 * and even root needs to set up some magic numbers in the registers
384 * so that some mistake won't make this reboot the whole machine.
385 * You can also set the meaning of the ctrl-alt-del-key here.
387 * reboot doesn't sync: do that yourself before calling this.
389 asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
391 char buffer[256];
393 /* We only trust the superuser with rebooting the system. */
394 if (!capable(CAP_SYS_BOOT))
395 return -EPERM;
397 /* For safety, we require "magic" arguments. */
398 if (magic1 != LINUX_REBOOT_MAGIC1 ||
399 (magic2 != LINUX_REBOOT_MAGIC2 &&
400 magic2 != LINUX_REBOOT_MAGIC2A &&
401 magic2 != LINUX_REBOOT_MAGIC2B &&
402 magic2 != LINUX_REBOOT_MAGIC2C))
403 return -EINVAL;
405 lock_kernel();
406 switch (cmd) {
407 case LINUX_REBOOT_CMD_RESTART:
408 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, NULL);
409 system_running = 0;
410 device_shutdown();
411 printk(KERN_EMERG "Restarting system.\n");
412 machine_restart(NULL);
413 break;
415 case LINUX_REBOOT_CMD_CAD_ON:
416 C_A_D = 1;
417 break;
419 case LINUX_REBOOT_CMD_CAD_OFF:
420 C_A_D = 0;
421 break;
423 case LINUX_REBOOT_CMD_HALT:
424 notifier_call_chain(&reboot_notifier_list, SYS_HALT, NULL);
425 system_running = 0;
426 device_shutdown();
427 printk(KERN_EMERG "System halted.\n");
428 machine_halt();
429 unlock_kernel();
430 do_exit(0);
431 break;
433 case LINUX_REBOOT_CMD_POWER_OFF:
434 notifier_call_chain(&reboot_notifier_list, SYS_POWER_OFF, NULL);
435 system_running = 0;
436 device_shutdown();
437 printk(KERN_EMERG "Power down.\n");
438 machine_power_off();
439 unlock_kernel();
440 do_exit(0);
441 break;
443 case LINUX_REBOOT_CMD_RESTART2:
444 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
445 unlock_kernel();
446 return -EFAULT;
448 buffer[sizeof(buffer) - 1] = '\0';
450 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, buffer);
451 system_running = 0;
452 device_shutdown();
453 printk(KERN_EMERG "Restarting system with command '%s'.\n", buffer);
454 machine_restart(buffer);
455 break;
457 #ifdef CONFIG_SOFTWARE_SUSPEND
458 case LINUX_REBOOT_CMD_SW_SUSPEND:
459 if (!software_suspend_enabled) {
460 unlock_kernel();
461 return -EAGAIN;
463 software_suspend();
464 do_exit(0);
465 break;
466 #endif
468 default:
469 unlock_kernel();
470 return -EINVAL;
472 unlock_kernel();
473 return 0;
476 static void deferred_cad(void *dummy)
478 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, NULL);
479 machine_restart(NULL);
483 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
484 * As it's called within an interrupt, it may NOT sync: the only choice
485 * is whether to reboot at once, or just ignore the ctrl-alt-del.
487 void ctrl_alt_del(void)
489 static DECLARE_WORK(cad_work, deferred_cad, NULL);
491 if (C_A_D)
492 schedule_work(&cad_work);
493 else
494 kill_proc(cad_pid, SIGINT, 1);
499 * Unprivileged users may change the real gid to the effective gid
500 * or vice versa. (BSD-style)
502 * If you set the real gid at all, or set the effective gid to a value not
503 * equal to the real gid, then the saved gid is set to the new effective gid.
505 * This makes it possible for a setgid program to completely drop its
506 * privileges, which is often a useful assertion to make when you are doing
507 * a security audit over a program.
509 * The general idea is that a program which uses just setregid() will be
510 * 100% compatible with BSD. A program which uses just setgid() will be
511 * 100% compatible with POSIX with saved IDs.
513 * SMP: There are not races, the GIDs are checked only by filesystem
514 * operations (as far as semantic preservation is concerned).
516 asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
518 int old_rgid = current->gid;
519 int old_egid = current->egid;
520 int new_rgid = old_rgid;
521 int new_egid = old_egid;
522 int retval;
524 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
525 if (retval)
526 return retval;
528 if (rgid != (gid_t) -1) {
529 if ((old_rgid == rgid) ||
530 (current->egid==rgid) ||
531 capable(CAP_SETGID))
532 new_rgid = rgid;
533 else
534 return -EPERM;
536 if (egid != (gid_t) -1) {
537 if ((old_rgid == egid) ||
538 (current->egid == egid) ||
539 (current->sgid == egid) ||
540 capable(CAP_SETGID))
541 new_egid = egid;
542 else {
543 return -EPERM;
546 if (new_egid != old_egid)
548 current->mm->dumpable = 0;
549 wmb();
551 if (rgid != (gid_t) -1 ||
552 (egid != (gid_t) -1 && egid != old_rgid))
553 current->sgid = new_egid;
554 current->fsgid = new_egid;
555 current->egid = new_egid;
556 current->gid = new_rgid;
557 return 0;
561 * setgid() is implemented like SysV w/ SAVED_IDS
563 * SMP: Same implicit races as above.
565 asmlinkage long sys_setgid(gid_t gid)
567 int old_egid = current->egid;
568 int retval;
570 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
571 if (retval)
572 return retval;
574 if (capable(CAP_SETGID))
576 if(old_egid != gid)
578 current->mm->dumpable=0;
579 wmb();
581 current->gid = current->egid = current->sgid = current->fsgid = gid;
583 else if ((gid == current->gid) || (gid == current->sgid))
585 if(old_egid != gid)
587 current->mm->dumpable=0;
588 wmb();
590 current->egid = current->fsgid = gid;
592 else
593 return -EPERM;
594 return 0;
597 static int set_user(uid_t new_ruid, int dumpclear)
599 struct user_struct *new_user;
601 new_user = alloc_uid(new_ruid);
602 if (!new_user)
603 return -EAGAIN;
604 switch_uid(new_user);
606 if(dumpclear)
608 current->mm->dumpable = 0;
609 wmb();
611 current->uid = new_ruid;
612 return 0;
616 * Unprivileged users may change the real uid to the effective uid
617 * or vice versa. (BSD-style)
619 * If you set the real uid at all, or set the effective uid to a value not
620 * equal to the real uid, then the saved uid is set to the new effective uid.
622 * This makes it possible for a setuid program to completely drop its
623 * privileges, which is often a useful assertion to make when you are doing
624 * a security audit over a program.
626 * The general idea is that a program which uses just setreuid() will be
627 * 100% compatible with BSD. A program which uses just setuid() will be
628 * 100% compatible with POSIX with saved IDs.
630 asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
632 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
633 int retval;
635 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
636 if (retval)
637 return retval;
639 new_ruid = old_ruid = current->uid;
640 new_euid = old_euid = current->euid;
641 old_suid = current->suid;
643 if (ruid != (uid_t) -1) {
644 new_ruid = ruid;
645 if ((old_ruid != ruid) &&
646 (current->euid != ruid) &&
647 !capable(CAP_SETUID))
648 return -EPERM;
651 if (euid != (uid_t) -1) {
652 new_euid = euid;
653 if ((old_ruid != euid) &&
654 (current->euid != euid) &&
655 (current->suid != euid) &&
656 !capable(CAP_SETUID))
657 return -EPERM;
660 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
661 return -EAGAIN;
663 if (new_euid != old_euid)
665 current->mm->dumpable=0;
666 wmb();
668 current->fsuid = current->euid = new_euid;
669 if (ruid != (uid_t) -1 ||
670 (euid != (uid_t) -1 && euid != old_ruid))
671 current->suid = current->euid;
672 current->fsuid = current->euid;
674 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
680 * setuid() is implemented like SysV with SAVED_IDS
682 * Note that SAVED_ID's is deficient in that a setuid root program
683 * like sendmail, for example, cannot set its uid to be a normal
684 * user and then switch back, because if you're root, setuid() sets
685 * the saved uid too. If you don't like this, blame the bright people
686 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
687 * will allow a root program to temporarily drop privileges and be able to
688 * regain them by swapping the real and effective uid.
690 asmlinkage long sys_setuid(uid_t uid)
692 int old_euid = current->euid;
693 int old_ruid, old_suid, new_ruid, new_suid;
694 int retval;
696 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
697 if (retval)
698 return retval;
700 old_ruid = new_ruid = current->uid;
701 old_suid = current->suid;
702 new_suid = old_suid;
704 if (capable(CAP_SETUID)) {
705 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
706 return -EAGAIN;
707 new_suid = uid;
708 } else if ((uid != current->uid) && (uid != new_suid))
709 return -EPERM;
711 if (old_euid != uid)
713 current->mm->dumpable = 0;
714 wmb();
716 current->fsuid = current->euid = uid;
717 current->suid = new_suid;
719 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
724 * This function implements a generic ability to update ruid, euid,
725 * and suid. This allows you to implement the 4.4 compatible seteuid().
727 asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
729 int old_ruid = current->uid;
730 int old_euid = current->euid;
731 int old_suid = current->suid;
732 int retval;
734 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
735 if (retval)
736 return retval;
738 if (!capable(CAP_SETUID)) {
739 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
740 (ruid != current->euid) && (ruid != current->suid))
741 return -EPERM;
742 if ((euid != (uid_t) -1) && (euid != current->uid) &&
743 (euid != current->euid) && (euid != current->suid))
744 return -EPERM;
745 if ((suid != (uid_t) -1) && (suid != current->uid) &&
746 (suid != current->euid) && (suid != current->suid))
747 return -EPERM;
749 if (ruid != (uid_t) -1) {
750 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
751 return -EAGAIN;
753 if (euid != (uid_t) -1) {
754 if (euid != current->euid)
756 current->mm->dumpable = 0;
757 wmb();
759 current->euid = euid;
761 current->fsuid = current->euid;
762 if (suid != (uid_t) -1)
763 current->suid = suid;
765 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
768 asmlinkage long sys_getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
770 int retval;
772 if (!(retval = put_user(current->uid, ruid)) &&
773 !(retval = put_user(current->euid, euid)))
774 retval = put_user(current->suid, suid);
776 return retval;
780 * Same as above, but for rgid, egid, sgid.
782 asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
784 int retval;
786 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
787 if (retval)
788 return retval;
790 if (!capable(CAP_SETGID)) {
791 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
792 (rgid != current->egid) && (rgid != current->sgid))
793 return -EPERM;
794 if ((egid != (gid_t) -1) && (egid != current->gid) &&
795 (egid != current->egid) && (egid != current->sgid))
796 return -EPERM;
797 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
798 (sgid != current->egid) && (sgid != current->sgid))
799 return -EPERM;
801 if (egid != (gid_t) -1) {
802 if (egid != current->egid)
804 current->mm->dumpable = 0;
805 wmb();
807 current->egid = egid;
809 current->fsgid = current->egid;
810 if (rgid != (gid_t) -1)
811 current->gid = rgid;
812 if (sgid != (gid_t) -1)
813 current->sgid = sgid;
814 return 0;
817 asmlinkage long sys_getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
819 int retval;
821 if (!(retval = put_user(current->gid, rgid)) &&
822 !(retval = put_user(current->egid, egid)))
823 retval = put_user(current->sgid, sgid);
825 return retval;
830 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
831 * is used for "access()" and for the NFS daemon (letting nfsd stay at
832 * whatever uid it wants to). It normally shadows "euid", except when
833 * explicitly set by setfsuid() or for access..
835 asmlinkage long sys_setfsuid(uid_t uid)
837 int old_fsuid;
839 old_fsuid = current->fsuid;
840 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
841 return old_fsuid;
843 if (uid == current->uid || uid == current->euid ||
844 uid == current->suid || uid == current->fsuid ||
845 capable(CAP_SETUID))
847 if (uid != old_fsuid)
849 current->mm->dumpable = 0;
850 wmb();
852 current->fsuid = uid;
855 security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
857 return old_fsuid;
861 * Samma på svenska..
863 asmlinkage long sys_setfsgid(gid_t gid)
865 int old_fsgid;
867 old_fsgid = current->fsgid;
868 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
869 return old_fsgid;
871 if (gid == current->gid || gid == current->egid ||
872 gid == current->sgid || gid == current->fsgid ||
873 capable(CAP_SETGID))
875 if (gid != old_fsgid)
877 current->mm->dumpable = 0;
878 wmb();
880 current->fsgid = gid;
882 return old_fsgid;
885 asmlinkage long sys_times(struct tms __user * tbuf)
888 * In the SMP world we might just be unlucky and have one of
889 * the times increment as we use it. Since the value is an
890 * atomically safe type this is just fine. Conceptually its
891 * as if the syscall took an instant longer to occur.
893 if (tbuf) {
894 struct tms tmp;
895 tmp.tms_utime = jiffies_to_clock_t(current->utime);
896 tmp.tms_stime = jiffies_to_clock_t(current->stime);
897 tmp.tms_cutime = jiffies_to_clock_t(current->cutime);
898 tmp.tms_cstime = jiffies_to_clock_t(current->cstime);
899 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
900 return -EFAULT;
902 return (long) jiffies_64_to_clock_t(get_jiffies_64());
906 * This needs some heavy checking ...
907 * I just haven't the stomach for it. I also don't fully
908 * understand sessions/pgrp etc. Let somebody who does explain it.
910 * OK, I think I have the protection semantics right.... this is really
911 * only important on a multi-user system anyway, to make sure one user
912 * can't send a signal to a process owned by another. -TYT, 12/12/91
914 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
915 * LBT 04.03.94
918 asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
920 struct task_struct *p;
921 int err = -EINVAL;
923 if (!pid)
924 pid = current->pid;
925 if (!pgid)
926 pgid = pid;
927 if (pgid < 0)
928 return -EINVAL;
930 /* From this point forward we keep holding onto the tasklist lock
931 * so that our parent does not change from under us. -DaveM
933 write_lock_irq(&tasklist_lock);
935 err = -ESRCH;
936 p = find_task_by_pid(pid);
937 if (!p)
938 goto out;
940 err = -EINVAL;
941 if (!thread_group_leader(p))
942 goto out;
944 if (p->parent == current || p->real_parent == current) {
945 err = -EPERM;
946 if (p->session != current->session)
947 goto out;
948 err = -EACCES;
949 if (p->did_exec)
950 goto out;
951 } else {
952 err = -ESRCH;
953 if (p != current)
954 goto out;
957 err = -EPERM;
958 if (p->leader)
959 goto out;
961 if (pgid != pid) {
962 struct task_struct *p;
963 struct pid *pid;
964 struct list_head *l;
966 for_each_task_pid(pgid, PIDTYPE_PGID, p, l, pid)
967 if (p->session == current->session)
968 goto ok_pgid;
969 goto out;
972 ok_pgid:
973 err = security_task_setpgid(p, pgid);
974 if (err)
975 goto out;
977 if (p->pgrp != pgid) {
978 detach_pid(p, PIDTYPE_PGID);
979 p->pgrp = pgid;
980 attach_pid(p, PIDTYPE_PGID, pgid);
982 err = 0;
983 out:
984 /* All paths lead to here, thus we are safe. -DaveM */
985 write_unlock_irq(&tasklist_lock);
986 return err;
989 asmlinkage long sys_getpgid(pid_t pid)
991 if (!pid) {
992 return current->pgrp;
993 } else {
994 int retval;
995 struct task_struct *p;
997 read_lock(&tasklist_lock);
998 p = find_task_by_pid(pid);
1000 retval = -ESRCH;
1001 if (p) {
1002 retval = security_task_getpgid(p);
1003 if (!retval)
1004 retval = p->pgrp;
1006 read_unlock(&tasklist_lock);
1007 return retval;
1011 asmlinkage long sys_getpgrp(void)
1013 /* SMP - assuming writes are word atomic this is fine */
1014 return current->pgrp;
1017 asmlinkage long sys_getsid(pid_t pid)
1019 if (!pid) {
1020 return current->session;
1021 } else {
1022 int retval;
1023 struct task_struct *p;
1025 read_lock(&tasklist_lock);
1026 p = find_task_by_pid(pid);
1028 retval = -ESRCH;
1029 if(p) {
1030 retval = security_task_getsid(p);
1031 if (!retval)
1032 retval = p->session;
1034 read_unlock(&tasklist_lock);
1035 return retval;
1039 asmlinkage long sys_setsid(void)
1041 struct pid *pid;
1042 int err = -EPERM;
1044 if (!thread_group_leader(current))
1045 return -EINVAL;
1047 write_lock_irq(&tasklist_lock);
1049 pid = find_pid(PIDTYPE_PGID, current->pid);
1050 if (pid)
1051 goto out;
1053 current->leader = 1;
1054 __set_special_pids(current->pid, current->pid);
1055 current->tty = NULL;
1056 current->tty_old_pgrp = 0;
1057 err = current->pgrp;
1058 out:
1059 write_unlock_irq(&tasklist_lock);
1060 return err;
1064 * Supplementary group IDs
1066 asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1068 int i;
1071 * SMP: Nobody else can change our grouplist. Thus we are
1072 * safe.
1075 if (gidsetsize < 0)
1076 return -EINVAL;
1077 i = current->ngroups;
1078 if (gidsetsize) {
1079 if (i > gidsetsize)
1080 return -EINVAL;
1081 if (copy_to_user(grouplist, current->groups, sizeof(gid_t)*i))
1082 return -EFAULT;
1084 return i;
1088 * SMP: Our groups are not shared. We can copy to/from them safely
1089 * without another task interfering.
1092 asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1094 gid_t groups[NGROUPS];
1095 int retval;
1097 if (!capable(CAP_SETGID))
1098 return -EPERM;
1099 if ((unsigned) gidsetsize > NGROUPS)
1100 return -EINVAL;
1101 if (copy_from_user(groups, grouplist, gidsetsize * sizeof(gid_t)))
1102 return -EFAULT;
1103 retval = security_task_setgroups(gidsetsize, groups);
1104 if (retval)
1105 return retval;
1106 memcpy(current->groups, groups, gidsetsize * sizeof(gid_t));
1107 current->ngroups = gidsetsize;
1108 return 0;
1111 static int supplemental_group_member(gid_t grp)
1113 int i = current->ngroups;
1115 if (i) {
1116 gid_t *groups = current->groups;
1117 do {
1118 if (*groups == grp)
1119 return 1;
1120 groups++;
1121 i--;
1122 } while (i);
1124 return 0;
1128 * Check whether we're fsgid/egid or in the supplemental group..
1130 int in_group_p(gid_t grp)
1132 int retval = 1;
1133 if (grp != current->fsgid)
1134 retval = supplemental_group_member(grp);
1135 return retval;
1138 int in_egroup_p(gid_t grp)
1140 int retval = 1;
1141 if (grp != current->egid)
1142 retval = supplemental_group_member(grp);
1143 return retval;
1146 DECLARE_RWSEM(uts_sem);
1148 asmlinkage long sys_newuname(struct new_utsname __user * name)
1150 int errno = 0;
1152 down_read(&uts_sem);
1153 if (copy_to_user(name,&system_utsname,sizeof *name))
1154 errno = -EFAULT;
1155 up_read(&uts_sem);
1156 return errno;
1159 asmlinkage long sys_sethostname(char __user *name, int len)
1161 int errno;
1163 if (!capable(CAP_SYS_ADMIN))
1164 return -EPERM;
1165 if (len < 0 || len > __NEW_UTS_LEN)
1166 return -EINVAL;
1167 down_write(&uts_sem);
1168 errno = -EFAULT;
1169 if (!copy_from_user(system_utsname.nodename, name, len)) {
1170 system_utsname.nodename[len] = 0;
1171 errno = 0;
1173 up_write(&uts_sem);
1174 return errno;
1177 asmlinkage long sys_gethostname(char __user *name, int len)
1179 int i, errno;
1181 if (len < 0)
1182 return -EINVAL;
1183 down_read(&uts_sem);
1184 i = 1 + strlen(system_utsname.nodename);
1185 if (i > len)
1186 i = len;
1187 errno = 0;
1188 if (copy_to_user(name, system_utsname.nodename, i))
1189 errno = -EFAULT;
1190 up_read(&uts_sem);
1191 return errno;
1195 * Only setdomainname; getdomainname can be implemented by calling
1196 * uname()
1198 asmlinkage long sys_setdomainname(char __user *name, int len)
1200 int errno;
1202 if (!capable(CAP_SYS_ADMIN))
1203 return -EPERM;
1204 if (len < 0 || len > __NEW_UTS_LEN)
1205 return -EINVAL;
1207 down_write(&uts_sem);
1208 errno = -EFAULT;
1209 if (!copy_from_user(system_utsname.domainname, name, len)) {
1210 errno = 0;
1211 system_utsname.domainname[len] = 0;
1213 up_write(&uts_sem);
1214 return errno;
1217 asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1219 if (resource >= RLIM_NLIMITS)
1220 return -EINVAL;
1221 else
1222 return copy_to_user(rlim, current->rlim + resource, sizeof(*rlim))
1223 ? -EFAULT : 0;
1226 #if defined(COMPAT_RLIM_OLD_INFINITY) || !(defined(CONFIG_IA64) || defined(CONFIG_V850))
1229 * Back compatibility for getrlimit. Needed for some apps.
1232 asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1234 struct rlimit x;
1235 if (resource >= RLIM_NLIMITS)
1236 return -EINVAL;
1238 memcpy(&x, current->rlim + resource, sizeof(*rlim));
1239 if(x.rlim_cur > 0x7FFFFFFF)
1240 x.rlim_cur = 0x7FFFFFFF;
1241 if(x.rlim_max > 0x7FFFFFFF)
1242 x.rlim_max = 0x7FFFFFFF;
1243 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1246 #endif
1248 asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1250 struct rlimit new_rlim, *old_rlim;
1251 int retval;
1253 if (resource >= RLIM_NLIMITS)
1254 return -EINVAL;
1255 if(copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1256 return -EFAULT;
1257 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1258 return -EINVAL;
1259 old_rlim = current->rlim + resource;
1260 if (((new_rlim.rlim_cur > old_rlim->rlim_max) ||
1261 (new_rlim.rlim_max > old_rlim->rlim_max)) &&
1262 !capable(CAP_SYS_RESOURCE))
1263 return -EPERM;
1264 if (resource == RLIMIT_NOFILE) {
1265 if (new_rlim.rlim_cur > NR_OPEN || new_rlim.rlim_max > NR_OPEN)
1266 return -EPERM;
1269 retval = security_task_setrlimit(resource, &new_rlim);
1270 if (retval)
1271 return retval;
1273 *old_rlim = new_rlim;
1274 return 0;
1278 * It would make sense to put struct rusage in the task_struct,
1279 * except that would make the task_struct be *really big*. After
1280 * task_struct gets moved into malloc'ed memory, it would
1281 * make sense to do this. It will make moving the rest of the information
1282 * a lot simpler! (Which we're not doing right now because we're not
1283 * measuring them yet).
1285 * This is SMP safe. Either we are called from sys_getrusage on ourselves
1286 * below (we know we aren't going to exit/disappear and only we change our
1287 * rusage counters), or we are called from wait4() on a process which is
1288 * either stopped or zombied. In the zombied case the task won't get
1289 * reaped till shortly after the call to getrusage(), in both cases the
1290 * task being examined is in a frozen state so the counters won't change.
1292 * FIXME! Get the fault counts properly!
1294 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1296 struct rusage r;
1298 memset((char *) &r, 0, sizeof(r));
1299 switch (who) {
1300 case RUSAGE_SELF:
1301 jiffies_to_timeval(p->utime, &r.ru_utime);
1302 jiffies_to_timeval(p->stime, &r.ru_stime);
1303 r.ru_minflt = p->min_flt;
1304 r.ru_majflt = p->maj_flt;
1305 r.ru_nswap = p->nswap;
1306 break;
1307 case RUSAGE_CHILDREN:
1308 jiffies_to_timeval(p->cutime, &r.ru_utime);
1309 jiffies_to_timeval(p->cstime, &r.ru_stime);
1310 r.ru_minflt = p->cmin_flt;
1311 r.ru_majflt = p->cmaj_flt;
1312 r.ru_nswap = p->cnswap;
1313 break;
1314 default:
1315 jiffies_to_timeval(p->utime + p->cutime, &r.ru_utime);
1316 jiffies_to_timeval(p->stime + p->cstime, &r.ru_stime);
1317 r.ru_minflt = p->min_flt + p->cmin_flt;
1318 r.ru_majflt = p->maj_flt + p->cmaj_flt;
1319 r.ru_nswap = p->nswap + p->cnswap;
1320 break;
1322 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1325 asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
1327 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
1328 return -EINVAL;
1329 return getrusage(current, who, ru);
1332 asmlinkage long sys_umask(int mask)
1334 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1335 return mask;
1338 asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
1339 unsigned long arg4, unsigned long arg5)
1341 int error;
1342 int sig;
1344 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1345 if (error)
1346 return error;
1348 switch (option) {
1349 case PR_SET_PDEATHSIG:
1350 sig = arg2;
1351 if (sig < 0 || sig > _NSIG) {
1352 error = -EINVAL;
1353 break;
1355 current->pdeath_signal = sig;
1356 break;
1357 case PR_GET_PDEATHSIG:
1358 error = put_user(current->pdeath_signal, (int __user *)arg2);
1359 break;
1360 case PR_GET_DUMPABLE:
1361 if (current->mm->dumpable)
1362 error = 1;
1363 break;
1364 case PR_SET_DUMPABLE:
1365 if (arg2 != 0 && arg2 != 1) {
1366 error = -EINVAL;
1367 break;
1369 current->mm->dumpable = arg2;
1370 break;
1372 case PR_SET_UNALIGN:
1373 error = SET_UNALIGN_CTL(current, arg2);
1374 break;
1375 case PR_GET_UNALIGN:
1376 error = GET_UNALIGN_CTL(current, arg2);
1377 break;
1378 case PR_SET_FPEMU:
1379 error = SET_FPEMU_CTL(current, arg2);
1380 break;
1381 case PR_GET_FPEMU:
1382 error = GET_FPEMU_CTL(current, arg2);
1383 break;
1384 case PR_SET_FPEXC:
1385 error = SET_FPEXC_CTL(current, arg2);
1386 break;
1387 case PR_GET_FPEXC:
1388 error = GET_FPEXC_CTL(current, arg2);
1389 break;
1392 case PR_GET_KEEPCAPS:
1393 if (current->keep_capabilities)
1394 error = 1;
1395 break;
1396 case PR_SET_KEEPCAPS:
1397 if (arg2 != 0 && arg2 != 1) {
1398 error = -EINVAL;
1399 break;
1401 current->keep_capabilities = arg2;
1402 break;
1403 default:
1404 error = -EINVAL;
1405 break;
1407 return error;
1410 EXPORT_SYMBOL(notifier_chain_register);
1411 EXPORT_SYMBOL(notifier_chain_unregister);
1412 EXPORT_SYMBOL(notifier_call_chain);
1413 EXPORT_SYMBOL(register_reboot_notifier);
1414 EXPORT_SYMBOL(unregister_reboot_notifier);
1415 EXPORT_SYMBOL(in_group_p);
1416 EXPORT_SYMBOL(in_egroup_p);