Skip to content

eBPF-based security platform

treewide: bound the sizes that could wrap unchecked

Daniel Borkmann7b82e10a9c18
diff --git a/builtin-upatch.c b/builtin-upatch.c
index eeeb706b..3a85710e 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -33,6 +33,7 @@
#include <bpf/libbpf.h>
#include <shirudo/misc.h>
+#include <shirudo/overflow.h>
#include <shirudo/string.h>
#include "builtin.h"
@@ -1942,15 +1943,27 @@ static int __upatch_apply(const char *path, bool verify_only,
* bpf_sys_bpf() cannot BPF_OBJ_PIN. Its descriptors trail the context,
* sized from the header's counts.
*/
- prog_desc_off = sizeof(struct loader_ctx) +
- (size_t)u.hdr.nr_maps * sizeof(struct map_desc);
+ prog_desc_off = size_add(sizeof(struct loader_ctx),
+ size_mul(u.hdr.nr_maps,
+ sizeof(struct map_desc)));
/*
* The loader's max_ctx_offset can exceed the descriptor tail, and
* TEST_RUN rejects a smaller ctx_size_in, so pad generously.
*/
- ctx_sz = prog_desc_off +
- (size_t)u.hdr.nr_progs * sizeof(struct prog_desc) +
- 1024;
+ ctx_sz = size_add(size_add(prog_desc_off,
+ size_mul(u.hdr.nr_progs,
+ sizeof(struct prog_desc))),
+ 1024);
+ /* TEST_RUN takes no larger a context for a syscall program. */
+ if (ctx_sz > UINT16_MAX) {
+ pr_err("upatch: %u maps and %u programs do not fit the loader context",
+ u.hdr.nr_maps, u.hdr.nr_progs);
+ rep_fail(rep, "payload_load", "unknown", E2BIG,
+ "loader context too large");
+ ctx_sz = 0;
+ ret = 1;
+ goto out;
+ }
cx = xmalloc(ctx_sz);
if (!cx) {
pr_err("ctx alloc");
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.c
index 2d80ffe3..a07bc2bb 100644
--- a/tools/shirudo-build/bpm-build.c
+++ b/tools/shirudo-build/bpm-build.c
@@ -1309,6 +1309,8 @@ static int tar_write(struct bpm_tar *tar, const void *data, size_t len)
{
if (!len)
return 0;
+ if (len > INT_MAX)
+ return -EFBIG;
return gzwrite(tar->gz, data, (__u32)len) == (int)len ? 0 : -EIO;
}
diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.c
index db2b8bd5..aad881db 100644
--- a/tools/shirudo-build/bpm-publish.c
+++ b/tools/shirudo-build/bpm-publish.c
@@ -18,6 +18,7 @@
#include <curl/curl.h>
#include <shirudo/misc.h>
+#include <shirudo/overflow.h>
#include <shirudo/string.h>
#include "bpm.h"
@@ -726,7 +727,12 @@ static int cmd_bpm_tree_append(int argc, char **argv)
ret = -EINVAL;
goto out;
}
- pub.generation++;
+ if (check_add_overflow(pub.generation, 1, &pub.generation) ||
+ pub.generation == INT64_MAX) {
+ pr_err("bpm tree append: the tree's generation is exhausted");
+ ret = -EOVERFLOW;
+ goto out;
+ }
}
pub.schema = 1;
if (!pub.tree_id[0])
@@ -744,9 +750,16 @@ static int cmd_bpm_tree_append(int argc, char **argv)
const char *member;
__u8 *body = NULL;
size_t body_len;
+ __s64 seq;
if (!u->tree.present)
continue;
+ if (check_add_overflow(pub.head_seq, 1, &seq) ||
+ seq == INT64_MAX) {
+ pr_err("bpm tree append: the tree's sequence is exhausted");
+ ret = -EOVERFLOW;
+ goto out;
+ }
member = unload ? u->tree.unload_artifact : u->tree.load_artifact;
ret = bpm_pkg_read_member_in(package, member, package_dir, NULL, 0,
&body, &body_len, BPM_MAX_HTTP_BODY);
@@ -754,7 +767,7 @@ static int cmd_bpm_tree_append(int argc, char **argv)
pr_err("bpm tree append: package entry %s not found", member);
goto out;
}
- *op = (struct tree_operation){ .seq = pub.head_seq + 1 };
+ *op = (struct tree_operation){ .seq = seq };
bpm_copy_string(op->op, sizeof(op->op), operation);
bpm_copy_string(op->id, sizeof(op->id), u->tree.id);
if (u->tree.selector)
diff --git a/util/attest.c b/util/attest.c
index b16f231b..57846070 100644
--- a/util/attest.c
+++ b/util/attest.c
@@ -18,6 +18,7 @@
#include <unistd.h>
#include <shirudo/misc.h>
+#include <shirudo/overflow.h>
#include <shirudo/string.h>
#include "attest.h"
@@ -291,7 +292,7 @@ int attest_evidence_config(const __u8 *nonce, size_t nonce_len,
need = 64 * 1024 + fw_cap + sd_cap + ek_b64_cap +
config.n_events * 160;
if (stat(eventlog_path(), &st) == 0 && st.st_size > 0)
- need += 2 * (size_t)st.st_size;
+ need = size_add(need, size_mul(2, st.st_size));
if (need > JSON_MAX_BYTES)
need = JSON_MAX_BYTES;
*out = xmalloc(need);
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index d46b2894..97bcc284 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -1143,6 +1143,8 @@ int bpm_trim(const char *in, char *out, size_t out_sz)
const char *start = in, *end;
int ret = 0;
+ if (!out_sz)
+ return -E2BIG;
while (*start && isspace((__u8)*start))
start++;
end = start + strlen(start);
diff --git a/util/hex.c b/util/hex.c
index 27898501..a7a025f0 100644
--- a/util/hex.c
+++ b/util/hex.c
@@ -5,6 +5,7 @@
#include <string.h>
#include <shirudo/misc.h>
+#include <shirudo/overflow.h>
#include "hex.h"
#include "malloc.h"
@@ -23,7 +24,7 @@ void hex_encode(const __u8 *in, size_t n, char *out)
char *hex_alloc(const void *in, size_t n)
{
- char *out = xmalloc(2 * n + 1);
+ char *out = xmalloc(size_add(size_mul(n, 2), 1));
if (out)
hex_encode(in, n, out);
@@ -45,7 +46,7 @@ int hex_decode(const char *hex, __u8 *out, size_t n)
{
size_t i;
- if (strlen(hex) != 2 * n)
+ if (strlen(hex) != size_mul(n, 2))
return -EINVAL;
for (i = 0; i < n; i++) {
int hi = hex_nibble(hex[2 * i]);
diff --git a/util/kmodules.c b/util/kmodules.c
index 3f0bf05b..5b46ce1f 100644
--- a/util/kmodules.c
+++ b/util/kmodules.c
@@ -172,6 +172,12 @@ int kmodules_parse(char *buf, struct kmod_set *set)
}
qsort(set->mods, set->n, sizeof(*set->mods), kmod_cmp);
+ /* A module index and a dependency offset are __u16. */
+ if (set->n > UINT16_MAX) {
+ ret = -E2BIG;
+ goto out;
+ }
+
/*
* The fourth column of a line names who uses the module, so each
* entry there is an edge from that user down to the module.
@@ -247,6 +253,10 @@ int kmodules_parse(char *buf, struct kmod_set *set)
for (j = 0; j < n_edges; j++) {
if (edges[j].user != i)
continue;
+ if (set->n_deps >= UINT16_MAX) {
+ ret = -E2BIG;
+ goto out;
+ }
if (set->n_deps == deps_cap) {
void *np = xreallocarray(set->deps, deps_cap,
deps_cap * 2,
diff --git a/util/upatch_maps.c b/util/upatch_maps.c
index b4fe0d63..2e64e715 100644
--- a/util/upatch_maps.c
+++ b/util/upatch_maps.c
@@ -1645,7 +1645,7 @@ void upatch_maps_emit_json_fields(struct json_emit *e,
json_kv_int(e, "bytes", page->bytes);
json_kv_string(e, "data_hex",
hex ? hex : "");
- xfree(hex, (size_t)page->bytes * 2 + 1);
+ xfree(hex, size_add(size_mul(page->bytes, 2), 1));
json_obj_close(e);
}
json_arr_close(e);
diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.c
index 81d3bdc2..db5e1020 100644
--- a/util/upatch_maps_otel.c
+++ b/util/upatch_maps_otel.c
@@ -725,7 +725,7 @@ static void emit_log_map_data(struct json_emit *e,
otlp_kv_int(e, "chunks", page->chunks);
otlp_kv_int(e, "bytes", page->bytes);
otlp_kv_string(e, "data_hex", hex ? hex : "");
- xfree(hex, (size_t)page->bytes * 2 + 1);
+ xfree(hex, size_add(size_mul(page->bytes, 2), 1));
otlp_array_kvlist_end(e);
}
otlp_array_end(e);
@@ -1288,10 +1288,14 @@ static int gzip_data(const char *in, size_t in_len,
__u8 **out, size_t *out_len)
{
z_stream z = {};
- size_t cap = compressBound(in_len) + 32;
__u8 *buf;
+ size_t cap;
int zr;
+ /* zlib counts in uInt; a longer input would be cut short silently. */
+ if (in_len > UINT_MAX / 2)
+ return -EFBIG;
+ cap = size_add(compressBound(in_len), 32);
buf = xmalloc(cap);
if (!buf)
return -ENOMEM;
@@ -1492,9 +1496,11 @@ static int otel_post(struct otel_state *st, const char *json, size_t json_len)
__u8 *compressed = NULL;
const void *body = json;
size_t body_len = json_len, compressed_cap = 0;
- __u32 attempt, max_attempts = st->opts.retries + 1;
+ __u32 attempt, max_attempts;
int ret = -EIO;
+ if (check_add_overflow(st->opts.retries, 1, &max_attempts))
+ return -EINVAL;
if (!st->s3_reference && st->opts.otlp_max_bytes &&
json_len > st->opts.otlp_max_bytes)
return -E2BIG;
@@ -2303,10 +2309,12 @@ static int s3_put(struct otel_s3_state *st,
{
struct curl_slist *headers = NULL;
char sigv4[128];
- __u32 max_attempts = st->opts.retries + 1;
+ __u32 max_attempts;
int ret;
*already_present = false;
+ if (check_add_overflow(st->opts.retries, 1, &max_attempts))
+ return -EINVAL;
ret = s3_headers(st, v, snapshot, &headers);
if (ret) {
curl_slist_free_all(headers);
diff --git a/util/upatch_rodata.c b/util/upatch_rodata.c
index 11736055..6854affe 100644
--- a/util/upatch_rodata.c
+++ b/util/upatch_rodata.c
@@ -14,6 +14,7 @@
#include <bpf/libbpf.h>
#include <shirudo/misc.h>
+#include <shirudo/overflow.h>
#include <shirudo/string.h>
#include "io.h"
@@ -125,8 +126,12 @@ static bool map_update_value(const struct bpf_insn *insns, size_t count,
size_t call, __u32 attr, __u32 *value_off)
{
size_t start = call > 24 ? call - 24 : 0;
+ __u32 value_field;
size_t i;
- __u32 value_field = attr + offsetof(union bpf_attr, value);
+
+ if (check_add_overflow(attr, offsetof(union bpf_attr, value),
+ &value_field))
+ return false;
for (i = start; i + 4 < call; i++) {
__u32 source_off, dest_off;