core_elf32.c revision 1.61.2.1 1 /* $NetBSD: core_elf32.c,v 1.61.2.1 2020/01/17 21:47:35 ad Exp $ */
2
3 /*
4 * Copyright (c) 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * core_elf32.c/core_elf64.c: Support for the Elf32/Elf64 core file format.
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(1, "$NetBSD: core_elf32.c,v 1.61.2.1 2020/01/17 21:47:35 ad Exp $");
44
45 #ifdef _KERNEL_OPT
46 #include "opt_compat_netbsd32.h"
47 #endif
48
49 #ifndef ELFSIZE
50 #define ELFSIZE 32
51 #endif
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/proc.h>
56 #include <sys/vnode.h>
57 #include <sys/exec.h>
58 #include <sys/exec_elf.h>
59 #include <sys/ptrace.h>
60 #include <sys/kmem.h>
61 #include <sys/kauth.h>
62 #include <sys/compat_stub.h>
63
64 #include <machine/reg.h>
65
66 #include <uvm/uvm_extern.h>
67
68 struct writesegs_state {
69 Elf_Phdr *psections;
70 proc_t *p;
71 off_t secoff;
72 size_t npsections;
73 };
74
75 /*
76 * We need to know how big the 'notes' are before we write the main header.
77 * To avoid problems with double-processing we save the data.
78 */
79 struct note_buf {
80 struct note_buf *nb_next;
81 unsigned char nb_data[4096 - sizeof (void *)];
82 };
83
84 struct note_state {
85 struct note_buf *ns_first;
86 struct note_buf *ns_last;
87 unsigned int ns_count; /* Of full buffers */
88 unsigned int ns_offset; /* Write point in last buffer */
89 };
90
91 static int ELFNAMEEND(coredump_getseghdrs)(struct uvm_coredump_state *);
92
93 static int ELFNAMEEND(coredump_notes)(struct lwp *, struct note_state *);
94 static int ELFNAMEEND(coredump_note)(struct lwp *, struct note_state *);
95
96 /* The 'note' section names and data are always 4-byte aligned. */
97 #define ELFROUNDSIZE 4 /* XXX Should it be sizeof(Elf_Word)? */
98
99 #define elf_read_lwpstatus CONCAT(process_read_lwpstatus, ELFSIZE)
100 #define elf_lwpstatus CONCAT(process_lwpstatus, ELFSIZE)
101
102 #define elf_process_read_regs CONCAT(process_read_regs, ELFSIZE)
103 #define elf_process_read_fpregs CONCAT(process_read_fpregs, ELFSIZE)
104 #define elf_reg CONCAT(process_reg, ELFSIZE)
105 #define elf_fpreg CONCAT(process_fpreg, ELFSIZE)
106
107 int
108 ELFNAMEEND(coredump)(struct lwp *l, struct coredump_iostate *cookie)
109 {
110 Elf_Ehdr ehdr;
111 Elf_Shdr shdr;
112 Elf_Phdr *psections;
113 size_t psectionssize;
114 int npsections;
115 struct writesegs_state ws;
116 off_t notestart;
117 size_t notesize;
118 int error, i;
119 off_t offset __diagused;
120
121 struct note_state ns;
122 struct note_buf *nb;
123
124 psections = NULL;
125
126 /* Get all of the notes (mostly all the registers). */
127 ns.ns_first = kmem_alloc(sizeof *ns.ns_first, KM_SLEEP);
128 ns.ns_last = ns.ns_first;
129 ns.ns_count = 0;
130 ns.ns_offset = 0;
131 error = ELFNAMEEND(coredump_notes)(l, &ns);
132 ns.ns_last->nb_next = NULL;
133 if (error)
134 goto out;
135 notesize = ns.ns_count * sizeof nb->nb_data + ns.ns_offset;
136
137 /*
138 * We have to make a total of 3 passes across the map:
139 *
140 * 1. Count the number of map entries (the number of
141 * PT_LOAD sections in the dump).
142 *
143 * 2. Write the P-section headers.
144 *
145 * 3. Write the P-sections.
146 */
147
148 /* Pass 1: count the entries. */
149 MODULE_HOOK_CALL(uvm_coredump_count_segs_hook,
150 (l->l_proc), 0, npsections);
151 /* Allow for the PT_NOTE section. */
152 npsections++;
153
154 /* Build the main elf header */
155 memset(&ehdr.e_ident[EI_PAD], 0, sizeof(ehdr.e_ident) - EI_PAD);
156 memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
157 #if ELFSIZE == 32
158 ehdr.e_ident[EI_CLASS] = ELFCLASS32;
159 #elif ELFSIZE == 64
160 ehdr.e_ident[EI_CLASS] = ELFCLASS64;
161 #endif
162 ehdr.e_ident[EI_DATA] = ELFDEFNNAME(MACHDEP_ENDIANNESS);
163 ehdr.e_ident[EI_VERSION] = EV_CURRENT;
164 /*
165 * NetBSD sets generic SYSV OSABI and ABI version 0
166 * Native ELF files are distinguishable with NetBSD specific notes
167 */
168 ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
169 ehdr.e_ident[EI_ABIVERSION] = 0;
170
171 ehdr.e_type = ET_CORE;
172 /* XXX This should be the e_machine of the executable. */
173 ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
174 ehdr.e_version = EV_CURRENT;
175 ehdr.e_entry = 0;
176 ehdr.e_flags = 0;
177 ehdr.e_ehsize = sizeof(ehdr);
178 ehdr.e_phentsize = sizeof(Elf_Phdr);
179 if (npsections < PN_XNUM) {
180 ehdr.e_phnum = npsections;
181 ehdr.e_shentsize = 0;
182 ehdr.e_shnum = 0;
183 ehdr.e_shoff = 0;
184 ehdr.e_phoff = sizeof(ehdr);
185 } else {
186 ehdr.e_phnum = PN_XNUM;
187 ehdr.e_shentsize = sizeof(Elf_Shdr);
188 ehdr.e_shnum = 1;
189 ehdr.e_shoff = sizeof(ehdr);
190 ehdr.e_phoff = sizeof(ehdr) + sizeof(shdr);
191 }
192 ehdr.e_shstrndx = 0;
193
194 #ifdef ELF_MD_COREDUMP_SETUP
195 ELF_MD_COREDUMP_SETUP(l, &ehdr);
196 #endif
197
198 /* Write out the ELF header. */
199 MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_SYSSPACE, &ehdr,
200 sizeof(ehdr)), ENOSYS, error);
201 if (error)
202 goto out;
203
204 /* Write out sections, if needed */
205 if (npsections >= PN_XNUM) {
206 memset(&shdr, 0, sizeof(shdr));
207 shdr.sh_type = SHT_NULL;
208 shdr.sh_info = npsections;
209 MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_SYSSPACE,
210 &shdr, sizeof(shdr)), ENOSYS, error);
211 if (error)
212 goto out;
213 }
214
215 psectionssize = npsections * sizeof(*psections);
216 notestart = ehdr.e_phoff + psectionssize;
217
218 psections = kmem_zalloc(psectionssize, KM_SLEEP);
219
220 /* Pass 2: now find the P-section headers. */
221 ws.secoff = notestart + notesize;
222 ws.psections = psections;
223 ws.npsections = npsections - 1;
224 ws.p = l->l_proc;
225 MODULE_HOOK_CALL(uvm_coredump_walkmap_hook,
226 (l->l_proc, ELFNAMEEND(coredump_getseghdrs), &ws), ENOSYS, error);
227 if (error)
228 goto out;
229 if (ws.npsections != 0) {
230 /* A section went away */
231 error = ENOMEM;
232 goto out;
233 }
234
235 /* Add the PT_NOTE header after the P-section headers. */
236 ws.psections->p_type = PT_NOTE;
237 ws.psections->p_offset = notestart;
238 ws.psections->p_vaddr = 0;
239 ws.psections->p_paddr = 0;
240 ws.psections->p_filesz = notesize;
241 ws.psections->p_memsz = 0;
242 ws.psections->p_flags = PF_R;
243 ws.psections->p_align = ELFROUNDSIZE;
244
245 /* Write the P-section headers followed by the PT_NOTE header */
246 MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_SYSSPACE, psections,
247 psectionssize), ENOSYS, error);
248 if (error)
249 goto out;
250
251 #ifdef DIAGNOSTIC
252 MODULE_HOOK_CALL(coredump_offset_hook, (cookie), 0, offset);
253 if (offset != notestart)
254 panic("coredump: offset %lld != notestart %lld",
255 (long long) offset,
256 (long long) notestart);
257 #endif
258
259 /* Write out the notes. */
260 for (nb = ns.ns_first; nb != NULL; nb = nb->nb_next) {
261 MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_SYSSPACE,
262 nb->nb_data,
263 nb->nb_next == NULL ? ns.ns_offset : sizeof nb->nb_data),
264 ENOSYS, error);
265 if (error)
266 goto out;
267 }
268
269 /* Finally, write the sections themselves. */
270 for (i = 0; i < npsections - 1; i++) {
271 if (psections[i].p_filesz == 0)
272 continue;
273
274 #ifdef DIAGNOSTIC
275 MODULE_HOOK_CALL(coredump_offset_hook, (cookie), 0, offset);
276 if (offset != psections[i].p_offset)
277 panic("coredump: offset %lld != p_offset[%d] %lld",
278 (long long) offset, i,
279 (long long) psections[i].p_filesz);
280 #endif
281
282 MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_USERSPACE,
283 (void *)(vaddr_t)psections[i].p_vaddr,
284 psections[i].p_filesz), ENOSYS, error);
285 if (error)
286 goto out;
287 }
288
289 out:
290 if (psections)
291 kmem_free(psections, psectionssize);
292 while ((nb = ns.ns_first) != NULL) {
293 ns.ns_first = nb->nb_next;
294 kmem_free(nb, sizeof *nb);
295 }
296 return (error);
297 }
298
299 static int
300 ELFNAMEEND(coredump_getseghdrs)(struct uvm_coredump_state *us)
301 {
302 struct writesegs_state *ws = us->cookie;
303 Elf_Phdr phdr;
304 vsize_t size, realsize;
305 vaddr_t end;
306 int error;
307
308 /* Don't overrun if there are more sections */
309 if (ws->npsections == 0)
310 return ENOMEM;
311 ws->npsections--;
312
313 size = us->end - us->start;
314 realsize = us->realend - us->start;
315 end = us->realend;
316
317 /* Don't bother writing out trailing zeros */
318 while (realsize > 0) {
319 long buf[1024 / sizeof(long)];
320 size_t slen = realsize > sizeof(buf) ? sizeof(buf) : realsize;
321 const long *ep;
322 int i;
323
324 end -= slen;
325 if ((error = copyin_proc(ws->p, (void *)end, buf, slen)) != 0) {
326 /*
327 * In case of any errors of scanning the segments reset
328 * their content to a default value with zeros. This is
329 * achieved with shortening the p_filesz parameter.
330 *
331 * This allows to emit core(5) files for a process
332 * regardless of its state of mappings, such as mapping
333 * pages after EOF in a file.
334 */
335 realsize -= slen;
336 continue;
337 }
338
339 ep = (const long *) &buf[slen / sizeof(buf[0])];
340 for (i = 0, ep--; buf <= ep; ep--, i++) {
341 if (*ep)
342 break;
343 }
344 realsize -= i * sizeof(buf[0]);
345 if (i * sizeof(buf[0]) < slen)
346 break;
347 }
348
349 phdr.p_type = PT_LOAD;
350 phdr.p_offset = ws->secoff;
351 phdr.p_vaddr = us->start;
352 phdr.p_paddr = 0;
353 phdr.p_filesz = realsize;
354 phdr.p_memsz = size;
355 phdr.p_flags = 0;
356 if (us->prot & VM_PROT_READ)
357 phdr.p_flags |= PF_R;
358 if (us->prot & VM_PROT_WRITE)
359 phdr.p_flags |= PF_W;
360 if (us->prot & VM_PROT_EXECUTE)
361 phdr.p_flags |= PF_X;
362 phdr.p_align = PAGE_SIZE;
363
364 ws->secoff += phdr.p_filesz;
365 *ws->psections++ = phdr;
366
367 return (0);
368 }
369
370 static void
371 coredump_note_procinfo(struct lwp *l, struct note_state *ns)
372 {
373 struct proc *p;
374 struct netbsd_elfcore_procinfo cpi;
375
376 p = l->l_proc;
377
378 /* First, write an elfcore_procinfo. */
379 cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
380 cpi.cpi_cpisize = sizeof(cpi);
381 cpi.cpi_signo = p->p_sigctx.ps_info._signo;
382 cpi.cpi_sigcode = p->p_sigctx.ps_info._code;
383 cpi.cpi_siglwp = p->p_sigctx.ps_lwp;
384
385 /*
386 * per-LWP pending signals are stored in PT_LWPSTATUS@nnn.
387 */
388 memcpy(&cpi.cpi_sigpend, &p->p_sigpend.sp_set, sizeof(cpi.cpi_sigpend));
389
390 /*
391 * Signal mask is stored on a per-LWP basis in PT_LWPSTATUS@nnn.
392 * For compatibility purposes, cpi_sigmask is present, but zeroed.
393 */
394 memset(&cpi.cpi_sigmask, 0, sizeof(cpi.cpi_sigmask));
395
396 memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
397 sizeof(cpi.cpi_sigignore));
398 memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
399 sizeof(cpi.cpi_sigcatch));
400
401 cpi.cpi_pid = p->p_pid;
402 mutex_enter(proc_lock);
403 cpi.cpi_ppid = p->p_pptr->p_pid;
404 cpi.cpi_pgrp = p->p_pgid;
405 cpi.cpi_sid = p->p_session->s_sid;
406 mutex_exit(proc_lock);
407
408 cpi.cpi_ruid = kauth_cred_getuid(l->l_cred);
409 cpi.cpi_euid = kauth_cred_geteuid(l->l_cred);
410 cpi.cpi_svuid = kauth_cred_getsvuid(l->l_cred);
411
412 cpi.cpi_rgid = kauth_cred_getgid(l->l_cred);
413 cpi.cpi_egid = kauth_cred_getegid(l->l_cred);
414 cpi.cpi_svgid = kauth_cred_getsvgid(l->l_cred);
415
416 cpi.cpi_nlwps = p->p_nlwps;
417 (void)strncpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name));
418 cpi.cpi_name[sizeof(cpi.cpi_name) - 1] = '\0';
419
420 ELFNAMEEND(coredump_savenote)(ns, ELF_NOTE_NETBSD_CORE_PROCINFO,
421 ELF_NOTE_NETBSD_CORE_NAME, &cpi, sizeof(cpi));
422 }
423
424 static int
425 coredump_note_auxv(struct lwp *l, struct note_state *ns)
426 {
427 int error;
428 size_t len;
429 void *kauxv;
430
431 if ((error = proc_getauxv(l->l_proc, &kauxv, &len)) != 0)
432 return error;
433
434 ELFNAMEEND(coredump_savenote)(ns, ELF_NOTE_NETBSD_CORE_AUXV,
435 ELF_NOTE_NETBSD_CORE_NAME, kauxv, len);
436
437 kmem_free(kauxv, len);
438 return 0;
439 }
440
441 static int
442 ELFNAMEEND(coredump_notes)(struct lwp *l, struct note_state *ns)
443 {
444 int error;
445 struct lwp *l0;
446 struct proc *p = l->l_proc;
447
448 coredump_note_procinfo(l, ns);
449 error = coredump_note_auxv(l, ns);
450 if (error)
451 return error;
452
453 /* XXX Add hook for machdep per-proc notes. */
454
455 /*
456 * Now write the register info for the thread that caused the
457 * coredump.
458 */
459 error = ELFNAMEEND(coredump_note)(l, ns);
460 if (error)
461 return error;
462
463 /*
464 * Now, for each LWP, write the register info and any other
465 * per-LWP notes.
466 * Lock in case this is a gcore requested dump.
467 */
468 mutex_enter(p->p_lock);
469 LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
470 if (l0 == l) /* we've taken care of this thread */
471 continue;
472 error = ELFNAMEEND(coredump_note)(l0, ns);
473 if (error)
474 break;
475 }
476 mutex_exit(p->p_lock);
477
478 return error;
479 }
480
481 static int
482 ELFNAMEEND(coredump_note)(struct lwp *l, struct note_state *ns)
483 {
484 int error;
485 char name[64];
486 elf_lwpstatus els;
487 elf_reg intreg;
488 #ifdef PT_GETFPREGS
489 elf_fpreg freg;
490 size_t freglen;
491 #endif
492
493 snprintf(name, sizeof(name), "%s@%d",
494 ELF_NOTE_NETBSD_CORE_NAME, l->l_lid);
495
496 elf_read_lwpstatus(l, &els);
497
498 ELFNAMEEND(coredump_savenote)(ns, PT_LWPSTATUS, name, &els,
499 sizeof(els));
500
501 error = elf_process_read_regs(l, &intreg);
502 if (error)
503 return (error);
504
505 ELFNAMEEND(coredump_savenote)(ns, PT_GETREGS, name, &intreg,
506 sizeof(intreg));
507
508 #ifdef PT_GETFPREGS
509 freglen = sizeof(freg);
510 error = elf_process_read_fpregs(l, &freg, &freglen);
511 if (error)
512 return (error);
513
514 ELFNAMEEND(coredump_savenote)(ns, PT_GETFPREGS, name, &freg, freglen);
515 #endif
516
517 #ifdef COREDUMP_MACHDEP_LWP_NOTES
518 COREDUMP_MACHDEP_LWP_NOTES(l, ns, name);
519 #endif
520
521 return (0);
522 }
523
524 static void
525 save_note_bytes(struct note_state *ns, const void *data, size_t len)
526 {
527 struct note_buf *nb = ns->ns_last;
528 size_t copylen;
529 unsigned char *wp;
530
531 /*
532 * Just copy the data into a buffer list.
533 * All but the last buffer is full.
534 */
535 for (;;) {
536 copylen = uimin(len, sizeof(nb->nb_data) - ns->ns_offset);
537 wp = nb->nb_data + ns->ns_offset;
538 memcpy(wp, data, copylen);
539 if (copylen == len)
540 break;
541 nb->nb_next = kmem_alloc(sizeof(*nb->nb_next), KM_SLEEP);
542 nb = nb->nb_next;
543 ns->ns_last = nb;
544 ns->ns_count++;
545 ns->ns_offset = 0;
546 len -= copylen;
547 data = (const unsigned char *)data + copylen;
548 }
549
550 while ((copylen & (ELFROUNDSIZE - 1)) &&
551 wp + copylen < nb->nb_data + sizeof(nb->nb_data))
552 wp[copylen++] = 0;
553
554 ns->ns_offset += copylen;
555 }
556
557 void
558 ELFNAMEEND(coredump_savenote)(struct note_state *ns, unsigned int type,
559 const char *name, void *data, size_t data_len)
560 {
561 Elf_Nhdr nhdr;
562
563 nhdr.n_namesz = strlen(name) + 1;
564 nhdr.n_descsz = data_len;
565 nhdr.n_type = type;
566
567 save_note_bytes(ns, &nhdr, sizeof (nhdr));
568 save_note_bytes(ns, name, nhdr.n_namesz);
569 save_note_bytes(ns, data, data_len);
570 }
571