From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id E2B5945CFC;
	Thu, 14 Nov 2024 02:11:59 +0100 (CET)
Received: from mails.dpdk.org (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id D1CA1427A5;
	Thu, 14 Nov 2024 02:11:45 +0100 (CET)
Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com
 [209.85.215.177])
 by mails.dpdk.org (Postfix) with ESMTP id 9FFC741143
 for <dev@dpdk.org>; Thu, 14 Nov 2024 02:11:43 +0100 (CET)
Received: by mail-pg1-f177.google.com with SMTP id
 41be03b00d2f7-7ee7e87f6e4so22582a12.2
 for <dev@dpdk.org>; Wed, 13 Nov 2024 17:11:43 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1731546703;
 x=1732151503; darn=dpdk.org; 
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:cc:to:from:from:to:cc:subject:date
 :message-id:reply-to;
 bh=Xbu7/x2u6B0f4NR7fqNiivoQ71uwlE5LuH1+ITHjMk8=;
 b=r2A/dz6LI+WtJWbWgLBdm+rTMbqWZojW3j9cOarCtfRKj9HnQ5apXB5oHTYGwbWmgt
 0pMrZLq2BJ+xkHOpp2mqGqokS/UFjhUYy1UjDKUKCWcbTl1WSbYrvfCCr9aZSW/XhbjG
 xvrWmA/7jD2Op8wE8hLS3Xp2au8v0avIR9kf3uhidpKPhOlIsil7GyjATTjVXvcBNDGd
 ywSun4dBbpRdjLgE+w3i11uI4SCl5N9A4Z+9kKn5Cxx91gQAQviz81NO7d4jbty2bgho
 WiK4FQS/5ASmeuwwzpKL4KlJUbSLXCw70mE+8iFEDMXQwUqDpfU3RsD20X4SGAimp3VJ
 KB5g==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1731546703; x=1732151503;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc
 :subject:date:message-id:reply-to;
 bh=Xbu7/x2u6B0f4NR7fqNiivoQ71uwlE5LuH1+ITHjMk8=;
 b=wD4k6wSaCHq7lVaYFyCW2LxHBSOScf14ZeGxURlpb8ApXZ5tpTsXhCbPIbYsZmaPUc
 jk3T8xKx4aHSjtlHnDx4j6WUYjgP2qYIQ5RezL6IM0FZsU7OOv7qJTj//7yECbjOrDay
 4zRQmhE2wud1g4XqPSkHu0mrji2i+QgtPkhn9e5WQYZBEQzYZMvGwa14wF2bCFSTaKa9
 BA1x8H0Iz4qb/3mskIiHXGF+BAopYeYnQsv9aDK1Knnbo5VZr2qYITwpxVulVoJUDhjB
 3EJsBoCtvUM8an1FgJwXBoMTz81ufwrs9eTvhmwdRkQ7K+cyWTl9VH8iJdxkexNFLJVB
 KH0g==
X-Gm-Message-State: AOJu0YwcnynvXa9Z3fwiv+h4KHndfDRNIbt7izZniBhU+qjtRkMOwcQO
 BkHah03NI4BjiakYC8dGaWWEU5rjZWlhMFcaloSomZpghda/110EFCDjLym0QX9XhtAA11dPCDB
 6
X-Google-Smtp-Source: AGHT+IG/jMuEkwAsNK5D4UT3wVPZikV2MC0r9Smts8ps6F7A2ylQlKl3HR1yXc3BWgBccI0rsvuzuw==
X-Received: by 2002:a05:6a21:778f:b0:1db:f06e:f666 with SMTP id
 adf61e73a8af0-1dc834aede0mr423126637.41.1731546702845; 
 Wed, 13 Nov 2024 17:11:42 -0800 (PST)
Received: from hermes.local (204-195-96-226.wavecable.com. [204.195.96.226])
 by smtp.gmail.com with ESMTPSA id
 d2e1a72fcca58-7246a9a8102sm60950b3a.125.2024.11.13.17.11.42
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Wed, 13 Nov 2024 17:11:42 -0800 (PST)
From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>,
 Anatoly Burakov <anatoly.burakov@intel.com>,
 Tyler Retzlaff <roretzla@linux.microsoft.com>
