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