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