kern_veriexec.c revision 1.2 1 1.2 maxv /* $NetBSD: kern_veriexec.c,v 1.2 2015/04/25 08:19:06 maxv Exp $ */
2 1.1 maxv
3 1.1 maxv /*-
4 1.1 maxv * Copyright (c) 2005, 2006 Elad Efrat <elad (at) NetBSD.org>
5 1.1 maxv * Copyright (c) 2005, 2006 Brett Lymn <blymn (at) NetBSD.org>
6 1.1 maxv * All rights reserved.
7 1.1 maxv *
8 1.1 maxv * Redistribution and use in source and binary forms, with or without
9 1.1 maxv * modification, are permitted provided that the following conditions
10 1.1 maxv * are met:
11 1.1 maxv * 1. Redistributions of source code must retain the above copyright
12 1.1 maxv * notice, this list of conditions and the following disclaimer.
13 1.1 maxv * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 maxv * notice, this list of conditions and the following disclaimer in the
15 1.1 maxv * documentation and/or other materials provided with the distribution.
16 1.1 maxv * 3. The name of the authors may not be used to endorse or promote products
17 1.1 maxv * derived from this software without specific prior written permission.
18 1.1 maxv *
19 1.1 maxv * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
20 1.1 maxv * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 maxv * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 maxv * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 maxv * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.1 maxv * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.1 maxv * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.1 maxv * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.1 maxv * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 1.1 maxv * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1 maxv */
30 1.1 maxv
31 1.1 maxv #include <sys/cdefs.h>
32 1.2 maxv __KERNEL_RCSID(0, "$NetBSD: kern_veriexec.c,v 1.2 2015/04/25 08:19:06 maxv Exp $");
33 1.1 maxv
34 1.1 maxv #include "opt_veriexec.h"
35 1.1 maxv
36 1.1 maxv #include <sys/param.h>
37 1.1 maxv #include <sys/mount.h>
38 1.1 maxv #include <sys/kmem.h>
39 1.1 maxv #include <sys/vnode.h>
40 1.1 maxv #include <sys/namei.h>
41 1.1 maxv #include <sys/exec.h>
42 1.1 maxv #include <sys/once.h>
43 1.1 maxv #include <sys/proc.h>
44 1.1 maxv #include <sys/rwlock.h>
45 1.1 maxv #include <sys/syslog.h>
46 1.1 maxv #include <sys/sysctl.h>
47 1.1 maxv #include <sys/inttypes.h>
48 1.1 maxv #include <sys/verified_exec.h>
49 1.1 maxv #if defined(__FreeBSD__)
50 1.1 maxv # include <sys/systm.h>
51 1.1 maxv # include <sys/imgact.h>
52 1.1 maxv # include <crypto/sha1.h>
53 1.1 maxv # include <crypto/sha2/sha2.h>
54 1.1 maxv # include <crypto/ripemd160/rmd160.h>
55 1.1 maxv #else
56 1.1 maxv # include <sys/sha1.h>
57 1.1 maxv # include <sys/sha2.h>
58 1.1 maxv # include <sys/rmd160.h>
59 1.1 maxv #endif
60 1.1 maxv #include <sys/md5.h>
61 1.1 maxv #include <uvm/uvm_extern.h>
62 1.1 maxv #include <sys/fileassoc.h>
63 1.1 maxv #include <sys/kauth.h>
64 1.1 maxv #include <sys/conf.h>
65 1.1 maxv #include <miscfs/specfs/specdev.h>
66 1.1 maxv #include <prop/proplib.h>
67 1.1 maxv #include <sys/fcntl.h>
68 1.1 maxv
69 1.1 maxv /* Readable values for veriexec_file_report(). */
70 1.1 maxv #define REPORT_ALWAYS 0x01 /* Always print */
71 1.1 maxv #define REPORT_VERBOSE 0x02 /* Print when verbose >= 1 */
72 1.1 maxv #define REPORT_DEBUG 0x04 /* Print when verbose >= 2 (debug) */
73 1.1 maxv #define REPORT_PANIC 0x08 /* Call panic() */
74 1.1 maxv #define REPORT_ALARM 0x10 /* Alarm - also print pid/uid/.. */
75 1.1 maxv #define REPORT_LOGMASK (REPORT_ALWAYS|REPORT_VERBOSE|REPORT_DEBUG)
76 1.1 maxv
77 1.1 maxv /* state of locking for veriexec_file_verify */
78 1.1 maxv #define VERIEXEC_UNLOCKED 0x00 /* Nothing locked, callee does it */
79 1.1 maxv #define VERIEXEC_LOCKED 0x01 /* Global op lock held */
80 1.1 maxv
81 1.1 maxv
82 1.1 maxv #define VERIEXEC_RW_UPGRADE(lock) while((rw_tryupgrade(lock)) == 0){};
83 1.1 maxv
84 1.1 maxv struct veriexec_fpops {
85 1.1 maxv const char *type;
86 1.1 maxv size_t hash_len;
87 1.1 maxv size_t context_size;
88 1.1 maxv veriexec_fpop_init_t init;
89 1.1 maxv veriexec_fpop_update_t update;
90 1.1 maxv veriexec_fpop_final_t final;
91 1.1 maxv LIST_ENTRY(veriexec_fpops) entries;
92 1.1 maxv };
93 1.1 maxv
94 1.1 maxv /* Veriexec per-file entry data. */
95 1.1 maxv struct veriexec_file_entry {
96 1.1 maxv krwlock_t lock; /* r/w lock */
97 1.1 maxv u_char *filename; /* File name. */
98 1.1 maxv u_char type; /* Entry type. */
99 1.1 maxv u_char status; /* Evaluation status. */
100 1.1 maxv u_char page_fp_status; /* Per-page FP status. */
101 1.1 maxv u_char *fp; /* Fingerprint. */
102 1.1 maxv void *page_fp; /* Per-page fingerprints */
103 1.1 maxv size_t npages; /* Number of pages. */
104 1.1 maxv size_t last_page_size; /* To support < PAGE_SIZE */
105 1.1 maxv struct veriexec_fpops *ops; /* Fingerprint ops vector*/
106 1.1 maxv size_t filename_len; /* Length of filename. */
107 1.1 maxv };
108 1.1 maxv
109 1.1 maxv /* Veriexec per-table data. */
110 1.1 maxv struct veriexec_table_entry {
111 1.1 maxv uint64_t vte_count; /* Number of Veriexec entries. */
112 1.1 maxv const struct sysctlnode *vte_node;
113 1.1 maxv };
114 1.1 maxv
115 1.1 maxv static int veriexec_verbose;
116 1.1 maxv static int veriexec_strict;
117 1.1 maxv static int veriexec_bypass = 1;
118 1.1 maxv
119 1.1 maxv static char *veriexec_fp_names = NULL;
120 1.1 maxv static size_t veriexec_name_max = 0;
121 1.1 maxv
122 1.1 maxv static const struct sysctlnode *veriexec_count_node;
123 1.1 maxv
124 1.1 maxv static fileassoc_t veriexec_hook;
125 1.1 maxv static specificdata_key_t veriexec_mountspecific_key;
126 1.1 maxv
127 1.1 maxv static LIST_HEAD(, veriexec_fpops) veriexec_fpops_list =
128 1.1 maxv LIST_HEAD_INITIALIZER(veriexec_fpops_list);
129 1.1 maxv
130 1.1 maxv static int veriexec_raw_cb(kauth_cred_t, kauth_action_t, void *,
131 1.1 maxv void *, void *, void *, void *);
132 1.1 maxv static struct veriexec_fpops *veriexec_fpops_lookup(const char *);
133 1.1 maxv static void veriexec_file_free(struct veriexec_file_entry *);
134 1.1 maxv
135 1.1 maxv static unsigned int veriexec_tablecount = 0;
136 1.1 maxv
137 1.1 maxv /*
138 1.1 maxv * Veriexec operations global lock - most ops hold this as a read
139 1.1 maxv * lock, it is upgraded to a write lock when destroying veriexec file
140 1.1 maxv * table entries.
141 1.1 maxv */
142 1.1 maxv static krwlock_t veriexec_op_lock;
143 1.1 maxv
144 1.1 maxv /*
145 1.1 maxv * Sysctl helper routine for Veriexec.
146 1.1 maxv */
147 1.1 maxv static int
148 1.1 maxv sysctl_kern_veriexec_algorithms(SYSCTLFN_ARGS)
149 1.1 maxv {
150 1.1 maxv size_t len;
151 1.1 maxv int error;
152 1.1 maxv const char *p;
153 1.1 maxv
154 1.1 maxv if (newp != NULL)
155 1.1 maxv return EPERM;
156 1.1 maxv
157 1.1 maxv if (namelen != 0)
158 1.1 maxv return EINVAL;
159 1.1 maxv
160 1.1 maxv p = veriexec_fp_names == NULL ? "" : veriexec_fp_names;
161 1.1 maxv
162 1.1 maxv len = strlen(p) + 1;
163 1.1 maxv
164 1.1 maxv if (*oldlenp < len && oldp)
165 1.1 maxv return ENOMEM;
166 1.1 maxv
167 1.1 maxv if (oldp && (error = copyout(p, oldp, len)) != 0)
168 1.1 maxv return error;
169 1.1 maxv
170 1.1 maxv *oldlenp = len;
171 1.1 maxv return 0;
172 1.1 maxv }
173 1.1 maxv
174 1.1 maxv static int
175 1.1 maxv sysctl_kern_veriexec_strict(SYSCTLFN_ARGS)
176 1.1 maxv {
177 1.1 maxv struct sysctlnode node;
178 1.1 maxv int error, newval;
179 1.1 maxv
180 1.1 maxv node = *rnode;
181 1.1 maxv node.sysctl_data = &newval;
182 1.1 maxv
183 1.1 maxv newval = veriexec_strict;
184 1.1 maxv error = sysctl_lookup(SYSCTLFN_CALL(&node));
185 1.1 maxv if (error || newp == NULL)
186 1.1 maxv return error;
187 1.1 maxv
188 1.1 maxv if (newval < veriexec_strict)
189 1.1 maxv return EPERM;
190 1.1 maxv
191 1.1 maxv veriexec_strict = newval;
192 1.1 maxv
193 1.1 maxv return 0;
194 1.1 maxv }
195 1.1 maxv
196 1.1 maxv SYSCTL_SETUP(sysctl_kern_veriexec_setup, "sysctl kern.veriexec setup")
197 1.1 maxv {
198 1.1 maxv const struct sysctlnode *rnode = NULL;
199 1.1 maxv
200 1.1 maxv sysctl_createv(clog, 0, NULL, &rnode,
201 1.1 maxv CTLFLAG_PERMANENT,
202 1.1 maxv CTLTYPE_NODE, "veriexec",
203 1.1 maxv SYSCTL_DESCR("Veriexec"),
204 1.1 maxv NULL, 0, NULL, 0,
205 1.1 maxv CTL_KERN, CTL_CREATE, CTL_EOL);
206 1.1 maxv
207 1.1 maxv sysctl_createv(clog, 0, &rnode, NULL,
208 1.1 maxv CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
209 1.1 maxv CTLTYPE_INT, "verbose",
210 1.1 maxv SYSCTL_DESCR("Veriexec verbose level"),
211 1.1 maxv NULL, 0, &veriexec_verbose, 0,
212 1.1 maxv CTL_CREATE, CTL_EOL);
213 1.1 maxv sysctl_createv(clog, 0, &rnode, NULL,
214 1.1 maxv CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
215 1.1 maxv CTLTYPE_INT, "strict",
216 1.1 maxv SYSCTL_DESCR("Veriexec strict level"),
217 1.1 maxv sysctl_kern_veriexec_strict, 0, NULL, 0,
218 1.1 maxv CTL_CREATE, CTL_EOL);
219 1.1 maxv sysctl_createv(clog, 0, &rnode, NULL,
220 1.1 maxv CTLFLAG_PERMANENT,
221 1.1 maxv CTLTYPE_STRING, "algorithms",
222 1.1 maxv SYSCTL_DESCR("Veriexec supported hashing "
223 1.1 maxv "algorithms"),
224 1.1 maxv sysctl_kern_veriexec_algorithms, 0, NULL, 0,
225 1.1 maxv CTL_CREATE, CTL_EOL);
226 1.1 maxv sysctl_createv(clog, 0, &rnode, &veriexec_count_node,
227 1.1 maxv CTLFLAG_PERMANENT,
228 1.1 maxv CTLTYPE_NODE, "count",
229 1.1 maxv SYSCTL_DESCR("Number of fingerprints on mount(s)"),
230 1.1 maxv NULL, 0, NULL, 0,
231 1.1 maxv CTL_CREATE, CTL_EOL);
232 1.1 maxv }
233 1.1 maxv
234 1.1 maxv /*
235 1.1 maxv * Add ops to the fignerprint ops vector list.
236 1.1 maxv */
237 1.1 maxv int
238 1.1 maxv veriexec_fpops_add(const char *fp_type, size_t hash_len, size_t ctx_size,
239 1.1 maxv veriexec_fpop_init_t init, veriexec_fpop_update_t update,
240 1.1 maxv veriexec_fpop_final_t final)
241 1.1 maxv {
242 1.1 maxv struct veriexec_fpops *ops;
243 1.1 maxv
244 1.1 maxv /* Sanity check all parameters. */
245 1.1 maxv if ((fp_type == NULL) || (hash_len == 0) || (ctx_size == 0) ||
246 1.1 maxv (init == NULL) || (update == NULL) || (final == NULL))
247 1.1 maxv return (EFAULT);
248 1.1 maxv
249 1.1 maxv if (veriexec_fpops_lookup(fp_type) != NULL)
250 1.1 maxv return (EEXIST);
251 1.1 maxv
252 1.1 maxv ops = kmem_alloc(sizeof(*ops), KM_SLEEP);
253 1.1 maxv
254 1.1 maxv ops->type = fp_type;
255 1.1 maxv ops->hash_len = hash_len;
256 1.1 maxv ops->context_size = ctx_size;
257 1.1 maxv ops->init = init;
258 1.1 maxv ops->update = update;
259 1.1 maxv ops->final = final;
260 1.1 maxv
261 1.1 maxv LIST_INSERT_HEAD(&veriexec_fpops_list, ops, entries);
262 1.1 maxv
263 1.1 maxv /*
264 1.1 maxv * If we don't have space for any names, allocate enough for six
265 1.1 maxv * which should be sufficient. (it's also enough for all algorithms
266 1.1 maxv * we can support at the moment)
267 1.1 maxv */
268 1.1 maxv if (veriexec_fp_names == NULL) {
269 1.1 maxv veriexec_name_max = 64;
270 1.1 maxv veriexec_fp_names = kmem_zalloc(veriexec_name_max, KM_SLEEP);
271 1.1 maxv }
272 1.1 maxv
273 1.1 maxv /*
274 1.1 maxv * If we're running out of space for storing supported algorithms,
275 1.1 maxv * extend the buffer with space for four names.
276 1.1 maxv */
277 1.1 maxv while (veriexec_name_max - (strlen(veriexec_fp_names) + 1) <
278 1.1 maxv strlen(fp_type)) {
279 1.1 maxv char *newp;
280 1.1 maxv unsigned int new_max;
281 1.1 maxv
282 1.1 maxv /* Add space for four algorithm names. */
283 1.1 maxv new_max = veriexec_name_max + 64;
284 1.1 maxv newp = kmem_zalloc(new_max, KM_SLEEP);
285 1.1 maxv strlcpy(newp, veriexec_fp_names, new_max);
286 1.1 maxv kmem_free(veriexec_fp_names, veriexec_name_max);
287 1.1 maxv veriexec_fp_names = newp;
288 1.1 maxv veriexec_name_max = new_max;
289 1.1 maxv }
290 1.1 maxv
291 1.1 maxv if (*veriexec_fp_names != '\0')
292 1.1 maxv strlcat(veriexec_fp_names, " ", veriexec_name_max);
293 1.1 maxv
294 1.1 maxv strlcat(veriexec_fp_names, fp_type, veriexec_name_max);
295 1.1 maxv
296 1.1 maxv return (0);
297 1.1 maxv }
298 1.1 maxv
299 1.1 maxv static void
300 1.1 maxv veriexec_mountspecific_dtor(void *v)
301 1.1 maxv {
302 1.1 maxv struct veriexec_table_entry *vte = v;
303 1.1 maxv
304 1.1 maxv if (vte == NULL) {
305 1.1 maxv return;
306 1.1 maxv }
307 1.1 maxv sysctl_free(__UNCONST(vte->vte_node));
308 1.1 maxv veriexec_tablecount--;
309 1.1 maxv kmem_free(vte, sizeof(*vte));
310 1.1 maxv }
311 1.1 maxv
312 1.1 maxv static int
313 1.1 maxv veriexec_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
314 1.1 maxv void *arg0, void *arg1, void *arg2, void *arg3)
315 1.1 maxv {
316 1.1 maxv int result;
317 1.1 maxv enum kauth_system_req req;
318 1.1 maxv
319 1.1 maxv if (action != KAUTH_SYSTEM_VERIEXEC)
320 1.1 maxv return KAUTH_RESULT_DEFER;
321 1.1 maxv
322 1.1 maxv result = KAUTH_RESULT_DEFER;
323 1.1 maxv req = (enum kauth_system_req)arg0;
324 1.1 maxv
325 1.1 maxv if (req == KAUTH_REQ_SYSTEM_VERIEXEC_MODIFY &&
326 1.1 maxv veriexec_strict > VERIEXEC_LEARNING) {
327 1.1 maxv log(LOG_WARNING, "Veriexec: Strict mode, modifying "
328 1.1 maxv "tables not permitted.\n");
329 1.1 maxv
330 1.1 maxv result = KAUTH_RESULT_DENY;
331 1.1 maxv }
332 1.1 maxv
333 1.1 maxv return result;
334 1.1 maxv }
335 1.1 maxv
336 1.1 maxv /*
337 1.1 maxv * Initialise Veriexec.
338 1.1 maxv */
339 1.1 maxv void
340 1.1 maxv veriexec_init(void)
341 1.1 maxv {
342 1.1 maxv int error;
343 1.1 maxv
344 1.1 maxv /* Register a fileassoc for Veriexec. */
345 1.1 maxv error = fileassoc_register("veriexec",
346 1.1 maxv (fileassoc_cleanup_cb_t)veriexec_file_free, &veriexec_hook);
347 1.1 maxv if (error)
348 1.1 maxv panic("Veriexec: Can't register fileassoc: error=%d", error);
349 1.1 maxv
350 1.1 maxv /* Register listener to handle raw disk access. */
351 1.1 maxv if (kauth_listen_scope(KAUTH_SCOPE_DEVICE, veriexec_raw_cb, NULL) ==
352 1.1 maxv NULL)
353 1.1 maxv panic("Veriexec: Can't listen on device scope");
354 1.1 maxv
355 1.1 maxv error = mount_specific_key_create(&veriexec_mountspecific_key,
356 1.1 maxv veriexec_mountspecific_dtor);
357 1.1 maxv if (error)
358 1.1 maxv panic("Veriexec: Can't create mountspecific key");
359 1.1 maxv
360 1.1 maxv if (kauth_listen_scope(KAUTH_SCOPE_SYSTEM, veriexec_listener_cb,
361 1.1 maxv NULL) == NULL)
362 1.1 maxv panic("Veriexec: Can't listen on system scope");
363 1.1 maxv
364 1.1 maxv rw_init(&veriexec_op_lock);
365 1.1 maxv
366 1.1 maxv #define FPOPS_ADD(a, b, c, d, e, f) \
367 1.1 maxv veriexec_fpops_add(a, b, c, (veriexec_fpop_init_t)d, \
368 1.1 maxv (veriexec_fpop_update_t)e, (veriexec_fpop_final_t)f)
369 1.1 maxv
370 1.1 maxv #ifdef VERIFIED_EXEC_FP_RMD160
371 1.1 maxv FPOPS_ADD("RMD160", RMD160_DIGEST_LENGTH, sizeof(RMD160_CTX),
372 1.1 maxv RMD160Init, RMD160Update, RMD160Final);
373 1.1 maxv #endif /* VERIFIED_EXEC_FP_RMD160 */
374 1.1 maxv
375 1.1 maxv #ifdef VERIFIED_EXEC_FP_SHA256
376 1.1 maxv FPOPS_ADD("SHA256", SHA256_DIGEST_LENGTH, sizeof(SHA256_CTX),
377 1.1 maxv SHA256_Init, SHA256_Update, SHA256_Final);
378 1.1 maxv #endif /* VERIFIED_EXEC_FP_SHA256 */
379 1.1 maxv
380 1.1 maxv #ifdef VERIFIED_EXEC_FP_SHA384
381 1.1 maxv FPOPS_ADD("SHA384", SHA384_DIGEST_LENGTH, sizeof(SHA384_CTX),
382 1.1 maxv SHA384_Init, SHA384_Update, SHA384_Final);
383 1.1 maxv #endif /* VERIFIED_EXEC_FP_SHA384 */
384 1.1 maxv
385 1.1 maxv #ifdef VERIFIED_EXEC_FP_SHA512
386 1.1 maxv FPOPS_ADD("SHA512", SHA512_DIGEST_LENGTH, sizeof(SHA512_CTX),
387 1.1 maxv SHA512_Init, SHA512_Update, SHA512_Final);
388 1.1 maxv #endif /* VERIFIED_EXEC_FP_SHA512 */
389 1.1 maxv
390 1.1 maxv #ifdef VERIFIED_EXEC_FP_SHA1
391 1.1 maxv FPOPS_ADD("SHA1", SHA1_DIGEST_LENGTH, sizeof(SHA1_CTX),
392 1.1 maxv SHA1Init, SHA1Update, SHA1Final);
393 1.1 maxv #endif /* VERIFIED_EXEC_FP_SHA1 */
394 1.1 maxv
395 1.1 maxv #ifdef VERIFIED_EXEC_FP_MD5
396 1.1 maxv FPOPS_ADD("MD5", MD5_DIGEST_LENGTH, sizeof(MD5_CTX),
397 1.1 maxv MD5Init, MD5Update, MD5Final);
398 1.1 maxv #endif /* VERIFIED_EXEC_FP_MD5 */
399 1.1 maxv
400 1.1 maxv #undef FPOPS_ADD
401 1.1 maxv }
402 1.1 maxv
403 1.1 maxv static struct veriexec_fpops *
404 1.1 maxv veriexec_fpops_lookup(const char *name)
405 1.1 maxv {
406 1.1 maxv struct veriexec_fpops *ops;
407 1.1 maxv
408 1.1 maxv if (name == NULL)
409 1.1 maxv return (NULL);
410 1.1 maxv
411 1.1 maxv LIST_FOREACH(ops, &veriexec_fpops_list, entries) {
412 1.1 maxv if (strcasecmp(name, ops->type) == 0)
413 1.1 maxv return (ops);
414 1.1 maxv }
415 1.1 maxv
416 1.1 maxv return (NULL);
417 1.1 maxv }
418 1.1 maxv
419 1.1 maxv /*
420 1.1 maxv * Calculate fingerprint. Information on hash length and routines used is
421 1.1 maxv * extracted from veriexec_hash_list according to the hash type.
422 1.1 maxv *
423 1.1 maxv * NOTE: vfe is assumed to be locked for writing on entry.
424 1.1 maxv */
425 1.1 maxv static int
426 1.1 maxv veriexec_fp_calc(struct lwp *l, struct vnode *vp, int lock_state,
427 1.1 maxv struct veriexec_file_entry *vfe, u_char *fp)
428 1.1 maxv {
429 1.1 maxv struct vattr va;
430 1.1 maxv void *ctx, *page_ctx;
431 1.1 maxv u_char *buf, *page_fp;
432 1.1 maxv off_t offset, len;
433 1.1 maxv size_t resid, npages;
434 1.1 maxv int error, do_perpage, pagen;
435 1.1 maxv
436 1.1 maxv if (lock_state == VERIEXEC_UNLOCKED)
437 1.1 maxv vn_lock(vp, LK_SHARED | LK_RETRY);
438 1.1 maxv error = VOP_GETATTR(vp, &va, l->l_cred);
439 1.1 maxv if (lock_state == VERIEXEC_UNLOCKED)
440 1.1 maxv VOP_UNLOCK(vp);
441 1.1 maxv if (error)
442 1.1 maxv return (error);
443 1.1 maxv
444 1.1 maxv #ifdef notyet /* XXX - for now */
445 1.1 maxv if ((vfe->type & VERIEXEC_UNTRUSTED) &&
446 1.1 maxv (vfe->page_fp_status == PAGE_FP_NONE))
447 1.1 maxv do_perpage = 1;
448 1.1 maxv else
449 1.1 maxv #endif /* notyet */
450 1.1 maxv do_perpage = 0;
451 1.1 maxv
452 1.1 maxv ctx = kmem_alloc(vfe->ops->context_size, KM_SLEEP);
453 1.1 maxv buf = kmem_alloc(PAGE_SIZE, KM_SLEEP);
454 1.1 maxv
455 1.1 maxv page_ctx = NULL;
456 1.1 maxv page_fp = NULL;
457 1.1 maxv npages = 0;
458 1.1 maxv if (do_perpage) {
459 1.1 maxv npages = (va.va_size >> PAGE_SHIFT) + 1;
460 1.1 maxv page_fp = kmem_alloc(vfe->ops->hash_len * npages, KM_SLEEP);
461 1.1 maxv vfe->page_fp = page_fp;
462 1.1 maxv page_ctx = kmem_alloc(vfe->ops->context_size, KM_SLEEP);
463 1.1 maxv }
464 1.1 maxv
465 1.1 maxv (vfe->ops->init)(ctx);
466 1.1 maxv
467 1.1 maxv len = 0;
468 1.1 maxv error = 0;
469 1.1 maxv pagen = 0;
470 1.1 maxv for (offset = 0; offset < va.va_size; offset += PAGE_SIZE) {
471 1.1 maxv len = ((va.va_size - offset) < PAGE_SIZE) ?
472 1.1 maxv (va.va_size - offset) : PAGE_SIZE;
473 1.1 maxv
474 1.1 maxv error = vn_rdwr(UIO_READ, vp, buf, len, offset,
475 1.1 maxv UIO_SYSSPACE,
476 1.1 maxv ((lock_state == VERIEXEC_LOCKED)?
477 1.1 maxv IO_NODELOCKED : 0),
478 1.1 maxv l->l_cred, &resid, NULL);
479 1.1 maxv
480 1.1 maxv if (error) {
481 1.1 maxv if (do_perpage) {
482 1.1 maxv kmem_free(vfe->page_fp,
483 1.1 maxv vfe->ops->hash_len * npages);
484 1.1 maxv vfe->page_fp = NULL;
485 1.1 maxv }
486 1.1 maxv
487 1.1 maxv goto bad;
488 1.1 maxv }
489 1.1 maxv
490 1.1 maxv (vfe->ops->update)(ctx, buf, (unsigned int) len);
491 1.1 maxv
492 1.1 maxv if (do_perpage) {
493 1.1 maxv (vfe->ops->init)(page_ctx);
494 1.1 maxv (vfe->ops->update)(page_ctx, buf, (unsigned int)len);
495 1.1 maxv (vfe->ops->final)(page_fp, page_ctx);
496 1.1 maxv
497 1.1 maxv if (veriexec_verbose >= 2) {
498 1.1 maxv int i;
499 1.1 maxv
500 1.1 maxv printf("hash for page %d: ", pagen);
501 1.1 maxv for (i = 0; i < vfe->ops->hash_len; i++)
502 1.1 maxv printf("%02x", page_fp[i]);
503 1.1 maxv printf("\n");
504 1.1 maxv }
505 1.1 maxv
506 1.1 maxv page_fp += vfe->ops->hash_len;
507 1.1 maxv pagen++;
508 1.1 maxv }
509 1.1 maxv
510 1.1 maxv if (len != PAGE_SIZE)
511 1.1 maxv break;
512 1.1 maxv }
513 1.1 maxv
514 1.1 maxv (vfe->ops->final)(fp, ctx);
515 1.1 maxv
516 1.1 maxv if (do_perpage) {
517 1.1 maxv vfe->last_page_size = len;
518 1.1 maxv vfe->page_fp_status = PAGE_FP_READY;
519 1.1 maxv vfe->npages = npages;
520 1.1 maxv }
521 1.1 maxv
522 1.1 maxv bad:
523 1.1 maxv if (do_perpage)
524 1.1 maxv kmem_free(page_ctx, vfe->ops->context_size);
525 1.1 maxv
526 1.1 maxv kmem_free(ctx, vfe->ops->context_size);
527 1.1 maxv kmem_free(buf, PAGE_SIZE);
528 1.1 maxv
529 1.1 maxv return (error);
530 1.1 maxv }
531 1.1 maxv
532 1.1 maxv /* Compare two fingerprints of the same type. */
533 1.1 maxv static int
534 1.1 maxv veriexec_fp_cmp(struct veriexec_fpops *ops, u_char *fp1, u_char *fp2)
535 1.1 maxv {
536 1.1 maxv if (veriexec_verbose >= 2) {
537 1.1 maxv int i;
538 1.1 maxv
539 1.1 maxv printf("comparing hashes...\n");
540 1.1 maxv printf("fp1: ");
541 1.1 maxv for (i = 0; i < ops->hash_len; i++) {
542 1.1 maxv printf("%02x", fp1[i]);
543 1.1 maxv }
544 1.1 maxv printf("\nfp2: ");
545 1.1 maxv for (i = 0; i < ops->hash_len; i++) {
546 1.1 maxv printf("%02x", fp2[i]);
547 1.1 maxv }
548 1.1 maxv printf("\n");
549 1.1 maxv }
550 1.1 maxv
551 1.1 maxv return (memcmp(fp1, fp2, ops->hash_len));
552 1.1 maxv }
553 1.1 maxv
554 1.1 maxv static struct veriexec_table_entry *
555 1.1 maxv veriexec_table_lookup(struct mount *mp)
556 1.1 maxv {
557 1.1 maxv /* XXX: From raidframe init */
558 1.1 maxv if (mp == NULL)
559 1.1 maxv return NULL;
560 1.1 maxv
561 1.1 maxv return mount_getspecific(mp, veriexec_mountspecific_key);
562 1.1 maxv }
563 1.1 maxv
564 1.1 maxv static struct veriexec_file_entry *
565 1.1 maxv veriexec_get(struct vnode *vp)
566 1.1 maxv {
567 1.1 maxv return (fileassoc_lookup(vp, veriexec_hook));
568 1.1 maxv }
569 1.1 maxv
570 1.1 maxv bool
571 1.1 maxv veriexec_lookup(struct vnode *vp)
572 1.1 maxv {
573 1.1 maxv return (veriexec_get(vp) == NULL ? false : true);
574 1.1 maxv }
575 1.1 maxv
576 1.1 maxv /*
577 1.1 maxv * Routine for maintaining mostly consistent message formats in Veriexec.
578 1.1 maxv */
579 1.1 maxv static void
580 1.1 maxv veriexec_file_report(struct veriexec_file_entry *vfe, const u_char *msg,
581 1.1 maxv const u_char *filename, struct lwp *l, int f)
582 1.1 maxv {
583 1.1 maxv if (vfe != NULL && vfe->filename != NULL)
584 1.1 maxv filename = vfe->filename;
585 1.1 maxv
586 1.1 maxv if (filename == NULL)
587 1.1 maxv return;
588 1.1 maxv
589 1.1 maxv if (((f & REPORT_LOGMASK) >> 1) <= veriexec_verbose) {
590 1.1 maxv if (!(f & REPORT_ALARM) || (l == NULL))
591 1.1 maxv log(LOG_NOTICE, "Veriexec: %s [%s]\n", msg,
592 1.1 maxv filename);
593 1.1 maxv else
594 1.1 maxv log(LOG_ALERT, "Veriexec: %s [%s, prog=%s pid=%u, "
595 1.1 maxv "uid=%u, gid=%u]\n", msg, filename,
596 1.1 maxv l->l_proc->p_comm, l->l_proc->p_pid,
597 1.1 maxv kauth_cred_getuid(l->l_cred),
598 1.1 maxv kauth_cred_getgid(l->l_cred));
599 1.1 maxv }
600 1.1 maxv
601 1.1 maxv if (f & REPORT_PANIC)
602 1.1 maxv panic("Veriexec: Unrecoverable error.");
603 1.1 maxv }
604 1.1 maxv
605 1.1 maxv /*
606 1.1 maxv * Verify the fingerprint of the given file. If we're called directly from
607 1.1 maxv * sys_execve(), 'flag' will be VERIEXEC_DIRECT. If we're called from
608 1.1 maxv * exec_script(), 'flag' will be VERIEXEC_INDIRECT. If we are called from
609 1.1 maxv * vn_open(), 'flag' will be VERIEXEC_FILE.
610 1.1 maxv *
611 1.1 maxv * NOTE: The veriexec file entry pointer (vfep) will be returned LOCKED
612 1.1 maxv * on no error.
613 1.1 maxv */
614 1.1 maxv static int
615 1.1 maxv veriexec_file_verify(struct lwp *l, struct vnode *vp, const u_char *name,
616 1.1 maxv int flag, int lockstate, struct veriexec_file_entry **vfep)
617 1.1 maxv {
618 1.1 maxv struct veriexec_file_entry *vfe;
619 1.1 maxv int error;
620 1.1 maxv
621 1.1 maxv #define VFE_NEEDS_EVAL(vfe) ((vfe->status == FINGERPRINT_NOTEVAL) || \
622 1.1 maxv (vfe->type & VERIEXEC_UNTRUSTED))
623 1.1 maxv
624 1.1 maxv if (vfep != NULL)
625 1.1 maxv *vfep = NULL;
626 1.1 maxv
627 1.1 maxv if (vp->v_type != VREG)
628 1.1 maxv return (0);
629 1.1 maxv
630 1.1 maxv if (lockstate == VERIEXEC_UNLOCKED)
631 1.1 maxv rw_enter(&veriexec_op_lock, RW_READER);
632 1.1 maxv
633 1.1 maxv /* Lookup veriexec table entry, save pointer if requested. */
634 1.1 maxv vfe = veriexec_get(vp);
635 1.1 maxv if (vfep != NULL)
636 1.1 maxv *vfep = vfe;
637 1.1 maxv if (vfe == NULL)
638 1.1 maxv goto out;
639 1.1 maxv
640 1.1 maxv error = 0;
641 1.1 maxv
642 1.1 maxv /*
643 1.1 maxv * Grab the lock for the entry, if we need to do an evaluation
644 1.1 maxv * then the lock is a write lock, after we have the write
645 1.1 maxv * lock, check if we really need it - some other thread may
646 1.1 maxv * have already done the work for us.
647 1.1 maxv */
648 1.1 maxv if (VFE_NEEDS_EVAL(vfe)) {
649 1.1 maxv rw_enter(&vfe->lock, RW_WRITER);
650 1.1 maxv if (!VFE_NEEDS_EVAL(vfe))
651 1.1 maxv rw_downgrade(&vfe->lock);
652 1.1 maxv } else
653 1.1 maxv rw_enter(&vfe->lock, RW_READER);
654 1.1 maxv
655 1.1 maxv /* Evaluate fingerprint if needed. */
656 1.1 maxv if (VFE_NEEDS_EVAL(vfe)) {
657 1.1 maxv u_char *digest;
658 1.1 maxv
659 1.1 maxv /* Calculate fingerprint for on-disk file. */
660 1.1 maxv digest = kmem_zalloc(vfe->ops->hash_len, KM_SLEEP);
661 1.1 maxv
662 1.1 maxv error = veriexec_fp_calc(l, vp, lockstate, vfe, digest);
663 1.1 maxv if (error) {
664 1.1 maxv veriexec_file_report(vfe, "Fingerprint calculation error.",
665 1.1 maxv name, NULL, REPORT_ALWAYS);
666 1.1 maxv kmem_free(digest, vfe->ops->hash_len);
667 1.1 maxv rw_exit(&vfe->lock);
668 1.1 maxv if (lockstate == VERIEXEC_UNLOCKED)
669 1.1 maxv rw_exit(&veriexec_op_lock);
670 1.1 maxv return (error);
671 1.1 maxv }
672 1.1 maxv
673 1.1 maxv /* Compare fingerprint with loaded data. */
674 1.1 maxv if (veriexec_fp_cmp(vfe->ops, vfe->fp, digest) == 0)
675 1.1 maxv vfe->status = FINGERPRINT_VALID;
676 1.1 maxv else
677 1.1 maxv vfe->status = FINGERPRINT_NOMATCH;
678 1.1 maxv
679 1.1 maxv kmem_free(digest, vfe->ops->hash_len);
680 1.1 maxv rw_downgrade(&vfe->lock);
681 1.1 maxv }
682 1.1 maxv
683 1.1 maxv if (!(vfe->type & flag)) {
684 1.1 maxv veriexec_file_report(vfe, "Incorrect access type.", name, l,
685 1.1 maxv REPORT_ALWAYS|REPORT_ALARM);
686 1.1 maxv
687 1.1 maxv /* IPS mode: Enforce access type. */
688 1.1 maxv if (veriexec_strict >= VERIEXEC_IPS) {
689 1.1 maxv rw_exit(&vfe->lock);
690 1.1 maxv if (lockstate == VERIEXEC_UNLOCKED)
691 1.1 maxv rw_exit(&veriexec_op_lock);
692 1.1 maxv return (EPERM);
693 1.1 maxv }
694 1.1 maxv }
695 1.1 maxv
696 1.1 maxv out:
697 1.1 maxv /* No entry in the veriexec tables. */
698 1.1 maxv if (vfe == NULL) {
699 1.1 maxv veriexec_file_report(NULL, "No entry.", name,
700 1.1 maxv l, REPORT_VERBOSE);
701 1.1 maxv
702 1.1 maxv if (lockstate == VERIEXEC_UNLOCKED)
703 1.1 maxv rw_exit(&veriexec_op_lock);
704 1.1 maxv /*
705 1.1 maxv * Lockdown mode: Deny access to non-monitored files.
706 1.1 maxv * IPS mode: Deny execution of non-monitored files.
707 1.1 maxv */
708 1.1 maxv if ((veriexec_strict >= VERIEXEC_LOCKDOWN) ||
709 1.1 maxv ((veriexec_strict >= VERIEXEC_IPS) &&
710 1.1 maxv (flag != VERIEXEC_FILE)))
711 1.1 maxv return (EPERM);
712 1.1 maxv
713 1.1 maxv return (0);
714 1.1 maxv }
715 1.1 maxv
716 1.2 maxv switch (vfe->status) {
717 1.1 maxv case FINGERPRINT_NOTEVAL:
718 1.1 maxv /* Should not happen. */
719 1.1 maxv rw_exit(&vfe->lock);
720 1.1 maxv if (lockstate == VERIEXEC_UNLOCKED)
721 1.1 maxv rw_exit(&veriexec_op_lock);
722 1.1 maxv veriexec_file_report(vfe, "Not-evaluated status "
723 1.1 maxv "post evaluation; inconsistency detected.", name,
724 1.1 maxv NULL, REPORT_ALWAYS|REPORT_PANIC);
725 1.2 maxv /* NOTREACHED */
726 1.1 maxv
727 1.1 maxv case FINGERPRINT_VALID:
728 1.1 maxv /* Valid fingerprint. */
729 1.1 maxv veriexec_file_report(vfe, "Match.", name, NULL,
730 1.1 maxv REPORT_VERBOSE);
731 1.1 maxv
732 1.1 maxv break;
733 1.1 maxv
734 1.1 maxv case FINGERPRINT_NOMATCH:
735 1.1 maxv /* Fingerprint mismatch. */
736 1.1 maxv veriexec_file_report(vfe, "Mismatch.", name,
737 1.1 maxv NULL, REPORT_ALWAYS|REPORT_ALARM);
738 1.1 maxv
739 1.1 maxv /* IDS mode: Deny access on fingerprint mismatch. */
740 1.1 maxv if (veriexec_strict >= VERIEXEC_IDS) {
741 1.1 maxv rw_exit(&vfe->lock);
742 1.1 maxv error = EPERM;
743 1.1 maxv }
744 1.1 maxv
745 1.1 maxv break;
746 1.1 maxv
747 1.1 maxv default:
748 1.1 maxv /* Should never happen. */
749 1.1 maxv rw_exit(&vfe->lock);
750 1.1 maxv if (lockstate == VERIEXEC_UNLOCKED)
751 1.1 maxv rw_exit(&veriexec_op_lock);
752 1.1 maxv veriexec_file_report(vfe, "Invalid status "
753 1.1 maxv "post evaluation.", name, NULL, REPORT_ALWAYS|REPORT_PANIC);
754 1.2 maxv /* NOTREACHED */
755 1.2 maxv }
756 1.1 maxv
757 1.1 maxv if (lockstate == VERIEXEC_UNLOCKED)
758 1.1 maxv rw_exit(&veriexec_op_lock);
759 1.1 maxv return (error);
760 1.1 maxv }
761 1.1 maxv
762 1.1 maxv int
763 1.1 maxv veriexec_verify(struct lwp *l, struct vnode *vp, const u_char *name, int flag,
764 1.1 maxv bool *found)
765 1.1 maxv {
766 1.1 maxv struct veriexec_file_entry *vfe;
767 1.1 maxv int r;
768 1.1 maxv
769 1.1 maxv if (veriexec_bypass && (veriexec_strict == VERIEXEC_LEARNING))
770 1.1 maxv return 0;
771 1.1 maxv
772 1.1 maxv r = veriexec_file_verify(l, vp, name, flag, VERIEXEC_UNLOCKED, &vfe);
773 1.1 maxv
774 1.1 maxv if ((r == 0) && (vfe != NULL))
775 1.1 maxv rw_exit(&vfe->lock);
776 1.1 maxv
777 1.1 maxv if (found != NULL)
778 1.1 maxv *found = (vfe != NULL) ? true : false;
779 1.1 maxv
780 1.1 maxv return (r);
781 1.1 maxv }
782 1.1 maxv
783 1.1 maxv #ifdef notyet
784 1.1 maxv /*
785 1.1 maxv * Evaluate per-page fingerprints.
786 1.1 maxv */
787 1.1 maxv int
788 1.1 maxv veriexec_page_verify(struct veriexec_file_entry *vfe, struct vm_page *pg,
789 1.1 maxv size_t idx, struct lwp *l)
790 1.1 maxv {
791 1.1 maxv void *ctx;
792 1.1 maxv u_char *fp;
793 1.1 maxv u_char *page_fp;
794 1.1 maxv int error;
795 1.1 maxv vaddr_t kva;
796 1.1 maxv
797 1.1 maxv if (vfe->page_fp_status == PAGE_FP_NONE)
798 1.1 maxv return (0);
799 1.1 maxv
800 1.1 maxv if (vfe->page_fp_status == PAGE_FP_FAIL)
801 1.1 maxv return (EPERM);
802 1.1 maxv
803 1.1 maxv if (idx >= vfe->npages)
804 1.1 maxv return (0);
805 1.1 maxv
806 1.1 maxv ctx = kmem_alloc(vfe->ops->context_size, KM_SLEEP);
807 1.1 maxv fp = kmem_alloc(vfe->ops->hash_len, KM_SLEEP);
808 1.1 maxv kva = uvm_km_alloc(kernel_map, PAGE_SIZE, VM_PGCOLOR_BUCKET(pg),
809 1.1 maxv UVM_KMF_COLORMATCH | UVM_KMF_VAONLY | UVM_KMF_WAITVA);
810 1.1 maxv pmap_kenter_pa(kva, VM_PAGE_TO_PHYS(pg), VM_PROT_READ, 0);
811 1.1 maxv pmap_update(pmap_kernel());
812 1.1 maxv
813 1.1 maxv page_fp = (u_char *) vfe->page_fp + (vfe->ops->hash_len * idx);
814 1.1 maxv (vfe->ops->init)(ctx);
815 1.1 maxv (vfe->ops->update)(ctx, (void *) kva,
816 1.1 maxv ((vfe->npages - 1) == idx) ? vfe->last_page_size
817 1.1 maxv : PAGE_SIZE);
818 1.1 maxv (vfe->ops->final)(fp, ctx);
819 1.1 maxv
820 1.1 maxv pmap_kremove(kva, PAGE_SIZE);
821 1.1 maxv pmap_update(pmap_kernel());
822 1.1 maxv uvm_km_free(kernel_map, kva, PAGE_SIZE, UVM_KMF_VAONLY);
823 1.1 maxv
824 1.1 maxv error = veriexec_fp_cmp(vfe->ops, page_fp, fp);
825 1.1 maxv if (error) {
826 1.1 maxv const char *msg;
827 1.1 maxv
828 1.1 maxv if (veriexec_strict > VERIEXEC_LEARNING) {
829 1.1 maxv msg = "Pages modified: Killing process.";
830 1.1 maxv } else {
831 1.1 maxv msg = "Pages modified.";
832 1.1 maxv error = 0;
833 1.1 maxv }
834 1.1 maxv
835 1.1 maxv veriexec_file_report(msg, "[page_in]", l,
836 1.1 maxv REPORT_ALWAYS|REPORT_ALARM);
837 1.1 maxv
838 1.1 maxv if (error) {
839 1.1 maxv ksiginfo_t ksi;
840 1.1 maxv
841 1.1 maxv KSI_INIT(&ksi);
842 1.1 maxv ksi.ksi_signo = SIGKILL;
843 1.1 maxv ksi.ksi_code = SI_NOINFO;
844 1.1 maxv ksi.ksi_pid = l->l_proc->p_pid;
845 1.1 maxv ksi.ksi_uid = 0;
846 1.1 maxv
847 1.1 maxv kpsignal(l->l_proc, &ksi, NULL);
848 1.1 maxv }
849 1.1 maxv }
850 1.1 maxv
851 1.1 maxv kmem_free(ctx, vfe->ops->context_size);
852 1.1 maxv kmem_free(fp, vfe->ops->hash_len);
853 1.1 maxv
854 1.1 maxv return (error);
855 1.1 maxv }
856 1.1 maxv #endif /* notyet */
857 1.1 maxv
858 1.1 maxv /*
859 1.1 maxv * Veriexec remove policy code.
860 1.1 maxv */
861 1.1 maxv int
862 1.1 maxv veriexec_removechk(struct lwp *l, struct vnode *vp, const char *pathbuf)
863 1.1 maxv {
864 1.1 maxv struct veriexec_file_entry *vfe;
865 1.1 maxv int error;
866 1.1 maxv
867 1.1 maxv if (veriexec_bypass && (veriexec_strict == VERIEXEC_LEARNING))
868 1.1 maxv return 0;
869 1.1 maxv
870 1.1 maxv rw_enter(&veriexec_op_lock, RW_READER);
871 1.1 maxv
872 1.1 maxv vfe = veriexec_get(vp);
873 1.1 maxv rw_exit(&veriexec_op_lock);
874 1.1 maxv
875 1.1 maxv if (vfe == NULL) {
876 1.1 maxv /* Lockdown mode: Deny access to non-monitored files. */
877 1.1 maxv if (veriexec_strict >= VERIEXEC_LOCKDOWN)
878 1.1 maxv return (EPERM);
879 1.1 maxv
880 1.1 maxv return (0);
881 1.1 maxv }
882 1.1 maxv
883 1.1 maxv veriexec_file_report(vfe, "Remove request.", pathbuf, l,
884 1.1 maxv REPORT_ALWAYS|REPORT_ALARM);
885 1.1 maxv
886 1.1 maxv /* IDS mode: Deny removal of monitored files. */
887 1.1 maxv if (veriexec_strict >= VERIEXEC_IDS)
888 1.1 maxv error = EPERM;
889 1.1 maxv else
890 1.1 maxv error = veriexec_file_delete(l, vp);
891 1.1 maxv
892 1.1 maxv return error;
893 1.1 maxv }
894 1.1 maxv
895 1.1 maxv /*
896 1.1 maxv * Veriexec rename policy.
897 1.1 maxv *
898 1.1 maxv * XXX: Once there's a way to hook after a successful rename, it would be
899 1.1 maxv * XXX: nice to update vfe->filename to the new name if it's not NULL and
900 1.1 maxv * XXX: the new name is absolute (ie., starts with a slash).
901 1.1 maxv */
902 1.1 maxv int
903 1.1 maxv veriexec_renamechk(struct lwp *l, struct vnode *fromvp, const char *fromname,
904 1.1 maxv struct vnode *tovp, const char *toname)
905 1.1 maxv {
906 1.1 maxv struct veriexec_file_entry *vfe, *tvfe;
907 1.1 maxv
908 1.1 maxv if (veriexec_bypass && (veriexec_strict == VERIEXEC_LEARNING))
909 1.1 maxv return 0;
910 1.1 maxv
911 1.1 maxv rw_enter(&veriexec_op_lock, RW_READER);
912 1.1 maxv
913 1.1 maxv if (veriexec_strict >= VERIEXEC_LOCKDOWN) {
914 1.1 maxv log(LOG_ALERT, "Veriexec: Preventing rename of `%s' to "
915 1.1 maxv "`%s', uid=%u, pid=%u: Lockdown mode.\n", fromname, toname,
916 1.1 maxv kauth_cred_geteuid(l->l_cred), l->l_proc->p_pid);
917 1.1 maxv
918 1.1 maxv rw_exit(&veriexec_op_lock);
919 1.1 maxv return (EPERM);
920 1.1 maxv }
921 1.1 maxv
922 1.1 maxv vfe = veriexec_get(fromvp);
923 1.1 maxv tvfe = NULL;
924 1.1 maxv if (tovp != NULL)
925 1.1 maxv tvfe = veriexec_get(tovp);
926 1.1 maxv
927 1.1 maxv if ((vfe != NULL) || (tvfe != NULL)) {
928 1.1 maxv if (veriexec_strict >= VERIEXEC_IPS) {
929 1.1 maxv log(LOG_ALERT, "Veriexec: Preventing rename of `%s' "
930 1.1 maxv "to `%s', uid=%u, pid=%u: IPS mode, %s "
931 1.1 maxv "monitored.\n", fromname, toname,
932 1.1 maxv kauth_cred_geteuid(l->l_cred),
933 1.1 maxv l->l_proc->p_pid, (vfe != NULL && tvfe != NULL) ?
934 1.1 maxv "files" : "file");
935 1.1 maxv
936 1.1 maxv rw_exit(&veriexec_op_lock);
937 1.1 maxv return (EPERM);
938 1.1 maxv }
939 1.1 maxv
940 1.1 maxv /*
941 1.1 maxv * Monitored file is renamed; filename no longer relevant.
942 1.1 maxv *
943 1.1 maxv * XXX: We could keep the buffer, and when (and if) updating the
944 1.1 maxv * XXX: filename post-rename, re-allocate it only if it's not
945 1.1 maxv * XXX: big enough for the new filename.
946 1.1 maxv */
947 1.1 maxv if (vfe != NULL) {
948 1.1 maxv /* XXXX get write lock on vfe here? */
949 1.1 maxv
950 1.1 maxv VERIEXEC_RW_UPGRADE(&veriexec_op_lock);
951 1.1 maxv /* once we have the op lock in write mode
952 1.1 maxv * there should be no locks on any file
953 1.1 maxv * entries so we can destroy the object.
954 1.1 maxv */
955 1.1 maxv
956 1.1 maxv if (vfe->filename_len > 0)
957 1.1 maxv kmem_free(vfe->filename, vfe->filename_len);
958 1.1 maxv
959 1.1 maxv vfe->filename = NULL;
960 1.1 maxv vfe->filename_len = 0;
961 1.1 maxv
962 1.1 maxv rw_downgrade(&veriexec_op_lock);
963 1.1 maxv }
964 1.1 maxv
965 1.1 maxv log(LOG_NOTICE, "Veriexec: %s file `%s' renamed to "
966 1.1 maxv "%s file `%s', uid=%u, pid=%u.\n", (vfe != NULL) ?
967 1.1 maxv "Monitored" : "Non-monitored", fromname, (tvfe != NULL) ?
968 1.1 maxv "monitored" : "non-monitored", toname,
969 1.1 maxv kauth_cred_geteuid(l->l_cred), l->l_proc->p_pid);
970 1.1 maxv
971 1.1 maxv rw_exit(&veriexec_op_lock);
972 1.1 maxv
973 1.1 maxv /*
974 1.1 maxv * Monitored file is overwritten. Remove the entry.
975 1.1 maxv */
976 1.1 maxv if (tvfe != NULL)
977 1.1 maxv (void)veriexec_file_delete(l, tovp);
978 1.1 maxv
979 1.1 maxv } else
980 1.1 maxv rw_exit(&veriexec_op_lock);
981 1.1 maxv
982 1.1 maxv return (0);
983 1.1 maxv }
984 1.1 maxv
985 1.1 maxv static void
986 1.1 maxv veriexec_file_free(struct veriexec_file_entry *vfe)
987 1.1 maxv {
988 1.1 maxv if (vfe != NULL) {
989 1.1 maxv if (vfe->fp != NULL)
990 1.1 maxv kmem_free(vfe->fp, vfe->ops->hash_len);
991 1.1 maxv if (vfe->page_fp != NULL)
992 1.1 maxv kmem_free(vfe->page_fp, vfe->ops->hash_len);
993 1.1 maxv if (vfe->filename != NULL)
994 1.1 maxv kmem_free(vfe->filename, vfe->filename_len);
995 1.1 maxv rw_destroy(&vfe->lock);
996 1.1 maxv kmem_free(vfe, sizeof(*vfe));
997 1.1 maxv }
998 1.1 maxv }
999 1.1 maxv
1000 1.1 maxv static void
1001 1.1 maxv veriexec_file_purge(struct veriexec_file_entry *vfe, int have_lock)
1002 1.1 maxv {
1003 1.1 maxv if (vfe == NULL)
1004 1.1 maxv return;
1005 1.1 maxv
1006 1.1 maxv if (have_lock == VERIEXEC_UNLOCKED)
1007 1.1 maxv rw_enter(&vfe->lock, RW_WRITER);
1008 1.1 maxv else
1009 1.1 maxv VERIEXEC_RW_UPGRADE(&vfe->lock);
1010 1.1 maxv
1011 1.1 maxv vfe->status = FINGERPRINT_NOTEVAL;
1012 1.1 maxv if (have_lock == VERIEXEC_UNLOCKED)
1013 1.1 maxv rw_exit(&vfe->lock);
1014 1.1 maxv else
1015 1.1 maxv rw_downgrade(&vfe->lock);
1016 1.1 maxv }
1017 1.1 maxv
1018 1.1 maxv static void
1019 1.1 maxv veriexec_file_purge_cb(struct veriexec_file_entry *vfe, void *cookie)
1020 1.1 maxv {
1021 1.1 maxv veriexec_file_purge(vfe, VERIEXEC_UNLOCKED);
1022 1.1 maxv }
1023 1.1 maxv
1024 1.1 maxv /*
1025 1.1 maxv * Invalidate a Veriexec file entry.
1026 1.1 maxv * XXX: This should be updated when per-page fingerprints are added.
1027 1.1 maxv */
1028 1.1 maxv void
1029 1.1 maxv veriexec_purge(struct vnode *vp)
1030 1.1 maxv {
1031 1.1 maxv rw_enter(&veriexec_op_lock, RW_READER);
1032 1.1 maxv veriexec_file_purge(veriexec_get(vp), VERIEXEC_UNLOCKED);
1033 1.1 maxv rw_exit(&veriexec_op_lock);
1034 1.1 maxv }
1035 1.1 maxv
1036 1.1 maxv /*
1037 1.1 maxv * Enforce raw disk access policy.
1038 1.1 maxv *
1039 1.1 maxv * IDS mode: Invalidate fingerprints on a mount if it's opened for writing.
1040 1.1 maxv * IPS mode: Don't allow raw writing to disks we monitor.
1041 1.1 maxv * Lockdown mode: Don't allow raw writing to all disks.
1042 1.1 maxv *
1043 1.1 maxv * XXX: This is bogus. There's an obvious race condition between the time
1044 1.1 maxv * XXX: the disk is open for writing, in which an attacker can access a
1045 1.1 maxv * XXX: monitored file to get its signature cached again, and when the raw
1046 1.1 maxv * XXX: file is overwritten on disk.
1047 1.1 maxv * XXX:
1048 1.1 maxv * XXX: To solve this, we need something like the following:
1049 1.1 maxv * XXX: open raw disk:
1050 1.1 maxv * XXX: - raise refcount,
1051 1.1 maxv * XXX: - invalidate fingerprints,
1052 1.1 maxv * XXX: - mark all entries for that disk with "no cache" flag
1053 1.1 maxv * XXX:
1054 1.1 maxv * XXX: veriexec_verify:
1055 1.1 maxv * XXX: - if "no cache", don't cache evaluation result
1056 1.1 maxv * XXX:
1057 1.1 maxv * XXX: close raw disk:
1058 1.1 maxv * XXX: - lower refcount,
1059 1.1 maxv * XXX: - if refcount == 0, remove "no cache" flag from all entries
1060 1.1 maxv */
1061 1.1 maxv static int
1062 1.1 maxv veriexec_raw_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
1063 1.1 maxv void *arg0, void *arg1, void *arg2, void *arg3)
1064 1.1 maxv {
1065 1.1 maxv int result;
1066 1.1 maxv enum kauth_device_req req;
1067 1.1 maxv struct veriexec_table_entry *vte;
1068 1.1 maxv
1069 1.1 maxv result = KAUTH_RESULT_DENY;
1070 1.1 maxv req = (enum kauth_device_req)arg0;
1071 1.1 maxv
1072 1.1 maxv switch (action) {
1073 1.1 maxv case KAUTH_DEVICE_RAWIO_SPEC: {
1074 1.1 maxv struct vnode *vp, *bvp;
1075 1.1 maxv int error;
1076 1.1 maxv
1077 1.1 maxv if (req == KAUTH_REQ_DEVICE_RAWIO_SPEC_READ) {
1078 1.1 maxv result = KAUTH_RESULT_DEFER;
1079 1.1 maxv break;
1080 1.1 maxv }
1081 1.1 maxv
1082 1.1 maxv vp = arg1;
1083 1.1 maxv KASSERT(vp != NULL);
1084 1.1 maxv
1085 1.1 maxv /* Handle /dev/mem and /dev/kmem. */
1086 1.1 maxv if (iskmemvp(vp)) {
1087 1.1 maxv if (veriexec_strict < VERIEXEC_IPS)
1088 1.1 maxv result = KAUTH_RESULT_DEFER;
1089 1.1 maxv
1090 1.1 maxv break;
1091 1.1 maxv }
1092 1.1 maxv
1093 1.1 maxv error = rawdev_mounted(vp, &bvp);
1094 1.1 maxv if (error == EINVAL) {
1095 1.1 maxv result = KAUTH_RESULT_DEFER;
1096 1.1 maxv break;
1097 1.1 maxv }
1098 1.1 maxv
1099 1.1 maxv /*
1100 1.1 maxv * XXX: See vfs_mountedon() comment in rawdev_mounted().
1101 1.1 maxv */
1102 1.1 maxv vte = veriexec_table_lookup(bvp->v_mount);
1103 1.1 maxv if (vte == NULL) {
1104 1.1 maxv result = KAUTH_RESULT_DEFER;
1105 1.1 maxv break;
1106 1.1 maxv }
1107 1.1 maxv
1108 1.1 maxv switch (veriexec_strict) {
1109 1.1 maxv case VERIEXEC_LEARNING:
1110 1.1 maxv case VERIEXEC_IDS:
1111 1.1 maxv result = KAUTH_RESULT_DEFER;
1112 1.1 maxv
1113 1.1 maxv rw_enter(&veriexec_op_lock, RW_WRITER);
1114 1.1 maxv fileassoc_table_run(bvp->v_mount, veriexec_hook,
1115 1.1 maxv (fileassoc_cb_t)veriexec_file_purge_cb, NULL);
1116 1.1 maxv rw_exit(&veriexec_op_lock);
1117 1.1 maxv
1118 1.1 maxv break;
1119 1.1 maxv case VERIEXEC_IPS:
1120 1.1 maxv result = KAUTH_RESULT_DENY;
1121 1.1 maxv break;
1122 1.1 maxv case VERIEXEC_LOCKDOWN:
1123 1.1 maxv result = KAUTH_RESULT_DENY;
1124 1.1 maxv break;
1125 1.1 maxv }
1126 1.1 maxv
1127 1.1 maxv break;
1128 1.1 maxv }
1129 1.1 maxv
1130 1.1 maxv case KAUTH_DEVICE_RAWIO_PASSTHRU:
1131 1.1 maxv /* XXX What can we do here? */
1132 1.1 maxv if (veriexec_strict < VERIEXEC_IPS)
1133 1.1 maxv result = KAUTH_RESULT_DEFER;
1134 1.1 maxv
1135 1.1 maxv break;
1136 1.1 maxv
1137 1.1 maxv default:
1138 1.1 maxv result = KAUTH_RESULT_DEFER;
1139 1.1 maxv break;
1140 1.1 maxv }
1141 1.1 maxv
1142 1.1 maxv return (result);
1143 1.1 maxv }
1144 1.1 maxv
1145 1.1 maxv /*
1146 1.1 maxv * Create a new Veriexec table.
1147 1.1 maxv */
1148 1.1 maxv static struct veriexec_table_entry *
1149 1.1 maxv veriexec_table_add(struct lwp *l, struct mount *mp)
1150 1.1 maxv {
1151 1.1 maxv struct veriexec_table_entry *vte;
1152 1.1 maxv u_char buf[16];
1153 1.1 maxv
1154 1.1 maxv vte = kmem_zalloc(sizeof(*vte), KM_SLEEP);
1155 1.1 maxv mount_setspecific(mp, veriexec_mountspecific_key, vte);
1156 1.1 maxv
1157 1.1 maxv snprintf(buf, sizeof(buf), "table%u", veriexec_tablecount++);
1158 1.1 maxv sysctl_createv(NULL, 0, &veriexec_count_node, &vte->vte_node,
1159 1.1 maxv 0, CTLTYPE_NODE, buf, NULL, NULL, 0, NULL,
1160 1.1 maxv 0, CTL_CREATE, CTL_EOL);
1161 1.1 maxv
1162 1.1 maxv sysctl_createv(NULL, 0, &vte->vte_node, NULL,
1163 1.1 maxv CTLFLAG_READONLY, CTLTYPE_STRING, "mntpt",
1164 1.1 maxv NULL, NULL, 0, mp->mnt_stat.f_mntonname,
1165 1.1 maxv 0, CTL_CREATE, CTL_EOL);
1166 1.1 maxv sysctl_createv(NULL, 0, &vte->vte_node, NULL,
1167 1.1 maxv CTLFLAG_READONLY, CTLTYPE_STRING, "fstype",
1168 1.1 maxv NULL, NULL, 0, mp->mnt_stat.f_fstypename,
1169 1.1 maxv 0, CTL_CREATE, CTL_EOL);
1170 1.1 maxv sysctl_createv(NULL, 0, &vte->vte_node, NULL,
1171 1.1 maxv CTLFLAG_READONLY, CTLTYPE_QUAD, "nentries",
1172 1.1 maxv NULL, NULL, 0, &vte->vte_count, 0, CTL_CREATE, CTL_EOL);
1173 1.1 maxv
1174 1.1 maxv return (vte);
1175 1.1 maxv }
1176 1.1 maxv
1177 1.1 maxv /*
1178 1.1 maxv * Add a file to be monitored by Veriexec.
1179 1.1 maxv *
1180 1.1 maxv * Expected elements in dict: file, fp, fp-type, entry-type.
1181 1.1 maxv */
1182 1.1 maxv int
1183 1.1 maxv veriexec_file_add(struct lwp *l, prop_dictionary_t dict)
1184 1.1 maxv {
1185 1.1 maxv struct veriexec_table_entry *vte;
1186 1.1 maxv struct veriexec_file_entry *vfe = NULL, *hh;
1187 1.1 maxv struct vnode *vp;
1188 1.1 maxv const char *file, *fp_type;
1189 1.1 maxv int error;
1190 1.1 maxv
1191 1.1 maxv if (!prop_dictionary_get_cstring_nocopy(dict, "file", &file))
1192 1.1 maxv return (EINVAL);
1193 1.1 maxv
1194 1.1 maxv error = namei_simple_kernel(file, NSM_FOLLOW_NOEMULROOT, &vp);
1195 1.1 maxv if (error)
1196 1.1 maxv return (error);
1197 1.1 maxv
1198 1.1 maxv /* Add only regular files. */
1199 1.1 maxv if (vp->v_type != VREG) {
1200 1.1 maxv log(LOG_ERR, "Veriexec: Not adding `%s': Not a regular file.\n",
1201 1.1 maxv file);
1202 1.1 maxv error = EBADF;
1203 1.1 maxv goto out;
1204 1.1 maxv }
1205 1.1 maxv
1206 1.1 maxv vfe = kmem_zalloc(sizeof(*vfe), KM_SLEEP);
1207 1.1 maxv rw_init(&vfe->lock);
1208 1.1 maxv
1209 1.1 maxv /* Lookup fingerprint hashing algorithm. */
1210 1.1 maxv fp_type = prop_string_cstring_nocopy(prop_dictionary_get(dict,
1211 1.1 maxv "fp-type"));
1212 1.1 maxv if ((vfe->ops = veriexec_fpops_lookup(fp_type)) == NULL) {
1213 1.1 maxv log(LOG_ERR, "Veriexec: Invalid or unknown fingerprint type "
1214 1.1 maxv "`%s' for file `%s'.\n", fp_type, file);
1215 1.1 maxv error = EOPNOTSUPP;
1216 1.1 maxv goto out;
1217 1.1 maxv }
1218 1.1 maxv
1219 1.1 maxv if (prop_data_size(prop_dictionary_get(dict, "fp")) !=
1220 1.1 maxv vfe->ops->hash_len) {
1221 1.1 maxv log(LOG_ERR, "Veriexec: Bad fingerprint length for `%s'.\n",
1222 1.1 maxv file);
1223 1.1 maxv error = EINVAL;
1224 1.1 maxv goto out;
1225 1.1 maxv }
1226 1.1 maxv
1227 1.1 maxv vfe->fp = kmem_alloc(vfe->ops->hash_len, KM_SLEEP);
1228 1.1 maxv memcpy(vfe->fp, prop_data_data_nocopy(prop_dictionary_get(dict, "fp")),
1229 1.1 maxv vfe->ops->hash_len);
1230 1.1 maxv
1231 1.1 maxv rw_enter(&veriexec_op_lock, RW_WRITER);
1232 1.1 maxv
1233 1.1 maxv /*
1234 1.1 maxv * See if we already have an entry for this file. If we do, then
1235 1.1 maxv * let the user know and silently pretend to succeed.
1236 1.1 maxv */
1237 1.1 maxv hh = veriexec_get(vp);
1238 1.1 maxv if (hh != NULL) {
1239 1.1 maxv bool fp_mismatch;
1240 1.1 maxv
1241 1.1 maxv if (strcmp(vfe->ops->type, fp_type) ||
1242 1.1 maxv memcmp(hh->fp, vfe->fp, hh->ops->hash_len))
1243 1.1 maxv fp_mismatch = true;
1244 1.1 maxv else
1245 1.1 maxv fp_mismatch = false;
1246 1.1 maxv
1247 1.2 maxv if ((veriexec_verbose >= 1) || fp_mismatch) {
1248 1.1 maxv log(LOG_NOTICE, "Veriexec: Duplicate entry for `%s' "
1249 1.1 maxv "ignored. (%s fingerprint)\n", file,
1250 1.1 maxv fp_mismatch ? "different" : "same");
1251 1.2 maxv }
1252 1.1 maxv
1253 1.1 maxv veriexec_file_free(vfe);
1254 1.1 maxv
1255 1.1 maxv /* XXX Should this be EEXIST if fp_mismatch is true? */
1256 1.1 maxv error = 0;
1257 1.1 maxv goto unlock_out;
1258 1.1 maxv }
1259 1.1 maxv
1260 1.1 maxv /* Continue entry initialization. */
1261 1.1 maxv if (prop_dictionary_get_uint8(dict, "entry-type", &vfe->type) == FALSE)
1262 1.1 maxv vfe->type = 0;
1263 1.1 maxv else {
1264 1.1 maxv uint8_t extra_flags;
1265 1.1 maxv
1266 1.1 maxv extra_flags = vfe->type & ~(VERIEXEC_DIRECT |
1267 1.1 maxv VERIEXEC_INDIRECT | VERIEXEC_FILE | VERIEXEC_UNTRUSTED);
1268 1.1 maxv if (extra_flags) {
1269 1.1 maxv log(LOG_NOTICE, "Veriexec: Contaminated flags `0x%x' "
1270 1.1 maxv "for `%s', skipping.\n", extra_flags, file);
1271 1.1 maxv error = EINVAL;
1272 1.1 maxv goto unlock_out;
1273 1.1 maxv }
1274 1.1 maxv }
1275 1.1 maxv if (!(vfe->type & (VERIEXEC_DIRECT | VERIEXEC_INDIRECT |
1276 1.1 maxv VERIEXEC_FILE)))
1277 1.1 maxv vfe->type |= VERIEXEC_DIRECT;
1278 1.1 maxv
1279 1.1 maxv vfe->status = FINGERPRINT_NOTEVAL;
1280 1.1 maxv if (prop_bool_true(prop_dictionary_get(dict, "keep-filename"))) {
1281 1.1 maxv vfe->filename_len = strlen(file) + 1;
1282 1.1 maxv vfe->filename = kmem_alloc(vfe->filename_len, KM_SLEEP);
1283 1.1 maxv strlcpy(vfe->filename, file, vfe->filename_len);
1284 1.1 maxv } else
1285 1.1 maxv vfe->filename = NULL;
1286 1.1 maxv
1287 1.1 maxv vfe->page_fp = NULL;
1288 1.1 maxv vfe->page_fp_status = PAGE_FP_NONE;
1289 1.1 maxv vfe->npages = 0;
1290 1.1 maxv vfe->last_page_size = 0;
1291 1.1 maxv
1292 1.1 maxv if (prop_bool_true(prop_dictionary_get(dict, "eval-on-load")) ||
1293 1.1 maxv (vfe->type & VERIEXEC_UNTRUSTED)) {
1294 1.1 maxv u_char *digest;
1295 1.1 maxv
1296 1.1 maxv digest = kmem_zalloc(vfe->ops->hash_len, KM_SLEEP);
1297 1.1 maxv
1298 1.1 maxv error = veriexec_fp_calc(l, vp, VERIEXEC_UNLOCKED,
1299 1.1 maxv vfe, digest);
1300 1.1 maxv if (error) {
1301 1.1 maxv kmem_free(digest, vfe->ops->hash_len);
1302 1.1 maxv goto unlock_out;
1303 1.1 maxv }
1304 1.1 maxv
1305 1.1 maxv if (veriexec_fp_cmp(vfe->ops, vfe->fp, digest) == 0)
1306 1.1 maxv vfe->status = FINGERPRINT_VALID;
1307 1.1 maxv else
1308 1.1 maxv vfe->status = FINGERPRINT_NOMATCH;
1309 1.1 maxv
1310 1.1 maxv kmem_free(digest, vfe->ops->hash_len);
1311 1.1 maxv }
1312 1.1 maxv
1313 1.1 maxv vte = veriexec_table_lookup(vp->v_mount);
1314 1.1 maxv if (vte == NULL)
1315 1.1 maxv vte = veriexec_table_add(l, vp->v_mount);
1316 1.1 maxv
1317 1.1 maxv /* XXX if we bail below this, we might want to gc newly created vtes. */
1318 1.1 maxv
1319 1.1 maxv error = fileassoc_add(vp, veriexec_hook, vfe);
1320 1.1 maxv if (error)
1321 1.1 maxv goto unlock_out;
1322 1.1 maxv
1323 1.1 maxv vte->vte_count++;
1324 1.1 maxv
1325 1.1 maxv veriexec_file_report(NULL, "New entry.", file, NULL, REPORT_DEBUG);
1326 1.1 maxv veriexec_bypass = 0;
1327 1.1 maxv
1328 1.1 maxv unlock_out:
1329 1.1 maxv rw_exit(&veriexec_op_lock);
1330 1.1 maxv
1331 1.1 maxv out:
1332 1.1 maxv vrele(vp);
1333 1.1 maxv if (error)
1334 1.1 maxv veriexec_file_free(vfe);
1335 1.1 maxv
1336 1.1 maxv return (error);
1337 1.1 maxv }
1338 1.1 maxv
1339 1.1 maxv int
1340 1.1 maxv veriexec_table_delete(struct lwp *l, struct mount *mp) {
1341 1.1 maxv struct veriexec_table_entry *vte;
1342 1.1 maxv
1343 1.1 maxv vte = veriexec_table_lookup(mp);
1344 1.1 maxv if (vte == NULL)
1345 1.1 maxv return (ENOENT);
1346 1.1 maxv
1347 1.1 maxv veriexec_mountspecific_dtor(vte);
1348 1.1 maxv mount_setspecific(mp, veriexec_mountspecific_key, NULL);
1349 1.1 maxv
1350 1.1 maxv return (fileassoc_table_clear(mp, veriexec_hook));
1351 1.1 maxv }
1352 1.1 maxv
1353 1.1 maxv int
1354 1.1 maxv veriexec_file_delete(struct lwp *l, struct vnode *vp) {
1355 1.1 maxv struct veriexec_table_entry *vte;
1356 1.1 maxv int error;
1357 1.1 maxv
1358 1.1 maxv vte = veriexec_table_lookup(vp->v_mount);
1359 1.1 maxv if (vte == NULL)
1360 1.1 maxv return (ENOENT);
1361 1.1 maxv
1362 1.1 maxv rw_enter(&veriexec_op_lock, RW_WRITER);
1363 1.1 maxv error = fileassoc_clear(vp, veriexec_hook);
1364 1.1 maxv rw_exit(&veriexec_op_lock);
1365 1.1 maxv if (!error)
1366 1.1 maxv vte->vte_count--;
1367 1.1 maxv
1368 1.1 maxv return (error);
1369 1.1 maxv }
1370 1.1 maxv
1371 1.1 maxv /*
1372 1.1 maxv * Convert Veriexec entry data to a dictionary readable by userland tools.
1373 1.1 maxv */
1374 1.1 maxv static void
1375 1.1 maxv veriexec_file_convert(struct veriexec_file_entry *vfe, prop_dictionary_t rdict)
1376 1.1 maxv {
1377 1.1 maxv if (vfe->filename)
1378 1.1 maxv prop_dictionary_set(rdict, "file",
1379 1.1 maxv prop_string_create_cstring(vfe->filename));
1380 1.1 maxv prop_dictionary_set_uint8(rdict, "entry-type", vfe->type);
1381 1.1 maxv prop_dictionary_set_uint8(rdict, "status", vfe->status);
1382 1.1 maxv prop_dictionary_set(rdict, "fp-type",
1383 1.1 maxv prop_string_create_cstring(vfe->ops->type));
1384 1.1 maxv prop_dictionary_set(rdict, "fp",
1385 1.1 maxv prop_data_create_data(vfe->fp, vfe->ops->hash_len));
1386 1.1 maxv }
1387 1.1 maxv
1388 1.1 maxv int
1389 1.1 maxv veriexec_convert(struct vnode *vp, prop_dictionary_t rdict)
1390 1.1 maxv {
1391 1.1 maxv struct veriexec_file_entry *vfe;
1392 1.1 maxv
1393 1.1 maxv rw_enter(&veriexec_op_lock, RW_READER);
1394 1.1 maxv
1395 1.1 maxv vfe = veriexec_get(vp);
1396 1.1 maxv if (vfe == NULL) {
1397 1.1 maxv rw_exit(&veriexec_op_lock);
1398 1.1 maxv return (ENOENT);
1399 1.1 maxv }
1400 1.1 maxv
1401 1.1 maxv rw_enter(&vfe->lock, RW_READER);
1402 1.1 maxv veriexec_file_convert(vfe, rdict);
1403 1.2 maxv rw_exit(&vfe->lock);
1404 1.1 maxv
1405 1.1 maxv rw_exit(&veriexec_op_lock);
1406 1.1 maxv return (0);
1407 1.1 maxv }
1408 1.1 maxv
1409 1.1 maxv int
1410 1.1 maxv veriexec_unmountchk(struct mount *mp)
1411 1.1 maxv {
1412 1.1 maxv int error;
1413 1.1 maxv
1414 1.1 maxv if ((veriexec_bypass && (veriexec_strict == VERIEXEC_LEARNING))
1415 1.1 maxv || doing_shutdown)
1416 1.1 maxv return (0);
1417 1.1 maxv
1418 1.1 maxv rw_enter(&veriexec_op_lock, RW_READER);
1419 1.1 maxv
1420 1.1 maxv switch (veriexec_strict) {
1421 1.1 maxv case VERIEXEC_LEARNING:
1422 1.1 maxv error = 0;
1423 1.1 maxv break;
1424 1.1 maxv
1425 1.1 maxv case VERIEXEC_IDS:
1426 1.1 maxv if (veriexec_table_lookup(mp) != NULL) {
1427 1.1 maxv log(LOG_INFO, "Veriexec: IDS mode, allowing unmount "
1428 1.1 maxv "of \"%s\".\n", mp->mnt_stat.f_mntonname);
1429 1.1 maxv }
1430 1.1 maxv
1431 1.1 maxv error = 0;
1432 1.1 maxv break;
1433 1.1 maxv
1434 1.1 maxv case VERIEXEC_IPS: {
1435 1.1 maxv struct veriexec_table_entry *vte;
1436 1.1 maxv
1437 1.1 maxv vte = veriexec_table_lookup(mp);
1438 1.1 maxv if ((vte != NULL) && (vte->vte_count > 0)) {
1439 1.1 maxv log(LOG_ALERT, "Veriexec: IPS mode, preventing"
1440 1.1 maxv " unmount of \"%s\" with monitored files.\n",
1441 1.1 maxv mp->mnt_stat.f_mntonname);
1442 1.1 maxv
1443 1.1 maxv error = EPERM;
1444 1.1 maxv } else
1445 1.1 maxv error = 0;
1446 1.1 maxv break;
1447 1.1 maxv }
1448 1.1 maxv
1449 1.1 maxv case VERIEXEC_LOCKDOWN:
1450 1.1 maxv default:
1451 1.1 maxv log(LOG_ALERT, "Veriexec: Lockdown mode, preventing unmount "
1452 1.1 maxv "of \"%s\".\n", mp->mnt_stat.f_mntonname);
1453 1.1 maxv error = EPERM;
1454 1.1 maxv break;
1455 1.1 maxv }
1456 1.1 maxv
1457 1.1 maxv rw_exit(&veriexec_op_lock);
1458 1.1 maxv return (error);
1459 1.1 maxv }
1460 1.1 maxv
1461 1.1 maxv int
1462 1.1 maxv veriexec_openchk(struct lwp *l, struct vnode *vp, const char *path, int fmode)
1463 1.1 maxv {
1464 1.1 maxv struct veriexec_file_entry *vfe = NULL;
1465 1.1 maxv int error = 0;
1466 1.1 maxv
1467 1.1 maxv if (veriexec_bypass && (veriexec_strict == VERIEXEC_LEARNING))
1468 1.1 maxv return 0;
1469 1.1 maxv
1470 1.1 maxv if (vp == NULL) {
1471 1.1 maxv /* If no creation requested, let this fail normally. */
1472 1.1 maxv if (!(fmode & O_CREAT))
1473 1.1 maxv goto out;
1474 1.1 maxv
1475 1.1 maxv /* Lockdown mode: Prevent creation of new files. */
1476 1.1 maxv if (veriexec_strict >= VERIEXEC_LOCKDOWN) {
1477 1.1 maxv log(LOG_ALERT, "Veriexec: Preventing new file "
1478 1.1 maxv "creation in `%s'.\n", path);
1479 1.1 maxv error = EPERM;
1480 1.1 maxv }
1481 1.1 maxv
1482 1.1 maxv goto out;
1483 1.1 maxv }
1484 1.1 maxv
1485 1.1 maxv rw_enter(&veriexec_op_lock, RW_READER);
1486 1.1 maxv error = veriexec_file_verify(l, vp, path, VERIEXEC_FILE,
1487 1.1 maxv VERIEXEC_LOCKED, &vfe);
1488 1.1 maxv
1489 1.1 maxv if (error) {
1490 1.1 maxv rw_exit(&veriexec_op_lock);
1491 1.1 maxv goto out;
1492 1.1 maxv }
1493 1.1 maxv
1494 1.1 maxv if ((vfe != NULL) && ((fmode & FWRITE) || (fmode & O_TRUNC))) {
1495 1.1 maxv veriexec_file_report(vfe, "Write access request.", path, l,
1496 1.1 maxv REPORT_ALWAYS | REPORT_ALARM);
1497 1.1 maxv
1498 1.1 maxv /* IPS mode: Deny write access to monitored files. */
1499 1.1 maxv if (veriexec_strict >= VERIEXEC_IPS)
1500 1.1 maxv error = EPERM;
1501 1.1 maxv else
1502 1.1 maxv veriexec_file_purge(vfe, VERIEXEC_LOCKED);
1503 1.1 maxv }
1504 1.1 maxv
1505 1.1 maxv if (vfe != NULL)
1506 1.1 maxv rw_exit(&vfe->lock);
1507 1.1 maxv
1508 1.1 maxv rw_exit(&veriexec_op_lock);
1509 1.1 maxv out:
1510 1.1 maxv return (error);
1511 1.1 maxv }
1512 1.1 maxv
1513 1.1 maxv static void
1514 1.1 maxv veriexec_file_dump(struct veriexec_file_entry *vfe, prop_array_t entries)
1515 1.1 maxv {
1516 1.1 maxv prop_dictionary_t entry;
1517 1.1 maxv
1518 1.1 maxv /* If we don't have a filename, this is meaningless. */
1519 1.1 maxv if (vfe->filename == NULL)
1520 1.1 maxv return;
1521 1.1 maxv
1522 1.1 maxv entry = prop_dictionary_create();
1523 1.1 maxv
1524 1.1 maxv veriexec_file_convert(vfe, entry);
1525 1.1 maxv
1526 1.1 maxv prop_array_add(entries, entry);
1527 1.1 maxv }
1528 1.1 maxv
1529 1.1 maxv int
1530 1.1 maxv veriexec_dump(struct lwp *l, prop_array_t rarray)
1531 1.1 maxv {
1532 1.1 maxv struct mount *mp, *nmp;
1533 1.1 maxv
1534 1.1 maxv mutex_enter(&mountlist_lock);
1535 1.1 maxv for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1536 1.1 maxv /* If it fails, the file-system is [being] unmounted. */
1537 1.1 maxv if (vfs_busy(mp, &nmp) != 0)
1538 1.1 maxv continue;
1539 1.1 maxv
1540 1.1 maxv fileassoc_table_run(mp, veriexec_hook,
1541 1.1 maxv (fileassoc_cb_t)veriexec_file_dump, rarray);
1542 1.1 maxv
1543 1.1 maxv vfs_unbusy(mp, false, &nmp);
1544 1.1 maxv }
1545 1.1 maxv mutex_exit(&mountlist_lock);
1546 1.1 maxv
1547 1.1 maxv return (0);
1548 1.1 maxv }
1549 1.1 maxv
1550 1.1 maxv int
1551 1.1 maxv veriexec_flush(struct lwp *l)
1552 1.1 maxv {
1553 1.1 maxv struct mount *mp, *nmp;
1554 1.1 maxv int error = 0;
1555 1.1 maxv
1556 1.1 maxv mutex_enter(&mountlist_lock);
1557 1.1 maxv for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1558 1.1 maxv int lerror;
1559 1.1 maxv
1560 1.1 maxv /* If it fails, the file-system is [being] unmounted. */
1561 1.1 maxv if (vfs_busy(mp, &nmp) != 0)
1562 1.1 maxv continue;
1563 1.1 maxv
1564 1.1 maxv lerror = veriexec_table_delete(l, mp);
1565 1.1 maxv if (lerror && lerror != ENOENT)
1566 1.1 maxv error = lerror;
1567 1.1 maxv
1568 1.1 maxv vfs_unbusy(mp, false, &nmp);
1569 1.1 maxv }
1570 1.1 maxv mutex_exit(&mountlist_lock);
1571 1.1 maxv
1572 1.1 maxv return (error);
1573 1.1 maxv }
1574