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