verified_exec.h revision 1.35 1 /* $NetBSD: verified_exec.h,v 1.35 2006/07/24 21:32:39 elad Exp $ */
2
3 /*-
4 * Copyright 2005 Elad Efrat <elad (at) NetBSD.org>
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 size_t hash_size;
55 u_char file[MAXPATHLEN];
56 };
57
58 struct veriexec_delete_params {
59 u_char file[MAXPATHLEN];
60 };
61
62 struct veriexec_query_params {
63 u_char file[MAXPATHLEN];
64 unsigned char fp_type[VERIEXEC_TYPE_MAXLEN];
65 unsigned char type;
66 unsigned char status;
67 unsigned char *fp;
68 size_t fp_bufsize;
69 size_t hash_len;
70 void *uaddr;
71 };
72
73 /* Flags for a Veriexec entry. These can be OR'd together. */
74 #define VERIEXEC_DIRECT 0x01 /* Direct execution (exec) */
75 #define VERIEXEC_INDIRECT 0x02 /* Indirect execution (#!) */
76 #define VERIEXEC_FILE 0x04 /* Plain file (open) */
77 #define VERIEXEC_UNTRUSTED 0x10 /* Untrusted storage */
78
79 /* Operations for /dev/veriexec. */
80 #define VERIEXEC_LOAD _IOW('S', 0x1, struct veriexec_params)
81 #define VERIEXEC_TABLESIZE _IOW('S', 0x2, struct veriexec_sizing_params)
82 #define VERIEXEC_DELETE _IOW('S', 0x3, struct veriexec_delete_params)
83 #define VERIEXEC_QUERY _IOW('S', 0x4, struct veriexec_query_params)
84
85 /* Verified exec sysctl objects. */
86 #define VERIEXEC_VERBOSE 1 /* Verbosity level. */
87 #define VERIEXEC_STRICT 2 /* Strict mode level. */
88 #define VERIEXEC_ALGORITHMS 3 /* Supported hashing algorithms. */
89 #define VERIEXEC_COUNT 4 /* # of fingerprinted files on device. */
90
91 /* Valid status field values. */
92 #define FINGERPRINT_NOTEVAL 0 /* fingerprint has not been evaluated */
93 #define FINGERPRINT_VALID 1 /* fingerprint evaluated and matches list */
94 #define FINGERPRINT_NOMATCH 2 /* fingerprint evaluated but does not match */
95
96 /* Per-page fingerprint status. */
97 #define PAGE_FP_NONE 0 /* no per-page fingerprints. */
98 #define PAGE_FP_READY 1 /* per-page fingerprints ready for use. */
99 #define PAGE_FP_FAIL 2 /* mismatch in per-page fingerprints. */
100
101 #ifdef _KERNEL
102 void veriexecattach(struct device *, struct device *, void *);
103 int veriexecopen(dev_t, int, int, struct lwp *);
104 int veriexecclose(dev_t, int, int, struct lwp *);
105 int veriexecioctl(dev_t, u_long, caddr_t, int, struct lwp *);
106
107 /* defined in kern_verifiedexec.c */
108 extern char *veriexec_fp_names;
109 extern int veriexec_verbose;
110 extern int veriexec_strict;
111 /* this one requires sysctl.h to be included before verified_exec.h */
112 #ifdef VERIEXEC_NEED_NODE
113 extern const struct sysctlnode *veriexec_count_node;
114 #endif /* VERIEXEC_NEED_NODE */
115 extern int veriexec_hook;
116
117 /*
118 * Operations vector for verified exec, this defines the characteristics
119 * for the fingerprint type.
120 * Function types: init, update, final.
121 */
122 typedef void (*VERIEXEC_INIT_FN)(void *);
123 typedef void (*VERIEXEC_UPDATE_FN)(void *, u_char *, u_int);
124 typedef void (*VERIEXEC_FINAL_FN)(u_char *, void *);
125
126 struct veriexec_fp_ops {
127 char type[VERIEXEC_TYPE_MAXLEN];
128 size_t hash_len;
129 size_t context_size;
130 VERIEXEC_INIT_FN init;
131 VERIEXEC_UPDATE_FN update;
132 VERIEXEC_FINAL_FN final;
133 LIST_ENTRY(veriexec_fp_ops) entries;
134 };
135
136 /* Veriexec per-file entry data. */
137 struct veriexec_file_entry {
138 u_char type; /* Entry type. */
139 u_char status; /* Evaluation status. */
140 u_char page_fp_status; /* Per-page FP status. */
141 u_char *fp; /* Fingerprint. */
142 void *page_fp; /* Per-page fingerprints */
143 size_t npages; /* Number of pages. */
144 size_t last_page_size; /* To support < PAGE_SIZE */
145 struct veriexec_fp_ops *ops; /* Fingerprint ops vector*/
146 };
147
148 /* Veriexec per-table data. */
149 struct veriexec_table_entry {
150 uint64_t vte_count; /* Number of Veriexec entries. */
151 const struct sysctlnode *vte_node;
152 };
153
154 /* Veriexec modes (strict levels). */
155 #define VERIEXEC_LEARNING 0 /* Learning mode. */
156 #define VERIEXEC_IDS 1 /* Intrusion detection mode. */
157 #defien VERIEXEC_IPS 2 /* Intrusion prevention mode. */
158 #define VERIEXEC_LOCKDOWN 3 /* Lockdown mode. */
159
160 /* Readable values for veriexec_report(). */
161 #define REPORT_ALWAYS 0x01 /* Always print */
162 #define REPORT_VERBOSE 0x02 /* Print when verbose >= 1 */
163 #define REPORT_DEBUG 0x04 /* Print when verbose >= 2 (debug) */
164 #define REPORT_PANIC 0x08 /* Call panic() */
165 #define REPORT_ALARM 0x10 /* Alarm - also print pid/uid/.. */
166 #define REPORT_LOGMASK (REPORT_ALWAYS|REPORT_VERBOSE|REPORT_DEBUG)
167
168 /* Initialize a fingerprint ops struct. */
169 #define VERIEXEC_OPINIT(ops, fp_type, hashlen, ctx_size, init_fn, \
170 update_fn, final_fn) \
171 do { \
172 (void) strlcpy((ops)->type, fp_type, sizeof((ops)->type)); \
173 (ops)->hash_len = (hashlen); \
174 (ops)->context_size = (ctx_size); \
175 (ops)->init = (VERIEXEC_INIT_FN) (init_fn); \
176 (ops)->update = (VERIEXEC_UPDATE_FN) (update_fn); \
177 (ops)->final = (VERIEXEC_FINAL_FN) (final_fn); \
178 } while (0);
179
180 int veriexec_add_fp_ops(struct veriexec_fp_ops *);
181 void veriexec_init_fp_ops(void);
182 struct veriexec_fp_ops *veriexec_find_ops(u_char *name);
183 int veriexec_fp_calc(struct lwp *, struct vnode *, struct veriexec_file_entry *,
184 u_char *);
185 int veriexec_fp_cmp(struct veriexec_fp_ops *, u_char *, u_char *);
186 struct veriexec_table_entry *veriexec_tblfind(struct vnode *);
187 struct veriexec_file_entry *veriexec_lookup(struct vnode *);
188 int veriexec_hashadd(struct vnode *, struct veriexec_file_entry *);
189 int veriexec_verify(struct lwp *, struct vnode *,
190 const u_char *, int, struct veriexec_file_entry **);
191 int veriexec_page_verify(struct veriexec_file_entry *, struct vm_page *, size_t,
192 struct lwp *);
193 int veriexec_removechk(struct lwp *, struct vnode *, const char *);
194 int veriexec_renamechk(struct vnode *, struct vnode *, const char *,
195 const char *, struct lwp *);
196 void veriexec_init_fp_ops(void);
197 void veriexec_report(const u_char *, const u_char *, struct lwp *, int);
198 int veriexec_newtable(struct veriexec_sizing_params *, struct lwp *);
199 int veriexec_load(struct veriexec_params *, struct lwp *);
200 int veriexec_delete(struct veriexec_delete_params *, struct lwp *);
201 int veriexec_query(struct veriexec_query_params *, struct lwp *);
202 void veriexec_clear(void *, int);
203
204 #endif /* _KERNEL */
205
206 #endif /* !_SYS_VERIFIED_EXEC_H_ */
207