From: Stephen Hemminger Subject: [RFC] mem: poison memory when freed Date: Wed, 18 Jul 2018 14:44:34 -0700 Message-ID: <20180718214434.608-1-stephen@networkplumber.org> Cc: dev@dpdk.org, Stephen Hemminger To: sergio.gonzalez.monroy@intel.com Return-path: Received: from mail-pf0-f194.google.com (mail-pf0-f194.google.com [209.85.192.194]) by dpdk.org (Postfix) with ESMTP id 00E6310A3 for ; Wed, 18 Jul 2018 23:44:38 +0200 (CEST) Received: by mail-pf0-f194.google.com with SMTP id c21-v6so2808564pfn.8 for ; Wed, 18 Jul 2018 14:44:38 -0700 (PDT) List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" DPDK malloc library allows broken programs to work because the semantics of zmalloc and malloc are the same. This patch changes to a more secure model which will catch (and crash) programs that reuse memory already freed. This supersedes earlier changes to zero memory on free and avoid zeroing memory in zmalloc. Signed-off-by: Stephen Hemminger --- lib/librte_eal/common/malloc_elem.c | 5 ++++- lib/librte_eal/common/rte_malloc.c | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/librte_eal/common/malloc_elem.c b/lib/librte_eal/common/malloc_elem.c index efcb82677198..62cc0b385c0c 100644 --- a/lib/librte_eal/common/malloc_elem.c +++ b/lib/librte_eal/common/malloc_elem.c @@ -23,6 +23,8 @@ #include "malloc_elem.h" #include "malloc_heap.h" +#define MALLOC_POISON 0x6b /**< Free memory. */ + size_t malloc_elem_find_max_iova_contig(struct malloc_elem *elem, size_t align) { @@ -531,7 +533,8 @@ malloc_elem_free(struct malloc_elem *elem) /* decrease heap's count of allocated elements */ elem->heap->alloc_count--; - memset(ptr, 0, data_len); + /* poison memory */ + memset(ptr, MALLOC_POISON, data_len); return elem; } diff --git a/lib/librte_eal/common/rte_malloc.c b/lib/librte_eal/common/rte_malloc.c index b51a6d111bde..b33c936fd491 100644 --- a/lib/librte_eal/common/rte_malloc.c +++ b/lib/librte_eal/common/rte_malloc.c @@ -70,7 +70,11 @@ rte_malloc(const char *type, size_t size, unsigned align) void * rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket) { - return rte_malloc_socket(type, size, align, socket); + void *ptr = rte_malloc_socket(type, size, align, socket); + + if (ptr != NULL) + memset(ptr, 0, size); + return ptr; } /* -- 2.18.0