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