Skip to content

eBPF-based security platform

util: add bounded number parsing

Daniel Borkmanndbb7b50cf8f3
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 184539fd..7dde4630 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -180,6 +180,7 @@ UTIL_SRCS := \
util/attest.c \
util/chain.c \
util/hex.c \
+ util/string.c \
util/bpm_tree.c \
util/bpm_tree_state.c \
util/bpm_oci.c \
@@ -406,7 +407,7 @@ tools/shirudo-build/shirudo-build: $(BUILD_OBJS) util/sign.o \
util/io.o util/malloc.o util/paths.o \
util/upatch.o util/timefmt.o \
util/elf_target.o util/hex.o util/keys.o \
- util/seal.o util/harden.o \
+ util/seal.o util/harden.o util/string.o \
util/crypto_openssl.o util/tls_openssl.o \
util/bpm_oci.o util/bpm_package.o \
util/bpm_tree.o $(VERSION_OBJ) \
@@ -524,6 +525,7 @@ TPMSIM_HELPER := $(if $(TPMSIM_LIBS),testing/helper/tpmsim-cmd,)
# Compiled test helpers (also usable as fuzz entry points).
TEST_HELPERS := testing/helper/test-json-parser \
+ testing/helper/test-string \
testing/helper/test-chain \
testing/helper/test-tpm-wire \
testing/helper/test-register \
@@ -551,6 +553,9 @@ TEST_HELPERS := testing/helper/test-json-parser \
testing/helper/test-json-parser: testing/helper/test-json-parser.c util/json.o util/hex.o util/malloc.o
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^
+testing/helper/test-string: testing/helper/test-string.c util/string.o util/malloc.o
+ @printf ' CC $@\n'
+ $(Q)$(CC) $(CFLAGS) -o $@ $^
# chain.o's read/extend paths reference util/tpm (libtss2); the tests
# only exercise the pure helpers, but the symbols must still resolve.
diff --git a/include/shirudo/string.h b/include/shirudo/string.h
new file mode 100644
index 00000000..8e84a907
--- /dev/null
+++ b/include/shirudo/string.h
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * Bounded number parsing, from util/string.c.
+ */
+
+#ifndef SHIRUDO_STRING_H
+#define SHIRUDO_STRING_H
+
+#include <stddef.h>
+
+#include <shirudo/misc.h>
+
+/* How many of the len bytes at s, from the first, are decimal digits. */
+size_t shirudo_digits(const char *s, size_t len);
+
+/*
+ * The number the len bytes at s spell, decimal for u64 and s64 and
+ * hexadecimal for x64, within the bounds given: digits only, s64 with an
+ * optional leading '-', and no blank, '+' or base prefix. Nothing past
+ * len is read, so s need not be terminated. -EINVAL when the bytes are
+ * not such a number, -ERANGE when it is out of bounds; *out is only
+ * written on success.
+ */
+int parse_u64(const char *s, size_t len, __u64 max, __u64 *out);
+int parse_x64(const char *s, size_t len, __u64 max, __u64 *out);
+int parse_s64(const char *s, size_t len, __s64 min, __s64 max, __s64 *out);
+
+#endif /* SHIRUDO_STRING_H */
diff --git a/testing/helper/test-string.c b/testing/helper/test-string.c
new file mode 100644
index 00000000..2b9fb514
--- /dev/null
+++ b/testing/helper/test-string.c
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+/*
+ * Drives util/string.c for t0049. The text under test sits in a buffer of
+ * exactly the length given, so a read past it shows up under ASan.
+ *
+ * test-string digits <text>
+ * test-string u64|x64 <max> <text> [<len>]
+ * test-string s64 <min> <max> <text> [<len>]
+ */
+
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <shirudo/string.h>
+
+#include "util/malloc.h"
+
+static int bound_u64(const char *s, __u64 *out)
+{
+ return parse_u64(s, strlen(s), UINT64_MAX, out);
+}
+
+static int bound_s64(const char *s, __s64 *out)
+{
+ return parse_s64(s, strlen(s), INT64_MIN, INT64_MAX, out);
+}
+
+/* The text in a buffer of its own, len bytes long and unterminated. */
+static char *exact(const char *text, const char *len_arg, size_t *len)
+{
+ __u64 n = strlen(text);
+ char *buf;
+
+ if (len_arg && (bound_u64(len_arg, &n) || n > strlen(text)))
+ return NULL;
+ buf = xmalloc(n ? n : 1);
+ if (buf)
+ memcpy(buf, text, n);
+ *len = n;
+ return buf;
+}
+
+int main(int argc, char **argv)
+{
+ const char *op = argc > 1 ? argv[1] : "";
+ __s64 min, smax, s = 0;
+ __u64 max, u = 0;
+ size_t len;
+ char *buf;
+ int rc;
+
+ if (!strcmp(op, "digits") && argc == 3) {
+ printf("n=%zu\n", shirudo_digits(argv[2], strlen(argv[2])));
+ return 0;
+ }
+ if ((!strcmp(op, "u64") || !strcmp(op, "x64")) &&
+ (argc == 4 || argc == 5)) {
+ if (bound_u64(argv[2], &max))
+ return 2;
+ buf = exact(argv[3], argc == 5 ? argv[4] : NULL, &len);
+ if (!buf)
+ return 2;
+ rc = op[0] == 'u' ? parse_u64(buf, len, max, &u) :
+ parse_x64(buf, len, max, &u);
+ printf("rc=%d v=%llu\n", rc, u);
+ xfree(buf, len ? len : 1);
+ return 0;
+ }
+ if (!strcmp(op, "s64") && (argc == 5 || argc == 6)) {
+ if (bound_s64(argv[2], &min) || bound_s64(argv[3], &smax))
+ return 2;
+ buf = exact(argv[4], argc == 6 ? argv[5] : NULL, &len);
+ if (!buf)
+ return 2;
+ rc = parse_s64(buf, len, min, smax, &s);
+ printf("rc=%d v=%lld\n", rc, s);
+ xfree(buf, len ? len : 1);
+ return 0;
+ }
+ fprintf(stderr, "usage: test-string digits|u64|x64|s64 ...\n");
+ return 2;
+}
diff --git a/testing/t0049-string.sh b/testing/t0049-string.sh
new file mode 100755
index 00000000..f6593018
--- /dev/null
+++ b/testing/t0049-string.sh
@@ -0,0 +1,82 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t0049 -- util/string.c: numbers parsed from a bounded, unterminated
+# span, digits only, within the bounds the caller gives.
+
+cd "$(dirname "$0")"
+. ./test-lib.sh
+
+H=helper/test-string
+U64_MAX=18446744073709551615
+S64_MIN=-9223372036854775808
+S64_MAX=9223372036854775807
+
+# EINVAL=22, ERANGE=34 on Linux.
+
+test_expect_success 'leading digits are counted' '
+ ./$H digits 123abc | grep -qx "n=3" &&
+ ./$H digits abc | grep -qx "n=0" &&
+ ./$H digits "" | grep -qx "n=0"
+'
+
+test_expect_success 'a decimal fills its span' '
+ ./$H u64 $U64_MAX 0 | grep -qx "rc=0 v=0" &&
+ ./$H u64 $U64_MAX 007 | grep -qx "rc=0 v=7" &&
+ ./$H u64 $U64_MAX $U64_MAX | grep -qx "rc=0 v=$U64_MAX"
+'
+
+test_expect_success 'nothing past the span is read' '
+ ./$H u64 $U64_MAX 123456 3 | grep -qx "rc=0 v=123" &&
+ ./$H u64 $U64_MAX 123 0 | grep -qx "rc=-22 v=0"
+'
+
+test_expect_success 'a sign, blank or base prefix is not a number' '
+ for t in "" -1 +1 " 1" "1 " 0x10 1.5 1e3 12a; do
+ ./$H u64 $U64_MAX "$t" | grep -qx "rc=-22 v=0" || return 1
+ done
+'
+
+test_expect_success 'a number past its bound is out of range' '
+ ./$H u64 $U64_MAX 18446744073709551616 | grep -qx "rc=-34 v=0" &&
+ ./$H u64 10 10 | grep -qx "rc=0 v=10" &&
+ ./$H u64 10 11 | grep -qx "rc=-34 v=0" &&
+ ./$H u64 5 9 | grep -qx "rc=-34 v=0" &&
+ ./$H u64 0 0 | grep -qx "rc=0 v=0"
+'
+
+test_expect_success 'a malformed number is not one, however long' '
+ ./$H u64 $U64_MAX 99999999999999999999x | grep -qx "rc=-22 v=0"
+'
+
+test_expect_success 'hexadecimal takes either case and no prefix' '
+ ./$H x64 $U64_MAX ff | grep -qx "rc=0 v=255" &&
+ ./$H x64 $U64_MAX FF | grep -qx "rc=0 v=255" &&
+ ./$H x64 $U64_MAX ffffffffffffffff | grep -qx "rc=0 v=$U64_MAX" &&
+ ./$H x64 $U64_MAX 10000000000000000 | grep -qx "rc=-34 v=0" &&
+ ./$H x64 $U64_MAX 0x10 | grep -qx "rc=-22 v=0" &&
+ ./$H x64 $U64_MAX g | grep -qx "rc=-22 v=0"
+'
+
+test_expect_success 'a signed number spans the whole range' '
+ ./$H s64 $S64_MIN $S64_MAX $S64_MIN | grep -qx "rc=0 v=$S64_MIN" &&
+ ./$H s64 $S64_MIN $S64_MAX $S64_MAX | grep -qx "rc=0 v=$S64_MAX" &&
+ ./$H s64 $S64_MIN $S64_MAX -0 | grep -qx "rc=0 v=0" &&
+ ./$H s64 $S64_MIN $S64_MAX 9223372036854775808 |
+ grep -qx "rc=-34 v=0" &&
+ ./$H s64 $S64_MIN $S64_MAX -9223372036854775809 |
+ grep -qx "rc=-34 v=0"
+'
+
+test_expect_success 'a signed number keeps to its bounds and form' '
+ ./$H s64 1 10 0 | grep -qx "rc=-34 v=0" &&
+ ./$H s64 -5 5 -6 | grep -qx "rc=-34 v=0" &&
+ ./$H s64 -5 5 -5 | grep -qx "rc=0 v=-5" &&
+ for t in - -- --1 +1 " -1" -x; do
+ ./$H s64 $S64_MIN $S64_MAX "$t" | grep -qx "rc=-22 v=0" ||
+ return 1
+ done
+'
+
+test_done
diff --git a/util/string.c b/util/string.c
new file mode 100644
index 00000000..80a434bf
--- /dev/null
+++ b/util/string.c
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * Bounded number parsing.
+ */
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stdint.h>
+
+#include <shirudo/string.h>
+
+size_t shirudo_digits(const char *s, size_t len)
+{
+ size_t n = 0;
+
+ while (n < len && s[n] >= '0' && s[n] <= '9')
+ n++;
+ return n;
+}
+
+static int digit_value(char c, __u32 base)
+{
+ if (c >= '0' && c <= '9')
+ return c - '0';
+ if (base == 16 && c >= 'a' && c <= 'f')
+ return c - 'a' + 10;
+ if (base == 16 && c >= 'A' && c <= 'F')
+ return c - 'A' + 10;
+ return -1;
+}
+
+static int parse_base(const char *s, size_t len, __u32 base, __u64 max,
+ __u64 *out)
+{
+ __u64 v = 0;
+ size_t i;
+
+ if (!len)
+ return -EINVAL;
+ for (i = 0; i < len; i++) {
+ int d = digit_value(s[i], base);
+
+ if (d < 0)
+ return -EINVAL;
+ }
+ for (i = 0; i < len; i++) {
+ __u64 d = (__u64)digit_value(s[i], base);
+
+ if (d > max || v > (max - d) / base)
+ return -ERANGE;
+ v = v * base + d;
+ }
+ *out = v;
+ return 0;
+}
+
+int parse_u64(const char *s, size_t len, __u64 max, __u64 *out)
+{
+ return parse_base(s, len, 10, max, out);
+}
+
+int parse_x64(const char *s, size_t len, __u64 max, __u64 *out)
+{
+ return parse_base(s, len, 16, max, out);
+}
+
+int parse_s64(const char *s, size_t len, __s64 min, __s64 max, __s64 *out)
+{
+ bool neg = len && s[0] == '-';
+ __u64 v;
+ __s64 r;
+ int rc;
+
+ rc = parse_base(s + neg, len - neg, 10,
+ neg ? (__u64)INT64_MAX + 1 : (__u64)INT64_MAX, &v);
+ if (rc)
+ return rc;
+ if (!neg)
+ r = (__s64)v;
+ else if (v == (__u64)INT64_MAX + 1)
+ r = INT64_MIN;
+ else
+ r = -(__s64)v;
+ if (r < min || r > max)
+ return -ERANGE;
+ *out = r;
+ return 0;
+}