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>
36 #include "libsecureboot.h"
37 #include <verify_file.h>
38 #include <manifests.h>
44 * define MANIFEST_SKIP to Skip - in tests/tvo.c so that
45 * tvo can control the value we use in find_manifest()
48 extern size_t DestdirLen;
51 # define MANIFEST_SKIP Skip
52 # undef VE_DEBUG_LEVEL
56 * We sometimes need to know if input is verified or not.
57 * The extra slot is for tracking most recently opened.
59 static int ve_status[SOPEN_MAX+1];
60 static int ve_status_state;
62 struct verify_status *verified_files = NULL;
63 static int loaded_manifests = 0; /* have we loaded anything? */
65 #define VE_STATUS_NONE 1
66 #define VE_STATUS_VALID 2
69 * @brief set ve status for fd
72 ve_status_set(int fd, int ves)
74 if (fd >= 0 && fd < SOPEN_MAX) {
76 ve_status_state = VE_STATUS_VALID;
78 ve_status[SOPEN_MAX] = ves;
82 * @brief get ve status of fd
84 * What we return depends on ve_status_state.
87 * @li ve_status[fd] if ve_status_state is valid
88 * @li ve_status[SOPEN_MAX] if ve_status_state is none
89 * @li VE_NOT_CHECKED if ve_status_state uninitialized
94 if (!ve_status_state) {
95 return (VE_NOT_CHECKED);
97 if (ve_status_state == VE_STATUS_VALID &&
98 fd >= 0 && fd < SOPEN_MAX)
99 return (ve_status[fd]);
100 return (ve_status[SOPEN_MAX]); /* most recent */
104 * @brief track verify status
106 * occasionally loader will make multiple calls
107 * for the same file, we need only check it once.
109 struct verify_status {
113 struct verify_status *vs_next;
117 is_verified(struct stat *stp)
119 struct verify_status *vsp;
121 if (stp->st_ino > 0) {
122 for (vsp = verified_files; vsp != NULL; vsp = vsp->vs_next) {
123 if (stp->st_dev == vsp->vs_dev &&
124 stp->st_ino == vsp->vs_ino)
125 return (vsp->vs_status);
128 return (VE_NOT_CHECKED);
131 /* most recent first, since most likely to see repeated calls. */
133 add_verify_status(struct stat *stp, int status)
135 struct verify_status *vsp;
137 vsp = malloc(sizeof(struct verify_status));
138 vsp->vs_next = verified_files;
139 vsp->vs_dev = stp->st_dev;
140 vsp->vs_ino = stp->st_ino;
141 vsp->vs_status = status;
142 verified_files = vsp;
148 * load specified manifest if verified
151 load_manifest(const char *name, const char *prefix,
152 const char *skip, struct stat *stp)
159 rc = VE_FINGERPRINT_NONE;
164 if (stat(name, &st) < 0 || !S_ISREG(st.st_mode))
167 rc = is_verified(stp);
168 if (rc != VE_NOT_CHECKED) {
171 /* loader has no sense of time */
172 ve_utc_set(stp->st_mtime);
173 content = (char *)verify_signed(name, VEF_VERBOSE);
176 if (DestdirLen > 0 &&
177 strncmp(name, Destdir, DestdirLen) == 0) {
180 strncmp(prefix, Destdir, DestdirLen) == 0)
181 prefix += DestdirLen;
184 fingerprint_info_add(name, prefix, skip, content, stp);
185 add_verify_status(stp, VE_VERIFIED);
186 loaded_manifests = 1; /* we are verifying! */
187 DEBUG_PRINTF(3, ("loaded: %s %s %s\n",
188 name, prefix, skip));
191 rc = VE_FINGERPRINT_WRONG;
192 add_verify_status(stp, rc); /* remember */
199 find_manifest(const char *name)
202 char buf[MAXPATHLEN];
208 strncpy(buf, name, MAXPATHLEN - 1);
209 if (!(prefix = strrchr(buf, '/')))
212 prefix = strdup(buf);
213 rc = VE_FINGERPRINT_NONE;
214 for (tp = manifest_names; *tp; tp++) {
215 snprintf(buf, sizeof(buf), "%s/%s", prefix, *tp);
216 DEBUG_PRINTF(5, ("looking for %s\n", buf));
217 if (stat(buf, &st) == 0 && st.st_size > 0) {
218 #ifdef MANIFEST_SKIP_ALWAYS /* very unlikely */
219 skip = MANIFEST_SKIP_ALWAYS;
221 #ifdef MANIFEST_SKIP /* rare */
223 skip = MANIFEST_SKIP;
228 rc = load_manifest(buf, skip ? prefix : NULL,
238 #ifdef LOADER_VERIEXEC_TESTING
239 # define ACCEPT_NO_FP_DEFAULT VE_MUST + 1
241 # define ACCEPT_NO_FP_DEFAULT VE_MUST
243 #ifndef VE_VERBOSE_DEFAULT
244 # define VE_VERBOSE_DEFAULT 0
248 severity_guess(const char *filename)
252 /* Some files like *.conf and *.hints may be unsigned */
253 if ((cp = strrchr(filename, '.'))) {
254 if (strcmp(cp, ".conf") == 0 ||
255 strcmp(cp, ".cookie") == 0 ||
256 strcmp(cp, ".hints") == 0)
262 static int Verifying = -1; /* 0 if not verifying */
265 verify_tweak(int fd, off_t off, struct stat *stp,
266 char *tweak, int *accept_no_fp,
269 if (strcmp(tweak, "off") == 0) {
271 } else if (strcmp(tweak, "strict") == 0) {
272 /* anything caller wants verified must be */
273 *accept_no_fp = VE_WANT;
274 *verbose = 1; /* warn of anything unverified */
275 /* treat self test failure as fatal */
276 if (!ve_self_tests()) {
277 panic("verify self tests failed");
279 } else if (strcmp(tweak, "modules") == 0) {
280 /* modules/kernel must be verified */
281 *accept_no_fp = VE_MUST;
282 } else if (strcmp(tweak, "try") == 0) {
283 /* best effort: always accept no fp */
284 *accept_no_fp = VE_MUST + 1;
285 } else if (strcmp(tweak, "verbose") == 0) {
287 } else if (strcmp(tweak, "quiet") == 0) {
289 } else if (strncmp(tweak, "trust", 5) == 0) {
290 /* content is trust anchor to add or revoke */
295 lseek(fd, 0, SEEK_SET);
296 ucp = read_fd(fd, stp->st_size);
299 if (strstr(tweak, "revoke")) {
300 num = ve_trust_anchors_revoke(ucp, stp->st_size);
301 DEBUG_PRINTF(3, ("revoked %d trust anchors\n",
304 num = ve_trust_anchors_add_buf(ucp, stp->st_size);
305 DEBUG_PRINTF(3, ("added %d trust anchors\n",
311 #ifndef VE_DEBUG_LEVEL
312 # define VE_DEBUG_LEVEL 0
316 getenv_int(const char *var, int def)
325 val = strtol(cp, &ep, 0);
326 if ((ep && *ep) || val != (int)val) {
335 * @brief prepare to verify an open file
340 * @param[in] filename
341 * path we opened and will use to lookup fingerprint
344 * stat pointer so we can check file type
347 verify_prep(int fd, const char *filename, off_t off, struct stat *stp,
353 Verifying = ve_trust_init();
355 ve_debug_set(getenv_int("VE_DEBUG_LEVEL", VE_DEBUG_LEVEL));
357 /* initialize ve_status with default result */
358 rc = Verifying ? VE_NOT_CHECKED : VE_NOT_VERIFYING;
359 ve_status_set(0, rc);
360 ve_status_state = VE_STATUS_NONE;
363 ve_anchor_verbose_set(1);
366 if (!Verifying || fd < 0)
369 if (fstat(fd, stp) < 0 || !S_ISREG(stp->st_mode))
373 ("verify_prep: caller=%s,fd=%d,name='%s',off=%lld,dev=%lld,ino=%lld\n",
374 caller, fd, filename, (long long)off, (long long)stp->st_dev,
375 (long long)stp->st_ino));
376 rc = is_verified(stp);
377 DEBUG_PRINTF(4,("verify_prep: is_verified()->%d\n", rc));
378 if (rc == VE_NOT_CHECKED) {
379 rc = find_manifest(filename);
381 ve_status_set(fd, rc);
387 * @brief verify an open file
392 * @param[in] filename
393 * path we opened and will use to lookup fingerprint
396 * current offset in fd, must be restored on return
398 * @param[in] severity
399 * indicator of how to handle case of missing fingerprint
401 * We look for a signed manifest relative to the filename
402 * just opened and verify/load it if needed.
404 * We then use verify_fd() in libve to actually verify that hash for
405 * open file. If it returns < 0 we look at the severity arg to decide
406 * what to do about it.
408 * If verify_fd() returns VE_FINGERPRINT_NONE we accept it if severity
411 * @return >= 0 on success < 0 on failure
414 verify_file(int fd, const char *filename, off_t off, int severity,
418 static int accept_no_fp = ACCEPT_NO_FP_DEFAULT;
419 static int verbose = VE_VERBOSE_DEFAULT;
424 rc = verify_prep(fd, filename, off, &st, caller);
431 verbose = getenv_int("VE_VERBOSE", VE_VERBOSE_DEFAULT);
434 if (rc != VE_FINGERPRINT_WRONG && loaded_manifests) {
435 if (severity <= VE_GUESS)
436 severity = severity_guess(filename);
437 #ifdef VE_PCR_SUPPORT
439 * Only update pcr with things that must verify
440 * these tend to be processed in a more deterministic
441 * order, which makes our pseudo pcr more useful.
443 ve_pcr_updating_set((severity == VE_MUST));
446 if (DestdirLen > 0 &&
447 strncmp(filename, Destdir, DestdirLen) == 0) {
448 filename += DestdirLen;
451 if ((rc = verify_fd(fd, filename, off, &st)) >= 0) {
452 if (verbose || severity > VE_WANT) {
453 #if defined(VE_DEBUG_LEVEL) && VE_DEBUG_LEVEL > 0
454 printf("%serified %s %llu,%llu\n",
455 (rc == VE_FINGERPRINT_IGNORE) ? "Unv" : "V",
457 (long long)st.st_dev, (long long)st.st_ino);
459 printf("%serified %s\n",
460 (rc == VE_FINGERPRINT_IGNORE) ? "Unv" : "V",
464 if (severity < VE_MUST) { /* not a kernel or module */
465 if ((cp = strrchr(filename, '/'))) {
467 if (strncmp(cp, "loader.ve.", 10) == 0) {
469 verify_tweak(fd, off, &st, cp,
470 &accept_no_fp, &verbose);
474 add_verify_status(&st, rc);
475 ve_status_set(fd, rc);
479 if (severity || verbose || rc == VE_FINGERPRINT_WRONG)
480 printf("Unverified: %s\n", ve_error_get());
481 if (rc == VE_FINGERPRINT_UNKNOWN && severity < VE_MUST)
482 rc = VE_UNVERIFIED_OK;
483 else if (rc == VE_FINGERPRINT_NONE && severity < accept_no_fp)
484 rc = VE_UNVERIFIED_OK;
486 add_verify_status(&st, rc);
488 #ifdef LOADER_VERIEXEC_TESTING
489 else if (rc != VE_FINGERPRINT_WRONG) {
491 * We have not loaded any manifest and
492 * not because of verication failure.
493 * Most likely reason is we have none.
494 * Allow boot to proceed if we are just testing.
496 return (VE_UNVERIFIED_OK);
499 if (rc == VE_FINGERPRINT_WRONG && severity > accept_no_fp)
500 panic("cannot continue");
501 ve_status_set(fd, rc);
506 * @brief get hex string for pcr value and export
508 * In case we are doing measured boot, provide
509 * value of the "pcr" data we have accumulated.
512 verify_pcr_export(void)
514 #ifdef VE_PCR_SUPPORT
515 char hexbuf[br_sha256_SIZE * 2 + 2];
516 unsigned char hbuf[br_sha256_SIZE];
521 hlen = ve_pcr_get(hbuf, sizeof(hbuf));
523 hex = hexdigest(hexbuf, sizeof(hexbuf), hbuf, hlen);
525 hex[hlen*2] = '\0'; /* clobber newline */
526 setenv("loader.ve.pcr", hex, 1);
528 ("%s: setenv(loader.ve.pcr, %s\n", __func__,
530 hinfo = ve_pcr_hashed_get(1);
532 setenv("loader.ve.hashed", hinfo, 1);
534 ("%s: setenv(loader.ve.hashed, %s\n",
536 if ((hlen = strlen(hinfo)) > KENV_MVALLEN) {
539 * roundup to multiple of KENV_MVALLEN
543 hlen += KENV_MVALLEN -
544 (hlen % KENV_MVALLEN);
545 if (snprintf(mvallen, sizeof(mvallen),
546 "%d", (int) hlen) < sizeof(mvallen))
547 setenv("kenv_mvallen", mvallen, 1);