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