veriexecctl.c revision 1.19 1 /* $NetBSD: veriexecctl.c,v 1.19 2005/12/12 21:47:58 elad Exp $ */
2
3 /*-
4 * Copyright 2005 Elad Efrat <elad (at) bsd.org.il>
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
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <fcntl.h>
42 #include <unistd.h>
43 #include <err.h>
44 #include <errno.h>
45
46 #include "veriexecctl.h"
47
48 #define VERIEXEC_DEVICE "/dev/veriexec"
49
50 extern struct veriexec_params params; /* in veriexecctl_parse.y */
51 extern char *filename; /* in veriexecctl_conf.l */
52 extern int yynerrs;
53 int gfd, verbose = 0, phase;
54 size_t line;
55
56 /*
57 * Prototypes
58 */
59 static FILE *openlock(const char *);
60 static void phase1_preload(void);
61 static int fingerprint_load(char*);
62 static void usage(void) __attribute__((__noreturn__));
63
64 static FILE *
65 openlock(const char *path)
66 {
67 int lfd;
68
69 if ((lfd = open(path, O_RDONLY|O_EXLOCK, 0)) == -1)
70 return NULL;
71
72 return fdopen(lfd, "r");
73 }
74
75 struct veriexec_up *
76 dev_lookup(dev_t d)
77 {
78 struct veriexec_up *p;
79
80 CIRCLEQ_FOREACH(p, ¶ms_list, vu_list)
81 if (p->vu_param.dev == d)
82 return (p);
83
84 return NULL;
85 }
86
87 struct veriexec_up *
88 dev_add(dev_t d)
89 {
90 struct veriexec_up *up;
91
92 if ((up = calloc((size_t)1, sizeof(*up))) == NULL)
93 err(1, "No memory");
94
95 up->vu_param.dev = d;
96 up->vu_param.hash_size = 1;
97
98 CIRCLEQ_INSERT_TAIL(¶ms_list, up, vu_list);
99
100 return up;
101 }
102
103 /* Load all devices, get rid of the list. */
104 static void
105 phase1_preload(void)
106 {
107 if (verbose)
108 printf("Phase 1: Calculating hash table sizes:\n");
109
110 while (!CIRCLEQ_EMPTY(¶ms_list)) {
111 struct veriexec_up *vup;
112
113 vup = CIRCLEQ_FIRST(¶ms_list);
114
115 if (ioctl(gfd, VERIEXEC_TABLESIZE, &(vup->vu_param)) == -1) {
116 if (errno != EEXIST)
117 err(1, "Error in phase 1: Can't "
118 "set hash table size for device %d",
119 vup->vu_param.dev);
120 }
121
122 if (verbose) {
123 printf(" => Hash table sizing successful for device "
124 "%d. (%zu entries)\n", vup->vu_param.dev,
125 vup->vu_param.hash_size);
126 }
127
128 CIRCLEQ_REMOVE(¶ms_list, vup, vu_list);
129 free(vup);
130 }
131 }
132
133 /*
134 * Load the fingerprint. Assumes that the fingerprint pseudo-device is
135 * opened and the file handle is in gfd.
136 */
137 void
138 phase2_load(void)
139 {
140 /*
141 * If there's no access type specified, use the default.
142 */
143 if (!(params.type & (VERIEXEC_DIRECT|VERIEXEC_INDIRECT|VERIEXEC_FILE)))
144 params.type |= VERIEXEC_DIRECT;
145 if (ioctl(gfd, VERIEXEC_LOAD, ¶ms) == -1)
146 warn("Cannot load params from `%s'", params.file);
147 free(params.fingerprint);
148 }
149
150 /*
151 * Fingerprint load handling.
152 */
153 static int
154 fingerprint_load(char *ifile)
155 {
156 CIRCLEQ_INIT(¶ms_list);
157 memset(¶ms, 0, sizeof(params));
158
159 if ((yyin = openlock(ifile)) == NULL)
160 err(1, "Cannot open `%s'", ifile);
161
162 /*
163 * Phase 1: Scan all config files, creating the list of devices
164 * we have fingerprinted files on, and the amount of
165 * files per device. Lock all files to maintain sync.
166 */
167 phase = 1;
168
169 if (verbose) {
170 (void)printf("Phase 1: Building hash table information:\n");
171 (void)printf("=> Parsing \"%s\"\n", ifile);
172 }
173
174 line = 1;
175 yyparse();
176 if (yynerrs)
177 return -1;
178
179 phase1_preload();
180
181 /*
182 * Phase 2: After we have a circular queue containing all the
183 * devices we care about and the sizes for the hash
184 * tables, do a rescan, this time actually loading the
185 * file data.
186 */
187 rewind(yyin);
188 phase = 2;
189 if (verbose) {
190 (void)printf("Phase 2: Loading per-file fingerprints.\n");
191 (void)printf("=> Parsing \"%s\"\n", ifile);
192 }
193
194 line = 1;
195 yyparse();
196
197 (void)fclose(yyin);
198
199 return 0;
200 }
201
202 static void
203 usage(void)
204 {
205 (void)fprintf(stderr, "Usage: %s [-v] [load <signature_file>]\n",
206 getprogname());
207 exit(1);
208 }
209
210 static void
211 print_flags(unsigned char flags)
212 {
213 char buf[64];
214
215 if (!flags) {
216 printf("<none>\n");
217 return;
218 }
219
220 memset(buf, 0, sizeof(buf));
221
222 while (flags) {
223 if (*buf)
224 strlcat(buf, ", ", sizeof(buf));
225
226 if (flags & VERIEXEC_DIRECT) {
227 strlcat(buf, "direct", sizeof(buf));
228 flags &= ~VERIEXEC_DIRECT;
229 continue;
230 }
231 if (flags & VERIEXEC_INDIRECT) {
232 strlcat(buf, "indirect", sizeof(buf));
233 flags &= ~VERIEXEC_INDIRECT;
234 continue;
235 }
236 if (flags & VERIEXEC_FILE) {
237 strlcat(buf, "file", sizeof(buf));
238 flags &= ~VERIEXEC_FILE;
239 continue;
240 }
241 if (flags & VERIEXEC_UNTRUSTED) {
242 strlcat(buf, "untrusted", sizeof(buf));
243 flags &= ~VERIEXEC_UNTRUSTED;
244 continue;
245 }
246 }
247
248 printf("%s\n", buf);
249 }
250
251 static void
252 print_query(struct veriexec_query_params *qp, char *file)
253 {
254 int i;
255
256 printf("Filename: %s\n", file);
257 printf("Device: %d, inode: %lu\n", qp->dev, qp->ino);
258 printf("Entry flags: ");
259 print_flags(qp->type);
260 printf("Entry status: %s\n", STATUS_STRING(qp->status));
261 printf("Hashing algorithm: %s\n", qp->fp_type);
262 printf("Fingerprint: ");
263 for (i = 0; i < qp->hash_len; i++)
264 printf("%02x", qp->fp[i]);
265 printf("\n");
266 }
267
268 int
269 main(int argc, char **argv)
270 {
271 int c;
272
273 setprogname(argv[0]);
274
275 while ((c = getopt(argc, argv, "v")) != -1)
276 switch (c) {
277 case 'v':
278 verbose = 1;
279 break;
280
281 default:
282 usage();
283 }
284
285 argc -= optind;
286 argv += optind;
287
288 if ((gfd = open(VERIEXEC_DEVICE, O_RDWR, 0)) == -1)
289 err(1, "Cannot open `%s'", VERIEXEC_DEVICE);
290
291 /*
292 * Handle the different commands we can do.
293 */
294 if (argc == 2 && strcasecmp(argv[0], "load") == 0) {
295 filename = argv[1];
296 fingerprint_load(argv[1]);
297 } else if (argc == 2 && strcasecmp(argv[0], "delete") == 0) {
298 struct veriexec_delete_params dp;
299 struct stat sb;
300
301 /* Get device and inode */
302 if (stat(argv[1], &sb) == -1)
303 err(1, "Can't stat `%s'", argv[1]);
304
305 /*
306 * If it's a regular file, remove it. If it's a directory,
307 * remove the entire table. If it's neither, abort.
308 */
309 if (S_ISDIR(sb.st_mode))
310 dp.ino = 0;
311 else if (S_ISREG(sb.st_mode))
312 dp.ino = sb.st_ino;
313 else
314 errx(1, "`%s' is not a regular file or directory.", argv[1]);
315
316 dp.dev = sb.st_dev;
317
318 if (ioctl(gfd, VERIEXEC_DELETE, &dp) == -1)
319 err(1, "Error deleting `%s'", argv[1]);
320 } else if (argc == 2 && strcasecmp(argv[0], "query") == 0) {
321 struct veriexec_query_params qp;
322 struct stat sb;
323 char fp[512]; /* XXX */
324
325 memset(&qp, 0, sizeof(qp));
326 qp.uaddr = &qp;
327
328 /* Get device and inode */
329 if (stat(argv[1], &sb) == -1)
330 err(1, "Can't stat `%s'", argv[1]);
331 if (!S_ISREG(sb.st_mode))
332 errx(1, "`%s' is not a regular file.", argv[1]);
333
334 qp.ino = sb.st_ino;
335 qp.dev = sb.st_dev;
336 memset(fp, 0, sizeof(fp));
337 qp.fp = &fp[0];
338 qp.fp_bufsize = sizeof(fp);
339
340 if (ioctl(gfd, VERIEXEC_QUERY, &qp) == -1)
341 err(1, "Error querying `%s'", argv[1]);
342
343 print_query(&qp, argv[1]);
344 } else
345 usage();
346
347 (void)close(gfd);
348 return 0;
349 }
350