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