eBPF-based security platform
hex: check lowercase hex digits with one helper
John Fastabendd2d6a572884f
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex 6ce57fde..c7560bc7 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -56,7 +56,7 @@ log-escape --unwind 7 LEN=0-5json-emit-string --unwind 5 --unwindset json_string.0:27,put_string_n.0:5,put_raw.0:7,well_formed.0:27,replaced.0:5,memchr.0:9,memcmp.0:13 LEN=0-4json-emit-buffer --unwind 1 --unwindset json_raw_value.0:5,harness.0:5 --malloc-may-fail --malloc-fail-null LEN=0-4json-emit-nest --object-bits 12 --unwind 1 --unwindset json_text.0:57,sequences.0:4,sequences.1:5,any_depth.0:4,any_depth.1:4,strcmp.0:12,memchr.0:9,put_string_n.0:2,put_string_n.1:2,put_raw.0:8,json_kv_raw.0:3,json_raw_value.0:3,json_raw.0:5,strlen.0:3,vsnprintf.2:5,vsnprintf.0:2,format_number.0:3,format_number.2:5,format_number.3:3-keepalive-emit -I ../../.. --object-bits 12 --unwind 1 --unwindset json_members.0:833,power_of_ten.0:6,put_string_n.0:5,put_string_n.1:66,put_raw.0:7,strlen.0:66,is_hash.0:66,kv_tamper.0:38,kv_no_tree.0:8,json_raw_value.0:6,vsnprintf.2:4,vsnprintf.0:2,format_number.0:6,format_number.2:5,format_number.3:6,ends_in_any_number.0:6,ends_in_any_number.1:833,ends_in_any_number.2:5 LEN=0-3+keepalive-emit -I ../../.. --object-bits 12 --unwind 1 --unwindset json_members.0:833,power_of_ten.0:6,put_string_n.0:5,put_string_n.1:66,put_raw.0:7,strlen.0:66,hex_is_lower.0:66,kv_tamper.0:38,kv_no_tree.0:8,json_raw_value.0:6,vsnprintf.2:4,vsnprintf.0:2,format_number.0:6,format_number.2:5,format_number.3:6,ends_in_any_number.0:6,ends_in_any_number.1:833,ends_in_any_number.2:5 LEN=0-3json-string --unwind 5 --unwindset json_string.0:7,parse_string.0:5,parse_string.1:7,memchr.0:9,memcmp.0:7,harness.0:8 LEN=0-6json-escape --unwind 5 --unwindset parse_string.0:4,memcmp.0:9json-roundtrip --unwind 1 --unwindset put_string_n.0:5,put_string_n.1:5,put_raw.0:7,vsnprintf.2:4,vsnprintf.0:2,format_number.0:3,format_number.2:5,format_number.3:3,parse_string.1:6,parse_string.0:4,hex4.0:5,utf8_width.0:4,memcmp.0:5 LEN=0-4diff --git a/testing/unit/hex.c b/testing/unit/hex.cindex e92f5f7e..ecf13ad8 100644--- a/testing/unit/hex.c+++ b/testing/unit/hex.c@@ -42,6 +42,20 @@ static void decode_refuses_non_hex(void)EXPECT_EQ(hex_decode("zz", out, 1), -EINVAL);}+/* Lowercase digits only, and nothing read past the first other byte. */+static void is_lower_takes_lowercase_digits_only(void)+{+ char stop[2] = { 'a', '\0' };++ EXPECT_TRUE(hex_is_lower("0123456789abcdef", 16));+ EXPECT_TRUE(hex_is_lower("", 0));+ EXPECT_TRUE(hex_is_lower("ab\x01", 2));+ EXPECT_FALSE(hex_is_lower("0123456789ABCDEF", 16));+ EXPECT_FALSE(hex_is_lower("abcg", 4));+ EXPECT_FALSE(hex_is_lower("ab", 3));+ EXPECT_FALSE(hex_is_lower(stop, 64));+}+static void decode_max_takes_an_even_count_up_to_cap(void){__u8 out[3] = {};@@ -88,6 +102,7 @@ static const struct unit_case cases[] = {UNIT_CASE(decode_takes_either_case),UNIT_CASE(decode_wants_the_exact_length),UNIT_CASE(decode_refuses_non_hex),+ UNIT_CASE(is_lower_takes_lowercase_digits_only),UNIT_CASE(decode_max_takes_an_even_count_up_to_cap),UNIT_CASE(sha256_digest_is_prefix_and_64_digits),{}diff --git a/tools/shirudo-build/image.c b/tools/shirudo-build/image.cindex 23e9caaf..4f4040b4 100644--- a/tools/shirudo-build/image.c+++ b/tools/shirudo-build/image.c@@ -210,10 +210,8 @@ static int sign_root(const char *input, const char *output, const char *pkey,close(fd);if (ret)return ret;- for (done = 0; done < 64; done++)- if (!((root[done] >= '0' && root[done] <= '9') ||- (root[done] >= 'a' && root[done] <= 'f')))- return -EINVAL;+ if (!hex_is_lower(root, 64))+ return -EINVAL;sign.insns = root;sign.insns_sz = 64;sign.signature = signature;diff --git a/util/config_image.c b/util/config_image.cindex eb5469e0..6f1ae1bd 100644--- a/util/config_image.c+++ b/util/config_image.c@@ -165,7 +165,6 @@ static int read_root(const char *prefix, __u8 root[32]){char text[65] = {};__u64 size;- __u32 i;int fd, ret;fd = open_part(prefix, ".roothash");@@ -179,10 +178,8 @@ static int read_root(const char *prefix, __u8 root[32])close(fd);if (ret)return ret;- for (i = 0; i < 64; i++)- if (!((text[i] >= '0' && text[i] <= '9') ||- (text[i] >= 'a' && text[i] <= 'f')))- return -EINVAL;+ if (!hex_is_lower(text, 64))+ return -EINVAL;return hex_decode(text, root, 32);}diff --git a/util/hex.c b/util/hex.cindex 0b522791..5ae407f4 100644--- a/util/hex.c+++ b/util/hex.c@@ -42,6 +42,17 @@ int hex_nibble(int c)return -1;}+bool hex_is_lower(const char *s, size_t n)+{+ size_t i;++ for (i = 0; i < n; i++)+ if (!(s[i] >= '0' && s[i] <= '9') &&+ !(s[i] >= 'a' && s[i] <= 'f'))+ return false;+ return true;+}+int hex_decode(const char *hex, __u8 *out, size_t n){size_t i;diff --git a/util/hex.h b/util/hex.hindex 04c2c613..e776b9c8 100644--- a/util/hex.h+++ b/util/hex.h@@ -8,6 +8,7 @@#ifndef SHIRUDO_HEX_H#define SHIRUDO_HEX_H+#include <stdbool.h>#include <stddef.h>#include <shirudo/misc.h>@@ -19,6 +20,11 @@ char *hex_alloc(const void *in, size_t n);/* The value of one hex digit, -1 for any other byte. */int hex_nibble(int c);+/*+ * Whether the n bytes at s are all lowercase hex digits; it reads no+ * further than the first that is not, a terminator included.+ */+bool hex_is_lower(const char *s, size_t n);/* Exactly 2 * n hex digits, either case. 0 or -EINVAL. */int hex_decode(const char *hex, __u8 *out, size_t n);diff --git a/util/keepalive.c b/util/keepalive.cindex 723121f0..a3fdfbff 100644--- a/util/keepalive.c+++ b/util/keepalive.c@@ -8,6 +8,7 @@#include <shirudo/string.h>#include "crypto.h"+#include "hex.h"#include "json.h"#include "keepalive.h"@@ -93,15 +94,7 @@ __u16 keepalive_cond(const struct condition *conds, size_t n)static bool is_hash(const char *s){- size_t i;-- if (!s)- return false;- for (i = 0; i < HASH_HEX_LEN; i++)- if (!(s[i] >= '0' && s[i] <= '9') &&- !(s[i] >= 'a' && s[i] <= 'f'))- return false;- return !s[HASH_HEX_LEN];+ return s && hex_is_lower(s, HASH_HEX_LEN) && !s[HASH_HEX_LEN];}static bool is_absent(const char *s)