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