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