Skip to content

eBPF-based security platform

upatch: check every string copy of the upatch commands and map views

Daniel Borkmannf673f3e165f5
diff --git a/builtin-upatch.c b/builtin-upatch.c
index bd70ac68..7ca771bb 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -90,8 +90,8 @@ static void rep_fail(struct op_report *rep, const char *phase,
rep->phase = phase;
rep->category = cat;
rep->err = err < 0 ? -err : err;
- if (msg)
- strscpy(rep->message, msg, sizeof(rep->message));
+ if (msg && strscpy(rep->message, msg, sizeof(rep->message)) < 0)
+ memcpy(rep->message + sizeof(rep->message) - 4, "...", 4);
}
/*
@@ -573,7 +573,8 @@ static int cmd_upatch_info(int argc, const char **argv)
goto out;
}
- strscpy(file, path, sizeof(file));
+ if (strscpy(file, path, sizeof(file)) < 0)
+ strscpy_literal(file, "?");
printf("File: %s\n", printable(file));
printf("Name: %s\n", printable(name));
printf("Type: %s\n", flag_str(&u));
@@ -849,8 +850,7 @@ struct prog_desc {
int prog_fd;
};
-static int loader_map_create(const char *name, __u32 value_size,
- const void *excl_prog_hash,
+static int loader_map_create(__u32 value_size, const void *excl_prog_hash,
__u32 excl_prog_hash_sz)
{
union bpf_attr attr = {};
@@ -862,7 +862,7 @@ static int loader_map_create(const char *name, __u32 value_size,
attr.max_entries = 1;
attr.excl_prog_hash = ptr_to_u64(excl_prog_hash);
attr.excl_prog_hash_size = excl_prog_hash_sz;
- strscpy(attr.map_name, name, sizeof(attr.map_name));
+ strscpy_literal(attr.map_name, "__loader.map");
fd = syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
return (int)fd;
@@ -1000,10 +1000,10 @@ static int prog_func_name(int prog_fd, const struct bpf_prog_info *base,
name = t ? btf__name_by_offset(btf, t->name_off) : NULL;
if (!name || !name[0]) {
ret = -ENOENT;
- } else if (strlen(name) >= cap) {
+ } else if (strscpy(out, name, cap) < 0) {
+ out[0] = '\0';
ret = -ENAMETOOLONG;
} else {
- strscpy(out, name, cap);
ret = 0;
}
btf__free(btf);
@@ -2312,8 +2312,8 @@ static int loader_map_prepare(struct signed_load *sl)
"loader digest failed");
return 1;
}
- sl->map_fd = loader_map_create("__loader.map", u->hdr.data_len,
- loader_digest, sizeof(loader_digest));
+ sl->map_fd = loader_map_create(u->hdr.data_len, loader_digest,
+ sizeof(loader_digest));
if (sl->map_fd < 0)
return loader_map_failed(rep, "__loader.map create",
"__loader.map create failed");
@@ -3514,7 +3514,7 @@ static int java_unload_locked(const char *name,
java_options_sync("unload");
rep->ok = true;
rep->category = "already_unloaded";
- strscpy(rep->message, "already unloaded", sizeof(rep->message));
+ strscpy_literal(rep->message, "already unloaded");
return 0;
}
if (!err) {
@@ -3734,7 +3734,7 @@ static int cmd_upatch_unload(int argc, const char **argv)
rep.ok = true;
rep.phase = "done";
rep.category = "already_unloaded";
- strscpy(rep.message, "already unloaded", sizeof(rep.message));
+ strscpy_literal(rep.message, "already unloaded");
ret = 0;
goto done;
}
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index 67416a9a..4a226fbf 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -31,7 +31,7 @@ upatch-loader --unwind 50 LEN=4,5,25,49
upatch-rodata-btf --refine-arithmetic --object-bits 10 --unwind 16 LEN=4,10,15
upatch-btf --unwind 6 --unwindset btf_decl_printable.0:33,btf_name_printable.0:7,btf_unwrap.0:9
upatch-btf-int --unwind 2 --unwindset btf_integer.0:65,value_of_bits.0:65
-upatch-rodata --refine-arithmetic --object-bits 12 --max-field-sensitivity-array-size 32 --unwind 25 --unwindset memcpy.0:49,strlen.0:18,strcmp.0:18,strnlen.0:18 LEN=6,8,24
+upatch-rodata --refine-arithmetic --object-bits 12 --max-field-sensitivity-array-size 32 --unwind 25 --unwindset memcpy.0:49,strlen.0:18,strcmp.0:18 LEN=6,8,24
string-number-model --unwind 6
measured-fold-model --unwind 7
bpm-tree-follows --unwind 4
diff --git a/util/kernel_abi.c b/util/kernel_abi.c
index 0b8ed539..348584b3 100644
--- a/util/kernel_abi.c
+++ b/util/kernel_abi.c
@@ -37,8 +37,9 @@ static __printf(3, 4) int abi_fail(char *detail, size_t detail_sz,
if (detail && detail_sz) {
va_start(ap, fmt);
- if (vstrsprintf(detail, detail_sz, fmt, ap) < 0)
- strscpy(detail, "?", detail_sz);
+ if (vstrsprintf(detail, detail_sz, fmt, ap) < 0 &&
+ strscpy(detail, "?", detail_sz) < 0)
+ detail[0] = '\0';
va_end(ap);
}
return -EOPNOTSUPP;
diff --git a/util/upatch_maps.c b/util/upatch_maps.c
index c537cc5f..4965b02f 100644
--- a/util/upatch_maps.c
+++ b/util/upatch_maps.c
@@ -98,23 +98,19 @@ static bool is_hash_type(__u32 type)
type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
}
-static void btf_type_name(struct btf *btf, __u32 type_id,
- char *dst, size_t dst_cap)
+static const char *btf_type_name(struct btf *btf, __u32 type_id)
{
const struct btf_type *t;
const char *name;
- dst[0] = '\0';
if (!btf || !type_id)
- return;
+ return "";
t = btf__type_by_id(btf, type_id);
if (!t)
- return;
+ return "";
name = btf__name_by_offset(btf, t->name_off);
- if (name && name[0]) {
- strscpy(dst, name, dst_cap);
- return;
- }
+ if (name && name[0])
+ return name;
/*
* Anonymous types: walk one indirection to pick up a useful
* label (e.g. unnamed PTR -> named STRUCT). Bounded loop.
@@ -128,15 +124,14 @@ static void btf_type_name(struct btf *btf, __u32 type_id,
if (!t)
break;
name = btf__name_by_offset(btf, t->name_off);
- if (name && name[0]) {
- strscpy(dst, name, dst_cap);
- return;
- }
+ if (name && name[0])
+ return name;
id = t->type;
if (!id)
break;
}
}
+ return "";
}
static __u32 btf_unwrap(const struct btf *btf, __u32 id)
@@ -1282,10 +1277,14 @@ int upatch_maps_read(const char *upatch, const char *map,
out->key_type_id = info.btf_key_type_id;
out->value_type_id = info.btf_value_type_id;
- btf_type_name(out->btf, info.btf_key_type_id,
- out->btf_key_type, sizeof(out->btf_key_type));
- btf_type_name(out->btf, info.btf_value_type_id,
- out->btf_value_type, sizeof(out->btf_value_type));
+ if (strscpy(out->btf_key_type,
+ btf_type_name(out->btf, info.btf_key_type_id),
+ sizeof(out->btf_key_type)) < 0)
+ strscpy_literal(out->btf_key_type, "?");
+ if (strscpy(out->btf_value_type,
+ btf_type_name(out->btf, info.btf_value_type_id),
+ sizeof(out->btf_value_type)) < 0)
+ strscpy_literal(out->btf_value_type, "?");
off = 0;
out->key_type_def[0] = '\0';
btf_type_signature_into(out->btf, info.btf_key_type_id,
@@ -1345,8 +1344,9 @@ static void scan_upatch_maps(const char *upatch,
if (ret)
continue;
close(fd);
- strscpy(one.upatch, upatch, sizeof(one.upatch));
- strscpy(one.name, e->d_name, sizeof(one.name));
+ if (strscpy(one.upatch, upatch, sizeof(one.upatch)) < 0 ||
+ strscpy(one.name, e->d_name, sizeof(one.name)) < 0)
+ continue;
one.type = info.type;
one.type_name = map_type_name(info.type);
one.max_entries = info.max_entries;
diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.c
index 50fcefa3..de48aab1 100644
--- a/util/upatch_maps_otel.c
+++ b/util/upatch_maps_otel.c
@@ -151,38 +151,36 @@ static int metric_type_from_view(const struct map_view *v,
return 0;
}
+/* Why the map exports as logs, or NULL when its values make a metric. */
+static const char *otel_log_reason(const struct map_view *v,
+ struct metric_type *mt)
+{
+ if (v->serialized)
+ return "map uses a serializer";
+ if (v->skipped)
+ return "map type is not generically readable";
+ if (!upatch_map_is_percpu(v->type))
+ return "map is not per-CPU";
+ if (metric_type_from_view(v, mt))
+ return "value is not a BTF integer scalar";
+ if (mt->is_signed)
+ return "value is signed";
+ return NULL;
+}
+
int upatch_maps_otel_classify(const struct map_view *v,
enum otel_signal *signal,
char *reason, size_t reason_sz)
{
struct metric_type mt;
- int ret;
+ const char *why;
if (!v || !signal)
return -EINVAL;
*signal = OTEL_LOG;
- if (v->serialized) {
- strscpy(reason, "map uses a serializer", reason_sz);
- return 0;
- }
- if (v->skipped) {
- strscpy(reason, "map type is not generically readable",
- reason_sz);
- return 0;
- }
- if (!upatch_map_is_percpu(v->type)) {
- strscpy(reason, "map is not per-CPU", reason_sz);
- return 0;
- }
- ret = metric_type_from_view(v, &mt);
- if (ret) {
- strscpy(reason, "value is not a BTF integer scalar", reason_sz);
- return 0;
- }
- if (mt.is_signed) {
- strscpy(reason, "value is signed", reason_sz);
- return 0;
- }
+ why = otel_log_reason(v, &mt);
+ if (why)
+ return strscpy(reason, why, reason_sz) < 0 ? -E2BIG : 0;
*signal = OTEL_METRIC;
if (strsprintf(reason, reason_sz, "unsigned per-CPU %u-bit scalar",
mt.width * 8) < 0)
@@ -289,7 +287,7 @@ static void emit_resource_attributes(struct json_emit *e)
char hostname[256] = "unknown";
if (gethostname(hostname, sizeof(hostname)) != 0)
- strscpy(hostname, "unknown", sizeof(hostname));
+ strscpy_literal(hostname, "unknown");
hostname[sizeof(hostname) - 1] = '\0';
json_arr_open(e, "attributes");
otlp_kv_string(e, "service.name", "shirudo");
@@ -1855,7 +1853,7 @@ static int build_s3_urls(const struct otel_s3_state *st,
int n, ret;
if (gethostname(hostname, sizeof(hostname)) != 0)
- strscpy(hostname, "unknown", sizeof(hostname));
+ strscpy_literal(hostname, "unknown");
hostname[sizeof(hostname) - 1] = '\0';
safe_key_segment(hostname);
n = strsprintf(key, sizeof(key), "%s%s%s/%s/%s/%s.json.gz",
diff --git a/util/upatch_resolve.c b/util/upatch_resolve.c
index 670349e9..e27437ac 100644
--- a/util/upatch_resolve.c
+++ b/util/upatch_resolve.c
@@ -259,6 +259,15 @@ static int prog_info_by_id(__u32 prog_id, struct bpf_prog_info *info)
return ret;
}
+/* Empty when text does not fit, as strsprintf() leaves a location. */
+static int location_copy(char *out, size_t cap, const char *text)
+{
+ if (strscpy(out, text, cap) >= 0)
+ return 0;
+ out[0] = '\0';
+ return -E2BIG;
+}
+
/* pinfo is the program's info when the caller has read it already. */
static int link_location(struct btf *vbtf, int link_fd,
const struct bpf_link_info *linfo,
@@ -284,10 +293,8 @@ static int link_location(struct btf *vbtf, int link_fd,
if (linfo->type == BPF_LINK_TYPE_NETFILTER &&
linfo->netfilter.pf == NFPROTO_IPV6 &&
linfo->netfilter.hooknum == NF_INET_PRE_ROUTING &&
- linfo->netfilter.priority == NF_IP6_PRI_RAW_BEFORE_DEFRAG) {
- strscpy(out, "netfilter/ipv6-pre-routing", cap);
- return 0;
- }
+ linfo->netfilter.priority == NF_IP6_PRI_RAW_BEFORE_DEFRAG)
+ return location_copy(out, cap, "netfilter/ipv6-pre-routing");
if (!pinfo) {
ret = prog_info_by_id(linfo->prog_id, &info);
@@ -318,8 +325,7 @@ static int link_location(struct btf *vbtf, int link_fd,
if (attach_name && *attach_name)
return strsprintf(out, cap, "%s/%s", prefix, attach_name) < 0 ?
-E2BIG : 0;
- strscpy(out, prefix, cap);
- return 0;
+ return location_copy(out, cap, prefix);
}
int upatch_resolve_location(struct btf *vbtf, const char *name,
diff --git a/util/upatch_rodata.c b/util/upatch_rodata.c
index b12cf686..cf33867d 100644
--- a/util/upatch_rodata.c
+++ b/util/upatch_rodata.c
@@ -209,12 +209,12 @@ static int find_rodata_data(const struct upatch *u,
if (map_name_rodata(name)) {
__u32 slot;
+ BUILD_BUG_ON(sizeof(maps[0].name) != sizeof(name));
if (!map_create_slot(insns, count, i, end, &slot))
return -EOPNOTSUPP;
maps[slot].value_size = attr.value_size;
maps[slot].found = true;
- strscpy(maps[slot].name, name,
- sizeof(maps[slot].name));
+ memcpy(maps[slot].name, name, sizeof(name));
}
} else if (cmd == BPF_MAP_UPDATE_ELEM) {
__u32 slot, value_field, value_off;