eBPF-based security platform
upatch: type the on-disk fields little-endian
Daniel Borkmann23b39eed4988
diff --git a/include/shirudo/upatch.h b/include/shirudo/upatch.hindex f29fc297..2d972edd 100644--- a/include/shirudo/upatch.h+++ b/include/shirudo/upatch.h@@ -34,9 +34,9 @@ enum shirudo_upatch_type {#define UPATCH_PAYLOAD_HASH_LEN 32 /* SHA-256 */struct shirudo_upatch_header {- __u64 magic; /* UPATCH_MAGIC */- __u32 version; /* UPATCH_VERSION */- __u32 flags; /* SHIRUDO_DIRECTIVE_* */+ __le64 magic; /* UPATCH_MAGIC */+ __le32 version; /* UPATCH_VERSION */+ __le32 flags; /* DIRECTIVE_* */char name[UPATCH_NAME_MAX];/** SHA-256 of the signed content, insns || data. Distinct from the@@ -45,20 +45,20 @@ struct shirudo_upatch_header {* the TPM register event, and the unload match target.*/__u8 payload_sha256[UPATCH_PAYLOAD_HASH_LEN];- __u32 loader_insns_off; /* file offset */- __u32 loader_insns_len;- __u32 signature_off;- __u32 signature_len;- __u32 metadata_off;- __u32 metadata_len;- __u32 data_off; /* gen_loader_opts.data */- __u32 data_len;- __u32 log_buf_sz; /* verifier-log buf size */- __u32 nr_maps; /* loader ctx bpf_map_desc count */- __u32 nr_progs; /* loader ctx bpf_prog_desc count */- __u32 rodata_data_off; /* offset in gen_loader_opts.data */- __u32 type; /* enum shirudo_upatch_type */- __u32 reserved1; /* zero; keeps the header 8-byte aligned */+ __le32 loader_insns_off; /* file offset */+ __le32 loader_insns_len;+ __le32 signature_off;+ __le32 signature_len;+ __le32 metadata_off;+ __le32 metadata_len;+ __le32 data_off; /* gen_loader_opts.data */+ __le32 data_len;+ __le32 log_buf_sz; /* verifier-log buf size */+ __le32 nr_maps; /* loader ctx bpf_map_desc count */+ __le32 nr_progs; /* loader ctx bpf_prog_desc count */+ __le32 rodata_data_off; /* offset in gen_loader_opts.data */+ __le32 type; /* enum shirudo_upatch_type */+ __le32 reserved1; /* zero; keeps the header 8-byte aligned */};BUILD_BUG_ON(sizeof(struct shirudo_upatch_header) != UPATCH_HEADER_SIZE);@@ -84,17 +84,17 @@ enum shirudo_upatch_section_tag {};struct shirudo_upatch_signed_section {- __u32 tag; /* enum shirudo_upatch_section_tag */- __u32 reserved; /* zero */- __u32 off; /* from the start of data */- __u32 len;+ __le32 tag; /* enum shirudo_upatch_section_tag */+ __le32 reserved; /* zero */+ __le32 off; /* from the start of data */+ __le32 len;};struct shirudo_upatch_signed_trailer {- __u32 magic; /* UPATCH_SIGNED_TRAILER_MAGIC */- __u32 version; /* UPATCH_SIGNED_VERSION */- __u32 dir_off; /* the section directory, from the start of data */- __u32 nr_sections;+ __le32 magic; /* UPATCH_SIGNED_TRAILER_MAGIC */+ __le32 version; /* UPATCH_SIGNED_VERSION */+ __le32 dir_off; /* the section directory, from the start of data */+ __le32 nr_sections;};/*@@ -114,13 +114,13 @@ enum shirudo_upatch_decoder_export_signal {};struct shirudo_decoder_manifest {- __u32 magic;- __u16 version;- __u16 language;- __u16 api_version;- __u16 input_format; /* UPATCH_DECODER_INPUT_MAP_V1 */- __u16 export_signal;- __u16 reserved;+ __le32 magic;+ __le16 version;+ __le16 language;+ __le16 api_version;+ __le16 input_format; /* UPATCH_DECODER_INPUT_MAP_V1 */+ __le16 export_signal;+ __le16 reserved;char upatch[64];char map[16];char source[64];@@ -128,23 +128,23 @@ struct shirudo_decoder_manifest {};struct shirudo_upatch_decoder_entry {- __u32 language;- __u32 api_version;- __u32 input_format;- __u32 export_signal;+ __le32 language;+ __le32 api_version;+ __le32 input_format;+ __le32 export_signal;char upatch[64];char map[16];char source[64];char entrypoint[32];- __u32 source_off;- __u32 source_len;+ __le32 source_off;+ __le32 source_len;};struct shirudo_upatch_decoder_blob {- __u32 magic;- __u32 version;- __u32 nr_decoders;- __u32 reserved;+ __le32 magic;+ __le32 version;+ __le32 nr_decoders;+ __le32 reserved;struct shirudo_upatch_decoder_entry decoders[];};@@ -184,18 +184,18 @@ enum shirudo_upatch_prog_role {};struct shirudo_upatch_prog_entry {- __u32 role; /* enum shirudo_upatch_prog_role */- __u32 kind; /* enum shirudo_upatch_attach_kind */- __u64 offset; /* kprobe offset; zero for kretprobe */+ __le32 role; /* enum shirudo_upatch_prog_role */+ __le32 kind; /* enum shirudo_upatch_attach_kind */+ __le64 offset; /* kprobe offset; zero for kretprobe */char target[UPATCH_ATTACH_TARGET_MAX]; /* kprobe symbol */char map[UPATCH_PROG_MAP_MAX];};struct shirudo_upatch_prog_metadata {- __u32 magic; /* UPATCH_PROG_MAGIC */- __u32 version; /* UPATCH_PROG_VERSION */- __u32 nr_progs;- __u32 reserved;+ __le32 magic; /* UPATCH_PROG_MAGIC */+ __le32 version; /* UPATCH_PROG_VERSION */+ __le32 nr_progs;+ __le32 reserved;/** Then: nr_progs struct shirudo_upatch_prog_entry entries in* libbpf/gen_loader program iteration order.@@ -211,17 +211,17 @@ struct shirudo_upatch_prog_metadata {* nr_offsets little-endian __u64 file offsets. Records have no padding.*/struct shirudo_upatch_uprobe_targets {- __u32 magic; /* UPATCH_UPROBE_TARGET_MAGIC */- __u32 version; /* UPATCH_UPROBE_TARGET_VERSION */- __u32 nr_targets;- __u32 reserved;+ __le32 magic; /* UPATCH_UPROBE_TARGET_MAGIC */+ __le32 version; /* UPATCH_UPROBE_TARGET_VERSION */+ __le32 nr_targets;+ __le32 reserved;};struct shirudo_upatch_uprobe_target {- __u32 prog_idx;- __u32 path_len;- __u32 build_id_len;- __u32 nr_offsets;+ __le32 prog_idx;+ __le32 path_len;+ __le32 build_id_len;+ __le32 nr_offsets;};diff --git a/testing/helper/test-upatch-prog-metadata.c b/testing/helper/test-upatch-prog-metadata.cindex db011929..8c1d9a68 100644--- a/testing/helper/test-upatch-prog-metadata.c+++ b/testing/helper/test-upatch-prog-metadata.c@@ -150,11 +150,9 @@ static int expect_uprobe_multi(const struct shirudo_upatch *u,return fail("uprobe path or metadata length mismatch");cursor += path_len + build_id_len;for (i = 0; i < nr_offsets; i++) {- __u64 offset;+ __u64 offset = get_le64(cursor);- memcpy(&offset, cursor, sizeof(offset));- cursor += sizeof(offset);- offset = le64_to_cpu(offset);+ cursor += sizeof(__le64);if (i && offset <= previous)return fail("uprobe offsets are not sorted and unique");previous = offset;diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.cindex ab6d4716..ba18e95a 100644--- a/tools/shirudo-build/package.c+++ b/tools/shirudo-build/package.c@@ -1432,7 +1432,7 @@ static int build_prog_metadata(struct bpf_object *obj, __u32 nr_progs,memcpy(cursor, ut->elf.build_id, ut->elf.build_id_len);cursor += ut->elf.build_id_len;for (k = 0; k < ut->elf.nr_offsets; k++) {- __u64 offset = cpu_to_le64(ut->elf.offsets[k]);+ __le64 offset = cpu_to_le64(ut->elf.offsets[k]);memcpy(cursor, &offset, sizeof(offset));cursor += sizeof(offset);diff --git a/util/config_image.c b/util/config_image.cindex b5fbd609..150ccf1a 100644--- a/util/config_image.c+++ b/util/config_image.c@@ -22,14 +22,14 @@/* The standard veritysetup superblock occupies the first 512 bytes. */struct verity_superblock {__u8 magic[8];- __u32 version_le;- __u32 hash_type_le;+ __le32 version;+ __le32 hash_type;__u8 uuid[16];char algorithm[32];- __u32 data_block_size_le;- __u32 hash_block_size_le;- __u64 data_blocks_le;- __u16 salt_size_le;+ __le32 data_block_size;+ __le32 hash_block_size;+ __le64 data_blocks;+ __le16 salt_size;__u8 reserved[6];__u8 salt[256];__u8 padding[168];@@ -91,14 +91,14 @@ static int validate_superblock(const struct verity_superblock *header)static const char algorithm[32] = "sha256";if (memcmp(header->magic, magic, sizeof(magic)) ||- le32_to_cpu(header->version_le) != 1 ||- le32_to_cpu(header->hash_type_le) != 1 ||+ le32_to_cpu(header->version) != 1 ||+ le32_to_cpu(header->hash_type) != 1 ||memcmp(header->algorithm, algorithm, sizeof(algorithm)) ||- le32_to_cpu(header->data_block_size_le) !=+ le32_to_cpu(header->data_block_size) !=CONFIG_IMAGE_BLOCK_SIZE ||- le32_to_cpu(header->hash_block_size_le) !=+ le32_to_cpu(header->hash_block_size) !=CONFIG_IMAGE_BLOCK_SIZE ||- le16_to_cpu(header->salt_size_le) != CONFIG_IMAGE_SALT_SIZE)+ le16_to_cpu(header->salt_size) != CONFIG_IMAGE_SALT_SIZE)return -EINVAL;return 0;}@@ -117,7 +117,7 @@ int shirudo_config_image_read_header(int hash_fd,rc = validate_superblock(&header);if (rc)return rc;- image.data_blocks = le64_to_cpu(header.data_blocks_le);+ image.data_blocks = le64_to_cpu(header.data_blocks);rc = verity_hash_blocks(image.data_blocks, &image.hash_blocks);if (rc)return rc;diff --git a/util/upatch.c b/util/upatch.cindex 519b9531..6df4d520 100644--- a/util/upatch.c+++ b/util/upatch.c@@ -20,7 +20,7 @@#include "upatch.h"static int parse_header(const __u8 *m, size_t n,- struct shirudo_upatch_header *out)+ struct shirudo_upatch_host_header *out){const __u8 *p = m;@@ -92,7 +92,8 @@ static int section_inside(__u32 off, __u32 len, size_t n)return 1;}-static int validate_bounds(const struct shirudo_upatch_header *h, size_t n)+static int validate_bounds(const struct shirudo_upatch_host_header *h,+ size_t n){if (h->type != SHIRUDO_UPATCH_TYPE_BPF)return -EINVAL;@@ -486,8 +487,7 @@ int prog_specs_parse(const void *metadata, __u32 metadata_len,*out = NULL;if (!metadata || metadata_len < sizeof(__u32))return -EINVAL;- memcpy(&magic, metadata, sizeof(magic));- magic = le32_to_cpu(magic);+ magic = get_le32(metadata);if (magic != UPATCH_PROG_MAGIC)return -EINVAL;if (metadata_len < sizeof(md))@@ -664,11 +664,8 @@ int prog_specs_parse(const void *metadata, __u32 metadata_len,}spec->nr_uprobe_offsets = nr_offsets;for (j = 0; j < nr_offsets; j++) {- __u64 offset;-- memcpy(&offset, cursor, sizeof(offset));- cursor += sizeof(offset);- spec->uprobe_offsets[j] = le64_to_cpu(offset);+ spec->uprobe_offsets[j] = get_le64(cursor);+ cursor += sizeof(__le64);if (j && spec->uprobe_offsets[j] <=spec->uprobe_offsets[j - 1])goto out;diff --git a/util/upatch.h b/util/upatch.hindex bb6c690e..09f5d48f 100644--- a/util/upatch.h+++ b/util/upatch.h@@ -18,8 +18,31 @@/* The largest container the parser accepts. */#define UPATCH_FILE_MAX (16u * 1024u * 1024u)+/* struct shirudo_upatch_header as parse_header() reads it, in host order. */+struct shirudo_upatch_host_header {+ __u64 magic;+ __u32 version;+ __u32 flags;+ char name[UPATCH_NAME_MAX];+ __u8 payload_sha256[UPATCH_PAYLOAD_HASH_LEN];+ __u32 loader_insns_off;+ __u32 loader_insns_len;+ __u32 signature_off;+ __u32 signature_len;+ __u32 metadata_off;+ __u32 metadata_len;+ __u32 data_off;+ __u32 data_len;+ __u32 log_buf_sz;+ __u32 nr_maps;+ __u32 nr_progs;+ __u32 rodata_data_off;+ __u32 type;+ __u32 reserved1;+};+struct shirudo_upatch {- struct shirudo_upatch_header hdr; /* host byte order */+ struct shirudo_upatch_host_header hdr;const __u8 *map;size_t map_size;bool mapped;diff --git a/util/upatch_rodata.c b/util/upatch_rodata.cindex 63522277..51b031ef 100644--- a/util/upatch_rodata.c+++ b/util/upatch_rodata.c@@ -241,6 +241,7 @@ static int btf_rodata_btf(const struct shirudo_upatch *u,size_t i;for (i = 0; i + sizeof(struct btf_header) <= u->hdr.data_len; i++) {+ const struct btf_header *raw = (const void *)(data + i);struct btf_header hdr;__u32 end, type_end, str_end, layout_end;struct btf *btf;@@ -250,13 +251,13 @@ static int btf_rodata_btf(const struct shirudo_upatch *u,data[i + 1] != (BTF_MAGIC >> 8))continue;memcpy(&hdr, data + i, sizeof(hdr));- hdr.hdr_len = le32_to_cpu(hdr.hdr_len);- hdr.type_off = le32_to_cpu(hdr.type_off);- hdr.type_len = le32_to_cpu(hdr.type_len);- hdr.str_off = le32_to_cpu(hdr.str_off);- hdr.str_len = le32_to_cpu(hdr.str_len);- hdr.layout_off = le32_to_cpu(hdr.layout_off);- hdr.layout_len = le32_to_cpu(hdr.layout_len);+ hdr.hdr_len = get_le32(&raw->hdr_len);+ hdr.type_off = get_le32(&raw->type_off);+ hdr.type_len = get_le32(&raw->type_len);+ hdr.str_off = get_le32(&raw->str_off);+ hdr.str_len = get_le32(&raw->str_len);+ hdr.layout_off = get_le32(&raw->layout_off);+ hdr.layout_len = get_le32(&raw->layout_len);if (hdr.hdr_len < offsetof(struct btf_header, layout_off) ||hdr.hdr_len > u->hdr.data_len - i)continue;