Skip to content

eBPF-based security platform

kernel_build_id: take a build ID from the GNU note alone

Daniel Borkmann8e8d030a9c8b
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 83c26572..3370af99 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -275,7 +275,8 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
- ``kernel_build_id_from_notes()``
- Notes of 0 to 24, 28, 32, 36 and 40 bytes
- Reads nothing past the notes; a build ID of 1 to 64 lowercase hex
- digits that is the first GNU build ID note's, or empty
+ digits that is the first GNU build ID note's, or empty, and none
+ from a section whose one note is another
* - ``kernel-release``
- ``kernel_release_prefix_matches()``
- Releases and prefixes up to 8 bytes
diff --git a/testing/proofs/cbmc/kernel-notes.c b/testing/proofs/cbmc/kernel-notes.c
index b2b452a4..72426578 100644
--- a/testing/proofs/cbmc/kernel-notes.c
+++ b/testing/proofs/cbmc/kernel-notes.c
@@ -5,7 +5,8 @@
* kernel_build_id_from_notes() of util/kernel_build_id.c on every note
* section of LEN bytes: it reads nothing outside the section, a refusal
* leaves an empty string, and a build ID is the lowercase hex of at most
- * 64 bytes, that of the first note when it is the GNU build ID.
+ * 64 bytes, that of the first note when it is the GNU build ID, type 3
+ * named "GNU", and none from a section whose one note is anything else.
*/
#include "proof.h"
@@ -25,6 +26,21 @@ static bool lower_hex(char c)
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
}
+static bool gnu_build_id(const __u8 *notes)
+{
+ return word(notes + 8) == 3 && word(notes) == 4 &&
+ !memcmp(notes + 12, "GNU", 4);
+}
+
+/* A section of one note, of a name of up to 4 bytes and a descriptor. */
+static bool one_note(const __u8 *notes)
+{
+ __u32 namesz = word(notes), descsz = word(notes + 4);
+
+ return LEN >= 12 && namesz <= 4 && descsz <= LEN &&
+ 12 + align4(namesz) + align4(descsz) == LEN;
+}
+
static void first_note(const __u8 *notes, const char *out)
{
static const char d[] = "0123456789abcdef";
@@ -35,8 +51,7 @@ static void first_note(const __u8 *notes, const char *out)
return;
namesz = word(notes);
descsz = word(notes + 4);
- if (word(notes + 8) != 3 || namesz < 3 || namesz > LEN ||
- memcmp(notes + 12, "GNU", 3) || !descsz || descsz > LEN)
+ if (!gnu_build_id(notes) || !descsz || descsz > LEN)
return;
desc = 12 + align4(namesz);
if (desc + align4(descsz) > LEN)
@@ -59,6 +74,8 @@ void harness(void)
__CPROVER_assert(!out[0], "a refusal leaves an empty string");
return;
}
+ __CPROVER_assert(!one_note(notes) || gnu_build_id(notes),
+ "only a GNU build ID note gives one");
n = strnlen(out, sizeof(out));
__CPROVER_assert(n < sizeof(out) && n >= 2 && !(n % 2),
"a terminated ID of whole bytes, at most 64");
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index 60e2c8fb..d6df3578 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -41,6 +41,6 @@ config-hosts --unwind 9 LEN=0-7
image-bootstrap --unwind 12 --unwindset bootstrap_parse.0:2,hex_decode.0:1 LEN=0-8
image-path --unwind 11
dm-names --unwind 10
-kernel-notes --unwind 1 --unwindset kernel_build_id_from_notes.0:5,hex_encode.0:29,strnlen.0:58,harness.0:58,first_note.0:29,memcmp.0:4 LEN=0-24,28,32,36,40
+kernel-notes --unwind 1 --unwindset kernel_build_id_from_notes.0:5,hex_encode.0:29,strnlen.0:58,harness.0:58,first_note.0:29,memcmp.0:5 LEN=0-24,28,32,36,40
kernel-release --unwind 10
log-escape --unwind 7 LEN=0-5
diff --git a/testing/unit/kernel_build_id.c b/testing/unit/kernel_build_id.c
index 278d6757..8dcda1c3 100644
--- a/testing/unit/kernel_build_id.c
+++ b/testing/unit/kernel_build_id.c
@@ -38,10 +38,27 @@ static void oversized_name_is_malformed(void)
EXPECT_EQ(kernel_build_id_from_notes(bad, sizeof(bad), id), -EINVAL);
}
+/* A vendor's type-3 note first, then the GNU one. */
+static void only_the_gnu_name_names_the_build_id(void)
+{
+ static const __u8 vendor[] = {
+ 4, 0, 0, 0, 4, 0, 0, 0, 3, 0, 0, 0,
+ 'G', 'N', 'U', 'X', 0x89, 0xab, 0xcd, 0xef,
+ 4, 0, 0, 0, 4, 0, 0, 0, 3, 0, 0, 0,
+ 'G', 'N', 'U', 0, 0x01, 0x23, 0x45, 0x67,
+ };
+ char id[KERNEL_BUILD_ID_STRING_MAX];
+
+ ASSERT_EQ(kernel_build_id_from_notes(vendor, sizeof(vendor), id), 0);
+ EXPECT_STREQ(id, "01234567");
+ EXPECT_EQ(kernel_build_id_from_notes(vendor, 20, id), -ENOENT);
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(gnu_note_gives_the_build_id),
UNIT_CASE(empty_notes_have_no_build_id),
UNIT_CASE(oversized_name_is_malformed),
+ UNIT_CASE(only_the_gnu_name_names_the_build_id),
{}
};
diff --git a/util/kernel_build_id.c b/util/kernel_build_id.c
index 7861a4bd..b06d6fec 100644
--- a/util/kernel_build_id.c
+++ b/util/kernel_build_id.c
@@ -53,7 +53,7 @@ int kernel_build_id_from_notes(const void *notes, size_t notes_len,
align4(descsz) > notes_len - desc_off)
return -EINVAL;
next = desc_off + align4(descsz);
- if (type == 3 && namesz >= 3 && !memcmp(p + name_off, "GNU", 3)) {
+ if (type == 3 && namesz == 4 && !memcmp(p + name_off, "GNU", 4)) {
if (!descsz || descsz > KERNEL_BUILD_ID_MAX)
return -E2BIG;
hex_encode(p + desc_off, descsz, out);