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