aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2026-05-15 01:22:19 +0200
committerMattias Andrée <m@maandree.se>2026-05-15 01:22:19 +0200
commit68d6804a43dca2749a68a557e67b98e6005ead83 (patch)
tree9c4137a48cd37ea423578d1ac3df6e3953cb2156
parentFix minor errors in the test and check that we are not writing out of bounds (diff)
downloadlibrecrypt-68d6804a43dca2749a68a557e67b98e6005ead83.tar.gz
librecrypt-68d6804a43dca2749a68a557e67b98e6005ead83.tar.bz2
librecrypt-68d6804a43dca2749a68a557e67b98e6005ead83.tar.xz
Fix some minor issues
Signed-off-by: Mattias Andrée <m@maandree.se>
-rw-r--r--argon2/hash.c5
-rw-r--r--argon2/make_settings.c9
-rw-r--r--common.h15
-rw-r--r--config.mk10
-rw-r--r--librecrypt_add_algorithm.c9
-rw-r--r--librecrypt_hash_.c9
-rw-r--r--librecrypt_realise_salts.c6
-rw-r--r--libtest/common.h5
-rw-r--r--libtest/libtest_fd_tracking.c2
-rw-r--r--libtest/libtest_free.c2
10 files changed, 56 insertions, 16 deletions
diff --git a/argon2/hash.c b/argon2/hash.c
index 9223e55..9c65e6a 100644
--- a/argon2/hash.c
+++ b/argon2/hash.c
@@ -100,8 +100,9 @@ librecrypt__argon2__hash(char *restrict out_buffer, size_t size, const char *phr
type[1u] == 's' ? LIBAR2_ARGON2DS :
type[0u] == 'i' ? LIBAR2_ARGON2I :
LIBAR2_ARGON2D;
- params.version = version[3u] == '9' ? LIBAR2_ARGON2_VERSION_13 : /* 19 = 0x13 = 1.3 */
- LIBAR2_ARGON2_VERSION_10; /* 16 = 0x10 = 1.0 */
+ params.version = !*version ? LIBAR2_ARGON2_VERSION_10 :
+ version[3u] == '9' ? LIBAR2_ARGON2_VERSION_13 : /* 19 = 0x13 = 1.3 */
+ LIBAR2_ARGON2_VERSION_10; /* 16 = 0x10 = 1.0 */
params.t_cost = (uint_least32_t)tcost;
params.m_cost = (uint_least32_t)mcost;
params.lanes = (uint_least32_t)lanes;
diff --git a/argon2/make_settings.c b/argon2/make_settings.c
index 4354196..bac49a4 100644
--- a/argon2/make_settings.c
+++ b/argon2/make_settings.c
@@ -47,7 +47,8 @@ make_settings(char *out_buffer, size_t size, const char *algorithm, size_t memco
algolen = p ? (size_t)(p - algorithm) : strlen(algorithm);
if (algolen > 32u) /* just some small value absolute will fit all variants */
abort(); /* $covered$ */
- if (p++ && *p++ == 'v') {
+ if (p && p[1u] == 'v') {
+ p = &p[2u];
if (!strncmp(p, "=16", 3u) && (!p[3u] || p[3u] == '$'))
version = "16";
else if (!strncmp(p, "=19", 3u) && (!p[3u] || p[3u] == '$'))
@@ -81,7 +82,8 @@ make_settings(char *out_buffer, size_t size, const char *algorithm, size_t memco
} else {
ret += len = sizeof("*16") - 1u;
min = size ? MIN(len, size - 1u) : 0u;
- memcpy(out_buffer, "*16", min);
+ if (min)
+ memcpy(out_buffer, "*16", min);
}
out_buffer = &out_buffer[min];
size -= min;
@@ -89,7 +91,8 @@ make_settings(char *out_buffer, size_t size, const char *algorithm, size_t memco
/* Add tag size (size of hash result) */
ret += len = sizeof("$*32") - 1u;
min = size ? MIN(len, size - 1u) : 0u;
- memcpy(out_buffer, "$*32", min);
+ if (min)
+ memcpy(out_buffer, "$*32", min);
out_buffer = &out_buffer[min];
size -= min;
diff --git a/common.h b/common.h
index 02b99fa..717f43b 100644
--- a/common.h
+++ b/common.h
@@ -24,6 +24,9 @@
# pragma clang diagnostic ignored "-Wimplicit-void-ptr-cast" /* C++ warning, and we are in internal files */
# pragma clang diagnostic ignored "-Wc++-keyword" /* C++ warning, and we are in internal files */
#endif
+#if defined(__GNUC__)
+# pragma GCC diagnostic ignored "-Winline"
+#endif
#if defined(__GNUC__)
@@ -512,6 +515,16 @@ int librecrypt_check_settings_(const char *settings, size_t len, const char *fmt
EXPECT(libtest_check_no_leaks());\
} while (0)
+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
+# include <stdatomic.h>
+# define MEMFENCE() atomic_thread_fence(memory_order_seq_cst)
+#elif defined(_MSC_VER)
+# include <intrin.h>
+# define MEMFENCE() _ReadWriteBarrier()
+#else
+# define MEMFENCE() __asm__ volatile("" ::: "memory")
+#endif
+
# define EXPECT__(EXPR, HOW, RETEXTRACT, RETEXPECT)\
do {\
pid_t pid__;\
@@ -534,6 +547,7 @@ int librecrypt_check_settings_(const char *settings, size_t len, const char *fmt
# define EXPECT(EXPR)\
do {\
+ MEMFENCE();\
if (!(EXPR)) {\
int test_expect_saved_errno__ = errno;\
libtest_expect_zeroed_on_free(0);\
@@ -547,6 +561,7 @@ int librecrypt_check_settings_(const char *settings, size_t len, const char *fmt
# define assert(EXPR)\
do {\
+ MEMFENCE();\
if (!(EXPR)) {\
libtest_expect_zeroed_on_free(0);\
libtest_stop_tracking();\
diff --git a/config.mk b/config.mk
index 36994f3..e0359ba 100644
--- a/config.mk
+++ b/config.mk
@@ -3,9 +3,15 @@ MANPREFIX = $(PREFIX)/share/man
CC = c99
+COMMON_SANITIZE = -fsanitize=alignment,shift,signed-integer-overflow,object-size,null,undefined,bounds,address
+CLANG_SANITIZE = -O1 $(COMMON_SANITIZE),cfi -flto -fvisibility=hidden -fno-sanitize-trap=cfi
+GCC_SANITIZE = -O1 $(COMMON_SANITIZE)
+#SANITIZE = $(CLANG_SANITIZE)
+#SANITIZE = $(GCC_SANITIZE)
+
CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_GNU_SOURCE
-CFLAGS =
-LDFLAGS =
+CFLAGS = $(SANITIZE)
+LDFLAGS = $(SANITIZE)
G = -g
diff --git a/librecrypt_add_algorithm.c b/librecrypt_add_algorithm.c
index 920577d..4c6520e 100644
--- a/librecrypt_add_algorithm.c
+++ b/librecrypt_add_algorithm.c
@@ -67,8 +67,15 @@ librecrypt_add_algorithm(char *out_buffer, size_t size, const char *augend, cons
out_buffer[0u] = '\0';
}
} else {
+#if defined(__GNUC__)
+# pragma GCC diagnostic push
+# pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
+#endif
if (!hashsize2)
goto out;
+#if defined(__GNUC__)
+# pragma GCC diagnostic pop
+#endif
r_int = snprintf(NULL, 0u, "*%zu", hashsize2);
if (r_int < 2)
abort(); /* $covered$ (impossible reliably) */
@@ -126,7 +133,7 @@ librecrypt_add_algorithm(char *out_buffer, size_t size, const char *augend, cons
/* Chain the hash algorithms: write `augent` */
min = MIN(prefix1, size);
- if (out_buffer != augend)
+ if (out_buffer != augend && min)
memmove(out_buffer, augend, min);
out_buffer = &out_buffer[min];
size -= min;
diff --git a/librecrypt_hash_.c b/librecrypt_hash_.c
index 7438a48..473b738 100644
--- a/librecrypt_hash_.c
+++ b/librecrypt_hash_.c
@@ -184,7 +184,8 @@ next:
}
min = size ? MIN(size - 1u, prefix) : 0u;
size -= min;
- memcpy(out_buffer, settings, min);
+ if (min)
+ memcpy(out_buffer, settings, min);
out_buffer = &out_buffer[min];
ret += prefix;
}
@@ -386,7 +387,7 @@ main(void)
for (i = 0u; i <= sizeof(sbuf); i++) {
CANARY_FILL(sbuf);
EXPECT(librecrypt_hash_(sbuf, i, NULL, 0u, ARGON2ID_PREFIX"*1000$", NULL, ASCII_CRYPT) == r);
- CANARY_X_CHECK(sbuf, (size_t)r, MIN(i, 32u));
+ CANARY_X_CHECK(sbuf, MIN(i, (size_t)r), MIN(i, 32u));
}
if (libtest_have_custom_malloc()) {
@@ -475,7 +476,7 @@ main(void)
EXPECT(!memcmp(buf, buf1, n));
EXPECT(buf[n] == '\0');
}
- CANARY_X_CHECK(buf, (size_t)r1, MIN(i, 32u));
+ CANARY_X_CHECK(buf, MIN(i, (size_t)r1), MIN(i, 32u));
}
if (i <= (size_t)r2 + 10u) {
CANARY_C_FILL(88, buf);
@@ -485,7 +486,7 @@ main(void)
EXPECT(!memcmp(buf, buf2, n));
EXPECT(buf[n] == '\0');
}
- CANARY_X_CHECK(buf, (size_t)r2, MIN(i, 32u));
+ CANARY_X_CHECK(buf, MIN(i, (size_t)r2), MIN(i, 32u));
}
if (i <= (size_t)r3 + 10u) {
CANARY_C_FILL(88, buf);
diff --git a/librecrypt_realise_salts.c b/librecrypt_realise_salts.c
index 8a4b769..8f6d4eb 100644
--- a/librecrypt_realise_salts.c
+++ b/librecrypt_realise_salts.c
@@ -50,7 +50,8 @@ librecrypt_realise_salts(char *restrict out_buffer, size_t size, const char *set
/* Copy text before next '*' */
for (i = 0u; settings[i] != '*'; i++);
min = MIN(i, size);
- memcpy(out_buffer, settings, min);
+ if (min)
+ memcpy(out_buffer, settings, min);
out_buffer = &out_buffer[min];
size -= min;
settings = &settings[i];
@@ -136,7 +137,8 @@ librecrypt_realise_salts(char *restrict out_buffer, size_t size, const char *set
if (settings[i++] == LIBRECRYPT_ALGORITHM_LINK_DELIMITER)
break;
min = MIN(i, size);
- memcpy(out_buffer, settings, min);
+ if (min)
+ memcpy(out_buffer, settings, min);
out_buffer = &out_buffer[min];
size -= min;
settings = &settings[i];
diff --git a/libtest/common.h b/libtest/common.h
index ed9272e..ab59bab 100644
--- a/libtest/common.h
+++ b/libtest/common.h
@@ -34,6 +34,9 @@
# pragma clang diagnostic ignored "-Wunsafe-buffer-usage" /* completely broken warning */
# pragma clang diagnostic ignored "-Wdisabled-macro-expansion" /* clang is being silly: it is common practice, and it complains about libc code */
#endif
+#if defined(__GNUC__)
+# pragma GCC diagnostic ignored "-Winline"
+#endif
#include "libtest.h"
@@ -229,6 +232,7 @@ void *__mremap(void *, size_t, size_t, int, ...);
#define assert(EXPR)\
do {\
+ atomic_thread_fence(memory_order_seq_cst);\
if (!(EXPR)) {\
libtest_malloc_internal_usage++;\
fprintf(stderr, "Assetion failure at %s:%i: %s\n", __FILE__, __LINE__, #EXPR);\
@@ -299,6 +303,7 @@ void *__mremap(void *, size_t, size_t, int, ...);
# define EXPECT(EXPR)\
do {\
+ atomic_thread_fence(memory_order_seq_cst);\
if (!(EXPR)) {\
libtest_malloc_internal_usage++;\
fprintf(stderr, "Failure at %s:%i: %s\n", __FILE__, __LINE__, #EXPR);\
diff --git a/libtest/libtest_fd_tracking.c b/libtest/libtest_fd_tracking.c
index 0dd69d2..81bd9cd 100644
--- a/libtest/libtest_fd_tracking.c
+++ b/libtest/libtest_fd_tracking.c
@@ -134,11 +134,11 @@ next:
new_nopened = 0u;
}
+ free(opened);
if (action >= 0) {
opened = new_opened;
nopened = new_nopened;
} else {
- free(opened);
opened = NULL;
nopened = 0u;
}
diff --git a/libtest/libtest_free.c b/libtest/libtest_free.c
index 396c716..5592e0a 100644
--- a/libtest/libtest_free.c
+++ b/libtest/libtest_free.c
@@ -74,7 +74,7 @@ libtest_free(void *ptr, enum libtest_zero_check zero_checking)
#ifdef WITH_BACKTRACE
if (!inside_free && getenv("TRACE_MALLOC")) {
inside_free = 1;
- fprintf(stderr, "Memory deallocated: %p\n (alloc-size=%zu, real-size=%zu)",
+ fprintf(stderr, "Memory deallocated: %p (alloc-size=%zu, real-size=%zu)\n",
ptr, mem->requested_alloc_size, mem->real_alloc_size);
if (getenv("TRACE_FREE") && !getenv("PRETRACE_FREE"))
libtest_print_backtrace(stderr, NULL, "\tat ", 0u, NULL, NULL);