xref: /netbsd-src/sys/kern/kern_core.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: kern_core.c,v 1.30 2020/05/23 23:42:43 ad 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.30 2020/05/23 23:42:43 ad Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/vnode.h>
44 #include <sys/namei.h>
45 #include <sys/acct.h>
46 #include <sys/file.h>
47 #include <sys/stat.h>
48 #include <sys/proc.h>
49 #include <sys/exec.h>
50 #include <sys/filedesc.h>
51 #include <sys/kauth.h>
52 #include <sys/module.h>
53 #include <sys/compat_stub.h>
54 
55 MODULE(MODULE_CLASS_MISC, coredump, NULL);
56 
57 struct coredump_iostate {
58 	struct lwp *io_lwp;
59 	struct vnode *io_vp;
60 	kauth_cred_t io_cred;
61 	off_t io_offset;
62 };
63 
64 static int	coredump(struct lwp *, const char *);
65 static int	coredump_buildname(struct proc *, char *, const char *, size_t);
66 static int	coredump_write(struct coredump_iostate *, enum uio_seg segflg,
67 		    const void *, size_t);
68 static off_t	coredump_offset(struct coredump_iostate *);
69 
70 static int
71 coredump_modcmd(modcmd_t cmd, void *arg)
72 {
73 
74 	switch (cmd) {
75 	case MODULE_CMD_INIT:
76 		MODULE_HOOK_SET(coredump_hook, coredump);
77 		MODULE_HOOK_SET(coredump_write_hook, coredump_write);
78 		MODULE_HOOK_SET(coredump_offset_hook, coredump_offset);
79 		MODULE_HOOK_SET(coredump_netbsd_hook, real_coredump_netbsd);
80 		MODULE_HOOK_SET(uvm_coredump_walkmap_hook,
81 		    uvm_coredump_walkmap);
82 		MODULE_HOOK_SET(uvm_coredump_count_segs_hook,
83 		    uvm_coredump_count_segs);
84 		return 0;
85 	case MODULE_CMD_FINI:
86 		MODULE_HOOK_UNSET(uvm_coredump_count_segs_hook);
87 		MODULE_HOOK_UNSET(uvm_coredump_walkmap_hook);
88 		MODULE_HOOK_UNSET(coredump_netbsd_hook);
89 		MODULE_HOOK_UNSET(coredump_offset_hook);
90 		MODULE_HOOK_UNSET(coredump_write_hook);
91 		MODULE_HOOK_UNSET(coredump_hook);
92 		return 0;
93 	default:
94 		return ENOTTY;
95 	}
96 }
97 
98 /*
99  * Dump core, into a file named "progname.core" or "core" (depending on the
100  * value of shortcorename), unless the process was setuid/setgid.
101  */
102 static int
103 coredump(struct lwp *l, const char *pattern)
104 {
105 	struct vnode		*vp;
106 	struct proc		*p;
107 	struct vmspace		*vm;
108 	kauth_cred_t		cred;
109 	struct pathbuf		*pb;
110 	struct nameidata	nd;
111 	struct vattr		vattr;
112 	struct coredump_iostate	io;
113 	struct plimit		*lim;
114 	int			error, error1;
115 	char			*name, *lastslash;
116 
117 	name = PNBUF_GET();
118 
119 	p = l->l_proc;
120 	vm = p->p_vmspace;
121 
122 	mutex_enter(&proc_lock);		/* p_session */
123 	mutex_enter(p->p_lock);
124 
125 	/*
126 	 * Refuse to core if the data + stack + user size is larger than
127 	 * the core dump limit.  XXX THIS IS WRONG, because of mapped
128 	 * data.
129 	 */
130 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
131 	    p->p_rlimit[RLIMIT_CORE].rlim_cur) {
132 		error = EFBIG;		/* better error code? */
133 		mutex_exit(p->p_lock);
134 		mutex_exit(&proc_lock);
135 		goto done;
136 	}
137 
138 	/*
139 	 * It may well not be curproc, so grab a reference to its current
140 	 * credentials.
141 	 */
142 	kauth_cred_hold(p->p_cred);
143 	cred = p->p_cred;
144 
145 	/*
146 	 * Make sure the process has not set-id, to prevent data leaks,
147 	 * unless it was specifically requested to allow set-id coredumps.
148 	 */
149 	if (p->p_flag & PK_SUGID) {
150 		if (!security_setidcore_dump) {
151 			error = EPERM;
152 			mutex_exit(p->p_lock);
153 			mutex_exit(&proc_lock);
154 			goto done;
155 		}
156 		pattern = security_setidcore_path;
157 	}
158 
159 	/* Lock, as p_limit and pl_corename might change. */
160 	lim = p->p_limit;
161 	mutex_enter(&lim->pl_lock);
162 	if (pattern == NULL) {
163 		pattern = lim->pl_corename;
164 	}
165 	error = coredump_buildname(p, name, pattern, MAXPATHLEN);
166 	mutex_exit(&lim->pl_lock);
167 
168 	if (error) {
169 		mutex_exit(p->p_lock);
170 		mutex_exit(&proc_lock);
171 		goto done;
172 	}
173 
174 	/*
175 	 * On a simple filename, see if the filesystem allow us to write
176 	 * core dumps there.
177 	 */
178 	lastslash = strrchr(name, '/');
179 	if (!lastslash) {
180 		vp = p->p_cwdi->cwdi_cdir;
181 		if (vp->v_mount == NULL ||
182 		    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
183 			error = EPERM;
184 	}
185 
186 	mutex_exit(p->p_lock);
187 	mutex_exit(&proc_lock);
188 	if (error)
189 		goto done;
190 
191 	/*
192 	 * On a complex filename, see if the filesystem allow us to write
193 	 * core dumps there.
194 	 *
195 	 * XXX: We should have an API that avoids double lookups
196 	 */
197 	if (lastslash) {
198 		char c[2];
199 
200 		if (lastslash - name >= MAXPATHLEN - 2) {
201 			error = EPERM;
202 			goto done;
203 		}
204 
205 		c[0] = lastslash[1];
206 		c[1] = lastslash[2];
207 		lastslash[1] = '.';
208 		lastslash[2] = '\0';
209 		error = namei_simple_kernel(name, NSM_FOLLOW_NOEMULROOT, &vp);
210 		if (error)
211 			goto done;
212 		if (vp->v_mount == NULL ||
213 		    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
214 			error = EPERM;
215 		vrele(vp);
216 		if (error)
217 			goto done;
218 		lastslash[1] = c[0];
219 		lastslash[2] = c[1];
220 	}
221 
222 	pb = pathbuf_create(name);
223 	if (pb == NULL) {
224 		error = ENOMEM;
225 		goto done;
226 	}
227 	NDINIT(&nd, LOOKUP, NOFOLLOW, pb);
228 	if ((error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE,
229 	    S_IRUSR | S_IWUSR)) != 0) {
230 		pathbuf_destroy(pb);
231 		goto done;
232 	}
233 	vp = nd.ni_vp;
234 	pathbuf_destroy(pb);
235 
236 	/*
237 	 * Don't dump to:
238 	 * 	- non-regular files
239 	 * 	- files with links
240 	 * 	- files we don't own
241 	 */
242 	if (vp->v_type != VREG ||
243 	    VOP_GETATTR(vp, &vattr, cred) || vattr.va_nlink != 1 ||
244 	    vattr.va_uid != kauth_cred_geteuid(cred)) {
245 		error = EACCES;
246 		goto out;
247 	}
248 	vattr_null(&vattr);
249 	vattr.va_size = 0;
250 
251 	if ((p->p_flag & PK_SUGID) && security_setidcore_dump) {
252 		vattr.va_uid = security_setidcore_owner;
253 		vattr.va_gid = security_setidcore_group;
254 		vattr.va_mode = security_setidcore_mode;
255 	}
256 
257 	VOP_SETATTR(vp, &vattr, cred);
258 	p->p_acflag |= ACORE;
259 
260 	io.io_lwp = l;
261 	io.io_vp = vp;
262 	io.io_cred = cred;
263 	io.io_offset = 0;
264 
265 	/* Now dump the actual core file. */
266 	error = (*p->p_execsw->es_coredump)(l, &io);
267  out:
268 	VOP_UNLOCK(vp);
269 	error1 = vn_close(vp, FWRITE, cred);
270 	if (error == 0)
271 		error = error1;
272 done:
273 	if (name != NULL)
274 		PNBUF_PUT(name);
275 	return error;
276 }
277 
278 static int
279 coredump_buildname(struct proc *p, char *dst, const char *src, size_t len)
280 {
281 	const char	*s;
282 	char		*d, *end;
283 	int		i;
284 
285 	KASSERT(mutex_owned(&proc_lock));
286 
287 	for (s = src, d = dst, end = d + len; *s != '\0'; s++) {
288 		if (*s == '%') {
289 			switch (*(s + 1)) {
290 			case 'n':
291 				i = snprintf(d, end - d, "%s", p->p_comm);
292 				break;
293 			case 'p':
294 				i = snprintf(d, end - d, "%d", p->p_pid);
295 				break;
296 			case 'u':
297 				i = snprintf(d, end - d, "%.*s",
298 				    (int)sizeof p->p_pgrp->pg_session->s_login,
299 				    p->p_pgrp->pg_session->s_login);
300 				break;
301 			case 't':
302 				i = snprintf(d, end - d, "%lld",
303 				    (long long)p->p_stats->p_start.tv_sec);
304 				break;
305 			default:
306 				goto copy;
307 			}
308 			d += i;
309 			s++;
310 		} else {
311  copy:			*d = *s;
312 			d++;
313 		}
314 		if (d >= end)
315 			return (ENAMETOOLONG);
316 	}
317 	*d = '\0';
318 	return 0;
319 }
320 
321 static int
322 coredump_write(struct coredump_iostate *io, enum uio_seg segflg,
323     const void *data, size_t len)
324 {
325 	int error;
326 
327 	error = vn_rdwr(UIO_WRITE, io->io_vp, __UNCONST(data), len,
328 	    io->io_offset, segflg,
329 	    IO_NODELOCKED|IO_UNIT, io->io_cred, NULL,
330 	    segflg == UIO_USERSPACE ? io->io_lwp : NULL);
331 	if (error) {
332 		printf("pid %d (%s): %s write of %zu@%p at %lld failed: %d\n",
333 		    io->io_lwp->l_proc->p_pid, io->io_lwp->l_proc->p_comm,
334 		    segflg == UIO_USERSPACE ? "user" : "system",
335 		    len, data, (long long) io->io_offset, error);
336 		return (error);
337 	}
338 
339 	io->io_offset += len;
340 	return (0);
341 }
342 
343 static off_t
344 coredump_offset(struct coredump_iostate *io)
345 {
346 	return io->io_offset;
347 }
348