Home | History | Annotate | Line # | Download | only in kern
kern_core.c revision 1.33.2.1
      1  1.33.2.1   thorpej /*	$NetBSD: kern_core.c,v 1.33.2.1 2020/12/14 14:38:13 thorpej Exp $	*/
      2       1.2        ad 
      3       1.2        ad /*
      4       1.2        ad  * Copyright (c) 1982, 1986, 1989, 1991, 1993
      5       1.2        ad  *	The Regents of the University of California.  All rights reserved.
      6       1.2        ad  * (c) UNIX System Laboratories, Inc.
      7       1.2        ad  * All or some portions of this file are derived from material licensed
      8       1.2        ad  * to the University of California by American Telephone and Telegraph
      9       1.2        ad  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10       1.2        ad  * the permission of UNIX System Laboratories, Inc.
     11       1.2        ad  *
     12       1.2        ad  * Redistribution and use in source and binary forms, with or without
     13       1.2        ad  * modification, are permitted provided that the following conditions
     14       1.2        ad  * are met:
     15       1.2        ad  * 1. Redistributions of source code must retain the above copyright
     16       1.2        ad  *    notice, this list of conditions and the following disclaimer.
     17       1.2        ad  * 2. Redistributions in binary form must reproduce the above copyright
     18       1.2        ad  *    notice, this list of conditions and the following disclaimer in the
     19       1.2        ad  *    documentation and/or other materials provided with the distribution.
     20       1.2        ad  * 3. Neither the name of the University nor the names of its contributors
     21       1.2        ad  *    may be used to endorse or promote products derived from this software
     22       1.2        ad  *    without specific prior written permission.
     23       1.2        ad  *
     24       1.2        ad  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25       1.2        ad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26       1.2        ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27       1.2        ad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28       1.2        ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29       1.2        ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30       1.2        ad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31       1.2        ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32       1.2        ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33       1.2        ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34       1.2        ad  * SUCH DAMAGE.
     35       1.2        ad  *
     36       1.2        ad  *	@(#)kern_sig.c	8.14 (Berkeley) 5/14/95
     37       1.2        ad  */
     38       1.2        ad 
     39       1.2        ad #include <sys/cdefs.h>
     40  1.33.2.1   thorpej __KERNEL_RCSID(0, "$NetBSD: kern_core.c,v 1.33.2.1 2020/12/14 14:38:13 thorpej Exp $");
     41      1.31  christos 
     42      1.31  christos #ifdef _KERNEL_OPT
     43      1.33  christos #include "opt_execfmt.h"
     44      1.31  christos #include "opt_compat_netbsd32.h"
     45      1.31  christos #endif
     46       1.2        ad 
     47       1.2        ad #include <sys/param.h>
     48       1.2        ad #include <sys/vnode.h>
     49       1.2        ad #include <sys/namei.h>
     50       1.2        ad #include <sys/acct.h>
     51       1.2        ad #include <sys/file.h>
     52       1.2        ad #include <sys/stat.h>
     53       1.2        ad #include <sys/proc.h>
     54       1.2        ad #include <sys/exec.h>
     55       1.2        ad #include <sys/filedesc.h>
     56       1.2        ad #include <sys/kauth.h>
     57      1.13        ad #include <sys/module.h>
     58      1.27  pgoyette #include <sys/compat_stub.h>
     59      1.31  christos #include <sys/exec_elf.h>
     60      1.31  christos 
     61  1.33.2.1   thorpej MODULE(MODULE_CLASS_MISC, coredump, NULL);
     62       1.2        ad 
     63       1.2        ad struct coredump_iostate {
     64       1.2        ad 	struct lwp *io_lwp;
     65       1.2        ad 	struct vnode *io_vp;
     66       1.2        ad 	kauth_cred_t io_cred;
     67       1.2        ad 	off_t io_offset;
     68       1.2        ad };
     69       1.2        ad 
     70      1.13        ad static int	coredump(struct lwp *, const char *);
     71       1.2        ad static int	coredump_buildname(struct proc *, char *, const char *, size_t);
     72      1.28  pgoyette static int	coredump_write(struct coredump_iostate *, enum uio_seg segflg,
     73      1.28  pgoyette 		    const void *, size_t);
     74      1.28  pgoyette static off_t	coredump_offset(struct coredump_iostate *);
     75       1.2        ad 
     76      1.13        ad static int
     77      1.13        ad coredump_modcmd(modcmd_t cmd, void *arg)
     78      1.13        ad {
     79      1.13        ad 
     80      1.13        ad 	switch (cmd) {
     81      1.13        ad 	case MODULE_CMD_INIT:
     82      1.29  pgoyette 		MODULE_HOOK_SET(coredump_hook, coredump);
     83      1.29  pgoyette 		MODULE_HOOK_SET(coredump_write_hook, coredump_write);
     84      1.29  pgoyette 		MODULE_HOOK_SET(coredump_offset_hook, coredump_offset);
     85      1.29  pgoyette 		MODULE_HOOK_SET(coredump_netbsd_hook, real_coredump_netbsd);
     86  1.33.2.1   thorpej #if defined(EXEC_ELF64)
     87      1.31  christos 		MODULE_HOOK_SET(coredump_elf64_hook, real_coredump_elf64);
     88  1.33.2.1   thorpej #elif defined(EXEC_ELF32)
     89  1.33.2.1   thorpej 		MODULE_HOOK_SET(coredump_elf32_hook, real_coredump_elf32);
     90      1.31  christos #endif
     91      1.29  pgoyette 		MODULE_HOOK_SET(uvm_coredump_walkmap_hook,
     92      1.28  pgoyette 		    uvm_coredump_walkmap);
     93      1.29  pgoyette 		MODULE_HOOK_SET(uvm_coredump_count_segs_hook,
     94      1.28  pgoyette 		    uvm_coredump_count_segs);
     95      1.13        ad 		return 0;
     96      1.13        ad 	case MODULE_CMD_FINI:
     97      1.28  pgoyette 		MODULE_HOOK_UNSET(uvm_coredump_count_segs_hook);
     98      1.28  pgoyette 		MODULE_HOOK_UNSET(uvm_coredump_walkmap_hook);
     99  1.33.2.1   thorpej #if defined(EXEC_ELF64)
    100      1.31  christos 		MODULE_HOOK_UNSET(coredump_elf64_hook);
    101  1.33.2.1   thorpej #elif defined(EXEC_ELF32)
    102      1.31  christos 		MODULE_HOOK_UNSET(coredump_elf32_hook);
    103      1.32  christos #endif
    104      1.28  pgoyette 		MODULE_HOOK_UNSET(coredump_netbsd_hook);
    105      1.28  pgoyette 		MODULE_HOOK_UNSET(coredump_offset_hook);
    106      1.28  pgoyette 		MODULE_HOOK_UNSET(coredump_write_hook);
    107      1.27  pgoyette 		MODULE_HOOK_UNSET(coredump_hook);
    108      1.13        ad 		return 0;
    109      1.13        ad 	default:
    110      1.13        ad 		return ENOTTY;
    111      1.13        ad 	}
    112      1.13        ad }
    113      1.13        ad 
    114       1.2        ad /*
    115       1.2        ad  * Dump core, into a file named "progname.core" or "core" (depending on the
    116       1.2        ad  * value of shortcorename), unless the process was setuid/setgid.
    117       1.2        ad  */
    118      1.13        ad static int
    119       1.2        ad coredump(struct lwp *l, const char *pattern)
    120       1.2        ad {
    121       1.2        ad 	struct vnode		*vp;
    122       1.2        ad 	struct proc		*p;
    123       1.2        ad 	struct vmspace		*vm;
    124       1.2        ad 	kauth_cred_t		cred;
    125      1.17  dholland 	struct pathbuf		*pb;
    126       1.2        ad 	struct nameidata	nd;
    127       1.2        ad 	struct vattr		vattr;
    128       1.2        ad 	struct coredump_iostate	io;
    129       1.6       dsl 	struct plimit		*lim;
    130       1.2        ad 	int			error, error1;
    131      1.19  christos 	char			*name, *lastslash;
    132       1.6       dsl 
    133       1.6       dsl 	name = PNBUF_GET();
    134       1.2        ad 
    135       1.2        ad 	p = l->l_proc;
    136       1.2        ad 	vm = p->p_vmspace;
    137       1.2        ad 
    138      1.30        ad 	mutex_enter(&proc_lock);		/* p_session */
    139      1.12        ad 	mutex_enter(p->p_lock);
    140       1.2        ad 
    141       1.2        ad 	/*
    142       1.2        ad 	 * Refuse to core if the data + stack + user size is larger than
    143       1.2        ad 	 * the core dump limit.  XXX THIS IS WRONG, because of mapped
    144       1.2        ad 	 * data.
    145       1.2        ad 	 */
    146       1.2        ad 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
    147       1.2        ad 	    p->p_rlimit[RLIMIT_CORE].rlim_cur) {
    148       1.6       dsl 		error = EFBIG;		/* better error code? */
    149      1.12        ad 		mutex_exit(p->p_lock);
    150      1.30        ad 		mutex_exit(&proc_lock);
    151       1.6       dsl 		goto done;
    152       1.2        ad 	}
    153       1.2        ad 
    154       1.2        ad 	/*
    155       1.2        ad 	 * It may well not be curproc, so grab a reference to its current
    156       1.2        ad 	 * credentials.
    157       1.2        ad 	 */
    158       1.2        ad 	kauth_cred_hold(p->p_cred);
    159       1.2        ad 	cred = p->p_cred;
    160       1.2        ad 
    161       1.2        ad 	/*
    162       1.6       dsl 	 * Make sure the process has not set-id, to prevent data leaks,
    163       1.6       dsl 	 * unless it was specifically requested to allow set-id coredumps.
    164       1.6       dsl 	 */
    165       1.6       dsl 	if (p->p_flag & PK_SUGID) {
    166       1.6       dsl 		if (!security_setidcore_dump) {
    167       1.6       dsl 			error = EPERM;
    168      1.12        ad 			mutex_exit(p->p_lock);
    169      1.30        ad 			mutex_exit(&proc_lock);
    170       1.6       dsl 			goto done;
    171       1.6       dsl 		}
    172       1.2        ad 		pattern = security_setidcore_path;
    173       1.6       dsl 	}
    174       1.2        ad 
    175      1.18     rmind 	/* Lock, as p_limit and pl_corename might change. */
    176       1.6       dsl 	lim = p->p_limit;
    177       1.6       dsl 	mutex_enter(&lim->pl_lock);
    178      1.18     rmind 	if (pattern == NULL) {
    179       1.6       dsl 		pattern = lim->pl_corename;
    180      1.18     rmind 	}
    181       1.2        ad 	error = coredump_buildname(p, name, pattern, MAXPATHLEN);
    182       1.6       dsl 	mutex_exit(&lim->pl_lock);
    183      1.18     rmind 
    184      1.23      maxv 	if (error) {
    185      1.23      maxv 		mutex_exit(p->p_lock);
    186      1.30        ad 		mutex_exit(&proc_lock);
    187      1.23      maxv 		goto done;
    188      1.23      maxv 	}
    189      1.23      maxv 
    190      1.19  christos 	/*
    191      1.19  christos 	 * On a simple filename, see if the filesystem allow us to write
    192      1.19  christos 	 * core dumps there.
    193      1.19  christos 	 */
    194      1.19  christos 	lastslash = strrchr(name, '/');
    195      1.19  christos 	if (!lastslash) {
    196      1.19  christos 		vp = p->p_cwdi->cwdi_cdir;
    197      1.19  christos 		if (vp->v_mount == NULL ||
    198      1.19  christos 		    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
    199      1.19  christos 			error = EPERM;
    200      1.19  christos 	}
    201      1.19  christos 
    202      1.12        ad 	mutex_exit(p->p_lock);
    203      1.30        ad 	mutex_exit(&proc_lock);
    204      1.19  christos 	if (error)
    205       1.2        ad 		goto done;
    206      1.19  christos 
    207      1.19  christos 	/*
    208      1.19  christos 	 * On a complex filename, see if the filesystem allow us to write
    209      1.19  christos 	 * core dumps there.
    210      1.19  christos 	 *
    211      1.19  christos 	 * XXX: We should have an API that avoids double lookups
    212      1.19  christos 	 */
    213      1.19  christos 	if (lastslash) {
    214      1.19  christos 		char c[2];
    215      1.19  christos 
    216      1.19  christos 		if (lastslash - name >= MAXPATHLEN - 2) {
    217      1.19  christos 			error = EPERM;
    218      1.19  christos 			goto done;
    219      1.19  christos 		}
    220      1.19  christos 
    221      1.19  christos 		c[0] = lastslash[1];
    222      1.19  christos 		c[1] = lastslash[2];
    223      1.19  christos 		lastslash[1] = '.';
    224      1.19  christos 		lastslash[2] = '\0';
    225      1.19  christos 		error = namei_simple_kernel(name, NSM_FOLLOW_NOEMULROOT, &vp);
    226      1.19  christos 		if (error)
    227      1.19  christos 			goto done;
    228      1.19  christos 		if (vp->v_mount == NULL ||
    229      1.19  christos 		    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
    230      1.19  christos 			error = EPERM;
    231      1.19  christos 		vrele(vp);
    232      1.19  christos 		if (error)
    233      1.19  christos 			goto done;
    234      1.19  christos 		lastslash[1] = c[0];
    235      1.19  christos 		lastslash[2] = c[1];
    236      1.18     rmind 	}
    237      1.17  dholland 
    238      1.17  dholland 	pb = pathbuf_create(name);
    239      1.17  dholland 	if (pb == NULL) {
    240      1.17  dholland 		error = ENOMEM;
    241      1.17  dholland 		goto done;
    242      1.17  dholland 	}
    243      1.17  dholland 	NDINIT(&nd, LOOKUP, NOFOLLOW, pb);
    244       1.2        ad 	if ((error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE,
    245      1.17  dholland 	    S_IRUSR | S_IWUSR)) != 0) {
    246      1.17  dholland 		pathbuf_destroy(pb);
    247       1.2        ad 		goto done;
    248      1.17  dholland 	}
    249       1.2        ad 	vp = nd.ni_vp;
    250      1.17  dholland 	pathbuf_destroy(pb);
    251       1.2        ad 
    252      1.20  christos 	/*
    253      1.20  christos 	 * Don't dump to:
    254      1.20  christos 	 * 	- non-regular files
    255      1.20  christos 	 * 	- files with links
    256      1.20  christos 	 * 	- files we don't own
    257      1.20  christos 	 */
    258       1.2        ad 	if (vp->v_type != VREG ||
    259      1.20  christos 	    VOP_GETATTR(vp, &vattr, cred) || vattr.va_nlink != 1 ||
    260      1.20  christos 	    vattr.va_uid != kauth_cred_geteuid(cred)) {
    261      1.20  christos 		error = EACCES;
    262       1.2        ad 		goto out;
    263       1.2        ad 	}
    264      1.15     pooka 	vattr_null(&vattr);
    265       1.2        ad 	vattr.va_size = 0;
    266       1.2        ad 
    267       1.3     pavel 	if ((p->p_flag & PK_SUGID) && security_setidcore_dump) {
    268       1.2        ad 		vattr.va_uid = security_setidcore_owner;
    269       1.2        ad 		vattr.va_gid = security_setidcore_group;
    270       1.2        ad 		vattr.va_mode = security_setidcore_mode;
    271       1.2        ad 	}
    272       1.2        ad 
    273       1.7     pooka 	VOP_SETATTR(vp, &vattr, cred);
    274       1.2        ad 	p->p_acflag |= ACORE;
    275       1.2        ad 
    276       1.2        ad 	io.io_lwp = l;
    277       1.2        ad 	io.io_vp = vp;
    278       1.2        ad 	io.io_cred = cred;
    279       1.2        ad 	io.io_offset = 0;
    280       1.2        ad 
    281       1.2        ad 	/* Now dump the actual core file. */
    282       1.2        ad 	error = (*p->p_execsw->es_coredump)(l, &io);
    283       1.2        ad  out:
    284      1.16   hannken 	VOP_UNLOCK(vp);
    285      1.10        ad 	error1 = vn_close(vp, FWRITE, cred);
    286       1.2        ad 	if (error == 0)
    287       1.2        ad 		error = error1;
    288       1.2        ad done:
    289       1.2        ad 	if (name != NULL)
    290       1.2        ad 		PNBUF_PUT(name);
    291       1.2        ad 	return error;
    292       1.2        ad }
    293       1.2        ad 
    294       1.2        ad static int
    295       1.2        ad coredump_buildname(struct proc *p, char *dst, const char *src, size_t len)
    296       1.2        ad {
    297       1.2        ad 	const char	*s;
    298       1.2        ad 	char		*d, *end;
    299       1.2        ad 	int		i;
    300       1.2        ad 
    301      1.30        ad 	KASSERT(mutex_owned(&proc_lock));
    302       1.2        ad 
    303       1.2        ad 	for (s = src, d = dst, end = d + len; *s != '\0'; s++) {
    304       1.2        ad 		if (*s == '%') {
    305       1.2        ad 			switch (*(s + 1)) {
    306       1.2        ad 			case 'n':
    307       1.2        ad 				i = snprintf(d, end - d, "%s", p->p_comm);
    308       1.2        ad 				break;
    309       1.2        ad 			case 'p':
    310       1.2        ad 				i = snprintf(d, end - d, "%d", p->p_pid);
    311       1.2        ad 				break;
    312       1.2        ad 			case 'u':
    313       1.2        ad 				i = snprintf(d, end - d, "%.*s",
    314       1.2        ad 				    (int)sizeof p->p_pgrp->pg_session->s_login,
    315       1.2        ad 				    p->p_pgrp->pg_session->s_login);
    316       1.2        ad 				break;
    317       1.2        ad 			case 't':
    318      1.14  christos 				i = snprintf(d, end - d, "%lld",
    319      1.14  christos 				    (long long)p->p_stats->p_start.tv_sec);
    320       1.2        ad 				break;
    321       1.2        ad 			default:
    322       1.2        ad 				goto copy;
    323       1.2        ad 			}
    324       1.2        ad 			d += i;
    325       1.2        ad 			s++;
    326       1.2        ad 		} else {
    327       1.2        ad  copy:			*d = *s;
    328       1.2        ad 			d++;
    329       1.2        ad 		}
    330       1.2        ad 		if (d >= end)
    331       1.2        ad 			return (ENAMETOOLONG);
    332       1.2        ad 	}
    333       1.2        ad 	*d = '\0';
    334       1.2        ad 	return 0;
    335       1.2        ad }
    336       1.2        ad 
    337      1.28  pgoyette static int
    338      1.22       dsl coredump_write(struct coredump_iostate *io, enum uio_seg segflg,
    339      1.21       dsl     const void *data, size_t len)
    340       1.2        ad {
    341       1.2        ad 	int error;
    342       1.2        ad 
    343       1.2        ad 	error = vn_rdwr(UIO_WRITE, io->io_vp, __UNCONST(data), len,
    344       1.2        ad 	    io->io_offset, segflg,
    345       1.2        ad 	    IO_NODELOCKED|IO_UNIT, io->io_cred, NULL,
    346       1.2        ad 	    segflg == UIO_USERSPACE ? io->io_lwp : NULL);
    347       1.2        ad 	if (error) {
    348       1.2        ad 		printf("pid %d (%s): %s write of %zu@%p at %lld failed: %d\n",
    349       1.2        ad 		    io->io_lwp->l_proc->p_pid, io->io_lwp->l_proc->p_comm,
    350       1.2        ad 		    segflg == UIO_USERSPACE ? "user" : "system",
    351       1.2        ad 		    len, data, (long long) io->io_offset, error);
    352       1.2        ad 		return (error);
    353       1.2        ad 	}
    354       1.2        ad 
    355       1.2        ad 	io->io_offset += len;
    356       1.2        ad 	return (0);
    357       1.2        ad }
    358      1.22       dsl 
    359      1.28  pgoyette static off_t
    360      1.22       dsl coredump_offset(struct coredump_iostate *io)
    361      1.22       dsl {
    362      1.24   msaitoh 	return io->io_offset;
    363      1.22       dsl }
    364