kern_veriexec.c revision 1.10 1 /* $NetBSD: kern_veriexec.c,v 1.10 2015/08/04 11:42:08 maxv Exp $ */
2
3 /*-
4 * Copyright (c) 2005, 2006 Elad Efrat <elad (at) NetBSD.org>
5 * Copyright (c) 2005, 2006 Brett Lymn <blymn (at) NetBSD.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the authors may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: kern_veriexec.c,v 1.10 2015/08/04 11:42:08 maxv Exp $");
33
34 #include "opt_veriexec.h"
35
36 #include <sys/param.h>
37 #include <sys/mount.h>
38 #include <sys/kmem.h>
39 #include <sys/vnode.h>
40 #include <sys/namei.h>
41 #include <sys/exec.h>
42 #include <sys/once.h>
43 #include <sys/proc.h>
44 #include <sys/rwlock.h>
45 #include <sys/syslog.h>
46 #include <sys/sysctl.h>
47 #include <sys/inttypes.h>
48 #include <sys/verified_exec.h>
49 #include <sys/sha1.h>
50 #include <sys/sha2.h>
51 #include <sys/rmd160.h>
52 #include <sys/md5.h>
53 #include <uvm/uvm_extern.h>
54 #include <sys/fileassoc.h>
55 #include <sys/kauth.h>
56 #include <sys/conf.h>
57 #include <miscfs/specfs/specdev.h>
58 #include <prop/proplib.h>
59 #include <sys/fcntl.h>
60
61 /* Readable values for veriexec_file_report(). */
62 #define REPORT_ALWAYS 0x01 /* Always print */
63 #define REPORT_VERBOSE 0x02 /* Print when verbose >= 1 */
64 #define REPORT_DEBUG 0x04 /* Print when verbose >= 2 (debug) */
65 #define REPORT_PANIC 0x08 /* Call panic() */
66 #define REPORT_ALARM 0x10 /* Alarm - also print pid/uid/.. */
67 #define REPORT_LOGMASK (REPORT_ALWAYS|REPORT_VERBOSE|REPORT_DEBUG)
68
69 /* state of locking for veriexec_file_verify */
70 #define VERIEXEC_UNLOCKED 0x00 /* Nothing locked, callee does it */
71 #define VERIEXEC_LOCKED 0x01 /* Global op lock held */
72
73 /* state of file locking for veriexec_file_verify */
74 #define VERIEXEC_FILE_UNLOCKED 0x02 /* Nothing locked, callee does it */
75 #define VERIEXEC_FILE_LOCKED 0x04 /* File locked */
76
77 #define VERIEXEC_RW_UPGRADE(lock) while((rw_tryupgrade(lock)) == 0){};
78
79 struct veriexec_fpops {
80 const char *type;
81 size_t hash_len;
82 size_t context_size;
83 veriexec_fpop_init_t init;
84 veriexec_fpop_update_t update;
85 veriexec_fpop_final_t final;
86 LIST_ENTRY(veriexec_fpops) entries;
87 };
88
89 /* Veriexec per-file entry data. */
90 struct veriexec_file_entry {
91 krwlock_t lock; /* r/w lock */
92 u_char *filename; /* File name. */
93 u_char type; /* Entry type. */
94 u_char status; /* Evaluation status. */
95 u_char *fp; /* Fingerprint. */
96 struct veriexec_fpops *ops; /* Fingerprint ops vector*/
97 size_t filename_len; /* Length of filename. */
98 };
99
100 /* Veriexec per-table data. */
101 struct veriexec_table_entry {
102 uint64_t vte_count; /* Number of Veriexec entries. */
103 const struct sysctlnode *vte_node;
104 };
105
106 static int veriexec_verbose;
107 static int veriexec_strict;
108 static int veriexec_bypass = 1;
109
110 static char *veriexec_fp_names = NULL;
111 static size_t veriexec_name_max = 0;
112
113 static const struct sysctlnode *veriexec_count_node;
114
115 static fileassoc_t veriexec_hook;
116 static specificdata_key_t veriexec_mountspecific_key;
117
118 static LIST_HEAD(, veriexec_fpops) veriexec_fpops_list =
119 LIST_HEAD_INITIALIZER(veriexec_fpops_list);
120
121 static int veriexec_raw_cb(kauth_cred_t, kauth_action_t, void *,
122 void *, void *, void *, void *);
123 static struct veriexec_fpops *veriexec_fpops_lookup(const char *);
124 static void veriexec_file_free(struct veriexec_file_entry *);
125
126 static unsigned int veriexec_tablecount = 0;
127
128 /*
129 * Veriexec operations global lock - most ops hold this as a read
130 * lock, it is upgraded to a write lock when destroying veriexec file
131 * table entries.
132 */
133 static krwlock_t veriexec_op_lock;
134
135 /*
136 * Sysctl helper routine for Veriexec.
137 */
138 static int
139 sysctl_kern_veriexec_algorithms(SYSCTLFN_ARGS)
140 {
141 size_t len;
142 int error;
143 const char *p;
144
145 if (newp != NULL)
146 return EPERM;
147
148 if (namelen != 0)
149 return EINVAL;
150
151 p = veriexec_fp_names == NULL ? "" : veriexec_fp_names;
152
153 len = strlen(p) + 1;
154
155 if (*oldlenp < len && oldp)
156 return ENOMEM;
157
158 if (oldp && (error = copyout(p, oldp, len)) != 0)
159 return error;
160
161 *oldlenp = len;
162 return 0;
163 }
164
165 static int
166 sysctl_kern_veriexec_strict(SYSCTLFN_ARGS)
167 {
168 struct sysctlnode node;
169 int error, newval;
170
171 node = *rnode;
172 node.sysctl_data = &newval;
173
174 newval = veriexec_strict;
175 error = sysctl_lookup(SYSCTLFN_CALL(&node));
176 if (error || newp == NULL)
177 return error;
178
179 if (newval < veriexec_strict)
180 return EPERM;
181
182 veriexec_strict = newval;
183
184 return 0;
185 }
186
187 SYSCTL_SETUP(sysctl_kern_veriexec_setup, "sysctl kern.veriexec setup")
188 {
189 const struct sysctlnode *rnode = NULL;
190
191 sysctl_createv(clog, 0, NULL, &rnode,
192 CTLFLAG_PERMANENT,
193 CTLTYPE_NODE, "veriexec",
194 SYSCTL_DESCR("Veriexec"),
195 NULL, 0, NULL, 0,
196 CTL_KERN, CTL_CREATE, CTL_EOL);
197
198 sysctl_createv(clog, 0, &rnode, NULL,
199 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
200 CTLTYPE_INT, "verbose",
201 SYSCTL_DESCR("Veriexec verbose level"),
202 NULL, 0, &veriexec_verbose, 0,
203 CTL_CREATE, CTL_EOL);
204 sysctl_createv(clog, 0, &rnode, NULL,
205 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
206 CTLTYPE_INT, "strict",
207 SYSCTL_DESCR("Veriexec strict level"),
208 sysctl_kern_veriexec_strict, 0, NULL, 0,
209 CTL_CREATE, CTL_EOL);
210 sysctl_createv(clog, 0, &rnode, NULL,
211 CTLFLAG_PERMANENT,
212 CTLTYPE_STRING, "algorithms",
213 SYSCTL_DESCR("Veriexec supported hashing "
214 "algorithms"),
215 sysctl_kern_veriexec_algorithms, 0, NULL, 0,
216 CTL_CREATE, CTL_EOL);
217 sysctl_createv(clog, 0, &rnode, &veriexec_count_node,
218 CTLFLAG_PERMANENT,
219 CTLTYPE_NODE, "count",
220 SYSCTL_DESCR("Number of fingerprints on mount(s)"),
221 NULL, 0, NULL, 0,
222 CTL_CREATE, CTL_EOL);
223 }
224
225 /*
226 * Add ops to the fingerprint ops vector list.
227 */
228 int
229 veriexec_fpops_add(const char *fp_type, size_t hash_len, size_t ctx_size,
230 veriexec_fpop_init_t init, veriexec_fpop_update_t update,
231 veriexec_fpop_final_t final)
232 {
233 struct veriexec_fpops *ops;
234
235 KASSERT((init != NULL) && (update != NULL) && (final != NULL));
236 KASSERT((hash_len != 0) && (ctx_size != 0));
237 KASSERT(fp_type != NULL);
238
239 if (veriexec_fpops_lookup(fp_type) != NULL)
240 return (EEXIST);
241
242 ops = kmem_alloc(sizeof(*ops), KM_SLEEP);
243 ops->type = fp_type;
244 ops->hash_len = hash_len;
245 ops->context_size = ctx_size;
246 ops->init = init;
247 ops->update = update;
248 ops->final = final;
249
250 LIST_INSERT_HEAD(&veriexec_fpops_list, ops, entries);
251
252 /*
253 * If we don't have space for any names, allocate enough for six
254 * which should be sufficient. (it's also enough for all algorithms
255 * we can support at the moment)
256 */
257 if (veriexec_fp_names == NULL) {
258 veriexec_name_max = 64;
259 veriexec_fp_names = kmem_zalloc(veriexec_name_max, KM_SLEEP);
260 }
261
262 /*
263 * If we're running out of space for storing supported algorithms,
264 * extend the buffer with space for four names.
265 */
266 while (veriexec_name_max - (strlen(veriexec_fp_names) + 1) <
267 strlen(fp_type)) {
268 char *newp;
269 unsigned int new_max;
270
271 /* Add space for four algorithm names. */
272 new_max = veriexec_name_max + 64;
273 newp = kmem_zalloc(new_max, KM_SLEEP);
274 strlcpy(newp, veriexec_fp_names, new_max);
275 kmem_free(veriexec_fp_names, veriexec_name_max);
276 veriexec_fp_names = newp;
277 veriexec_name_max = new_max;
278 }
279
280 if (*veriexec_fp_names != '\0')
281 strlcat(veriexec_fp_names, " ", veriexec_name_max);
282
283 strlcat(veriexec_fp_names, fp_type, veriexec_name_max);
284
285 return (0);
286 }
287
288 static void
289 veriexec_mountspecific_dtor(void *v)
290 {
291 struct veriexec_table_entry *vte = v;
292
293 if (vte == NULL) {
294 return;
295 }
296 sysctl_free(__UNCONST(vte->vte_node));
297 veriexec_tablecount--;
298 kmem_free(vte, sizeof(*vte));
299 }
300
301 static int
302 veriexec_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
303 void *arg0, void *arg1, void *arg2, void *arg3)
304 {
305 int result;
306 enum kauth_system_req req;
307
308 if (action != KAUTH_SYSTEM_VERIEXEC)
309 return KAUTH_RESULT_DEFER;
310
311 result = KAUTH_RESULT_DEFER;
312 req = (enum kauth_system_req)arg0;
313
314 if (req == KAUTH_REQ_SYSTEM_VERIEXEC_MODIFY &&
315 veriexec_strict > VERIEXEC_LEARNING) {
316 log(LOG_WARNING, "Veriexec: Strict mode, modifying "
317 "tables not permitted.\n");
318
319 result = KAUTH_RESULT_DENY;
320 }
321
322 return result;
323 }
324
325 /*
326 * Initialise Veriexec.
327 */
328 void
329 veriexec_init(void)
330 {
331 int error;
332
333 /* Register a fileassoc for Veriexec. */
334 error = fileassoc_register("veriexec",
335 (fileassoc_cleanup_cb_t)veriexec_file_free, &veriexec_hook);
336 if (error)
337 panic("Veriexec: Can't register fileassoc: error=%d", error);
338
339 /* Register listener to handle raw disk access. */
340 if (kauth_listen_scope(KAUTH_SCOPE_DEVICE, veriexec_raw_cb, NULL) ==
341 NULL)
342 panic("Veriexec: Can't listen on device scope");
343
344 error = mount_specific_key_create(&veriexec_mountspecific_key,
345 veriexec_mountspecific_dtor);
346 if (error)
347 panic("Veriexec: Can't create mountspecific key");
348
349 if (kauth_listen_scope(KAUTH_SCOPE_SYSTEM, veriexec_listener_cb,
350 NULL) == NULL)
351 panic("Veriexec: Can't listen on system scope");
352
353 rw_init(&veriexec_op_lock);
354
355 #define FPOPS_ADD(a, b, c, d, e, f) \
356 veriexec_fpops_add(a, b, c, (veriexec_fpop_init_t)d, \
357 (veriexec_fpop_update_t)e, (veriexec_fpop_final_t)f)
358
359 #ifdef VERIFIED_EXEC_FP_RMD160
360 FPOPS_ADD("RMD160", RMD160_DIGEST_LENGTH, sizeof(RMD160_CTX),
361 RMD160Init, RMD160Update, RMD160Final);
362 #endif /* VERIFIED_EXEC_FP_RMD160 */
363
364 #ifdef VERIFIED_EXEC_FP_SHA256
365 FPOPS_ADD("SHA256", SHA256_DIGEST_LENGTH, sizeof(SHA256_CTX),
366 SHA256_Init, SHA256_Update, SHA256_Final);
367 #endif /* VERIFIED_EXEC_FP_SHA256 */
368
369 #ifdef VERIFIED_EXEC_FP_SHA384
370 FPOPS_ADD("SHA384", SHA384_DIGEST_LENGTH, sizeof(SHA384_CTX),
371 SHA384_Init, SHA384_Update, SHA384_Final);
372 #endif /* VERIFIED_EXEC_FP_SHA384 */
373
374 #ifdef VERIFIED_EXEC_FP_SHA512
375 FPOPS_ADD("SHA512", SHA512_DIGEST_LENGTH, sizeof(SHA512_CTX),
376 SHA512_Init, SHA512_Update, SHA512_Final);
377 #endif /* VERIFIED_EXEC_FP_SHA512 */
378
379 #ifdef VERIFIED_EXEC_FP_SHA1
380 FPOPS_ADD("SHA1", SHA1_DIGEST_LENGTH, sizeof(SHA1_CTX),
381 SHA1Init, SHA1Update, SHA1Final);
382 #endif /* VERIFIED_EXEC_FP_SHA1 */
383
384 #ifdef VERIFIED_EXEC_FP_MD5
385 FPOPS_ADD("MD5", MD5_DIGEST_LENGTH, sizeof(MD5_CTX),
386 MD5Init, MD5Update, MD5Final);
387 #endif /* VERIFIED_EXEC_FP_MD5 */
388
389 #undef FPOPS_ADD
390 }
391
392 static struct veriexec_fpops *
393 veriexec_fpops_lookup(const char *name)
394 {
395 struct veriexec_fpops *ops;
396
397 if (name == NULL)
398 return (NULL);
399
400 LIST_FOREACH(ops, &veriexec_fpops_list, entries) {
401 if (strcasecmp(name, ops->type) == 0)
402 return (ops);
403 }
404
405 return (NULL);
406 }
407
408 /*
409 * Calculate fingerprint. Information on hash length and routines used is
410 * extracted from veriexec_hash_list according to the hash type.
411 *
412 * NOTE: vfe is assumed to be locked for writing on entry.
413 */
414 static int
415 veriexec_fp_calc(struct lwp *l, struct vnode *vp, int file_lock_state,
416 struct veriexec_file_entry *vfe, u_char *fp)
417 {
418 struct vattr va;
419 void *ctx;
420 u_char *buf;
421 off_t offset, len;
422 size_t resid;
423 int error;
424
425 KASSERT(file_lock_state != VERIEXEC_LOCKED);
426 KASSERT(file_lock_state != VERIEXEC_UNLOCKED);
427
428 if (file_lock_state == VERIEXEC_FILE_UNLOCKED)
429 vn_lock(vp, LK_SHARED | LK_RETRY);
430 error = VOP_GETATTR(vp, &va, l->l_cred);
431 if (file_lock_state == VERIEXEC_FILE_UNLOCKED)
432 VOP_UNLOCK(vp);
433 if (error)
434 return (error);
435
436 ctx = kmem_alloc(vfe->ops->context_size, KM_SLEEP);
437 buf = kmem_alloc(PAGE_SIZE, KM_SLEEP);
438
439 (vfe->ops->init)(ctx);
440
441 len = 0;
442 error = 0;
443 for (offset = 0; offset < va.va_size; offset += PAGE_SIZE) {
444 len = ((va.va_size - offset) < PAGE_SIZE) ?
445 (va.va_size - offset) : PAGE_SIZE;
446
447 error = vn_rdwr(UIO_READ, vp, buf, len, offset,
448 UIO_SYSSPACE,
449 ((file_lock_state == VERIEXEC_FILE_LOCKED)?
450 IO_NODELOCKED : 0),
451 l->l_cred, &resid, NULL);
452
453 if (error) {
454 goto bad;
455 }
456
457 (vfe->ops->update)(ctx, buf, (unsigned int) len);
458
459 if (len != PAGE_SIZE)
460 break;
461 }
462
463 (vfe->ops->final)(fp, ctx);
464
465 bad:
466 kmem_free(ctx, vfe->ops->context_size);
467 kmem_free(buf, PAGE_SIZE);
468
469 return (error);
470 }
471
472 /* Compare two fingerprints of the same type. */
473 static int
474 veriexec_fp_cmp(struct veriexec_fpops *ops, u_char *fp1, u_char *fp2)
475 {
476 if (veriexec_verbose >= 2) {
477 int i;
478
479 printf("comparing hashes...\n");
480 printf("fp1: ");
481 for (i = 0; i < ops->hash_len; i++) {
482 printf("%02x", fp1[i]);
483 }
484 printf("\nfp2: ");
485 for (i = 0; i < ops->hash_len; i++) {
486 printf("%02x", fp2[i]);
487 }
488 printf("\n");
489 }
490
491 return (memcmp(fp1, fp2, ops->hash_len));
492 }
493
494 static int
495 veriexec_fp_status(struct lwp *l, struct vnode *vp, int file_lock_state,
496 struct veriexec_file_entry *vfe, u_char *status)
497 {
498 size_t hash_len = vfe->ops->hash_len;
499 u_char *digest;
500 int error;
501
502 digest = kmem_zalloc(hash_len, KM_SLEEP);
503
504 error = veriexec_fp_calc(l, vp, file_lock_state, vfe, digest);
505 if (error)
506 goto out;
507
508 /* Compare fingerprint with loaded data. */
509 if (veriexec_fp_cmp(vfe->ops, vfe->fp, digest) == 0)
510 *status = FINGERPRINT_VALID;
511 else
512 *status = FINGERPRINT_NOMATCH;
513
514 out:
515 kmem_free(digest, hash_len);
516 return error;
517 }
518
519
520 static struct veriexec_table_entry *
521 veriexec_table_lookup(struct mount *mp)
522 {
523 /* XXX: From raidframe init */
524 if (mp == NULL)
525 return NULL;
526
527 return mount_getspecific(mp, veriexec_mountspecific_key);
528 }
529
530 static struct veriexec_file_entry *
531 veriexec_get(struct vnode *vp)
532 {
533 return (fileassoc_lookup(vp, veriexec_hook));
534 }
535
536 bool
537 veriexec_lookup(struct vnode *vp)
538 {
539 return (veriexec_get(vp) == NULL ? false : true);
540 }
541
542 /*
543 * Routine for maintaining mostly consistent message formats in Veriexec.
544 */
545 static void
546 veriexec_file_report(struct veriexec_file_entry *vfe, const u_char *msg,
547 const u_char *filename, struct lwp *l, int f)
548 {
549 if (vfe != NULL && vfe->filename != NULL)
550 filename = vfe->filename;
551 if (filename == NULL)
552 return;
553
554 if (((f & REPORT_LOGMASK) >> 1) <= veriexec_verbose) {
555 if (!(f & REPORT_ALARM) || (l == NULL))
556 log(LOG_NOTICE, "Veriexec: %s [%s]\n", msg,
557 filename);
558 else
559 log(LOG_ALERT, "Veriexec: %s [%s, prog=%s pid=%u, "
560 "uid=%u, gid=%u]\n", msg, filename,
561 l->l_proc->p_comm, l->l_proc->p_pid,
562 kauth_cred_getuid(l->l_cred),
563 kauth_cred_getgid(l->l_cred));
564 }
565
566 if (f & REPORT_PANIC)
567 panic("Veriexec: Unrecoverable error.");
568 }
569
570 /*
571 * Verify the fingerprint of the given file. If we're called directly from
572 * sys_execve(), 'flag' will be VERIEXEC_DIRECT. If we're called from
573 * exec_script(), 'flag' will be VERIEXEC_INDIRECT. If we are called from
574 * vn_open(), 'flag' will be VERIEXEC_FILE.
575 *
576 * 'veriexec_op_lock' must be locked (and remains locked).
577 *
578 * NOTE: The veriexec file entry pointer (vfep) will be returned LOCKED
579 * on no error.
580 */
581 static int
582 veriexec_file_verify(struct lwp *l, struct vnode *vp, const u_char *name,
583 int flag, int file_lock_state, struct veriexec_file_entry **vfep)
584 {
585 struct veriexec_file_entry *vfe;
586 int error = 0;
587
588 KASSERT(rw_lock_held(&veriexec_op_lock));
589 KASSERT(file_lock_state != VERIEXEC_LOCKED);
590 KASSERT(file_lock_state != VERIEXEC_UNLOCKED);
591
592 #define VFE_NEEDS_EVAL(vfe) ((vfe->status == FINGERPRINT_NOTEVAL) || \
593 (vfe->type & VERIEXEC_UNTRUSTED))
594
595 if (vfep != NULL)
596 *vfep = NULL;
597
598 if (vp->v_type != VREG)
599 return (0);
600
601 /* Lookup veriexec table entry, save pointer if requested. */
602 vfe = veriexec_get(vp);
603 if (vfep != NULL)
604 *vfep = vfe;
605
606 /* No entry in the veriexec tables. */
607 if (vfe == NULL) {
608 veriexec_file_report(NULL, "No entry.", name,
609 l, REPORT_VERBOSE);
610
611 /*
612 * Lockdown mode: Deny access to non-monitored files.
613 * IPS mode: Deny execution of non-monitored files.
614 */
615 if ((veriexec_strict >= VERIEXEC_LOCKDOWN) ||
616 ((veriexec_strict >= VERIEXEC_IPS) &&
617 (flag != VERIEXEC_FILE)))
618 return (EPERM);
619
620 return (0);
621 }
622
623 /*
624 * Grab the lock for the entry, if we need to do an evaluation
625 * then the lock is a write lock, after we have the write
626 * lock, check if we really need it - some other thread may
627 * have already done the work for us.
628 */
629 if (VFE_NEEDS_EVAL(vfe)) {
630 rw_enter(&vfe->lock, RW_WRITER);
631 if (!VFE_NEEDS_EVAL(vfe))
632 rw_downgrade(&vfe->lock);
633 } else
634 rw_enter(&vfe->lock, RW_READER);
635
636 /* Evaluate fingerprint if needed. */
637 if (VFE_NEEDS_EVAL(vfe)) {
638 u_char status;
639
640 error = veriexec_fp_status(l, vp, file_lock_state, vfe, &status);
641 if (error) {
642 veriexec_file_report(vfe, "Fingerprint calculation error.",
643 name, NULL, REPORT_ALWAYS);
644 rw_exit(&vfe->lock);
645 return (error);
646 }
647 vfe->status = status;
648 rw_downgrade(&vfe->lock);
649 }
650
651 if (!(vfe->type & flag)) {
652 veriexec_file_report(vfe, "Incorrect access type.", name, l,
653 REPORT_ALWAYS|REPORT_ALARM);
654
655 /* IPS mode: Enforce access type. */
656 if (veriexec_strict >= VERIEXEC_IPS) {
657 rw_exit(&vfe->lock);
658 return (EPERM);
659 }
660 }
661
662 switch (vfe->status) {
663 case FINGERPRINT_NOTEVAL:
664 /* Should not happen. */
665 rw_exit(&vfe->lock);
666 veriexec_file_report(vfe, "Not-evaluated status "
667 "post evaluation; inconsistency detected.", name,
668 NULL, REPORT_ALWAYS|REPORT_PANIC);
669 /* NOTREACHED */
670
671 case FINGERPRINT_VALID:
672 /* Valid fingerprint. */
673 veriexec_file_report(vfe, "Match.", name, NULL,
674 REPORT_VERBOSE);
675
676 break;
677
678 case FINGERPRINT_NOMATCH:
679 /* Fingerprint mismatch. */
680 veriexec_file_report(vfe, "Mismatch.", name,
681 NULL, REPORT_ALWAYS|REPORT_ALARM);
682
683 /* IDS mode: Deny access on fingerprint mismatch. */
684 if (veriexec_strict >= VERIEXEC_IDS) {
685 rw_exit(&vfe->lock);
686 error = EPERM;
687 }
688
689 break;
690
691 default:
692 /* Should never happen. */
693 rw_exit(&vfe->lock);
694 veriexec_file_report(vfe, "Invalid status "
695 "post evaluation.", name, NULL, REPORT_ALWAYS|REPORT_PANIC);
696 /* NOTREACHED */
697 }
698
699 return (error);
700 }
701
702 int
703 veriexec_verify(struct lwp *l, struct vnode *vp, const u_char *name, int flag,
704 bool *found)
705 {
706 struct veriexec_file_entry *vfe;
707 int r;
708
709 if (veriexec_bypass && (veriexec_strict == VERIEXEC_LEARNING))
710 return 0;
711
712 rw_enter(&veriexec_op_lock, RW_READER);
713 r = veriexec_file_verify(l, vp, name, flag, VERIEXEC_FILE_UNLOCKED,
714 &vfe);
715 rw_exit(&veriexec_op_lock);
716
717 if ((r == 0) && (vfe != NULL))
718 rw_exit(&vfe->lock);
719
720 if (found != NULL)
721 *found = (vfe != NULL) ? true : false;
722
723 return (r);
724 }
725
726 /*
727 * Veriexec remove policy code.
728 */
729 int
730 veriexec_removechk(struct lwp *l, struct vnode *vp, const char *pathbuf)
731 {
732 struct veriexec_file_entry *vfe;
733 int error;
734
735 if (veriexec_bypass && (veriexec_strict == VERIEXEC_LEARNING))
736 return 0;
737
738 rw_enter(&veriexec_op_lock, RW_READER);
739 vfe = veriexec_get(vp);
740 rw_exit(&veriexec_op_lock);
741
742 if (vfe == NULL) {
743 /* Lockdown mode: Deny access to non-monitored files. */
744 if (veriexec_strict >= VERIEXEC_LOCKDOWN)
745 return (EPERM);
746
747 return (0);
748 }
749
750 veriexec_file_report(vfe, "Remove request.", pathbuf, l,
751 REPORT_ALWAYS|REPORT_ALARM);
752
753 /* IDS mode: Deny removal of monitored files. */
754 if (veriexec_strict >= VERIEXEC_IDS)
755 error = EPERM;
756 else
757 error = veriexec_file_delete(l, vp);
758
759 return error;
760 }
761
762 /*
763 * Veriexec rename policy.
764 *
765 * XXX: Once there's a way to hook after a successful rename, it would be
766 * XXX: nice to update vfe->filename to the new name if it's not NULL and
767 * XXX: the new name is absolute (ie., starts with a slash).
768 */
769 int
770 veriexec_renamechk(struct lwp *l, struct vnode *fromvp, const char *fromname,
771 struct vnode *tovp, const char *toname)
772 {
773 struct veriexec_file_entry *fvfe = NULL, *tvfe = NULL;
774
775 if (veriexec_bypass && (veriexec_strict == VERIEXEC_LEARNING))
776 return 0;
777
778 rw_enter(&veriexec_op_lock, RW_READER);
779
780 if (veriexec_strict >= VERIEXEC_LOCKDOWN) {
781 log(LOG_ALERT, "Veriexec: Preventing rename of `%s' to "
782 "`%s', uid=%u, pid=%u: Lockdown mode.\n", fromname, toname,
783 kauth_cred_geteuid(l->l_cred), l->l_proc->p_pid);
784 rw_exit(&veriexec_op_lock);
785 return (EPERM);
786 }
787
788 fvfe = veriexec_get(fromvp);
789 if (tovp != NULL)
790 tvfe = veriexec_get(tovp);
791
792 if ((fvfe == NULL) && (tvfe == NULL)) {
793 /* None of them is monitored */
794 rw_exit(&veriexec_op_lock);
795 return 0;
796 }
797
798 if (veriexec_strict >= VERIEXEC_IPS) {
799 log(LOG_ALERT, "Veriexec: Preventing rename of `%s' "
800 "to `%s', uid=%u, pid=%u: IPS mode, %s "
801 "monitored.\n", fromname, toname,
802 kauth_cred_geteuid(l->l_cred),
803 l->l_proc->p_pid, (fvfe != NULL && tvfe != NULL) ?
804 "files" : "file");
805 rw_exit(&veriexec_op_lock);
806 return (EPERM);
807 }
808
809 if (fvfe != NULL) {
810 /*
811 * Monitored file is renamed; filename no longer relevant.
812 */
813
814 /*
815 * XXX: We could keep the buffer, and when (and if) updating the
816 * XXX: filename post-rename, re-allocate it only if it's not
817 * XXX: big enough for the new filename.
818 */
819
820 /* XXX: Get write lock on fvfe here? */
821
822 VERIEXEC_RW_UPGRADE(&veriexec_op_lock);
823 /* once we have the op lock in write mode
824 * there should be no locks on any file
825 * entries so we can destroy the object.
826 */
827
828 if (fvfe->filename_len > 0)
829 kmem_free(fvfe->filename, fvfe->filename_len);
830
831 fvfe->filename = NULL;
832 fvfe->filename_len = 0;
833
834 rw_downgrade(&veriexec_op_lock);
835 }
836
837 log(LOG_NOTICE, "Veriexec: %s file `%s' renamed to "
838 "%s file `%s', uid=%u, pid=%u.\n", (fvfe != NULL) ?
839 "Monitored" : "Non-monitored", fromname, (tvfe != NULL) ?
840 "monitored" : "non-monitored", toname,
841 kauth_cred_geteuid(l->l_cred), l->l_proc->p_pid);
842
843 rw_exit(&veriexec_op_lock);
844
845 if (tvfe != NULL) {
846 /*
847 * Monitored file is overwritten. Remove the entry.
848 */
849 (void)veriexec_file_delete(l, tovp);
850 }
851
852 return (0);
853 }
854
855 static void
856 veriexec_file_free(struct veriexec_file_entry *vfe)
857 {
858 if (vfe != NULL) {
859 if (vfe->fp != NULL)
860 kmem_free(vfe->fp, vfe->ops->hash_len);
861 if (vfe->filename != NULL)
862 kmem_free(vfe->filename, vfe->filename_len);
863 rw_destroy(&vfe->lock);
864 kmem_free(vfe, sizeof(*vfe));
865 }
866 }
867
868 static void
869 veriexec_file_purge(struct veriexec_file_entry *vfe, int have_lock)
870 {
871 if (vfe == NULL)
872 return;
873
874 if (have_lock == VERIEXEC_UNLOCKED)
875 rw_enter(&vfe->lock, RW_WRITER);
876 else
877 VERIEXEC_RW_UPGRADE(&vfe->lock);
878
879 vfe->status = FINGERPRINT_NOTEVAL;
880 if (have_lock == VERIEXEC_UNLOCKED)
881 rw_exit(&vfe->lock);
882 else
883 rw_downgrade(&vfe->lock);
884 }
885
886 static void
887 veriexec_file_purge_cb(struct veriexec_file_entry *vfe, void *cookie)
888 {
889 veriexec_file_purge(vfe, VERIEXEC_UNLOCKED);
890 }
891
892 /*
893 * Invalidate a Veriexec file entry.
894 * XXX: This should be updated when per-page fingerprints are added.
895 */
896 void
897 veriexec_purge(struct vnode *vp)
898 {
899 rw_enter(&veriexec_op_lock, RW_READER);
900 veriexec_file_purge(veriexec_get(vp), VERIEXEC_UNLOCKED);
901 rw_exit(&veriexec_op_lock);
902 }
903
904 /*
905 * Enforce raw disk access policy.
906 *
907 * IDS mode: Invalidate fingerprints on a mount if it's opened for writing.
908 * IPS mode: Don't allow raw writing to disks we monitor.
909 * Lockdown mode: Don't allow raw writing to all disks.
910 *
911 * XXX: This is bogus. There's an obvious race condition between the time
912 * XXX: the disk is open for writing, in which an attacker can access a
913 * XXX: monitored file to get its signature cached again, and when the raw
914 * XXX: file is overwritten on disk.
915 * XXX:
916 * XXX: To solve this, we need something like the following:
917 * XXX: open raw disk:
918 * XXX: - raise refcount,
919 * XXX: - invalidate fingerprints,
920 * XXX: - mark all entries for that disk with "no cache" flag
921 * XXX:
922 * XXX: veriexec_verify:
923 * XXX: - if "no cache", don't cache evaluation result
924 * XXX:
925 * XXX: close raw disk:
926 * XXX: - lower refcount,
927 * XXX: - if refcount == 0, remove "no cache" flag from all entries
928 */
929 static int
930 veriexec_raw_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
931 void *arg0, void *arg1, void *arg2, void *arg3)
932 {
933 int result;
934 enum kauth_device_req req;
935 struct veriexec_table_entry *vte;
936
937 result = KAUTH_RESULT_DENY;
938 req = (enum kauth_device_req)arg0;
939
940 switch (action) {
941 case KAUTH_DEVICE_RAWIO_SPEC: {
942 struct vnode *vp, *bvp;
943 int error;
944
945 if (req == KAUTH_REQ_DEVICE_RAWIO_SPEC_READ) {
946 result = KAUTH_RESULT_DEFER;
947 break;
948 }
949
950 vp = arg1;
951 KASSERT(vp != NULL);
952
953 /* Handle /dev/mem and /dev/kmem. */
954 if (iskmemvp(vp)) {
955 if (veriexec_strict < VERIEXEC_IPS)
956 result = KAUTH_RESULT_DEFER;
957
958 break;
959 }
960
961 error = rawdev_mounted(vp, &bvp);
962 if (error == EINVAL) {
963 result = KAUTH_RESULT_DEFER;
964 break;
965 }
966
967 /*
968 * XXX: See vfs_mountedon() comment in rawdev_mounted().
969 */
970 vte = veriexec_table_lookup(bvp->v_mount);
971 if (vte == NULL) {
972 result = KAUTH_RESULT_DEFER;
973 break;
974 }
975
976 switch (veriexec_strict) {
977 case VERIEXEC_LEARNING:
978 case VERIEXEC_IDS:
979 result = KAUTH_RESULT_DEFER;
980
981 rw_enter(&veriexec_op_lock, RW_WRITER);
982 fileassoc_table_run(bvp->v_mount, veriexec_hook,
983 (fileassoc_cb_t)veriexec_file_purge_cb, NULL);
984 rw_exit(&veriexec_op_lock);
985
986 break;
987 case VERIEXEC_IPS:
988 result = KAUTH_RESULT_DENY;
989 break;
990 case VERIEXEC_LOCKDOWN:
991 result = KAUTH_RESULT_DENY;
992 break;
993 }
994
995 break;
996 }
997
998 case KAUTH_DEVICE_RAWIO_PASSTHRU:
999 /* XXX What can we do here? */
1000 if (veriexec_strict < VERIEXEC_IPS)
1001 result = KAUTH_RESULT_DEFER;
1002
1003 break;
1004
1005 default:
1006 result = KAUTH_RESULT_DEFER;
1007 break;
1008 }
1009
1010 return (result);
1011 }
1012
1013 /*
1014 * Create a new Veriexec table.
1015 */
1016 static struct veriexec_table_entry *
1017 veriexec_table_add(struct lwp *l, struct mount *mp)
1018 {
1019 struct veriexec_table_entry *vte;
1020 u_char buf[16];
1021
1022 vte = kmem_zalloc(sizeof(*vte), KM_SLEEP);
1023 mount_setspecific(mp, veriexec_mountspecific_key, vte);
1024
1025 snprintf(buf, sizeof(buf), "table%u", veriexec_tablecount++);
1026 sysctl_createv(NULL, 0, &veriexec_count_node, &vte->vte_node,
1027 0, CTLTYPE_NODE, buf, NULL, NULL, 0, NULL,
1028 0, CTL_CREATE, CTL_EOL);
1029
1030 sysctl_createv(NULL, 0, &vte->vte_node, NULL,
1031 CTLFLAG_READONLY, CTLTYPE_STRING, "mntpt",
1032 NULL, NULL, 0, mp->mnt_stat.f_mntonname,
1033 0, CTL_CREATE, CTL_EOL);
1034 sysctl_createv(NULL, 0, &vte->vte_node, NULL,
1035 CTLFLAG_READONLY, CTLTYPE_STRING, "fstype",
1036 NULL, NULL, 0, mp->mnt_stat.f_fstypename,
1037 0, CTL_CREATE, CTL_EOL);
1038 sysctl_createv(NULL, 0, &vte->vte_node, NULL,
1039 CTLFLAG_READONLY, CTLTYPE_QUAD, "nentries",
1040 NULL, NULL, 0, &vte->vte_count, 0, CTL_CREATE, CTL_EOL);
1041
1042 return (vte);
1043 }
1044
1045 /*
1046 * Add a file to be monitored by Veriexec.
1047 *
1048 * Expected elements in dict: file, fp, fp-type, entry-type.
1049 */
1050 int
1051 veriexec_file_add(struct lwp *l, prop_dictionary_t dict)
1052 {
1053 struct veriexec_table_entry *vte;
1054 struct veriexec_file_entry *vfe = NULL;
1055 struct vnode *vp;
1056 const char *file, *fp_type;
1057 int error;
1058
1059 if (!prop_dictionary_get_cstring_nocopy(dict, "file", &file))
1060 return (EINVAL);
1061
1062 error = namei_simple_kernel(file, NSM_FOLLOW_NOEMULROOT, &vp);
1063 if (error)
1064 return (error);
1065
1066 /* Add only regular files. */
1067 if (vp->v_type != VREG) {
1068 log(LOG_ERR, "Veriexec: Not adding `%s': Not a regular file.\n",
1069 file);
1070 error = EBADF;
1071 goto out;
1072 }
1073
1074 vfe = kmem_zalloc(sizeof(*vfe), KM_SLEEP);
1075 rw_init(&vfe->lock);
1076
1077 /* Lookup fingerprint hashing algorithm. */
1078 fp_type = prop_string_cstring_nocopy(prop_dictionary_get(dict,
1079 "fp-type"));
1080 if ((vfe->ops = veriexec_fpops_lookup(fp_type)) == NULL) {
1081 log(LOG_ERR, "Veriexec: Invalid or unknown fingerprint type "
1082 "`%s' for file `%s'.\n", fp_type, file);
1083 error = EOPNOTSUPP;
1084 goto out;
1085 }
1086
1087 if (prop_data_size(prop_dictionary_get(dict, "fp")) !=
1088 vfe->ops->hash_len) {
1089 log(LOG_ERR, "Veriexec: Bad fingerprint length for `%s'.\n",
1090 file);
1091 error = EINVAL;
1092 goto out;
1093 }
1094
1095 vfe->fp = kmem_alloc(vfe->ops->hash_len, KM_SLEEP);
1096 memcpy(vfe->fp, prop_data_data_nocopy(prop_dictionary_get(dict, "fp")),
1097 vfe->ops->hash_len);
1098
1099 rw_enter(&veriexec_op_lock, RW_WRITER);
1100
1101 if (veriexec_get(vp)) {
1102 /* We already have an entry for this file. */
1103 error = EEXIST;
1104 goto unlock_out;
1105 }
1106
1107 /* Continue entry initialization. */
1108 if (prop_dictionary_get_uint8(dict, "entry-type", &vfe->type) == FALSE)
1109 vfe->type = 0;
1110 else {
1111 uint8_t extra_flags;
1112
1113 extra_flags = vfe->type & ~(VERIEXEC_DIRECT |
1114 VERIEXEC_INDIRECT | VERIEXEC_FILE | VERIEXEC_UNTRUSTED);
1115 if (extra_flags) {
1116 log(LOG_NOTICE, "Veriexec: Contaminated flags `0x%x' "
1117 "for `%s', skipping.\n", extra_flags, file);
1118 error = EINVAL;
1119 goto unlock_out;
1120 }
1121 }
1122 if (!(vfe->type & (VERIEXEC_DIRECT | VERIEXEC_INDIRECT |
1123 VERIEXEC_FILE)))
1124 vfe->type |= VERIEXEC_DIRECT;
1125
1126 vfe->status = FINGERPRINT_NOTEVAL;
1127 if (prop_bool_true(prop_dictionary_get(dict, "keep-filename"))) {
1128 vfe->filename_len = strlen(file) + 1;
1129 vfe->filename = kmem_alloc(vfe->filename_len, KM_SLEEP);
1130 strlcpy(vfe->filename, file, vfe->filename_len);
1131 } else
1132 vfe->filename = NULL;
1133
1134 if (prop_bool_true(prop_dictionary_get(dict, "eval-on-load")) ||
1135 (vfe->type & VERIEXEC_UNTRUSTED)) {
1136 u_char status;
1137
1138 error = veriexec_fp_status(l, vp, VERIEXEC_FILE_UNLOCKED,
1139 vfe, &status);
1140 if (error)
1141 goto unlock_out;
1142 vfe->status = status;
1143 }
1144
1145 vte = veriexec_table_lookup(vp->v_mount);
1146 if (vte == NULL)
1147 vte = veriexec_table_add(l, vp->v_mount);
1148
1149 /* XXX if we bail below this, we might want to gc newly created vtes. */
1150
1151 error = fileassoc_add(vp, veriexec_hook, vfe);
1152 if (error)
1153 goto unlock_out;
1154
1155 vte->vte_count++;
1156
1157 veriexec_file_report(NULL, "New entry.", file, NULL, REPORT_DEBUG);
1158 veriexec_bypass = 0;
1159
1160 unlock_out:
1161 rw_exit(&veriexec_op_lock);
1162
1163 out:
1164 vrele(vp);
1165 if (error)
1166 veriexec_file_free(vfe);
1167
1168 return (error);
1169 }
1170
1171 int
1172 veriexec_table_delete(struct lwp *l, struct mount *mp)
1173 {
1174 struct veriexec_table_entry *vte;
1175
1176 vte = veriexec_table_lookup(mp);
1177 if (vte == NULL)
1178 return (ENOENT);
1179
1180 veriexec_mountspecific_dtor(vte);
1181 mount_setspecific(mp, veriexec_mountspecific_key, NULL);
1182
1183 return (fileassoc_table_clear(mp, veriexec_hook));
1184 }
1185
1186 int
1187 veriexec_file_delete(struct lwp *l, struct vnode *vp)
1188 {
1189 struct veriexec_table_entry *vte;
1190 int error;
1191
1192 vte = veriexec_table_lookup(vp->v_mount);
1193 if (vte == NULL)
1194 return (ENOENT);
1195
1196 rw_enter(&veriexec_op_lock, RW_WRITER);
1197 error = fileassoc_clear(vp, veriexec_hook);
1198 rw_exit(&veriexec_op_lock);
1199 if (!error) {
1200 KASSERT(vte->vte_count > 0);
1201 vte->vte_count--;
1202 }
1203
1204 return (error);
1205 }
1206
1207 /*
1208 * Convert Veriexec entry data to a dictionary readable by userland tools.
1209 */
1210 static void
1211 veriexec_file_convert(struct veriexec_file_entry *vfe, prop_dictionary_t rdict)
1212 {
1213 if (vfe->filename)
1214 prop_dictionary_set(rdict, "file",
1215 prop_string_create_cstring(vfe->filename));
1216 prop_dictionary_set_uint8(rdict, "entry-type", vfe->type);
1217 prop_dictionary_set_uint8(rdict, "status", vfe->status);
1218 prop_dictionary_set(rdict, "fp-type",
1219 prop_string_create_cstring(vfe->ops->type));
1220 prop_dictionary_set(rdict, "fp",
1221 prop_data_create_data(vfe->fp, vfe->ops->hash_len));
1222 }
1223
1224 int
1225 veriexec_convert(struct vnode *vp, prop_dictionary_t rdict)
1226 {
1227 struct veriexec_file_entry *vfe;
1228
1229 rw_enter(&veriexec_op_lock, RW_READER);
1230
1231 vfe = veriexec_get(vp);
1232 if (vfe == NULL) {
1233 rw_exit(&veriexec_op_lock);
1234 return (ENOENT);
1235 }
1236
1237 rw_enter(&vfe->lock, RW_READER);
1238 veriexec_file_convert(vfe, rdict);
1239 rw_exit(&vfe->lock);
1240
1241 rw_exit(&veriexec_op_lock);
1242 return (0);
1243 }
1244
1245 int
1246 veriexec_unmountchk(struct mount *mp)
1247 {
1248 int error;
1249
1250 if ((veriexec_bypass && (veriexec_strict == VERIEXEC_LEARNING))
1251 || doing_shutdown)
1252 return (0);
1253
1254 rw_enter(&veriexec_op_lock, RW_READER);
1255
1256 switch (veriexec_strict) {
1257 case VERIEXEC_LEARNING:
1258 error = 0;
1259 break;
1260
1261 case VERIEXEC_IDS:
1262 if (veriexec_table_lookup(mp) != NULL) {
1263 log(LOG_INFO, "Veriexec: IDS mode, allowing unmount "
1264 "of \"%s\".\n", mp->mnt_stat.f_mntonname);
1265 }
1266
1267 error = 0;
1268 break;
1269
1270 case VERIEXEC_IPS: {
1271 struct veriexec_table_entry *vte;
1272
1273 vte = veriexec_table_lookup(mp);
1274 if ((vte != NULL) && (vte->vte_count > 0)) {
1275 log(LOG_ALERT, "Veriexec: IPS mode, preventing"
1276 " unmount of \"%s\" with monitored files.\n",
1277 mp->mnt_stat.f_mntonname);
1278
1279 error = EPERM;
1280 } else
1281 error = 0;
1282 break;
1283 }
1284
1285 case VERIEXEC_LOCKDOWN:
1286 default:
1287 log(LOG_ALERT, "Veriexec: Lockdown mode, preventing unmount "
1288 "of \"%s\".\n", mp->mnt_stat.f_mntonname);
1289 error = EPERM;
1290 break;
1291 }
1292
1293 rw_exit(&veriexec_op_lock);
1294 return (error);
1295 }
1296
1297 int
1298 veriexec_openchk(struct lwp *l, struct vnode *vp, const char *path, int fmode)
1299 {
1300 struct veriexec_file_entry *vfe = NULL;
1301 int error = 0;
1302
1303 if (veriexec_bypass && (veriexec_strict == VERIEXEC_LEARNING))
1304 return 0;
1305
1306 if (vp == NULL) {
1307 /* If no creation requested, let this fail normally. */
1308 if (!(fmode & O_CREAT))
1309 goto out;
1310
1311 /* Lockdown mode: Prevent creation of new files. */
1312 if (veriexec_strict >= VERIEXEC_LOCKDOWN) {
1313 log(LOG_ALERT, "Veriexec: Preventing new file "
1314 "creation in `%s'.\n", path);
1315 error = EPERM;
1316 }
1317
1318 goto out;
1319 }
1320
1321 rw_enter(&veriexec_op_lock, RW_READER);
1322 error = veriexec_file_verify(l, vp, path, VERIEXEC_FILE,
1323 VERIEXEC_FILE_LOCKED, &vfe);
1324
1325 if (error) {
1326 rw_exit(&veriexec_op_lock);
1327 goto out;
1328 }
1329
1330 if ((vfe != NULL) && ((fmode & FWRITE) || (fmode & O_TRUNC))) {
1331 veriexec_file_report(vfe, "Write access request.", path, l,
1332 REPORT_ALWAYS | REPORT_ALARM);
1333
1334 /* IPS mode: Deny write access to monitored files. */
1335 if (veriexec_strict >= VERIEXEC_IPS)
1336 error = EPERM;
1337 else
1338 veriexec_file_purge(vfe, VERIEXEC_LOCKED);
1339 }
1340
1341 if (vfe != NULL)
1342 rw_exit(&vfe->lock);
1343
1344 rw_exit(&veriexec_op_lock);
1345 out:
1346 return (error);
1347 }
1348
1349 static void
1350 veriexec_file_dump(struct veriexec_file_entry *vfe, prop_array_t entries)
1351 {
1352 prop_dictionary_t entry;
1353
1354 /* If we don't have a filename, this is meaningless. */
1355 if (vfe->filename == NULL)
1356 return;
1357
1358 entry = prop_dictionary_create();
1359
1360 veriexec_file_convert(vfe, entry);
1361
1362 prop_array_add(entries, entry);
1363 }
1364
1365 int
1366 veriexec_dump(struct lwp *l, prop_array_t rarray)
1367 {
1368 struct mount *mp, *nmp;
1369
1370 mutex_enter(&mountlist_lock);
1371 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1372 /* If it fails, the file-system is [being] unmounted. */
1373 if (vfs_busy(mp, &nmp) != 0)
1374 continue;
1375
1376 fileassoc_table_run(mp, veriexec_hook,
1377 (fileassoc_cb_t)veriexec_file_dump, rarray);
1378
1379 vfs_unbusy(mp, false, &nmp);
1380 }
1381 mutex_exit(&mountlist_lock);
1382
1383 return (0);
1384 }
1385
1386 int
1387 veriexec_flush(struct lwp *l)
1388 {
1389 struct mount *mp, *nmp;
1390 int error = 0;
1391
1392 mutex_enter(&mountlist_lock);
1393 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1394 int lerror;
1395
1396 /* If it fails, the file-system is [being] unmounted. */
1397 if (vfs_busy(mp, &nmp) != 0)
1398 continue;
1399
1400 lerror = veriexec_table_delete(l, mp);
1401 if (lerror && lerror != ENOENT)
1402 error = lerror;
1403
1404 vfs_unbusy(mp, false, &nmp);
1405 }
1406 mutex_exit(&mountlist_lock);
1407
1408 return (error);
1409 }
1410