Home | History | Annotate | Line # | Download | only in sys
verified_exec.h revision 1.28
      1 /*	$NetBSD: verified_exec.h,v 1.28 2005/12/12 21:47:58 elad Exp $	*/
      2 
      3 /*-
      4  * Copyright 2005 Elad Efrat <elad (at) bsd.org.il>
      5  * Copyright 2005 Brett Lymn <blymn (at) netbsd.org>
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Brett Lymn and Elad Efrat
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Neither the name of The NetBSD Foundation nor the names of its
     16  *    contributors may be used to endorse or promote products derived
     17  *    from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _SYS_VERIFIED_EXEC_H_
     33 #define _SYS_VERIFIED_EXEC_H_
     34 
     35 #include <sys/cdefs.h>
     36 #include <sys/param.h>
     37 #include <sys/hash.h>
     38 #include <uvm/uvm_extern.h>
     39 #include <uvm/uvm_pglist.h>
     40 #include <uvm/uvm_page.h>
     41 
     42 /* Max length of the fingerprint type string, including terminating \0 char */
     43 #define	VERIEXEC_TYPE_MAXLEN	9
     44 
     45 struct veriexec_params  {
     46 	unsigned char type;
     47 	unsigned char fp_type[VERIEXEC_TYPE_MAXLEN];
     48 	char file[MAXPATHLEN];
     49 	unsigned int size;  /* number of bytes in the fingerprint */
     50 	unsigned char *fingerprint;
     51 };
     52 
     53 struct veriexec_sizing_params {
     54 	dev_t dev;
     55 	size_t hash_size;
     56 };
     57 
     58 struct veriexec_delete_params {
     59 	dev_t dev;
     60 	ino_t ino;
     61 };
     62 
     63 struct veriexec_query_params {
     64 	unsigned char fp_type[VERIEXEC_TYPE_MAXLEN];
     65 	dev_t dev;
     66 	ino_t ino;
     67 	unsigned char type;
     68 	unsigned char status;
     69 	unsigned char *fp;
     70 	size_t fp_bufsize;
     71 	size_t hash_len;
     72 	void *uaddr;
     73 };
     74 
     75 /* Flags for a Veriexec entry. These can be OR'd together. */
     76 #define VERIEXEC_DIRECT		0x01 /* Direct execution (exec) */
     77 #define VERIEXEC_INDIRECT	0x02 /* Indirect execution (#!) */
     78 #define VERIEXEC_FILE		0x04 /* Plain file (open) */
     79 #define	VERIEXEC_UNTRUSTED	0x10 /* Untrusted storage */
     80 
     81 /* Operations for /dev/veriexec. */
     82 #define VERIEXEC_LOAD		_IOW('S', 0x1, struct veriexec_params)
     83 #define VERIEXEC_TABLESIZE	_IOW('S', 0x2, struct veriexec_sizing_params)
     84 #define VERIEXEC_DELETE		_IOW('S', 0x3, struct veriexec_delete_params)
     85 #define VERIEXEC_QUERY		_IOW('S', 0x4, struct veriexec_query_params)
     86 
     87 /* Verified exec sysctl objects. */
     88 #define	VERIEXEC_VERBOSE	1 /* Verbosity level. */
     89 #define	VERIEXEC_STRICT		2 /* Strict mode level. */
     90 #define	VERIEXEC_ALGORITHMS	3 /* Supported hashing algorithms. */
     91 #define	VERIEXEC_COUNT		4 /* # of fingerprinted files on device. */
     92 
     93 /* Valid status field values. */
     94 #define FINGERPRINT_NOTEVAL  0  /* fingerprint has not been evaluated */
     95 #define FINGERPRINT_VALID    1  /* fingerprint evaluated and matches list */
     96 #define FINGERPRINT_NOMATCH  2  /* fingerprint evaluated but does not match */
     97 
     98 /* Per-page fingerprint status. */
     99 #define	PAGE_FP_NONE	0	/* no per-page fingerprints. */
    100 #define	PAGE_FP_READY	1	/* per-page fingerprints ready for use. */
    101 #define	PAGE_FP_FAIL	2	/* mismatch in per-page fingerprints. */
    102 
    103 #ifdef _KERNEL
    104 void	veriexecattach(struct device *, struct device *, void *);
    105 int     veriexecopen(dev_t, int, int, struct lwp *);
    106 int     veriexecclose(dev_t, int, int, struct lwp *);
    107 int     veriexecioctl(dev_t, u_long, caddr_t, int, struct lwp *);
    108 
    109 /* defined in kern_verifiedexec.c */
    110 extern char *veriexec_fp_names;
    111 extern int veriexec_verbose;
    112 extern int veriexec_strict;
    113 /* this one requires sysctl.h to be included before verified_exec.h */
    114 #ifdef VERIEXEC_NEED_NODE
    115 extern const struct sysctlnode *veriexec_count_node;
    116 #endif /* VERIEXEC_NEED_NODE */
    117 
    118 /*
    119  * Operations vector for verified exec, this defines the characteristics
    120  * for the fingerprint type.
    121  * Function types: init, update, final.
    122  */
    123 typedef void (*VERIEXEC_INIT_FN)(void *);
    124 typedef void (*VERIEXEC_UPDATE_FN)(void *, u_char *, u_int);
    125 typedef void (*VERIEXEC_FINAL_FN)(u_char *, void *);
    126 
    127 struct veriexec_fp_ops {
    128 	char type[VERIEXEC_TYPE_MAXLEN];
    129 	size_t hash_len;
    130 	size_t context_size;
    131 	VERIEXEC_INIT_FN init;
    132 	VERIEXEC_UPDATE_FN update;
    133 	VERIEXEC_FINAL_FN final;
    134 	LIST_ENTRY(veriexec_fp_ops) entries;
    135 };
    136 
    137 /* An entry in the per-device hash table. */
    138 struct veriexec_hash_entry {
    139 	ino_t inode;				    /* Inode number. */
    140 	unsigned char type;			    /* Entry type. */
    141 	unsigned char status;			    /* Evaluation status. */
    142 	unsigned char page_fp_status;		    /* Per-page FP status. */
    143 	unsigned char *fp;			    /* Fingerprint. */
    144 	void *page_fp;				    /* Per-page fingerprints */
    145 	size_t npages;			    	    /* Number of pages. */
    146 	size_t last_page_size;			    /* To support < PAGE_SIZE */
    147 	struct veriexec_fp_ops *ops;		    /* Fingerprint ops vector*/
    148 	LIST_ENTRY(veriexec_hash_entry) entries;    /* List pointer. */
    149 };
    150 
    151 LIST_HEAD(veriexec_hashhead, veriexec_hash_entry);
    152 
    153 /* Veriexec hash table information. */
    154 struct veriexec_hashtbl {
    155 	struct veriexec_hashhead *hash_tbl;
    156 	size_t hash_size;	/* Number of slots in the table. */
    157 	dev_t hash_dev;		/* Device ID the hash table refers to. */
    158 	uint64_t hash_count;	/* # of fingerprinted files in table. */
    159 	LIST_ENTRY(veriexec_hashtbl) hash_list;
    160 };
    161 
    162 /* Global list of hash tables, one per device. */
    163 LIST_HEAD(, veriexec_hashtbl) veriexec_tables;
    164 
    165 /* Mask to ensure bounded access to elements in the hash table. */
    166 #define VERIEXEC_HASH_MASK(tbl)		((tbl)->hash_size - 1)
    167 
    168 /* Readable values for veriexec_report(). */
    169 #define	REPORT_NOVERBOSE	0	/* Always print */
    170 #define	REPORT_VERBOSE		1	/* Print when verbose >= 1 */
    171 #define	REPORT_VERBOSE_HIGH	2	/* Print when verbose >= 2 (debug) */
    172 #define	REPORT_NOPANIC		0	/* Normal report */
    173 #define	REPORT_PANIC		1	/* Use panic() */
    174 #define	REPORT_NOALARM		0	/* Normal report */
    175 #define	REPORT_ALARM		1	/* Alarm - also print pid/uid/.. */
    176 
    177 /*
    178  * Hashing function: Takes an inode number modulus the mask to give back
    179  * an index into the hash table.
    180  */
    181 #define VERIEXEC_HASH(tbl, inode)	\
    182 	(hash32_buf(&(inode), sizeof((inode)), HASH32_BUF_INIT) \
    183 	 & VERIEXEC_HASH_MASK(tbl))
    184 
    185 /* Initialize a fingerprint ops struct. */
    186 #define	VERIEXEC_OPINIT(ops, fp_type, hashlen, ctx_size, init_fn, \
    187 	update_fn, final_fn) \
    188 	do {								    \
    189 		(void) strlcpy((ops)->type, fp_type, sizeof((ops)->type));  \
    190 		(ops)->hash_len = (hashlen);				    \
    191 		(ops)->context_size = (ctx_size);			    \
    192 		(ops)->init = (VERIEXEC_INIT_FN) (init_fn);		    \
    193 		(ops)->update = (VERIEXEC_UPDATE_FN) (update_fn);	    \
    194 		(ops)->final = (VERIEXEC_FINAL_FN) (final_fn);		    \
    195 	} while (0);
    196 
    197 int veriexec_add_fp_ops(struct veriexec_fp_ops *);
    198 void veriexec_init_fp_ops(void);
    199 struct veriexec_fp_ops *veriexec_find_ops(u_char *name);
    200 int veriexec_fp_calc(struct lwp *, struct vnode *,
    201 		     struct veriexec_hash_entry *, uint64_t, u_char *);
    202 int veriexec_fp_cmp(struct veriexec_fp_ops *, u_char *, u_char *);
    203 struct veriexec_hashtbl *veriexec_tblfind(dev_t);
    204 struct veriexec_hash_entry *veriexec_lookup(dev_t, ino_t);
    205 int veriexec_hashadd(struct veriexec_hashtbl *, struct veriexec_hash_entry *);
    206 int veriexec_verify(struct lwp *, struct vnode *, struct vattr *,
    207 		    const u_char *, int, struct veriexec_hash_entry **);
    208 int veriexec_page_verify(struct veriexec_hash_entry *, struct vattr *,
    209 			 struct vm_page *, size_t, struct lwp *);
    210 int veriexec_removechk(struct lwp *, struct vnode *, const char *);
    211 int veriexec_renamechk(struct vnode *, const char *, const char *,
    212 		       struct lwp *);
    213 void veriexec_init_fp_ops(void);
    214 void veriexec_report(const u_char *, const u_char *, struct vattr *,
    215 		     struct lwp *, int, int, int);
    216 int veriexec_newtable(struct veriexec_sizing_params *);
    217 int veriexec_load(struct veriexec_params *, struct lwp *);
    218 int veriexec_delete(struct veriexec_delete_params *);
    219 int veriexec_query(struct veriexec_query_params *);
    220 
    221 #endif /* _KERNEL */
    222 
    223 #endif /* !_SYS_VERIFIED_EXEC_H_ */
    224