1 /* $NetBSD: kern_core.c,v 1.14 2009/01/11 02:45:52 christos Exp $ */
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.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
36 * @(#)kern_sig.c 8.14 (Berkeley) 5/14/95
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: kern_core.c,v 1.14 2009/01/11 02:45:52 christos Exp $");
42 #include <sys/param.h>
43 #include <sys/vnode.h>
44 #include <sys/namei.h>
50 #include <sys/filedesc.h>
51 #include <sys/kauth.h>
52 #include <sys/module.h>
54 MODULE(MODULE_CLASS_MISC
, coredump
, NULL
);
56 struct coredump_iostate
{
63 static int coredump(struct lwp
*, const char *);
64 static int coredump_buildname(struct proc
*, char *, const char *, size_t);
67 coredump_modcmd(modcmd_t cmd
, void *arg
)
72 coredump_vec
= coredump
;
76 * In theory we don't need to patch this, as the various
77 * exec formats depend on this module. If this module has
78 * no references, and so can be unloaded, no user programs
79 * can be running and so nothing can call *coredump_vec.
81 coredump_vec
= (int (*)(struct lwp
*, const char *))enosys
;
89 * Dump core, into a file named "progname.core" or "core" (depending on the
90 * value of shortcorename), unless the process was setuid/setgid.
93 coredump(struct lwp
*l
, const char *pattern
)
101 struct coredump_iostate io
;
111 mutex_enter(proc_lock
); /* p_session */
112 mutex_enter(p
->p_lock
);
115 * Refuse to core if the data + stack + user size is larger than
116 * the core dump limit. XXX THIS IS WRONG, because of mapped
119 if (USPACE
+ ctob(vm
->vm_dsize
+ vm
->vm_ssize
) >=
120 p
->p_rlimit
[RLIMIT_CORE
].rlim_cur
) {
121 error
= EFBIG
; /* better error code? */
122 mutex_exit(p
->p_lock
);
123 mutex_exit(proc_lock
);
128 * It may well not be curproc, so grab a reference to its current
131 kauth_cred_hold(p
->p_cred
);
135 * The core dump will go in the current working directory. Make
136 * sure that the directory is still there and that the mount flags
137 * allow us to write core dumps there.
139 * XXX: this is partially bogus, it should be checking the directory
140 * into which the file is actually written - which probably needs
143 vp
= p
->p_cwdi
->cwdi_cdir
;
144 if (vp
->v_mount
== NULL
||
145 (vp
->v_mount
->mnt_flag
& MNT_NOCOREDUMP
) != 0) {
147 mutex_exit(p
->p_lock
);
148 mutex_exit(proc_lock
);
153 * Make sure the process has not set-id, to prevent data leaks,
154 * unless it was specifically requested to allow set-id coredumps.
156 if (p
->p_flag
& PK_SUGID
) {
157 if (!security_setidcore_dump
) {
159 mutex_exit(p
->p_lock
);
160 mutex_exit(proc_lock
);
163 pattern
= security_setidcore_path
;
166 /* It is (just) possible for p_limit and pl_corename to change */
168 mutex_enter(&lim
->pl_lock
);
170 pattern
= lim
->pl_corename
;
171 error
= coredump_buildname(p
, name
, pattern
, MAXPATHLEN
);
172 mutex_exit(&lim
->pl_lock
);
173 mutex_exit(p
->p_lock
);
174 mutex_exit(proc_lock
);
177 NDINIT(&nd
, LOOKUP
, NOFOLLOW
, UIO_SYSSPACE
, name
);
178 if ((error
= vn_open(&nd
, O_CREAT
| O_NOFOLLOW
| FWRITE
,
179 S_IRUSR
| S_IWUSR
)) != 0)
183 /* Don't dump to non-regular files or files with links. */
184 if (vp
->v_type
!= VREG
||
185 VOP_GETATTR(vp
, &vattr
, cred
) || vattr
.va_nlink
!= 1) {
192 if ((p
->p_flag
& PK_SUGID
) && security_setidcore_dump
) {
193 vattr
.va_uid
= security_setidcore_owner
;
194 vattr
.va_gid
= security_setidcore_group
;
195 vattr
.va_mode
= security_setidcore_mode
;
198 VOP_SETATTR(vp
, &vattr
, cred
);
199 p
->p_acflag
|= ACORE
;
206 /* Now dump the actual core file. */
207 error
= (*p
->p_execsw
->es_coredump
)(l
, &io
);
210 error1
= vn_close(vp
, FWRITE
, cred
);
220 coredump_buildname(struct proc
*p
, char *dst
, const char *src
, size_t len
)
226 KASSERT(mutex_owned(proc_lock
));
228 for (s
= src
, d
= dst
, end
= d
+ len
; *s
!= '\0'; s
++) {
232 i
= snprintf(d
, end
- d
, "%s", p
->p_comm
);
235 i
= snprintf(d
, end
- d
, "%d", p
->p_pid
);
238 i
= snprintf(d
, end
- d
, "%.*s",
239 (int)sizeof p
->p_pgrp
->pg_session
->s_login
,
240 p
->p_pgrp
->pg_session
->s_login
);
243 i
= snprintf(d
, end
- d
, "%lld",
244 (long long)p
->p_stats
->p_start
.tv_sec
);
256 return (ENAMETOOLONG
);
263 coredump_write(void *cookie
, enum uio_seg segflg
, const void *data
, size_t len
)
265 struct coredump_iostate
*io
= cookie
;
268 error
= vn_rdwr(UIO_WRITE
, io
->io_vp
, __UNCONST(data
), len
,
269 io
->io_offset
, segflg
,
270 IO_NODELOCKED
|IO_UNIT
, io
->io_cred
, NULL
,
271 segflg
== UIO_USERSPACE
? io
->io_lwp
: NULL
);
273 printf("pid %d (%s): %s write of %zu@%p at %lld failed: %d\n",
274 io
->io_lwp
->l_proc
->p_pid
, io
->io_lwp
->l_proc
->p_comm
,
275 segflg
== UIO_USERSPACE
? "user" : "system",
276 len
, data
, (long long) io
->io_offset
, error
);
280 io
->io_offset
+= len
;