The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_core.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: kern_core.c,v 1.37 2022/09/10 07:30:41 mrg 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.37 2022/09/10 07:30:41 mrg 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 = NULL;
  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 = NULL /* XXXgcc */;
  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                 goto release;
  149         }
  150 
  151         /*
  152          * It may well not be curproc, so grab a reference to its current
  153          * credentials.
  154          */
  155         kauth_cred_hold(p->p_cred);
  156         cred = 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
  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
  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
  354 coredump_offset(struct coredump_iostate *io)
  355 {
  356         return io->io_offset;
  357 }

Cache object: b39f110e8e7052aee5c123e91bbcf6c7


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.