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