veriexecctl.c revision 1.25 1 1.25 elad /* $NetBSD: veriexecctl.c,v 1.25 2006/11/28 22:22:03 elad Exp $ */
2 1.1 blymn
3 1.1 blymn /*-
4 1.24 elad * Copyright 2005 Elad Efrat <elad (at) NetBSD.org>
5 1.6 blymn * Copyright 2005 Brett Lymn <blymn (at) netbsd.org>
6 1.6 blymn *
7 1.1 blymn * All rights reserved.
8 1.1 blymn *
9 1.1 blymn * This code has been donated to The NetBSD Foundation by the Author.
10 1.1 blymn *
11 1.1 blymn * Redistribution and use in source and binary forms, with or without
12 1.1 blymn * modification, are permitted provided that the following conditions
13 1.1 blymn * are met:
14 1.1 blymn * 1. Redistributions of source code must retain the above copyright
15 1.1 blymn * notice, this list of conditions and the following disclaimer.
16 1.1 blymn * 2. The name of the author may not be used to endorse or promote products
17 1.1 blymn * derived from this software withough specific prior written permission
18 1.1 blymn *
19 1.1 blymn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1 blymn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 blymn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 blymn * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 blymn * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.1 blymn * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.1 blymn * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.1 blymn * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.1 blymn * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 1.1 blymn * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1 blymn *
30 1.1 blymn *
31 1.1 blymn */
32 1.1 blymn
33 1.6 blymn #include <sys/ioctl.h>
34 1.6 blymn #include <sys/param.h>
35 1.6 blymn #include <sys/queue.h>
36 1.6 blymn #include <sys/verified_exec.h>
37 1.23 elad #include <sys/statvfs.h>
38 1.1 blymn
39 1.1 blymn #include <stdio.h>
40 1.2 thorpej #include <stdlib.h>
41 1.6 blymn #include <string.h>
42 1.1 blymn #include <fcntl.h>
43 1.6 blymn #include <unistd.h>
44 1.5 blymn #include <err.h>
45 1.6 blymn #include <errno.h>
46 1.6 blymn
47 1.25 elad #include <prop/proplib.h>
48 1.25 elad
49 1.6 blymn #include "veriexecctl.h"
50 1.6 blymn
51 1.6 blymn #define VERIEXEC_DEVICE "/dev/veriexec"
52 1.6 blymn
53 1.25 elad extern prop_dictionary_t load_params;
54 1.25 elad extern char *filename;
55 1.16 elad extern int yynerrs;
56 1.10 elad int gfd, verbose = 0, phase;
57 1.8 christos size_t line;
58 1.6 blymn
59 1.6 blymn /*
60 1.6 blymn * Prototypes
61 1.6 blymn */
62 1.8 christos static FILE *openlock(const char *);
63 1.8 christos static void phase1_preload(void);
64 1.8 christos static int fingerprint_load(char*);
65 1.8 christos static void usage(void) __attribute__((__noreturn__));
66 1.7 he
67 1.8 christos static FILE *
68 1.6 blymn openlock(const char *path)
69 1.6 blymn {
70 1.7 he int lfd;
71 1.6 blymn
72 1.8 christos if ((lfd = open(path, O_RDONLY|O_EXLOCK, 0)) == -1)
73 1.8 christos return NULL;
74 1.6 blymn
75 1.8 christos return fdopen(lfd, "r");
76 1.6 blymn }
77 1.6 blymn
78 1.10 elad struct veriexec_up *
79 1.21 elad dev_lookup(char *vfs)
80 1.6 blymn {
81 1.10 elad struct veriexec_up *p;
82 1.6 blymn
83 1.8 christos CIRCLEQ_FOREACH(p, ¶ms_list, vu_list)
84 1.25 elad if (strcmp(dict_gets(p->vu_preload, "mount"), vfs) == 0)
85 1.6 blymn return (p);
86 1.6 blymn
87 1.8 christos return NULL;
88 1.6 blymn }
89 1.6 blymn
90 1.10 elad struct veriexec_up *
91 1.23 elad dev_add(char *vfs)
92 1.6 blymn {
93 1.10 elad struct veriexec_up *up;
94 1.6 blymn
95 1.8 christos if ((up = calloc((size_t)1, sizeof(*up))) == NULL)
96 1.6 blymn err(1, "No memory");
97 1.6 blymn
98 1.25 elad up->vu_preload = prop_dictionary_create();
99 1.25 elad
100 1.25 elad dict_sets(up->vu_preload, "mount", vfs);
101 1.25 elad prop_dictionary_set_uint64(up->vu_preload, "count", 1);
102 1.6 blymn
103 1.6 blymn CIRCLEQ_INSERT_TAIL(¶ms_list, up, vu_list);
104 1.6 blymn
105 1.8 christos return up;
106 1.6 blymn }
107 1.6 blymn
108 1.6 blymn /* Load all devices, get rid of the list. */
109 1.8 christos static void
110 1.6 blymn phase1_preload(void)
111 1.6 blymn {
112 1.6 blymn if (verbose)
113 1.6 blymn printf("Phase 1: Calculating hash table sizes:\n");
114 1.6 blymn
115 1.6 blymn while (!CIRCLEQ_EMPTY(¶ms_list)) {
116 1.10 elad struct veriexec_up *vup;
117 1.23 elad struct statvfs sv;
118 1.6 blymn
119 1.6 blymn vup = CIRCLEQ_FIRST(¶ms_list);
120 1.6 blymn
121 1.25 elad if (statvfs(dict_gets(vup->vu_preload, "mount"), &sv) != 0)
122 1.25 elad err(1, "Can't statvfs() `%s'",
123 1.25 elad dict_gets(vup->vu_preload, "mount"));
124 1.23 elad
125 1.25 elad if (prop_dictionary_send_ioctl(vup->vu_preload, gfd,
126 1.25 elad VERIEXEC_TABLESIZE) == -1) {
127 1.14 elad if (errno != EEXIST)
128 1.14 elad err(1, "Error in phase 1: Can't "
129 1.23 elad "set hash table size for mount `%s'",
130 1.23 elad sv.f_mntonname);
131 1.14 elad }
132 1.5 blymn
133 1.6 blymn if (verbose) {
134 1.25 elad uint64_t count;
135 1.25 elad
136 1.25 elad prop_dictionary_get_uint64(vup->vu_preload, "count",
137 1.25 elad &count);
138 1.21 elad printf(" => Hash table sizing successful for mount "
139 1.25 elad "`%s'. (%zu entries)\n", sv.f_mntonname, count);
140 1.6 blymn }
141 1.1 blymn
142 1.6 blymn CIRCLEQ_REMOVE(¶ms_list, vup, vu_list);
143 1.25 elad
144 1.25 elad prop_object_release(vup->vu_preload);
145 1.25 elad
146 1.6 blymn free(vup);
147 1.6 blymn }
148 1.6 blymn }
149 1.1 blymn
150 1.6 blymn /*
151 1.6 blymn * Load the fingerprint. Assumes that the fingerprint pseudo-device is
152 1.7 he * opened and the file handle is in gfd.
153 1.6 blymn */
154 1.6 blymn void
155 1.6 blymn phase2_load(void)
156 1.6 blymn {
157 1.25 elad uint8_t t;
158 1.25 elad
159 1.17 elad /*
160 1.17 elad * If there's no access type specified, use the default.
161 1.17 elad */
162 1.25 elad prop_dictionary_get_uint8(load_params, "entry-type", &t);
163 1.25 elad if (!(t & (VERIEXEC_DIRECT|VERIEXEC_INDIRECT|VERIEXEC_FILE))) {
164 1.25 elad t |= VERIEXEC_DIRECT;
165 1.25 elad prop_dictionary_set_uint8(load_params, "entry-type", t);
166 1.25 elad }
167 1.25 elad
168 1.25 elad if (prop_dictionary_send_ioctl(load_params, gfd, VERIEXEC_LOAD) == -1)
169 1.25 elad warn("Cannot load params from `%s'",
170 1.25 elad dict_gets(load_params, "file"));
171 1.25 elad
172 1.25 elad prop_object_release(load_params);
173 1.25 elad
174 1.25 elad load_params = NULL;
175 1.6 blymn }
176 1.6 blymn
177 1.6 blymn /*
178 1.6 blymn * Fingerprint load handling.
179 1.6 blymn */
180 1.8 christos static int
181 1.7 he fingerprint_load(char *ifile)
182 1.1 blymn {
183 1.6 blymn CIRCLEQ_INIT(¶ms_list);
184 1.6 blymn
185 1.8 christos if ((yyin = openlock(ifile)) == NULL)
186 1.8 christos err(1, "Cannot open `%s'", ifile);
187 1.6 blymn
188 1.6 blymn /*
189 1.6 blymn * Phase 1: Scan all config files, creating the list of devices
190 1.6 blymn * we have fingerprinted files on, and the amount of
191 1.6 blymn * files per device. Lock all files to maintain sync.
192 1.6 blymn */
193 1.6 blymn phase = 1;
194 1.6 blymn
195 1.6 blymn if (verbose) {
196 1.8 christos (void)printf("Phase 1: Building hash table information:\n");
197 1.8 christos (void)printf("=> Parsing \"%s\"\n", ifile);
198 1.6 blymn }
199 1.6 blymn
200 1.16 elad line = 1;
201 1.6 blymn yyparse();
202 1.16 elad if (yynerrs)
203 1.16 elad return -1;
204 1.6 blymn
205 1.8 christos phase1_preload();
206 1.6 blymn
207 1.6 blymn /*
208 1.6 blymn * Phase 2: After we have a circular queue containing all the
209 1.6 blymn * devices we care about and the sizes for the hash
210 1.6 blymn * tables, do a rescan, this time actually loading the
211 1.6 blymn * file data.
212 1.6 blymn */
213 1.6 blymn rewind(yyin);
214 1.6 blymn phase = 2;
215 1.6 blymn if (verbose) {
216 1.8 christos (void)printf("Phase 2: Loading per-file fingerprints.\n");
217 1.8 christos (void)printf("=> Parsing \"%s\"\n", ifile);
218 1.6 blymn }
219 1.6 blymn
220 1.16 elad line = 1;
221 1.6 blymn yyparse();
222 1.6 blymn
223 1.8 christos (void)fclose(yyin);
224 1.6 blymn
225 1.8 christos return 0;
226 1.8 christos }
227 1.8 christos
228 1.8 christos static void
229 1.8 christos usage(void)
230 1.8 christos {
231 1.13 elad (void)fprintf(stderr, "Usage: %s [-v] [load <signature_file>]\n",
232 1.9 elad getprogname());
233 1.8 christos exit(1);
234 1.6 blymn }
235 1.6 blymn
236 1.19 elad static void
237 1.19 elad print_flags(unsigned char flags)
238 1.19 elad {
239 1.19 elad char buf[64];
240 1.19 elad
241 1.19 elad if (!flags) {
242 1.19 elad printf("<none>\n");
243 1.19 elad return;
244 1.19 elad }
245 1.19 elad
246 1.19 elad memset(buf, 0, sizeof(buf));
247 1.19 elad
248 1.19 elad while (flags) {
249 1.19 elad if (*buf)
250 1.19 elad strlcat(buf, ", ", sizeof(buf));
251 1.19 elad
252 1.19 elad if (flags & VERIEXEC_DIRECT) {
253 1.19 elad strlcat(buf, "direct", sizeof(buf));
254 1.19 elad flags &= ~VERIEXEC_DIRECT;
255 1.19 elad continue;
256 1.19 elad }
257 1.19 elad if (flags & VERIEXEC_INDIRECT) {
258 1.19 elad strlcat(buf, "indirect", sizeof(buf));
259 1.19 elad flags &= ~VERIEXEC_INDIRECT;
260 1.19 elad continue;
261 1.19 elad }
262 1.19 elad if (flags & VERIEXEC_FILE) {
263 1.19 elad strlcat(buf, "file", sizeof(buf));
264 1.19 elad flags &= ~VERIEXEC_FILE;
265 1.19 elad continue;
266 1.19 elad }
267 1.19 elad if (flags & VERIEXEC_UNTRUSTED) {
268 1.19 elad strlcat(buf, "untrusted", sizeof(buf));
269 1.19 elad flags &= ~VERIEXEC_UNTRUSTED;
270 1.19 elad continue;
271 1.19 elad }
272 1.19 elad }
273 1.19 elad
274 1.19 elad printf("%s\n", buf);
275 1.19 elad }
276 1.19 elad
277 1.19 elad static void
278 1.25 elad print_query(prop_dictionary_t qp, char *file)
279 1.19 elad {
280 1.23 elad struct statvfs sv;
281 1.25 elad const char *v;
282 1.19 elad int i;
283 1.25 elad uint8_t u8;
284 1.19 elad
285 1.23 elad if (statvfs(file, &sv) != 0)
286 1.23 elad err(1, "Can't statvfs() `%s'\n", file);
287 1.23 elad
288 1.19 elad printf("Filename: %s\n", file);
289 1.23 elad printf("Mount: %s\n", sv.f_mntonname);
290 1.19 elad printf("Entry flags: ");
291 1.25 elad prop_dictionary_get_uint8(qp, "entry-type", &u8);
292 1.25 elad print_flags(u8);
293 1.25 elad prop_dictionary_get_uint8(qp, "status", &u8);
294 1.25 elad printf("Entry status: %s\n", STATUS_STRING(u8));
295 1.25 elad printf("Fingerprint algorithm: %s\n", dict_gets(qp, "fp-type"));
296 1.19 elad printf("Fingerprint: ");
297 1.25 elad v = dict_getd(qp, "fp");
298 1.25 elad for (i = 0; i < prop_data_size(prop_dictionary_get(qp, "fp")); i++)
299 1.25 elad printf("%02x", v[i] & 0xff);
300 1.19 elad printf("\n");
301 1.19 elad }
302 1.19 elad
303 1.6 blymn int
304 1.6 blymn main(int argc, char **argv)
305 1.6 blymn {
306 1.6 blymn int c;
307 1.6 blymn
308 1.8 christos setprogname(argv[0]);
309 1.6 blymn
310 1.8 christos while ((c = getopt(argc, argv, "v")) != -1)
311 1.6 blymn switch (c) {
312 1.6 blymn case 'v':
313 1.6 blymn verbose = 1;
314 1.6 blymn break;
315 1.6 blymn
316 1.6 blymn default:
317 1.8 christos usage();
318 1.6 blymn }
319 1.1 blymn
320 1.6 blymn argc -= optind;
321 1.6 blymn argv += optind;
322 1.6 blymn
323 1.8 christos if ((gfd = open(VERIEXEC_DEVICE, O_RDWR, 0)) == -1)
324 1.8 christos err(1, "Cannot open `%s'", VERIEXEC_DEVICE);
325 1.1 blymn
326 1.9 elad /*
327 1.9 elad * Handle the different commands we can do.
328 1.9 elad */
329 1.8 christos if (argc == 2 && strcasecmp(argv[0], "load") == 0) {
330 1.25 elad load_params = NULL;
331 1.6 blymn filename = argv[1];
332 1.6 blymn fingerprint_load(argv[1]);
333 1.18 elad } else if (argc == 2 && strcasecmp(argv[0], "delete") == 0) {
334 1.25 elad prop_dictionary_t dp;
335 1.18 elad struct stat sb;
336 1.18 elad
337 1.18 elad if (stat(argv[1], &sb) == -1)
338 1.18 elad err(1, "Can't stat `%s'", argv[1]);
339 1.18 elad
340 1.25 elad dp = prop_dictionary_create();
341 1.25 elad dict_sets(dp, "file", argv[1]);
342 1.21 elad
343 1.18 elad /*
344 1.18 elad * If it's a regular file, remove it. If it's a directory,
345 1.18 elad * remove the entire table. If it's neither, abort.
346 1.18 elad */
347 1.21 elad if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode))
348 1.18 elad errx(1, "`%s' is not a regular file or directory.", argv[1]);
349 1.18 elad
350 1.25 elad if (prop_dictionary_send_ioctl(dp, gfd, VERIEXEC_DELETE) == -1)
351 1.18 elad err(1, "Error deleting `%s'", argv[1]);
352 1.25 elad
353 1.25 elad prop_object_release(dp);
354 1.19 elad } else if (argc == 2 && strcasecmp(argv[0], "query") == 0) {
355 1.25 elad prop_dictionary_t qp, rqp;
356 1.19 elad struct stat sb;
357 1.19 elad
358 1.19 elad if (stat(argv[1], &sb) == -1)
359 1.19 elad err(1, "Can't stat `%s'", argv[1]);
360 1.19 elad if (!S_ISREG(sb.st_mode))
361 1.19 elad errx(1, "`%s' is not a regular file.", argv[1]);
362 1.19 elad
363 1.25 elad qp = prop_dictionary_create();
364 1.21 elad
365 1.25 elad dict_sets(qp, "file", argv[1]);
366 1.19 elad
367 1.25 elad if (prop_dictionary_sendrecv_ioctl(qp, gfd, VERIEXEC_QUERY,
368 1.25 elad &rqp) == -1)
369 1.19 elad err(1, "Error querying `%s'", argv[1]);
370 1.19 elad
371 1.25 elad if (rqp != NULL) {
372 1.25 elad print_query(rqp, argv[1]);
373 1.25 elad prop_object_release(rqp);
374 1.25 elad }
375 1.25 elad
376 1.25 elad prop_object_release(qp);
377 1.8 christos } else
378 1.8 christos usage();
379 1.1 blymn
380 1.8 christos (void)close(gfd);
381 1.8 christos return 0;
382 1.1 blymn }
383