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