Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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