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