// SPDX-License-Identifier: GPL-2.0-only /* * linux/fs/binfmt_elf.c * * These are the functions used to load ELF format executables as used * on SVr4 machines. Information on the format may be found in the book * "UNIX SYSTEM V RELEASE 4 Programmers Guide: Ansi C and Programming Support * Tools". * * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
*/
/* * If we don't support core dumping, then supply a NULL so we * don't even try.
*/ #ifdef CONFIG_ELF_CORE staticint elf_core_dump(struct coredump_params *cprm); #else #define elf_core_dump NULL #endif
/* * We need to explicitly zero any trailing portion of the page that follows * p_filesz when it ends before the page ends (e.g. bss), otherwise this * memory will contain the junk from the file that should not be present.
*/ staticint padzero(unsignedlong address)
{ unsignedlong nbyte;
#ifndef ELF_BASE_PLATFORM /* * AT_BASE_PLATFORM indicates the "real" hardware/microarchitecture. * If the arch defines ELF_BASE_PLATFORM (in asm/elf.h), the value * will be copied to the user stack in the same manner as AT_PLATFORM.
*/ #define ELF_BASE_PLATFORM NULL #endif
/* * In some cases (e.g. Hyper-Threading), we want to avoid L1 * evictions by the processes running on the same package. One * thing we can do is to shuffle the initial stack for them.
*/
p = arch_align_stack(p);
/* * If this architecture has a platform capability string, copy it * to userspace. In some cases (Sparc), this info is impossible * for userspace to get any other way, in others (i386) it is * merely difficult.
*/
u_platform = NULL; if (k_platform) {
size_t len = strlen(k_platform) + 1;
/* * If this architecture has a "base" platform capability * string, copy it to userspace.
*/
u_base_platform = NULL; if (k_base_platform) {
size_t len = strlen(k_base_platform) + 1;
/* * Generate 16 random bytes for userspace PRNG seeding.
*/
get_random_bytes(k_rand_bytes, sizeof(k_rand_bytes));
u_rand_bytes = (elf_addr_t __user *)
STACK_ALLOC(p, sizeof(k_rand_bytes)); if (copy_to_user(u_rand_bytes, k_rand_bytes, sizeof(k_rand_bytes))) return -EFAULT;
/* Create the ELF interpreter info */
elf_info = (elf_addr_t *)mm->saved_auxv; /* update AT_VECTOR_SIZE_BASE if the number of NEW_AUX_ENT() changes */ #define NEW_AUX_ENT(id, val) \ do { \
*elf_info++ = id; \
*elf_info++ = val; \
} while (0)
#ifdef ARCH_DLINFO /* * ARCH_DLINFO must come first so PPC can do its special alignment of * AUXV. * update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT() in * ARCH_DLINFO changes
*/
ARCH_DLINFO; #endif
NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
NEW_AUX_ENT(AT_PHDR, phdr_addr);
NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
NEW_AUX_ENT(AT_BASE, interp_load_addr); if (bprm->interp_flags & BINPRM_FLAGS_PRESERVE_ARGV0)
flags |= AT_FLAGS_PRESERVE_ARGV0;
NEW_AUX_ENT(AT_FLAGS, flags);
NEW_AUX_ENT(AT_ENTRY, e_entry);
NEW_AUX_ENT(AT_UID, from_kuid_munged(cred->user_ns, cred->uid));
NEW_AUX_ENT(AT_EUID, from_kuid_munged(cred->user_ns, cred->euid));
NEW_AUX_ENT(AT_GID, from_kgid_munged(cred->user_ns, cred->gid));
NEW_AUX_ENT(AT_EGID, from_kgid_munged(cred->user_ns, cred->egid));
NEW_AUX_ENT(AT_SECURE, bprm->secureexec);
NEW_AUX_ENT(AT_RANDOM, (elf_addr_t)(unsignedlong)u_rand_bytes); #ifdef ELF_HWCAP2
NEW_AUX_ENT(AT_HWCAP2, ELF_HWCAP2); #endif #ifdef ELF_HWCAP3
NEW_AUX_ENT(AT_HWCAP3, ELF_HWCAP3); #endif #ifdef ELF_HWCAP4
NEW_AUX_ENT(AT_HWCAP4, ELF_HWCAP4); #endif
NEW_AUX_ENT(AT_EXECFN, bprm->exec); if (k_platform) {
NEW_AUX_ENT(AT_PLATFORM,
(elf_addr_t)(unsignedlong)u_platform);
} if (k_base_platform) {
NEW_AUX_ENT(AT_BASE_PLATFORM,
(elf_addr_t)(unsignedlong)u_base_platform);
} if (bprm->have_execfd) {
NEW_AUX_ENT(AT_EXECFD, bprm->execfd);
} #ifdef CONFIG_RSEQ
NEW_AUX_ENT(AT_RSEQ_FEATURE_SIZE, offsetof(struct rseq, end));
NEW_AUX_ENT(AT_RSEQ_ALIGN, __alignof__(struct rseq)); #endif #undef NEW_AUX_ENT /* AT_NULL is zero; clear the rest too */
memset(elf_info, 0, (char *)mm->saved_auxv + sizeof(mm->saved_auxv) - (char *)elf_info);
/* And advance past the AT_NULL entry. */
elf_info += 2;
/* Point sp at the lowest address on the stack */ #ifdef CONFIG_STACK_GROWSUP
sp = (elf_addr_t __user *)bprm->p - items - ei_index;
bprm->exec = (unsignedlong)sp; /* XXX: PARISC HACK */ #else
sp = (elf_addr_t __user *)bprm->p; #endif
/* * Grow the stack manually; some architectures have a limit on how * far ahead a user-space access may be in order to grow the stack.
*/ if (mmap_write_lock_killable(mm)) return -EINTR;
vma = find_extend_vma_locked(mm, bprm->p);
mmap_write_unlock(mm); if (!vma) return -EFAULT;
/* Now, let's put argc (and argv, envp if appropriate) on the stack */ if (put_user(argc, sp++)) return -EFAULT;
/* Populate list of argv pointers back to argv strings. */
p = mm->arg_end = mm->arg_start; while (argc-- > 0) {
size_t len; if (put_user((elf_addr_t)p, sp++)) return -EFAULT;
len = strnlen_user((void __user *)p, MAX_ARG_STRLEN); if (!len || len > MAX_ARG_STRLEN) return -EINVAL;
p += len;
} if (put_user(0, sp++)) return -EFAULT;
mm->arg_end = p;
/* Populate list of envp pointers back to envp strings. */
mm->env_end = mm->env_start = p; while (envc-- > 0) {
size_t len; if (put_user((elf_addr_t)p, sp++)) return -EFAULT;
len = strnlen_user((void __user *)p, MAX_ARG_STRLEN); if (!len || len > MAX_ARG_STRLEN) return -EINVAL;
p += len;
} if (put_user(0, sp++)) return -EFAULT;
mm->env_end = p;
/* Put the elf_info on the stack in the right place. */ if (copy_to_user(sp, mm->saved_auxv, ei_index * sizeof(elf_addr_t))) return -EFAULT; return 0;
}
/* * Map "eppnt->p_filesz" bytes from "filep" offset "eppnt->p_offset" * into memory at "addr". (Note that p_filesz is rounded up to the * next page, so any extra bytes from the file must be wiped.)
*/ staticunsignedlong elf_map(struct file *filep, unsignedlong addr, conststruct elf_phdr *eppnt, int prot, int type, unsignedlong total_size)
{ unsignedlong map_addr; unsignedlong size = eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr); unsignedlong off = eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr);
addr = ELF_PAGESTART(addr);
size = ELF_PAGEALIGN(size);
/* mmap() will return -EINVAL if given a zero size, but a
* segment with zero filesize is perfectly valid */ if (!size) return addr;
/* * total_size is the size of the ELF (interpreter) image. * The _first_ mmap needs to know the full size, otherwise * randomization might put this image into an overlapping * position with the ELF binary image. (since size < total_size) * So we first map the 'big' image - and unmap the remainder at * the end. (which unmap is needed for ELF images with holes.)
*/ if (total_size) {
total_size = ELF_PAGEALIGN(total_size);
map_addr = vm_mmap(filep, addr, total_size, prot, type, off); if (!BAD_ADDR(map_addr))
vm_munmap(map_addr+size, total_size-size);
} else
map_addr = vm_mmap(filep, addr, size, prot, type, off);
if ((type & MAP_FIXED_NOREPLACE) &&
PTR_ERR((void *)map_addr) == -EEXIST)
pr_info("%d (%s): Uhuuh, elf segment at %px requested but the memory is mapped already\n",
task_pid_nr(current), current->comm, (void *)addr);
return(map_addr);
}
/* * Map "eppnt->p_filesz" bytes from "filep" offset "eppnt->p_offset" * into memory at "addr". Memory from "p_filesz" through "p_memsz" * rounded up to the next page is zeroed.
*/ staticunsignedlong elf_load(struct file *filep, unsignedlong addr, conststruct elf_phdr *eppnt, int prot, int type, unsignedlong total_size)
{ unsignedlong zero_start, zero_end; unsignedlong map_addr;
/* * Zero the end of the last mapped page but ignore * any errors if the segment isn't writable.
*/ if (padzero(zero_start) && (prot & PROT_WRITE)) return -EFAULT;
}
} else {
map_addr = zero_start = ELF_PAGESTART(addr);
zero_end = zero_start + ELF_PAGEOFFSET(eppnt->p_vaddr) +
eppnt->p_memsz;
} if (eppnt->p_memsz > eppnt->p_filesz) { /* * Map the last of the segment. * If the header is requesting these pages to be * executable, honour that (ppc32 needs this).
*/ int error;
staticunsignedlong maximum_alignment(struct elf_phdr *cmds, int nr)
{ unsignedlong alignment = 0; int i;
for (i = 0; i < nr; i++) { if (cmds[i].p_type == PT_LOAD) { unsignedlong p_align = cmds[i].p_align;
/* skip non-power of two alignments as invalid */ if (!is_power_of_2(p_align)) continue;
alignment = max(alignment, p_align);
}
}
/* ensure we align to at least one page */ return ELF_PAGEALIGN(alignment);
}
/** * load_elf_phdrs() - load ELF program headers * @elf_ex: ELF header of the binary whose program headers should be loaded * @elf_file: the opened ELF binary file * * Loads ELF program headers from the binary file elf_file, which has the ELF * header pointed to by elf_ex, into a newly allocated array. The caller is * responsible for freeing the allocated data. Returns NULL upon failure.
*/ staticstruct elf_phdr *load_elf_phdrs(conststruct elfhdr *elf_ex, struct file *elf_file)
{ struct elf_phdr *elf_phdata = NULL; int retval = -1; unsignedint size;
/* * If the size of this structure has changed, then punt, since * we will be doing the wrong thing.
*/ if (elf_ex->e_phentsize != sizeof(struct elf_phdr)) goto out;
/* Sanity check the number of program headers... */ /* ...and their total size. */
size = sizeof(struct elf_phdr) * elf_ex->e_phnum; if (size == 0 || size > 65536) goto out;
elf_phdata = kmalloc(size, GFP_KERNEL); if (!elf_phdata) goto out;
/* Read in the program headers */
retval = elf_read(elf_file, elf_phdata, size, elf_ex->e_phoff);
/** * struct arch_elf_state - arch-specific ELF loading state * * This structure is used to preserve architecture specific data during * the loading of an ELF file, throughout the checking of architecture * specific ELF headers & through to the point where the ELF load is * known to be proceeding (ie. SET_PERSONALITY). * * This implementation is a dummy for architectures which require no * specific state.
*/ struct arch_elf_state {
};
#define INIT_ARCH_ELF_STATE {}
/** * arch_elf_pt_proc() - check a PT_LOPROC..PT_HIPROC ELF program header * @ehdr: The main ELF header * @phdr: The program header to check * @elf: The open ELF file * @is_interp: True if the phdr is from the interpreter of the ELF being * loaded, else false. * @state: Architecture-specific state preserved throughout the process * of loading the ELF. * * Inspects the program header phdr to validate its correctness and/or * suitability for the system. Called once per ELF program header in the * range PT_LOPROC to PT_HIPROC, for both the ELF being loaded and its * interpreter. * * Return: Zero to proceed with the ELF load, non-zero to fail the ELF load * with that return code.
*/ staticinlineint arch_elf_pt_proc(struct elfhdr *ehdr, struct elf_phdr *phdr, struct file *elf, bool is_interp, struct arch_elf_state *state)
{ /* Dummy implementation, always proceed */ return 0;
}
/** * arch_check_elf() - check an ELF executable * @ehdr: The main ELF header * @has_interp: True if the ELF has an interpreter, else false. * @interp_ehdr: The interpreter's ELF header * @state: Architecture-specific state preserved throughout the process * of loading the ELF. * * Provides a final opportunity for architecture code to reject the loading * of the ELF & cause an exec syscall to return an error. This is called after * all program headers to be checked by arch_elf_pt_proc have been. * * Return: Zero to proceed with the ELF load, non-zero to fail the ELF load * with that return code.
*/ staticinlineint arch_check_elf(struct elfhdr *ehdr, bool has_interp, struct elfhdr *interp_ehdr, struct arch_elf_state *state)
{ /* Dummy implementation, always proceed */ return 0;
}
/* This is much more generalized than the library routine read function, so we keep this separate. Technically the library read function is only provided so that we can read a.out libraries that have
an ELF header */
/* First of all, some simple consistency checks */ if (interp_elf_ex->e_type != ET_EXEC &&
interp_elf_ex->e_type != ET_DYN) goto out; if (!elf_check_arch(interp_elf_ex) ||
elf_check_fdpic(interp_elf_ex)) goto out; if (!can_mmap_file(interpreter)) goto out;
/* * Check to see if the section's size will overflow the * allowed task size. Note that p_filesz must always be * <= p_memsize so it's only necessary to check p_memsz.
*/
k = load_addr + eppnt->p_vaddr; if (BAD_ADDR(k) ||
eppnt->p_filesz > eppnt->p_memsz ||
eppnt->p_memsz > TASK_SIZE ||
TASK_SIZE - eppnt->p_memsz < k) {
error = -ENOMEM; goto out;
}
}
}
error = load_addr;
out: return error;
}
/* * These are the functions used to load ELF style executables and shared * libraries. There is no binary dependent code anywhere else.
*/
/* Properties are supposed to be unique and sorted on pr_type: */ if (have_prev_type && pr->pr_type <= *prev_type) return -ENOEXEC;
*prev_type = pr->pr_type;
ret = arch_parse_elf_property(pr->pr_type, data + o,
pr->pr_datasz, ELF_COMPAT, arch); if (ret) return ret;
retval = -ENOEXEC; /* First of all, some simple consistency checks */ if (memcmp(elf_ex->e_ident, ELFMAG, SELFMAG) != 0) goto out;
if (elf_ex->e_type != ET_EXEC && elf_ex->e_type != ET_DYN) goto out; if (!elf_check_arch(elf_ex)) goto out; if (elf_check_fdpic(elf_ex)) goto out; if (!can_mmap_file(bprm->file)) goto out;
elf_phdata = load_elf_phdrs(elf_ex, bprm->file); if (!elf_phdata) goto out;
elf_ppnt = elf_phdata; for (i = 0; i < elf_ex->e_phnum; i++, elf_ppnt++) { char *elf_interpreter;
if (elf_ppnt->p_type == PT_GNU_PROPERTY) {
elf_property_phdata = elf_ppnt; continue;
}
if (elf_ppnt->p_type != PT_INTERP) continue;
/* * This is the program interpreter used for shared libraries - * for now assume that this is an a.out format binary.
*/
retval = -ENOEXEC; if (elf_ppnt->p_filesz > PATH_MAX || elf_ppnt->p_filesz < 2) goto out_free_ph;
elf_ppnt = elf_phdata; for (i = 0; i < elf_ex->e_phnum; i++, elf_ppnt++) switch (elf_ppnt->p_type) { case PT_GNU_STACK: if (elf_ppnt->p_flags & PF_X)
executable_stack = EXSTACK_ENABLE_X; else
executable_stack = EXSTACK_DISABLE_X; break;
case PT_LOPROC ... PT_HIPROC:
retval = arch_elf_pt_proc(elf_ex, elf_ppnt,
bprm->file, false,
&arch_state); if (retval) goto out_free_dentry; break;
}
/* Some simple consistency checks for the interpreter */ if (interpreter) {
retval = -ELIBBAD; /* Not an ELF interpreter */ if (memcmp(interp_elf_ex->e_ident, ELFMAG, SELFMAG) != 0) goto out_free_dentry; /* Verify the interpreter has a valid arch */ if (!elf_check_arch(interp_elf_ex) ||
elf_check_fdpic(interp_elf_ex)) goto out_free_dentry;
/* Load the interpreter program headers */
interp_elf_phdata = load_elf_phdrs(interp_elf_ex,
interpreter); if (!interp_elf_phdata) goto out_free_dentry;
/* Pass PT_LOPROC..PT_HIPROC headers to arch code */
elf_property_phdata = NULL;
elf_ppnt = interp_elf_phdata; for (i = 0; i < interp_elf_ex->e_phnum; i++, elf_ppnt++) switch (elf_ppnt->p_type) { case PT_GNU_PROPERTY:
elf_property_phdata = elf_ppnt; break;
case PT_LOPROC ... PT_HIPROC:
retval = arch_elf_pt_proc(interp_elf_ex,
elf_ppnt, interpreter, true, &arch_state); if (retval) goto out_free_dentry; break;
}
}
/* * Allow arch code to reject the ELF at this point, whilst it's * still possible to return an error to the code that invoked * the exec syscall.
*/
retval = arch_check_elf(elf_ex,
!!interpreter, interp_elf_ex,
&arch_state); if (retval) goto out_free_dentry;
/* Flush all traces of the currently running executable */
retval = begin_new_exec(bprm); if (retval) goto out_free_dentry;
/* Do this immediately, since STACK_TOP as used in setup_arg_pages
may depend on the personality. */
SET_PERSONALITY2(*elf_ex, &arch_state); if (elf_read_implies_exec(*elf_ex, executable_stack))
current->personality |= READ_IMPLIES_EXEC;
/* Do this so that we can load the interpreter, if need be. We will
change some of these later */
retval = setup_arg_pages(bprm, randomize_stack_top(STACK_TOP),
executable_stack); if (retval < 0) goto out_free_dentry;
/* Now we do a little grungy work by mmapping the ELF image into
the correct location in memory. */ for(i = 0, elf_ppnt = elf_phdata;
i < elf_ex->e_phnum; i++, elf_ppnt++) { int elf_prot, elf_flags; unsignedlong k, vaddr; unsignedlong total_size = 0; unsignedlong alignment;
vaddr = elf_ppnt->p_vaddr; /* * The first time through the loop, first_pt_load is true: * layout will be calculated. Once set, use MAP_FIXED since * we know we've already safely mapped the entire region with * MAP_FIXED_NOREPLACE in the once-per-binary logic following.
*/ if (!first_pt_load) {
elf_flags |= MAP_FIXED;
} elseif (elf_ex->e_type == ET_EXEC) { /* * This logic is run once for the first LOAD Program * Header for ET_EXEC binaries. No special handling * is needed.
*/
elf_flags |= MAP_FIXED_NOREPLACE;
} elseif (elf_ex->e_type == ET_DYN) { /* * This logic is run once for the first LOAD Program * Header for ET_DYN binaries to calculate the * randomization (load_bias) for all the LOAD * Program Headers.
*/
/* * Calculate the entire size of the ELF mapping * (total_size), used for the initial mapping, * due to load_addr_set which is set to true later * once the initial mapping is performed. * * Note that this is only sensible when the LOAD * segments are contiguous (or overlapping). If * used for LOADs that are far apart, this would * cause the holes between LOADs to be mapped, * running the risk of having the mapping fail, * as it would be larger than the ELF file itself. * * As a result, only ET_DYN does this, since * some ET_EXEC (e.g. ia64) may have large virtual * memory holes between LOADs. *
*/
total_size = total_mapping_size(elf_phdata,
elf_ex->e_phnum); if (!total_size) {
retval = -EINVAL; goto out_free_dentry;
}
/* Calculate any requested alignment. */
alignment = maximum_alignment(elf_phdata, elf_ex->e_phnum);
/** * DOC: PIE handling * * There are effectively two types of ET_DYN ELF * binaries: programs (i.e. PIE: ET_DYN with * PT_INTERP) and loaders (i.e. static PIE: ET_DYN * without PT_INTERP, usually the ELF interpreter * itself). Loaders must be loaded away from programs * since the program may otherwise collide with the * loader (especially for ET_EXEC which does not have * a randomized position). * * For example, to handle invocations of * "./ld.so someprog" to test out a new version of * the loader, the subsequent program that the * loader loads must avoid the loader itself, so * they cannot share the same load range. Sufficient * room for the brk must be allocated with the * loader as well, since brk must be available with * the loader. * * Therefore, programs are loaded offset from * ELF_ET_DYN_BASE and loaders are loaded into the * independently randomized mmap region (0 load_bias * without MAP_FIXED nor MAP_FIXED_NOREPLACE). * * See below for "brk" handling details, which is * also affected by program vs loader and ASLR.
*/ if (interpreter) { /* On ET_DYN with PT_INTERP, we do the ASLR. */
load_bias = ELF_ET_DYN_BASE; if (current->flags & PF_RANDOMIZE)
load_bias += arch_mmap_rnd(); /* Adjust alignment as requested. */ if (alignment)
load_bias &= ~(alignment - 1);
elf_flags |= MAP_FIXED_NOREPLACE;
} else { /* * For ET_DYN without PT_INTERP, we rely on * the architectures's (potentially ASLR) mmap * base address (via a load_bias of 0). * * When a large alignment is requested, we * must do the allocation at address "0" right * now to discover where things will load so * that we can adjust the resulting alignment. * In this case (load_bias != 0), we can use * MAP_FIXED_NOREPLACE to make sure the mapping * doesn't collide with anything.
*/ if (alignment > ELF_MIN_ALIGN) {
load_bias = elf_load(bprm->file, 0, elf_ppnt,
elf_prot, elf_flags, total_size); if (BAD_ADDR(load_bias)) {
retval = IS_ERR_VALUE(load_bias) ?
PTR_ERR((void*)load_bias) : -EINVAL; goto out_free_dentry;
}
vm_munmap(load_bias, total_size); /* Adjust alignment as requested. */ if (alignment)
load_bias &= ~(alignment - 1);
elf_flags |= MAP_FIXED_NOREPLACE;
} else
load_bias = 0;
}
/* * Since load_bias is used for all subsequent loading * calculations, we must lower it by the first vaddr * so that the remaining calculations based on the * ELF vaddrs will be correctly offset. The result * is then page aligned.
*/
load_bias = ELF_PAGESTART(load_bias - vaddr);
}
/* * Figure out which segment in the file contains the Program * Header table, and map to the associated memory address.
*/ if (elf_ppnt->p_offset <= elf_ex->e_phoff &&
elf_ex->e_phoff < elf_ppnt->p_offset + elf_ppnt->p_filesz) {
phdr_addr = elf_ex->e_phoff - elf_ppnt->p_offset +
elf_ppnt->p_vaddr;
}
k = elf_ppnt->p_vaddr; if ((elf_ppnt->p_flags & PF_X) && k < start_code)
start_code = k; if (start_data < k)
start_data = k;
/* * Check to see if the section's size will overflow the * allowed task size. Note that p_filesz must always be * <= p_memsz so it is only necessary to check p_memsz.
*/ if (BAD_ADDR(k) || elf_ppnt->p_filesz > elf_ppnt->p_memsz ||
elf_ppnt->p_memsz > TASK_SIZE ||
TASK_SIZE - elf_ppnt->p_memsz < k) { /* set_brk can never work. Avoid overflows. */
retval = -EINVAL; goto out_free_dentry;
}
k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
if ((elf_ppnt->p_flags & PF_X) && end_code < k)
end_code = k; if (end_data < k)
end_data = k;
k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz; if (k > elf_brk)
elf_brk = k;
}
/** * DOC: "brk" handling * * For architectures with ELF randomization, when executing a * loader directly (i.e. static PIE: ET_DYN without PT_INTERP), * move the brk area out of the mmap region and into the unused * ELF_ET_DYN_BASE region. Since "brk" grows up it may collide * early with the stack growing down or other regions being put * into the mmap region by the kernel (e.g. vdso). * * In the CONFIG_COMPAT_BRK case, though, everything is turned * off because we're not allowed to move the brk at all.
*/ if (!IS_ENABLED(CONFIG_COMPAT_BRK) &&
IS_ENABLED(CONFIG_ARCH_HAS_ELF_RANDOMIZE) &&
elf_ex->e_type == ET_DYN && !interpreter) {
elf_brk = ELF_ET_DYN_BASE; /* This counts as moving the brk, so let brk(2) know. */
brk_moved = true;
}
mm->start_brk = mm->brk = ELF_PAGEALIGN(elf_brk);
if ((current->flags & PF_RANDOMIZE) && snapshot_randomize_va_space > 1) { /* * If we didn't move the brk to ELF_ET_DYN_BASE (above), * leave a gap between .bss and brk.
*/ if (!brk_moved)
mm->brk = mm->start_brk = mm->brk + PAGE_SIZE;
#ifdef compat_brk_randomized if (brk_moved)
current->brk_randomized = 1; #endif
if (current->personality & MMAP_PAGE_ZERO) { /* Why this, you ask??? Well SVr4 maps page 0 as read-only, and some applications "depend" upon this behavior. Since we do not have the power to recompile these, we
emulate the SVr4 behavior. Sigh. */
error = vm_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
MAP_FIXED | MAP_PRIVATE, 0);
retval = do_mseal(0, PAGE_SIZE, 0); if (retval)
pr_warn_ratelimited("pid=%d, couldn't seal address 0, ret=%d.\n",
task_pid_nr(current), retval);
}
regs = current_pt_regs(); #ifdef ELF_PLAT_INIT /* * The ABI may specify that certain registers be set up in special * ways (on i386 %edx is the address of a DT_FINI function, for * example. In addition, it may also specify (eg, PowerPC64 ELF) * that the e_entry field is the address of the function descriptor * for the startup routine, rather than the address of the startup * routine itself. This macro performs whatever initialization to * the regs structure is required as well as any relocations to the * function descriptor entries when executing dynamically links apps.
*/
ELF_PLAT_INIT(regs, reloc_func_desc); #endif
/* * fill up all the fields in prstatus from the given task struct, except * registers which need to be filled up separately.
*/ staticvoid fill_prstatus(struct elf_prstatus_common *prstatus, struct task_struct *p, long signr)
{
prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
prstatus->pr_sigpend = p->pending.signal.sig[0];
prstatus->pr_sighold = p->blocked.sig[0];
rcu_read_lock();
prstatus->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent));
rcu_read_unlock();
prstatus->pr_pid = task_pid_vnr(p);
prstatus->pr_pgrp = task_pgrp_vnr(p);
prstatus->pr_sid = task_session_vnr(p); if (thread_group_leader(p)) { struct task_cputime cputime;
/* * This is the record for the group leader. It shows the * group-wide total, not its individual thread total.
*/
thread_group_cputime(p, &cputime);
prstatus->pr_utime = ns_to_kernel_old_timeval(cputime.utime);
prstatus->pr_stime = ns_to_kernel_old_timeval(cputime.stime);
} else {
u64 utime, stime;
/* * Format of NT_FILE note: * * long count -- how many files are mapped * long page_size -- units for file_ofs * array of [COUNT] elements of * long start * long end * long file_ofs * followed by COUNT filenames in ASCII: "FILE1" NUL "FILE2" NUL...
*/ staticint fill_files_note(struct memelfnote *note, struct coredump_params *cprm)
{ unsigned count, size, names_ofs, remaining, n;
user_long_t *data;
user_long_t *start_end_ofs; char *name_base, *name_curpos; int i;
/* *Estimated* file count and total data size needed */
count = cprm->vma_count; if (count > UINT_MAX / 64) return -EINVAL;
size = count * 64;
names_ofs = (2 + 3 * count) * sizeof(data[0]);
alloc: /* paranoia check */ if (size >= core_file_note_size_limit) {
pr_warn_once("coredump Note size too large: %u (does kernel.core_file_note_size_limit sysctl need adjustment?\n",
size); return -EINVAL;
}
size = round_up(size, PAGE_SIZE); /* * "size" can be 0 here legitimately. * Let it ENOMEM and omit NT_FILE section which will be empty anyway.
*/
data = kvmalloc(size, GFP_KERNEL); if (ZERO_OR_NULL_PTR(data)) return -ENOMEM;
start_end_ofs = data + 2;
name_base = name_curpos = ((char *)data) + names_ofs;
remaining = size - names_ofs;
count = 0; for (i = 0; i < cprm->vma_count; i++) { struct core_vma_metadata *m = &cprm->vma_meta[i]; struct file *file; constchar *filename;
/* Now we know exact count of files, can store it */
data[0] = count;
data[1] = PAGE_SIZE; /* * Count usually is less than mm->map_count, * we need to move filenames down.
*/
n = cprm->vma_count - count; if (n != 0) { unsigned shift_bytes = n * 3 * sizeof(data[0]);
memmove(name_base - shift_bytes, name_base,
name_curpos - name_base);
name_curpos -= shift_bytes;
}
#ifdef CORE_DUMP_USE_REGSET /* * When a regset has a writeback hook, we call it on each thread before * dumping user memory. On register window machines, this makes sure the * user memory backing the register data is up to date before we read it.
*/ staticvoid do_thread_regset_writeback(struct task_struct *task, conststruct user_regset *regset)
{ if (regset->writeback)
regset->writeback(task, regset, 1);
}
/* * NT_PRSTATUS is the one special case, because the regset data * goes into the pr_reg field inside the note contents, rather * than being the whole note contents. We fill the regset in here. * We assume that regset 0 is NT_PRSTATUS.
*/
fill_prstatus(&t->prstatus.common, t->task, signr);
regset_get(t->task, &view->regsets[0], sizeof(t->prstatus.pr_reg), &t->prstatus.pr_reg);
/* * Each other regset might generate a note too. For each regset * that has no core_note_type or is inactive, skip it.
*/
note_iter = 1; for (view_iter = 1; view_iter < view->n; ++view_iter) { conststruct user_regset *regset = &view->regsets[view_iter]; int note_type = regset->core_note_type; constchar *note_name = regset->core_note_name; bool is_fpreg = note_type == NT_PRFPREG; void *data; int ret;
do_thread_regset_writeback(t->task, regset); if (!note_type) // not for coredumps continue; if (regset->active && regset->active(t->task, regset) <= 0) continue;
ret = regset_get_alloc(t->task, regset, ~0U, &data); if (ret < 0) continue;
if (WARN_ON_ONCE(note_iter >= info->thread_notes)) break;
if (is_fpreg)
SET_PR_FPVALID(&t->prstatus);
/* There should be a note name, but if not, guess: */ if (WARN_ON_ONCE(!note_name))
note_name = "LINUX"; else /* Warn on non-legacy-compatible names, for now. */
WARN_ON_ONCE(strcmp(note_name,
is_fpreg ? "CORE" : "LINUX"));
/* * Figure out how many notes we're going to need for each thread.
*/
info->thread_notes = 0; for (int i = 0; i < view->n; ++i) if (view->regsets[i].core_note_type != 0)
++info->thread_notes;
/* * Sanity check. We rely on regset 0 being in NT_PRSTATUS, * since it is our one special case.
*/ if (unlikely(info->thread_notes == 0) ||
unlikely(view->regsets[0].core_note_type != NT_PRSTATUS)) {
WARN_ON(1); return 0;
}
/* * Allocate a structure for each thread.
*/
info->thread = kzalloc(offsetof(struct elf_thread_core_info,
notes[info->thread_notes]),
GFP_KERNEL); if (unlikely(!info->thread)) return 0;
info->thread->task = dump_task; for (ct = dump_task->signal->core_state->dumper.next; ct; ct = ct->next) {
t = kzalloc(offsetof(struct elf_thread_core_info,
notes[info->thread_notes]),
GFP_KERNEL); if (unlikely(!t)) return 0;
/* * Now fill in each thread's information.
*/ for (t = info->thread; t != NULL; t = t->next) if (!fill_thread_core_info(t, view, cprm->siginfo->si_signo, info)) return 0;
/* * Fill in the two process-wide notes.
*/
fill_psinfo(psinfo, dump_task->group_leader, dump_task->mm);
info->size += notesize(&info->psinfo);
if (fill_files_note(&info->files, cprm) == 0)
info->size += notesize(&info->files);
return 1;
}
/* * Write all the notes for each thread. When writing the first thread, the * process-wide notes are interleaved after the first thread-specific note.
*/ staticint write_note_info(struct elf_note_info *info, struct coredump_params *cprm)
{ bool first = true; struct elf_thread_core_info *t = info->thread;
do { int i;
if (!writenote(&t->notes[0], cprm)) return 0;
if (first && !writenote(&info->psinfo, cprm)) return 0; if (first && !writenote(&info->signote, cprm)) return 0; if (first && !writenote(&info->auxv, cprm)) return 0; if (first && info->files.data &&
!writenote(&info->files, cprm)) return 0;
for (i = 1; i < info->thread_notes; ++i) if (t->notes[i].data &&
!writenote(&t->notes[i], cprm)) return 0;
/* * Actual dumper * * This is a two-pass process; first we find the offsets of the bits, * and then they are actually written out. If we run out of core limit * we just truncate.
*/ staticint elf_core_dump(struct coredump_params *cprm)
{ int has_dumped = 0; int segs, i; struct elfhdr elf;
loff_t offset = 0, dataoff; struct elf_note_info info = { }; struct elf_phdr *phdr4note = NULL; struct elf_shdr *shdr4extnum = NULL;
Elf_Half e_phnum;
elf_addr_t e_shoff;
/* * The number of segs are recored into ELF header as 16bit value. * Please check DEFAULT_MAX_MAP_COUNT definition when you modify here.
*/
segs = cprm->vma_count + elf_core_extra_phdrs(cprm);
/* for notes section */
segs++;
/* If segs > PN_XNUM(0xffff), then e_phnum overflows. To avoid * this, kernel supports extended numbering. Have a look at
* include/linux/elf.h for further information. */
e_phnum = segs > PN_XNUM ? PN_XNUM : segs;
/* * Collect all the non-memory information about the process for the * notes. This also sets up the file header.
*/ if (!fill_note_info(&elf, e_phnum, &info, cprm)) goto end_coredump;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.