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