Home | History | Annotate | Line # | Download | only in kern
core_elf32.c revision 1.32.16.3
      1 /*	$NetBSD: core_elf32.c,v 1.32.16.3 2014/04/02 17:34:51 matt 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.32.16.3 2014/04/02 17:34:51 matt Exp $");
     44 
     45 #ifdef _KERNEL_OPT
     46 #include "opt_coredump.h"
     47 #endif
     48 
     49 /* If not included by core_elf64.c, ELFSIZE won't be defined. */
     50 #ifndef ELFSIZE
     51 #define	ELFSIZE		32
     52 #endif
     53 
     54 #include <sys/param.h>
     55 #include <sys/systm.h>
     56 #include <sys/proc.h>
     57 #include <sys/vnode.h>
     58 #include <sys/exec.h>
     59 #include <sys/exec_elf.h>
     60 #include <sys/ptrace.h>
     61 #include <sys/malloc.h>
     62 #include <sys/kauth.h>
     63 
     64 #include <machine/reg.h>
     65 
     66 #include <uvm/uvm_extern.h>
     67 
     68 struct countsegs_state {
     69 	int	npsections;
     70 };
     71 
     72 static int	ELFNAMEEND(coredump_countsegs)(struct proc *, void *,
     73 		    struct uvm_coredump_state *);
     74 
     75 struct writesegs_state {
     76 	Elf_Phdr *psections;
     77 	off_t	secoff;
     78 };
     79 
     80 static int	ELFNAMEEND(coredump_writeseghdrs)(struct proc *, void *,
     81 		    struct uvm_coredump_state *);
     82 
     83 static int	ELFNAMEEND(coredump_notes)(struct proc *, struct lwp *, void *,
     84 		    size_t *);
     85 static int	ELFNAMEEND(coredump_note)(struct proc *, struct lwp *, void *,
     86 		    size_t *);
     87 
     88 #define	ELFROUNDSIZE	4	/* XXX Should it be sizeof(Elf_Word)? */
     89 #define	elfround(x)	roundup((x), ELFROUNDSIZE)
     90 
     91 #define elf_process_read_regs	CONCAT(process_read_regs, ELFSIZE)
     92 #ifdef __HAVE_PROCESS_XFPREGS
     93 #define elf_process_read_xfpregs CONCAT(process_read_xfpregs, ELFSIZE)
     94 #else
     95 #define elf_process_read_fpregs	CONCAT(process_read_fpregs, ELFSIZE)
     96 #endif
     97 #define elf_reg			CONCAT(process_reg, ELFSIZE)
     98 #define elf_fpreg		CONCAT(process_fpreg, ELFSIZE)
     99 
    100 int
    101 ELFNAMEEND(coredump)(struct lwp *l, void *cookie)
    102 {
    103 	struct proc *p;
    104 	Elf_Ehdr ehdr;
    105 	Elf_Shdr shdr;
    106 	Elf_Phdr *psections;
    107 	struct countsegs_state cs;
    108 	struct writesegs_state ws;
    109 	off_t notestart, secstart, offset;
    110 	size_t notesize;
    111 	int error, i;
    112 
    113 	psections = NULL;
    114 	p = l->l_proc;
    115 	/*
    116 	 * We have to make a total of 3 passes across the map:
    117 	 *
    118 	 *	1. Count the number of map entries (the number of
    119 	 *	   PT_LOAD sections).
    120 	 *
    121 	 *	2. Write the P-section headers.
    122 	 *
    123 	 *	3. Write the P-sections.
    124 	 */
    125 
    126 	/* Pass 1: count the entries. */
    127 	cs.npsections = 0;
    128 	error = uvm_coredump_walkmap(p, NULL,
    129 	    ELFNAMEEND(coredump_countsegs), &cs);
    130 	if (error)
    131 		goto out;
    132 
    133 	/* Count the PT_NOTE section. */
    134 	cs.npsections++;
    135 
    136 	/* Get the size of the notes. */
    137 	error = ELFNAMEEND(coredump_notes)(p, l, NULL, &notesize);
    138 	if (error)
    139 		goto out;
    140 
    141 	memset(&ehdr.e_ident[EI_PAD], 0, sizeof(ehdr.e_ident) - EI_PAD);
    142 	memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
    143 #if ELFSIZE == 32
    144 	ehdr.e_ident[EI_CLASS] = ELFCLASS32;
    145 #elif ELFSIZE == 64
    146 	ehdr.e_ident[EI_CLASS] = ELFCLASS64;
    147 #endif
    148 	ehdr.e_ident[EI_DATA] = ELFDEFNNAME(MACHDEP_ENDIANNESS);
    149 	ehdr.e_ident[EI_VERSION] = EV_CURRENT;
    150 	/* XXX Should be the OSABI/ABI version of the executable. */
    151 	ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
    152 	ehdr.e_ident[EI_ABIVERSION] = 0;
    153 
    154 	ehdr.e_type = ET_CORE;
    155 	/* XXX This should be the e_machine of the executable. */
    156 	ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
    157 	ehdr.e_version = EV_CURRENT;
    158 	ehdr.e_entry = 0;
    159 	ehdr.e_flags = 0;
    160 	ehdr.e_ehsize = sizeof(ehdr);
    161 	ehdr.e_phentsize = sizeof(Elf_Phdr);
    162 	if (cs.npsections < PN_XNUM) {
    163 		ehdr.e_phnum = cs.npsections;
    164 		ehdr.e_shentsize = 0;
    165 		ehdr.e_shnum = 0;
    166 		ehdr.e_shoff = 0;
    167 		ehdr.e_phoff = sizeof(ehdr);
    168 	} else {
    169 		ehdr.e_phnum = PN_XNUM;
    170 		ehdr.e_shentsize = sizeof(Elf_Shdr);
    171 		ehdr.e_shnum = 1;
    172 		ehdr.e_shoff = sizeof(ehdr);
    173 		ehdr.e_phoff = sizeof(ehdr) + sizeof(shdr);
    174 	}
    175 	ehdr.e_shstrndx = 0;
    176 
    177 #ifdef ELF_MD_COREDUMP_SETUP
    178 	ELF_MD_COREDUMP_SETUP(l, &ehdr);
    179 #endif
    180 
    181 	/* Write out the ELF header. */
    182 	error = coredump_write(cookie, UIO_SYSSPACE, &ehdr, sizeof(ehdr));
    183 	if (error)
    184 		goto out;
    185 
    186 	/* Write out sections, if needed */
    187 	if (cs.npsections >= PN_XNUM) {
    188 		memset(&shdr, 0, sizeof(shdr));
    189 		shdr.sh_type = SHT_NULL;
    190 		shdr.sh_info = cs.npsections;
    191 		error = coredump_write(cookie, UIO_SYSSPACE, &shdr,
    192 		    sizeof(shdr));
    193 		if (error)
    194 			goto out;
    195 	}
    196 
    197 	offset = ehdr.e_phoff;
    198 
    199 	notestart = offset + sizeof(Elf_Phdr) * cs.npsections;
    200 	secstart = notestart + notesize;
    201 
    202 	psections = malloc(cs.npsections * sizeof(Elf_Phdr),
    203 	    M_TEMP, M_WAITOK|M_ZERO);
    204 
    205 	/* Pass 2: now write the P-section headers. */
    206 	ws.secoff = secstart;
    207 	ws.psections = psections;
    208 	error = uvm_coredump_walkmap(p, cookie,
    209 	    ELFNAMEEND(coredump_writeseghdrs), &ws);
    210 	if (error)
    211 		goto out;
    212 
    213 	/* Write out the PT_NOTE header. */
    214 	ws.psections->p_type = PT_NOTE;
    215 	ws.psections->p_offset = notestart;
    216 	ws.psections->p_vaddr = 0;
    217 	ws.psections->p_paddr = 0;
    218 	ws.psections->p_filesz = notesize;
    219 	ws.psections->p_memsz = 0;
    220 	ws.psections->p_flags = PF_R;
    221 	ws.psections->p_align = ELFROUNDSIZE;
    222 
    223 	error = coredump_write(cookie, UIO_SYSSPACE, psections,
    224 	    cs.npsections * sizeof(Elf_Phdr));
    225 	if (error)
    226 		goto out;
    227 
    228 #ifdef DIAGNOSTIC
    229 	offset += cs.npsections * sizeof(Elf_Phdr);
    230 	if (offset != notestart)
    231 		panic("coredump: offset %lld != notestart %lld",
    232 		    (long long) offset, (long long) notestart);
    233 #endif
    234 
    235 	/* Write out the notes. */
    236 	error = ELFNAMEEND(coredump_notes)(p, l, cookie, &notesize);
    237 	if (error)
    238 		goto out;
    239 
    240 #ifdef DIAGNOSTIC
    241 	offset += notesize;
    242 	if (offset != secstart)
    243 		panic("coredump: offset %lld != secstart %lld",
    244 		    (long long) offset, (long long) secstart);
    245 #endif
    246 
    247 	/* Pass 3: finally, write the sections themselves. */
    248 	for (i = 0; i < cs.npsections - 1; i++) {
    249 		if (psections[i].p_filesz == 0)
    250 			continue;
    251 
    252 #ifdef DIAGNOSTIC
    253 		if (offset != psections[i].p_offset)
    254 			panic("coredump: offset %lld != p_offset[%d] %lld",
    255 			    (long long) offset, i,
    256 			    (long long) psections[i].p_filesz);
    257 #endif
    258 
    259 		error = coredump_write(cookie, UIO_USERSPACE,
    260 		    (void *)(vaddr_t)psections[i].p_vaddr,
    261 		    psections[i].p_filesz);
    262 		if (error)
    263 			goto out;
    264 
    265 #ifdef DIAGNOSTIC
    266 		offset += psections[i].p_filesz;
    267 #endif
    268 	}
    269 
    270   out:
    271 	if (psections)
    272 		free(psections, M_TEMP);
    273 	return (error);
    274 }
    275 
    276 static int
    277 ELFNAMEEND(coredump_countsegs)(struct proc *p, void *iocookie,
    278     struct uvm_coredump_state *us)
    279 {
    280 	struct countsegs_state *cs = us->cookie;
    281 
    282 	cs->npsections++;
    283 	return (0);
    284 }
    285 
    286 static int
    287 ELFNAMEEND(coredump_writeseghdrs)(struct proc *p, void *iocookie,
    288     struct uvm_coredump_state *us)
    289 {
    290 	struct writesegs_state *ws = us->cookie;
    291 	Elf_Phdr phdr;
    292 	vsize_t size, realsize;
    293 	vaddr_t end;
    294 	int error;
    295 
    296 	size = us->end - us->start;
    297 	realsize = us->realend - us->start;
    298 	end = us->realend;
    299 
    300 	while (realsize > 0) {
    301 		long buf[1024 / sizeof(long)];
    302 		size_t slen = realsize > sizeof(buf) ? sizeof(buf) : realsize;
    303 		const long *ep;
    304 		int i;
    305 
    306 		end -= slen;
    307 		if ((error = copyin_proc(p, (void *)end, buf, slen)) != 0)
    308 			return error;
    309 
    310 		ep = (const long *) &buf[slen / sizeof(buf[0])];
    311 		for (i = 0, ep--; buf <= ep; ep--, i++) {
    312 			if (*ep)
    313 				break;
    314 		}
    315 		realsize -= i * sizeof(buf[0]);
    316 		if (i * sizeof(buf[0]) < slen)
    317 			break;
    318 	}
    319 
    320 	phdr.p_type = PT_LOAD;
    321 	phdr.p_offset = ws->secoff;
    322 	phdr.p_vaddr = us->start;
    323 	phdr.p_paddr = 0;
    324 	phdr.p_filesz = realsize;
    325 	phdr.p_memsz = size;
    326 	phdr.p_flags = 0;
    327 	if (us->prot & VM_PROT_READ)
    328 		phdr.p_flags |= PF_R;
    329 	if (us->prot & VM_PROT_WRITE)
    330 		phdr.p_flags |= PF_W;
    331 	if (us->prot & VM_PROT_EXECUTE)
    332 		phdr.p_flags |= PF_X;
    333 	phdr.p_align = PAGE_SIZE;
    334 
    335 	ws->secoff += phdr.p_filesz;
    336 	*ws->psections++ = phdr;
    337 
    338 	return (0);
    339 }
    340 
    341 static int
    342 ELFNAMEEND(coredump_notes)(struct proc *p, struct lwp *l,
    343     void *iocookie, size_t *sizep)
    344 {
    345 	struct netbsd_elfcore_procinfo cpi;
    346 	Elf_Nhdr nhdr;
    347 	size_t size, notesize;
    348 	int error;
    349 	struct lwp *l0;
    350 	sigset_t ss1, ss2;
    351 
    352 	size = 0;
    353 
    354 	/* First, write an elfcore_procinfo. */
    355 	notesize = sizeof(nhdr) + elfround(sizeof(ELF_NOTE_NETBSD_CORE_NAME)) +
    356 	    elfround(sizeof(cpi));
    357 	if (iocookie) {
    358 		cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
    359 		cpi.cpi_cpisize = sizeof(cpi);
    360 		cpi.cpi_signo = p->p_sigctx.ps_signo;
    361 		cpi.cpi_sigcode = p->p_sigctx.ps_code;
    362 		cpi.cpi_siglwp = p->p_sigctx.ps_lwp;
    363 
    364 		/*
    365 		 * XXX This should be per-LWP.
    366 		 */
    367 		ss1 = p->p_sigpend.sp_set;
    368 		sigemptyset(&ss2);
    369 		LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
    370 			sigplusset(&l0->l_sigpend.sp_set, &ss1);
    371 			sigplusset(&l0->l_sigmask, &ss2);
    372 		}
    373 		memcpy(&cpi.cpi_sigpend, &ss1, sizeof(cpi.cpi_sigpend));
    374 		memcpy(&cpi.cpi_sigmask, &ss2, sizeof(cpi.cpi_sigmask));
    375 		memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
    376 		    sizeof(cpi.cpi_sigignore));
    377 		memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
    378 		    sizeof(cpi.cpi_sigcatch));
    379 
    380 		cpi.cpi_pid = p->p_pid;
    381 		mutex_enter(proc_lock);
    382 		cpi.cpi_ppid = p->p_pptr->p_pid;
    383 		cpi.cpi_pgrp = p->p_pgid;
    384 		cpi.cpi_sid = p->p_session->s_sid;
    385 		mutex_exit(proc_lock);
    386 
    387 		cpi.cpi_ruid = kauth_cred_getuid(l->l_cred);
    388 		cpi.cpi_euid = kauth_cred_geteuid(l->l_cred);
    389 		cpi.cpi_svuid = kauth_cred_getsvuid(l->l_cred);
    390 
    391 		cpi.cpi_rgid = kauth_cred_getgid(l->l_cred);
    392 		cpi.cpi_egid = kauth_cred_getegid(l->l_cred);
    393 		cpi.cpi_svgid = kauth_cred_getsvgid(l->l_cred);
    394 
    395 		cpi.cpi_nlwps = p->p_nlwps;
    396 		(void)strncpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name));
    397 		cpi.cpi_name[sizeof(cpi.cpi_name) - 1] = '\0';
    398 
    399 		nhdr.n_namesz = sizeof(ELF_NOTE_NETBSD_CORE_NAME);
    400 		nhdr.n_descsz = sizeof(cpi);
    401 		nhdr.n_type = ELF_NOTE_NETBSD_CORE_PROCINFO;
    402 
    403 		error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
    404 		    ELF_NOTE_NETBSD_CORE_NAME "\0\0\0", &cpi);
    405 		if (error)
    406 			return (error);
    407 	}
    408 
    409 	size += notesize;
    410 
    411 	/* XXX Add hook for machdep per-proc notes. */
    412 
    413 	/*
    414 	 * Now write the register info for the thread that caused the
    415 	 * coredump.
    416 	 */
    417 	error = ELFNAMEEND(coredump_note)(p, l, iocookie, &notesize);
    418 	if (error)
    419 		return (error);
    420 	size += notesize;
    421 
    422 	/*
    423 	 * Now, for each LWP, write the register info and any other
    424 	 * per-LWP notes.  Since we're dumping core, we don't bother
    425 	 * locking.
    426 	 */
    427 	LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
    428 		if (l0 == l)		/* we've taken care of this thread */
    429 			continue;
    430 		error = ELFNAMEEND(coredump_note)(p, l0, iocookie, &notesize);
    431 		if (error)
    432 			return (error);
    433 		size += notesize;
    434 	}
    435 
    436 	*sizep = size;
    437 	return (0);
    438 }
    439 
    440 static int
    441 ELFNAMEEND(coredump_note)(struct proc *p, struct lwp *l, void *iocookie,
    442     size_t *sizep)
    443 {
    444 	Elf_Nhdr nhdr;
    445 	int size, notesize, error;
    446 	int namesize;
    447 	char name[64+ELFROUNDSIZE];
    448 	elf_reg intreg;
    449 #ifdef PT_GETFPREGS
    450 	elf_fpreg freg;
    451 #endif
    452 
    453 	size = 0;
    454 
    455 	snprintf(name, sizeof(name)-ELFROUNDSIZE, "%s@%d",
    456 	    ELF_NOTE_NETBSD_CORE_NAME, l->l_lid);
    457 	namesize = strlen(name) + 1;
    458 	memset(name + namesize, 0, elfround(namesize) - namesize);
    459 
    460 	notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(intreg));
    461 	if (iocookie) {
    462 		uvm_lwp_hold(l);
    463 		error = elf_process_read_regs(l, &intreg);
    464 		uvm_lwp_rele(l);
    465 		if (error)
    466 			return (error);
    467 
    468 		nhdr.n_namesz = namesize;
    469 		nhdr.n_descsz = sizeof(intreg);
    470 		nhdr.n_type = PT_GETREGS;
    471 
    472 		error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
    473 		    name, &intreg);
    474 		if (error)
    475 			return (error);
    476 
    477 	}
    478 	size += notesize;
    479 
    480 #ifdef PT_GETFPREGS
    481 	notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(freg));
    482 	if (iocookie) {
    483 		size_t freglen = sizeof(freg);
    484 		uvm_lwp_hold(l);
    485 #ifdef __HAVE_PROCESS_XFPREGS
    486 		error = elf_process_read_xfpregs(l, &freg, &freglen);
    487 #else
    488 		error = elf_process_read_fpregs(l, &freg);
    489 #endif
    490 		uvm_lwp_rele(l);
    491 		if (error)
    492 			return (error);
    493 
    494 		nhdr.n_namesz = namesize;
    495 		nhdr.n_descsz = freglen;
    496 		nhdr.n_type = PT_GETFPREGS;
    497 
    498 		error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
    499 		    name, &freg);
    500 		if (error)
    501 			return (error);
    502 	}
    503 	size += notesize;
    504 #endif
    505 	*sizep = size;
    506 	/* XXX Add hook for machdep per-LWP notes. */
    507 	return (0);
    508 }
    509 
    510 int
    511 ELFNAMEEND(coredump_writenote)(struct proc *p, void *cookie, Elf_Nhdr *nhdr,
    512     const char *name, void *data)
    513 {
    514 	int error;
    515 
    516 	error = coredump_write(cookie, UIO_SYSSPACE, nhdr, sizeof(*nhdr));
    517 	if (error)
    518 		return error;
    519 
    520 	error = coredump_write(cookie, UIO_SYSSPACE, name,
    521 	    elfround(nhdr->n_namesz));
    522 	if (error)
    523 		return error;
    524 
    525 	return coredump_write(cookie, UIO_SYSSPACE, data, nhdr->n_descsz);
    526 }
    527