linux-bk/kernel/sys.c
<<
>>
Prefs
   1/*
   2 *  linux/kernel/sys.c
   3 *
   4 *  Copyright (C) 1991, 1992  Linus Torvalds
   5 */
   6
   7#include <linux/config.h>
   8#include <linux/module.h>
   9#include <linux/mm.h>
  10#include <linux/utsname.h>
  11#include <linux/mman.h>
  12#include <linux/smp_lock.h>
  13#include <linux/notifier.h>
  14#include <linux/reboot.h>
  15#include <linux/prctl.h>
  16#include <linux/init.h>
  17#include <linux/highuid.h>
  18#include <linux/fs.h>
  19#include <linux/tqueue.h>
  20#include <linux/device.h>
  21#include <linux/times.h>
  22#include <linux/security.h>
  23
  24#include <asm/uaccess.h>
  25#include <asm/io.h>
  26#include <asm/unistd.h>
  27
  28#ifndef SET_UNALIGN_CTL
  29# define SET_UNALIGN_CTL(a,b)   (-EINVAL)
  30#endif
  31#ifndef GET_UNALIGN_CTL
  32# define GET_UNALIGN_CTL(a,b)   (-EINVAL)
  33#endif
  34#ifndef SET_FPEMU_CTL
  35# define SET_FPEMU_CTL(a,b)     (-EINVAL)
  36#endif
  37#ifndef GET_FPEMU_CTL
  38# define GET_FPEMU_CTL(a,b)     (-EINVAL)
  39#endif
  40#ifndef SET_FPEXC_CTL
  41# define SET_FPEXC_CTL(a,b)     (-EINVAL)
  42#endif
  43#ifndef GET_FPEXC_CTL
  44# define GET_FPEXC_CTL(a,b)     (-EINVAL)
  45#endif
  46
  47/*
  48 * this is where the system-wide overflow UID and GID are defined, for
  49 * architectures that now have 32-bit UID/GID but didn't in the past
  50 */
  51
  52int overflowuid = DEFAULT_OVERFLOWUID;
  53int overflowgid = DEFAULT_OVERFLOWGID;
  54
  55/*
  56 * the same as above, but for filesystems which can only store a 16-bit
  57 * UID and GID. as such, this is needed on all architectures
  58 */
  59
  60int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
  61int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
  62
  63/*
  64 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
  65 */
  66
  67int C_A_D = 1;
  68int cad_pid = 1;
  69
  70extern int system_running;
  71
  72/*
  73 *      Notifier list for kernel code which wants to be called
  74 *      at shutdown. This is used to stop any idling DMA operations
  75 *      and the like. 
  76 */
  77
  78static struct notifier_block *reboot_notifier_list;
  79rwlock_t notifier_lock = RW_LOCK_UNLOCKED;
  80
  81/**
  82 *      notifier_chain_register - Add notifier to a notifier chain
  83 *      @list: Pointer to root list pointer
  84 *      @n: New entry in notifier chain
  85 *
  86 *      Adds a notifier to a notifier chain.
  87 *
  88 *      Currently always returns zero.
  89 */
  90 
  91int notifier_chain_register(struct notifier_block **list, struct notifier_block *n)
  92{
  93        write_lock(&notifier_lock);
  94        while(*list)
  95        {
  96                if(n->priority > (*list)->priority)
  97                        break;
  98                list= &((*list)->next);
  99        }
 100        n->next = *list;
 101        *list=n;
 102        write_unlock(&notifier_lock);
 103        return 0;
 104}
 105
 106/**
 107 *      notifier_chain_unregister - Remove notifier from a notifier chain
 108 *      @nl: Pointer to root list pointer
 109 *      @n: New entry in notifier chain
 110 *
 111 *      Removes a notifier from a notifier chain.
 112 *
 113 *      Returns zero on success, or %-ENOENT on failure.
 114 */
 115 
 116int notifier_chain_unregister(struct notifier_block **nl, struct notifier_block *n)
 117{
 118        write_lock(&notifier_lock);
 119        while((*nl)!=NULL)
 120        {
 121                if((*nl)==n)
 122                {
 123                        *nl=n->next;
 124                        write_unlock(&notifier_lock);
 125                        return 0;
 126                }
 127                nl=&((*nl)->next);
 128        }
 129        write_unlock(&notifier_lock);
 130        return -ENOENT;
 131}
 132
 133/**
 134 *      notifier_call_chain - Call functions in a notifier chain
 135 *      @n: Pointer to root pointer of notifier chain
 136 *      @val: Value passed unmodified to notifier function
 137 *      @v: Pointer passed unmodified to notifier function
 138 *
 139 *      Calls each function in a notifier chain in turn.
 140 *
 141 *      If the return value of the notifier can be and'd
 142 *      with %NOTIFY_STOP_MASK, then notifier_call_chain
 143 *      will return immediately, with the return value of
 144 *      the notifier function which halted execution.
 145 *      Otherwise, the return value is the return value
 146 *      of the last notifier function called.
 147 */
 148 
 149int notifier_call_chain(struct notifier_block **n, unsigned long val, void *v)
 150{
 151        int ret=NOTIFY_DONE;
 152        struct notifier_block *nb = *n;
 153
 154        while(nb)
 155        {
 156                ret=nb->notifier_call(nb,val,v);
 157                if(ret&NOTIFY_STOP_MASK)
 158                {
 159                        return ret;
 160                }
 161                nb=nb->next;
 162        }
 163        return ret;
 164}
 165
 166/**
 167 *      register_reboot_notifier - Register function to be called at reboot time
 168 *      @nb: Info about notifier function to be called
 169 *
 170 *      Registers a function with the list of functions
 171 *      to be called at reboot time.
 172 *
 173 *      Currently always returns zero, as notifier_chain_register
 174 *      always returns zero.
 175 */
 176 
 177int register_reboot_notifier(struct notifier_block * nb)
 178{
 179        return notifier_chain_register(&reboot_notifier_list, nb);
 180}
 181
 182/**
 183 *      unregister_reboot_notifier - Unregister previously registered reboot notifier
 184 *      @nb: Hook to be unregistered
 185 *
 186 *      Unregisters a previously registered reboot
 187 *      notifier function.
 188 *
 189 *      Returns zero on success, or %-ENOENT on failure.
 190 */
 191 
 192int unregister_reboot_notifier(struct notifier_block * nb)
 193{
 194        return notifier_chain_unregister(&reboot_notifier_list, nb);
 195}
 196
 197asmlinkage long sys_ni_syscall(void)
 198{
 199        return -ENOSYS;
 200}
 201
 202cond_syscall(sys_nfsservctl)
 203cond_syscall(sys_quotactl)
 204cond_syscall(sys_acct)
 205
 206static int set_one_prio(struct task_struct *p, int niceval, int error)
 207{
 208        if (p->uid != current->euid &&
 209                p->uid != current->uid && !capable(CAP_SYS_NICE)) {
 210                error = -EPERM;
 211                goto out;
 212        }
 213
 214        if (error == -ESRCH)
 215                error = 0;
 216        if (niceval < task_nice(p) && !capable(CAP_SYS_NICE))
 217                error = -EACCES;
 218        else
 219                set_user_nice(p, niceval);
 220out:
 221        return error;
 222}
 223
 224asmlinkage long sys_setpriority(int which, int who, int niceval)
 225{
 226        struct task_struct *g, *p;
 227        struct user_struct *user;
 228        struct pid *pid;
 229        struct list_head *l;
 230        int error = -EINVAL;
 231
 232        if (which > 2 || which < 0)
 233                goto out;
 234
 235        /* normalize: avoid signed division (rounding problems) */
 236        error = -ESRCH;
 237        if (niceval < -20)
 238                niceval = -20;
 239        if (niceval > 19)
 240                niceval = 19;
 241
 242        read_lock(&tasklist_lock);
 243        switch (which) {
 244                case PRIO_PROCESS:
 245                        if (!who)
 246                                who = current->pid;
 247                        p = find_task_by_pid(who);
 248                        if (p)
 249                                error = set_one_prio(p, niceval, error);
 250                        break;
 251                case PRIO_PGRP:
 252                        if (!who)
 253                                who = current->pgrp;
 254                        for_each_task_pid(who, PIDTYPE_PGID, p, l, pid)
 255                                error = set_one_prio(p, niceval, error);
 256                        break;
 257                case PRIO_USER:
 258                        if (!who)
 259                                user = current->user;
 260                        else
 261                                user = find_user(who);
 262
 263                        if (!user)
 264                                goto out_unlock;
 265
 266                        do_each_thread(g, p)
 267                                if (p->uid == who)
 268                                        error = set_one_prio(p, niceval, error);
 269                        while_each_thread(g, p);
 270                        break;
 271        }
 272out_unlock:
 273        read_unlock(&tasklist_lock);
 274out:
 275        return error;
 276}
 277
 278/*
 279 * Ugh. To avoid negative return values, "getpriority()" will
 280 * not return the normal nice-value, but a negated value that
 281 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
 282 * to stay compatible.
 283 */
 284asmlinkage long sys_getpriority(int which, int who)
 285{
 286        struct task_struct *g, *p;
 287        struct list_head *l;
 288        struct pid *pid;
 289        struct user_struct *user;
 290        long niceval, retval = -ESRCH;
 291
 292        if (which > 2 || which < 0)
 293                return -EINVAL;
 294
 295        read_lock(&tasklist_lock);
 296        switch (which) {
 297                case PRIO_PROCESS:
 298                        if (!who)
 299                                who = current->pid;
 300                        p = find_task_by_pid(who);
 301                        if (p) {
 302                                niceval = 20 - task_nice(p);
 303                                if (niceval > retval)
 304                                        retval = niceval;
 305                        }
 306                        break;
 307                case PRIO_PGRP:
 308                        if (!who)
 309                                who = current->pgrp;
 310                        for_each_task_pid(who, PIDTYPE_PGID, p, l, pid) {
 311                                niceval = 20 - task_nice(p);
 312                                if (niceval > retval)
 313                                        retval = niceval;
 314                        }
 315                        break;
 316                case PRIO_USER:
 317                        if (!who)
 318                                user = current->user;
 319                        else
 320                                user = find_user(who);
 321
 322                        if (!user)
 323                                goto out_unlock;
 324
 325                        do_each_thread(g, p)
 326                                if (p->uid == who) {
 327                                        niceval = 20 - task_nice(p);
 328                                        if (niceval > retval)
 329                                                retval = niceval;
 330                                }
 331                        while_each_thread(g, p);
 332                        break;
 333        }
 334out_unlock:
 335        read_unlock(&tasklist_lock);
 336
 337        return retval;
 338}
 339
 340
 341/*
 342 * Reboot system call: for obvious reasons only root may call it,
 343 * and even root needs to set up some magic numbers in the registers
 344 * so that some mistake won't make this reboot the whole machine.
 345 * You can also set the meaning of the ctrl-alt-del-key here.
 346 *
 347 * reboot doesn't sync: do that yourself before calling this.
 348 */
 349asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void * arg)
 350{
 351        char buffer[256];
 352
 353        /* We only trust the superuser with rebooting the system. */
 354        if (!capable(CAP_SYS_BOOT))
 355                return -EPERM;
 356
 357        /* For safety, we require "magic" arguments. */
 358        if (magic1 != LINUX_REBOOT_MAGIC1 ||
 359            (magic2 != LINUX_REBOOT_MAGIC2 && magic2 != LINUX_REBOOT_MAGIC2A &&
 360                        magic2 != LINUX_REBOOT_MAGIC2B))
 361                return -EINVAL;
 362
 363        lock_kernel();
 364        switch (cmd) {
 365        case LINUX_REBOOT_CMD_RESTART:
 366                notifier_call_chain(&reboot_notifier_list, SYS_RESTART, NULL);
 367                system_running = 0;
 368                device_shutdown();
 369                printk(KERN_EMERG "Restarting system.\n");
 370                machine_restart(NULL);
 371                break;
 372
 373        case LINUX_REBOOT_CMD_CAD_ON:
 374                C_A_D = 1;
 375                break;
 376
 377        case LINUX_REBOOT_CMD_CAD_OFF:
 378                C_A_D = 0;
 379                break;
 380
 381        case LINUX_REBOOT_CMD_HALT:
 382                notifier_call_chain(&reboot_notifier_list, SYS_HALT, NULL);
 383                system_running = 0;
 384                device_shutdown();
 385                printk(KERN_EMERG "System halted.\n");
 386                machine_halt();
 387                do_exit(0);
 388                break;
 389
 390        case LINUX_REBOOT_CMD_POWER_OFF:
 391                notifier_call_chain(&reboot_notifier_list, SYS_POWER_OFF, NULL);
 392                system_running = 0;
 393                device_shutdown();
 394                printk(KERN_EMERG "Power down.\n");
 395                machine_power_off();
 396                do_exit(0);
 397                break;
 398
 399        case LINUX_REBOOT_CMD_RESTART2:
 400                if (strncpy_from_user(&buffer[0], (char *)arg, sizeof(buffer) - 1) < 0) {
 401                        unlock_kernel();
 402                        return -EFAULT;
 403                }
 404                buffer[sizeof(buffer) - 1] = '\0';
 405
 406                notifier_call_chain(&reboot_notifier_list, SYS_RESTART, buffer);
 407                system_running = 0;
 408                device_shutdown();
 409                printk(KERN_EMERG "Restarting system with command '%s'.\n", buffer);
 410                machine_restart(buffer);
 411                break;
 412
 413#ifdef CONFIG_SOFTWARE_SUSPEND
 414        case LINUX_REBOOT_CMD_SW_SUSPEND:
 415                if (!software_suspend_enabled) {
 416                        unlock_kernel();
 417                        return -EAGAIN;
 418                }               
 419                software_suspend();
 420                do_exit(0);
 421                break;
 422#endif
 423
 424        default:
 425                unlock_kernel();
 426                return -EINVAL;
 427        }
 428        unlock_kernel();
 429        return 0;
 430}
 431
 432static void deferred_cad(void *dummy)
 433{
 434        notifier_call_chain(&reboot_notifier_list, SYS_RESTART, NULL);
 435        machine_restart(NULL);
 436}
 437
 438/*
 439 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
 440 * As it's called within an interrupt, it may NOT sync: the only choice
 441 * is whether to reboot at once, or just ignore the ctrl-alt-del.
 442 */
 443void ctrl_alt_del(void)
 444{
 445        static struct tq_struct cad_tq = {
 446                .routine = deferred_cad,
 447        };
 448
 449        if (C_A_D)
 450                schedule_task(&cad_tq);
 451        else
 452                kill_proc(cad_pid, SIGINT, 1);
 453}
 454        
 455
 456/*
 457 * Unprivileged users may change the real gid to the effective gid
 458 * or vice versa.  (BSD-style)
 459 *
 460 * If you set the real gid at all, or set the effective gid to a value not
 461 * equal to the real gid, then the saved gid is set to the new effective gid.
 462 *
 463 * This makes it possible for a setgid program to completely drop its
 464 * privileges, which is often a useful assertion to make when you are doing
 465 * a security audit over a program.
 466 *
 467 * The general idea is that a program which uses just setregid() will be
 468 * 100% compatible with BSD.  A program which uses just setgid() will be
 469 * 100% compatible with POSIX with saved IDs. 
 470 *
 471 * SMP: There are not races, the GIDs are checked only by filesystem
 472 *      operations (as far as semantic preservation is concerned).
 473 */
 474asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
 475{
 476        int old_rgid = current->gid;
 477        int old_egid = current->egid;
 478        int new_rgid = old_rgid;
 479        int new_egid = old_egid;
 480        int retval;
 481
 482        retval = security_ops->task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
 483        if (retval)
 484                return retval;
 485
 486        if (rgid != (gid_t) -1) {
 487                if ((old_rgid == rgid) ||
 488                    (current->egid==rgid) ||
 489                    capable(CAP_SETGID))
 490                        new_rgid = rgid;
 491                else
 492                        return -EPERM;
 493        }
 494        if (egid != (gid_t) -1) {
 495                if ((old_rgid == egid) ||
 496                    (current->egid == egid) ||
 497                    (current->sgid == egid) ||
 498                    capable(CAP_SETGID))
 499                        new_egid = egid;
 500                else {
 501                        return -EPERM;
 502                }
 503        }
 504        if (new_egid != old_egid)
 505        {
 506                current->mm->dumpable = 0;
 507                wmb();
 508        }
 509        if (rgid != (gid_t) -1 ||
 510            (egid != (gid_t) -1 && egid != old_rgid))
 511                current->sgid = new_egid;
 512        current->fsgid = new_egid;
 513        current->egid = new_egid;
 514        current->gid = new_rgid;
 515        return 0;
 516}
 517
 518/*
 519 * setgid() is implemented like SysV w/ SAVED_IDS 
 520 *
 521 * SMP: Same implicit races as above.
 522 */
 523asmlinkage long sys_setgid(gid_t gid)
 524{
 525        int old_egid = current->egid;
 526        int retval;
 527
 528        retval = security_ops->task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
 529        if (retval)
 530                return retval;
 531
 532        if (capable(CAP_SETGID))
 533        {
 534                if(old_egid != gid)
 535                {
 536                        current->mm->dumpable=0;
 537                        wmb();
 538                }
 539                current->gid = current->egid = current->sgid = current->fsgid = gid;
 540        }
 541        else if ((gid == current->gid) || (gid == current->sgid))
 542        {
 543                if(old_egid != gid)
 544                {
 545                        current->mm->dumpable=0;
 546                        wmb();
 547                }
 548                current->egid = current->fsgid = gid;
 549        }
 550        else
 551                return -EPERM;
 552        return 0;
 553}
 554  
 555static int set_user(uid_t new_ruid, int dumpclear)
 556{
 557        struct user_struct *new_user, *old_user;
 558
 559        /* What if a process setreuid()'s and this brings the
 560         * new uid over his NPROC rlimit?  We can check this now
 561         * cheaply with the new uid cache, so if it matters
 562         * we should be checking for it.  -DaveM
 563         */
 564        new_user = alloc_uid(new_ruid);
 565        if (!new_user)
 566                return -EAGAIN;
 567        old_user = current->user;
 568        atomic_dec(&old_user->processes);
 569        atomic_inc(&new_user->processes);
 570
 571        if(dumpclear)
 572        {
 573                current->mm->dumpable = 0;
 574                wmb();
 575        }
 576        current->uid = new_ruid;
 577        current->user = new_user;
 578        free_uid(old_user);
 579        return 0;
 580}
 581
 582/*
 583 * Unprivileged users may change the real uid to the effective uid
 584 * or vice versa.  (BSD-style)
 585 *
 586 * If you set the real uid at all, or set the effective uid to a value not
 587 * equal to the real uid, then the saved uid is set to the new effective uid.
 588 *
 589 * This makes it possible for a setuid program to completely drop its
 590 * privileges, which is often a useful assertion to make when you are doing
 591 * a security audit over a program.
 592 *
 593 * The general idea is that a program which uses just setreuid() will be
 594 * 100% compatible with BSD.  A program which uses just setuid() will be
 595 * 100% compatible with POSIX with saved IDs. 
 596 */
 597asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
 598{
 599        int old_ruid, old_euid, old_suid, new_ruid, new_euid;
 600        int retval;
 601
 602        retval = security_ops->task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
 603        if (retval)
 604                return retval;
 605
 606        new_ruid = old_ruid = current->uid;
 607        new_euid = old_euid = current->euid;
 608        old_suid = current->suid;
 609
 610        if (ruid != (uid_t) -1) {
 611                new_ruid = ruid;
 612                if ((old_ruid != ruid) &&
 613                    (current->euid != ruid) &&
 614                    !capable(CAP_SETUID))
 615                        return -EPERM;
 616        }
 617
 618        if (euid != (uid_t) -1) {
 619                new_euid = euid;
 620                if ((old_ruid != euid) &&
 621                    (current->euid != euid) &&
 622                    (current->suid != euid) &&
 623                    !capable(CAP_SETUID))
 624                        return -EPERM;
 625        }
 626
 627        if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
 628                return -EAGAIN;
 629
 630        if (new_euid != old_euid)
 631        {
 632                current->mm->dumpable=0;
 633                wmb();
 634        }
 635        current->fsuid = current->euid = new_euid;
 636        if (ruid != (uid_t) -1 ||
 637            (euid != (uid_t) -1 && euid != old_ruid))
 638                current->suid = current->euid;
 639        current->fsuid = current->euid;
 640
 641        return security_ops->task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
 642}
 643
 644
 645                
 646/*
 647 * setuid() is implemented like SysV with SAVED_IDS 
 648 * 
 649 * Note that SAVED_ID's is deficient in that a setuid root program
 650 * like sendmail, for example, cannot set its uid to be a normal 
 651 * user and then switch back, because if you're root, setuid() sets
 652 * the saved uid too.  If you don't like this, blame the bright people
 653 * in the POSIX committee and/or USG.  Note that the BSD-style setreuid()
 654 * will allow a root program to temporarily drop privileges and be able to
 655 * regain them by swapping the real and effective uid.  
 656 */
 657asmlinkage long sys_setuid(uid_t uid)
 658{
 659        int old_euid = current->euid;
 660        int old_ruid, old_suid, new_ruid, new_suid;
 661        int retval;
 662
 663        retval = security_ops->task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
 664        if (retval)
 665                return retval;
 666
 667        old_ruid = new_ruid = current->uid;
 668        old_suid = current->suid;
 669        new_suid = old_suid;
 670        
 671        if (capable(CAP_SETUID)) {
 672                if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
 673                        return -EAGAIN;
 674                new_suid = uid;
 675        } else if ((uid != current->uid) && (uid != new_suid))
 676                return -EPERM;
 677
 678        if (old_euid != uid)
 679        {
 680                current->mm->dumpable = 0;
 681                wmb();
 682        }
 683        current->fsuid = current->euid = uid;
 684        current->suid = new_suid;
 685
 686        return security_ops->task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
 687}
 688
 689
 690/*
 691 * This function implements a generic ability to update ruid, euid,
 692 * and suid.  This allows you to implement the 4.4 compatible seteuid().
 693 */
 694asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
 695{
 696        int old_ruid = current->uid;
 697        int old_euid = current->euid;
 698        int old_suid = current->suid;
 699        int retval;
 700
 701        retval = security_ops->task_setuid(ruid, euid, suid, LSM_SETID_RES);
 702        if (retval)
 703                return retval;
 704
 705        if (!capable(CAP_SETUID)) {
 706                if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
 707                    (ruid != current->euid) && (ruid != current->suid))
 708                        return -EPERM;
 709                if ((euid != (uid_t) -1) && (euid != current->uid) &&
 710                    (euid != current->euid) && (euid != current->suid))
 711                        return -EPERM;
 712                if ((suid != (uid_t) -1) && (suid != current->uid) &&
 713                    (suid != current->euid) && (suid != current->suid))
 714                        return -EPERM;
 715        }
 716        if (ruid != (uid_t) -1) {
 717                if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
 718                        return -EAGAIN;
 719        }
 720        if (euid != (uid_t) -1) {
 721                if (euid != current->euid)
 722                {
 723                        current->mm->dumpable = 0;
 724                        wmb();
 725                }
 726                current->euid = euid;
 727        }
 728        current->fsuid = current->euid;
 729        if (suid != (uid_t) -1)
 730                current->suid = suid;
 731
 732        return security_ops->task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
 733}
 734
 735asmlinkage long sys_getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
 736{
 737        int retval;
 738
 739        if (!(retval = put_user(current->uid, ruid)) &&
 740            !(retval = put_user(current->euid, euid)))
 741                retval = put_user(current->suid, suid);
 742
 743        return retval;
 744}
 745
 746/*
 747 * Same as above, but for rgid, egid, sgid.
 748 */
 749asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
 750{
 751        int retval;
 752
 753        retval = security_ops->task_setgid(rgid, egid, sgid, LSM_SETID_RES);
 754        if (retval)
 755                return retval;
 756
 757        if (!capable(CAP_SETGID)) {
 758                if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
 759                    (rgid != current->egid) && (rgid != current->sgid))
 760                        return -EPERM;
 761                if ((egid != (gid_t) -1) && (egid != current->gid) &&
 762                    (egid != current->egid) && (egid != current->sgid))
 763                        return -EPERM;
 764                if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
 765                    (sgid != current->egid) && (sgid != current->sgid))
 766                        return -EPERM;
 767        }
 768        if (egid != (gid_t) -1) {
 769                if (egid != current->egid)
 770                {
 771                        current->mm->dumpable = 0;
 772                        wmb();
 773                }
 774                current->egid = egid;
 775        }
 776        current->fsgid = current->egid;
 777        if (rgid != (gid_t) -1)
 778                current->gid = rgid;
 779        if (sgid != (gid_t) -1)
 780                current->sgid = sgid;
 781        return 0;
 782}
 783
 784asmlinkage long sys_getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
 785{
 786        int retval;
 787
 788        if (!(retval = put_user(current->gid, rgid)) &&
 789            !(retval = put_user(current->egid, egid)))
 790                retval = put_user(current->sgid, sgid);
 791
 792        return retval;
 793}
 794
 795
 796/*
 797 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
 798 * is used for "access()" and for the NFS daemon (letting nfsd stay at
 799 * whatever uid it wants to). It normally shadows "euid", except when
 800 * explicitly set by setfsuid() or for access..
 801 */
 802asmlinkage long sys_setfsuid(uid_t uid)
 803{
 804        int old_fsuid;
 805        int retval;
 806
 807        retval = security_ops->task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
 808        if (retval)
 809                return retval;
 810
 811        old_fsuid = current->fsuid;
 812        if (uid == current->uid || uid == current->euid ||
 813            uid == current->suid || uid == current->fsuid || 
 814            capable(CAP_SETUID))
 815        {
 816                if (uid != old_fsuid)
 817                {
 818                        current->mm->dumpable = 0;
 819                        wmb();
 820                }
 821                current->fsuid = uid;
 822        }
 823
 824        retval = security_ops->task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
 825        if (retval)
 826                return retval;
 827
 828        return old_fsuid;
 829}
 830
 831/*
 832 * Samma på svenska..
 833 */
 834asmlinkage long sys_setfsgid(gid_t gid)
 835{
 836        int old_fsgid;
 837        int retval;
 838
 839        retval = security_ops->task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS);
 840        if (retval)
 841                return retval;
 842
 843        old_fsgid = current->fsgid;
 844        if (gid == current->gid || gid == current->egid ||
 845            gid == current->sgid || gid == current->fsgid || 
 846            capable(CAP_SETGID))
 847        {
 848                if (gid != old_fsgid)
 849                {
 850                        current->mm->dumpable = 0;
 851                        wmb();
 852                }
 853                current->fsgid = gid;
 854        }
 855        return old_fsgid;
 856}
 857
 858asmlinkage long sys_times(struct tms * tbuf)
 859{
 860        /*
 861         *      In the SMP world we might just be unlucky and have one of
 862         *      the times increment as we use it. Since the value is an
 863         *      atomically safe type this is just fine. Conceptually its
 864         *      as if the syscall took an instant longer to occur.
 865         */
 866        if (tbuf) {
 867                struct tms tmp;
 868                tmp.tms_utime = jiffies_to_clock_t(current->utime);
 869                tmp.tms_stime = jiffies_to_clock_t(current->stime);
 870                tmp.tms_cutime = jiffies_to_clock_t(current->cutime);
 871                tmp.tms_cstime = jiffies_to_clock_t(current->cstime);
 872                if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
 873                        return -EFAULT;
 874        }
 875        return jiffies_to_clock_t(jiffies);
 876}
 877
 878/*
 879 * This needs some heavy checking ...
 880 * I just haven't the stomach for it. I also don't fully
 881 * understand sessions/pgrp etc. Let somebody who does explain it.
 882 *
 883 * OK, I think I have the protection semantics right.... this is really
 884 * only important on a multi-user system anyway, to make sure one user
 885 * can't send a signal to a process owned by another.  -TYT, 12/12/91
 886 *
 887 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
 888 * LBT 04.03.94
 889 */
 890
 891asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
 892{
 893        struct task_struct *p;
 894        int err = -EINVAL;
 895
 896        if (!pid)
 897                pid = current->pid;
 898        if (!pgid)
 899                pgid = pid;
 900        if (pgid < 0)
 901                return -EINVAL;
 902
 903        /* From this point forward we keep holding onto the tasklist lock
 904         * so that our parent does not change from under us. -DaveM
 905         */
 906        write_lock_irq(&tasklist_lock);
 907
 908        err = -ESRCH;
 909        p = find_task_by_pid(pid);
 910        if (!p)
 911                goto out;
 912        err = -EINVAL;
 913        if (!thread_group_leader(p))
 914                goto out;
 915
 916        if (p->parent == current || p->real_parent == current) {
 917                err = -EPERM;
 918                if (p->session != current->session)
 919                        goto out;
 920                err = -EACCES;
 921                if (p->did_exec)
 922                        goto out;
 923        } else if (p != current)
 924                goto out;
 925        err = -EPERM;
 926        if (p->leader)
 927                goto out;
 928        if (pgid != pid) {
 929                struct task_struct *p;
 930                struct pid *pid;
 931                struct list_head *l;
 932
 933                for_each_task_pid(pgid, PIDTYPE_PGID, p, l, pid)
 934                        if (p->session == current->session)
 935                                goto ok_pgid;
 936                goto out;
 937        }
 938
 939ok_pgid:
 940        if (p->pgrp != pgid) {
 941                detach_pid(p, PIDTYPE_PGID);
 942                p->pgrp = pgid;
 943                attach_pid(p, PIDTYPE_PGID, pgid);
 944        }
 945        err = 0;
 946out:
 947        /* All paths lead to here, thus we are safe. -DaveM */
 948        write_unlock_irq(&tasklist_lock);
 949        return err;
 950}
 951
 952asmlinkage long sys_getpgid(pid_t pid)
 953{
 954        if (!pid) {
 955                return current->pgrp;
 956        } else {
 957                int retval;
 958                struct task_struct *p;
 959
 960                read_lock(&tasklist_lock);
 961                p = find_task_by_pid(pid);
 962
 963                retval = -ESRCH;
 964                if (p) {
 965                        retval = security_ops->task_getpgid(p);
 966                        if (!retval)
 967                                retval = p->pgrp;
 968                }
 969                read_unlock(&tasklist_lock);
 970                return retval;
 971        }
 972}
 973
 974asmlinkage long sys_getpgrp(void)
 975{
 976        /* SMP - assuming writes are word atomic this is fine */
 977        return current->pgrp;
 978}
 979
 980asmlinkage long sys_getsid(pid_t pid)
 981{
 982        if (!pid) {
 983                return current->session;
 984        } else {
 985                int retval;
 986                struct task_struct *p;
 987
 988                read_lock(&tasklist_lock);
 989                p = find_task_by_pid(pid);
 990
 991                retval = -ESRCH;
 992                if(p) {
 993                        retval = security_ops->task_getsid(p);
 994                        if (!retval)
 995                                retval = p->session;
 996                }
 997                read_unlock(&tasklist_lock);
 998                return retval;
 999        }
1000}
1001
1002asmlinkage long sys_setsid(void)
1003{
1004        struct pid *pid;
1005        int err = -EPERM;
1006
1007        if (!thread_group_leader(current))
1008                return -EINVAL;
1009
1010        write_lock_irq(&tasklist_lock);
1011
1012        pid = find_pid(PIDTYPE_PGID, current->pid);
1013        if (pid)
1014                goto out;
1015
1016        current->leader = 1;
1017        if (current->session != current->pid) {
1018                detach_pid(current, PIDTYPE_SID);
1019                current->session = current->pid;
1020                attach_pid(current, PIDTYPE_SID, current->pid);
1021        }
1022        if (current->pgrp != current->pid) {
1023                detach_pid(current, PIDTYPE_PGID);
1024                current->pgrp = current->pid;
1025                attach_pid(current, PIDTYPE_PGID, current->pid);
1026        }
1027        current->tty = NULL;
1028        current->tty_old_pgrp = 0;
1029        err = current->pgrp;
1030out:
1031        write_unlock_irq(&tasklist_lock);
1032        return err;
1033}
1034
1035/*
1036 * Supplementary group IDs
1037 */
1038asmlinkage long sys_getgroups(int gidsetsize, gid_t *grouplist)
1039{
1040        int i;
1041        
1042        /*
1043         *      SMP: Nobody else can change our grouplist. Thus we are
1044         *      safe.
1045         */
1046
1047        if (gidsetsize < 0)
1048                return -EINVAL;
1049        i = current->ngroups;
1050        if (gidsetsize) {
1051                if (i > gidsetsize)
1052                        return -EINVAL;
1053                if (copy_to_user(grouplist, current->groups, sizeof(gid_t)*i))
1054                        return -EFAULT;
1055        }
1056        return i;
1057}
1058
1059/*
1060 *      SMP: Our groups are not shared. We can copy to/from them safely
1061 *      without another task interfering.
1062 */
1063 
1064asmlinkage long sys_setgroups(int gidsetsize, gid_t *grouplist)
1065{
1066        gid_t groups[NGROUPS];
1067        int retval;
1068
1069        if (!capable(CAP_SETGID))
1070                return -EPERM;
1071        if ((unsigned) gidsetsize > NGROUPS)
1072                return -EINVAL;
1073        if(copy_from_user(groups, grouplist, gidsetsize * sizeof(gid_t)))
1074                return -EFAULT;
1075        retval = security_ops->task_setgroups(gidsetsize, groups);
1076        if (retval)
1077                return retval;
1078        memcpy(current->groups, groups, gidsetsize * sizeof(gid_t));
1079        current->ngroups = gidsetsize;
1080        return 0;
1081}
1082
1083static int supplemental_group_member(gid_t grp)
1084{
1085        int i = current->ngroups;
1086
1087        if (i) {
1088                gid_t *groups = current->groups;
1089                do {
1090                        if (*groups == grp)
1091                                return 1;
1092                        groups++;
1093                        i--;
1094                } while (i);
1095        }
1096        return 0;
1097}
1098
1099/*
1100 * Check whether we're fsgid/egid or in the supplemental group..
1101 */
1102int in_group_p(gid_t grp)
1103{
1104        int retval = 1;
1105        if (grp != current->fsgid)
1106                retval = supplemental_group_member(grp);
1107        return retval;
1108}
1109
1110int in_egroup_p(gid_t grp)
1111{
1112        int retval = 1;
1113        if (grp != current->egid)
1114                retval = supplemental_group_member(grp);
1115        return retval;
1116}
1117
1118DECLARE_RWSEM(uts_sem);
1119
1120asmlinkage long sys_newuname(struct new_utsname * name)
1121{
1122        int errno = 0;
1123
1124        down_read(&uts_sem);
1125        if (copy_to_user(name,&system_utsname,sizeof *name))
1126                errno = -EFAULT;
1127        up_read(&uts_sem);
1128        return errno;
1129}
1130
1131asmlinkage long sys_sethostname(char *name, int len)
1132{
1133        int errno;
1134
1135        if (!capable(CAP_SYS_ADMIN))
1136                return -EPERM;
1137        if (len < 0 || len > __NEW_UTS_LEN)
1138                return -EINVAL;
1139        down_write(&uts_sem);
1140        errno = -EFAULT;
1141        if (!copy_from_user(system_utsname.nodename, name, len)) {
1142                system_utsname.nodename[len] = 0;
1143                errno = 0;
1144        }
1145        up_write(&uts_sem);
1146        return errno;
1147}
1148
1149asmlinkage long sys_gethostname(char *name, int len)
1150{
1151        int i, errno;
1152
1153        if (len < 0)
1154                return -EINVAL;
1155        down_read(&uts_sem);
1156        i = 1 + strlen(system_utsname.nodename);
1157        if (i > len)
1158                i = len;
1159        errno = 0;
1160        if (copy_to_user(name, system_utsname.nodename, i))
1161                errno = -EFAULT;
1162        up_read(&uts_sem);
1163        return errno;
1164}
1165
1166/*
1167 * Only setdomainname; getdomainname can be implemented by calling
1168 * uname()
1169 */
1170asmlinkage long sys_setdomainname(char *name, int len)
1171{
1172        int errno;
1173
1174        if (!capable(CAP_SYS_ADMIN))
1175                return -EPERM;
1176        if (len < 0 || len > __NEW_UTS_LEN)
1177                return -EINVAL;
1178
1179        down_write(&uts_sem);
1180        errno = -EFAULT;
1181        if (!copy_from_user(system_utsname.domainname, name, len)) {
1182                errno = 0;
1183                system_utsname.domainname[len] = 0;
1184        }
1185        up_write(&uts_sem);
1186        return errno;
1187}
1188
1189asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit *rlim)
1190{
1191        if (resource >= RLIM_NLIMITS)
1192                return -EINVAL;
1193        else
1194                return copy_to_user(rlim, current->rlim + resource, sizeof(*rlim))
1195                        ? -EFAULT : 0;
1196}
1197
1198#if !defined(__ia64__) 
1199
1200/*
1201 *      Back compatibility for getrlimit. Needed for some apps.
1202 */
1203 
1204asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit *rlim)
1205{
1206        struct rlimit x;
1207        if (resource >= RLIM_NLIMITS)
1208                return -EINVAL;
1209
1210        memcpy(&x, current->rlim + resource, sizeof(*rlim));
1211        if(x.rlim_cur > 0x7FFFFFFF)
1212                x.rlim_cur = 0x7FFFFFFF;
1213        if(x.rlim_max > 0x7FFFFFFF)
1214                x.rlim_max = 0x7FFFFFFF;
1215        return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1216}
1217
1218#endif
1219
1220asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit *rlim)
1221{
1222        struct rlimit new_rlim, *old_rlim;
1223        int retval;
1224
1225        if (resource >= RLIM_NLIMITS)
1226                return -EINVAL;
1227        if(copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1228                return -EFAULT;
1229        old_rlim = current->rlim + resource;
1230        if (((new_rlim.rlim_cur > old_rlim->rlim_max) ||
1231             (new_rlim.rlim_max > old_rlim->rlim_max)) &&
1232            !capable(CAP_SYS_RESOURCE))
1233                return -EPERM;
1234        if (resource == RLIMIT_NOFILE) {
1235                if (new_rlim.rlim_cur > NR_OPEN || new_rlim.rlim_max > NR_OPEN)
1236                        return -EPERM;
1237        }
1238
1239        retval = security_ops->task_setrlimit(resource, &new_rlim);
1240        if (retval)
1241                return retval;
1242
1243        *old_rlim = new_rlim;
1244        return 0;
1245}
1246
1247/*
1248 * It would make sense to put struct rusage in the task_struct,
1249 * except that would make the task_struct be *really big*.  After
1250 * task_struct gets moved into malloc'ed memory, it would
1251 * make sense to do this.  It will make moving the rest of the information
1252 * a lot simpler!  (Which we're not doing right now because we're not
1253 * measuring them yet).
1254 *
1255 * This is SMP safe.  Either we are called from sys_getrusage on ourselves
1256 * below (we know we aren't going to exit/disappear and only we change our
1257 * rusage counters), or we are called from wait4() on a process which is
1258 * either stopped or zombied.  In the zombied case the task won't get
1259 * reaped till shortly after the call to getrusage(), in both cases the
1260 * task being examined is in a frozen state so the counters won't change.
1261 *
1262 * FIXME! Get the fault counts properly!
1263 */
1264int getrusage(struct task_struct *p, int who, struct rusage *ru)
1265{
1266        struct rusage r;
1267
1268        memset((char *) &r, 0, sizeof(r));
1269        switch (who) {
1270                case RUSAGE_SELF:
1271                        jiffies_to_timeval(p->utime, &r.ru_utime);
1272                        jiffies_to_timeval(p->stime, &r.ru_stime);
1273                        r.ru_minflt = p->min_flt;
1274                        r.ru_majflt = p->maj_flt;
1275                        r.ru_nswap = p->nswap;
1276                        break;
1277                case RUSAGE_CHILDREN:
1278                        jiffies_to_timeval(p->cutime, &r.ru_utime);
1279                        jiffies_to_timeval(p->cstime, &r.ru_stime);
1280                        r.ru_minflt = p->cmin_flt;
1281                        r.ru_majflt = p->cmaj_flt;
1282                        r.ru_nswap = p->cnswap;
1283                        break;
1284                default:
1285                        jiffies_to_timeval(p->utime + p->cutime, &r.ru_utime);
1286                        jiffies_to_timeval(p->stime + p->cstime, &r.ru_stime);
1287                        r.ru_minflt = p->min_flt + p->cmin_flt;
1288                        r.ru_majflt = p->maj_flt + p->cmaj_flt;
1289                        r.ru_nswap = p->nswap + p->cnswap;
1290                        break;
1291        }
1292        return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1293}
1294
1295asmlinkage long sys_getrusage(int who, struct rusage *ru)
1296{
1297        if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
1298                return -EINVAL;
1299        return getrusage(current, who, ru);
1300}
1301
1302asmlinkage long sys_umask(int mask)
1303{
1304        mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1305        return mask;
1306}
1307    
1308asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
1309                          unsigned long arg4, unsigned long arg5)
1310{
1311        int error = 0;
1312        int sig;
1313
1314        error = security_ops->task_prctl(option, arg2, arg3, arg4, arg5);
1315        if (error)
1316                return error;
1317
1318        switch (option) {
1319                case PR_SET_PDEATHSIG:
1320                        sig = arg2;
1321                        if (sig < 0 || sig > _NSIG) {
1322                                error = -EINVAL;
1323                                break;
1324                        }
1325                        current->pdeath_signal = sig;
1326                        break;
1327                case PR_GET_PDEATHSIG:
1328                        error = put_user(current->pdeath_signal, (int *)arg2);
1329                        break;
1330                case PR_GET_DUMPABLE:
1331                        if (current->mm->dumpable)
1332                                error = 1;
1333                        break;
1334                case PR_SET_DUMPABLE:
1335                        if (arg2 != 0 && arg2 != 1) {
1336                                error = -EINVAL;
1337                                break;
1338                        }
1339                        current->mm->dumpable = arg2;
1340                        break;
1341
1342                case PR_SET_UNALIGN:
1343                        error = SET_UNALIGN_CTL(current, arg2);
1344                        break;
1345                case PR_GET_UNALIGN:
1346                        error = GET_UNALIGN_CTL(current, arg2);
1347                        break;
1348                case PR_SET_FPEMU:
1349                        error = SET_FPEMU_CTL(current, arg2);
1350                        break;
1351                case PR_GET_FPEMU:
1352                        error = GET_FPEMU_CTL(current, arg2);
1353                        break;
1354                case PR_SET_FPEXC:
1355                        error = SET_FPEXC_CTL(current, arg2);
1356                        break;
1357                case PR_GET_FPEXC:
1358                        error = GET_FPEXC_CTL(current, arg2);
1359                        break;
1360
1361
1362                case PR_GET_KEEPCAPS:
1363                        if (current->keep_capabilities)
1364                                error = 1;
1365                        break;
1366                case PR_SET_KEEPCAPS:
1367                        if (arg2 != 0 && arg2 != 1) {
1368                                error = -EINVAL;
1369                                break;
1370                        }
1371                        current->keep_capabilities = arg2;
1372                        break;
1373                default:
1374                        error = -EINVAL;
1375                        break;
1376        }
1377        return error;
1378}
1379
1380EXPORT_SYMBOL(notifier_chain_register);
1381EXPORT_SYMBOL(notifier_chain_unregister);
1382EXPORT_SYMBOL(notifier_call_chain);
1383EXPORT_SYMBOL(register_reboot_notifier);
1384EXPORT_SYMBOL(unregister_reboot_notifier);
1385EXPORT_SYMBOL(in_group_p);
1386EXPORT_SYMBOL(in_egroup_p);
1387
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.