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()
48 # define MANIFEST_SKIP Skip
49 # undef VE_DEBUG_LEVEL
53 * We sometimes need to know if input is verified or not.
54 * The extra slot is for tracking most recently opened.
56 static int ve_status[SOPEN_MAX+1];
57 static int ve_status_state;
59 struct verify_status *verified_files = NULL;
60 static int loaded_manifests = 0; /* have we loaded anything? */
62 #define VE_STATUS_NONE 1
63 #define VE_STATUS_VALID 2
66 * @brief set ve status for fd
69 ve_status_set(int fd, int ves)
71 if (fd >= 0 && fd < SOPEN_MAX) {
73 ve_status_state = VE_STATUS_VALID;
75 ve_status[SOPEN_MAX] = ves;
79 * @brief get ve status of fd
81 * What we return depends on ve_status_state.
84 * @li ve_status[fd] if ve_status_state is valid
85 * @li ve_status[SOPEN_MAX] if ve_status_state is none
86 * @li VE_NOT_CHECKED if ve_status_state uninitialized
91 if (!ve_status_state) {
92 return (VE_NOT_CHECKED);
94 if (ve_status_state == VE_STATUS_VALID &&
95 fd >= 0 && fd < SOPEN_MAX)
96 return (ve_status[fd]);
97 return (ve_status[SOPEN_MAX]); /* most recent */
101 * @brief track verify status
103 * occasionally loader will make multiple calls
104 * for the same file, we need only check it once.
106 struct verify_status {
110 struct verify_status *vs_next;
114 is_verified(struct stat *stp)
116 struct verify_status *vsp;
118 for (vsp = verified_files; vsp != NULL; vsp = vsp->vs_next) {
119 if (stp->st_dev == vsp->vs_dev &&
120 stp->st_ino == vsp->vs_ino)
121 return (vsp->vs_status);
123 return (VE_NOT_CHECKED);
126 /* most recent first, since most likely to see repeated calls. */
128 add_verify_status(struct stat *stp, int status)
130 struct verify_status *vsp;
132 vsp = malloc(sizeof(struct verify_status));
133 vsp->vs_next = verified_files;
134 vsp->vs_dev = stp->st_dev;
135 vsp->vs_ino = stp->st_ino;
136 vsp->vs_status = status;
137 verified_files = vsp;
143 * load specified manifest if verified
146 load_manifest(const char *name, const char *prefix,
147 const char *skip, struct stat *stp)
154 rc = VE_FINGERPRINT_NONE;
159 if (stat(name, &st) < 0 || !S_ISREG(st.st_mode))
162 rc = is_verified(stp);
163 if (rc != VE_NOT_CHECKED) {
166 /* loader has no sense of time */
167 ve_utc_set(stp->st_mtime);
168 content = (char *)verify_signed(name, VEF_VERBOSE);
170 fingerprint_info_add(name, prefix, skip, content, stp);
171 add_verify_status(stp, VE_VERIFIED);
172 loaded_manifests = 1; /* we are verifying! */
173 DEBUG_PRINTF(3, ("loaded: %s %s %s\n",
174 name, prefix, skip));
177 rc = VE_FINGERPRINT_WRONG;
178 add_verify_status(stp, rc); /* remember */
185 find_manifest(const char *name)
188 char buf[MAXPATHLEN];
194 strncpy(buf, name, MAXPATHLEN - 1);
195 if (!(prefix = strrchr(buf, '/')))
198 prefix = strdup(buf);
199 rc = VE_FINGERPRINT_NONE;
200 for (tp = manifest_names; *tp; tp++) {
201 snprintf(buf, sizeof(buf), "%s/%s", prefix, *tp);
202 DEBUG_PRINTF(5, ("looking for %s\n", buf));
203 if (stat(buf, &st) == 0 && st.st_size > 0) {
204 #ifdef MANIFEST_SKIP_ALWAYS /* very unlikely */
205 skip = MANIFEST_SKIP_ALWAYS;
207 #ifdef MANIFEST_SKIP /* rare */
209 skip = MANIFEST_SKIP;
214 rc = load_manifest(buf, skip ? prefix : NULL,
224 #ifdef LOADER_VERIEXEC_TESTING
225 # define ACCEPT_NO_FP_DEFAULT VE_MUST + 1
227 # define ACCEPT_NO_FP_DEFAULT VE_MUST
229 #ifndef VE_VERBOSE_DEFAULT
230 # define VE_VERBOSE_DEFAULT 0
234 severity_guess(const char *filename)
238 /* Some files like *.conf and *.hints may be unsigned */
239 if ((cp = strrchr(filename, '.'))) {
240 if (strcmp(cp, ".conf") == 0 ||
241 strcmp(cp, ".cookie") == 0 ||
242 strcmp(cp, ".hints") == 0)
249 verify_tweak(int fd, off_t off, struct stat *stp,
250 char *tweak, int *accept_no_fp,
251 int *verbose, int *verifying)
253 if (strcmp(tweak, "off") == 0) {
255 } else if (strcmp(tweak, "strict") == 0) {
256 /* anything caller wants verified must be */
257 *accept_no_fp = VE_WANT;
258 *verbose = 1; /* warn of anything unverified */
259 /* treat self test failure as fatal */
260 if (!ve_self_tests()) {
261 panic("verify self tests failed");
263 } else if (strcmp(tweak, "modules") == 0) {
264 /* modules/kernel must be verified */
265 *accept_no_fp = VE_MUST;
266 } else if (strcmp(tweak, "try") == 0) {
267 /* best effort: always accept no fp */
268 *accept_no_fp = VE_MUST + 1;
269 } else if (strcmp(tweak, "verbose") == 0) {
271 } else if (strcmp(tweak, "quiet") == 0) {
273 } else if (strncmp(tweak, "trust", 5) == 0) {
274 /* content is trust anchor to add or revoke */
279 lseek(fd, 0, SEEK_SET);
280 ucp = read_fd(fd, stp->st_size);
283 if (strstr(tweak, "revoke")) {
284 num = ve_trust_anchors_revoke(ucp, stp->st_size);
285 DEBUG_PRINTF(3, ("revoked %d trust anchors\n",
288 num = ve_trust_anchors_add_buf(ucp, stp->st_size);
289 DEBUG_PRINTF(3, ("added %d trust anchors\n",
296 * @brief verify an open file
301 * @param[in] filename
302 * path we opened and will use to lookup fingerprint
305 * current offset in fd, must be restored on return
307 * @param[in] severity
308 * indicator of how to handle case of missing fingerprint
310 * We look for a signed manifest relative to the filename
311 * just opened and verify/load it if needed.
313 * We then use verify_fd() in libve to actually verify that hash for
314 * open file. If it returns < 0 we look at the severity arg to decide
315 * what to do about it.
317 * If verify_fd() returns VE_FINGERPRINT_NONE we accept it if severity
320 * @return >= 0 on success < 0 on failure
323 verify_file(int fd, const char *filename, off_t off, int severity)
325 static int verifying = -1;
326 static int accept_no_fp = ACCEPT_NO_FP_DEFAULT;
327 static int verbose = VE_VERBOSE_DEFAULT;
333 verifying = ve_trust_init();
334 #ifdef VE_DEBUG_LEVEL
335 ve_debug_set(VE_DEBUG_LEVEL);
337 /* initialize ve_status with default result */
338 rc = verifying ? VE_NOT_CHECKED : VE_NOT_VERIFYING;
339 ve_status_set(0, rc);
340 ve_status_state = VE_STATUS_NONE;
343 ve_anchor_verbose_set(1);
349 if (fd < 0 || fstat(fd, &st) < 0 || !S_ISREG(st.st_mode))
352 DEBUG_PRINTF(3, ("fd=%d,name='%s',off=%lld,dev=%lld,ino=%lld\n",
353 fd, filename, (long long)off, (long long)st.st_dev,
354 (long long)st.st_ino));
357 rc = is_verified(&st);
358 if (rc != VE_NOT_CHECKED) {
359 ve_status_set(fd, rc);
362 rc = find_manifest(filename);
363 if (rc != VE_FINGERPRINT_WRONG && loaded_manifests) {
364 if (severity <= VE_GUESS)
365 severity = severity_guess(filename);
366 #ifdef VE_PCR_SUPPORT
368 * Only update pcr with things that must verify
369 * these tend to be processed in a more deterministic
370 * order, which makes our pseudo pcr more useful.
372 ve_pcr_updating_set((severity == VE_MUST));
374 if ((rc = verify_fd(fd, filename, off, &st)) >= 0) {
375 if (verbose || severity > VE_WANT) {
376 #if defined(VE_DEBUG_LEVEL) && VE_DEBUG_LEVEL > 0
377 printf("%serified %s %llu,%llu\n",
378 (rc == VE_FINGERPRINT_IGNORE) ? "Unv" : "V",
380 (long long)st.st_dev, (long long)st.st_ino);
382 printf("%serified %s\n",
383 (rc == VE_FINGERPRINT_IGNORE) ? "Unv" : "V",
387 if (severity < VE_MUST) { /* not a kernel or module */
389 if ((cp = strrchr(filename, '/'))) {
391 if (strncmp(cp, "loader.ve.", 10) == 0) {
393 verify_tweak(fd, off, &st, cp,
394 &accept_no_fp, &verbose,
399 add_verify_status(&st, rc);
400 ve_status_set(fd, rc);
404 if (severity || verbose || rc == VE_FINGERPRINT_WRONG)
405 printf("Unverified: %s\n", ve_error_get());
406 if (rc == VE_FINGERPRINT_UNKNOWN && severity < VE_MUST)
407 rc = VE_UNVERIFIED_OK;
408 else if (rc == VE_FINGERPRINT_NONE && severity < accept_no_fp)
409 rc = VE_UNVERIFIED_OK;
411 add_verify_status(&st, rc);
413 #ifdef LOADER_VERIEXEC_TESTING
414 else if (rc != VE_FINGERPRINT_WRONG) {
416 * We have not loaded any manifest and
417 * not because of verication failure.
418 * Most likely reason is we have none.
419 * Allow boot to proceed if we are just testing.
421 return (VE_UNVERIFIED_OK);
424 if (rc == VE_FINGERPRINT_WRONG && severity > accept_no_fp)
425 panic("cannot continue");
426 ve_status_set(fd, rc);
431 * @brief get hex string for pcr value and export
433 * In case we are doing measured boot, provide
434 * value of the "pcr" data we have accumulated.
437 verify_pcr_export(void)
439 #ifdef VE_PCR_SUPPORT
440 char hexbuf[br_sha256_SIZE * 2 + 2];
441 unsigned char hbuf[br_sha256_SIZE];
445 hlen = ve_pcr_get(hbuf, sizeof(hbuf));
447 hex = hexdigest(hexbuf, sizeof(hexbuf), hbuf, hlen);
449 hex[hlen*2] = '\0'; /* clobber newline */
450 setenv("loader.ve.pcr", hex, 1);