Subject: [PATCH 3/3] eal: add rte_free_sensative
Date: Wed, 13 Nov 2024 17:10:18 -0800
Message-ID: <20241114011129.451243-4-stephen@networkplumber.org>
X-Mailer: git-send-email 2.45.2
In-Reply-To: <20241114011129.451243-1-stephen@networkplumber.org>
References: <20241114011129.451243-1-stephen@networkplumber.org>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org

Although internally rte_free does poison the buffer in most
cases, it is useful to have function that explicitly does
this to avoid any security issues.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/rte_malloc.c  | 30 ++++++++++++++++++++++++------
 lib/eal/include/rte_malloc.h | 18 ++++++++++++++++++
 lib/eal/version.map          |  3 +++
 3 files changed, 45 insertions(+), 6 deletions(-)

diff --git a/lib/eal/common/rte_malloc.c b/lib/eal/common/rte_malloc.c
index 3eed4d4be6..bd543b46e8 100644
--- a/lib/eal/common/rte_malloc.c
+++ b/lib/eal/common/rte_malloc.c
@@ -15,6 +15,7 @@
 #include <rte_eal_memconfig.h>
 #include <rte_common.h>
 #include <rte_spinlock.h>
+#include <rte_string_fns.h>
 
 #include <eal_trace_internal.h>
 
@@ -27,27 +28,44 @@
 
 
 /* Free the memory space back to heap */
-static void
-mem_free(void *addr, const bool trace_ena)
+static inline void
+mem_free(void *addr, const bool trace_ena, bool zero)
 {
+	struct malloc_elem *elem;
+
 	if (trace_ena)
 		rte_eal_trace_mem_free(addr);
 
-	if (addr == NULL) return;
-	if (malloc_heap_free(malloc_elem_from_data(addr)) < 0)
+	if (addr == NULL)
+		return;
+
+	elem = malloc_elem_from_data(addr);
+	if (zero) {
+		size_t data_len = elem->size - MALLOC_ELEM_OVERHEAD;
+
+		rte_memset_sensative(addr, 0, data_len);
+	}
+
+	if (malloc_heap_free(elem) < 0)
 		EAL_LOG(ERR, "Error: Invalid memory");
 }
 
 void
 rte_free(void *addr)
 {
-	mem_free(addr, true);
+	mem_free(addr, true, false);
+}
+
+void
+rte_free_sensative(void *addr)
+{
+	mem_free(addr, true, true);
 }
 
 void
 eal_free_no_trace(void *addr)
 {
-	mem_free(addr, false);
+	mem_free(addr, false, false);
 }
 
 static void *
diff --git a/lib/eal/include/rte_malloc.h b/lib/eal/include/rte_malloc.h
index c8836de67c..6675c3f405 100644
--- a/lib/eal/include/rte_malloc.h
+++ b/lib/eal/include/rte_malloc.h
@@ -51,6 +51,24 @@ struct rte_malloc_socket_stats {
 void
 rte_free(void *ptr);
 
+
+/**
+ * Frees the memory space pointed to by the provided pointer
+ * and guarantees it will be zero'd before reuse.
+ *
+ * This pointer must have been returned by a previous call to
+ * rte_malloc(), rte_zmalloc(), rte_calloc() or rte_realloc(). The behaviour of
+ * rte_free() is undefined if the pointer does not match this requirement.
+ *
+ * If the pointer is NULL, the function does nothing.
+ *
+ * @param ptr
+ *   The pointer to memory to be freed.
+ */
+__rte_experimental
+void
+rte_free_sensative(void *ptr);
+
 /**
  * This function allocates memory from the huge-page area of memory. The memory
  * is not cleared. In NUMA systems, the memory allocated resides on the same
diff --git a/lib/eal/version.map b/lib/eal/version.map
index a20c713eb1..4e0203053a 100644
--- a/lib/eal/version.map
+++ b/lib/eal/version.map
@@ -398,6 +398,9 @@ EXPERIMENTAL {
 	# added in 24.11
 	rte_bitset_to_str;
 	rte_lcore_var_alloc;
+
+	# added in 25.03
+	rte_free_sensative;
 };
 
 INTERNAL {
-- 
2.45.2