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