1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California.
6 * Copyright (c) 2005 Robert N. M. Watson
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)kern_ktrace.c 8.2 (Berkeley) 9/23/93
34 */
35
36 #include <sys/cdefs.h>
37 #include "opt_ktrace.h"
38
39 #include <sys/param.h>
40 #include <sys/capsicum.h>
41 #include <sys/systm.h>
42 #include <sys/fcntl.h>
43 #include <sys/kernel.h>
44 #include <sys/kthread.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/namei.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/resourcevar.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
55 #include <sys/socket.h>
56 #include <sys/stat.h>
57 #include <sys/ktrace.h>
58 #include <sys/sx.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysent.h>
61 #include <sys/syslog.h>
62 #include <sys/sysproto.h>
63
64 #include <security/mac/mac_framework.h>
65
66 /*
67 * The ktrace facility allows the tracing of certain key events in user space
68 * processes, such as system calls, signal delivery, context switches, and
69 * user generated events using utrace(2). It works by streaming event
70 * records and data to a vnode associated with the process using the
71 * ktrace(2) system call. In general, records can be written directly from
72 * the context that generates the event. One important exception to this is
73 * during a context switch, where sleeping is not permitted. To handle this
74 * case, trace events are generated using in-kernel ktr_request records, and
75 * then delivered to disk at a convenient moment -- either immediately, the
76 * next traceable event, at system call return, or at process exit.
77 *
78 * When dealing with multiple threads or processes writing to the same event
79 * log, ordering guarantees are weak: specifically, if an event has multiple
80 * records (i.e., system call enter and return), they may be interlaced with
81 * records from another event. Process and thread ID information is provided
82 * in the record, and user applications can de-interlace events if required.
83 */
84
85 static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
86
87 #ifdef KTRACE
88
89 FEATURE(ktrace, "Kernel support for system-call tracing");
90
91 #ifndef KTRACE_REQUEST_POOL
92 #define KTRACE_REQUEST_POOL 100
93 #endif
94
95 struct ktr_request {
96 struct ktr_header ktr_header;
97 void *ktr_buffer;
98 union {
99 struct ktr_proc_ctor ktr_proc_ctor;
100 struct ktr_cap_fail ktr_cap_fail;
101 struct ktr_syscall ktr_syscall;
102 struct ktr_sysret ktr_sysret;
103 struct ktr_genio ktr_genio;
104 struct ktr_psig ktr_psig;
105 struct ktr_csw ktr_csw;
106 struct ktr_fault ktr_fault;
107 struct ktr_faultend ktr_faultend;
108 struct ktr_struct_array ktr_struct_array;
109 } ktr_data;
110 STAILQ_ENTRY(ktr_request) ktr_list;
111 };
112
113 static const int data_lengths[] = {
114 [KTR_SYSCALL] = offsetof(struct ktr_syscall, ktr_args),
115 [KTR_SYSRET] = sizeof(struct ktr_sysret),
116 [KTR_NAMEI] = 0,
117 [KTR_GENIO] = sizeof(struct ktr_genio),
118 [KTR_PSIG] = sizeof(struct ktr_psig),
119 [KTR_CSW] = sizeof(struct ktr_csw),
120 [KTR_USER] = 0,
121 [KTR_STRUCT] = 0,
122 [KTR_SYSCTL] = 0,
123 [KTR_PROCCTOR] = sizeof(struct ktr_proc_ctor),
124 [KTR_PROCDTOR] = 0,
125 [KTR_CAPFAIL] = sizeof(struct ktr_cap_fail),
126 [KTR_FAULT] = sizeof(struct ktr_fault),
127 [KTR_FAULTEND] = sizeof(struct ktr_faultend),
128 [KTR_STRUCT_ARRAY] = sizeof(struct ktr_struct_array),
129 };
130
131 static STAILQ_HEAD(, ktr_request) ktr_free;
132
133 static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
134 "KTRACE options");
135
136 static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
137 TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
138
139 u_int ktr_geniosize = PAGE_SIZE;
140 SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RWTUN, &ktr_geniosize,
141 0, "Maximum size of genio event payload");
142
143 /*
144 * Allow to not to send signal to traced process, in which context the
145 * ktr record is written. The limit is applied from the process that
146 * set up ktrace, so killing the traced process is not completely fair.
147 */
148 int ktr_filesize_limit_signal = 0;
149 SYSCTL_INT(_kern_ktrace, OID_AUTO, filesize_limit_signal, CTLFLAG_RWTUN,
150 &ktr_filesize_limit_signal, 0,
151 "Send SIGXFSZ to the traced process when the log size limit is exceeded");
152
153 static int print_message = 1;
154 static struct mtx ktrace_mtx;
155 static struct sx ktrace_sx;
156
157 struct ktr_io_params {
158 struct vnode *vp;
159 struct ucred *cr;
160 off_t lim;
161 u_int refs;
162 };
163
164 static void ktrace_init(void *dummy);
165 static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
166 static u_int ktrace_resize_pool(u_int oldsize, u_int newsize);
167 static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type);
168 static struct ktr_request *ktr_getrequest(int type);
169 static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
170 static struct ktr_io_params *ktr_freeproc(struct proc *p);
171 static void ktr_freerequest(struct ktr_request *req);
172 static void ktr_freerequest_locked(struct ktr_request *req);
173 static void ktr_writerequest(struct thread *td, struct ktr_request *req);
174 static int ktrcanset(struct thread *,struct proc *);
175 static int ktrsetchildren(struct thread *, struct proc *, int, int,
176 struct ktr_io_params *);
177 static int ktrops(struct thread *, struct proc *, int, int,
178 struct ktr_io_params *);
179 static void ktrprocctor_entered(struct thread *, struct proc *);
180
181 /*
182 * ktrace itself generates events, such as context switches, which we do not
183 * wish to trace. Maintain a flag, TDP_INKTRACE, on each thread to determine
184 * whether or not it is in a region where tracing of events should be
185 * suppressed.
186 */
187 static void
ktrace_enter(struct thread * td)188 ktrace_enter(struct thread *td)
189 {
190
191 KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
192 td->td_pflags |= TDP_INKTRACE;
193 }
194
195 static void
ktrace_exit(struct thread * td)196 ktrace_exit(struct thread *td)
197 {
198
199 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
200 td->td_pflags &= ~TDP_INKTRACE;
201 }
202
203 static void
ktrace_assert(struct thread * td)204 ktrace_assert(struct thread *td)
205 {
206
207 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
208 }
209
210 static void
ast_ktrace(struct thread * td,int tda __unused)211 ast_ktrace(struct thread *td, int tda __unused)
212 {
213 KTRUSERRET(td);
214 }
215
216 static void
ktrace_init(void * dummy)217 ktrace_init(void *dummy)
218 {
219 struct ktr_request *req;
220 int i;
221
222 mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
223 sx_init(&ktrace_sx, "ktrace_sx");
224 STAILQ_INIT(&ktr_free);
225 for (i = 0; i < ktr_requestpool; i++) {
226 req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK |
227 M_ZERO);
228 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
229 }
230 ast_register(TDA_KTRACE, ASTR_ASTF_REQUIRED, 0, ast_ktrace);
231 }
232 SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
233
234 static int
sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)235 sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
236 {
237 struct thread *td;
238 u_int newsize, oldsize, wantsize;
239 int error;
240
241 /* Handle easy read-only case first to avoid warnings from GCC. */
242 if (!req->newptr) {
243 oldsize = ktr_requestpool;
244 return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
245 }
246
247 error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
248 if (error)
249 return (error);
250 td = curthread;
251 ktrace_enter(td);
252 oldsize = ktr_requestpool;
253 newsize = ktrace_resize_pool(oldsize, wantsize);
254 ktrace_exit(td);
255 error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
256 if (error)
257 return (error);
258 if (wantsize > oldsize && newsize < wantsize)
259 return (ENOSPC);
260 return (0);
261 }
262 SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool,
263 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &ktr_requestpool, 0,
264 sysctl_kern_ktrace_request_pool, "IU",
265 "Pool buffer size for ktrace(1)");
266
267 static u_int
ktrace_resize_pool(u_int oldsize,u_int newsize)268 ktrace_resize_pool(u_int oldsize, u_int newsize)
269 {
270 STAILQ_HEAD(, ktr_request) ktr_new;
271 struct ktr_request *req;
272 int bound;
273
274 print_message = 1;
275 bound = newsize - oldsize;
276 if (bound == 0)
277 return (ktr_requestpool);
278 if (bound < 0) {
279 mtx_lock(&ktrace_mtx);
280 /* Shrink pool down to newsize if possible. */
281 while (bound++ < 0) {
282 req = STAILQ_FIRST(&ktr_free);
283 if (req == NULL)
284 break;
285 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
286 ktr_requestpool--;
287 free(req, M_KTRACE);
288 }
289 } else {
290 /* Grow pool up to newsize. */
291 STAILQ_INIT(&ktr_new);
292 while (bound-- > 0) {
293 req = malloc(sizeof(struct ktr_request), M_KTRACE,
294 M_WAITOK | M_ZERO);
295 STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
296 }
297 mtx_lock(&ktrace_mtx);
298 STAILQ_CONCAT(&ktr_free, &ktr_new);
299 ktr_requestpool += (newsize - oldsize);
300 }
301 mtx_unlock(&ktrace_mtx);
302 return (ktr_requestpool);
303 }
304
305 /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
306 CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
307 (sizeof((struct thread *)NULL)->td_name));
308
309 static struct ktr_request *
ktr_getrequest_entered(struct thread * td,int type)310 ktr_getrequest_entered(struct thread *td, int type)
311 {
312 struct ktr_request *req;
313 struct proc *p = td->td_proc;
314 int pm;
315
316 mtx_lock(&ktrace_mtx);
317 if (!KTRCHECK(td, type)) {
318 mtx_unlock(&ktrace_mtx);
319 return (NULL);
320 }
321 req = STAILQ_FIRST(&ktr_free);
322 if (req != NULL) {
323 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
324 req->ktr_header.ktr_type = type;
325 if (p->p_traceflag & KTRFAC_DROP) {
326 req->ktr_header.ktr_type |= KTR_DROP;
327 p->p_traceflag &= ~KTRFAC_DROP;
328 }
329 mtx_unlock(&ktrace_mtx);
330 nanotime(&req->ktr_header.ktr_time);
331 req->ktr_header.ktr_type |= KTR_VERSIONED;
332 req->ktr_header.ktr_pid = p->p_pid;
333 req->ktr_header.ktr_tid = td->td_tid;
334 req->ktr_header.ktr_cpu = PCPU_GET(cpuid);
335 req->ktr_header.ktr_version = KTR_VERSION1;
336 bcopy(td->td_name, req->ktr_header.ktr_comm,
337 sizeof(req->ktr_header.ktr_comm));
338 req->ktr_buffer = NULL;
339 req->ktr_header.ktr_len = 0;
340 } else {
341 p->p_traceflag |= KTRFAC_DROP;
342 pm = print_message;
343 print_message = 0;
344 mtx_unlock(&ktrace_mtx);
345 if (pm)
346 printf("Out of ktrace request objects.\n");
347 }
348 return (req);
349 }
350
351 static struct ktr_request *
ktr_getrequest(int type)352 ktr_getrequest(int type)
353 {
354 struct thread *td = curthread;
355 struct ktr_request *req;
356
357 ktrace_enter(td);
358 req = ktr_getrequest_entered(td, type);
359 if (req == NULL)
360 ktrace_exit(td);
361
362 return (req);
363 }
364
365 /*
366 * Some trace generation environments don't permit direct access to VFS,
367 * such as during a context switch where sleeping is not allowed. Under these
368 * circumstances, queue a request to the thread to be written asynchronously
369 * later.
370 */
371 static void
ktr_enqueuerequest(struct thread * td,struct ktr_request * req)372 ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
373 {
374
375 mtx_lock(&ktrace_mtx);
376 STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
377 mtx_unlock(&ktrace_mtx);
378 ast_sched(td, TDA_KTRACE);
379 }
380
381 /*
382 * Drain any pending ktrace records from the per-thread queue to disk. This
383 * is used both internally before committing other records, and also on
384 * system call return. We drain all the ones we can find at the time when
385 * drain is requested, but don't keep draining after that as those events
386 * may be approximately "after" the current event.
387 */
388 static void
ktr_drain(struct thread * td)389 ktr_drain(struct thread *td)
390 {
391 struct ktr_request *queued_req;
392 STAILQ_HEAD(, ktr_request) local_queue;
393
394 ktrace_assert(td);
395 sx_assert(&ktrace_sx, SX_XLOCKED);
396
397 STAILQ_INIT(&local_queue);
398
399 if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
400 mtx_lock(&ktrace_mtx);
401 STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
402 mtx_unlock(&ktrace_mtx);
403
404 while ((queued_req = STAILQ_FIRST(&local_queue))) {
405 STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
406 ktr_writerequest(td, queued_req);
407 ktr_freerequest(queued_req);
408 }
409 }
410 }
411
412 /*
413 * Submit a trace record for immediate commit to disk -- to be used only
414 * where entering VFS is OK. First drain any pending records that may have
415 * been cached in the thread.
416 */
417 static void
ktr_submitrequest(struct thread * td,struct ktr_request * req)418 ktr_submitrequest(struct thread *td, struct ktr_request *req)
419 {
420
421 ktrace_assert(td);
422
423 sx_xlock(&ktrace_sx);
424 ktr_drain(td);
425 ktr_writerequest(td, req);
426 ktr_freerequest(req);
427 sx_xunlock(&ktrace_sx);
428 ktrace_exit(td);
429 }
430
431 static void
ktr_freerequest(struct ktr_request * req)432 ktr_freerequest(struct ktr_request *req)
433 {
434
435 mtx_lock(&ktrace_mtx);
436 ktr_freerequest_locked(req);
437 mtx_unlock(&ktrace_mtx);
438 }
439
440 static void
ktr_freerequest_locked(struct ktr_request * req)441 ktr_freerequest_locked(struct ktr_request *req)
442 {
443
444 mtx_assert(&ktrace_mtx, MA_OWNED);
445 if (req->ktr_buffer != NULL)
446 free(req->ktr_buffer, M_KTRACE);
447 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
448 }
449
450 static void
ktr_io_params_ref(struct ktr_io_params * kiop)451 ktr_io_params_ref(struct ktr_io_params *kiop)
452 {
453 mtx_assert(&ktrace_mtx, MA_OWNED);
454 kiop->refs++;
455 }
456
457 static struct ktr_io_params *
ktr_io_params_rele(struct ktr_io_params * kiop)458 ktr_io_params_rele(struct ktr_io_params *kiop)
459 {
460 mtx_assert(&ktrace_mtx, MA_OWNED);
461 if (kiop == NULL)
462 return (NULL);
463 KASSERT(kiop->refs > 0, ("kiop ref == 0 %p", kiop));
464 return (--(kiop->refs) == 0 ? kiop : NULL);
465 }
466
467 void
ktr_io_params_free(struct ktr_io_params * kiop)468 ktr_io_params_free(struct ktr_io_params *kiop)
469 {
470 if (kiop == NULL)
471 return;
472
473 MPASS(kiop->refs == 0);
474 vn_close(kiop->vp, FWRITE, kiop->cr, curthread);
475 crfree(kiop->cr);
476 free(kiop, M_KTRACE);
477 }
478
479 static struct ktr_io_params *
ktr_io_params_alloc(struct thread * td,struct vnode * vp)480 ktr_io_params_alloc(struct thread *td, struct vnode *vp)
481 {
482 struct ktr_io_params *res;
483
484 res = malloc(sizeof(struct ktr_io_params), M_KTRACE, M_WAITOK);
485 res->vp = vp;
486 res->cr = crhold(td->td_ucred);
487 res->lim = lim_cur(td, RLIMIT_FSIZE);
488 res->refs = 1;
489 return (res);
490 }
491
492 /*
493 * Disable tracing for a process and release all associated resources.
494 * The caller is responsible for releasing a reference on the returned
495 * vnode and credentials.
496 */
497 static struct ktr_io_params *
ktr_freeproc(struct proc * p)498 ktr_freeproc(struct proc *p)
499 {
500 struct ktr_io_params *kiop;
501 struct ktr_request *req;
502
503 PROC_LOCK_ASSERT(p, MA_OWNED);
504 mtx_assert(&ktrace_mtx, MA_OWNED);
505 kiop = ktr_io_params_rele(p->p_ktrioparms);
506 p->p_ktrioparms = NULL;
507 p->p_traceflag = 0;
508 while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
509 STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
510 ktr_freerequest_locked(req);
511 }
512 return (kiop);
513 }
514
515 struct vnode *
ktr_get_tracevp(struct proc * p,bool ref)516 ktr_get_tracevp(struct proc *p, bool ref)
517 {
518 struct vnode *vp;
519
520 PROC_LOCK_ASSERT(p, MA_OWNED);
521
522 if (p->p_ktrioparms != NULL) {
523 vp = p->p_ktrioparms->vp;
524 if (ref)
525 vrefact(vp);
526 } else {
527 vp = NULL;
528 }
529 return (vp);
530 }
531
532 void
ktrsyscall(int code,int narg,syscallarg_t args[])533 ktrsyscall(int code, int narg, syscallarg_t args[])
534 {
535 struct ktr_request *req;
536 struct ktr_syscall *ktp;
537 size_t buflen;
538 char *buf = NULL;
539
540 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
541 return;
542
543 buflen = sizeof(register_t) * narg;
544 if (buflen > 0) {
545 buf = malloc(buflen, M_KTRACE, M_WAITOK);
546 bcopy(args, buf, buflen);
547 }
548 req = ktr_getrequest(KTR_SYSCALL);
549 if (req == NULL) {
550 if (buf != NULL)
551 free(buf, M_KTRACE);
552 return;
553 }
554 ktp = &req->ktr_data.ktr_syscall;
555 ktp->ktr_code = code;
556 ktp->ktr_narg = narg;
557 if (buflen > 0) {
558 req->ktr_header.ktr_len = buflen;
559 req->ktr_buffer = buf;
560 }
561 ktr_submitrequest(curthread, req);
562 }
563
564 void
ktrsysret(int code,int error,register_t retval)565 ktrsysret(int code, int error, register_t retval)
566 {
567 struct ktr_request *req;
568 struct ktr_sysret *ktp;
569
570 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
571 return;
572
573 req = ktr_getrequest(KTR_SYSRET);
574 if (req == NULL)
575 return;
576 ktp = &req->ktr_data.ktr_sysret;
577 ktp->ktr_code = code;
578 ktp->ktr_error = error;
579 ktp->ktr_retval = ((error == 0) ? retval: 0); /* what about val2 ? */
580 ktr_submitrequest(curthread, req);
581 }
582
583 /*
584 * When a setuid process execs, disable tracing.
585 *
586 * XXX: We toss any pending asynchronous records.
587 */
588 struct ktr_io_params *
ktrprocexec(struct proc * p)589 ktrprocexec(struct proc *p)
590 {
591 struct ktr_io_params *kiop;
592
593 PROC_LOCK_ASSERT(p, MA_OWNED);
594
595 kiop = p->p_ktrioparms;
596 if (kiop == NULL || priv_check_cred(kiop->cr, PRIV_DEBUG_DIFFCRED) == 0)
597 return (NULL);
598
599 mtx_lock(&ktrace_mtx);
600 kiop = ktr_freeproc(p);
601 mtx_unlock(&ktrace_mtx);
602 return (kiop);
603 }
604
605 /*
606 * When a process exits, drain per-process asynchronous trace records
607 * and disable tracing.
608 */
609 void
ktrprocexit(struct thread * td)610 ktrprocexit(struct thread *td)
611 {
612 struct ktr_request *req;
613 struct proc *p;
614 struct ktr_io_params *kiop;
615
616 p = td->td_proc;
617 if (p->p_traceflag == 0)
618 return;
619
620 ktrace_enter(td);
621 req = ktr_getrequest_entered(td, KTR_PROCDTOR);
622 if (req != NULL)
623 ktr_enqueuerequest(td, req);
624 sx_xlock(&ktrace_sx);
625 ktr_drain(td);
626 sx_xunlock(&ktrace_sx);
627 PROC_LOCK(p);
628 mtx_lock(&ktrace_mtx);
629 kiop = ktr_freeproc(p);
630 mtx_unlock(&ktrace_mtx);
631 PROC_UNLOCK(p);
632 ktr_io_params_free(kiop);
633 ktrace_exit(td);
634 }
635
636 static void
ktrprocctor_entered(struct thread * td,struct proc * p)637 ktrprocctor_entered(struct thread *td, struct proc *p)
638 {
639 struct ktr_proc_ctor *ktp;
640 struct ktr_request *req;
641 struct thread *td2;
642
643 ktrace_assert(td);
644 td2 = FIRST_THREAD_IN_PROC(p);
645 req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
646 if (req == NULL)
647 return;
648 ktp = &req->ktr_data.ktr_proc_ctor;
649 ktp->sv_flags = p->p_sysent->sv_flags;
650 ktr_enqueuerequest(td2, req);
651 }
652
653 void
ktrprocctor(struct proc * p)654 ktrprocctor(struct proc *p)
655 {
656 struct thread *td = curthread;
657
658 if ((p->p_traceflag & KTRFAC_MASK) == 0)
659 return;
660
661 ktrace_enter(td);
662 ktrprocctor_entered(td, p);
663 ktrace_exit(td);
664 }
665
666 /*
667 * When a process forks, enable tracing in the new process if needed.
668 */
669 void
ktrprocfork(struct proc * p1,struct proc * p2)670 ktrprocfork(struct proc *p1, struct proc *p2)
671 {
672
673 MPASS(p2->p_ktrioparms == NULL);
674 MPASS(p2->p_traceflag == 0);
675
676 if (p1->p_traceflag == 0)
677 return;
678
679 PROC_LOCK(p1);
680 mtx_lock(&ktrace_mtx);
681 if (p1->p_traceflag & KTRFAC_INHERIT) {
682 p2->p_traceflag = p1->p_traceflag;
683 if ((p2->p_ktrioparms = p1->p_ktrioparms) != NULL)
684 p1->p_ktrioparms->refs++;
685 }
686 mtx_unlock(&ktrace_mtx);
687 PROC_UNLOCK(p1);
688
689 ktrprocctor(p2);
690 }
691
692 /*
693 * When a thread returns, drain any asynchronous records generated by the
694 * system call.
695 */
696 void
ktruserret(struct thread * td)697 ktruserret(struct thread *td)
698 {
699
700 ktrace_enter(td);
701 sx_xlock(&ktrace_sx);
702 ktr_drain(td);
703 sx_xunlock(&ktrace_sx);
704 ktrace_exit(td);
705 }
706
707 void
ktrnamei(const char * path)708 ktrnamei(const char *path)
709 {
710 struct ktr_request *req;
711 int namelen;
712 char *buf = NULL;
713
714 namelen = strlen(path);
715 if (namelen > 0) {
716 buf = malloc(namelen, M_KTRACE, M_WAITOK);
717 bcopy(path, buf, namelen);
718 }
719 req = ktr_getrequest(KTR_NAMEI);
720 if (req == NULL) {
721 if (buf != NULL)
722 free(buf, M_KTRACE);
723 return;
724 }
725 if (namelen > 0) {
726 req->ktr_header.ktr_len = namelen;
727 req->ktr_buffer = buf;
728 }
729 ktr_submitrequest(curthread, req);
730 }
731
732 void
ktrsysctl(int * name,u_int namelen)733 ktrsysctl(int *name, u_int namelen)
734 {
735 struct ktr_request *req;
736 u_int mib[CTL_MAXNAME + 2];
737 char *mibname;
738 size_t mibnamelen;
739 int error;
740
741 /* Lookup name of mib. */
742 KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
743 mib[0] = 0;
744 mib[1] = 1;
745 bcopy(name, mib + 2, namelen * sizeof(*name));
746 mibnamelen = 128;
747 mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
748 error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
749 NULL, 0, &mibnamelen, 0);
750 if (error) {
751 free(mibname, M_KTRACE);
752 return;
753 }
754 req = ktr_getrequest(KTR_SYSCTL);
755 if (req == NULL) {
756 free(mibname, M_KTRACE);
757 return;
758 }
759 req->ktr_header.ktr_len = mibnamelen;
760 req->ktr_buffer = mibname;
761 ktr_submitrequest(curthread, req);
762 }
763
764 void
ktrgenio(int fd,enum uio_rw rw,struct uio * uio,int error)765 ktrgenio(int fd, enum uio_rw rw, struct uio *uio, int error)
766 {
767 struct ktr_request *req;
768 struct ktr_genio *ktg;
769 int datalen;
770 char *buf;
771
772 if (error != 0 && (rw == UIO_READ || error == EFAULT)) {
773 freeuio(uio);
774 return;
775 }
776 uio->uio_offset = 0;
777 uio->uio_rw = UIO_WRITE;
778 datalen = MIN(uio->uio_resid, ktr_geniosize);
779 buf = malloc(datalen, M_KTRACE, M_WAITOK);
780 error = uiomove(buf, datalen, uio);
781 freeuio(uio);
782 if (error) {
783 free(buf, M_KTRACE);
784 return;
785 }
786 req = ktr_getrequest(KTR_GENIO);
787 if (req == NULL) {
788 free(buf, M_KTRACE);
789 return;
790 }
791 ktg = &req->ktr_data.ktr_genio;
792 ktg->ktr_fd = fd;
793 ktg->ktr_rw = rw;
794 req->ktr_header.ktr_len = datalen;
795 req->ktr_buffer = buf;
796 ktr_submitrequest(curthread, req);
797 }
798
799 void
ktrpsig(int sig,sig_t action,sigset_t * mask,int code)800 ktrpsig(int sig, sig_t action, sigset_t *mask, int code)
801 {
802 struct thread *td = curthread;
803 struct ktr_request *req;
804 struct ktr_psig *kp;
805
806 req = ktr_getrequest(KTR_PSIG);
807 if (req == NULL)
808 return;
809 kp = &req->ktr_data.ktr_psig;
810 kp->signo = (char)sig;
811 kp->action = action;
812 kp->mask = *mask;
813 kp->code = code;
814 ktr_enqueuerequest(td, req);
815 ktrace_exit(td);
816 }
817
818 void
ktrcsw(int out,int user,const char * wmesg)819 ktrcsw(int out, int user, const char *wmesg)
820 {
821 struct thread *td = curthread;
822 struct ktr_request *req;
823 struct ktr_csw *kc;
824
825 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
826 return;
827
828 req = ktr_getrequest(KTR_CSW);
829 if (req == NULL)
830 return;
831 kc = &req->ktr_data.ktr_csw;
832 kc->out = out;
833 kc->user = user;
834 if (wmesg != NULL)
835 strlcpy(kc->wmesg, wmesg, sizeof(kc->wmesg));
836 else
837 bzero(kc->wmesg, sizeof(kc->wmesg));
838 ktr_enqueuerequest(td, req);
839 ktrace_exit(td);
840 }
841
842 void
ktrstruct(const char * name,const void * data,size_t datalen)843 ktrstruct(const char *name, const void *data, size_t datalen)
844 {
845 struct ktr_request *req;
846 char *buf;
847 size_t buflen, namelen;
848
849 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
850 return;
851
852 if (data == NULL)
853 datalen = 0;
854 namelen = strlen(name) + 1;
855 buflen = namelen + datalen;
856 buf = malloc(buflen, M_KTRACE, M_WAITOK);
857 strcpy(buf, name);
858 bcopy(data, buf + namelen, datalen);
859 if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
860 free(buf, M_KTRACE);
861 return;
862 }
863 req->ktr_buffer = buf;
864 req->ktr_header.ktr_len = buflen;
865 ktr_submitrequest(curthread, req);
866 }
867
868 void
ktrstruct_error(const char * name,const void * data,size_t datalen,int error)869 ktrstruct_error(const char *name, const void *data, size_t datalen, int error)
870 {
871
872 if (error == 0)
873 ktrstruct(name, data, datalen);
874 }
875
876 void
ktrstructarray(const char * name,enum uio_seg seg,const void * data,int num_items,size_t struct_size)877 ktrstructarray(const char *name, enum uio_seg seg, const void *data,
878 int num_items, size_t struct_size)
879 {
880 struct ktr_request *req;
881 struct ktr_struct_array *ksa;
882 char *buf;
883 size_t buflen, datalen, namelen;
884 int max_items;
885
886 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
887 return;
888 if (num_items < 0)
889 return;
890
891 /* Trim array length to genio size. */
892 max_items = ktr_geniosize / struct_size;
893 if (num_items > max_items) {
894 if (max_items == 0)
895 num_items = 1;
896 else
897 num_items = max_items;
898 }
899 datalen = num_items * struct_size;
900
901 if (data == NULL)
902 datalen = 0;
903
904 namelen = strlen(name) + 1;
905 buflen = namelen + datalen;
906 buf = malloc(buflen, M_KTRACE, M_WAITOK);
907 strcpy(buf, name);
908 if (seg == UIO_SYSSPACE)
909 bcopy(data, buf + namelen, datalen);
910 else {
911 if (copyin(data, buf + namelen, datalen) != 0) {
912 free(buf, M_KTRACE);
913 return;
914 }
915 }
916 if ((req = ktr_getrequest(KTR_STRUCT_ARRAY)) == NULL) {
917 free(buf, M_KTRACE);
918 return;
919 }
920 ksa = &req->ktr_data.ktr_struct_array;
921 ksa->struct_size = struct_size;
922 req->ktr_buffer = buf;
923 req->ktr_header.ktr_len = buflen;
924 ktr_submitrequest(curthread, req);
925 }
926
927 void
ktrcapfail(enum ktr_cap_violation type,const void * data)928 ktrcapfail(enum ktr_cap_violation type, const void *data)
929 {
930 struct thread *td = curthread;
931 struct ktr_request *req;
932 struct ktr_cap_fail *kcf;
933 union ktr_cap_data *kcd;
934
935 if (__predict_false(td->td_pflags & TDP_INKTRACE))
936 return;
937 if (type != CAPFAIL_SYSCALL &&
938 (td->td_sa.callp->sy_flags & SYF_CAPENABLED) == 0)
939 return;
940
941 req = ktr_getrequest(KTR_CAPFAIL);
942 if (req == NULL)
943 return;
944 kcf = &req->ktr_data.ktr_cap_fail;
945 kcf->cap_type = type;
946 kcf->cap_code = td->td_sa.code;
947 kcf->cap_svflags = td->td_proc->p_sysent->sv_flags;
948 if (data != NULL) {
949 kcd = &kcf->cap_data;
950 switch (type) {
951 case CAPFAIL_NOTCAPABLE:
952 case CAPFAIL_INCREASE:
953 kcd->cap_needed = *(const cap_rights_t *)data;
954 kcd->cap_held = *((const cap_rights_t *)data + 1);
955 break;
956 case CAPFAIL_SYSCALL:
957 case CAPFAIL_SIGNAL:
958 case CAPFAIL_PROTO:
959 kcd->cap_int = *(const int *)data;
960 break;
961 case CAPFAIL_SOCKADDR: {
962 size_t len;
963
964 len = MIN(((const struct sockaddr *)data)->sa_len,
965 sizeof(kcd->cap_sockaddr));
966 memset(&kcd->cap_sockaddr, 0,
967 sizeof(kcd->cap_sockaddr));
968 memcpy(&kcd->cap_sockaddr, data, len);
969 break;
970 }
971 case CAPFAIL_NAMEI:
972 strlcpy(kcd->cap_path, data, MAXPATHLEN);
973 break;
974 case CAPFAIL_CPUSET:
975 default:
976 break;
977 }
978 }
979 ktr_enqueuerequest(td, req);
980 ktrace_exit(td);
981 }
982
983 void
ktrfault(vm_offset_t vaddr,int type)984 ktrfault(vm_offset_t vaddr, int type)
985 {
986 struct thread *td = curthread;
987 struct ktr_request *req;
988 struct ktr_fault *kf;
989
990 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
991 return;
992
993 req = ktr_getrequest(KTR_FAULT);
994 if (req == NULL)
995 return;
996 kf = &req->ktr_data.ktr_fault;
997 kf->vaddr = vaddr;
998 kf->type = type;
999 ktr_enqueuerequest(td, req);
1000 ktrace_exit(td);
1001 }
1002
1003 void
ktrfaultend(int result)1004 ktrfaultend(int result)
1005 {
1006 struct thread *td = curthread;
1007 struct ktr_request *req;
1008 struct ktr_faultend *kf;
1009
1010 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
1011 return;
1012
1013 req = ktr_getrequest(KTR_FAULTEND);
1014 if (req == NULL)
1015 return;
1016 kf = &req->ktr_data.ktr_faultend;
1017 kf->result = result;
1018 ktr_enqueuerequest(td, req);
1019 ktrace_exit(td);
1020 }
1021 #endif /* KTRACE */
1022
1023 /* Interface and common routines */
1024
1025 #ifndef _SYS_SYSPROTO_H_
1026 struct ktrace_args {
1027 char *fname;
1028 int ops;
1029 int facs;
1030 int pid;
1031 };
1032 #endif
1033 /* ARGSUSED */
1034 int
sys_ktrace(struct thread * td,struct ktrace_args * uap)1035 sys_ktrace(struct thread *td, struct ktrace_args *uap)
1036 {
1037 #ifdef KTRACE
1038 struct vnode *vp = NULL;
1039 struct proc *p;
1040 struct pgrp *pg;
1041 int facs = uap->facs & ~KTRFAC_ROOT;
1042 int ops = KTROP(uap->ops);
1043 int descend = uap->ops & KTRFLAG_DESCEND;
1044 int ret = 0;
1045 int flags, error = 0;
1046 struct nameidata nd;
1047 struct ktr_io_params *kiop, *old_kiop;
1048
1049 /*
1050 * Need something to (un)trace.
1051 */
1052 if (ops != KTROP_CLEARFILE && facs == 0)
1053 return (EINVAL);
1054
1055 kiop = NULL;
1056 if (ops != KTROP_CLEAR) {
1057 /*
1058 * an operation which requires a file argument.
1059 */
1060 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname);
1061 flags = FREAD | FWRITE | O_NOFOLLOW;
1062 error = vn_open(&nd, &flags, 0, NULL);
1063 if (error)
1064 return (error);
1065 NDFREE_PNBUF(&nd);
1066 vp = nd.ni_vp;
1067 VOP_UNLOCK(vp);
1068 if (vp->v_type != VREG) {
1069 (void)vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
1070 return (EACCES);
1071 }
1072 kiop = ktr_io_params_alloc(td, vp);
1073 }
1074
1075 /*
1076 * Clear all uses of the tracefile.
1077 */
1078 ktrace_enter(td);
1079 if (ops == KTROP_CLEARFILE) {
1080 restart:
1081 sx_slock(&allproc_lock);
1082 FOREACH_PROC_IN_SYSTEM(p) {
1083 old_kiop = NULL;
1084 PROC_LOCK(p);
1085 if (p->p_ktrioparms != NULL &&
1086 p->p_ktrioparms->vp == vp) {
1087 if (ktrcanset(td, p)) {
1088 mtx_lock(&ktrace_mtx);
1089 old_kiop = ktr_freeproc(p);
1090 mtx_unlock(&ktrace_mtx);
1091 } else
1092 error = EPERM;
1093 }
1094 PROC_UNLOCK(p);
1095 if (old_kiop != NULL) {
1096 sx_sunlock(&allproc_lock);
1097 ktr_io_params_free(old_kiop);
1098 goto restart;
1099 }
1100 }
1101 sx_sunlock(&allproc_lock);
1102 goto done;
1103 }
1104 /*
1105 * do it
1106 */
1107 sx_slock(&proctree_lock);
1108 if (uap->pid < 0) {
1109 /*
1110 * by process group
1111 */
1112 pg = pgfind(-uap->pid);
1113 if (pg == NULL) {
1114 sx_sunlock(&proctree_lock);
1115 error = ESRCH;
1116 goto done;
1117 }
1118
1119 /*
1120 * ktrops() may call vrele(). Lock pg_members
1121 * by the proctree_lock rather than pg_mtx.
1122 */
1123 PGRP_UNLOCK(pg);
1124 if (LIST_EMPTY(&pg->pg_members)) {
1125 sx_sunlock(&proctree_lock);
1126 error = ESRCH;
1127 goto done;
1128 }
1129 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1130 PROC_LOCK(p);
1131 if (descend)
1132 ret |= ktrsetchildren(td, p, ops, facs, kiop);
1133 else
1134 ret |= ktrops(td, p, ops, facs, kiop);
1135 }
1136 } else {
1137 /*
1138 * by pid
1139 */
1140 p = pfind(uap->pid);
1141 if (p == NULL) {
1142 error = ESRCH;
1143 sx_sunlock(&proctree_lock);
1144 goto done;
1145 }
1146 if (descend)
1147 ret |= ktrsetchildren(td, p, ops, facs, kiop);
1148 else
1149 ret |= ktrops(td, p, ops, facs, kiop);
1150 }
1151 sx_sunlock(&proctree_lock);
1152 if (!ret)
1153 error = EPERM;
1154 done:
1155 if (kiop != NULL) {
1156 mtx_lock(&ktrace_mtx);
1157 kiop = ktr_io_params_rele(kiop);
1158 mtx_unlock(&ktrace_mtx);
1159 ktr_io_params_free(kiop);
1160 }
1161 ktrace_exit(td);
1162 return (error);
1163 #else /* !KTRACE */
1164 return (ENOSYS);
1165 #endif /* KTRACE */
1166 }
1167
1168 /* ARGSUSED */
1169 int
sys_utrace(struct thread * td,struct utrace_args * uap)1170 sys_utrace(struct thread *td, struct utrace_args *uap)
1171 {
1172
1173 #ifdef KTRACE
1174 struct ktr_request *req;
1175 void *cp;
1176 int error;
1177
1178 if (!KTRPOINT(td, KTR_USER))
1179 return (0);
1180 if (uap->len > KTR_USER_MAXLEN)
1181 return (EINVAL);
1182 cp = malloc(uap->len, M_KTRACE, M_WAITOK);
1183 error = copyin(uap->addr, cp, uap->len);
1184 if (error) {
1185 free(cp, M_KTRACE);
1186 return (error);
1187 }
1188 req = ktr_getrequest(KTR_USER);
1189 if (req == NULL) {
1190 free(cp, M_KTRACE);
1191 return (ENOMEM);
1192 }
1193 req->ktr_buffer = cp;
1194 req->ktr_header.ktr_len = uap->len;
1195 ktr_submitrequest(td, req);
1196 return (0);
1197 #else /* !KTRACE */
1198 return (ENOSYS);
1199 #endif /* KTRACE */
1200 }
1201
1202 #ifdef KTRACE
1203 static int
ktrops(struct thread * td,struct proc * p,int ops,int facs,struct ktr_io_params * new_kiop)1204 ktrops(struct thread *td, struct proc *p, int ops, int facs,
1205 struct ktr_io_params *new_kiop)
1206 {
1207 struct ktr_io_params *old_kiop;
1208
1209 PROC_LOCK_ASSERT(p, MA_OWNED);
1210 if (!ktrcanset(td, p)) {
1211 PROC_UNLOCK(p);
1212 return (0);
1213 }
1214 if ((ops == KTROP_SET && p->p_state == PRS_NEW) ||
1215 p_cansee(td, p) != 0) {
1216 /*
1217 * Disallow setting trace points if the process is being born.
1218 * This avoids races with trace point inheritance in
1219 * ktrprocfork().
1220 */
1221 PROC_UNLOCK(p);
1222 return (0);
1223 }
1224 if ((p->p_flag & P_WEXIT) != 0) {
1225 /*
1226 * There's nothing to do if the process is exiting, but avoid
1227 * signaling an error.
1228 */
1229 PROC_UNLOCK(p);
1230 return (1);
1231 }
1232 old_kiop = NULL;
1233 mtx_lock(&ktrace_mtx);
1234 if (ops == KTROP_SET) {
1235 if (p->p_ktrioparms != NULL &&
1236 p->p_ktrioparms->vp != new_kiop->vp) {
1237 /* if trace file already in use, relinquish below */
1238 old_kiop = ktr_io_params_rele(p->p_ktrioparms);
1239 p->p_ktrioparms = NULL;
1240 }
1241 if (p->p_ktrioparms == NULL) {
1242 p->p_ktrioparms = new_kiop;
1243 ktr_io_params_ref(new_kiop);
1244 }
1245 p->p_traceflag |= facs;
1246 if (priv_check(td, PRIV_KTRACE) == 0)
1247 p->p_traceflag |= KTRFAC_ROOT;
1248 } else {
1249 /* KTROP_CLEAR */
1250 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1251 /* no more tracing */
1252 old_kiop = ktr_freeproc(p);
1253 }
1254 mtx_unlock(&ktrace_mtx);
1255 if ((p->p_traceflag & KTRFAC_MASK) != 0)
1256 ktrprocctor_entered(td, p);
1257 PROC_UNLOCK(p);
1258 ktr_io_params_free(old_kiop);
1259
1260 return (1);
1261 }
1262
1263 static int
ktrsetchildren(struct thread * td,struct proc * top,int ops,int facs,struct ktr_io_params * new_kiop)1264 ktrsetchildren(struct thread *td, struct proc *top, int ops, int facs,
1265 struct ktr_io_params *new_kiop)
1266 {
1267 struct proc *p;
1268 int ret = 0;
1269
1270 p = top;
1271 PROC_LOCK_ASSERT(p, MA_OWNED);
1272 sx_assert(&proctree_lock, SX_LOCKED);
1273 for (;;) {
1274 ret |= ktrops(td, p, ops, facs, new_kiop);
1275 /*
1276 * If this process has children, descend to them next,
1277 * otherwise do any siblings, and if done with this level,
1278 * follow back up the tree (but not past top).
1279 */
1280 if (!LIST_EMPTY(&p->p_children))
1281 p = LIST_FIRST(&p->p_children);
1282 else for (;;) {
1283 if (p == top)
1284 return (ret);
1285 if (LIST_NEXT(p, p_sibling)) {
1286 p = LIST_NEXT(p, p_sibling);
1287 break;
1288 }
1289 p = p->p_pptr;
1290 }
1291 PROC_LOCK(p);
1292 }
1293 /*NOTREACHED*/
1294 }
1295
1296 static void
ktr_writerequest(struct thread * td,struct ktr_request * req)1297 ktr_writerequest(struct thread *td, struct ktr_request *req)
1298 {
1299 struct ktr_io_params *kiop, *kiop1;
1300 struct ktr_header *kth;
1301 struct vnode *vp;
1302 struct proc *p;
1303 struct ucred *cred;
1304 struct uio auio;
1305 struct iovec aiov[3];
1306 struct mount *mp;
1307 off_t lim;
1308 int datalen, buflen;
1309 int error;
1310
1311 p = td->td_proc;
1312
1313 /*
1314 * We reference the kiop for use in I/O in case ktrace is
1315 * disabled on the process as we write out the request.
1316 */
1317 mtx_lock(&ktrace_mtx);
1318 kiop = p->p_ktrioparms;
1319
1320 /*
1321 * If kiop is NULL, it has been cleared out from under this
1322 * request, so just drop it.
1323 */
1324 if (kiop == NULL) {
1325 mtx_unlock(&ktrace_mtx);
1326 return;
1327 }
1328
1329 ktr_io_params_ref(kiop);
1330 vp = kiop->vp;
1331 cred = kiop->cr;
1332 lim = kiop->lim;
1333
1334 KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1335 mtx_unlock(&ktrace_mtx);
1336
1337 kth = &req->ktr_header;
1338 KASSERT(((u_short)kth->ktr_type & ~KTR_TYPE) < nitems(data_lengths),
1339 ("data_lengths array overflow"));
1340 datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_TYPE];
1341 buflen = kth->ktr_len;
1342 auio.uio_iov = &aiov[0];
1343 auio.uio_offset = 0;
1344 auio.uio_segflg = UIO_SYSSPACE;
1345 auio.uio_rw = UIO_WRITE;
1346 aiov[0].iov_base = (caddr_t)kth;
1347 aiov[0].iov_len = sizeof(struct ktr_header);
1348 auio.uio_resid = sizeof(struct ktr_header);
1349 auio.uio_iovcnt = 1;
1350 auio.uio_td = td;
1351 if (datalen != 0) {
1352 aiov[1].iov_base = (caddr_t)&req->ktr_data;
1353 aiov[1].iov_len = datalen;
1354 auio.uio_resid += datalen;
1355 auio.uio_iovcnt++;
1356 kth->ktr_len += datalen;
1357 }
1358 if (buflen != 0) {
1359 KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1360 aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1361 aiov[auio.uio_iovcnt].iov_len = buflen;
1362 auio.uio_resid += buflen;
1363 auio.uio_iovcnt++;
1364 }
1365
1366 vn_start_write(vp, &mp, V_WAIT);
1367 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1368 td->td_ktr_io_lim = lim;
1369 #ifdef MAC
1370 error = mac_vnode_check_write(cred, NOCRED, vp);
1371 if (error == 0)
1372 #endif
1373 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
1374 VOP_UNLOCK(vp);
1375 vn_finished_write(mp);
1376 if (error == 0) {
1377 mtx_lock(&ktrace_mtx);
1378 kiop = ktr_io_params_rele(kiop);
1379 mtx_unlock(&ktrace_mtx);
1380 ktr_io_params_free(kiop);
1381 return;
1382 }
1383
1384 /*
1385 * If error encountered, give up tracing on this vnode on this
1386 * process. Other processes might still be suitable for
1387 * writes to this vnode.
1388 */
1389 log(LOG_NOTICE,
1390 "ktrace write failed, errno %d, tracing stopped for pid %d\n",
1391 error, p->p_pid);
1392
1393 kiop1 = NULL;
1394 PROC_LOCK(p);
1395 mtx_lock(&ktrace_mtx);
1396 if (p->p_ktrioparms != NULL && p->p_ktrioparms->vp == vp)
1397 kiop1 = ktr_freeproc(p);
1398 kiop = ktr_io_params_rele(kiop);
1399 mtx_unlock(&ktrace_mtx);
1400 PROC_UNLOCK(p);
1401 ktr_io_params_free(kiop1);
1402 ktr_io_params_free(kiop);
1403 }
1404
1405 /*
1406 * Return true if caller has permission to set the ktracing state
1407 * of target. Essentially, the target can't possess any
1408 * more permissions than the caller. KTRFAC_ROOT signifies that
1409 * root previously set the tracing status on the target process, and
1410 * so, only root may further change it.
1411 */
1412 static int
ktrcanset(struct thread * td,struct proc * targetp)1413 ktrcanset(struct thread *td, struct proc *targetp)
1414 {
1415
1416 PROC_LOCK_ASSERT(targetp, MA_OWNED);
1417 if (targetp->p_traceflag & KTRFAC_ROOT &&
1418 priv_check(td, PRIV_KTRACE))
1419 return (0);
1420
1421 if (p_candebug(td, targetp) != 0)
1422 return (0);
1423
1424 return (1);
1425 }
1426
1427 #endif /* KTRACE */
1428