From d6fdadf6730727c26d0644d84faaa562cf75f4d3 Mon Sep 17 00:00:00 2001 From: Andrew Kozlik Date: Thu, 13 Jan 2022 11:33:58 +0100 Subject: [PATCH] feat(crypto): Sanitize undefined behavior. --- crypto/Makefile | 2 +- crypto/groestl.c | 4 ++++ crypto/sha3.c | 2 ++ storage/tests/c/Makefile | 2 +- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/crypto/Makefile b/crypto/Makefile index bcb098621..c5e85001f 100644 --- a/crypto/Makefile +++ b/crypto/Makefile @@ -3,7 +3,7 @@ CC ?= clang LD ?= $(CC) SANFLAGS += -fsanitize=fuzzer else ifeq ($(ADDRESS_SANITIZER),1) -SANFLAGS += -fsanitize=address +SANFLAGS += -fsanitize=address,undefined endif CC ?= gcc diff --git a/crypto/groestl.c b/crypto/groestl.c index 14db2fd5e..d913c76e0 100644 --- a/crypto/groestl.c +++ b/crypto/groestl.c @@ -688,6 +688,10 @@ groestl_big_init(sph_groestl_big_context *sc, unsigned out_size) static void groestl_big_core(sph_groestl_big_context *sc, const void *data, size_t len) { + if (len == 0) { + return; + } + unsigned char *buf = NULL; size_t ptr = 0; DECL_STATE_BIG diff --git a/crypto/sha3.c b/crypto/sha3.c index 55fbef335..8971300c0 100644 --- a/crypto/sha3.c +++ b/crypto/sha3.c @@ -283,6 +283,8 @@ static void sha3_process_block(uint64_t hash[25], const uint64_t *block, size_t */ void sha3_Update(SHA3_CTX *ctx, const unsigned char *msg, size_t size) { + if (size == 0) return; + size_t idx = (size_t)ctx->rest; size_t block_size = (size_t)ctx->block_size; diff --git a/storage/tests/c/Makefile b/storage/tests/c/Makefile index 6b7c54eac..ba0dd1330 100644 --- a/storage/tests/c/Makefile +++ b/storage/tests/c/Makefile @@ -1,5 +1,5 @@ CC = gcc -CFLAGS = -Wall -Wshadow -Wextra -Wpedantic -Werror -Wno-missing-braces -fPIC -fsanitize=address +CFLAGS = -Wall -Wshadow -Wextra -Wpedantic -Werror -Wno-missing-braces -fPIC -fsanitize=address,undefined LIBS = INC = -I ../../../crypto -I ../.. -I . BASE = ../../../