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