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