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