1 /* $NetBSD: kern_core.c,v 1.38 2023/07/11 09:48:56 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.38 2023/07/11 09:48:56 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/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 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 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 kauth_cred_hold(p->p_cred); 157 cred = p->p_cred; 158 159 /* 160 * Make sure the process has not set-id, to prevent data leaks, 161 * unless it was specifically requested to allow set-id coredumps. 162 */ 163 if (p->p_flag & PK_SUGID) { 164 if (!security_setidcore_dump) { 165 error = EPERM; 166 goto release; 167 } 168 pattern = security_setidcore_path; 169 } 170 171 /* Lock, as p_limit and pl_corename might change. */ 172 lim = p->p_limit; 173 mutex_enter(&lim->pl_lock); 174 if (pattern == NULL) { 175 pattern = lim->pl_corename; 176 } 177 error = coredump_buildname(p, name, pattern, MAXPATHLEN); 178 mutex_exit(&lim->pl_lock); 179 180 if (error) 181 goto release; 182 183 /* 184 * On a simple filename, see if the filesystem allow us to write 185 * core dumps there. 186 */ 187 lastslash = strrchr(name, '/'); 188 if (!lastslash) { 189 vp = p->p_cwdi->cwdi_cdir; 190 if (vp->v_mount == NULL || 191 (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0) 192 error = EPERM; 193 } 194 195 release: 196 mutex_exit(p->p_lock); 197 mutex_exit(&proc_lock); 198 if (error) 199 goto done; 200 201 /* 202 * On a complex filename, see if the filesystem allow us to write 203 * core dumps there. 204 * 205 * XXX: We should have an API that avoids double lookups 206 */ 207 if (lastslash) { 208 char c[2]; 209 210 if (lastslash - name >= MAXPATHLEN - 2) { 211 error = EPERM; 212 goto done; 213 } 214 215 c[0] = lastslash[1]; 216 c[1] = lastslash[2]; 217 lastslash[1] = '.'; 218 lastslash[2] = '\0'; 219 error = namei_simple_kernel(name, NSM_FOLLOW_NOEMULROOT, &vp); 220 if (error) 221 goto done; 222 if (vp->v_mount == NULL || 223 (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0) 224 error = EPERM; 225 vrele(vp); 226 if (error) 227 goto done; 228 lastslash[1] = c[0]; 229 lastslash[2] = c[1]; 230 } 231 232 pb = pathbuf_create(name); 233 if (pb == NULL) { 234 error = ENOMEM; 235 goto done; 236 } 237 error = vn_open(NULL, pb, 0, O_CREAT | O_NOFOLLOW | FWRITE, 238 S_IRUSR | S_IWUSR, &vp, NULL, NULL); 239 if (error != 0) { 240 pathbuf_destroy(pb); 241 goto done; 242 } 243 pathbuf_destroy(pb); 244 245 /* 246 * Don't dump to: 247 * - non-regular files 248 * - files with links 249 * - files we don't own 250 */ 251 if (vp->v_type != VREG || 252 VOP_GETATTR(vp, &vattr, cred) || vattr.va_nlink != 1 || 253 vattr.va_uid != kauth_cred_geteuid(cred)) { 254 error = EACCES; 255 goto out; 256 } 257 vattr_null(&vattr); 258 vattr.va_size = 0; 259 260 if ((p->p_flag & PK_SUGID) && security_setidcore_dump) { 261 vattr.va_uid = security_setidcore_owner; 262 vattr.va_gid = security_setidcore_group; 263 vattr.va_mode = security_setidcore_mode; 264 } 265 266 VOP_SETATTR(vp, &vattr, cred); 267 p->p_acflag |= ACORE; 268 269 io.io_lwp = l; 270 io.io_vp = vp; 271 io.io_cred = cred; 272 io.io_offset = 0; 273 274 /* Now dump the actual core file. */ 275 error = (*p->p_execsw->es_coredump)(l, &io); 276 out: 277 VOP_UNLOCK(vp); 278 error1 = vn_close(vp, FWRITE, cred); 279 if (error == 0) 280 error = error1; 281 done: 282 if (cred != NULL) 283 kauth_cred_free(cred); 284 if (name != NULL) 285 PNBUF_PUT(name); 286 return error; 287 } 288 289 static int 290 coredump_buildname(struct proc *p, char *dst, const char *src, size_t len) 291 { 292 const char *s; 293 char *d, *end; 294 int i; 295 296 KASSERT(mutex_owned(&proc_lock)); 297 298 for (s = src, d = dst, end = d + len; *s != '\0'; s++) { 299 if (*s == '%') { 300 switch (*(s + 1)) { 301 case 'n': 302 i = snprintf(d, end - d, "%s", p->p_comm); 303 break; 304 case 'p': 305 i = snprintf(d, end - d, "%d", p->p_pid); 306 break; 307 case 'u': 308 i = snprintf(d, end - d, "%.*s", 309 (int)sizeof p->p_pgrp->pg_session->s_login, 310 p->p_pgrp->pg_session->s_login); 311 break; 312 case 't': 313 i = snprintf(d, end - d, "%lld", 314 (long long)p->p_stats->p_start.tv_sec); 315 break; 316 default: 317 goto copy; 318 } 319 d += i; 320 s++; 321 } else { 322 copy: *d = *s; 323 d++; 324 } 325 if (d >= end) 326 return (ENAMETOOLONG); 327 } 328 *d = '\0'; 329 return 0; 330 } 331 332 static int 333 coredump_write(struct coredump_iostate *io, enum uio_seg segflg, 334 const void *data, size_t len) 335 { 336 int error; 337 338 error = vn_rdwr(UIO_WRITE, io->io_vp, __UNCONST(data), len, 339 io->io_offset, segflg, 340 IO_NODELOCKED|IO_UNIT, io->io_cred, NULL, 341 segflg == UIO_USERSPACE ? io->io_lwp : NULL); 342 if (error) { 343 printf("pid %d (%s): %s write of %zu@%p at %lld failed: %d\n", 344 io->io_lwp->l_proc->p_pid, io->io_lwp->l_proc->p_comm, 345 segflg == UIO_USERSPACE ? "user" : "system", 346 len, data, (long long) io->io_offset, error); 347 return (error); 348 } 349 350 io->io_offset += len; 351 return (0); 352 } 353 354 static off_t 355 coredump_offset(struct coredump_iostate *io) 356 { 357 return io->io_offset; 358 } 359