Skip to content

eBPF-based security platform

treewide: refuse a NUL among counted bytes with span_to_str()

Daniel Borkmannd754028c05b3
diff --git a/testing/proofs/cbmc/eventlog-records.c b/testing/proofs/cbmc/eventlog-records.c
index 6801bd1e..d232fb83 100644
--- a/testing/proofs/cbmc/eventlog-records.c
+++ b/testing/proofs/cbmc/eventlog-records.c
@@ -8,9 +8,13 @@
* byte as it was, cutting only a last line without its newline.
*/
+/* util/string.c is included behind a file that includes its header. */
+#define STRING_INTERNAL
+
#include "proof.h"
#include "../../../util/io.c"
#include "../../../util/eventlog.c"
+#include "../../../util/string.c"
void harness(void)
{
diff --git a/testing/proofs/cbmc/xdp-attrs.c b/testing/proofs/cbmc/xdp-attrs.c
index bfcdc950..302588ce 100644
--- a/testing/proofs/cbmc/xdp-attrs.c
+++ b/testing/proofs/cbmc/xdp-attrs.c
@@ -10,12 +10,16 @@
* devlink port, its bus and device.
*/
+/* util/string.c is included behind a file that includes its header. */
+#define STRING_INTERNAL
+
#include "proof.h"
/* NLA_HDRLEN casts the negative ~(NLA_ALIGNTO - 1) on purpose. */
#pragma CPROVER check push
#pragma CPROVER check disable "conversion"
#include "../../../util/xdp.c"
#pragma CPROVER check pop
+#include "../../../util/string.c"
#define ATTR_HEADER sizeof(struct nlattr)
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.c
index ae8d0aa3..dd21a13e 100644
--- a/tools/shirudo-build/bpm-build.c
+++ b/tools/shirudo-build/bpm-build.c
@@ -772,25 +772,25 @@ static int build_parse_spec(struct bpm_build_spec *spec, char *body)
int bpm_build_parse_beefile(const __u8 *body, size_t len,
struct bpm_build_spec *spec)
{
- __u8 *scratch;
+ __u8 *build_file;
+ char *scratch;
int ret;
- if (!body || !spec || memchr(body, '\0', len))
+ if (!body || !spec)
return -EINVAL;
- spec->build_file = xmalloc(len + 1);
+ build_file = xmalloc(len + 1);
scratch = xmalloc(len + 1);
- if (!spec->build_file || !scratch) {
+ ret = !build_file || !scratch ? -ENOMEM :
+ span_to_str(scratch, len + 1, body, len);
+ if (ret) {
xfree(scratch, len + 1);
- xfree(spec->build_file, len + 1);
- spec->build_file = NULL;
- return -ENOMEM;
+ xfree(build_file, len + 1);
+ return ret;
}
- memcpy(spec->build_file, body, len);
- spec->build_file[len] = '\0';
+ memcpy(build_file, scratch, len + 1);
+ spec->build_file = build_file;
spec->build_file_len = len;
- memcpy(scratch, body, len);
- scratch[len] = '\0';
- ret = build_parse_spec(spec, (char *)scratch);
+ ret = build_parse_spec(spec, scratch);
xfree(scratch, len + 1);
return ret;
}
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index 2e4c5494..06ee8f63 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -227,8 +227,7 @@ static int decode_basic(const char *in, char *user, size_t usz,
ret = -E2BIG;
goto out;
}
- out[n] = '\0';
- if (memchr(out, '\0', n))
+ if (span_to_str((char *)out, sizeof(out), out, n))
goto out;
colon = memchr(out, ':', n);
if (!colon || (size_t)(colon - (char *)out) >= usz ||
diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.c
index bd587fb9..87f5e698 100644
--- a/util/bpm_tree_state.c
+++ b/util/bpm_tree_state.c
@@ -699,9 +699,10 @@ int tree_event_replay(__u8 out[32])
if (ret)
return ret;
/* A NUL would end the walk early and replay a shorter chain. */
- if (memchr(body, '\0', len)) {
+ ret = span_to_str((char *)body, len + 1, body, len);
+ if (ret) {
xfree(body, len + 1);
- return -EINVAL;
+ return ret;
}
body[lines_len((const char *)body, len)] = '\0';
for_each_token(line, line_len, (const char *)body,
diff --git a/util/eventlog.c b/util/eventlog.c
index 32351f72..7ddc0251 100644
--- a/util/eventlog.c
+++ b/util/eventlog.c
@@ -106,10 +106,11 @@ int eventlog_would_exceed(__u64 max_bytes)
*/
static int log_records(char *buf, size_t len)
{
- if (memchr(buf, '\0', len))
- return -EINVAL;
- buf[lines_len(buf, len)] = '\0';
- return 0;
+ int ret = span_to_str(buf, len + 1, buf, len);
+
+ if (!ret)
+ buf[lines_len(buf, len)] = '\0';
+ return ret;
}
/*
diff --git a/util/xdp.c b/util/xdp.c
index 28c1ce38..0cabc39c 100644
--- a/util/xdp.c
+++ b/util/xdp.c
@@ -76,11 +76,9 @@ static int attr_string(const void *data, size_t len, __u16 type,
return ret;
size = attr->nla_len - NLA_HDRLEN;
value = (const char *)attr + NLA_HDRLEN;
- if (!size || size > cap || value[size - 1] ||
- memchr(value, '\0', size - 1))
+ if (!size || size > cap || value[size - 1])
return -EINVAL;
- memcpy(out, value, size);
- return 0;
+ return span_to_str(out, cap, value, size - 1);
}
static int attr_number(const void *data, size_t len, __u16 type,