[v7,06/16] app/test: add test for rte_free_sensitive

Message ID 20250215190544.988310-7-stephen@networkplumber.org (mailing list archive)
State Superseded
Delegated to: Thomas Monjalon
Headers
Series Introduce secure memory zero functions |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Stephen Hemminger Feb. 15, 2025, 7:04 p.m. UTC
Similar to test for rte_memset_explicit, use a worker thread
to free and then check the result.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/test_malloc.c | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
  

Patch

diff --git a/app/test/test_malloc.c b/app/test/test_malloc.c
index 4acb827543..e6ca60ff8c 100644
--- a/app/test/test_malloc.c
+++ b/app/test/test_malloc.c
@@ -1040,6 +1040,44 @@  test_alloc_socket(void)
 	return 0;
 }
 
+static int
+run_rte_free_sensitive(void *arg)
+{
+	rte_free_sensitive(arg);
+	return 0;
+}
+
+static int
+test_free_sensitive(void)
+{
+#define SENSITIVE_KEY_SIZE	128
+
+	if (rte_lcore_count() < 2) {
+		printf("Need multiple cores to run memzero explicit test.\n");
+		return TEST_SKIPPED;
+	}
+
+	unsigned int worker_lcore_id = rte_get_next_lcore(-1, 1, 0);
+	TEST_ASSERT(worker_lcore_id < RTE_MAX_LCORE, "get_next_lcore failed");
+
+	/* Allocate a buffer and fill with sensitive data */
+	char *key = rte_zmalloc("dummy", SENSITIVE_KEY_SIZE, 0);
+	TEST_ASSERT(key != NULL, "rte_zmalloc failed");
+	rte_strscpy(key, "Super secret key", SENSITIVE_KEY_SIZE);
+
+	/* Pass that data to worker thread to free */
+	int rc = rte_eal_remote_launch(run_rte_free_sensitive, key, worker_lcore_id);
+	TEST_ASSERT(rc == 0, "Worker thread launch failed");
+
+	rte_eal_mp_wait_lcore();
+
+	/* Do intentional use after free check to ensure it is zero */
+	for (unsigned int i = 0; i < SENSITIVE_KEY_SIZE; i++)
+		TEST_ASSERT(key[i] == 0, "rte_free_sensitive data not zero");
+	return 0;
+}
+
+
 static struct unit_test_suite test_suite = {
 	.suite_name = "Malloc test suite",
 	.unit_test_cases = {
@@ -1053,6 +1091,7 @@  static struct unit_test_suite test_suite = {
 		TEST_CASE(test_rte_malloc_validate),
 		TEST_CASE(test_alloc_socket),
 		TEST_CASE(test_multi_alloc_statistics),
+		TEST_CASE(test_free_sensitive),
 		TEST_CASES_END()
 	}
 };