/** * elf_is_phdr_sane - check that it is safe to use the program header * @buf_len: size of the buffer in which the ELF file is loaded.
*/ staticbool elf_is_phdr_sane(conststruct elf_phdr *phdr, size_t buf_len)
{
/** * elf_read_phdrs - read the program headers from the buffer * * This function assumes that the program header table was checked for sanity. * Use elf_is_ehdr_sane() if it wasn't.
*/ staticint elf_read_phdrs(constchar *buf, size_t len, struct kexec_elf_info *elf_info)
{
size_t phdr_size, i; conststruct elfhdr *ehdr = elf_info->ehdr;
/* * e_phnum is at most 65535 so calculating the size of the * program header cannot overflow.
*/
phdr_size = sizeof(struct elf_phdr) * ehdr->e_phnum;
elf_info->proghdrs = kzalloc(phdr_size, GFP_KERNEL); if (!elf_info->proghdrs) return -ENOMEM;
for (i = 0; i < ehdr->e_phnum; i++) { int ret;
ret = elf_read_phdr(buf, len, elf_info, i); if (ret) {
kfree(elf_info->proghdrs);
elf_info->proghdrs = NULL; return ret;
}
}
return 0;
}
/** * elf_read_from_buffer - read ELF file and sets up ELF header and ELF info * @buf: Buffer to read ELF file from. * @len: Size of @buf. * @ehdr: Pointer to existing struct which will be populated. * @elf_info: Pointer to existing struct which will be populated. * * This function allows reading ELF files with different byte order than * the kernel, byte-swapping the fields as needed. * * Return: * On success returns 0, and the caller should call * kexec_free_elf_info(elf_info) to free the memory allocated for the section * and program headers.
*/ staticint elf_read_from_buffer(constchar *buf, size_t len, struct elfhdr *ehdr, struct kexec_elf_info *elf_info)
{ int ret;
ret = elf_read_ehdr(buf, len, ehdr); if (ret) return ret;
elf_info->buffer = buf;
elf_info->ehdr = ehdr; if (ehdr->e_phoff > 0 && ehdr->e_phnum > 0) {
ret = elf_read_phdrs(buf, len, elf_info); if (ret) return ret;
} return 0;
}
/** * kexec_free_elf_info - free memory allocated by elf_read_from_buffer
*/ void kexec_free_elf_info(struct kexec_elf_info *elf_info)
{
kfree(elf_info->proghdrs);
memset(elf_info, 0, sizeof(*elf_info));
} /** * kexec_build_elf_info - read ELF executable and check that we can use it
*/ int kexec_build_elf_info(constchar *buf, size_t len, struct elfhdr *ehdr, struct kexec_elf_info *elf_info)
{ int i; int ret;
ret = elf_read_from_buffer(buf, len, ehdr, elf_info); if (ret) return ret;
/* Big endian vmlinux has type ET_DYN. */ if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
pr_err("Not an ELF executable.\n"); goto error;
} elseif (!elf_info->proghdrs) {
pr_err("No ELF program header.\n"); goto error;
}
for (i = 0; i < ehdr->e_phnum; i++) { /* * Kexec does not support loading interpreters. * In addition this check keeps us from attempting * to kexec ordinay executables.
*/ if (elf_info->proghdrs[i].p_type == PT_INTERP) {
pr_err("Requires an ELF interpreter.\n"); goto error;
}
}
int kexec_elf_probe(constchar *buf, unsignedlong len)
{ struct elfhdr ehdr; struct kexec_elf_info elf_info; int ret;
ret = kexec_build_elf_info(buf, len, &ehdr, &elf_info); if (ret) return ret;
kexec_free_elf_info(&elf_info);
return elf_check_arch(&ehdr) ? 0 : -ENOEXEC;
}
/** * kexec_elf_load - load ELF executable image * @lowest_load_addr: On return, will be the address where the first PT_LOAD * section will be loaded in memory. * * Return: * 0 on success, negative value on failure.
*/ int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr, struct kexec_elf_info *elf_info, struct kexec_buf *kbuf, unsignedlong *lowest_load_addr)
{ unsignedlong lowest_addr = ULONG_MAX; int ret;
size_t i;
/* Read in the PT_LOAD segments. */ for (i = 0; i < ehdr->e_phnum; i++) { unsignedlong load_addr;
size_t size; conststruct elf_phdr *phdr;
phdr = &elf_info->proghdrs[i]; if (phdr->p_type != PT_LOAD) continue;
size = phdr->p_filesz; if (size > phdr->p_memsz)
size = phdr->p_memsz;
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.