Commit a590ab2c3a for strongswan.org
commit a590ab2c3a940d85248c0cb765344ce465e1e5b5
Author: Tobias Brunner <tobias@strongswan.org>
Date: Thu Nov 26 15:43:41 2026 +0100
utils: Add helpers to wipe and free memory/strings
diff --git a/src/libstrongswan/tests/suites/test_utils.c b/src/libstrongswan/tests/suites/test_utils.c
index a41e9a9c3f..088a0f1dfa 100644
--- a/src/libstrongswan/tests/suites/test_utils.c
+++ b/src/libstrongswan/tests/suites/test_utils.c
@@ -951,6 +951,7 @@ END_TEST
START_TEST(test_memwipe_null)
{
memwipe(NULL, 16);
+ memwipe_free(NULL, 16);
}
END_TEST
@@ -998,6 +999,64 @@ START_TEST(test_memwipe_heap)
memwipe(buf, len);
ck_assert(test_wiped_memory(buf, len));
free(buf);
+
+ buf = malloc(len);
+ for (i = 0; i < len; i++)
+ {
+ buf[i] = i;
+ }
+ memwipe_free(buf, len);
+}
+END_TEST
+
+/*******************************************************************************
+ * strwipe
+ */
+
+START_TEST(test_strwipe_null)
+{
+ strwipe(NULL);
+ strwipe_free(NULL);
+}
+END_TEST
+
+START_TEST(test_strwipe_stack)
+{
+ char buf[64];
+ int i;
+
+ for (i = 0; i < sizeof(buf); i++)
+ {
+ buf[i] = i+1;
+ }
+ buf[sizeof(buf)-1] = 0;
+ strwipe(buf);
+ ck_assert(test_wiped_memory(buf, sizeof(buf)));
+}
+END_TEST
+
+START_TEST(test_strwipe_heap)
+{
+ size_t len = 64;
+ char *buf = malloc(len);
+ int i;
+
+ for (i = 0; i < len; i++)
+ {
+ buf[i] = i+1;
+ }
+ buf[len-1] = 0;
+ strwipe(buf);
+ ck_assert(test_wiped_memory(buf, len));
+ free(buf);
+
+ buf = malloc(len);
+ for (i = 0; i < len; i++)
+ {
+ buf[i] = i+1;
+ }
+ buf[len-1] = 0;
+ strwipe_free(buf);
}
END_TEST
@@ -1739,6 +1798,12 @@ Suite *utils_suite_create()
tcase_add_test(tc, test_memwipe_heap);
suite_add_tcase(s, tc);
+ tc = tcase_create("strwipe");
+ tcase_add_test(tc, test_strwipe_null);
+ tcase_add_test(tc, test_strwipe_stack);
+ tcase_add_test(tc, test_strwipe_heap);
+ suite_add_tcase(s, tc);
+
tc = tcase_create("utils_memrchr");
tcase_add_loop_test(tc, test_utils_memrchr, 0, countof(memrchr_data));
suite_add_tcase(s, tc);
diff --git a/src/libstrongswan/utils/utils/memory.h b/src/libstrongswan/utils/utils/memory.h
index c69b36759d..96a133b186 100644
--- a/src/libstrongswan/utils/utils/memory.h
+++ b/src/libstrongswan/utils/utils/memory.h
@@ -158,6 +158,15 @@ static inline void memwipe(void *ptr, size_t n)
}
#endif /* HAVE_EXPLICIT_BZERO */
+/**
+ * Safely overwrite n bytes of memory at ptr with zero and then free it.
+ */
+static inline void memwipe_free(void *ptr, size_t n)
+{
+ memwipe(ptr, n);
+ free(ptr);
+}
+
/**
* A variant of strstr with the characteristics of memchr, where haystack is not
* a null-terminated string but simply a memory area of length n.
diff --git a/src/libstrongswan/utils/utils/string.h b/src/libstrongswan/utils/utils/string.h
index 8fdb242400..3e6465067c 100644
--- a/src/libstrongswan/utils/utils/string.h
+++ b/src/libstrongswan/utils/utils/string.h
@@ -23,6 +23,8 @@
#ifndef STRING_H_
#define STRING_H_
+#include "memory.h"
+
#include <ctype.h>
/**
@@ -103,6 +105,28 @@ static inline char *strdupnull(const char *s)
return s ? strdup(s) : NULL;
}
+/**
+ * Wipe the given string (only to the first 0-byte, use memwipe() if the buffer
+ * is larger and the length is known).
+ */
+static inline void strwipe(char *s)
+{
+ if (s)
+ {
+ memwipe(s, strlen(s));
+ }
+}
+
+/**
+ * Wipe and free the given string (only to the first 0-byte, use memwipe_free()
+ * if the buffer is larger and the length is known).
+ */
+static inline void strwipe_free(char *s)
+{
+ strwipe(s);
+ free(s);
+}
+
/**
* Translates the characters in the given string, searching for characters
* in 'from' and mapping them to characters in 'to'.