^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Load ELF vmlinux file for the kexec_file_load syscall.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2004 Adam Litke (agl@us.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2004 IBM Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2005 R Sharada (sharada@in.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (C) 2006 Mohan Kumar M (mohan@in.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright (C) 2016 IBM Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Based on kexec-tools' kexec-elf-exec.c and kexec-elf-ppc64.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Heavily modified for the kernel by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define pr_fmt(fmt) "kexec_elf: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/kexec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static inline bool elf_is_elf_file(const struct elfhdr *ehdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return memcmp(ehdr->e_ident, ELFMAG, SELFMAG) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static uint64_t elf64_to_cpu(const struct elfhdr *ehdr, uint64_t value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) value = le64_to_cpu(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) else if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) value = be64_to_cpu(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static uint32_t elf32_to_cpu(const struct elfhdr *ehdr, uint32_t value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) value = le32_to_cpu(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) else if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) value = be32_to_cpu(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static uint16_t elf16_to_cpu(const struct elfhdr *ehdr, uint16_t value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) value = le16_to_cpu(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) else if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) value = be16_to_cpu(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * elf_is_ehdr_sane - check that it is safe to use the ELF header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * @buf_len: size of the buffer in which the ELF file is loaded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static bool elf_is_ehdr_sane(const struct elfhdr *ehdr, size_t buf_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (ehdr->e_phnum > 0 && ehdr->e_phentsize != sizeof(struct elf_phdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) pr_debug("Bad program header size.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) } else if (ehdr->e_shnum > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) ehdr->e_shentsize != sizeof(struct elf_shdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) pr_debug("Bad section header size.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) } else if (ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) ehdr->e_version != EV_CURRENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) pr_debug("Unknown ELF version.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (ehdr->e_phoff > 0 && ehdr->e_phnum > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) size_t phdr_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * e_phnum is at most 65535 so calculating the size of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * program header cannot overflow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) phdr_size = sizeof(struct elf_phdr) * ehdr->e_phnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /* Sanity check the program header table location. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (ehdr->e_phoff + phdr_size < ehdr->e_phoff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) pr_debug("Program headers at invalid location.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) } else if (ehdr->e_phoff + phdr_size > buf_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) pr_debug("Program headers truncated.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (ehdr->e_shoff > 0 && ehdr->e_shnum > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) size_t shdr_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * e_shnum is at most 65536 so calculating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * the size of the section header cannot overflow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) shdr_size = sizeof(struct elf_shdr) * ehdr->e_shnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* Sanity check the section header table location. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (ehdr->e_shoff + shdr_size < ehdr->e_shoff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) pr_debug("Section headers at invalid location.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) } else if (ehdr->e_shoff + shdr_size > buf_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) pr_debug("Section headers truncated.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int elf_read_ehdr(const char *buf, size_t len, struct elfhdr *ehdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct elfhdr *buf_ehdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (len < sizeof(*buf_ehdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) pr_debug("Buffer is too small to hold ELF header.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) memset(ehdr, 0, sizeof(*ehdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) memcpy(ehdr->e_ident, buf, sizeof(ehdr->e_ident));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (!elf_is_elf_file(ehdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) pr_debug("No ELF header magic.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (ehdr->e_ident[EI_CLASS] != ELF_CLASS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) pr_debug("Not a supported ELF class.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) } else if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) pr_debug("Not a supported ELF data format.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) buf_ehdr = (struct elfhdr *) buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (elf16_to_cpu(ehdr, buf_ehdr->e_ehsize) != sizeof(*buf_ehdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) pr_debug("Bad ELF header size.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) ehdr->e_type = elf16_to_cpu(ehdr, buf_ehdr->e_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) ehdr->e_machine = elf16_to_cpu(ehdr, buf_ehdr->e_machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) ehdr->e_version = elf32_to_cpu(ehdr, buf_ehdr->e_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) ehdr->e_flags = elf32_to_cpu(ehdr, buf_ehdr->e_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) ehdr->e_phentsize = elf16_to_cpu(ehdr, buf_ehdr->e_phentsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) ehdr->e_phnum = elf16_to_cpu(ehdr, buf_ehdr->e_phnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) ehdr->e_shentsize = elf16_to_cpu(ehdr, buf_ehdr->e_shentsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) ehdr->e_shnum = elf16_to_cpu(ehdr, buf_ehdr->e_shnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) ehdr->e_shstrndx = elf16_to_cpu(ehdr, buf_ehdr->e_shstrndx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) switch (ehdr->e_ident[EI_CLASS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) case ELFCLASS64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) ehdr->e_entry = elf64_to_cpu(ehdr, buf_ehdr->e_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) ehdr->e_phoff = elf64_to_cpu(ehdr, buf_ehdr->e_phoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) ehdr->e_shoff = elf64_to_cpu(ehdr, buf_ehdr->e_shoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) case ELFCLASS32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) ehdr->e_entry = elf32_to_cpu(ehdr, buf_ehdr->e_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ehdr->e_phoff = elf32_to_cpu(ehdr, buf_ehdr->e_phoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) ehdr->e_shoff = elf32_to_cpu(ehdr, buf_ehdr->e_shoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) pr_debug("Unknown ELF class.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return elf_is_ehdr_sane(ehdr, len) ? 0 : -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * elf_is_phdr_sane - check that it is safe to use the program header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * @buf_len: size of the buffer in which the ELF file is loaded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static bool elf_is_phdr_sane(const struct elf_phdr *phdr, size_t buf_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (phdr->p_offset + phdr->p_filesz < phdr->p_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) pr_debug("ELF segment location wraps around.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) } else if (phdr->p_offset + phdr->p_filesz > buf_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) pr_debug("ELF segment not in file.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) } else if (phdr->p_paddr + phdr->p_memsz < phdr->p_paddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) pr_debug("ELF segment address wraps around.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static int elf_read_phdr(const char *buf, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct kexec_elf_info *elf_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /* Override the const in proghdrs, we are the ones doing the loading. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct elf_phdr *phdr = (struct elf_phdr *) &elf_info->proghdrs[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) const struct elfhdr *ehdr = elf_info->ehdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) const char *pbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct elf_phdr *buf_phdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) pbuf = buf + elf_info->ehdr->e_phoff + (idx * sizeof(*buf_phdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) buf_phdr = (struct elf_phdr *) pbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) phdr->p_type = elf32_to_cpu(elf_info->ehdr, buf_phdr->p_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) phdr->p_flags = elf32_to_cpu(elf_info->ehdr, buf_phdr->p_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) switch (ehdr->e_ident[EI_CLASS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) case ELFCLASS64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) phdr->p_offset = elf64_to_cpu(ehdr, buf_phdr->p_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) phdr->p_paddr = elf64_to_cpu(ehdr, buf_phdr->p_paddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) phdr->p_vaddr = elf64_to_cpu(ehdr, buf_phdr->p_vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) phdr->p_filesz = elf64_to_cpu(ehdr, buf_phdr->p_filesz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) phdr->p_memsz = elf64_to_cpu(ehdr, buf_phdr->p_memsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) phdr->p_align = elf64_to_cpu(ehdr, buf_phdr->p_align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) case ELFCLASS32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) phdr->p_offset = elf32_to_cpu(ehdr, buf_phdr->p_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) phdr->p_paddr = elf32_to_cpu(ehdr, buf_phdr->p_paddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) phdr->p_vaddr = elf32_to_cpu(ehdr, buf_phdr->p_vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) phdr->p_filesz = elf32_to_cpu(ehdr, buf_phdr->p_filesz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) phdr->p_memsz = elf32_to_cpu(ehdr, buf_phdr->p_memsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) phdr->p_align = elf32_to_cpu(ehdr, buf_phdr->p_align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) pr_debug("Unknown ELF class.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return elf_is_phdr_sane(phdr, len) ? 0 : -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * elf_read_phdrs - read the program headers from the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * This function assumes that the program header table was checked for sanity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * Use elf_is_ehdr_sane() if it wasn't.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static int elf_read_phdrs(const char *buf, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) struct kexec_elf_info *elf_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) size_t phdr_size, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) const struct elfhdr *ehdr = elf_info->ehdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * e_phnum is at most 65535 so calculating the size of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * program header cannot overflow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) phdr_size = sizeof(struct elf_phdr) * ehdr->e_phnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) elf_info->proghdrs = kzalloc(phdr_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (!elf_info->proghdrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) for (i = 0; i < ehdr->e_phnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) ret = elf_read_phdr(buf, len, elf_info, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) kfree(elf_info->proghdrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) elf_info->proghdrs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * elf_read_from_buffer - read ELF file and sets up ELF header and ELF info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * @buf: Buffer to read ELF file from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * @len: Size of @buf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * @ehdr: Pointer to existing struct which will be populated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * @elf_info: Pointer to existing struct which will be populated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * This function allows reading ELF files with different byte order than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * the kernel, byte-swapping the fields as needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * On success returns 0, and the caller should call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * kexec_free_elf_info(elf_info) to free the memory allocated for the section
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * and program headers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) static int elf_read_from_buffer(const char *buf, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct elfhdr *ehdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct kexec_elf_info *elf_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) ret = elf_read_ehdr(buf, len, ehdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) elf_info->buffer = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) elf_info->ehdr = ehdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (ehdr->e_phoff > 0 && ehdr->e_phnum > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) ret = elf_read_phdrs(buf, len, elf_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * kexec_free_elf_info - free memory allocated by elf_read_from_buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) void kexec_free_elf_info(struct kexec_elf_info *elf_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) kfree(elf_info->proghdrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) memset(elf_info, 0, sizeof(*elf_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * kexec_build_elf_info - read ELF executable and check that we can use it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) int kexec_build_elf_info(const char *buf, size_t len, struct elfhdr *ehdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) struct kexec_elf_info *elf_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) ret = elf_read_from_buffer(buf, len, ehdr, elf_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /* Big endian vmlinux has type ET_DYN. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) pr_err("Not an ELF executable.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) } else if (!elf_info->proghdrs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) pr_err("No ELF program header.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) for (i = 0; i < ehdr->e_phnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * Kexec does not support loading interpreters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * In addition this check keeps us from attempting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * to kexec ordinay executables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (elf_info->proghdrs[i].p_type == PT_INTERP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) pr_err("Requires an ELF interpreter.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) kexec_free_elf_info(elf_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) int kexec_elf_probe(const char *buf, unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) struct elfhdr ehdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct kexec_elf_info elf_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) ret = kexec_build_elf_info(buf, len, &ehdr, &elf_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) kexec_free_elf_info(&elf_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return elf_check_arch(&ehdr) ? 0 : -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) * kexec_elf_load - load ELF executable image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * @lowest_load_addr: On return, will be the address where the first PT_LOAD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * section will be loaded in memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * 0 on success, negative value on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) struct kexec_elf_info *elf_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) struct kexec_buf *kbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) unsigned long *lowest_load_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) unsigned long lowest_addr = UINT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) /* Read in the PT_LOAD segments. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) for (i = 0; i < ehdr->e_phnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) unsigned long load_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) const struct elf_phdr *phdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) phdr = &elf_info->proghdrs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (phdr->p_type != PT_LOAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) size = phdr->p_filesz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) if (size > phdr->p_memsz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) size = phdr->p_memsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) kbuf->buffer = (void *) elf_info->buffer + phdr->p_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) kbuf->bufsz = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) kbuf->memsz = phdr->p_memsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) kbuf->buf_align = phdr->p_align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) kbuf->buf_min = phdr->p_paddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) kbuf->mem = KEXEC_BUF_MEM_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) ret = kexec_add_buffer(kbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) load_addr = kbuf->mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (load_addr < lowest_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) lowest_addr = load_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) *lowest_load_addr = lowest_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }