Skip to content

eBPF-based security platform

include: poison snprintf() and vsnprintf() in <shirudo/string.h>

Daniel Borkmann9192eabf1f6d
diff --git a/include/shirudo/string.h b/include/shirudo/string.h
index 39667b15..ff4ab37b 100644
--- a/include/shirudo/string.h
+++ b/include/shirudo/string.h
@@ -52,13 +52,17 @@ int vstrsprintf(char *buf, size_t size, const char *fmt, va_list ap)
/*
* What the functions above replace, refused once the headers included
- * above have declared it, so that a later include of those still works.
+ * above have declared it, so that a later include of those still works;
+ * util/string.c opts out with SHIRUDO_STRING_INTERNAL.
*/
-#pragma GCC poison strtol strtoll strtoul strtoull strtoimax strtoumax
-#pragma GCC poison strtod strtof strtold atoi atol atoll atof
-#pragma GCC poison strcpy strncpy strcat strncat strlcpy
-/* Fortified glibc makes sprintf() a macro for clang; poison the name alone. */
-#undef sprintf
-#pragma GCC poison sprintf vsprintf
+#ifndef SHIRUDO_STRING_INTERNAL
+# pragma GCC poison strtol strtoll strtoul strtoull strtoimax strtoumax
+# pragma GCC poison strtod strtof strtold atoi atol atoll atof
+# pragma GCC poison strcpy strncpy strcat strncat strlcpy
+/* Fortified glibc makes these macros for clang; poison the names alone. */
+# undef sprintf
+# undef snprintf
+# pragma GCC poison sprintf vsprintf snprintf vsnprintf
+#endif
#endif /* SHIRUDO_STRING_H */
diff --git a/testing/fuzz/fuzz-decoder.c b/testing/fuzz/fuzz-decoder.c
index b02609bc..e17d744f 100644
--- a/testing/fuzz/fuzz-decoder.c
+++ b/testing/fuzz/fuzz-decoder.c
@@ -36,18 +36,21 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
size_t len;
char *src;
- snprintf(self, sizeof(self), "%s", (*argv)[0]);
+ if (strscpy(self, (*argv)[0], sizeof(self)) < 0)
+ abort();
for (size_t i = 0; i < ARRAY_SIZE(sources); i++) {
- snprintf(path, sizeof(path), "%s/%s", dirname(self), sources[i]);
- snprintf(self, sizeof(self), "%s", (*argv)[0]);
+ if (strsprintf(path, sizeof(path), "%s/%s", dirname(self),
+ sources[i]) < 0)
+ abort();
+ strscpy(self, (*argv)[0], sizeof(self));
if (read_file(path, 1 << 20, &src, &len))
abort();
views[i].source = src;
views[i].source_len = (__u32)len;
- snprintf(views[i].source_name, sizeof(views[i].source_name),
- "%s", strrchr(sources[i], '/') + 1);
- snprintf(views[i].entrypoint, sizeof(views[i].entrypoint),
- "decode");
+ strscpy(views[i].source_name, strrchr(sources[i], '/') + 1,
+ sizeof(views[i].source_name));
+ strscpy(views[i].entrypoint, "decode",
+ sizeof(views[i].entrypoint));
}
return 0;
}
@@ -77,8 +80,8 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)
memcpy(&page.cursor, data + 1, sizeof(page.cursor));
data += 9;
size -= 9;
- snprintf(v.upatch, sizeof(v.upatch), "fuzz");
- snprintf(v.name, sizeof(v.name), "fuzz_map");
+ strscpy(v.upatch, "fuzz", sizeof(v.upatch));
+ strscpy(v.name, "fuzz_map", sizeof(v.name));
page.data = xmalloc(size + 1);
if (!page.data)
diff --git a/testing/fuzz/fuzz-kconfig.c b/testing/fuzz/fuzz-kconfig.c
index f9c1e49c..dd20965b 100644
--- a/testing/fuzz/fuzz-kconfig.c
+++ b/testing/fuzz/fuzz-kconfig.c
@@ -20,8 +20,7 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
};
for (size_t i = 0; i < ARRAY_SIZE(keys); i++)
- snprintf(sel.kconfig[i].key, sizeof(sel.kconfig[i].key), "%s",
- keys[i]);
+ strscpy(sel.kconfig[i].key, keys[i], sizeof(sel.kconfig[i].key));
sel.n_kconfig = ARRAY_SIZE(keys);
selector_hash_build(&sel, slots);
return 0;
diff --git a/testing/helper/test-image-cli.c b/testing/helper/test-image-cli.c
index 44ecf8f7..43d90ada 100644
--- a/testing/helper/test-image-cli.c
+++ b/testing/helper/test-image-cli.c
@@ -42,8 +42,7 @@ int main(int argc, const char **argv)
if (!rc) {
memcpy(bootstrap.tpm_owner_auth, "bootstrap-owner", 15);
bootstrap.tpm_owner_auth_len = 15;
- snprintf(bootstrap.tpm_tcti, sizeof(bootstrap.tpm_tcti),
- "bootstrap-tcti");
+ strscpy(bootstrap.tpm_tcti, "bootstrap-tcti", sizeof(bootstrap.tpm_tcti));
rc = shirudo_image_config_set(&bootstrap, fd);
}
close(fd);
@@ -78,9 +77,8 @@ int main(int argc, const char **argv)
struct bpm_tree_config config = {};
char digest[BPM_MAX_DIGEST];
- snprintf(config.endpoint, sizeof(config.endpoint),
- "oci://example.invalid/tree");
- snprintf(config.reference, sizeof(config.reference), "latest");
+ strscpy(config.endpoint, "oci://example.invalid/tree", sizeof(config.endpoint));
+ strscpy(config.reference, "latest", sizeof(config.reference));
rc = tree_fetch_publication(&config, &publication, digest,
sizeof(digest));
} else {
diff --git a/util/string.c b/util/string.c
index 3abb8c0e..a7722438 100644
--- a/util/string.c
+++ b/util/string.c
@@ -12,6 +12,8 @@
#include <stdio.h>
#include <string.h>
+/* Implements the wrappers; opt out of the poison in <shirudo/string.h>. */
+#define SHIRUDO_STRING_INTERNAL
#include <shirudo/string.h>
size_t digits(const char *s, size_t len)