linux-old/fs/exec.c
<<
>>
Prefs
   1/*
   2 *  linux/fs/exec.c
   3 *
   4 *  Copyright (C) 1991, 1992  Linus Torvalds
   5 */
   6
   7/*
   8 * #!-checking implemented by tytso.
   9 */
  10/*
  11 * Demand-loading implemented 01.12.91 - no need to read anything but
  12 * the header into memory. The inode of the executable is put into
  13 * "current->executable", and page faults do the actual loading. Clean.
  14 *
  15 * Once more I can proudly say that linux stood up to being changed: it
  16 * was less than 2 hours work to get demand-loading completely implemented.
  17 *
  18 * Demand loading changed July 1993 by Eric Youngdale.   Use mmap instead,
  19 * current->executable is only used by the procfs.  This allows a dispatch
  20 * table to check for several different types  of binary formats.  We keep
  21 * trying until we recognize the file or we run out of supported binary
  22 * formats. 
  23 */
  24
  25#include <linux/config.h>
  26#include <linux/slab.h>
  27#include <linux/file.h>
  28#include <linux/mman.h>
  29#include <linux/a.out.h>
  30#include <linux/stat.h>
  31#include <linux/fcntl.h>
  32#include <linux/smp_lock.h>
  33#include <linux/init.h>
  34#define __NO_VERSION__
  35#include <linux/module.h>
  36
  37#include <asm/uaccess.h>
  38#include <asm/pgtable.h>
  39#include <asm/mmu_context.h>
  40
  41#ifdef CONFIG_KMOD
  42#include <linux/kmod.h>
  43#endif
  44
  45/*
  46 * Here are the actual binaries that will be accepted:
  47 * add more with "register_binfmt()" if using modules...
  48 *
  49 * These are defined again for the 'real' modules if you are using a
  50 * module definition for these routines.
  51 */
  52
  53static struct linux_binfmt *formats = (struct linux_binfmt *) NULL;
  54
  55void __init binfmt_setup(void)
  56{
  57#ifdef CONFIG_BINFMT_MISC
  58        init_misc_binfmt();
  59#endif
  60
  61#ifdef CONFIG_BINFMT_ELF
  62        init_elf_binfmt();
  63#endif
  64
  65#ifdef CONFIG_BINFMT_ELF32
  66        init_elf32_binfmt();
  67#endif
  68
  69#ifdef CONFIG_BINFMT_AOUT
  70        init_aout_binfmt();
  71#endif
  72
  73#ifdef CONFIG_BINFMT_AOUT32
  74        init_aout32_binfmt();
  75#endif
  76
  77#ifdef CONFIG_BINFMT_JAVA
  78        init_java_binfmt();
  79#endif
  80
  81#ifdef CONFIG_BINFMT_EM86
  82        init_em86_binfmt();
  83#endif
  84
  85        /* This cannot be configured out of the kernel */
  86        init_script_binfmt();
  87}
  88
  89int register_binfmt(struct linux_binfmt * fmt)
  90{
  91        struct linux_binfmt ** tmp = &formats;
  92
  93        if (!fmt)
  94                return -EINVAL;
  95        if (fmt->next)
  96                return -EBUSY;
  97        while (*tmp) {
  98                if (fmt == *tmp)
  99                        return -EBUSY;
 100                tmp = &(*tmp)->next;
 101        }
 102        fmt->next = formats;
 103        formats = fmt;
 104        return 0;       
 105}
 106
 107#ifdef CONFIG_MODULES
 108int unregister_binfmt(struct linux_binfmt * fmt)
 109{
 110        struct linux_binfmt ** tmp = &formats;
 111
 112        while (*tmp) {
 113                if (fmt == *tmp) {
 114                        *tmp = fmt->next;
 115                        return 0;
 116                }
 117                tmp = &(*tmp)->next;
 118        }
 119        return -EINVAL;
 120}
 121#endif  /* CONFIG_MODULES */
 122
 123/* N.B. Error returns must be < 0 */
 124int open_dentry(struct dentry * dentry, int mode)
 125{
 126        struct inode * inode = dentry->d_inode;
 127        struct file * f;
 128        int fd, error;
 129
 130        error = -EINVAL;
 131        if (!inode->i_op || !inode->i_op->default_file_ops)
 132                goto out;
 133        fd = get_unused_fd();
 134        if (fd >= 0) {
 135                error = -ENFILE;
 136                f = get_empty_filp();
 137                if (!f)
 138                        goto out_fd;
 139                f->f_flags = mode;
 140                f->f_mode = (mode+1) & O_ACCMODE;
 141                f->f_dentry = dentry;
 142                f->f_pos = 0;
 143                f->f_reada = 0;
 144                f->f_op = inode->i_op->default_file_ops;
 145                if (f->f_op->open) {
 146                        error = f->f_op->open(inode,f);
 147                        if (error)
 148                                goto out_filp;
 149                }
 150                fd_install(fd, f);
 151                dget(dentry);
 152        }
 153        return fd;
 154
 155out_filp:
 156        if (error > 0)
 157                error = -EIO;
 158        put_filp(f);
 159out_fd:
 160        put_unused_fd(fd);
 161out:
 162        return error;
 163}
 164
 165/*
 166 * Note that a shared library must be both readable and executable due to
 167 * security reasons.
 168 *
 169 * Also note that we take the address to load from from the file itself.
 170 */
 171asmlinkage int sys_uselib(const char * library)
 172{
 173        int retval;
 174        struct file * file;
 175        struct linux_binfmt * fmt;
 176        char * tmp = getname(library);
 177
 178        lock_kernel();
 179        retval = PTR_ERR(tmp);
 180        if (IS_ERR(tmp))
 181                goto out;
 182
 183        file = filp_open(tmp, 0, 0);
 184        putname(tmp);
 185
 186        retval = PTR_ERR(file);
 187        if (IS_ERR(file))
 188                goto out;
 189
 190        retval = -EINVAL;
 191        if (!S_ISREG(file->f_dentry->d_inode->i_mode))
 192                goto out_fput;
 193
 194        retval = -ENOEXEC;
 195        if (file->f_op && file->f_op->read) {
 196                for (fmt = formats ; fmt ; fmt = fmt->next) {
 197                        int (*fn)(struct file *) = fmt->load_shlib;
 198                        if (!fn)
 199                                continue;
 200                        retval = fn(file);
 201                        if (retval != -ENOEXEC)
 202                                break;
 203                }
 204        }
 205out_fput:
 206        fput(file);
 207out:
 208        unlock_kernel();
 209        return retval;
 210}
 211
 212/*
 213 * count() counts the number of arguments/envelopes
 214 */
 215static int count(char ** argv, int max)
 216{
 217        int i = 0;
 218
 219        if (argv != NULL) {
 220                for (;;) {
 221                        char * p;
 222                        int error;
 223
 224                        error = get_user(p,argv);
 225                        if (error)
 226                                return error;
 227                        if (!p)
 228                                break;
 229                        argv++;
 230                        if (++i > max) return -E2BIG;
 231                }
 232        }
 233        return i;
 234}
 235
 236/*
 237 * 'copy_string()' copies argument/envelope strings from user
 238 * memory to free pages in kernel mem. These are in a format ready
 239 * to be put directly into the top of new user memory.
 240 *
 241 * Modified by TYT, 11/24/91 to add the from_kmem argument, which specifies
 242 * whether the string and the string array are from user or kernel segments:
 243 * 
 244 * from_kmem     argv *        argv **
 245 *    0          user space    user space
 246 *    1          kernel space  user space
 247 *    2          kernel space  kernel space
 248 * 
 249 * We do this by playing games with the fs segment register.  Since it
 250 * is expensive to load a segment register, we try to avoid calling
 251 * set_fs() unless we absolutely have to.
 252 */
 253unsigned long copy_strings(int argc,char ** argv,unsigned long *page,
 254                unsigned long p, int from_kmem)
 255{
 256        char *str;
 257        mm_segment_t old_fs;
 258
 259        if ((long)p <= 0)
 260                return p;       /* bullet-proofing */
 261        old_fs = get_fs();
 262        if (from_kmem==2)
 263                set_fs(KERNEL_DS);
 264        while (argc-- > 0) {
 265                int len;
 266                unsigned long pos;
 267
 268                if (from_kmem == 1)
 269                        set_fs(KERNEL_DS);
 270                get_user(str, argv+argc);
 271                if (!str)
 272                {
 273                        set_fs(old_fs);
 274                        return -EFAULT;
 275                }
 276                if (from_kmem == 1)
 277                        set_fs(old_fs);
 278                len = strnlen_user(str, p);     /* includes the '\0' */
 279                if (!len || len > p) {  /* EFAULT or E2BIG */
 280                        set_fs(old_fs);
 281                        return len ? -E2BIG : -EFAULT;
 282                }
 283                p -= len;
 284                pos = p;
 285                while (len>0) {
 286                        char *pag;
 287                        int offset, bytes_to_copy;
 288
 289                        offset = pos % PAGE_SIZE;
 290                        if (!(pag = (char *) page[pos/PAGE_SIZE]) &&
 291                            !(pag = (char *) page[pos/PAGE_SIZE] =
 292                              (unsigned long *) get_free_page(GFP_USER))) {
 293                                if (from_kmem==2)
 294                                        set_fs(old_fs);
 295                                return -EFAULT;
 296                        }
 297                        bytes_to_copy = PAGE_SIZE - offset;
 298                        if (bytes_to_copy > len)
 299                                bytes_to_copy = len;
 300                        copy_from_user(pag + offset, str, bytes_to_copy);
 301                        pos += bytes_to_copy;
 302                        str += bytes_to_copy;
 303                        len -= bytes_to_copy;
 304                }
 305        }
 306        if (from_kmem==2)
 307                set_fs(old_fs);
 308        return p;
 309}
 310
 311unsigned long setup_arg_pages(unsigned long p, struct linux_binprm * bprm)
 312{
 313        unsigned long stack_base;
 314        struct vm_area_struct *mpnt;
 315        int i;
 316
 317        stack_base = STACK_TOP - MAX_ARG_PAGES*PAGE_SIZE;
 318
 319        p += stack_base;
 320        if (bprm->loader)
 321                bprm->loader += stack_base;
 322        bprm->exec += stack_base;
 323
 324        mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
 325        if (mpnt) {
 326                mpnt->vm_mm = current->mm;
 327                mpnt->vm_start = PAGE_MASK & (unsigned long) p;
 328                mpnt->vm_end = STACK_TOP;
 329                mpnt->vm_page_prot = PAGE_COPY;
 330                mpnt->vm_flags = VM_STACK_FLAGS;
 331                mpnt->vm_ops = NULL;
 332                mpnt->vm_offset = 0;
 333                mpnt->vm_file = NULL;
 334                mpnt->vm_pte = 0;
 335                insert_vm_struct(current->mm, mpnt);
 336                current->mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
 337        }
 338
 339        for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
 340                if (bprm->page[i]) {
 341                        current->mm->rss++;
 342                        put_dirty_page(current,bprm->page[i],stack_base);
 343                }
 344                stack_base += PAGE_SIZE;
 345        }
 346        return p;
 347}
 348
 349/*
 350 * Read in the complete executable. This is used for "-N" files
 351 * that aren't on a block boundary, and for files on filesystems
 352 * without bmap support.
 353 */
 354int read_exec(struct dentry *dentry, unsigned long offset,
 355        char * addr, unsigned long count, int to_kmem)
 356{
 357        struct file file;
 358        struct inode * inode = dentry->d_inode;
 359        int result = -ENOEXEC;
 360
 361        if (!inode->i_op || !inode->i_op->default_file_ops)
 362                goto end_readexec;
 363        if (init_private_file(&file, dentry, 1))
 364                goto end_readexec;
 365        if (!file.f_op->read)
 366                goto close_readexec;
 367        if (file.f_op->llseek) {
 368                if (file.f_op->llseek(&file,offset,0) != offset)
 369                        goto close_readexec;
 370        } else
 371                file.f_pos = offset;
 372        if (to_kmem) {
 373                mm_segment_t old_fs = get_fs();
 374                set_fs(get_ds());
 375                result = file.f_op->read(&file, addr, count, &file.f_pos);
 376                set_fs(old_fs);
 377        } else {
 378                result = verify_area(VERIFY_WRITE, addr, count);
 379                if (result)
 380                        goto close_readexec;
 381                result = file.f_op->read(&file, addr, count, &file.f_pos);
 382        }
 383close_readexec:
 384        if (file.f_op->release)
 385                file.f_op->release(inode,&file);
 386end_readexec:
 387        return result;
 388}
 389
 390static int exec_mmap(void)
 391{
 392        struct mm_struct * mm, * old_mm;
 393        int retval, nr;
 394
 395        if (atomic_read(&current->mm->count) == 1) {
 396                flush_cache_mm(current->mm);
 397                mm_release();
 398                release_segments(current->mm);
 399                exit_mmap(current->mm);
 400                flush_tlb_mm(current->mm);
 401                return 0;
 402        }
 403
 404        retval = -ENOMEM;
 405        mm = mm_alloc();
 406        if (!mm)
 407                goto fail_nomem;
 408
 409        mm->cpu_vm_mask = (1UL << smp_processor_id());
 410        mm->total_vm = 0;
 411        mm->rss = 0;
 412        /*
 413         * Make sure we have a private ldt if needed ...
 414         */
 415        nr = current->tarray_ptr - &task[0]; 
 416        copy_segments(nr, current, mm);
 417
 418        old_mm = current->mm;
 419        current->mm = mm;
 420        retval = new_page_tables(current);
 421        if (retval)
 422                goto fail_restore;
 423        activate_context(current);
 424        up(&mm->mmap_sem);
 425        mm_release();
 426        mmput(old_mm);
 427        return 0;
 428
 429        /*
 430         * Failure ... restore the prior mm_struct.
 431         */
 432fail_restore:
 433        /* The pgd belongs to the parent ... don't free it! */
 434        mm->pgd = NULL;
 435        current->mm = old_mm;
 436        /* restore the ldt for this task */
 437        copy_segments(nr, current, NULL);
 438        mmput(mm);
 439
 440fail_nomem:
 441        return retval;
 442}
 443
 444/*
 445 * This function makes sure the current process has its own signal table,
 446 * so that flush_signal_handlers can later reset the handlers without
 447 * disturbing other processes.  (Other processes might share the signal
 448 * table via the CLONE_SIGHAND option to clone().)
 449 */
 450 
 451static inline int make_private_signals(void)
 452{
 453        struct signal_struct * newsig;
 454
 455        if (atomic_read(&current->sig->count) <= 1)
 456                return 0;
 457        newsig = kmalloc(sizeof(*newsig), GFP_KERNEL);
 458        if (newsig == NULL)
 459                return -ENOMEM;
 460        spin_lock_init(&newsig->siglock);
 461        atomic_set(&newsig->count, 1);
 462        memcpy(newsig->action, current->sig->action, sizeof(newsig->action));
 463        current->sig = newsig;
 464        return 0;
 465}
 466        
 467/*
 468 * If make_private_signals() made a copy of the signal table, decrement the
 469 * refcount of the original table, and free it if necessary.
 470 * We don't do that in make_private_signals() so that we can back off
 471 * in flush_old_exec() if an error occurs after calling make_private_signals().
 472 */
 473
 474static inline void release_old_signals(struct signal_struct * oldsig)
 475{
 476        if (current->sig == oldsig)
 477                return;
 478        if (atomic_dec_and_test(&oldsig->count))
 479                kfree(oldsig);
 480}
 481
 482/*
 483 * These functions flushes out all traces of the currently running executable
 484 * so that a new one can be started
 485 */
 486
 487static inline void flush_old_files(struct files_struct * files)
 488{
 489        unsigned long j;
 490
 491        j = 0;
 492        for (;;) {
 493                unsigned long set, i;
 494
 495                i = j * __NFDBITS;
 496                if (i >= files->max_fds || i >= files->max_fdset)
 497                        break;
 498                set = files->close_on_exec->fds_bits[j];
 499                files->close_on_exec->fds_bits[j] = 0;
 500                j++;
 501                for ( ; set ; i++,set >>= 1) {
 502                        if (set & 1)
 503                                sys_close(i);
 504                }
 505        }
 506}
 507
 508int flush_old_exec(struct linux_binprm * bprm)
 509{
 510        char * name;
 511        int i, ch, retval;
 512        struct signal_struct * oldsig;
 513
 514        /*
 515         * Make sure we have a private signal table
 516         */
 517        oldsig = current->sig;
 518        retval = make_private_signals();
 519        if (retval) goto flush_failed;
 520
 521        /* 
 522         * Release all of the old mmap stuff
 523         */
 524        retval = exec_mmap();
 525        if (retval) goto mmap_failed;
 526
 527        /* This is the point of no return */
 528        release_old_signals(oldsig);
 529
 530        current->sas_ss_sp = current->sas_ss_size = 0;
 531
 532        bprm->dumpable = 0;
 533        if (current->euid == current->uid && current->egid == current->gid)
 534                bprm->dumpable = !bprm->priv_change;
 535        else
 536                current->dumpable = 0;
 537        name = bprm->filename;
 538        for (i=0; (ch = *(name++)) != '\0';) {
 539                if (ch == '/')
 540                        i = 0;
 541                else
 542                        if (i < 15)
 543                                current->comm[i++] = ch;
 544        }
 545        current->comm[i] = '\0';
 546
 547        flush_thread();
 548
 549        if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
 550            permission(bprm->dentry->d_inode, MAY_READ)) {
 551                bprm->dumpable = 0;
 552                current->dumpable = 0;
 553        }
 554
 555        current->self_exec_id++;
 556
 557        flush_signal_handlers(current);
 558        flush_old_files(current->files);
 559
 560        return 0;
 561
 562mmap_failed:
 563        if (current->sig != oldsig)
 564                kfree(current->sig);
 565flush_failed:
 566        current->sig = oldsig;
 567        return retval;
 568}
 569
 570/*
 571 * We mustn't allow tracing of suid binaries, no matter what.
 572 */
 573static inline int must_not_trace_exec(struct task_struct * p)
 574{
 575        return (p->ptrace & PT_PTRACED);
 576}
 577
 578/* 
 579 * Fill the binprm structure from the inode. 
 580 * Check permissions, then read the first 128 bytes
 581 */
 582int prepare_binprm(struct linux_binprm *bprm)
 583{
 584        int mode;
 585        int retval,id_change,cap_raised;
 586        struct inode * inode = bprm->dentry->d_inode;
 587
 588        mode = inode->i_mode;
 589        if (!S_ISREG(mode))                     /* must be regular file */
 590                return -EACCES;
 591        if (!(mode & 0111))                     /* with at least _one_ execute bit set */
 592                return -EACCES;
 593        if (IS_NOEXEC(inode))                   /* FS mustn't be mounted noexec */
 594                return -EACCES;
 595        if (!inode->i_sb)
 596                return -EACCES;
 597        if ((retval = permission(inode, MAY_EXEC)) != 0)
 598                return retval;
 599        /* better not execute files which are being written to */
 600        if (inode->i_writecount > 0)
 601                return -ETXTBSY;
 602
 603        bprm->e_uid = current->euid;
 604        bprm->e_gid = current->egid;
 605        id_change = cap_raised = 0;
 606
 607        /* Set-uid? */
 608        if (mode & S_ISUID) {
 609                bprm->e_uid = inode->i_uid;
 610                if (bprm->e_uid != current->euid)
 611                        id_change = 1;
 612        }
 613
 614        /* Set-gid? */
 615        /*
 616         * If setgid is set but no group execute bit then this
 617         * is a candidate for mandatory locking, not a setgid
 618         * executable.
 619         */
 620        if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
 621                bprm->e_gid = inode->i_gid;
 622                if (!in_group_p(bprm->e_gid))
 623                        id_change = 1;
 624        }
 625
 626        /* We don't have VFS support for capabilities yet */
 627        cap_clear(bprm->cap_inheritable);
 628        cap_clear(bprm->cap_permitted);
 629        cap_clear(bprm->cap_effective);
 630
 631        /*  To support inheritance of root-permissions and suid-root
 632         *  executables under compatibility mode, we raise all three
 633         *  capability sets for the file.
 634         *
 635         *  If only the real uid is 0, we only raise the inheritable
 636         *  and permitted sets of the executable file.
 637         */
 638
 639        if (!issecure(SECURE_NOROOT)) {
 640                if (bprm->e_uid == 0 || current->uid == 0) {
 641                        cap_set_full(bprm->cap_inheritable);
 642                        cap_set_full(bprm->cap_permitted);
 643                }
 644                if (bprm->e_uid == 0) 
 645                        cap_set_full(bprm->cap_effective);
 646        }
 647
 648        /* Only if pP' is _not_ a subset of pP, do we consider there
 649         * has been a capability related "change of capability".  In
 650         * such cases, we need to check that the elevation of
 651         * privilege does not go against other system constraints.
 652         * The new Permitted set is defined below -- see (***). */
 653        {
 654                kernel_cap_t permitted, working;
 655
 656                permitted = cap_intersect(bprm->cap_permitted, cap_bset);
 657                working = cap_intersect(bprm->cap_inheritable,
 658                                        current->cap_inheritable);
 659                working = cap_combine(permitted, working);
 660                if (!cap_issubset(working, current->cap_permitted)) {
 661                        cap_raised = 1;
 662                }
 663        }
 664
 665        bprm->priv_change = id_change || cap_raised;
 666        if (bprm->priv_change) {
 667                current->dumpable = 0;
 668                /* We can't suid-execute if we're sharing parts of the executable */
 669                /* or if we're being traced (or if suid execs are not allowed)    */
 670                /* (current->mm->count > 1 is ok, as we'll get a new mm anyway)   */
 671                if (IS_NOSUID(inode)
 672                    || must_not_trace_exec(current)
 673                    || (atomic_read(&current->fs->count) > 1)
 674                    || (atomic_read(&current->sig->count) > 1)
 675                    || (atomic_read(&current->files->count) > 1)) {
 676                        if (id_change && !capable(CAP_SETUID))
 677                                return -EPERM;
 678                        if (cap_raised && !capable(CAP_SETPCAP))
 679                                return -EPERM;
 680                }
 681        }
 682
 683        memset(bprm->buf,0,sizeof(bprm->buf));
 684        return read_exec(bprm->dentry,0,bprm->buf,128,1);
 685}
 686
 687/*
 688 * This function is used to produce the new IDs and capabilities
 689 * from the old ones and the file's capabilities.
 690 *
 691 * The formula used for evolving capabilities is:
 692 *
 693 *       pI' = pI
 694 * (***) pP' = (fP & X) | (fI & pI)
 695 *       pE' = pP' & fE          [NB. fE is 0 or ~0]
 696 *
 697 * I=Inheritable, P=Permitted, E=Effective // p=process, f=file
 698 * ' indicates post-exec(), and X is the global 'cap_bset'.
 699 */
 700
 701void compute_creds(struct linux_binprm *bprm) 
 702{
 703        kernel_cap_t new_permitted, working;
 704
 705        new_permitted = cap_intersect(bprm->cap_permitted, cap_bset);
 706        working = cap_intersect(bprm->cap_inheritable,
 707                                current->cap_inheritable);
 708        new_permitted = cap_combine(new_permitted, working);
 709
 710        /* For init, we want to retain the capabilities set
 711         * in the init_task struct. Thus we skip the usual
 712         * capability rules */
 713        if (current->pid != 1) {
 714                current->cap_permitted = new_permitted;
 715                current->cap_effective =
 716                        cap_intersect(new_permitted, bprm->cap_effective);
 717        }
 718        
 719        /* AUD: Audit candidate if current->cap_effective is set */
 720
 721        current->suid = current->euid = current->fsuid = bprm->e_uid;
 722        current->sgid = current->egid = current->fsgid = bprm->e_gid;
 723        if (current->euid != current->uid || current->egid != current->gid ||
 724            !cap_issubset(new_permitted, current->cap_permitted)) {
 725                bprm->dumpable = 0;
 726                current->dumpable = 0;
 727        }
 728
 729        current->keep_capabilities = 0;
 730}
 731
 732
 733void remove_arg_zero(struct linux_binprm *bprm)
 734{
 735        if (bprm->argc) {
 736                unsigned long offset;
 737                char * page;
 738                offset = bprm->p % PAGE_SIZE;
 739                page = (char*)bprm->page[bprm->p/PAGE_SIZE];
 740                while(bprm->p++,*(page+offset++))
 741                        if(offset==PAGE_SIZE){
 742                                offset=0;
 743                                page = (char*)bprm->page[bprm->p/PAGE_SIZE];
 744                        }
 745                bprm->argc--;
 746        }
 747}
 748
 749/*
 750 * cycle the list of binary formats handler, until one recognizes the image
 751 */
 752int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
 753{
 754        int try,retval=0;
 755        struct linux_binfmt *fmt;
 756#ifdef __alpha__
 757        /* handle /sbin/loader.. */
 758        {
 759            struct exec * eh = (struct exec *) bprm->buf;
 760            struct linux_binprm bprm_loader;
 761
 762            if (!bprm->loader && eh->fh.f_magic == 0x183 &&
 763                (eh->fh.f_flags & 0x3000) == 0x3000)
 764            {
 765                int i;
 766                char * dynloader[] = { "/sbin/loader" };
 767                struct dentry * dentry;
 768
 769                dput(bprm->dentry);
 770                bprm->dentry = NULL;
 771
 772                bprm_loader.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
 773                for (i=0 ; i<MAX_ARG_PAGES ; i++)       /* clear page-table */
 774                    bprm_loader.page[i] = 0;
 775
 776                dentry = open_namei(dynloader[0], 0, 0);
 777                retval = PTR_ERR(dentry);
 778                if (IS_ERR(dentry))
 779                        return retval;
 780                bprm->dentry = dentry;
 781                bprm->loader = bprm_loader.p;
 782                retval = prepare_binprm(bprm);
 783                if (retval<0)
 784                        return retval;
 785                /* should call search_binary_handler recursively here,
 786                   but it does not matter */
 787            }
 788        }
 789#endif
 790        /*
 791         * kernel module loader fixup 
 792         * We don't try to load run modprobe in kernel space but at the
 793         * same time kernel/kmod.c calls us with fs set to KERNEL_DS. This
 794         * would cause us to explode messily on a split address space machine
 795         * and its sort of lucky it ever worked before. Since the S/390 is
 796         * such a split address space box we have to fix it..
 797         */
 798         
 799        set_fs(USER_DS);
 800
 801        for (try=0; try<2; try++) {
 802                for (fmt = formats ; fmt ; fmt = fmt->next) {
 803                        int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
 804                        if (!fn)
 805                                continue;
 806                        retval = fn(bprm, regs);
 807                        if (retval >= 0) {
 808                                if (bprm->dentry)
 809                                        dput(bprm->dentry);
 810                                bprm->dentry = NULL;
 811                                current->did_exec = 1;
 812                                return retval;
 813                        }
 814                        if (retval != -ENOEXEC)
 815                                break;
 816                        if (!bprm->dentry) /* We don't have the dentry anymore */
 817                                return retval;
 818                }
 819                if (retval != -ENOEXEC) {
 820                        break;
 821#ifdef CONFIG_KMOD
 822                }else{
 823#define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
 824                        char modname[20];
 825                        if (printable(bprm->buf[0]) &&
 826                            printable(bprm->buf[1]) &&
 827                            printable(bprm->buf[2]) &&
 828                            printable(bprm->buf[3]))
 829                                break; /* -ENOEXEC */
 830                        sprintf(modname, "binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
 831                        request_module(modname);
 832#endif
 833                }
 834        }
 835        return retval;
 836}
 837
 838
 839/*
 840 * sys_execve() executes a new program.
 841 */
 842int do_execve(char * filename, char ** argv, char ** envp, struct pt_regs * regs)
 843{
 844        struct linux_binprm bprm;
 845        struct dentry * dentry;
 846        int was_dumpable;
 847        int retval;
 848        int i;
 849
 850        bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
 851        for (i=0 ; i<MAX_ARG_PAGES ; i++)       /* clear page-table */
 852                bprm.page[i] = 0;
 853
 854        dentry = open_namei(filename, 0, 0);
 855        retval = PTR_ERR(dentry);
 856        if (IS_ERR(dentry))
 857                return retval;
 858
 859        bprm.dentry = dentry;
 860        bprm.filename = filename;
 861        bprm.sh_bang = 0;
 862        bprm.java = 0;
 863        bprm.loader = 0;
 864        bprm.exec = 0;
 865        if ((bprm.argc = count(argv, bprm.p / sizeof(void *))) < 0) {
 866                dput(dentry);
 867                return bprm.argc;
 868        }
 869
 870        if ((bprm.envc = count(envp, bprm.p / sizeof(void *))) < 0) {
 871                dput(dentry);
 872                return bprm.envc;
 873        }
 874
 875        was_dumpable = current->dumpable;
 876        current->dumpable = 0;
 877
 878        retval = prepare_binprm(&bprm);
 879        
 880        if (retval >= 0) {
 881                bprm.p = copy_strings(1, &bprm.filename, bprm.page, bprm.p, 2);
 882                bprm.exec = bprm.p;
 883                bprm.p = copy_strings(bprm.envc,envp,bprm.page,bprm.p,0);
 884                bprm.p = copy_strings(bprm.argc,argv,bprm.page,bprm.p,0);
 885                if ((long)bprm.p < 0)
 886                        retval = (long)bprm.p;
 887        }
 888
 889        if (retval >= 0)
 890                retval = search_binary_handler(&bprm,regs);
 891
 892        if (retval >= 0) {
 893                /* execve success */
 894                current->dumpable = bprm.dumpable;
 895                return retval;
 896        }
 897
 898        /* Something went wrong, return the inode and free the argument pages*/
 899        if (bprm.dentry)
 900                dput(bprm.dentry);
 901
 902        for (i=0 ; i<MAX_ARG_PAGES ; i++)
 903                free_page(bprm.page[i]);
 904
 905        current->dumpable = was_dumpable;
 906
 907        return retval;
 908}
 909
 910void set_binfmt(struct linux_binfmt *new)
 911{
 912        struct linux_binfmt *old = current->binfmt;
 913        if (new && new->module)
 914                __MOD_INC_USE_COUNT(new->module);
 915        current->binfmt = new;
 916        if (old && old->module)
 917                __MOD_DEC_USE_COUNT(old->module);
 918}
 919
 920int do_coredump(long signr, struct pt_regs * regs)
 921{
 922        struct linux_binfmt *binfmt;
 923        char corename[6+sizeof(current->comm)];
 924        struct file * file;
 925        struct inode * inode;
 926
 927        lock_kernel();
 928        binfmt = current->binfmt;
 929        if (!binfmt || !binfmt->core_dump)
 930                goto fail;
 931        if (!current->dumpable || atomic_read(&current->mm->count) != 1)
 932                goto fail;
 933        if (current->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
 934                goto fail;
 935        current->dumpable = 0;
 936
 937        memcpy(corename,"core.", 5);
 938#if 0
 939        memcpy(corename+5,current->comm,sizeof(current->comm));
 940#else
 941        corename[4] = '\0';
 942#endif
 943        file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW, 0600);
 944        if (IS_ERR(file))
 945                goto fail;
 946        inode = file->f_dentry->d_inode;
 947        if (inode->i_nlink > 1)
 948                goto close_fail;        /* multiple links - don't dump */
 949        if (list_empty(&file->f_dentry->d_hash))
 950                goto close_fail;
 951
 952        if (!S_ISREG(inode->i_mode))
 953                goto close_fail;
 954        if (!file->f_op)
 955                goto close_fail;
 956        if (!file->f_op->write)
 957                goto close_fail;
 958        if (do_truncate(file->f_dentry, 0) != 0)
 959                goto close_fail;
 960        if (!binfmt->core_dump(signr, regs, file))
 961                goto close_fail;
 962        filp_close(file, NULL);
 963        unlock_kernel();
 964        return 1;
 965
 966close_fail:
 967        filp_close(file, NULL);
 968fail:
 969        unlock_kernel();
 970        return 0;
 971}
 972
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.