Skip to content

eBPF-based security platform

treewide: copy constant text with strscpy()

Daniel Borkmann490ccdac6442
diff --git a/builtin-keys.c b/builtin-keys.c
index 5d6d3456..554968ef 100644
--- a/builtin-keys.c
+++ b/builtin-keys.c
@@ -94,7 +94,7 @@ static EVP_PKEY *read_priv_pem(const char *path)
static void name_to_str(X509_NAME *name, char *out, size_t out_sz)
{
if (!X509_NAME_oneline(name, out, (int)out_sz)) {
- snprintf(out, out_sz, "(parse error)");
+ strscpy(out, "(parse error)", out_sz);
return;
}
}
@@ -104,11 +104,11 @@ static void asn1_time_to_iso(const ASN1_TIME *t, char *out, size_t out_sz)
struct tm tm;
if (!ASN1_TIME_to_tm(t, &tm)) {
- snprintf(out, out_sz, "(unparseable)");
+ strscpy(out, "(unparseable)", out_sz);
return;
}
if (!strftime(out, out_sz, "%Y-%m-%dT%H:%M:%SZ", &tm))
- snprintf(out, out_sz, "(format error)");
+ strscpy(out, "(format error)", out_sz);
}
static long days_remaining(const ASN1_TIME *not_after)
diff --git a/builtin-status.c b/builtin-status.c
index e1735b24..b66bf464 100644
--- a/builtin-status.c
+++ b/builtin-status.c
@@ -409,8 +409,7 @@ static size_t build_upatches_table(struct dirent **ups, int n_ups,
ups[i]->d_name) == 0)
name = raw_name;
- snprintf(r->name, sizeof(r->name), "%.*s",
- (int)(sizeof(r->name) - 1), name);
+ strscpy(r->name, name, sizeof(r->name));
upatch_resolve_location(vbtf, name,
r->location,
sizeof(r->location));
diff --git a/builtin-upatch.c b/builtin-upatch.c
index a3640192..f5a9fbdd 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -2598,7 +2598,7 @@ static int cmd_upatch_unload(int argc, const char **argv)
rep.ok = true;
rep.phase = "done";
rep.category = "already_unloaded";
- snprintf(rep.message, sizeof(rep.message), "already unloaded");
+ strscpy(rep.message, "already unloaded", sizeof(rep.message));
ret = 0;
goto done;
}
diff --git a/tools/shirudo-build/compile.c b/tools/shirudo-build/compile.c
index 11cf5ee2..d2ef5cb9 100644
--- a/tools/shirudo-build/compile.c
+++ b/tools/shirudo-build/compile.c
@@ -90,7 +90,7 @@ int shirudo_build_compile(const char *source, const char *output,
if (slash)
*slash = '\0';
else
- snprintf(vmlinux_dir, sizeof(vmlinux_dir), ".");
+ strscpy(vmlinux_dir, ".", sizeof(vmlinux_dir));
}
snprintf(inc_vmlinux, sizeof(inc_vmlinux), "-I%s", vmlinux_dir);
diff --git a/util/attest.c b/util/attest.c
index c124645b..a89e2bb3 100644
--- a/util/attest.c
+++ b/util/attest.c
@@ -550,7 +550,7 @@ static int on_record(void *ctx)
p->n_recs = 0; /* this boot's register starts here */
r = &p->recs[0];
memset(r, 0, sizeof(*r));
- snprintf(r->type, sizeof(r->type), "init");
+ strscpy(r->type, "init", sizeof(r->type));
memcpy(r->digest, in->digest, 32);
} else if (!strncmp(p->type, "upatch_", 7)) {
strscpy(r->label, p->upatch_name, sizeof(r->label));
@@ -621,7 +621,7 @@ static void note(struct reporter *r, const char *fmt, ...)
if (r->len + 8 >= r->cap)
return;
- n = snprintf(r->buf + r->len, r->cap - r->len, "note: ");
+ n = strscpy(r->buf + r->len, "note: ", r->cap - r->len);
if (n > 0)
r->len += (size_t)n;
va_start(ap, fmt);
diff --git a/util/conditions.c b/util/conditions.c
index 0f14714f..b2493352 100644
--- a/util/conditions.c
+++ b/util/conditions.c
@@ -37,8 +37,9 @@ size_t conditions(const struct shirudo_conditions_input *in,
(__s64)in->keyring_serial_pinned);
} else if (!in->keyring_present) {
c[n].status = "WARN";
- snprintf(c[n].detail, sizeof(c[n].detail),
- "shirudo keyring absent (not init'd?)");
+ strscpy(c[n].detail,
+ "shirudo keyring absent (not init'd?)",
+ sizeof(c[n].detail));
} else if (in->ksm_seen && !in->keyring_serial_matches) {
c[n].status = "ALERT";
snprintf(c[n].detail, sizeof(c[n].detail),
@@ -59,22 +60,24 @@ size_t conditions(const struct shirudo_conditions_input *in,
c[n].type = "ChainIntact";
if (!in->chain_seen) {
c[n].status = "WARN";
- snprintf(c[n].detail, sizeof(c[n].detail),
- in->tpm_reachable ?
- "no register bound this boot (init incomplete)" :
- "no TPM answered, no register to replay");
+ strscpy(c[n].detail, in->tpm_reachable ?
+ "no register bound this boot (init incomplete)" :
+ "no TPM answered, no register to replay",
+ sizeof(c[n].detail));
} else if (in->cm_seen && in->chain_matches) {
c[n].status = "OK";
- snprintf(c[n].detail, sizeof(c[n].detail),
- "live register == eventlog replay");
+ strscpy(c[n].detail, "live register == eventlog replay",
+ sizeof(c[n].detail));
} else if (in->cm_seen) {
c[n].status = "ALERT";
- snprintf(c[n].detail, sizeof(c[n].detail),
- "live register != eventlog replay (tamper?)");
+ strscpy(c[n].detail,
+ "live register != eventlog replay (tamper?)",
+ sizeof(c[n].detail));
} else {
c[n].status = "WARN";
- snprintf(c[n].detail, sizeof(c[n].detail),
- "chain present, replay not evaluated");
+ strscpy(c[n].detail,
+ "chain present, replay not evaluated",
+ sizeof(c[n].detail));
}
n++;
}
@@ -90,8 +93,8 @@ size_t conditions(const struct shirudo_conditions_input *in,
"NV index %s not bound this boot",
in->nv_index ? in->nv_index : "?");
else
- snprintf(c[n].detail, sizeof(c[n].detail),
- "no TPM answered");
+ strscpy(c[n].detail, "no TPM answered",
+ sizeof(c[n].detail));
n++;
}
return n;
@@ -137,20 +140,20 @@ void status_reason(const struct shirudo_conditions_input *in,
if (in->unreach)
strscpy(buf, in->unreach, cap);
} else if (strcmp(v, "unparseable") == 0) {
- snprintf(buf, cap, "status JSON unparseable");
+ strscpy(buf, "status JSON unparseable", cap);
} else if (strcmp(v, "tampered") == 0) {
if (in->cm_seen && !in->chain_matches)
- snprintf(buf, cap, "live register != eventlog replay");
+ strscpy(buf, "live register != eventlog replay", cap);
else if (!in->keyring_present)
- snprintf(buf, cap, "the keyring init froze is gone");
+ strscpy(buf, "the keyring init froze is gone", cap);
else
- snprintf(buf, cap, "keyring serial swap detected");
+ strscpy(buf, "keyring serial swap detected", cap);
} else if (strcmp(v, "lsm-off") == 0) {
- snprintf(buf, cap, "LSM not attached -- enforcement OFF");
+ strscpy(buf, "LSM not attached -- enforcement OFF", cap);
} else if (strcmp(v, "init-pending") == 0) {
- snprintf(buf, cap, "target hasn't been `shirudo init`'d");
+ strscpy(buf, "target hasn't been `shirudo init`'d", cap);
} else if (strcmp(v, "degraded") == 0) {
- snprintf(buf, cap, in->tpm_reachable ?
- "no register bound this boot" : "no TPM answered");
+ strscpy(buf, in->tpm_reachable ?
+ "no register bound this boot" : "no TPM answered", cap);
}
}
diff --git a/util/dm_verity.c b/util/dm_verity.c
index 7b675beb..1d649326 100644
--- a/util/dm_verity.c
+++ b/util/dm_verity.c
@@ -507,7 +507,7 @@ int shirudo_dm_verity_create(const struct shirudo_config_image_open *image,
target = (void *)(buf.bytes + buf.io.data_start);
target->length = image->image.data_blocks *
(SHIRUDO_CONFIG_IMAGE_BLOCK_SIZE / 512);
- snprintf(target->target_type, sizeof(target->target_type), "verity");
+ strscpy(target->target_type, "verity", sizeof(target->target_type));
memcpy(target + 1, params, n + 1);
target->next = (sizeof(*target) + n + 1 + 7) & ~7u;
buf.io.data_size = buf.io.data_start + target->next;
diff --git a/util/kmodules.c b/util/kmodules.c
index 1c52834c..4bcdb067 100644
--- a/util/kmodules.c
+++ b/util/kmodules.c
@@ -458,13 +458,13 @@ void kmodules_print_recent(const struct shirudo_kmod_set *set,
if (m->loaded_at_sec <= 0 ||
iso8601((time_t)m->loaded_at_sec, ts, sizeof(ts)))
- snprintf(ts, sizeof(ts), "?");
+ strscpy(ts, "?", sizeof(ts));
human_size(m->size, size_s, sizeof(size_s));
if (boot_sec > 0 && m->loaded_at_sec >= boot_sec)
format_duration((long)(m->loaded_at_sec - boot_sec),
age, sizeof(age));
else
- snprintf(age, sizeof(age), "?");
+ strscpy(age, "?", sizeof(age));
printf(" [%s] %s (%s, refs=%u, %s since boot)\n",
ts, m->name, size_s, (__u32)m->refcount, age);
diff --git a/util/kmsg.c b/util/kmsg.c
index ef91f8c0..f62fa1bf 100644
--- a/util/kmsg.c
+++ b/util/kmsg.c
@@ -125,7 +125,7 @@ static void monotonic_to_iso(__u64 mono_us,
}
if (iso8601(rec.tv_sec, out, out_cap))
- snprintf(out, out_cap, "?");
+ strscpy(out, "?", out_cap);
}
int kmsg_tail(struct shirudo_kmsg_record *out, size_t cap)
diff --git a/util/preflight.c b/util/preflight.c
index 3a44f027..74c19496 100644
--- a/util/preflight.c
+++ b/util/preflight.c
@@ -17,6 +17,7 @@
#include <linux/keyctl.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "config.h"
#include "initrd.h"
@@ -247,8 +248,8 @@ static void check_cert_algorithm(struct shirudo_preflight_result *r)
if (preflight_keys(&set)) {
pass(c);
- snprintf(c->detail, sizeof(c->detail),
- "skipped (cert unreadable)");
+ strscpy(c->detail, "skipped (cert unreadable)",
+ sizeof(c->detail));
return;
}
for (i = 0; i < set.n; i++) {
@@ -296,8 +297,8 @@ static void check_cert_kernel_algo(struct shirudo_preflight_result *r)
if (preflight_keys(&set)) {
pass(c);
- snprintf(c->detail, sizeof(c->detail),
- "skipped (cert unreadable)");
+ strscpy(c->detail, "skipped (cert unreadable)",
+ sizeof(c->detail));
return;
}
for (i = 0; i < set.n; i++) {
diff --git a/util/sysinfo.c b/util/sysinfo.c
index 112109d0..be255b6c 100644
--- a/util/sysinfo.c
+++ b/util/sysinfo.c
@@ -60,7 +60,7 @@ static void read_os_pretty(char *buf, size_t cap)
v = line + 12;
if (*v == '"')
v++;
- snprintf(buf, cap, "%.*s", (int)(cap - 1), v);
+ strscpy(buf, v, cap);
len = strlen(buf);
while (len && (buf[len - 1] == '\n' || buf[len - 1] == '\r' ||
buf[len - 1] == '"'))
diff --git a/util/timefmt.c b/util/timefmt.c
index 0e5f31df..a204bf4f 100644
--- a/util/timefmt.c
+++ b/util/timefmt.c
@@ -8,12 +8,14 @@
#include <errno.h>
#include <stdio.h>
+#include <shirudo/string.h>
+
#include "timefmt.h"
void format_duration(long seconds, char *buf, size_t cap)
{
if (seconds < 0)
- snprintf(buf, cap, "?");
+ strscpy(buf, "?", cap);
else if (seconds < 60)
snprintf(buf, cap, "%lds", seconds);
else if (seconds < 3600)
diff --git a/util/upatch_maps.c b/util/upatch_maps.c
index 94de05b4..0616efae 100644
--- a/util/upatch_maps.c
+++ b/util/upatch_maps.c
@@ -1337,8 +1337,7 @@ static void scan_upatch_maps(const char *upatch,
continue;
close(fd);
strscpy(one.upatch, upatch, sizeof(one.upatch));
- snprintf(one.name, sizeof(one.name), "%.*s",
- (int)(sizeof(one.name) - 1), e->d_name);
+ strscpy(one.name, e->d_name, sizeof(one.name));
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 c8ecf3ce..0c2f696e 100644
--- a/util/upatch_maps_otel.c
+++ b/util/upatch_maps_otel.c
@@ -159,24 +159,25 @@ int shirudo_upatch_maps_otel_classify(const struct shirudo_map_view *v,
return -EINVAL;
*signal = SHIRUDO_OTEL_LOG;
if (v->serialized) {
- snprintf(reason, reason_sz, "map uses a serializer");
+ strscpy(reason, "map uses a serializer", reason_sz);
return 0;
}
if (v->skipped) {
- snprintf(reason, reason_sz, "map type is not generically readable");
+ strscpy(reason, "map type is not generically readable",
+ reason_sz);
return 0;
}
if (!upatch_map_is_percpu(v->type)) {
- snprintf(reason, reason_sz, "map is not per-CPU");
+ strscpy(reason, "map is not per-CPU", reason_sz);
return 0;
}
rc = metric_type_from_view(v, &mt);
if (rc) {
- snprintf(reason, reason_sz, "value is not a BTF integer scalar");
+ strscpy(reason, "value is not a BTF integer scalar", reason_sz);
return 0;
}
if (mt.is_signed) {
- snprintf(reason, reason_sz, "value is signed");
+ strscpy(reason, "value is signed", reason_sz);
return 0;
}
*signal = SHIRUDO_OTEL_METRIC;
@@ -596,7 +597,7 @@ static void emit_resource_attributes(struct shirudo_json_emit *e)
char hostname[256] = "unknown";
if (gethostname(hostname, sizeof(hostname)) != 0)
- snprintf(hostname, sizeof(hostname), "unknown");
+ strscpy(hostname, "unknown", sizeof(hostname));
hostname[sizeof(hostname) - 1] = '\0';
json_arr_open(e, "attributes");
otlp_kv_string(e, "service.name", "shirudo");
@@ -2494,7 +2495,7 @@ static int build_s3_urls(const struct otel_s3_state *st,
int n, rc;
if (gethostname(hostname, sizeof(hostname)) != 0)
- snprintf(hostname, sizeof(hostname), "unknown");
+ strscpy(hostname, "unknown", sizeof(hostname));
hostname[sizeof(hostname) - 1] = '\0';
safe_key_segment(hostname);
n = snprintf(key, sizeof(key), "%s%s%s/%s/%s/%s.json.gz",
diff --git a/util/upatch_resolve.c b/util/upatch_resolve.c
index 295f2f7a..ea898ff3 100644
--- a/util/upatch_resolve.c
+++ b/util/upatch_resolve.c
@@ -150,7 +150,7 @@ int upatch_resolve_location(struct btf *vbtf, const char *name,
linfo.netfilter.pf == NFPROTO_IPV6 &&
linfo.netfilter.hooknum == NF_INET_PRE_ROUTING &&
linfo.netfilter.priority == NF_IP6_PRI_RAW_BEFORE_DEFRAG) {
- snprintf(out, cap, "netfilter/ipv6-pre-routing");
+ strscpy(out, "netfilter/ipv6-pre-routing", cap);
return 0;
}