xref: /freebsd/sys/dev/filemon/filemon.c (revision ddd5b8e9b4d8957fce018c520657cdfa4ecffad3)
1 /*-
2  * Copyright (c) 2011, David E. O'Brien.
3  * Copyright (c) 2009-2011, Juniper Networks, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY JUNIPER NETWORKS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL JUNIPER NETWORKS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/file.h>
33 #include <sys/systm.h>
34 #include <sys/buf.h>
35 #include <sys/condvar.h>
36 #include <sys/conf.h>
37 #include <sys/fcntl.h>
38 #include <sys/ioccom.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/poll.h>
44 #include <sys/proc.h>
45 #include <sys/queue.h>
46 #include <sys/syscall.h>
47 #include <sys/sysent.h>
48 #include <sys/sysproto.h>
49 #include <sys/uio.h>
50 
51 #if __FreeBSD_version >= 900041
52 #include <sys/capability.h>
53 #endif
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 static int		filemon_unload(void);
71 static void		filemon_load(void *);
72 
73 static struct cdevsw filemon_cdevsw = {
74 	.d_version	= D_VERSION,
75 	.d_close	= filemon_close,
76 	.d_ioctl	= filemon_ioctl,
77 	.d_open		= filemon_open,
78 	.d_name		= "filemon",
79 };
80 
81 MALLOC_DECLARE(M_FILEMON);
82 MALLOC_DEFINE(M_FILEMON, "filemon", "File access monitor");
83 
84 struct filemon {
85 	TAILQ_ENTRY(filemon) link;	/* Link into the in-use list. */
86 	struct mtx	mtx;		/* Lock mutex for this filemon. */
87 	struct cv	cv;		/* Lock condition variable for this
88 					   filemon. */
89 	struct file	*fp;		/* Output file pointer. */
90 	struct thread	*locker;	/* Ptr to the thread locking this
91 					   filemon. */
92 	pid_t		pid;		/* The process ID being monitored. */
93 	char		fname1[MAXPATHLEN]; /* Temporary filename buffer. */
94 	char		fname2[MAXPATHLEN]; /* Temporary filename buffer. */
95 	char		msgbufr[1024];	/* Output message buffer. */
96 };
97 
98 static TAILQ_HEAD(, filemon) filemons_inuse = TAILQ_HEAD_INITIALIZER(filemons_inuse);
99 static TAILQ_HEAD(, filemon) filemons_free = TAILQ_HEAD_INITIALIZER(filemons_free);
100 static int n_readers = 0;
101 static struct mtx access_mtx;
102 static struct cv access_cv;
103 static struct thread *access_owner = NULL;
104 static struct thread *access_requester = NULL;
105 
106 static struct cdev *filemon_dev;
107 
108 #include "filemon_lock.c"
109 #include "filemon_wrapper.c"
110 
111 static void
112 filemon_dtr(void *data)
113 {
114 	struct filemon *filemon = data;
115 
116 	if (filemon != NULL) {
117 		struct file *fp = filemon->fp;
118 
119 		/* Get exclusive write access. */
120 		filemon_lock_write();
121 
122 		/* Remove from the in-use list. */
123 		TAILQ_REMOVE(&filemons_inuse, filemon, link);
124 
125 		filemon->fp = NULL;
126 		filemon->pid = -1;
127 
128 		/* Add to the free list. */
129 		TAILQ_INSERT_TAIL(&filemons_free, filemon, link);
130 
131 		/* Give up write access. */
132 		filemon_unlock_write();
133 
134 		if (fp != NULL)
135 			fdrop(fp, curthread);
136 	}
137 }
138 
139 #if __FreeBSD_version < 900041
140 #define FGET_WRITE(a1, a2, a3) fget_write((a1), (a2), (a3))
141 #else
142 #define FGET_WRITE(a1, a2, a3) fget_write((a1), (a2), CAP_WRITE | CAP_SEEK, (a3))
143 #endif
144 
145 static int
146 filemon_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag __unused,
147     struct thread *td)
148 {
149 	int error = 0;
150 	struct filemon *filemon;
151 
152 	devfs_get_cdevpriv((void **) &filemon);
153 
154 	switch (cmd) {
155 	/* Set the output file descriptor. */
156 	case FILEMON_SET_FD:
157 		if ((error = FGET_WRITE(td, *(int *)data, &filemon->fp)) == 0)
158 			/* Write the file header. */
159 			filemon_comment(filemon);
160 		break;
161 
162 	/* Set the monitored process ID. */
163 	case FILEMON_SET_PID:
164 		filemon->pid = *((pid_t *)data);
165 		break;
166 
167 	default:
168 		error = EINVAL;
169 		break;
170 	}
171 
172 	return (error);
173 }
174 
175 static int
176 filemon_open(struct cdev *dev, int oflags __unused, int devtype __unused,
177     struct thread *td __unused)
178 {
179 	struct filemon *filemon;
180 
181 	/* Get exclusive write access. */
182 	filemon_lock_write();
183 
184 	if ((filemon = TAILQ_FIRST(&filemons_free)) != NULL)
185 		TAILQ_REMOVE(&filemons_free, filemon, link);
186 
187 	/* Give up write access. */
188 	filemon_unlock_write();
189 
190 	if (filemon == NULL) {
191 		filemon = malloc(sizeof(struct filemon), M_FILEMON,
192 		    M_WAITOK | M_ZERO);
193 
194 		filemon->fp = NULL;
195 
196 		mtx_init(&filemon->mtx, "filemon", "filemon", MTX_DEF);
197 		cv_init(&filemon->cv, "filemon");
198 	}
199 
200 	filemon->pid = curproc->p_pid;
201 
202 	devfs_set_cdevpriv(filemon, filemon_dtr);
203 
204 	/* Get exclusive write access. */
205 	filemon_lock_write();
206 
207 	/* Add to the in-use list. */
208 	TAILQ_INSERT_TAIL(&filemons_inuse, filemon, link);
209 
210 	/* Give up write access. */
211 	filemon_unlock_write();
212 
213 	return (0);
214 }
215 
216 static int
217 filemon_close(struct cdev *dev __unused, int flag __unused, int fmt __unused,
218     struct thread *td __unused)
219 {
220 
221 	return (0);
222 }
223 
224 static void
225 filemon_load(void *dummy __unused)
226 {
227 	mtx_init(&access_mtx, "filemon", "filemon", MTX_DEF);
228 	cv_init(&access_cv, "filemon");
229 
230 	/* Install the syscall wrappers. */
231 	filemon_wrapper_install();
232 
233 	filemon_dev = make_dev(&filemon_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666,
234 	    "filemon");
235 }
236 
237 static int
238 filemon_unload(void)
239 {
240  	struct filemon *filemon;
241 	int error = 0;
242 
243 	/* Get exclusive write access. */
244 	filemon_lock_write();
245 
246 	if (TAILQ_FIRST(&filemons_inuse) != NULL)
247 		error = EBUSY;
248 	else {
249 		destroy_dev(filemon_dev);
250 
251 		/* Deinstall the syscall wrappers. */
252 		filemon_wrapper_deinstall();
253 	}
254 
255 	/* Give up write access. */
256 	filemon_unlock_write();
257 
258 	if (error == 0) {
259 		/* free() filemon structs free list. */
260 		filemon_lock_write();
261 		while ((filemon = TAILQ_FIRST(&filemons_free)) != NULL) {
262 			TAILQ_REMOVE(&filemons_free, filemon, link);
263 			mtx_destroy(&filemon->mtx);
264 			cv_destroy(&filemon->cv);
265 			free(filemon, M_FILEMON);
266 		}
267 		filemon_unlock_write();
268 
269 		mtx_destroy(&access_mtx);
270 		cv_destroy(&access_cv);
271 	}
272 
273 	return (error);
274 }
275 
276 static int
277 filemon_modevent(module_t mod __unused, int type, void *data)
278 {
279 	int error = 0;
280 
281 	switch (type) {
282 	case MOD_LOAD:
283 		filemon_load(data);
284 		break;
285 
286 	case MOD_UNLOAD:
287 		error = filemon_unload();
288 		break;
289 
290 	case MOD_SHUTDOWN:
291 		break;
292 
293 	default:
294 		error = EOPNOTSUPP;
295 		break;
296 
297 	}
298 
299 	return (error);
300 }
301 
302 DEV_MODULE(filemon, filemon_modevent, NULL);
303 MODULE_VERSION(filemon, 1);
304