eBPF-based security platform
upatch, otel: copy strings with strscpy()
Daniel Borkmann084af810fc8c
diff --git a/builtin-upatch.c b/builtin-upatch.cindex d82b9daa..6e48f35d 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -83,7 +83,7 @@ static void rep_fail(struct shirudo_op_report *rep, const char *phase,rep->category = cat;rep->err = err < 0 ? -err : err;if (msg)- snprintf(rep->message, sizeof(rep->message), "%s", msg);+ strscpy(rep->message, msg, sizeof(rep->message));}/*@@ -651,7 +651,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;- snprintf(attr.map_name, sizeof(attr.map_name), "%s", name);+ strscpy(attr.map_name, name, sizeof(attr.map_name));fd = syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));return (int)fd;@@ -789,8 +789,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]) {rc = -ENOENT;+ } else if (strlen(name) >= cap) {+ rc = -ENAMETOOLONG;} else {- snprintf(out, cap, "%s", name);+ strscpy(out, name, cap);rc = 0;}btf__free(btf);@@ -824,7 +826,7 @@ static int prog_attach_cgroup_skb_fallback(int prog_fd,char func[128];int rc;- snprintf(func, sizeof(func), "%s", info->name);+ strscpy(func, info->name, sizeof(func));rc = prog_func_name(prog_fd, info, func, sizeof(func));if (rc && !func[0]) {pr_err("program metadata says raw_tracepoint, but loaded "diff --git a/util/upatch_maps.c b/util/upatch_maps.cindex f6016a84..27386c01 100644--- a/util/upatch_maps.c+++ b/util/upatch_maps.c@@ -23,6 +23,7 @@#include <bpf/libbpf.h>#include <shirudo/misc.h>+#include <shirudo/string.h>#include <shirudo/upatch.h>#include "json.h"@@ -110,7 +111,7 @@ static void btf_type_name(struct btf *btf, __u32 type_id,return;name = btf__name_by_offset(btf, t->name_off);if (name && name[0]) {- snprintf(dst, dst_cap, "%s", name);+ strscpy(dst, name, dst_cap);return;}/*@@ -127,7 +128,7 @@ static void btf_type_name(struct btf *btf, __u32 type_id,break;name = btf__name_by_offset(btf, t->name_off);if (name && name[0]) {- snprintf(dst, dst_cap, "%s", name);+ strscpy(dst, name, dst_cap);return;}id = t->type;@@ -1251,8 +1252,9 @@ int upatch_maps_read_flags(const char *upatch, const char *map,int rc;memset(out, 0, sizeof(*out));- snprintf(out->upatch, sizeof(out->upatch), "%s", upatch);- snprintf(out->name, sizeof(out->name), "%s", map);+ if (strscpy(out->upatch, upatch, sizeof(out->upatch)) < 0 ||+ strscpy(out->name, map, sizeof(out->name)) < 0)+ return -ENAMETOOLONG;rc = open_map_pin(upatch, map, &fd, &info);if (rc)@@ -1365,7 +1367,7 @@ static void scan_upatch_maps(const char *upatch,if (rc)continue;close(fd);- snprintf(one.upatch, sizeof(one.upatch), "%s", upatch);+ strscpy(one.upatch, upatch, sizeof(one.upatch));snprintf(one.name, sizeof(one.name), "%.*s",(int)(sizeof(one.name) - 1), e->d_name);one.type = info.type;diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.cindex 2596bba6..30c01134 100644--- a/util/upatch_maps_otel.c+++ b/util/upatch_maps_otel.c@@ -907,7 +907,7 @@ static int metric_name(const struct otel_state *st,if (st->opts.metric_name) {if (!valid_metric_name(st->opts.metric_name))return -EINVAL;- snprintf(out, 256, "%s", st->opts.metric_name);+ strscpy(out, st->opts.metric_name, 256);return 0;}n = snprintf(out, 256, "shirudo.%s.%s", v->upatch, v->name);@@ -1207,17 +1207,15 @@ static int resolve_endpoint(struct otel_state *st, const char *spec,if (spec_is_base)return append_signal_path(spec, st->signal, st->endpoint,sizeof(st->endpoint));- if (strlen(spec) >= sizeof(st->endpoint))+ if (strscpy(st->endpoint, spec, sizeof(st->endpoint)) < 0)return -ENAMETOOLONG;- snprintf(st->endpoint, sizeof(st->endpoint), "%s", spec);return 0;}signal_endpoint = otel_signal_env(st->signal, "ENDPOINT");if (signal_endpoint) {- if (strlen(signal_endpoint) >= sizeof(st->endpoint))+ if (strscpy(st->endpoint, signal_endpoint,+ sizeof(st->endpoint)) < 0)return -ENAMETOOLONG;- snprintf(st->endpoint, sizeof(st->endpoint), "%s",- signal_endpoint);return 0;}generic_endpoint = getenv("OTEL_EXPORTER_OTLP_ENDPOINT");@@ -1256,7 +1254,10 @@ static int validate_endpoint_security(struct otel_state *st)rc = -EINVAL;goto out;}- snprintf(st->host, sizeof(st->host), "%s", host);+ if (strscpy(st->host, host, sizeof(st->host)) < 0) {+ rc = -ENAMETOOLONG;+ goto out;+ }if (strcmp(scheme, "http") == 0) {if (!loopback_host(host) && !st->opts.allow_insecure) {rc = -EPERM;@@ -2250,7 +2251,7 @@ static int parse_s3_spec(struct otel_s3_state *st, const char *spec)region = "us-east-1";if (!valid_region(region))return -EINVAL;- snprintf(st->region, sizeof(st->region), "%s", region);+ strscpy(st->region, region, sizeof(st->region));endpoint = getenv("AWS_ENDPOINT_URL_S3");if (endpoint && endpoint[0]) {size_t len = strlen(endpoint);@@ -2318,11 +2319,10 @@ static int validate_s3_endpoint(struct otel_s3_state *st)rc = -EINVAL;goto out;}- if (strlen(host) >= sizeof(st->host)) {+ if (strscpy(st->host, host, sizeof(st->host)) < 0) {rc = -ENAMETOOLONG;goto out;}- snprintf(st->host, sizeof(st->host), "%s", host);if (strcmp(scheme, "http") == 0) {if (!loopback_host(host) && !st->opts.allow_insecure) {rc = -EPERM;diff --git a/util/upatch_resolve.c b/util/upatch_resolve.cindex 9074db40..a9d14e58 100644--- a/util/upatch_resolve.c+++ b/util/upatch_resolve.c@@ -20,6 +20,7 @@#include <bpf/libbpf.h>#include <shirudo/misc.h>+#include <shirudo/string.h>#include <shirudo/upatch.h>#include "malloc.h"@@ -185,7 +186,7 @@ int upatch_resolve_location(struct btf *vbtf, const char *name,if (attach_name && *attach_name)snprintf(out, cap, "%s/%s", prefix, attach_name);else- snprintf(out, cap, "%s", prefix);+ strscpy(out, prefix, cap);return 0;}