Skip to content

eBPF-based security platform

util, keys: copy strings with strscpy()

Daniel Borkmann1f17273d6f60
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 9d172729..2d2b6740 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -577,7 +577,7 @@ testing/helper/test-keys: testing/helper/test-keys.c util/keys.o \
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(REGISTER_LIBS)
testing/helper/test-seal: testing/helper/test-seal.c util/seal.o util/io.o \
- util/log.o util/malloc.o
+ util/log.o util/string.o util/malloc.o
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^
@@ -590,7 +590,8 @@ testing/helper/test-chain: testing/helper/test-chain.c $(REGISTER_OBJS)
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(REGISTER_LIBS)
testing/helper/test-tpm-wire: testing/helper/test-tpm-wire.c util/tpm_wire.o util/hex.o \
- util/crypto_openssl.o util/io.o util/malloc.o util/log.o
+ util/crypto_openssl.o util/io.o util/malloc.o util/log.o \
+ util/string.o
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^ -lcrypto -lssl
diff --git a/builtin-build.c b/builtin-build.c
index c017ce15..925ef9cf 100644
--- a/builtin-build.c
+++ b/builtin-build.c
@@ -13,6 +13,8 @@
#include <string.h>
#include <unistd.h>
+#include <shirudo/string.h>
+
#include "builtin.h"
#include "util/help.h"
#include "util/log.h"
@@ -66,7 +68,8 @@ static bool on_path(const char *name)
if (len && len + 1 + strlen(name) < sizeof(candidate)) {
memcpy(candidate, path, len);
candidate[len] = '/';
- strcpy(candidate + len + 1, name);
+ strscpy(candidate + len + 1, name,
+ sizeof(candidate) - len - 1);
if (access(candidate, X_OK) == 0)
return true;
}
diff --git a/builtin-keys.c b/builtin-keys.c
index d14f3439..9934e955 100644
--- a/builtin-keys.c
+++ b/builtin-keys.c
@@ -26,6 +26,7 @@
#include <openssl/x509.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "builtin.h"
#include "util/chain.h"
@@ -189,7 +190,7 @@ static void format_pkey_algorithm(EVP_PKEY *pkey, char *out, size_t out_sz)
if (id == EVP_PKEY_RSA || id == EVP_PKEY_EC)
snprintf(out, out_sz, "%s-%d", name, EVP_PKEY_get_bits(pkey));
else
- snprintf(out, out_sz, "%s", name);
+ strscpy(out, name, out_sz);
}
static int dump_operator_cert(X509 *cert, const char *cert_path,
@@ -908,7 +909,7 @@ static int revoked_cert(const __u8 fp[32], const char *marker,
*der = NULL;
return rc;
}
- snprintf(c->path, sizeof(c->path), "%s", trusted);
+ strscpy(c->path, trusted, sizeof(c->path));
return 0;
}
diff --git a/util/attest.c b/util/attest.c
index 875fd743..9c9a3f60 100644
--- a/util/attest.c
+++ b/util/attest.c
@@ -18,6 +18,7 @@
#include <unistd.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "attest.h"
#include "chain.h"
@@ -469,7 +470,7 @@ static int on_record(void *ctx)
return -E2BIG;
r = &p->recs[p->n_recs];
memset(r, 0, sizeof(*r));
- snprintf(r->type, sizeof(r->type), "%s", p->type);
+ strscpy(r->type, p->type, sizeof(r->type));
if (hex_decode(p->digest_hex, r->digest, 32))
return -EINVAL;
if (!strcmp(p->type, "init")) {
@@ -497,7 +498,7 @@ static int on_record(void *ctx)
in->initrd = !strcmp(p->bound_in, "initrd");
in->have_reset_count = p->rec_reset >= 0;
in->reset_count = (__u32)p->rec_reset;
- snprintf(in->anchor, sizeof(in->anchor), "%s", p->anchor);
+ strscpy(in->anchor, p->anchor, sizeof(in->anchor));
in->present = true;
/*
* Records are of one TPM reset unless both carry a count
@@ -516,12 +517,12 @@ static int on_record(void *ctx)
snprintf(r->type, sizeof(r->type), "init");
memcpy(r->digest, in->digest, 32);
} else if (!strncmp(p->type, "upatch_", 7)) {
- snprintf(r->label, sizeof(r->label), "%s", p->upatch_name);
+ strscpy(r->label, p->upatch_name, sizeof(r->label));
} else if (!strcmp(p->type, "tree_op")) {
snprintf(r->label, sizeof(r->label), "%s %s seq %lld",
p->tree_op, p->tree_id, (__s64)p->tree_seq);
} else if (!strncmp(p->type, "key_", 4)) {
- snprintf(r->label, sizeof(r->label), "%s", p->key_fingerprint);
+ strscpy(r->label, p->key_fingerprint, sizeof(r->label));
}
p->n_recs++;
/* Reset the per-element scratch the next element may not set. */
diff --git a/util/conditions.c b/util/conditions.c
index 426c66c3..1e04a879 100644
--- a/util/conditions.c
+++ b/util/conditions.c
@@ -9,6 +9,7 @@
#include <string.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "conditions.h"
@@ -20,10 +21,10 @@ size_t conditions(const struct shirudo_conditions_input *in,
if (n < cap) {
c[n].type = "LsmAttached";
c[n].status = in->lsm_attached ? "OK" : "ALERT";
- snprintf(c[n].detail, sizeof(c[n].detail), "%s",
- in->lsm_attached ?
- "lsm/bpf link present and pinned" :
- "lsm/bpf absent -- enforcement OFF");
+ strscpy(c[n].detail, in->lsm_attached ?
+ "lsm/bpf link present and pinned" :
+ "lsm/bpf absent -- enforcement OFF",
+ sizeof(c[n].detail));
n++;
}
if (n < cap) {
@@ -128,7 +129,7 @@ void status_reason(const struct shirudo_conditions_input *in,
buf[0] = '\0';
if (strcmp(v, "unreachable") == 0) {
if (in->unreach)
- snprintf(buf, cap, "%s", in->unreach);
+ strscpy(buf, in->unreach, cap);
} else if (strcmp(v, "unparseable") == 0) {
snprintf(buf, cap, "status JSON unparseable");
} else if (strcmp(v, "tampered") == 0) {
diff --git a/util/config.c b/util/config.c
index 6a4fa1c5..361afe76 100644
--- a/util/config.c
+++ b/util/config.c
@@ -58,10 +58,7 @@ static bool parse_bool(const char *val, bool *out)
static bool set_string(char *dst, size_t cap, const char *val)
{
- if (strlen(val) >= cap)
- return false;
- snprintf(dst, cap, "%s", val);
- return true;
+ return strscpy(dst, val, cap) >= 0;
}
static void config_defaults(struct shirudo_config *cfg)
@@ -169,15 +166,12 @@ int config_load_path(struct shirudo_config *cfg, const char *path)
} else if (strcmp(key, "tpm.owner_auth") == 0) {
set_owner_auth(cfg, val);
} else if (strcmp(key, "tpm.tcti") == 0) {
- size_t l = strlen(val);
-
- if (l >= sizeof(cfg->tpm_tcti))
+ if (strlen(val) >= sizeof(cfg->tpm_tcti))
pr_warn("config: tpm.tcti too long "
"(ignored)");
else
- snprintf(cfg->tpm_tcti,
- sizeof(cfg->tpm_tcti),
- "%s", val);
+ strscpy(cfg->tpm_tcti, val,
+ sizeof(cfg->tpm_tcti));
} else if (strcmp(key, "map_export.json_path") == 0) {
if (!set_string(cfg->map_export_json_path,
sizeof(cfg->map_export_json_path), val))
@@ -277,11 +271,8 @@ int config_load(struct shirudo_config *cfg)
*/
env = getenv("SHIRUDO_TPM_TCTI");
if (env && *env) {
- size_t l = strlen(env);
-
- if (l < sizeof(cfg->tpm_tcti))
- snprintf(cfg->tpm_tcti, sizeof(cfg->tpm_tcti),
- "%s", env);
+ if (strlen(env) < sizeof(cfg->tpm_tcti))
+ strscpy(cfg->tpm_tcti, env, sizeof(cfg->tpm_tcti));
else
pr_warn("SHIRUDO_TPM_TCTI too long (ignored)");
}
diff --git a/util/crypto_openssl.c b/util/crypto_openssl.c
index b060dcd9..600c23be 100644
--- a/util/crypto_openssl.c
+++ b/util/crypto_openssl.c
@@ -27,6 +27,7 @@
#include <openssl/x509_vfy.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "crypto.h"
#include "io.h"
@@ -258,7 +259,8 @@ int cert_key_algorithm(const struct shirudo_cert *cert,
name = pub ? EVP_PKEY_get0_type_name(pub) : NULL;
if (!name)
return -ENOPKG;
- snprintf(out, out_sz, "%s", name);
+ if (strscpy(out, name, out_sz) < 0)
+ return -ENAMETOOLONG;
return 0;
}
diff --git a/util/keys.c b/util/keys.c
index 507c68ce..7c49aa2b 100644
--- a/util/keys.c
+++ b/util/keys.c
@@ -15,6 +15,7 @@
#include <unistd.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "crypto.h"
#include "hex.h"
@@ -205,8 +206,7 @@ static int set_add(struct shirudo_trusted_set *set, const char *path,
return -E2BIG;
c = &set->certs[set->n];
memset(c, 0, sizeof(*c));
- if (snprintf(c->path, sizeof(c->path), "%s", path) >=
- (int)sizeof(c->path))
+ if (strscpy(c->path, path, sizeof(c->path)) < 0)
return -ENAMETOOLONG;
memcpy(c->sha256, sha256, 32);
memcpy(c->fingerprint, fingerprint, 32);
diff --git a/util/seal.c b/util/seal.c
index 6e50dc9b..4fc39206 100644
--- a/util/seal.c
+++ b/util/seal.c
@@ -17,6 +17,8 @@
#include <sys/xattr.h>
#include <unistd.h>
+#include <shirudo/string.h>
+
#include "io.h"
#include "log.h"
#include "paths.h"
@@ -203,7 +205,7 @@ int seal_mkdir_p(const char *path, mode_t mode)
int dfd, nfd, rc = 0;
bool created;
- if (snprintf(buf, sizeof(buf), "%s", path) >= (int)sizeof(buf))
+ if (strscpy(buf, path, sizeof(buf)) < 0)
return -ENAMETOOLONG;
dfd = open(buf[0] == '/' ? "/" : ".",
O_RDONLY | O_DIRECTORY | O_CLOEXEC);
diff --git a/util/sysinfo.c b/util/sysinfo.c
index bd1c8670..a94e003c 100644
--- a/util/sysinfo.c
+++ b/util/sysinfo.c
@@ -293,14 +293,14 @@ void sysinfo_gather(struct shirudo_sysinfo *si)
si->hostname[sizeof(si->hostname) - 1] = '\0';
if (uname(&u) == 0) {
- snprintf(si->kernel_release, sizeof(si->kernel_release),
- "%s", u.release);
- snprintf(si->kernel_version, sizeof(si->kernel_version),
- "%s", u.version);
- snprintf(si->machine, sizeof(si->machine), "%s", u.machine);
+ strscpy(si->kernel_release, u.release,
+ sizeof(si->kernel_release));
+ strscpy(si->kernel_version, u.version,
+ sizeof(si->kernel_version));
+ strscpy(si->machine, u.machine, sizeof(si->machine));
if (si->hostname[0] == '\0')
- snprintf(si->hostname, sizeof(si->hostname), "%s",
- u.nodename);
+ strscpy(si->hostname, u.nodename,
+ sizeof(si->hostname));
}
read_os_pretty(si->os_pretty, sizeof(si->os_pretty));