2 * Copyright (c) 2017-2018, Juniper Networks, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
17 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * Routines to verify files loaded.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/queue.h>
35 #include "libsecureboot.h"
36 #include <verify_file.h>
37 #include <manifests.h>
43 * define MANIFEST_SKIP to Skip - in tests/tvo.c so that
44 * tvo can control the value we use in find_manifest()
47 extern size_t DestdirLen;
50 # define MANIFEST_SKIP Skip
51 # undef VE_DEBUG_LEVEL
55 * We sometimes need to know if input is verified or not.
56 * The extra slot is for tracking most recently opened.
58 static int ve_status[SOPEN_MAX+1];
59 static int ve_status_state;
61 struct verify_status *verified_files = NULL;
62 static int loaded_manifests = 0; /* have we loaded anything? */
64 #define VE_STATUS_NONE 1
65 #define VE_STATUS_VALID 2
68 * @brief set ve status for fd
71 ve_status_set(int fd, int ves)
73 if (fd >= 0 && fd < SOPEN_MAX) {
75 ve_status_state = VE_STATUS_VALID;
77 ve_status[SOPEN_MAX] = ves;
81 * @brief get ve status of fd
83 * What we return depends on ve_status_state.
86 * @li ve_status[fd] if ve_status_state is valid
87 * @li ve_status[SOPEN_MAX] if ve_status_state is none
88 * @li VE_NOT_CHECKED if ve_status_state uninitialized
93 if (!ve_status_state) {
94 return (VE_NOT_CHECKED);
96 if (ve_status_state == VE_STATUS_VALID &&
97 fd >= 0 && fd < SOPEN_MAX)
98 return (ve_status[fd]);
99 return (ve_status[SOPEN_MAX]); /* most recent */
103 * @brief track verify status
105 * occasionally loader will make multiple calls
106 * for the same file, we need only check it once.
108 struct verify_status {
112 struct verify_status *vs_next;
116 is_verified(struct stat *stp)
118 struct verify_status *vsp;
120 if (stp->st_ino > 0) {
121 for (vsp = verified_files; vsp != NULL; vsp = vsp->vs_next) {
122 if (stp->st_dev == vsp->vs_dev &&
123 stp->st_ino == vsp->vs_ino)
124 return (vsp->vs_status);
127 return (VE_NOT_CHECKED);
130 /* most recent first, since most likely to see repeated calls. */
132 add_verify_status(struct stat *stp, int status)
134 struct verify_status *vsp;
136 vsp = malloc(sizeof(struct verify_status));
137 vsp->vs_next = verified_files;
138 vsp->vs_dev = stp->st_dev;
139 vsp->vs_ino = stp->st_ino;
140 vsp->vs_status = status;
141 verified_files = vsp;
147 * load specified manifest if verified
150 load_manifest(const char *name, const char *prefix,
151 const char *skip, struct stat *stp)
158 rc = VE_FINGERPRINT_NONE;
163 if (stat(name, &st) < 0 || !S_ISREG(st.st_mode))
166 rc = is_verified(stp);
167 if (rc != VE_NOT_CHECKED) {
170 /* loader has no sense of time */
171 ve_utc_set(stp->st_mtime);
172 content = (char *)verify_signed(name, VEF_VERBOSE);
175 if (DestdirLen > 0 &&
176 strncmp(name, Destdir, DestdirLen) == 0) {
179 strncmp(prefix, Destdir, DestdirLen) == 0)
180 prefix += DestdirLen;
183 fingerprint_info_add(name, prefix, skip, content, stp);
184 add_verify_status(stp, VE_VERIFIED);
185 loaded_manifests = 1; /* we are verifying! */
186 DEBUG_PRINTF(3, ("loaded: %s %s %s\n",
187 name, prefix, skip));
190 rc = VE_FINGERPRINT_WRONG;
191 add_verify_status(stp, rc); /* remember */
198 find_manifest(const char *name)
201 char buf[MAXPATHLEN];
207 strncpy(buf, name, MAXPATHLEN - 1);
208 if (!(prefix = strrchr(buf, '/')))
211 prefix = strdup(buf);
212 rc = VE_FINGERPRINT_NONE;
213 for (tp = manifest_names; *tp; tp++) {
214 snprintf(buf, sizeof(buf), "%s/%s", prefix, *tp);
215 DEBUG_PRINTF(5, ("looking for %s\n", buf));
216 if (stat(buf, &st) == 0 && st.st_size > 0) {
217 #ifdef MANIFEST_SKIP_ALWAYS /* very unlikely */
218 skip = MANIFEST_SKIP_ALWAYS;
220 #ifdef MANIFEST_SKIP /* rare */
222 skip = MANIFEST_SKIP;
227 rc = load_manifest(buf, skip ? prefix : NULL,
237 #ifdef LOADER_VERIEXEC_TESTING
238 # define ACCEPT_NO_FP_DEFAULT VE_MUST + 1
240 # define ACCEPT_NO_FP_DEFAULT VE_MUST
242 #ifndef VE_VERBOSE_DEFAULT
243 # define VE_VERBOSE_DEFAULT 0
247 severity_guess(const char *filename)
251 /* Some files like *.conf and *.hints may be unsigned */
252 if ((cp = strrchr(filename, '.'))) {
253 if (strcmp(cp, ".conf") == 0 ||
254 strcmp(cp, ".cookie") == 0 ||
255 strcmp(cp, ".hints") == 0)
261 static int Verifying = -1; /* 0 if not verifying */
264 verify_tweak(int fd, off_t off, struct stat *stp,
265 char *tweak, int *accept_no_fp,
268 if (strcmp(tweak, "off") == 0) {
270 } else if (strcmp(tweak, "strict") == 0) {
271 /* anything caller wants verified must be */
272 *accept_no_fp = VE_WANT;
273 *verbose = 1; /* warn of anything unverified */
274 /* treat self test failure as fatal */
275 if (!ve_self_tests()) {
276 panic("verify self tests failed");
278 } else if (strcmp(tweak, "modules") == 0) {
279 /* modules/kernel must be verified */
280 *accept_no_fp = VE_MUST;
281 } else if (strcmp(tweak, "try") == 0) {
282 /* best effort: always accept no fp */
283 *accept_no_fp = VE_MUST + 1;
284 } else if (strcmp(tweak, "verbose") == 0) {
286 } else if (strcmp(tweak, "quiet") == 0) {
288 } else if (strncmp(tweak, "trust", 5) == 0) {
289 /* content is trust anchor to add or revoke */
294 lseek(fd, 0, SEEK_SET);
295 ucp = read_fd(fd, stp->st_size);
298 if (strstr(tweak, "revoke")) {
299 num = ve_trust_anchors_revoke(ucp, stp->st_size);
300 DEBUG_PRINTF(3, ("revoked %d trust anchors\n",
303 num = ve_trust_anchors_add_buf(ucp, stp->st_size);
304 DEBUG_PRINTF(3, ("added %d trust anchors\n",
310 #ifndef VE_DEBUG_LEVEL
311 # define VE_DEBUG_LEVEL 0
315 getenv_int(const char *var, int def)
324 val = strtol(cp, &ep, 0);
325 if ((ep && *ep) || val != (int)val) {
334 * @brief prepare to verify an open file
339 * @param[in] filename
340 * path we opened and will use to lookup fingerprint
343 * stat pointer so we can check file type
346 verify_prep(int fd, const char *filename, off_t off, struct stat *stp,
352 Verifying = ve_trust_init();
354 ve_debug_set(getenv_int("VE_DEBUG_LEVEL", VE_DEBUG_LEVEL));
356 /* initialize ve_status with default result */
357 rc = Verifying ? VE_NOT_CHECKED : VE_NOT_VERIFYING;
358 ve_status_set(0, rc);
359 ve_status_state = VE_STATUS_NONE;
362 ve_anchor_verbose_set(1);
365 if (!Verifying || fd < 0)
368 if (fstat(fd, stp) < 0 || !S_ISREG(stp->st_mode))
372 ("verify_prep: caller=%s,fd=%d,name='%s',off=%lld,dev=%lld,ino=%lld\n",
373 caller, fd, filename, (long long)off, (long long)stp->st_dev,
374 (long long)stp->st_ino));
375 rc = is_verified(stp);
376 DEBUG_PRINTF(4,("verify_prep: is_verified()->%d\n", rc));
377 if (rc == VE_NOT_CHECKED) {
378 rc = find_manifest(filename);
380 ve_status_set(fd, rc);
386 * @brief verify an open file
391 * @param[in] filename
392 * path we opened and will use to lookup fingerprint
395 * current offset in fd, must be restored on return
397 * @param[in] severity
398 * indicator of how to handle case of missing fingerprint
400 * We look for a signed manifest relative to the filename
401 * just opened and verify/load it if needed.
403 * We then use verify_fd() in libve to actually verify that hash for
404 * open file. If it returns < 0 we look at the severity arg to decide
405 * what to do about it.
407 * If verify_fd() returns VE_FINGERPRINT_NONE we accept it if severity
410 * @return >= 0 on success < 0 on failure
413 verify_file(int fd, const char *filename, off_t off, int severity,
417 static int accept_no_fp = ACCEPT_NO_FP_DEFAULT;
418 static int verbose = VE_VERBOSE_DEFAULT;
423 rc = verify_prep(fd, filename, off, &st, caller);
430 verbose = getenv_int("VE_VERBOSE", VE_VERBOSE_DEFAULT);
433 if (rc != VE_FINGERPRINT_WRONG && loaded_manifests) {
434 if (severity <= VE_GUESS)
435 severity = severity_guess(filename);
436 #ifdef VE_PCR_SUPPORT
438 * Only update pcr with things that must verify
439 * these tend to be processed in a more deterministic
440 * order, which makes our pseudo pcr more useful.
442 ve_pcr_updating_set((severity == VE_MUST));
445 if (DestdirLen > 0 &&
446 strncmp(filename, Destdir, DestdirLen) == 0) {
447 filename += DestdirLen;
450 if ((rc = verify_fd(fd, filename, off, &st)) >= 0) {
451 if (verbose || severity > VE_WANT) {
452 #if defined(VE_DEBUG_LEVEL) && VE_DEBUG_LEVEL > 0
453 printf("%serified %s %llu,%llu\n",
454 (rc == VE_FINGERPRINT_IGNORE) ? "Unv" : "V",
456 (long long)st.st_dev, (long long)st.st_ino);
458 printf("%serified %s\n",
459 (rc == VE_FINGERPRINT_IGNORE) ? "Unv" : "V",
463 if (severity < VE_MUST) { /* not a kernel or module */
464 if ((cp = strrchr(filename, '/'))) {
466 if (strncmp(cp, "loader.ve.", 10) == 0) {
468 verify_tweak(fd, off, &st, cp,
469 &accept_no_fp, &verbose);
473 add_verify_status(&st, rc);
474 ve_status_set(fd, rc);
478 if (severity || verbose || rc == VE_FINGERPRINT_WRONG)
479 printf("Unverified: %s\n", ve_error_get());
480 if (rc == VE_FINGERPRINT_UNKNOWN && severity < VE_MUST)
481 rc = VE_UNVERIFIED_OK;
482 else if (rc == VE_FINGERPRINT_NONE && severity < accept_no_fp)
483 rc = VE_UNVERIFIED_OK;
485 add_verify_status(&st, rc);
487 #ifdef LOADER_VERIEXEC_TESTING
488 else if (rc != VE_FINGERPRINT_WRONG) {
490 * We have not loaded any manifest and
491 * not because of verication failure.
492 * Most likely reason is we have none.
493 * Allow boot to proceed if we are just testing.
495 return (VE_UNVERIFIED_OK);
498 if (rc == VE_FINGERPRINT_WRONG && severity > accept_no_fp)
499 panic("cannot continue");
500 ve_status_set(fd, rc);
505 * @brief get hex string for pcr value and export
507 * In case we are doing measured boot, provide
508 * value of the "pcr" data we have accumulated.
511 verify_pcr_export(void)
513 #ifdef VE_PCR_SUPPORT
514 char hexbuf[br_sha256_SIZE * 2 + 2];
515 unsigned char hbuf[br_sha256_SIZE];
520 hlen = ve_pcr_get(hbuf, sizeof(hbuf));
522 hex = hexdigest(hexbuf, sizeof(hexbuf), hbuf, hlen);
524 hex[hlen*2] = '\0'; /* clobber newline */
525 setenv("loader.ve.pcr", hex, 1);
527 ("%s: setenv(loader.ve.pcr, %s\n", __func__,
529 hinfo = ve_pcr_hashed_get(1);
531 setenv("loader.ve.hashed", hinfo, 1);
533 ("%s: setenv(loader.ve.hashed, %s\n",