Skip to content

Commit 6316c6a

Browse files
committed
Revert "crypto: streaming blake2b for validation"
This introduced a TOCTOU vulnerability in our checksum verification and file use. This reverts commit db799de.
1 parent d45e7a6 commit 6316c6a

File tree

3 files changed

+8
-27
lines changed

3 files changed

+8
-27
lines changed

common/crypt/blake2b.c

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <stdint.h>
66
#include <stddef.h>
77
#include <crypt/blake2b.h>
8-
#include <fs/file.h>
98
#include <lib/libc.h>
109

1110
#define BLAKE2B_BLOCK_BYTES 128
@@ -219,25 +218,3 @@ void blake2b(void *out, const void *in, size_t in_len) {
219218
blake2b_update(&state, in, in_len);
220219
blake2b_final(&state, out);
221220
}
222-
223-
bool blake2b_verify_file(struct file_handle *fd, const uint8_t expected[BLAKE2B_OUT_BYTES]) {
224-
uint8_t out_buf[BLAKE2B_OUT_BYTES];
225-
226-
if (fd->is_memfile) {
227-
blake2b(out_buf, fd->fd, fd->size);
228-
return memcmp(out_buf, expected, BLAKE2B_OUT_BYTES) == 0;
229-
}
230-
231-
struct blake2b_state state;
232-
blake2b_init(&state);
233-
char chunk_buf[4096];
234-
235-
for (uint64_t r = fd->size, off = 0, sz; r > 0; off += sz, r -= sz) {
236-
fd->read(fd, chunk_buf, off, sz = r < 4096 ? r : 4096);
237-
blake2b_update(&state, chunk_buf, sz);
238-
}
239-
240-
blake2b_final(&state, out_buf);
241-
242-
return memcmp(out_buf, expected, BLAKE2B_OUT_BYTES) == 0;
243-
}

common/crypt/blake2b.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,4 @@
77

88
void blake2b(void *out, const void *in, size_t in_len);
99

10-
struct file_handle;
11-
bool blake2b_verify_file(struct file_handle *fd, const uint8_t expected[BLAKE2B_OUT_BYTES]);
12-
1310
#endif

common/lib/uri.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,20 @@ struct file_handle *uri_open(char *uri) {
257257
}
258258

259259
if (hash != NULL && ret != NULL) {
260+
uint8_t out_buf[BLAKE2B_OUT_BYTES];
261+
#if defined (UEFI) && defined (__x86_64__)
262+
void *file_buf = freadall_mode(ret, MEMMAP_BOOTLOADER_RECLAIMABLE, true);
263+
#else
264+
void *file_buf = freadall(ret, MEMMAP_BOOTLOADER_RECLAIMABLE);
265+
#endif
266+
blake2b(out_buf, file_buf, ret->size);
260267
uint8_t hash_buf[BLAKE2B_OUT_BYTES];
261268

262269
for (size_t i = 0; i < sizeof(hash_buf); i++) {
263270
hash_buf[i] = digit_to_int(hash[i * 2]) << 4 | digit_to_int(hash[i * 2 + 1]);
264271
}
265272

266-
if (!blake2b_verify_file(ret, hash_buf)) {
273+
if (memcmp(hash_buf, out_buf, sizeof(out_buf)) != 0) {
267274
if (hash_mismatch_panic) {
268275
panic(true, "Blake2b hash for URI `%#` does not match!", uri);
269276
} else {

0 commit comments

Comments
 (0)