2021-01-14 13:40:42 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include <linux/buildid.h>
|
2021-07-08 01:09:13 +00:00
|
|
|
#include <linux/cache.h>
|
2021-01-14 13:40:42 +00:00
|
|
|
#include <linux/elf.h>
|
2021-07-08 01:09:10 +00:00
|
|
|
#include <linux/kernel.h>
|
2021-01-14 13:40:42 +00:00
|
|
|
#include <linux/pagemap.h>
|
|
|
|
|
|
|
|
#define BUILD_ID 3
|
2021-07-08 01:09:10 +00:00
|
|
|
|
2021-01-14 13:40:42 +00:00
|
|
|
/*
|
|
|
|
* Parse build id from the note segment. This logic can be shared between
|
|
|
|
* 32-bit and 64-bit system, because Elf32_Nhdr and Elf64_Nhdr are
|
|
|
|
* identical.
|
|
|
|
*/
|
2021-07-08 01:09:10 +00:00
|
|
|
static int parse_build_id_buf(unsigned char *build_id,
|
|
|
|
__u32 *size,
|
|
|
|
const void *note_start,
|
|
|
|
Elf32_Word note_size)
|
2021-01-14 13:40:42 +00:00
|
|
|
{
|
2024-08-29 17:42:23 +00:00
|
|
|
const char note_name[] = "GNU";
|
|
|
|
const size_t note_name_sz = sizeof(note_name);
|
|
|
|
u64 note_off = 0, new_off, name_sz, desc_sz;
|
|
|
|
const char *data;
|
|
|
|
|
|
|
|
while (note_off + sizeof(Elf32_Nhdr) < note_size &&
|
|
|
|
note_off + sizeof(Elf32_Nhdr) > note_off /* overflow */) {
|
|
|
|
Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_off);
|
|
|
|
|
|
|
|
name_sz = READ_ONCE(nhdr->n_namesz);
|
|
|
|
desc_sz = READ_ONCE(nhdr->n_descsz);
|
|
|
|
|
|
|
|
new_off = note_off + sizeof(Elf32_Nhdr);
|
|
|
|
if (check_add_overflow(new_off, ALIGN(name_sz, 4), &new_off) ||
|
|
|
|
check_add_overflow(new_off, ALIGN(desc_sz, 4), &new_off) ||
|
|
|
|
new_off > note_size)
|
|
|
|
break;
|
2021-01-14 13:40:42 +00:00
|
|
|
|
|
|
|
if (nhdr->n_type == BUILD_ID &&
|
2024-08-29 17:42:23 +00:00
|
|
|
name_sz == note_name_sz &&
|
|
|
|
memcmp(nhdr + 1, note_name, note_name_sz) == 0 &&
|
|
|
|
desc_sz > 0 && desc_sz <= BUILD_ID_SIZE_MAX) {
|
|
|
|
data = note_start + note_off + ALIGN(note_name_sz, 4);
|
|
|
|
memcpy(build_id, data, desc_sz);
|
|
|
|
memset(build_id + desc_sz, 0, BUILD_ID_SIZE_MAX - desc_sz);
|
2021-01-14 13:40:43 +00:00
|
|
|
if (size)
|
2024-08-29 17:42:23 +00:00
|
|
|
*size = desc_sz;
|
2021-01-14 13:40:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2024-08-29 17:42:23 +00:00
|
|
|
|
|
|
|
note_off = new_off;
|
2021-01-14 13:40:42 +00:00
|
|
|
}
|
2021-07-08 01:09:10 +00:00
|
|
|
|
2021-01-14 13:40:42 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2021-07-08 01:09:42 +00:00
|
|
|
static inline int parse_build_id(const void *page_addr,
|
2021-07-08 01:09:10 +00:00
|
|
|
unsigned char *build_id,
|
|
|
|
__u32 *size,
|
2021-07-08 01:09:42 +00:00
|
|
|
const void *note_start,
|
2021-07-08 01:09:10 +00:00
|
|
|
Elf32_Word note_size)
|
|
|
|
{
|
|
|
|
/* check for overflow */
|
|
|
|
if (note_start < page_addr || note_start + note_size < note_start)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* only supports note that fits in the first page */
|
|
|
|
if (note_start + note_size > page_addr + PAGE_SIZE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return parse_build_id_buf(build_id, size, note_start, note_size);
|
|
|
|
}
|
|
|
|
|
2021-01-14 13:40:42 +00:00
|
|
|
/* Parse build ID from 32-bit ELF */
|
2021-07-08 01:09:42 +00:00
|
|
|
static int get_build_id_32(const void *page_addr, unsigned char *build_id,
|
2021-01-14 13:40:43 +00:00
|
|
|
__u32 *size)
|
2021-01-14 13:40:42 +00:00
|
|
|
{
|
|
|
|
Elf32_Ehdr *ehdr = (Elf32_Ehdr *)page_addr;
|
|
|
|
Elf32_Phdr *phdr;
|
2024-08-29 17:42:23 +00:00
|
|
|
__u32 i, phnum;
|
2021-01-14 13:40:42 +00:00
|
|
|
|
2024-06-21 18:39:33 +00:00
|
|
|
/*
|
|
|
|
* FIXME
|
|
|
|
* Neither ELF spec nor ELF loader require that program headers
|
|
|
|
* start immediately after ELF header.
|
|
|
|
*/
|
|
|
|
if (ehdr->e_phoff != sizeof(Elf32_Ehdr))
|
|
|
|
return -EINVAL;
|
2024-08-29 17:42:23 +00:00
|
|
|
|
|
|
|
phnum = READ_ONCE(ehdr->e_phnum);
|
2021-01-14 13:40:42 +00:00
|
|
|
/* only supports phdr that fits in one page */
|
2024-08-29 17:42:23 +00:00
|
|
|
if (phnum > (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr))
|
2021-01-14 13:40:42 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
phdr = (Elf32_Phdr *)(page_addr + sizeof(Elf32_Ehdr));
|
|
|
|
|
2024-08-29 17:42:23 +00:00
|
|
|
for (i = 0; i < phnum; ++i) {
|
2021-01-14 13:40:42 +00:00
|
|
|
if (phdr[i].p_type == PT_NOTE &&
|
2021-01-14 13:40:43 +00:00
|
|
|
!parse_build_id(page_addr, build_id, size,
|
2024-08-29 17:42:23 +00:00
|
|
|
page_addr + READ_ONCE(phdr[i].p_offset),
|
|
|
|
READ_ONCE(phdr[i].p_filesz)))
|
2021-01-14 13:40:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse build ID from 64-bit ELF */
|
2021-07-08 01:09:42 +00:00
|
|
|
static int get_build_id_64(const void *page_addr, unsigned char *build_id,
|
2021-01-14 13:40:43 +00:00
|
|
|
__u32 *size)
|
2021-01-14 13:40:42 +00:00
|
|
|
{
|
|
|
|
Elf64_Ehdr *ehdr = (Elf64_Ehdr *)page_addr;
|
|
|
|
Elf64_Phdr *phdr;
|
2024-08-29 17:42:23 +00:00
|
|
|
__u32 i, phnum;
|
2021-01-14 13:40:42 +00:00
|
|
|
|
2024-06-21 18:39:33 +00:00
|
|
|
/*
|
|
|
|
* FIXME
|
|
|
|
* Neither ELF spec nor ELF loader require that program headers
|
|
|
|
* start immediately after ELF header.
|
|
|
|
*/
|
|
|
|
if (ehdr->e_phoff != sizeof(Elf64_Ehdr))
|
|
|
|
return -EINVAL;
|
2024-08-29 17:42:23 +00:00
|
|
|
|
|
|
|
phnum = READ_ONCE(ehdr->e_phnum);
|
2021-01-14 13:40:42 +00:00
|
|
|
/* only supports phdr that fits in one page */
|
2024-08-29 17:42:23 +00:00
|
|
|
if (phnum > (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr))
|
2021-01-14 13:40:42 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
phdr = (Elf64_Phdr *)(page_addr + sizeof(Elf64_Ehdr));
|
|
|
|
|
2024-08-29 17:42:23 +00:00
|
|
|
for (i = 0; i < phnum; ++i) {
|
2021-01-14 13:40:42 +00:00
|
|
|
if (phdr[i].p_type == PT_NOTE &&
|
2021-01-14 13:40:43 +00:00
|
|
|
!parse_build_id(page_addr, build_id, size,
|
2024-08-29 17:42:23 +00:00
|
|
|
page_addr + READ_ONCE(phdr[i].p_offset),
|
|
|
|
READ_ONCE(phdr[i].p_filesz)))
|
2021-01-14 13:40:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2021-01-14 13:40:43 +00:00
|
|
|
/*
|
|
|
|
* Parse build ID of ELF file mapped to vma
|
|
|
|
* @vma: vma object
|
|
|
|
* @build_id: buffer to store build id, at least BUILD_ID_SIZE long
|
|
|
|
* @size: returns actual build id size in case of success
|
|
|
|
*
|
2021-07-08 01:09:46 +00:00
|
|
|
* Return: 0 on success, -EINVAL otherwise
|
2021-01-14 13:40:43 +00:00
|
|
|
*/
|
|
|
|
int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id,
|
|
|
|
__u32 *size)
|
2021-01-14 13:40:42 +00:00
|
|
|
{
|
|
|
|
Elf32_Ehdr *ehdr;
|
|
|
|
struct page *page;
|
|
|
|
void *page_addr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* only works for page backed storage */
|
|
|
|
if (!vma->vm_file)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
page = find_get_page(vma->vm_file->f_mapping, 0);
|
|
|
|
if (!page)
|
|
|
|
return -EFAULT; /* page not mapped */
|
2024-08-29 17:42:23 +00:00
|
|
|
if (!PageUptodate(page)) {
|
|
|
|
put_page(page);
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
2021-01-14 13:40:42 +00:00
|
|
|
|
|
|
|
ret = -EINVAL;
|
2024-03-06 03:48:04 +00:00
|
|
|
page_addr = kmap_local_page(page);
|
2021-01-14 13:40:42 +00:00
|
|
|
ehdr = (Elf32_Ehdr *)page_addr;
|
|
|
|
|
|
|
|
/* compare magic x7f "ELF" */
|
|
|
|
if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* only support executable file and shared object file */
|
|
|
|
if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
|
2021-01-14 13:40:43 +00:00
|
|
|
ret = get_build_id_32(page_addr, build_id, size);
|
2021-01-14 13:40:42 +00:00
|
|
|
else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
|
2021-01-14 13:40:43 +00:00
|
|
|
ret = get_build_id_64(page_addr, build_id, size);
|
2021-01-14 13:40:42 +00:00
|
|
|
out:
|
2024-03-06 03:48:04 +00:00
|
|
|
kunmap_local(page_addr);
|
2021-01-14 13:40:42 +00:00
|
|
|
put_page(page);
|
|
|
|
return ret;
|
|
|
|
}
|
2021-07-08 01:09:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* build_id_parse_buf - Get build ID from a buffer
|
2023-02-28 12:14:17 +00:00
|
|
|
* @buf: ELF note section(s) to parse
|
2021-07-08 01:09:10 +00:00
|
|
|
* @buf_size: Size of @buf in bytes
|
|
|
|
* @build_id: Build ID parsed from @buf, at least BUILD_ID_SIZE_MAX long
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -EINVAL otherwise
|
|
|
|
*/
|
|
|
|
int build_id_parse_buf(const void *buf, unsigned char *build_id, u32 buf_size)
|
|
|
|
{
|
|
|
|
return parse_build_id_buf(build_id, NULL, buf, buf_size);
|
|
|
|
}
|
2021-07-08 01:09:13 +00:00
|
|
|
|
2024-01-24 05:12:42 +00:00
|
|
|
#if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) || IS_ENABLED(CONFIG_VMCORE_INFO)
|
2021-07-08 01:09:13 +00:00
|
|
|
unsigned char vmlinux_build_id[BUILD_ID_SIZE_MAX] __ro_after_init;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* init_vmlinux_build_id - Compute and stash the running kernel's build ID
|
|
|
|
*/
|
|
|
|
void __init init_vmlinux_build_id(void)
|
|
|
|
{
|
2024-04-15 16:20:44 +00:00
|
|
|
extern const void __start_notes;
|
|
|
|
extern const void __stop_notes;
|
2021-07-08 01:09:13 +00:00
|
|
|
unsigned int size = &__stop_notes - &__start_notes;
|
|
|
|
|
|
|
|
build_id_parse_buf(&__start_notes, vmlinux_build_id, size);
|
|
|
|
}
|
2021-07-08 01:09:17 +00:00
|
|
|
#endif
|