Home | History | Annotate | Line # | Download | only in veriexecctl
veriexecctl.c revision 1.21
      1 /*	$NetBSD: veriexecctl.c,v 1.21 2006/07/14 18:41:40 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 dev_lookup(char *vfs)
     78 {
     79 	struct veriexec_up *p;
     80 
     81 	CIRCLEQ_FOREACH(p, &params_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(dev_t d) */
     90 dev_add(dev_t d, char *vfs)
     91 {
     92 	struct veriexec_up *up;
     93 
     94 	if ((up = calloc((size_t)1, sizeof(*up))) == NULL)
     95 		err(1, "No memory");
     96 
     97 	up->vu_param.hash_size = 1;
     98 	strlcpy(up->vu_param.file, vfs, sizeof(up->vu_param.file));
     99 
    100 	CIRCLEQ_INSERT_TAIL(&params_list, up, vu_list);
    101 
    102 	return up;
    103 }
    104 
    105 /* Load all devices, get rid of the list. */
    106 static void
    107 phase1_preload(void)
    108 {
    109 	if (verbose)
    110 		printf("Phase 1: Calculating hash table sizes:\n");
    111 
    112 	while (!CIRCLEQ_EMPTY(&params_list)) {
    113 		struct veriexec_up *vup;
    114 
    115 		vup = CIRCLEQ_FIRST(&params_list);
    116 
    117 		if (ioctl(gfd, VERIEXEC_TABLESIZE, &(vup->vu_param)) == -1) {
    118 			if (errno != EEXIST)
    119 				err(1, "Error in phase 1: Can't "
    120 				    "set hash table size for mount %s",
    121 				    vup->vu_param.file);
    122 		}
    123 
    124 		if (verbose) {
    125 			printf(" => Hash table sizing successful for mount "
    126 			    "%s. (%zu entries)\n", vup->vu_param.file,
    127 			    vup->vu_param.hash_size);
    128 		}
    129 
    130 		CIRCLEQ_REMOVE(&params_list, vup, vu_list);
    131 		free(vup);
    132 	}
    133 }
    134 
    135 /*
    136  * Load the fingerprint. Assumes that the fingerprint pseudo-device is
    137  * opened and the file handle is in gfd.
    138  */
    139 void
    140 phase2_load(void)
    141 {
    142 	/*
    143 	 * If there's no access type specified, use the default.
    144 	 */
    145 	if (!(params.type & (VERIEXEC_DIRECT|VERIEXEC_INDIRECT|VERIEXEC_FILE)))
    146 		params.type |= VERIEXEC_DIRECT;
    147 	if (ioctl(gfd, VERIEXEC_LOAD, &params) == -1)
    148 		warn("Cannot load params from `%s'", params.file);
    149 	free(params.fingerprint);
    150 }
    151 
    152 /*
    153  * Fingerprint load handling.
    154  */
    155 static int
    156 fingerprint_load(char *ifile)
    157 {
    158 	CIRCLEQ_INIT(&params_list);
    159 	memset(&params, 0, sizeof(params));
    160 
    161 	if ((yyin = openlock(ifile)) == NULL)
    162 		err(1, "Cannot open `%s'", ifile);
    163 
    164 	/*
    165 	 * Phase 1: Scan all config files, creating the list of devices
    166 	 *	    we have fingerprinted files on, and the amount of
    167 	 *	    files per device. Lock all files to maintain sync.
    168 	 */
    169 	phase = 1;
    170 
    171 	if (verbose) {
    172 		(void)printf("Phase 1: Building hash table information:\n");
    173 		(void)printf("=> Parsing \"%s\"\n", ifile);
    174 	}
    175 
    176 	line = 1;
    177 	yyparse();
    178 	if (yynerrs)
    179 		return -1;
    180 
    181 	phase1_preload();
    182 
    183 	/*
    184 	 * Phase 2: After we have a circular queue containing all the
    185 	 * 	    devices we care about and the sizes for the hash
    186 	 *	    tables, do a rescan, this time actually loading the
    187 	 *	    file data.
    188 	 */
    189 	rewind(yyin);
    190 	phase = 2;
    191 	if (verbose) {
    192 		(void)printf("Phase 2: Loading per-file fingerprints.\n");
    193 		(void)printf("=> Parsing \"%s\"\n", ifile);
    194 	}
    195 
    196 	line = 1;
    197 	yyparse();
    198 
    199 	(void)fclose(yyin);
    200 
    201 	return 0;
    202 }
    203 
    204 static void
    205 usage(void)
    206 {
    207 	(void)fprintf(stderr, "Usage: %s [-v] [load <signature_file>]\n",
    208 	    getprogname());
    209 	exit(1);
    210 }
    211 
    212 static void
    213 print_flags(unsigned char flags)
    214 {
    215 	char buf[64];
    216 
    217 	if (!flags) {
    218 		printf("<none>\n");
    219 		return;
    220 	}
    221 
    222 	memset(buf, 0, sizeof(buf));
    223 
    224 	while (flags) {
    225 		if (*buf)
    226 			strlcat(buf, ", ", sizeof(buf));
    227 
    228 		if (flags & VERIEXEC_DIRECT) {
    229 			strlcat(buf, "direct", sizeof(buf));
    230 			flags &= ~VERIEXEC_DIRECT;
    231 			continue;
    232 		}
    233 		if (flags & VERIEXEC_INDIRECT) {
    234 			strlcat(buf, "indirect", sizeof(buf));
    235 			flags &= ~VERIEXEC_INDIRECT;
    236 			continue;
    237 		}
    238 		if (flags & VERIEXEC_FILE) {
    239 			strlcat(buf, "file", sizeof(buf));
    240 			flags &= ~VERIEXEC_FILE;
    241 			continue;
    242 		}
    243 		if (flags & VERIEXEC_UNTRUSTED) {
    244 			strlcat(buf, "untrusted", sizeof(buf));
    245 			flags &= ~VERIEXEC_UNTRUSTED;
    246 			continue;
    247 		}
    248 	}
    249 
    250 	printf("%s\n", buf);
    251 }
    252 
    253 static void
    254 print_query(struct veriexec_query_params *qp, char *file)
    255 {
    256 	int i;
    257 
    258 	printf("Filename: %s\n", file);
    259 	printf("Device: %d, inode: %" PRIu64 "\n", qp->dev, qp->ino);
    260 	printf("Entry flags: ");
    261 	print_flags(qp->type);
    262 	printf("Entry status: %s\n", STATUS_STRING(qp->status));
    263 	printf("Hashing algorithm: %s\n", qp->fp_type);
    264 	printf("Fingerprint: ");
    265 	for (i = 0; i < qp->hash_len; i++)
    266 		printf("%02x", qp->fp[i]);
    267 	printf("\n");
    268 }
    269 
    270 int
    271 main(int argc, char **argv)
    272 {
    273 	int c;
    274 
    275 	setprogname(argv[0]);
    276 
    277 	while ((c = getopt(argc, argv, "v")) != -1)
    278 		switch (c) {
    279 		case 'v':
    280 			verbose = 1;
    281 			break;
    282 
    283 		default:
    284 			usage();
    285 		}
    286 
    287 	argc -= optind;
    288 	argv += optind;
    289 
    290 	if ((gfd = open(VERIEXEC_DEVICE, O_RDWR, 0)) == -1)
    291 		err(1, "Cannot open `%s'", VERIEXEC_DEVICE);
    292 
    293 	/*
    294 	 * Handle the different commands we can do.
    295 	 */
    296 	if (argc == 2 && strcasecmp(argv[0], "load") == 0) {
    297 		filename = argv[1];
    298 		fingerprint_load(argv[1]);
    299 	} else if (argc == 2 && strcasecmp(argv[0], "delete") == 0) {
    300 		struct veriexec_delete_params dp;
    301 		struct stat sb;
    302 
    303 		/* Get device and inode */
    304 		if (stat(argv[1], &sb) == -1)
    305 			err(1, "Can't stat `%s'", argv[1]);
    306 
    307 		strlcpy(dp.file, argv[1], sizeof(dp.file));
    308 
    309 		/*
    310 		 * If it's a regular file, remove it. If it's a directory,
    311 		 * remove the entire table. If it's neither, abort.
    312 		 */
    313 		if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode))
    314 			errx(1, "`%s' is not a regular file or directory.", argv[1]);
    315 
    316 		if (ioctl(gfd, VERIEXEC_DELETE, &dp) == -1)
    317 			err(1, "Error deleting `%s'", argv[1]);
    318 	} else if (argc == 2 && strcasecmp(argv[0], "query") == 0) {
    319 		struct veriexec_query_params qp;
    320 		struct stat sb;
    321 		char fp[512]; /* XXX */
    322 
    323 		memset(&qp, 0, sizeof(qp));
    324 		qp.uaddr = &qp;
    325 
    326 		/* Get device and inode */
    327 		if (stat(argv[1], &sb) == -1)
    328 			err(1, "Can't stat `%s'", argv[1]);
    329 		if (!S_ISREG(sb.st_mode))
    330 			errx(1, "`%s' is not a regular file.", argv[1]);
    331 
    332 		strlcpy(qp.file, argv[1], sizeof(qp.file));
    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