1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <linux/buildid.h> 4 #include <linux/cache.h> 5 #include <linux/elf.h> 6 #include <linux/kernel.h> 7 #include <linux/pagemap.h> 8 #include <linux/secretmem.h> 9 10 #define BUILD_ID 3 11 12 /* 13 * Parse build id from the note segment. This logic can be shared between 14 * 32-bit and 64-bit system, because Elf32_Nhdr and Elf64_Nhdr are 15 * identical. 16 */ 17 static int parse_build_id_buf(unsigned char *build_id, 18 __u32 *size, 19 const void *note_start, 20 Elf32_Word note_size) 21 { 22 const char note_name[] = "GNU"; 23 const size_t note_name_sz = sizeof(note_name); 24 u64 note_off = 0, new_off, name_sz, desc_sz; 25 const char *data; 26 27 while (note_off + sizeof(Elf32_Nhdr) < note_size && 28 note_off + sizeof(Elf32_Nhdr) > note_off /* overflow */) { 29 Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_off); 30 31 name_sz = READ_ONCE(nhdr->n_namesz); 32 desc_sz = READ_ONCE(nhdr->n_descsz); 33 34 new_off = note_off + sizeof(Elf32_Nhdr); 35 if (check_add_overflow(new_off, ALIGN(name_sz, 4), &new_off) || 36 check_add_overflow(new_off, ALIGN(desc_sz, 4), &new_off) || 37 new_off > note_size) 38 break; 39 40 if (nhdr->n_type == BUILD_ID && 41 name_sz == note_name_sz && 42 memcmp(nhdr + 1, note_name, note_name_sz) == 0 && 43 desc_sz > 0 && desc_sz <= BUILD_ID_SIZE_MAX) { 44 data = note_start + note_off + sizeof(Elf32_Nhdr) + ALIGN(note_name_sz, 4); 45 memcpy(build_id, data, desc_sz); 46 memset(build_id + desc_sz, 0, BUILD_ID_SIZE_MAX - desc_sz); 47 if (size) 48 *size = desc_sz; 49 return 0; 50 } 51 52 note_off = new_off; 53 } 54 55 return -EINVAL; 56 } 57 58 static inline int parse_build_id(const void *page_addr, 59 unsigned char *build_id, 60 __u32 *size, 61 const void *note_start, 62 Elf32_Word note_size) 63 { 64 /* check for overflow */ 65 if (note_start < page_addr || note_start + note_size < note_start) 66 return -EINVAL; 67 68 /* only supports note that fits in the first page */ 69 if (note_start + note_size > page_addr + PAGE_SIZE) 70 return -EINVAL; 71 72 return parse_build_id_buf(build_id, size, note_start, note_size); 73 } 74 75 /* Parse build ID from 32-bit ELF */ 76 static int get_build_id_32(const void *page_addr, unsigned char *build_id, 77 __u32 *size) 78 { 79 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)page_addr; 80 Elf32_Phdr *phdr; 81 __u32 i, phnum; 82 83 /* 84 * FIXME 85 * Neither ELF spec nor ELF loader require that program headers 86 * start immediately after ELF header. 87 */ 88 if (ehdr->e_phoff != sizeof(Elf32_Ehdr)) 89 return -EINVAL; 90 91 phnum = READ_ONCE(ehdr->e_phnum); 92 /* only supports phdr that fits in one page */ 93 if (phnum > (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr)) 94 return -EINVAL; 95 96 phdr = (Elf32_Phdr *)(page_addr + sizeof(Elf32_Ehdr)); 97 98 for (i = 0; i < phnum; ++i) { 99 if (phdr[i].p_type == PT_NOTE && 100 !parse_build_id(page_addr, build_id, size, 101 page_addr + READ_ONCE(phdr[i].p_offset), 102 READ_ONCE(phdr[i].p_filesz))) 103 return 0; 104 } 105 return -EINVAL; 106 } 107 108 /* Parse build ID from 64-bit ELF */ 109 static int get_build_id_64(const void *page_addr, unsigned char *build_id, 110 __u32 *size) 111 { 112 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)page_addr; 113 Elf64_Phdr *phdr; 114 __u32 i, phnum; 115 116 /* 117 * FIXME 118 * Neither ELF spec nor ELF loader require that program headers 119 * start immediately after ELF header. 120 */ 121 if (ehdr->e_phoff != sizeof(Elf64_Ehdr)) 122 return -EINVAL; 123 124 phnum = READ_ONCE(ehdr->e_phnum); 125 /* only supports phdr that fits in one page */ 126 if (phnum > (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr)) 127 return -EINVAL; 128 129 phdr = (Elf64_Phdr *)(page_addr + sizeof(Elf64_Ehdr)); 130 131 for (i = 0; i < phnum; ++i) { 132 if (phdr[i].p_type == PT_NOTE && 133 !parse_build_id(page_addr, build_id, size, 134 page_addr + READ_ONCE(phdr[i].p_offset), 135 READ_ONCE(phdr[i].p_filesz))) 136 return 0; 137 } 138 return -EINVAL; 139 } 140 141 /* 142 * Parse build ID of ELF file mapped to vma 143 * @vma: vma object 144 * @build_id: buffer to store build id, at least BUILD_ID_SIZE long 145 * @size: returns actual build id size in case of success 146 * 147 * Return: 0 on success, -EINVAL otherwise 148 */ 149 int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id, 150 __u32 *size) 151 { 152 Elf32_Ehdr *ehdr; 153 struct page *page; 154 void *page_addr; 155 int ret; 156 157 /* only works for page backed storage */ 158 if (!vma->vm_file) 159 return -EINVAL; 160 161 /* reject secretmem folios created with memfd_secret() */ 162 if (vma_is_secretmem(vma)) 163 return -EFAULT; 164 165 page = find_get_page(vma->vm_file->f_mapping, 0); 166 if (!page) 167 return -EFAULT; /* page not mapped */ 168 if (!PageUptodate(page)) { 169 put_page(page); 170 return -EFAULT; 171 } 172 173 ret = -EINVAL; 174 page_addr = kmap_atomic(page); 175 ehdr = (Elf32_Ehdr *)page_addr; 176 177 /* compare magic x7f "ELF" */ 178 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) 179 goto out; 180 181 /* only support executable file and shared object file */ 182 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) 183 goto out; 184 185 if (ehdr->e_ident[EI_CLASS] == ELFCLASS32) 186 ret = get_build_id_32(page_addr, build_id, size); 187 else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64) 188 ret = get_build_id_64(page_addr, build_id, size); 189 out: 190 kunmap_atomic(page_addr); 191 put_page(page); 192 return ret; 193 } 194 195 /** 196 * build_id_parse_buf - Get build ID from a buffer 197 * @buf: ELF note section(s) to parse 198 * @buf_size: Size of @buf in bytes 199 * @build_id: Build ID parsed from @buf, at least BUILD_ID_SIZE_MAX long 200 * 201 * Return: 0 on success, -EINVAL otherwise 202 */ 203 int build_id_parse_buf(const void *buf, unsigned char *build_id, u32 buf_size) 204 { 205 return parse_build_id_buf(build_id, NULL, buf, buf_size); 206 } 207 208 #if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) || IS_ENABLED(CONFIG_CRASH_CORE) 209 unsigned char vmlinux_build_id[BUILD_ID_SIZE_MAX] __ro_after_init; 210 211 /** 212 * init_vmlinux_build_id - Compute and stash the running kernel's build ID 213 */ 214 void __init init_vmlinux_build_id(void) 215 { 216 extern const void __start_notes __weak; 217 extern const void __stop_notes __weak; 218 unsigned int size = &__stop_notes - &__start_notes; 219 220 build_id_parse_buf(&__start_notes, vmlinux_build_id, size); 221 } 222 #endif 223