Home | History | Annotate | Line # | Download | only in kern
core_elf32.c revision 1.59
      1 /*	$NetBSD: core_elf32.c,v 1.59 2019/11/20 19:37:53 pgoyette 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.59 2019/11/20 19:37:53 pgoyette 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_process_read_regs	CONCAT(process_read_regs, ELFSIZE)
    100 #define elf_process_read_fpregs	CONCAT(process_read_fpregs, ELFSIZE)
    101 #define elf_reg			CONCAT(process_reg, ELFSIZE)
    102 #define elf_fpreg		CONCAT(process_fpreg, ELFSIZE)
    103 
    104 int
    105 ELFNAMEEND(coredump)(struct lwp *l, struct coredump_iostate *cookie)
    106 {
    107 	Elf_Ehdr ehdr;
    108 	Elf_Shdr shdr;
    109 	Elf_Phdr *psections;
    110 	size_t psectionssize;
    111 	int npsections;
    112 	struct writesegs_state ws;
    113 	off_t notestart;
    114 	size_t notesize;
    115 	int error, i;
    116 #ifdef DIAGNOSTIC
    117 	off_t offset;
    118 #endif
    119 
    120 	struct note_state ns;
    121 	struct note_buf *nb;
    122 
    123 	psections = NULL;
    124 
    125 	/* Get all of the notes (mostly all the registers). */
    126 	ns.ns_first = kmem_alloc(sizeof *ns.ns_first, KM_SLEEP);
    127 	ns.ns_last = ns.ns_first;
    128 	ns.ns_count = 0;
    129 	ns.ns_offset = 0;
    130 	error = ELFNAMEEND(coredump_notes)(l, &ns);
    131 	ns.ns_last->nb_next = NULL;
    132 	if (error)
    133 		goto out;
    134 	notesize = ns.ns_count * sizeof nb->nb_data + ns.ns_offset;
    135 
    136 	/*
    137 	 * We have to make a total of 3 passes across the map:
    138 	 *
    139 	 *	1. Count the number of map entries (the number of
    140 	 *	   PT_LOAD sections in the dump).
    141 	 *
    142 	 *	2. Write the P-section headers.
    143 	 *
    144 	 *	3. Write the P-sections.
    145 	 */
    146 
    147 	/* Pass 1: count the entries. */
    148 	MODULE_HOOK_CALL(uvm_coredump_count_segs_hook,
    149 	    (l->l_proc), 0, npsections);
    150 	/* Allow for the PT_NOTE section. */
    151 	npsections++;
    152 
    153 	/* Build the main elf header */
    154 	memset(&ehdr.e_ident[EI_PAD], 0, sizeof(ehdr.e_ident) - EI_PAD);
    155 	memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
    156 #if ELFSIZE == 32
    157 	ehdr.e_ident[EI_CLASS] = ELFCLASS32;
    158 #elif ELFSIZE == 64
    159 	ehdr.e_ident[EI_CLASS] = ELFCLASS64;
    160 #endif
    161 	ehdr.e_ident[EI_DATA] = ELFDEFNNAME(MACHDEP_ENDIANNESS);
    162 	ehdr.e_ident[EI_VERSION] = EV_CURRENT;
    163 	/*
    164 	 * NetBSD sets generic SYSV OSABI and ABI version 0
    165 	 * Native ELF files are distinguishable with NetBSD specific notes
    166 	 */
    167 	ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
    168 	ehdr.e_ident[EI_ABIVERSION] = 0;
    169 
    170 	ehdr.e_type = ET_CORE;
    171 	/* XXX This should be the e_machine of the executable. */
    172 	ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
    173 	ehdr.e_version = EV_CURRENT;
    174 	ehdr.e_entry = 0;
    175 	ehdr.e_flags = 0;
    176 	ehdr.e_ehsize = sizeof(ehdr);
    177 	ehdr.e_phentsize = sizeof(Elf_Phdr);
    178 	if (npsections < PN_XNUM) {
    179 		ehdr.e_phnum = npsections;
    180 		ehdr.e_shentsize = 0;
    181 		ehdr.e_shnum = 0;
    182 		ehdr.e_shoff = 0;
    183 		ehdr.e_phoff = sizeof(ehdr);
    184 	} else {
    185 		ehdr.e_phnum = PN_XNUM;
    186 		ehdr.e_shentsize = sizeof(Elf_Shdr);
    187 		ehdr.e_shnum = 1;
    188 		ehdr.e_shoff = sizeof(ehdr);
    189 		ehdr.e_phoff = sizeof(ehdr) + sizeof(shdr);
    190 	}
    191 	ehdr.e_shstrndx = 0;
    192 
    193 #ifdef ELF_MD_COREDUMP_SETUP
    194 	ELF_MD_COREDUMP_SETUP(l, &ehdr);
    195 #endif
    196 
    197 	/* Write out the ELF header. */
    198 	MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_SYSSPACE, &ehdr,
    199 	    sizeof(ehdr)), ENOSYS, error);
    200 	if (error)
    201 		goto out;
    202 
    203 	/* Write out sections, if needed */
    204 	if (npsections >= PN_XNUM) {
    205 		memset(&shdr, 0, sizeof(shdr));
    206 		shdr.sh_type = SHT_NULL;
    207 		shdr.sh_info = npsections;
    208 		MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_SYSSPACE,
    209 		    &shdr, sizeof(shdr)), ENOSYS, error);
    210 		if (error)
    211 			goto out;
    212 	}
    213 
    214 	psectionssize = npsections * sizeof(*psections);
    215 	notestart = ehdr.e_phoff + psectionssize;
    216 
    217 	psections = kmem_zalloc(psectionssize, KM_SLEEP);
    218 
    219 	/* Pass 2: now find the P-section headers. */
    220 	ws.secoff = notestart + notesize;
    221 	ws.psections = psections;
    222 	ws.npsections = npsections - 1;
    223 	ws.p = l->l_proc;
    224 	MODULE_HOOK_CALL(uvm_coredump_walkmap_hook,
    225 	    (l->l_proc, ELFNAMEEND(coredump_getseghdrs), &ws), ENOSYS, error);
    226 	if (error)
    227 		goto out;
    228 	if (ws.npsections != 0) {
    229 		/* A section went away */
    230 		error = ENOMEM;
    231 		goto out;
    232 	}
    233 
    234 	/* Add the PT_NOTE header after the P-section headers. */
    235 	ws.psections->p_type = PT_NOTE;
    236 	ws.psections->p_offset = notestart;
    237 	ws.psections->p_vaddr = 0;
    238 	ws.psections->p_paddr = 0;
    239 	ws.psections->p_filesz = notesize;
    240 	ws.psections->p_memsz = 0;
    241 	ws.psections->p_flags = PF_R;
    242 	ws.psections->p_align = ELFROUNDSIZE;
    243 
    244 	/* Write the P-section headers followed by the PT_NOTE header */
    245 	MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_SYSSPACE, psections,
    246 	    psectionssize), ENOSYS, error);
    247 	if (error)
    248 		goto out;
    249 
    250 #ifdef DIAGNOSTIC
    251 	MODULE_HOOK_CALL(coredump_offset_hook, (cookie), 0, offset);
    252 	if (offset != notestart)
    253 		panic("coredump: offset %lld != notestart %lld",
    254 		    (long long) offset,
    255 		    (long long) notestart);
    256 #endif
    257 
    258 	/* Write out the notes. */
    259 	for (nb = ns.ns_first; nb != NULL; nb = nb->nb_next) {
    260 		MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_SYSSPACE,
    261 		    nb->nb_data,
    262 		    nb->nb_next == NULL ? ns.ns_offset : sizeof nb->nb_data),
    263 		    ENOSYS, error);
    264 		if (error)
    265 			goto out;
    266 	}
    267 
    268 	/* Finally, write the sections themselves. */
    269 	for (i = 0; i < npsections - 1; i++) {
    270 		if (psections[i].p_filesz == 0)
    271 			continue;
    272 
    273 #ifdef DIAGNOSTIC
    274 		MODULE_HOOK_CALL(coredump_offset_hook, (cookie), 0, offset);
    275 		if (offset != psections[i].p_offset)
    276 			panic("coredump: offset %lld != p_offset[%d] %lld",
    277 			    (long long) offset, i,
    278 			    (long long) psections[i].p_filesz);
    279 #endif
    280 
    281 		MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_USERSPACE,
    282 		    (void *)(vaddr_t)psections[i].p_vaddr,
    283 		    psections[i].p_filesz), ENOSYS, error);
    284 		if (error)
    285 			goto out;
    286 	}
    287 
    288   out:
    289 	if (psections)
    290 		kmem_free(psections, psectionssize);
    291 	while ((nb = ns.ns_first) != NULL) {
    292 		ns.ns_first = nb->nb_next;
    293 		kmem_free(nb, sizeof *nb);
    294 	}
    295 	return (error);
    296 }
    297 
    298 static int
    299 ELFNAMEEND(coredump_getseghdrs)(struct uvm_coredump_state *us)
    300 {
    301 	struct writesegs_state *ws = us->cookie;
    302 	Elf_Phdr phdr;
    303 	vsize_t size, realsize;
    304 	vaddr_t end;
    305 	int error;
    306 
    307 	/* Don't overrun if there are more sections */
    308 	if (ws->npsections == 0)
    309 		return ENOMEM;
    310 	ws->npsections--;
    311 
    312 	size = us->end - us->start;
    313 	realsize = us->realend - us->start;
    314 	end = us->realend;
    315 
    316 	/* Don't bother writing out trailing zeros */
    317 	while (realsize > 0) {
    318 		long buf[1024 / sizeof(long)];
    319 		size_t slen = realsize > sizeof(buf) ? sizeof(buf) : realsize;
    320 		const long *ep;
    321 		int i;
    322 
    323 		end -= slen;
    324 		if ((error = copyin_proc(ws->p, (void *)end, buf, slen)) != 0) {
    325 			/*
    326 			 * In case of any errors of scanning the segments reset
    327 			 * their content to a default value with zeros. This is
    328 			 * achieved with shortening the p_filesz parameter.
    329 			 *
    330 			 * This allows to emit core(5) files for a process
    331 			 * regardless of its state of mappings, such as mapping
    332 			 * pages after EOF in a file.
    333 			 */
    334 			realsize -= slen;
    335 			continue;
    336 		}
    337 
    338 		ep = (const long *) &buf[slen / sizeof(buf[0])];
    339 		for (i = 0, ep--; buf <= ep; ep--, i++) {
    340 			if (*ep)
    341 				break;
    342 		}
    343 		realsize -= i * sizeof(buf[0]);
    344 		if (i * sizeof(buf[0]) < slen)
    345 			break;
    346 	}
    347 
    348 	phdr.p_type = PT_LOAD;
    349 	phdr.p_offset = ws->secoff;
    350 	phdr.p_vaddr = us->start;
    351 	phdr.p_paddr = 0;
    352 	phdr.p_filesz = realsize;
    353 	phdr.p_memsz = size;
    354 	phdr.p_flags = 0;
    355 	if (us->prot & VM_PROT_READ)
    356 		phdr.p_flags |= PF_R;
    357 	if (us->prot & VM_PROT_WRITE)
    358 		phdr.p_flags |= PF_W;
    359 	if (us->prot & VM_PROT_EXECUTE)
    360 		phdr.p_flags |= PF_X;
    361 	phdr.p_align = PAGE_SIZE;
    362 
    363 	ws->secoff += phdr.p_filesz;
    364 	*ws->psections++ = phdr;
    365 
    366 	return (0);
    367 }
    368 
    369 static void
    370 coredump_note_procinfo(struct lwp *l, struct note_state *ns)
    371 {
    372 	struct proc *p;
    373 	struct netbsd_elfcore_procinfo cpi;
    374 	struct lwp *l0;
    375 	sigset_t ss1, ss2;
    376 
    377 	p = l->l_proc;
    378 
    379 	/* First, write an elfcore_procinfo. */
    380 	cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
    381 	cpi.cpi_cpisize = sizeof(cpi);
    382 	cpi.cpi_signo = p->p_sigctx.ps_info._signo;
    383 	cpi.cpi_sigcode = p->p_sigctx.ps_info._code;
    384 	cpi.cpi_siglwp = p->p_sigctx.ps_lwp;
    385 
    386 	/*
    387 	 * XXX This should be per-LWP.
    388 	 */
    389 	ss1 = p->p_sigpend.sp_set;
    390 	sigemptyset(&ss2);
    391 	LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
    392 		sigplusset(&l0->l_sigpend.sp_set, &ss1);
    393 		sigplusset(&l0->l_sigmask, &ss2);
    394 	}
    395 	memcpy(&cpi.cpi_sigpend, &ss1, sizeof(cpi.cpi_sigpend));
    396 	memcpy(&cpi.cpi_sigmask, &ss2, sizeof(cpi.cpi_sigmask));
    397 	memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
    398 	    sizeof(cpi.cpi_sigignore));
    399 	memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
    400 	    sizeof(cpi.cpi_sigcatch));
    401 
    402 	cpi.cpi_pid = p->p_pid;
    403 	mutex_enter(proc_lock);
    404 	cpi.cpi_ppid = p->p_pptr->p_pid;
    405 	cpi.cpi_pgrp = p->p_pgid;
    406 	cpi.cpi_sid = p->p_session->s_sid;
    407 	mutex_exit(proc_lock);
    408 
    409 	cpi.cpi_ruid = kauth_cred_getuid(l->l_cred);
    410 	cpi.cpi_euid = kauth_cred_geteuid(l->l_cred);
    411 	cpi.cpi_svuid = kauth_cred_getsvuid(l->l_cred);
    412 
    413 	cpi.cpi_rgid = kauth_cred_getgid(l->l_cred);
    414 	cpi.cpi_egid = kauth_cred_getegid(l->l_cred);
    415 	cpi.cpi_svgid = kauth_cred_getsvgid(l->l_cred);
    416 
    417 	cpi.cpi_nlwps = p->p_nlwps;
    418 	(void)strncpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name));
    419 	cpi.cpi_name[sizeof(cpi.cpi_name) - 1] = '\0';
    420 
    421 	ELFNAMEEND(coredump_savenote)(ns, ELF_NOTE_NETBSD_CORE_PROCINFO,
    422 	    ELF_NOTE_NETBSD_CORE_NAME, &cpi, sizeof(cpi));
    423 }
    424 
    425 static int
    426 coredump_note_auxv(struct lwp *l, struct note_state *ns)
    427 {
    428 	int error;
    429 	size_t len;
    430 	void *kauxv;
    431 
    432 	if ((error = proc_getauxv(l->l_proc, &kauxv, &len)) != 0)
    433 		return error;
    434 
    435 	ELFNAMEEND(coredump_savenote)(ns, ELF_NOTE_NETBSD_CORE_AUXV,
    436 	    ELF_NOTE_NETBSD_CORE_NAME, kauxv, len);
    437 
    438 	kmem_free(kauxv, len);
    439 	return 0;
    440 }
    441 
    442 static int
    443 ELFNAMEEND(coredump_notes)(struct lwp *l, struct note_state *ns)
    444 {
    445 	int error;
    446 	struct lwp *l0;
    447 	struct proc *p = l->l_proc;
    448 
    449 	coredump_note_procinfo(l, ns);
    450 	error = coredump_note_auxv(l, ns);
    451 	if (error)
    452 		return error;
    453 
    454 	/* XXX Add hook for machdep per-proc notes. */
    455 
    456 	/*
    457 	 * Now write the register info for the thread that caused the
    458 	 * coredump.
    459 	 */
    460 	error = ELFNAMEEND(coredump_note)(l, ns);
    461 	if (error)
    462 		return error;
    463 
    464 	/*
    465 	 * Now, for each LWP, write the register info and any other
    466 	 * per-LWP notes.
    467 	 * Lock in case this is a gcore requested dump.
    468 	 */
    469 	mutex_enter(p->p_lock);
    470 	LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
    471 		if (l0 == l)		/* we've taken care of this thread */
    472 			continue;
    473 		error = ELFNAMEEND(coredump_note)(l0, ns);
    474 		if (error)
    475 			break;
    476 	}
    477 	mutex_exit(p->p_lock);
    478 
    479 	return error;
    480 }
    481 
    482 static int
    483 ELFNAMEEND(coredump_note)(struct lwp *l, struct note_state *ns)
    484 {
    485 	int error;
    486 	char name[64];
    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 	error = elf_process_read_regs(l, &intreg);
    497 	if (error)
    498 		return (error);
    499 
    500 	ELFNAMEEND(coredump_savenote)(ns, PT_GETREGS, name, &intreg,
    501 	    sizeof(intreg));
    502 
    503 #ifdef PT_GETFPREGS
    504 	freglen = sizeof(freg);
    505 	error = elf_process_read_fpregs(l, &freg, &freglen);
    506 	if (error)
    507 		return (error);
    508 
    509 	ELFNAMEEND(coredump_savenote)(ns, PT_GETFPREGS, name, &freg, freglen);
    510 #endif
    511 	/* XXX Add hook for machdep per-LWP notes. */
    512 	return (0);
    513 }
    514 
    515 static void
    516 save_note_bytes(struct note_state *ns, const void *data, size_t len)
    517 {
    518 	struct note_buf *nb = ns->ns_last;
    519 	size_t copylen;
    520 	unsigned char *wp;
    521 
    522 	/*
    523 	 * Just copy the data into a buffer list.
    524 	 * All but the last buffer is full.
    525 	 */
    526 	for (;;) {
    527 		copylen = uimin(len, sizeof(nb->nb_data) - ns->ns_offset);
    528 		wp = nb->nb_data + ns->ns_offset;
    529 		memcpy(wp, data, copylen);
    530 		if (copylen == len)
    531 			break;
    532 		nb->nb_next = kmem_alloc(sizeof(*nb->nb_next), KM_SLEEP);
    533 		nb = nb->nb_next;
    534 		ns->ns_last = nb;
    535 		ns->ns_count++;
    536 		ns->ns_offset = 0;
    537 		len -= copylen;
    538 		data = (const unsigned char *)data + copylen;
    539 	}
    540 
    541 	while ((copylen & (ELFROUNDSIZE - 1)) &&
    542 	    wp + copylen < nb->nb_data + sizeof(nb->nb_data))
    543 		wp[copylen++] = 0;
    544 
    545 	ns->ns_offset += copylen;
    546 }
    547 
    548 void
    549 ELFNAMEEND(coredump_savenote)(struct note_state *ns, unsigned int type,
    550     const char *name, void *data, size_t data_len)
    551 {
    552 	Elf_Nhdr nhdr;
    553 
    554 	nhdr.n_namesz = strlen(name) + 1;
    555 	nhdr.n_descsz = data_len;
    556 	nhdr.n_type = type;
    557 
    558 	save_note_bytes(ns, &nhdr, sizeof (nhdr));
    559 	save_note_bytes(ns, name, nhdr.n_namesz);
    560 	save_note_bytes(ns, data, data_len);
    561 }
    562