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, '/');
92 free(nfip->fi_prefix);
97 /* collapse any trailing ..[/] */
99 while ((cp = strrchr(nfip->fi_prefix, '/')) != NULL) {
100 if (cp[1] == '\0') { /* trailing "/" */
104 if (strcmp(&cp[1], "..") == 0) {
119 nfip->fi_dev = stp->st_dev;
121 nfip->fi_data = data;
122 nfip->fi_prefix_len = strlen(nfip->fi_prefix);
124 nfip->fi_skip_len = strlen(skip);
125 if (nfip->fi_skip_len)
126 nfip->fi_skip = strdup(skip);
128 nfip->fi_skip = NULL;
130 nfip->fi_skip = NULL;
131 nfip->fi_skip_len = 0;
134 if (LIST_EMPTY(&fi_list)) {
135 LIST_INSERT_HEAD(&fi_list, nfip, entries);
136 DEBUG_PRINTF(4, ("inserted %zu %s at head\n",
137 nfip->fi_prefix_len, nfip->fi_prefix));
140 LIST_FOREACH(fip, &fi_list, entries) {
141 if (nfip->fi_prefix_len >= fip->fi_prefix_len) {
142 LIST_INSERT_BEFORE(fip, nfip, entries);
143 DEBUG_PRINTF(4, ("inserted %zu %s before %zu %s\n",
144 nfip->fi_prefix_len, nfip->fi_prefix,
145 fip->fi_prefix_len, fip->fi_prefix));
150 LIST_INSERT_AFTER(lfip, nfip, entries);
151 DEBUG_PRINTF(4, ("inserted %zu %s after %zu %s\n",
152 nfip->fi_prefix_len, nfip->fi_prefix,
153 lfip->fi_prefix_len, lfip->fi_prefix));
156 #ifdef MANIFEST_SKIP_MAYBE
158 * Deal with old incompatible boot/manifest
159 * if fp[-1] is '/' and start of entry matches
160 * MANIFEST_SKIP_MAYBE, we want it.
163 maybe_skip(char *fp, struct fingerprint_info *fip, size_t *nplenp)
167 tp = fp - sizeof(MANIFEST_SKIP_MAYBE);
169 if (tp >= fip->fi_data) {
170 DEBUG_PRINTF(3, ("maybe: %.48s\n", tp));
171 if ((tp == fip->fi_data || tp[-1] == '\n') &&
172 strncmp(tp, MANIFEST_SKIP_MAYBE,
173 sizeof(MANIFEST_SKIP_MAYBE) - 1) == 0) {
175 *nplenp += sizeof(MANIFEST_SKIP_MAYBE);
183 fingerprint_info_lookup(int fd, const char *path)
185 char pbuf[MAXPATHLEN+1];
186 char nbuf[MAXPATHLEN+1];
188 struct fingerprint_info *fip;
189 char *cp, *ep, *fp, *np;
191 size_t n, plen, nlen, nplen;
194 fingerprint_info_init();
196 n = strlcpy(pbuf, path, sizeof(pbuf));
197 if (n >= sizeof(pbuf))
200 if (fstat(fd, &st) == 0)
204 * get the first entry - it will have longest prefix
205 * so we can can work out how to initially split path
207 fip = LIST_FIRST(&fi_list);
212 cp = &pbuf[fip->fi_prefix_len];
220 nlen = plen = 0; /* keep gcc quiet */
222 for ( ; cp >= pbuf && *cp != '/'; cp--)
246 DEBUG_PRINTF(2, ("looking for %s %zu %s\n", prefix, plen, cp));
248 LIST_FOREACH(fip, &fi_list, entries) {
249 DEBUG_PRINTF(4, ("at %zu %s\n",
250 fip->fi_prefix_len, fip->fi_prefix));
252 if (fip->fi_prefix_len < plen) {
253 DEBUG_PRINTF(3, ("skipping prefix=%s %zu %zu\n",
254 fip->fi_prefix, fip->fi_prefix_len,
258 if (fip->fi_prefix_len == plen) {
259 if (fip->fi_dev != 0 && fip->fi_dev != dev) {
261 "skipping dev=%ld != %ld\n",
266 if (strcmp(prefix, fip->fi_prefix)) {
268 "skipping prefix=%s\n",
272 DEBUG_PRINTF(3, ("checking prefix=%s\n",
274 if (fip->fi_skip_len) {
276 nplen = snprintf(nbuf, sizeof(nbuf),
279 nplen = MIN(nplen, sizeof(nbuf) - 1);
284 DEBUG_PRINTF(3, ("lookup: '%s'\n", np));
285 if (!(fp = strstr(fip->fi_data, np)))
287 #ifdef MANIFEST_SKIP_MAYBE
288 if (fip->fi_skip_len == 0 &&
289 fp > fip->fi_data && fp[-1] == '/') {
290 fp = maybe_skip(fp, fip, &nplen);
294 * when we find a match:
295 * fp[nplen] will be space and
296 * fp will be fip->fi_data or
299 if (!((fp == fip->fi_data || fp[-1] == '\n') &&
305 #ifdef MANIFEST_SKIP_MAYBE
306 if (fip->fi_skip_len == 0 &&
309 fp = maybe_skip(fp, fip, &nplen);
313 ("fp[-1]=%#x fp[%zu]=%#x fp=%.78s\n",
318 } while (fp != NULL &&
324 DEBUG_PRINTF(2, ("found %.78s\n", fp));
325 /* we have a match! */
326 for (cp = &fp[nplen]; *cp == ' '; cp++)
331 ("Ignoring prefix=%s\n", fip->fi_prefix));
334 } while (cp > &pbuf[1]);
340 verify_fingerprint(int fd, const char *path, const char *cp, off_t off)
342 unsigned char buf[PAGE_SIZE];
343 const br_hash_class *md;
344 br_hash_compat_context mctx;
348 if (strncmp(cp, "no_hash", 7) == 0) {
349 return (VE_FINGERPRINT_IGNORE);
350 } else if (strncmp(cp, "sha256=", 7) == 0) {
351 md = &br_sha256_vtable;
352 hlen = br_sha256_SIZE;
354 #ifdef VE_SHA1_SUPPORT
355 } else if (strncmp(cp, "sha1=", 5) == 0) {
356 md = &br_sha1_vtable;
360 #ifdef VE_SHA384_SUPPORT
361 } else if (strncmp(cp, "sha384=", 7) == 0) {
362 md = &br_sha384_vtable;
363 hlen = br_sha384_SIZE;
366 #ifdef VE_SHA512_SUPPORT
367 } else if (strncmp(cp, "sha512=", 7) == 0) {
368 md = &br_sha512_vtable;
369 hlen = br_sha512_SIZE;
373 ve_error_set("%s: no supported fingerprint", path);
374 return (VE_FINGERPRINT_UNKNOWN);
377 md->init(&mctx.vtable);
379 lseek(fd, 0, SEEK_SET);
381 n = read(fd, buf, sizeof(buf));
385 md->update(&mctx.vtable, buf, n);
387 lseek(fd, off, SEEK_SET);
388 return (ve_check_hash(&mctx, md, path, cp, hlen));
394 * verify an open file
405 * @return 0, VE_FINGERPRINT_OK or VE_FINGERPRINT_NONE, VE_FINGERPRINT_WRONG
408 verify_fd(int fd, const char *path, off_t off, struct stat *stp)
415 if (fstat(fd, &st) == 0)
418 if (stp && !S_ISREG(stp->st_mode))
419 return (0); /* not relevant */
420 cp = fingerprint_info_lookup(fd, path);
422 ve_error_set("%s: no entry", path);
423 return (VE_FINGERPRINT_NONE);
425 rc = verify_fingerprint(fd, path, cp, off);
427 case VE_FINGERPRINT_OK:
428 case VE_FINGERPRINT_IGNORE:
429 case VE_FINGERPRINT_UNKNOWN:
432 return (VE_FINGERPRINT_WRONG);
438 * open a file if it can be verified
446 * @return fd or VE_FINGERPRINT_NONE, VE_FINGERPRINT_WRONG
449 verify_open(const char *path, int flags)
454 if ((fd = open(path, flags)) >= 0) {
455 if ((rc = verify_fd(fd, path, 0, NULL)) < 0) {