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