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.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
27 #include <sys/queue.h>
29 #include "libsecureboot-priv.h"
32 struct fingerprint_info {
33 char *fi_prefix; /**< manifest entries relative to */
34 char *fi_skip; /**< manifest entries prefixed with */
35 const char *fi_data; /**< manifest data */
36 size_t fi_prefix_len; /**< length of prefix */
37 size_t fi_skip_len; /**< length of skip */
38 dev_t fi_dev; /**< device id */
39 LIST_ENTRY(fingerprint_info) entries;
42 static LIST_HEAD(, fingerprint_info) fi_list;
45 fingerprint_info_init(void)
57 * add manifest data to list
59 * list is kept sorted by longest prefix.
62 * path that all manifest entries are resolved via
65 * optional prefix within manifest entries which should be skipped
71 fingerprint_info_add(const char *filename, const char *prefix,
72 const char *skip, const char *data, struct stat *stp)
74 struct fingerprint_info *fip, *nfip, *lfip;
78 fingerprint_info_init();
79 nfip = malloc(sizeof(struct fingerprint_info));
81 nfip->fi_prefix = strdup(prefix);
87 nfip->fi_prefix = strdup(filename);
88 cp = strrchr(nfip->fi_prefix, '/');
89 if (cp == nfip->fi_prefix) {
94 free(nfip->fi_prefix);
99 /* collapse any trailing ..[/] */
101 while ((cp = strrchr(nfip->fi_prefix, '/')) > nfip->fi_prefix) {
102 if (cp[1] == '\0') { /* trailing "/" */
106 if (strcmp(&cp[1], "..") == 0) {
121 nfip->fi_dev = stp->st_dev;
123 nfip->fi_data = data;
124 nfip->fi_prefix_len = strlen(nfip->fi_prefix);
126 nfip->fi_skip_len = strlen(skip);
127 if (nfip->fi_skip_len)
128 nfip->fi_skip = strdup(skip);
130 nfip->fi_skip = NULL;
132 nfip->fi_skip = NULL;
133 nfip->fi_skip_len = 0;
136 if (LIST_EMPTY(&fi_list)) {
137 LIST_INSERT_HEAD(&fi_list, nfip, entries);
138 DEBUG_PRINTF(4, ("inserted %zu %s at head\n",
139 nfip->fi_prefix_len, nfip->fi_prefix));
142 LIST_FOREACH(fip, &fi_list, entries) {
143 if (nfip->fi_prefix_len >= fip->fi_prefix_len) {
144 LIST_INSERT_BEFORE(fip, nfip, entries);
145 DEBUG_PRINTF(4, ("inserted %zu %s before %zu %s\n",
146 nfip->fi_prefix_len, nfip->fi_prefix,
147 fip->fi_prefix_len, fip->fi_prefix));
152 LIST_INSERT_AFTER(lfip, nfip, entries);
153 DEBUG_PRINTF(4, ("inserted %zu %s after %zu %s\n",
154 nfip->fi_prefix_len, nfip->fi_prefix,
155 lfip->fi_prefix_len, lfip->fi_prefix));
158 #ifdef MANIFEST_SKIP_MAYBE
160 * Deal with old incompatible boot/manifest
161 * if fp[-1] is '/' and start of entry matches
162 * MANIFEST_SKIP_MAYBE, we want it.
165 maybe_skip(char *fp, struct fingerprint_info *fip, size_t *nplenp)
169 tp = fp - sizeof(MANIFEST_SKIP_MAYBE);
171 if (tp >= fip->fi_data) {
172 DEBUG_PRINTF(3, ("maybe: %.48s\n", tp));
173 if ((tp == fip->fi_data || tp[-1] == '\n') &&
174 strncmp(tp, MANIFEST_SKIP_MAYBE,
175 sizeof(MANIFEST_SKIP_MAYBE) - 1) == 0) {
177 *nplenp += sizeof(MANIFEST_SKIP_MAYBE);
185 fingerprint_info_lookup(int fd, const char *path)
187 char pbuf[MAXPATHLEN+1];
188 char nbuf[MAXPATHLEN+1];
190 struct fingerprint_info *fip;
191 char *cp, *ep, *fp, *np;
193 size_t n, plen, nlen, nplen;
196 fingerprint_info_init();
198 n = strlcpy(pbuf, path, sizeof(pbuf));
199 if (n >= sizeof(pbuf))
202 if (fstat(fd, &st) == 0)
206 * get the first entry - it will have longest prefix
207 * so we can can work out how to initially split path
209 fip = LIST_FIRST(&fi_list);
214 cp = &pbuf[fip->fi_prefix_len];
222 nlen = plen = 0; /* keep gcc quiet */
224 for ( ; cp >= pbuf && *cp != '/'; cp--)
248 DEBUG_PRINTF(2, ("looking for %s %zu %s\n", prefix, plen, cp));
250 LIST_FOREACH(fip, &fi_list, entries) {
251 DEBUG_PRINTF(4, ("at %zu %s\n",
252 fip->fi_prefix_len, fip->fi_prefix));
254 if (fip->fi_prefix_len < plen) {
255 DEBUG_PRINTF(3, ("skipping prefix=%s %zu %zu\n",
256 fip->fi_prefix, fip->fi_prefix_len,
260 if (fip->fi_prefix_len == plen) {
261 if (fip->fi_dev != 0 && fip->fi_dev != dev) {
263 "skipping dev=%ld != %ld\n",
268 if (strcmp(prefix, fip->fi_prefix)) {
270 "skipping prefix=%s\n",
274 DEBUG_PRINTF(3, ("checking prefix=%s\n",
276 if (fip->fi_skip_len) {
278 nplen = snprintf(nbuf, sizeof(nbuf),
281 nplen = MIN(nplen, sizeof(nbuf) - 1);
286 DEBUG_PRINTF(3, ("lookup: '%s'\n", np));
287 if (!(fp = strstr(fip->fi_data, np)))
289 #ifdef MANIFEST_SKIP_MAYBE
290 if (fip->fi_skip_len == 0 &&
291 fp > fip->fi_data && fp[-1] == '/') {
292 fp = maybe_skip(fp, fip, &nplen);
296 * when we find a match:
297 * fp[nplen] will be space and
298 * fp will be fip->fi_data or
301 if (!((fp == fip->fi_data || fp[-1] == '\n') &&
307 #ifdef MANIFEST_SKIP_MAYBE
308 if (fip->fi_skip_len == 0 &&
311 fp = maybe_skip(fp, fip, &nplen);
315 ("fp[-1]=%#x fp[%zu]=%#x fp=%.78s\n",
320 } while (fp != NULL &&
326 DEBUG_PRINTF(2, ("found %.78s\n", fp));
327 /* we have a match! */
328 for (cp = &fp[nplen]; *cp == ' '; cp++)
333 ("Ignoring prefix=%s\n", fip->fi_prefix));
336 } while (cp > &pbuf[1]);
342 verify_fingerprint(int fd, const char *path, const char *cp, off_t off)
344 unsigned char buf[PAGE_SIZE];
345 const br_hash_class *md;
346 br_hash_compat_context mctx;
350 if (strncmp(cp, "no_hash", 7) == 0) {
351 return (VE_FINGERPRINT_IGNORE);
352 } else if (strncmp(cp, "sha256=", 7) == 0) {
353 md = &br_sha256_vtable;
354 hlen = br_sha256_SIZE;
356 #ifdef VE_SHA1_SUPPORT
357 } else if (strncmp(cp, "sha1=", 5) == 0) {
358 md = &br_sha1_vtable;
362 #ifdef VE_SHA384_SUPPORT
363 } else if (strncmp(cp, "sha384=", 7) == 0) {
364 md = &br_sha384_vtable;
365 hlen = br_sha384_SIZE;
368 #ifdef VE_SHA512_SUPPORT
369 } else if (strncmp(cp, "sha512=", 7) == 0) {
370 md = &br_sha512_vtable;
371 hlen = br_sha512_SIZE;
375 ve_error_set("%s: no supported fingerprint", path);
376 return (VE_FINGERPRINT_UNKNOWN);
379 md->init(&mctx.vtable);
381 lseek(fd, 0, SEEK_SET);
383 n = read(fd, buf, sizeof(buf));
387 md->update(&mctx.vtable, buf, n);
389 lseek(fd, off, SEEK_SET);
390 return (ve_check_hash(&mctx, md, path, cp, hlen));
396 * verify an open file
407 * @return 0, VE_FINGERPRINT_OK or VE_FINGERPRINT_NONE, VE_FINGERPRINT_WRONG
410 verify_fd(int fd, const char *path, off_t off, struct stat *stp)
417 if (fstat(fd, &st) == 0)
420 if (stp && !S_ISREG(stp->st_mode))
421 return (0); /* not relevant */
422 cp = fingerprint_info_lookup(fd, path);
424 ve_error_set("%s: no entry", path);
425 return (VE_FINGERPRINT_NONE);
427 rc = verify_fingerprint(fd, path, cp, off);
429 case VE_FINGERPRINT_OK:
430 case VE_FINGERPRINT_IGNORE:
431 case VE_FINGERPRINT_UNKNOWN:
434 return (VE_FINGERPRINT_WRONG);
440 * open a file if it can be verified
448 * @return fd or VE_FINGERPRINT_NONE, VE_FINGERPRINT_WRONG
451 verify_open(const char *path, int flags)
456 if ((fd = open(path, flags)) >= 0) {
457 if ((rc = verify_fd(fd, path, 0, NULL)) < 0) {