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