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