veriexecctl.c revision 1.5.6.1 1 1.5.6.1 tron /* $NetBSD: veriexecctl.c,v 1.5.6.1 2005/06/10 14:47:43 tron Exp $ */
2 1.1 blymn
3 1.1 blymn /*-
4 1.5.6.1 tron * Copyright 2005 Elad Efrat <elad (at) bsd.org.il>
5 1.5.6.1 tron * Copyright 2005 Brett Lymn <blymn (at) netbsd.org>
6 1.5.6.1 tron *
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.5.6.1 tron #include <sys/ioctl.h>
34 1.5.6.1 tron #include <sys/param.h>
35 1.5.6.1 tron #include <sys/queue.h>
36 1.5.6.1 tron #include <sys/verified_exec.h>
37 1.1 blymn
38 1.1 blymn #include <stdio.h>
39 1.2 thorpej #include <stdlib.h>
40 1.5.6.1 tron #include <string.h>
41 1.1 blymn #include <fcntl.h>
42 1.5.6.1 tron #include <unistd.h>
43 1.5 blymn #include <err.h>
44 1.5.6.1 tron #include <errno.h>
45 1.5.6.1 tron
46 1.5.6.1 tron #include "veriexecctl.h"
47 1.5.6.1 tron
48 1.5.6.1 tron #define VERIEXEC_DEVICE "/dev/veriexec"
49 1.5 blymn
50 1.5.6.1 tron #define usage(code) errx(code, "Usage: %s [-v] [load <signature_file>] " \
51 1.5.6.1 tron "[fingerprints]", \
52 1.5.6.1 tron getprogname())
53 1.1 blymn
54 1.5.6.1 tron extern struct veriexec_params params; /* in veriexecctl_parse.y */
55 1.5.6.1 tron extern char *filename; /* in veriexecctl_conf.l */
56 1.5.6.1 tron int fd, verbose = 0, no_mem = 0, phase;
57 1.5.6.1 tron unsigned line;
58 1.1 blymn
59 1.5.6.1 tron /*
60 1.5.6.1 tron * Prototypes
61 1.5.6.1 tron */
62 1.1 blymn int
63 1.5.6.1 tron fingerprint_load(char *infile);
64 1.5.6.1 tron
65 1.5.6.1 tron FILE *
66 1.5.6.1 tron openlock(const char *path)
67 1.1 blymn {
68 1.5.6.1 tron int fd;
69 1.5.6.1 tron
70 1.5.6.1 tron fd = open(path, O_RDONLY|O_EXLOCK, 0);
71 1.5.6.1 tron if (fd < 0)
72 1.5.6.1 tron return (NULL);
73 1.5.6.1 tron
74 1.5.6.1 tron return (fdopen(fd, "r"));
75 1.5.6.1 tron }
76 1.5.6.1 tron
77 1.5.6.1 tron struct vexec_up *
78 1.5.6.1 tron dev_lookup(dev_t d)
79 1.5.6.1 tron {
80 1.5.6.1 tron struct vexec_up *p;
81 1.5.6.1 tron
82 1.5.6.1 tron CIRCLEQ_FOREACH(p, ¶ms_list, vu_list) {
83 1.5.6.1 tron if (p->vu_param.dev == d)
84 1.5.6.1 tron return (p);
85 1.5.6.1 tron }
86 1.5.6.1 tron
87 1.5.6.1 tron return (NULL);
88 1.5.6.1 tron }
89 1.5.6.1 tron
90 1.5.6.1 tron struct vexec_up *
91 1.5.6.1 tron dev_add(dev_t d)
92 1.5.6.1 tron {
93 1.5.6.1 tron struct vexec_up *up;
94 1.5.6.1 tron
95 1.5.6.1 tron up = calloc(1, sizeof(*up));
96 1.5.6.1 tron if (up == NULL)
97 1.5.6.1 tron err(1, "No memory");
98 1.5.6.1 tron
99 1.5.6.1 tron up->vu_param.dev = d;
100 1.5.6.1 tron up->vu_param.hash_size = 1;
101 1.5.6.1 tron
102 1.5.6.1 tron CIRCLEQ_INSERT_TAIL(¶ms_list, up, vu_list);
103 1.5.6.1 tron
104 1.5.6.1 tron return (up);
105 1.5.6.1 tron }
106 1.5.6.1 tron
107 1.5.6.1 tron /* Load all devices, get rid of the list. */
108 1.5.6.1 tron int
109 1.5.6.1 tron phase1_preload(void)
110 1.5.6.1 tron {
111 1.5.6.1 tron if (verbose)
112 1.5.6.1 tron printf("Phase 1: Calculating hash table sizes:\n");
113 1.5.6.1 tron
114 1.5.6.1 tron while (!CIRCLEQ_EMPTY(¶ms_list)) {
115 1.5.6.1 tron struct vexec_up *vup;
116 1.5.6.1 tron
117 1.5.6.1 tron vup = CIRCLEQ_FIRST(¶ms_list);
118 1.5.6.1 tron
119 1.5.6.1 tron if (ioctl(fd, VERIEXEC_TABLESIZE, &(vup->vu_param)) < 0) {
120 1.5.6.1 tron (void) fprintf(stderr, "Error in phase 1: Can't "
121 1.5.6.1 tron "set hash table size for device %d: %s.\n",
122 1.5.6.1 tron vup->vu_param.dev, strerror(errno));
123 1.5.6.1 tron
124 1.5.6.1 tron return (-1);
125 1.5.6.1 tron }
126 1.5.6.1 tron
127 1.5.6.1 tron if (verbose) {
128 1.5.6.1 tron printf(" => Hash table sizing successful for device "
129 1.5.6.1 tron "%d. (%u entries)\n", vup->vu_param.dev,
130 1.5.6.1 tron vup->vu_param.hash_size);
131 1.5.6.1 tron }
132 1.5.6.1 tron
133 1.5.6.1 tron CIRCLEQ_REMOVE(¶ms_list, vup, vu_list);
134 1.5.6.1 tron free(vup);
135 1.5.6.1 tron }
136 1.5.6.1 tron
137 1.5.6.1 tron return (0);
138 1.5.6.1 tron }
139 1.5.6.1 tron
140 1.5.6.1 tron /*
141 1.5.6.1 tron * Load the fingerprint. Assumes that the fingerprint pseudo-device is
142 1.5.6.1 tron * opened and the file handle is in fd.
143 1.5.6.1 tron */
144 1.5.6.1 tron void
145 1.5.6.1 tron phase2_load(void)
146 1.5.6.1 tron {
147 1.5.6.1 tron if (ioctl(fd, VERIEXEC_LOAD, ¶ms) < 0) {
148 1.5.6.1 tron (void) fprintf(stderr, "%s: %s\n", params.file,
149 1.5.6.1 tron strerror(errno));
150 1.1 blymn }
151 1.5.6.1 tron free(params.fingerprint);
152 1.5.6.1 tron }
153 1.5.6.1 tron
154 1.5.6.1 tron /*
155 1.5.6.1 tron * Fingerprint load handling.
156 1.5.6.1 tron */
157 1.5.6.1 tron int
158 1.5.6.1 tron fingerprint_load(char *infile)
159 1.5.6.1 tron {
160 1.5.6.1 tron CIRCLEQ_INIT(¶ms_list);
161 1.1 blymn
162 1.5.6.1 tron if ((yyin = openlock(infile)) == NULL) {
163 1.5.6.1 tron err(1, "Failed to open %s", infile);
164 1.1 blymn }
165 1.1 blymn
166 1.5.6.1 tron /*
167 1.5.6.1 tron * Phase 1: Scan all config files, creating the list of devices
168 1.5.6.1 tron * we have fingerprinted files on, and the amount of
169 1.5.6.1 tron * files per device. Lock all files to maintain sync.
170 1.5.6.1 tron */
171 1.5.6.1 tron phase = 1;
172 1.5.6.1 tron
173 1.5.6.1 tron if (verbose) {
174 1.5.6.1 tron (void) fprintf(stderr, "Phase 1: Building hash table information:\n");
175 1.5.6.1 tron (void) fprintf(stderr, "=> Parsing \"%s\"\n", infile);
176 1.5.6.1 tron }
177 1.5.6.1 tron
178 1.5.6.1 tron yyparse();
179 1.5.6.1 tron
180 1.5.6.1 tron if (phase1_preload() < 0)
181 1.5.6.1 tron exit(1);
182 1.5.6.1 tron
183 1.5.6.1 tron /*
184 1.5.6.1 tron * Phase 2: After we have a circular queue containing all the
185 1.5.6.1 tron * devices we care about and the sizes for the hash
186 1.5.6.1 tron * tables, do a rescan, this time actually loading the
187 1.5.6.1 tron * file data.
188 1.5.6.1 tron */
189 1.5.6.1 tron rewind(yyin);
190 1.5.6.1 tron phase = 2;
191 1.5.6.1 tron if (verbose) {
192 1.5.6.1 tron (void) fprintf(stderr, "Phase 2: Loading per-file "
193 1.5.6.1 tron "fingerprints.\n");
194 1.5.6.1 tron (void) fprintf(stderr, "=> Parsing \"%s\"\n", infile);
195 1.1 blymn }
196 1.1 blymn
197 1.1 blymn yyparse();
198 1.5.6.1 tron
199 1.5.6.1 tron (void) fclose(yyin);
200 1.5.6.1 tron
201 1.5.6.1 tron return(0);
202 1.5.6.1 tron }
203 1.5.6.1 tron
204 1.5.6.1 tron int
205 1.5.6.1 tron main(int argc, char **argv)
206 1.5.6.1 tron {
207 1.5.6.1 tron char *newp;
208 1.5.6.1 tron int c;
209 1.5.6.1 tron unsigned size;
210 1.5.6.1 tron struct veriexec_fp_report report;
211 1.5.6.1 tron
212 1.5.6.1 tron if ((argc < 2) || (argc > 4)) {
213 1.5.6.1 tron usage(1);
214 1.5.6.1 tron }
215 1.5.6.1 tron
216 1.5.6.1 tron while ((c = getopt(argc, argv, "v")) != -1) {
217 1.5.6.1 tron switch (c) {
218 1.5.6.1 tron case 'v':
219 1.5.6.1 tron verbose = 1;
220 1.5.6.1 tron break;
221 1.5.6.1 tron
222 1.5.6.1 tron default:
223 1.5.6.1 tron usage(1);
224 1.5.6.1 tron }
225 1.5.6.1 tron }
226 1.5.6.1 tron
227 1.5.6.1 tron argc -= optind;
228 1.5.6.1 tron argv += optind;
229 1.5.6.1 tron
230 1.5.6.1 tron fd = open(VERIEXEC_DEVICE, O_RDWR, 0);
231 1.5.6.1 tron if (fd == -1) {
232 1.5.6.1 tron err(1, "Failed to open pseudo-device");
233 1.5.6.1 tron }
234 1.5.6.1 tron
235 1.5.6.1 tron /*
236 1.5.6.1 tron * Handle the different commands we can do.
237 1.5.6.1 tron */
238 1.5.6.1 tron if (strcasecmp(argv[0], "load") == 0) {
239 1.5.6.1 tron line = 0;
240 1.5.6.1 tron filename = argv[1];
241 1.5.6.1 tron fingerprint_load(argv[1]);
242 1.5.6.1 tron } else if (strcasecmp(argv[0], "fingerprints") == 0) {
243 1.5.6.1 tron size = report.size = 100;
244 1.5.6.1 tron if ((report.fingerprints = malloc(report.size)) == NULL) {
245 1.5.6.1 tron fprintf(stderr, "fingerprints: malloc failed.\n");
246 1.5.6.1 tron exit(1);
247 1.5.6.1 tron }
248 1.5.6.1 tron
249 1.5.6.1 tron if (ioctl(fd, VERIEXEC_FINGERPRINTS, &report) == 0) {
250 1.5.6.1 tron if (size != report.size) {
251 1.5.6.1 tron if (verbose)
252 1.5.6.1 tron fprintf(stderr, "fingerprints: "
253 1.5.6.1 tron "buffer insufficient, "
254 1.5.6.1 tron "reallocating to %d bytes.\n",
255 1.5.6.1 tron report.size);
256 1.5.6.1 tron
257 1.5.6.1 tron /* fingerprint store was not large enough
258 1.5.6.1 tron make more room and try again. */
259 1.5.6.1 tron if ((newp = realloc(report.fingerprints,
260 1.5.6.1 tron report.size)) == NULL) {
261 1.5.6.1 tron fprintf(stderr, "fingerprints: "
262 1.5.6.1 tron "realloc failed\n");
263 1.5.6.1 tron exit(1);
264 1.5.6.1 tron }
265 1.5.6.1 tron if (ioctl(fd, VERIEXEC_FINGERPRINTS,
266 1.5.6.1 tron &report) < 0) {
267 1.5.6.1 tron fprintf(stderr,
268 1.5.6.1 tron "fingerprints ioctl: %s\n",
269 1.5.6.1 tron strerror(errno));
270 1.5.6.1 tron exit(1);
271 1.5.6.1 tron }
272 1.5.6.1 tron }
273 1.5.6.1 tron } else {
274 1.5.6.1 tron (void) fprintf(stderr,
275 1.5.6.1 tron "fingerprints ioctl: %s\n",
276 1.5.6.1 tron strerror(errno));
277 1.5.6.1 tron exit(1);
278 1.5.6.1 tron }
279 1.5.6.1 tron
280 1.5.6.1 tron printf("Supported fingerprints: %s\n", report.fingerprints);
281 1.5.6.1 tron } else {
282 1.5.6.1 tron usage(1);
283 1.5.6.1 tron }
284 1.5.6.1 tron
285 1.5.6.1 tron (void) close(fd);
286 1.5.6.1 tron return (0);
287 1.1 blymn }
288