|
7 | 7 | #include <linux/kernel.h>
|
8 | 8 | #include <linux/stacktrace.h>
|
9 | 9 | #include <linux/perf_event.h>
|
10 |
| -#include <linux/elf.h> |
11 |
| -#include <linux/pagemap.h> |
12 | 10 | #include <linux/irq_work.h>
|
13 | 11 | #include <linux/btf_ids.h>
|
| 12 | +#include <linux/buildid.h> |
14 | 13 | #include "percpu_freelist.h"
|
15 | 14 |
|
16 | 15 | #define STACK_CREATE_FLAG_MASK \
|
@@ -143,140 +142,6 @@ static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
|
143 | 142 | return ERR_PTR(err);
|
144 | 143 | }
|
145 | 144 |
|
146 |
| -#define BPF_BUILD_ID 3 |
147 |
| -/* |
148 |
| - * Parse build id from the note segment. This logic can be shared between |
149 |
| - * 32-bit and 64-bit system, because Elf32_Nhdr and Elf64_Nhdr are |
150 |
| - * identical. |
151 |
| - */ |
152 |
| -static inline int stack_map_parse_build_id(void *page_addr, |
153 |
| - unsigned char *build_id, |
154 |
| - void *note_start, |
155 |
| - Elf32_Word note_size) |
156 |
| -{ |
157 |
| - Elf32_Word note_offs = 0, new_offs; |
158 |
| - |
159 |
| - /* check for overflow */ |
160 |
| - if (note_start < page_addr || note_start + note_size < note_start) |
161 |
| - return -EINVAL; |
162 |
| - |
163 |
| - /* only supports note that fits in the first page */ |
164 |
| - if (note_start + note_size > page_addr + PAGE_SIZE) |
165 |
| - return -EINVAL; |
166 |
| - |
167 |
| - while (note_offs + sizeof(Elf32_Nhdr) < note_size) { |
168 |
| - Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_offs); |
169 |
| - |
170 |
| - if (nhdr->n_type == BPF_BUILD_ID && |
171 |
| - nhdr->n_namesz == sizeof("GNU") && |
172 |
| - nhdr->n_descsz > 0 && |
173 |
| - nhdr->n_descsz <= BPF_BUILD_ID_SIZE) { |
174 |
| - memcpy(build_id, |
175 |
| - note_start + note_offs + |
176 |
| - ALIGN(sizeof("GNU"), 4) + sizeof(Elf32_Nhdr), |
177 |
| - nhdr->n_descsz); |
178 |
| - memset(build_id + nhdr->n_descsz, 0, |
179 |
| - BPF_BUILD_ID_SIZE - nhdr->n_descsz); |
180 |
| - return 0; |
181 |
| - } |
182 |
| - new_offs = note_offs + sizeof(Elf32_Nhdr) + |
183 |
| - ALIGN(nhdr->n_namesz, 4) + ALIGN(nhdr->n_descsz, 4); |
184 |
| - if (new_offs <= note_offs) /* overflow */ |
185 |
| - break; |
186 |
| - note_offs = new_offs; |
187 |
| - } |
188 |
| - return -EINVAL; |
189 |
| -} |
190 |
| - |
191 |
| -/* Parse build ID from 32-bit ELF */ |
192 |
| -static int stack_map_get_build_id_32(void *page_addr, |
193 |
| - unsigned char *build_id) |
194 |
| -{ |
195 |
| - Elf32_Ehdr *ehdr = (Elf32_Ehdr *)page_addr; |
196 |
| - Elf32_Phdr *phdr; |
197 |
| - int i; |
198 |
| - |
199 |
| - /* only supports phdr that fits in one page */ |
200 |
| - if (ehdr->e_phnum > |
201 |
| - (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr)) |
202 |
| - return -EINVAL; |
203 |
| - |
204 |
| - phdr = (Elf32_Phdr *)(page_addr + sizeof(Elf32_Ehdr)); |
205 |
| - |
206 |
| - for (i = 0; i < ehdr->e_phnum; ++i) { |
207 |
| - if (phdr[i].p_type == PT_NOTE && |
208 |
| - !stack_map_parse_build_id(page_addr, build_id, |
209 |
| - page_addr + phdr[i].p_offset, |
210 |
| - phdr[i].p_filesz)) |
211 |
| - return 0; |
212 |
| - } |
213 |
| - return -EINVAL; |
214 |
| -} |
215 |
| - |
216 |
| -/* Parse build ID from 64-bit ELF */ |
217 |
| -static int stack_map_get_build_id_64(void *page_addr, |
218 |
| - unsigned char *build_id) |
219 |
| -{ |
220 |
| - Elf64_Ehdr *ehdr = (Elf64_Ehdr *)page_addr; |
221 |
| - Elf64_Phdr *phdr; |
222 |
| - int i; |
223 |
| - |
224 |
| - /* only supports phdr that fits in one page */ |
225 |
| - if (ehdr->e_phnum > |
226 |
| - (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr)) |
227 |
| - return -EINVAL; |
228 |
| - |
229 |
| - phdr = (Elf64_Phdr *)(page_addr + sizeof(Elf64_Ehdr)); |
230 |
| - |
231 |
| - for (i = 0; i < ehdr->e_phnum; ++i) { |
232 |
| - if (phdr[i].p_type == PT_NOTE && |
233 |
| - !stack_map_parse_build_id(page_addr, build_id, |
234 |
| - page_addr + phdr[i].p_offset, |
235 |
| - phdr[i].p_filesz)) |
236 |
| - return 0; |
237 |
| - } |
238 |
| - return -EINVAL; |
239 |
| -} |
240 |
| - |
241 |
| -/* Parse build ID of ELF file mapped to vma */ |
242 |
| -static int stack_map_get_build_id(struct vm_area_struct *vma, |
243 |
| - unsigned char *build_id) |
244 |
| -{ |
245 |
| - Elf32_Ehdr *ehdr; |
246 |
| - struct page *page; |
247 |
| - void *page_addr; |
248 |
| - int ret; |
249 |
| - |
250 |
| - /* only works for page backed storage */ |
251 |
| - if (!vma->vm_file) |
252 |
| - return -EINVAL; |
253 |
| - |
254 |
| - page = find_get_page(vma->vm_file->f_mapping, 0); |
255 |
| - if (!page) |
256 |
| - return -EFAULT; /* page not mapped */ |
257 |
| - |
258 |
| - ret = -EINVAL; |
259 |
| - page_addr = kmap_atomic(page); |
260 |
| - ehdr = (Elf32_Ehdr *)page_addr; |
261 |
| - |
262 |
| - /* compare magic x7f "ELF" */ |
263 |
| - if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) |
264 |
| - goto out; |
265 |
| - |
266 |
| - /* only support executable file and shared object file */ |
267 |
| - if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) |
268 |
| - goto out; |
269 |
| - |
270 |
| - if (ehdr->e_ident[EI_CLASS] == ELFCLASS32) |
271 |
| - ret = stack_map_get_build_id_32(page_addr, build_id); |
272 |
| - else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64) |
273 |
| - ret = stack_map_get_build_id_64(page_addr, build_id); |
274 |
| -out: |
275 |
| - kunmap_atomic(page_addr); |
276 |
| - put_page(page); |
277 |
| - return ret; |
278 |
| -} |
279 |
| - |
280 | 145 | static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
|
281 | 146 | u64 *ips, u32 trace_nr, bool user)
|
282 | 147 | {
|
@@ -317,18 +182,18 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
|
317 | 182 | for (i = 0; i < trace_nr; i++) {
|
318 | 183 | id_offs[i].status = BPF_STACK_BUILD_ID_IP;
|
319 | 184 | id_offs[i].ip = ips[i];
|
320 |
| - memset(id_offs[i].build_id, 0, BPF_BUILD_ID_SIZE); |
| 185 | + memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX); |
321 | 186 | }
|
322 | 187 | return;
|
323 | 188 | }
|
324 | 189 |
|
325 | 190 | for (i = 0; i < trace_nr; i++) {
|
326 | 191 | vma = find_vma(current->mm, ips[i]);
|
327 |
| - if (!vma || stack_map_get_build_id(vma, id_offs[i].build_id)) { |
| 192 | + if (!vma || build_id_parse(vma, id_offs[i].build_id)) { |
328 | 193 | /* per entry fall back to ips */
|
329 | 194 | id_offs[i].status = BPF_STACK_BUILD_ID_IP;
|
330 | 195 | id_offs[i].ip = ips[i];
|
331 |
| - memset(id_offs[i].build_id, 0, BPF_BUILD_ID_SIZE); |
| 196 | + memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX); |
332 | 197 | continue;
|
333 | 198 | }
|
334 | 199 | id_offs[i].offset = (vma->vm_pgoff << PAGE_SHIFT) + ips[i]
|
|
0 commit comments