buildid.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/buildid.h>
  3. #include <linux/cache.h>
  4. #include <linux/elf.h>
  5. #include <linux/kernel.h>
  6. #include <linux/pagemap.h>
  7. #define BUILD_ID 3
  8. /*
  9. * Parse build id from the note segment. This logic can be shared between
  10. * 32-bit and 64-bit system, because Elf32_Nhdr and Elf64_Nhdr are
  11. * identical.
  12. */
  13. static int parse_build_id_buf(unsigned char *build_id,
  14. __u32 *size,
  15. const void *note_start,
  16. Elf32_Word note_size)
  17. {
  18. Elf32_Word note_offs = 0, new_offs;
  19. while (note_offs + sizeof(Elf32_Nhdr) < note_size) {
  20. Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_offs);
  21. if (nhdr->n_type == BUILD_ID &&
  22. nhdr->n_namesz == sizeof("GNU") &&
  23. !strcmp((char *)(nhdr + 1), "GNU") &&
  24. nhdr->n_descsz > 0 &&
  25. nhdr->n_descsz <= BUILD_ID_SIZE_MAX) {
  26. memcpy(build_id,
  27. note_start + note_offs +
  28. ALIGN(sizeof("GNU"), 4) + sizeof(Elf32_Nhdr),
  29. nhdr->n_descsz);
  30. memset(build_id + nhdr->n_descsz, 0,
  31. BUILD_ID_SIZE_MAX - nhdr->n_descsz);
  32. if (size)
  33. *size = nhdr->n_descsz;
  34. return 0;
  35. }
  36. new_offs = note_offs + sizeof(Elf32_Nhdr) +
  37. ALIGN(nhdr->n_namesz, 4) + ALIGN(nhdr->n_descsz, 4);
  38. if (new_offs <= note_offs) /* overflow */
  39. break;
  40. note_offs = new_offs;
  41. }
  42. return -EINVAL;
  43. }
  44. static inline int parse_build_id(const void *page_addr,
  45. unsigned char *build_id,
  46. __u32 *size,
  47. const void *note_start,
  48. Elf32_Word note_size)
  49. {
  50. /* check for overflow */
  51. if (note_start < page_addr || note_start + note_size < note_start)
  52. return -EINVAL;
  53. /* only supports note that fits in the first page */
  54. if (note_start + note_size > page_addr + PAGE_SIZE)
  55. return -EINVAL;
  56. return parse_build_id_buf(build_id, size, note_start, note_size);
  57. }
  58. /* Parse build ID from 32-bit ELF */
  59. static int get_build_id_32(const void *page_addr, unsigned char *build_id,
  60. __u32 *size)
  61. {
  62. Elf32_Ehdr *ehdr = (Elf32_Ehdr *)page_addr;
  63. Elf32_Phdr *phdr;
  64. int i;
  65. /* only supports phdr that fits in one page */
  66. if (ehdr->e_phnum >
  67. (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr))
  68. return -EINVAL;
  69. phdr = (Elf32_Phdr *)(page_addr + sizeof(Elf32_Ehdr));
  70. for (i = 0; i < ehdr->e_phnum; ++i) {
  71. if (phdr[i].p_type == PT_NOTE &&
  72. !parse_build_id(page_addr, build_id, size,
  73. page_addr + phdr[i].p_offset,
  74. phdr[i].p_filesz))
  75. return 0;
  76. }
  77. return -EINVAL;
  78. }
  79. /* Parse build ID from 64-bit ELF */
  80. static int get_build_id_64(const void *page_addr, unsigned char *build_id,
  81. __u32 *size)
  82. {
  83. Elf64_Ehdr *ehdr = (Elf64_Ehdr *)page_addr;
  84. Elf64_Phdr *phdr;
  85. int i;
  86. /* only supports phdr that fits in one page */
  87. if (ehdr->e_phnum >
  88. (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr))
  89. return -EINVAL;
  90. phdr = (Elf64_Phdr *)(page_addr + sizeof(Elf64_Ehdr));
  91. for (i = 0; i < ehdr->e_phnum; ++i) {
  92. if (phdr[i].p_type == PT_NOTE &&
  93. !parse_build_id(page_addr, build_id, size,
  94. page_addr + phdr[i].p_offset,
  95. phdr[i].p_filesz))
  96. return 0;
  97. }
  98. return -EINVAL;
  99. }
  100. /*
  101. * Parse build ID of ELF file mapped to vma
  102. * @vma: vma object
  103. * @build_id: buffer to store build id, at least BUILD_ID_SIZE long
  104. * @size: returns actual build id size in case of success
  105. *
  106. * Return: 0 on success, -EINVAL otherwise
  107. */
  108. int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id,
  109. __u32 *size)
  110. {
  111. Elf32_Ehdr *ehdr;
  112. struct page *page;
  113. void *page_addr;
  114. int ret;
  115. /* only works for page backed storage */
  116. if (!vma->vm_file)
  117. return -EINVAL;
  118. page = find_get_page(vma->vm_file->f_mapping, 0);
  119. if (!page)
  120. return -EFAULT; /* page not mapped */
  121. ret = -EINVAL;
  122. page_addr = kmap_atomic(page);
  123. ehdr = (Elf32_Ehdr *)page_addr;
  124. /* compare magic x7f "ELF" */
  125. if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0)
  126. goto out;
  127. /* only support executable file and shared object file */
  128. if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)
  129. goto out;
  130. if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
  131. ret = get_build_id_32(page_addr, build_id, size);
  132. else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
  133. ret = get_build_id_64(page_addr, build_id, size);
  134. out:
  135. kunmap_atomic(page_addr);
  136. put_page(page);
  137. return ret;
  138. }
  139. /**
  140. * build_id_parse_buf - Get build ID from a buffer
  141. * @buf: Elf note section(s) to parse
  142. * @buf_size: Size of @buf in bytes
  143. * @build_id: Build ID parsed from @buf, at least BUILD_ID_SIZE_MAX long
  144. *
  145. * Return: 0 on success, -EINVAL otherwise
  146. */
  147. int build_id_parse_buf(const void *buf, unsigned char *build_id, u32 buf_size)
  148. {
  149. return parse_build_id_buf(build_id, NULL, buf, buf_size);
  150. }
  151. #if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) || IS_ENABLED(CONFIG_CRASH_CORE)
  152. unsigned char vmlinux_build_id[BUILD_ID_SIZE_MAX] __ro_after_init;
  153. /**
  154. * init_vmlinux_build_id - Compute and stash the running kernel's build ID
  155. */
  156. void __init init_vmlinux_build_id(void)
  157. {
  158. extern const void __start_notes __weak;
  159. extern const void __stop_notes __weak;
  160. unsigned int size = &__stop_notes - &__start_notes;
  161. build_id_parse_buf(&__start_notes, vmlinux_build_id, size);
  162. }
  163. #endif