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