Home | History | Annotate | Line # | Download | only in kern
kern_core.c revision 1.5.10.1
      1  1.5.10.1   matt /*	$NetBSD: kern_core.c,v 1.5.10.1 2007/11/06 23:31:31 matt 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.5.10.1   matt __KERNEL_RCSID(0, "$NetBSD: kern_core.c,v 1.5.10.1 2007/11/06 23:31:31 matt Exp $");
     41       1.2     ad 
     42       1.2     ad #include "opt_coredump.h"
     43       1.2     ad 
     44       1.2     ad #include <sys/param.h>
     45       1.2     ad #include <sys/vnode.h>
     46       1.2     ad #include <sys/namei.h>
     47       1.2     ad #include <sys/acct.h>
     48       1.2     ad #include <sys/file.h>
     49       1.2     ad #include <sys/stat.h>
     50       1.2     ad #include <sys/proc.h>
     51       1.2     ad #include <sys/exec.h>
     52       1.2     ad #include <sys/filedesc.h>
     53       1.2     ad #include <sys/kauth.h>
     54       1.2     ad 
     55       1.2     ad #if !defined(COREDUMP)
     56       1.2     ad 
     57       1.2     ad int
     58       1.2     ad coredump(struct lwp *l, const char *pattern)
     59       1.2     ad {
     60       1.2     ad 
     61       1.2     ad 	return (ENOSYS);
     62       1.2     ad }
     63       1.2     ad 
     64       1.2     ad #else	/* COREDUMP */
     65       1.2     ad 
     66       1.2     ad struct coredump_iostate {
     67       1.2     ad 	struct lwp *io_lwp;
     68       1.2     ad 	struct vnode *io_vp;
     69       1.2     ad 	kauth_cred_t io_cred;
     70       1.2     ad 	off_t io_offset;
     71       1.2     ad };
     72       1.2     ad 
     73       1.2     ad static int	coredump_buildname(struct proc *, char *, const char *, size_t);
     74       1.2     ad 
     75       1.2     ad /*
     76       1.2     ad  * Dump core, into a file named "progname.core" or "core" (depending on the
     77       1.2     ad  * value of shortcorename), unless the process was setuid/setgid.
     78       1.2     ad  */
     79       1.2     ad int
     80       1.2     ad coredump(struct lwp *l, const char *pattern)
     81       1.2     ad {
     82       1.2     ad 	struct vnode		*vp;
     83       1.2     ad 	struct proc		*p;
     84       1.2     ad 	struct vmspace		*vm;
     85       1.2     ad 	kauth_cred_t		cred;
     86       1.2     ad 	struct nameidata	nd;
     87       1.2     ad 	struct vattr		vattr;
     88       1.2     ad 	struct coredump_iostate	io;
     89  1.5.10.1   matt 	struct plimit		*lim;
     90       1.2     ad 	int			error, error1;
     91  1.5.10.1   matt 	char			*name;
     92  1.5.10.1   matt 
     93  1.5.10.1   matt 	name = PNBUF_GET();
     94       1.2     ad 
     95       1.2     ad 	p = l->l_proc;
     96       1.2     ad 	vm = p->p_vmspace;
     97       1.2     ad 
     98       1.4     ad 	mutex_enter(&proclist_lock);	/* p_session */
     99       1.2     ad 	mutex_enter(&p->p_mutex);
    100       1.2     ad 
    101       1.2     ad 	/*
    102       1.2     ad 	 * Refuse to core if the data + stack + user size is larger than
    103       1.2     ad 	 * the core dump limit.  XXX THIS IS WRONG, because of mapped
    104       1.2     ad 	 * data.
    105       1.2     ad 	 */
    106       1.2     ad 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
    107       1.2     ad 	    p->p_rlimit[RLIMIT_CORE].rlim_cur) {
    108  1.5.10.1   matt 		error = EFBIG;		/* better error code? */
    109       1.2     ad 		mutex_exit(&p->p_mutex);
    110       1.4     ad 		mutex_exit(&proclist_lock);
    111  1.5.10.1   matt 		goto done;
    112       1.2     ad 	}
    113       1.2     ad 
    114       1.2     ad 	/*
    115       1.2     ad 	 * It may well not be curproc, so grab a reference to its current
    116       1.2     ad 	 * credentials.
    117       1.2     ad 	 */
    118       1.2     ad 	kauth_cred_hold(p->p_cred);
    119       1.2     ad 	cred = p->p_cred;
    120       1.2     ad 
    121       1.2     ad 	/*
    122       1.2     ad 	 * The core dump will go in the current working directory.  Make
    123       1.2     ad 	 * sure that the directory is still there and that the mount flags
    124       1.2     ad 	 * allow us to write core dumps there.
    125  1.5.10.1   matt 	 *
    126  1.5.10.1   matt 	 * XXX: this is partially bogus, it should be checking the directory
    127  1.5.10.1   matt 	 * into which the file is actually written - which probably needs
    128  1.5.10.1   matt 	 * a flag on namei()
    129       1.2     ad 	 */
    130       1.2     ad 	vp = p->p_cwdi->cwdi_cdir;
    131       1.2     ad 	if (vp->v_mount == NULL ||
    132       1.2     ad 	    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0) {
    133       1.2     ad 		error = EPERM;
    134       1.2     ad 		mutex_exit(&p->p_mutex);
    135       1.4     ad 		mutex_exit(&proclist_lock);
    136       1.2     ad 		goto done;
    137       1.2     ad 	}
    138       1.2     ad 
    139  1.5.10.1   matt 	/*
    140  1.5.10.1   matt 	 * Make sure the process has not set-id, to prevent data leaks,
    141  1.5.10.1   matt 	 * unless it was specifically requested to allow set-id coredumps.
    142  1.5.10.1   matt 	 */
    143  1.5.10.1   matt 	if (p->p_flag & PK_SUGID) {
    144  1.5.10.1   matt 		if (!security_setidcore_dump) {
    145  1.5.10.1   matt 			error = EPERM;
    146  1.5.10.1   matt 			mutex_exit(&p->p_mutex);
    147  1.5.10.1   matt 			mutex_exit(&proclist_lock);
    148  1.5.10.1   matt 			goto done;
    149  1.5.10.1   matt 		}
    150       1.2     ad 		pattern = security_setidcore_path;
    151  1.5.10.1   matt 	}
    152       1.2     ad 
    153  1.5.10.1   matt 	/* It is (just) possible for p_limit and pl_corename to change */
    154  1.5.10.1   matt 	lim = p->p_limit;
    155  1.5.10.1   matt 	mutex_enter(&lim->pl_lock);
    156       1.2     ad 	if (pattern == NULL)
    157  1.5.10.1   matt 		pattern = lim->pl_corename;
    158       1.2     ad 	error = coredump_buildname(p, name, pattern, MAXPATHLEN);
    159  1.5.10.1   matt 	mutex_exit(&lim->pl_lock);
    160       1.2     ad 	mutex_exit(&p->p_mutex);
    161       1.4     ad 	mutex_exit(&proclist_lock);
    162       1.2     ad 	if (error)
    163       1.2     ad 		goto done;
    164       1.2     ad 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, l);
    165       1.2     ad 	if ((error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE,
    166       1.2     ad 	    S_IRUSR | S_IWUSR)) != 0)
    167       1.2     ad 		goto done;
    168       1.2     ad 	vp = nd.ni_vp;
    169       1.2     ad 
    170       1.2     ad 	/* Don't dump to non-regular files or files with links. */
    171       1.2     ad 	if (vp->v_type != VREG ||
    172       1.2     ad 	    VOP_GETATTR(vp, &vattr, cred, l) || vattr.va_nlink != 1) {
    173       1.2     ad 		error = EINVAL;
    174       1.2     ad 		goto out;
    175       1.2     ad 	}
    176       1.2     ad 	VATTR_NULL(&vattr);
    177       1.2     ad 	vattr.va_size = 0;
    178       1.2     ad 
    179       1.3  pavel 	if ((p->p_flag & PK_SUGID) && security_setidcore_dump) {
    180       1.2     ad 		vattr.va_uid = security_setidcore_owner;
    181       1.2     ad 		vattr.va_gid = security_setidcore_group;
    182       1.2     ad 		vattr.va_mode = security_setidcore_mode;
    183       1.2     ad 	}
    184       1.2     ad 
    185       1.2     ad 	VOP_LEASE(vp, l, cred, LEASE_WRITE);
    186       1.2     ad 	VOP_SETATTR(vp, &vattr, cred, l);
    187       1.2     ad 	p->p_acflag |= ACORE;
    188       1.2     ad 
    189       1.2     ad 	io.io_lwp = l;
    190       1.2     ad 	io.io_vp = vp;
    191       1.2     ad 	io.io_cred = cred;
    192       1.2     ad 	io.io_offset = 0;
    193       1.2     ad 
    194       1.2     ad 	/* Now dump the actual core file. */
    195       1.2     ad 	error = (*p->p_execsw->es_coredump)(l, &io);
    196       1.2     ad  out:
    197       1.2     ad 	VOP_UNLOCK(vp, 0);
    198       1.2     ad 	error1 = vn_close(vp, FWRITE, cred, l);
    199       1.2     ad 	if (error == 0)
    200       1.2     ad 		error = error1;
    201       1.2     ad done:
    202       1.2     ad 	if (name != NULL)
    203       1.2     ad 		PNBUF_PUT(name);
    204       1.2     ad 	return error;
    205       1.2     ad }
    206       1.2     ad 
    207       1.2     ad static int
    208       1.2     ad coredump_buildname(struct proc *p, char *dst, const char *src, size_t len)
    209       1.2     ad {
    210       1.2     ad 	const char	*s;
    211       1.2     ad 	char		*d, *end;
    212       1.2     ad 	int		i;
    213       1.2     ad 
    214       1.4     ad 	KASSERT(mutex_owned(&proclist_lock));
    215       1.2     ad 
    216       1.2     ad 	for (s = src, d = dst, end = d + len; *s != '\0'; s++) {
    217       1.2     ad 		if (*s == '%') {
    218       1.2     ad 			switch (*(s + 1)) {
    219       1.2     ad 			case 'n':
    220       1.2     ad 				i = snprintf(d, end - d, "%s", p->p_comm);
    221       1.2     ad 				break;
    222       1.2     ad 			case 'p':
    223       1.2     ad 				i = snprintf(d, end - d, "%d", p->p_pid);
    224       1.2     ad 				break;
    225       1.2     ad 			case 'u':
    226       1.2     ad 				i = snprintf(d, end - d, "%.*s",
    227       1.2     ad 				    (int)sizeof p->p_pgrp->pg_session->s_login,
    228       1.2     ad 				    p->p_pgrp->pg_session->s_login);
    229       1.2     ad 				break;
    230       1.2     ad 			case 't':
    231       1.2     ad 				i = snprintf(d, end - d, "%ld",
    232       1.2     ad 				    p->p_stats->p_start.tv_sec);
    233       1.2     ad 				break;
    234       1.2     ad 			default:
    235       1.2     ad 				goto copy;
    236       1.2     ad 			}
    237       1.2     ad 			d += i;
    238       1.2     ad 			s++;
    239       1.2     ad 		} else {
    240       1.2     ad  copy:			*d = *s;
    241       1.2     ad 			d++;
    242       1.2     ad 		}
    243       1.2     ad 		if (d >= end)
    244       1.2     ad 			return (ENAMETOOLONG);
    245       1.2     ad 	}
    246       1.2     ad 	*d = '\0';
    247       1.2     ad 	return 0;
    248       1.2     ad }
    249       1.2     ad 
    250       1.2     ad int
    251       1.2     ad coredump_write(void *cookie, enum uio_seg segflg, const void *data, size_t len)
    252       1.2     ad {
    253       1.2     ad 	struct coredump_iostate *io = cookie;
    254       1.2     ad 	int error;
    255       1.2     ad 
    256       1.2     ad 	error = vn_rdwr(UIO_WRITE, io->io_vp, __UNCONST(data), len,
    257       1.2     ad 	    io->io_offset, segflg,
    258       1.2     ad 	    IO_NODELOCKED|IO_UNIT, io->io_cred, NULL,
    259       1.2     ad 	    segflg == UIO_USERSPACE ? io->io_lwp : NULL);
    260       1.2     ad 	if (error) {
    261       1.2     ad 		printf("pid %d (%s): %s write of %zu@%p at %lld failed: %d\n",
    262       1.2     ad 		    io->io_lwp->l_proc->p_pid, io->io_lwp->l_proc->p_comm,
    263       1.2     ad 		    segflg == UIO_USERSPACE ? "user" : "system",
    264       1.2     ad 		    len, data, (long long) io->io_offset, error);
    265       1.2     ad 		return (error);
    266       1.2     ad 	}
    267       1.2     ad 
    268       1.2     ad 	io->io_offset += len;
    269       1.2     ad 	return (0);
    270       1.2     ad }
    271       1.2     ad 
    272       1.2     ad #endif	/* COREDUMP */
    273