kern_core.c revision 1.2.2.3 1 /* $NetBSD: kern_core.c,v 1.2.2.3 2007/04/15 16:03:48 yamt 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.2.2.3 2007/04/15 16:03:48 yamt Exp $");
41
42 #include "opt_coredump.h"
43
44 #include <sys/param.h>
45 #include <sys/vnode.h>
46 #include <sys/namei.h>
47 #include <sys/acct.h>
48 #include <sys/file.h>
49 #include <sys/stat.h>
50 #include <sys/proc.h>
51 #include <sys/exec.h>
52 #include <sys/filedesc.h>
53 #include <sys/kauth.h>
54
55 #if !defined(COREDUMP)
56
57 int
58 coredump(struct lwp *l, const char *pattern)
59 {
60
61 return (ENOSYS);
62 }
63
64 #else /* COREDUMP */
65
66 struct coredump_iostate {
67 struct lwp *io_lwp;
68 struct vnode *io_vp;
69 kauth_cred_t io_cred;
70 off_t io_offset;
71 };
72
73 static int coredump_buildname(struct proc *, char *, const char *, size_t);
74
75 /*
76 * Dump core, into a file named "progname.core" or "core" (depending on the
77 * value of shortcorename), unless the process was setuid/setgid.
78 */
79 int
80 coredump(struct lwp *l, const char *pattern)
81 {
82 struct vnode *vp;
83 struct proc *p;
84 struct vmspace *vm;
85 kauth_cred_t cred;
86 struct nameidata nd;
87 struct vattr vattr;
88 struct coredump_iostate io;
89 int error, error1;
90 char *name = NULL;
91
92 p = l->l_proc;
93 vm = p->p_vmspace;
94
95 mutex_enter(&proclist_lock); /* p_session */
96 mutex_enter(&p->p_mutex);
97
98 /*
99 * Make sure the process has not set-id, to prevent data leaks,
100 * unless it was specifically requested to allow set-id coredumps.
101 */
102 if ((p->p_flag & PK_SUGID) && !security_setidcore_dump) {
103 mutex_exit(&p->p_mutex);
104 mutex_exit(&proclist_lock);
105 return EPERM;
106 }
107
108 /*
109 * Refuse to core if the data + stack + user size is larger than
110 * the core dump limit. XXX THIS IS WRONG, because of mapped
111 * data.
112 */
113 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
114 p->p_rlimit[RLIMIT_CORE].rlim_cur) {
115 mutex_exit(&p->p_mutex);
116 mutex_exit(&proclist_lock);
117 return EFBIG; /* better error code? */
118 }
119
120 /*
121 * It may well not be curproc, so grab a reference to its current
122 * credentials.
123 */
124 kauth_cred_hold(p->p_cred);
125 cred = p->p_cred;
126
127 /*
128 * The core dump will go in the current working directory. Make
129 * sure that the directory is still there and that the mount flags
130 * allow us to write core dumps there.
131 */
132 vp = p->p_cwdi->cwdi_cdir;
133 if (vp->v_mount == NULL ||
134 (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0) {
135 error = EPERM;
136 mutex_exit(&p->p_mutex);
137 mutex_exit(&proclist_lock);
138 goto done;
139 }
140
141 if ((p->p_flag & PK_SUGID) && security_setidcore_dump)
142 pattern = security_setidcore_path;
143
144 if (pattern == NULL)
145 pattern = p->p_limit->pl_corename;
146 if (name == NULL) {
147 name = PNBUF_GET();
148 }
149 error = coredump_buildname(p, name, pattern, MAXPATHLEN);
150 mutex_exit(&p->p_mutex);
151 mutex_exit(&proclist_lock);
152 if (error)
153 goto done;
154 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, l);
155 if ((error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE,
156 S_IRUSR | S_IWUSR)) != 0)
157 goto done;
158 vp = nd.ni_vp;
159
160 /* Don't dump to non-regular files or files with links. */
161 if (vp->v_type != VREG ||
162 VOP_GETATTR(vp, &vattr, cred, l) || vattr.va_nlink != 1) {
163 error = EINVAL;
164 goto out;
165 }
166 VATTR_NULL(&vattr);
167 vattr.va_size = 0;
168
169 if ((p->p_flag & PK_SUGID) && security_setidcore_dump) {
170 vattr.va_uid = security_setidcore_owner;
171 vattr.va_gid = security_setidcore_group;
172 vattr.va_mode = security_setidcore_mode;
173 }
174
175 VOP_LEASE(vp, l, cred, LEASE_WRITE);
176 VOP_SETATTR(vp, &vattr, cred, l);
177 p->p_acflag |= ACORE;
178
179 io.io_lwp = l;
180 io.io_vp = vp;
181 io.io_cred = cred;
182 io.io_offset = 0;
183
184 /* Now dump the actual core file. */
185 error = (*p->p_execsw->es_coredump)(l, &io);
186 out:
187 VOP_UNLOCK(vp, 0);
188 error1 = vn_close(vp, FWRITE, cred, l);
189 if (error == 0)
190 error = error1;
191 done:
192 if (name != NULL)
193 PNBUF_PUT(name);
194 return error;
195 }
196
197 static int
198 coredump_buildname(struct proc *p, char *dst, const char *src, size_t len)
199 {
200 const char *s;
201 char *d, *end;
202 int i;
203
204 KASSERT(mutex_owned(&proclist_lock));
205
206 for (s = src, d = dst, end = d + len; *s != '\0'; s++) {
207 if (*s == '%') {
208 switch (*(s + 1)) {
209 case 'n':
210 i = snprintf(d, end - d, "%s", p->p_comm);
211 break;
212 case 'p':
213 i = snprintf(d, end - d, "%d", p->p_pid);
214 break;
215 case 'u':
216 i = snprintf(d, end - d, "%.*s",
217 (int)sizeof p->p_pgrp->pg_session->s_login,
218 p->p_pgrp->pg_session->s_login);
219 break;
220 case 't':
221 i = snprintf(d, end - d, "%ld",
222 p->p_stats->p_start.tv_sec);
223 break;
224 default:
225 goto copy;
226 }
227 d += i;
228 s++;
229 } else {
230 copy: *d = *s;
231 d++;
232 }
233 if (d >= end)
234 return (ENAMETOOLONG);
235 }
236 *d = '\0';
237 return 0;
238 }
239
240 int
241 coredump_write(void *cookie, enum uio_seg segflg, const void *data, size_t len)
242 {
243 struct coredump_iostate *io = cookie;
244 int error;
245
246 error = vn_rdwr(UIO_WRITE, io->io_vp, __UNCONST(data), len,
247 io->io_offset, segflg,
248 IO_NODELOCKED|IO_UNIT, io->io_cred, NULL,
249 segflg == UIO_USERSPACE ? io->io_lwp : NULL);
250 if (error) {
251 printf("pid %d (%s): %s write of %zu@%p at %lld failed: %d\n",
252 io->io_lwp->l_proc->p_pid, io->io_lwp->l_proc->p_comm,
253 segflg == UIO_USERSPACE ? "user" : "system",
254 len, data, (long long) io->io_offset, error);
255 return (error);
256 }
257
258 io->io_offset += len;
259 return (0);
260 }
261
262 #endif /* COREDUMP */
263