From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-f193.google.com (mail-pl1-f193.google.com [209.85.214.193]) by dpdk.org (Postfix) with ESMTP id 166081B4CF for ; Sat, 16 Feb 2019 02:50:21 +0100 (CET) Received: by mail-pl1-f193.google.com with SMTP id 101so5824502pld.6 for ; Fri, 15 Feb 2019 17:50:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=j2BldGwwj/pi13xXOwSUgIXaYVUq0OHxmega3IX8bAk=; b=WeRnsaLoglAlix3yqFxTg7dXb4UfnGUA0jisZF7lNJy5XeV3y1mrpaDip2c0YMNv3a c03slREITmkx8FFhdQBwEzlVN3CGLRMs60XnNfySfZL/JTob6vb4qnLa5DFeMoZhvcE0 wkJESfo+L3wGaTkHG8fDHg79rUxoG8KWhTyAJJodolJPaEPsvGXLvQFa/htKL1me7qpw qYODw1R981sViDT0+zmxQEPNusWcG+ePJrXDKEisq3TWvOqBsje+T3sUX+Ri8UQMspe3 Z19iXHzNZhudGQgKCziG7cUE2FpodxjR4cJ5Tu1JxE/ay0HEyXtjyHUUJhhs1vQ1m/Sd ew2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=j2BldGwwj/pi13xXOwSUgIXaYVUq0OHxmega3IX8bAk=; b=c9EPRe0LMFNU/QnFMQ3OmAuSb+pNUxKN8ZLXmS6RiiaSiUXJN8xeG0bM9I3zwWz2Ri yxXUPjANU7PughG5n5bShgVeAylWaKGOHZ6nMvoLbkpWZdfqA3PIfN1Cf7fpHRjd/WpL RN88Hz+TRthopaGRqiBR02Gp+JCnjP3TFHYm3bFKosN4pEDgXtAnL44AhnGEi4raozjb d45KbqsjPi8qlHUT7aRttQQ/9hWwZkG1yuvAJI1s3li+rp3PM149GjKYhMBdlxFJnF9Q DMg2mS1kbzVZNYbOLYPnNos79vKzDTf6jCKKwxWRiB0CohULB0iQReznsqxgMsIRApHW 6WDQ== X-Gm-Message-State: AHQUAuYUTscJg5bk4uo4SNzlqhMGlaAB96qJRES4D9CU++KF7oox8K98 lfQ7Md+vGzGy3y1DbC6oo8NcGSi50xQ= X-Google-Smtp-Source: AHgI3Ib9C+kLBtjL2VGQm6RU7vHE66BNWvGJ5cc3un7HTDAAOBAGSG2QkHiDCDD7/K9txB5cAZtK2Q== X-Received: by 2002:a17:902:bd43:: with SMTP id b3mr381162plx.186.1550281819694; Fri, 15 Feb 2019 17:50:19 -0800 (PST) Received: from shemminger-XPS-13-9360.lan (204-195-22-127.wavecable.com. [204.195.22.127]) by smtp.gmail.com with ESMTPSA id q5sm9726111pga.71.2019.02.15.17.50.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 15 Feb 2019 17:50:18 -0800 (PST) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Date: Fri, 15 Feb 2019 17:50:16 -0800 Message-Id: <20190216015016.10426-1-stephen@networkplumber.org> X-Mailer: git-send-email 2.17.1 Subject: [dpdk-dev] [PATCH v2] mem: poison memory when freed X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 16 Feb 2019 01:50:21 -0000 DPDK malloc library allows broken programs to work because the semantics of zmalloc and malloc are the same. This patch enables a more secure model which will catch (and crash) programs that reuse memory already freed if RTE_MALLOC_DEBUG is enabled. Signed-off-by: Stephen Hemminger Acked-by: Andrew Rybchenko Reviewed-by: Anatoly Burakov --- v2 -- fix #ifdef to get correct semantics, and add more comments lib/librte_eal/common/malloc_elem.c | 18 +++++++++++++++--- lib/librte_eal/common/rte_malloc.c | 13 ++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/librte_eal/common/malloc_elem.c b/lib/librte_eal/common/malloc_elem.c index d54a528ed653..658c9b5b7993 100644 --- a/lib/librte_eal/common/malloc_elem.c +++ b/lib/librte_eal/common/malloc_elem.c @@ -23,6 +23,17 @@ #include "malloc_elem.h" #include "malloc_heap.h" +/* + * If debugging is enabled, freed memory is set to poison value + * to catch buggy programs. Otherwise, freed memory is set to zero + * to avoid having to zero in zmalloc + */ +#ifdef RTE_MALLOC_DEBUG +#define MALLOC_POISON 0x6b +#else +#define MALLOC_POISON 0 +#endif + size_t malloc_elem_find_max_iova_contig(struct malloc_elem *elem, size_t align) { @@ -494,7 +505,7 @@ malloc_elem_join_adjacent_free(struct malloc_elem *elem) join_elem(elem, elem->next); /* erase header, trailer and pad */ - memset(erase, 0, erase_len); + memset(erase, MALLOC_POISON, erase_len); } /* @@ -518,7 +529,7 @@ malloc_elem_join_adjacent_free(struct malloc_elem *elem) join_elem(new_elem, elem); /* erase header, trailer and pad */ - memset(erase, 0, erase_len); + memset(erase, MALLOC_POISON, erase_len); elem = new_elem; } @@ -549,7 +560,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 b39de3c99e58..9db8ded73bfc 100644 --- a/lib/librte_eal/common/rte_malloc.c +++ b/lib/librte_eal/common/rte_malloc.c @@ -74,7 +74,18 @@ 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); + +#ifdef RTE_MALLOC_DEBUG + /* + * If DEBUG is enabled, then freed memory is marked with poison + * value and set to zero on allocation. + * If DEBUG is not enabled then memory is already zeroed. + */ + if (ptr != NULL) + memset(ptr, 0, size); +#endif + return ptr; } /* -- 2.17.1