6f2b0f170fd9c9a395f8b94743a76dc3d44924e2
[linux-2.6.git] / kernel / sys.c
1 /*
2  *  linux/kernel/sys.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
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/kmod.h>
16 #include <linux/reboot.h>
17 #include <linux/prctl.h>
18 #include <linux/init.h>
19 #include <linux/highuid.h>
20 #include <linux/fs.h>
21 #include <linux/workqueue.h>
22 #include <linux/device.h>
23 #include <linux/times.h>
24 #include <linux/security.h>
25 #include <linux/dcookies.h>
26 #include <linux/suspend.h>
27 #include <linux/ckrm.h>
28
29 #include <asm/uaccess.h>
30 #include <asm/io.h>
31 #include <asm/unistd.h>
32
33 #ifndef SET_UNALIGN_CTL
34 # define SET_UNALIGN_CTL(a,b)   (-EINVAL)
35 #endif
36 #ifndef GET_UNALIGN_CTL
37 # define GET_UNALIGN_CTL(a,b)   (-EINVAL)
38 #endif
39 #ifndef SET_FPEMU_CTL
40 # define SET_FPEMU_CTL(a,b)     (-EINVAL)
41 #endif
42 #ifndef GET_FPEMU_CTL
43 # define GET_FPEMU_CTL(a,b)     (-EINVAL)
44 #endif
45 #ifndef SET_FPEXC_CTL
46 # define SET_FPEXC_CTL(a,b)     (-EINVAL)
47 #endif
48 #ifndef GET_FPEXC_CTL
49 # define GET_FPEXC_CTL(a,b)     (-EINVAL)
50 #endif
51
52 /*
53  * this is where the system-wide overflow UID and GID are defined, for
54  * architectures that now have 32-bit UID/GID but didn't in the past
55  */
56
57 int overflowuid = DEFAULT_OVERFLOWUID;
58 int overflowgid = DEFAULT_OVERFLOWGID;
59
60 #ifdef CONFIG_UID16
61 EXPORT_SYMBOL(overflowuid);
62 EXPORT_SYMBOL(overflowgid);
63 #endif
64
65 /*
66  * the same as above, but for filesystems which can only store a 16-bit
67  * UID and GID. as such, this is needed on all architectures
68  */
69
70 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
71 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
72
73 EXPORT_SYMBOL(fs_overflowuid);
74 EXPORT_SYMBOL(fs_overflowgid);
75
76 /*
77  * this indicates whether you can reboot with ctrl-alt-del: the default is yes
78  */
79
80 int C_A_D = 1;
81 int cad_pid = 1;
82
83 /*
84  *      Notifier list for kernel code which wants to be called
85  *      at shutdown. This is used to stop any idling DMA operations
86  *      and the like. 
87  */
88
89 static struct notifier_block *reboot_notifier_list;
90 rwlock_t notifier_lock = RW_LOCK_UNLOCKED;
91
92 /**
93  *      notifier_chain_register - Add notifier to a notifier chain
94  *      @list: Pointer to root list pointer
95  *      @n: New entry in notifier chain
96  *
97  *      Adds a notifier to a notifier chain.
98  *
99  *      Currently always returns zero.
100  */
101  
102 int notifier_chain_register(struct notifier_block **list, struct notifier_block *n)
103 {
104         write_lock(&notifier_lock);
105         while(*list)
106         {
107                 if(n->priority > (*list)->priority)
108                         break;
109                 list= &((*list)->next);
110         }
111         n->next = *list;
112         *list=n;
113         write_unlock(&notifier_lock);
114         return 0;
115 }
116
117 EXPORT_SYMBOL(notifier_chain_register);
118
119 /**
120  *      notifier_chain_unregister - Remove notifier from a notifier chain
121  *      @nl: Pointer to root list pointer
122  *      @n: New entry in notifier chain
123  *
124  *      Removes a notifier from a notifier chain.
125  *
126  *      Returns zero on success, or %-ENOENT on failure.
127  */
128  
129 int notifier_chain_unregister(struct notifier_block **nl, struct notifier_block *n)
130 {
131         write_lock(&notifier_lock);
132         while((*nl)!=NULL)
133         {
134                 if((*nl)==n)
135                 {
136                         *nl=n->next;
137                         write_unlock(&notifier_lock);
138                         return 0;
139                 }
140                 nl=&((*nl)->next);
141         }
142         write_unlock(&notifier_lock);
143         return -ENOENT;
144 }
145
146 EXPORT_SYMBOL(notifier_chain_unregister);
147
148 /**
149  *      notifier_call_chain - Call functions in a notifier chain
150  *      @n: Pointer to root pointer of notifier chain
151  *      @val: Value passed unmodified to notifier function
152  *      @v: Pointer passed unmodified to notifier function
153  *
154  *      Calls each function in a notifier chain in turn.
155  *
156  *      If the return value of the notifier can be and'd
157  *      with %NOTIFY_STOP_MASK, then notifier_call_chain
158  *      will return immediately, with the return value of
159  *      the notifier function which halted execution.
160  *      Otherwise, the return value is the return value
161  *      of the last notifier function called.
162  */
163  
164 int notifier_call_chain(struct notifier_block **n, unsigned long val, void *v)
165 {
166         int ret=NOTIFY_DONE;
167         struct notifier_block *nb = *n;
168
169         while(nb)
170         {
171                 ret=nb->notifier_call(nb,val,v);
172                 if(ret&NOTIFY_STOP_MASK)
173                 {
174                         return ret;
175                 }
176                 nb=nb->next;
177         }
178         return ret;
179 }
180
181 EXPORT_SYMBOL(notifier_call_chain);
182
183 /**
184  *      register_reboot_notifier - Register function to be called at reboot time
185  *      @nb: Info about notifier function to be called
186  *
187  *      Registers a function with the list of functions
188  *      to be called at reboot time.
189  *
190  *      Currently always returns zero, as notifier_chain_register
191  *      always returns zero.
192  */
193  
194 int register_reboot_notifier(struct notifier_block * nb)
195 {
196         return notifier_chain_register(&reboot_notifier_list, nb);
197 }
198
199 EXPORT_SYMBOL(register_reboot_notifier);
200
201 /**
202  *      unregister_reboot_notifier - Unregister previously registered reboot notifier
203  *      @nb: Hook to be unregistered
204  *
205  *      Unregisters a previously registered reboot
206  *      notifier function.
207  *
208  *      Returns zero on success, or %-ENOENT on failure.
209  */
210  
211 int unregister_reboot_notifier(struct notifier_block * nb)
212 {
213         return notifier_chain_unregister(&reboot_notifier_list, nb);
214 }
215
216 EXPORT_SYMBOL(unregister_reboot_notifier);
217
218 asmlinkage long sys_ni_syscall(void)
219 {
220         return -ENOSYS;
221 }
222
223 cond_syscall(sys_nfsservctl)
224 cond_syscall(sys_quotactl)
225 cond_syscall(sys_acct)
226 cond_syscall(sys_lookup_dcookie)
227 cond_syscall(sys_swapon)
228 cond_syscall(sys_swapoff)
229 cond_syscall(sys_init_module)
230 cond_syscall(sys_delete_module)
231 cond_syscall(sys_socketpair)
232 cond_syscall(sys_bind)
233 cond_syscall(sys_listen)
234 cond_syscall(sys_accept)
235 cond_syscall(sys_connect)
236 cond_syscall(sys_getsockname)
237 cond_syscall(sys_getpeername)
238 cond_syscall(sys_sendto)
239 cond_syscall(sys_send)
240 cond_syscall(sys_recvfrom)
241 cond_syscall(sys_recv)
242 cond_syscall(sys_socket)
243 cond_syscall(sys_setsockopt)
244 cond_syscall(sys_getsockopt)
245 cond_syscall(sys_shutdown)
246 cond_syscall(sys_sendmsg)
247 cond_syscall(sys_recvmsg)
248 cond_syscall(sys_socketcall)
249 cond_syscall(sys_futex)
250 cond_syscall(compat_sys_futex)
251 cond_syscall(sys_epoll_create)
252 cond_syscall(sys_epoll_ctl)
253 cond_syscall(sys_epoll_wait)
254 cond_syscall(sys_semget)
255 cond_syscall(sys_semop)
256 cond_syscall(sys_semtimedop)
257 cond_syscall(sys_semctl)
258 cond_syscall(sys_msgget)
259 cond_syscall(sys_msgsnd)
260 cond_syscall(sys_msgrcv)
261 cond_syscall(sys_msgctl)
262 cond_syscall(sys_shmget)
263 cond_syscall(sys_shmdt)
264 cond_syscall(sys_shmctl)
265 cond_syscall(sys_mq_open)
266 cond_syscall(sys_mq_unlink)
267 cond_syscall(sys_mq_timedsend)
268 cond_syscall(sys_mq_timedreceive)
269 cond_syscall(sys_mq_notify)
270 cond_syscall(sys_mq_getsetattr)
271 cond_syscall(compat_sys_mq_open)
272 cond_syscall(compat_sys_mq_timedsend)
273 cond_syscall(compat_sys_mq_timedreceive)
274 cond_syscall(compat_sys_mq_notify)
275 cond_syscall(compat_sys_mq_getsetattr)
276 cond_syscall(sys_mbind)
277 cond_syscall(sys_get_mempolicy)
278 cond_syscall(sys_set_mempolicy)
279 cond_syscall(compat_get_mempolicy)
280
281 /* arch-specific weak syscall entries */
282 cond_syscall(sys_pciconfig_read)
283 cond_syscall(sys_pciconfig_write)
284 cond_syscall(sys_pciconfig_iobase)
285
286 static int set_one_prio(struct task_struct *p, int niceval, int error)
287 {
288         int no_nice;
289
290         if (p->uid != current->euid &&
291                 p->uid != current->uid && !capable(CAP_SYS_NICE)) {
292                 error = -EPERM;
293                 goto out;
294         }
295         if (niceval < task_nice(p) && !capable(CAP_SYS_NICE)) {
296                 error = -EACCES;
297                 goto out;
298         }
299         no_nice = security_task_setnice(p, niceval);
300         if (no_nice) {
301                 error = no_nice;
302                 goto out;
303         }
304         if (error == -ESRCH)
305                 error = 0;
306         set_user_nice(p, niceval);
307 out:
308         return error;
309 }
310
311 asmlinkage long sys_setpriority(int which, int who, int niceval)
312 {
313         struct task_struct *g, *p;
314         struct user_struct *user;
315         struct pid *pid;
316         struct list_head *l;
317         int error = -EINVAL;
318
319         if (which > 2 || which < 0)
320                 goto out;
321
322         /* normalize: avoid signed division (rounding problems) */
323         error = -ESRCH;
324         if (niceval < -20)
325                 niceval = -20;
326         if (niceval > 19)
327                 niceval = 19;
328
329         read_lock(&tasklist_lock);
330         switch (which) {
331                 case PRIO_PROCESS:
332                         if (!who)
333                                 who = current->pid;
334                         p = find_task_by_pid(who);
335                         if (p)
336                                 error = set_one_prio(p, niceval, error);
337                         break;
338                 case PRIO_PGRP:
339                         if (!who)
340                                 who = process_group(current);
341                         for_each_task_pid(who, PIDTYPE_PGID, p, l, pid)
342                                 error = set_one_prio(p, niceval, error);
343                         break;
344                 case PRIO_USER:
345                         if (!who)
346                                 user = current->user;
347                         else
348                                 user = find_user(vx_current_xid(), who);
349
350                         if (!user)
351                                 goto out_unlock;
352
353                         do_each_thread(g, p)
354                                 if (p->uid == who)
355                                         error = set_one_prio(p, niceval, error);
356                         while_each_thread(g, p);
357                         if (who)
358                                 free_uid(user);         /* For find_user() */
359                         break;
360         }
361 out_unlock:
362         read_unlock(&tasklist_lock);
363 out:
364         return error;
365 }
366
367 /*
368  * Ugh. To avoid negative return values, "getpriority()" will
369  * not return the normal nice-value, but a negated value that
370  * has been offset by 20 (ie it returns 40..1 instead of -20..19)
371  * to stay compatible.
372  */
373 asmlinkage long sys_getpriority(int which, int who)
374 {
375         struct task_struct *g, *p;
376         struct list_head *l;
377         struct pid *pid;
378         struct user_struct *user;
379         long niceval, retval = -ESRCH;
380
381         if (which > 2 || which < 0)
382                 return -EINVAL;
383
384         read_lock(&tasklist_lock);
385         switch (which) {
386                 case PRIO_PROCESS:
387                         if (!who)
388                                 who = current->pid;
389                         p = find_task_by_pid(who);
390                         if (p) {
391                                 niceval = 20 - task_nice(p);
392                                 if (niceval > retval)
393                                         retval = niceval;
394                         }
395                         break;
396                 case PRIO_PGRP:
397                         if (!who)
398                                 who = process_group(current);
399                         for_each_task_pid(who, PIDTYPE_PGID, p, l, pid) {
400                                 niceval = 20 - task_nice(p);
401                                 if (niceval > retval)
402                                         retval = niceval;
403                         }
404                         break;
405                 case PRIO_USER:
406                         if (!who)
407                                 user = current->user;
408                         else
409                                 user = find_user(vx_current_xid(), who);
410
411                         if (!user)
412                                 goto out_unlock;
413
414                         do_each_thread(g, p)
415                                 if (p->uid == who) {
416                                         niceval = 20 - task_nice(p);
417                                         if (niceval > retval)
418                                                 retval = niceval;
419                                 }
420                         while_each_thread(g, p);
421                         if (who)
422                                 free_uid(user);         /* for find_user() */
423                         break;
424         }
425 out_unlock:
426         read_unlock(&tasklist_lock);
427
428         return retval;
429 }
430
431 /*
432  *      vshelper path is set via /proc/sys
433  *      invoked by vserver sys_reboot(), with
434  *      the following arguments
435  *
436  *      argv [0] = vshelper_path;
437  *      argv [1] = action: "restart", "halt", "poweroff", ...
438  *      argv [2] = context identifier
439  *      argv [3] = additional argument (restart2)
440  *
441  *      envp [*] = type-specific parameters
442  */
443 char vshelper_path[255] = "/sbin/vshelper";
444
445 long vs_reboot(unsigned int cmd, void * arg)
446 {
447         char id_buf[8], cmd_buf[32];
448         char uid_buf[32], pid_buf[32];
449         char buffer[256];
450
451         char *argv[] = {vshelper_path, NULL, id_buf, NULL, 0};
452         char *envp[] = {"HOME=/", "TERM=linux",
453                         "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
454                         uid_buf, pid_buf, cmd_buf, 0};
455
456         snprintf(id_buf, sizeof(id_buf)-1, "%d", vx_current_xid());
457
458         snprintf(cmd_buf, sizeof(cmd_buf)-1, "VS_CMD=%08x", cmd);
459         snprintf(uid_buf, sizeof(uid_buf)-1, "VS_UID=%d", current->uid);
460         snprintf(pid_buf, sizeof(pid_buf)-1, "VS_PID=%d", current->pid);
461
462         switch (cmd) {
463         case LINUX_REBOOT_CMD_RESTART:
464                 argv[1] = "restart";
465                 break;  
466
467         case LINUX_REBOOT_CMD_HALT:
468                 argv[1] = "halt";
469                 break;  
470
471         case LINUX_REBOOT_CMD_POWER_OFF:
472                 argv[1] = "poweroff";
473                 break;  
474
475         case LINUX_REBOOT_CMD_SW_SUSPEND:
476                 argv[1] = "swsusp";
477                 break;  
478
479         case LINUX_REBOOT_CMD_RESTART2:
480                 if (strncpy_from_user(&buffer[0], (char *)arg, sizeof(buffer) - 1) < 0)
481                         return -EFAULT;
482                 argv[3] = buffer;
483         default:
484                 argv[1] = "restart2";
485                 break;  
486         }
487
488         /* maybe we should wait ? */
489         if (call_usermodehelper(*argv, argv, envp, 0)) {
490                 printk( KERN_WARNING
491                         "vs_reboot(): failed to exec (%s %s %s %s)\n",
492                         vshelper_path, argv[1], argv[2], argv[3]);
493                 return -EPERM;
494         }
495         return 0;
496 }
497
498 /*
499  * Reboot system call: for obvious reasons only root may call it,
500  * and even root needs to set up some magic numbers in the registers
501  * so that some mistake won't make this reboot the whole machine.
502  * You can also set the meaning of the ctrl-alt-del-key here.
503  *
504  * reboot doesn't sync: do that yourself before calling this.
505  */
506 asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
507 {
508         char buffer[256];
509
510         /* We only trust the superuser with rebooting the system. */
511         if (!capable(CAP_SYS_BOOT))
512                 return -EPERM;
513
514         /* For safety, we require "magic" arguments. */
515         if (magic1 != LINUX_REBOOT_MAGIC1 ||
516             (magic2 != LINUX_REBOOT_MAGIC2 &&
517                         magic2 != LINUX_REBOOT_MAGIC2A &&
518                         magic2 != LINUX_REBOOT_MAGIC2B &&
519                         magic2 != LINUX_REBOOT_MAGIC2C))
520                 return -EINVAL;
521
522         if (!vx_check(0, VX_ADMIN|VX_WATCH))
523                 return vs_reboot(cmd, arg);
524
525         lock_kernel();
526         switch (cmd) {
527         case LINUX_REBOOT_CMD_RESTART:
528                 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, NULL);
529                 system_state = SYSTEM_RESTART;
530                 device_shutdown();
531                 printk(KERN_EMERG "Restarting system.\n");
532                 machine_restart(NULL);
533                 break;
534
535         case LINUX_REBOOT_CMD_CAD_ON:
536                 C_A_D = 1;
537                 break;
538
539         case LINUX_REBOOT_CMD_CAD_OFF:
540                 C_A_D = 0;
541                 break;
542
543         case LINUX_REBOOT_CMD_HALT:
544                 notifier_call_chain(&reboot_notifier_list, SYS_HALT, NULL);
545                 system_state = SYSTEM_HALT;
546                 device_shutdown();
547                 printk(KERN_EMERG "System halted.\n");
548                 machine_halt();
549                 unlock_kernel();
550                 do_exit(0);
551                 break;
552
553         case LINUX_REBOOT_CMD_POWER_OFF:
554                 notifier_call_chain(&reboot_notifier_list, SYS_POWER_OFF, NULL);
555                 system_state = SYSTEM_POWER_OFF;
556                 device_shutdown();
557                 printk(KERN_EMERG "Power down.\n");
558                 machine_power_off();
559                 unlock_kernel();
560                 do_exit(0);
561                 break;
562
563         case LINUX_REBOOT_CMD_RESTART2:
564                 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
565                         unlock_kernel();
566                         return -EFAULT;
567                 }
568                 buffer[sizeof(buffer) - 1] = '\0';
569
570                 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, buffer);
571                 system_state = SYSTEM_RESTART;
572                 device_shutdown();
573                 printk(KERN_EMERG "Restarting system with command '%s'.\n", buffer);
574                 machine_restart(buffer);
575                 break;
576
577 #ifdef CONFIG_SOFTWARE_SUSPEND
578         case LINUX_REBOOT_CMD_SW_SUSPEND:
579                 {
580                         int ret = software_suspend();
581                         unlock_kernel();
582                         return ret;
583                 }
584 #endif
585
586         default:
587                 unlock_kernel();
588                 return -EINVAL;
589         }
590         unlock_kernel();
591         return 0;
592 }
593
594 static void deferred_cad(void *dummy)
595 {
596         notifier_call_chain(&reboot_notifier_list, SYS_RESTART, NULL);
597         machine_restart(NULL);
598 }
599
600 /*
601  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
602  * As it's called within an interrupt, it may NOT sync: the only choice
603  * is whether to reboot at once, or just ignore the ctrl-alt-del.
604  */
605 void ctrl_alt_del(void)
606 {
607         static DECLARE_WORK(cad_work, deferred_cad, NULL);
608
609         if (C_A_D)
610                 schedule_work(&cad_work);
611         else
612                 kill_proc(cad_pid, SIGINT, 1);
613 }
614         
615
616 /*
617  * Unprivileged users may change the real gid to the effective gid
618  * or vice versa.  (BSD-style)
619  *
620  * If you set the real gid at all, or set the effective gid to a value not
621  * equal to the real gid, then the saved gid is set to the new effective gid.
622  *
623  * This makes it possible for a setgid program to completely drop its
624  * privileges, which is often a useful assertion to make when you are doing
625  * a security audit over a program.
626  *
627  * The general idea is that a program which uses just setregid() will be
628  * 100% compatible with BSD.  A program which uses just setgid() will be
629  * 100% compatible with POSIX with saved IDs. 
630  *
631  * SMP: There are not races, the GIDs are checked only by filesystem
632  *      operations (as far as semantic preservation is concerned).
633  */
634 asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
635 {
636         int old_rgid = current->gid;
637         int old_egid = current->egid;
638         int new_rgid = old_rgid;
639         int new_egid = old_egid;
640         int retval;
641
642         retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
643         if (retval)
644                 return retval;
645
646         if (rgid != (gid_t) -1) {
647                 if ((old_rgid == rgid) ||
648                     (current->egid==rgid) ||
649                     capable(CAP_SETGID))
650                         new_rgid = rgid;
651                 else
652                         return -EPERM;
653         }
654         if (egid != (gid_t) -1) {
655                 if ((old_rgid == egid) ||
656                     (current->egid == egid) ||
657                     (current->sgid == egid) ||
658                     capable(CAP_SETGID))
659                         new_egid = egid;
660                 else {
661                         return -EPERM;
662                 }
663         }
664         if (new_egid != old_egid)
665         {
666                 current->mm->dumpable = 0;
667                 wmb();
668         }
669         if (rgid != (gid_t) -1 ||
670             (egid != (gid_t) -1 && egid != old_rgid))
671                 current->sgid = new_egid;
672         current->fsgid = new_egid;
673         current->egid = new_egid;
674         current->gid = new_rgid;
675
676         ckrm_cb_gid();
677
678         return 0;
679 }
680
681 /*
682  * setgid() is implemented like SysV w/ SAVED_IDS 
683  *
684  * SMP: Same implicit races as above.
685  */
686 asmlinkage long sys_setgid(gid_t gid)
687 {
688         int old_egid = current->egid;
689         int retval;
690
691         retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
692         if (retval)
693                 return retval;
694
695         if (capable(CAP_SETGID))
696         {
697                 if(old_egid != gid)
698                 {
699                         current->mm->dumpable=0;
700                         wmb();
701                 }
702                 current->gid = current->egid = current->sgid = current->fsgid = gid;
703         }
704         else if ((gid == current->gid) || (gid == current->sgid))
705         {
706                 if(old_egid != gid)
707                 {
708                         current->mm->dumpable=0;
709                         wmb();
710                 }
711                 current->egid = current->fsgid = gid;
712         }
713         else
714                 return -EPERM;
715
716         ckrm_cb_gid();
717
718         return 0;
719 }
720   
721 static int set_user(uid_t new_ruid, int dumpclear)
722 {
723         struct user_struct *new_user;
724
725         new_user = alloc_uid(vx_current_xid(), new_ruid);
726         if (!new_user)
727                 return -EAGAIN;
728
729         if (atomic_read(&new_user->processes) >=
730                                 current->rlim[RLIMIT_NPROC].rlim_cur &&
731                         new_user != &root_user) {
732                 free_uid(new_user);
733                 return -EAGAIN;
734         }
735
736         switch_uid(new_user);
737
738         if(dumpclear)
739         {
740                 current->mm->dumpable = 0;
741                 wmb();
742         }
743         current->uid = new_ruid;
744         return 0;
745 }
746
747 /*
748  * Unprivileged users may change the real uid to the effective uid
749  * or vice versa.  (BSD-style)
750  *
751  * If you set the real uid at all, or set the effective uid to a value not
752  * equal to the real uid, then the saved uid is set to the new effective uid.
753  *
754  * This makes it possible for a setuid program to completely drop its
755  * privileges, which is often a useful assertion to make when you are doing
756  * a security audit over a program.
757  *
758  * The general idea is that a program which uses just setreuid() will be
759  * 100% compatible with BSD.  A program which uses just setuid() will be
760  * 100% compatible with POSIX with saved IDs. 
761  */
762 asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
763 {
764         int old_ruid, old_euid, old_suid, new_ruid, new_euid;
765         int retval;
766
767         retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
768         if (retval)
769                 return retval;
770
771         new_ruid = old_ruid = current->uid;
772         new_euid = old_euid = current->euid;
773         old_suid = current->suid;
774
775         if (ruid != (uid_t) -1) {
776                 new_ruid = ruid;
777                 if ((old_ruid != ruid) &&
778                     (current->euid != ruid) &&
779                     !capable(CAP_SETUID))
780                         return -EPERM;
781         }
782
783         if (euid != (uid_t) -1) {
784                 new_euid = euid;
785                 if ((old_ruid != euid) &&
786                     (current->euid != euid) &&
787                     (current->suid != euid) &&
788                     !capable(CAP_SETUID))
789                         return -EPERM;
790         }
791
792         if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
793                 return -EAGAIN;
794
795         if (new_euid != old_euid)
796         {
797                 current->mm->dumpable=0;
798                 wmb();
799         }
800         current->fsuid = current->euid = new_euid;
801         if (ruid != (uid_t) -1 ||
802             (euid != (uid_t) -1 && euid != old_ruid))
803                 current->suid = current->euid;
804         current->fsuid = current->euid;
805
806         ckrm_cb_uid();
807
808         return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
809 }
810
811
812                 
813 /*
814  * setuid() is implemented like SysV with SAVED_IDS 
815  * 
816  * Note that SAVED_ID's is deficient in that a setuid root program
817  * like sendmail, for example, cannot set its uid to be a normal 
818  * user and then switch back, because if you're root, setuid() sets
819  * the saved uid too.  If you don't like this, blame the bright people
820  * in the POSIX committee and/or USG.  Note that the BSD-style setreuid()
821  * will allow a root program to temporarily drop privileges and be able to
822  * regain them by swapping the real and effective uid.  
823  */
824 asmlinkage long sys_setuid(uid_t uid)
825 {
826         int old_euid = current->euid;
827         int old_ruid, old_suid, new_ruid, new_suid;
828         int retval;
829
830         retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
831         if (retval)
832                 return retval;
833
834         old_ruid = new_ruid = current->uid;
835         old_suid = current->suid;
836         new_suid = old_suid;
837         
838         if (capable(CAP_SETUID)) {
839                 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
840                         return -EAGAIN;
841                 new_suid = uid;
842         } else if ((uid != current->uid) && (uid != new_suid))
843                 return -EPERM;
844
845         if (old_euid != uid)
846         {
847                 current->mm->dumpable = 0;
848                 wmb();
849         }
850         current->fsuid = current->euid = uid;
851         current->suid = new_suid;
852
853         ckrm_cb_uid();
854
855         return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
856 }
857
858
859 /*
860  * This function implements a generic ability to update ruid, euid,
861  * and suid.  This allows you to implement the 4.4 compatible seteuid().
862  */
863 asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
864 {
865         int old_ruid = current->uid;
866         int old_euid = current->euid;
867         int old_suid = current->suid;
868         int retval;
869
870         retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
871         if (retval)
872                 return retval;
873
874         if (!capable(CAP_SETUID)) {
875                 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
876                     (ruid != current->euid) && (ruid != current->suid))
877                         return -EPERM;
878                 if ((euid != (uid_t) -1) && (euid != current->uid) &&
879                     (euid != current->euid) && (euid != current->suid))
880                         return -EPERM;
881                 if ((suid != (uid_t) -1) && (suid != current->uid) &&
882                     (suid != current->euid) && (suid != current->suid))
883                         return -EPERM;
884         }
885         if (ruid != (uid_t) -1) {
886                 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
887                         return -EAGAIN;
888         }
889         if (euid != (uid_t) -1) {
890                 if (euid != current->euid)
891                 {
892                         current->mm->dumpable = 0;
893                         wmb();
894                 }
895                 current->euid = euid;
896         }
897         current->fsuid = current->euid;
898         if (suid != (uid_t) -1)
899                 current->suid = suid;
900
901         ckrm_cb_uid();
902
903         return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
904 }
905
906 asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
907 {
908         int retval;
909
910         if (!(retval = put_user(current->uid, ruid)) &&
911             !(retval = put_user(current->euid, euid)))
912                 retval = put_user(current->suid, suid);
913
914         return retval;
915 }
916
917 /*
918  * Same as above, but for rgid, egid, sgid.
919  */
920 asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
921 {
922         int retval;
923
924         retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
925         if (retval)
926                 return retval;
927
928         if (!capable(CAP_SETGID)) {
929                 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
930                     (rgid != current->egid) && (rgid != current->sgid))
931                         return -EPERM;
932                 if ((egid != (gid_t) -1) && (egid != current->gid) &&
933                     (egid != current->egid) && (egid != current->sgid))
934                         return -EPERM;
935                 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
936                     (sgid != current->egid) && (sgid != current->sgid))
937                         return -EPERM;
938         }
939         if (egid != (gid_t) -1) {
940                 if (egid != current->egid)
941                 {
942                         current->mm->dumpable = 0;
943                         wmb();
944                 }
945                 current->egid = egid;
946         }
947         current->fsgid = current->egid;
948         if (rgid != (gid_t) -1)
949                 current->gid = rgid;
950         if (sgid != (gid_t) -1)
951                 current->sgid = sgid;
952
953         ckrm_cb_gid();
954
955         return 0;
956 }
957
958 asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
959 {
960         int retval;
961
962         if (!(retval = put_user(current->gid, rgid)) &&
963             !(retval = put_user(current->egid, egid)))
964                 retval = put_user(current->sgid, sgid);
965
966         return retval;
967 }
968
969
970 /*
971  * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
972  * is used for "access()" and for the NFS daemon (letting nfsd stay at
973  * whatever uid it wants to). It normally shadows "euid", except when
974  * explicitly set by setfsuid() or for access..
975  */
976 asmlinkage long sys_setfsuid(uid_t uid)
977 {
978         int old_fsuid;
979
980         old_fsuid = current->fsuid;
981         if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
982                 return old_fsuid;
983
984         if (uid == current->uid || uid == current->euid ||
985             uid == current->suid || uid == current->fsuid || 
986             capable(CAP_SETUID))
987         {
988                 if (uid != old_fsuid)
989                 {
990                         current->mm->dumpable = 0;
991                         wmb();
992                 }
993                 current->fsuid = uid;
994         }
995
996         security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
997
998         return old_fsuid;
999 }
1000
1001 /*
1002  * Samma pÃ¥ svenska..
1003  */
1004 asmlinkage long sys_setfsgid(gid_t gid)
1005 {
1006         int old_fsgid;
1007
1008         old_fsgid = current->fsgid;
1009         if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
1010                 return old_fsgid;
1011
1012         if (gid == current->gid || gid == current->egid ||
1013             gid == current->sgid || gid == current->fsgid || 
1014             capable(CAP_SETGID))
1015         {
1016                 if (gid != old_fsgid)
1017                 {
1018                         current->mm->dumpable = 0;
1019                         wmb();
1020                 }
1021                 current->fsgid = gid;
1022         }
1023         return old_fsgid;
1024 }
1025
1026 asmlinkage long sys_times(struct tms __user * tbuf)
1027 {
1028         /*
1029          *      In the SMP world we might just be unlucky and have one of
1030          *      the times increment as we use it. Since the value is an
1031          *      atomically safe type this is just fine. Conceptually its
1032          *      as if the syscall took an instant longer to occur.
1033          */
1034         if (tbuf) {
1035                 struct tms tmp;
1036                 tmp.tms_utime = jiffies_to_clock_t(current->utime);
1037                 tmp.tms_stime = jiffies_to_clock_t(current->stime);
1038                 tmp.tms_cutime = jiffies_to_clock_t(current->cutime);
1039                 tmp.tms_cstime = jiffies_to_clock_t(current->cstime);
1040                 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1041                         return -EFAULT;
1042         }
1043         return (long) jiffies_64_to_clock_t(get_jiffies_64());
1044 }
1045
1046 /*
1047  * This needs some heavy checking ...
1048  * I just haven't the stomach for it. I also don't fully
1049  * understand sessions/pgrp etc. Let somebody who does explain it.
1050  *
1051  * OK, I think I have the protection semantics right.... this is really
1052  * only important on a multi-user system anyway, to make sure one user
1053  * can't send a signal to a process owned by another.  -TYT, 12/12/91
1054  *
1055  * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1056  * LBT 04.03.94
1057  */
1058
1059 asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
1060 {
1061         struct task_struct *p;
1062         int err = -EINVAL;
1063
1064         if (!pid)
1065                 pid = current->pid;
1066         if (!pgid)
1067                 pgid = pid;
1068         if (pgid < 0)
1069                 return -EINVAL;
1070
1071         /* From this point forward we keep holding onto the tasklist lock
1072          * so that our parent does not change from under us. -DaveM
1073          */
1074         write_lock_irq(&tasklist_lock);
1075
1076         err = -ESRCH;
1077         p = find_task_by_pid(pid);
1078         if (!p)
1079                 goto out;
1080
1081         err = -EINVAL;
1082         if (!thread_group_leader(p))
1083                 goto out;
1084
1085         if (p->parent == current || p->real_parent == current) {
1086                 err = -EPERM;
1087                 if (p->signal->session != current->signal->session)
1088                         goto out;
1089                 err = -EACCES;
1090                 if (p->did_exec)
1091                         goto out;
1092         } else {
1093                 err = -ESRCH;
1094                 if (p != current)
1095                         goto out;
1096         }
1097
1098         err = -EPERM;
1099         if (p->signal->leader)
1100                 goto out;
1101
1102         if (pgid != pid) {
1103                 struct task_struct *p;
1104                 struct pid *pid;
1105                 struct list_head *l;
1106
1107                 for_each_task_pid(pgid, PIDTYPE_PGID, p, l, pid)
1108                         if (p->signal->session == current->signal->session)
1109                                 goto ok_pgid;
1110                 goto out;
1111         }
1112
1113 ok_pgid:
1114         err = security_task_setpgid(p, pgid);
1115         if (err)
1116                 goto out;
1117
1118         if (process_group(p) != pgid) {
1119                 detach_pid(p, PIDTYPE_PGID);
1120                 p->signal->pgrp = pgid;
1121                 attach_pid(p, PIDTYPE_PGID, pgid);
1122         }
1123
1124         err = 0;
1125 out:
1126         /* All paths lead to here, thus we are safe. -DaveM */
1127         write_unlock_irq(&tasklist_lock);
1128         return err;
1129 }
1130
1131 asmlinkage long sys_getpgid(pid_t pid)
1132 {
1133         if (!pid) {
1134                 return process_group(current);
1135         } else {
1136                 int retval;
1137                 struct task_struct *p;
1138
1139                 read_lock(&tasklist_lock);
1140                 p = find_task_by_pid(pid);
1141
1142                 retval = -ESRCH;
1143                 if (p) {
1144                         retval = security_task_getpgid(p);
1145                         if (!retval)
1146                                 retval = process_group(p);
1147                 }
1148                 read_unlock(&tasklist_lock);
1149                 return retval;
1150         }
1151 }
1152
1153 #ifdef __ARCH_WANT_SYS_GETPGRP
1154
1155 asmlinkage long sys_getpgrp(void)
1156 {
1157         /* SMP - assuming writes are word atomic this is fine */
1158         return process_group(current);
1159 }
1160
1161 #endif
1162
1163 asmlinkage long sys_getsid(pid_t pid)
1164 {
1165         if (!pid) {
1166                 return current->signal->session;
1167         } else {
1168                 int retval;
1169                 struct task_struct *p;
1170
1171                 read_lock(&tasklist_lock);
1172                 p = find_task_by_pid(pid);
1173
1174                 retval = -ESRCH;
1175                 if(p) {
1176                         retval = security_task_getsid(p);
1177                         if (!retval)
1178                                 retval = p->signal->session;
1179                 }
1180                 read_unlock(&tasklist_lock);
1181                 return retval;
1182         }
1183 }
1184
1185 asmlinkage long sys_setsid(void)
1186 {
1187         struct pid *pid;
1188         int err = -EPERM;
1189
1190         if (!thread_group_leader(current))
1191                 return -EINVAL;
1192
1193         write_lock_irq(&tasklist_lock);
1194
1195         pid = find_pid(PIDTYPE_PGID, current->pid);
1196         if (pid)
1197                 goto out;
1198
1199         current->signal->leader = 1;
1200         __set_special_pids(current->pid, current->pid);
1201         current->signal->tty = NULL;
1202         current->signal->tty_old_pgrp = 0;
1203         err = process_group(current);
1204 out:
1205         write_unlock_irq(&tasklist_lock);
1206         return err;
1207 }
1208
1209 /*
1210  * Supplementary group IDs
1211  */
1212
1213 /* init to 2 - one for init_task, one to ensure it is never freed */
1214 struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1215
1216 struct group_info *groups_alloc(int gidsetsize)
1217 {
1218         struct group_info *group_info;
1219         int nblocks;
1220         int i;
1221
1222         nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1223         /* Make sure we always allocate at least one indirect block pointer */
1224         nblocks = nblocks ? : 1;
1225         group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1226         if (!group_info)
1227                 return NULL;
1228         group_info->ngroups = gidsetsize;
1229         group_info->nblocks = nblocks;
1230         atomic_set(&group_info->usage, 1);
1231
1232         if (gidsetsize <= NGROUPS_SMALL) {
1233                 group_info->blocks[0] = group_info->small_block;
1234         } else {
1235                 for (i = 0; i < nblocks; i++) {
1236                         gid_t *b;
1237                         b = (void *)__get_free_page(GFP_USER);
1238                         if (!b)
1239                                 goto out_undo_partial_alloc;
1240                         group_info->blocks[i] = b;
1241                 }
1242         }
1243         return group_info;
1244
1245 out_undo_partial_alloc:
1246         while (--i >= 0) {
1247                 free_page((unsigned long)group_info->blocks[i]);
1248         }
1249         kfree(group_info);
1250         return NULL;
1251 }
1252
1253 EXPORT_SYMBOL(groups_alloc);
1254
1255 void groups_free(struct group_info *group_info)
1256 {
1257         if (group_info->blocks[0] != group_info->small_block) {
1258                 int i;
1259                 for (i = 0; i < group_info->nblocks; i++)
1260                         free_page((unsigned long)group_info->blocks[i]);
1261         }
1262         kfree(group_info);
1263 }
1264
1265 EXPORT_SYMBOL(groups_free);
1266
1267 /* export the group_info to a user-space array */
1268 static int groups_to_user(gid_t __user *grouplist,
1269     struct group_info *group_info)
1270 {
1271         int i;
1272         int count = group_info->ngroups;
1273
1274         for (i = 0; i < group_info->nblocks; i++) {
1275                 int cp_count = min(NGROUPS_PER_BLOCK, count);
1276                 int off = i * NGROUPS_PER_BLOCK;
1277                 int len = cp_count * sizeof(*grouplist);
1278
1279                 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1280                         return -EFAULT;
1281
1282                 count -= cp_count;
1283         }
1284         return 0;
1285 }
1286
1287 /* fill a group_info from a user-space array - it must be allocated already */
1288 static int groups_from_user(struct group_info *group_info,
1289     gid_t __user *grouplist)
1290  {
1291         int i;
1292         int count = group_info->ngroups;
1293
1294         for (i = 0; i < group_info->nblocks; i++) {
1295                 int cp_count = min(NGROUPS_PER_BLOCK, count);
1296                 int off = i * NGROUPS_PER_BLOCK;
1297                 int len = cp_count * sizeof(*grouplist);
1298
1299                 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1300                         return -EFAULT;
1301
1302                 count -= cp_count;
1303         }
1304         return 0;
1305 }
1306
1307 /* a simple shell-metzner sort */
1308 static void groups_sort(struct group_info *group_info)
1309 {
1310         int base, max, stride;
1311         int gidsetsize = group_info->ngroups;
1312
1313         for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1314                 ; /* nothing */
1315         stride /= 3;
1316
1317         while (stride) {
1318                 max = gidsetsize - stride;
1319                 for (base = 0; base < max; base++) {
1320                         int left = base;
1321                         int right = left + stride;
1322                         gid_t tmp = GROUP_AT(group_info, right);
1323
1324                         while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1325                                 GROUP_AT(group_info, right) =
1326                                     GROUP_AT(group_info, left);
1327                                 right = left;
1328                                 left -= stride;
1329                         }
1330                         GROUP_AT(group_info, right) = tmp;
1331                 }
1332                 stride /= 3;
1333         }
1334 }
1335
1336 /* a simple bsearch */
1337 static int groups_search(struct group_info *group_info, gid_t grp)
1338 {
1339         int left, right;
1340
1341         if (!group_info)
1342                 return 0;
1343
1344         left = 0;
1345         right = group_info->ngroups;
1346         while (left < right) {
1347                 int mid = (left+right)/2;
1348                 int cmp = grp - GROUP_AT(group_info, mid);
1349                 if (cmp > 0)
1350                         left = mid + 1;
1351                 else if (cmp < 0)
1352                         right = mid;
1353                 else
1354                         return 1;
1355         }
1356         return 0;
1357 }
1358
1359 /* validate and set current->group_info */
1360 int set_current_groups(struct group_info *group_info)
1361 {
1362         int retval;
1363         struct group_info *old_info;
1364
1365         retval = security_task_setgroups(group_info);
1366         if (retval)
1367                 return retval;
1368
1369         groups_sort(group_info);
1370         get_group_info(group_info);
1371
1372         task_lock(current);
1373         old_info = current->group_info;
1374         current->group_info = group_info;
1375         task_unlock(current);
1376
1377         put_group_info(old_info);
1378
1379         return 0;
1380 }
1381
1382 EXPORT_SYMBOL(set_current_groups);
1383
1384 asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1385 {
1386         int i = 0;
1387
1388         /*
1389          *      SMP: Nobody else can change our grouplist. Thus we are
1390          *      safe.
1391          */
1392
1393         if (gidsetsize < 0)
1394                 return -EINVAL;
1395
1396         /* no need to grab task_lock here; it cannot change */
1397         get_group_info(current->group_info);
1398         i = current->group_info->ngroups;
1399         if (gidsetsize) {
1400                 if (i > gidsetsize) {
1401                         i = -EINVAL;
1402                         goto out;
1403                 }
1404                 if (groups_to_user(grouplist, current->group_info)) {
1405                         i = -EFAULT;
1406                         goto out;
1407                 }
1408         }
1409 out:
1410         put_group_info(current->group_info);
1411         return i;
1412 }
1413
1414 /*
1415  *      SMP: Our groups are copy-on-write. We can set them safely
1416  *      without another task interfering.
1417  */
1418  
1419 asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1420 {
1421         struct group_info *group_info;
1422         int retval;
1423
1424         if (!capable(CAP_SETGID))
1425                 return -EPERM;
1426         if ((unsigned)gidsetsize > NGROUPS_MAX)
1427                 return -EINVAL;
1428
1429         group_info = groups_alloc(gidsetsize);
1430         if (!group_info)
1431                 return -ENOMEM;
1432         retval = groups_from_user(group_info, grouplist);
1433         if (retval) {
1434                 put_group_info(group_info);
1435                 return retval;
1436         }
1437
1438         retval = set_current_groups(group_info);
1439         put_group_info(group_info);
1440
1441         return retval;
1442 }
1443
1444 /*
1445  * Check whether we're fsgid/egid or in the supplemental group..
1446  */
1447 int in_group_p(gid_t grp)
1448 {
1449         int retval = 1;
1450         if (grp != current->fsgid) {
1451                 get_group_info(current->group_info);
1452                 retval = groups_search(current->group_info, grp);
1453                 put_group_info(current->group_info);
1454         }
1455         return retval;
1456 }
1457
1458 EXPORT_SYMBOL(in_group_p);
1459
1460 int in_egroup_p(gid_t grp)
1461 {
1462         int retval = 1;
1463         if (grp != current->egid) {
1464                 get_group_info(current->group_info);
1465                 retval = groups_search(current->group_info, grp);
1466                 put_group_info(current->group_info);
1467         }
1468         return retval;
1469 }
1470
1471 EXPORT_SYMBOL(in_egroup_p);
1472
1473 DECLARE_RWSEM(uts_sem);
1474
1475 EXPORT_SYMBOL(uts_sem);
1476
1477 asmlinkage long sys_newuname(struct new_utsname __user * name)
1478 {
1479         int errno = 0;
1480
1481         down_read(&uts_sem);
1482         if (copy_to_user(name, vx_new_utsname(), sizeof *name))
1483                 errno = -EFAULT;
1484         up_read(&uts_sem);
1485         return errno;
1486 }
1487
1488 asmlinkage long sys_sethostname(char __user *name, int len)
1489 {
1490         int errno;
1491         char tmp[__NEW_UTS_LEN];
1492
1493         if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SET_UTSNAME))
1494                 return -EPERM;
1495         if (len < 0 || len > __NEW_UTS_LEN)
1496                 return -EINVAL;
1497         down_write(&uts_sem);
1498         errno = -EFAULT;
1499         if (!copy_from_user(tmp, name, len)) {
1500                 char *ptr = vx_new_uts(nodename);
1501
1502                 memcpy(ptr, tmp, len);
1503                 ptr[len] = 0;
1504                 errno = 0;
1505         }
1506         up_write(&uts_sem);
1507         return errno;
1508 }
1509
1510 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1511
1512 asmlinkage long sys_gethostname(char __user *name, int len)
1513 {
1514         int i, errno;
1515         char *ptr;
1516
1517         if (len < 0)
1518                 return -EINVAL;
1519         down_read(&uts_sem);
1520         ptr = vx_new_uts(nodename);
1521         i = 1 + strlen(ptr);
1522         if (i > len)
1523                 i = len;
1524         errno = 0;
1525         if (copy_to_user(name, ptr, i))
1526                 errno = -EFAULT;
1527         up_read(&uts_sem);
1528         return errno;
1529 }
1530
1531 #endif
1532
1533 /*
1534  * Only setdomainname; getdomainname can be implemented by calling
1535  * uname()
1536  */
1537 asmlinkage long sys_setdomainname(char __user *name, int len)
1538 {
1539         int errno;
1540         char tmp[__NEW_UTS_LEN];
1541
1542         if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SET_UTSNAME))
1543                 return -EPERM;
1544         if (len < 0 || len > __NEW_UTS_LEN)
1545                 return -EINVAL;
1546
1547         down_write(&uts_sem);
1548         errno = -EFAULT;
1549         if (!copy_from_user(tmp, name, len)) {
1550                 char *ptr = vx_new_uts(domainname);
1551
1552                 memcpy(ptr, tmp, len);
1553                 ptr[len] = 0;
1554                 errno = 0;
1555         }
1556         up_write(&uts_sem);
1557         return errno;
1558 }
1559
1560 asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1561 {
1562         if (resource >= RLIM_NLIMITS)
1563                 return -EINVAL;
1564         else
1565                 return copy_to_user(rlim, current->rlim + resource, sizeof(*rlim))
1566                         ? -EFAULT : 0;
1567 }
1568
1569 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1570
1571 /*
1572  *      Back compatibility for getrlimit. Needed for some apps.
1573  */
1574  
1575 asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1576 {
1577         struct rlimit x;
1578         if (resource >= RLIM_NLIMITS)
1579                 return -EINVAL;
1580
1581         memcpy(&x, current->rlim + resource, sizeof(*rlim));
1582         if(x.rlim_cur > 0x7FFFFFFF)
1583                 x.rlim_cur = 0x7FFFFFFF;
1584         if(x.rlim_max > 0x7FFFFFFF)
1585                 x.rlim_max = 0x7FFFFFFF;
1586         return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1587 }
1588
1589 #endif
1590
1591 asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1592 {
1593         struct rlimit new_rlim, *old_rlim;
1594         int retval;
1595
1596         if (resource >= RLIM_NLIMITS)
1597                 return -EINVAL;
1598         if(copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1599                 return -EFAULT;
1600        if (new_rlim.rlim_cur > new_rlim.rlim_max)
1601                return -EINVAL;
1602         old_rlim = current->rlim + resource;
1603         if (((new_rlim.rlim_cur > old_rlim->rlim_max) ||
1604              (new_rlim.rlim_max > old_rlim->rlim_max)) &&
1605             !capable(CAP_SYS_RESOURCE) && vx_ccaps(VXC_SET_RLIMIT))
1606                 return -EPERM;
1607         if (resource == RLIMIT_NOFILE) {
1608                 if (new_rlim.rlim_cur > NR_OPEN || new_rlim.rlim_max > NR_OPEN)
1609                         return -EPERM;
1610         }
1611
1612         retval = security_task_setrlimit(resource, &new_rlim);
1613         if (retval)
1614                 return retval;
1615
1616         *old_rlim = new_rlim;
1617         return 0;
1618 }
1619
1620 /*
1621  * It would make sense to put struct rusage in the task_struct,
1622  * except that would make the task_struct be *really big*.  After
1623  * task_struct gets moved into malloc'ed memory, it would
1624  * make sense to do this.  It will make moving the rest of the information
1625  * a lot simpler!  (Which we're not doing right now because we're not
1626  * measuring them yet).
1627  *
1628  * This is SMP safe.  Either we are called from sys_getrusage on ourselves
1629  * below (we know we aren't going to exit/disappear and only we change our
1630  * rusage counters), or we are called from wait4() on a process which is
1631  * either stopped or zombied.  In the zombied case the task won't get
1632  * reaped till shortly after the call to getrusage(), in both cases the
1633  * task being examined is in a frozen state so the counters won't change.
1634  */
1635 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1636 {
1637         struct rusage r;
1638
1639         memset((char *) &r, 0, sizeof(r));
1640         switch (who) {
1641                 case RUSAGE_SELF:
1642                         jiffies_to_timeval(p->utime, &r.ru_utime);
1643                         jiffies_to_timeval(p->stime, &r.ru_stime);
1644                         r.ru_nvcsw = p->nvcsw;
1645                         r.ru_nivcsw = p->nivcsw;
1646                         r.ru_minflt = p->min_flt;
1647                         r.ru_majflt = p->maj_flt;
1648                         break;
1649                 case RUSAGE_CHILDREN:
1650                         jiffies_to_timeval(p->cutime, &r.ru_utime);
1651                         jiffies_to_timeval(p->cstime, &r.ru_stime);
1652                         r.ru_nvcsw = p->cnvcsw;
1653                         r.ru_nivcsw = p->cnivcsw;
1654                         r.ru_minflt = p->cmin_flt;
1655                         r.ru_majflt = p->cmaj_flt;
1656                         break;
1657                 default:
1658                         jiffies_to_timeval(p->utime + p->cutime, &r.ru_utime);
1659                         jiffies_to_timeval(p->stime + p->cstime, &r.ru_stime);
1660                         r.ru_nvcsw = p->nvcsw + p->cnvcsw;
1661                         r.ru_nivcsw = p->nivcsw + p->cnivcsw;
1662                         r.ru_minflt = p->min_flt + p->cmin_flt;
1663                         r.ru_majflt = p->maj_flt + p->cmaj_flt;
1664                         break;
1665         }
1666         return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1667 }
1668
1669 asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
1670 {
1671         if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
1672                 return -EINVAL;
1673         return getrusage(current, who, ru);
1674 }
1675
1676 asmlinkage long sys_umask(int mask)
1677 {
1678         mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1679         return mask;
1680 }
1681     
1682 asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
1683                           unsigned long arg4, unsigned long arg5)
1684 {
1685         int error;
1686         int sig;
1687
1688         error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1689         if (error)
1690                 return error;
1691
1692         switch (option) {
1693                 case PR_SET_PDEATHSIG:
1694                         sig = arg2;
1695                         if (sig < 0 || sig > _NSIG) {
1696                                 error = -EINVAL;
1697                                 break;
1698                         }
1699                         current->pdeath_signal = sig;
1700                         break;
1701                 case PR_GET_PDEATHSIG:
1702                         error = put_user(current->pdeath_signal, (int __user *)arg2);
1703                         break;
1704                 case PR_GET_DUMPABLE:
1705                         if (current->mm->dumpable)
1706                                 error = 1;
1707                         break;
1708                 case PR_SET_DUMPABLE:
1709                         if (arg2 != 0 && arg2 != 1) {
1710                                 error = -EINVAL;
1711                                 break;
1712                         }
1713                         current->mm->dumpable = arg2;
1714                         break;
1715
1716                 case PR_SET_UNALIGN:
1717                         error = SET_UNALIGN_CTL(current, arg2);
1718                         break;
1719                 case PR_GET_UNALIGN:
1720                         error = GET_UNALIGN_CTL(current, arg2);
1721                         break;
1722                 case PR_SET_FPEMU:
1723                         error = SET_FPEMU_CTL(current, arg2);
1724                         break;
1725                 case PR_GET_FPEMU:
1726                         error = GET_FPEMU_CTL(current, arg2);
1727                         break;
1728                 case PR_SET_FPEXC:
1729                         error = SET_FPEXC_CTL(current, arg2);
1730                         break;
1731                 case PR_GET_FPEXC:
1732                         error = GET_FPEXC_CTL(current, arg2);
1733                         break;
1734                 case PR_GET_TIMING:
1735                         error = PR_TIMING_STATISTICAL;
1736                         break;
1737                 case PR_SET_TIMING:
1738                         if (arg2 == PR_TIMING_STATISTICAL)
1739                                 error = 0;
1740                         else
1741                                 error = -EINVAL;
1742                         break;
1743
1744                 case PR_GET_KEEPCAPS:
1745                         if (current->keep_capabilities)
1746                                 error = 1;
1747                         break;
1748                 case PR_SET_KEEPCAPS:
1749                         if (arg2 != 0 && arg2 != 1) {
1750                                 error = -EINVAL;
1751                                 break;
1752                         }
1753                         current->keep_capabilities = arg2;
1754                         break;
1755                 default:
1756                         error = -EINVAL;
1757                         break;
1758         }
1759         return error;
1760 }