RHEL5/fs/binfmt_elf_fdpic.c
<<
>>
Prefs
   1/* binfmt_elf_fdpic.c: FDPIC ELF binary format
   2 *
   3 * Copyright (C) 2003, 2004, 2006 Red Hat, Inc. All Rights Reserved.
   4 * Written by David Howells (dhowells@redhat.com)
   5 * Derived from binfmt_elf.c
   6 *
   7 * This program is free software; you can redistribute it and/or
   8 * modify it under the terms of the GNU General Public License
   9 * as published by the Free Software Foundation; either version
  10 * 2 of the License, or (at your option) any later version.
  11 */
  12
  13#include <linux/module.h>
  14
  15#include <linux/fs.h>
  16#include <linux/stat.h>
  17#include <linux/sched.h>
  18#include <linux/mm.h>
  19#include <linux/mman.h>
  20#include <linux/errno.h>
  21#include <linux/signal.h>
  22#include <linux/binfmts.h>
  23#include <linux/string.h>
  24#include <linux/file.h>
  25#include <linux/fcntl.h>
  26#include <linux/slab.h>
  27#include <linux/pagemap.h>
  28#include <linux/highmem.h>
  29#include <linux/highuid.h>
  30#include <linux/personality.h>
  31#include <linux/ptrace.h>
  32#include <linux/init.h>
  33#include <linux/smp_lock.h>
  34#include <linux/elf.h>
  35#include <linux/elf-fdpic.h>
  36#include <linux/elfcore.h>
  37
  38#include <asm/uaccess.h>
  39#include <asm/param.h>
  40#include <asm/pgalloc.h>
  41
  42typedef char *elf_caddr_t;
  43#ifndef elf_addr_t
  44#define elf_addr_t unsigned long
  45#endif
  46
  47#if 0
  48#define kdebug(fmt, ...) printk("FDPIC "fmt"\n" ,##__VA_ARGS__ )
  49#else
  50#define kdebug(fmt, ...) do {} while(0)
  51#endif
  52
  53#if 0
  54#define kdcore(fmt, ...) printk("FDPIC "fmt"\n" ,##__VA_ARGS__ )
  55#else
  56#define kdcore(fmt, ...) do {} while(0)
  57#endif
  58
  59MODULE_LICENSE("GPL");
  60
  61static int load_elf_fdpic_binary(struct linux_binprm *, struct pt_regs *);
  62static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *, struct file *);
  63static int elf_fdpic_map_file(struct elf_fdpic_params *, struct file *,
  64                              struct mm_struct *, const char *);
  65
  66static int create_elf_fdpic_tables(struct linux_binprm *, struct mm_struct *,
  67                                   struct elf_fdpic_params *,
  68                                   struct elf_fdpic_params *);
  69
  70#ifndef CONFIG_MMU
  71static int elf_fdpic_transfer_args_to_stack(struct linux_binprm *,
  72                                            unsigned long *);
  73static int elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params *,
  74                                                   struct file *,
  75                                                   struct mm_struct *);
  76#endif
  77
  78static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *,
  79                                             struct file *, struct mm_struct *);
  80
  81#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
  82static int elf_fdpic_core_dump(long, struct pt_regs *, struct file *);
  83#endif
  84
  85static struct linux_binfmt elf_fdpic_format = {
  86        .module         = THIS_MODULE,
  87        .load_binary    = load_elf_fdpic_binary,
  88#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
  89        .core_dump      = elf_fdpic_core_dump,
  90#endif
  91        .min_coredump   = ELF_EXEC_PAGESIZE,
  92};
  93
  94static int __init init_elf_fdpic_binfmt(void)
  95{
  96        return register_binfmt(&elf_fdpic_format);
  97}
  98
  99static void __exit exit_elf_fdpic_binfmt(void)
 100{
 101        unregister_binfmt(&elf_fdpic_format);
 102}
 103
 104core_initcall(init_elf_fdpic_binfmt);
 105module_exit(exit_elf_fdpic_binfmt);
 106
 107static int is_elf_fdpic(struct elfhdr *hdr, struct file *file)
 108{
 109        if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0)
 110                return 0;
 111        if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)
 112                return 0;
 113        if (!elf_check_arch(hdr) || !elf_check_fdpic(hdr))
 114                return 0;
 115        if (!file->f_op || !file->f_op->mmap)
 116                return 0;
 117        return 1;
 118}
 119
 120/*****************************************************************************/
 121/*
 122 * read the program headers table into memory
 123 */
 124static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params,
 125                                 struct file *file)
 126{
 127        struct elf32_phdr *phdr;
 128        unsigned long size;
 129        int retval, loop;
 130
 131        if (params->hdr.e_phentsize != sizeof(struct elf_phdr))
 132                return -ENOMEM;
 133        if (params->hdr.e_phnum > 65536U / sizeof(struct elf_phdr))
 134                return -ENOMEM;
 135
 136        size = params->hdr.e_phnum * sizeof(struct elf_phdr);
 137        params->phdrs = kmalloc(size, GFP_KERNEL);
 138        if (!params->phdrs)
 139                return -ENOMEM;
 140
 141        retval = kernel_read(file, params->hdr.e_phoff,
 142                             (char *) params->phdrs, size);
 143        if (retval < 0)
 144                return retval;
 145
 146        /* determine stack size for this binary */
 147        phdr = params->phdrs;
 148        for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
 149                if (phdr->p_type != PT_GNU_STACK)
 150                        continue;
 151
 152                if (phdr->p_flags & PF_X)
 153                        params->flags |= ELF_FDPIC_FLAG_EXEC_STACK;
 154                else
 155                        params->flags |= ELF_FDPIC_FLAG_NOEXEC_STACK;
 156
 157                params->stack_size = phdr->p_memsz;
 158                break;
 159        }
 160
 161        return 0;
 162}
 163
 164/*****************************************************************************/
 165/*
 166 * load an fdpic binary into various bits of memory
 167 */
 168static int load_elf_fdpic_binary(struct linux_binprm *bprm,
 169                                 struct pt_regs *regs)
 170{
 171        struct elf_fdpic_params exec_params, interp_params;
 172        struct elf_phdr *phdr;
 173        unsigned long stack_size, entryaddr;
 174#ifndef CONFIG_MMU
 175        unsigned long fullsize;
 176#endif
 177#ifdef ELF_FDPIC_PLAT_INIT
 178        unsigned long dynaddr;
 179#endif
 180        struct file *interpreter = NULL; /* to shut gcc up */
 181        char *interpreter_name = NULL;
 182        int executable_stack;
 183        int retval, i;
 184
 185        memset(&exec_params, 0, sizeof(exec_params));
 186        memset(&interp_params, 0, sizeof(interp_params));
 187
 188        exec_params.hdr = *(struct elfhdr *) bprm->buf;
 189        exec_params.flags = ELF_FDPIC_FLAG_PRESENT | ELF_FDPIC_FLAG_EXECUTABLE;
 190
 191        /* check that this is a binary we know how to deal with */
 192        retval = -ENOEXEC;
 193        if (!is_elf_fdpic(&exec_params.hdr, bprm->file))
 194                goto error;
 195
 196        /* read the program header table */
 197        retval = elf_fdpic_fetch_phdrs(&exec_params, bprm->file);
 198        if (retval < 0)
 199                goto error;
 200
 201        /* scan for a program header that specifies an interpreter */
 202        phdr = exec_params.phdrs;
 203
 204        for (i = 0; i < exec_params.hdr.e_phnum; i++, phdr++) {
 205                switch (phdr->p_type) {
 206                case PT_INTERP:
 207                        retval = -ENOMEM;
 208                        if (phdr->p_filesz > PATH_MAX)
 209                                goto error;
 210                        retval = -ENOENT;
 211                        if (phdr->p_filesz < 2)
 212                                goto error;
 213
 214                        /* read the name of the interpreter into memory */
 215                        interpreter_name = kmalloc(phdr->p_filesz, GFP_KERNEL);
 216                        if (!interpreter_name)
 217                                goto error;
 218
 219                        retval = kernel_read(bprm->file,
 220                                             phdr->p_offset,
 221                                             interpreter_name,
 222                                             phdr->p_filesz);
 223                        if (retval < 0)
 224                                goto error;
 225
 226                        retval = -ENOENT;
 227                        if (interpreter_name[phdr->p_filesz - 1] != '\0')
 228                                goto error;
 229
 230                        kdebug("Using ELF interpreter %s", interpreter_name);
 231
 232                        /* replace the program with the interpreter */
 233                        interpreter = open_exec(interpreter_name);
 234                        retval = PTR_ERR(interpreter);
 235                        if (IS_ERR(interpreter)) {
 236                                interpreter = NULL;
 237                                goto error;
 238                        }
 239
 240                        retval = kernel_read(interpreter, 0, bprm->buf,
 241                                             BINPRM_BUF_SIZE);
 242                        if (retval < 0)
 243                                goto error;
 244
 245                        interp_params.hdr = *((struct elfhdr *) bprm->buf);
 246                        break;
 247
 248                case PT_LOAD:
 249#ifdef CONFIG_MMU
 250                        if (exec_params.load_addr == 0)
 251                                exec_params.load_addr = phdr->p_vaddr;
 252#endif
 253                        break;
 254                }
 255
 256        }
 257
 258        if (elf_check_const_displacement(&exec_params.hdr))
 259                exec_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
 260
 261        /* perform insanity checks on the interpreter */
 262        if (interpreter_name) {
 263                retval = -ELIBBAD;
 264                if (!is_elf_fdpic(&interp_params.hdr, interpreter))
 265                        goto error;
 266
 267                interp_params.flags = ELF_FDPIC_FLAG_PRESENT;
 268
 269                /* read the interpreter's program header table */
 270                retval = elf_fdpic_fetch_phdrs(&interp_params, interpreter);
 271                if (retval < 0)
 272                        goto error;
 273        }
 274
 275        stack_size = exec_params.stack_size;
 276        if (stack_size < interp_params.stack_size)
 277                stack_size = interp_params.stack_size;
 278
 279        if (exec_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
 280                executable_stack = EXSTACK_ENABLE_X;
 281        else if (exec_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
 282                executable_stack = EXSTACK_DISABLE_X;
 283        else if (interp_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
 284                executable_stack = EXSTACK_ENABLE_X;
 285        else if (interp_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
 286                executable_stack = EXSTACK_DISABLE_X;
 287        else
 288                executable_stack = EXSTACK_DEFAULT;
 289
 290        retval = -ENOEXEC;
 291        if (stack_size == 0)
 292                goto error;
 293
 294        if (elf_check_const_displacement(&interp_params.hdr))
 295                interp_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
 296
 297        /* flush all traces of the currently running executable */
 298        retval = flush_old_exec(bprm);
 299        if (retval)
 300                goto error;
 301
 302        /* there's now no turning back... the old userspace image is dead,
 303         * defunct, deceased, etc. after this point we have to exit via
 304         * error_kill */
 305        set_personality(PER_LINUX_FDPIC);
 306        set_binfmt(&elf_fdpic_format);
 307
 308        current->mm->start_code = 0;
 309        current->mm->end_code = 0;
 310        current->mm->start_stack = 0;
 311        current->mm->start_data = 0;
 312        current->mm->end_data = 0;
 313        current->mm->context.exec_fdpic_loadmap = 0;
 314        current->mm->context.interp_fdpic_loadmap = 0;
 315
 316        current->flags &= ~PF_FORKNOEXEC;
 317
 318#ifdef CONFIG_MMU
 319        elf_fdpic_arch_lay_out_mm(&exec_params,
 320                                  &interp_params,
 321                                  &current->mm->start_stack,
 322                                  &current->mm->start_brk);
 323
 324        retval = setup_arg_pages(bprm, current->mm->start_stack,
 325                                 executable_stack);
 326        if (retval < 0) {
 327                send_sig(SIGKILL, current, 0);
 328                goto error_kill;
 329        }
 330#endif
 331
 332        /* load the executable and interpreter into memory */
 333        retval = elf_fdpic_map_file(&exec_params, bprm->file, current->mm,
 334                                    "executable");
 335        if (retval < 0)
 336                goto error_kill;
 337
 338        if (interpreter_name) {
 339                retval = elf_fdpic_map_file(&interp_params, interpreter,
 340                                            current->mm, "interpreter");
 341                if (retval < 0) {
 342                        printk(KERN_ERR "Unable to load interpreter\n");
 343                        goto error_kill;
 344                }
 345
 346                allow_write_access(interpreter);
 347                fput(interpreter);
 348                interpreter = NULL;
 349        }
 350
 351#ifdef CONFIG_MMU
 352        if (!current->mm->start_brk)
 353                current->mm->start_brk = current->mm->end_data;
 354
 355        current->mm->brk = current->mm->start_brk =
 356                PAGE_ALIGN(current->mm->start_brk);
 357
 358#else
 359        /* create a stack and brk area big enough for everyone
 360         * - the brk heap starts at the bottom and works up
 361         * - the stack starts at the top and works down
 362         */
 363        stack_size = (stack_size + PAGE_SIZE - 1) & PAGE_MASK;
 364        if (stack_size < PAGE_SIZE * 2)
 365                stack_size = PAGE_SIZE * 2;
 366
 367        down_write(&current->mm->mmap_sem);
 368        current->mm->start_brk = do_mmap(NULL, 0, stack_size,
 369                                         PROT_READ | PROT_WRITE | PROT_EXEC,
 370                                         MAP_PRIVATE | MAP_ANON | MAP_GROWSDOWN,
 371                                         0);
 372
 373        if (IS_ERR_VALUE(current->mm->start_brk)) {
 374                up_write(&current->mm->mmap_sem);
 375                retval = current->mm->start_brk;
 376                current->mm->start_brk = 0;
 377                goto error_kill;
 378        }
 379
 380        /* expand the stack mapping to use up the entire allocation granule */
 381        fullsize = ksize((char *) current->mm->start_brk);
 382        if (!IS_ERR_VALUE(do_mremap(current->mm->start_brk, stack_size,
 383                                    fullsize, 0, 0)))
 384                stack_size = fullsize;
 385        up_write(&current->mm->mmap_sem);
 386
 387        current->mm->brk = current->mm->start_brk;
 388        current->mm->context.end_brk = current->mm->start_brk;
 389        current->mm->context.end_brk +=
 390                (stack_size > PAGE_SIZE) ? (stack_size - PAGE_SIZE) : 0;
 391        current->mm->start_stack = current->mm->start_brk + stack_size;
 392#endif
 393
 394        compute_creds(bprm);
 395        current->flags &= ~PF_FORKNOEXEC;
 396        if (create_elf_fdpic_tables(bprm, current->mm,
 397                                    &exec_params, &interp_params) < 0)
 398                goto error_kill;
 399
 400        kdebug("- start_code  %lx", current->mm->start_code);
 401        kdebug("- end_code    %lx", current->mm->end_code);
 402        kdebug("- start_data  %lx", current->mm->start_data);
 403        kdebug("- end_data    %lx", current->mm->end_data);
 404        kdebug("- start_brk   %lx", current->mm->start_brk);
 405        kdebug("- brk         %lx", current->mm->brk);
 406        kdebug("- start_stack %lx", current->mm->start_stack);
 407
 408#ifdef ELF_FDPIC_PLAT_INIT
 409        /*
 410         * The ABI may specify that certain registers be set up in special
 411         * ways (on i386 %edx is the address of a DT_FINI function, for
 412         * example.  This macro performs whatever initialization to
 413         * the regs structure is required.
 414         */
 415        dynaddr = interp_params.dynamic_addr ?: exec_params.dynamic_addr;
 416        ELF_FDPIC_PLAT_INIT(regs, exec_params.map_addr, interp_params.map_addr,
 417                            dynaddr);
 418#endif
 419
 420        /* everything is now ready... get the userspace context ready to roll */
 421        entryaddr = interp_params.entry_addr ?: exec_params.entry_addr;
 422        start_thread(regs, entryaddr, current->mm->start_stack);
 423
 424        retval = 0;
 425
 426error:
 427        if (interpreter) {
 428                allow_write_access(interpreter);
 429                fput(interpreter);
 430        }
 431        kfree(interpreter_name);
 432        kfree(exec_params.phdrs);
 433        kfree(exec_params.loadmap);
 434        kfree(interp_params.phdrs);
 435        kfree(interp_params.loadmap);
 436        return retval;
 437
 438        /* unrecoverable error - kill the process */
 439error_kill:
 440        send_sig(SIGSEGV, current, 0);
 441        goto error;
 442
 443}
 444
 445/*****************************************************************************/
 446/*
 447 * present useful information to the program
 448 */
 449static int create_elf_fdpic_tables(struct linux_binprm *bprm,
 450                                   struct mm_struct *mm,
 451                                   struct elf_fdpic_params *exec_params,
 452                                   struct elf_fdpic_params *interp_params)
 453{
 454        unsigned long sp, csp, nitems;
 455        elf_caddr_t __user *argv, *envp;
 456        size_t platform_len = 0, len;
 457        char *k_platform;
 458        char __user *u_platform, *p;
 459        long hwcap;
 460        int loop;
 461
 462        /* we're going to shovel a whole load of stuff onto the stack */
 463#ifdef CONFIG_MMU
 464        sp = bprm->p;
 465#else
 466        sp = mm->start_stack;
 467
 468        /* stack the program arguments and environment */
 469        if (elf_fdpic_transfer_args_to_stack(bprm, &sp) < 0)
 470                return -EFAULT;
 471#endif
 472
 473        /* get hold of platform and hardware capabilities masks for the machine
 474         * we are running on.  In some cases (Sparc), this info is impossible
 475         * to get, in others (i386) it is merely difficult.
 476         */
 477        hwcap = ELF_HWCAP;
 478        k_platform = ELF_PLATFORM;
 479        u_platform = NULL;
 480
 481        if (k_platform) {
 482                platform_len = strlen(k_platform) + 1;
 483                sp -= platform_len;
 484                u_platform = (char __user *) sp;
 485                if (__copy_to_user(u_platform, k_platform, platform_len) != 0)
 486                        return -EFAULT;
 487        }
 488
 489#if defined(__i386__) && defined(CONFIG_SMP)
 490        /* in some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
 491         * by the processes running on the same package. One thing we can do is
 492         * to shuffle the initial stack for them.
 493         *
 494         * the conditionals here are unneeded, but kept in to make the code
 495         * behaviour the same as pre change unless we have hyperthreaded
 496         * processors. This keeps Mr Marcelo Person happier but should be
 497         * removed for 2.5
 498         */
 499        if (smp_num_siblings > 1)
 500                sp = sp - ((current->pid % 64) << 7);
 501#endif
 502
 503        sp &= ~7UL;
 504
 505        /* stack the load map(s) */
 506        len = sizeof(struct elf32_fdpic_loadmap);
 507        len += sizeof(struct elf32_fdpic_loadseg) * exec_params->loadmap->nsegs;
 508        sp = (sp - len) & ~7UL;
 509        exec_params->map_addr = sp;
 510
 511        if (copy_to_user((void __user *) sp, exec_params->loadmap, len) != 0)
 512                return -EFAULT;
 513
 514        current->mm->context.exec_fdpic_loadmap = (unsigned long) sp;
 515
 516        if (interp_params->loadmap) {
 517                len = sizeof(struct elf32_fdpic_loadmap);
 518                len += sizeof(struct elf32_fdpic_loadseg) *
 519                        interp_params->loadmap->nsegs;
 520                sp = (sp - len) & ~7UL;
 521                interp_params->map_addr = sp;
 522
 523                if (copy_to_user((void __user *) sp, interp_params->loadmap,
 524                                 len) != 0)
 525                        return -EFAULT;
 526
 527                current->mm->context.interp_fdpic_loadmap = (unsigned long) sp;
 528        }
 529
 530        /* force 16 byte _final_ alignment here for generality */
 531#define DLINFO_ITEMS 13
 532
 533        nitems = 1 + DLINFO_ITEMS + (k_platform ? 1 : 0);
 534#ifdef DLINFO_ARCH_ITEMS
 535        nitems += DLINFO_ARCH_ITEMS;
 536#endif
 537
 538        csp = sp;
 539        sp -= nitems * 2 * sizeof(unsigned long);
 540        sp -= (bprm->envc + 1) * sizeof(char *);        /* envv[] */
 541        sp -= (bprm->argc + 1) * sizeof(char *);        /* argv[] */
 542        sp -= 1 * sizeof(unsigned long);                /* argc */
 543
 544        csp -= sp & 15UL;
 545        sp -= sp & 15UL;
 546
 547        /* put the ELF interpreter info on the stack */
 548#define NEW_AUX_ENT(nr, id, val)                                        \
 549        do {                                                            \
 550                struct { unsigned long _id, _val; } __user *ent;        \
 551                                                                        \
 552                ent = (void __user *) csp;                              \
 553                __put_user((id), &ent[nr]._id);                         \
 554                __put_user((val), &ent[nr]._val);                       \
 555        } while (0)
 556
 557        csp -= 2 * sizeof(unsigned long);
 558        NEW_AUX_ENT(0, AT_NULL, 0);
 559        if (k_platform) {
 560                csp -= 2 * sizeof(unsigned long);
 561                NEW_AUX_ENT(0, AT_PLATFORM,
 562                            (elf_addr_t) (unsigned long) u_platform);
 563        }
 564
 565        csp -= DLINFO_ITEMS * 2 * sizeof(unsigned long);
 566        NEW_AUX_ENT( 0, AT_HWCAP,       hwcap);
 567        NEW_AUX_ENT( 1, AT_PAGESZ,      PAGE_SIZE);
 568        NEW_AUX_ENT( 2, AT_CLKTCK,      CLOCKS_PER_SEC);
 569        NEW_AUX_ENT( 3, AT_PHDR,        exec_params->ph_addr);
 570        NEW_AUX_ENT( 4, AT_PHENT,       sizeof(struct elf_phdr));
 571        NEW_AUX_ENT( 5, AT_PHNUM,       exec_params->hdr.e_phnum);
 572        NEW_AUX_ENT( 6, AT_BASE,        interp_params->elfhdr_addr);
 573        NEW_AUX_ENT( 7, AT_FLAGS,       0);
 574        NEW_AUX_ENT( 8, AT_ENTRY,       exec_params->entry_addr);
 575        NEW_AUX_ENT( 9, AT_UID,         (elf_addr_t) current->uid);
 576        NEW_AUX_ENT(10, AT_EUID,        (elf_addr_t) current->euid);
 577        NEW_AUX_ENT(11, AT_GID,         (elf_addr_t) current->gid);
 578        NEW_AUX_ENT(12, AT_EGID,        (elf_addr_t) current->egid);
 579
 580#ifdef ARCH_DLINFO
 581        /* ARCH_DLINFO must come last so platform specific code can enforce
 582         * special alignment requirements on the AUXV if necessary (eg. PPC).
 583         */
 584        ARCH_DLINFO;
 585#endif
 586#undef NEW_AUX_ENT
 587
 588        /* allocate room for argv[] and envv[] */
 589        csp -= (bprm->envc + 1) * sizeof(elf_caddr_t);
 590        envp = (elf_caddr_t __user *) csp;
 591        csp -= (bprm->argc + 1) * sizeof(elf_caddr_t);
 592        argv = (elf_caddr_t __user *) csp;
 593
 594        /* stack argc */
 595        csp -= sizeof(unsigned long);
 596        __put_user(bprm->argc, (unsigned long __user *) csp);
 597
 598        BUG_ON(csp != sp);
 599
 600        /* fill in the argv[] array */
 601#ifdef CONFIG_MMU
 602        current->mm->arg_start = bprm->p;
 603#else
 604        current->mm->arg_start = current->mm->start_stack -
 605                (MAX_ARG_PAGES * PAGE_SIZE - bprm->p);
 606#endif
 607
 608        p = (char __user *) current->mm->arg_start;
 609        for (loop = bprm->argc; loop > 0; loop--) {
 610                __put_user((elf_caddr_t) p, argv++);
 611                len = strnlen_user(p, PAGE_SIZE * MAX_ARG_PAGES);
 612                if (!len || len > PAGE_SIZE * MAX_ARG_PAGES)
 613                        return -EINVAL;
 614                p += len;
 615        }
 616        __put_user(NULL, argv);
 617        current->mm->arg_end = (unsigned long) p;
 618
 619        /* fill in the envv[] array */
 620        current->mm->env_start = (unsigned long) p;
 621        for (loop = bprm->envc; loop > 0; loop--) {
 622                __put_user((elf_caddr_t)(unsigned long) p, envp++);
 623                len = strnlen_user(p, PAGE_SIZE * MAX_ARG_PAGES);
 624                if (!len || len > PAGE_SIZE * MAX_ARG_PAGES)
 625                        return -EINVAL;
 626                p += len;
 627        }
 628        __put_user(NULL, envp);
 629        current->mm->env_end = (unsigned long) p;
 630
 631        mm->start_stack = (unsigned long) sp;
 632        return 0;
 633}
 634
 635/*****************************************************************************/
 636/*
 637 * transfer the program arguments and environment from the holding pages onto
 638 * the stack
 639 */
 640#ifndef CONFIG_MMU
 641static int elf_fdpic_transfer_args_to_stack(struct linux_binprm *bprm,
 642                                            unsigned long *_sp)
 643{
 644        unsigned long index, stop, sp;
 645        char *src;
 646        int ret = 0;
 647
 648        stop = bprm->p >> PAGE_SHIFT;
 649        sp = *_sp;
 650
 651        for (index = MAX_ARG_PAGES - 1; index >= stop; index--) {
 652                src = kmap(bprm->page[index]);
 653                sp -= PAGE_SIZE;
 654                if (copy_to_user((void *) sp, src, PAGE_SIZE) != 0)
 655                        ret = -EFAULT;
 656                kunmap(bprm->page[index]);
 657                if (ret < 0)
 658                        goto out;
 659        }
 660
 661        *_sp = (*_sp - (MAX_ARG_PAGES * PAGE_SIZE - bprm->p)) & ~15;
 662
 663out:
 664        return ret;
 665}
 666#endif
 667
 668/*****************************************************************************/
 669/*
 670 * load the appropriate binary image (executable or interpreter) into memory
 671 * - we assume no MMU is available
 672 * - if no other PIC bits are set in params->hdr->e_flags
 673 *   - we assume that the LOADable segments in the binary are independently relocatable
 674 *   - we assume R/O executable segments are shareable
 675 * - else
 676 *   - we assume the loadable parts of the image to require fixed displacement
 677 *   - the image is not shareable
 678 */
 679static int elf_fdpic_map_file(struct elf_fdpic_params *params,
 680                              struct file *file,
 681                              struct mm_struct *mm,
 682                              const char *what)
 683{
 684        struct elf32_fdpic_loadmap *loadmap;
 685#ifdef CONFIG_MMU
 686        struct elf32_fdpic_loadseg *mseg;
 687#endif
 688        struct elf32_fdpic_loadseg *seg;
 689        struct elf32_phdr *phdr;
 690        unsigned long load_addr, stop;
 691        unsigned nloads, tmp;
 692        size_t size;
 693        int loop, ret;
 694
 695        /* allocate a load map table */
 696        nloads = 0;
 697        for (loop = 0; loop < params->hdr.e_phnum; loop++)
 698                if (params->phdrs[loop].p_type == PT_LOAD)
 699                        nloads++;
 700
 701        if (nloads == 0)
 702                return -ELIBBAD;
 703
 704        size = sizeof(*loadmap) + nloads * sizeof(*seg);
 705        loadmap = kmalloc(size, GFP_KERNEL);
 706        if (!loadmap)
 707                return -ENOMEM;
 708
 709        params->loadmap = loadmap;
 710        memset(loadmap, 0, size);
 711
 712        loadmap->version = ELF32_FDPIC_LOADMAP_VERSION;
 713        loadmap->nsegs = nloads;
 714
 715        load_addr = params->load_addr;
 716        seg = loadmap->segs;
 717
 718        /* map the requested LOADs into the memory space */
 719        switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
 720        case ELF_FDPIC_FLAG_CONSTDISP:
 721        case ELF_FDPIC_FLAG_CONTIGUOUS:
 722#ifndef CONFIG_MMU
 723                ret = elf_fdpic_map_file_constdisp_on_uclinux(params, file, mm);
 724                if (ret < 0)
 725                        return ret;
 726                break;
 727#endif
 728        default:
 729                ret = elf_fdpic_map_file_by_direct_mmap(params, file, mm);
 730                if (ret < 0)
 731                        return ret;
 732                break;
 733        }
 734
 735        /* map the entry point */
 736        if (params->hdr.e_entry) {
 737                seg = loadmap->segs;
 738                for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
 739                        if (params->hdr.e_entry >= seg->p_vaddr &&
 740                            params->hdr.e_entry < seg->p_vaddr + seg->p_memsz) {
 741                                params->entry_addr =
 742                                        (params->hdr.e_entry - seg->p_vaddr) +
 743                                        seg->addr;
 744                                break;
 745                        }
 746                }
 747        }
 748
 749        /* determine where the program header table has wound up if mapped */
 750        stop = params->hdr.e_phoff;
 751        stop += params->hdr.e_phnum * sizeof (struct elf_phdr);
 752        phdr = params->phdrs;
 753
 754        for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
 755                if (phdr->p_type != PT_LOAD)
 756                        continue;
 757
 758                if (phdr->p_offset > params->hdr.e_phoff ||
 759                    phdr->p_offset + phdr->p_filesz < stop)
 760                        continue;
 761
 762                seg = loadmap->segs;
 763                for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
 764                        if (phdr->p_vaddr >= seg->p_vaddr &&
 765                            phdr->p_vaddr + phdr->p_filesz <=
 766                            seg->p_vaddr + seg->p_memsz) {
 767                                params->ph_addr =
 768                                        (phdr->p_vaddr - seg->p_vaddr) +
 769                                        seg->addr +
 770                                        params->hdr.e_phoff - phdr->p_offset;
 771                                break;
 772                        }
 773                }
 774                break;
 775        }
 776
 777        /* determine where the dynamic section has wound up if there is one */
 778        phdr = params->phdrs;
 779        for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
 780                if (phdr->p_type != PT_DYNAMIC)
 781                        continue;
 782
 783                seg = loadmap->segs;
 784                for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
 785                        if (phdr->p_vaddr >= seg->p_vaddr &&
 786                            phdr->p_vaddr + phdr->p_memsz <=
 787                            seg->p_vaddr + seg->p_memsz) {
 788                                params->dynamic_addr =
 789                                        (phdr->p_vaddr - seg->p_vaddr) +
 790                                        seg->addr;
 791
 792                                /* check the dynamic section contains at least
 793                                 * one item, and that the last item is a NULL
 794                                 * entry */
 795                                if (phdr->p_memsz == 0 ||
 796                                    phdr->p_memsz % sizeof(Elf32_Dyn) != 0)
 797                                        goto dynamic_error;
 798
 799                                tmp = phdr->p_memsz / sizeof(Elf32_Dyn);
 800                                if (((Elf32_Dyn *)
 801                                     params->dynamic_addr)[tmp - 1].d_tag != 0)
 802                                        goto dynamic_error;
 803                                break;
 804                        }
 805                }
 806                break;
 807        }
 808
 809        /* now elide adjacent segments in the load map on MMU linux
 810         * - on uClinux the holes between may actually be filled with system
 811         *   stuff or stuff from other processes
 812         */
 813#ifdef CONFIG_MMU
 814        nloads = loadmap->nsegs;
 815        mseg = loadmap->segs;
 816        seg = mseg + 1;
 817        for (loop = 1; loop < nloads; loop++) {
 818                /* see if we have a candidate for merging */
 819                if (seg->p_vaddr - mseg->p_vaddr == seg->addr - mseg->addr) {
 820                        load_addr = PAGE_ALIGN(mseg->addr + mseg->p_memsz);
 821                        if (load_addr == (seg->addr & PAGE_MASK)) {
 822                                mseg->p_memsz +=
 823                                        load_addr -
 824                                        (mseg->addr + mseg->p_memsz);
 825                                mseg->p_memsz += seg->addr & ~PAGE_MASK;
 826                                mseg->p_memsz += seg->p_memsz;
 827                                loadmap->nsegs--;
 828                                continue;
 829                        }
 830                }
 831
 832                mseg++;
 833                if (mseg != seg)
 834                        *mseg = *seg;
 835        }
 836#endif
 837
 838        kdebug("Mapped Object [%s]:", what);
 839        kdebug("- elfhdr   : %lx", params->elfhdr_addr);
 840        kdebug("- entry    : %lx", params->entry_addr);
 841        kdebug("- PHDR[]   : %lx", params->ph_addr);
 842        kdebug("- DYNAMIC[]: %lx", params->dynamic_addr);
 843        seg = loadmap->segs;
 844        for (loop = 0; loop < loadmap->nsegs; loop++, seg++)
 845                kdebug("- LOAD[%d] : %08x-%08x [va=%x ms=%x]",
 846                       loop,
 847                       seg->addr, seg->addr + seg->p_memsz - 1,
 848                       seg->p_vaddr, seg->p_memsz);
 849
 850        return 0;
 851
 852dynamic_error:
 853        printk("ELF FDPIC %s with invalid DYNAMIC section (inode=%lu)\n",
 854               what, file->f_dentry->d_inode->i_ino);
 855        return -ELIBBAD;
 856}
 857
 858/*****************************************************************************/
 859/*
 860 * map a file with constant displacement under uClinux
 861 */
 862#ifndef CONFIG_MMU
 863static int elf_fdpic_map_file_constdisp_on_uclinux(
 864        struct elf_fdpic_params *params,
 865        struct file *file,
 866        struct mm_struct *mm)
 867{
 868        struct elf32_fdpic_loadseg *seg;
 869        struct elf32_phdr *phdr;
 870        unsigned long load_addr, base = ULONG_MAX, top = 0, maddr = 0, mflags;
 871        loff_t fpos;
 872        int loop, ret;
 873
 874        load_addr = params->load_addr;
 875        seg = params->loadmap->segs;
 876
 877        /* determine the bounds of the contiguous overall allocation we must
 878         * make */
 879        phdr = params->phdrs;
 880        for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
 881                if (params->phdrs[loop].p_type != PT_LOAD)
 882                        continue;
 883
 884                if (base > phdr->p_vaddr)
 885                        base = phdr->p_vaddr;
 886                if (top < phdr->p_vaddr + phdr->p_memsz)
 887                        top = phdr->p_vaddr + phdr->p_memsz;
 888        }
 889
 890        /* allocate one big anon block for everything */
 891        mflags = MAP_PRIVATE;
 892        if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
 893                mflags |= MAP_EXECUTABLE;
 894
 895        down_write(&mm->mmap_sem);
 896        maddr = do_mmap(NULL, load_addr, top - base,
 897                        PROT_READ | PROT_WRITE | PROT_EXEC, mflags, 0);
 898        up_write(&mm->mmap_sem);
 899        if (IS_ERR_VALUE(maddr))
 900                return (int) maddr;
 901
 902        if (load_addr != 0)
 903                load_addr += PAGE_ALIGN(top - base);
 904
 905        /* and then load the file segments into it */
 906        phdr = params->phdrs;
 907        for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
 908                if (params->phdrs[loop].p_type != PT_LOAD)
 909                        continue;
 910
 911                fpos = phdr->p_offset;
 912
 913                seg->addr = maddr + (phdr->p_vaddr - base);
 914                seg->p_vaddr = phdr->p_vaddr;
 915                seg->p_memsz = phdr->p_memsz;
 916
 917                ret = file->f_op->read(file, (void *) seg->addr,
 918                                       phdr->p_filesz, &fpos);
 919                if (ret < 0)
 920                        return ret;
 921
 922                /* map the ELF header address if in this segment */
 923                if (phdr->p_offset == 0)
 924                        params->elfhdr_addr = seg->addr;
 925
 926                /* clear any space allocated but not loaded */
 927                if (phdr->p_filesz < phdr->p_memsz)
 928                        clear_user((void *) (seg->addr + phdr->p_filesz),
 929                                   phdr->p_memsz - phdr->p_filesz);
 930
 931                if (mm) {
 932                        if (phdr->p_flags & PF_X) {
 933                                mm->start_code = seg->addr;
 934                                mm->end_code = seg->addr + phdr->p_memsz;
 935                        } else if (!mm->start_data) {
 936                                mm->start_data = seg->addr;
 937#ifndef CONFIG_MMU
 938                                mm->end_data = seg->addr + phdr->p_memsz;
 939#endif
 940                        }
 941
 942#ifdef CONFIG_MMU
 943                        if (seg->addr + phdr->p_memsz > mm->end_data)
 944                                mm->end_data = seg->addr + phdr->p_memsz;
 945#endif
 946                }
 947
 948                seg++;
 949        }
 950
 951        return 0;
 952}
 953#endif
 954
 955/*****************************************************************************/
 956/*
 957 * map a binary by direct mmap() of the individual PT_LOAD segments
 958 */
 959static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params,
 960                                             struct file *file,
 961                                             struct mm_struct *mm)
 962{
 963        struct elf32_fdpic_loadseg *seg;
 964        struct elf32_phdr *phdr;
 965        unsigned long load_addr, delta_vaddr;
 966        int loop, dvset;
 967
 968        load_addr = params->load_addr;
 969        delta_vaddr = 0;
 970        dvset = 0;
 971
 972        seg = params->loadmap->segs;
 973
 974        /* deal with each load segment separately */
 975        phdr = params->phdrs;
 976        for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
 977                unsigned long maddr, disp, excess, excess1;
 978                int prot = 0, flags;
 979
 980                if (phdr->p_type != PT_LOAD)
 981                        continue;
 982
 983                kdebug("[LOAD] va=%lx of=%lx fs=%lx ms=%lx",
 984                       (unsigned long) phdr->p_vaddr,
 985                       (unsigned long) phdr->p_offset,
 986                       (unsigned long) phdr->p_filesz,
 987                       (unsigned long) phdr->p_memsz);
 988
 989                /* determine the mapping parameters */
 990                if (phdr->p_flags & PF_R) prot |= PROT_READ;
 991                if (phdr->p_flags & PF_W) prot |= PROT_WRITE;
 992                if (phdr->p_flags & PF_X) prot |= PROT_EXEC;
 993
 994                flags = MAP_PRIVATE | MAP_DENYWRITE;
 995                if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
 996                        flags |= MAP_EXECUTABLE;
 997
 998                maddr = 0;
 999
1000                switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
1001                case ELF_FDPIC_FLAG_INDEPENDENT:
1002                        /* PT_LOADs are independently locatable */
1003                        break;
1004
1005                case ELF_FDPIC_FLAG_HONOURVADDR:
1006                        /* the specified virtual address must be honoured */
1007                        maddr = phdr->p_vaddr;
1008                        flags |= MAP_FIXED;
1009                        break;
1010
1011                case ELF_FDPIC_FLAG_CONSTDISP:
1012                        /* constant displacement
1013                         * - can be mapped anywhere, but must be mapped as a
1014                         *   unit
1015                         */
1016                        if (!dvset) {
1017                                maddr = load_addr;
1018                                delta_vaddr = phdr->p_vaddr;
1019                                dvset = 1;
1020                        } else {
1021                                maddr = load_addr + phdr->p_vaddr - delta_vaddr;
1022                                flags |= MAP_FIXED;
1023                        }
1024                        break;
1025
1026                case ELF_FDPIC_FLAG_CONTIGUOUS:
1027                        /* contiguity handled later */
1028                        break;
1029
1030                default:
1031                        BUG();
1032                }
1033
1034                maddr &= PAGE_MASK;
1035
1036                /* create the mapping */
1037                disp = phdr->p_vaddr & ~PAGE_MASK;
1038                down_write(&mm->mmap_sem);
1039                maddr = do_mmap(file, maddr, phdr->p_memsz + disp, prot, flags,
1040                                phdr->p_offset - disp);
1041                up_write(&mm->mmap_sem);
1042
1043                kdebug("mmap[%d] <file> sz=%lx pr=%x fl=%x of=%lx --> %08lx",
1044                       loop, phdr->p_memsz + disp, prot, flags,
1045                       phdr->p_offset - disp, maddr);
1046
1047                if (IS_ERR_VALUE(maddr))
1048                        return (int) maddr;
1049
1050                if ((params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) ==
1051                    ELF_FDPIC_FLAG_CONTIGUOUS)
1052                        load_addr += PAGE_ALIGN(phdr->p_memsz + disp);
1053
1054                seg->addr = maddr + disp;
1055                seg->p_vaddr = phdr->p_vaddr;
1056                seg->p_memsz = phdr->p_memsz;
1057
1058                /* map the ELF header address if in this segment */
1059                if (phdr->p_offset == 0)
1060                        params->elfhdr_addr = seg->addr;
1061
1062                /* clear the bit between beginning of mapping and beginning of
1063                 * PT_LOAD */
1064                if (prot & PROT_WRITE && disp > 0) {
1065                        kdebug("clear[%d] ad=%lx sz=%lx", loop, maddr, disp);
1066                        clear_user((void __user *) maddr, disp);
1067                        maddr += disp;
1068                }
1069
1070                /* clear any space allocated but not loaded
1071                 * - on uClinux we can just clear the lot
1072                 * - on MMU linux we'll get a SIGBUS beyond the last page
1073                 *   extant in the file
1074                 */
1075                excess = phdr->p_memsz - phdr->p_filesz;
1076                excess1 = PAGE_SIZE - ((maddr + phdr->p_filesz) & ~PAGE_MASK);
1077
1078#ifdef CONFIG_MMU
1079                if (excess > excess1) {
1080                        unsigned long xaddr = maddr + phdr->p_filesz + excess1;
1081                        unsigned long xmaddr;
1082
1083                        flags |= MAP_FIXED | MAP_ANONYMOUS;
1084                        down_write(&mm->mmap_sem);
1085                        xmaddr = do_mmap(NULL, xaddr, excess - excess1,
1086                                         prot, flags, 0);
1087                        up_write(&mm->mmap_sem);
1088
1089                        kdebug("mmap[%d] <anon>"
1090                               " ad=%lx sz=%lx pr=%x fl=%x of=0 --> %08lx",
1091                               loop, xaddr, excess - excess1, prot, flags,
1092                               xmaddr);
1093
1094                        if (xmaddr != xaddr)
1095                                return -ENOMEM;
1096                }
1097
1098                if (prot & PROT_WRITE && excess1 > 0) {
1099                        kdebug("clear[%d] ad=%lx sz=%lx",
1100                               loop, maddr + phdr->p_filesz, excess1);
1101                        clear_user((void __user *) maddr + phdr->p_filesz,
1102                                   excess1);
1103                }
1104
1105#else
1106                if (excess > 0) {
1107                        kdebug("clear[%d] ad=%lx sz=%lx",
1108                               loop, maddr + phdr->p_filesz, excess);
1109                        clear_user((void *) maddr + phdr->p_filesz, excess);
1110                }
1111#endif
1112
1113                if (mm) {
1114                        if (phdr->p_flags & PF_X) {
1115                                mm->start_code = maddr;
1116                                mm->end_code = maddr + phdr->p_memsz;
1117                        } else if (!mm->start_data) {
1118                                mm->start_data = maddr;
1119                                mm->end_data = maddr + phdr->p_memsz;
1120                        }
1121                }
1122
1123                seg++;
1124        }
1125
1126        return 0;
1127}
1128
1129/*****************************************************************************/
1130/*
1131 * ELF-FDPIC core dumper
1132 *
1133 * Modelled on fs/exec.c:aout_core_dump()
1134 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1135 *
1136 * Modelled on fs/binfmt_elf.c core dumper
1137 */
1138#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
1139
1140/*
1141 * These are the only things you should do on a core-file: use only these
1142 * functions to write out all the necessary info.
1143 */
1144static int dump_write(struct file *file, const void *addr, int nr)
1145{
1146        return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
1147}
1148
1149static int dump_seek(struct file *file, loff_t off)
1150{
1151        if (file->f_op->llseek) {
1152                if (file->f_op->llseek(file, off, SEEK_SET) != off)
1153                        return 0;
1154        } else {
1155                file->f_pos = off;
1156        }
1157        return 1;
1158}
1159
1160/*
1161 * Decide whether a segment is worth dumping; default is yes to be
1162 * sure (missing info is worse than too much; etc).
1163 * Personally I'd include everything, and use the coredump limit...
1164 *
1165 * I think we should skip something. But I am not sure how. H.J.
1166 */
1167static int maydump(struct vm_area_struct *vma)
1168{
1169        /* Do not dump I/O mapped devices or special mappings */
1170        if (vma->vm_flags & (VM_IO | VM_RESERVED)) {
1171                kdcore("%08lx: %08lx: no (IO)", vma->vm_start, vma->vm_flags);
1172                return 0;
1173        }
1174
1175        /* If we may not read the contents, don't allow us to dump
1176         * them either. "dump_write()" can't handle it anyway.
1177         */
1178        if (!(vma->vm_flags & VM_READ)) {
1179                kdcore("%08lx: %08lx: no (!read)", vma->vm_start, vma->vm_flags);
1180                return 0;
1181        }
1182
1183        /* Dump shared memory only if mapped from an anonymous file. */
1184        if (vma->vm_flags & VM_SHARED) {
1185                if (vma->vm_file->f_dentry->d_inode->i_nlink == 0) {
1186                        kdcore("%08lx: %08lx: no (share)", vma->vm_start, vma->vm_flags);
1187                        return 1;
1188                }
1189
1190                kdcore("%08lx: %08lx: no (share)", vma->vm_start, vma->vm_flags);
1191                return 0;
1192        }
1193
1194#ifdef CONFIG_MMU
1195        /* If it hasn't been written to, don't write it out */
1196        if (!vma->anon_vma) {
1197                kdcore("%08lx: %08lx: no (!anon)", vma->vm_start, vma->vm_flags);
1198                return 0;
1199        }
1200#endif
1201
1202        kdcore("%08lx: %08lx: yes", vma->vm_start, vma->vm_flags);
1203        return 1;
1204}
1205
1206/* An ELF note in memory */
1207struct memelfnote
1208{
1209        const char *name;
1210        int type;
1211        unsigned int datasz;
1212        void *data;
1213};
1214
1215static int notesize(struct memelfnote *en)
1216{
1217        int sz;
1218
1219        sz = sizeof(struct elf_note);
1220        sz += roundup(strlen(en->name) + 1, 4);
1221        sz += roundup(en->datasz, 4);
1222
1223        return sz;
1224}
1225
1226/* #define DEBUG */
1227
1228#define DUMP_WRITE(addr, nr)    \
1229        do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
1230#define DUMP_SEEK(off)  \
1231        do { if (!dump_seek(file, (off))) return 0; } while(0)
1232
1233static int writenote(struct memelfnote *men, struct file *file)
1234{
1235        struct elf_note en;
1236
1237        en.n_namesz = strlen(men->name) + 1;
1238        en.n_descsz = men->datasz;
1239        en.n_type = men->type;
1240
1241        DUMP_WRITE(&en, sizeof(en));
1242        DUMP_WRITE(men->name, en.n_namesz);
1243        /* XXX - cast from long long to long to avoid need for libgcc.a */
1244        DUMP_SEEK(roundup((unsigned long)file->f_pos, 4));      /* XXX */
1245        DUMP_WRITE(men->data, men->datasz);
1246        DUMP_SEEK(roundup((unsigned long)file->f_pos, 4));      /* XXX */
1247
1248        return 1;
1249}
1250#undef DUMP_WRITE
1251#undef DUMP_SEEK
1252
1253#define DUMP_WRITE(addr, nr)    \
1254        if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1255                goto end_coredump;
1256#define DUMP_SEEK(off)  \
1257        if (!dump_seek(file, (off))) \
1258                goto end_coredump;
1259
1260static inline void fill_elf_fdpic_header(struct elfhdr *elf, int segs)
1261{
1262        memcpy(elf->e_ident, ELFMAG, SELFMAG);
1263        elf->e_ident[EI_CLASS] = ELF_CLASS;
1264        elf->e_ident[EI_DATA] = ELF_DATA;
1265        elf->e_ident[EI_VERSION] = EV_CURRENT;
1266        elf->e_ident[EI_OSABI] = ELF_OSABI;
1267        memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1268
1269        elf->e_type = ET_CORE;
1270        elf->e_machine = ELF_ARCH;
1271        elf->e_version = EV_CURRENT;
1272        elf->e_entry = 0;
1273        elf->e_phoff = sizeof(struct elfhdr);
1274        elf->e_shoff = 0;
1275        elf->e_flags = ELF_FDPIC_CORE_EFLAGS;
1276        elf->e_ehsize = sizeof(struct elfhdr);
1277        elf->e_phentsize = sizeof(struct elf_phdr);
1278        elf->e_phnum = segs;
1279        elf->e_shentsize = 0;
1280        elf->e_shnum = 0;
1281        elf->e_shstrndx = 0;
1282        return;
1283}
1284
1285static inline void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
1286{
1287        phdr->p_type = PT_NOTE;
1288        phdr->p_offset = offset;
1289        phdr->p_vaddr = 0;
1290        phdr->p_paddr = 0;
1291        phdr->p_filesz = sz;
1292        phdr->p_memsz = 0;
1293        phdr->p_flags = 0;
1294        phdr->p_align = 0;
1295        return;
1296}
1297
1298static inline void fill_note(struct memelfnote *note, const char *name, int type,
1299                unsigned int sz, void *data)
1300{
1301        note->name = name;
1302        note->type = type;
1303        note->datasz = sz;
1304        note->data = data;
1305        return;
1306}
1307
1308/*
1309 * fill up all the fields in prstatus from the given task struct, except
1310 * registers which need to be filled up seperately.
1311 */
1312static void fill_prstatus(struct elf_prstatus *prstatus,
1313                          struct task_struct *p, long signr)
1314{
1315        prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
1316        prstatus->pr_sigpend = p->pending.signal.sig[0];
1317        prstatus->pr_sighold = p->blocked.sig[0];
1318        prstatus->pr_pid = p->pid;
1319        prstatus->pr_ppid = p->parent->pid;
1320        prstatus->pr_pgrp = process_group(p);
1321        prstatus->pr_sid = p->signal->session;
1322        if (thread_group_leader(p)) {
1323                /*
1324                 * This is the record for the group leader.  Add in the
1325                 * cumulative times of previous dead threads.  This total
1326                 * won't include the time of each live thread whose state
1327                 * is included in the core dump.  The final total reported
1328                 * to our parent process when it calls wait4 will include
1329                 * those sums as well as the little bit more time it takes
1330                 * this and each other thread to finish dying after the
1331                 * core dump synchronization phase.
1332                 */
1333                cputime_to_timeval(cputime_add(p->utime, p->signal->utime),
1334                                   &prstatus->pr_utime);
1335                cputime_to_timeval(cputime_add(p->stime, p->signal->stime),
1336                                   &prstatus->pr_stime);
1337        } else {
1338                cputime_to_timeval(p->utime, &prstatus->pr_utime);
1339                cputime_to_timeval(p->stime, &prstatus->pr_stime);
1340        }
1341        cputime_to_timeval(p->signal->cutime, &prstatus->pr_cutime);
1342        cputime_to_timeval(p->signal->cstime, &prstatus->pr_cstime);
1343
1344        prstatus->pr_exec_fdpic_loadmap = p->mm->context.exec_fdpic_loadmap;
1345        prstatus->pr_interp_fdpic_loadmap = p->mm->context.interp_fdpic_loadmap;
1346}
1347
1348static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
1349                       struct mm_struct *mm)
1350{
1351        unsigned int i, len;
1352
1353        /* first copy the parameters from user space */
1354        memset(psinfo, 0, sizeof(struct elf_prpsinfo));
1355
1356        len = mm->arg_end - mm->arg_start;
1357        if (len >= ELF_PRARGSZ)
1358                len = ELF_PRARGSZ - 1;
1359        if (copy_from_user(&psinfo->pr_psargs,
1360                           (const char __user *) mm->arg_start, len))
1361                return -EFAULT;
1362        for (i = 0; i < len; i++)
1363                if (psinfo->pr_psargs[i] == 0)
1364                        psinfo->pr_psargs[i] = ' ';
1365        psinfo->pr_psargs[len] = 0;
1366
1367        psinfo->pr_pid = p->pid;
1368        psinfo->pr_ppid = p->parent->pid;
1369        psinfo->pr_pgrp = process_group(p);
1370        psinfo->pr_sid = p->signal->session;
1371
1372        i = p->state ? ffz(~p->state) + 1 : 0;
1373        psinfo->pr_state = i;
1374        psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
1375        psinfo->pr_zomb = psinfo->pr_sname == 'Z';
1376        psinfo->pr_nice = task_nice(p);
1377        psinfo->pr_flag = p->flags;
1378        SET_UID(psinfo->pr_uid, p->uid);
1379        SET_GID(psinfo->pr_gid, p->gid);
1380        strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
1381
1382        return 0;
1383}
1384
1385/* Here is the structure in which status of each thread is captured. */
1386struct elf_thread_status
1387{
1388        struct list_head list;
1389        struct elf_prstatus prstatus;   /* NT_PRSTATUS */
1390        elf_fpregset_t fpu;             /* NT_PRFPREG */
1391        struct task_struct *thread;
1392#ifdef ELF_CORE_COPY_XFPREGS
1393        elf_fpxregset_t xfpu;           /* NT_PRXFPREG */
1394#endif
1395        struct memelfnote notes[3];
1396        int num_notes;
1397};
1398
1399/*
1400 * In order to add the specific thread information for the elf file format,
1401 * we need to keep a linked list of every thread's pr_status and then create
1402 * a single section for them in the final core file.
1403 */
1404static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
1405{
1406        struct task_struct *p = t->thread;
1407        int sz = 0;
1408
1409        t->num_notes = 0;
1410
1411        fill_prstatus(&t->prstatus, p, signr);
1412        elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
1413
1414        fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus),
1415                  &t->prstatus);
1416        t->num_notes++;
1417        sz += notesize(&t->notes[0]);
1418
1419        t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL, &t->fpu);
1420        if (t->prstatus.pr_fpvalid) {
1421                fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu),
1422                          &t->fpu);
1423                t->num_notes++;
1424                sz += notesize(&t->notes[1]);
1425        }
1426
1427#ifdef ELF_CORE_COPY_XFPREGS
1428        if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
1429                fill_note(&t->notes[2], "LINUX", NT_PRXFPREG, sizeof(t->xfpu),
1430                          &t->xfpu);
1431                t->num_notes++;
1432                sz += notesize(&t->notes[2]);
1433        }
1434#endif
1435        return sz;
1436}
1437
1438/*
1439 * dump the segments for an MMU process
1440 */
1441#ifdef CONFIG_MMU
1442static int elf_fdpic_dump_segments(struct file *file, struct mm_struct *mm,
1443                                   size_t *size, unsigned long *limit)
1444{
1445        struct vm_area_struct *vma;
1446
1447        for (vma = current->mm->mmap; vma; vma = vma->vm_next) {
1448                unsigned long addr;
1449
1450                if (!maydump(vma))
1451                        continue;
1452
1453                for (addr = vma->vm_start;
1454                     addr < vma->vm_end;
1455                     addr += PAGE_SIZE
1456                     ) {
1457                        struct vm_area_struct *vma;
1458                        struct page *page;
1459
1460                        if (get_user_pages(current, current->mm, addr, 1, 0, 1,
1461                                           &page, &vma) <= 0) {
1462                                DUMP_SEEK(file->f_pos + PAGE_SIZE);
1463                        }
1464                        else if (page == ZERO_PAGE(addr)) {
1465                                DUMP_SEEK(file->f_pos + PAGE_SIZE);
1466                                page_cache_release(page);
1467                        }
1468                        else {
1469                                void *kaddr;
1470
1471                                flush_cache_page(vma, addr, page_to_pfn(page));
1472                                kaddr = kmap(page);
1473                                if ((*size += PAGE_SIZE) > *limit ||
1474                                    !dump_write(file, kaddr, PAGE_SIZE)
1475                                    ) {
1476                                        kunmap(page);
1477                                        page_cache_release(page);
1478                                        return -EIO;
1479                                }
1480                                kunmap(page);
1481                                page_cache_release(page);
1482                        }
1483                }
1484        }
1485
1486        return 0;
1487
1488end_coredump:
1489        return -EFBIG;
1490}
1491#endif
1492
1493/*
1494 * dump the segments for a NOMMU process
1495 */
1496#ifndef CONFIG_MMU
1497static int elf_fdpic_dump_segments(struct file *file, struct mm_struct *mm,
1498                                   size_t *size, unsigned long *limit)
1499{
1500        struct vm_list_struct *vml;
1501
1502        for (vml = current->mm->context.vmlist; vml; vml = vml->next) {
1503        struct vm_area_struct *vma = vml->vma;
1504
1505                if (!maydump(vma))
1506                        continue;
1507
1508                if ((*size += PAGE_SIZE) > *limit)
1509                        return -EFBIG;
1510
1511                if (!dump_write(file, (void *) vma->vm_start,
1512                                vma->vm_end - vma->vm_start))
1513                        return -EIO;
1514        }
1515
1516        return 0;
1517}
1518#endif
1519
1520/*
1521 * Actual dumper
1522 *
1523 * This is a two-pass process; first we find the offsets of the bits,
1524 * and then they are actually written out.  If we run out of core limit
1525 * we just truncate.
1526 */
1527static int elf_fdpic_core_dump(long signr, struct pt_regs *regs,
1528                               struct file *file)
1529{
1530#define NUM_NOTES       6
1531        int has_dumped = 0;
1532        mm_segment_t fs;
1533        int segs;
1534        size_t size = 0;
1535        int i;
1536        struct vm_area_struct *vma;
1537        struct elfhdr *elf = NULL;
1538        loff_t offset = 0, dataoff;
1539        unsigned long limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
1540        int numnote;
1541        struct memelfnote *notes = NULL;
1542        struct elf_prstatus *prstatus = NULL;   /* NT_PRSTATUS */
1543        struct elf_prpsinfo *psinfo = NULL;     /* NT_PRPSINFO */
1544        struct task_struct *g, *p;
1545        LIST_HEAD(thread_list);
1546        struct list_head *t;
1547        elf_fpregset_t *fpu = NULL;
1548#ifdef ELF_CORE_COPY_XFPREGS
1549        elf_fpxregset_t *xfpu = NULL;
1550#endif
1551        int thread_status_size = 0;
1552#ifndef CONFIG_MMU
1553        struct vm_list_struct *vml;
1554#endif
1555        elf_addr_t *auxv;
1556
1557        /*
1558         * We no longer stop all VM operations.
1559         *
1560         * This is because those proceses that could possibly change map_count
1561         * or the mmap / vma pages are now blocked in do_exit on current
1562         * finishing this core dump.
1563         *
1564         * Only ptrace can touch these memory addresses, but it doesn't change
1565         * the map_count or the pages allocated. So no possibility of crashing
1566         * exists while dumping the mm->vm_next areas to the core file.
1567         */
1568
1569        /* alloc memory for large data structures: too large to be on stack */
1570        elf = kmalloc(sizeof(*elf), GFP_KERNEL);
1571        if (!elf)
1572                goto cleanup;
1573        prstatus = kzalloc(sizeof(*prstatus), GFP_KERNEL);
1574        if (!prstatus)
1575                goto cleanup;
1576        psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
1577        if (!psinfo)
1578                goto cleanup;
1579        notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote), GFP_KERNEL);
1580        if (!notes)
1581                goto cleanup;
1582        fpu = kmalloc(sizeof(*fpu), GFP_KERNEL);
1583        if (!fpu)
1584                goto cleanup;
1585#ifdef ELF_CORE_COPY_XFPREGS
1586        xfpu = kmalloc(sizeof(*xfpu), GFP_KERNEL);
1587        if (!xfpu)
1588                goto cleanup;
1589#endif
1590
1591        if (signr) {
1592                struct elf_thread_status *tmp;
1593                read_lock(&tasklist_lock);
1594                do_each_thread(g,p)
1595                        if (current->mm == p->mm && current != p) {
1596                                tmp = kzalloc(sizeof(*tmp), GFP_ATOMIC);
1597                                if (!tmp) {
1598                                        read_unlock(&tasklist_lock);
1599                                        goto cleanup;
1600                                }
1601                                INIT_LIST_HEAD(&tmp->list);
1602                                tmp->thread = p;
1603                                list_add(&tmp->list, &thread_list);
1604                        }
1605                while_each_thread(g,p);
1606                read_unlock(&tasklist_lock);
1607                list_for_each(t, &thread_list) {
1608                        struct elf_thread_status *tmp;
1609                        int sz;
1610
1611                        tmp = list_entry(t, struct elf_thread_status, list);
1612                        sz = elf_dump_thread_status(signr, tmp);
1613                        thread_status_size += sz;
1614                }
1615        }
1616
1617        /* now collect the dump for the current */
1618        fill_prstatus(prstatus, current, signr);
1619        elf_core_copy_regs(&prstatus->pr_reg, regs);
1620
1621#ifdef CONFIG_MMU
1622        segs = current->mm->map_count;
1623#else
1624        segs = 0;
1625        for (vml = current->mm->context.vmlist; vml; vml = vml->next)
1626            segs++;
1627#endif
1628#ifdef ELF_CORE_EXTRA_PHDRS
1629        segs += ELF_CORE_EXTRA_PHDRS;
1630#endif
1631
1632        /* Set up header */
1633        fill_elf_fdpic_header(elf, segs + 1);   /* including notes section */
1634
1635        has_dumped = 1;
1636        current->flags |= PF_DUMPCORE;
1637
1638        /*
1639         * Set up the notes in similar form to SVR4 core dumps made
1640         * with info from their /proc.
1641         */
1642
1643        fill_note(notes + 0, "CORE", NT_PRSTATUS, sizeof(*prstatus), prstatus);
1644        fill_psinfo(psinfo, current->group_leader, current->mm);
1645        fill_note(notes + 1, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
1646
1647        numnote = 2;
1648
1649        auxv = (elf_addr_t *) current->mm->saved_auxv;
1650
1651        i = 0;
1652        do
1653                i += 2;
1654        while (auxv[i - 2] != AT_NULL);
1655        fill_note(&notes[numnote++], "CORE", NT_AUXV,
1656                  i * sizeof(elf_addr_t), auxv);
1657
1658        /* Try to dump the FPU. */
1659        if ((prstatus->pr_fpvalid =
1660             elf_core_copy_task_fpregs(current, regs, fpu)))
1661                fill_note(notes + numnote++,
1662                          "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
1663#ifdef ELF_CORE_COPY_XFPREGS
1664        if (elf_core_copy_task_xfpregs(current, xfpu))
1665                fill_note(notes + numnote++,
1666                          "LINUX", NT_PRXFPREG, sizeof(*xfpu), xfpu);
1667#endif
1668
1669        fs = get_fs();
1670        set_fs(KERNEL_DS);
1671
1672        DUMP_WRITE(elf, sizeof(*elf));
1673        offset += sizeof(*elf);                         /* Elf header */
1674        offset += (segs+1) * sizeof(struct elf_phdr);   /* Program headers */
1675
1676        /* Write notes phdr entry */
1677        {
1678                struct elf_phdr phdr;
1679                int sz = 0;
1680
1681                for (i = 0; i < numnote; i++)
1682                        sz += notesize(notes + i);
1683
1684                sz += thread_status_size;
1685
1686                fill_elf_note_phdr(&phdr, sz, offset);
1687                offset += sz;
1688                DUMP_WRITE(&phdr, sizeof(phdr));
1689        }
1690
1691        /* Page-align dumped data */
1692        dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1693
1694        /* write program headers for segments dump */
1695        for (
1696#ifdef CONFIG_MMU
1697                vma = current->mm->mmap; vma; vma = vma->vm_next
1698#else
1699                        vml = current->mm->context.vmlist; vml; vml = vml->next
1700#endif
1701             ) {
1702                struct elf_phdr phdr;
1703                size_t sz;
1704
1705#ifndef CONFIG_MMU
1706                vma = vml->vma;
1707#endif
1708
1709                sz = vma->vm_end - vma->vm_start;
1710
1711                phdr.p_type = PT_LOAD;
1712                phdr.p_offset = offset;
1713                phdr.p_vaddr = vma->vm_start;
1714                phdr.p_paddr = 0;
1715                phdr.p_filesz = maydump(vma) ? sz : 0;
1716                phdr.p_memsz = sz;
1717                offset += phdr.p_filesz;
1718                phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1719                if (vma->vm_flags & VM_WRITE)
1720                        phdr.p_flags |= PF_W;
1721                if (vma->vm_flags & VM_EXEC)
1722                        phdr.p_flags |= PF_X;
1723                phdr.p_align = ELF_EXEC_PAGESIZE;
1724
1725                DUMP_WRITE(&phdr, sizeof(phdr));
1726        }
1727
1728#ifdef ELF_CORE_WRITE_EXTRA_PHDRS
1729        ELF_CORE_WRITE_EXTRA_PHDRS;
1730#endif
1731
1732        /* write out the notes section */
1733        for (i = 0; i < numnote; i++)
1734                if (!writenote(notes + i, file))
1735                        goto end_coredump;
1736
1737        /* write out the thread status notes section */
1738        list_for_each(t, &thread_list) {
1739                struct elf_thread_status *tmp =
1740                                list_entry(t, struct elf_thread_status, list);
1741
1742                for (i = 0; i < tmp->num_notes; i++)
1743                        if (!writenote(&tmp->notes[i], file))
1744                                goto end_coredump;
1745        }
1746
1747        DUMP_SEEK(dataoff);
1748
1749        if (elf_fdpic_dump_segments(file, current->mm, &size, &limit) < 0)
1750                goto end_coredump;
1751
1752#ifdef ELF_CORE_WRITE_EXTRA_DATA
1753        ELF_CORE_WRITE_EXTRA_DATA;
1754#endif
1755
1756        if (file->f_pos != offset) {
1757                /* Sanity check */
1758                printk(KERN_WARNING
1759                       "elf_core_dump: file->f_pos (%lld) != offset (%lld)\n",
1760                       file->f_pos, offset);
1761        }
1762
1763end_coredump:
1764        set_fs(fs);
1765
1766cleanup:
1767        while (!list_empty(&thread_list)) {
1768                struct list_head *tmp = thread_list.next;
1769                list_del(tmp);
1770                kfree(list_entry(tmp, struct elf_thread_status, list));
1771        }
1772
1773        kfree(elf);
1774        kfree(prstatus);
1775        kfree(psinfo);
1776        kfree(notes);
1777        kfree(fpu);
1778#ifdef ELF_CORE_COPY_XFPREGS
1779        kfree(xfpu);
1780#endif
1781        return has_dumped;
1782#undef NUM_NOTES
1783}
1784
1785#endif          /* USE_ELF_CORE_DUMP */
1786