xref: /freebsd/sys/dev/filemon/filemon.c (revision 4177d9f7d3bc8e8bbf8d7bc6fc0df86028e7b066)
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 	int		error;		/* Log write error, returned on close(2). */
96 	u_int		refcnt;		/* Pointer reference count. */
97 	u_int		proccnt;	/* Process count. */
98 };
99 
100 static struct cdev *filemon_dev;
101 static void filemon_output(struct filemon *filemon, char *msg, size_t len);
102 
103 static __inline struct filemon *
104 filemon_acquire(struct filemon *filemon)
105 {
106 
107 	if (filemon != NULL)
108 		refcount_acquire(&filemon->refcnt);
109 	return (filemon);
110 }
111 
112 /*
113  * Release a reference and free on the last one.
114  */
115 static void
116 filemon_release(struct filemon *filemon)
117 {
118 
119 	if (refcount_release(&filemon->refcnt) == 0)
120 		return;
121 	/*
122 	 * There are valid cases of releasing while locked, such as in
123 	 * filemon_untrack_processes, but none which are done where there
124 	 * is not at least 1 reference remaining.
125 	 */
126 	sx_assert(&filemon->lock, SA_UNLOCKED);
127 
128 	sx_destroy(&filemon->lock);
129 	free(filemon, M_FILEMON);
130 }
131 
132 /*
133  * Acquire the proc's p_filemon reference and lock the filemon.
134  * The proc's p_filemon may not match this filemon on return.
135  */
136 static struct filemon *
137 filemon_proc_get(struct proc *p)
138 {
139 	struct filemon *filemon;
140 
141 	PROC_LOCK(p);
142 	filemon = filemon_acquire(p->p_filemon);
143 	PROC_UNLOCK(p);
144 
145 	if (filemon == NULL)
146 		return (NULL);
147 	/*
148 	 * The p->p_filemon may have changed by now.  That case is handled
149 	 * by the exit and fork hooks and filemon_attach_proc specially.
150 	 */
151 	sx_xlock(&filemon->lock);
152 	return (filemon);
153 }
154 
155 /* Remove and release the filemon on the given process. */
156 static void
157 filemon_proc_drop(struct proc *p)
158 {
159 	struct filemon *filemon;
160 
161 	KASSERT(p->p_filemon != NULL, ("%s: proc %p NULL p_filemon",
162 	    __func__, p));
163 	sx_assert(&p->p_filemon->lock, SA_XLOCKED);
164 	PROC_LOCK(p);
165 	filemon = p->p_filemon;
166 	p->p_filemon = NULL;
167 	--filemon->proccnt;
168 	PROC_UNLOCK(p);
169 	/*
170 	 * This should not be the last reference yet.  filemon_release()
171 	 * cannot be called with filemon locked, which the caller expects
172 	 * will stay locked.
173 	 */
174 	KASSERT(filemon->refcnt > 1, ("%s: proc %p dropping filemon %p "
175 	    "with last reference", __func__, p, filemon));
176 	filemon_release(filemon);
177 }
178 
179 /* Unlock and release the filemon. */
180 static __inline void
181 filemon_drop(struct filemon *filemon)
182 {
183 
184 	sx_xunlock(&filemon->lock);
185 	filemon_release(filemon);
186 }
187 
188 #include "filemon_wrapper.c"
189 
190 static void
191 filemon_comment(struct filemon *filemon)
192 {
193 	int len;
194 	struct timeval now;
195 
196 	getmicrotime(&now);
197 
198 	len = snprintf(filemon->msgbufr, sizeof(filemon->msgbufr),
199 	    "# filemon version %d\n# Target pid %d\n# Start %ju.%06ju\nV %d\n",
200 	    FILEMON_VERSION, curproc->p_pid, (uintmax_t)now.tv_sec,
201 	    (uintmax_t)now.tv_usec, FILEMON_VERSION);
202 
203 	filemon_output(filemon, filemon->msgbufr, len);
204 }
205 
206 /*
207  * Invalidate the passed filemon in all processes.
208  */
209 static void
210 filemon_untrack_processes(struct filemon *filemon)
211 {
212 	struct proc *p;
213 
214 	sx_assert(&filemon->lock, SA_XLOCKED);
215 
216 	/* Avoid allproc loop if there is no need. */
217 	if (filemon->proccnt == 0)
218 		return;
219 
220 	/*
221 	 * Processes in this list won't go away while here since
222 	 * filemon_event_process_exit() will lock on filemon->lock
223 	 * which we hold.
224 	 */
225 	sx_slock(&allproc_lock);
226 	FOREACH_PROC_IN_SYSTEM(p) {
227 		/*
228 		 * No PROC_LOCK is needed to compare here since it is
229 		 * guaranteed to not change since we have its filemon
230 		 * locked.  Everything that changes this p_filemon will
231 		 * be locked on it.
232 		 */
233 		if (p->p_filemon == filemon)
234 			filemon_proc_drop(p);
235 	}
236 	sx_sunlock(&allproc_lock);
237 
238 	/*
239 	 * It's possible some references were acquired but will be
240 	 * dropped shortly as they are restricted from being
241 	 * inherited.  There is at least the reference in cdevpriv remaining.
242 	 */
243 	KASSERT(filemon->refcnt > 0, ("%s: filemon %p should have "
244 	    "references still.", __func__, filemon));
245 	KASSERT(filemon->proccnt == 0, ("%s: filemon %p should not have "
246 	    "attached procs still.", __func__, filemon));
247 }
248 
249 /*
250  * Close out the log.
251  */
252 static void
253 filemon_close_log(struct filemon *filemon)
254 {
255 	struct file *fp;
256 	struct timeval now;
257 	size_t len;
258 
259 	sx_assert(&filemon->lock, SA_XLOCKED);
260 	if (filemon->fp == NULL)
261 		return;
262 
263 	getmicrotime(&now);
264 
265 	len = snprintf(filemon->msgbufr,
266 	    sizeof(filemon->msgbufr),
267 	    "# Stop %ju.%06ju\n# Bye bye\n",
268 	    (uintmax_t)now.tv_sec, (uintmax_t)now.tv_usec);
269 
270 	filemon_output(filemon, filemon->msgbufr, len);
271 	fp = filemon->fp;
272 	filemon->fp = NULL;
273 
274 	sx_xunlock(&filemon->lock);
275 	fdrop(fp, curthread);
276 	sx_xlock(&filemon->lock);
277 
278 	return;
279 }
280 
281 /*
282  * The devfs file is being closed.  Untrace all processes.  It is possible
283  * filemon_close/close(2) was not called.
284  */
285 static void
286 filemon_dtr(void *data)
287 {
288 	struct filemon *filemon = data;
289 
290 	if (filemon == NULL)
291 		return;
292 
293 	sx_xlock(&filemon->lock);
294 	/*
295 	 * Detach the filemon.  It cannot be inherited after this.
296 	 */
297 	filemon_untrack_processes(filemon);
298 	filemon_close_log(filemon);
299 	filemon_drop(filemon);
300 }
301 
302 /* Attach the filemon to the process. */
303 static int
304 filemon_attach_proc(struct filemon *filemon, struct proc *p)
305 {
306 	struct filemon *filemon2;
307 
308 	sx_assert(&filemon->lock, SA_XLOCKED);
309 	PROC_LOCK_ASSERT(p, MA_OWNED);
310 	KASSERT((p->p_flag & P_WEXIT) == 0,
311 	    ("%s: filemon %p attaching to exiting process %p",
312 	    __func__, filemon, p));
313 
314 	if (p->p_filemon == filemon)
315 		return (0);
316 	/*
317 	 * Don't allow truncating other process traces.  It is
318 	 * not really intended to trace procs other than curproc
319 	 * anyhow.
320 	 */
321 	if (p->p_filemon != NULL && p != curproc)
322 		return (EBUSY);
323 	/*
324 	 * Historic behavior of filemon has been to let a child initiate
325 	 * tracing on itself and cease existing tracing.  Bmake
326 	 * .META + .MAKE relies on this.  It is only relevant for attaching to
327 	 * curproc.
328 	 */
329 	while (p->p_filemon != NULL) {
330 		PROC_UNLOCK(p);
331 		sx_xunlock(&filemon->lock);
332 		while ((filemon2 = filemon_proc_get(p)) != NULL) {
333 			/* It may have changed. */
334 			if (p->p_filemon == filemon2)
335 				filemon_proc_drop(p);
336 			filemon_drop(filemon2);
337 		}
338 		sx_xlock(&filemon->lock);
339 		PROC_LOCK(p);
340 		/*
341 		 * It may have been attached to, though unlikely.
342 		 * Try again if needed.
343 		 */
344 	}
345 
346 	KASSERT(p->p_filemon == NULL,
347 	    ("%s: proc %p didn't detach filemon %p", __func__, p,
348 	    p->p_filemon));
349 	p->p_filemon = filemon_acquire(filemon);
350 	++filemon->proccnt;
351 
352 	return (0);
353 }
354 
355 static int
356 filemon_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag __unused,
357     struct thread *td)
358 {
359 	int error = 0;
360 	struct filemon *filemon;
361 	struct proc *p;
362 	cap_rights_t rights;
363 
364 	if ((error = devfs_get_cdevpriv((void **) &filemon)) != 0)
365 		return (error);
366 
367 	sx_xlock(&filemon->lock);
368 
369 	switch (cmd) {
370 	/* Set the output file descriptor. */
371 	case FILEMON_SET_FD:
372 		if (filemon->fp != NULL) {
373 			error = EEXIST;
374 			break;
375 		}
376 
377 		error = fget_write(td, *(int *)data,
378 		    cap_rights_init(&rights, CAP_PWRITE),
379 		    &filemon->fp);
380 		if (error == 0)
381 			/* Write the file header. */
382 			filemon_comment(filemon);
383 		break;
384 
385 	/* Set the monitored process ID. */
386 	case FILEMON_SET_PID:
387 		/* Invalidate any existing processes already set. */
388 		filemon_untrack_processes(filemon);
389 
390 		error = pget(*((pid_t *)data), PGET_CANDEBUG | PGET_NOTWEXIT,
391 		    &p);
392 		if (error == 0) {
393 			KASSERT(p->p_filemon != filemon,
394 			    ("%s: proc %p didn't untrack filemon %p",
395 			    __func__, p, filemon));
396 			error = filemon_attach_proc(filemon, p);
397 			PROC_UNLOCK(p);
398 		}
399 		break;
400 
401 	default:
402 		error = EINVAL;
403 		break;
404 	}
405 
406 	sx_xunlock(&filemon->lock);
407 	return (error);
408 }
409 
410 static int
411 filemon_open(struct cdev *dev, int oflags __unused, int devtype __unused,
412     struct thread *td __unused)
413 {
414 	int error;
415 	struct filemon *filemon;
416 
417 	filemon = malloc(sizeof(*filemon), M_FILEMON,
418 	    M_WAITOK | M_ZERO);
419 	sx_init(&filemon->lock, "filemon");
420 	refcount_init(&filemon->refcnt, 1);
421 
422 	error = devfs_set_cdevpriv(filemon, filemon_dtr);
423 	if (error != 0)
424 		filemon_release(filemon);
425 
426 	return (error);
427 }
428 
429 /* Called on close of last devfs file handle, before filemon_dtr(). */
430 static int
431 filemon_close(struct cdev *dev __unused, int flag __unused, int fmt __unused,
432     struct thread *td __unused)
433 {
434 	struct filemon *filemon;
435 	int error;
436 
437 	if ((error = devfs_get_cdevpriv((void **) &filemon)) != 0)
438 		return (error);
439 
440 	sx_xlock(&filemon->lock);
441 	filemon_close_log(filemon);
442 	error = filemon->error;
443 	sx_xunlock(&filemon->lock);
444 	/*
445 	 * Processes are still being traced but won't log anything
446 	 * now.  After this call returns filemon_dtr() is called which
447 	 * will detach processes.
448 	 */
449 
450 	return (error);
451 }
452 
453 static void
454 filemon_load(void *dummy __unused)
455 {
456 
457 	/* Install the syscall wrappers. */
458 	filemon_wrapper_install();
459 
460 	filemon_dev = make_dev(&filemon_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666,
461 	    "filemon");
462 }
463 
464 static int
465 filemon_unload(void)
466 {
467 
468 	destroy_dev(filemon_dev);
469 	filemon_wrapper_deinstall();
470 
471 	return (0);
472 }
473 
474 static int
475 filemon_modevent(module_t mod __unused, int type, void *data)
476 {
477 	int error = 0;
478 
479 	switch (type) {
480 	case MOD_LOAD:
481 		filemon_load(data);
482 		break;
483 
484 	case MOD_UNLOAD:
485 		error = filemon_unload();
486 		break;
487 
488 	case MOD_QUIESCE:
489 		/*
490 		 * The wrapper implementation is unsafe for reliable unload.
491 		 * Require forcing an unload.
492 		 */
493 		error = EBUSY;
494 		break;
495 
496 	case MOD_SHUTDOWN:
497 		break;
498 
499 	default:
500 		error = EOPNOTSUPP;
501 		break;
502 
503 	}
504 
505 	return (error);
506 }
507 
508 DEV_MODULE(filemon, filemon_modevent, NULL);
509 MODULE_VERSION(filemon, 1);
510