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