Home | History | Annotate | Line # | Download | only in veriexecctl
veriexecctl.c revision 1.27
      1  1.27      elad /*	$NetBSD: veriexecctl.c,v 1.27 2007/05/15 19:47:46 elad Exp $	*/
      2   1.1     blymn 
      3   1.1     blymn /*-
      4  1.24      elad  * Copyright 2005 Elad Efrat <elad (at) NetBSD.org>
      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/param.h>
     34  1.27      elad #include <sys/statvfs.h>
     35   1.6     blymn #include <sys/verified_exec.h>
     36   1.1     blymn 
     37   1.1     blymn #include <stdio.h>
     38   1.2   thorpej #include <stdlib.h>
     39   1.6     blymn #include <string.h>
     40   1.1     blymn #include <fcntl.h>
     41   1.6     blymn #include <unistd.h>
     42   1.5     blymn #include <err.h>
     43   1.6     blymn #include <errno.h>
     44   1.6     blymn 
     45  1.25      elad #include <prop/proplib.h>
     46  1.25      elad 
     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.27      elad #define	STATUS_STRING(status)	((status) == FINGERPRINT_NOTEVAL ?	\
     52  1.27      elad 					     "not evaluated" :		\
     53  1.27      elad 				 (status) == FINGERPRINT_VALID ?	\
     54  1.27      elad 					     "valid" :			\
     55  1.27      elad 				 (status) == FINGERPRINT_NOMATCH ?	\
     56  1.27      elad 					     "mismatch" :		\
     57  1.27      elad 					     "<unknown>")
     58   1.6     blymn 
     59  1.27      elad extern int yyparse(void);
     60   1.7        he 
     61  1.27      elad int gfd, verbose = 0, error = EXIT_SUCCESS;
     62  1.27      elad size_t line = 0;
     63   1.6     blymn 
     64   1.8  christos static void
     65  1.27      elad usage(void)
     66   1.6     blymn {
     67  1.27      elad 	const char *progname = getprogname();
     68  1.25      elad 
     69  1.27      elad 	(void)fprintf(stderr, "Usage:\n"
     70  1.27      elad 	    "%s [-ekv] load <signature_file>\n"
     71  1.27      elad 	    "%s delete <file | mount_point>\n"
     72  1.27      elad 	    "%s query <file>\n"
     73  1.27      elad 	    "%s dump\n"
     74  1.27      elad 	    "%s flush\n", progname, progname, progname, progname, progname);
     75   1.1     blymn 
     76   1.8  christos 	exit(1);
     77   1.6     blymn }
     78   1.6     blymn 
     79  1.19      elad static void
     80  1.27      elad flags2str(uint8_t flags, char *buf, size_t len)
     81  1.19      elad {
     82  1.27      elad 	uint8_t all;
     83  1.19      elad 
     84  1.27      elad 	all = (VERIEXEC_DIRECT | VERIEXEC_INDIRECT | VERIEXEC_FILE |
     85  1.27      elad 	    VERIEXEC_UNTRUSTED);
     86  1.27      elad 	if (flags & ~all) {
     87  1.27      elad 		if (verbose)
     88  1.27      elad 			warnx("Contaminated flags `0x%x'", (flags & ~all));
     89  1.19      elad 		return;
     90  1.19      elad 	}
     91  1.19      elad 
     92  1.19      elad 	while (flags) {
     93  1.19      elad 		if (*buf)
     94  1.27      elad 			strlcat(buf, ", ", len);
     95  1.19      elad 
     96  1.19      elad 		if (flags & VERIEXEC_DIRECT) {
     97  1.27      elad 			strlcat(buf, "direct", len);
     98  1.19      elad 			flags &= ~VERIEXEC_DIRECT;
     99  1.19      elad 			continue;
    100  1.19      elad 		}
    101  1.19      elad 		if (flags & VERIEXEC_INDIRECT) {
    102  1.27      elad 			strlcat(buf, "indirect", len);
    103  1.19      elad 			flags &= ~VERIEXEC_INDIRECT;
    104  1.19      elad 			continue;
    105  1.19      elad 		}
    106  1.19      elad 		if (flags & VERIEXEC_FILE) {
    107  1.27      elad 			strlcat(buf, "file", len);
    108  1.19      elad 			flags &= ~VERIEXEC_FILE;
    109  1.19      elad 			continue;
    110  1.19      elad 		}
    111  1.19      elad 		if (flags & VERIEXEC_UNTRUSTED) {
    112  1.27      elad 			strlcat(buf, "untrusted", len);
    113  1.19      elad 			flags &= ~VERIEXEC_UNTRUSTED;
    114  1.19      elad 			continue;
    115  1.19      elad 		}
    116  1.19      elad 	}
    117  1.19      elad }
    118  1.19      elad 
    119  1.19      elad static void
    120  1.25      elad print_query(prop_dictionary_t qp, char *file)
    121  1.19      elad {
    122  1.23      elad 	struct statvfs sv;
    123  1.25      elad 	const char *v;
    124  1.19      elad 	int i;
    125  1.25      elad 	uint8_t u8;
    126  1.27      elad 	char buf[64];
    127  1.19      elad 
    128  1.23      elad 	if (statvfs(file, &sv) != 0)
    129  1.23      elad 		err(1, "Can't statvfs() `%s'\n", file);
    130  1.23      elad 
    131  1.19      elad 	printf("Filename: %s\n", file);
    132  1.23      elad 	printf("Mount: %s\n", sv.f_mntonname);
    133  1.25      elad 	prop_dictionary_get_uint8(qp, "entry-type", &u8);
    134  1.27      elad 	memset(buf, 0, sizeof(buf));
    135  1.27      elad 	flags2str(u8, buf, sizeof(buf));
    136  1.27      elad 	printf("Entry flags: %s\n", buf);
    137  1.25      elad 	prop_dictionary_get_uint8(qp, "status", &u8);
    138  1.25      elad 	printf("Entry status: %s\n", STATUS_STRING(u8));
    139  1.25      elad 	printf("Fingerprint algorithm: %s\n", dict_gets(qp, "fp-type"));
    140  1.19      elad 	printf("Fingerprint: ");
    141  1.27      elad 	 v = dict_getd(qp, "fp");
    142  1.25      elad 	for (i = 0; i < prop_data_size(prop_dictionary_get(qp, "fp")); i++)
    143  1.25      elad 		printf("%02x", v[i] & 0xff);
    144  1.19      elad 	printf("\n");
    145  1.19      elad }
    146  1.19      elad 
    147  1.27      elad static char *
    148  1.27      elad escape(const char *s)
    149  1.27      elad {
    150  1.27      elad 	char *q, *p;
    151  1.27      elad 	size_t len;
    152  1.27      elad 
    153  1.27      elad 	len = strlen(s);
    154  1.27      elad 	if (len >= MAXPATHLEN)
    155  1.27      elad 		return (NULL);
    156  1.27      elad 
    157  1.27      elad 	len *= 2;
    158  1.27      elad 	q = p = calloc(1, len + 1);
    159  1.27      elad 
    160  1.27      elad 	while (*s) {
    161  1.27      elad 		if (*s == ' ' || *s == '\t')
    162  1.27      elad 			*p++ = '\\';
    163  1.27      elad 
    164  1.27      elad 		*p++ = *s++;
    165  1.27      elad 	}
    166  1.27      elad 
    167  1.27      elad 	return (q);
    168  1.27      elad }
    169  1.27      elad 
    170  1.27      elad static void
    171  1.27      elad print_entry(prop_dictionary_t entry)
    172  1.27      elad {
    173  1.27      elad 	char *file, *fp;
    174  1.27      elad 	const uint8_t *v;
    175  1.27      elad 	size_t len, i;
    176  1.27      elad 	uint8_t u8;
    177  1.27      elad 	char flags[64];
    178  1.27      elad 
    179  1.27      elad 	/* Get fingerprint in ASCII. */
    180  1.27      elad 	len = prop_data_size(prop_dictionary_get(entry, "fp"));
    181  1.27      elad 	len *= 2;
    182  1.27      elad 	fp = calloc(1, len + 1);
    183  1.27      elad 	v = dict_getd(entry, "fp");
    184  1.27      elad 	for (i = 0; i < len; i++)
    185  1.27      elad 		snprintf(fp, len + 1, "%s%02x", fp, v[i] & 0xff);
    186  1.27      elad 
    187  1.27      elad 	/* Get flags. */
    188  1.27      elad 	memset(flags, 0, sizeof(flags));
    189  1.27      elad 	prop_dictionary_get_uint8(entry, "entry-type", &u8);
    190  1.27      elad 	flags2str(u8, flags, sizeof(flags));
    191  1.27      elad 
    192  1.27      elad 	file = escape(dict_gets(entry, "file"));
    193  1.27      elad 	printf("%s %s %s %s\n", file, dict_gets(entry, "fp-type"), fp, flags);
    194  1.27      elad 	free(file);
    195  1.27      elad }
    196  1.27      elad 
    197   1.6     blymn int
    198   1.6     blymn main(int argc, char **argv)
    199   1.6     blymn {
    200  1.27      elad 	extern boolean_t keep_filename, eval_on_load;
    201   1.6     blymn 	int c;
    202   1.6     blymn 
    203   1.8  christos 	setprogname(argv[0]);
    204   1.6     blymn 
    205  1.27      elad 	while ((c = getopt(argc, argv, "ekv")) != -1)
    206   1.6     blymn 		switch (c) {
    207  1.27      elad 		case 'e':
    208  1.27      elad 			eval_on_load = TRUE;
    209  1.27      elad 			break;
    210  1.27      elad 
    211  1.27      elad 		case 'k':
    212  1.27      elad 			keep_filename = TRUE;
    213  1.27      elad 			break;
    214  1.27      elad 
    215   1.6     blymn 		case 'v':
    216   1.6     blymn 			verbose = 1;
    217   1.6     blymn 			break;
    218   1.6     blymn 
    219   1.6     blymn 		default:
    220   1.8  christos 			usage();
    221   1.6     blymn 		}
    222   1.1     blymn 
    223   1.6     blymn 	argc -= optind;
    224   1.6     blymn 	argv += optind;
    225   1.6     blymn 
    226   1.8  christos 	if ((gfd = open(VERIEXEC_DEVICE, O_RDWR, 0)) == -1)
    227   1.8  christos 		err(1, "Cannot open `%s'", VERIEXEC_DEVICE);
    228   1.1     blymn 
    229   1.9      elad 	/*
    230   1.9      elad 	 * Handle the different commands we can do.
    231   1.9      elad 	 */
    232   1.8  christos 	if (argc == 2 && strcasecmp(argv[0], "load") == 0) {
    233  1.27      elad 		extern FILE *yyin;
    234  1.27      elad 		int lfd;
    235  1.27      elad 
    236  1.27      elad 		lfd = open(argv[1], O_RDONLY|O_EXLOCK, 0);
    237  1.27      elad 		if (lfd == -1)
    238  1.27      elad 			err(1, "Cannot open `%s'", argv[1]);
    239  1.27      elad 
    240  1.27      elad 		yyin = fdopen(lfd, "r");
    241  1.27      elad 
    242  1.27      elad 		yyparse();
    243  1.27      elad 
    244  1.27      elad 		(void)fclose(yyin);
    245  1.18      elad 	} else if (argc == 2 && strcasecmp(argv[0], "delete") == 0) {
    246  1.25      elad 		prop_dictionary_t dp;
    247  1.18      elad 		struct stat sb;
    248  1.18      elad 
    249  1.18      elad 		if (stat(argv[1], &sb) == -1)
    250  1.18      elad 			err(1, "Can't stat `%s'", argv[1]);
    251  1.18      elad 
    252  1.25      elad 		dp = prop_dictionary_create();
    253  1.25      elad 		dict_sets(dp, "file", argv[1]);
    254  1.21      elad 
    255  1.18      elad 		/*
    256  1.18      elad 		 * If it's a regular file, remove it. If it's a directory,
    257  1.18      elad 		 * remove the entire table. If it's neither, abort.
    258  1.18      elad 		 */
    259  1.21      elad 		if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode))
    260  1.27      elad 			errx(1, "`%s' is not a regular file or directory.",
    261  1.27      elad 			    argv[1]);
    262  1.18      elad 
    263  1.27      elad 		if (prop_dictionary_send_ioctl(dp, gfd, VERIEXEC_DELETE) != 0)
    264  1.18      elad 			err(1, "Error deleting `%s'", argv[1]);
    265  1.25      elad 
    266  1.25      elad 		prop_object_release(dp);
    267  1.19      elad 	} else if (argc == 2 && strcasecmp(argv[0], "query") == 0) {
    268  1.25      elad 		prop_dictionary_t qp, rqp;
    269  1.27      elad 		int r;
    270  1.19      elad 
    271  1.25      elad 		qp = prop_dictionary_create();
    272  1.21      elad 
    273  1.25      elad 		dict_sets(qp, "file", argv[1]);
    274  1.19      elad 
    275  1.27      elad 		r = prop_dictionary_sendrecv_ioctl(qp, gfd, VERIEXEC_QUERY,
    276  1.27      elad 		    &rqp);
    277  1.27      elad 		if (r) {
    278  1.27      elad 			if (r == ENOENT)
    279  1.27      elad 				errx(1, "No Veriexec entry for `%s'", argv[1]);
    280  1.27      elad 
    281  1.19      elad 			err(1, "Error querying `%s'", argv[1]);
    282  1.27      elad 		}
    283  1.19      elad 
    284  1.25      elad 		if (rqp != NULL) {
    285  1.25      elad 			print_query(rqp, argv[1]);
    286  1.25      elad 			prop_object_release(rqp);
    287  1.25      elad 		}
    288  1.25      elad 
    289  1.25      elad 		prop_object_release(qp);
    290  1.27      elad 	} else if (argc == 1 && strcasecmp(argv[0], "dump") == 0) {
    291  1.27      elad 		prop_array_t entries;
    292  1.27      elad 		size_t nentries, i;
    293  1.27      elad 
    294  1.27      elad 		if (prop_array_recv_ioctl(gfd, VERIEXEC_DUMP,
    295  1.27      elad 		    &entries) == -1)
    296  1.27      elad 			err(1, "Error dumping tables");
    297  1.27      elad 
    298  1.27      elad 		nentries = prop_array_count(entries);
    299  1.27      elad 		for (i = 0; i < nentries; i++)
    300  1.27      elad 			print_entry(prop_array_get(entries, i));
    301  1.27      elad 
    302  1.27      elad 		prop_object_release(entries);
    303  1.27      elad 	} else if (argc == 1 && strcasecmp(argv[0], "flush") == 0) {
    304  1.27      elad 		if (ioctl(gfd, VERIEXEC_FLUSH) == -1)
    305  1.27      elad 			err(1, "Cannot flush Veriexec database");
    306   1.8  christos 	} else
    307   1.8  christos 		usage();
    308   1.1     blymn 
    309   1.8  christos 	(void)close(gfd);
    310  1.27      elad 	return error;
    311   1.1     blymn }
    312