Home | History | Annotate | Line # | Download | only in sys
verified_exec.h revision 1.8
      1 /*	$NetBSD: verified_exec.h,v 1.8 2005/05/19 20:16:19 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 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: verified_exec.h,v 1.8 2005/05/19 20:16:19 elad Exp $");
     34 
     35 /*
     36  *
     37  * Definitions for the Verified Executables kernel function.
     38  *
     39  */
     40 #include <sys/param.h>
     41 #include <sys/hash.h>
     42 
     43 #ifndef V_EXEC_H
     44 #define V_EXEC_H 1
     45 
     46 /* Max length of the fingerprint type string, including terminating \0 char */
     47 #define VERIEXEC_TYPE_MAXLEN 9
     48 
     49 struct veriexec_params  {
     50 	unsigned char type;
     51 	unsigned char fp_type[VERIEXEC_TYPE_MAXLEN];  /* type of fingerprint
     52 							 this is */
     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 struct veriexec_fp_report {
     64 	unsigned int size;
     65 	unsigned char *fingerprints;
     66 };
     67 
     68 
     69 /*
     70  * Types of veriexec inodes we can have
     71  */
     72 #define VERIEXEC_DIRECT		0 /* Allow direct execution */
     73 #define VERIEXEC_INDIRECT	1 /* Only allow indirect execution */
     74 #define VERIEXEC_FILE		2 /* Fingerprint of a plain file */
     75 
     76 #define VERIEXEC_LOAD _IOW('S', 0x1, struct veriexec_params)
     77 #define VERIEXEC_TABLESIZE _IOW('S', 0x2, struct veriexec_sizing_params)
     78 #define VERIEXEC_FINGERPRINTS _IOWR('S', 0x3, struct veriexec_fp_report)
     79 
     80 /* Verified exec sysctl objects. */
     81 #define	VERIEXEC_VERBOSE	1 /* Verbosity level. */
     82 #define	VERIEXEC_STRICT		2 /* Strict mode level. */
     83 #define	VERIEXEC_ALGORITHMS	3 /* Supported hashing algorithms. */
     84 
     85 #ifdef _KERNEL
     86 void	veriexecattach(struct device *, struct device *, void *);
     87 int     veriexecopen(dev_t, int, int, struct proc *);
     88 int     veriexecclose(dev_t, int, int, struct proc *);
     89 int     veriexecioctl(dev_t, u_long, caddr_t, int, struct proc *);
     90 
     91 /* defined in kern_verifiedexec.c */
     92 extern char *veriexec_fp_names;
     93 extern int veriexec_verbose;
     94 extern int veriexec_strict;
     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 *fp;                          /* Fingerprint. */
    125 	struct veriexec_fp_ops *ops;                /* Fingerprint ops vector*/
    126         LIST_ENTRY(veriexec_hash_entry) entries;    /* List pointer. */
    127 };
    128 
    129 LIST_HEAD(veriexec_hashhead, veriexec_hash_entry) *hash_tbl;
    130 
    131 /* Veriexec hash table information. */
    132 struct veriexec_hashtbl {
    133         struct veriexec_hashhead *hash_tbl;
    134         size_t hash_size;       /* Number of slots in the table. */
    135         dev_t hash_dev;         /* Device ID the hash table refers to. */
    136         LIST_ENTRY(veriexec_hashtbl) hash_list;
    137 };
    138 
    139 /* Global list of hash tables. */
    140 LIST_HEAD(, veriexec_hashtbl) veriexec_tables;
    141 
    142 /* Mask to ensure bounded access to elements in the hash table. */
    143 #define VERIEXEC_HASH_MASK(tbl)    ((tbl)->hash_size - 1)
    144 
    145 /* Readable values for veriexec_report(). */
    146 #define	REPORT_NOVERBOSE	0
    147 #define	REPORT_VERBOSE		1
    148 #define	REPORT_NOPANIC		0
    149 #define	REPORT_PANIC		1
    150 #define	REPORT_NOALARM		0
    151 #define	REPORT_ALARM		1
    152 
    153 /*
    154  * Hashing function: Takes an inode number modulus the mask to give back
    155  * an index into the hash table.
    156  */
    157 #define VERIEXEC_HASH(tbl, inode)  \
    158         (hash32_buf(&(inode), sizeof((inode)), HASH32_BUF_INIT) \
    159 	 & VERIEXEC_HASH_MASK(tbl))
    160 
    161 void veriexec_init_fp_ops(void);
    162 struct veriexec_fp_ops *veriexec_find_ops(u_char *name);
    163 int veriexec_fp_calc(struct proc *, struct vnode *,
    164 		     struct veriexec_hash_entry *, uint64_t, u_char *);
    165 int veriexec_fp_cmp(struct veriexec_hash_entry *, u_char *);
    166 
    167 struct veriexec_hashtbl *veriexec_tblfind(dev_t);
    168 struct veriexec_hash_entry *veriexec_lookup(dev_t, ino_t);
    169 int veriexec_hashadd(struct veriexec_hashtbl *, struct veriexec_hash_entry *);
    170 
    171 int veriexec_verify(struct proc *, struct vnode *, struct vattr *,
    172 		    const u_char *, int);
    173 int veriexec_removechk(struct proc *, struct vnode *, const char *);
    174 void veriexec_init_fp_ops(void);
    175 void veriexec_report(const u_char *, const u_char *, struct vattr *,
    176 		     struct proc *, int, int, int);
    177 
    178 #endif
    179 
    180 #ifdef VERIFIED_EXEC_DEBUG
    181 #define veriexec_dprintf(x) printf x
    182 #else
    183 #define veriexec_dprintf(x)
    184 #endif /* VERIFIED_EXEC_DEBUG */
    185 
    186 #endif
    187