eBPF-based security platform
upatch: drop the 352-byte header
Daniel Borkmannf69c4c979d36
diff --git a/Documentation/upatch-spec.rst b/Documentation/upatch-spec.rstindex c648219e..e04aa089 100644--- a/Documentation/upatch-spec.rst+++ b/Documentation/upatch-spec.rst@@ -28,8 +28,7 @@ architecture. Operator and target may differ in architecture; thewire format does not.The file begins with a 360-byte header of naturally aligned fields-with no padding. Readers also accept the original 352-byte header-and treat it as ``type = bpf``. All other content is referenced by+with no padding. All other content is referenced by``*_off``/``*_len`` pairs in the header, as byte offsets from the startof the file. Sections do not overlap the header and must lie withinthe file.@@ -57,8 +56,8 @@ Artifacts are small in practice.The writer emits the sections in this order, but a reader must relyonly on the header offsets, never on adjacency. ``loader_insns``-begins at offset 360, immediately after the header; legacy artifacts-begin at 352. ``data`` and ``metadata`` are optional; an absent section+begins at offset 360, immediately after the header. ``data`` and+``metadata`` are optional; an absent sectionhas its ``*_off`` set to 0.diff --git a/include/shirudo/upatch.h b/include/shirudo/upatch.hindex b31ebd6c..d5c3c758 100644--- a/include/shirudo/upatch.h+++ b/include/shirudo/upatch.h@@ -20,7 +20,6 @@* and Documentation/upatch-spec.rst in this tree.*/#define SHIRUDO_UPATCH_VERSION 1u-#define SHIRUDO_UPATCH_LEGACY_HEADER_SIZE 352u#define SHIRUDO_UPATCH_HEADER_SIZE 360u#define SHIRUDO_DIRECTIVE_UNLOAD (1u << 0)diff --git a/util/upatch.c b/util/upatch.cindex 87897358..a0fdbef9 100644--- a/util/upatch.c+++ b/util/upatch.c@@ -18,15 +18,11 @@#include "upatch.h"static int parse_header(const unsigned char *m, size_t n,- struct shirudo_upatch_header *out, size_t *header_size)+ struct shirudo_upatch_header *out){const unsigned char *p = m;- uint32_t type = SHIRUDO_UPATCH_TYPE_BPF;- uint32_t reserved = 0;- uint32_t loader_off, signature_off, metadata_off, data_off;- bool new_header = false;- if (n < SHIRUDO_UPATCH_LEGACY_HEADER_SIZE)+ if (n < SHIRUDO_UPATCH_HEADER_SIZE)return -EINVAL;out->magic = rd_le64(p); p += 8;@@ -45,80 +41,47 @@ static int parse_header(const unsigned char *m, size_t n,memcpy(out->payload_sha256, p, sizeof(out->payload_sha256));p += sizeof(out->payload_sha256);- loader_off = rd_le32(p); p += 4;- out->loader_insns_off = loader_off;- out->loader_insns_len = rd_le32(p); p += 4;- signature_off = rd_le32(p); p += 4;- out->signature_off = signature_off;- out->signature_len = rd_le32(p); p += 4;- metadata_off = rd_le32(p); p += 4;- out->metadata_off = metadata_off;- out->metadata_len = rd_le32(p); p += 4;- data_off = rd_le32(p); p += 4;- out->data_off = data_off;- out->data_len = rd_le32(p); p += 4;+ out->loader_insns_off = rd_le32(p); p += 4;+ out->loader_insns_len = rd_le32(p); p += 4;+ out->signature_off = rd_le32(p); p += 4;+ out->signature_len = rd_le32(p); p += 4;+ out->metadata_off = rd_le32(p); p += 4;+ out->metadata_len = rd_le32(p); p += 4;+ out->data_off = rd_le32(p); p += 4;+ out->data_len = rd_le32(p); p += 4;out->log_buf_sz = rd_le32(p); p += 4;out->nr_maps = rd_le32(p); p += 4;out->nr_progs = rd_le32(p); p += 4;out->reserved0 = rd_le32(p); p += 4;- if (out->reserved0 != 0)+ out->type = rd_le32(p); p += 4;+ out->reserved1 = rd_le32(p); p += 4;+ if (out->reserved0 != 0 || out->reserved1 != 0)return -EINVAL;-- /*- * New v1 files append type + an explicit alignment word. A legacy- * file is distinguishable by its loader starting at the old- * 352-byte boundary; default those files to BPF. Do not infer the- * header size from later sections, since those naturally lie- * beyond 360 too.- */- if (n >= SHIRUDO_UPATCH_HEADER_SIZE) {- uint32_t tail_type = rd_le32(m + SHIRUDO_UPATCH_LEGACY_HEADER_SIZE);- uint32_t tail_reserved = rd_le32(m + SHIRUDO_UPATCH_LEGACY_HEADER_SIZE + 4);-- if (tail_reserved == 0 &&- loader_off >= SHIRUDO_UPATCH_HEADER_SIZE) {- new_header = true;- type = tail_type;- reserved = tail_reserved;- }- }- if (new_header && type != SHIRUDO_UPATCH_TYPE_BPF)- return -EINVAL;- out->type = type;- out->reserved1 = reserved;- *header_size = new_header ? SHIRUDO_UPATCH_HEADER_SIZE :- SHIRUDO_UPATCH_LEGACY_HEADER_SIZE;-return 0;}-static int section_inside(uint32_t off, uint32_t len, size_t n,- size_t header_size)+static int section_inside(uint32_t off, uint32_t len, size_t n){if (off > n || len > n)return 0;if ((uint64_t)off + (uint64_t)len > (uint64_t)n)return 0;- if (len != 0 && off < header_size)+ if (len != 0 && off < SHIRUDO_UPATCH_HEADER_SIZE)return 0;return 1;}-static int validate_bounds(const struct shirudo_upatch_header *h, size_t n,- size_t header_size)+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,- header_size))+ if (!section_inside(h->loader_insns_off, h->loader_insns_len, n))return -EINVAL;- if (!section_inside(h->signature_off, h->signature_len, n,- header_size))+ if (!section_inside(h->signature_off, h->signature_len, n))return -EINVAL;- if (!section_inside(h->metadata_off, h->metadata_len, n,- header_size))+ if (!section_inside(h->metadata_off, h->metadata_len, n))return -EINVAL;- if (!section_inside(h->data_off, h->data_len, n, header_size))+ if (!section_inside(h->data_off, h->data_len, n))return -EINVAL;if (h->loader_insns_len == 0 || h->signature_len == 0)return -EINVAL;@@ -244,13 +207,12 @@ int shirudo_upatch_open(const char *path, struct shirudo_upatch *out)out->map = m;out->map_size = (size_t)st.st_size;- rc = parse_header(out->map, out->map_size, &out->hdr,- &out->header_size);+ rc = parse_header(out->map, out->map_size, &out->hdr);if (rc != 0) {shirudo_upatch_close(out);return rc;}- rc = validate_bounds(&out->hdr, out->map_size, out->header_size);+ rc = validate_bounds(&out->hdr, out->map_size);if (rc != 0) {shirudo_upatch_close(out);return rc;@@ -265,7 +227,6 @@ void shirudo_upatch_close(struct shirudo_upatch *u)munmap((void *)u->map, u->map_size);u->map = NULL;u->map_size = 0;- u->header_size = 0;u->decoder_blob = NULL;u->decoder_blob_len = 0;u->nr_decoders = 0;diff --git a/util/upatch.h b/util/upatch.hindex 1521f37c..9ba67384 100644--- a/util/upatch.h+++ b/util/upatch.h@@ -17,7 +17,6 @@ struct shirudo_upatch {struct shirudo_upatch_header hdr; /* host byte order */const unsigned char *map;size_t map_size;- size_t header_size;const struct shirudo_upatch_decoder_blob *decoder_blob;uint32_t decoder_blob_len;uint32_t nr_decoders;