1 /*- 2 * Copyright (c) 2011, David E. O'Brien. 3 * Copyright (c) 2009-2011, Juniper Networks, Inc. 4 * Copyright (c) 2015-2016, EMC Corp. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY JUNIPER NETWORKS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL JUNIPER NETWORKS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_compat.h" 33 34 #include <sys/param.h> 35 #include <sys/file.h> 36 #include <sys/systm.h> 37 #include <sys/buf.h> 38 #include <sys/capsicum.h> 39 #include <sys/condvar.h> 40 #include <sys/conf.h> 41 #include <sys/fcntl.h> 42 #include <sys/ioccom.h> 43 #include <sys/kernel.h> 44 #include <sys/lock.h> 45 #include <sys/malloc.h> 46 #include <sys/module.h> 47 #include <sys/poll.h> 48 #include <sys/proc.h> 49 #include <sys/sx.h> 50 #include <sys/syscall.h> 51 #include <sys/sysent.h> 52 #include <sys/sysproto.h> 53 #include <sys/uio.h> 54 55 #include "filemon.h" 56 57 #if defined(COMPAT_IA32) || defined(COMPAT_FREEBSD32) || defined(COMPAT_ARCH32) 58 #include <compat/freebsd32/freebsd32_syscall.h> 59 #include <compat/freebsd32/freebsd32_proto.h> 60 61 extern struct sysentvec ia32_freebsd_sysvec; 62 #endif 63 64 extern struct sysentvec elf32_freebsd_sysvec; 65 extern struct sysentvec elf64_freebsd_sysvec; 66 67 static d_close_t filemon_close; 68 static d_ioctl_t filemon_ioctl; 69 static d_open_t filemon_open; 70 71 static struct cdevsw filemon_cdevsw = { 72 .d_version = D_VERSION, 73 .d_close = filemon_close, 74 .d_ioctl = filemon_ioctl, 75 .d_open = filemon_open, 76 .d_name = "filemon", 77 }; 78 79 MALLOC_DECLARE(M_FILEMON); 80 MALLOC_DEFINE(M_FILEMON, "filemon", "File access monitor"); 81 82 /* 83 * The filemon->lock protects several things currently: 84 * - fname1/fname2/msgbufr are pre-allocated and used per syscall 85 * for logging and copyins rather than stack variables. 86 * - Serializing the filemon's log output. 87 * - Preventing inheritance or removal of the filemon into proc.p_filemon. 88 */ 89 struct filemon { 90 struct sx lock; /* Lock for this filemon. */ 91 struct file *fp; /* Output file pointer. */ 92 char fname1[MAXPATHLEN]; /* Temporary filename buffer. */ 93 char fname2[MAXPATHLEN]; /* Temporary filename buffer. */ 94 char msgbufr[1024]; /* Output message buffer. */ 95 u_int refcnt; /* Pointer reference count. */ 96 u_int proccnt; /* Process count. */ 97 }; 98 99 static struct cdev *filemon_dev; 100 static void filemon_output(struct filemon *filemon, char *msg, size_t len); 101 102 static __inline struct filemon * 103 filemon_acquire(struct filemon *filemon) 104 { 105 106 if (filemon != NULL) 107 refcount_acquire(&filemon->refcnt); 108 return (filemon); 109 } 110 111 /* 112 * Release a reference and free on the last one. 113 */ 114 static void 115 filemon_release(struct filemon *filemon) 116 { 117 118 if (refcount_release(&filemon->refcnt) == 0) 119 return; 120 /* 121 * There are valid cases of releasing while locked, such as in 122 * filemon_untrack_processes, but none which are done where there 123 * is not at least 1 reference remaining. 124 */ 125 sx_assert(&filemon->lock, SA_UNLOCKED); 126 127 sx_destroy(&filemon->lock); 128 free(filemon, M_FILEMON); 129 } 130 131 /* 132 * Acquire the proc's p_filemon reference and lock the filemon. 133 * The proc's p_filemon may not match this filemon on return. 134 */ 135 static struct filemon * 136 filemon_proc_get(struct proc *p) 137 { 138 struct filemon *filemon; 139 140 PROC_LOCK(p); 141 filemon = filemon_acquire(p->p_filemon); 142 PROC_UNLOCK(p); 143 144 if (filemon == NULL) 145 return (NULL); 146 /* 147 * The p->p_filemon may have changed by now. That case is handled 148 * by the exit and fork hooks and filemon_attach_proc specially. 149 */ 150 sx_xlock(&filemon->lock); 151 return (filemon); 152 } 153 154 /* Remove and release the filemon on the given process. */ 155 static void 156 filemon_proc_drop(struct proc *p) 157 { 158 struct filemon *filemon; 159 160 KASSERT(p->p_filemon != NULL, ("%s: proc %p NULL p_filemon", 161 __func__, p)); 162 sx_assert(&p->p_filemon->lock, SA_XLOCKED); 163 PROC_LOCK(p); 164 filemon = p->p_filemon; 165 p->p_filemon = NULL; 166 --filemon->proccnt; 167 PROC_UNLOCK(p); 168 /* 169 * This should not be the last reference yet. filemon_release() 170 * cannot be called with filemon locked, which the caller expects 171 * will stay locked. 172 */ 173 KASSERT(filemon->refcnt > 1, ("%s: proc %p dropping filemon %p " 174 "with last reference", __func__, p, filemon)); 175 filemon_release(filemon); 176 } 177 178 /* Unlock and release the filemon. */ 179 static __inline void 180 filemon_drop(struct filemon *filemon) 181 { 182 183 sx_xunlock(&filemon->lock); 184 filemon_release(filemon); 185 } 186 187 #include "filemon_wrapper.c" 188 189 static void 190 filemon_comment(struct filemon *filemon) 191 { 192 int len; 193 struct timeval now; 194 195 getmicrotime(&now); 196 197 len = snprintf(filemon->msgbufr, sizeof(filemon->msgbufr), 198 "# filemon version %d\n# Target pid %d\n# Start %ju.%06ju\nV %d\n", 199 FILEMON_VERSION, curproc->p_pid, (uintmax_t)now.tv_sec, 200 (uintmax_t)now.tv_usec, FILEMON_VERSION); 201 202 filemon_output(filemon, filemon->msgbufr, len); 203 } 204 205 /* 206 * Invalidate the passed filemon in all processes. 207 */ 208 static void 209 filemon_untrack_processes(struct filemon *filemon) 210 { 211 struct proc *p; 212 213 sx_assert(&filemon->lock, SA_XLOCKED); 214 215 /* Avoid allproc loop if there is no need. */ 216 if (filemon->proccnt == 0) 217 return; 218 219 /* 220 * Processes in this list won't go away while here since 221 * filemon_event_process_exit() will lock on filemon->lock 222 * which we hold. 223 */ 224 sx_slock(&allproc_lock); 225 FOREACH_PROC_IN_SYSTEM(p) { 226 /* 227 * No PROC_LOCK is needed to compare here since it is 228 * guaranteed to not change since we have its filemon 229 * locked. Everything that changes this p_filemon will 230 * be locked on it. 231 */ 232 if (p->p_filemon == filemon) 233 filemon_proc_drop(p); 234 } 235 sx_sunlock(&allproc_lock); 236 237 /* 238 * It's possible some references were acquired but will be 239 * dropped shortly as they are restricted from being 240 * inherited. There is at least the reference in cdevpriv remaining. 241 */ 242 KASSERT(filemon->refcnt > 0, ("%s: filemon %p should have " 243 "references still.", __func__, filemon)); 244 KASSERT(filemon->proccnt == 0, ("%s: filemon %p should not have " 245 "attached procs still.", __func__, filemon)); 246 } 247 248 /* 249 * Close out the log. 250 */ 251 static void 252 filemon_close_log(struct filemon *filemon) 253 { 254 struct file *fp; 255 struct timeval now; 256 size_t len; 257 258 sx_assert(&filemon->lock, SA_XLOCKED); 259 if (filemon->fp == NULL) 260 return; 261 262 getmicrotime(&now); 263 264 len = snprintf(filemon->msgbufr, 265 sizeof(filemon->msgbufr), 266 "# Stop %ju.%06ju\n# Bye bye\n", 267 (uintmax_t)now.tv_sec, (uintmax_t)now.tv_usec); 268 269 filemon_output(filemon, filemon->msgbufr, len); 270 fp = filemon->fp; 271 filemon->fp = NULL; 272 273 sx_xunlock(&filemon->lock); 274 fdrop(fp, curthread); 275 sx_xlock(&filemon->lock); 276 277 return; 278 } 279 280 /* The devfs file is being closed. Untrace all processes. */ 281 static void 282 filemon_dtr(void *data) 283 { 284 struct filemon *filemon = data; 285 286 if (filemon == NULL) 287 return; 288 289 sx_xlock(&filemon->lock); 290 /* 291 * Detach the filemon. It cannot be inherited after this. 292 */ 293 filemon_untrack_processes(filemon); 294 filemon_close_log(filemon); 295 filemon_drop(filemon); 296 } 297 298 /* Attach the filemon to the process. */ 299 static int 300 filemon_attach_proc(struct filemon *filemon, struct proc *p) 301 { 302 struct filemon *filemon2; 303 304 sx_assert(&filemon->lock, SA_XLOCKED); 305 PROC_LOCK_ASSERT(p, MA_OWNED); 306 KASSERT((p->p_flag & P_WEXIT) == 0, 307 ("%s: filemon %p attaching to exiting process %p", 308 __func__, filemon, p)); 309 310 if (p->p_filemon == filemon) 311 return (0); 312 /* 313 * Don't allow truncating other process traces. It is 314 * not really intended to trace procs other than curproc 315 * anyhow. 316 */ 317 if (p->p_filemon != NULL && p != curproc) 318 return (EBUSY); 319 /* 320 * Historic behavior of filemon has been to let a child initiate 321 * tracing on itself and cease existing tracing. Bmake 322 * .META + .MAKE relies on this. It is only relevant for attaching to 323 * curproc. 324 */ 325 while (p->p_filemon != NULL) { 326 PROC_UNLOCK(p); 327 sx_xunlock(&filemon->lock); 328 while ((filemon2 = filemon_proc_get(p)) != NULL) { 329 /* It may have changed. */ 330 if (p->p_filemon == filemon2) 331 filemon_proc_drop(p); 332 filemon_drop(filemon2); 333 } 334 sx_xlock(&filemon->lock); 335 PROC_LOCK(p); 336 /* 337 * It may have been attached to, though unlikely. 338 * Try again if needed. 339 */ 340 } 341 342 KASSERT(p->p_filemon == NULL, 343 ("%s: proc %p didn't detach filemon %p", __func__, p, 344 p->p_filemon)); 345 p->p_filemon = filemon_acquire(filemon); 346 ++filemon->proccnt; 347 348 return (0); 349 } 350 351 static int 352 filemon_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag __unused, 353 struct thread *td) 354 { 355 int error = 0; 356 struct filemon *filemon; 357 struct proc *p; 358 cap_rights_t rights; 359 360 if ((error = devfs_get_cdevpriv((void **) &filemon)) != 0) 361 return (error); 362 363 sx_xlock(&filemon->lock); 364 365 switch (cmd) { 366 /* Set the output file descriptor. */ 367 case FILEMON_SET_FD: 368 if (filemon->fp != NULL) { 369 error = EEXIST; 370 break; 371 } 372 373 error = fget_write(td, *(int *)data, 374 cap_rights_init(&rights, CAP_PWRITE), 375 &filemon->fp); 376 if (error == 0) 377 /* Write the file header. */ 378 filemon_comment(filemon); 379 break; 380 381 /* Set the monitored process ID. */ 382 case FILEMON_SET_PID: 383 /* Invalidate any existing processes already set. */ 384 filemon_untrack_processes(filemon); 385 386 error = pget(*((pid_t *)data), PGET_CANDEBUG | PGET_NOTWEXIT, 387 &p); 388 if (error == 0) { 389 KASSERT(p->p_filemon != filemon, 390 ("%s: proc %p didn't untrack filemon %p", 391 __func__, p, filemon)); 392 error = filemon_attach_proc(filemon, p); 393 PROC_UNLOCK(p); 394 } 395 break; 396 397 default: 398 error = EINVAL; 399 break; 400 } 401 402 sx_xunlock(&filemon->lock); 403 return (error); 404 } 405 406 static int 407 filemon_open(struct cdev *dev, int oflags __unused, int devtype __unused, 408 struct thread *td __unused) 409 { 410 int error; 411 struct filemon *filemon; 412 413 filemon = malloc(sizeof(*filemon), M_FILEMON, 414 M_WAITOK | M_ZERO); 415 sx_init(&filemon->lock, "filemon"); 416 refcount_init(&filemon->refcnt, 1); 417 418 error = devfs_set_cdevpriv(filemon, filemon_dtr); 419 if (error != 0) 420 filemon_release(filemon); 421 422 return (error); 423 } 424 425 static int 426 filemon_close(struct cdev *dev __unused, int flag __unused, int fmt __unused, 427 struct thread *td __unused) 428 { 429 430 return (0); 431 } 432 433 static void 434 filemon_load(void *dummy __unused) 435 { 436 437 /* Install the syscall wrappers. */ 438 filemon_wrapper_install(); 439 440 filemon_dev = make_dev(&filemon_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, 441 "filemon"); 442 } 443 444 static int 445 filemon_unload(void) 446 { 447 448 destroy_dev(filemon_dev); 449 filemon_wrapper_deinstall(); 450 451 return (0); 452 } 453 454 static int 455 filemon_modevent(module_t mod __unused, int type, void *data) 456 { 457 int error = 0; 458 459 switch (type) { 460 case MOD_LOAD: 461 filemon_load(data); 462 break; 463 464 case MOD_UNLOAD: 465 error = filemon_unload(); 466 break; 467 468 case MOD_QUIESCE: 469 /* 470 * The wrapper implementation is unsafe for reliable unload. 471 * Require forcing an unload. 472 */ 473 error = EBUSY; 474 break; 475 476 case MOD_SHUTDOWN: 477 break; 478 479 default: 480 error = EOPNOTSUPP; 481 break; 482 483 } 484 485 return (error); 486 } 487 488 DEV_MODULE(filemon, filemon_modevent, NULL); 489 MODULE_VERSION(filemon, 1); 490