Skip to content

eBPF-based security platform

json: hold numbers to the JSON grammar

Daniel Borkmanne73a84497f1b
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 7dde4630..000c5765 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -550,7 +550,8 @@ TEST_HELPERS := testing/helper/test-json-parser \
testing/helper/test-exec-churn \
testing/upatch/kernel-obs/exec/exec-paths
-testing/helper/test-json-parser: testing/helper/test-json-parser.c util/json.o util/hex.o util/malloc.o
+testing/helper/test-json-parser: testing/helper/test-json-parser.c util/json.o util/hex.o \
+ util/string.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
@@ -565,8 +566,8 @@ testing/helper/test-string: testing/helper/test-string.c util/string.o util/mall
# label writer and the JSON codec.
REGISTER_OBJS := util/chain.o util/tpm.o util/tpm_wire.o util/eventlog.o \
util/timefmt.o \
- util/hex.o util/crypto_openssl.o util/json.o util/io.o \
- util/seal.o util/config.o util/log.o util/malloc.o \
+ util/hex.o util/crypto_openssl.o util/json.o util/string.o \
+ util/io.o util/seal.o util/config.o util/log.o util/malloc.o \
util/initrd.o $(VERSION_OBJ)
REGISTER_LIBS := $(LIBCRYPTO_LIBS) $(LIBSSL_LIBS) $(TSS2_LIBS)
@@ -646,7 +647,7 @@ testing/helper/test-otel-map: testing/helper/test-otel-map.c util/upatch_maps.o
util/upatch_maps_otel.o util/crypto_openssl.o \
util/tls_openssl.o util/serializer.o util/seal.o \
util/upatch_decode.o util/upatch_resolve.o \
- util/upatch.o util/json.o util/log.o \
+ util/upatch.o util/json.o util/string.o util/log.o \
util/io.o util/malloc.o $(LIBBPF_A) util/hex.o util/timefmt.o
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(DYNAMIC_LIBS)
@@ -663,7 +664,8 @@ testing/helper/test-upatch-prog-metadata: testing/helper/test-upatch-prog-metada
testing/helper/test-upatch-serializer-pages: testing/helper/test-upatch-serializer-pages.c \
util/upatch_maps.o util/upatch_resolve.o \
util/serializer.o util/seal.o \
- util/json.o util/malloc.o util/io.o \
+ util/json.o util/string.o \
+ util/malloc.o util/io.o \
util/log.o util/hex.o util/timefmt.o
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(STATIC_LIBS) $(DYNAMIC_LIBS)
@@ -696,7 +698,8 @@ FUZZ_CFLAGS := -O1 -g -std=gnu11 -fno-omit-frame-pointer \
-D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(INCLUDES) \
$(LIBCRYPTO_CFLAGS) $(LIBCURL_CFLAGS) $(ZLIB_CFLAGS)
FUZZ_LIBS := $(LIBCRYPTO_LIBS) $(LIBSSL_LIBS) $(LIBCURL_LIBS) $(ZLIB_LIBS)
-FUZZ_BASE := util/json.c util/hex.c util/malloc.c testing/fuzz/log.c
+FUZZ_BASE := util/json.c util/string.c util/hex.c util/malloc.c \
+ testing/fuzz/log.c
FUZZ_OCI := util/bpm_oci.c util/io.c util/seal.c util/crypto_openssl.c \
util/tls_openssl.c
FUZZ_PACKAGE := util/bpm_package.c util/bpm_tree.c $(FUZZ_OCI)
diff --git a/testing/fuzz/corpus/json/numbers b/testing/fuzz/corpus/json/numbers
new file mode 100644
index 00000000..747050f5
--- /dev/null
+++ b/testing/fuzz/corpus/json/numbers
@@ -0,0 +1 @@
+{"i":-9223372036854775808,"o":{"n":9223372036854775807},"skip":[0,-0,1.5e+10,-0.0E-1,2E5,-7]}
\ No newline at end of file
diff --git a/testing/t0001-json-parser.sh b/testing/t0001-json-parser.sh
index f9d73733..9144d125 100644
--- a/testing/t0001-json-parser.sh
+++ b/testing/t0001-json-parser.sh
@@ -95,4 +95,32 @@ test_expect_success 'unterminated array -> EINVAL (no crash)' '
printf "%s" "{\"names\":[\"a\",\"b\"" | ./$H | grep -qx "rc=-22"
'
+test_expect_success 'an integer field takes an integer as JSON spells one' '
+ for v in 1.9 1e9 1E9 7-3 1+1 - . 01 -01 1. .5 1e --1 0x1 +1; do
+ printf "%s" "{\"schema\":$v}" | ./$H | grep -qx "rc=-22" ||
+ return 1
+ done
+'
+
+test_expect_success 'an integer field holds any __s64 and nothing past it' '
+ printf "%s" "{\"schema\":-9223372036854775808}" | ./$H |
+ grep -q "schema=-9223372036854775808 " &&
+ printf "%s" "{\"schema\":9223372036854775807}" | ./$H |
+ grep -q "schema=9223372036854775807 " &&
+ printf "%s" "{\"schema\":-0}" | ./$H | grep -q "schema=0 " &&
+ printf "%s" "{\"schema\":9223372036854775808}" | ./$H |
+ grep -qx "rc=-22" &&
+ printf "%s" "{\"schema\":-9223372036854775809}" | ./$H |
+ grep -qx "rc=-22"
+'
+
+test_expect_success 'a number in a discarded value follows the grammar too' '
+ printf "%s" "{\"x\":[1.5e+10,-0.0E-1,2e5,0],\"schema\":2}" | ./$H |
+ grep -q "schema=2 " &&
+ for v in 1. 01 1e+ -.5 1.e5; do
+ printf "%s" "{\"x\":$v,\"schema\":2}" | ./$H |
+ grep -qx "rc=-22" || return 1
+ done
+'
+
test_done
diff --git a/util/json.c b/util/json.c
index 11febe76..743f770d 100644
--- a/util/json.c
+++ b/util/json.c
@@ -7,11 +7,12 @@
*/
#include <errno.h>
+#include <stdint.h>
#include <stdio.h>
-#include <stdlib.h>
#include <string.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "hex.h"
#include "json.h"
@@ -558,38 +559,50 @@ static int lit(struct pctx *c, const char *s)
return 0;
}
+static int skip_digits(struct pctx *c)
+{
+ size_t n = shirudo_digits(c->p, (size_t)(c->end - c->p));
+
+ c->p += n;
+ return n ? 0 : -EINVAL;
+}
+
+/*
+ * A number as JSON spells one: -?(0|[1-9][0-9]*)(.[0-9]+)?([eE][+-]?[0-9]+)?
+ * An integer field takes the integer part alone, and only within __s64.
+ */
static int parse_number(struct pctx *c, struct shirudo_json_field *f)
{
const char *start = c->p;
+ const char *int_end;
if (c->p < c->end && *c->p == '-')
c->p++;
- while (c->p < c->end && ((*c->p >= '0' && *c->p <= '9') ||
- *c->p == '.' || *c->p == 'e' ||
- *c->p == 'E' || *c->p == '+' || *c->p == '-'))
+ if (c->p < c->end && *c->p == '0')
c->p++;
- if (c->p == start)
+ else if (skip_digits(c))
return -EINVAL;
- if (f) {
- char numbuf[32];
- size_t nlen = (size_t)(c->p - start);
-
- if (f->type != SHIRUDO_JSON_T_INT)
+ int_end = c->p;
+ if (c->p < c->end && *c->p == '.') {
+ c->p++;
+ if (skip_digits(c))
return -EINVAL;
- /*
- * strtoll scans to a non-numeric byte or NUL; the input
- * buffer is not guaranteed NUL-terminated (the API is
- * (data, len)), so copy the already-bounded token into a
- * NUL-terminated scratch first. A token too long to be a
- * valid int64 is rejected rather than read.
- */
- if (nlen >= sizeof(numbuf))
+ }
+ if (c->p < c->end && (*c->p == 'e' || *c->p == 'E')) {
+ c->p++;
+ if (c->p < c->end && (*c->p == '+' || *c->p == '-'))
+ c->p++;
+ if (skip_digits(c))
return -EINVAL;
- memcpy(numbuf, start, nlen);
- numbuf[nlen] = '\0';
- *(__s64 *)f->dst = (__s64)strtoll(numbuf, NULL, 10);
- f->seen = 1;
}
+ if (!f)
+ return 0;
+ if (f->type != SHIRUDO_JSON_T_INT || c->p != int_end)
+ return -EINVAL;
+ if (parse_s64(start, (size_t)(int_end - start), INT64_MIN, INT64_MAX,
+ f->dst))
+ return -EINVAL;
+ f->seen = 1;
return 0;
}