Home | History | Annotate | Line # | Download | only in kern
kern_core.c revision 1.3.2.7
      1  1.3.2.7  yamt /*	$NetBSD: kern_core.c,v 1.3.2.7 2008/02/04 09:24:08 yamt Exp $	*/
      2  1.3.2.2  yamt 
      3  1.3.2.2  yamt /*
      4  1.3.2.2  yamt  * Copyright (c) 1982, 1986, 1989, 1991, 1993
      5  1.3.2.2  yamt  *	The Regents of the University of California.  All rights reserved.
      6  1.3.2.2  yamt  * (c) UNIX System Laboratories, Inc.
      7  1.3.2.2  yamt  * All or some portions of this file are derived from material licensed
      8  1.3.2.2  yamt  * to the University of California by American Telephone and Telegraph
      9  1.3.2.2  yamt  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  1.3.2.2  yamt  * the permission of UNIX System Laboratories, Inc.
     11  1.3.2.2  yamt  *
     12  1.3.2.2  yamt  * Redistribution and use in source and binary forms, with or without
     13  1.3.2.2  yamt  * modification, are permitted provided that the following conditions
     14  1.3.2.2  yamt  * are met:
     15  1.3.2.2  yamt  * 1. Redistributions of source code must retain the above copyright
     16  1.3.2.2  yamt  *    notice, this list of conditions and the following disclaimer.
     17  1.3.2.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.3.2.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     19  1.3.2.2  yamt  *    documentation and/or other materials provided with the distribution.
     20  1.3.2.2  yamt  * 3. Neither the name of the University nor the names of its contributors
     21  1.3.2.2  yamt  *    may be used to endorse or promote products derived from this software
     22  1.3.2.2  yamt  *    without specific prior written permission.
     23  1.3.2.2  yamt  *
     24  1.3.2.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.3.2.2  yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.3.2.2  yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.3.2.2  yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.3.2.2  yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.3.2.2  yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.3.2.2  yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.3.2.2  yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.3.2.2  yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.3.2.2  yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.3.2.2  yamt  * SUCH DAMAGE.
     35  1.3.2.2  yamt  *
     36  1.3.2.2  yamt  *	@(#)kern_sig.c	8.14 (Berkeley) 5/14/95
     37  1.3.2.2  yamt  */
     38  1.3.2.2  yamt 
     39  1.3.2.2  yamt #include <sys/cdefs.h>
     40  1.3.2.7  yamt __KERNEL_RCSID(0, "$NetBSD: kern_core.c,v 1.3.2.7 2008/02/04 09:24:08 yamt Exp $");
     41  1.3.2.2  yamt 
     42  1.3.2.2  yamt #include "opt_coredump.h"
     43  1.3.2.2  yamt 
     44  1.3.2.2  yamt #include <sys/param.h>
     45  1.3.2.2  yamt #include <sys/vnode.h>
     46  1.3.2.2  yamt #include <sys/namei.h>
     47  1.3.2.2  yamt #include <sys/acct.h>
     48  1.3.2.2  yamt #include <sys/file.h>
     49  1.3.2.2  yamt #include <sys/stat.h>
     50  1.3.2.2  yamt #include <sys/proc.h>
     51  1.3.2.2  yamt #include <sys/exec.h>
     52  1.3.2.2  yamt #include <sys/filedesc.h>
     53  1.3.2.2  yamt #include <sys/kauth.h>
     54  1.3.2.2  yamt 
     55  1.3.2.2  yamt #if !defined(COREDUMP)
     56  1.3.2.2  yamt 
     57  1.3.2.2  yamt int
     58  1.3.2.2  yamt coredump(struct lwp *l, const char *pattern)
     59  1.3.2.2  yamt {
     60  1.3.2.2  yamt 
     61  1.3.2.2  yamt 	return (ENOSYS);
     62  1.3.2.2  yamt }
     63  1.3.2.2  yamt 
     64  1.3.2.2  yamt #else	/* COREDUMP */
     65  1.3.2.2  yamt 
     66  1.3.2.2  yamt struct coredump_iostate {
     67  1.3.2.2  yamt 	struct lwp *io_lwp;
     68  1.3.2.2  yamt 	struct vnode *io_vp;
     69  1.3.2.2  yamt 	kauth_cred_t io_cred;
     70  1.3.2.2  yamt 	off_t io_offset;
     71  1.3.2.2  yamt };
     72  1.3.2.2  yamt 
     73  1.3.2.2  yamt static int	coredump_buildname(struct proc *, char *, const char *, size_t);
     74  1.3.2.2  yamt 
     75  1.3.2.2  yamt /*
     76  1.3.2.2  yamt  * Dump core, into a file named "progname.core" or "core" (depending on the
     77  1.3.2.2  yamt  * value of shortcorename), unless the process was setuid/setgid.
     78  1.3.2.2  yamt  */
     79  1.3.2.2  yamt int
     80  1.3.2.2  yamt coredump(struct lwp *l, const char *pattern)
     81  1.3.2.2  yamt {
     82  1.3.2.2  yamt 	struct vnode		*vp;
     83  1.3.2.2  yamt 	struct proc		*p;
     84  1.3.2.2  yamt 	struct vmspace		*vm;
     85  1.3.2.2  yamt 	kauth_cred_t		cred;
     86  1.3.2.2  yamt 	struct nameidata	nd;
     87  1.3.2.2  yamt 	struct vattr		vattr;
     88  1.3.2.2  yamt 	struct coredump_iostate	io;
     89  1.3.2.4  yamt 	struct plimit		*lim;
     90  1.3.2.2  yamt 	int			error, error1;
     91  1.3.2.4  yamt 	char			*name;
     92  1.3.2.4  yamt 
     93  1.3.2.4  yamt 	name = PNBUF_GET();
     94  1.3.2.2  yamt 
     95  1.3.2.2  yamt 	p = l->l_proc;
     96  1.3.2.2  yamt 	vm = p->p_vmspace;
     97  1.3.2.2  yamt 
     98  1.3.2.3  yamt 	mutex_enter(&proclist_lock);	/* p_session */
     99  1.3.2.2  yamt 	mutex_enter(&p->p_mutex);
    100  1.3.2.2  yamt 
    101  1.3.2.2  yamt 	/*
    102  1.3.2.2  yamt 	 * Refuse to core if the data + stack + user size is larger than
    103  1.3.2.2  yamt 	 * the core dump limit.  XXX THIS IS WRONG, because of mapped
    104  1.3.2.2  yamt 	 * data.
    105  1.3.2.2  yamt 	 */
    106  1.3.2.2  yamt 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
    107  1.3.2.2  yamt 	    p->p_rlimit[RLIMIT_CORE].rlim_cur) {
    108  1.3.2.4  yamt 		error = EFBIG;		/* better error code? */
    109  1.3.2.2  yamt 		mutex_exit(&p->p_mutex);
    110  1.3.2.3  yamt 		mutex_exit(&proclist_lock);
    111  1.3.2.4  yamt 		goto done;
    112  1.3.2.2  yamt 	}
    113  1.3.2.2  yamt 
    114  1.3.2.2  yamt 	/*
    115  1.3.2.2  yamt 	 * It may well not be curproc, so grab a reference to its current
    116  1.3.2.2  yamt 	 * credentials.
    117  1.3.2.2  yamt 	 */
    118  1.3.2.2  yamt 	kauth_cred_hold(p->p_cred);
    119  1.3.2.2  yamt 	cred = p->p_cred;
    120  1.3.2.2  yamt 
    121  1.3.2.2  yamt 	/*
    122  1.3.2.2  yamt 	 * The core dump will go in the current working directory.  Make
    123  1.3.2.2  yamt 	 * sure that the directory is still there and that the mount flags
    124  1.3.2.2  yamt 	 * allow us to write core dumps there.
    125  1.3.2.4  yamt 	 *
    126  1.3.2.4  yamt 	 * XXX: this is partially bogus, it should be checking the directory
    127  1.3.2.4  yamt 	 * into which the file is actually written - which probably needs
    128  1.3.2.4  yamt 	 * a flag on namei()
    129  1.3.2.2  yamt 	 */
    130  1.3.2.2  yamt 	vp = p->p_cwdi->cwdi_cdir;
    131  1.3.2.2  yamt 	if (vp->v_mount == NULL ||
    132  1.3.2.2  yamt 	    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0) {
    133  1.3.2.2  yamt 		error = EPERM;
    134  1.3.2.2  yamt 		mutex_exit(&p->p_mutex);
    135  1.3.2.3  yamt 		mutex_exit(&proclist_lock);
    136  1.3.2.2  yamt 		goto done;
    137  1.3.2.2  yamt 	}
    138  1.3.2.2  yamt 
    139  1.3.2.4  yamt 	/*
    140  1.3.2.4  yamt 	 * Make sure the process has not set-id, to prevent data leaks,
    141  1.3.2.4  yamt 	 * unless it was specifically requested to allow set-id coredumps.
    142  1.3.2.4  yamt 	 */
    143  1.3.2.4  yamt 	if (p->p_flag & PK_SUGID) {
    144  1.3.2.4  yamt 		if (!security_setidcore_dump) {
    145  1.3.2.4  yamt 			error = EPERM;
    146  1.3.2.4  yamt 			mutex_exit(&p->p_mutex);
    147  1.3.2.4  yamt 			mutex_exit(&proclist_lock);
    148  1.3.2.4  yamt 			goto done;
    149  1.3.2.4  yamt 		}
    150  1.3.2.2  yamt 		pattern = security_setidcore_path;
    151  1.3.2.4  yamt 	}
    152  1.3.2.2  yamt 
    153  1.3.2.4  yamt 	/* It is (just) possible for p_limit and pl_corename to change */
    154  1.3.2.4  yamt 	lim = p->p_limit;
    155  1.3.2.4  yamt 	mutex_enter(&lim->pl_lock);
    156  1.3.2.2  yamt 	if (pattern == NULL)
    157  1.3.2.4  yamt 		pattern = lim->pl_corename;
    158  1.3.2.2  yamt 	error = coredump_buildname(p, name, pattern, MAXPATHLEN);
    159  1.3.2.4  yamt 	mutex_exit(&lim->pl_lock);
    160  1.3.2.2  yamt 	mutex_exit(&p->p_mutex);
    161  1.3.2.3  yamt 	mutex_exit(&proclist_lock);
    162  1.3.2.2  yamt 	if (error)
    163  1.3.2.2  yamt 		goto done;
    164  1.3.2.6  yamt 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name);
    165  1.3.2.2  yamt 	if ((error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE,
    166  1.3.2.2  yamt 	    S_IRUSR | S_IWUSR)) != 0)
    167  1.3.2.2  yamt 		goto done;
    168  1.3.2.2  yamt 	vp = nd.ni_vp;
    169  1.3.2.2  yamt 
    170  1.3.2.2  yamt 	/* Don't dump to non-regular files or files with links. */
    171  1.3.2.2  yamt 	if (vp->v_type != VREG ||
    172  1.3.2.5  yamt 	    VOP_GETATTR(vp, &vattr, cred) || vattr.va_nlink != 1) {
    173  1.3.2.2  yamt 		error = EINVAL;
    174  1.3.2.2  yamt 		goto out;
    175  1.3.2.2  yamt 	}
    176  1.3.2.2  yamt 	VATTR_NULL(&vattr);
    177  1.3.2.2  yamt 	vattr.va_size = 0;
    178  1.3.2.2  yamt 
    179  1.3.2.2  yamt 	if ((p->p_flag & PK_SUGID) && security_setidcore_dump) {
    180  1.3.2.2  yamt 		vattr.va_uid = security_setidcore_owner;
    181  1.3.2.2  yamt 		vattr.va_gid = security_setidcore_group;
    182  1.3.2.2  yamt 		vattr.va_mode = security_setidcore_mode;
    183  1.3.2.2  yamt 	}
    184  1.3.2.2  yamt 
    185  1.3.2.5  yamt 	VOP_SETATTR(vp, &vattr, cred);
    186  1.3.2.2  yamt 	p->p_acflag |= ACORE;
    187  1.3.2.2  yamt 
    188  1.3.2.2  yamt 	io.io_lwp = l;
    189  1.3.2.2  yamt 	io.io_vp = vp;
    190  1.3.2.2  yamt 	io.io_cred = cred;
    191  1.3.2.2  yamt 	io.io_offset = 0;
    192  1.3.2.2  yamt 
    193  1.3.2.2  yamt 	/* Now dump the actual core file. */
    194  1.3.2.2  yamt 	error = (*p->p_execsw->es_coredump)(l, &io);
    195  1.3.2.2  yamt  out:
    196  1.3.2.2  yamt 	VOP_UNLOCK(vp, 0);
    197  1.3.2.2  yamt 	error1 = vn_close(vp, FWRITE, cred, l);
    198  1.3.2.2  yamt 	if (error == 0)
    199  1.3.2.2  yamt 		error = error1;
    200  1.3.2.2  yamt done:
    201  1.3.2.2  yamt 	if (name != NULL)
    202  1.3.2.2  yamt 		PNBUF_PUT(name);
    203  1.3.2.2  yamt 	return error;
    204  1.3.2.2  yamt }
    205  1.3.2.2  yamt 
    206  1.3.2.2  yamt static int
    207  1.3.2.2  yamt coredump_buildname(struct proc *p, char *dst, const char *src, size_t len)
    208  1.3.2.2  yamt {
    209  1.3.2.2  yamt 	const char	*s;
    210  1.3.2.2  yamt 	char		*d, *end;
    211  1.3.2.2  yamt 	int		i;
    212  1.3.2.2  yamt 
    213  1.3.2.3  yamt 	KASSERT(mutex_owned(&proclist_lock));
    214  1.3.2.2  yamt 
    215  1.3.2.2  yamt 	for (s = src, d = dst, end = d + len; *s != '\0'; s++) {
    216  1.3.2.2  yamt 		if (*s == '%') {
    217  1.3.2.2  yamt 			switch (*(s + 1)) {
    218  1.3.2.2  yamt 			case 'n':
    219  1.3.2.2  yamt 				i = snprintf(d, end - d, "%s", p->p_comm);
    220  1.3.2.2  yamt 				break;
    221  1.3.2.2  yamt 			case 'p':
    222  1.3.2.2  yamt 				i = snprintf(d, end - d, "%d", p->p_pid);
    223  1.3.2.2  yamt 				break;
    224  1.3.2.2  yamt 			case 'u':
    225  1.3.2.2  yamt 				i = snprintf(d, end - d, "%.*s",
    226  1.3.2.2  yamt 				    (int)sizeof p->p_pgrp->pg_session->s_login,
    227  1.3.2.2  yamt 				    p->p_pgrp->pg_session->s_login);
    228  1.3.2.2  yamt 				break;
    229  1.3.2.2  yamt 			case 't':
    230  1.3.2.2  yamt 				i = snprintf(d, end - d, "%ld",
    231  1.3.2.2  yamt 				    p->p_stats->p_start.tv_sec);
    232  1.3.2.2  yamt 				break;
    233  1.3.2.2  yamt 			default:
    234  1.3.2.2  yamt 				goto copy;
    235  1.3.2.2  yamt 			}
    236  1.3.2.2  yamt 			d += i;
    237  1.3.2.2  yamt 			s++;
    238  1.3.2.2  yamt 		} else {
    239  1.3.2.2  yamt  copy:			*d = *s;
    240  1.3.2.2  yamt 			d++;
    241  1.3.2.2  yamt 		}
    242  1.3.2.2  yamt 		if (d >= end)
    243  1.3.2.2  yamt 			return (ENAMETOOLONG);
    244  1.3.2.2  yamt 	}
    245  1.3.2.2  yamt 	*d = '\0';
    246  1.3.2.2  yamt 	return 0;
    247  1.3.2.2  yamt }
    248  1.3.2.2  yamt 
    249  1.3.2.2  yamt int
    250  1.3.2.2  yamt coredump_write(void *cookie, enum uio_seg segflg, const void *data, size_t len)
    251  1.3.2.2  yamt {
    252  1.3.2.2  yamt 	struct coredump_iostate *io = cookie;
    253  1.3.2.2  yamt 	int error;
    254  1.3.2.2  yamt 
    255  1.3.2.2  yamt 	error = vn_rdwr(UIO_WRITE, io->io_vp, __UNCONST(data), len,
    256  1.3.2.2  yamt 	    io->io_offset, segflg,
    257  1.3.2.2  yamt 	    IO_NODELOCKED|IO_UNIT, io->io_cred, NULL,
    258  1.3.2.2  yamt 	    segflg == UIO_USERSPACE ? io->io_lwp : NULL);
    259  1.3.2.2  yamt 	if (error) {
    260  1.3.2.2  yamt 		printf("pid %d (%s): %s write of %zu@%p at %lld failed: %d\n",
    261  1.3.2.2  yamt 		    io->io_lwp->l_proc->p_pid, io->io_lwp->l_proc->p_comm,
    262  1.3.2.2  yamt 		    segflg == UIO_USERSPACE ? "user" : "system",
    263  1.3.2.2  yamt 		    len, data, (long long) io->io_offset, error);
    264  1.3.2.2  yamt 		return (error);
    265  1.3.2.2  yamt 	}
    266  1.3.2.2  yamt 
    267  1.3.2.2  yamt 	io->io_offset += len;
    268  1.3.2.2  yamt 	return (0);
    269  1.3.2.2  yamt }
    270  1.3.2.2  yamt 
    271  1.3.2.2  yamt #endif	/* COREDUMP */
    272