eBPF-based security platform
string: add span_to_str() for bytes that become a string
Daniel Borkmann001806f5dfab
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex 296f08a0..c0f4d8c3 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -61,6 +61,13 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- Strings and buffers up to 8 bytes- Terminates what it writes; the whole string and its length, orwhat fits and ``-E2BIG``+ * - ``string-span``+ - ``span_to_str()``+ - Spans and buffers up to 6 bytes, apart and the span in the buffer+ - A string exactly when no byte of the span is a NUL and the bytes+ fit with a terminator, ``-EINVAL`` and ``-E2BIG`` otherwise; the+ string is the bytes, terminated behind them; nothing past the+ span read and nothing written on a refusal* - ``string-suffix``- ``name_has_suffix()``- Names up to 6 bytes, suffixes up to 3diff --git a/include/shirudo/string.h b/include/shirudo/string.hindex 0bb7926f..a53e53b1 100644--- a/include/shirudo/string.h+++ b/include/shirudo/string.h@@ -44,6 +44,19 @@ int parse_s64(const char *s, size_t len, __s64 min, __s64 max, __s64 *out);*/ssize_t strscpy(char *dst, const char *src, size_t size);+/*+ * The len bytes at src as a string in the cap bytes at dst, terminated:+ * 0, -EINVAL when a NUL is among them, where a string would end short of+ * the bytes it was given, or -E2BIG when they and their terminator do+ * not fit. Nothing past len is read, so src need not be terminated; dst+ * is only written on success, and may be src where the bytes already sit+ * in a buffer with room for the terminator. What comes from outside the+ * program, a file, a reply or a field of a format, becomes a string+ * here, and only then goes to a function that reads up to a terminator.+ */+int span_to_str(char *dst, size_t cap, const void *src, size_t len)+ __must_check;+/** snprintf() into the size bytes at buf: the length written, or -E2BIG,* with buf left empty, when the output did not fit. vstrsprintf() takesdiff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex a08e88e4..c9820600 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -3,6 +3,7 @@string-number --unwind 24string-utf8 --unwind 10string-copy --unwind 10+string-span --unwind 9string-suffix --unwind 8string-sanitize --unwind 8 LEN=0-6hex --unwind 12diff --git a/testing/proofs/cbmc/string-span.c b/testing/proofs/cbmc/string-span.cnew file mode 100644index 00000000..a89212c8--- /dev/null+++ b/testing/proofs/cbmc/string-span.c@@ -0,0 +1,84 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * span_to_str() of util/string.c for every span and buffer of up to+ * LEN_MAX bytes, apart or the span in the buffer: the bytes are a string+ * exactly when none of them is a NUL and they fit with a terminator, the+ * string is then the bytes and ends behind them, and a refusal writes+ * nothing. The span is allocated at its length, so a read past it fails.+ */++#include <errno.h>+#include <stdbool.h>++#include "proof.h"+#include "../../../util/string.c"++#define LEN_MAX 6++static bool holds_nul(const char *s, size_t len)+{+ size_t i;++ for (i = 0; i < len; i++)+ if (!s[i])+ return true;+ return false;+}++static void check(const char *dst, const char *was, size_t cap,+ const char *src, size_t len, int ret)+{+ size_t i;++ if (holds_nul(src, len)) {+ __CPROVER_assert(ret == -EINVAL, "a NUL among the bytes refused");+ } else if (len >= cap) {+ __CPROVER_assert(ret == -E2BIG, "no room for the terminator");+ } else {+ __CPROVER_assert(!ret, "bytes without a NUL that fit taken");+ __CPROVER_assert(!dst[len], "terminated behind the bytes");+ for (i = 0; i < len; i++)+ __CPROVER_assert(dst[i] == src[i] && dst[i],+ "the string is the bytes");+ }+ for (i = ret ? 0 : len + 1; i < cap; i++)+ __CPROVER_assert(dst[i] == was[i],+ "nothing else written, nothing at all on a refusal");+}++static void span_apart(void)+{+ char was[LEN_MAX + 1];+ char *dst, *src;+ size_t cap, len;+ int ret;++ __CPROVER_assume(cap <= LEN_MAX + 1 && len <= LEN_MAX);+ src = any_bytes(len);+ dst = any_bytes(cap);+ memcpy(was, dst, cap);+ ret = span_to_str(dst, cap, src, len);+ check(dst, was, cap, src, len, ret);+}++static void span_in_place(void)+{+ char was[LEN_MAX + 1];+ size_t cap, len;+ char *buf;+ int ret;++ __CPROVER_assume(cap <= LEN_MAX + 1 && len <= cap);+ buf = any_bytes(cap);+ memcpy(was, buf, cap);+ ret = span_to_str(buf, cap, buf, len);+ check(buf, was, cap, was, len, ret);+}++void harness(void)+{+ span_apart();+ span_in_place();+}diff --git a/testing/unit/string.c b/testing/unit/string.cindex 171ff90b..dd29e531 100644--- a/testing/unit/string.c+++ b/testing/unit/string.c@@ -527,6 +527,61 @@ static void span_equals_wants_the_whole_literal(void)EXPECT_TRUE(span_equals(path, 0, ""));}+static void span_to_str_copies_and_terminates(void)+{+ char *bytes = span("abc", 3);+ char dst[4] = "xxx";++ ASSERT_EQ(span_to_str(dst, sizeof(dst), bytes, 3), 0);+ EXPECT_STREQ(dst, "abc");+ ASSERT_EQ(span_to_str(dst, sizeof(dst), bytes, 2), 0);+ EXPECT_STREQ(dst, "ab");+ ASSERT_EQ(span_to_str(dst, 1, bytes, 0), 0);+ EXPECT_STREQ(dst, "");+ xfree(bytes, 3);+}++/* A string that ended at the NUL would be "ab", not the four bytes. */+static void span_to_str_refuses_a_nul_among_the_bytes(void)+{+ static const char first[] = { '\0', 'a', 'b', 'c' };+ static const char inner[] = { 'a', 'b', '\0', 'c' };+ static const char last[] = { 'a', 'b', 'c', '\0' };+ char dst[8] = "kept";++ EXPECT_EQ(span_to_str(dst, sizeof(dst), first, 4), -EINVAL);+ EXPECT_EQ(span_to_str(dst, sizeof(dst), inner, 4), -EINVAL);+ EXPECT_EQ(span_to_str(dst, sizeof(dst), last, 4), -EINVAL);+ EXPECT_STREQ(dst, "kept");+ ASSERT_EQ(span_to_str(dst, sizeof(dst), last, 3), 0);+ EXPECT_STREQ(dst, "abc");+}++static void span_to_str_wants_room_for_the_terminator(void)+{+ char *bytes = span("abcd", 4);+ char dst[8] = "kept";++ EXPECT_EQ(span_to_str(dst, 4, bytes, 4), -E2BIG);+ EXPECT_EQ(span_to_str(dst, 0, bytes, 0), -E2BIG);+ EXPECT_STREQ(dst, "kept");+ ASSERT_EQ(span_to_str(dst, 5, bytes, 4), 0);+ EXPECT_STREQ(dst, "abcd");+ xfree(bytes, 4);+}++/* As a reader of a whole file leaves it: len bytes and room for one more. */+static void span_to_str_takes_a_buffer_in_place(void)+{+ char text[5] = { 'a', 'b', 'c', 'd', 'x' };+ char cut[5] = { 'a', '\0', 'c', 'd', '\0' };++ ASSERT_EQ(span_to_str(text, sizeof(text), text, 4), 0);+ EXPECT_STREQ(text, "abcd");+ EXPECT_EQ(span_to_str(cut, sizeof(cut), cut, 4), -EINVAL);+ EXPECT_EQ(span_to_str(text, 4, text, 4), -E2BIG);+}+static const struct unit_case cases[] = {UNIT_CASE(digits_counts_leading_digits),UNIT_CASE(decimal_fills_its_span),@@ -553,6 +608,10 @@ static const struct unit_case cases[] = {UNIT_CASE(str_has_prefix_gives_its_length),UNIT_CASE(str_skip_prefix_moves_only_on_a_match),UNIT_CASE(span_equals_wants_the_whole_literal),+ UNIT_CASE(span_to_str_copies_and_terminates),+ UNIT_CASE(span_to_str_refuses_a_nul_among_the_bytes),+ UNIT_CASE(span_to_str_wants_room_for_the_terminator),+ UNIT_CASE(span_to_str_takes_a_buffer_in_place),{}};diff --git a/util/string.c b/util/string.cindex 50356be6..8ecf5916 100644--- a/util/string.c+++ b/util/string.c@@ -112,6 +112,17 @@ ssize_t strscpy(char *dst, const char *src, size_t size)return (ssize_t)len;}+int span_to_str(char *dst, size_t cap, const void *src, size_t len)+{+ if (memchr(src, '\0', len))+ return -EINVAL;+ if (len >= cap)+ return -E2BIG;+ memmove(dst, src, len);+ dst[len] = '\0';+ return 0;+}+static bool numeric_dns_label(const char *label, size_t len){size_t i = 0;