2 * Copyright (c) 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>
27 /* Avoid unwanted userlandish components */
29 #include <sys/errno.h>
34 static int vectx_debug = VECTX_DEBUG;
35 # define DEBUG_PRINTF(n, x) if (vectx_debug >= n) printf x
38 #include "libsecureboot-priv.h"
39 #include <verify_file.h>
43 * @brief api to verify file while reading
45 * This API allows the hash of a file to be computed as it is read.
46 * Key to this is seeking by reading.
48 * On close an indication of the verification result is returned.
52 br_hash_compat_context vec_ctx; /* hash ctx */
53 const br_hash_class *vec_md; /* hash method */
54 const char *vec_path; /* path we are verifying */
55 const char *vec_want; /* hash value we want */
56 off_t vec_off; /* current offset */
57 off_t vec_hashed; /* where we have hashed to */
58 off_t vec_size; /* size of path */
59 size_t vec_hashsz; /* size of hash */
60 int vec_fd; /* file descriptor */
61 int vec_status; /* verification status */
62 int vec_closing; /* we are closing */
68 * verify an open file as we read it
70 * If the file has no fingerprint to match, we will still return a
71 * verification context containing little more than the file
72 * descriptor, and an error code in @c error.
84 * pointer to struct stat
88 * @li ENOMEM out of memory
89 * @li VE_FINGERPRINT_NONE no entry found
90 * @li VE_FINGERPRINT_UNKNOWN no fingerprint in entry
92 * @return ctx or NULL on error.
93 * NULL is only returned for non-files or out-of-memory.
96 vectx_open(int fd, const char *path, off_t off, struct stat *stp,
97 int *error, const char *caller)
108 rc = verify_prep(fd, path, off, stp, __func__);
111 ("vectx_open: caller=%s,fd=%d,name='%s',prep_rc=%d\n",
112 caller, fd, path, rc));
115 case VE_FINGERPRINT_NONE:
116 case VE_FINGERPRINT_UNKNOWN:
117 case VE_FINGERPRINT_WRONG:
121 ctx = malloc(sizeof(struct vectx));
125 ctx->vec_path = path;
126 ctx->vec_size = stp->st_size;
129 ctx->vec_want = NULL;
131 ctx->vec_hashsz = hashsz = 0;
132 ctx->vec_closing = 0;
135 /* we are not verifying this */
139 cp = fingerprint_info_lookup(fd, path);
141 ctx->vec_status = VE_FINGERPRINT_NONE;
142 ve_error_set("%s: no entry", path);
144 if (strncmp(cp, "no_hash", 7) == 0) {
145 ctx->vec_status = VE_FINGERPRINT_IGNORE;
147 } else if (strncmp(cp, "sha256=", 7) == 0) {
148 ctx->vec_md = &br_sha256_vtable;
149 hashsz = br_sha256_SIZE;
151 #ifdef VE_SHA1_SUPPORT
152 } else if (strncmp(cp, "sha1=", 5) == 0) {
153 ctx->vec_md = &br_sha1_vtable;
154 hashsz = br_sha1_SIZE;
157 #ifdef VE_SHA384_SUPPORT
158 } else if (strncmp(cp, "sha384=", 7) == 0) {
159 ctx->vec_md = &br_sha384_vtable;
160 hashsz = br_sha384_SIZE;
163 #ifdef VE_SHA512_SUPPORT
164 } else if (strncmp(cp, "sha512=", 7) == 0) {
165 ctx->vec_md = &br_sha512_vtable;
166 hashsz = br_sha512_SIZE;
170 ctx->vec_status = VE_FINGERPRINT_UNKNOWN;
171 ve_error_set("%s: no supported fingerprint", path);
174 *error = ctx->vec_status;
175 ctx->vec_hashsz = hashsz;
178 ctx->vec_md->init(&ctx->vec_ctx.vtable);
181 lseek(fd, 0, SEEK_SET);
182 vectx_lseek(ctx, off, SEEK_SET);
186 ("vectx_open: caller=%s,name='%s',hashsz=%lu,status=%d\n",
187 caller, path, (unsigned long)ctx->vec_hashsz,
191 enomem: /* unlikely */
199 * read bytes from file and update hash
201 * It is critical that all file I/O comes through here.
202 * We keep track of current offset.
203 * We also track what offset we have hashed to,
204 * so we won't replay data if we seek backwards.
213 * @return bytes read or error.
216 vectx_read(struct vectx *ctx, void *buf, size_t nbytes)
218 unsigned char *bp = buf;
225 if (ctx->vec_hashsz == 0) /* nothing to do */
226 return (read(ctx->vec_fd, buf, nbytes));
231 * Do this in reasonable chunks so
232 * we don't timeout if doing tftp
235 x = MIN(PAGE_SIZE, x);
236 d = n = read(ctx->vec_fd, &bp[off], x);
237 if (ctx->vec_closing && n < x) {
239 ("%s: read %d off=%ld hashed=%ld size=%ld\n",
240 __func__, n, (long)ctx->vec_off,
241 (long)ctx->vec_hashed, (long)ctx->vec_size));
247 /* we may have seeked backwards! */
248 delta = ctx->vec_hashed - ctx->vec_off;
256 if (ctx->vec_closing && d < PAGE_SIZE) {
258 ("%s: update %ld + %d\n",
260 (long)ctx->vec_hashed, d));
262 ctx->vec_md->update(&ctx->vec_ctx.vtable, &bp[off], d);
265 ctx->vec_hashed += d;
268 } while (n > 0 && off < nbytes);
274 * vectx equivalent of lseek
276 * When seeking forwards we actually call vectx_read
277 * to reach the desired offset.
279 * We support seeking backwards.
288 * We try to convert whence to ``SEEK_SET``.
289 * We do not support ``SEEK_DATA`` or ``SEEK_HOLE``.
291 * @return offset or error.
294 vectx_lseek(struct vectx *ctx, off_t off, int whence)
296 unsigned char buf[PAGE_SIZE];
300 if (ctx->vec_hashsz == 0) /* nothing to do */
301 return (lseek(ctx->vec_fd, off, whence));
304 * Convert whence to SEEK_SET
307 ("%s(%s, %ld, %d)\n", __func__, ctx->vec_path, (long)off, whence));
308 if (whence == SEEK_END && off <= 0) {
309 if (ctx->vec_closing && ctx->vec_hashed < ctx->vec_size) {
310 DEBUG_PRINTF(3, ("%s: SEEK_END %ld\n",
312 (long)(ctx->vec_size - ctx->vec_hashed)));
315 off += ctx->vec_size;
316 } else if (whence == SEEK_CUR) {
320 if (whence != SEEK_SET ||
321 off > ctx->vec_size) {
322 printf("ERROR: %s: unsupported operation: whence=%d off=%ld -> %ld\n",
323 __func__, whence, (long)ctx->vec_off, (long)off);
326 if (off < ctx->vec_hashed) {
328 struct open_file *f = fd2open_file(ctx->vec_fd);
331 strncmp(f->f_ops->fs_name, "tftp", 4) == 0) {
332 /* we cannot rewind if we've hashed much of the file */
333 if (ctx->vec_hashed > ctx->vec_size / 5)
334 return (-1); /* refuse! */
337 /* seeking backwards! just do it */
338 ctx->vec_off = lseek(ctx->vec_fd, off, whence);
339 return (ctx->vec_off);
343 delta = off - ctx->vec_off;
345 delta = MIN(PAGE_SIZE, delta);
346 n = vectx_read(ctx, buf, delta);
350 } while (ctx->vec_off < off && n > 0);
351 return (ctx->vec_off);
356 * check that hashes match and cleanup
358 * We have finished reading file, compare the hash with what
361 * Be sure to call this before closing the file, since we may
362 * need to seek to the end to ensure hashing is complete.
367 * @return 0 or an error.
370 vectx_close(struct vectx *ctx, int severity, const char *caller)
374 ctx->vec_closing = 1;
375 if (ctx->vec_hashsz == 0) {
376 rc = ctx->vec_status;
378 #ifdef VE_PCR_SUPPORT
380 * Only update pcr with things that must verify
381 * these tend to be processed in a more deterministic
382 * order, which makes our pseudo pcr more useful.
384 ve_pcr_updating_set((severity == VE_MUST));
386 /* make sure we have hashed it all */
387 vectx_lseek(ctx, 0, SEEK_END);
388 rc = ve_check_hash(&ctx->vec_ctx, ctx->vec_md,
389 ctx->vec_path, ctx->vec_want, ctx->vec_hashsz);
392 ("vectx_close: caller=%s,name='%s',rc=%d,severity=%d\n",
393 caller,ctx->vec_path, rc, severity));
394 verify_report(ctx->vec_path, severity, rc, NULL);
395 if (rc == VE_FINGERPRINT_WRONG) {
396 #if !defined(UNIT_TEST) && !defined(DEBUG_VECTX)
397 /* we are generally called with VE_MUST */
398 if (severity > VE_WANT)
399 panic("cannot continue");
403 return ((rc < 0) ? rc : 0);