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