|
Zbigniew Jędrzejewski-Szmek |
d743bb |
From bdd5ae00e8cf4f45183a20e5bc89efd3dcc02266 Mon Sep 17 00:00:00 2001
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
From: Tobias Stoeckmann <stoeckmann@users.noreply.github.com>
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
Date: Mon, 13 Mar 2017 08:14:42 +0100
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
Subject: [PATCH] journal: prevent integer overflow while validating header
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
(#5569)
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
It is possible to overflow uint64_t while validating the header of
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
a journal file. To prevent this, the addition itself is checked to
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
be within the limits of UINT64_MAX first.
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
To keep this readable, I have introduced two stack variables which
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
hold the converted values during validation.
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
(cherry picked from commit 6f94e420e8355421fc31713a0df760d6b20473ac)
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
---
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
src/journal/journal-file.c | 12 +++++++++---
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
1 file changed, 9 insertions(+), 3 deletions(-)
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
index a6ccb679a8..14cb01a600 100644
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
--- a/src/journal/journal-file.c
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
+++ b/src/journal/journal-file.c
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
@@ -546,6 +546,8 @@ static bool warn_wrong_flags(const JournalFile *f, bool compatible) {
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
}
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
static int journal_file_verify_header(JournalFile *f) {
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
+ uint64_t arena_size, header_size;
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
+
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
assert(f);
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
assert(f->header);
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
@@ -564,17 +566,21 @@ static int journal_file_verify_header(JournalFile *f) {
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
if (f->header->state >= _STATE_MAX)
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
return -EBADMSG;
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
+ header_size = le64toh(f->header->header_size);
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
+
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
/* The first addition was n_data, so check that we are at least this large */
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
- if (le64toh(f->header->header_size) < HEADER_SIZE_MIN)
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
+ if (header_size < HEADER_SIZE_MIN)
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
return -EBADMSG;
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
if (JOURNAL_HEADER_SEALED(f->header) && !JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays))
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
return -EBADMSG;
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
- if ((le64toh(f->header->header_size) + le64toh(f->header->arena_size)) > (uint64_t) f->last_stat.st_size)
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
+ arena_size = le64toh(f->header->arena_size);
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
+
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
+ if (UINT64_MAX - header_size < arena_size || header_size + arena_size > (uint64_t) f->last_stat.st_size)
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
return -ENODATA;
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
- if (le64toh(f->header->tail_object_offset) > (le64toh(f->header->header_size) + le64toh(f->header->arena_size)))
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
+ if (le64toh(f->header->tail_object_offset) > header_size + arena_size)
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
return -ENODATA;
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
|
|
Zbigniew Jędrzejewski-Szmek |
d743bb |
if (!VALID64(le64toh(f->header->data_hash_table_offset)) ||
|