| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: kern_ktrace.c,v 1.149 2009/08/05 19:53:42 dsl Exp $ */ | | 1 | /* $NetBSD: kern_ktrace.c,v 1.150 2009/10/02 21:47:35 elad Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Andrew Doran. | | 8 | * by Andrew Doran. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
| @@ -51,27 +51,27 @@ | | | @@ -51,27 +51,27 @@ |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | | 51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | | 52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | | 55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | | 56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
58 | * SUCH DAMAGE. | | 58 | * SUCH DAMAGE. |
59 | * | | 59 | * |
60 | * @(#)kern_ktrace.c 8.5 (Berkeley) 5/14/95 | | 60 | * @(#)kern_ktrace.c 8.5 (Berkeley) 5/14/95 |
61 | */ | | 61 | */ |
62 | | | 62 | |
63 | #include <sys/cdefs.h> | | 63 | #include <sys/cdefs.h> |
64 | __KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.149 2009/08/05 19:53:42 dsl Exp $"); | | 64 | __KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.150 2009/10/02 21:47:35 elad Exp $"); |
65 | | | 65 | |
66 | #include <sys/param.h> | | 66 | #include <sys/param.h> |
67 | #include <sys/systm.h> | | 67 | #include <sys/systm.h> |
68 | #include <sys/proc.h> | | 68 | #include <sys/proc.h> |
69 | #include <sys/file.h> | | 69 | #include <sys/file.h> |
70 | #include <sys/namei.h> | | 70 | #include <sys/namei.h> |
71 | #include <sys/vnode.h> | | 71 | #include <sys/vnode.h> |
72 | #include <sys/kernel.h> | | 72 | #include <sys/kernel.h> |
73 | #include <sys/kthread.h> | | 73 | #include <sys/kthread.h> |
74 | #include <sys/ktrace.h> | | 74 | #include <sys/ktrace.h> |
75 | #include <sys/kmem.h> | | 75 | #include <sys/kmem.h> |
76 | #include <sys/syslog.h> | | 76 | #include <sys/syslog.h> |
77 | #include <sys/filedesc.h> | | 77 | #include <sys/filedesc.h> |
| @@ -169,26 +169,28 @@ static int ktrderefall(struct ktr_desc * | | | @@ -169,26 +169,28 @@ static int ktrderefall(struct ktr_desc * |
169 | * Patchable variables. | | 169 | * Patchable variables. |
170 | */ | | 170 | */ |
171 | int ktd_maxentry = KTD_MAXENTRY; /* max # of entry in the queue */ | | 171 | int ktd_maxentry = KTD_MAXENTRY; /* max # of entry in the queue */ |
172 | int ktd_timeout = KTD_TIMEOUT; /* timeout in seconds */ | | 172 | int ktd_timeout = KTD_TIMEOUT; /* timeout in seconds */ |
173 | int ktd_delayqcnt = KTD_DELAYQCNT; /* # of entry allowed to delay */ | | 173 | int ktd_delayqcnt = KTD_DELAYQCNT; /* # of entry allowed to delay */ |
174 | int ktd_wakedelay = KTD_WAKEDELAY; /* delay of wakeup in *ms* */ | | 174 | int ktd_wakedelay = KTD_WAKEDELAY; /* delay of wakeup in *ms* */ |
175 | int ktd_intrwakdl = KTD_INTRWAKDL; /* ditto, but when interactive */ | | 175 | int ktd_intrwakdl = KTD_INTRWAKDL; /* ditto, but when interactive */ |
176 | | | 176 | |
177 | kmutex_t ktrace_lock; | | 177 | kmutex_t ktrace_lock; |
178 | int ktrace_on; | | 178 | int ktrace_on; |
179 | static TAILQ_HEAD(, ktr_desc) ktdq = TAILQ_HEAD_INITIALIZER(ktdq); | | 179 | static TAILQ_HEAD(, ktr_desc) ktdq = TAILQ_HEAD_INITIALIZER(ktdq); |
180 | static pool_cache_t kte_cache; | | 180 | static pool_cache_t kte_cache; |
181 | | | 181 | |
| | | 182 | static kauth_listener_t ktrace_listener; |
| | | 183 | |
182 | static void | | 184 | static void |
183 | ktd_wakeup(struct ktr_desc *ktd) | | 185 | ktd_wakeup(struct ktr_desc *ktd) |
184 | { | | 186 | { |
185 | | | 187 | |
186 | callout_stop(&ktd->ktd_wakch); | | 188 | callout_stop(&ktd->ktd_wakch); |
187 | cv_signal(&ktd->ktd_cv); | | 189 | cv_signal(&ktd->ktd_cv); |
188 | } | | 190 | } |
189 | | | 191 | |
190 | static void | | 192 | static void |
191 | ktd_callout(void *arg) | | 193 | ktd_callout(void *arg) |
192 | { | | 194 | { |
193 | | | 195 | |
194 | mutex_enter(&ktrace_lock); | | 196 | mutex_enter(&ktrace_lock); |
| @@ -227,36 +229,72 @@ ktrenter(lwp_t *l) | | | @@ -227,36 +229,72 @@ ktrenter(lwp_t *l) |
227 | if ((l->l_pflag & LP_KTRACTIVE) != 0) | | 229 | if ((l->l_pflag & LP_KTRACTIVE) != 0) |
228 | return 1; | | 230 | return 1; |
229 | l->l_pflag |= LP_KTRACTIVE; | | 231 | l->l_pflag |= LP_KTRACTIVE; |
230 | return 0; | | 232 | return 0; |
231 | } | | 233 | } |
232 | | | 234 | |
233 | static inline void | | 235 | static inline void |
234 | ktrexit(lwp_t *l) | | 236 | ktrexit(lwp_t *l) |
235 | { | | 237 | { |
236 | | | 238 | |
237 | l->l_pflag &= ~LP_KTRACTIVE; | | 239 | l->l_pflag &= ~LP_KTRACTIVE; |
238 | } | | 240 | } |
239 | | | 241 | |
| | | 242 | static int |
| | | 243 | ktrace_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie, |
| | | 244 | void *arg0, void *arg1, void *arg2, void *arg3) |
| | | 245 | { |
| | | 246 | struct proc *p; |
| | | 247 | int result; |
| | | 248 | enum kauth_process_req req; |
| | | 249 | |
| | | 250 | result = KAUTH_RESULT_DEFER; |
| | | 251 | p = arg0; |
| | | 252 | |
| | | 253 | if (action != KAUTH_PROCESS_KTRACE) |
| | | 254 | return result; |
| | | 255 | |
| | | 256 | req = (enum kauth_process_req)(unsigned long)arg1; |
| | | 257 | |
| | | 258 | /* Privileged; secmodel should handle these. */ |
| | | 259 | if (req == KAUTH_REQ_PROCESS_KTRACE_PERSISTENT) |
| | | 260 | return result; |
| | | 261 | |
| | | 262 | if ((p->p_traceflag & KTRFAC_PERSISTENT) || |
| | | 263 | (p->p_flag & PK_SUGID)) |
| | | 264 | return result; |
| | | 265 | |
| | | 266 | if (kauth_cred_geteuid(cred) == kauth_cred_getuid(p->p_cred) && |
| | | 267 | kauth_cred_getuid(cred) == kauth_cred_getsvuid(p->p_cred) && |
| | | 268 | kauth_cred_getgid(cred) == kauth_cred_getgid(p->p_cred) && |
| | | 269 | kauth_cred_getgid(cred) == kauth_cred_getsvgid(p->p_cred)) |
| | | 270 | result = KAUTH_RESULT_ALLOW; |
| | | 271 | |
| | | 272 | return result; |
| | | 273 | } |
| | | 274 | |
240 | /* | | 275 | /* |
241 | * Initialise the ktrace system. | | 276 | * Initialise the ktrace system. |
242 | */ | | 277 | */ |
243 | void | | 278 | void |
244 | ktrinit(void) | | 279 | ktrinit(void) |
245 | { | | 280 | { |
246 | | | 281 | |
247 | mutex_init(&ktrace_lock, MUTEX_DEFAULT, IPL_NONE); | | 282 | mutex_init(&ktrace_lock, MUTEX_DEFAULT, IPL_NONE); |
248 | kte_cache = pool_cache_init(sizeof(struct ktrace_entry), 0, 0, 0, | | 283 | kte_cache = pool_cache_init(sizeof(struct ktrace_entry), 0, 0, 0, |
249 | "ktrace", &pool_allocator_nointr, IPL_NONE, NULL, NULL, NULL); | | 284 | "ktrace", &pool_allocator_nointr, IPL_NONE, NULL, NULL, NULL); |
| | | 285 | |
| | | 286 | ktrace_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS, |
| | | 287 | ktrace_listener_cb, NULL); |
250 | } | | 288 | } |
251 | | | 289 | |
252 | /* | | 290 | /* |
253 | * Release a reference. Called with ktrace_lock held. | | 291 | * Release a reference. Called with ktrace_lock held. |
254 | */ | | 292 | */ |
255 | void | | 293 | void |
256 | ktdrel(struct ktr_desc *ktd) | | 294 | ktdrel(struct ktr_desc *ktd) |
257 | { | | 295 | { |
258 | | | 296 | |
259 | KASSERT(mutex_owned(&ktrace_lock)); | | 297 | KASSERT(mutex_owned(&ktrace_lock)); |
260 | | | 298 | |
261 | KDASSERT(ktd->ktd_ref != 0); | | 299 | KDASSERT(ktd->ktd_ref != 0); |
262 | KASSERT(ktd->ktd_ref > 0); | | 300 | KASSERT(ktd->ktd_ref > 0); |