eBPF-based security platform
upatch: take the .rodata offset from the signed loader
Daniel Borkmannb92de68f7e7e
diff --git a/Documentation/reference/formats/upatch-format.rst b/Documentation/reference/formats/upatch-format.rstindex 81755178..1fd07f0b 100644--- a/Documentation/reference/formats/upatch-format.rst+++ b/Documentation/reference/formats/upatch-format.rst@@ -663,11 +663,14 @@ signed bytes and trusts others as they are.descriptors in the loader context, and to pin and close the mapdescriptors.* - ``rodata_data_off``- - Bounds-checked only: the ``.rodata`` size from the signed BTF must- fit between it and the end of ``data``. ``bpm info data`` reads- ``.rodata`` at this offset, and the operator's ``.rodata`` rewrites- (``bpm build`` and ``bpm config update`` rodata entries) patch at it- and re-sign.+ - Must equal the offset in ``data`` from which the signed loader's+ ``BPF_MAP_UPDATE_ELEM`` of the ``.rodata`` map takes its value,+ which also holds the ``.rodata`` size from the signed BTF. ``bpm+ info data`` reads ``.rodata`` there, and the operator's ``.rodata``+ rewrites (``bpm build`` and ``bpm config update`` rodata entries)+ patch there and re-sign. A header that names another offset is+ refused, so a package from a registry cannot aim the rewrite at+ other signed bytes.* - ``log_buf_sz``- Ignored.* - ``metadata_off``, ``metadata_len``diff --git a/util/upatch.c b/util/upatch.cindex 9245361d..98add6de 100644--- a/util/upatch.c+++ b/util/upatch.c@@ -324,7 +324,7 @@ static int upatch_parse(const void *data, size_t size,ret = parse_header(out->map, out->map_size, &out->hdr);if (ret)goto err;- out->rodata_offset_valid = true;+ out->rodata_offset_in_header = true;ret = validate_bounds(&out->hdr, out->map_size);if (ret)goto err;diff --git a/util/upatch.h b/util/upatch.hindex 23f51f4d..4a8c6b0b 100644--- a/util/upatch.h+++ b/util/upatch.h@@ -46,7 +46,7 @@ struct upatch {const __u8 *map;size_t map_size;bool owned;- bool rodata_offset_valid;+ bool rodata_offset_in_header;const struct upatch_decoder_blob *decoder_blob;__u32 decoder_blob_len;__u32 nr_decoders;diff --git a/util/upatch_rodata.c b/util/upatch_rodata.cindex f0aaf278..b2f13356 100644--- a/util/upatch_rodata.c+++ b/util/upatch_rodata.c@@ -243,6 +243,7 @@ static int btf_rodata_btf(const struct upatch *u,struct btf *found = NULL;__u32 found_id = 0;size_t i;+ int ret;for (i = 0; i + sizeof(struct btf_header) <= u->hdr.data_len; i++) {const struct btf_header *raw = (const void *)(data + i);@@ -297,24 +298,16 @@ static int btf_rodata_btf(const struct upatch *u,if (!found)return -ENOENT;view->btf = found;- if (u->rodata_offset_valid) {- const struct btf_type *sec = btf__type_by_id(found, found_id);- __u32 size = sec ? sec->size : 0;-- if (!size || u->hdr.rodata_data_off > u->hdr.data_len ||- size > u->hdr.data_len - u->hdr.rodata_data_off) {- btf__free(found);- view->btf = NULL;- return -EINVAL;- }- view->bytes = (const __u8 *)upatch_data(u) +- u->hdr.rodata_data_off;- view->data_offset = u->hdr.rodata_data_off;- view->size = size;- view->type_id = found_id;- return 0;- }- return find_rodata_data(u, found, found_id, view);+ /*+ * The header is not signed: .rodata is where the signed loader+ * takes it from, and a header that names another offset would aim+ * a rewrite the operator then signs at other bytes.+ */+ ret = find_rodata_data(u, found, found_id, view);+ if (!ret && u->rodata_offset_in_header &&+ view->data_offset != u->hdr.rodata_data_off)+ return -EINVAL;+ return ret;}int upatch_rodata_data_offset(const struct upatch *u, __u32 *offset)