Skip to content

eBPF-based security platform

upatch: type the policy label fields little-endian

Daniel Borkmannbdbe46e1b474
diff --git a/builtin-upatch.c b/builtin-upatch.c
index b9ab0b44..91999996 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -2766,11 +2766,11 @@ static void apply_policy_labels(const char *artifact)
if (!labels)
goto out;
header = labels;
- count = le32toh(header->count);
+ count = le32_to_cpu(header->count);
entries = (const void *)((const __u8 *)labels + sizeof(*header));
for (__u32 i = 0; i < count; i++) {
const struct upatch_policy_label_entry *entry = &entries[i];
- __u64 bits = le64toh(entry->bit), current = 0;
+ __u64 bits = le64_to_cpu(entry->bit), current = 0;
ssize_t n;
int fd;
diff --git a/include/shirudo/upatch.h b/include/shirudo/upatch.h
index 58225df7..bb30a063 100644
--- a/include/shirudo/upatch.h
+++ b/include/shirudo/upatch.h
@@ -93,19 +93,22 @@ enum upatch_section_tag {
#define UPATCH_POLICY_PATH_MAX 4096u
struct upatch_policy_label_header {
- __u32 magic;
- __u32 version;
- __u32 count;
- __u32 policy_map_idx;
- __u32 socket_policy_map_idx;
+ __le32 magic;
+ __le32 version;
+ __le32 count;
+ __le32 policy_map_idx;
+ __le32 socket_policy_map_idx;
};
struct upatch_policy_label_entry {
- __u64 bit;
+ __le64 bit;
char policy[UPATCH_POLICY_NAME_MAX];
char path[UPATCH_POLICY_PATH_MAX];
};
+BUILD_BUG_ON(sizeof(struct upatch_policy_label_header) != 20);
+BUILD_BUG_ON(sizeof(struct upatch_policy_label_entry) != 4168);
+
struct upatch_signed_section {
__le32 tag; /* enum upatch_section_tag */
__le32 reserved; /* zero */
diff --git a/testing/helper/test-upatch-prog-metadata.c b/testing/helper/test-upatch-prog-metadata.c
index 29b221d1..63c41a25 100644
--- a/testing/helper/test-upatch-prog-metadata.c
+++ b/testing/helper/test-upatch-prog-metadata.c
@@ -182,17 +182,17 @@ static int expect_policy_labels(const struct upatch *u)
if (!raw || len != sizeof(header) + count * sizeof(entry))
return fail("policy label section length mismatch");
memcpy(&header, raw, sizeof(header));
- if (le32toh(header.magic) != UPATCH_POLICY_LABELS_MAGIC ||
- le32toh(header.version) != UPATCH_POLICY_LABELS_VERSION ||
- le32toh(header.count) != count ||
- le32toh(header.policy_map_idx) != 0 ||
- le32toh(header.socket_policy_map_idx) != 1)
+ if (le32_to_cpu(header.magic) != UPATCH_POLICY_LABELS_MAGIC ||
+ le32_to_cpu(header.version) != UPATCH_POLICY_LABELS_VERSION ||
+ le32_to_cpu(header.count) != count ||
+ le32_to_cpu(header.policy_map_idx) != 0 ||
+ le32_to_cpu(header.socket_policy_map_idx) != 1)
return fail("policy label header mismatch");
cursor = raw + sizeof(header);
for (size_t i = 0; i < count; i++) {
memcpy(&entry, cursor, sizeof(entry));
cursor += sizeof(entry);
- if (le64toh(entry.bit) != bits[i] ||
+ if (le64_to_cpu(entry.bit) != bits[i] ||
strcmp(entry.policy, policies[i]) ||
strcmp(entry.path, paths[i]))
return fail("policy label entry mismatch");
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.c
index 673ef9d3..68fd2055 100644
--- a/tools/shirudo-build/bpm-build.c
+++ b/tools/shirudo-build/bpm-build.c
@@ -1301,7 +1301,7 @@ static int build_upatches(struct bpm_build_spec *spec, const char *pkey,
struct upatch_policy_label_entry *e =
&labels[nr_labels++];
- e->bit = p->bit;
+ e->bit = cpu_to_le64(p->bit);
strscpy(e->policy, p->id, sizeof(e->policy));
strscpy(e->path, p->paths[k], sizeof(e->path));
}
diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.c
index 87d2728b..6311543a 100644
--- a/tools/shirudo-build/package.c
+++ b/tools/shirudo-build/package.c
@@ -1591,9 +1591,9 @@ static int finalize_object(struct bpf_object *obj, const char *out_path,
if (policy_labels_len) {
const struct upatch_policy_label_header *labels =
policy_labels;
- __u32 map_idx = le32toh(labels->policy_map_idx);
+ __u32 map_idx = le32_to_cpu(labels->policy_map_idx);
__u32 socket_map_idx =
- le32toh(labels->socket_policy_map_idx);
+ le32_to_cpu(labels->socket_policy_map_idx);
if (map_idx >= nr_maps || map_idx >= UPATCH_MAPS_MAX ||
socket_map_idx >= nr_maps ||
@@ -1850,17 +1850,15 @@ int build_package_load(const char *bpf_o, const char *src_path,
err = -ENOMEM;
goto out;
}
- label_blob->magic = htole32(UPATCH_POLICY_LABELS_MAGIC);
- label_blob->version = htole32(UPATCH_POLICY_LABELS_VERSION);
- label_blob->count = htole32(nr_policies);
- label_blob->policy_map_idx = htole32(policy_map_idx);
+ label_blob->magic = cpu_to_le32(UPATCH_POLICY_LABELS_MAGIC);
+ label_blob->version = cpu_to_le32(UPATCH_POLICY_LABELS_VERSION);
+ label_blob->count = cpu_to_le32(nr_policies);
+ label_blob->policy_map_idx = cpu_to_le32(policy_map_idx);
label_blob->socket_policy_map_idx =
- htole32(socket_policy_map_idx);
+ cpu_to_le32(socket_policy_map_idx);
label_entries = (void *)(label_blob + 1);
- for (__u32 i = 0; i < nr_policies; i++) {
+ for (__u32 i = 0; i < nr_policies; i++)
label_entries[i] = policies[i];
- label_entries[i].bit = htole64(policies[i].bit);
- }
}
err = finalize_object(obj, out_path, upatch_name, 0, NULL, src_path,
label_blob, label_len, pkey_path, cert_path);
diff --git a/util/identity.h b/util/identity.h
index 8f69316c..7a5114af 100644
--- a/util/identity.h
+++ b/util/identity.h
@@ -64,17 +64,15 @@ struct task_authority {
__u32 pad2;
};
-BUILD_BUG_ON(sizeof(struct device_authority) != 48);
-BUILD_BUG_ON(sizeof(struct task_authority) != 8);
-BUILD_BUG_ON(sizeof(struct identity) != 584);
-
struct task_policy {
__u64 active;
__u64 candidate;
};
-_Static_assert(sizeof(struct task_policy) == 16,
- "task policy ABI");
+BUILD_BUG_ON(sizeof(struct device_authority) != 48);
+BUILD_BUG_ON(sizeof(struct task_authority) != 8);
+BUILD_BUG_ON(sizeof(struct identity) != 584);
+BUILD_BUG_ON(sizeof(struct task_policy) != 16);
/* The bpffs mount init made, which the unmount guard keeps mounted. */
struct mount_record {
diff --git a/util/upatch.c b/util/upatch.c
index 88d35f6b..44c3b2e1 100644
--- a/util/upatch.c
+++ b/util/upatch.c
@@ -134,11 +134,11 @@ static bool parse_policy_labels(const __u8 *p, __u32 len, __u32 nr_maps,
return false;
memset(&h, 0, sizeof(h));
memcpy(&h, p, header_len);
- count = le32toh(h.count);
- version = le32toh(h.version);
- idx = le32toh(h.policy_map_idx);
- socket_idx = le32toh(h.socket_policy_map_idx);
- if (le32toh(h.magic) != UPATCH_POLICY_LABELS_MAGIC ||
+ count = le32_to_cpu(h.count);
+ version = le32_to_cpu(h.version);
+ idx = le32_to_cpu(h.policy_map_idx);
+ socket_idx = le32_to_cpu(h.socket_policy_map_idx);
+ if (le32_to_cpu(h.magic) != UPATCH_POLICY_LABELS_MAGIC ||
version != (bind_policy_maps ?
UPATCH_POLICY_LABELS_VERSION :
UPATCH_POLICY_UNLABELS_VERSION) ||
@@ -155,7 +155,7 @@ static bool parse_policy_labels(const __u8 *p, __u32 len, __u32 nr_maps,
__u64 bit;
memcpy(&e, p + header_len + i * sizeof(e), sizeof(e));
- bit = le64toh(e.bit);
+ bit = le64_to_cpu(e.bit);
if (!bit || (bit & (bit - 1)) ||
!has_nul(e.policy, sizeof(e.policy)) || !e.policy[0] ||
!has_nul(e.path, sizeof(e.path)) || e.path[0] != '/')