kern_core.c revision 1.3.2.2 1 /* $NetBSD: kern_core.c,v 1.3.2.2 2007/02/26 09:11:05 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.3.2.2 2007/02/26 09:11:05 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 mount *mp;
89 struct coredump_iostate io;
90 int error, error1;
91 char *name = NULL;
92
93 p = l->l_proc;
94 vm = p->p_vmspace;
95
96 rw_enter(&proclist_lock, RW_READER); /* p_session */
97 mutex_enter(&p->p_mutex);
98
99 /*
100 * Make sure the process has not set-id, to prevent data leaks,
101 * unless it was specifically requested to allow set-id coredumps.
102 */
103 if ((p->p_flag & PK_SUGID) && !security_setidcore_dump) {
104 mutex_exit(&p->p_mutex);
105 rw_exit(&proclist_lock);
106 return EPERM;
107 }
108
109 /*
110 * Refuse to core if the data + stack + user size is larger than
111 * the core dump limit. XXX THIS IS WRONG, because of mapped
112 * data.
113 */
114 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
115 p->p_rlimit[RLIMIT_CORE].rlim_cur) {
116 mutex_exit(&p->p_mutex);
117 rw_exit(&proclist_lock);
118 return EFBIG; /* better error code? */
119 }
120
121 /*
122 * It may well not be curproc, so grab a reference to its current
123 * credentials.
124 */
125 kauth_cred_hold(p->p_cred);
126 cred = p->p_cred;
127
128 restart:
129 /*
130 * The core dump will go in the current working directory. Make
131 * sure that the directory is still there and that the mount flags
132 * allow us to write core dumps there.
133 */
134 vp = p->p_cwdi->cwdi_cdir;
135 if (vp->v_mount == NULL ||
136 (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0) {
137 error = EPERM;
138 mutex_exit(&p->p_mutex);
139 rw_exit(&proclist_lock);
140 goto done;
141 }
142
143 if ((p->p_flag & PK_SUGID) && security_setidcore_dump)
144 pattern = security_setidcore_path;
145
146 if (pattern == NULL)
147 pattern = p->p_limit->pl_corename;
148 if (name == NULL) {
149 name = PNBUF_GET();
150 }
151 error = coredump_buildname(p, name, pattern, MAXPATHLEN);
152 mutex_exit(&p->p_mutex);
153 rw_exit(&proclist_lock);
154 if (error)
155 goto done;
156 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, l);
157 if ((error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE,
158 S_IRUSR | S_IWUSR)) != 0)
159 goto done;
160 vp = nd.ni_vp;
161
162 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
163 VOP_UNLOCK(vp, 0);
164 if ((error = vn_close(vp, FWRITE, cred, l)) != 0)
165 goto done;
166 if ((error = vn_start_write(NULL, &mp,
167 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
168 goto done;
169 rw_enter(&proclist_lock, RW_READER); /* p_session */
170 mutex_enter(&p->p_mutex);
171 goto restart;
172 }
173
174 /* Don't dump to non-regular files or files with links. */
175 if (vp->v_type != VREG ||
176 VOP_GETATTR(vp, &vattr, cred, l) || vattr.va_nlink != 1) {
177 error = EINVAL;
178 goto out;
179 }
180 VATTR_NULL(&vattr);
181 vattr.va_size = 0;
182
183 if ((p->p_flag & PK_SUGID) && security_setidcore_dump) {
184 vattr.va_uid = security_setidcore_owner;
185 vattr.va_gid = security_setidcore_group;
186 vattr.va_mode = security_setidcore_mode;
187 }
188
189 VOP_LEASE(vp, l, cred, LEASE_WRITE);
190 VOP_SETATTR(vp, &vattr, cred, l);
191 p->p_acflag |= ACORE;
192
193 io.io_lwp = l;
194 io.io_vp = vp;
195 io.io_cred = cred;
196 io.io_offset = 0;
197
198 /* Now dump the actual core file. */
199 error = (*p->p_execsw->es_coredump)(l, &io);
200 out:
201 VOP_UNLOCK(vp, 0);
202 vn_finished_write(mp, 0);
203 error1 = vn_close(vp, FWRITE, cred, l);
204 if (error == 0)
205 error = error1;
206 done:
207 if (name != NULL)
208 PNBUF_PUT(name);
209 return error;
210 }
211
212 static int
213 coredump_buildname(struct proc *p, char *dst, const char *src, size_t len)
214 {
215 const char *s;
216 char *d, *end;
217 int i;
218
219 LOCK_ASSERT(rw_read_held(&proclist_lock));
220
221 for (s = src, d = dst, end = d + len; *s != '\0'; s++) {
222 if (*s == '%') {
223 switch (*(s + 1)) {
224 case 'n':
225 i = snprintf(d, end - d, "%s", p->p_comm);
226 break;
227 case 'p':
228 i = snprintf(d, end - d, "%d", p->p_pid);
229 break;
230 case 'u':
231 i = snprintf(d, end - d, "%.*s",
232 (int)sizeof p->p_pgrp->pg_session->s_login,
233 p->p_pgrp->pg_session->s_login);
234 break;
235 case 't':
236 i = snprintf(d, end - d, "%ld",
237 p->p_stats->p_start.tv_sec);
238 break;
239 default:
240 goto copy;
241 }
242 d += i;
243 s++;
244 } else {
245 copy: *d = *s;
246 d++;
247 }
248 if (d >= end)
249 return (ENAMETOOLONG);
250 }
251 *d = '\0';
252 return 0;
253 }
254
255 int
256 coredump_write(void *cookie, enum uio_seg segflg, const void *data, size_t len)
257 {
258 struct coredump_iostate *io = cookie;
259 int error;
260
261 error = vn_rdwr(UIO_WRITE, io->io_vp, __UNCONST(data), len,
262 io->io_offset, segflg,
263 IO_NODELOCKED|IO_UNIT, io->io_cred, NULL,
264 segflg == UIO_USERSPACE ? io->io_lwp : NULL);
265 if (error) {
266 printf("pid %d (%s): %s write of %zu@%p at %lld failed: %d\n",
267 io->io_lwp->l_proc->p_pid, io->io_lwp->l_proc->p_comm,
268 segflg == UIO_USERSPACE ? "user" : "system",
269 len, data, (long long) io->io_offset, error);
270 return (error);
271 }
272
273 io->io_offset += len;
274 return (0);
275 }
276
277 #endif /* COREDUMP */
278