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