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