Skip to content

eBPF-based security platform

upatch: size the policy labels with the overflow helpers

Daniel Borkmann64c339eefeed
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.c
index 68fd2055..27b68e84 100644
--- a/tools/shirudo-build/bpm-build.c
+++ b/tools/shirudo-build/bpm-build.c
@@ -1309,7 +1309,7 @@ static int build_upatches(struct bpm_build_spec *spec, const char *pkey,
}
ret = build_package_load(bpf_o, src_path, load, name,
labels, nr_labels, pkey, cert);
- xfree(labels, (size_t)nr_labels * sizeof(*labels));
+ xfree(labels, size_mul(nr_labels, sizeof(*labels)));
if (bpf_o == object)
unlink(object);
if (ret)
diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.c
index 6311543a..2e689f5c 100644
--- a/tools/shirudo-build/package.c
+++ b/tools/shirudo-build/package.c
@@ -1844,7 +1844,13 @@ int build_package_load(const char *bpf_o, const char *src_path,
goto out;
}
socket_policy_map_idx = map_idx;
- label_len = sizeof(*label_blob) + nr_policies * sizeof(*label_entries);
+ if (check_mul_overflow(nr_policies, sizeof(*label_entries),
+ &label_len) ||
+ check_add_overflow(label_len, sizeof(*label_blob),
+ &label_len)) {
+ err = -EOVERFLOW;
+ goto out;
+ }
label_blob = xcalloc(1, label_len);
if (!label_blob) {
err = -ENOMEM;
diff --git a/util/upatch.c b/util/upatch.c
index 44c3b2e1..2c1aaeaa 100644
--- a/util/upatch.c
+++ b/util/upatch.c
@@ -124,8 +124,9 @@ static bool parse_policy_labels(const __u8 *p, __u32 len, __u32 nr_maps,
bool bind_policy_maps, __u32 *map_idx,
__u32 *socket_map_idx)
{
- struct upatch_policy_label_header h;
__u32 count, version, idx, socket_idx;
+ struct upatch_policy_label_header h;
+ struct upatch_policy_label_entry e;
__u32 header_len = bind_policy_maps ? sizeof(h) :
offsetof(struct upatch_policy_label_header,
socket_policy_map_idx);
@@ -147,11 +148,9 @@ static bool parse_policy_labels(const __u8 *p, __u32 len, __u32 nr_maps,
socket_idx >= nr_maps ||
socket_idx >= UPATCH_MAPS_MAX || idx == socket_idx)) ||
(!bind_policy_maps && (idx || socket_idx)) || !count ||
- (__u64)header_len + (__u64)count *
- sizeof(struct upatch_policy_label_entry) != len)
+ size_add(header_len, size_mul(count, sizeof(e))) != len)
return false;
for (__u32 i = 0; i < count; i++) {
- struct upatch_policy_label_entry e;
__u64 bit;
memcpy(&e, p + header_len + i * sizeof(e), sizeof(e));