Home | History | Annotate | Line # | Download | only in kern
kern_core.c revision 1.31
      1 /*	$NetBSD: kern_core.c,v 1.31 2020/10/19 19:33:02 christos 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.31 2020/10/19 19:33:02 christos Exp $");
     41 
     42 #ifdef _KERNEL_OPT
     43 #include "opt_compat_netbsd32.h"
     44 #endif
     45 
     46 #include <sys/param.h>
     47 #include <sys/vnode.h>
     48 #include <sys/namei.h>
     49 #include <sys/acct.h>
     50 #include <sys/file.h>
     51 #include <sys/stat.h>
     52 #include <sys/proc.h>
     53 #include <sys/exec.h>
     54 #include <sys/filedesc.h>
     55 #include <sys/kauth.h>
     56 #include <sys/module.h>
     57 #include <sys/compat_stub.h>
     58 #include <sys/exec_elf.h>
     59 
     60 #ifdef COMPAT_NETBSD32
     61 #define COREDUMP_MODULE_DEP	"compat_netbsd32_ptrace"
     62 #else
     63 #define COREDUMP_MODULE_DEP	NULL
     64 #endif
     65 
     66 MODULE(MODULE_CLASS_MISC, coredump, COREDUMP_MODULE_DEP);
     67 
     68 struct coredump_iostate {
     69 	struct lwp *io_lwp;
     70 	struct vnode *io_vp;
     71 	kauth_cred_t io_cred;
     72 	off_t io_offset;
     73 };
     74 
     75 static int	coredump(struct lwp *, const char *);
     76 static int	coredump_buildname(struct proc *, char *, const char *, size_t);
     77 static int	coredump_write(struct coredump_iostate *, enum uio_seg segflg,
     78 		    const void *, size_t);
     79 static off_t	coredump_offset(struct coredump_iostate *);
     80 
     81 static int
     82 coredump_modcmd(modcmd_t cmd, void *arg)
     83 {
     84 
     85 	switch (cmd) {
     86 	case MODULE_CMD_INIT:
     87 		MODULE_HOOK_SET(coredump_hook, coredump);
     88 		MODULE_HOOK_SET(coredump_write_hook, coredump_write);
     89 		MODULE_HOOK_SET(coredump_offset_hook, coredump_offset);
     90 		MODULE_HOOK_SET(coredump_netbsd_hook, real_coredump_netbsd);
     91 		MODULE_HOOK_SET(coredump_elf32_hook, real_coredump_elf32);
     92 #ifdef _LP64
     93 		MODULE_HOOK_SET(coredump_elf64_hook, real_coredump_elf64);
     94 #endif
     95 		MODULE_HOOK_SET(uvm_coredump_walkmap_hook,
     96 		    uvm_coredump_walkmap);
     97 		MODULE_HOOK_SET(uvm_coredump_count_segs_hook,
     98 		    uvm_coredump_count_segs);
     99 		return 0;
    100 	case MODULE_CMD_FINI:
    101 		MODULE_HOOK_UNSET(uvm_coredump_count_segs_hook);
    102 		MODULE_HOOK_UNSET(uvm_coredump_walkmap_hook);
    103 #ifdef _LP64
    104 		MODULE_HOOK_UNSET(coredump_elf64_hook);
    105 #endif
    106 		MODULE_HOOK_UNSET(coredump_elf32_hook);
    107 		MODULE_HOOK_UNSET(coredump_netbsd_hook);
    108 		MODULE_HOOK_UNSET(coredump_offset_hook);
    109 		MODULE_HOOK_UNSET(coredump_write_hook);
    110 		MODULE_HOOK_UNSET(coredump_hook);
    111 		return 0;
    112 	default:
    113 		return ENOTTY;
    114 	}
    115 }
    116 
    117 /*
    118  * Dump core, into a file named "progname.core" or "core" (depending on the
    119  * value of shortcorename), unless the process was setuid/setgid.
    120  */
    121 static int
    122 coredump(struct lwp *l, const char *pattern)
    123 {
    124 	struct vnode		*vp;
    125 	struct proc		*p;
    126 	struct vmspace		*vm;
    127 	kauth_cred_t		cred;
    128 	struct pathbuf		*pb;
    129 	struct nameidata	nd;
    130 	struct vattr		vattr;
    131 	struct coredump_iostate	io;
    132 	struct plimit		*lim;
    133 	int			error, error1;
    134 	char			*name, *lastslash;
    135 
    136 	name = PNBUF_GET();
    137 
    138 	p = l->l_proc;
    139 	vm = p->p_vmspace;
    140 
    141 	mutex_enter(&proc_lock);		/* p_session */
    142 	mutex_enter(p->p_lock);
    143 
    144 	/*
    145 	 * Refuse to core if the data + stack + user size is larger than
    146 	 * the core dump limit.  XXX THIS IS WRONG, because of mapped
    147 	 * data.
    148 	 */
    149 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
    150 	    p->p_rlimit[RLIMIT_CORE].rlim_cur) {
    151 		error = EFBIG;		/* better error code? */
    152 		mutex_exit(p->p_lock);
    153 		mutex_exit(&proc_lock);
    154 		goto done;
    155 	}
    156 
    157 	/*
    158 	 * It may well not be curproc, so grab a reference to its current
    159 	 * credentials.
    160 	 */
    161 	kauth_cred_hold(p->p_cred);
    162 	cred = p->p_cred;
    163 
    164 	/*
    165 	 * Make sure the process has not set-id, to prevent data leaks,
    166 	 * unless it was specifically requested to allow set-id coredumps.
    167 	 */
    168 	if (p->p_flag & PK_SUGID) {
    169 		if (!security_setidcore_dump) {
    170 			error = EPERM;
    171 			mutex_exit(p->p_lock);
    172 			mutex_exit(&proc_lock);
    173 			goto done;
    174 		}
    175 		pattern = security_setidcore_path;
    176 	}
    177 
    178 	/* Lock, as p_limit and pl_corename might change. */
    179 	lim = p->p_limit;
    180 	mutex_enter(&lim->pl_lock);
    181 	if (pattern == NULL) {
    182 		pattern = lim->pl_corename;
    183 	}
    184 	error = coredump_buildname(p, name, pattern, MAXPATHLEN);
    185 	mutex_exit(&lim->pl_lock);
    186 
    187 	if (error) {
    188 		mutex_exit(p->p_lock);
    189 		mutex_exit(&proc_lock);
    190 		goto done;
    191 	}
    192 
    193 	/*
    194 	 * On a simple filename, see if the filesystem allow us to write
    195 	 * core dumps there.
    196 	 */
    197 	lastslash = strrchr(name, '/');
    198 	if (!lastslash) {
    199 		vp = p->p_cwdi->cwdi_cdir;
    200 		if (vp->v_mount == NULL ||
    201 		    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
    202 			error = EPERM;
    203 	}
    204 
    205 	mutex_exit(p->p_lock);
    206 	mutex_exit(&proc_lock);
    207 	if (error)
    208 		goto done;
    209 
    210 	/*
    211 	 * On a complex filename, see if the filesystem allow us to write
    212 	 * core dumps there.
    213 	 *
    214 	 * XXX: We should have an API that avoids double lookups
    215 	 */
    216 	if (lastslash) {
    217 		char c[2];
    218 
    219 		if (lastslash - name >= MAXPATHLEN - 2) {
    220 			error = EPERM;
    221 			goto done;
    222 		}
    223 
    224 		c[0] = lastslash[1];
    225 		c[1] = lastslash[2];
    226 		lastslash[1] = '.';
    227 		lastslash[2] = '\0';
    228 		error = namei_simple_kernel(name, NSM_FOLLOW_NOEMULROOT, &vp);
    229 		if (error)
    230 			goto done;
    231 		if (vp->v_mount == NULL ||
    232 		    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
    233 			error = EPERM;
    234 		vrele(vp);
    235 		if (error)
    236 			goto done;
    237 		lastslash[1] = c[0];
    238 		lastslash[2] = c[1];
    239 	}
    240 
    241 	pb = pathbuf_create(name);
    242 	if (pb == NULL) {
    243 		error = ENOMEM;
    244 		goto done;
    245 	}
    246 	NDINIT(&nd, LOOKUP, NOFOLLOW, pb);
    247 	if ((error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE,
    248 	    S_IRUSR | S_IWUSR)) != 0) {
    249 		pathbuf_destroy(pb);
    250 		goto done;
    251 	}
    252 	vp = nd.ni_vp;
    253 	pathbuf_destroy(pb);
    254 
    255 	/*
    256 	 * Don't dump to:
    257 	 * 	- non-regular files
    258 	 * 	- files with links
    259 	 * 	- files we don't own
    260 	 */
    261 	if (vp->v_type != VREG ||
    262 	    VOP_GETATTR(vp, &vattr, cred) || vattr.va_nlink != 1 ||
    263 	    vattr.va_uid != kauth_cred_geteuid(cred)) {
    264 		error = EACCES;
    265 		goto out;
    266 	}
    267 	vattr_null(&vattr);
    268 	vattr.va_size = 0;
    269 
    270 	if ((p->p_flag & PK_SUGID) && security_setidcore_dump) {
    271 		vattr.va_uid = security_setidcore_owner;
    272 		vattr.va_gid = security_setidcore_group;
    273 		vattr.va_mode = security_setidcore_mode;
    274 	}
    275 
    276 	VOP_SETATTR(vp, &vattr, cred);
    277 	p->p_acflag |= ACORE;
    278 
    279 	io.io_lwp = l;
    280 	io.io_vp = vp;
    281 	io.io_cred = cred;
    282 	io.io_offset = 0;
    283 
    284 	/* Now dump the actual core file. */
    285 	error = (*p->p_execsw->es_coredump)(l, &io);
    286  out:
    287 	VOP_UNLOCK(vp);
    288 	error1 = vn_close(vp, FWRITE, cred);
    289 	if (error == 0)
    290 		error = error1;
    291 done:
    292 	if (name != NULL)
    293 		PNBUF_PUT(name);
    294 	return error;
    295 }
    296 
    297 static int
    298 coredump_buildname(struct proc *p, char *dst, const char *src, size_t len)
    299 {
    300 	const char	*s;
    301 	char		*d, *end;
    302 	int		i;
    303 
    304 	KASSERT(mutex_owned(&proc_lock));
    305 
    306 	for (s = src, d = dst, end = d + len; *s != '\0'; s++) {
    307 		if (*s == '%') {
    308 			switch (*(s + 1)) {
    309 			case 'n':
    310 				i = snprintf(d, end - d, "%s", p->p_comm);
    311 				break;
    312 			case 'p':
    313 				i = snprintf(d, end - d, "%d", p->p_pid);
    314 				break;
    315 			case 'u':
    316 				i = snprintf(d, end - d, "%.*s",
    317 				    (int)sizeof p->p_pgrp->pg_session->s_login,
    318 				    p->p_pgrp->pg_session->s_login);
    319 				break;
    320 			case 't':
    321 				i = snprintf(d, end - d, "%lld",
    322 				    (long long)p->p_stats->p_start.tv_sec);
    323 				break;
    324 			default:
    325 				goto copy;
    326 			}
    327 			d += i;
    328 			s++;
    329 		} else {
    330  copy:			*d = *s;
    331 			d++;
    332 		}
    333 		if (d >= end)
    334 			return (ENAMETOOLONG);
    335 	}
    336 	*d = '\0';
    337 	return 0;
    338 }
    339 
    340 static int
    341 coredump_write(struct coredump_iostate *io, enum uio_seg segflg,
    342     const void *data, size_t len)
    343 {
    344 	int error;
    345 
    346 	error = vn_rdwr(UIO_WRITE, io->io_vp, __UNCONST(data), len,
    347 	    io->io_offset, segflg,
    348 	    IO_NODELOCKED|IO_UNIT, io->io_cred, NULL,
    349 	    segflg == UIO_USERSPACE ? io->io_lwp : NULL);
    350 	if (error) {
    351 		printf("pid %d (%s): %s write of %zu@%p at %lld failed: %d\n",
    352 		    io->io_lwp->l_proc->p_pid, io->io_lwp->l_proc->p_comm,
    353 		    segflg == UIO_USERSPACE ? "user" : "system",
    354 		    len, data, (long long) io->io_offset, error);
    355 		return (error);
    356 	}
    357 
    358 	io->io_offset += len;
    359 	return (0);
    360 }
    361 
    362 static off_t
    363 coredump_offset(struct coredump_iostate *io)
    364 {
    365 	return io->io_offset;
    366 }
    367