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