Skip to content

eBPF-based security platform

config: parse numbers in decimal and nothing else

Daniel Borkmann690a716bbfa1
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 000c5765..f9644018 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -603,7 +603,8 @@ testing/helper/tpmsim-cmd: testing/helper/tpmsim-cmd.c
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS_BASE) -Iinclude $(TPMSIM_CFLAGS) -o $@ $< $(TPMSIM_LIBS) -lcrypto
-testing/helper/test-config: testing/helper/test-config.c util/config.o util/io.o util/log.o util/hex.o util/malloc.o
+testing/helper/test-config: testing/helper/test-config.c util/config.o util/io.o util/log.o util/hex.o \
+ util/string.o util/malloc.o
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^
diff --git a/testing/helper/test-config.c b/testing/helper/test-config.c
index c344d2a8..e9431344 100644
--- a/testing/helper/test-config.c
+++ b/testing/helper/test-config.c
@@ -86,6 +86,19 @@ static int check_invalid(const char *path, const char *input)
cfg.map_export_valid;
}
+/* A bad eventlog.max_bytes is ignored, leaving the default of 0. */
+static int check_max_bytes(const char *path, const char *value,
+ __u64 expected)
+{
+ struct shirudo_config cfg;
+ char input[128];
+
+ snprintf(input, sizeof(input), "eventlog.max_bytes = %s\n", value);
+ return write_config(path, input) != 0 ||
+ shirudo_config_load_path(&cfg, path) != 0 ||
+ cfg.eventlog_max_bytes != expected;
+}
+
int main(void)
{
char path[] = "/tmp/shirudo-config-test.XXXXXX";
@@ -99,8 +112,12 @@ int main(void)
failed += check_valid(path);
failed += check_invalid(path, "map_export.json_path = relative.json\n");
failed += check_invalid(path, "map_export.otel_retries = 11\n");
+ failed += check_invalid(path, "map_export.otel_retries = +5\n");
+ failed += check_invalid(path, "map_export.otel_retries = -1\n");
failed += check_invalid(path, "map_export.otel_insecure = yes\n");
failed += check_invalid(path, "map_export.otlp_max_bytes = 0\n");
+ failed += check_invalid(path, "map_export.otlp_max_bytes = +1024\n");
+ failed += check_invalid(path, "map_export.otlp_max_bytes = 0x400\n");
failed += check_invalid(path, "map_export.otlp_max_bytes = 1024\n"
"map_export.log_record_max_bytes = 2048\n");
failed += check_invalid(path, "map_export.tenant_id = tenant uuid\n");
@@ -111,6 +128,12 @@ int main(void)
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"a\n");
failed += check_invalid(path, "map_export.unknown = value\n");
+ failed += check_max_bytes(path, "1048576", 1048576);
+ failed += check_max_bytes(path, "010", 10);
+ failed += check_max_bytes(path, "0x10", 0);
+ failed += check_max_bytes(path, "+16", 0);
+ failed += check_max_bytes(path, "-1", 0);
+ failed += check_max_bytes(path, "", 0);
unlink(path);
return failed ? EXIT_FAILURE : EXIT_SUCCESS;
}
diff --git a/util/config.c b/util/config.c
index d8c2701c..80814e1b 100644
--- a/util/config.c
+++ b/util/config.c
@@ -8,11 +8,13 @@
#include <errno.h>
#include <limits.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "config.h"
#include "hex.h"
@@ -157,14 +159,9 @@ int shirudo_config_load_path(struct shirudo_config *cfg, const char *path)
key = trim(p);
val = trim(eq + 1);
if (strcmp(key, "eventlog.max_bytes") == 0) {
- char *end;
__u64 v;
- /* strtoul takes "-1" and "" as numbers. */
- errno = 0;
- v = strtoul(val, &end, 0);
- if (errno || end == val || *end != '\0' ||
- val[0] == '-')
+ if (parse_u64(val, strlen(val), UINT64_MAX, &v))
pr_warn("config: eventlog.max_bytes: "
"bad value '%s' (ignored)", val);
else
@@ -207,12 +204,9 @@ int shirudo_config_load_path(struct shirudo_config *cfg, const char *path)
map_export_bad(cfg, key, val,
"expected true or false");
} else if (strcmp(key, "map_export.otel_retries") == 0) {
- char *end;
__u64 v;
- errno = 0;
- v = strtoul(val, &end, 10);
- if (errno || *end || v > 10)
+ if (parse_u64(val, strlen(val), 10, &v))
map_export_bad(cfg, key, val,
"expected 0..10");
else
@@ -224,12 +218,10 @@ int shirudo_config_load_path(struct shirudo_config *cfg, const char *path)
map_export_bad(cfg, key, val, "value too long");
} else if (strcmp(key, "map_export.log_record_max_bytes") == 0 ||
strcmp(key, "map_export.otlp_max_bytes") == 0) {
- char *end;
__u64 v;
- errno = 0;
- v = strtoul(val, &end, 10);
- if (errno || *end || v == 0 || v > SHIRUDO_JSON_MAX_BYTES)
+ if (parse_u64(val, strlen(val), SHIRUDO_JSON_MAX_BYTES,
+ &v) || v == 0)
map_export_bad(cfg, key, val, "expected 1..8388608 bytes");
else if (strcmp(key, "map_export.log_record_max_bytes") == 0)
cfg->map_export_log_record_max_bytes = (__u32)v;