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