Home | History | Annotate | Line # | Download | only in sys
verified_exec.h revision 1.21
      1 /*	$NetBSD: verified_exec.h,v 1.21 2005/10/07 18:07:46 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 /*
     33  *
     34  * Definitions for the Verified Executables kernel function.
     35  *
     36  */
     37 #ifndef _SYS_VERIFIED_EXEC_H_
     38 #define _SYS_VERIFIED_EXEC_H_
     39 
     40 #include <sys/cdefs.h>
     41 #include <sys/param.h>
     42 #include <sys/hash.h>
     43 #include <uvm/uvm_extern.h>
     44 #include <uvm/uvm_pglist.h>
     45 #include <uvm/uvm_page.h>
     46 
     47 /* Max length of the fingerprint type string, including terminating \0 char */
     48 #define VERIEXEC_TYPE_MAXLEN 9
     49 
     50 struct veriexec_params  {
     51 	unsigned char type;
     52 	unsigned char fp_type[VERIEXEC_TYPE_MAXLEN];
     53 	char file[MAXPATHLEN];
     54 	unsigned int size;  /* number of bytes in the fingerprint */
     55 	unsigned char *fingerprint;
     56 };
     57 
     58 struct veriexec_sizing_params {
     59 	dev_t dev;
     60 	size_t hash_size;
     61 };
     62 
     63 /*
     64  * Types of veriexec inodes we can have. Ordered from less strict to
     65  * most strict -- this is enforced if a duplicate entry is loaded.
     66  */
     67 #define VERIEXEC_DIRECT		0x01 /* Direct execution (exec) */
     68 #define VERIEXEC_INDIRECT	0x02 /* Indirect execution (#!) */
     69 #define VERIEXEC_FILE		0x04 /* Plain file (open) */
     70 #define	VERIEXEC_UNTRUSTED	0x10 /* Untrusted storage */
     71 
     72 #define VERIEXEC_LOAD _IOW('S', 0x1, struct veriexec_params)
     73 #define VERIEXEC_TABLESIZE _IOW('S', 0x2, struct veriexec_sizing_params)
     74 
     75 /* Verified exec sysctl objects. */
     76 #define	VERIEXEC_VERBOSE	1 /* Verbosity level. */
     77 #define	VERIEXEC_STRICT		2 /* Strict mode level. */
     78 #define	VERIEXEC_ALGORITHMS	3 /* Supported hashing algorithms. */
     79 #define	VERIEXEC_COUNT		4 /* # of fingerprinted files on device. */
     80 
     81 #ifdef _KERNEL
     82 void	veriexecattach(struct device *, struct device *, void *);
     83 int     veriexecopen(dev_t, int, int, struct proc *);
     84 int     veriexecclose(dev_t, int, int, struct proc *);
     85 int     veriexecioctl(dev_t, u_long, caddr_t, int, struct proc *);
     86 
     87 /* defined in kern_verifiedexec.c */
     88 extern char *veriexec_fp_names;
     89 extern int veriexec_verbose;
     90 extern int veriexec_strict;
     91 /* this one requires sysctl.h to be included before verified_exec.h */
     92 #ifdef VERIEXEC_NEED_NODE
     93 extern const struct sysctlnode *veriexec_count_node;
     94 #endif /* VERIEXEC_NEED_NODE */
     95 
     96 /*
     97  * Operations vector for verified exec, this defines the characteristics
     98  * for the fingerprint type.
     99  */
    100 
    101 /* Function types: init, update, final. */
    102 typedef void (*VERIEXEC_INIT_FN)(void *);
    103 typedef void (*VERIEXEC_UPDATE_FN)(void *, u_char *, u_int);
    104 typedef void (*VERIEXEC_FINAL_FN)(u_char *, void *);
    105 
    106 struct veriexec_fp_ops {
    107 	char type[VERIEXEC_TYPE_MAXLEN];
    108 	size_t hash_len;
    109 	size_t context_size;
    110 	VERIEXEC_INIT_FN init;
    111 	VERIEXEC_UPDATE_FN update;
    112 	VERIEXEC_FINAL_FN final;
    113 	LIST_ENTRY(veriexec_fp_ops) entries;
    114 };
    115 
    116 /*
    117  * list structure definitions - needed in kern_exec.c
    118  */
    119 
    120 /* An entry in the per-device hash table. */
    121 struct veriexec_hash_entry {
    122         ino_t         inode;                        /* Inode number. */
    123         unsigned char type;                         /* Entry type. */
    124 	unsigned char status;			    /* Evaluation status. */
    125 	unsigned char page_fp_status;		    /* Per-page FP status. */
    126         unsigned char *fp;                          /* Fingerprint. */
    127 	void *page_fp;				    /* Per-page fingerprints */
    128 	size_t npages;			    	    /* Number of pages. */
    129 	size_t last_page_size;
    130 	struct veriexec_fp_ops *ops;                /* Fingerprint ops vector*/
    131         LIST_ENTRY(veriexec_hash_entry) entries;    /* List pointer. */
    132 };
    133 
    134 /* Valid status field values. */
    135 #define FINGERPRINT_NOTEVAL  0  /* fingerprint has not been evaluated */
    136 #define FINGERPRINT_VALID    1  /* fingerprint evaluated and matches list */
    137 #define FINGERPRINT_NOMATCH  2  /* fingerprint evaluated but does not match */
    138 
    139 /* Per-page fingerprint status. */
    140 #define	PAGE_FP_NONE	0	/* no per-page fingerprints. */
    141 #define	PAGE_FP_READY	1	/* per-page fingerprints ready for use. */
    142 #define	PAGE_FP_FAIL	2	/* mismatch in per-page fingerprints. */
    143 
    144 LIST_HEAD(veriexec_hashhead, veriexec_hash_entry) *hash_tbl;
    145 
    146 /* Veriexec hash table information. */
    147 struct veriexec_hashtbl {
    148         struct veriexec_hashhead *hash_tbl;
    149         size_t hash_size;       /* Number of slots in the table. */
    150         dev_t hash_dev;         /* Device ID the hash table refers to. */
    151 	uint64_t hash_count;	/* # of fingerprinted files in table. */
    152         LIST_ENTRY(veriexec_hashtbl) hash_list;
    153 };
    154 
    155 /* Global list of hash tables. */
    156 LIST_HEAD(, veriexec_hashtbl) veriexec_tables;
    157 
    158 /* Mask to ensure bounded access to elements in the hash table. */
    159 #define VERIEXEC_HASH_MASK(tbl)    ((tbl)->hash_size - 1)
    160 
    161 /* Readable values for veriexec_report(). */
    162 #define	REPORT_NOVERBOSE	0
    163 #define	REPORT_VERBOSE		1
    164 #define	REPORT_VERBOSE_HIGH	2
    165 #define	REPORT_NOPANIC		0
    166 #define	REPORT_PANIC		1
    167 #define	REPORT_NOALARM		0
    168 #define	REPORT_ALARM		1
    169 
    170 /*
    171  * Hashing function: Takes an inode number modulus the mask to give back
    172  * an index into the hash table.
    173  */
    174 #define VERIEXEC_HASH(tbl, inode)  \
    175         (hash32_buf(&(inode), sizeof((inode)), HASH32_BUF_INIT) \
    176 	 & VERIEXEC_HASH_MASK(tbl))
    177 
    178 /* Initialize a fingerprint ops struct. */
    179 #define	VERIEXEC_OPINIT(ops, fp_type, hashlen, ctx_size, init_fn, \
    180 	update_fn, final_fn) \
    181 	do {								    \
    182 		(void) strlcpy((ops)->type, fp_type, sizeof((ops)->type));  \
    183 		(ops)->hash_len = (hashlen);				    \
    184 		(ops)->context_size = (ctx_size);			    \
    185 		(ops)->init = (VERIEXEC_INIT_FN) (init_fn);		    \
    186 		(ops)->update = (VERIEXEC_UPDATE_FN) (update_fn);	    \
    187 		(ops)->final = (VERIEXEC_FINAL_FN) (final_fn);		    \
    188 	} while (0);
    189 
    190 int veriexec_add_fp_ops(struct veriexec_fp_ops *);
    191 void veriexec_init_fp_ops(void);
    192 struct veriexec_fp_ops *veriexec_find_ops(u_char *name);
    193 int veriexec_fp_calc(struct proc *, struct vnode *,
    194 		     struct veriexec_hash_entry *, uint64_t, u_char *);
    195 int veriexec_fp_cmp(struct veriexec_fp_ops *, u_char *, u_char *);
    196 struct veriexec_hashtbl *veriexec_tblfind(dev_t);
    197 struct veriexec_hash_entry *veriexec_lookup(dev_t, ino_t);
    198 int veriexec_hashadd(struct veriexec_hashtbl *, struct veriexec_hash_entry *);
    199 int veriexec_verify(struct proc *, struct vnode *, struct vattr *,
    200 		    const u_char *, int, struct veriexec_hash_entry **);
    201 int veriexec_page_verify(struct veriexec_hash_entry *, struct vattr *,
    202 			 struct vm_page *, size_t);
    203 int veriexec_removechk(struct proc *, struct vnode *, const char *);
    204 int veriexec_renamechk(struct vnode *, const char *, const char *);
    205 void veriexec_init_fp_ops(void);
    206 void veriexec_report(const u_char *, const u_char *, struct vattr *,
    207 		     struct proc *, int, int, int);
    208 
    209 #endif /* _KERNEL */
    210 
    211 #endif /* !_SYS_VERIFIED_EXEC_H_ */
    212