xref: /freebsd/sys/dev/filemon/filemon.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*-
2  * Copyright (c) 2011, David E. O'Brien.
3  * Copyright (c) 2009-2011, Juniper Networks, Inc.
4  * Copyright (c) 2015, 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/condvar.h>
39 #include <sys/conf.h>
40 #include <sys/fcntl.h>
41 #include <sys/ioccom.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/poll.h>
47 #include <sys/proc.h>
48 #include <sys/queue.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 #if __FreeBSD_version >= 900041
56 #include <sys/capsicum.h>
57 #endif
58 
59 #include "filemon.h"
60 
61 #if defined(COMPAT_IA32) || defined(COMPAT_FREEBSD32) || defined(COMPAT_ARCH32)
62 #include <compat/freebsd32/freebsd32_syscall.h>
63 #include <compat/freebsd32/freebsd32_proto.h>
64 
65 extern struct sysentvec ia32_freebsd_sysvec;
66 #endif
67 
68 extern struct sysentvec elf32_freebsd_sysvec;
69 extern struct sysentvec elf64_freebsd_sysvec;
70 
71 static d_close_t	filemon_close;
72 static d_ioctl_t	filemon_ioctl;
73 static d_open_t		filemon_open;
74 static int		filemon_unload(void);
75 static void		filemon_load(void *);
76 
77 static struct cdevsw filemon_cdevsw = {
78 	.d_version	= D_VERSION,
79 	.d_close	= filemon_close,
80 	.d_ioctl	= filemon_ioctl,
81 	.d_open		= filemon_open,
82 	.d_name		= "filemon",
83 };
84 
85 MALLOC_DECLARE(M_FILEMON);
86 MALLOC_DEFINE(M_FILEMON, "filemon", "File access monitor");
87 
88 struct filemon {
89 	TAILQ_ENTRY(filemon) link;	/* Link into the in-use list. */
90 	struct sx	lock;		/* Lock mutex for this filemon. */
91 	struct file	*fp;		/* Output file pointer. */
92 	struct proc     *p;		/* The process 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 struct sx access_lock;
101 
102 static struct cdev *filemon_dev;
103 
104 #include "filemon_lock.c"
105 #include "filemon_wrapper.c"
106 
107 static void
108 filemon_comment(struct filemon *filemon)
109 {
110 	int len;
111 	struct timeval now;
112 
113 	getmicrotime(&now);
114 
115 	len = snprintf(filemon->msgbufr, sizeof(filemon->msgbufr),
116 	    "# filemon version %d\n# Target pid %d\n# Start %ju.%06ju\nV %d\n",
117 	    FILEMON_VERSION, curproc->p_pid, (uintmax_t)now.tv_sec,
118 	    (uintmax_t)now.tv_usec, FILEMON_VERSION);
119 
120 	filemon_output(filemon, filemon->msgbufr, len);
121 }
122 
123 static void
124 filemon_dtr(void *data)
125 {
126 	struct filemon *filemon = data;
127 
128 	if (filemon != NULL) {
129 		struct file *fp;
130 
131 		/* Follow same locking order as filemon_pid_check. */
132 		filemon_lock_write();
133 		filemon_filemon_lock(filemon);
134 
135 		/* Remove from the in-use list. */
136 		TAILQ_REMOVE(&filemons_inuse, filemon, link);
137 
138 		fp = filemon->fp;
139 		filemon->fp = NULL;
140 		filemon->p = NULL;
141 
142 		/* Add to the free list. */
143 		TAILQ_INSERT_TAIL(&filemons_free, filemon, link);
144 
145 		/* Give up write access. */
146 		filemon_filemon_unlock(filemon);
147 		filemon_unlock_write();
148 
149 		if (fp != NULL)
150 			fdrop(fp, curthread);
151 	}
152 }
153 
154 static int
155 filemon_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag __unused,
156     struct thread *td)
157 {
158 	int error = 0;
159 	struct filemon *filemon;
160 	struct proc *p;
161 #if __FreeBSD_version >= 900041
162 	cap_rights_t rights;
163 #endif
164 
165 	if ((error = devfs_get_cdevpriv((void **) &filemon)) != 0)
166 		return (error);
167 
168 	filemon_filemon_lock(filemon);
169 
170 	switch (cmd) {
171 	/* Set the output file descriptor. */
172 	case FILEMON_SET_FD:
173 		if (filemon->fp != NULL)
174 			fdrop(filemon->fp, td);
175 
176 		error = fget_write(td, *(int *)data,
177 #if __FreeBSD_version >= 900041
178 		    cap_rights_init(&rights, CAP_PWRITE),
179 #endif
180 		    &filemon->fp);
181 		if (error == 0)
182 			/* Write the file header. */
183 			filemon_comment(filemon);
184 		break;
185 
186 	/* Set the monitored process ID. */
187 	case FILEMON_SET_PID:
188 		error = pget(*((pid_t *)data), PGET_CANDEBUG | PGET_NOTWEXIT,
189 		    &p);
190 		if (error == 0) {
191 			filemon->p = p;
192 			PROC_UNLOCK(p);
193 		}
194 		break;
195 
196 	default:
197 		error = EINVAL;
198 		break;
199 	}
200 
201 	filemon_filemon_unlock(filemon);
202 	return (error);
203 }
204 
205 static int
206 filemon_open(struct cdev *dev, int oflags __unused, int devtype __unused,
207     struct thread *td __unused)
208 {
209 	struct filemon *filemon;
210 
211 	/* Get exclusive write access. */
212 	filemon_lock_write();
213 
214 	if ((filemon = TAILQ_FIRST(&filemons_free)) != NULL)
215 		TAILQ_REMOVE(&filemons_free, filemon, link);
216 
217 	/* Give up write access. */
218 	filemon_unlock_write();
219 
220 	if (filemon == NULL) {
221 		filemon = malloc(sizeof(struct filemon), M_FILEMON,
222 		    M_WAITOK | M_ZERO);
223 		sx_init(&filemon->lock, "filemon");
224 	}
225 
226 	devfs_set_cdevpriv(filemon, filemon_dtr);
227 
228 	/* Get exclusive write access. */
229 	filemon_lock_write();
230 
231 	/* Add to the in-use list. */
232 	TAILQ_INSERT_TAIL(&filemons_inuse, filemon, link);
233 
234 	/* Give up write access. */
235 	filemon_unlock_write();
236 
237 	return (0);
238 }
239 
240 static int
241 filemon_close(struct cdev *dev __unused, int flag __unused, int fmt __unused,
242     struct thread *td __unused)
243 {
244 
245 	return (0);
246 }
247 
248 static void
249 filemon_load(void *dummy __unused)
250 {
251 	sx_init(&access_lock, "filemons_inuse");
252 
253 	/* Install the syscall wrappers. */
254 	filemon_wrapper_install();
255 
256 	filemon_dev = make_dev(&filemon_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666,
257 	    "filemon");
258 }
259 
260 static int
261 filemon_unload(void)
262 {
263  	struct filemon *filemon;
264 	int error = 0;
265 
266 	/* Get exclusive write access. */
267 	filemon_lock_write();
268 
269 	if (TAILQ_FIRST(&filemons_inuse) != NULL)
270 		error = EBUSY;
271 	else {
272 		destroy_dev(filemon_dev);
273 
274 		/* Deinstall the syscall wrappers. */
275 		filemon_wrapper_deinstall();
276 	}
277 
278 	/* Give up write access. */
279 	filemon_unlock_write();
280 
281 	if (error == 0) {
282 		/* free() filemon structs free list. */
283 		filemon_lock_write();
284 		while ((filemon = TAILQ_FIRST(&filemons_free)) != NULL) {
285 			TAILQ_REMOVE(&filemons_free, filemon, link);
286 			sx_destroy(&filemon->lock);
287 			free(filemon, M_FILEMON);
288 		}
289 		filemon_unlock_write();
290 
291 		sx_destroy(&access_lock);
292 	}
293 
294 	return (error);
295 }
296 
297 static int
298 filemon_modevent(module_t mod __unused, int type, void *data)
299 {
300 	int error = 0;
301 
302 	switch (type) {
303 	case MOD_LOAD:
304 		filemon_load(data);
305 		break;
306 
307 	case MOD_UNLOAD:
308 		error = filemon_unload();
309 		break;
310 
311 	case MOD_SHUTDOWN:
312 		break;
313 
314 	default:
315 		error = EOPNOTSUPP;
316 		break;
317 
318 	}
319 
320 	return (error);
321 }
322 
323 DEV_MODULE(filemon, filemon_modevent, NULL);
324 MODULE_VERSION(filemon, 1);
325