2 * Copyright (c) 2017-2020, 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)
258 if (strcmp(cp, ".4th") == 0 ||
259 strcmp(cp, ".lua") == 0 ||
260 strcmp(cp, ".rc") == 0)
266 static int Verifying = -1; /* 0 if not verifying */
269 verify_tweak(int fd, off_t off, struct stat *stp,
270 char *tweak, int *accept_no_fp,
273 if (strcmp(tweak, "off") == 0) {
275 } else if (strcmp(tweak, "strict") == 0) {
276 /* anything caller wants verified must be */
277 *accept_no_fp = VE_WANT;
278 *verbose = 1; /* warn of anything unverified */
279 /* treat self test failure as fatal */
280 if (!ve_self_tests()) {
281 panic("verify self tests failed");
283 } else if (strcmp(tweak, "modules") == 0) {
284 /* modules/kernel must be verified */
285 *accept_no_fp = VE_MUST;
286 } else if (strcmp(tweak, "try") == 0) {
287 /* best effort: always accept no fp */
288 *accept_no_fp = VE_MUST + 1;
289 } else if (strcmp(tweak, "verbose") == 0) {
291 } else if (strcmp(tweak, "quiet") == 0) {
293 } else if (strncmp(tweak, "trust", 5) == 0) {
294 /* content is trust anchor to add or revoke */
299 lseek(fd, 0, SEEK_SET);
300 ucp = read_fd(fd, stp->st_size);
303 if (strstr(tweak, "revoke")) {
304 num = ve_trust_anchors_revoke(ucp, stp->st_size);
305 DEBUG_PRINTF(3, ("revoked %d trust anchors\n",
308 num = ve_trust_anchors_add_buf(ucp, stp->st_size);
309 DEBUG_PRINTF(3, ("added %d trust anchors\n",
315 #ifndef VE_DEBUG_LEVEL
316 # define VE_DEBUG_LEVEL 0
320 getenv_int(const char *var, int def)
329 val = strtol(cp, &ep, 0);
330 if ((ep && *ep) || val != (int)val) {
339 * @brief prepare to verify an open file
344 * @param[in] filename
345 * path we opened and will use to lookup fingerprint
348 * stat pointer so we can check file type
351 verify_prep(int fd, const char *filename, off_t off, struct stat *stp,
357 Verifying = ve_trust_init();
359 ve_debug_set(getenv_int("VE_DEBUG_LEVEL", VE_DEBUG_LEVEL));
361 /* initialize ve_status with default result */
362 rc = Verifying ? VE_NOT_CHECKED : VE_NOT_VERIFYING;
363 ve_status_set(0, rc);
364 ve_status_state = VE_STATUS_NONE;
367 ve_anchor_verbose_set(1);
370 if (!Verifying || fd < 0)
373 if (fstat(fd, stp) < 0 || !S_ISREG(stp->st_mode))
377 ("verify_prep: caller=%s,fd=%d,name='%s',off=%lld,dev=%lld,ino=%lld\n",
378 caller, fd, filename, (long long)off, (long long)stp->st_dev,
379 (long long)stp->st_ino));
380 rc = is_verified(stp);
381 DEBUG_PRINTF(4,("verify_prep: is_verified()->%d\n", rc));
382 if (rc == VE_NOT_CHECKED) {
383 rc = find_manifest(filename);
385 ve_status_set(fd, rc);
391 * @brief verify an open file
396 * @param[in] filename
397 * path we opened and will use to lookup fingerprint
400 * current offset in fd, must be restored on return
402 * @param[in] severity
403 * indicator of how to handle case of missing fingerprint
405 * We look for a signed manifest relative to the filename
406 * just opened and verify/load it if needed.
408 * We then use verify_fd() in libve to actually verify that hash for
409 * open file. If it returns < 0 we look at the severity arg to decide
410 * what to do about it.
412 * If verify_fd() returns VE_FINGERPRINT_NONE we accept it if severity
415 * @return >= 0 on success < 0 on failure
418 verify_file(int fd, const char *filename, off_t off, int severity,
422 static int accept_no_fp = ACCEPT_NO_FP_DEFAULT;
423 static int verbose = VE_VERBOSE_DEFAULT;
428 rc = verify_prep(fd, filename, off, &st, caller);
435 verbose = getenv_int("VE_VERBOSE", VE_VERBOSE_DEFAULT);
438 if (rc != VE_FINGERPRINT_WRONG && loaded_manifests) {
439 if (severity <= VE_GUESS)
440 severity = severity_guess(filename);
441 #ifdef VE_PCR_SUPPORT
443 * Only update pcr with things that must verify
444 * these tend to be processed in a more deterministic
445 * order, which makes our pseudo pcr more useful.
447 ve_pcr_updating_set((severity == VE_MUST));
450 if (DestdirLen > 0 &&
451 strncmp(filename, Destdir, DestdirLen) == 0) {
452 filename += DestdirLen;
455 if ((rc = verify_fd(fd, filename, off, &st)) >= 0) {
456 if (verbose || severity > VE_WANT) {
457 #if defined(VE_DEBUG_LEVEL) && VE_DEBUG_LEVEL > 0
458 printf("%serified %s %llu,%llu\n",
459 (rc == VE_FINGERPRINT_IGNORE) ? "Unv" : "V",
461 (long long)st.st_dev, (long long)st.st_ino);
463 printf("%serified %s\n",
464 (rc == VE_FINGERPRINT_IGNORE) ? "Unv" : "V",
468 if (severity < VE_MUST) { /* not a kernel or module */
469 if ((cp = strrchr(filename, '/'))) {
471 if (strncmp(cp, "loader.ve.", 10) == 0) {
473 verify_tweak(fd, off, &st, cp,
474 &accept_no_fp, &verbose);
478 add_verify_status(&st, rc);
479 ve_status_set(fd, rc);
483 if (severity || verbose || rc == VE_FINGERPRINT_WRONG)
484 printf("Unverified: %s\n", ve_error_get());
485 if (rc == VE_FINGERPRINT_UNKNOWN && severity < VE_MUST)
486 rc = VE_UNVERIFIED_OK;
487 else if (rc == VE_FINGERPRINT_NONE && severity < accept_no_fp)
488 rc = VE_UNVERIFIED_OK;
490 add_verify_status(&st, rc);
492 #ifdef LOADER_VERIEXEC_TESTING
493 else if (rc != VE_FINGERPRINT_WRONG) {
495 * We have not loaded any manifest and
496 * not because of verication failure.
497 * Most likely reason is we have none.
498 * Allow boot to proceed if we are just testing.
500 return (VE_UNVERIFIED_OK);
503 if (rc == VE_FINGERPRINT_WRONG && severity > accept_no_fp)
504 panic("cannot continue");
505 ve_status_set(fd, rc);
510 * @brief get hex string for pcr value and export
512 * In case we are doing measured boot, provide
513 * value of the "pcr" data we have accumulated.
516 verify_pcr_export(void)
518 #ifdef VE_PCR_SUPPORT
519 char hexbuf[br_sha256_SIZE * 2 + 2];
520 unsigned char hbuf[br_sha256_SIZE];
525 hlen = ve_pcr_get(hbuf, sizeof(hbuf));
527 hex = hexdigest(hexbuf, sizeof(hexbuf), hbuf, hlen);
529 hex[hlen*2] = '\0'; /* clobber newline */
530 setenv("loader.ve.pcr", hex, 1);
532 ("%s: setenv(loader.ve.pcr, %s\n", __func__,
534 hinfo = ve_pcr_hashed_get(1);
536 setenv("loader.ve.hashed", hinfo, 1);
538 ("%s: setenv(loader.ve.hashed, %s\n",
540 if ((hlen = strlen(hinfo)) > KENV_MVALLEN) {
543 * roundup to multiple of KENV_MVALLEN
547 hlen += KENV_MVALLEN -
548 (hlen % KENV_MVALLEN);
549 if (snprintf(mvallen, sizeof(mvallen),
550 "%d", (int) hlen) < sizeof(mvallen))
551 setenv("kenv_mvallen", mvallen, 1);