1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/fs/binfmt_elf.c 4 * 5 * These are the functions used to load ELF format executables as used 6 * on SVr4 machines. Information on the format may be found in the book 7 * "UNIX SYSTEM V RELEASE 4 Programmers Guide: Ansi C and Programming Support 8 * Tools". 9 * 10 * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com). 11 */ 12 13 #include <linux/module.h> 14 #include <linux/kernel.h> 15 #include <linux/fs.h> 16 #include <linux/log2.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/slab.h> 25 #include <linux/personality.h> 26 #include <linux/elfcore.h> 27 #include <linux/init.h> 28 #include <linux/highuid.h> 29 #include <linux/compiler.h> 30 #include <linux/highmem.h> 31 #include <linux/hugetlb.h> 32 #include <linux/pagemap.h> 33 #include <linux/vmalloc.h> 34 #include <linux/security.h> 35 #include <linux/random.h> 36 #include <linux/elf.h> 37 #include <linux/elf-randomize.h> 38 #include <linux/utsname.h> 39 #include <linux/coredump.h> 40 #include <linux/sched.h> 41 #include <linux/sched/coredump.h> 42 #include <linux/sched/task_stack.h> 43 #include <linux/sched/cputime.h> 44 #include <linux/sizes.h> 45 #include <linux/types.h> 46 #include <linux/cred.h> 47 #include <linux/dax.h> 48 #include <linux/uaccess.h> 49 #include <uapi/linux/rseq.h> 50 #include <linux/rseq.h> 51 #include <asm/param.h> 52 #include <asm/page.h> 53 54 #ifndef ELF_COMPAT 55 #define ELF_COMPAT 0 56 #endif 57 58 #ifndef user_long_t 59 #define user_long_t long 60 #endif 61 #ifndef user_siginfo_t 62 #define user_siginfo_t siginfo_t 63 #endif 64 65 /* That's for binfmt_elf_fdpic to deal with */ 66 #ifndef elf_check_fdpic 67 #define elf_check_fdpic(ex) false 68 #endif 69 70 static int load_elf_binary(struct linux_binprm *bprm); 71 72 /* 73 * If we don't support core dumping, then supply a NULL so we 74 * don't even try. 75 */ 76 #ifdef CONFIG_ELF_CORE 77 static int elf_core_dump(struct coredump_params *cprm); 78 #else 79 #define elf_core_dump NULL 80 #endif 81 82 #if ELF_EXEC_PAGESIZE > PAGE_SIZE 83 #define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE 84 #else 85 #define ELF_MIN_ALIGN PAGE_SIZE 86 #endif 87 88 #ifndef ELF_CORE_EFLAGS 89 #define ELF_CORE_EFLAGS 0 90 #endif 91 92 #define ELF_PAGESTART(_v) ((_v) & ~(int)(ELF_MIN_ALIGN-1)) 93 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1)) 94 #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1)) 95 96 static struct linux_binfmt elf_format = { 97 .module = THIS_MODULE, 98 .load_binary = load_elf_binary, 99 #ifdef CONFIG_COREDUMP 100 .core_dump = elf_core_dump, 101 .min_coredump = ELF_EXEC_PAGESIZE, 102 #endif 103 }; 104 105 #define BAD_ADDR(x) (unlikely((unsigned long)(x) >= TASK_SIZE)) 106 107 static inline void elf_coredump_set_mm_eflags(struct mm_struct *mm, u32 flags) 108 { 109 #ifdef CONFIG_ARCH_HAS_ELF_CORE_EFLAGS 110 mm->saved_e_flags = flags; 111 #endif 112 } 113 114 static inline u32 elf_coredump_get_mm_eflags(struct mm_struct *mm, u32 flags) 115 { 116 #ifdef CONFIG_ARCH_HAS_ELF_CORE_EFLAGS 117 flags = mm->saved_e_flags; 118 #endif 119 return flags; 120 } 121 122 /* 123 * We need to explicitly zero any trailing portion of the page that follows 124 * p_filesz when it ends before the page ends (e.g. bss), otherwise this 125 * memory will contain the junk from the file that should not be present. 126 */ 127 static int padzero(unsigned long address) 128 { 129 unsigned long nbyte; 130 131 nbyte = ELF_PAGEOFFSET(address); 132 if (nbyte) { 133 nbyte = ELF_MIN_ALIGN - nbyte; 134 if (clear_user((void __user *)address, nbyte)) 135 return -EFAULT; 136 } 137 return 0; 138 } 139 140 /* Let's use some macros to make this stack manipulation a little clearer */ 141 #ifdef CONFIG_STACK_GROWSUP 142 #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) + (items)) 143 #define STACK_ROUND(sp, items) \ 144 ((15 + (unsigned long) ((sp) + (items))) &~ 15UL) 145 #define STACK_ALLOC(sp, len) ({ \ 146 elf_addr_t __user *old_sp = (elf_addr_t __user *)sp; sp += len; \ 147 old_sp; }) 148 #else 149 #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) - (items)) 150 #define STACK_ROUND(sp, items) \ 151 (((unsigned long) (sp - items)) &~ 15UL) 152 #define STACK_ALLOC(sp, len) (sp -= len) 153 #endif 154 155 #ifndef ELF_BASE_PLATFORM 156 /* 157 * AT_BASE_PLATFORM indicates the "real" hardware/microarchitecture. 158 * If the arch defines ELF_BASE_PLATFORM (in asm/elf.h), the value 159 * will be copied to the user stack in the same manner as AT_PLATFORM. 160 */ 161 #define ELF_BASE_PLATFORM NULL 162 #endif 163 164 static int 165 create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec, 166 unsigned long interp_load_addr, 167 unsigned long e_entry, unsigned long phdr_addr) 168 { 169 struct mm_struct *mm = current->mm; 170 unsigned long p = bprm->p; 171 int argc = bprm->argc; 172 int envc = bprm->envc; 173 elf_addr_t __user *sp; 174 elf_addr_t __user *u_platform; 175 elf_addr_t __user *u_base_platform; 176 elf_addr_t __user *u_rand_bytes; 177 const char *k_platform = ELF_PLATFORM; 178 const char *k_base_platform = ELF_BASE_PLATFORM; 179 unsigned char k_rand_bytes[16]; 180 int items; 181 elf_addr_t *elf_info; 182 int ei_index; 183 const struct cred *cred = current_cred(); 184 struct vm_area_struct *vma; 185 186 /* 187 * In some cases (e.g. Hyper-Threading), we want to avoid L1 188 * evictions by the processes running on the same package. One 189 * thing we can do is to shuffle the initial stack for them. 190 */ 191 192 p = arch_align_stack(p); 193 194 /* 195 * If this architecture has a platform capability string, copy it 196 * to userspace. In some cases (Sparc), this info is impossible 197 * for userspace to get any other way, in others (i386) it is 198 * merely difficult. 199 */ 200 u_platform = NULL; 201 if (k_platform) { 202 size_t len = strlen(k_platform) + 1; 203 204 u_platform = (elf_addr_t __user *)STACK_ALLOC(p, len); 205 if (copy_to_user(u_platform, k_platform, len)) 206 return -EFAULT; 207 } 208 209 /* 210 * If this architecture has a "base" platform capability 211 * string, copy it to userspace. 212 */ 213 u_base_platform = NULL; 214 if (k_base_platform) { 215 size_t len = strlen(k_base_platform) + 1; 216 217 u_base_platform = (elf_addr_t __user *)STACK_ALLOC(p, len); 218 if (copy_to_user(u_base_platform, k_base_platform, len)) 219 return -EFAULT; 220 } 221 222 /* 223 * Generate 16 random bytes for userspace PRNG seeding. 224 */ 225 get_random_bytes(k_rand_bytes, sizeof(k_rand_bytes)); 226 u_rand_bytes = (elf_addr_t __user *) 227 STACK_ALLOC(p, sizeof(k_rand_bytes)); 228 if (copy_to_user(u_rand_bytes, k_rand_bytes, sizeof(k_rand_bytes))) 229 return -EFAULT; 230 231 /* Create the ELF interpreter info */ 232 elf_info = (elf_addr_t *)mm->saved_auxv; 233 /* update AT_VECTOR_SIZE_BASE if the number of NEW_AUX_ENT() changes */ 234 #define NEW_AUX_ENT(id, val) \ 235 do { \ 236 *elf_info++ = id; \ 237 *elf_info++ = val; \ 238 } while (0) 239 240 #ifdef ARCH_DLINFO 241 /* 242 * ARCH_DLINFO must come first so PPC can do its special alignment of 243 * AUXV. 244 * update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT() in 245 * ARCH_DLINFO changes 246 */ 247 ARCH_DLINFO; 248 #endif 249 NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP); 250 NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE); 251 NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC); 252 NEW_AUX_ENT(AT_PHDR, phdr_addr); 253 NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr)); 254 NEW_AUX_ENT(AT_PHNUM, exec->e_phnum); 255 NEW_AUX_ENT(AT_BASE, interp_load_addr); 256 NEW_AUX_ENT(AT_FLAGS, bprm_at_flags(bprm)); 257 NEW_AUX_ENT(AT_ENTRY, e_entry); 258 NEW_AUX_ENT(AT_UID, from_kuid_munged(cred->user_ns, cred->uid)); 259 NEW_AUX_ENT(AT_EUID, from_kuid_munged(cred->user_ns, cred->euid)); 260 NEW_AUX_ENT(AT_GID, from_kgid_munged(cred->user_ns, cred->gid)); 261 NEW_AUX_ENT(AT_EGID, from_kgid_munged(cred->user_ns, cred->egid)); 262 NEW_AUX_ENT(AT_SECURE, bprm->secureexec); 263 NEW_AUX_ENT(AT_RANDOM, (elf_addr_t)(unsigned long)u_rand_bytes); 264 #ifdef ELF_HWCAP2 265 NEW_AUX_ENT(AT_HWCAP2, ELF_HWCAP2); 266 #endif 267 #ifdef ELF_HWCAP3 268 NEW_AUX_ENT(AT_HWCAP3, ELF_HWCAP3); 269 #endif 270 #ifdef ELF_HWCAP4 271 NEW_AUX_ENT(AT_HWCAP4, ELF_HWCAP4); 272 #endif 273 NEW_AUX_ENT(AT_EXECFN, bprm->exec); 274 if (k_platform) { 275 NEW_AUX_ENT(AT_PLATFORM, 276 (elf_addr_t)(unsigned long)u_platform); 277 } 278 if (k_base_platform) { 279 NEW_AUX_ENT(AT_BASE_PLATFORM, 280 (elf_addr_t)(unsigned long)u_base_platform); 281 } 282 if (bprm->have_execfd) { 283 NEW_AUX_ENT(AT_EXECFD, bprm->execfd); 284 } 285 #ifdef CONFIG_RSEQ 286 NEW_AUX_ENT(AT_RSEQ_FEATURE_SIZE, offsetof(struct rseq, end)); 287 NEW_AUX_ENT(AT_RSEQ_ALIGN, rseq_alloc_align()); 288 #endif 289 #undef NEW_AUX_ENT 290 /* AT_NULL is zero; clear the rest too */ 291 memset(elf_info, 0, (char *)mm->saved_auxv + 292 sizeof(mm->saved_auxv) - (char *)elf_info); 293 294 /* And advance past the AT_NULL entry. */ 295 elf_info += 2; 296 297 ei_index = elf_info - (elf_addr_t *)mm->saved_auxv; 298 sp = STACK_ADD(p, ei_index); 299 300 items = (argc + 1) + (envc + 1) + 1; 301 bprm->p = STACK_ROUND(sp, items); 302 303 /* Point sp at the lowest address on the stack */ 304 #ifdef CONFIG_STACK_GROWSUP 305 sp = (elf_addr_t __user *)bprm->p - items - ei_index; 306 bprm->exec = (unsigned long)sp; /* XXX: PARISC HACK */ 307 #else 308 sp = (elf_addr_t __user *)bprm->p; 309 #endif 310 311 312 /* 313 * Grow the stack manually; some architectures have a limit on how 314 * far ahead a user-space access may be in order to grow the stack. 315 */ 316 if (mmap_write_lock_killable(mm)) 317 return -EINTR; 318 vma = find_extend_vma_locked(mm, bprm->p); 319 mmap_write_unlock(mm); 320 if (!vma) 321 return -EFAULT; 322 323 /* Now, let's put argc (and argv, envp if appropriate) on the stack */ 324 if (put_user(argc, sp++)) 325 return -EFAULT; 326 327 /* Populate list of argv pointers back to argv strings. */ 328 p = mm->arg_end = mm->arg_start; 329 while (argc-- > 0) { 330 size_t len; 331 if (put_user((elf_addr_t)p, sp++)) 332 return -EFAULT; 333 len = strnlen_user((void __user *)p, MAX_ARG_STRLEN); 334 if (!len || len > MAX_ARG_STRLEN) 335 return -EINVAL; 336 p += len; 337 } 338 if (put_user(0, sp++)) 339 return -EFAULT; 340 mm->arg_end = p; 341 342 /* Populate list of envp pointers back to envp strings. */ 343 mm->env_end = mm->env_start = p; 344 while (envc-- > 0) { 345 size_t len; 346 if (put_user((elf_addr_t)p, sp++)) 347 return -EFAULT; 348 len = strnlen_user((void __user *)p, MAX_ARG_STRLEN); 349 if (!len || len > MAX_ARG_STRLEN) 350 return -EINVAL; 351 p += len; 352 } 353 if (put_user(0, sp++)) 354 return -EFAULT; 355 mm->env_end = p; 356 357 /* Put the elf_info on the stack in the right place. */ 358 if (copy_to_user(sp, mm->saved_auxv, ei_index * sizeof(elf_addr_t))) 359 return -EFAULT; 360 return 0; 361 } 362 363 /* 364 * Map "eppnt->p_filesz" bytes from "filep" offset "eppnt->p_offset" 365 * into memory at "addr". (Note that p_filesz is rounded up to the 366 * next page, so any extra bytes from the file must be wiped.) 367 */ 368 static unsigned long elf_map(struct file *filep, unsigned long addr, 369 const struct elf_phdr *eppnt, int prot, int type, 370 unsigned long total_size) 371 { 372 unsigned long map_addr; 373 unsigned long size = eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr); 374 unsigned long off = eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr); 375 addr = ELF_PAGESTART(addr); 376 size = ELF_PAGEALIGN(size); 377 378 /* mmap() will return -EINVAL if given a zero size, but a 379 * segment with zero filesize is perfectly valid */ 380 if (!size) 381 return addr; 382 383 /* 384 * total_size is the size of the ELF (interpreter) image. 385 * The _first_ mmap needs to know the full size, otherwise 386 * randomization might put this image into an overlapping 387 * position with the ELF binary image. (since size < total_size) 388 * So we first map the 'big' image - and unmap the remainder at 389 * the end. (which unmap is needed for ELF images with holes.) 390 */ 391 if (total_size) { 392 total_size = ELF_PAGEALIGN(total_size); 393 map_addr = vm_mmap(filep, addr, total_size, prot, type, off); 394 if (!BAD_ADDR(map_addr)) 395 vm_munmap(map_addr+size, total_size-size); 396 } else 397 map_addr = vm_mmap(filep, addr, size, prot, type, off); 398 399 if ((type & MAP_FIXED_NOREPLACE) && 400 PTR_ERR((void *)map_addr) == -EEXIST) 401 pr_info("%d (%s): Uhuuh, elf segment at %px requested but the memory is mapped already\n", 402 task_pid_nr(current), current->comm, (void *)addr); 403 404 return(map_addr); 405 } 406 407 /* 408 * Map "eppnt->p_filesz" bytes from "filep" offset "eppnt->p_offset" 409 * into memory at "addr". Memory from "p_filesz" through "p_memsz" 410 * rounded up to the next page is zeroed. 411 */ 412 static unsigned long elf_load(struct file *filep, unsigned long addr, 413 const struct elf_phdr *eppnt, int prot, int type, 414 unsigned long total_size) 415 { 416 unsigned long zero_start, zero_end; 417 unsigned long map_addr; 418 419 if (eppnt->p_filesz) { 420 map_addr = elf_map(filep, addr, eppnt, prot, type, total_size); 421 if (BAD_ADDR(map_addr)) 422 return map_addr; 423 if (eppnt->p_memsz > eppnt->p_filesz) { 424 zero_start = map_addr + ELF_PAGEOFFSET(eppnt->p_vaddr) + 425 eppnt->p_filesz; 426 zero_end = map_addr + ELF_PAGEOFFSET(eppnt->p_vaddr) + 427 eppnt->p_memsz; 428 429 /* 430 * Zero the end of the last mapped page but ignore 431 * any errors if the segment isn't writable. 432 */ 433 if (padzero(zero_start) && (prot & PROT_WRITE)) 434 return -EFAULT; 435 } 436 } else { 437 map_addr = zero_start = ELF_PAGESTART(addr); 438 zero_end = zero_start + ELF_PAGEOFFSET(eppnt->p_vaddr) + 439 eppnt->p_memsz; 440 } 441 if (eppnt->p_memsz > eppnt->p_filesz) { 442 /* 443 * Map the last of the segment. 444 * If the header is requesting these pages to be 445 * executable, honour that (ppc32 needs this). 446 */ 447 int error; 448 449 zero_start = ELF_PAGEALIGN(zero_start); 450 zero_end = ELF_PAGEALIGN(zero_end); 451 452 error = vm_brk_flags(zero_start, zero_end - zero_start, 453 prot & PROT_EXEC); 454 if (error) 455 map_addr = error; 456 } 457 return map_addr; 458 } 459 460 static unsigned long total_mapping_size(const struct elf_phdr *phdr, int nr) 461 { 462 elf_addr_t min_addr = -1; 463 elf_addr_t max_addr = 0; 464 bool pt_load = false; 465 int i; 466 467 for (i = 0; i < nr; i++) { 468 if (phdr[i].p_type == PT_LOAD) { 469 min_addr = min(min_addr, ELF_PAGESTART(phdr[i].p_vaddr)); 470 max_addr = max(max_addr, phdr[i].p_vaddr + phdr[i].p_memsz); 471 pt_load = true; 472 } 473 } 474 return pt_load ? (max_addr - min_addr) : 0; 475 } 476 477 static int elf_read(struct file *file, void *buf, size_t len, loff_t pos) 478 { 479 ssize_t rv; 480 481 rv = kernel_read(file, buf, len, &pos); 482 if (unlikely(rv != len)) { 483 return (rv < 0) ? rv : -EIO; 484 } 485 return 0; 486 } 487 488 static unsigned long maximum_alignment(struct elf_phdr *cmds, int nr) 489 { 490 unsigned long alignment = 0; 491 int i; 492 493 for (i = 0; i < nr; i++) { 494 if (cmds[i].p_type == PT_LOAD) { 495 unsigned long p_align = cmds[i].p_align; 496 497 /* skip non-power of two alignments as invalid */ 498 if (!is_power_of_2(p_align)) 499 continue; 500 alignment = max(alignment, p_align); 501 } 502 } 503 504 /* ensure we align to at least one page */ 505 return ELF_PAGEALIGN(alignment); 506 } 507 508 /** 509 * load_elf_phdrs() - load ELF program headers 510 * @elf_ex: ELF header of the binary whose program headers should be loaded 511 * @elf_file: the opened ELF binary file 512 * 513 * Loads ELF program headers from the binary file elf_file, which has the ELF 514 * header pointed to by elf_ex, into a newly allocated array. The caller is 515 * responsible for freeing the allocated data. Returns NULL upon failure. 516 */ 517 static struct elf_phdr *load_elf_phdrs(const struct elfhdr *elf_ex, 518 struct file *elf_file) 519 { 520 struct elf_phdr *elf_phdata = NULL; 521 int retval = -1; 522 unsigned int size; 523 524 /* 525 * If the size of this structure has changed, then punt, since 526 * we will be doing the wrong thing. 527 */ 528 if (elf_ex->e_phentsize != sizeof(struct elf_phdr)) 529 goto out; 530 531 /* Sanity check the number of program headers... */ 532 /* ...and their total size. */ 533 size = sizeof(struct elf_phdr) * elf_ex->e_phnum; 534 if (size == 0 || size > 65536) 535 goto out; 536 537 elf_phdata = kmalloc(size, GFP_KERNEL); 538 if (!elf_phdata) 539 goto out; 540 541 /* Read in the program headers */ 542 retval = elf_read(elf_file, elf_phdata, size, elf_ex->e_phoff); 543 544 out: 545 if (retval) { 546 kfree(elf_phdata); 547 elf_phdata = NULL; 548 } 549 return elf_phdata; 550 } 551 552 #ifndef CONFIG_ARCH_BINFMT_ELF_STATE 553 554 /** 555 * struct arch_elf_state - arch-specific ELF loading state 556 * 557 * This structure is used to preserve architecture specific data during 558 * the loading of an ELF file, throughout the checking of architecture 559 * specific ELF headers & through to the point where the ELF load is 560 * known to be proceeding (ie. SET_PERSONALITY). 561 * 562 * This implementation is a dummy for architectures which require no 563 * specific state. 564 */ 565 struct arch_elf_state { 566 }; 567 568 #define INIT_ARCH_ELF_STATE {} 569 570 /** 571 * arch_elf_pt_proc() - check a PT_LOPROC..PT_HIPROC ELF program header 572 * @ehdr: The main ELF header 573 * @phdr: The program header to check 574 * @elf: The open ELF file 575 * @is_interp: True if the phdr is from the interpreter of the ELF being 576 * loaded, else false. 577 * @state: Architecture-specific state preserved throughout the process 578 * of loading the ELF. 579 * 580 * Inspects the program header phdr to validate its correctness and/or 581 * suitability for the system. Called once per ELF program header in the 582 * range PT_LOPROC to PT_HIPROC, for both the ELF being loaded and its 583 * interpreter. 584 * 585 * Return: Zero to proceed with the ELF load, non-zero to fail the ELF load 586 * with that return code. 587 */ 588 static inline int arch_elf_pt_proc(struct elfhdr *ehdr, 589 struct elf_phdr *phdr, 590 struct file *elf, bool is_interp, 591 struct arch_elf_state *state) 592 { 593 /* Dummy implementation, always proceed */ 594 return 0; 595 } 596 597 /** 598 * arch_check_elf() - check an ELF executable 599 * @ehdr: The main ELF header 600 * @has_interp: True if the ELF has an interpreter, else false. 601 * @interp_ehdr: The interpreter's ELF header 602 * @state: Architecture-specific state preserved throughout the process 603 * of loading the ELF. 604 * 605 * Provides a final opportunity for architecture code to reject the loading 606 * of the ELF & cause an exec syscall to return an error. This is called after 607 * all program headers to be checked by arch_elf_pt_proc have been. 608 * 609 * Return: Zero to proceed with the ELF load, non-zero to fail the ELF load 610 * with that return code. 611 */ 612 static inline int arch_check_elf(struct elfhdr *ehdr, bool has_interp, 613 struct elfhdr *interp_ehdr, 614 struct arch_elf_state *state) 615 { 616 /* Dummy implementation, always proceed */ 617 return 0; 618 } 619 620 #endif /* !CONFIG_ARCH_BINFMT_ELF_STATE */ 621 622 static inline int make_prot(u32 p_flags, struct arch_elf_state *arch_state, 623 bool has_interp, bool is_interp) 624 { 625 int prot = 0; 626 627 if (p_flags & PF_R) 628 prot |= PROT_READ; 629 if (p_flags & PF_W) 630 prot |= PROT_WRITE; 631 if (p_flags & PF_X) 632 prot |= PROT_EXEC; 633 634 return arch_elf_adjust_prot(prot, arch_state, has_interp, is_interp); 635 } 636 637 /* This is much more generalized than the library routine read function, 638 so we keep this separate. Technically the library read function 639 is only provided so that we can read a.out libraries that have 640 an ELF header */ 641 642 static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex, 643 struct file *interpreter, 644 unsigned long no_base, struct elf_phdr *interp_elf_phdata, 645 struct arch_elf_state *arch_state) 646 { 647 struct elf_phdr *eppnt; 648 unsigned long load_addr = 0; 649 int load_addr_set = 0; 650 unsigned long error = ~0UL; 651 unsigned long total_size; 652 int i; 653 654 /* First of all, some simple consistency checks */ 655 if (interp_elf_ex->e_type != ET_EXEC && 656 interp_elf_ex->e_type != ET_DYN) 657 goto out; 658 if (!elf_check_arch(interp_elf_ex) || 659 elf_check_fdpic(interp_elf_ex)) 660 goto out; 661 if (!can_mmap_file(interpreter)) 662 goto out; 663 664 total_size = total_mapping_size(interp_elf_phdata, 665 interp_elf_ex->e_phnum); 666 if (!total_size) { 667 error = -EINVAL; 668 goto out; 669 } 670 671 eppnt = interp_elf_phdata; 672 for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++) { 673 if (eppnt->p_type == PT_LOAD) { 674 int elf_type = MAP_PRIVATE; 675 int elf_prot = make_prot(eppnt->p_flags, arch_state, 676 true, true); 677 unsigned long vaddr = 0; 678 unsigned long k, map_addr; 679 680 vaddr = eppnt->p_vaddr; 681 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set) 682 elf_type |= MAP_FIXED; 683 else if (no_base && interp_elf_ex->e_type == ET_DYN) 684 load_addr = -vaddr; 685 686 map_addr = elf_load(interpreter, load_addr + vaddr, 687 eppnt, elf_prot, elf_type, total_size); 688 total_size = 0; 689 error = map_addr; 690 if (BAD_ADDR(map_addr)) 691 goto out; 692 693 if (!load_addr_set && 694 interp_elf_ex->e_type == ET_DYN) { 695 load_addr = map_addr - ELF_PAGESTART(vaddr); 696 load_addr_set = 1; 697 } 698 699 /* 700 * Check to see if the section's size will overflow the 701 * allowed task size. Note that p_filesz must always be 702 * <= p_memsize so it's only necessary to check p_memsz. 703 */ 704 k = load_addr + eppnt->p_vaddr; 705 if (BAD_ADDR(k) || 706 eppnt->p_filesz > eppnt->p_memsz || 707 eppnt->p_memsz > TASK_SIZE || 708 TASK_SIZE - eppnt->p_memsz < k) { 709 error = -ENOMEM; 710 goto out; 711 } 712 } 713 } 714 715 error = load_addr; 716 out: 717 return error; 718 } 719 720 /* 721 * These are the functions used to load ELF style executables and shared 722 * libraries. There is no binary dependent code anywhere else. 723 */ 724 725 static int parse_elf_property(const char *data, size_t *off, size_t datasz, 726 struct arch_elf_state *arch, 727 bool have_prev_type, u32 *prev_type) 728 { 729 size_t o, step; 730 const struct gnu_property *pr; 731 int ret; 732 733 if (*off == datasz) 734 return -ENOENT; 735 736 if (WARN_ON_ONCE(*off > datasz || *off % ELF_GNU_PROPERTY_ALIGN)) 737 return -EIO; 738 o = *off; 739 datasz -= *off; 740 741 if (datasz < sizeof(*pr)) 742 return -ENOEXEC; 743 pr = (const struct gnu_property *)(data + o); 744 o += sizeof(*pr); 745 datasz -= sizeof(*pr); 746 747 if (pr->pr_datasz > datasz) 748 return -ENOEXEC; 749 750 WARN_ON_ONCE(o % ELF_GNU_PROPERTY_ALIGN); 751 step = round_up(pr->pr_datasz, ELF_GNU_PROPERTY_ALIGN); 752 if (step > datasz) 753 return -ENOEXEC; 754 755 /* Properties are supposed to be unique and sorted on pr_type: */ 756 if (have_prev_type && pr->pr_type <= *prev_type) 757 return -ENOEXEC; 758 *prev_type = pr->pr_type; 759 760 ret = arch_parse_elf_property(pr->pr_type, data + o, 761 pr->pr_datasz, ELF_COMPAT, arch); 762 if (ret) 763 return ret; 764 765 *off = o + step; 766 return 0; 767 } 768 769 #define NOTE_DATA_SZ SZ_1K 770 #define NOTE_NAME_SZ (sizeof(NN_GNU_PROPERTY_TYPE_0)) 771 772 static int parse_elf_properties(struct file *f, const struct elf_phdr *phdr, 773 struct arch_elf_state *arch) 774 { 775 union { 776 struct elf_note nhdr; 777 char data[NOTE_DATA_SZ]; 778 } note; 779 loff_t pos; 780 ssize_t n; 781 size_t off, datasz; 782 int ret; 783 bool have_prev_type; 784 u32 prev_type; 785 786 if (!IS_ENABLED(CONFIG_ARCH_USE_GNU_PROPERTY) || !phdr) 787 return 0; 788 789 /* load_elf_binary() shouldn't call us unless this is true... */ 790 if (WARN_ON_ONCE(phdr->p_type != PT_GNU_PROPERTY)) 791 return -ENOEXEC; 792 793 /* If the properties are crazy large, that's too bad (for now): */ 794 if (phdr->p_filesz > sizeof(note)) 795 return -ENOEXEC; 796 797 pos = phdr->p_offset; 798 n = kernel_read(f, ¬e, phdr->p_filesz, &pos); 799 800 BUILD_BUG_ON(sizeof(note) < sizeof(note.nhdr) + NOTE_NAME_SZ); 801 if (n < 0 || n < sizeof(note.nhdr) + NOTE_NAME_SZ) 802 return -EIO; 803 804 if (note.nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 || 805 note.nhdr.n_namesz != NOTE_NAME_SZ || 806 strncmp(note.data + sizeof(note.nhdr), 807 NN_GNU_PROPERTY_TYPE_0, n - sizeof(note.nhdr))) 808 return -ENOEXEC; 809 810 off = round_up(sizeof(note.nhdr) + NOTE_NAME_SZ, 811 ELF_GNU_PROPERTY_ALIGN); 812 if (off > n) 813 return -ENOEXEC; 814 815 if (note.nhdr.n_descsz > n - off) 816 return -ENOEXEC; 817 datasz = off + note.nhdr.n_descsz; 818 819 have_prev_type = false; 820 do { 821 ret = parse_elf_property(note.data, &off, datasz, arch, 822 have_prev_type, &prev_type); 823 have_prev_type = true; 824 } while (!ret); 825 826 return ret == -ENOENT ? 0 : ret; 827 } 828 829 static int load_elf_binary(struct linux_binprm *bprm) 830 { 831 struct file *interpreter = NULL; /* to shut gcc up */ 832 unsigned long load_bias = 0, phdr_addr = 0; 833 int first_pt_load = 1; 834 unsigned long error; 835 struct elf_phdr *elf_ppnt, *elf_phdata, *interp_elf_phdata = NULL; 836 struct elf_phdr *elf_property_phdata = NULL; 837 unsigned long elf_brk; 838 bool brk_moved = false; 839 int retval, i; 840 unsigned long elf_entry; 841 unsigned long e_entry; 842 unsigned long interp_load_addr = 0; 843 unsigned long start_code, end_code, start_data, end_data; 844 unsigned long reloc_func_desc __maybe_unused = 0; 845 int executable_stack = EXSTACK_DEFAULT; 846 struct elfhdr *elf_ex = (struct elfhdr *)bprm->buf; 847 struct elfhdr *interp_elf_ex = NULL; 848 struct arch_elf_state arch_state = INIT_ARCH_ELF_STATE; 849 struct mm_struct *mm; 850 struct pt_regs *regs; 851 852 retval = -ENOEXEC; 853 /* First of all, some simple consistency checks */ 854 if (memcmp(elf_ex->e_ident, ELFMAG, SELFMAG) != 0) 855 goto out; 856 857 if (elf_ex->e_type != ET_EXEC && elf_ex->e_type != ET_DYN) 858 goto out; 859 if (!elf_check_arch(elf_ex)) 860 goto out; 861 if (elf_check_fdpic(elf_ex)) 862 goto out; 863 if (!can_mmap_file(bprm->file)) 864 goto out; 865 866 elf_phdata = load_elf_phdrs(elf_ex, bprm->file); 867 if (!elf_phdata) 868 goto out; 869 870 elf_ppnt = elf_phdata; 871 for (i = 0; i < elf_ex->e_phnum; i++, elf_ppnt++) { 872 char *elf_interpreter; 873 874 if (elf_ppnt->p_type == PT_GNU_PROPERTY) { 875 elf_property_phdata = elf_ppnt; 876 continue; 877 } 878 879 if (elf_ppnt->p_type != PT_INTERP) 880 continue; 881 882 /* 883 * This is the program interpreter used for shared libraries - 884 * for now assume that this is an a.out format binary. 885 */ 886 retval = -ENOEXEC; 887 if (elf_ppnt->p_filesz > PATH_MAX || elf_ppnt->p_filesz < 2) 888 goto out_free_ph; 889 890 retval = -ENOMEM; 891 elf_interpreter = kmalloc(elf_ppnt->p_filesz, GFP_KERNEL); 892 if (!elf_interpreter) 893 goto out_free_ph; 894 895 retval = elf_read(bprm->file, elf_interpreter, elf_ppnt->p_filesz, 896 elf_ppnt->p_offset); 897 if (retval < 0) 898 goto out_free_interp; 899 /* make sure path is NULL terminated */ 900 retval = -ENOEXEC; 901 if (elf_interpreter[elf_ppnt->p_filesz - 1] != '\0') 902 goto out_free_interp; 903 904 interpreter = bprm_open_interpreter(bprm, elf_interpreter); 905 kfree(elf_interpreter); 906 retval = PTR_ERR(interpreter); 907 if (IS_ERR(interpreter)) 908 goto out_free_ph; 909 910 /* 911 * If the binary is not readable then enforce mm->dumpable = 0 912 * regardless of the interpreter's permissions. 913 */ 914 would_dump(bprm, interpreter); 915 916 interp_elf_ex = kmalloc_obj(*interp_elf_ex); 917 if (!interp_elf_ex) { 918 retval = -ENOMEM; 919 goto out_free_file; 920 } 921 922 /* Get the exec headers */ 923 retval = elf_read(interpreter, interp_elf_ex, 924 sizeof(*interp_elf_ex), 0); 925 if (retval < 0) 926 goto out_free_dentry; 927 928 break; 929 930 out_free_interp: 931 kfree(elf_interpreter); 932 goto out_free_ph; 933 } 934 935 /* No PT_INTERP to substitute for: the override does not apply. */ 936 bprm_drop_loader(bprm); 937 938 elf_ppnt = elf_phdata; 939 for (i = 0; i < elf_ex->e_phnum; i++, elf_ppnt++) 940 switch (elf_ppnt->p_type) { 941 case PT_GNU_STACK: 942 if (elf_ppnt->p_flags & PF_X) 943 executable_stack = EXSTACK_ENABLE_X; 944 else 945 executable_stack = EXSTACK_DISABLE_X; 946 break; 947 948 case PT_LOPROC ... PT_HIPROC: 949 retval = arch_elf_pt_proc(elf_ex, elf_ppnt, 950 bprm->file, false, 951 &arch_state); 952 if (retval) 953 goto out_free_dentry; 954 break; 955 } 956 957 /* Some simple consistency checks for the interpreter */ 958 if (interpreter) { 959 retval = -ELIBBAD; 960 /* Not an ELF interpreter */ 961 if (memcmp(interp_elf_ex->e_ident, ELFMAG, SELFMAG) != 0) 962 goto out_free_dentry; 963 /* Verify the interpreter has a valid arch */ 964 if (!elf_check_arch(interp_elf_ex) || 965 elf_check_fdpic(interp_elf_ex)) 966 goto out_free_dentry; 967 968 /* Load the interpreter program headers */ 969 interp_elf_phdata = load_elf_phdrs(interp_elf_ex, 970 interpreter); 971 if (!interp_elf_phdata) 972 goto out_free_dentry; 973 974 /* Pass PT_LOPROC..PT_HIPROC headers to arch code */ 975 elf_property_phdata = NULL; 976 elf_ppnt = interp_elf_phdata; 977 for (i = 0; i < interp_elf_ex->e_phnum; i++, elf_ppnt++) 978 switch (elf_ppnt->p_type) { 979 case PT_GNU_PROPERTY: 980 elf_property_phdata = elf_ppnt; 981 break; 982 983 case PT_LOPROC ... PT_HIPROC: 984 retval = arch_elf_pt_proc(interp_elf_ex, 985 elf_ppnt, interpreter, 986 true, &arch_state); 987 if (retval) 988 goto out_free_dentry; 989 break; 990 } 991 } 992 993 retval = parse_elf_properties(interpreter ?: bprm->file, 994 elf_property_phdata, &arch_state); 995 if (retval) 996 goto out_free_dentry; 997 998 /* 999 * Allow arch code to reject the ELF at this point, whilst it's 1000 * still possible to return an error to the code that invoked 1001 * the exec syscall. 1002 */ 1003 retval = arch_check_elf(elf_ex, 1004 !!interpreter, interp_elf_ex, 1005 &arch_state); 1006 if (retval) 1007 goto out_free_dentry; 1008 1009 /* Flush all traces of the currently running executable */ 1010 retval = begin_new_exec(bprm); 1011 if (retval) 1012 goto out_free_dentry; 1013 1014 /* Do this immediately, since STACK_TOP as used in setup_arg_pages 1015 may depend on the personality. */ 1016 SET_PERSONALITY2(*elf_ex, &arch_state); 1017 if (elf_read_implies_exec(*elf_ex, executable_stack)) 1018 current->personality |= READ_IMPLIES_EXEC; 1019 1020 const int snapshot_randomize_va_space = READ_ONCE(randomize_va_space); 1021 if (!(current->personality & ADDR_NO_RANDOMIZE) && snapshot_randomize_va_space) 1022 current->flags |= PF_RANDOMIZE; 1023 1024 setup_new_exec(bprm); 1025 1026 /* Do this so that we can load the interpreter, if need be. We will 1027 change some of these later */ 1028 retval = setup_arg_pages(bprm, randomize_stack_top(STACK_TOP), 1029 executable_stack); 1030 if (retval < 0) 1031 goto out_free_dentry; 1032 1033 elf_brk = 0; 1034 1035 start_code = ~0UL; 1036 end_code = 0; 1037 start_data = 0; 1038 end_data = 0; 1039 1040 /* Now we do a little grungy work by mmapping the ELF image into 1041 the correct location in memory. */ 1042 for(i = 0, elf_ppnt = elf_phdata; 1043 i < elf_ex->e_phnum; i++, elf_ppnt++) { 1044 int elf_prot, elf_flags; 1045 unsigned long k, vaddr; 1046 unsigned long total_size = 0; 1047 unsigned long alignment; 1048 1049 if (elf_ppnt->p_type != PT_LOAD) 1050 continue; 1051 1052 elf_prot = make_prot(elf_ppnt->p_flags, &arch_state, 1053 !!interpreter, false); 1054 1055 elf_flags = MAP_PRIVATE; 1056 1057 vaddr = elf_ppnt->p_vaddr; 1058 /* 1059 * The first time through the loop, first_pt_load is true: 1060 * layout will be calculated. Once set, use MAP_FIXED since 1061 * we know we've already safely mapped the entire region with 1062 * MAP_FIXED_NOREPLACE in the once-per-binary logic following. 1063 */ 1064 if (!first_pt_load) { 1065 elf_flags |= MAP_FIXED; 1066 } else if (elf_ex->e_type == ET_EXEC) { 1067 /* 1068 * This logic is run once for the first LOAD Program 1069 * Header for ET_EXEC binaries. No special handling 1070 * is needed. 1071 */ 1072 elf_flags |= MAP_FIXED_NOREPLACE; 1073 } else if (elf_ex->e_type == ET_DYN) { 1074 /* 1075 * This logic is run once for the first LOAD Program 1076 * Header for ET_DYN binaries to calculate the 1077 * randomization (load_bias) for all the LOAD 1078 * Program Headers. 1079 */ 1080 1081 /* 1082 * Calculate the entire size of the ELF mapping 1083 * (total_size), used for the initial mapping, 1084 * due to load_addr_set which is set to true later 1085 * once the initial mapping is performed. 1086 * 1087 * Note that this is only sensible when the LOAD 1088 * segments are contiguous (or overlapping). If 1089 * used for LOADs that are far apart, this would 1090 * cause the holes between LOADs to be mapped, 1091 * running the risk of having the mapping fail, 1092 * as it would be larger than the ELF file itself. 1093 * 1094 * As a result, only ET_DYN does this, since 1095 * some ET_EXEC (e.g. ia64) may have large virtual 1096 * memory holes between LOADs. 1097 * 1098 */ 1099 total_size = total_mapping_size(elf_phdata, 1100 elf_ex->e_phnum); 1101 if (!total_size) { 1102 retval = -EINVAL; 1103 goto out_free_dentry; 1104 } 1105 1106 /* Calculate any requested alignment. */ 1107 alignment = maximum_alignment(elf_phdata, elf_ex->e_phnum); 1108 1109 /** 1110 * DOC: PIE handling 1111 * 1112 * There are effectively two types of ET_DYN ELF 1113 * binaries: programs (i.e. PIE: ET_DYN with 1114 * PT_INTERP) and loaders (i.e. static PIE: ET_DYN 1115 * without PT_INTERP, usually the ELF interpreter 1116 * itself). Loaders must be loaded away from programs 1117 * since the program may otherwise collide with the 1118 * loader (especially for ET_EXEC which does not have 1119 * a randomized position). 1120 * 1121 * For example, to handle invocations of 1122 * "./ld.so someprog" to test out a new version of 1123 * the loader, the subsequent program that the 1124 * loader loads must avoid the loader itself, so 1125 * they cannot share the same load range. Sufficient 1126 * room for the brk must be allocated with the 1127 * loader as well, since brk must be available with 1128 * the loader. 1129 * 1130 * Therefore, programs are loaded offset from 1131 * ELF_ET_DYN_BASE and loaders are loaded into the 1132 * independently randomized mmap region (0 load_bias 1133 * without MAP_FIXED nor MAP_FIXED_NOREPLACE). 1134 * 1135 * See below for "brk" handling details, which is 1136 * also affected by program vs loader and ASLR. 1137 */ 1138 if (interpreter) { 1139 /* On ET_DYN with PT_INTERP, we do the ASLR. */ 1140 load_bias = ELF_ET_DYN_BASE; 1141 if (current->flags & PF_RANDOMIZE) 1142 load_bias += arch_mmap_rnd(); 1143 /* Adjust alignment as requested. */ 1144 if (alignment) 1145 load_bias &= ~(alignment - 1); 1146 elf_flags |= MAP_FIXED_NOREPLACE; 1147 } else { 1148 /* 1149 * For ET_DYN without PT_INTERP, we rely on 1150 * the architectures's (potentially ASLR) mmap 1151 * base address (via a load_bias of 0). 1152 * 1153 * When a large alignment is requested, we 1154 * must do the allocation at address "0" right 1155 * now to discover where things will load so 1156 * that we can adjust the resulting alignment. 1157 * In this case (load_bias != 0), we can use 1158 * MAP_FIXED_NOREPLACE to make sure the mapping 1159 * doesn't collide with anything. 1160 */ 1161 if (alignment > ELF_MIN_ALIGN) { 1162 load_bias = elf_load(bprm->file, 0, elf_ppnt, 1163 elf_prot, elf_flags, total_size); 1164 if (BAD_ADDR(load_bias)) { 1165 retval = IS_ERR_VALUE(load_bias) ? 1166 PTR_ERR((void*)load_bias) : -EINVAL; 1167 goto out_free_dentry; 1168 } 1169 vm_munmap(load_bias, total_size); 1170 /* Adjust alignment as requested. */ 1171 if (alignment) 1172 load_bias &= ~(alignment - 1); 1173 elf_flags |= MAP_FIXED_NOREPLACE; 1174 } else 1175 load_bias = 0; 1176 } 1177 1178 /* 1179 * Since load_bias is used for all subsequent loading 1180 * calculations, we must lower it by the first vaddr 1181 * so that the remaining calculations based on the 1182 * ELF vaddrs will be correctly offset. The result 1183 * is then page aligned. 1184 */ 1185 load_bias = ELF_PAGESTART(load_bias - vaddr); 1186 } 1187 1188 error = elf_load(bprm->file, load_bias + vaddr, elf_ppnt, 1189 elf_prot, elf_flags, total_size); 1190 if (BAD_ADDR(error)) { 1191 retval = IS_ERR_VALUE(error) ? 1192 PTR_ERR((void*)error) : -EINVAL; 1193 goto out_free_dentry; 1194 } 1195 1196 if (first_pt_load) { 1197 first_pt_load = 0; 1198 if (elf_ex->e_type == ET_DYN) { 1199 load_bias += error - 1200 ELF_PAGESTART(load_bias + vaddr); 1201 reloc_func_desc = load_bias; 1202 } 1203 } 1204 1205 /* 1206 * Figure out which segment in the file contains the Program 1207 * Header table, and map to the associated memory address. 1208 */ 1209 if (elf_ppnt->p_offset <= elf_ex->e_phoff && 1210 elf_ex->e_phoff < elf_ppnt->p_offset + elf_ppnt->p_filesz) { 1211 phdr_addr = elf_ex->e_phoff - elf_ppnt->p_offset + 1212 elf_ppnt->p_vaddr; 1213 } 1214 1215 k = elf_ppnt->p_vaddr; 1216 if ((elf_ppnt->p_flags & PF_X) && k < start_code) 1217 start_code = k; 1218 if (start_data < k) 1219 start_data = k; 1220 1221 /* 1222 * Check to see if the section's size will overflow the 1223 * allowed task size. Note that p_filesz must always be 1224 * <= p_memsz so it is only necessary to check p_memsz. 1225 */ 1226 if (BAD_ADDR(k) || elf_ppnt->p_filesz > elf_ppnt->p_memsz || 1227 elf_ppnt->p_memsz > TASK_SIZE || 1228 TASK_SIZE - elf_ppnt->p_memsz < k) { 1229 /* set_brk can never work. Avoid overflows. */ 1230 retval = -EINVAL; 1231 goto out_free_dentry; 1232 } 1233 1234 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz; 1235 1236 if ((elf_ppnt->p_flags & PF_X) && end_code < k) 1237 end_code = k; 1238 if (end_data < k) 1239 end_data = k; 1240 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz; 1241 if (k > elf_brk) 1242 elf_brk = k; 1243 } 1244 1245 e_entry = elf_ex->e_entry + load_bias; 1246 phdr_addr += load_bias; 1247 elf_brk += load_bias; 1248 start_code += load_bias; 1249 end_code += load_bias; 1250 start_data += load_bias; 1251 end_data += load_bias; 1252 1253 if (interpreter) { 1254 elf_entry = load_elf_interp(interp_elf_ex, 1255 interpreter, 1256 load_bias, interp_elf_phdata, 1257 &arch_state); 1258 if (!IS_ERR_VALUE(elf_entry)) { 1259 /* 1260 * load_elf_interp() returns relocation 1261 * adjustment 1262 */ 1263 interp_load_addr = elf_entry; 1264 elf_entry += interp_elf_ex->e_entry; 1265 } 1266 if (BAD_ADDR(elf_entry)) { 1267 retval = IS_ERR_VALUE(elf_entry) ? 1268 (int)elf_entry : -EINVAL; 1269 goto out_free_dentry; 1270 } 1271 reloc_func_desc = interp_load_addr; 1272 1273 exe_file_allow_write_access(interpreter); 1274 fput(interpreter); 1275 1276 kfree(interp_elf_ex); 1277 kfree(interp_elf_phdata); 1278 } else { 1279 elf_entry = e_entry; 1280 if (BAD_ADDR(elf_entry)) { 1281 retval = -EINVAL; 1282 goto out_free_dentry; 1283 } 1284 } 1285 1286 kfree(elf_phdata); 1287 1288 set_binfmt(&elf_format); 1289 1290 #ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES 1291 retval = ARCH_SETUP_ADDITIONAL_PAGES(bprm, elf_ex, !!interpreter); 1292 if (retval < 0) 1293 goto out; 1294 #endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */ 1295 1296 retval = create_elf_tables(bprm, elf_ex, interp_load_addr, 1297 e_entry, phdr_addr); 1298 if (retval < 0) 1299 goto out; 1300 1301 mm = current->mm; 1302 mm->end_code = end_code; 1303 mm->start_code = start_code; 1304 mm->start_data = start_data; 1305 mm->end_data = end_data; 1306 mm->start_stack = bprm->p; 1307 1308 elf_coredump_set_mm_eflags(mm, elf_ex->e_flags); 1309 1310 /** 1311 * DOC: "brk" handling 1312 * 1313 * For architectures with ELF randomization, when executing a 1314 * loader directly (i.e. static PIE: ET_DYN without PT_INTERP), 1315 * move the brk area out of the mmap region and into the unused 1316 * ELF_ET_DYN_BASE region. Since "brk" grows up it may collide 1317 * early with the stack growing down or other regions being put 1318 * into the mmap region by the kernel (e.g. vdso). 1319 * 1320 * In the CONFIG_COMPAT_BRK case, though, everything is turned 1321 * off because we're not allowed to move the brk at all. 1322 */ 1323 if (!IS_ENABLED(CONFIG_COMPAT_BRK) && 1324 IS_ENABLED(CONFIG_ARCH_HAS_ELF_RANDOMIZE) && 1325 elf_ex->e_type == ET_DYN && !interpreter) { 1326 elf_brk = ELF_ET_DYN_BASE; 1327 /* This counts as moving the brk, so let brk(2) know. */ 1328 brk_moved = true; 1329 } 1330 mm->start_brk = mm->brk = ELF_PAGEALIGN(elf_brk); 1331 1332 if ((current->flags & PF_RANDOMIZE) && snapshot_randomize_va_space > 1) { 1333 /* 1334 * If we didn't move the brk to ELF_ET_DYN_BASE (above), 1335 * leave a gap between .bss and brk. 1336 */ 1337 if (!brk_moved) 1338 mm->brk = mm->start_brk = mm->brk + PAGE_SIZE; 1339 1340 mm->brk = mm->start_brk = arch_randomize_brk(mm); 1341 brk_moved = true; 1342 } 1343 1344 #ifdef compat_brk_randomized 1345 if (brk_moved) 1346 current->brk_randomized = 1; 1347 #endif 1348 1349 if (current->personality & MMAP_PAGE_ZERO) { 1350 /* Why this, you ask??? Well SVr4 maps page 0 as read-only, 1351 and some applications "depend" upon this behavior. 1352 Since we do not have the power to recompile these, we 1353 emulate the SVr4 behavior. Sigh. */ 1354 error = vm_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC, 1355 MAP_FIXED | MAP_PRIVATE, 0); 1356 1357 retval = do_mseal(0, PAGE_SIZE, 0); 1358 if (retval) 1359 pr_warn_ratelimited("pid=%d, couldn't seal address 0, ret=%d.\n", 1360 task_pid_nr(current), retval); 1361 } 1362 1363 regs = current_pt_regs(); 1364 #ifdef ELF_PLAT_INIT 1365 /* 1366 * The ABI may specify that certain registers be set up in special 1367 * ways (on i386 %edx is the address of a DT_FINI function, for 1368 * example. In addition, it may also specify (eg, PowerPC64 ELF) 1369 * that the e_entry field is the address of the function descriptor 1370 * for the startup routine, rather than the address of the startup 1371 * routine itself. This macro performs whatever initialization to 1372 * the regs structure is required as well as any relocations to the 1373 * function descriptor entries when executing dynamically links apps. 1374 */ 1375 ELF_PLAT_INIT(regs, reloc_func_desc); 1376 #endif 1377 1378 finalize_exec(bprm); 1379 START_THREAD(elf_ex, regs, elf_entry, bprm->p); 1380 retval = 0; 1381 out: 1382 return retval; 1383 1384 /* error cleanup */ 1385 out_free_dentry: 1386 kfree(interp_elf_ex); 1387 kfree(interp_elf_phdata); 1388 out_free_file: 1389 exe_file_allow_write_access(interpreter); 1390 if (interpreter) 1391 fput(interpreter); 1392 out_free_ph: 1393 kfree(elf_phdata); 1394 goto out; 1395 } 1396 1397 #ifdef CONFIG_ELF_CORE 1398 /* 1399 * ELF core dumper 1400 * 1401 * Modelled on fs/exec.c:aout_core_dump() 1402 * Jeremy Fitzhardinge <jeremy@sw.oz.au> 1403 */ 1404 1405 /* An ELF note in memory */ 1406 struct memelfnote 1407 { 1408 const char *name; 1409 int type; 1410 unsigned int datasz; 1411 void *data; 1412 }; 1413 1414 static int notesize(struct memelfnote *en) 1415 { 1416 int sz; 1417 1418 sz = sizeof(struct elf_note); 1419 sz += roundup(strlen(en->name) + 1, 4); 1420 sz += roundup(en->datasz, 4); 1421 1422 return sz; 1423 } 1424 1425 static int writenote(struct memelfnote *men, struct coredump_params *cprm) 1426 { 1427 struct elf_note en; 1428 en.n_namesz = strlen(men->name) + 1; 1429 en.n_descsz = men->datasz; 1430 en.n_type = men->type; 1431 1432 return dump_emit(cprm, &en, sizeof(en)) && 1433 dump_emit(cprm, men->name, en.n_namesz) && dump_align(cprm, 4) && 1434 dump_emit(cprm, men->data, men->datasz) && dump_align(cprm, 4); 1435 } 1436 1437 static void fill_elf_header(struct elfhdr *elf, int segs, 1438 u16 machine, u32 flags) 1439 { 1440 memset(elf, 0, sizeof(*elf)); 1441 1442 memcpy(elf->e_ident, ELFMAG, SELFMAG); 1443 elf->e_ident[EI_CLASS] = ELF_CLASS; 1444 elf->e_ident[EI_DATA] = ELF_DATA; 1445 elf->e_ident[EI_VERSION] = EV_CURRENT; 1446 elf->e_ident[EI_OSABI] = ELF_OSABI; 1447 1448 elf->e_type = ET_CORE; 1449 elf->e_machine = machine; 1450 elf->e_version = EV_CURRENT; 1451 elf->e_phoff = sizeof(struct elfhdr); 1452 elf->e_flags = flags; 1453 elf->e_ehsize = sizeof(struct elfhdr); 1454 elf->e_phentsize = sizeof(struct elf_phdr); 1455 elf->e_phnum = segs; 1456 } 1457 1458 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset) 1459 { 1460 phdr->p_type = PT_NOTE; 1461 phdr->p_offset = offset; 1462 phdr->p_vaddr = 0; 1463 phdr->p_paddr = 0; 1464 phdr->p_filesz = sz; 1465 phdr->p_memsz = 0; 1466 phdr->p_flags = 0; 1467 phdr->p_align = 4; 1468 } 1469 1470 static void __fill_note(struct memelfnote *note, const char *name, int type, 1471 unsigned int sz, void *data) 1472 { 1473 note->name = name; 1474 note->type = type; 1475 note->datasz = sz; 1476 note->data = data; 1477 } 1478 1479 #define fill_note(note, type, sz, data) \ 1480 __fill_note(note, NN_ ## type, NT_ ## type, sz, data) 1481 1482 /* 1483 * fill up all the fields in prstatus from the given task struct, except 1484 * registers which need to be filled up separately. 1485 */ 1486 static void fill_prstatus(struct elf_prstatus_common *prstatus, 1487 struct task_struct *p, long signr) 1488 { 1489 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr; 1490 prstatus->pr_sigpend = p->pending.signal.sig[0]; 1491 prstatus->pr_sighold = p->blocked.sig[0]; 1492 rcu_read_lock(); 1493 prstatus->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent)); 1494 rcu_read_unlock(); 1495 prstatus->pr_pid = task_pid_vnr(p); 1496 prstatus->pr_pgrp = task_pgrp_vnr(p); 1497 prstatus->pr_sid = task_session_vnr(p); 1498 if (thread_group_leader(p)) { 1499 struct task_cputime cputime; 1500 1501 /* 1502 * This is the record for the group leader. It shows the 1503 * group-wide total, not its individual thread total. 1504 */ 1505 thread_group_cputime(p, &cputime); 1506 prstatus->pr_utime = ns_to_kernel_old_timeval(cputime.utime); 1507 prstatus->pr_stime = ns_to_kernel_old_timeval(cputime.stime); 1508 } else { 1509 u64 utime, stime; 1510 1511 task_cputime(p, &utime, &stime); 1512 prstatus->pr_utime = ns_to_kernel_old_timeval(utime); 1513 prstatus->pr_stime = ns_to_kernel_old_timeval(stime); 1514 } 1515 1516 prstatus->pr_cutime = ns_to_kernel_old_timeval(p->signal->cutime); 1517 prstatus->pr_cstime = ns_to_kernel_old_timeval(p->signal->cstime); 1518 } 1519 1520 static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p, 1521 struct mm_struct *mm) 1522 { 1523 const struct cred *cred; 1524 unsigned int i, len; 1525 unsigned int state; 1526 1527 /* first copy the parameters from user space */ 1528 memset(psinfo, 0, sizeof(struct elf_prpsinfo)); 1529 1530 len = mm->arg_end - mm->arg_start; 1531 if (len >= ELF_PRARGSZ) 1532 len = ELF_PRARGSZ-1; 1533 if (copy_from_user(&psinfo->pr_psargs, 1534 (const char __user *)mm->arg_start, len)) 1535 return -EFAULT; 1536 for(i = 0; i < len; i++) 1537 if (psinfo->pr_psargs[i] == 0) 1538 psinfo->pr_psargs[i] = ' '; 1539 psinfo->pr_psargs[len] = 0; 1540 1541 rcu_read_lock(); 1542 psinfo->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent)); 1543 rcu_read_unlock(); 1544 psinfo->pr_pid = task_pid_vnr(p); 1545 psinfo->pr_pgrp = task_pgrp_vnr(p); 1546 psinfo->pr_sid = task_session_vnr(p); 1547 1548 state = READ_ONCE(p->__state); 1549 i = state ? ffz(~state) + 1 : 0; 1550 psinfo->pr_state = i; 1551 psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i]; 1552 psinfo->pr_zomb = psinfo->pr_sname == 'Z'; 1553 psinfo->pr_nice = task_nice(p); 1554 psinfo->pr_flag = p->flags; 1555 rcu_read_lock(); 1556 cred = __task_cred(p); 1557 SET_UID(psinfo->pr_uid, from_kuid_munged(cred->user_ns, cred->uid)); 1558 SET_GID(psinfo->pr_gid, from_kgid_munged(cred->user_ns, cred->gid)); 1559 rcu_read_unlock(); 1560 get_task_comm(psinfo->pr_fname, p); 1561 1562 return 0; 1563 } 1564 1565 static void fill_auxv_note(struct memelfnote *note, struct mm_struct *mm) 1566 { 1567 elf_addr_t *auxv = (elf_addr_t *) mm->saved_auxv; 1568 int i = 0; 1569 do 1570 i += 2; 1571 while (auxv[i - 2] != AT_NULL); 1572 fill_note(note, AUXV, i * sizeof(elf_addr_t), auxv); 1573 } 1574 1575 static void fill_siginfo_note(struct memelfnote *note, user_siginfo_t *csigdata, 1576 const kernel_siginfo_t *siginfo) 1577 { 1578 copy_siginfo_to_external(csigdata, siginfo); 1579 fill_note(note, SIGINFO, sizeof(*csigdata), csigdata); 1580 } 1581 1582 /* 1583 * Format of NT_FILE note: 1584 * 1585 * long count -- how many files are mapped 1586 * long page_size -- units for file_ofs 1587 * array of [COUNT] elements of 1588 * long start 1589 * long end 1590 * long file_ofs 1591 * followed by COUNT filenames in ASCII: "FILE1" NUL "FILE2" NUL... 1592 */ 1593 static int fill_files_note(struct memelfnote *note, struct coredump_params *cprm) 1594 { 1595 unsigned count, size, names_ofs, remaining, n; 1596 user_long_t *data; 1597 user_long_t *start_end_ofs; 1598 char *name_base, *name_curpos; 1599 int i; 1600 1601 /* *Estimated* file count and total data size needed */ 1602 count = cprm->vma_count; 1603 if (count > UINT_MAX / 64) 1604 return -EINVAL; 1605 size = count * 64; 1606 1607 names_ofs = (2 + 3 * count) * sizeof(data[0]); 1608 alloc: 1609 /* paranoia check */ 1610 if (size >= core_file_note_size_limit) { 1611 pr_warn_once("coredump Note size too large: %u (does kernel.core_file_note_size_limit sysctl need adjustment?\n", 1612 size); 1613 return -EINVAL; 1614 } 1615 size = round_up(size, PAGE_SIZE); 1616 /* 1617 * "size" can be 0 here legitimately. 1618 * Let it ENOMEM and omit NT_FILE section which will be empty anyway. 1619 */ 1620 data = kvmalloc(size, GFP_KERNEL); 1621 if (ZERO_OR_NULL_PTR(data)) 1622 return -ENOMEM; 1623 1624 start_end_ofs = data + 2; 1625 name_base = name_curpos = ((char *)data) + names_ofs; 1626 remaining = size - names_ofs; 1627 count = 0; 1628 for (i = 0; i < cprm->vma_count; i++) { 1629 struct core_vma_metadata *m = &cprm->vma_meta[i]; 1630 struct file *file; 1631 const char *filename; 1632 1633 file = m->file; 1634 if (!file) 1635 continue; 1636 filename = file_path(file, name_curpos, remaining); 1637 if (IS_ERR(filename)) { 1638 if (PTR_ERR(filename) == -ENAMETOOLONG) { 1639 kvfree(data); 1640 size = size * 5 / 4; 1641 goto alloc; 1642 } 1643 continue; 1644 } 1645 1646 /* file_path() fills at the end, move name down */ 1647 /* n = strlen(filename) + 1: */ 1648 n = (name_curpos + remaining) - filename; 1649 remaining = filename - name_curpos; 1650 memmove(name_curpos, filename, n); 1651 name_curpos += n; 1652 1653 *start_end_ofs++ = m->start; 1654 *start_end_ofs++ = m->end; 1655 *start_end_ofs++ = m->pgoff; 1656 count++; 1657 } 1658 1659 /* Now we know exact count of files, can store it */ 1660 data[0] = count; 1661 data[1] = PAGE_SIZE; 1662 /* 1663 * Count usually is less than mm->map_count, 1664 * we need to move filenames down. 1665 */ 1666 n = cprm->vma_count - count; 1667 if (n != 0) { 1668 unsigned shift_bytes = n * 3 * sizeof(data[0]); 1669 memmove(name_base - shift_bytes, name_base, 1670 name_curpos - name_base); 1671 name_curpos -= shift_bytes; 1672 } 1673 1674 size = name_curpos - (char *)data; 1675 fill_note(note, FILE, size, data); 1676 return 0; 1677 } 1678 1679 #include <linux/regset.h> 1680 1681 struct elf_thread_core_info { 1682 struct elf_thread_core_info *next; 1683 struct task_struct *task; 1684 struct elf_prstatus prstatus; 1685 struct memelfnote notes[]; 1686 }; 1687 1688 struct elf_note_info { 1689 struct elf_thread_core_info *thread; 1690 struct memelfnote psinfo; 1691 struct memelfnote signote; 1692 struct memelfnote auxv; 1693 struct memelfnote files; 1694 user_siginfo_t csigdata; 1695 size_t size; 1696 int thread_notes; 1697 }; 1698 1699 #ifdef CORE_DUMP_USE_REGSET 1700 /* 1701 * When a regset has a writeback hook, we call it on each thread before 1702 * dumping user memory. On register window machines, this makes sure the 1703 * user memory backing the register data is up to date before we read it. 1704 */ 1705 static void do_thread_regset_writeback(struct task_struct *task, 1706 const struct user_regset *regset) 1707 { 1708 if (regset->writeback) 1709 regset->writeback(task, regset, 1); 1710 } 1711 1712 #ifndef PRSTATUS_SIZE 1713 #define PRSTATUS_SIZE sizeof(struct elf_prstatus) 1714 #endif 1715 1716 #ifndef SET_PR_FPVALID 1717 #define SET_PR_FPVALID(S) ((S)->pr_fpvalid = 1) 1718 #endif 1719 1720 static int fill_thread_core_info(struct elf_thread_core_info *t, 1721 const struct user_regset_view *view, 1722 long signr, struct elf_note_info *info) 1723 { 1724 unsigned int note_iter, view_iter; 1725 1726 /* 1727 * NT_PRSTATUS is the one special case, because the regset data 1728 * goes into the pr_reg field inside the note contents, rather 1729 * than being the whole note contents. We fill the regset in here. 1730 * We assume that regset 0 is NT_PRSTATUS. 1731 */ 1732 fill_prstatus(&t->prstatus.common, t->task, signr); 1733 regset_get(t->task, &view->regsets[0], 1734 sizeof(t->prstatus.pr_reg), &t->prstatus.pr_reg); 1735 1736 fill_note(&t->notes[0], PRSTATUS, PRSTATUS_SIZE, &t->prstatus); 1737 info->size += notesize(&t->notes[0]); 1738 1739 do_thread_regset_writeback(t->task, &view->regsets[0]); 1740 1741 /* 1742 * Each other regset might generate a note too. For each regset 1743 * that has no core_note_type or is inactive, skip it. 1744 */ 1745 note_iter = 1; 1746 for (view_iter = 1; view_iter < view->n; ++view_iter) { 1747 const struct user_regset *regset = &view->regsets[view_iter]; 1748 int note_type = regset->core_note_type; 1749 const char *note_name = regset->core_note_name; 1750 bool is_fpreg = note_type == NT_PRFPREG; 1751 void *data; 1752 int ret; 1753 1754 do_thread_regset_writeback(t->task, regset); 1755 if (!note_type) // not for coredumps 1756 continue; 1757 if (regset->active && regset->active(t->task, regset) <= 0) 1758 continue; 1759 1760 ret = regset_get_alloc(t->task, regset, ~0U, &data); 1761 if (ret < 0) 1762 continue; 1763 1764 if (WARN_ON_ONCE(note_iter >= info->thread_notes)) 1765 break; 1766 1767 if (is_fpreg) 1768 SET_PR_FPVALID(&t->prstatus); 1769 1770 /* There should be a note name, but if not, guess: */ 1771 if (WARN_ON_ONCE(!note_name)) 1772 note_name = "LINUX"; 1773 else 1774 /* Warn on non-legacy-compatible names, for now. */ 1775 WARN_ON_ONCE(strcmp(note_name, 1776 is_fpreg ? "CORE" : "LINUX")); 1777 1778 __fill_note(&t->notes[note_iter], note_name, note_type, 1779 ret, data); 1780 1781 info->size += notesize(&t->notes[note_iter]); 1782 note_iter++; 1783 } 1784 1785 return 1; 1786 } 1787 #else 1788 static int fill_thread_core_info(struct elf_thread_core_info *t, 1789 const struct user_regset_view *view, 1790 long signr, struct elf_note_info *info) 1791 { 1792 struct task_struct *p = t->task; 1793 elf_fpregset_t *fpu; 1794 1795 fill_prstatus(&t->prstatus.common, p, signr); 1796 elf_core_copy_task_regs(p, &t->prstatus.pr_reg); 1797 1798 fill_note(&t->notes[0], PRSTATUS, sizeof(t->prstatus), &t->prstatus); 1799 info->size += notesize(&t->notes[0]); 1800 1801 fpu = kzalloc_obj(elf_fpregset_t); 1802 if (!fpu || !elf_core_copy_task_fpregs(p, fpu)) { 1803 kfree(fpu); 1804 return 1; 1805 } 1806 1807 t->prstatus.pr_fpvalid = 1; 1808 fill_note(&t->notes[1], PRFPREG, sizeof(*fpu), fpu); 1809 info->size += notesize(&t->notes[1]); 1810 1811 return 1; 1812 } 1813 #endif 1814 1815 static int fill_note_info(struct elfhdr *elf, int phdrs, 1816 struct elf_note_info *info, 1817 struct coredump_params *cprm) 1818 { 1819 struct task_struct *dump_task = current; 1820 const struct user_regset_view *view; 1821 struct elf_thread_core_info *t; 1822 struct elf_prpsinfo *psinfo; 1823 struct core_thread *ct; 1824 u16 machine; 1825 u32 flags; 1826 1827 psinfo = kmalloc_obj(*psinfo); 1828 if (!psinfo) 1829 return 0; 1830 fill_note(&info->psinfo, PRPSINFO, sizeof(*psinfo), psinfo); 1831 1832 #ifdef CORE_DUMP_USE_REGSET 1833 view = task_user_regset_view(dump_task); 1834 1835 /* 1836 * Figure out how many notes we're going to need for each thread. 1837 */ 1838 info->thread_notes = 0; 1839 for (int i = 0; i < view->n; ++i) 1840 if (view->regsets[i].core_note_type != 0) 1841 ++info->thread_notes; 1842 1843 /* 1844 * Sanity check. We rely on regset 0 being in NT_PRSTATUS, 1845 * since it is our one special case. 1846 */ 1847 if (unlikely(info->thread_notes == 0) || 1848 unlikely(view->regsets[0].core_note_type != NT_PRSTATUS)) { 1849 WARN_ON(1); 1850 return 0; 1851 } 1852 1853 machine = view->e_machine; 1854 flags = view->e_flags; 1855 #else 1856 view = NULL; 1857 info->thread_notes = 2; 1858 machine = ELF_ARCH; 1859 flags = ELF_CORE_EFLAGS; 1860 #endif 1861 1862 /* 1863 * Override ELF e_flags with value taken from process, 1864 * if arch needs that. 1865 */ 1866 flags = elf_coredump_get_mm_eflags(dump_task->mm, flags); 1867 1868 /* 1869 * Initialize the ELF file header. 1870 */ 1871 fill_elf_header(elf, phdrs, machine, flags); 1872 1873 /* 1874 * Allocate a structure for each thread. 1875 */ 1876 info->thread = kzalloc_flex(*info->thread, notes, info->thread_notes); 1877 if (unlikely(!info->thread)) 1878 return 0; 1879 1880 info->thread->task = dump_task; 1881 for (ct = dump_task->signal->core_state->dumper.next; ct; ct = ct->next) { 1882 t = kzalloc_flex(*t, notes, info->thread_notes); 1883 if (unlikely(!t)) 1884 return 0; 1885 1886 t->task = ct->task; 1887 t->next = info->thread->next; 1888 info->thread->next = t; 1889 } 1890 1891 /* 1892 * Now fill in each thread's information. 1893 */ 1894 for (t = info->thread; t != NULL; t = t->next) 1895 if (!fill_thread_core_info(t, view, cprm->siginfo->si_signo, info)) 1896 return 0; 1897 1898 /* 1899 * Fill in the two process-wide notes. 1900 */ 1901 fill_psinfo(psinfo, dump_task->group_leader, dump_task->mm); 1902 info->size += notesize(&info->psinfo); 1903 1904 fill_siginfo_note(&info->signote, &info->csigdata, cprm->siginfo); 1905 info->size += notesize(&info->signote); 1906 1907 fill_auxv_note(&info->auxv, current->mm); 1908 info->size += notesize(&info->auxv); 1909 1910 if (fill_files_note(&info->files, cprm) == 0) 1911 info->size += notesize(&info->files); 1912 1913 return 1; 1914 } 1915 1916 /* 1917 * Write all the notes for each thread. When writing the first thread, the 1918 * process-wide notes are interleaved after the first thread-specific note. 1919 */ 1920 static int write_note_info(struct elf_note_info *info, 1921 struct coredump_params *cprm) 1922 { 1923 bool first = true; 1924 struct elf_thread_core_info *t = info->thread; 1925 1926 do { 1927 int i; 1928 1929 if (!writenote(&t->notes[0], cprm)) 1930 return 0; 1931 1932 if (first && !writenote(&info->psinfo, cprm)) 1933 return 0; 1934 if (first && !writenote(&info->signote, cprm)) 1935 return 0; 1936 if (first && !writenote(&info->auxv, cprm)) 1937 return 0; 1938 if (first && info->files.data && 1939 !writenote(&info->files, cprm)) 1940 return 0; 1941 1942 for (i = 1; i < info->thread_notes; ++i) 1943 if (t->notes[i].data && 1944 !writenote(&t->notes[i], cprm)) 1945 return 0; 1946 1947 first = false; 1948 t = t->next; 1949 } while (t); 1950 1951 return 1; 1952 } 1953 1954 static void free_note_info(struct elf_note_info *info) 1955 { 1956 struct elf_thread_core_info *threads = info->thread; 1957 while (threads) { 1958 unsigned int i; 1959 struct elf_thread_core_info *t = threads; 1960 threads = t->next; 1961 WARN_ON(t->notes[0].data && t->notes[0].data != &t->prstatus); 1962 for (i = 1; i < info->thread_notes; ++i) 1963 kvfree(t->notes[i].data); 1964 kfree(t); 1965 } 1966 kfree(info->psinfo.data); 1967 kvfree(info->files.data); 1968 } 1969 1970 static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum, 1971 elf_addr_t e_shoff, int segs) 1972 { 1973 elf->e_shoff = e_shoff; 1974 elf->e_shentsize = sizeof(*shdr4extnum); 1975 elf->e_shnum = 1; 1976 elf->e_shstrndx = SHN_UNDEF; 1977 1978 memset(shdr4extnum, 0, sizeof(*shdr4extnum)); 1979 1980 shdr4extnum->sh_type = SHT_NULL; 1981 shdr4extnum->sh_size = elf->e_shnum; 1982 shdr4extnum->sh_link = elf->e_shstrndx; 1983 shdr4extnum->sh_info = segs; 1984 } 1985 1986 /* 1987 * Actual dumper 1988 * 1989 * This is a two-pass process; first we find the offsets of the bits, 1990 * and then they are actually written out. If we run out of core limit 1991 * we just truncate. 1992 */ 1993 static int elf_core_dump(struct coredump_params *cprm) 1994 { 1995 int has_dumped = 0; 1996 int segs, i; 1997 struct elfhdr elf; 1998 loff_t offset = 0, dataoff; 1999 struct elf_note_info info = { }; 2000 struct elf_phdr *phdr4note = NULL; 2001 struct elf_shdr *shdr4extnum = NULL; 2002 Elf_Half e_phnum; 2003 elf_addr_t e_shoff; 2004 2005 /* 2006 * The number of segs are recored into ELF header as 16bit value. 2007 * Please check DEFAULT_MAX_MAP_COUNT definition when you modify here. 2008 */ 2009 segs = cprm->vma_count + elf_core_extra_phdrs(cprm); 2010 2011 /* for notes section */ 2012 segs++; 2013 2014 /* If segs > PN_XNUM(0xffff), then e_phnum overflows. To avoid 2015 * this, kernel supports extended numbering. Have a look at 2016 * include/linux/elf.h for further information. */ 2017 e_phnum = segs > PN_XNUM ? PN_XNUM : segs; 2018 2019 /* 2020 * Collect all the non-memory information about the process for the 2021 * notes. This also sets up the file header. 2022 */ 2023 if (!fill_note_info(&elf, e_phnum, &info, cprm)) 2024 goto end_coredump; 2025 2026 has_dumped = 1; 2027 2028 offset += sizeof(elf); /* ELF header */ 2029 offset += segs * sizeof(struct elf_phdr); /* Program headers */ 2030 2031 /* Write notes phdr entry */ 2032 { 2033 size_t sz = info.size; 2034 2035 /* For cell spufs and x86 xstate */ 2036 sz += elf_coredump_extra_notes_size(); 2037 2038 phdr4note = kmalloc_obj(*phdr4note); 2039 if (!phdr4note) 2040 goto end_coredump; 2041 2042 fill_elf_note_phdr(phdr4note, sz, offset); 2043 offset += sz; 2044 } 2045 2046 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE); 2047 2048 offset += cprm->vma_data_size; 2049 offset += elf_core_extra_data_size(cprm); 2050 e_shoff = offset; 2051 2052 if (e_phnum == PN_XNUM) { 2053 shdr4extnum = kmalloc_obj(*shdr4extnum); 2054 if (!shdr4extnum) 2055 goto end_coredump; 2056 fill_extnum_info(&elf, shdr4extnum, e_shoff, segs); 2057 } 2058 2059 offset = dataoff; 2060 2061 if (!dump_emit(cprm, &elf, sizeof(elf))) 2062 goto end_coredump; 2063 2064 if (!dump_emit(cprm, phdr4note, sizeof(*phdr4note))) 2065 goto end_coredump; 2066 2067 /* Write program headers for segments dump */ 2068 for (i = 0; i < cprm->vma_count; i++) { 2069 struct core_vma_metadata *meta = cprm->vma_meta + i; 2070 struct elf_phdr phdr; 2071 2072 phdr.p_type = PT_LOAD; 2073 phdr.p_offset = offset; 2074 phdr.p_vaddr = meta->start; 2075 phdr.p_paddr = 0; 2076 phdr.p_filesz = meta->dump_size; 2077 phdr.p_memsz = meta->end - meta->start; 2078 offset += phdr.p_filesz; 2079 phdr.p_flags = 0; 2080 if (meta->flags & VM_READ) 2081 phdr.p_flags |= PF_R; 2082 if (meta->flags & VM_WRITE) 2083 phdr.p_flags |= PF_W; 2084 if (meta->flags & VM_EXEC) 2085 phdr.p_flags |= PF_X; 2086 phdr.p_align = ELF_EXEC_PAGESIZE; 2087 2088 if (!dump_emit(cprm, &phdr, sizeof(phdr))) 2089 goto end_coredump; 2090 } 2091 2092 if (!elf_core_write_extra_phdrs(cprm, offset)) 2093 goto end_coredump; 2094 2095 /* write out the notes section */ 2096 if (!write_note_info(&info, cprm)) 2097 goto end_coredump; 2098 2099 /* For cell spufs and x86 xstate */ 2100 if (elf_coredump_extra_notes_write(cprm)) 2101 goto end_coredump; 2102 2103 /* Align to page */ 2104 dump_skip_to(cprm, dataoff); 2105 2106 for (i = 0; i < cprm->vma_count; i++) { 2107 struct core_vma_metadata *meta = cprm->vma_meta + i; 2108 2109 if (!dump_user_range(cprm, meta->start, meta->dump_size)) 2110 goto end_coredump; 2111 } 2112 2113 if (!elf_core_write_extra_data(cprm)) 2114 goto end_coredump; 2115 2116 if (e_phnum == PN_XNUM) { 2117 if (!dump_emit(cprm, shdr4extnum, sizeof(*shdr4extnum))) 2118 goto end_coredump; 2119 } 2120 2121 end_coredump: 2122 free_note_info(&info); 2123 kfree(shdr4extnum); 2124 kfree(phdr4note); 2125 return has_dumped; 2126 } 2127 2128 #endif /* CONFIG_ELF_CORE */ 2129 2130 static int __init init_elf_binfmt(void) 2131 { 2132 register_binfmt(&elf_format); 2133 return 0; 2134 } 2135 2136 static void __exit exit_elf_binfmt(void) 2137 { 2138 /* Remove the COFF and ELF loaders. */ 2139 unregister_binfmt(&elf_format); 2140 } 2141 2142 core_initcall(init_elf_binfmt); 2143 module_exit(exit_elf_binfmt); 2144 2145 #ifdef CONFIG_BINFMT_ELF_KUNIT_TEST 2146 #include "tests/binfmt_elf_kunit.c" 2147 #endif 2148