| 1 | /* |
| 2 | * Copyright (c) 1989 The Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. All advertising materials mentioning features or use of this software |
| 14 | * must display the following acknowledgement: |
| 15 | * This product includes software developed by the University of |
| 16 | * California, Berkeley and its contributors. |
| 17 | * 4. 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 | * from: @(#)kern_ktrace.c 7.15 (Berkeley) 6/21/91 |
| 34 | * $Id: kern_ktrace.c,v 1.5 1993/11/25 01:33:00 wollman Exp $ |
| 35 | */ |
| 36 | |
| 37 | #ifdef KTRACE |
| 38 | |
| 39 | #include "param.h" |
| 40 | #include "systm.h" |
| 41 | #include "proc.h" |
| 42 | #include "file.h" |
| 43 | #include "namei.h" |
| 44 | #include "vnode.h" |
| 45 | #include "ktrace.h" |
| 46 | #include "malloc.h" |
| 47 | #include "syslog.h" |
| 48 | |
| 49 | static void ktrwrite(struct vnode *, struct ktr_header *); |
| 50 | static int ktrops(struct proc *, struct proc *, int, int, struct vnode *); |
| 51 | static int ktrsetchildren(struct proc *, struct proc *, int, int, struct vnode *); |
| 52 | static int ktrcanset(struct proc *, struct proc *); |
| 53 | |
| 54 | static struct ktr_header * |
| 55 | ktrgetheader(type) |
| 56 | int type; |
| 57 | { |
| 58 | register struct ktr_header *kth; |
| 59 | struct proc *p = curproc; /* XXX */ |
| 60 | |
| 61 | MALLOC(kth, struct ktr_header *, sizeof (struct ktr_header), |
| 62 | M_TEMP, M_WAITOK); |
| 63 | kth->ktr_type = type; |
| 64 | microtime(&kth->ktr_time); |
| 65 | kth->ktr_pid = p->p_pid; |
| 66 | bcopy(p->p_comm, kth->ktr_comm, MAXCOMLEN); |
| 67 | return (kth); |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | ktrsyscall(vp, code, narg, args) |
| 72 | struct vnode *vp; |
| 73 | int code, narg, args[]; |
| 74 | { |
| 75 | struct ktr_header *kth = ktrgetheader(KTR_SYSCALL); |
| 76 | struct ktr_syscall *ktp; |
| 77 | register len = sizeof(struct ktr_syscall) + (narg * sizeof(int)); |
| 78 | int *argp, i; |
| 79 | |
| 80 | MALLOC(ktp, struct ktr_syscall *, len, M_TEMP, M_WAITOK); |
| 81 | ktp->ktr_code = code; |
| 82 | ktp->ktr_narg = narg; |
| 83 | argp = (int *)((char *)ktp + sizeof(struct ktr_syscall)); |
| 84 | for (i = 0; i < narg; i++) |
| 85 | *argp++ = args[i]; |
| 86 | kth->ktr_buf = (caddr_t)ktp; |
| 87 | kth->ktr_len = len; |
| 88 | ktrwrite(vp, kth); |
| 89 | FREE(ktp, M_TEMP); |
| 90 | FREE(kth, M_TEMP); |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | ktrsysret(vp, code, error, retval) |
| 95 | struct vnode *vp; |
| 96 | int code, error, retval; |
| 97 | { |
| 98 | struct ktr_header *kth = ktrgetheader(KTR_SYSRET); |
| 99 | struct ktr_sysret ktp; |
| 100 | |
| 101 | ktp.ktr_code = code; |
| 102 | ktp.ktr_error = error; |
| 103 | ktp.ktr_retval = retval; /* what about val2 ? */ |
| 104 | |
| 105 | kth->ktr_buf = (caddr_t)&ktp; |
| 106 | kth->ktr_len = sizeof(struct ktr_sysret); |
| 107 | |
| 108 | ktrwrite(vp, kth); |
| 109 | FREE(kth, M_TEMP); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | ktrnamei(vp, path) |
| 114 | struct vnode *vp; |
| 115 | char *path; |
| 116 | { |
| 117 | struct ktr_header *kth = ktrgetheader(KTR_NAMEI); |
| 118 | |
| 119 | kth->ktr_len = strlen(path); |
| 120 | kth->ktr_buf = path; |
| 121 | |
| 122 | ktrwrite(vp, kth); |
| 123 | FREE(kth, M_TEMP); |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | ktrgenio(vp, fd, rw, iov, len, error) |
| 128 | struct vnode *vp; |
| 129 | int fd; |
| 130 | enum uio_rw rw; |
| 131 | register struct iovec *iov; |
| 132 | int len; |
| 133 | int error; |
| 134 | { |
| 135 | struct ktr_header *kth = ktrgetheader(KTR_GENIO); |
| 136 | register struct ktr_genio *ktp; |
| 137 | register caddr_t cp; |
| 138 | register int resid = len, cnt; |
| 139 | |
| 140 | if (error) |
| 141 | return; |
| 142 | MALLOC(ktp, struct ktr_genio *, sizeof(struct ktr_genio) + len, |
| 143 | M_TEMP, M_WAITOK); |
| 144 | ktp->ktr_fd = fd; |
| 145 | ktp->ktr_rw = rw; |
| 146 | cp = (caddr_t)((char *)ktp + sizeof (struct ktr_genio)); |
| 147 | while (resid > 0) { |
| 148 | if ((cnt = iov->iov_len) > resid) |
| 149 | cnt = resid; |
| 150 | if (copyin(iov->iov_base, cp, (unsigned)cnt)) |
| 151 | goto done; |
| 152 | cp += cnt; |
| 153 | resid -= cnt; |
| 154 | iov++; |
| 155 | } |
| 156 | kth->ktr_buf = (caddr_t)ktp; |
| 157 | kth->ktr_len = sizeof (struct ktr_genio) + len; |
| 158 | |
| 159 | ktrwrite(vp, kth); |
| 160 | done: |
| 161 | FREE(kth, M_TEMP); |
| 162 | FREE(ktp, M_TEMP); |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | ktrpsig(vp, sig, action, mask, code) |
| 167 | struct vnode *vp; |
| 168 | int sig; |
| 169 | sig_t action; |
| 170 | int mask; |
| 171 | int code; |
| 172 | { |
| 173 | struct ktr_header *kth = ktrgetheader(KTR_PSIG); |
| 174 | struct ktr_psig kp; |
| 175 | |
| 176 | kp.signo = (char)sig; |
| 177 | kp.action = action; |
| 178 | kp.mask = mask; |
| 179 | kp.code = code; |
| 180 | kth->ktr_buf = (caddr_t)&kp; |
| 181 | kth->ktr_len = sizeof (struct ktr_psig); |
| 182 | |
| 183 | ktrwrite(vp, kth); |
| 184 | FREE(kth, M_TEMP); |
| 185 | } |
| 186 | |
| 187 | /* Interface and common routines */ |
| 188 | |
| 189 | /* |
| 190 | * ktrace system call |
| 191 | */ |
| 192 | |
| 193 | struct ktrace_args { |
| 194 | char *fname; |
| 195 | int ops; |
| 196 | int facs; |
| 197 | int pid; |
| 198 | }; |
| 199 | |
| 200 | /* ARGSUSED */ |
| 201 | int |
| 202 | ktrace(curp, uap, retval) |
| 203 | struct proc *curp; |
| 204 | register struct ktrace_args *uap; |
| 205 | int *retval; |
| 206 | { |
| 207 | register struct vnode *vp = NULL; |
| 208 | register struct proc *p; |
| 209 | struct pgrp *pg; |
| 210 | int facs = uap->facs & ~KTRFAC_ROOT; |
| 211 | int ops = KTROP(uap->ops); |
| 212 | int descend = uap->ops & KTRFLAG_DESCEND; |
| 213 | int ret = 0; |
| 214 | int error = 0; |
| 215 | struct nameidata nd; |
| 216 | |
| 217 | if (ops != KTROP_CLEAR) { |
| 218 | /* |
| 219 | * an operation which requires a file argument. |
| 220 | */ |
| 221 | nd.ni_segflg = UIO_USERSPACE; |
| 222 | nd.ni_dirp = uap->fname; |
| 223 | if (error = vn_open(&nd, curp, FREAD|FWRITE, 0)) |
| 224 | return (error); |
| 225 | vp = nd.ni_vp; |
| 226 | VOP_UNLOCK(vp); |
| 227 | if (vp->v_type != VREG) { |
| 228 | (void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp); |
| 229 | return (EACCES); |
| 230 | } |
| 231 | } |
| 232 | /* |
| 233 | * Clear all uses of the tracefile |
| 234 | */ |
| 235 | if (ops == KTROP_CLEARFILE) { |
| 236 | for (p = allproc; p != NULL; p = p->p_nxt) { |
| 237 | if (p->p_tracep == vp) { |
| 238 | if (ktrcanset(curp, p)) { |
| 239 | p->p_tracep = NULL; |
| 240 | p->p_traceflag = 0; |
| 241 | (void) vn_close(vp, FREAD|FWRITE, |
| 242 | p->p_ucred, p); |
| 243 | } else |
| 244 | error = EPERM; |
| 245 | } |
| 246 | } |
| 247 | goto done; |
| 248 | } |
| 249 | /* |
| 250 | * need something to (un)trace (XXX - why is this here?) |
| 251 | */ |
| 252 | if (!facs) { |
| 253 | error = EINVAL; |
| 254 | goto done; |
| 255 | } |
| 256 | /* |
| 257 | * do it |
| 258 | */ |
| 259 | if (uap->pid < 0) { |
| 260 | /* |
| 261 | * by process group |
| 262 | */ |
| 263 | pg = pgfind(-uap->pid); |
| 264 | if (pg == NULL) { |
| 265 | error = ESRCH; |
| 266 | goto done; |
| 267 | } |
| 268 | for (p = pg->pg_mem; p != NULL; p = p->p_pgrpnxt) |
| 269 | if (descend) |
| 270 | ret |= ktrsetchildren(curp, p, ops, facs, vp); |
| 271 | else |
| 272 | ret |= ktrops(curp, p, ops, facs, vp); |
| 273 | |
| 274 | } else { |
| 275 | /* |
| 276 | * by pid |
| 277 | */ |
| 278 | p = pfind(uap->pid); |
| 279 | if (p == NULL) { |
| 280 | error = ESRCH; |
| 281 | goto done; |
| 282 | } |
| 283 | if (descend) |
| 284 | ret |= ktrsetchildren(curp, p, ops, facs, vp); |
| 285 | else |
| 286 | ret |= ktrops(curp, p, ops, facs, vp); |
| 287 | } |
| 288 | if (!ret) |
| 289 | error = EPERM; |
| 290 | done: |
| 291 | if (vp != NULL) |
| 292 | (void) vn_close(vp, FWRITE, curp->p_ucred, curp); |
| 293 | return (error); |
| 294 | } |
| 295 | |
| 296 | static int |
| 297 | ktrops(curp, p, ops, facs, vp) |
| 298 | struct proc *curp, *p; |
| 299 | int ops; |
| 300 | int facs; |
| 301 | struct vnode *vp; |
| 302 | { |
| 303 | |
| 304 | if (!ktrcanset(curp, p)) |
| 305 | return (0); |
| 306 | if (ops == KTROP_SET) { |
| 307 | if (p->p_tracep != vp) { |
| 308 | /* |
| 309 | * if trace file already in use, relinquish |
| 310 | */ |
| 311 | if (p->p_tracep != NULL) |
| 312 | vrele(p->p_tracep); |
| 313 | VREF(vp); |
| 314 | p->p_tracep = vp; |
| 315 | } |
| 316 | p->p_traceflag |= facs; |
| 317 | if (curp->p_ucred->cr_uid == 0) |
| 318 | p->p_traceflag |= KTRFAC_ROOT; |
| 319 | } else { |
| 320 | /* KTROP_CLEAR */ |
| 321 | if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) { |
| 322 | /* no more tracing */ |
| 323 | p->p_traceflag = 0; |
| 324 | if (p->p_tracep != NULL) { |
| 325 | vrele(p->p_tracep); |
| 326 | p->p_tracep = NULL; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | return (1); |
| 332 | } |
| 333 | |
| 334 | static int |
| 335 | ktrsetchildren(curp, top, ops, facs, vp) |
| 336 | struct proc *curp, *top; |
| 337 | int ops; |
| 338 | int facs; |
| 339 | struct vnode *vp; |
| 340 | { |
| 341 | register struct proc *p; |
| 342 | register int ret = 0; |
| 343 | |
| 344 | p = top; |
| 345 | for (;;) { |
| 346 | ret |= ktrops(curp, p, ops, facs, vp); |
| 347 | /* |
| 348 | * If this process has children, descend to them next, |
| 349 | * otherwise do any siblings, and if done with this level, |
| 350 | * follow back up the tree (but not past top). |
| 351 | */ |
| 352 | if (p->p_cptr) |
| 353 | p = p->p_cptr; |
| 354 | else if (p == top) |
| 355 | return (ret); |
| 356 | else if (p->p_osptr) |
| 357 | p = p->p_osptr; |
| 358 | else for (;;) { |
| 359 | p = p->p_pptr; |
| 360 | if (p == top) |
| 361 | return (ret); |
| 362 | if (p->p_osptr) { |
| 363 | p = p->p_osptr; |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | /*NOTREACHED*/ |
| 369 | } |
| 370 | |
| 371 | static void |
| 372 | ktrwrite(vp, kth) |
| 373 | struct vnode *vp; |
| 374 | register struct ktr_header *kth; |
| 375 | { |
| 376 | struct uio auio; |
| 377 | struct iovec aiov[2]; |
| 378 | register struct proc *p = curproc; /* XXX */ |
| 379 | int error; |
| 380 | |
| 381 | if (vp == NULL) |
| 382 | return; |
| 383 | auio.uio_iov = &aiov[0]; |
| 384 | auio.uio_offset = 0; |
| 385 | auio.uio_segflg = UIO_SYSSPACE; |
| 386 | auio.uio_rw = UIO_WRITE; |
| 387 | aiov[0].iov_base = (caddr_t)kth; |
| 388 | aiov[0].iov_len = sizeof(struct ktr_header); |
| 389 | auio.uio_resid = sizeof(struct ktr_header); |
| 390 | auio.uio_iovcnt = 1; |
| 391 | auio.uio_procp = (struct proc *)0; |
| 392 | if (kth->ktr_len > 0) { |
| 393 | auio.uio_iovcnt++; |
| 394 | aiov[1].iov_base = kth->ktr_buf; |
| 395 | aiov[1].iov_len = kth->ktr_len; |
| 396 | auio.uio_resid += kth->ktr_len; |
| 397 | } |
| 398 | VOP_LOCK(vp); |
| 399 | error = VOP_WRITE(vp, &auio, IO_UNIT|IO_APPEND, p->p_ucred); |
| 400 | VOP_UNLOCK(vp); |
| 401 | if (!error) |
| 402 | return; |
| 403 | /* |
| 404 | * If error encountered, give up tracing on this vnode. |
| 405 | */ |
| 406 | log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n", |
| 407 | error); |
| 408 | for (p = allproc; p != NULL; p = p->p_nxt) { |
| 409 | if (p->p_tracep == vp) { |
| 410 | p->p_tracep = NULL; |
| 411 | p->p_traceflag = 0; |
| 412 | vrele(vp); |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * Return true if caller has permission to set the ktracing state |
| 419 | * of target. Essentially, the target can't possess any |
| 420 | * more permissions than the caller. KTRFAC_ROOT signifies that |
| 421 | * root previously set the tracing status on the target process, and |
| 422 | * so, only root may further change it. |
| 423 | * |
| 424 | * TODO: check groups. use caller effective gid. |
| 425 | */ |
| 426 | static int |
| 427 | ktrcanset(callp, targetp) |
| 428 | struct proc *callp, *targetp; |
| 429 | { |
| 430 | register struct pcred *caller = callp->p_cred; |
| 431 | register struct pcred *target = targetp->p_cred; |
| 432 | |
| 433 | if ((caller->pc_ucred->cr_uid == target->p_ruid && |
| 434 | target->p_ruid == target->p_svuid && |
| 435 | caller->p_rgid == target->p_rgid && /* XXX */ |
| 436 | target->p_rgid == target->p_svgid && |
| 437 | (targetp->p_traceflag & KTRFAC_ROOT) == 0) || |
| 438 | caller->pc_ucred->cr_uid == 0) |
| 439 | return (1); |
| 440 | |
| 441 | return (0); |
| 442 | } |
| 443 | |
| 444 | #endif |