Skip to content

eBPF-based security platform

upatch: validate container header bounds

John Fastabende26ccfe4b63e
diff --git a/Documentation/upatch-spec.rst b/Documentation/upatch-spec.rst
index b2f76de4..a4e07b82 100644
--- a/Documentation/upatch-spec.rst
+++ b/Documentation/upatch-spec.rst
@@ -91,9 +91,6 @@ offset size type field meaning
356 4 u32 reserved1 zero
========== ==== ====== ===================== ===========================================
-The legacy 352-byte header ended at ``reserved0`` and implied a BPF
-payload. It remains readable for existing artifacts.
-
magic
~~~~~
@@ -345,9 +342,8 @@ bump:
- a signed decoder blob may be appended inside ``data`` and located by
the trailer at its end. Older loaders ignore the additional bytes;
older readers treat them as opaque loader data;
-- the 360-byte header appended ``type`` and ``reserved1`` after
- ``reserved0``; the 352-byte form is still read and means
- ``type = bpf``;
+- the 360-byte header includes ``type`` and ``reserved1`` after
+ ``reserved0``;
- ``reserved0`` and ``reserved1`` are zero in every file a writer
emits; a reader rejects a file in which ``reserved0`` is not, so the
slot can be given a meaning later without a flag day.
@@ -364,10 +360,9 @@ match, before trusting any offset.
1. the file is a regular file, non-empty, and at most 16 MiB;
2. ``magic`` and ``version`` match;
3. each section ``(off, len)`` lies within the file, and any non-empty
- section starts at or after the active header (360 bytes, or 352 for
- the legacy form);
+ section starts at or after the 360-byte header;
4. ``loader_insns_len`` and ``signature_len`` are non-zero, and
- ``reserved0`` is zero;
+ ``reserved0`` and ``reserved1`` are zero;
5. decoders are parsed only if ``data`` ends with a matching trailer,
all blob and entry bounds fit, fixed strings are NUL-terminated, and
every source range stays inside the blob. Malformed decoder
diff --git a/testing/t0004-upatch-parse.sh b/testing/t0004-upatch-parse.sh
index c62b369c..a49d4d0d 100644
--- a/testing/t0004-upatch-parse.sh
+++ b/testing/t0004-upatch-parse.sh
@@ -52,4 +52,45 @@ test_expect_success 'a directory path rejected, no crash' '
reject "$WORK"
'
+python3 - "$WORK" <<'PY'
+import pathlib, struct, sys
+root = pathlib.Path(sys.argv[1])
+header = bytearray(369)
+struct.pack_into("<QII", header, 0, 0x53485244554E4150, 1, 0)
+header[16:32] = b"structured-test".ljust(16, bytes([0]))
+struct.pack_into("<14I", header, 304, 360, 8, 368, 1, 0, 0, 0, 0,
+ 4096, 0, 0, 0, 1, 0)
+header[360:368] = b"12345678"
+header[368] = 0x41
+(root / "valid-structured").write_bytes(header)
+def mutate(name, off, fmt, value):
+ data = bytearray(header)
+ struct.pack_into(fmt, data, off, value)
+ (root / name).write_bytes(data)
+mutate("bad-version", 8, "<I", 2)
+mutate("reserved0", 348, "<I", 1)
+mutate("reserved1", 356, "<I", 1)
+mutate("zero-loader", 308, "<I", 0)
+mutate("zero-signature", 316, "<I", 0)
+mutate("loader-in-header", 304, "<I", 359)
+mutate("signature-oob", 312, "<I", 0xfffffff0)
+mutate("data-overflow", 328, "<I", 0xfffffff0)
+data = bytearray(header)
+struct.pack_into("<II", data, 320, 359, 1)
+(root / "metadata-in-header").write_bytes(data)
+mutate("data-len-overflow", 332, "<I", 0xffffffff)
+with (root / "oversized").open("wb") as f:
+ f.truncate(16 * 1024 * 1024 + 1)
+PY
+test_expect_success 'structured valid container parses' '
+ "$S" upatch info "$WORK/valid-structured" >/dev/null 2>&1
+'
+test_expect_success 'structured header and section bounds fail cleanly' '
+ for f in bad-version reserved0 reserved1 zero-loader zero-signature \
+ loader-in-header signature-oob data-overflow metadata-in-header \
+ data-len-overflow; do reject "$WORK/$f" || exit 1; done
+'
+test_expect_success 'file larger than 16 MiB is rejected without mapping it' '
+ reject "$WORK/oversized"
+'
test_done
diff --git a/util/upatch.c b/util/upatch.c
index 722531e2..ac68a68c 100644
--- a/util/upatch.c
+++ b/util/upatch.c
@@ -95,7 +95,8 @@ static int validate_bounds(const struct shirudo_upatch_header *h, size_t n)
{
if (h->type != SHIRUDO_UPATCH_TYPE_BPF)
return -EINVAL;
- if (!section_inside(h->loader_insns_off, h->loader_insns_len, n))
+ if (h->loader_insns_off < SHIRUDO_UPATCH_HEADER_SIZE ||
+ !section_inside(h->loader_insns_off, h->loader_insns_len, n))
return -EINVAL;
if (!section_inside(h->signature_off, h->signature_len, n))
return -EINVAL;