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