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