Skip to content

eBPF-based security platform

keys, init: format paths with strsprintf()

Daniel Borkmann7440ca507871
diff --git a/builtin-init.c b/builtin-init.c
index 241e5c3f..92ac05d6 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -24,6 +24,7 @@
#include <bpf/libbpf.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include <shirudo/upatch.h>
#include "builtin.h"
@@ -578,8 +579,8 @@ static int identity_executables(struct shirudo_identity *id)
return -errno;
}
self[n] = '\0';
- if (snprintf(root_exe, sizeof(root_exe), "%s%s", sysroot(),
- self) >= (int)sizeof(root_exe))
+ if (strsprintf(root_exe, sizeof(root_exe), "%s%s", sysroot(),
+ self) < 0)
return -ENAMETOOLONG;
if (stat(root_exe, &st) != 0) {
pr_err("initrd: %s: %s; the root file system needs shirudo "
@@ -1125,8 +1126,8 @@ static int boot_restore(struct shirudo_measured_upatch **out_set, size_t *out_n)
if (rc)
goto next;
- if (snprintf(path, sizeof(path), "%s/%s", SHIRUDO_UPATCHES_DIR,
- names[i]->d_name) >= (int)sizeof(path)) {
+ if (strsprintf(path, sizeof(path), "%s/%s", SHIRUDO_UPATCHES_DIR,
+ names[i]->d_name) < 0) {
pr_err("boot-restore: path too long: %s",
names[i]->d_name);
rc = -ENAMETOOLONG;
@@ -1429,8 +1430,8 @@ static int initrd_enter(void)
struct stat st;
int rc;
- if (snprintf(src, sizeof(src), "%s%s", sysroot(),
- SHIRUDO_VAR_LIB) >= (int)sizeof(src))
+ if (strsprintf(src, sizeof(src), "%s%s", sysroot(),
+ SHIRUDO_VAR_LIB) < 0)
return -ENAMETOOLONG;
if (stat(src, &st) != 0 || !S_ISDIR(st.st_mode)) {
pr_err("initrd: %s is not a directory; the root file system "
diff --git a/builtin-keys.c b/builtin-keys.c
index 9934e955..6e5ac826 100644
--- a/builtin-keys.c
+++ b/builtin-keys.c
@@ -568,8 +568,7 @@ static int keys_add(int argc, const char **argv)
return 2;
}
keys_dir = keys_dir_resolve(keys_dir_arg);
- if (snprintf(revoked, sizeof(revoked), "%s/revoked", keys_dir) >=
- (int)sizeof(revoked)) {
+ if (strsprintf(revoked, sizeof(revoked), "%s/revoked", keys_dir) < 0) {
pr_err("keys add: keys directory path too long");
return 2;
}
@@ -778,8 +777,8 @@ static int purge_signed_by(const __u8 *der, size_t der_sz,
char path[PATH_MAX];
int signed_by, prc;
- if (snprintf(path, sizeof(path), "%s/%s", SHIRUDO_UPATCHES_DIR,
- names[i]->d_name) >= (int)sizeof(path))
+ if (strsprintf(path, sizeof(path), "%s/%s", SHIRUDO_UPATCHES_DIR,
+ names[i]->d_name) < 0)
continue;
if (upatch_open(path, &u))
continue;
@@ -823,8 +822,8 @@ static int promote(const struct shirudo_trusted_cert *c)
xfree(der, der_sz);
if (rc)
return rc;
- if (snprintf(vouch, sizeof(vouch), "%.*s.vouch",
- (int)(strlen(c->path) - 4), c->path) < (int)sizeof(vouch))
+ if (strsprintf(vouch, sizeof(vouch), "%.*s.vouch",
+ (int)(strlen(c->path) - 4), c->path) >= 0)
unlink(vouch);
unlink(c->path);
return 0;
@@ -1096,9 +1095,8 @@ finish:
if (!victim->primary) {
char vouch[PATH_MAX];
- if (snprintf(vouch, sizeof(vouch), "%.*s.vouch",
- (int)(strlen(victim->path) - 4), victim->path) <
- (int)sizeof(vouch))
+ if (strsprintf(vouch, sizeof(vouch), "%.*s.vouch",
+ (int)(strlen(victim->path) - 4), victim->path) >= 0)
unlink(vouch);
unlink(victim->path);
}
diff --git a/util/keys.c b/util/keys.c
index 7c49aa2b..9332164c 100644
--- a/util/keys.c
+++ b/util/keys.c
@@ -117,7 +117,7 @@ int keys_path_for(const char *dir, const __u8 fingerprint[32],
char hex[65];
hex_encode(fingerprint, 32, hex);
- if ((size_t)snprintf(out, cap, "%s/%s%s", dir, hex, suffix) >= cap)
+ if (strsprintf(out, cap, "%s/%s%s", dir, hex, suffix) < 0)
return -ENAMETOOLONG;
return 0;
}
@@ -243,7 +243,7 @@ static int vouch_path_for(const char *der_path, char *out, size_t cap)
{
size_t n = strlen(der_path) - 4;
- if ((size_t)snprintf(out, cap, "%.*s.vouch", (int)n, der_path) >= cap)
+ if (strsprintf(out, cap, "%.*s.vouch", (int)n, der_path) < 0)
return -ENAMETOOLONG;
return 0;
}
@@ -320,8 +320,7 @@ static int scan_trusted(const char *keys_dir, const char *revoked_dir,
char dir[PATH_MAX];
int n, i, rc = 0;
- if (snprintf(dir, sizeof(dir), "%s/trusted", keys_dir) >=
- (int)sizeof(dir))
+ if (strsprintf(dir, sizeof(dir), "%s/trusted", keys_dir) < 0)
return -ENAMETOOLONG;
n = scandir(dir, &names, select_der, alphasort);
if (n < 0)
@@ -333,8 +332,8 @@ static int scan_trusted(const char *keys_dir, const char *revoked_dir,
rc = -E2BIG;
break;
}
- if (snprintf(c->path, sizeof(c->path), "%s/%s", dir,
- names[i]->d_name) >= (int)sizeof(c->path)) {
+ if (strsprintf(c->path, sizeof(c->path), "%s/%s", dir,
+ names[i]->d_name) < 0) {
rc = -ENAMETOOLONG;
break;
}
@@ -373,13 +372,13 @@ int keys_load(const char *keys_dir, const char *revoked_dir,
memset(set, 0, sizeof(*set));
if (!revoked_dir) {
- if (snprintf(revoked, sizeof(revoked), "%s/revoked",
- keys_dir) >= (int)sizeof(revoked))
+ if (strsprintf(revoked, sizeof(revoked), "%s/revoked",
+ keys_dir) < 0)
return -ENAMETOOLONG;
revoked_dir = revoked;
}
- if (snprintf(path, sizeof(path), "%s/%s", keys_dir,
- SHIRUDO_CERT_FILE) >= (int)sizeof(path))
+ if (strsprintf(path, sizeof(path), "%s/%s", keys_dir,
+ SHIRUDO_CERT_FILE) < 0)
return -ENAMETOOLONG;
rc = keys_digest_file(path, sha256, fingerprint);
if (rc)
@@ -457,8 +456,7 @@ int keys_install(const char *keys_dir, const __u8 *der,
rc = keys_digest_der(der, der_sz, sha256, fingerprint);
if (rc)
return rc;
- if (snprintf(dir, sizeof(dir), "%s/trusted", keys_dir) >=
- (int)sizeof(dir))
+ if (strsprintf(dir, sizeof(dir), "%s/trusted", keys_dir) < 0)
return -ENAMETOOLONG;
rc = seal_mkdir_p(dir, 0700);
if (rc)
diff --git a/util/paths.c b/util/paths.c
index da0c3d32..b058dd05 100644
--- a/util/paths.c
+++ b/util/paths.c
@@ -11,6 +11,8 @@
#include <string.h>
#include <sys/stat.h>
+#include <shirudo/string.h>
+
#include "paths.h"
const char *keys_dir_resolve(const char *keys_dir_arg)
@@ -30,14 +32,12 @@ int shirudo_key_path(char *out, size_t out_sz,
{
const char *dir = keys_dir ? keys_dir : SHIRUDO_KEYS_DIR;
const char *sep;
- int n;
if (!out || out_sz == 0 || !dir || !*dir || !file || !*file)
return -EINVAL;
sep = dir[strlen(dir) - 1] == '/' ? "" : "/";
- n = snprintf(out, out_sz, "%s%s%s", dir, sep, file);
- if (n < 0 || (size_t)n >= out_sz)
+ if (strsprintf(out, out_sz, "%s%s%s", dir, sep, file) < 0)
return -ENAMETOOLONG;
return 0;
}
diff --git a/util/seal.c b/util/seal.c
index 4fc39206..c972bcbd 100644
--- a/util/seal.c
+++ b/util/seal.c
@@ -244,7 +244,7 @@ int seal_write_file(const char *path, const void *data, size_t len,
dfd = seal_open_parent(path, &name);
if (dfd < 0)
return dfd;
- if (snprintf(tmp, sizeof(tmp), "%s.tmp", name) >= (int)sizeof(tmp)) {
+ if (strsprintf(tmp, sizeof(tmp), "%s.tmp", name) < 0) {
close(dfd);
return -ENAMETOOLONG;
}
diff --git a/util/sysinfo.c b/util/sysinfo.c
index a94e003c..112109d0 100644
--- a/util/sysinfo.c
+++ b/util/sysinfo.c
@@ -184,8 +184,7 @@ int sysinfo_kernel_config_foreach(const char *kernel_release,
size_t i;
if (!kernel_release || !kernel_release[0] || !cb ||
- snprintf(path, sizeof(path), "/boot/config-%s", kernel_release) >=
- (int)sizeof(path))
+ strsprintf(path, sizeof(path), "/boot/config-%s", kernel_release) < 0)
return -1;
f = fopen(path, "re");
if (!f)