eBPF-based security platform
json: emit an unsigned 64-bit number with the emitter's own helpers
John Fastabend01d93b956f17
diff --git a/testing/unit/json.c b/testing/unit/json.cindex b1b95213..fcf2ad11 100644--- a/testing/unit/json.c+++ b/testing/unit/json.c@@ -476,6 +476,35 @@ static void emitter_takes_a_string_of_its_length(void)EXPECT_EQ(e.err, -EILSEQ);}+/* All 64 bits of a __u64, as a member and as an array element. */+static void emitter_writes_any_u64(void)+{+ struct json_emit e = { .max = 256 };+ struct json_emit full = {};+ char buf[16];++ json_obj_open(&e);+ json_kv_u64(&e, "max", ~0ULL);+ json_kv_u64(&e, "zero", 0);+ json_arr_open(&e, "a");+ json_u64_value(&e, 1ULL << 63);+ json_u64_value(&e, 7);+ json_arr_close(&e);+ json_obj_close(&e);+ ASSERT_EQ(e.err, 0);+ EXPECT_STREQ(e.buf, "{\"max\":18446744073709551615,\"zero\":0,"+ "\"a\":[9223372036854775808,7]}");+ xfree(e.buf, e.cap);++ full.buf = buf;+ full.cap = sizeof(buf);+ json_obj_open(&full);+ json_kv_u64(&full, "v", ~0ULL);+ EXPECT_EQ(full.err, -ENOSPC);+ json_kv_u64(&full, "w", 1);+ EXPECT_EQ(full.err, -ENOSPC);+}+static void emitter_grows_up_to_its_max(void){struct json_emit deep = { .max = 1u << 20 };@@ -809,6 +838,7 @@ static const struct unit_case cases[] = {UNIT_CASE(malformed_utf8_is_refused),UNIT_CASE(emitter_takes_well_formed_utf8_only),UNIT_CASE(emitter_takes_a_string_of_its_length),+ UNIT_CASE(emitter_writes_any_u64),UNIT_CASE(emitter_grows_up_to_its_max),UNIT_CASE(lossy_writes_replace_bad_bytes),UNIT_CASE(walk_reports_every_token),diff --git a/tools/quote-check/row.c b/tools/quote-check/row.cindex 96a28cdd..edd8f563 100644--- a/tools/quote-check/row.c+++ b/tools/quote-check/row.c@@ -171,24 +171,20 @@ static const char *const reject_reason[] = {static int emit_accept(struct json_emit *e, const struct quote_result *res){- char ak_name[2 * TPM_NAME_LEN + 1], clock[24];char ak_public[BASE64_LEN(TPM_PUBLIC_MAX) + 1];+ char ak_name[2 * TPM_NAME_LEN + 1];int ret;ret = base64_encode(res->ak_public, res->ak_public_len, ak_public,sizeof(ak_public));if (ret)return ret;- /* json_kv_int() takes a signed value and a clock has all 64 bits. */- ret = strsprintf(clock, sizeof(clock), "%llu", res->clock);- if (ret < 0)- return ret;hex_encode(res->ak_name, sizeof(res->ak_name), ak_name);json_kv_int(e, "ok", 1);json_kv_string(e, "ak_name", ak_name);json_kv_string(e, "ak_public", ak_public);json_kv_int(e, "reset_count", res->reset_count);- json_kv_raw(e, "clock", clock, (size_t)ret);+ json_kv_u64(e, "clock", res->clock);return 0;}diff --git a/util/json.c b/util/json.cindex 26b03519..56ba7fa5 100644--- a/util/json.c+++ b/util/json.c@@ -251,6 +251,23 @@ void json_kv_int(struct json_emit *e,put_raw(e, num);}+void json_u64_value(struct json_emit *e, __u64 val)+{+ char num[24];+ int len = strsprintf(num, sizeof(num), "%llu", val);++ if (len < 0)+ e->err = -E2BIG;+ else+ json_raw_value(e, num, len);+}++void json_kv_u64(struct json_emit *e, const char *key, __u64 val)+{+ json_key(e, key);+ json_u64_value(e, val);+}+void json_kv_bool(struct json_emit *e, const char *key, int val){sep(e);diff --git a/util/json.h b/util/json.hindex d08dd21a..be55825f 100644--- a/util/json.h+++ b/util/json.h@@ -66,6 +66,9 @@ void json_kv_string_n(struct json_emit *e, const char *key,const char *val, size_t len);void json_kv_int(struct json_emit *e,const char *key, __s64 val);+/* json_kv_int() takes an __s64, and these all 64 bits of a __u64. */+void json_kv_u64(struct json_emit *e, const char *key, __u64 val);+void json_u64_value(struct json_emit *e, __u64 val);void json_kv_bool(struct json_emit *e,const char *key, int val);void json_kv_null(struct json_emit *e, const char *key);diff --git a/util/keepalive.c b/util/keepalive.cindex 3616dcb9..723121f0 100644--- a/util/keepalive.c+++ b/util/keepalive.c@@ -138,24 +138,6 @@ static void kv_string_or_null(struct json_emit *e, const char *key,json_kv_string(e, key, val);}-/* json_kv_int() takes an __s64, which a tamper counter can pass. */-static void u64_value(struct json_emit *e, __u64 val)-{- char num[24];- int len = strsprintf(num, sizeof(num), "%llu", val);-- if (len < 0)- e->err = -E2BIG;- else- json_raw_value(e, num, len);-}--static void kv_u64(struct json_emit *e, const char *key, __u64 val)-{- json_key(e, key);- u64_value(e, val);-}-static void kv_tamper(struct json_emit *e, const __u64 *tamper){size_t i;@@ -166,7 +148,7 @@ static void kv_tamper(struct json_emit *e, const __u64 *tamper)}json_arr_open(e, "tamper");for (i = 0; i < TAMPER_MAX; i++)- u64_value(e, tamper[i]);+ json_u64_value(e, tamper[i]);json_arr_close(e);}@@ -189,9 +171,9 @@ static void kv_tree(struct json_emit *e, const struct keepalive_input *in)return;}json_kv_string(e, "tree_id", in->tree_id);- kv_u64(e, "generation", in->generation);- kv_u64(e, "base_seq", in->base_seq);- kv_u64(e, "last_seq", in->last_seq);+ json_kv_u64(e, "generation", in->generation);+ json_kv_u64(e, "base_seq", in->base_seq);+ json_kv_u64(e, "last_seq", in->last_seq);kv_string_or_null(e, "base_root", in->base_root);json_kv_string(e, "projection_root", in->projection_root);if (in->mask)@@ -212,13 +194,13 @@ static void body(struct json_emit *e, const struct keepalive_input *in)kv_string_or_null(e, "image_version", in->image_version);kv_string_or_null(e, "image_root", in->image_root);json_kv_string(e, "verdict", in->verdict);- kv_u64(e, "cond", in->cond);- kv_u64(e, "flags", in->flags);+ json_kv_u64(e, "cond", in->cond);+ json_kv_u64(e, "flags", in->flags);kv_tamper(e, in->tamper);kv_tree(e, in);json_kv_string(e, "set_sha256", in->set_sha256);- kv_u64(e, "count", in->count);- kv_u64(e, "not_ok", in->not_ok);+ json_kv_u64(e, "count", in->count);+ json_kv_u64(e, "not_ok", in->not_ok);json_obj_close(e);}diff --git a/util/upatch_rodata.c b/util/upatch_rodata.cindex d33c169b..50408428 100644--- a/util/upatch_rodata.c+++ b/util/upatch_rodata.c@@ -523,11 +523,11 @@ static int json_number(struct json_emit *e, __u64 value,char number[32];int n;- if (is_signed)- n = strsprintf(number, sizeof(number), "%lld", (__s64)value);- else- n = strsprintf(number, sizeof(number), "%llu",- (__u64)value);+ if (!is_signed) {+ json_u64_value(e, value);+ return e->err;+ }+ n = strsprintf(number, sizeof(number), "%lld", (__s64)value);if (n < 0)return n;json_raw_value(e, number, (size_t)n);