Fix allocation issues and add hardening
Subsystems (4)
cover Fix allocation issues and add hardening
Recent versions of GCC have some additional function attributes that can help with DPDK performance and stability.
The alloc_align attribute can tell the compiler what the alignment of the allocation will be, and the optimizer can use this to produce better code (especially memcpy and structure copies).
The malloc attribute tells compiler that object is not overlapping and potentially aliasing. It also as an additional variant in GCC 11 or later that allows for detecting all sorts of common errors like calling free() on memory allocated with rte_malloc().
This patch set is structured with:
- add macros for enable the macros
- fix any new warnings that were discovered
- enable the attributes
The same attributes could be added to lots more functions in DPDK, but this patch set focuses on the key ones, and where problems exist in current code base.
The fixes should be backported to stable (they are real bugs), but the macros common and the annotation in malloc should not.
Stephen Hemminger (16):
eal: add function attributes for allocation functions
memzone: fix use after free in tracing cryptodev/bcmfs: fix mis-matched free dma/ixd: fix incorrect free function in cleanup event/cnxk: fix pointer mismatch in cleanup examples/vhost: fix free function mismatch net/cnxk: fix use-after-free
bpf: fix free mismatch if convert fails net/e1000: fix use-after-free
net/sfc: fix use-after-free warning messages net/cpfl: fix free of nonheap object raw/ifpga/base: fix use after free common/qat: fix use after free drivers/ifpga: fix free function mismatch
eal: add alloc_function attribute to rte_malloc
mempool: annotate mempool create
drivers/common/qat/qat_device.c | 6 +-- drivers/crypto/bcmfs/bcmfs_device.c | 4 +- drivers/dma/idxd/idxd_pci.c | 2 +- drivers/event/cnxk/cnxk_eventdev.c | 4 +- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- drivers/net/cpfl/cpfl_flow_parser.c | 1 - drivers/net/e1000/igb_ethdev.c | 4 +- drivers/net/sfc/sfc_flow_rss.c | 4 +- drivers/net/sfc/sfc_mae.c | 23 ++++----- drivers/raw/ifpga/base/opae_intel_max10.c | 11 +++- drivers/raw/ifpga/ifpga_rawdev.c | 8 +-- examples/vhost_blk/vhost_blk.c | 2 +- lib/bpf/bpf_convert.c | 2 +- lib/eal/common/eal_common_memzone.c | 3 +- lib/eal/include/rte_common.h | 30 +++++++++++ lib/eal/include/rte_malloc.h | 63 ++++++++++++++--------- lib/mempool/rte_mempool.h | 41 ++++++++------- 17 files changed, 130 insertions(+), 80 deletions(-)
Signature · 1 line
2.45.2
PATCH 1/16 eal: add function attributes for allocation functions
The allocation functions take a alignment argument that can be useful to hint the compiler optimizer.
This is supported by Gcc and Clang but only useful with Gcc because Clang gives warning if alignment is 0.
Recent versions of GCC have a malloc attribute that can be used to find mismatches between allocation and free; the typical problem caught is a pointer allocated with rte_malloc() that is then incorrectly freed using free().
--- lib/eal/include/rte_common.h | 30 ++++++++++++++++++++++++++++++ lib/eal/include/rte_malloc.h | 24 ++++++++++++++++-------- 2 files changed, 46 insertions(+), 8 deletions(-)
lib/eal/include/rte_common.h +30 -0
@@ -228,6 +228,36 @@ typedef uint16_t unaligned_uint16_t;#define __rte_alloc_size (...)#endif +/** +* Tells the compiler that the function returns a value that points to +* memory aligned by a function argument. +* Not enabled on clang because it warns if align argument is zero. +*/ +#if defined(RTE_CC_GCC ) +#define __rte_alloc_align (align_arg) \ + __attribute__((alloc_align(align_arg))) +#else +#define __rte_alloc_align (...) +#endif + +/** +* Tells the compiler this is a function like malloc and that the pointer +* returned cannot alias any other pointer (ie new memory). +* +* Also, with recent GCC versions also able to track that proper +* dealloctor function is used for this pointer. +*/ +#if defined(RTE_TOOLCHAIN_GCC ) && (GCC_VERSION >=110000 ) +#define __rte_alloc_func (free_func) \ + __attribute__((malloc, malloc(free_func))) + +#elif defined(RTE_CC_GCC ) || defined(RTE_CC_CLANG ) +#define __rte_alloc_func (free_func) \ + __attribute__((malloc)) +#else +#define __rte_alloc_func (free_func) +#endif +#define RTE_PRIORITY_LOG 101#define RTE_PRIORITY_BUS 110#define RTE_PRIORITY_CLASS 120
lib/eal/include/rte_malloc.h +16 -8
@@ -54,7 +54,8 @@ struct rte_malloc_socket_stats {*/ void *rte_malloc (const char *type,size_t size,unsigned align) -__rte_alloc_size (2 ); +__rte_alloc_size (2 ) +__rte_alloc_align (3 );/** * Allocate zeroed memory from the heap.
@@ -81,7 +82,8 @@ rte_malloc(const char *type, size_t size, unsigned align)*/ void *rte_zmalloc (const char *type,size_t size,unsigned align) -__rte_alloc_size (2 ); +__rte_alloc_size (2 ) +__rte_alloc_align (3 );/** * Replacement function for calloc(), using huge-page memory. Memory area is
@@ -108,7 +110,8 @@ rte_zmalloc(const char *type, size_t size, unsigned align)*/ void *rte_calloc (const char *type,size_t num,size_t size,unsigned align) -__rte_alloc_size (2 ,3 ); +__rte_alloc_size (2 ,3 ) +__rte_alloc_align (4 );/** * Replacement function for realloc(), using huge-page memory. Reserved area
@@ -132,7 +135,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)*/ void *rte_realloc (void *ptr,size_t size,unsigned int align) -__rte_alloc_size (2 ); +__rte_alloc_size (2 ) +__rte_alloc_align (3 );/** * Replacement function for realloc(), using huge-page memory. Reserved area
@@ -158,7 +162,8 @@ rte_realloc(void *ptr, size_t size, unsigned int align)*/ void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) -__rte_alloc_size (2 ); +__rte_alloc_size (2 ) +__rte_alloc_align (3 );/** * This function allocates memory from the huge-page area of memory. The memory
@@ -185,7 +190,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)*/ void *rte_malloc_socket (const char *type,size_t size,unsigned align,int socket) -__rte_alloc_size (2 ); +__rte_alloc_size (2 ) +__rte_alloc_align (3 );/** * Allocate zeroed memory from the heap.
@@ -214,7 +220,8 @@ rte_malloc_socket(const char *type, size_t size, unsigned align, int socket)*/ void *rte_zmalloc_socket (const char *type,size_t size,unsigned align,int socket) -__rte_alloc_size (2 ); +__rte_alloc_size (2 ) +__rte_alloc_align (3 );/** * Replacement function for calloc(), using huge-page memory. Memory area is
@@ -243,7 +250,8 @@ rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)*/ void *rte_calloc_socket (const char *type,size_t num,size_t size,unsigned align,int socket) -__rte_alloc_size (2 ,3 ); +__rte_alloc_size (2 ,3 ) +__rte_alloc_align (4 );/** * Frees the memory space pointed to by the provided pointer.
Signature · 1 line
2.45.2
Re: [PATCH 01/16] eal: add function attributes for allocation functions
Quoted 11 lines · Stephen Hemminger
The allocation functions take a alignment argument that can be useful to hint the compiler optimizer.
This is supported by Gcc and Clang but only useful with Gcc because Clang gives warning if alignment is 0.
Recent versions of GCC have a malloc attribute that can be used to find mismatches between allocation and free; the typical problem caught is a pointer allocated with
rte_malloc()that is then incorrectly freed usingfree().
Interesting tool.
Quoted 25 lines
Signed-off-by Stephen Hemminger <[email protected]>--- lib/eal/include/rte_common.h | 30 ++++++++++++++++++++++++++++++ lib/eal/include/rte_malloc.h | 24 ++++++++++++++++-------- 2 files changed, 46 insertions(+), 8 deletions(-)lib/eal/include/rte_common.h +10
-0@@ -228,6 +228,36 @@ typedef uint16_t unaligned_uint16_t;#define __rte_alloc_size (...)#endif +/** +* Tells the compiler that the function returns a value that points to +* memory aligned by a function argument. +* Not enabled on clang because it warns if align argument is zero. +*/ +#if defined(RTE_CC_GCC ) +#define __rte_alloc_align (align_arg) \ + __attribute__((alloc_align(align_arg))) +#else +#define __rte_alloc_align (...)
lib/eal/include/rte_common.h typedef uint16_t unaligned_uint16_t; · line 250
Quoted 10 lines
lib/eal/include/rte_common.h +10
-0+#endif + +/** +* Tells the compiler this is a function like malloc and that the pointer +* returned cannot alias any other pointer (ie new memory). +* +* Also, with recent GCC versions also able to track that proper +* dealloctor function is used for this pointer. +*/ +#if defined(RTE_TOOLCHAIN_GCC ) && (GCC_VERSION >=110000 )
Even though it is probably equivalent, GCC_VERSION is set with RTE_CC_GCC.
lib/eal/include/rte_common.h typedef uint16_t unaligned_uint16_t; · line 252
Quoted 2 lines
lib/eal/include/rte_common.h +2
-0+#define __rte_alloc_func (free_func) \ + __attribute__((malloc, malloc(free_func)))
I read that this malloc attribute can also make use of the arg index to assume the pointer is freed.
Did you try this feature?
Something like:
@@ -248,14 +248,13 @@ typedef uint16_t unaligned_uint16_t;
* dealloctor function is used for this pointer. */
#if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 110000)
-#define __rte_alloc_func(free_func) \
- __attribute__((malloc, malloc(free_func)))
-
+#define __rte_alloc_func(...) \
+ __attribute__((malloc, malloc(__VA_ARGS__)))
#elif defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
-#define __rte_alloc_func(free_func) \
+#define __rte_alloc_func(...) \
__attribute__((malloc)) #else
-#define __rte_alloc_func(free_func)
+#define __rte_alloc_func(...)
#endif
#define RTE_PRIORITY_LOG 101
Quoted 11 lines
lib/eal/include/rte_common.h +8
-0+ +#elif defined(RTE_CC_GCC ) || defined(RTE_CC_CLANG ) +#define __rte_alloc_func (free_func) \ + __attribute__((malloc)) +#else +#define __rte_alloc_func (free_func) +#endif +#define RTE_PRIORITY_LOG 101#define RTE_PRIORITY_BUS 110#define RTE_PRIORITY_CLASS 120
Signature · 1 line
David Marchand
Re: [PATCH 01/16] eal: add function attributes for allocation functions
Quoted 54 lines · On Fri, 27 Sep 2024 18:09:22 -0400 David Marchand <[email protected]>…
Quoted 11 lines · Stephen Hemminger
The allocation functions take a alignment argument that can be useful to hint the compiler optimizer.
This is supported by Gcc and Clang but only useful with Gcc because Clang gives warning if alignment is 0.
Recent versions of GCC have a malloc attribute that can be used to find mismatches between allocation and free; the typical problem caught is a pointer allocated with
rte_malloc()that is then incorrectly freed usingfree().Interesting tool.
Quoted 25 lines
Signed-off-by Stephen Hemminger <[email protected]>--- lib/eal/include/rte_common.h | 30 ++++++++++++++++++++++++++++++ lib/eal/include/rte_malloc.h | 24 ++++++++++++++++-------- 2 files changed, 46 insertions(+), 8 deletions(-)lib/eal/include/rte_common.h +10
-0@@ -228,6 +228,36 @@ typedef uint16_t unaligned_uint16_t;#define __rte_alloc_size (...)#endif +/** +* Tells the compiler that the function returns a value that points to +* memory aligned by a function argument. +* Not enabled on clang because it warns if align argument is zero. +*/ +#if defined(RTE_CC_GCC ) +#define __rte_alloc_align (align_arg) \ + __attribute__((alloc_align(align_arg))) +#else +#define __rte_alloc_align (...)lib/eal/include/rte_common.h typedef uint16_t unaligned_uint16_t; · line 250
Quoted 10 lines
lib/eal/include/rte_common.h +10
-0+#endif + +/** +* Tells the compiler this is a function like malloc and that the pointer +* returned cannot alias any other pointer (ie new memory). +* +* Also, with recent GCC versions also able to track that proper +* dealloctor function is used for this pointer. +*/ +#if defined(RTE_TOOLCHAIN_GCC ) && (GCC_VERSION >=110000 )Even though it is probably equivalent,
GCC_VERSIONis set withRTE_CC_GCC.
Quoted 7 lines
lib/eal/include/rte_common.h typedef uint16_t unaligned_uint16_t; · line 252
Quoted 2 lines
lib/eal/include/rte_common.h +2
-0+#define __rte_alloc_func (free_func) \ + __attribute__((malloc, malloc(free_func)))I read that this malloc attribute can also make use of the arg index to assume the pointer is freed.
Did you try this feature?
Yes, but all DPDK functions use first arg, so not really that relevant
PATCH 2/16 memzone: fix use after free in tracing
In reply to Stephen HemmingerUsing the freed value for tracing is not a good idea. Although it is harmless for tracing, it will cause analyzers to flag this as unsafe.
--- lib/eal/common/eal_common_memzone.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/eal/common/eal_common_memzone.c +2 -1
@@ -331,9 +331,10 @@ rte_memzone_free(const struct rte_memzone *mz)rte_rwlock_write_unlock (&mcfg->mlock); +rte_eal_trace_memzone_free (name, addr, ret); +rte_free (addr); -rte_eal_trace_memzone_free (name, addr, ret);return ret; }
Signature · 1 line
2.45.2
PATCH 3/16 cryptodev/bcmfs: fix mis-matched free
In reply to Stephen HemmingerThe device structure is allocated with rte_malloc() and then incorrectly freed with free(). This will lead to corrupt malloc pool.
Bugzilla ID: 1552
--- drivers/crypto/bcmfs/bcmfs_device.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/crypto/bcmfs/bcmfs_device.c +2 -2
@@ -139,7 +139,7 @@ fsdev_allocate_one_dev(struct rte_vdev_device *vdev,return fsdev; cleanup: -free (fsdev); +rte_free (fsdev);return NULL ; }
@@ -163,7 +163,7 @@ fsdev_release(struct bcmfs_device *fsdev)return ;TAILQ_REMOVE (&fsdev_list, fsdev, next); -free (fsdev); +rte_free (fsdev); } static int
Signature · 1 line
2.45.2
PATCH 4/16 dma/ixd: fix incorrect free function in cleanup
In reply to Stephen HemmingerThe data structure is allocated with rte_malloc and incorrectly freed in cleanup logic using free.
Bugzilla ID: 1549
--- drivers/dma/idxd/idxd_pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma/idxd/idxd_pci.c +1 -1
@@ -301,7 +301,7 @@ init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,return nb_wqs; err: -free (pci); +rte_free (pci);return err_code; }
Signature · 1 line
2.45.2
PATCH 5/16 event/cnxk: fix pointer mismatch in cleanup
In reply to Stephen HemmingerThe code to cleanup in case of error was passing incorrect value to rte_free. The ports[] entry was allocated with rte_malloc and that should be used instead of the offset in that object.
--- drivers/event/cnxk/cnxk_eventdev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/event/cnxk/cnxk_eventdev.c +3 -1
@@ -121,8 +121,10 @@ cnxk_setup_event_ports(const struct rte_eventdev *event_dev,return 0 ; hws_fini:for (i = i -1 ; i >=0 ; i--) { +void *ws = event_dev->data->ports[i]; + event_dev->data->ports[i] =NULL ; -rte_free (cnxk_sso_hws_get_cookie (event_dev->data->ports[i])); +rte_free (ws); }return -ENOMEM ; }
Signature · 1 line
2.45.2
PATCH 6/16 examples/vhost: fix free function mismatch
In reply to Stephen HemmingerThe pointer bdev is allocated with rte_zmalloc() and then incorrectly freed with free() which will lead pool corruption.
Bugzilla ID: 1553
--- examples/vhost_blk/vhost_blk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
examples/vhost_blk/vhost_blk.c +1 -1
@@ -776,7 +776,7 @@ vhost_blk_bdev_construct(const char *bdev_name, bdev->data =rte_zmalloc (NULL , blk_cnt * blk_size,0 );if (!bdev->data) {fprintf (stderr,"No enough reserved huge memory for disk\n" ); -free (bdev); +rte_free (bdev);return NULL ; }
Signature · 1 line
2.45.2
PATCH 7/16 net/cnxk: fix use-after-free
In reply to Stephen HemmingerThe driver would refer to the mempool object after it was freed.
Bugzilla ID: 1554
--- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/cnxk/cnxk_ethdev_sec.c +1 -1
@@ -136,8 +136,8 @@ cnxk_nix_inl_custom_meta_pool_cb(uintptr_t pmpool, uintptr_t *mpool, const charreturn -EINVAL ; } -rte_mempool_free (hp);plt_free (hp->pool_config); +rte_mempool_free (hp); *aura_handle =0 ; *mpool =0 ;
Signature · 1 line
2.45.2
PATCH 8/16 bpf: fix free mismatch if convert fails
In reply to Stephen HemmingerIf conversion of cBF to eBPF fails then an object allocated with rte_malloc() would be passed to free().
[908/3201] Compiling C object lib/librte_bpf.a.p/bpf_bpf_convert.c.o../lib/bpf/bpf_convert.c: In function ‘rte_bpf_convert’:../lib/bpf/bpf_convert.c:559:17: warning: ‘free’ called on pointer returned from a mismatched allocation function [-Wmismatched-dealloc]
559 | free(prm);
| ^~~~~~~~~
../lib/bpf/bpf_convert.c:545:15: note: returned from ‘rte_zmalloc’
545 | prm = rte_zmalloc("bpf_filter",
| ^~~~~~~~~~~~~~~~~~~~~~~~~
546 | sizeof(*prm) + ebpf_len * sizeof(*ebpf), 0);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~--- lib/bpf/bpf_convert.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/bpf/bpf_convert.c +1 -1
@@ -556,7 +556,7 @@ rte_bpf_convert(const struct bpf_program *prog) ret =bpf_convert_filter (prog->bf_insns, prog->bf_len, ebpf, &ebpf_len);if (ret <0 ) {RTE_BPF_LOG_LINE (ERR ,"%s: cannot convert cBPF to eBPF" , __func__); -free (prm); +rte_free (prm); rte_errno = -ret;return NULL ; }
Signature · 1 line
2.45.2
PATCH 9/16 net/e1000: fix use-after-free
In reply to Stephen HemmingerThe driver cleanup code was freeing the filter object then dereferencing it.
Bugzilla ID: 1550
--- drivers/net/e1000/igb_ethdev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/e1000/igb_ethdev.c +2 -2
@@ -3907,11 +3907,11 @@ igb_delete_2tuple_filter(struct rte_eth_dev *dev, filter_info->twotuple_mask &= ~(1 << filter->index);TAILQ_REMOVE (&filter_info->twotuple_list, filter, entries); -rte_free (filter);E1000_WRITE_REG (hw,E1000_TTQF (filter->index),E1000_TTQF_DISABLE_MASK );E1000_WRITE_REG (hw,E1000_IMIR (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIREXT (filter->index),0 ); +rte_free (filter);return 0 ; }
@@ -4348,7 +4348,6 @@ igb_delete_5tuple_filter_82576(struct rte_eth_dev *dev, filter_info->fivetuple_mask &= ~(1 << filter->index);TAILQ_REMOVE (&filter_info->fivetuple_list, filter, entries); -rte_free (filter);E1000_WRITE_REG (hw,E1000_FTQF (filter->index),E1000_FTQF_VF_BP |E1000_FTQF_MASK );
@@ -4357,6 +4356,7 @@ igb_delete_5tuple_filter_82576(struct rte_eth_dev *dev,E1000_WRITE_REG (hw,E1000_SPQF (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIR (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIREXT (filter->index),0 ); +rte_free (filter);return 0 ; }
Signature · 1 line
2.45.2
PATCH 10/16 net/sfc: fix use-after-free warning messages
In reply to Stephen HemmingerIf compiler detection of use-after-free is enabled then this drivers debug messages will cause warnings. Change to move debug message before the object is freed.
Bugzilla ID: 1551
--- drivers/net/sfc/sfc_flow_rss.c | 4 ++-- drivers/net/sfc/sfc_mae.c | 23 +++++++++-------------- 2 files changed, 11 insertions(+), 16 deletions(-)
drivers/net/sfc/sfc_flow_rss.c +2 -2
@@ -303,9 +303,9 @@ sfc_flow_rss_ctx_del(struct sfc_adapter *sa, struct sfc_flow_rss_ctx *ctx)TAILQ_REMOVE (&flow_rss->ctx_list, ctx, entries);rte_free (ctx->qid_offsets); -rte_free (ctx); -sfc_dbg (sa,"flow-rss: deleted ctx=%p" , ctx); + +rte_free (ctx); } static int
drivers/net/sfc/sfc_mae.c +9 -14
@@ -400,9 +400,8 @@ sfc_mae_outer_rule_del(struct sfc_adapter *sa,efx_mae_match_spec_fini (sa->nic, rule->match_spec);TAILQ_REMOVE (&mae->outer_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted outer_rule=%p" , rule); +rte_free (rule); } static int
@@ -585,9 +584,8 @@ sfc_mae_mac_addr_del(struct sfc_adapter *sa, struct sfc_mae_mac_addr *mac_addr) }TAILQ_REMOVE (&mae->mac_addrs, mac_addr, entries); -rte_free (mac_addr); -sfc_dbg (sa,"deleted mac_addr=%p" , mac_addr); +rte_free (mac_addr); }enum sfc_mae_mac_addr_type {
@@ -785,10 +783,10 @@ sfc_mae_encap_header_del(struct sfc_adapter *sa, }TAILQ_REMOVE (&mae->encap_headers, encap_header, entries); +sfc_dbg (sa,"deleted encap_header=%p" , encap_header); +rte_free (encap_header->buf);rte_free (encap_header); - -sfc_dbg (sa,"deleted encap_header=%p" , encap_header); } static int
@@ -983,9 +981,8 @@ sfc_mae_counter_del(struct sfc_adapter *sa, struct sfc_mae_counter *counter) }TAILQ_REMOVE (&mae->counters, counter, entries); -rte_free (counter); -sfc_dbg (sa,"deleted counter=%p" , counter); +rte_free (counter); } static int
@@ -1165,9 +1162,8 @@ sfc_mae_action_set_del(struct sfc_adapter *sa,sfc_mae_mac_addr_del (sa, action_set->src_mac_addr);sfc_mae_counter_del (sa, action_set->counter);TAILQ_REMOVE (&mae->action_sets, action_set, entries); -rte_free (action_set); -sfc_dbg (sa,"deleted action_set=%p" , action_set); +rte_free (action_set); } static int
@@ -1401,10 +1397,10 @@ sfc_mae_action_set_list_del(struct sfc_adapter *sa,sfc_mae_action_set_del (sa, action_set_list->action_sets[i]);TAILQ_REMOVE (&mae->action_set_lists, action_set_list, entries); +sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); +rte_free (action_set_list->action_sets);rte_free (action_set_list); - -sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); } static int
@@ -1667,9 +1663,8 @@ sfc_mae_action_rule_del(struct sfc_adapter *sa,sfc_mae_outer_rule_del (sa, rule->outer_rule);TAILQ_REMOVE (&mae->action_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted action_rule=%p" , rule); +rte_free (rule); } static int
Signature · 1 line
2.45.2
Re: [PATCH 10/16] net/sfc: fix use-after-free warning messages
Thank you.
Quoted 118 lines · Stephen Hemminger
If compiler detection of use-after-free is enabled then this drivers debug messages will cause warnings. Change to move debug message before the object is freed.
Bugzilla ID: 1551
Fixes 55c1238246d5 ("net/sfc: add more debug messages to transfer flows")Signed-off-by Stephen Hemminger <[email protected]>---
drivers/net/sfc/sfc_flow_rss.c| 4 ++--drivers/net/sfc/sfc_mae.c| 23 +++++++++--------------2 files changed, 11 insertions(+), 16 deletions(-)drivers/net/sfc/sfc_flow_rss.c +2
-2@@ -303,9 +303,9 @@ sfc_flow_rss_ctx_del(struct sfc_adapter *sa, struct sfc_flow_rss_ctx *ctx)TAILQ_REMOVE (&flow_rss->ctx_list, ctx, entries);rte_free (ctx->qid_offsets); -rte_free (ctx); -sfc_dbg (sa,"flow-rss: deleted ctx=%p" , ctx); + +rte_free (ctx); }static int
drivers/net/sfc/sfc_mae.c +1
-2@@ -400,9 +400,8 @@ sfc_mae_outer_rule_del(struct sfc_adapter *sa,efx_mae_match_spec_fini (sa->nic, rule->match_spec);TAILQ_REMOVE (&mae->outer_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted outer_rule=%p" , rule); +rte_free (rule); }static int
drivers/net/sfc/sfc_mae.c +3
-4@@ -585,9 +584,8 @@ sfc_mae_mac_addr_del(struct sfc_adapter *sa, struct sfc_mae_mac_addr *mac_addr) }TAILQ_REMOVE (&mae->mac_addrs, mac_addr, entries); -rte_free (mac_addr); -sfc_dbg (sa,"deleted mac_addr=%p" , mac_addr); +rte_free (mac_addr); }enum sfc_mae_mac_addr_type {@@ -785,10 +783,10 @@ sfc_mae_encap_header_del(struct sfc_adapter *sa, }TAILQ_REMOVE (&mae->encap_headers, encap_header, entries); +sfc_dbg (sa,"deleted encap_header=%p" , encap_header); +rte_free (encap_header->buf);rte_free (encap_header); - -sfc_dbg (sa,"deleted encap_header=%p" , encap_header); }static int
drivers/net/sfc/sfc_mae.c +1
-2@@ -983,9 +981,8 @@ sfc_mae_counter_del(struct sfc_adapter *sa, struct sfc_mae_counter *counter) }TAILQ_REMOVE (&mae->counters, counter, entries); -rte_free (counter); -sfc_dbg (sa,"deleted counter=%p" , counter); +rte_free (counter); }static int
drivers/net/sfc/sfc_mae.c +1
-2@@ -1165,9 +1162,8 @@ sfc_mae_action_set_del(struct sfc_adapter *sa,sfc_mae_mac_addr_del (sa, action_set->src_mac_addr);sfc_mae_counter_del (sa, action_set->counter);TAILQ_REMOVE (&mae->action_sets, action_set, entries); -rte_free (action_set); -sfc_dbg (sa,"deleted action_set=%p" , action_set); +rte_free (action_set); }static int
drivers/net/sfc/sfc_mae.c +2
-2@@ -1401,10 +1397,10 @@ sfc_mae_action_set_list_del(struct sfc_adapter *sa,sfc_mae_action_set_del (sa, action_set_list->action_sets[i]);TAILQ_REMOVE (&mae->action_set_lists, action_set_list, entries); +sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); +rte_free (action_set_list->action_sets);rte_free (action_set_list); - -sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); }static int
drivers/net/sfc/sfc_mae.c +1
-2@@ -1667,9 +1663,8 @@ sfc_mae_action_rule_del(struct sfc_adapter *sa,sfc_mae_outer_rule_del (sa, rule->outer_rule);TAILQ_REMOVE (&mae->action_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted action_rule=%p" , rule); +rte_free (rule); }static int
Signature · 1 line
2.45.2
PATCH 11/16 net/cpfl: fix free of nonheap object
In reply to Stephen HemmingerWith proper annotation, GCC discovers that this driver is calling rte_free() on an object that was not allocated (it is part of array in another object).
In function ‘cpfl_flow_js_mr_layout’,
inlined from ‘cpfl_flow_js_mr_action’ at ../drivers/net/cpfl/cpfl_flow_parser.c:848:9,
inlined from ‘cpfl_flow_js_mod_rule’ at ../drivers/net/cpfl/cpfl_flow_parser.c:908:9,
inlined from ‘cpfl_parser_init’ at ../drivers/net/cpfl/cpfl_flow_parser.c:932:8,
inlined from ‘cpfl_parser_create’ at ../drivers/net/cpfl/cpfl_flow_parser.c:959:8:../drivers/net/cpfl/cpfl_flow_parser.c:740:9: warning: ‘rte_free’ called on pointer ‘*parser.modifications’ with nonzero offset [28, 15479062120396] [-Wfree-nonheap-object]
740 | rte_free(js_mod->layout);
| ^~~~~~~~~~~~~~~~~~~~~~~~--- drivers/net/cpfl/cpfl_flow_parser.c | 1 - 1 file changed, 1 deletion(-)
drivers/net/cpfl/cpfl_flow_parser.c +0 -1
@@ -737,7 +737,6 @@ cpfl_flow_js_mr_layout(json_t *ob_layouts, struct cpfl_flow_js_mr_action_mod *jsreturn 0 ; err: -rte_free (js_mod->layout);return -EINVAL ; }
Signature · 1 line
2.45.2
PATCH 12/16 raw/ifpga/base: fix use after free
In reply to Stephen HemmingerThe TAILQ_FOREACH() macro would refer to info after it had been freed. Fix by introducing TAILQ_FOREACH_SAFE here.
--- drivers/raw/ifpga/base/opae_intel_max10.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/raw/ifpga/base/opae_intel_max10.c +9 -2
@@ -6,6 +6,13 @@#include <libfdt.h> #include "opae_osdep.h" +#ifndef TAILQ_FOREACH_SAFE +#define TAILQ_FOREACH_SAFE (var, head, field, tvar) \ + for ((var) = TAILQ_FIRST((head)); \ + (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) +#endif +int max10_sys_read (struct intel_max10_device *dev,unsigned int offset,unsigned int *val) {
@@ -746,9 +753,9 @@ static int fdt_get_named_reg(const void *fdt, int node, const char *name,static void max10_sensor_uinit (struct intel_max10_device *dev) { -struct opae_sensor_info *info; +struct opae_sensor_info *info, *next; - TAILQ_FOREACH(info, &dev->opae_sensor_list, node) { + TAILQ_FOREACH_SAFE(info, &dev->opae_sensor_list, node, next) {TAILQ_REMOVE (&dev->opae_sensor_list, info, node);opae_free (info); }
Signature · 1 line
2.45.2
PATCH 13/16 common/qat: fix use after free
In reply to Stephen HemmingerChecking return value of rte_memzone_free() is pointless and if it failed then it was because the pointer was null.
--- drivers/common/qat/qat_device.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/common/qat/qat_device.c +1 -5
@@ -390,11 +390,7 @@ qat_pci_device_allocate(struct rte_pci_device *pci_dev)return qat_dev; error:rte_free (qat_dev->command_line); -if (rte_memzone_free (qat_dev_mz)) { -QAT_LOG (DEBUG , -"QAT internal error! Trying to free already allocated memzone: %s" , - qat_dev_mz->name); - } +rte_memzone_free (qat_dev_mz);return NULL ; }
Signature · 1 line
2.45.2
PATCH 14/16 drivers/ifpga: fix free function mismatch
In reply to Stephen HemmingerThe raw ifpga driver redefines malloc to be opae_malloc and free to be opae_free; which is a bad idea.
This leads to case where interrupt efd array is allocated with calloc() and then passed to rte_free. The workaround is to allocate the array with rte_calloc() instead.
--- drivers/raw/ifpga/ifpga_rawdev.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/raw/ifpga/ifpga_rawdev.c +4 -4
@@ -1499,7 +1499,7 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, nb_intr =rte_intr_nb_intr_get (*intr_handle); - intr_efds =calloc (nb_intr,sizeof (int )); + intr_efds =rte_calloc ("ifpga_efds" , nb_intr,sizeof (int ),0 );if (!intr_efds)return -ENOMEM ;
@@ -1508,7 +1508,7 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, ret =opae_acc_set_irq (acc, vec_start, count, intr_efds);if (ret) { -free (intr_efds); +rte_free (intr_efds);return -EINVAL ; } }
@@ -1517,13 +1517,13 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, ret =rte_intr_callback_register (*intr_handle, handler, (void *)arg);if (ret) { -free (intr_efds); +rte_free (intr_efds);return -EINVAL ; }IFPGA_RAWDEV_PMD_INFO ("success register %s interrupt\n" , name); -free (intr_efds); +rte_free (intr_efds);return 0 ; }
Signature · 1 line
2.45.2
PATCH 15/16 eal: add alloc_function attribute to rte_malloc
In reply to Stephen HemmingerUse the GCC function attribute to detect cases where memory is allocated with rte_malloc and freed incorrectly with libc version of free (and vice versa). Also will detect some other pointer mismatches.
--- lib/eal/include/rte_malloc.h | 55 +++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 23 deletions(-)
lib/eal/include/rte_malloc.h +32 -23
@@ -31,6 +31,22 @@ struct rte_malloc_socket_stats {size_t heap_allocsz_bytes;/**< Total allocated bytes on heap */ }; + +/** +* Frees the memory space pointed to by the provided pointer. +* +* 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. +*/ +void +rte_free (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
@@ -55,7 +71,8 @@ struct rte_malloc_socket_stats {void *rte_malloc (const char *type,size_t size,unsigned align)__rte_alloc_size (2 ) -__rte_alloc_align (3 ); +__rte_alloc_align (3 ) +__rte_alloc_func (rte_free);/** * Allocate zeroed memory from the heap.
@@ -83,7 +100,8 @@ rte_malloc(const char *type, size_t size, unsigned align)void *rte_zmalloc (const char *type,size_t size,unsigned align)__rte_alloc_size (2 ) -__rte_alloc_align (3 ); +__rte_alloc_align (3 ) +__rte_alloc_func (rte_free);/** * Replacement function for calloc(), using huge-page memory. Memory area is
@@ -111,7 +129,8 @@ rte_zmalloc(const char *type, size_t size, unsigned align)void *rte_calloc (const char *type,size_t num,size_t size,unsigned align)__rte_alloc_size (2 ,3 ) -__rte_alloc_align (4 ); +__rte_alloc_align (4 ) +__rte_alloc_func (rte_free);/** * Replacement function for realloc(), using huge-page memory. Reserved area
@@ -136,7 +155,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc (void *ptr,size_t size,unsigned int align)__rte_alloc_size (2 ) -__rte_alloc_align (3 ); +__rte_alloc_align (3 ) +__rte_alloc_func (rte_free);/** * Replacement function for realloc(), using huge-page memory. Reserved area
@@ -163,7 +183,8 @@ rte_realloc(void *ptr, size_t size, unsigned int align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket)__rte_alloc_size (2 ) -__rte_alloc_align (3 ); +__rte_alloc_align (3 ) +__rte_alloc_func (rte_free);/** * This function allocates memory from the huge-page area of memory. The memory
@@ -191,7 +212,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)void *rte_malloc_socket (const char *type,size_t size,unsigned align,int socket)__rte_alloc_size (2 ) -__rte_alloc_align (3 ); +__rte_alloc_align (3 ) +__rte_alloc_func (rte_free);/** * Allocate zeroed memory from the heap.
@@ -221,7 +243,8 @@ rte_malloc_socket(const char *type, size_t size, unsigned align, int socket)void *rte_zmalloc_socket (const char *type,size_t size,unsigned align,int socket)__rte_alloc_size (2 ) -__rte_alloc_align (3 ); +__rte_alloc_align (3 ) +__rte_alloc_func (rte_free);/** * Replacement function for calloc(), using huge-page memory. Memory area is
@@ -251,22 +274,8 @@ rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)void *rte_calloc_socket (const char *type,size_t num,size_t size,unsigned align,int socket)__rte_alloc_size (2 ,3 ) -__rte_alloc_align (4 ); - -/** -* Frees the memory space pointed to by the provided pointer. -* -* 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. -*/ -void -rte_free (void *ptr); +__rte_alloc_align (4 ) +__rte_alloc_func (rte_free);/** * If malloc debug is enabled, check a memory block for header
Signature · 1 line
2.45.2
PATCH 16/16 mempool: annotate mempool create
In reply to Stephen HemmingerUse rte_alloc_function annotation to catch mismatch errors on memzone handling.
--- lib/mempool/rte_mempool.h | 41 +++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 19 deletions(-)
lib/mempool/rte_mempool.h +22 -19
@@ -1012,6 +1012,20 @@ typedef void (rte_mempool_mem_cb_t)(struct rte_mempool *mp,*/ typedef void (rte_mempool_ctor_t )(struct rte_mempool *,void *); +/** +* Free a mempool +* +* Unlink the mempool from global list, free the memory chunks, and all +* memory referenced by the mempool. The objects must not be used by +* other cores as they will be freed. +* +* @param mp +* A pointer to the mempool structure. +* If NULL then, the function does nothing. +*/ +void +rte_mempool_free (struct rte_mempool *mp); +/** * Create a new mempool named *name* in memory. *
@@ -1091,11 +1105,12 @@ typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);* - ENOMEM - no appropriate memory area found in which to create memzone */ struct rte_mempool * -rte_mempool_create (const char *name,unsigned n,unsigned elt_size, -unsigned cache_size,unsigned private_data_size, +rte_mempool_create (const char *name,unsigned int n,unsigned int elt_size, +unsigned int cache_size,unsigned int private_data_size,rte_mempool_ctor_t *mp_init,void *mp_init_arg,rte_mempool_obj_cb_t *obj_init,void *obj_init_arg, -int socket_id,unsigned flags); +int socket_id,unsigned int flags) +__rte_alloc_func (rte_mempool_free);/** * Create an empty mempool
@@ -1132,22 +1147,10 @@ rte_mempool_create(const char *name, unsigned n, unsigned elt_size,* with rte_errno set appropriately. See rte_mempool_create() for details. */ struct rte_mempool * -rte_mempool_create_empty (const char *name,unsigned n,unsigned elt_size, -unsigned cache_size,unsigned private_data_size, -int socket_id,unsigned flags); -/** -* Free a mempool -* -* Unlink the mempool from global list, free the memory chunks, and all -* memory referenced by the mempool. The objects must not be used by -* other cores as they will be freed. -* -* @param mp -* A pointer to the mempool structure. -* If NULL then, the function does nothing. -*/ -void -rte_mempool_free (struct rte_mempool *mp); +rte_mempool_create_empty (const char *name,unsigned int n,unsigned int elt_size, +unsigned int cache_size,unsigned int private_data_size, +int socket_id,unsigned int flags) +__rte_alloc_func (rte_mempool_free);/** * Add physically contiguous memory for objects in the pool at init
Signature · 1 line
2.45.2
RE: [PATCH 16/16] mempool: annotate mempool create
Quoted 8 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Friday, 27 September 2024 22.46Use
rte_alloc_functionannotation to catch mismatch errors on memzone handling.Signed-off-by Stephen Hemminger <[email protected]>---
Note for other reviewers:rte_mempool_free() was intentionally moved up,
so __rte_alloc_func attribute can refer to it.
v2 cover Fix allocation bugs and add malloc hardening
In reply to Stephen HemmingerRecent versions of Gcc have some additonal function attributes tha can help with DPDK performance and stability.
The alloc_align attribute can tell the compiler what the alignment of the allocation will be, and the optimizer can use this to produce better code (especially memcpy and structure copies).
The malloc attribute tells compiler that object is not overlapping and potentially aliasing. It also as an additional variant in Gcc 11 or later that allows for detecting all sorts of common errors like calling free() on memory allocated with rte_malloc().
This patch set is structured with:
- add macros for enable the macros
- fix any new warnings that were discovered
- enable the attributes
In order to use the malloc attribute the free function prototype needs to be moved before the allocator/create function prototype so that the malloc attribute can refer to it.
The same attributes could be added to lots more functions in DPDK, but this patchset focuses on the key ones, and where problems exist in current code base.
The fixes should be backported to stable (they are real bugs), but the macros common and the annotation in malloc should not.
v2 - add release note
- add fix for nfp device
- drop mempool (will pick it up in later series)
Stephen Hemminger (16):
eal: add function attributes for allocation functions
memzone: fix use after free in tracing cryptodev/bcmfs: fix mis-matched free dma/ixd: fix incorrect free function in cleanup event/cnxk: fix pointer mismatch in cleanup examples/vhost: fix free function mismatch net/cnxk: fix use-after-free
bpf: fix free mismatch if convert fails net/e1000: fix use-after-free
net/sfc: fix use-after-free warning messages net/cpfl: fix free of nonheap object net/nfp: fix duplicate call to rte_free raw/ifpga/base: fix use after free common/qat: fix use after free drivers/ifpga: fix free function mismatch
eal: add alloc_function attribute to rte_malloc
doc/guides/rel_notes/release_24_11.rst | 8 +++ drivers/common/qat/qat_device.c | 6 +-- drivers/crypto/bcmfs/bcmfs_device.c | 4 +- drivers/dma/idxd/idxd_pci.c | 2 +- drivers/event/cnxk/cnxk_eventdev.c | 4 +- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- drivers/net/cpfl/cpfl_flow_parser.c | 1 - drivers/net/e1000/igb_ethdev.c | 4 +- drivers/net/nfp/flower/nfp_flower_flow.c | 1 - drivers/net/sfc/sfc_flow_rss.c | 4 +- drivers/net/sfc/sfc_mae.c | 23 ++++----- drivers/raw/ifpga/base/opae_intel_max10.c | 11 +++- drivers/raw/ifpga/ifpga_rawdev.c | 8 +-- examples/vhost_blk/vhost_blk.c | 2 +- lib/bpf/bpf_convert.c | 2 +- lib/eal/common/eal_common_memzone.c | 3 +- lib/eal/include/rte_common.h | 30 +++++++++++ lib/eal/include/rte_malloc.h | 63 ++++++++++++++--------- 18 files changed, 116 insertions(+), 62 deletions(-)
Signature · 1 line
2.45.2
PATCH v2 1/16 eal: add function attributes for allocation functions
The allocation functions take a alignment argument that can be useful to hint the compiler optimizer.
This is supported by Gcc and Clang but only useful with Gcc because Clang gives warning if alignment is 0.
Recent versions of GCC have a malloc attribute that can be used to find mismatches between allocation and free; the typical problem caught is a pointer allocated with rte_malloc() that is then incorrectly freed using free().
--- lib/eal/include/rte_common.h | 30 ++++++++++++++++++++++++++++++ lib/eal/include/rte_malloc.h | 24 ++++++++++++++++-------- 2 files changed, 46 insertions(+), 8 deletions(-)
lib/eal/include/rte_common.h +30 -0
@@ -228,6 +228,36 @@ typedef uint16_t unaligned_uint16_t;#define __rte_alloc_size (...)#endif +/** +* Tells the compiler that the function returns a value that points to +* memory aligned by a function argument. +* Not enabled on clang because it warns if align argument is zero. +*/ +#if defined(RTE_CC_GCC ) +#define __rte_alloc_align (align_arg) \ + __attribute__((alloc_align(align_arg))) +#else +#define __rte_alloc_align (...) +#endif + +/** +* Tells the compiler this is a function like malloc and that the pointer +* returned cannot alias any other pointer (ie new memory). +* +* Also, with recent GCC versions also able to track that proper +* dealloctor function is used for this pointer. +*/ +#if defined(RTE_TOOLCHAIN_GCC ) && (GCC_VERSION >=110000 ) +#define __rte_alloc_func (...) \ + __attribute__((malloc, malloc(__VA_ARGS__))) + +#elif defined(RTE_CC_GCC ) || defined(RTE_CC_CLANG ) +#define __rte_alloc_func (...) \ + __attribute__((malloc)) +#else +#define __rte_alloc_func (...) +#endif +#define RTE_PRIORITY_LOG 101#define RTE_PRIORITY_BUS 110#define RTE_PRIORITY_CLASS 120
lib/eal/include/rte_malloc.h +16 -8
@@ -54,7 +54,8 @@ struct rte_malloc_socket_stats {*/ void *rte_malloc (const char *type,size_t size,unsigned align) -__rte_alloc_size (2 ); +__rte_alloc_size (2 ) +__rte_alloc_align (3 );/** * Allocate zeroed memory from the heap.
@@ -81,7 +82,8 @@ rte_malloc(const char *type, size_t size, unsigned align)*/ void *rte_zmalloc (const char *type,size_t size,unsigned align) -__rte_alloc_size (2 ); +__rte_alloc_size (2 ) +__rte_alloc_align (3 );/** * Replacement function for calloc(), using huge-page memory. Memory area is
@@ -108,7 +110,8 @@ rte_zmalloc(const char *type, size_t size, unsigned align)*/ void *rte_calloc (const char *type,size_t num,size_t size,unsigned align) -__rte_alloc_size (2 ,3 ); +__rte_alloc_size (2 ,3 ) +__rte_alloc_align (4 );/** * Replacement function for realloc(), using huge-page memory. Reserved area
@@ -132,7 +135,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)*/ void *rte_realloc (void *ptr,size_t size,unsigned int align) -__rte_alloc_size (2 ); +__rte_alloc_size (2 ) +__rte_alloc_align (3 );/** * Replacement function for realloc(), using huge-page memory. Reserved area
@@ -158,7 +162,8 @@ rte_realloc(void *ptr, size_t size, unsigned int align)*/ void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) -__rte_alloc_size (2 ); +__rte_alloc_size (2 ) +__rte_alloc_align (3 );/** * This function allocates memory from the huge-page area of memory. The memory
@@ -185,7 +190,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)*/ void *rte_malloc_socket (const char *type,size_t size,unsigned align,int socket) -__rte_alloc_size (2 ); +__rte_alloc_size (2 ) +__rte_alloc_align (3 );/** * Allocate zeroed memory from the heap.
@@ -214,7 +220,8 @@ rte_malloc_socket(const char *type, size_t size, unsigned align, int socket)*/ void *rte_zmalloc_socket (const char *type,size_t size,unsigned align,int socket) -__rte_alloc_size (2 ); +__rte_alloc_size (2 ) +__rte_alloc_align (3 );/** * Replacement function for calloc(), using huge-page memory. Memory area is
@@ -243,7 +250,8 @@ rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)*/ void *rte_calloc_socket (const char *type,size_t num,size_t size,unsigned align,int socket) -__rte_alloc_size (2 ,3 ); +__rte_alloc_size (2 ,3 ) +__rte_alloc_align (4 );/** * Frees the memory space pointed to by the provided pointer.
Signature · 1 line
2.45.2
PATCH v2 2/16 memzone: fix use after free in tracing
In reply to Stephen HemmingerUsing the freed value for tracing is not a good idea. Although it is harmless for tracing, it will cause analyzers to flag this as unsafe.
--- lib/eal/common/eal_common_memzone.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/eal/common/eal_common_memzone.c +2 -1
@@ -331,9 +331,10 @@ rte_memzone_free(const struct rte_memzone *mz)rte_rwlock_write_unlock (&mcfg->mlock); +rte_eal_trace_memzone_free (name, addr, ret); +rte_free (addr); -rte_eal_trace_memzone_free (name, addr, ret);return ret; }
Signature · 1 line
2.45.2
PATCH v2 3/16 cryptodev/bcmfs: fix mis-matched free
In reply to Stephen HemmingerThe device structure is allocated with rte_malloc() and then incorrectly freed with free(). This will lead to corrupt malloc pool.
Bugzilla ID: 1552
--- drivers/crypto/bcmfs/bcmfs_device.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/crypto/bcmfs/bcmfs_device.c +2 -2
@@ -139,7 +139,7 @@ fsdev_allocate_one_dev(struct rte_vdev_device *vdev,return fsdev; cleanup: -free (fsdev); +rte_free (fsdev);return NULL ; }
@@ -163,7 +163,7 @@ fsdev_release(struct bcmfs_device *fsdev)return ;TAILQ_REMOVE (&fsdev_list, fsdev, next); -free (fsdev); +rte_free (fsdev); } static int
Signature · 1 line
2.45.2
PATCH v2 4/16 dma/ixd: fix incorrect free function in cleanup
In reply to Stephen HemmingerThe data structure is allocated with rte_malloc and incorrectly freed in cleanup logic using free.
Bugzilla ID: 1549
--- drivers/dma/idxd/idxd_pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma/idxd/idxd_pci.c +1 -1
@@ -301,7 +301,7 @@ init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,return nb_wqs; err: -free (pci); +rte_free (pci);return err_code; }
Signature · 1 line
2.45.2
PATCH v2 5/16 event/cnxk: fix pointer mismatch in cleanup
In reply to Stephen HemmingerThe code to cleanup in case of error was passing incorrect value to rte_free. The ports[] entry was allocated with rte_malloc and that should be used instead of the offset in that object.
--- drivers/event/cnxk/cnxk_eventdev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/event/cnxk/cnxk_eventdev.c +3 -1
@@ -121,8 +121,10 @@ cnxk_setup_event_ports(const struct rte_eventdev *event_dev,return 0 ; hws_fini:for (i = i -1 ; i >=0 ; i--) { +void *ws = event_dev->data->ports[i]; + event_dev->data->ports[i] =NULL ; -rte_free (cnxk_sso_hws_get_cookie (event_dev->data->ports[i])); +rte_free (ws); }return -ENOMEM ; }
Signature · 1 line
2.45.2
PATCH v2 6/16 examples/vhost: fix free function mismatch
In reply to Stephen HemmingerThe pointer bdev is allocated with rte_zmalloc() and then incorrectly freed with free() which will lead pool corruption.
Bugzilla ID: 1553
--- examples/vhost_blk/vhost_blk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
examples/vhost_blk/vhost_blk.c +1 -1
@@ -776,7 +776,7 @@ vhost_blk_bdev_construct(const char *bdev_name, bdev->data =rte_zmalloc (NULL , blk_cnt * blk_size,0 );if (!bdev->data) {fprintf (stderr,"No enough reserved huge memory for disk\n" ); -free (bdev); +rte_free (bdev);return NULL ; }
Signature · 1 line
2.45.2
PATCH v2 7/16 net/cnxk: fix use-after-free
In reply to Stephen HemmingerThe driver would refer to the mempool object after it was freed.
Bugzilla ID: 1554
--- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/cnxk/cnxk_ethdev_sec.c +1 -1
@@ -136,8 +136,8 @@ cnxk_nix_inl_custom_meta_pool_cb(uintptr_t pmpool, uintptr_t *mpool, const charreturn -EINVAL ; } -rte_mempool_free (hp);plt_free (hp->pool_config); +rte_mempool_free (hp); *aura_handle =0 ; *mpool =0 ;
Signature · 1 line
2.45.2
PATCH v2 8/16 bpf: fix free mismatch if convert fails
In reply to Stephen HemmingerIf conversion of cBF to eBPF fails then an object allocated with rte_malloc() would be passed to free().
[908/3201] Compiling C object lib/librte_bpf.a.p/bpf_bpf_convert.c.o../lib/bpf/bpf_convert.c: In function ‘rte_bpf_convert’:../lib/bpf/bpf_convert.c:559:17: warning: ‘free’ called on pointer returned from a mismatched allocation function [-Wmismatched-dealloc]
559 | free(prm);
| ^~~~~~~~~
../lib/bpf/bpf_convert.c:545:15: note: returned from ‘rte_zmalloc’
545 | prm = rte_zmalloc("bpf_filter",
| ^~~~~~~~~~~~~~~~~~~~~~~~~
546 | sizeof(*prm) + ebpf_len * sizeof(*ebpf), 0);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~--- lib/bpf/bpf_convert.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/bpf/bpf_convert.c +1 -1
@@ -556,7 +556,7 @@ rte_bpf_convert(const struct bpf_program *prog) ret =bpf_convert_filter (prog->bf_insns, prog->bf_len, ebpf, &ebpf_len);if (ret <0 ) {RTE_BPF_LOG_LINE (ERR ,"%s: cannot convert cBPF to eBPF" , __func__); -free (prm); +rte_free (prm); rte_errno = -ret;return NULL ; }
Signature · 1 line
2.45.2
PATCH v2 9/16 net/e1000: fix use-after-free
In reply to Stephen HemmingerThe driver cleanup code was freeing the filter object then dereferencing it.
Bugzilla ID: 1550
--- drivers/net/e1000/igb_ethdev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/e1000/igb_ethdev.c +2 -2
@@ -3907,11 +3907,11 @@ igb_delete_2tuple_filter(struct rte_eth_dev *dev, filter_info->twotuple_mask &= ~(1 << filter->index);TAILQ_REMOVE (&filter_info->twotuple_list, filter, entries); -rte_free (filter);E1000_WRITE_REG (hw,E1000_TTQF (filter->index),E1000_TTQF_DISABLE_MASK );E1000_WRITE_REG (hw,E1000_IMIR (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIREXT (filter->index),0 ); +rte_free (filter);return 0 ; }
@@ -4348,7 +4348,6 @@ igb_delete_5tuple_filter_82576(struct rte_eth_dev *dev, filter_info->fivetuple_mask &= ~(1 << filter->index);TAILQ_REMOVE (&filter_info->fivetuple_list, filter, entries); -rte_free (filter);E1000_WRITE_REG (hw,E1000_FTQF (filter->index),E1000_FTQF_VF_BP |E1000_FTQF_MASK );
@@ -4357,6 +4356,7 @@ igb_delete_5tuple_filter_82576(struct rte_eth_dev *dev,E1000_WRITE_REG (hw,E1000_SPQF (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIR (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIREXT (filter->index),0 ); +rte_free (filter);return 0 ; }
Signature · 1 line
2.45.2
PATCH v2 10/16 net/sfc: fix use-after-free warning messages
In reply to Stephen HemmingerIf compiler detection of use-after-free is enabled then this drivers debug messages will cause warnings. Change to move debug message before the object is freed.
Bugzilla ID: 1551
--- drivers/net/sfc/sfc_flow_rss.c | 4 ++-- drivers/net/sfc/sfc_mae.c | 23 +++++++++-------------- 2 files changed, 11 insertions(+), 16 deletions(-)
drivers/net/sfc/sfc_flow_rss.c +2 -2
@@ -303,9 +303,9 @@ sfc_flow_rss_ctx_del(struct sfc_adapter *sa, struct sfc_flow_rss_ctx *ctx)TAILQ_REMOVE (&flow_rss->ctx_list, ctx, entries);rte_free (ctx->qid_offsets); -rte_free (ctx); -sfc_dbg (sa,"flow-rss: deleted ctx=%p" , ctx); + +rte_free (ctx); } static int
drivers/net/sfc/sfc_mae.c +9 -14
@@ -400,9 +400,8 @@ sfc_mae_outer_rule_del(struct sfc_adapter *sa,efx_mae_match_spec_fini (sa->nic, rule->match_spec);TAILQ_REMOVE (&mae->outer_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted outer_rule=%p" , rule); +rte_free (rule); } static int
@@ -585,9 +584,8 @@ sfc_mae_mac_addr_del(struct sfc_adapter *sa, struct sfc_mae_mac_addr *mac_addr) }TAILQ_REMOVE (&mae->mac_addrs, mac_addr, entries); -rte_free (mac_addr); -sfc_dbg (sa,"deleted mac_addr=%p" , mac_addr); +rte_free (mac_addr); }enum sfc_mae_mac_addr_type {
@@ -785,10 +783,10 @@ sfc_mae_encap_header_del(struct sfc_adapter *sa, }TAILQ_REMOVE (&mae->encap_headers, encap_header, entries); +sfc_dbg (sa,"deleted encap_header=%p" , encap_header); +rte_free (encap_header->buf);rte_free (encap_header); - -sfc_dbg (sa,"deleted encap_header=%p" , encap_header); } static int
@@ -983,9 +981,8 @@ sfc_mae_counter_del(struct sfc_adapter *sa, struct sfc_mae_counter *counter) }TAILQ_REMOVE (&mae->counters, counter, entries); -rte_free (counter); -sfc_dbg (sa,"deleted counter=%p" , counter); +rte_free (counter); } static int
@@ -1165,9 +1162,8 @@ sfc_mae_action_set_del(struct sfc_adapter *sa,sfc_mae_mac_addr_del (sa, action_set->src_mac_addr);sfc_mae_counter_del (sa, action_set->counter);TAILQ_REMOVE (&mae->action_sets, action_set, entries); -rte_free (action_set); -sfc_dbg (sa,"deleted action_set=%p" , action_set); +rte_free (action_set); } static int
@@ -1401,10 +1397,10 @@ sfc_mae_action_set_list_del(struct sfc_adapter *sa,sfc_mae_action_set_del (sa, action_set_list->action_sets[i]);TAILQ_REMOVE (&mae->action_set_lists, action_set_list, entries); +sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); +rte_free (action_set_list->action_sets);rte_free (action_set_list); - -sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); } static int
@@ -1667,9 +1663,8 @@ sfc_mae_action_rule_del(struct sfc_adapter *sa,sfc_mae_outer_rule_del (sa, rule->outer_rule);TAILQ_REMOVE (&mae->action_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted action_rule=%p" , rule); +rte_free (rule); } static int
Signature · 1 line
2.45.2
PATCH v2 11/16 net/cpfl: fix free of nonheap object
In reply to Stephen HemmingerWith proper annotation, GCC discovers that this driver is calling rte_free() on an object that was not allocated (it is part of array in another object).
In function ‘cpfl_flow_js_mr_layout’,
inlined from ‘cpfl_flow_js_mr_action’ at ../drivers/net/cpfl/cpfl_flow_parser.c:848:9,
inlined from ‘cpfl_flow_js_mod_rule’ at ../drivers/net/cpfl/cpfl_flow_parser.c:908:9,
inlined from ‘cpfl_parser_init’ at ../drivers/net/cpfl/cpfl_flow_parser.c:932:8,
inlined from ‘cpfl_parser_create’ at ../drivers/net/cpfl/cpfl_flow_parser.c:959:8:../drivers/net/cpfl/cpfl_flow_parser.c:740:9: warning: ‘rte_free’ called on pointer ‘*parser.modifications’ with nonzero offset [28, 15479062120396] [-Wfree-nonheap-object]
740 | rte_free(js_mod->layout);
| ^~~~~~~~~~~~~~~~~~~~~~~~--- drivers/net/cpfl/cpfl_flow_parser.c | 1 - 1 file changed, 1 deletion(-)
drivers/net/cpfl/cpfl_flow_parser.c +0 -1
@@ -737,7 +737,6 @@ cpfl_flow_js_mr_layout(json_t *ob_layouts, struct cpfl_flow_js_mr_action_mod *jsreturn 0 ; err: -rte_free (js_mod->layout);return -EINVAL ; }
Signature · 1 line
2.45.2
PATCH v2 12/16 net/nfp: fix duplicate call to rte_free
In reply to Stephen HemmingerCalling rte_free twice on same object will corrupt the heap.
Warning is:
In function 'nfp_pre_tun_table_check_del',
inlined from 'nfp_flow_destroy' at ../drivers/net/nfp/flower/nfp_flower_flow.c:5143:9:../drivers/net/nfp/flower/nfp_flower_flow.c:3830:9: error: pointer 'entry' used after 'rte_free' [-Werror=use-after-free]
3830 | rte_free(entry);
| ^~~~~~~~~~~~~~~../drivers/net/nfp/flower/nfp_flower_flow.c:3825:9: note: call to 'rte_free' here
3825 | rte_free(entry);
| ^~~~~~~~~~~~~~~
Bugzilla ID: 1555
--- drivers/net/nfp/flower/nfp_flower_flow.c | 1 - 1 file changed, 1 deletion(-)
drivers/net/nfp/flower/nfp_flower_flow.c +0 -1
@@ -3822,7 +3822,6 @@ nfp_pre_tun_table_check_del(struct nfp_flower_representor *repr, goto free_entry; } -rte_free (entry);rte_free (find_entry); priv->pre_tun_cnt--;
Signature · 1 line
2.45.2
PATCH v2 13/16 raw/ifpga/base: fix use after free
In reply to Stephen HemmingerThe TAILQ_FOREACH() macro would refer to info after it had been freed. Fix by introducing TAILQ_FOREACH_SAFE here.
--- drivers/raw/ifpga/base/opae_intel_max10.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/raw/ifpga/base/opae_intel_max10.c +9 -2
@@ -6,6 +6,13 @@#include <libfdt.h> #include "opae_osdep.h" +#ifndef TAILQ_FOREACH_SAFE +#define TAILQ_FOREACH_SAFE (var, head, field, tvar) \ + for ((var) = TAILQ_FIRST((head)); \ + (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) +#endif +int max10_sys_read (struct intel_max10_device *dev,unsigned int offset,unsigned int *val) {
@@ -746,9 +753,9 @@ static int fdt_get_named_reg(const void *fdt, int node, const char *name,static void max10_sensor_uinit (struct intel_max10_device *dev) { -struct opae_sensor_info *info; +struct opae_sensor_info *info, *next; - TAILQ_FOREACH(info, &dev->opae_sensor_list, node) { + TAILQ_FOREACH_SAFE(info, &dev->opae_sensor_list, node, next) {TAILQ_REMOVE (&dev->opae_sensor_list, info, node);opae_free (info); }
Signature · 1 line
2.45.2
PATCH v2 14/16 common/qat: fix use after free
In reply to Stephen HemmingerChecking return value of rte_memzone_free() is pointless and if it failed then it was because the pointer was null.
--- drivers/common/qat/qat_device.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/common/qat/qat_device.c +1 -5
@@ -390,11 +390,7 @@ qat_pci_device_allocate(struct rte_pci_device *pci_dev)return qat_dev; error:rte_free (qat_dev->command_line); -if (rte_memzone_free (qat_dev_mz)) { -QAT_LOG (DEBUG , -"QAT internal error! Trying to free already allocated memzone: %s" , - qat_dev_mz->name); - } +rte_memzone_free (qat_dev_mz);return NULL ; }
Signature · 1 line
2.45.2
PATCH v2 15/16 drivers/ifpga: fix free function mismatch
In reply to Stephen HemmingerThe raw ifpga driver redefines malloc to be opae_malloc and free to be opae_free; which is a bad idea.
This leads to case where interrupt efd array is allocated with calloc() and then passed to rte_free. The workaround is to allocate the array with rte_calloc() instead.
--- drivers/raw/ifpga/ifpga_rawdev.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/raw/ifpga/ifpga_rawdev.c +4 -4
@@ -1499,7 +1499,7 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, nb_intr =rte_intr_nb_intr_get (*intr_handle); - intr_efds =calloc (nb_intr,sizeof (int )); + intr_efds =rte_calloc ("ifpga_efds" , nb_intr,sizeof (int ),0 );if (!intr_efds)return -ENOMEM ;
@@ -1508,7 +1508,7 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, ret =opae_acc_set_irq (acc, vec_start, count, intr_efds);if (ret) { -free (intr_efds); +rte_free (intr_efds);return -EINVAL ; } }
@@ -1517,13 +1517,13 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, ret =rte_intr_callback_register (*intr_handle, handler, (void *)arg);if (ret) { -free (intr_efds); +rte_free (intr_efds);return -EINVAL ; }IFPGA_RAWDEV_PMD_INFO ("success register %s interrupt\n" , name); -free (intr_efds); +rte_free (intr_efds);return 0 ; }
Signature · 1 line
2.45.2
PATCH v2 16/16 eal: add alloc_function attribute to rte_malloc
In reply to Stephen HemmingerUse the GCC function attribute to detect cases where memory is allocated with rte_malloc and freed incorrectly with libc version of free (and vice versa). Also will detect some other pointer mismatches.
--- doc/guides/rel_notes/release_24_11.rst | 8 ++++ lib/eal/include/rte_malloc.h | 55 +++++++++++++++----------- 2 files changed, 40 insertions(+), 23 deletions(-)
doc/guides/rel_notes/release_24_11.rst +8 -0
@@ -55,6 +55,14 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* **Hardened rte_malloc and related functions.** + + * Added function attributes to ``rte_malloc`` and similar functions + that can catch some obvious bugs at compile time (with GCC 11.0 or later). + Examples: calling ``free()`` on pointer that was allocated with ``rte_malloc`` + (and vice versa); freeing the same pointer twice in the same routine; + freeing an object that was not created by allocation; etc. + Removed Items -------------
lib/eal/include/rte_malloc.h +32 -23
109 lines not on this page · raw message
Signature · 1 line
2.45.2
v3 cover Fix allocation bugs and add malloc hardening
In reply to Stephen HemmingerRecent versions of Gcc have some additonal function attributes that can help with DPDK performance and stability.
The alloc_align attribute tells the compiler what the alignment of the allocation will be, and the optimizer can use this to produce better code (especially memcpy and structure copies).
The malloc attribute tells compiler that object is not overlapping and potentially aliasing. It also as an additional variant in Gcc 11 or later that allows for detecting all sorts of common errors like calling free() on memory allocated with rte_malloc().
In order to use the malloc attribute the free function prototype needs to be moved before the allocator/create function prototype so that the malloc attribute can refer to it.
This uncovered at least 16 pre-existing bugs in DPDK, these should go to stable. This patch set is structured with:
- fix any new warnings that were discovered
- add macros for enable the macros
- enable the attributes
The same attributes could be added to lots more functions in DPDK, but this patchset focuses on the key ones, and where problems exist in current code base.
v3 - fix more broken devices
- reorder patches
Stephen Hemminger (18):
memzone: fix use after free in tracing cryptodev/bcmfs: fix mis-matched free dma/ixd: fix incorrect free function in cleanup event/cnxk: fix pointer mismatch in cleanup examples/vhost: fix free function mismatch net/cnxk: fix use-after-free
bpf: fix free mismatch if convert fails net/e1000: fix use-after-free
net/sfc: fix use-after-free warning messages net/cpfl: fix free of nonheap object net/nfp: fix duplicate call to rte_free raw/ifpga/base: fix use after free common/qat: fix use after free drivers/ifpga: fix free function mismatch baseband/la12xx: prevent use after free common/ipdf: fix use after free due
eal: add function attributes for allocation functions
eal: add alloc_function attribute to rte_malloc
doc/guides/rel_notes/release_24_11.rst | 8 +++ drivers/baseband/la12xx/bbdev_la12xx.c | 1 + drivers/common/idpf/base/idpf_osdep.h | 10 +++- drivers/common/idpf/idpf_common_device.c | 3 +- drivers/common/qat/qat_device.c | 6 +-- drivers/crypto/bcmfs/bcmfs_device.c | 4 +- drivers/dma/idxd/idxd_pci.c | 2 +- drivers/event/cnxk/cnxk_eventdev.c | 4 +- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- drivers/net/cpfl/cpfl_flow_parser.c | 1 - drivers/net/e1000/igb_ethdev.c | 4 +- drivers/net/nfp/flower/nfp_flower_flow.c | 1 - drivers/net/sfc/sfc_flow_rss.c | 4 +- drivers/net/sfc/sfc_mae.c | 23 ++++----- drivers/raw/ifpga/base/opae_intel_max10.c | 11 +++- drivers/raw/ifpga/ifpga_rawdev.c | 8 +-- examples/vhost_blk/vhost_blk.c | 2 +- lib/bpf/bpf_convert.c | 2 +- lib/eal/common/eal_common_memzone.c | 3 +- lib/eal/include/rte_common.h | 30 +++++++++++ lib/eal/include/rte_malloc.h | 63 ++++++++++++++--------- 21 files changed, 126 insertions(+), 66 deletions(-)
Signature · 1 line
2.45.2
PATCH v3 1/18 memzone: fix use after free in tracing
Using the freed value for tracing is not a good idea. Although it is harmless for tracing, it will cause analyzers to flag this as unsafe.
--- lib/eal/common/eal_common_memzone.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/eal/common/eal_common_memzone.c +2 -1
@@ -331,9 +331,10 @@ rte_memzone_free(const struct rte_memzone *mz)rte_rwlock_write_unlock (&mcfg->mlock); +rte_eal_trace_memzone_free (name, addr, ret); +rte_free (addr); -rte_eal_trace_memzone_free (name, addr, ret);return ret; }
Signature · 1 line
2.45.2
Re: [PATCH v3 01/18] memzone: fix use after free in tracing
Quoted 5 lines · Stephen Hemminger
Using the freed value for tracing is not a good idea. Although it is harmless for tracing, it will cause analyzers to flag this as unsafe.
Signed-off-by Stephen Hemminger <[email protected]>
PATCH v3 2/18 cryptodev/bcmfs: fix mis-matched free
In reply to Stephen HemmingerThe device structure is allocated with rte_malloc() and then incorrectly freed with free(). This will lead to corrupt malloc pool.
Bugzilla ID: 1552
--- drivers/crypto/bcmfs/bcmfs_device.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/crypto/bcmfs/bcmfs_device.c +2 -2
@@ -139,7 +139,7 @@ fsdev_allocate_one_dev(struct rte_vdev_device *vdev,return fsdev; cleanup: -free (fsdev); +rte_free (fsdev);return NULL ; }
@@ -163,7 +163,7 @@ fsdev_release(struct bcmfs_device *fsdev)return ;TAILQ_REMOVE (&fsdev_list, fsdev, next); -free (fsdev); +rte_free (fsdev); } static int
Signature · 1 line
2.45.2
PATCH v3 3/18 dma/ixd: fix incorrect free function in cleanup
In reply to Stephen HemmingerThe data structure is allocated with rte_malloc and incorrectly freed in cleanup logic using free.
Bugzilla ID: 1549
--- drivers/dma/idxd/idxd_pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma/idxd/idxd_pci.c +1 -1
@@ -301,7 +301,7 @@ init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,return nb_wqs; err: -free (pci); +rte_free (pci);return err_code; }
Signature · 1 line
2.45.2
PATCH v3 4/18 event/cnxk: fix pointer mismatch in cleanup
In reply to Stephen HemmingerThe code to cleanup in case of error was passing incorrect value to rte_free. The ports[] entry was allocated with rte_malloc and that should be used instead of the offset in that object.
--- drivers/event/cnxk/cnxk_eventdev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/event/cnxk/cnxk_eventdev.c +3 -1
@@ -121,8 +121,10 @@ cnxk_setup_event_ports(const struct rte_eventdev *event_dev,return 0 ; hws_fini:for (i = i -1 ; i >=0 ; i--) { +void *ws = event_dev->data->ports[i]; + event_dev->data->ports[i] =NULL ; -rte_free (cnxk_sso_hws_get_cookie (event_dev->data->ports[i])); +rte_free (ws); }return -ENOMEM ; }
Signature · 1 line
2.45.2
PATCH v3 5/18 examples/vhost: fix free function mismatch
In reply to Stephen HemmingerThe pointer bdev is allocated with rte_zmalloc() and then incorrectly freed with free() which will lead pool corruption.
Bugzilla ID: 1553
--- examples/vhost_blk/vhost_blk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
examples/vhost_blk/vhost_blk.c +1 -1
@@ -776,7 +776,7 @@ vhost_blk_bdev_construct(const char *bdev_name, bdev->data =rte_zmalloc (NULL , blk_cnt * blk_size,0 );if (!bdev->data) {fprintf (stderr,"No enough reserved huge memory for disk\n" ); -free (bdev); +rte_free (bdev);return NULL ; }
Signature · 1 line
2.45.2
Re: [PATCH v3 05/18] examples/vhost: fix free function mismatch
Quoted 9 lines · Stephen Hemminger
The pointer bdev is allocated with
rte_zmalloc()and then incorrectly freed withfree()which will lead pool corruption.Bugzilla ID: 1553
Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage sample")Signed-off-by Stephen Hemminger <[email protected]>
PATCH v3 6/18 net/cnxk: fix use-after-free
In reply to Stephen HemmingerThe driver would refer to the mempool object after it was freed.
Bugzilla ID: 1554
--- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/cnxk/cnxk_ethdev_sec.c +1 -1
@@ -136,8 +136,8 @@ cnxk_nix_inl_custom_meta_pool_cb(uintptr_t pmpool, uintptr_t *mpool, const charreturn -EINVAL ; } -rte_mempool_free (hp);plt_free (hp->pool_config); +rte_mempool_free (hp); *aura_handle =0 ; *mpool =0 ;
Signature · 1 line
2.45.2
PATCH v3 7/18 bpf: fix free mismatch if convert fails
In reply to Stephen HemmingerIf conversion of cBF to eBPF fails then an object allocated with rte_malloc() would be passed to free().
[908/3201] Compiling C object lib/librte_bpf.a.p/bpf_bpf_convert.c.o../lib/bpf/bpf_convert.c: In function ‘rte_bpf_convert’:../lib/bpf/bpf_convert.c:559:17: warning: ‘free’ called on pointer returned from a mismatched allocation function [-Wmismatched-dealloc]
559 | free(prm);
| ^~~~~~~~~
../lib/bpf/bpf_convert.c:545:15: note: returned from ‘rte_zmalloc’
545 | prm = rte_zmalloc("bpf_filter",
| ^~~~~~~~~~~~~~~~~~~~~~~~~
546 | sizeof(*prm) + ebpf_len * sizeof(*ebpf), 0);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~--- lib/bpf/bpf_convert.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/bpf/bpf_convert.c +1 -1
@@ -556,7 +556,7 @@ rte_bpf_convert(const struct bpf_program *prog) ret =bpf_convert_filter (prog->bf_insns, prog->bf_len, ebpf, &ebpf_len);if (ret <0 ) {RTE_BPF_LOG_LINE (ERR ,"%s: cannot convert cBPF to eBPF" , __func__); -free (prm); +rte_free (prm); rte_errno = -ret;return NULL ; }
Signature · 1 line
2.45.2
PATCH v3 8/18 net/e1000: fix use-after-free
In reply to Stephen HemmingerThe driver cleanup code was freeing the filter object then dereferencing it.
Bugzilla ID: 1550
--- drivers/net/e1000/igb_ethdev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/e1000/igb_ethdev.c +2 -2
@@ -3907,11 +3907,11 @@ igb_delete_2tuple_filter(struct rte_eth_dev *dev, filter_info->twotuple_mask &= ~(1 << filter->index);TAILQ_REMOVE (&filter_info->twotuple_list, filter, entries); -rte_free (filter);E1000_WRITE_REG (hw,E1000_TTQF (filter->index),E1000_TTQF_DISABLE_MASK );E1000_WRITE_REG (hw,E1000_IMIR (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIREXT (filter->index),0 ); +rte_free (filter);return 0 ; }
@@ -4348,7 +4348,6 @@ igb_delete_5tuple_filter_82576(struct rte_eth_dev *dev, filter_info->fivetuple_mask &= ~(1 << filter->index);TAILQ_REMOVE (&filter_info->fivetuple_list, filter, entries); -rte_free (filter);E1000_WRITE_REG (hw,E1000_FTQF (filter->index),E1000_FTQF_VF_BP |E1000_FTQF_MASK );
@@ -4357,6 +4356,7 @@ igb_delete_5tuple_filter_82576(struct rte_eth_dev *dev,E1000_WRITE_REG (hw,E1000_SPQF (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIR (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIREXT (filter->index),0 ); +rte_free (filter);return 0 ; }
Signature · 1 line
2.45.2
PATCH v3 9/18 net/sfc: fix use-after-free warning messages
In reply to Stephen HemmingerIf compiler detection of use-after-free is enabled then this drivers debug messages will cause warnings. Change to move debug message before the object is freed.
Bugzilla ID: 1551
--- drivers/net/sfc/sfc_flow_rss.c | 4 ++-- drivers/net/sfc/sfc_mae.c | 23 +++++++++-------------- 2 files changed, 11 insertions(+), 16 deletions(-)
drivers/net/sfc/sfc_flow_rss.c +2 -2
@@ -303,9 +303,9 @@ sfc_flow_rss_ctx_del(struct sfc_adapter *sa, struct sfc_flow_rss_ctx *ctx)TAILQ_REMOVE (&flow_rss->ctx_list, ctx, entries);rte_free (ctx->qid_offsets); -rte_free (ctx); -sfc_dbg (sa,"flow-rss: deleted ctx=%p" , ctx); + +rte_free (ctx); } static int
drivers/net/sfc/sfc_mae.c +9 -14
@@ -400,9 +400,8 @@ sfc_mae_outer_rule_del(struct sfc_adapter *sa,efx_mae_match_spec_fini (sa->nic, rule->match_spec);TAILQ_REMOVE (&mae->outer_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted outer_rule=%p" , rule); +rte_free (rule); } static int
@@ -585,9 +584,8 @@ sfc_mae_mac_addr_del(struct sfc_adapter *sa, struct sfc_mae_mac_addr *mac_addr) }TAILQ_REMOVE (&mae->mac_addrs, mac_addr, entries); -rte_free (mac_addr); -sfc_dbg (sa,"deleted mac_addr=%p" , mac_addr); +rte_free (mac_addr); }enum sfc_mae_mac_addr_type {
@@ -785,10 +783,10 @@ sfc_mae_encap_header_del(struct sfc_adapter *sa, }TAILQ_REMOVE (&mae->encap_headers, encap_header, entries); +sfc_dbg (sa,"deleted encap_header=%p" , encap_header); +rte_free (encap_header->buf);rte_free (encap_header); - -sfc_dbg (sa,"deleted encap_header=%p" , encap_header); } static int
@@ -983,9 +981,8 @@ sfc_mae_counter_del(struct sfc_adapter *sa, struct sfc_mae_counter *counter) }TAILQ_REMOVE (&mae->counters, counter, entries); -rte_free (counter); -sfc_dbg (sa,"deleted counter=%p" , counter); +rte_free (counter); } static int
@@ -1165,9 +1162,8 @@ sfc_mae_action_set_del(struct sfc_adapter *sa,sfc_mae_mac_addr_del (sa, action_set->src_mac_addr);sfc_mae_counter_del (sa, action_set->counter);TAILQ_REMOVE (&mae->action_sets, action_set, entries); -rte_free (action_set); -sfc_dbg (sa,"deleted action_set=%p" , action_set); +rte_free (action_set); } static int
@@ -1401,10 +1397,10 @@ sfc_mae_action_set_list_del(struct sfc_adapter *sa,sfc_mae_action_set_del (sa, action_set_list->action_sets[i]);TAILQ_REMOVE (&mae->action_set_lists, action_set_list, entries); +sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); +rte_free (action_set_list->action_sets);rte_free (action_set_list); - -sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); } static int
@@ -1667,9 +1663,8 @@ sfc_mae_action_rule_del(struct sfc_adapter *sa,sfc_mae_outer_rule_del (sa, rule->outer_rule);TAILQ_REMOVE (&mae->action_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted action_rule=%p" , rule); +rte_free (rule); } static int
Signature · 1 line
2.45.2
Re: [PATCH v3 09/18] net/sfc: fix use-after-free warning messages
Quoted 8 lines · Stephen Hemminger
If compiler detection of use-after-free is enabled then this drivers debug messages will cause warnings. Change to move debug message before the object is freed.
Bugzilla ID: 1551
Fixes 55c1238246d5 ("net/sfc: add more debug messages to transfer flows")Signed-off-by Stephen Hemminger <[email protected]>Reviewed-by Ivan Malov <[email protected]>
PATCH v3 10/18 net/cpfl: fix free of nonheap object
In reply to Stephen HemmingerWith proper annotation, GCC discovers that this driver is calling rte_free() on an object that was not allocated (it is part of array in another object).
In function ‘cpfl_flow_js_mr_layout’,
inlined from ‘cpfl_flow_js_mr_action’ at ../drivers/net/cpfl/cpfl_flow_parser.c:848:9,
inlined from ‘cpfl_flow_js_mod_rule’ at ../drivers/net/cpfl/cpfl_flow_parser.c:908:9,
inlined from ‘cpfl_parser_init’ at ../drivers/net/cpfl/cpfl_flow_parser.c:932:8,
inlined from ‘cpfl_parser_create’ at ../drivers/net/cpfl/cpfl_flow_parser.c:959:8:../drivers/net/cpfl/cpfl_flow_parser.c:740:9: warning: ‘rte_free’ called on pointer ‘*parser.modifications’ with nonzero offset [28, 15479062120396] [-Wfree-nonheap-object]
740 | rte_free(js_mod->layout);
| ^~~~~~~~~~~~~~~~~~~~~~~~--- drivers/net/cpfl/cpfl_flow_parser.c | 1 - 1 file changed, 1 deletion(-)
drivers/net/cpfl/cpfl_flow_parser.c +0 -1
@@ -737,7 +737,6 @@ cpfl_flow_js_mr_layout(json_t *ob_layouts, struct cpfl_flow_js_mr_action_mod *jsreturn 0 ; err: -rte_free (js_mod->layout);return -EINVAL ; }
Signature · 1 line
2.45.2
PATCH v3 11/18 net/nfp: fix duplicate call to rte_free
In reply to Stephen HemmingerCalling rte_free twice on same object will corrupt the heap.
Warning is:
In function 'nfp_pre_tun_table_check_del',
inlined from 'nfp_flow_destroy' at ../drivers/net/nfp/flower/nfp_flower_flow.c:5143:9:../drivers/net/nfp/flower/nfp_flower_flow.c:3830:9: error: pointer 'entry' used after 'rte_free' [-Werror=use-after-free]
3830 | rte_free(entry);
| ^~~~~~~~~~~~~~~../drivers/net/nfp/flower/nfp_flower_flow.c:3825:9: note: call to 'rte_free' here
3825 | rte_free(entry);
| ^~~~~~~~~~~~~~~
Bugzilla ID: 1555
--- drivers/net/nfp/flower/nfp_flower_flow.c | 1 - 1 file changed, 1 deletion(-)
drivers/net/nfp/flower/nfp_flower_flow.c +0 -1
@@ -3822,7 +3822,6 @@ nfp_pre_tun_table_check_del(struct nfp_flower_representor *repr, goto free_entry; } -rte_free (entry);rte_free (find_entry); priv->pre_tun_cnt--;
Signature · 1 line
2.45.2
PATCH v3 12/18 raw/ifpga/base: fix use after free
In reply to Stephen HemmingerThe TAILQ_FOREACH() macro would refer to info after it had been freed. Fix by introducing TAILQ_FOREACH_SAFE here.
--- drivers/raw/ifpga/base/opae_intel_max10.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/raw/ifpga/base/opae_intel_max10.c +9 -2
@@ -6,6 +6,13 @@#include <libfdt.h> #include "opae_osdep.h" +#ifndef TAILQ_FOREACH_SAFE +#define TAILQ_FOREACH_SAFE (var, head, field, tvar) \ + for ((var) = TAILQ_FIRST((head)); \ + (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) +#endif +int max10_sys_read (struct intel_max10_device *dev,unsigned int offset,unsigned int *val) {
@@ -746,9 +753,9 @@ static int fdt_get_named_reg(const void *fdt, int node, const char *name,static void max10_sensor_uinit (struct intel_max10_device *dev) { -struct opae_sensor_info *info; +struct opae_sensor_info *info, *next; - TAILQ_FOREACH(info, &dev->opae_sensor_list, node) { + TAILQ_FOREACH_SAFE(info, &dev->opae_sensor_list, node, next) {TAILQ_REMOVE (&dev->opae_sensor_list, info, node);opae_free (info); }
Signature · 1 line
2.45.2
PATCH v3 13/18 common/qat: fix use after free
In reply to Stephen HemmingerChecking return value of rte_memzone_free() is pointless and if it failed then it was because the pointer was null.
--- drivers/common/qat/qat_device.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/common/qat/qat_device.c +1 -5
@@ -390,11 +390,7 @@ qat_pci_device_allocate(struct rte_pci_device *pci_dev)return qat_dev; error:rte_free (qat_dev->command_line); -if (rte_memzone_free (qat_dev_mz)) { -QAT_LOG (DEBUG , -"QAT internal error! Trying to free already allocated memzone: %s" , - qat_dev_mz->name); - } +rte_memzone_free (qat_dev_mz);return NULL ; }
Signature · 1 line
2.45.2
PATCH v3 14/18 drivers/ifpga: fix free function mismatch
In reply to Stephen HemmingerThe raw ifpga driver redefines malloc to be opae_malloc and free to be opae_free; which is a bad idea.
This leads to case where interrupt efd array is allocated with calloc() and then passed to rte_free. The workaround is to allocate the array with rte_calloc() instead.
--- drivers/raw/ifpga/ifpga_rawdev.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/raw/ifpga/ifpga_rawdev.c +4 -4
@@ -1499,7 +1499,7 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, nb_intr =rte_intr_nb_intr_get (*intr_handle); - intr_efds =calloc (nb_intr,sizeof (int )); + intr_efds =rte_calloc ("ifpga_efds" , nb_intr,sizeof (int ),0 );if (!intr_efds)return -ENOMEM ;
@@ -1508,7 +1508,7 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, ret =opae_acc_set_irq (acc, vec_start, count, intr_efds);if (ret) { -free (intr_efds); +rte_free (intr_efds);return -EINVAL ; } }
@@ -1517,13 +1517,13 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, ret =rte_intr_callback_register (*intr_handle, handler, (void *)arg);if (ret) { -free (intr_efds); +rte_free (intr_efds);return -EINVAL ; }IFPGA_RAWDEV_PMD_INFO ("success register %s interrupt\n" , name); -free (intr_efds); +rte_free (intr_efds);return 0 ; }
Signature · 1 line
2.45.2
PATCH v3 15/18 baseband/la12xx: prevent use after free
In reply to Stephen HemmingerIt is possible that the info pointer (hp) could get freed twice. Fix by nulling after free.
In function 'setup_la12xx_dev',
inlined from 'la12xx_bbdev_create' at ../drivers/baseband/la12xx/bbdev_la12xx.c:1029:8,
inlined from 'la12xx_bbdev_probe' at ../drivers/baseband/la12xx/bbdev_la12xx.c:1075:9:../drivers/baseband/la12xx/bbdev_la12xx.c:901:9: error: pointer 'hp_info' may be used after 'rte_free' [-Werror=use-after-free]
901 | rte_free(hp);
| ^~~~~~~~~~~~../drivers/baseband/la12xx/bbdev_la12xx.c:791:17: note: call to 'rte_free' here
791 | rte_free(hp);
| ^~~~~~~~~~~~
--- drivers/baseband/la12xx/bbdev_la12xx.c | 1 + 1 file changed, 1 insertion(+)
drivers/baseband/la12xx/bbdev_la12xx.c +1 -0
@@ -789,6 +789,7 @@ setup_la12xx_dev(struct rte_bbdev *dev) ipc_priv->hugepg_start.size = hp->len;rte_free (hp); + hp =NULL ; } dev_ipc =open_ipc_dev (priv->modem_id);
Signature · 1 line
2.45.2
Re: [PATCH v3 15/18] baseband/la12xx: prevent use after free
Quoted 26 lines · Stephen Hemminger
It is possible that the info pointer (hp) could get freed twice. Fix by nulling after free.
In function '
setup_la12xx_dev',
inlined from 'la12xx_bbdev_create' at../drivers/baseband/la12xx/bbdev_la12xx.c:1029:8,
inlined from 'la12xx_bbdev_probe' at../drivers/baseband/la12xx/bbdev_la12xx.c:1075:9:../drivers/baseband/la12xx/bbdev_la12xx.c:901:9: error: pointer 'hp_info' may be used after 'rte_free' [-Werror=use-after-free]
901 |rte_free(hp);
| ^~~~~~~~~~~~../drivers/baseband/la12xx/bbdev_la12xx.c:791:17: note: call to 'rte_free' here
791 |rte_free(hp);
| ^~~~~~~~~~~~Fixes 24d0ba22546e ("baseband/la12xx: add queue and modem config")Signed-off-by Stephen Hemminger <[email protected]>--- drivers/baseband/la12xx/bbdev_la12xx.c | 1 + 1 file changed, 1 insertion(+)drivers/baseband/la12xx/bbdev_la12xx.c +0
-0
drivers/baseband/la12xx/bbdev_la12xx.c setup_la12xx_dev(struct rte_bbdev *dev) · line 795
Quoted 7 lines
drivers/baseband/la12xx/bbdev_la12xx.c +1
-0@@ -789,6 +789,7 @@ setup_la12xx_dev(struct rte_bbdev *dev) ipc_priv->hugepg_start.size = hp->len;rte_free (hp); + hp =NULL ; } dev_ipc =open_ipc_dev (priv->modem_id);
PATCH v3 16/18 common/ipdf: fix use after free due
In reply to Stephen HemmingerThe macro in this driver was redefining LIST_FOR_EACH_ENTRY_SAFE as a simple LIST_FOR_EACH macro. But they are not the same the _SAFE variant guarantees that there will not be use after free.
One other place was using LIST_FOR_EACH_ENTRY_SAFE() incorrectly.
--- drivers/common/idpf/base/idpf_osdep.h | 10 ++++++++-- drivers/common/idpf/idpf_common_device.c | 3 +-- 2 files changed, 9 insertions(+), 4 deletions(-)
drivers/common/idpf/base/idpf_osdep.h +8 -2
@@ -341,10 +341,16 @@ idpf_hweight32(u32 num)#define LIST_ENTRY_TYPE (type) LIST_ENTRY(type)#endif +#ifndef LIST_FOREACH_SAFE +#define LIST_FOREACH_SAFE (var, head, field, tvar) \ + for ((var) = LIST_FIRST((head)); \ + (var) && ((tvar) = LIST_NEXT((var), field), 1); \ + (var) = (tvar)) +#endif +#ifndef LIST_FOR_EACH_ENTRY_SAFE #define LIST_FOR_EACH_ENTRY_SAFE (pos, temp, head, entry_type, list) \ - LIST_FOREACH(pos, head, list) - + LIST_FOREACH_SAFE(pos, head, list, temp)#endif #ifndef LIST_FOR_EACH_ENTRY
drivers/common/idpf/idpf_common_device.c +1 -2
@@ -136,8 +136,7 @@ idpf_init_mbx(struct idpf_hw *hw)if (ret !=0 )return ret; -LIST_FOR_EACH_ENTRY_SAFE (ctlq,NULL , &hw->cq_list_head, - struct idpf_ctlq_info, cq_list) { + LIST_FOR_EACH_ENTRY(ctlq, &hw->cq_list_head, struct idpf_ctlq_info, cq_list) {if (ctlq->q_id ==IDPF_CTLQ_ID && ctlq->cq_type ==IDPF_CTLQ_TYPE_MAILBOX_TX ) hw->asq = ctlq;
Signature · 1 line
2.45.2
PATCH v3 17/18 eal: add function attributes for allocation functions
In reply to Stephen HemmingerThe allocation functions take a alignment argument that can be useful to hint the compiler optimizer.
This is supported by Gcc and Clang but only useful with Gcc because Clang gives warning if alignment is 0.
Recent versions of GCC have a malloc attribute that can be used to find mismatches between allocation and free; the typical problem caught is a pointer allocated with rte_malloc() that is then incorrectly freed using free().
--- lib/eal/include/rte_common.h | 30 ++++++++++++++++++++++++++++++ lib/eal/include/rte_malloc.h | 24 ++++++++++++++++-------- 2 files changed, 46 insertions(+), 8 deletions(-)
lib/eal/include/rte_common.h +30 -0
@@ -228,6 +228,36 @@ typedef uint16_t unaligned_uint16_t;#define __rte_alloc_size (...)#endif +/** +* Tells the compiler that the function returns a value that points to +* memory aligned by a function argument. +* Not enabled on clang because it warns if align argument is zero. +*/ +#if defined(RTE_CC_GCC ) +#define __rte_alloc_align (align_arg) \ + __attribute__((alloc_align(align_arg))) +#else +#define __rte_alloc_align (...) +#endif + +/** +* Tells the compiler this is a function like malloc and that the pointer +* returned cannot alias any other pointer (ie new memory). +* +* Also, with recent GCC versions also able to track that proper +* dealloctor function is used for this pointer. +*/ +#if defined(RTE_TOOLCHAIN_GCC ) && (GCC_VERSION >=110000 ) +#define __rte_alloc_func (...) \ + __attribute__((malloc, malloc(__VA_ARGS__))) + +#elif defined(RTE_CC_GCC ) || defined(RTE_CC_CLANG ) +#define __rte_alloc_func (...) \ + __attribute__((malloc)) +#else +#define __rte_alloc_func (...) +#endif +#define RTE_PRIORITY_LOG 101#define RTE_PRIORITY_BUS 110#define RTE_PRIORITY_CLASS 120
lib/eal/include/rte_malloc.h +16 -8
@@ -54,7 +54,8 @@ struct rte_malloc_socket_stats {*/ void *rte_malloc (const char *type,size_t size,unsigned align) -__rte_alloc_size (2 ); +__rte_alloc_size (2 ) +__rte_alloc_align (3 );/** * Allocate zeroed memory from the heap.
@@ -81,7 +82,8 @@ rte_malloc(const char *type, size_t size, unsigned align)*/ void *rte_zmalloc (const char *type,size_t size,unsigned align) -__rte_alloc_size (2 ); +__rte_alloc_size (2 ) +__rte_alloc_align (3 );/** * Replacement function for calloc(), using huge-page memory. Memory area is
@@ -108,7 +110,8 @@ rte_zmalloc(const char *type, size_t size, unsigned align)*/ void *rte_calloc (const char *type,size_t num,size_t size,unsigned align) -__rte_alloc_size (2 ,3 ); +__rte_alloc_size (2 ,3 ) +__rte_alloc_align (4 );/** * Replacement function for realloc(), using huge-page memory. Reserved area
@@ -132,7 +135,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)*/ void *rte_realloc (void *ptr,size_t size,unsigned int align) -__rte_alloc_size (2 ); +__rte_alloc_size (2 ) +__rte_alloc_align (3 );/** * Replacement function for realloc(), using huge-page memory. Reserved area
@@ -158,7 +162,8 @@ rte_realloc(void *ptr, size_t size, unsigned int align)*/ void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) -__rte_alloc_size (2 ); +__rte_alloc_size (2 ) +__rte_alloc_align (3 );/** * This function allocates memory from the huge-page area of memory. The memory
@@ -185,7 +190,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)*/ void *rte_malloc_socket (const char *type,size_t size,unsigned align,int socket) -__rte_alloc_size (2 ); +__rte_alloc_size (2 ) +__rte_alloc_align (3 );/** * Allocate zeroed memory from the heap.
@@ -214,7 +220,8 @@ rte_malloc_socket(const char *type, size_t size, unsigned align, int socket)*/ void *rte_zmalloc_socket (const char *type,size_t size,unsigned align,int socket) -__rte_alloc_size (2 ); +__rte_alloc_size (2 ) +__rte_alloc_align (3 );/** * Replacement function for calloc(), using huge-page memory. Memory area is
@@ -243,7 +250,8 @@ rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)*/ void *rte_calloc_socket (const char *type,size_t num,size_t size,unsigned align,int socket) -__rte_alloc_size (2 ,3 ); +__rte_alloc_size (2 ,3 ) +__rte_alloc_align (4 );/** * Frees the memory space pointed to by the provided pointer.
Signature · 1 line
2.45.2
Re: [PATCH v3 17/18] eal: add function attributes for allocation functions
Quoted 12 lines · Stephen Hemminger
The allocation functions take a alignment argument that can be useful to hint the compiler optimizer.
This is supported by Gcc and Clang but only useful with Gcc because Clang gives warning if alignment is 0.
Recent versions of GCC have a malloc attribute that can be used to find mismatches between allocation and free; the typical problem caught is a pointer allocated with
rte_malloc()that is then incorrectly freed usingfree().Signed-off-by Stephen Hemminger <[email protected]>
RE: [PATCH v3 17/18] eal: add function attributes for allocation functions
In reply to Stephen HemmingerQuoted 8 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Sunday, 29 September 2024 17.35The allocation functions take a alignment argument that can be useful to hint the compiler optimizer.
This is supported by Gcc and Clang but only useful with Gcc because Clang gives warning if alignment is 0.
This patch defines and uses __rte_alloc_align(). OK.
Quoted 5 lines
Recent versions of GCC have a malloc attribute that can be used to find mismatches between allocation and free; the typical problem caught is a pointer allocated with
rte_malloc()that is then incorrectly freed usingfree().
This patch defines __rte_alloc_func(), but uses it in the next patch in the series. Suggest either doing both here, or move the definition of __rte_alloc_func() to the next patch.
Quoted 4 lines
+/**
+ * Tells the compiler this is a function like malloc and that the pointer
+ * returned cannot alias any other pointer (ie new memory).
There's a good example of its use here:
https://developers.redhat.com/blog/2021/04/30/detecting-memory-management-bugs-with-gcc-11-part-1-understanding-dynamic-allocation#detecting_mismatched_deallocations
It not only refers to memory, but also handle pointers.
You might want to replace "ie new memory" by "ie new object" or similar.
Please add the optional arguments to pass to __rte_alloc_func to the macro description, e.g.:
@param [free_func]
The name of the deallocation function to free the allocated object
@param [free_func_ptr_index]
The deallocation function's argument index of the object pointer.
PS: The brackets indicate that the parameter is optional. I didn't know, so it is what I found on the internet.
Quoted 4 lines
+ *
+ * Also, with recent GCC versions also able to track that proper
+ * dealloctor function is used for this pointer.
+ */
lib/eal/include/rte_common.h typedef uint16_t unaligned_uint16_t; · line 259
Quoted 10 lines
lib/eal/include/rte_common.h +10
-0+#if defined(RTE_TOOLCHAIN_GCC ) && (GCC_VERSION >=110000 ) +#define __rte_alloc_func (...) \ + __attribute__((malloc, malloc(__VA_ARGS__))) + +#elif defined(RTE_CC_GCC ) || defined(RTE_CC_CLANG ) +#define __rte_alloc_func (...) \ + __attribute__((malloc)) +#else +#define __rte_alloc_func (...) +#endif
The _func postfix seems superfluous. Macros hinting about Hot and cold functions are simply __rte_hot and __rte_cold, without _func postfix. It's probably a matter of taste, so I'll leave it up to you.
Minor detail:
When looking at the code using the macro, it seems somewhat confusing that the macro name is "__rte_alloc" when its arguments describe the associated free function.
But I have no ideas for a better name...
Even if the two arguments were required, the primary purpose of the macro is to inform the compiler that the function is an allocation function; so that must be dominant in the name of the macro, which it is with the current name.
With the macro description updated,
Re: [PATCH v3 17/18] eal: add function attributes for allocation functions
Quoted 33 lines · On Tue, 8 Oct 2024 10:29:23 +0200 Morten Brørup <[email protected]>…
Quoted 8 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Sunday, 29 September 2024 17.35The allocation functions take a alignment argument that can be useful to hint the compiler optimizer.
This is supported by Gcc and Clang but only useful with Gcc because Clang gives warning if alignment is 0.
This patch defines and uses
__rte_alloc_align(). OK.Quoted 5 lines
Recent versions of GCC have a malloc attribute that can be used to find mismatches between allocation and free; the typical problem caught is a pointer allocated with
rte_malloc()that is then incorrectly freed usingfree().This patch defines
__rte_alloc_func(), but uses it in the next patch in the series. Suggest either doing both here, or move the definition of__rte_alloc_func()to the next patch.Quoted 4 lines
+/**
+ * Tells the compiler this is a function like malloc and that the pointer
+ * returned cannot alias any other pointer (ie new memory).There's a good example of its use here:
https://developers.redhat.com/blog/2021/04/30/detecting-memory-management-bugs-with-gcc-11-part-1-understanding-dynamic-allocation#detecting_mismatched_deallocationsIt not only refers to memory, but also handle pointers.
You might want to replace "ie new memory" by "ie new object" or similar.
Quoted 7 lines
Please add the optional arguments to pass to
__rte_alloc_functo the macro description, e.g.:
@param [free_func]
The name of the deallocation function to free the allocated object
@param [free_func_ptr_index]
The deallocation function's argument index of the object pointer.PS: The brackets indicate that the parameter is optional. I didn't know, so it is what I found on the internet.
In later versions of the patch decided that naming and parameters should follow the precedent used in glibc. See new version sent today.
PATCH v3 18/18 eal: add alloc_function attribute to rte_malloc
In reply to Stephen HemmingerUse the GCC function attribute to detect cases where memory is allocated with rte_malloc and freed incorrectly with libc version of free (and vice versa). Also will detect some other pointer mismatches.
--- doc/guides/rel_notes/release_24_11.rst | 8 ++++ lib/eal/include/rte_malloc.h | 55 +++++++++++++++----------- 2 files changed, 40 insertions(+), 23 deletions(-)
doc/guides/rel_notes/release_24_11.rst +8 -0
@@ -55,6 +55,14 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* **Hardened rte_malloc and related functions.** + + * Added function attributes to ``rte_malloc`` and similar functions + that can catch some obvious bugs at compile time (with GCC 11.0 or later). + Examples: calling ``free()`` on pointer that was allocated with ``rte_malloc`` + (and vice versa); freeing the same pointer twice in the same routine; + freeing an object that was not created by allocation; etc. + Removed Items -------------
lib/eal/include/rte_malloc.h +32 -23
109 lines not on this page · raw message
Signature · 1 line
2.45.2
Re: [PATCH v3 18/18] eal: add alloc_function attribute to rte_malloc
Quoted 6 lines · Stephen Hemminger
Use the GCC function attribute to detect cases where memory is allocated with
rte_mallocand freed incorrectly with libc version of free (and vice versa). Also will detect some other pointer mismatches.Signed-off-by Stephen Hemminger <[email protected]>
v4 cover Fix allocation bugs and hardening for rte_malloc
In reply to Stephen HemmingerRecent versions of GCC have some additional function attributes that can help with DPDK performance and stability.
The alloc_align attribute tells the compiler what the alignment of the allocation will be, and the optimizer can use this to produce better code (especially memcpy and structure copies).
The malloc attribute tells compiler that object is not overlapping and potentially aliasing. It also as an additional variant in GCC 11 or later that allows for detecting all sorts of common errors like calling free() on memory allocated with rte_malloc().
In order to use the malloc attribute the free function prototype needs to be moved before the allocator/create function prototype so that the malloc attribute can refer to it.
This uncovered at least 16 pre-existing bugs in DPDK, these should go to stable.
This patch set is structured with:
- fix any new warnings that were discovered
- add macros for enable the macros
- enable the attributes
The same attributes could be added to lots more functions in DPDK, but this patch set focuses on the key ones, and where problems exist in current code base.
v4 - rename the malloc attributes to align with what glibc uses in cdefs.h
combine the attribute and rte_malloc patches
Stephen Hemminger (17):
memzone: fix use after free in tracing cryptodev/bcmfs: fix mis-matched free dma/ixd: fix incorrect free function in cleanup event/cnxk: fix pointer mismatch in cleanup examples/vhost: fix free function mismatch net/cnxk: fix use-after-free
bpf: fix free mismatch if convert fails net/e1000: fix use-after-free
net/sfc: fix use-after-free warning messages net/cpfl: fix free of nonheap object net/nfp: fix duplicate call to rte_free raw/ifpga/base: fix use after free common/qat: fix use after free drivers/ifpga: fix free function mismatch baseband/la12xx: prevent use after free common/idpf: fix use after free due
eal: add function attributes for allocation functions
doc/guides/rel_notes/release_24_11.rst | 8 ++++ drivers/baseband/la12xx/bbdev_la12xx.c | 1 + drivers/common/idpf/base/idpf_osdep.h | 10 ++++- drivers/common/idpf/idpf_common_device.c | 3 +- drivers/common/qat/qat_device.c | 6 +-- drivers/crypto/bcmfs/bcmfs_device.c | 4 +- drivers/dma/idxd/idxd_pci.c | 2 +- drivers/event/cnxk/cnxk_eventdev.c | 4 +- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- drivers/net/cpfl/cpfl_flow_parser.c | 1 - drivers/net/e1000/igb_ethdev.c | 4 +- drivers/net/nfp/flower/nfp_flower_flow.c | 1 - drivers/net/sfc/sfc_flow_rss.c | 4 +- drivers/net/sfc/sfc_mae.c | 23 ++++------ drivers/raw/ifpga/base/opae_intel_max10.c | 11 ++++- drivers/raw/ifpga/ifpga_rawdev.c | 8 ++-- examples/vhost_blk/vhost_blk.c | 2 +- lib/bpf/bpf_convert.c | 2 +- lib/eal/common/eal_common_memzone.c | 3 +- lib/eal/include/rte_common.h | 36 +++++++++++++++ lib/eal/include/rte_malloc.h | 55 +++++++++++++---------- 21 files changed, 124 insertions(+), 66 deletions(-)
Signature · 1 line
2.45.2
PATCH v4 1/17 memzone: fix use after free in tracing
Using the freed value for tracing is not a good idea. Although it is harmless for tracing, it will cause analyzers to flag this as unsafe.
--- lib/eal/common/eal_common_memzone.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/eal/common/eal_common_memzone.c +2 -1
@@ -331,9 +331,10 @@ rte_memzone_free(const struct rte_memzone *mz)rte_rwlock_write_unlock (&mcfg->mlock); +rte_eal_trace_memzone_free (name, addr, ret); +rte_free (addr); -rte_eal_trace_memzone_free (name, addr, ret);return ret; }
Signature · 1 line
2.45.2
Re: [PATCH v4 01/17] memzone: fix use after free in tracing
Quoted 16 lines · Stephen Hemminger
Using the freed value for tracing is not a good idea. Although it is harmless for tracing, it will cause analyzers to flag this as unsafe.
Signed-off-by Stephen Hemminger <[email protected]>Acked-by Chengwen Feng <[email protected]>--- lib/eal/common/eal_common_memzone.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)lib/eal/common/eal_common_memzone.c +0
-0@@ -331,9 +331,10 @@ rte_memzone_free(const struct rte_memzone *mz)
lib/eal/common/eal_common_memzone.c rte_memzone_free(const struct rte_memzone *mz) · line 340
Quoted 10 lines
lib/eal/common/eal_common_memzone.c +2
-1rte_rwlock_write_unlock (&mcfg->mlock); +rte_eal_trace_memzone_free (name, addr, ret); +rte_free (addr); -rte_eal_trace_memzone_free (name, addr, ret);return ret; }
Signature · 2 lines
Thanks, Anatoly
PATCH v4 2/17 cryptodev/bcmfs: fix mis-matched free
In reply to Stephen HemmingerThe device structure is allocated with rte_malloc() and then incorrectly freed with free(). This will lead to corrupt malloc pool.
Bugzilla ID: 1552
--- drivers/crypto/bcmfs/bcmfs_device.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/crypto/bcmfs/bcmfs_device.c +2 -2
@@ -139,7 +139,7 @@ fsdev_allocate_one_dev(struct rte_vdev_device *vdev,return fsdev; cleanup: -free (fsdev); +rte_free (fsdev);return NULL ; }
@@ -163,7 +163,7 @@ fsdev_release(struct bcmfs_device *fsdev)return ;TAILQ_REMOVE (&fsdev_list, fsdev, next); -free (fsdev); +rte_free (fsdev); } static int
Signature · 1 line
2.45.2
Re: [PATCH v4 02/17] cryptodev/bcmfs: fix mis-matched free
Quoted 10 lines · Stephen Hemminger
The device structure is allocated with
rte_malloc()and then incorrectly freed withfree(). This will lead to corrupt malloc pool.Bugzilla ID: 1552
Fixes c8e79da7c676 ("crypto/bcmfs: introduce BCMFS driver")Signed-off-by Stephen Hemminger <[email protected]>
Quoted 29 lines
--- drivers/crypto/bcmfs/bcmfs_device.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/crypto/bcmfs/bcmfs_device.c +2
-3@@ -139,7 +139,7 @@ fsdev_allocate_one_dev(struct rte_vdev_device *vdev,return fsdev; cleanup: -free (fsdev); +rte_free (fsdev);return NULL ; }@@ -163,7 +163,7 @@ fsdev_release(struct bcmfs_device *fsdev)return ;TAILQ_REMOVE (&fsdev_list, fsdev, next); -free (fsdev); +rte_free (fsdev); } static int --2.45.2
PATCH v4 3/17 dma/ixd: fix incorrect free function in cleanup
In reply to Stephen HemmingerThe data structure is allocated with rte_malloc and incorrectly freed in cleanup logic using free.
Bugzilla ID: 1549
--- drivers/dma/idxd/idxd_pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma/idxd/idxd_pci.c +1 -1
@@ -301,7 +301,7 @@ init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,return nb_wqs; err: -free (pci); +rte_free (pci);return err_code; }
Signature · 1 line
2.45.2
Re: [PATCH v4 03/17] dma/ixd: fix incorrect free function in cleanup
Quoted 9 lines · Stephen Hemminger
The data structure is allocated with
rte_mallocand incorrectly freed in cleanup logic using free.Bugzilla ID: 1549
Fixes 9449330a8458 ("dma/idxd: create dmadev instances on PCI probe")Signed-off-by Stephen Hemminger <[email protected]>
Quoted 20 lines
--- drivers/dma/idxd/idxd_pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/dma/idxd/idxd_pci.c +1
-1@@ -301,7 +301,7 @@ init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,return nb_wqs; err: -free (pci); +rte_free (pci);return err_code; }Signature · 1 line
2.45.2
PATCH v4 4/17 event/cnxk: fix pointer mismatch in cleanup
In reply to Stephen HemmingerThe code to cleanup in case of error was passing incorrect value to rte_free. The ports[] entry was allocated with rte_malloc and that should be used instead of the offset in that object.
--- drivers/event/cnxk/cnxk_eventdev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/event/cnxk/cnxk_eventdev.c +3 -1
@@ -121,8 +121,10 @@ cnxk_setup_event_ports(const struct rte_eventdev *event_dev,return 0 ; hws_fini:for (i = i -1 ; i >=0 ; i--) { +void *ws = event_dev->data->ports[i]; + event_dev->data->ports[i] =NULL ; -rte_free (cnxk_sso_hws_get_cookie (event_dev->data->ports[i])); +rte_free (ws); }return -ENOMEM ; }
Signature · 1 line
2.45.2
PATCH v4 5/17 examples/vhost: fix free function mismatch
In reply to Stephen HemmingerThe pointer bdev is allocated with rte_zmalloc() and then incorrectly freed with free() which will lead pool corruption.
Bugzilla ID: 1553
--- examples/vhost_blk/vhost_blk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
examples/vhost_blk/vhost_blk.c +1 -1
@@ -776,7 +776,7 @@ vhost_blk_bdev_construct(const char *bdev_name, bdev->data =rte_zmalloc (NULL , blk_cnt * blk_size,0 );if (!bdev->data) {fprintf (stderr,"No enough reserved huge memory for disk\n" ); -free (bdev); +rte_free (bdev);return NULL ; }
Signature · 1 line
2.45.2
PATCH v4 6/17 net/cnxk: fix use-after-free
In reply to Stephen HemmingerThe driver would refer to the mempool object after it was freed.
Bugzilla ID: 1554
--- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/cnxk/cnxk_ethdev_sec.c +1 -1
@@ -136,8 +136,8 @@ cnxk_nix_inl_custom_meta_pool_cb(uintptr_t pmpool, uintptr_t *mpool, const charreturn -EINVAL ; } -rte_mempool_free (hp);plt_free (hp->pool_config); +rte_mempool_free (hp); *aura_handle =0 ; *mpool =0 ;
Signature · 1 line
2.45.2
PATCH v4 7/17 bpf: fix free mismatch if convert fails
In reply to Stephen HemmingerIf conversion of cBF to eBPF fails then an object allocated with rte_malloc() would be passed to free().
[908/3201] Compiling C object lib/librte_bpf.a.p/bpf_bpf_convert.c.o../lib/bpf/bpf_convert.c: In function ‘rte_bpf_convert’:../lib/bpf/bpf_convert.c:559:17: warning: ‘free’ called on pointer returned from a mismatched allocation function [-Wmismatched-dealloc]
559 | free(prm);
| ^~~~~~~~~
../lib/bpf/bpf_convert.c:545:15: note: returned from ‘rte_zmalloc’
545 | prm = rte_zmalloc("bpf_filter",
| ^~~~~~~~~~~~~~~~~~~~~~~~~
546 | sizeof(*prm) + ebpf_len * sizeof(*ebpf), 0);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~--- lib/bpf/bpf_convert.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/bpf/bpf_convert.c +1 -1
@@ -556,7 +556,7 @@ rte_bpf_convert(const struct bpf_program *prog) ret =bpf_convert_filter (prog->bf_insns, prog->bf_len, ebpf, &ebpf_len);if (ret <0 ) {RTE_BPF_LOG_LINE (ERR ,"%s: cannot convert cBPF to eBPF" , __func__); -free (prm); +rte_free (prm); rte_errno = -ret;return NULL ; }
Signature · 1 line
2.45.2
PATCH v4 8/17 net/e1000: fix use-after-free
In reply to Stephen HemmingerThe driver cleanup code was freeing the filter object then dereferencing it.
Bugzilla ID: 1550
--- drivers/net/e1000/igb_ethdev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/e1000/igb_ethdev.c +2 -2
@@ -3907,11 +3907,11 @@ igb_delete_2tuple_filter(struct rte_eth_dev *dev, filter_info->twotuple_mask &= ~(1 << filter->index);TAILQ_REMOVE (&filter_info->twotuple_list, filter, entries); -rte_free (filter);E1000_WRITE_REG (hw,E1000_TTQF (filter->index),E1000_TTQF_DISABLE_MASK );E1000_WRITE_REG (hw,E1000_IMIR (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIREXT (filter->index),0 ); +rte_free (filter);return 0 ; }
@@ -4348,7 +4348,6 @@ igb_delete_5tuple_filter_82576(struct rte_eth_dev *dev, filter_info->fivetuple_mask &= ~(1 << filter->index);TAILQ_REMOVE (&filter_info->fivetuple_list, filter, entries); -rte_free (filter);E1000_WRITE_REG (hw,E1000_FTQF (filter->index),E1000_FTQF_VF_BP |E1000_FTQF_MASK );
@@ -4357,6 +4356,7 @@ igb_delete_5tuple_filter_82576(struct rte_eth_dev *dev,E1000_WRITE_REG (hw,E1000_SPQF (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIR (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIREXT (filter->index),0 ); +rte_free (filter);return 0 ; }
Signature · 1 line
2.45.2
PATCH v4 9/17 net/sfc: fix use-after-free warning messages
In reply to Stephen HemmingerIf compiler detection of use-after-free is enabled then this drivers debug messages will cause warnings. Change to move debug message before the object is freed.
Bugzilla ID: 1551
--- drivers/net/sfc/sfc_flow_rss.c | 4 ++-- drivers/net/sfc/sfc_mae.c | 23 +++++++++-------------- 2 files changed, 11 insertions(+), 16 deletions(-)
drivers/net/sfc/sfc_flow_rss.c +2 -2
@@ -303,9 +303,9 @@ sfc_flow_rss_ctx_del(struct sfc_adapter *sa, struct sfc_flow_rss_ctx *ctx)TAILQ_REMOVE (&flow_rss->ctx_list, ctx, entries);rte_free (ctx->qid_offsets); -rte_free (ctx); -sfc_dbg (sa,"flow-rss: deleted ctx=%p" , ctx); + +rte_free (ctx); } static int
drivers/net/sfc/sfc_mae.c +9 -14
@@ -400,9 +400,8 @@ sfc_mae_outer_rule_del(struct sfc_adapter *sa,efx_mae_match_spec_fini (sa->nic, rule->match_spec);TAILQ_REMOVE (&mae->outer_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted outer_rule=%p" , rule); +rte_free (rule); } static int
@@ -585,9 +584,8 @@ sfc_mae_mac_addr_del(struct sfc_adapter *sa, struct sfc_mae_mac_addr *mac_addr) }TAILQ_REMOVE (&mae->mac_addrs, mac_addr, entries); -rte_free (mac_addr); -sfc_dbg (sa,"deleted mac_addr=%p" , mac_addr); +rte_free (mac_addr); }enum sfc_mae_mac_addr_type {
@@ -785,10 +783,10 @@ sfc_mae_encap_header_del(struct sfc_adapter *sa, }TAILQ_REMOVE (&mae->encap_headers, encap_header, entries); +sfc_dbg (sa,"deleted encap_header=%p" , encap_header); +rte_free (encap_header->buf);rte_free (encap_header); - -sfc_dbg (sa,"deleted encap_header=%p" , encap_header); } static int
@@ -983,9 +981,8 @@ sfc_mae_counter_del(struct sfc_adapter *sa, struct sfc_mae_counter *counter) }TAILQ_REMOVE (&mae->counters, counter, entries); -rte_free (counter); -sfc_dbg (sa,"deleted counter=%p" , counter); +rte_free (counter); } static int
@@ -1165,9 +1162,8 @@ sfc_mae_action_set_del(struct sfc_adapter *sa,sfc_mae_mac_addr_del (sa, action_set->src_mac_addr);sfc_mae_counter_del (sa, action_set->counter);TAILQ_REMOVE (&mae->action_sets, action_set, entries); -rte_free (action_set); -sfc_dbg (sa,"deleted action_set=%p" , action_set); +rte_free (action_set); } static int
@@ -1401,10 +1397,10 @@ sfc_mae_action_set_list_del(struct sfc_adapter *sa,sfc_mae_action_set_del (sa, action_set_list->action_sets[i]);TAILQ_REMOVE (&mae->action_set_lists, action_set_list, entries); +sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); +rte_free (action_set_list->action_sets);rte_free (action_set_list); - -sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); } static int
@@ -1667,9 +1663,8 @@ sfc_mae_action_rule_del(struct sfc_adapter *sa,sfc_mae_outer_rule_del (sa, rule->outer_rule);TAILQ_REMOVE (&mae->action_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted action_rule=%p" , rule); +rte_free (rule); } static int
Signature · 1 line
2.45.2
PATCH v4 10/17 net/cpfl: fix free of nonheap object
In reply to Stephen HemmingerWith proper annotation, GCC discovers that this driver is calling rte_free() on an object that was not allocated (it is part of array in another object).
In function ‘cpfl_flow_js_mr_layout’,
inlined from ‘cpfl_flow_js_mr_action’ at ../drivers/net/cpfl/cpfl_flow_parser.c:848:9,
inlined from ‘cpfl_flow_js_mod_rule’ at ../drivers/net/cpfl/cpfl_flow_parser.c:908:9,
inlined from ‘cpfl_parser_init’ at ../drivers/net/cpfl/cpfl_flow_parser.c:932:8,
inlined from ‘cpfl_parser_create’ at ../drivers/net/cpfl/cpfl_flow_parser.c:959:8:../drivers/net/cpfl/cpfl_flow_parser.c:740:9: warning: ‘rte_free’ called on pointer ‘*parser.modifications’ with nonzero offset [28, 15479062120396] [-Wfree-nonheap-object]
740 | rte_free(js_mod->layout);
| ^~~~~~~~~~~~~~~~~~~~~~~~--- drivers/net/cpfl/cpfl_flow_parser.c | 1 - 1 file changed, 1 deletion(-)
drivers/net/cpfl/cpfl_flow_parser.c +0 -1
@@ -737,7 +737,6 @@ cpfl_flow_js_mr_layout(json_t *ob_layouts, struct cpfl_flow_js_mr_action_mod *jsreturn 0 ; err: -rte_free (js_mod->layout);return -EINVAL ; }
Signature · 1 line
2.45.2
PATCH v4 11/17 net/nfp: fix duplicate call to rte_free
In reply to Stephen HemmingerCalling rte_free twice on same object will corrupt the heap.
Warning is:
In function 'nfp_pre_tun_table_check_del',
inlined from 'nfp_flow_destroy' at ../drivers/net/nfp/flower/nfp_flower_flow.c:5143:9:../drivers/net/nfp/flower/nfp_flower_flow.c:3830:9: error: pointer 'entry' used after 'rte_free' [-Werror=use-after-free]
3830 | rte_free(entry);
| ^~~~~~~~~~~~~~~../drivers/net/nfp/flower/nfp_flower_flow.c:3825:9: note: call to 'rte_free' here
3825 | rte_free(entry);
| ^~~~~~~~~~~~~~~
Bugzilla ID: 1555
--- drivers/net/nfp/flower/nfp_flower_flow.c | 1 - 1 file changed, 1 deletion(-)
drivers/net/nfp/flower/nfp_flower_flow.c +0 -1
@@ -3822,7 +3822,6 @@ nfp_pre_tun_table_check_del(struct nfp_flower_representor *repr, goto free_entry; } -rte_free (entry);rte_free (find_entry); priv->pre_tun_cnt--;
Signature · 1 line
2.45.2
PATCH v4 12/17 raw/ifpga/base: fix use after free
In reply to Stephen HemmingerThe TAILQ_FOREACH() macro would refer to info after it had been freed. Fix by introducing TAILQ_FOREACH_SAFE here.
--- drivers/raw/ifpga/base/opae_intel_max10.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/raw/ifpga/base/opae_intel_max10.c +9 -2
@@ -6,6 +6,13 @@#include <libfdt.h> #include "opae_osdep.h" +#ifndef TAILQ_FOREACH_SAFE +#define TAILQ_FOREACH_SAFE (var, head, field, tvar) \ + for ((var) = TAILQ_FIRST((head)); \ + (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) +#endif +int max10_sys_read (struct intel_max10_device *dev,unsigned int offset,unsigned int *val) {
@@ -746,9 +753,9 @@ static int fdt_get_named_reg(const void *fdt, int node, const char *name,static void max10_sensor_uinit (struct intel_max10_device *dev) { -struct opae_sensor_info *info; +struct opae_sensor_info *info, *next; - TAILQ_FOREACH(info, &dev->opae_sensor_list, node) { + TAILQ_FOREACH_SAFE(info, &dev->opae_sensor_list, node, next) {TAILQ_REMOVE (&dev->opae_sensor_list, info, node);opae_free (info); }
Signature · 1 line
2.45.2
PATCH v4 13/17 common/qat: fix use after free
In reply to Stephen HemmingerChecking return value of rte_memzone_free() is pointless and if it failed then it was because the pointer was null.
--- drivers/common/qat/qat_device.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/common/qat/qat_device.c +1 -5
@@ -390,11 +390,7 @@ qat_pci_device_allocate(struct rte_pci_device *pci_dev)return qat_dev; error:rte_free (qat_dev->command_line); -if (rte_memzone_free (qat_dev_mz)) { -QAT_LOG (DEBUG , -"QAT internal error! Trying to free already allocated memzone: %s" , - qat_dev_mz->name); - } +rte_memzone_free (qat_dev_mz);return NULL ; }
Signature · 1 line
2.45.2
PATCH v4 14/17 drivers/ifpga: fix free function mismatch
In reply to Stephen HemmingerThe raw ifpga driver redefines malloc to be opae_malloc and free to be opae_free; which is a bad idea.
This leads to case where interrupt efd array is allocated with calloc() and then passed to rte_free. The workaround is to allocate the array with rte_calloc() instead.
--- drivers/raw/ifpga/ifpga_rawdev.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/raw/ifpga/ifpga_rawdev.c +4 -4
@@ -1499,7 +1499,7 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, nb_intr =rte_intr_nb_intr_get (*intr_handle); - intr_efds =calloc (nb_intr,sizeof (int )); + intr_efds =rte_calloc ("ifpga_efds" , nb_intr,sizeof (int ),0 );if (!intr_efds)return -ENOMEM ;
@@ -1508,7 +1508,7 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, ret =opae_acc_set_irq (acc, vec_start, count, intr_efds);if (ret) { -free (intr_efds); +rte_free (intr_efds);return -EINVAL ; } }
@@ -1517,13 +1517,13 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, ret =rte_intr_callback_register (*intr_handle, handler, (void *)arg);if (ret) { -free (intr_efds); +rte_free (intr_efds);return -EINVAL ; }IFPGA_RAWDEV_PMD_INFO ("success register %s interrupt\n" , name); -free (intr_efds); +rte_free (intr_efds);return 0 ; }
Signature · 1 line
2.45.2
PATCH v4 15/17 baseband/la12xx: prevent use after free
In reply to Stephen HemmingerIt is possible that the info pointer (hp) could get freed twice. Fix by nulling after free.
In function 'setup_la12xx_dev',
inlined from 'la12xx_bbdev_create' at ../drivers/baseband/la12xx/bbdev_la12xx.c:1029:8,
inlined from 'la12xx_bbdev_probe' at ../drivers/baseband/la12xx/bbdev_la12xx.c:1075:9:../drivers/baseband/la12xx/bbdev_la12xx.c:901:9: error: pointer 'hp_info' may be used after 'rte_free' [-Werror=use-after-free]
901 | rte_free(hp);
| ^~~~~~~~~~~~../drivers/baseband/la12xx/bbdev_la12xx.c:791:17: note: call to 'rte_free' here
791 | rte_free(hp);
| ^~~~~~~~~~~~
--- drivers/baseband/la12xx/bbdev_la12xx.c | 1 + 1 file changed, 1 insertion(+)
drivers/baseband/la12xx/bbdev_la12xx.c +1 -0
@@ -789,6 +789,7 @@ setup_la12xx_dev(struct rte_bbdev *dev) ipc_priv->hugepg_start.size = hp->len;rte_free (hp); + hp =NULL ; } dev_ipc =open_ipc_dev (priv->modem_id);
Signature · 1 line
2.45.2
PATCH v4 16/17 common/idpf: fix use after free due
In reply to Stephen HemmingerThe macro in this driver was redefining LIST_FOR_EACH_ENTRY_SAFE as a simple LIST_FOR_EACH macro. But they are not the same the _SAFE variant guarantees that there will not be use after free.
--- drivers/common/idpf/base/idpf_osdep.h | 10 ++++++++-- drivers/common/idpf/idpf_common_device.c | 3 +-- 2 files changed, 9 insertions(+), 4 deletions(-)
drivers/common/idpf/base/idpf_osdep.h +8 -2
@@ -341,10 +341,16 @@ idpf_hweight32(u32 num)#define LIST_ENTRY_TYPE (type) LIST_ENTRY(type)#endif +#ifndef LIST_FOREACH_SAFE +#define LIST_FOREACH_SAFE (var, head, field, tvar) \ + for ((var) = LIST_FIRST((head)); \ + (var) && ((tvar) = LIST_NEXT((var), field), 1); \ + (var) = (tvar)) +#endif +#ifndef LIST_FOR_EACH_ENTRY_SAFE #define LIST_FOR_EACH_ENTRY_SAFE (pos, temp, head, entry_type, list) \ - LIST_FOREACH(pos, head, list) - + LIST_FOREACH_SAFE(pos, head, list, temp)#endif #ifndef LIST_FOR_EACH_ENTRY
drivers/common/idpf/idpf_common_device.c +1 -2
@@ -136,8 +136,7 @@ idpf_init_mbx(struct idpf_hw *hw)if (ret !=0 )return ret; -LIST_FOR_EACH_ENTRY_SAFE (ctlq,NULL , &hw->cq_list_head, - struct idpf_ctlq_info, cq_list) { + LIST_FOR_EACH_ENTRY(ctlq, &hw->cq_list_head, struct idpf_ctlq_info, cq_list) {if (ctlq->q_id ==IDPF_CTLQ_ID && ctlq->cq_type ==IDPF_CTLQ_TYPE_MAILBOX_TX ) hw->asq = ctlq;
Signature · 1 line
2.45.2
PATCH v4 17/17 eal: add function attributes for allocation functions
In reply to Stephen HemmingerThe allocation functions take a alignment argument that can be useful to hint the compiler optimizer.
This is supported by Gcc and Clang but only useful with Gcc because Clang gives warning if alignment is 0.
Recent versions of GCC have a malloc attribute that can be used to find mismatches between allocation and free; the typical problem caught is a pointer allocated with rte_malloc() that is then incorrectly freed using free().
--- doc/guides/rel_notes/release_24_11.rst | 8 ++++ lib/eal/include/rte_common.h | 36 +++++++++++++++++ lib/eal/include/rte_malloc.h | 55 +++++++++++++++----------- 3 files changed, 76 insertions(+), 23 deletions(-)
doc/guides/rel_notes/release_24_11.rst +8 -0
@@ -55,6 +55,14 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* **Hardened rte_malloc and related functions.** + + * Added function attributes to ``rte_malloc`` and similar functions + that can catch some obvious bugs at compile time (with GCC 11.0 or later). + Examples: calling ``free()`` on pointer that was allocated with ``rte_malloc`` + (and vice versa); freeing the same pointer twice in the same routine; + freeing an object that was not created by allocation; etc. + Removed Items -------------
lib/eal/include/rte_common.h +36 -0
@@ -228,6 +228,42 @@ typedef uint16_t unaligned_uint16_t;#define __rte_alloc_size (...)#endif +/** +* Tells the compiler that the function returns a value that points to +* memory aligned by a function argument. +* +* Note: not enabled on Clang because it warns if align argument is zero. +*/ +#if defined(RTE_CC_GCC ) +#define __rte_alloc_align (argno) \ + __attribute__((alloc_align(argno))) +#else +#define __rte_alloc_align (argno) +#endif + +/** +* Tells the compiler this is a function like malloc and that the pointer +* returned cannot alias any other pointer (ie new memory). +*/ +#if defined(RTE_CC_GCC ) || defined(RTE_CC_CLANG ) +#define __rte_malloc __attribute__((__malloc__)) +#else +#define __rte_malloc +#endif + +/** +* With recent GCC versions also able to track that proper +* dealloctor function is used for this pointer. +*/ +#if defined(RTE_TOOLCHAIN_GCC ) && (GCC_VERSION >=110000 ) +#define __rte_dealloc (dealloc, argno) \ + __attribute__((malloc(dealloc, argno))) +#define __rte_dealloc_free __rte_dealloc(rte_free, 1) +#else +#define __rte_dealloc (dealloc, argno) +#define __rte_dealloc_free +#endif +#define RTE_PRIORITY_LOG 101#define RTE_PRIORITY_BUS 110#define RTE_PRIORITY_CLASS 120
lib/eal/include/rte_malloc.h +32 -23
109 lines not on this page · raw message
Signature · 1 line
2.45.2
Re: [PATCH v4 17/17] eal: add function attributes for allocation functions
Quoted 14 lines · Stephen Hemminger
The allocation functions take a alignment argument that can be useful to hint the compiler optimizer.
This is supported by Gcc and Clang but only useful with Gcc because Clang gives warning if alignment is 0.
Recent versions of GCC have a malloc attribute that can be used to find mismatches between allocation and free; the typical problem caught is a pointer allocated with
rte_malloc()that is then incorrectly freed usingfree().Signed-off-by Stephen Hemminger <[email protected]>Acked-by Chengwen Feng <[email protected]>---
<snip>
Quoted 7 lines
lib/eal/include/rte_malloc.h +6
-0+ +/** +* Frees the memory space pointed to by the provided pointer. +* +* This pointer must have been returned by a previous call to +* rte_malloc(), rte_zmalloc(), rte_calloc() or rte_realloc(). The behaviour of
lib/eal/include/rte_malloc.h struct rte_malloc_socket_stats { · line 49
Quoted 10 lines
lib/eal/include/rte_malloc.h +10
-0+* 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. +*/ +void +rte_free (void *ptr); +
Is there any particular reason why rte_free was moved?
Otherwise,
Signature · 2 lines
Thanks, Anatoly
Re: [PATCH v4 17/17] eal: add function attributes for allocation functions
Quoted 19 lines · Anatoly
Quoted 6 lines
lib/eal/include/rte_malloc.h +6
-0+ +/** +* Frees the memory space pointed to by the provided pointer. +* +* This pointer must have been returned by a previous call to +* rte_malloc(), rte_zmalloc(), rte_calloc() or rte_realloc(). The behaviour of lib/eal/include/rte_malloc.h struct rte_malloc_socket_stats { · line 49
Quoted 10 lines
lib/eal/include/rte_malloc.h +10
-0+* 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. +*/ +void +rte_free (void *ptr); +Is there any particular reason why
rte_freewas moved?
Quoted 3 lines
Otherwise,
Acked-by Anatoly Burakov <[email protected]>
I guess this is for the added annotation which points at rte_free symbol. A forward declaration would be another option.
Signature · 1 line
David Marchand
Re: [PATCH v4 17/17] eal: add function attributes for allocation functions
Quoted 21 lines · On Tue, 1 Oct 2024 14:25:46 +0200 David Marchand <[email protected]>…
Quoted 19 lines · Anatoly
Quoted 6 lines
lib/eal/include/rte_malloc.h +6
-0+ +/** +* Frees the memory space pointed to by the provided pointer. +* +* This pointer must have been returned by a previous call to +* rte_malloc(), rte_zmalloc(), rte_calloc() or rte_realloc(). The behaviour of lib/eal/include/rte_malloc.h struct rte_malloc_socket_stats { · line 49
Quoted 10 lines
lib/eal/include/rte_malloc.h +10
-0+* 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. +*/ +void +rte_free (void *ptr); +Is there any particular reason why
rte_freewas moved?
Quoted 6 lines
Quoted 3 lines
Otherwise,
Acked-by Anatoly Burakov <[email protected]>I guess this is for the added annotation which points at
rte_freesymbol. A forward declaration would be another option.
Right, compiler now needs to know about the free function. Moving it was the clean solution and avoids duplication.
If rte_free prototype is not moved...
In file included from ../lib/eal/include/rte_string_fns.h:22,
from ../lib/eal/common/eal_common_config.c:5:
../lib/eal/include/rte_common.h:261:42: error: ‘rte_free’ undeclared here (not in a function)
261 | #define __rte_dealloc_free __rte_dealloc(rte_free, 1)
| ^~~~~~~~../lib/eal/include/rte_common.h:260:31: note: in definition of macro ‘__rte_dealloc’
260 | __attribute__((malloc(dealloc, argno)))
| ^~~~~~~../lib/eal/include/rte_malloc.h:58:22: note: in expansion of macro ‘__rte_dealloc_free’
58 | __rte_malloc __rte_dealloc_free;
| ^~~~~~~~~~~~~~~~~~[16/3024] Compiling C object lib/librte_telemetry.a.p/telemetry_telemetry.c.o
Re: [PATCH v4 17/17] eal: add function attributes for allocation functions
Quoted 45 lines · Stephen Hemminger
Quoted 21 lines · On Tue, 1 Oct 2024 14:25:46 +0200 David Marchand <[email protected]>…
Quoted 19 lines · Anatoly
Quoted 6 lines
lib/eal/include/rte_malloc.h +6
-0+ +/** +* Frees the memory space pointed to by the provided pointer. +* +* This pointer must have been returned by a previous call to +* rte_malloc(), rte_zmalloc(), rte_calloc() or rte_realloc(). The behaviour of lib/eal/include/rte_malloc.h struct rte_malloc_socket_stats { · line 49
Quoted 10 lines
lib/eal/include/rte_malloc.h +10
-0+* 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. +*/ +void +rte_free (void *ptr); +Is there any particular reason why
rte_freewas moved?Quoted 6 lines
Quoted 3 lines
Otherwise,
Acked-by Anatoly Burakov <[email protected]>I guess this is for the added annotation which points at
rte_freesymbol. A forward declaration would be another option.Right, compiler now needs to know about the free function. Moving it was the clean solution and avoids duplication.
If
rte_freeprototype is not moved...In file included from
../lib/eal/include/rte_string_fns.h:22,from ../lib/eal/common/eal_common_config.c:5:
../lib/eal/include/rte_common.h:261:42: error: ‘rte_free’ undeclared here (not in a function)
261 | #define__rte_dealloc_free__rte_dealloc(rte_free, 1)
| ^~~~~~~~../lib/eal/include/rte_common.h:260:31: note: in definition of macro ‘__rte_dealloc’
260 |__attribute__((malloc(dealloc, argno)))
| ^~~~~~~../lib/eal/include/rte_malloc.h:58:22: note: in expansion of macro ‘__rte_dealloc_free’
Quoted 3 lines
58 | __rte_malloc __rte_dealloc_free; | ^~~~~~~~~~~~~~~~~~[16/3024] Compiling C object
lib/librte_telemetry.a.p/telemetry_telemetry.c.o
Makes sense, let's move the function then.
Signature · 2 lines
Thanks, Anatoly
v5 cover Fix allocation related bugs and add attributes
In reply to Stephen HemmingerRecent versions of GCC have some additional function attributes that can help with DPDK performance and stability.
The alloc_align attribute tells the compiler what the alignment of the allocation will be, and the optimizer can use this to produce better code (especially memcpy and structure copies).
The malloc attribute tells compiler that object is not overlapping and potentially aliasing. It also as an additional variant in GCC 11 or later that allows for detecting all sorts of common errors like calling free() on memory allocated with rte_malloc().
In order to use the malloc attribute the free function prototype needs to be moved before the allocator/create function prototype so that the malloc attribute can refer to it.
This uncovered at least 16 pre-existing bugs in DPDK, these should go to stable. This patch set is structured with:
- fix any new warnings that were discovered
- add macros for enable the macros
- enable the attributes
The same attributes could be added to lots more functions in DPDK, but this patch set focuses on the key ones, and where problems exist in current code base.
v5 - minor touchup to the function attribute wrappers
Stephen Hemminger (17):
memzone: fix use after free in tracing cryptodev/bcmfs: fix mis-matched free dma/ixd: fix incorrect free function in cleanup event/cnxk: fix pointer mismatch in cleanup examples/vhost: fix free function mismatch net/cnxk: fix use-after-free
bpf: fix free mismatch if convert fails net/e1000: fix use-after-free
net/sfc: fix use-after-free warning messages net/cpfl: fix free of nonheap object net/nfp: fix duplicate call to rte_free raw/ifpga/base: fix use after free common/qat: fix use after free drivers/ifpga: fix free function mismatch baseband/la12xx: prevent use after free common/idpf: fix use after free due
eal: add function attributes for allocation functions
doc/guides/rel_notes/release_24_11.rst | 8 ++++ drivers/baseband/la12xx/bbdev_la12xx.c | 1 + drivers/common/idpf/base/idpf_osdep.h | 10 ++++- drivers/common/idpf/idpf_common_device.c | 3 +- drivers/common/qat/qat_device.c | 6 +-- drivers/crypto/bcmfs/bcmfs_device.c | 4 +- drivers/dma/idxd/idxd_pci.c | 2 +- drivers/event/cnxk/cnxk_eventdev.c | 4 +- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- drivers/net/cpfl/cpfl_flow_parser.c | 1 - drivers/net/e1000/igb_ethdev.c | 4 +- drivers/net/nfp/flower/nfp_flower_flow.c | 1 - drivers/net/sfc/sfc_flow_rss.c | 4 +- drivers/net/sfc/sfc_mae.c | 23 ++++------ drivers/raw/ifpga/base/opae_intel_max10.c | 11 ++++- drivers/raw/ifpga/ifpga_rawdev.c | 8 ++-- examples/vhost_blk/vhost_blk.c | 2 +- lib/bpf/bpf_convert.c | 2 +- lib/eal/common/eal_common_memzone.c | 3 +- lib/eal/include/rte_common.h | 34 ++++++++++++++ lib/eal/include/rte_malloc.h | 55 +++++++++++++---------- 21 files changed, 122 insertions(+), 66 deletions(-)
Signature · 1 line
2.45.2
PATCH v5 1/17 memzone: fix use after free in tracing
Using the freed value for tracing is not a good idea. Although it is harmless for tracing, it will cause analyzers to flag this as unsafe.
--- lib/eal/common/eal_common_memzone.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/eal/common/eal_common_memzone.c +2 -1
@@ -331,9 +331,10 @@ rte_memzone_free(const struct rte_memzone *mz)rte_rwlock_write_unlock (&mcfg->mlock); +rte_eal_trace_memzone_free (name, addr, ret); +rte_free (addr); -rte_eal_trace_memzone_free (name, addr, ret);return ret; }
Signature · 1 line
2.45.2
PATCH v5 2/17 cryptodev/bcmfs: fix mis-matched free
In reply to Stephen HemmingerThe device structure is allocated with rte_malloc() and then incorrectly freed with free(). This will lead to corrupt malloc pool.
Bugzilla ID: 1552
--- drivers/crypto/bcmfs/bcmfs_device.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/crypto/bcmfs/bcmfs_device.c +2 -2
@@ -139,7 +139,7 @@ fsdev_allocate_one_dev(struct rte_vdev_device *vdev,return fsdev; cleanup: -free (fsdev); +rte_free (fsdev);return NULL ; }
@@ -163,7 +163,7 @@ fsdev_release(struct bcmfs_device *fsdev)return ;TAILQ_REMOVE (&fsdev_list, fsdev, next); -free (fsdev); +rte_free (fsdev); } static int
Signature · 1 line
2.45.2
PATCH v5 3/17 dma/ixd: fix incorrect free function in cleanup
In reply to Stephen HemmingerThe data structure is allocated with rte_malloc and incorrectly freed in cleanup logic using free.
Bugzilla ID: 1549
--- drivers/dma/idxd/idxd_pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma/idxd/idxd_pci.c +1 -1
@@ -301,7 +301,7 @@ init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,return nb_wqs; err: -free (pci); +rte_free (pci);return err_code; }
Signature · 1 line
2.45.2
Re: [PATCH v5 03/17] dma/ixd: fix incorrect free function in cleanup
Quoted 9 lines · Stephen Hemminger
The data structure is allocated with
rte_mallocand incorrectly freed in cleanup logic using free.Bugzilla ID: 1549
Fixes 9449330a8458 ("dma/idxd: create dmadev instances on PCI probe")Signed-off-by Stephen Hemminger <[email protected]>
From previous revision:
PATCH v5 4/17 event/cnxk: fix pointer mismatch in cleanup
In reply to Stephen HemmingerThe code to cleanup in case of error was passing incorrect value to rte_free. The ports[] entry was allocated with rte_malloc and that should be used instead of the offset in that object.
--- drivers/event/cnxk/cnxk_eventdev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/event/cnxk/cnxk_eventdev.c +3 -1
@@ -121,8 +121,10 @@ cnxk_setup_event_ports(const struct rte_eventdev *event_dev,return 0 ; hws_fini:for (i = i -1 ; i >=0 ; i--) { +void *ws = event_dev->data->ports[i]; + event_dev->data->ports[i] =NULL ; -rte_free (cnxk_sso_hws_get_cookie (event_dev->data->ports[i])); +rte_free (ws); }return -ENOMEM ; }
Signature · 1 line
2.45.2
PATCH v5 5/17 examples/vhost: fix free function mismatch
In reply to Stephen HemmingerThe pointer bdev is allocated with rte_zmalloc() and then incorrectly freed with free() which will lead pool corruption.
Bugzilla ID: 1553
--- examples/vhost_blk/vhost_blk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
examples/vhost_blk/vhost_blk.c +1 -1
@@ -776,7 +776,7 @@ vhost_blk_bdev_construct(const char *bdev_name, bdev->data =rte_zmalloc (NULL , blk_cnt * blk_size,0 );if (!bdev->data) {fprintf (stderr,"No enough reserved huge memory for disk\n" ); -free (bdev); +rte_free (bdev);return NULL ; }
Signature · 1 line
2.45.2
PATCH v5 6/17 net/cnxk: fix use-after-free
In reply to Stephen HemmingerThe driver would refer to the mempool object after it was freed.
Bugzilla ID: 1554
--- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/cnxk/cnxk_ethdev_sec.c +1 -1
@@ -136,8 +136,8 @@ cnxk_nix_inl_custom_meta_pool_cb(uintptr_t pmpool, uintptr_t *mpool, const charreturn -EINVAL ; } -rte_mempool_free (hp);plt_free (hp->pool_config); +rte_mempool_free (hp); *aura_handle =0 ; *mpool =0 ;
Signature · 1 line
2.45.2
PATCH v5 7/17 bpf: fix free mismatch if convert fails
In reply to Stephen HemmingerIf conversion of cBF to eBPF fails then an object allocated with rte_malloc() would be passed to free().
[908/3201] Compiling C object lib/librte_bpf.a.p/bpf_bpf_convert.c.o../lib/bpf/bpf_convert.c: In function ‘rte_bpf_convert’:../lib/bpf/bpf_convert.c:559:17: warning: ‘free’ called on pointer returned from a mismatched allocation function [-Wmismatched-dealloc]
559 | free(prm);
| ^~~~~~~~~
../lib/bpf/bpf_convert.c:545:15: note: returned from ‘rte_zmalloc’
545 | prm = rte_zmalloc("bpf_filter",
| ^~~~~~~~~~~~~~~~~~~~~~~~~
546 | sizeof(*prm) + ebpf_len * sizeof(*ebpf), 0);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~--- lib/bpf/bpf_convert.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/bpf/bpf_convert.c +1 -1
@@ -556,7 +556,7 @@ rte_bpf_convert(const struct bpf_program *prog) ret =bpf_convert_filter (prog->bf_insns, prog->bf_len, ebpf, &ebpf_len);if (ret <0 ) {RTE_BPF_LOG_LINE (ERR ,"%s: cannot convert cBPF to eBPF" , __func__); -free (prm); +rte_free (prm); rte_errno = -ret;return NULL ; }
Signature · 1 line
2.45.2
PATCH v5 8/17 net/e1000: fix use-after-free
In reply to Stephen HemmingerThe driver cleanup code was freeing the filter object then dereferencing it.
Bugzilla ID: 1550
--- drivers/net/e1000/igb_ethdev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/e1000/igb_ethdev.c +2 -2
@@ -3907,11 +3907,11 @@ igb_delete_2tuple_filter(struct rte_eth_dev *dev, filter_info->twotuple_mask &= ~(1 << filter->index);TAILQ_REMOVE (&filter_info->twotuple_list, filter, entries); -rte_free (filter);E1000_WRITE_REG (hw,E1000_TTQF (filter->index),E1000_TTQF_DISABLE_MASK );E1000_WRITE_REG (hw,E1000_IMIR (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIREXT (filter->index),0 ); +rte_free (filter);return 0 ; }
@@ -4348,7 +4348,6 @@ igb_delete_5tuple_filter_82576(struct rte_eth_dev *dev, filter_info->fivetuple_mask &= ~(1 << filter->index);TAILQ_REMOVE (&filter_info->fivetuple_list, filter, entries); -rte_free (filter);E1000_WRITE_REG (hw,E1000_FTQF (filter->index),E1000_FTQF_VF_BP |E1000_FTQF_MASK );
@@ -4357,6 +4356,7 @@ igb_delete_5tuple_filter_82576(struct rte_eth_dev *dev,E1000_WRITE_REG (hw,E1000_SPQF (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIR (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIREXT (filter->index),0 ); +rte_free (filter);return 0 ; }
Signature · 1 line
2.45.2
PATCH v5 9/17 net/sfc: fix use-after-free warning messages
In reply to Stephen HemmingerIf compiler detection of use-after-free is enabled then this drivers debug messages will cause warnings. Change to move debug message before the object is freed.
Bugzilla ID: 1551
--- drivers/net/sfc/sfc_flow_rss.c | 4 ++-- drivers/net/sfc/sfc_mae.c | 23 +++++++++-------------- 2 files changed, 11 insertions(+), 16 deletions(-)
drivers/net/sfc/sfc_flow_rss.c +2 -2
@@ -303,9 +303,9 @@ sfc_flow_rss_ctx_del(struct sfc_adapter *sa, struct sfc_flow_rss_ctx *ctx)TAILQ_REMOVE (&flow_rss->ctx_list, ctx, entries);rte_free (ctx->qid_offsets); -rte_free (ctx); -sfc_dbg (sa,"flow-rss: deleted ctx=%p" , ctx); + +rte_free (ctx); } static int
drivers/net/sfc/sfc_mae.c +9 -14
@@ -400,9 +400,8 @@ sfc_mae_outer_rule_del(struct sfc_adapter *sa,efx_mae_match_spec_fini (sa->nic, rule->match_spec);TAILQ_REMOVE (&mae->outer_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted outer_rule=%p" , rule); +rte_free (rule); } static int
@@ -585,9 +584,8 @@ sfc_mae_mac_addr_del(struct sfc_adapter *sa, struct sfc_mae_mac_addr *mac_addr) }TAILQ_REMOVE (&mae->mac_addrs, mac_addr, entries); -rte_free (mac_addr); -sfc_dbg (sa,"deleted mac_addr=%p" , mac_addr); +rte_free (mac_addr); }enum sfc_mae_mac_addr_type {
@@ -785,10 +783,10 @@ sfc_mae_encap_header_del(struct sfc_adapter *sa, }TAILQ_REMOVE (&mae->encap_headers, encap_header, entries); +sfc_dbg (sa,"deleted encap_header=%p" , encap_header); +rte_free (encap_header->buf);rte_free (encap_header); - -sfc_dbg (sa,"deleted encap_header=%p" , encap_header); } static int
@@ -983,9 +981,8 @@ sfc_mae_counter_del(struct sfc_adapter *sa, struct sfc_mae_counter *counter) }TAILQ_REMOVE (&mae->counters, counter, entries); -rte_free (counter); -sfc_dbg (sa,"deleted counter=%p" , counter); +rte_free (counter); } static int
@@ -1165,9 +1162,8 @@ sfc_mae_action_set_del(struct sfc_adapter *sa,sfc_mae_mac_addr_del (sa, action_set->src_mac_addr);sfc_mae_counter_del (sa, action_set->counter);TAILQ_REMOVE (&mae->action_sets, action_set, entries); -rte_free (action_set); -sfc_dbg (sa,"deleted action_set=%p" , action_set); +rte_free (action_set); } static int
@@ -1401,10 +1397,10 @@ sfc_mae_action_set_list_del(struct sfc_adapter *sa,sfc_mae_action_set_del (sa, action_set_list->action_sets[i]);TAILQ_REMOVE (&mae->action_set_lists, action_set_list, entries); +sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); +rte_free (action_set_list->action_sets);rte_free (action_set_list); - -sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); } static int
@@ -1667,9 +1663,8 @@ sfc_mae_action_rule_del(struct sfc_adapter *sa,sfc_mae_outer_rule_del (sa, rule->outer_rule);TAILQ_REMOVE (&mae->action_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted action_rule=%p" , rule); +rte_free (rule); } static int
Signature · 1 line
2.45.2
PATCH v5 10/17 net/cpfl: fix free of nonheap object
In reply to Stephen HemmingerWith proper annotation, GCC discovers that this driver is calling rte_free() on an object that was not allocated (it is part of array in another object).
In function ‘cpfl_flow_js_mr_layout’,
inlined from ‘cpfl_flow_js_mr_action’ at ../drivers/net/cpfl/cpfl_flow_parser.c:848:9,
inlined from ‘cpfl_flow_js_mod_rule’ at ../drivers/net/cpfl/cpfl_flow_parser.c:908:9,
inlined from ‘cpfl_parser_init’ at ../drivers/net/cpfl/cpfl_flow_parser.c:932:8,
inlined from ‘cpfl_parser_create’ at ../drivers/net/cpfl/cpfl_flow_parser.c:959:8:../drivers/net/cpfl/cpfl_flow_parser.c:740:9: warning: ‘rte_free’ called on pointer ‘*parser.modifications’ with nonzero offset [28, 15479062120396] [-Wfree-nonheap-object]
740 | rte_free(js_mod->layout);
| ^~~~~~~~~~~~~~~~~~~~~~~~--- drivers/net/cpfl/cpfl_flow_parser.c | 1 - 1 file changed, 1 deletion(-)
drivers/net/cpfl/cpfl_flow_parser.c +0 -1
@@ -737,7 +737,6 @@ cpfl_flow_js_mr_layout(json_t *ob_layouts, struct cpfl_flow_js_mr_action_mod *jsreturn 0 ; err: -rte_free (js_mod->layout);return -EINVAL ; }
Signature · 1 line
2.45.2
PATCH v5 11/17 net/nfp: fix duplicate call to rte_free
In reply to Stephen HemmingerCalling rte_free twice on same object will corrupt the heap.
Warning is:
In function 'nfp_pre_tun_table_check_del',
inlined from 'nfp_flow_destroy' at ../drivers/net/nfp/flower/nfp_flower_flow.c:5143:9:../drivers/net/nfp/flower/nfp_flower_flow.c:3830:9: error: pointer 'entry' used after 'rte_free' [-Werror=use-after-free]
3830 | rte_free(entry);
| ^~~~~~~~~~~~~~~../drivers/net/nfp/flower/nfp_flower_flow.c:3825:9: note: call to 'rte_free' here
3825 | rte_free(entry);
| ^~~~~~~~~~~~~~~
Bugzilla ID: 1555
--- drivers/net/nfp/flower/nfp_flower_flow.c | 1 - 1 file changed, 1 deletion(-)
drivers/net/nfp/flower/nfp_flower_flow.c +0 -1
@@ -3822,7 +3822,6 @@ nfp_pre_tun_table_check_del(struct nfp_flower_representor *repr, goto free_entry; } -rte_free (entry);rte_free (find_entry); priv->pre_tun_cnt--;
Signature · 1 line
2.45.2
PATCH v5 12/17 raw/ifpga/base: fix use after free
In reply to Stephen HemmingerThe TAILQ_FOREACH() macro would refer to info after it had been freed. Fix by introducing TAILQ_FOREACH_SAFE here.
--- drivers/raw/ifpga/base/opae_intel_max10.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/raw/ifpga/base/opae_intel_max10.c +9 -2
@@ -6,6 +6,13 @@#include <libfdt.h> #include "opae_osdep.h" +#ifndef TAILQ_FOREACH_SAFE +#define TAILQ_FOREACH_SAFE (var, head, field, tvar) \ + for ((var) = TAILQ_FIRST((head)); \ + (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) +#endif +int max10_sys_read (struct intel_max10_device *dev,unsigned int offset,unsigned int *val) {
@@ -746,9 +753,9 @@ static int fdt_get_named_reg(const void *fdt, int node, const char *name,static void max10_sensor_uinit (struct intel_max10_device *dev) { -struct opae_sensor_info *info; +struct opae_sensor_info *info, *next; - TAILQ_FOREACH(info, &dev->opae_sensor_list, node) { + TAILQ_FOREACH_SAFE(info, &dev->opae_sensor_list, node, next) {TAILQ_REMOVE (&dev->opae_sensor_list, info, node);opae_free (info); }
Signature · 1 line
2.45.2
PATCH v5 13/17 common/qat: fix use after free
In reply to Stephen HemmingerChecking return value of rte_memzone_free() is pointless and if it failed then it was because the pointer was null.
--- drivers/common/qat/qat_device.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/common/qat/qat_device.c +1 -5
@@ -390,11 +390,7 @@ qat_pci_device_allocate(struct rte_pci_device *pci_dev)return qat_dev; error:rte_free (qat_dev->command_line); -if (rte_memzone_free (qat_dev_mz)) { -QAT_LOG (DEBUG , -"QAT internal error! Trying to free already allocated memzone: %s" , - qat_dev_mz->name); - } +rte_memzone_free (qat_dev_mz);return NULL ; }
Signature · 1 line
2.45.2
PATCH v5 14/17 drivers/ifpga: fix free function mismatch
In reply to Stephen HemmingerThe raw ifpga driver redefines malloc to be opae_malloc and free to be opae_free; which is a bad idea.
This leads to case where interrupt efd array is allocated with calloc() and then passed to rte_free. The workaround is to allocate the array with rte_calloc() instead.
--- drivers/raw/ifpga/ifpga_rawdev.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/raw/ifpga/ifpga_rawdev.c +4 -4
@@ -1499,7 +1499,7 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, nb_intr =rte_intr_nb_intr_get (*intr_handle); - intr_efds =calloc (nb_intr,sizeof (int )); + intr_efds =rte_calloc ("ifpga_efds" , nb_intr,sizeof (int ),0 );if (!intr_efds)return -ENOMEM ;
@@ -1508,7 +1508,7 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, ret =opae_acc_set_irq (acc, vec_start, count, intr_efds);if (ret) { -free (intr_efds); +rte_free (intr_efds);return -EINVAL ; } }
@@ -1517,13 +1517,13 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, ret =rte_intr_callback_register (*intr_handle, handler, (void *)arg);if (ret) { -free (intr_efds); +rte_free (intr_efds);return -EINVAL ; }IFPGA_RAWDEV_PMD_INFO ("success register %s interrupt\n" , name); -free (intr_efds); +rte_free (intr_efds);return 0 ; }
Signature · 1 line
2.45.2
PATCH v5 15/17 baseband/la12xx: prevent use after free
In reply to Stephen HemmingerIt is possible that the info pointer (hp) could get freed twice. Fix by nulling after free.
In function 'setup_la12xx_dev',
inlined from 'la12xx_bbdev_create' at ../drivers/baseband/la12xx/bbdev_la12xx.c:1029:8,
inlined from 'la12xx_bbdev_probe' at ../drivers/baseband/la12xx/bbdev_la12xx.c:1075:9:../drivers/baseband/la12xx/bbdev_la12xx.c:901:9: error: pointer 'hp_info' may be used after 'rte_free' [-Werror=use-after-free]
901 | rte_free(hp);
| ^~~~~~~~~~~~../drivers/baseband/la12xx/bbdev_la12xx.c:791:17: note: call to 'rte_free' here
791 | rte_free(hp);
| ^~~~~~~~~~~~
--- drivers/baseband/la12xx/bbdev_la12xx.c | 1 + 1 file changed, 1 insertion(+)
drivers/baseband/la12xx/bbdev_la12xx.c +1 -0
@@ -789,6 +789,7 @@ setup_la12xx_dev(struct rte_bbdev *dev) ipc_priv->hugepg_start.size = hp->len;rte_free (hp); + hp =NULL ; } dev_ipc =open_ipc_dev (priv->modem_id);
Signature · 1 line
2.45.2
PATCH v5 16/17 common/idpf: fix use after free due
In reply to Stephen HemmingerThe macro in this driver was redefining LIST_FOR_EACH_ENTRY_SAFE as a simple LIST_FOR_EACH macro. But they are not the same the _SAFE variant guarantees that there will not be use after free.
--- drivers/common/idpf/base/idpf_osdep.h | 10 ++++++++-- drivers/common/idpf/idpf_common_device.c | 3 +-- 2 files changed, 9 insertions(+), 4 deletions(-)
drivers/common/idpf/base/idpf_osdep.h +8 -2
@@ -341,10 +341,16 @@ idpf_hweight32(u32 num)#define LIST_ENTRY_TYPE (type) LIST_ENTRY(type)#endif +#ifndef LIST_FOREACH_SAFE +#define LIST_FOREACH_SAFE (var, head, field, tvar) \ + for ((var) = LIST_FIRST((head)); \ + (var) && ((tvar) = LIST_NEXT((var), field), 1); \ + (var) = (tvar)) +#endif +#ifndef LIST_FOR_EACH_ENTRY_SAFE #define LIST_FOR_EACH_ENTRY_SAFE (pos, temp, head, entry_type, list) \ - LIST_FOREACH(pos, head, list) - + LIST_FOREACH_SAFE(pos, head, list, temp)#endif #ifndef LIST_FOR_EACH_ENTRY
drivers/common/idpf/idpf_common_device.c +1 -2
@@ -136,8 +136,7 @@ idpf_init_mbx(struct idpf_hw *hw)if (ret !=0 )return ret; -LIST_FOR_EACH_ENTRY_SAFE (ctlq,NULL , &hw->cq_list_head, - struct idpf_ctlq_info, cq_list) { + LIST_FOR_EACH_ENTRY(ctlq, &hw->cq_list_head, struct idpf_ctlq_info, cq_list) {if (ctlq->q_id ==IDPF_CTLQ_ID && ctlq->cq_type ==IDPF_CTLQ_TYPE_MAILBOX_TX ) hw->asq = ctlq;
Signature · 1 line
2.45.2
PATCH v5 17/17 eal: add function attributes for allocation functions
In reply to Stephen HemmingerThe allocation functions take a alignment argument that can be useful to hint the compiler optimizer. This is supported by GCC and Clang but only useful with GCC because Clang gives warning if alignment is 0.
Newer versions of GCC have a malloc attribute that can be used to find mismatches between allocation and free; the typical problem caught is a pointer allocated with rte_malloc() that is then incorrectly freed using free(). The name of the DPDK wrapper macros for these attributes are chosen to be similar to what GLIBC is using in cdefs.h.
Note: The rte_free function prototype was moved ahead of the allocation functions since the dealloc attribute now refers to it.
--- doc/guides/rel_notes/release_24_11.rst | 8 ++++ lib/eal/include/rte_common.h | 34 ++++++++++++++++ lib/eal/include/rte_malloc.h | 55 +++++++++++++++----------- 3 files changed, 74 insertions(+), 23 deletions(-)
doc/guides/rel_notes/release_24_11.rst +8 -0
@@ -55,6 +55,14 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* **Hardened rte_malloc and related functions.** + + * Added function attributes to ``rte_malloc`` and similar functions + that can catch some obvious bugs at compile time (with GCC 11.0 or later). + Examples: calling ``free()`` on pointer that was allocated with ``rte_malloc`` + (and vice versa); freeing the same pointer twice in the same routine; + freeing an object that was not created by allocation; etc. + Removed Items -------------
lib/eal/include/rte_common.h +34 -0
@@ -228,6 +228,40 @@ typedef uint16_t unaligned_uint16_t;#define __rte_alloc_size (...)#endif +/** +* Tells the compiler that the function returns a value that points to +* memory aligned by a function argument. +* +* Note: not enabled on Clang because it warns if align argument is zero. +*/ +#if defined(RTE_CC_GCC ) +#define __rte_alloc_align (argno) __attribute__((alloc_align(argno))) +#else +#define __rte_alloc_align (argno) +#endif + +/** +* Tells the compiler this is a function like malloc and that the pointer +* returned cannot alias any other pointer (ie new memory). +*/ +#if defined(RTE_CC_GCC ) || defined(RTE_CC_CLANG ) +#define __rte_malloc __attribute__((malloc)) +#else +#define __rte_malloc +#endif + +/** +* With recent GCC versions also able to track that proper +* dealloctor function is used for this pointer. +*/ +#if defined(RTE_TOOLCHAIN_GCC ) && (GCC_VERSION >=110000 ) +#define __rte_dealloc (dealloc, argno) __attribute__((malloc(dealloc, argno))) +#define __rte_dealloc_free __rte_dealloc(rte_free, 1) +#else +#define __rte_dealloc (dealloc, argno) +#define __rte_dealloc_free +#endif +#define RTE_PRIORITY_LOG 101#define RTE_PRIORITY_BUS 110#define RTE_PRIORITY_CLASS 120
lib/eal/include/rte_malloc.h +32 -23
109 lines not on this page · raw message
Signature · 1 line
2.45.2
Re: [PATCH v5 17/17] eal: add function attributes for allocation functions
Quoted 23 lines · Stephen Hemminger
lib/eal/include/rte_common.h +15
-0@@ -228,6 +228,40 @@ typedef uint16_t unaligned_uint16_t;#define __rte_alloc_size (...)#endif +/** +* Tells the compiler that the function returns a value that points to +* memory aligned by a function argument. +* +* Note: not enabled on Clang because it warns if align argument is zero. +*/ +#if defined(RTE_CC_GCC ) +#define __rte_alloc_align (argno) __attribute__((alloc_align(argno))) +#else +#define __rte_alloc_align (argno) +#endif + +/** +* Tells the compiler this is a function like malloc and that the pointer +* returned cannot alias any other pointer (ie new memory).
lib/eal/include/rte_common.h typedef uint16_t unaligned_uint16_t; · line 255
Quoted 10 lines
lib/eal/include/rte_common.h +10
-0+*/ +#if defined(RTE_CC_GCC ) || defined(RTE_CC_CLANG ) +#define __rte_malloc __attribute__((malloc)) +#else +#define __rte_malloc +#endif + +/** +* With recent GCC versions also able to track that proper +* dealloctor function is used for this pointer.
deallocator*
lib/eal/include/rte_common.h typedef uint16_t unaligned_uint16_t; · line 259
Quoted 4 lines
lib/eal/include/rte_common.h +4
-0+*/ +#if defined(RTE_TOOLCHAIN_GCC ) && (GCC_VERSION >=110000 ) +#define __rte_dealloc (dealloc, argno) __attribute__((malloc(dealloc, argno))) +#define __rte_dealloc_free __rte_dealloc(rte_free, 1)
__rte_dealloc_free should not be in rte_common.h but in rte_malloc.h.
Quoted 13 lines
lib/eal/include/rte_common.h +5
-0+#else +#define __rte_dealloc (dealloc, argno) +#define __rte_dealloc_free +#endif +#define RTE_PRIORITY_LOG 101#define RTE_PRIORITY_BUS 110#define RTE_PRIORITY_CLASS 120lib/eal/include/rte_malloc.h +0
-0
lib/eal/include/rte_malloc.h struct rte_malloc_socket_stats { · line 34
Quoted 4 lines
lib/eal/include/rte_malloc.h +1
-0@@ -31,6 +31,22 @@ struct rte_malloc_socket_stats {size_t heap_allocsz_bytes;/**< Total allocated bytes on heap */ }; +
Nit: no need for extra line.
Quoted 7 lines
lib/eal/include/rte_malloc.h +7
-0+/** +* Frees the memory space pointed to by the provided pointer. +* +* 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. +*
Signature · 1 line
David Marchand
v6 cover Fix allocation related bugs and catch future bugs
In reply to Stephen HemmingerRecent versions of GCC have some additional function attributes that can help with DPDK performance and stability.
The alloc_align attribute tells the compiler what the alignment of the allocation will be, and the optimizer can use this to produce better code (especially memcpy and structure copies).
The malloc attribute tells compiler that object is not overlapping and potentially aliasing. It also as an additional variant in GCC 11 or later that allows for detecting all sorts of common errors like calling free() on memory allocated with rte_malloc().
In order to use the malloc attribute the free function prototype needs to be moved before the allocator/create function prototype so that the malloc attribute can refer to it.
This uncovered at least 16 pre-existing bugs in DPDK, these should go to stable. This patch set is structured with:
- fix any new warnings that were discovered
- add macros for enable the macros
- enable the attributes
The same attributes could be added to lots more functions in DPDK, but this patch set focuses on the key ones, and where problems exist in current code base.
v6 - more __rte_dealloc_free macro to rte_malloc.h
Stephen Hemminger (17):
memzone: fix use after free in tracing cryptodev/bcmfs: fix mis-matched free dma/ixd: fix incorrect free function in cleanup event/cnxk: fix pointer mismatch in cleanup examples/vhost: fix free function mismatch net/cnxk: fix use-after-free
bpf: fix free mismatch if convert fails net/e1000: fix use-after-free
net/sfc: fix use-after-free warning messages net/cpfl: fix free of nonheap object net/nfp: fix duplicate call to rte_free raw/ifpga/base: fix use after free common/qat: fix use after free drivers/ifpga: fix free function mismatch baseband/la12xx: prevent use after free common/idpf: fix use after free due
eal: add function attributes for allocation functions
doc/guides/rel_notes/release_24_11.rst | 8 +++ drivers/baseband/la12xx/bbdev_la12xx.c | 1 + drivers/common/idpf/base/idpf_osdep.h | 10 +++- drivers/common/idpf/idpf_common_device.c | 3 +- drivers/common/qat/qat_device.c | 6 +-- drivers/crypto/bcmfs/bcmfs_device.c | 4 +- drivers/dma/idxd/idxd_pci.c | 2 +- drivers/event/cnxk/cnxk_eventdev.c | 4 +- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- drivers/net/cpfl/cpfl_flow_parser.c | 1 - drivers/net/e1000/igb_ethdev.c | 4 +- drivers/net/nfp/flower/nfp_flower_flow.c | 1 - drivers/net/sfc/sfc_flow_rss.c | 4 +- drivers/net/sfc/sfc_mae.c | 23 ++++----- drivers/raw/ifpga/base/opae_intel_max10.c | 11 ++++- drivers/raw/ifpga/ifpga_rawdev.c | 8 +-- examples/vhost_blk/vhost_blk.c | 2 +- lib/bpf/bpf_convert.c | 2 +- lib/eal/common/eal_common_memzone.c | 3 +- lib/eal/include/rte_common.h | 34 +++++++++++++ lib/eal/include/rte_malloc.h | 59 ++++++++++++++--------- 21 files changed, 126 insertions(+), 66 deletions(-)
Signature · 1 line
2.45.2
PATCH v6 1/17 memzone: fix use after free in tracing
Using the freed value for tracing is not a good idea. Although it is harmless for tracing, it will cause analyzers to flag this as unsafe.
--- lib/eal/common/eal_common_memzone.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/eal/common/eal_common_memzone.c +2 -1
@@ -331,9 +331,10 @@ rte_memzone_free(const struct rte_memzone *mz)rte_rwlock_write_unlock (&mcfg->mlock); +rte_eal_trace_memzone_free (name, addr, ret); +rte_free (addr); -rte_eal_trace_memzone_free (name, addr, ret);return ret; }
Signature · 1 line
2.45.2
PATCH v6 2/17 cryptodev/bcmfs: fix mis-matched free
In reply to Stephen HemmingerThe device structure is allocated with rte_malloc() and then incorrectly freed with free(). This will lead to corrupt malloc pool.
Bugzilla ID: 1552
--- drivers/crypto/bcmfs/bcmfs_device.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/crypto/bcmfs/bcmfs_device.c +2 -2
@@ -139,7 +139,7 @@ fsdev_allocate_one_dev(struct rte_vdev_device *vdev,return fsdev; cleanup: -free (fsdev); +rte_free (fsdev);return NULL ; }
@@ -163,7 +163,7 @@ fsdev_release(struct bcmfs_device *fsdev)return ;TAILQ_REMOVE (&fsdev_list, fsdev, next); -free (fsdev); +rte_free (fsdev); } static int
Signature · 1 line
2.45.2
PATCH v6 3/17 dma/ixd: fix incorrect free function in cleanup
In reply to Stephen HemmingerThe data structure is allocated with rte_malloc and incorrectly freed in cleanup logic using free.
Bugzilla ID: 1549
--- drivers/dma/idxd/idxd_pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma/idxd/idxd_pci.c +1 -1
@@ -301,7 +301,7 @@ init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,return nb_wqs; err: -free (pci); +rte_free (pci);return err_code; }
Signature · 1 line
2.45.2
PATCH v6 4/17 event/cnxk: fix pointer mismatch in cleanup
In reply to Stephen HemmingerThe code to cleanup in case of error was passing incorrect value to rte_free. The ports[] entry was allocated with rte_malloc and that should be used instead of the offset in that object.
--- drivers/event/cnxk/cnxk_eventdev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/event/cnxk/cnxk_eventdev.c +3 -1
@@ -121,8 +121,10 @@ cnxk_setup_event_ports(const struct rte_eventdev *event_dev,return 0 ; hws_fini:for (i = i -1 ; i >=0 ; i--) { +void *ws = event_dev->data->ports[i]; + event_dev->data->ports[i] =NULL ; -rte_free (cnxk_sso_hws_get_cookie (event_dev->data->ports[i])); +rte_free (ws); }return -ENOMEM ; }
Signature · 1 line
2.45.2
PATCH v6 5/17 examples/vhost: fix free function mismatch
In reply to Stephen HemmingerThe pointer bdev is allocated with rte_zmalloc() and then incorrectly freed with free() which will lead pool corruption.
Bugzilla ID: 1553
--- examples/vhost_blk/vhost_blk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
examples/vhost_blk/vhost_blk.c +1 -1
@@ -776,7 +776,7 @@ vhost_blk_bdev_construct(const char *bdev_name, bdev->data =rte_zmalloc (NULL , blk_cnt * blk_size,0 );if (!bdev->data) {fprintf (stderr,"No enough reserved huge memory for disk\n" ); -free (bdev); +rte_free (bdev);return NULL ; }
Signature · 1 line
2.45.2
PATCH v6 6/17 net/cnxk: fix use-after-free
In reply to Stephen HemmingerThe driver would refer to the mempool object after it was freed.
Bugzilla ID: 1554
--- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/cnxk/cnxk_ethdev_sec.c +1 -1
@@ -136,8 +136,8 @@ cnxk_nix_inl_custom_meta_pool_cb(uintptr_t pmpool, uintptr_t *mpool, const charreturn -EINVAL ; } -rte_mempool_free (hp);plt_free (hp->pool_config); +rte_mempool_free (hp); *aura_handle =0 ; *mpool =0 ;
Signature · 1 line
2.45.2
PATCH v6 7/17 bpf: fix free mismatch if convert fails
In reply to Stephen HemmingerIf conversion of cBF to eBPF fails then an object allocated with rte_malloc() would be passed to free().
[908/3201] Compiling C object lib/librte_bpf.a.p/bpf_bpf_convert.c.o../lib/bpf/bpf_convert.c: In function ‘rte_bpf_convert’:../lib/bpf/bpf_convert.c:559:17: warning: ‘free’ called on pointer returned from a mismatched allocation function [-Wmismatched-dealloc]
559 | free(prm);
| ^~~~~~~~~
../lib/bpf/bpf_convert.c:545:15: note: returned from ‘rte_zmalloc’
545 | prm = rte_zmalloc("bpf_filter",
| ^~~~~~~~~~~~~~~~~~~~~~~~~
546 | sizeof(*prm) + ebpf_len * sizeof(*ebpf), 0);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~--- lib/bpf/bpf_convert.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/bpf/bpf_convert.c +1 -1
@@ -556,7 +556,7 @@ rte_bpf_convert(const struct bpf_program *prog) ret =bpf_convert_filter (prog->bf_insns, prog->bf_len, ebpf, &ebpf_len);if (ret <0 ) {RTE_BPF_LOG_LINE (ERR ,"%s: cannot convert cBPF to eBPF" , __func__); -free (prm); +rte_free (prm); rte_errno = -ret;return NULL ; }
Signature · 1 line
2.45.2
PATCH v6 8/17 net/e1000: fix use-after-free
In reply to Stephen HemmingerThe driver cleanup code was freeing the filter object then dereferencing it.
Bugzilla ID: 1550
--- drivers/net/e1000/igb_ethdev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/e1000/igb_ethdev.c +2 -2
@@ -3907,11 +3907,11 @@ igb_delete_2tuple_filter(struct rte_eth_dev *dev, filter_info->twotuple_mask &= ~(1 << filter->index);TAILQ_REMOVE (&filter_info->twotuple_list, filter, entries); -rte_free (filter);E1000_WRITE_REG (hw,E1000_TTQF (filter->index),E1000_TTQF_DISABLE_MASK );E1000_WRITE_REG (hw,E1000_IMIR (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIREXT (filter->index),0 ); +rte_free (filter);return 0 ; }
@@ -4348,7 +4348,6 @@ igb_delete_5tuple_filter_82576(struct rte_eth_dev *dev, filter_info->fivetuple_mask &= ~(1 << filter->index);TAILQ_REMOVE (&filter_info->fivetuple_list, filter, entries); -rte_free (filter);E1000_WRITE_REG (hw,E1000_FTQF (filter->index),E1000_FTQF_VF_BP |E1000_FTQF_MASK );
@@ -4357,6 +4356,7 @@ igb_delete_5tuple_filter_82576(struct rte_eth_dev *dev,E1000_WRITE_REG (hw,E1000_SPQF (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIR (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIREXT (filter->index),0 ); +rte_free (filter);return 0 ; }
Signature · 1 line
2.45.2
PATCH v6 9/17 net/sfc: fix use-after-free warning messages
In reply to Stephen HemmingerIf compiler detection of use-after-free is enabled then this drivers debug messages will cause warnings. Change to move debug message before the object is freed.
Bugzilla ID: 1551
--- drivers/net/sfc/sfc_flow_rss.c | 4 ++-- drivers/net/sfc/sfc_mae.c | 23 +++++++++-------------- 2 files changed, 11 insertions(+), 16 deletions(-)
drivers/net/sfc/sfc_flow_rss.c +2 -2
@@ -303,9 +303,9 @@ sfc_flow_rss_ctx_del(struct sfc_adapter *sa, struct sfc_flow_rss_ctx *ctx)TAILQ_REMOVE (&flow_rss->ctx_list, ctx, entries);rte_free (ctx->qid_offsets); -rte_free (ctx); -sfc_dbg (sa,"flow-rss: deleted ctx=%p" , ctx); + +rte_free (ctx); } static int
drivers/net/sfc/sfc_mae.c +9 -14
@@ -400,9 +400,8 @@ sfc_mae_outer_rule_del(struct sfc_adapter *sa,efx_mae_match_spec_fini (sa->nic, rule->match_spec);TAILQ_REMOVE (&mae->outer_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted outer_rule=%p" , rule); +rte_free (rule); } static int
@@ -585,9 +584,8 @@ sfc_mae_mac_addr_del(struct sfc_adapter *sa, struct sfc_mae_mac_addr *mac_addr) }TAILQ_REMOVE (&mae->mac_addrs, mac_addr, entries); -rte_free (mac_addr); -sfc_dbg (sa,"deleted mac_addr=%p" , mac_addr); +rte_free (mac_addr); }enum sfc_mae_mac_addr_type {
@@ -785,10 +783,10 @@ sfc_mae_encap_header_del(struct sfc_adapter *sa, }TAILQ_REMOVE (&mae->encap_headers, encap_header, entries); +sfc_dbg (sa,"deleted encap_header=%p" , encap_header); +rte_free (encap_header->buf);rte_free (encap_header); - -sfc_dbg (sa,"deleted encap_header=%p" , encap_header); } static int
@@ -983,9 +981,8 @@ sfc_mae_counter_del(struct sfc_adapter *sa, struct sfc_mae_counter *counter) }TAILQ_REMOVE (&mae->counters, counter, entries); -rte_free (counter); -sfc_dbg (sa,"deleted counter=%p" , counter); +rte_free (counter); } static int
@@ -1165,9 +1162,8 @@ sfc_mae_action_set_del(struct sfc_adapter *sa,sfc_mae_mac_addr_del (sa, action_set->src_mac_addr);sfc_mae_counter_del (sa, action_set->counter);TAILQ_REMOVE (&mae->action_sets, action_set, entries); -rte_free (action_set); -sfc_dbg (sa,"deleted action_set=%p" , action_set); +rte_free (action_set); } static int
@@ -1401,10 +1397,10 @@ sfc_mae_action_set_list_del(struct sfc_adapter *sa,sfc_mae_action_set_del (sa, action_set_list->action_sets[i]);TAILQ_REMOVE (&mae->action_set_lists, action_set_list, entries); +sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); +rte_free (action_set_list->action_sets);rte_free (action_set_list); - -sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); } static int
@@ -1667,9 +1663,8 @@ sfc_mae_action_rule_del(struct sfc_adapter *sa,sfc_mae_outer_rule_del (sa, rule->outer_rule);TAILQ_REMOVE (&mae->action_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted action_rule=%p" , rule); +rte_free (rule); } static int
Signature · 1 line
2.45.2
PATCH v6 10/17 net/cpfl: fix free of nonheap object
In reply to Stephen HemmingerWith proper annotation, GCC discovers that this driver is calling rte_free() on an object that was not allocated (it is part of array in another object).
In function ‘cpfl_flow_js_mr_layout’,
inlined from ‘cpfl_flow_js_mr_action’ at ../drivers/net/cpfl/cpfl_flow_parser.c:848:9,
inlined from ‘cpfl_flow_js_mod_rule’ at ../drivers/net/cpfl/cpfl_flow_parser.c:908:9,
inlined from ‘cpfl_parser_init’ at ../drivers/net/cpfl/cpfl_flow_parser.c:932:8,
inlined from ‘cpfl_parser_create’ at ../drivers/net/cpfl/cpfl_flow_parser.c:959:8:../drivers/net/cpfl/cpfl_flow_parser.c:740:9: warning: ‘rte_free’ called on pointer ‘*parser.modifications’ with nonzero offset [28, 15479062120396] [-Wfree-nonheap-object]
740 | rte_free(js_mod->layout);
| ^~~~~~~~~~~~~~~~~~~~~~~~--- drivers/net/cpfl/cpfl_flow_parser.c | 1 - 1 file changed, 1 deletion(-)
drivers/net/cpfl/cpfl_flow_parser.c +0 -1
@@ -737,7 +737,6 @@ cpfl_flow_js_mr_layout(json_t *ob_layouts, struct cpfl_flow_js_mr_action_mod *jsreturn 0 ; err: -rte_free (js_mod->layout);return -EINVAL ; }
Signature · 1 line
2.45.2
PATCH v6 11/17 net/nfp: fix duplicate call to rte_free
In reply to Stephen HemmingerCalling rte_free twice on same object will corrupt the heap.
Warning is:
In function 'nfp_pre_tun_table_check_del',
inlined from 'nfp_flow_destroy' at ../drivers/net/nfp/flower/nfp_flower_flow.c:5143:9:../drivers/net/nfp/flower/nfp_flower_flow.c:3830:9: error: pointer 'entry' used after 'rte_free' [-Werror=use-after-free]
3830 | rte_free(entry);
| ^~~~~~~~~~~~~~~../drivers/net/nfp/flower/nfp_flower_flow.c:3825:9: note: call to 'rte_free' here
3825 | rte_free(entry);
| ^~~~~~~~~~~~~~~
Bugzilla ID: 1555
--- drivers/net/nfp/flower/nfp_flower_flow.c | 1 - 1 file changed, 1 deletion(-)
drivers/net/nfp/flower/nfp_flower_flow.c +0 -1
@@ -3822,7 +3822,6 @@ nfp_pre_tun_table_check_del(struct nfp_flower_representor *repr, goto free_entry; } -rte_free (entry);rte_free (find_entry); priv->pre_tun_cnt--;
Signature · 1 line
2.45.2
PATCH v6 12/17 raw/ifpga/base: fix use after free
In reply to Stephen HemmingerThe TAILQ_FOREACH() macro would refer to info after it had been freed. Fix by introducing TAILQ_FOREACH_SAFE here.
--- drivers/raw/ifpga/base/opae_intel_max10.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/raw/ifpga/base/opae_intel_max10.c +9 -2
@@ -6,6 +6,13 @@#include <libfdt.h> #include "opae_osdep.h" +#ifndef TAILQ_FOREACH_SAFE +#define TAILQ_FOREACH_SAFE (var, head, field, tvar) \ + for ((var) = TAILQ_FIRST((head)); \ + (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) +#endif +int max10_sys_read (struct intel_max10_device *dev,unsigned int offset,unsigned int *val) {
@@ -746,9 +753,9 @@ static int fdt_get_named_reg(const void *fdt, int node, const char *name,static void max10_sensor_uinit (struct intel_max10_device *dev) { -struct opae_sensor_info *info; +struct opae_sensor_info *info, *next; - TAILQ_FOREACH(info, &dev->opae_sensor_list, node) { + TAILQ_FOREACH_SAFE(info, &dev->opae_sensor_list, node, next) {TAILQ_REMOVE (&dev->opae_sensor_list, info, node);opae_free (info); }
Signature · 1 line
2.45.2
PATCH v6 13/17 common/qat: fix use after free
In reply to Stephen HemmingerChecking return value of rte_memzone_free() is pointless and if it failed then it was because the pointer was null.
--- drivers/common/qat/qat_device.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/common/qat/qat_device.c +1 -5
@@ -390,11 +390,7 @@ qat_pci_device_allocate(struct rte_pci_device *pci_dev)return qat_dev; error:rte_free (qat_dev->command_line); -if (rte_memzone_free (qat_dev_mz)) { -QAT_LOG (DEBUG , -"QAT internal error! Trying to free already allocated memzone: %s" , - qat_dev_mz->name); - } +rte_memzone_free (qat_dev_mz);return NULL ; }
Signature · 1 line
2.45.2
PATCH v6 14/17 drivers/ifpga: fix free function mismatch
In reply to Stephen HemmingerThe raw ifpga driver redefines malloc to be opae_malloc and free to be opae_free; which is a bad idea.
This leads to case where interrupt efd array is allocated with calloc() and then passed to rte_free. The workaround is to allocate the array with rte_calloc() instead.
--- drivers/raw/ifpga/ifpga_rawdev.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/raw/ifpga/ifpga_rawdev.c +4 -4
@@ -1499,7 +1499,7 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, nb_intr =rte_intr_nb_intr_get (*intr_handle); - intr_efds =calloc (nb_intr,sizeof (int )); + intr_efds =rte_calloc ("ifpga_efds" , nb_intr,sizeof (int ),0 );if (!intr_efds)return -ENOMEM ;
@@ -1508,7 +1508,7 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, ret =opae_acc_set_irq (acc, vec_start, count, intr_efds);if (ret) { -free (intr_efds); +rte_free (intr_efds);return -EINVAL ; } }
@@ -1517,13 +1517,13 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, ret =rte_intr_callback_register (*intr_handle, handler, (void *)arg);if (ret) { -free (intr_efds); +rte_free (intr_efds);return -EINVAL ; }IFPGA_RAWDEV_PMD_INFO ("success register %s interrupt\n" , name); -free (intr_efds); +rte_free (intr_efds);return 0 ; }
Signature · 1 line
2.45.2
PATCH v6 15/17 baseband/la12xx: prevent use after free
In reply to Stephen HemmingerIt is possible that the info pointer (hp) could get freed twice. Fix by nulling after free.
In function 'setup_la12xx_dev',
inlined from 'la12xx_bbdev_create' at ../drivers/baseband/la12xx/bbdev_la12xx.c:1029:8,
inlined from 'la12xx_bbdev_probe' at ../drivers/baseband/la12xx/bbdev_la12xx.c:1075:9:../drivers/baseband/la12xx/bbdev_la12xx.c:901:9: error: pointer 'hp_info' may be used after 'rte_free' [-Werror=use-after-free]
901 | rte_free(hp);
| ^~~~~~~~~~~~../drivers/baseband/la12xx/bbdev_la12xx.c:791:17: note: call to 'rte_free' here
791 | rte_free(hp);
| ^~~~~~~~~~~~
--- drivers/baseband/la12xx/bbdev_la12xx.c | 1 + 1 file changed, 1 insertion(+)
drivers/baseband/la12xx/bbdev_la12xx.c +1 -0
@@ -789,6 +789,7 @@ setup_la12xx_dev(struct rte_bbdev *dev) ipc_priv->hugepg_start.size = hp->len;rte_free (hp); + hp =NULL ; } dev_ipc =open_ipc_dev (priv->modem_id);
Signature · 1 line
2.45.2
PATCH v6 16/17 common/idpf: fix use after free due
In reply to Stephen HemmingerThe macro in this driver was redefining LIST_FOR_EACH_ENTRY_SAFE as a simple LIST_FOR_EACH macro. But they are not the same the _SAFE variant guarantees that there will not be use after free.
--- drivers/common/idpf/base/idpf_osdep.h | 10 ++++++++-- drivers/common/idpf/idpf_common_device.c | 3 +-- 2 files changed, 9 insertions(+), 4 deletions(-)
drivers/common/idpf/base/idpf_osdep.h +8 -2
@@ -341,10 +341,16 @@ idpf_hweight32(u32 num)#define LIST_ENTRY_TYPE (type) LIST_ENTRY(type)#endif +#ifndef LIST_FOREACH_SAFE +#define LIST_FOREACH_SAFE (var, head, field, tvar) \ + for ((var) = LIST_FIRST((head)); \ + (var) && ((tvar) = LIST_NEXT((var), field), 1); \ + (var) = (tvar)) +#endif +#ifndef LIST_FOR_EACH_ENTRY_SAFE #define LIST_FOR_EACH_ENTRY_SAFE (pos, temp, head, entry_type, list) \ - LIST_FOREACH(pos, head, list) - + LIST_FOREACH_SAFE(pos, head, list, temp)#endif #ifndef LIST_FOR_EACH_ENTRY
drivers/common/idpf/idpf_common_device.c +1 -2
@@ -136,8 +136,7 @@ idpf_init_mbx(struct idpf_hw *hw)if (ret !=0 )return ret; -LIST_FOR_EACH_ENTRY_SAFE (ctlq,NULL , &hw->cq_list_head, - struct idpf_ctlq_info, cq_list) { + LIST_FOR_EACH_ENTRY(ctlq, &hw->cq_list_head, struct idpf_ctlq_info, cq_list) {if (ctlq->q_id ==IDPF_CTLQ_ID && ctlq->cq_type ==IDPF_CTLQ_TYPE_MAILBOX_TX ) hw->asq = ctlq;
Signature · 1 line
2.45.2
PATCH v6 17/17 eal: add function attributes for allocation functions
In reply to Stephen HemmingerThe allocation functions take a alignment argument that can be useful to hint the compiler optimizer. This is supported by GCC and Clang but only useful with GCC because Clang gives warning if alignment is 0.
Newer versions of GCC have a malloc attribute that can be used to find mismatches between allocation and free; the typical problem caught is a pointer allocated with rte_malloc() that is then incorrectly freed using free(). The name of the DPDK wrapper macros for these attributes are chosen to be similar to what GLIBC is using in cdefs.h.
Note: The rte_free function prototype was moved ahead of the allocation functions since the dealloc attribute now refers to it.
--- doc/guides/rel_notes/release_24_11.rst | 8 ++++ lib/eal/include/rte_common.h | 34 +++++++++++++++ lib/eal/include/rte_malloc.h | 59 ++++++++++++++++---------- 3 files changed, 78 insertions(+), 23 deletions(-)
doc/guides/rel_notes/release_24_11.rst +8 -0
@@ -55,6 +55,14 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* **Hardened rte_malloc and related functions.** + + * Added function attributes to ``rte_malloc`` and similar functions + that can catch some obvious bugs at compile time (with GCC 11.0 or later). + Examples: calling ``free()`` on pointer that was allocated with ``rte_malloc`` + (and vice versa); freeing the same pointer twice in the same routine; + freeing an object that was not created by allocation; etc. + Removed Items -------------
lib/eal/include/rte_common.h +34 -0
@@ -228,6 +228,40 @@ typedef uint16_t unaligned_uint16_t;#define __rte_alloc_size (...)#endif +/** +* Tells the compiler that the function returns a value that points to +* memory aligned by a function argument. +* +* Note: not enabled on Clang because it warns if align argument is zero. +*/ +#if defined(RTE_CC_GCC ) +#define __rte_alloc_align (argno) \ + __attribute__((alloc_align(argno))) +#else +#define __rte_alloc_align (argno) +#endif + +/** +* Tells the compiler this is a function like malloc and that the pointer +* returned cannot alias any other pointer (ie new memory). +*/ +#if defined(RTE_CC_GCC ) || defined(RTE_CC_CLANG ) +#define __rte_malloc __attribute__((__malloc__)) +#else +#define __rte_malloc +#endif + +/** +* With recent GCC versions also able to track that proper +* deallocator function is used for this pointer. +*/ +#if defined(RTE_TOOLCHAIN_GCC ) && (GCC_VERSION >=110000 ) +#define __rte_dealloc (dealloc, argno) \ + __attribute__((malloc(dealloc, argno))) +#else +#define __rte_dealloc (dealloc, argno) +#endif +#define RTE_PRIORITY_LOG 101#define RTE_PRIORITY_BUS 110#define RTE_PRIORITY_CLASS 120
lib/eal/include/rte_malloc.h +36 -23
113 lines not on this page · raw message
Signature · 1 line
2.45.2
RE: [PATCH v6 17/17] eal: add function attributes for allocation functions
Quoted 17 lines
-----Original Message-----
Subject: [PATCH v6 17/17] eal: add function attributes for allocation functionsThe allocation functions take a alignment argument that can be useful to hint the compiler optimizer. This is supported by GCC and Clang but only useful with GCC because Clang gives warning if alignment is 0.
Newer versions of GCC have a malloc attribute that can be used to find mismatches between allocation and free; the typical problem caught is a pointer allocated with
rte_malloc()that is then incorrectly freed usingfree(). The name of the DPDK wrapper macros for these attributes are chosen to be similar to what GLIBC is using incdefs.h.
Note: Therte_freefunction prototype was moved ahead of the allocation functions since the dealloc attribute now refers to it.
Quoted 3 lines
Signed-off-by Stephen Hemminger <[email protected]>Acked-by Chengwen Feng <[email protected]>Acked-by Anatoly Burakov <[email protected]>
Ajit Khaparde
Acked-byRe: [PATCH v6 17/17] eal: add function attributes for allocation functions
Quoted 18 lines · Wathsala Wathawana Vithanage
Quoted 18 lines
-----Original Message-----
Subject: [PATCH v6 17/17] eal: add function attributes for allocation functionsThe allocation functions take a alignment argument that can be useful to hint the compiler optimizer. This is supported by GCC and Clang but only useful with GCC because Clang gives warning if alignment is 0.
Newer versions of GCC have a malloc attribute that can be used to find mismatches between allocation and free; the typical problem caught is a pointer allocated with
rte_malloc()that is then incorrectly freed usingfree(). The name of the DPDK wrapper macros for these attributes are chosen to be similar to what GLIBC is using incdefs.h.
Note: Therte_freefunction prototype was moved ahead of the allocation functions since the dealloc attribute now refers to it.
Quoted 5 lines
Quoted 3 lines
Signed-off-by Stephen Hemminger <[email protected]>Acked-by Chengwen Feng <[email protected]>Acked-by Anatoly Burakov <[email protected]>
RE: [PATCH v6 17/17] eal: add function attributes for allocation functions
In reply to Stephen HemmingerQuoted 17 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Wednesday, 2 October 2024 17.43The allocation functions take a alignment argument that can be useful to hint the compiler optimizer. This is supported by GCC and Clang but only useful with GCC because Clang gives warning if alignment is 0.
Newer versions of GCC have a malloc attribute that can be used to find mismatches between allocation and free; the typical problem caught is a pointer allocated with
rte_malloc()that is then incorrectly freed usingfree(). The name of the DPDK wrapper macros for these attributes are chosen to be similar to what GLIBC is using incdefs.h.
Note: Therte_freefunction prototype was moved ahead of the allocation functions since the dealloc attribute now refers to it.
Quoted 4 lines
Signed-off-by Stephen Hemminger <[email protected]>Acked-by Chengwen Feng <[email protected]>Acked-by Anatoly Burakov <[email protected]>---
I see many of my comments to v3 have already been addressed. Great minds think alike. :-)
lib/eal/include/rte_common.h typedef uint16_t unaligned_uint16_t; · line 263
Quoted 10 lines
lib/eal/include/rte_common.h +10
-0+/** +* With recent GCC versions also able to track that proper +* deallocator function is used for this pointer. +*/ +#if defined(RTE_TOOLCHAIN_GCC ) && (GCC_VERSION >=110000 ) +#define __rte_dealloc (dealloc, argno) \ + __attribute__((malloc(dealloc, argno))) +#else +#define __rte_dealloc (dealloc, argno) +#endif
A matter of taste...
The name "__rte_malloc" is closely associated with the function name "malloc()"; for consistency suggest naming this "__rte_free" or "__rte_malloc_free".
<brainstorming>
If named __rte_malloc_free, it could include the __rte_malloc (as in previous versions of the patch). However, that might be confusing, so probably not a good idea.
I prefer keeping the attributes separate, as in this version.
</brainstorming>
lib/eal/include/rte_malloc.h struct rte_malloc_socket_stats { · line 36
Quoted 8 lines
+++
b/lib/eal/include/rte_malloc.hlib/eal/include/rte_malloc.h +2
-0@@ -31,6 +31,26 @@ struct rte_malloc_socket_stats {size_t heap_allocsz_bytes;/**< Total allocated bytes on heap */ }; +/** +* Function attribut for prototypes that expect to release memory with rte_free()
Typo: attribut -> attribute
lib/eal/include/rte_malloc.h struct rte_malloc_socket_stats { · line 37
Quoted 2 lines
lib/eal/include/rte_malloc.h +2
-0+*/ +#define __rte_dealloc_free __rte_dealloc(rte_free, 1)
Minor detail:
I'm worried someone might misunderstand the purpose of this shortcut, and use it with an allocator function where a different deallocator is associated. Moving it from rte_common.h to rte_malloc.h is a huge improvement; but please consider if the benefit outweighs the risk. Again, I'll leave it up to you.
v7 cover Fix allocation bugs and prevent future ones
In reply to Stephen HemmingerRecent versions of GCC have some additional function attributes that can help with DPDK performance and stability.
The alloc_align attribute tells the compiler what the alignment of the allocation will be, and the optimizer can use this to produce better code (especially memcpy and structure copies).
The malloc attribute tells compiler that object is not overlapping and potentially aliasing. It also as an additional variant in GCC 11 or later that allows for detecting all sorts of common errors like calling free() on memory allocated with rte_malloc().
In order to use the malloc attribute the free function prototype needs to be moved before the allocator/create function prototype so that the malloc attribute can refer to it.
This uncovered at least 16 pre-existing bugs in DPDK, these should go to stable. This patch set is structured with:
- fix any new warnings that were discovered
- add macros for enable the macros
- enable the attributes
The same attributes could be added to lots more functions in DPDK, but this patch set focuses on the key ones, and where problems exist in current code base.
v7 - rebase and reword the release note
Stephen Hemminger (16):
memzone: fix use after free in tracing
cryptodev/bcmfs: fix mis-matched free
dma/ixd: fix incorrect free function in cleanup
event/cnxk: fix pointer mismatch in cleanup
examples/vhost: fix free function mismatch
net/cnxk: fix use-after-free
bpf: fix free mismatch if convert fails
net/e1000: fix use-after-free
net/sfc: fix use-after-free warning messages
net/cpfl: fix free of nonheap object
net/nfp: fix duplicate call to rte_free
raw/ifpga/base: fix use after free
common/qat: fix use after free
drivers/ifpga: fix free function mismatch
baseband/la12xx: prevent use after free
common/idpf: fix use after free due
drivers/baseband/la12xx/bbdev_la12xx.c | 1 + drivers/common/idpf/base/idpf_osdep.h | 10 ++++++++-- drivers/common/idpf/idpf_common_device.c | 3 +-- drivers/common/qat/qat_device.c | 6 +----- drivers/crypto/bcmfs/bcmfs_device.c | 4 ++-- drivers/dma/idxd/idxd_pci.c | 2 +- drivers/event/cnxk/cnxk_eventdev.c | 4 +++- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- drivers/net/cpfl/cpfl_flow_parser.c | 1 - drivers/net/e1000/igb_ethdev.c | 4 ++-- drivers/net/nfp/flower/nfp_flower_flow.c | 1 - drivers/net/sfc/sfc_flow_rss.c | 4 ++-- drivers/net/sfc/sfc_mae.c | 23 +++++++++-------------- drivers/raw/ifpga/base/opae_intel_max10.c | 11 +++++++++-- drivers/raw/ifpga/ifpga_rawdev.c | 8 ++++---- examples/vhost_blk/vhost_blk.c | 2 +- lib/bpf/bpf_convert.c | 2 +- lib/eal/common/eal_common_memzone.c | 3 ++- 18 files changed, 48 insertions(+), 43 deletions(-)
Signature · 1 line
2.45.2
PATCH v7 1/16 memzone: fix use after free in tracing
Using the freed value for tracing is not a good idea. Although it is harmless for tracing, it will cause analyzers to flag this as unsafe.
--- lib/eal/common/eal_common_memzone.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/eal/common/eal_common_memzone.c +2 -1
@@ -331,9 +331,10 @@ rte_memzone_free(const struct rte_memzone *mz)rte_rwlock_write_unlock (&mcfg->mlock); +rte_eal_trace_memzone_free (name, addr, ret); +rte_free (addr); -rte_eal_trace_memzone_free (name, addr, ret);return ret; }
Signature · 1 line
2.45.2
PATCH v7 2/16 cryptodev/bcmfs: fix mis-matched free
In reply to Stephen HemmingerThe device structure is allocated with rte_malloc() and then incorrectly freed with free(). This will lead to corrupt malloc pool.
Bugzilla ID: 1552
--- drivers/crypto/bcmfs/bcmfs_device.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/crypto/bcmfs/bcmfs_device.c +2 -2
@@ -139,7 +139,7 @@ fsdev_allocate_one_dev(struct rte_vdev_device *vdev,return fsdev; cleanup: -free (fsdev); +rte_free (fsdev);return NULL ; }
@@ -163,7 +163,7 @@ fsdev_release(struct bcmfs_device *fsdev)return ;TAILQ_REMOVE (&fsdev_list, fsdev, next); -free (fsdev); +rte_free (fsdev); } static int
Signature · 1 line
2.45.2
PATCH v7 3/16 dma/ixd: fix incorrect free function in cleanup
In reply to Stephen HemmingerThe data structure is allocated with rte_malloc and incorrectly freed in cleanup logic using free.
Bugzilla ID: 1549
--- drivers/dma/idxd/idxd_pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma/idxd/idxd_pci.c +1 -1
@@ -301,7 +301,7 @@ init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,return nb_wqs; err: -free (pci); +rte_free (pci);return err_code; }
Signature · 1 line
2.45.2
PATCH v7 4/16 event/cnxk: fix pointer mismatch in cleanup
In reply to Stephen HemmingerThe code to cleanup in case of error was passing incorrect value to rte_free. The ports[] entry was allocated with rte_malloc and that should be used instead of the offset in that object.
--- drivers/event/cnxk/cnxk_eventdev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/event/cnxk/cnxk_eventdev.c +3 -1
@@ -121,8 +121,10 @@ cnxk_setup_event_ports(const struct rte_eventdev *event_dev,return 0 ; hws_fini:for (i = i -1 ; i >=0 ; i--) { +void *ws = event_dev->data->ports[i]; + event_dev->data->ports[i] =NULL ; -rte_free (cnxk_sso_hws_get_cookie (event_dev->data->ports[i])); +rte_free (ws); }return -ENOMEM ; }
Signature · 1 line
2.45.2
RE: [EXTERNAL] [PATCH v7 04/16] event/cnxk: fix pointer mismatch in cleanup
Quoted 20 lines
The code to cleanup in case of error was passing incorrect value to
rte_free. The ports[] entry was allocated withrte_mallocand that should be used instead of the offset in that object.Signed-off-by Stephen Hemminger <[email protected]>--- drivers/event/cnxk/cnxk_eventdev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/event/cnxk/cnxk_eventdev.c +0
-0
drivers/event/cnxk/cnxk_eventdev.c cnxk_setup_event_ports(const struct rte_eventdev · line 129
Quoted 10 lines
drivers/event/cnxk/cnxk_eventdev.c +3
-1@@ -121,8 +121,10 @@ cnxk_setup_event_ports(const struct rte_eventdev *event_dev, return 0; hws_fini:for (i = i -1 ; i >=0 ; i--) { +void *ws = event_dev->data->ports[i]; + event_dev->data->ports[i] =NULL ; -rte_free (cnxk_sso_hws_get_cookie (event_dev->data- >ports[i])); +rte_free (ws);
Hi Stephen,
The rte_zmalloc memory is pointing to the cookie[1], the memory assigned to event_dev->data->ports[i] is rte_zmalloc + RTE_CACHE_LINE_SIZE.
There is still a bug in the code where we are assigning NULL before freeing memory. The fix should be
rte_free(cnxk_sso_hws_get_cookie(event_dev->data->ports[i])); event_dev->data->ports[i] = NULL;
[1]
/* Allocate event port memory */
ws = rte_zmalloc("cn10k_ws",
sizeof(struct cn10k_sso_hws) + RTE_CACHE_LINE_SIZE,
RTE_CACHE_LINE_SIZE);
/* First cache line is reserved for cookie */
ws = (struct cn10k_sso_hws *)((uint8_t *)ws + RTE_CACHE_LINE_SIZE);Thanks,
Pavan.
Quoted 5 lines
drivers/event/cnxk/cnxk_eventdev.c +0
-0}return
-ENOMEM;
}
--
2.45.2
PATCH v7 5/16 examples/vhost: fix free function mismatch
In reply to Stephen HemmingerThe pointer bdev is allocated with rte_zmalloc() and then incorrectly freed with free() which will lead pool corruption.
Bugzilla ID: 1553
--- examples/vhost_blk/vhost_blk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
examples/vhost_blk/vhost_blk.c +1 -1
@@ -776,7 +776,7 @@ vhost_blk_bdev_construct(const char *bdev_name, bdev->data =rte_zmalloc (NULL , blk_cnt * blk_size,0 );if (!bdev->data) {fprintf (stderr,"No enough reserved huge memory for disk\n" ); -free (bdev); +rte_free (bdev);return NULL ; }
Signature · 1 line
2.45.2
PATCH v7 6/16 net/cnxk: fix use-after-free
In reply to Stephen HemmingerThe driver would refer to the mempool object after it was freed.
Bugzilla ID: 1554
--- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/cnxk/cnxk_ethdev_sec.c +1 -1
@@ -136,8 +136,8 @@ cnxk_nix_inl_custom_meta_pool_cb(uintptr_t pmpool, uintptr_t *mpool, const charreturn -EINVAL ; } -rte_mempool_free (hp);plt_free (hp->pool_config); +rte_mempool_free (hp); *aura_handle =0 ; *mpool =0 ;
Signature · 1 line
2.45.2
PATCH v7 7/16 bpf: fix free mismatch if convert fails
In reply to Stephen HemmingerIf conversion of cBF to eBPF fails then an object allocated with rte_malloc() would be passed to free().
[908/3201] Compiling C object lib/librte_bpf.a.p/bpf_bpf_convert.c.o../lib/bpf/bpf_convert.c: In function ‘rte_bpf_convert’:../lib/bpf/bpf_convert.c:559:17: warning: ‘free’ called on pointer returned from a mismatched allocation function [-Wmismatched-dealloc]
559 | free(prm);
| ^~~~~~~~~
../lib/bpf/bpf_convert.c:545:15: note: returned from ‘rte_zmalloc’
545 | prm = rte_zmalloc("bpf_filter",
| ^~~~~~~~~~~~~~~~~~~~~~~~~
546 | sizeof(*prm) + ebpf_len * sizeof(*ebpf), 0);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~--- lib/bpf/bpf_convert.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/bpf/bpf_convert.c +1 -1
@@ -556,7 +556,7 @@ rte_bpf_convert(const struct bpf_program *prog) ret =bpf_convert_filter (prog->bf_insns, prog->bf_len, ebpf, &ebpf_len);if (ret <0 ) {RTE_BPF_LOG_LINE (ERR ,"%s: cannot convert cBPF to eBPF" , __func__); -free (prm); +rte_free (prm); rte_errno = -ret;return NULL ; }
Signature · 1 line
2.45.2
PATCH v7 8/16 net/e1000: fix use-after-free
In reply to Stephen HemmingerThe driver cleanup code was freeing the filter object then dereferencing it.
Bugzilla ID: 1550
--- drivers/net/e1000/igb_ethdev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/e1000/igb_ethdev.c +2 -2
@@ -3907,11 +3907,11 @@ igb_delete_2tuple_filter(struct rte_eth_dev *dev, filter_info->twotuple_mask &= ~(1 << filter->index);TAILQ_REMOVE (&filter_info->twotuple_list, filter, entries); -rte_free (filter);E1000_WRITE_REG (hw,E1000_TTQF (filter->index),E1000_TTQF_DISABLE_MASK );E1000_WRITE_REG (hw,E1000_IMIR (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIREXT (filter->index),0 ); +rte_free (filter);return 0 ; }
@@ -4348,7 +4348,6 @@ igb_delete_5tuple_filter_82576(struct rte_eth_dev *dev, filter_info->fivetuple_mask &= ~(1 << filter->index);TAILQ_REMOVE (&filter_info->fivetuple_list, filter, entries); -rte_free (filter);E1000_WRITE_REG (hw,E1000_FTQF (filter->index),E1000_FTQF_VF_BP |E1000_FTQF_MASK );
@@ -4357,6 +4356,7 @@ igb_delete_5tuple_filter_82576(struct rte_eth_dev *dev,E1000_WRITE_REG (hw,E1000_SPQF (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIR (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIREXT (filter->index),0 ); +rte_free (filter);return 0 ; }
Signature · 1 line
2.45.2
PATCH v7 9/16 net/sfc: fix use-after-free warning messages
In reply to Stephen HemmingerIf compiler detection of use-after-free is enabled then this drivers debug messages will cause warnings. Change to move debug message before the object is freed.
Bugzilla ID: 1551
--- drivers/net/sfc/sfc_flow_rss.c | 4 ++-- drivers/net/sfc/sfc_mae.c | 23 +++++++++-------------- 2 files changed, 11 insertions(+), 16 deletions(-)
drivers/net/sfc/sfc_flow_rss.c +2 -2
@@ -303,9 +303,9 @@ sfc_flow_rss_ctx_del(struct sfc_adapter *sa, struct sfc_flow_rss_ctx *ctx)TAILQ_REMOVE (&flow_rss->ctx_list, ctx, entries);rte_free (ctx->qid_offsets); -rte_free (ctx); -sfc_dbg (sa,"flow-rss: deleted ctx=%p" , ctx); + +rte_free (ctx); } static int
drivers/net/sfc/sfc_mae.c +9 -14
@@ -400,9 +400,8 @@ sfc_mae_outer_rule_del(struct sfc_adapter *sa,efx_mae_match_spec_fini (sa->nic, rule->match_spec);TAILQ_REMOVE (&mae->outer_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted outer_rule=%p" , rule); +rte_free (rule); } static int
@@ -585,9 +584,8 @@ sfc_mae_mac_addr_del(struct sfc_adapter *sa, struct sfc_mae_mac_addr *mac_addr) }TAILQ_REMOVE (&mae->mac_addrs, mac_addr, entries); -rte_free (mac_addr); -sfc_dbg (sa,"deleted mac_addr=%p" , mac_addr); +rte_free (mac_addr); }enum sfc_mae_mac_addr_type {
@@ -785,10 +783,10 @@ sfc_mae_encap_header_del(struct sfc_adapter *sa, }TAILQ_REMOVE (&mae->encap_headers, encap_header, entries); +sfc_dbg (sa,"deleted encap_header=%p" , encap_header); +rte_free (encap_header->buf);rte_free (encap_header); - -sfc_dbg (sa,"deleted encap_header=%p" , encap_header); } static int
@@ -983,9 +981,8 @@ sfc_mae_counter_del(struct sfc_adapter *sa, struct sfc_mae_counter *counter) }TAILQ_REMOVE (&mae->counters, counter, entries); -rte_free (counter); -sfc_dbg (sa,"deleted counter=%p" , counter); +rte_free (counter); } static int
@@ -1165,9 +1162,8 @@ sfc_mae_action_set_del(struct sfc_adapter *sa,sfc_mae_mac_addr_del (sa, action_set->src_mac_addr);sfc_mae_counter_del (sa, action_set->counter);TAILQ_REMOVE (&mae->action_sets, action_set, entries); -rte_free (action_set); -sfc_dbg (sa,"deleted action_set=%p" , action_set); +rte_free (action_set); } static int
@@ -1401,10 +1397,10 @@ sfc_mae_action_set_list_del(struct sfc_adapter *sa,sfc_mae_action_set_del (sa, action_set_list->action_sets[i]);TAILQ_REMOVE (&mae->action_set_lists, action_set_list, entries); +sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); +rte_free (action_set_list->action_sets);rte_free (action_set_list); - -sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); } static int
@@ -1667,9 +1663,8 @@ sfc_mae_action_rule_del(struct sfc_adapter *sa,sfc_mae_outer_rule_del (sa, rule->outer_rule);TAILQ_REMOVE (&mae->action_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted action_rule=%p" , rule); +rte_free (rule); } static int
Signature · 1 line
2.45.2
PATCH v7 10/16 net/cpfl: fix free of nonheap object
In reply to Stephen HemmingerWith proper annotation, GCC discovers that this driver is calling rte_free() on an object that was not allocated (it is part of array in another object).
In function ‘cpfl_flow_js_mr_layout’,
inlined from ‘cpfl_flow_js_mr_action’ at ../drivers/net/cpfl/cpfl_flow_parser.c:848:9,
inlined from ‘cpfl_flow_js_mod_rule’ at ../drivers/net/cpfl/cpfl_flow_parser.c:908:9,
inlined from ‘cpfl_parser_init’ at ../drivers/net/cpfl/cpfl_flow_parser.c:932:8,
inlined from ‘cpfl_parser_create’ at ../drivers/net/cpfl/cpfl_flow_parser.c:959:8:../drivers/net/cpfl/cpfl_flow_parser.c:740:9: warning: ‘rte_free’ called on pointer ‘*parser.modifications’ with nonzero offset [28, 15479062120396] [-Wfree-nonheap-object]
740 | rte_free(js_mod->layout);
| ^~~~~~~~~~~~~~~~~~~~~~~~--- drivers/net/cpfl/cpfl_flow_parser.c | 1 - 1 file changed, 1 deletion(-)
drivers/net/cpfl/cpfl_flow_parser.c +0 -1
@@ -737,7 +737,6 @@ cpfl_flow_js_mr_layout(json_t *ob_layouts, struct cpfl_flow_js_mr_action_mod *jsreturn 0 ; err: -rte_free (js_mod->layout);return -EINVAL ; }
Signature · 1 line
2.45.2
PATCH v7 11/16 net/nfp: fix duplicate call to rte_free
In reply to Stephen HemmingerCalling rte_free twice on same object will corrupt the heap.
Warning is:
In function 'nfp_pre_tun_table_check_del',
inlined from 'nfp_flow_destroy' at ../drivers/net/nfp/flower/nfp_flower_flow.c:5143:9:../drivers/net/nfp/flower/nfp_flower_flow.c:3830:9: error: pointer 'entry' used after 'rte_free' [-Werror=use-after-free]
3830 | rte_free(entry);
| ^~~~~~~~~~~~~~~../drivers/net/nfp/flower/nfp_flower_flow.c:3825:9: note: call to 'rte_free' here
3825 | rte_free(entry);
| ^~~~~~~~~~~~~~~
Bugzilla ID: 1555
--- drivers/net/nfp/flower/nfp_flower_flow.c | 1 - 1 file changed, 1 deletion(-)
drivers/net/nfp/flower/nfp_flower_flow.c +0 -1
@@ -3822,7 +3822,6 @@ nfp_pre_tun_table_check_del(struct nfp_flower_representor *repr, goto free_entry; } -rte_free (entry);rte_free (find_entry); priv->pre_tun_cnt--;
Signature · 1 line
2.45.2
PATCH v7 12/16 raw/ifpga/base: fix use after free
In reply to Stephen HemmingerThe TAILQ_FOREACH() macro would refer to info after it had been freed. Fix by introducing TAILQ_FOREACH_SAFE here.
--- drivers/raw/ifpga/base/opae_intel_max10.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/raw/ifpga/base/opae_intel_max10.c +9 -2
@@ -6,6 +6,13 @@#include <libfdt.h> #include "opae_osdep.h" +#ifndef TAILQ_FOREACH_SAFE +#define TAILQ_FOREACH_SAFE (var, head, field, tvar) \ + for ((var) = TAILQ_FIRST((head)); \ + (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) +#endif +int max10_sys_read (struct intel_max10_device *dev,unsigned int offset,unsigned int *val) {
@@ -746,9 +753,9 @@ static int fdt_get_named_reg(const void *fdt, int node, const char *name,static void max10_sensor_uinit (struct intel_max10_device *dev) { -struct opae_sensor_info *info; +struct opae_sensor_info *info, *next; - TAILQ_FOREACH(info, &dev->opae_sensor_list, node) { + TAILQ_FOREACH_SAFE(info, &dev->opae_sensor_list, node, next) {TAILQ_REMOVE (&dev->opae_sensor_list, info, node);opae_free (info); }
Signature · 1 line
2.45.2
PATCH v7 13/16 common/qat: fix use after free
In reply to Stephen HemmingerChecking return value of rte_memzone_free() is pointless and if it failed then it was because the pointer was null.
--- drivers/common/qat/qat_device.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/common/qat/qat_device.c +1 -5
@@ -390,11 +390,7 @@ qat_pci_device_allocate(struct rte_pci_device *pci_dev)return qat_dev; error:rte_free (qat_dev->command_line); -if (rte_memzone_free (qat_dev_mz)) { -QAT_LOG (DEBUG , -"QAT internal error! Trying to free already allocated memzone: %s" , - qat_dev_mz->name); - } +rte_memzone_free (qat_dev_mz);return NULL ; }
Signature · 1 line
2.45.2
PATCH v7 14/16 drivers/ifpga: fix free function mismatch
In reply to Stephen HemmingerThe raw ifpga driver redefines malloc to be opae_malloc and free to be opae_free; which is a bad idea.
This leads to case where interrupt efd array is allocated with calloc() and then passed to rte_free. The workaround is to allocate the array with rte_calloc() instead.
--- drivers/raw/ifpga/ifpga_rawdev.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/raw/ifpga/ifpga_rawdev.c +4 -4
@@ -1499,7 +1499,7 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, nb_intr =rte_intr_nb_intr_get (*intr_handle); - intr_efds =calloc (nb_intr,sizeof (int )); + intr_efds =rte_calloc ("ifpga_efds" , nb_intr,sizeof (int ),0 );if (!intr_efds)return -ENOMEM ;
@@ -1508,7 +1508,7 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, ret =opae_acc_set_irq (acc, vec_start, count, intr_efds);if (ret) { -free (intr_efds); +rte_free (intr_efds);return -EINVAL ; } }
@@ -1517,13 +1517,13 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, ret =rte_intr_callback_register (*intr_handle, handler, (void *)arg);if (ret) { -free (intr_efds); +rte_free (intr_efds);return -EINVAL ; }IFPGA_RAWDEV_PMD_INFO ("success register %s interrupt\n" , name); -free (intr_efds); +rte_free (intr_efds);return 0 ; }
Signature · 1 line
2.45.2
PATCH v7 15/16 baseband/la12xx: prevent use after free
In reply to Stephen HemmingerIt is possible that the info pointer (hp) could get freed twice. Fix by nulling after free.
In function 'setup_la12xx_dev',
inlined from 'la12xx_bbdev_create' at ../drivers/baseband/la12xx/bbdev_la12xx.c:1029:8,
inlined from 'la12xx_bbdev_probe' at ../drivers/baseband/la12xx/bbdev_la12xx.c:1075:9:../drivers/baseband/la12xx/bbdev_la12xx.c:901:9: error: pointer 'hp_info' may be used after 'rte_free' [-Werror=use-after-free]
901 | rte_free(hp);
| ^~~~~~~~~~~~../drivers/baseband/la12xx/bbdev_la12xx.c:791:17: note: call to 'rte_free' here
791 | rte_free(hp);
| ^~~~~~~~~~~~
--- drivers/baseband/la12xx/bbdev_la12xx.c | 1 + 1 file changed, 1 insertion(+)
drivers/baseband/la12xx/bbdev_la12xx.c +1 -0
@@ -789,6 +789,7 @@ setup_la12xx_dev(struct rte_bbdev *dev) ipc_priv->hugepg_start.size = hp->len;rte_free (hp); + hp =NULL ; } dev_ipc =open_ipc_dev (priv->modem_id);
Signature · 1 line
2.45.2
PATCH v7 16/16 common/idpf: fix use after free due
In reply to Stephen HemmingerThe macro in this driver was redefining LIST_FOR_EACH_ENTRY_SAFE as a simple LIST_FOR_EACH macro. But they are not the same the _SAFE variant guarantees that there will not be use after free.
--- drivers/common/idpf/base/idpf_osdep.h | 10 ++++++++-- drivers/common/idpf/idpf_common_device.c | 3 +-- 2 files changed, 9 insertions(+), 4 deletions(-)
drivers/common/idpf/base/idpf_osdep.h +8 -2
@@ -341,10 +341,16 @@ idpf_hweight32(u32 num)#define LIST_ENTRY_TYPE (type) LIST_ENTRY(type)#endif +#ifndef LIST_FOREACH_SAFE +#define LIST_FOREACH_SAFE (var, head, field, tvar) \ + for ((var) = LIST_FIRST((head)); \ + (var) && ((tvar) = LIST_NEXT((var), field), 1); \ + (var) = (tvar)) +#endif +#ifndef LIST_FOR_EACH_ENTRY_SAFE #define LIST_FOR_EACH_ENTRY_SAFE (pos, temp, head, entry_type, list) \ - LIST_FOREACH(pos, head, list) - + LIST_FOREACH_SAFE(pos, head, list, temp)#endif #ifndef LIST_FOR_EACH_ENTRY
drivers/common/idpf/idpf_common_device.c +1 -2
@@ -136,8 +136,7 @@ idpf_init_mbx(struct idpf_hw *hw)if (ret !=0 )return ret; -LIST_FOR_EACH_ENTRY_SAFE (ctlq,NULL , &hw->cq_list_head, - struct idpf_ctlq_info, cq_list) { + LIST_FOR_EACH_ENTRY(ctlq, &hw->cq_list_head, struct idpf_ctlq_info, cq_list) {if (ctlq->q_id ==IDPF_CTLQ_ID && ctlq->cq_type ==IDPF_CTLQ_TYPE_MAILBOX_TX ) hw->asq = ctlq;
Signature · 1 line
2.45.2
Re: [PATCH v7 00/16] Fix allocation bugs and prevent future ones
In reply to Stephen HemmingerQuoted 17 lines · Stephen Hemminger
Recent versions of GCC have some additional function attributes that can help with DPDK performance and stability.
The
alloc_alignattribute tells the compiler what the alignment of the allocation will be, and the optimizer can use this to produce better code (especially memcpy and structure copies).The malloc attribute tells compiler that object is not overlapping and potentially aliasing. It also as an additional variant in GCC 11 or later that allows for detecting all sorts of common errors like calling
free()on memory allocated withrte_malloc().In order to use the malloc attribute the free function prototype needs to be moved before the allocator/create function prototype so that the malloc attribute can refer to it.
Quoted 5 lines
This uncovered at least 16 pre-existing bugs in DPDK, these should go to stable. This patch set is structured with:
- fix any new warnings that were discovered
- add macros for enable the macros
- enable the attributes
It seems v7 lost the last patch that was introducing and using the annotations in rte_malloc.
Was there an issue with this patch, or is it just a series submission issue?
Signature · 1 line
David Marchand
Re: [PATCH v7 00/16] Fix allocation bugs and prevent future ones
Quoted 26 lines · David Marchand
Quoted 17 lines · Stephen Hemminger
Recent versions of GCC have some additional function attributes that can help with DPDK performance and stability.
The
alloc_alignattribute tells the compiler what the alignment of the allocation will be, and the optimizer can use this to produce better code (especially memcpy and structure copies).The malloc attribute tells compiler that object is not overlapping and potentially aliasing. It also as an additional variant in GCC 11 or later that allows for detecting all sorts of common errors like calling
free()on memory allocated withrte_malloc().In order to use the malloc attribute the free function prototype needs to be moved before the allocator/create function prototype so that the malloc attribute can refer to it.
Quoted 5 lines
This uncovered at least 16 pre-existing bugs in DPDK, these should go to stable. This patch set is structured with:
- fix any new warnings that were discovered
- add macros for enable the macros
- enable the attributes
Quoted 3 lines
It seems v7 lost the last patch that was introducing and using the annotations in
rte_malloc.
Was there an issue with this patch, or is it just a series submission issue?
Btw, reading gcc documentation, it seems the check is only enabled with -fanalyzer.
Can we add this in the CI?
If we can't enable all of -fanalyzer checks, maybe go with a subset of it, like -Wanalyzer-allocation-size -Wanalyzer-mismatching-deallocation ?
Signature · 1 line
David Marchand
Re: [PATCH v7 00/16] Fix allocation bugs and prevent future ones
Quoted 5 lines · On Fri, 4 Oct 2024 16:57:16 +0200 David Marchand <[email protected]>…
Quoted 4 lines
It seems v7 lost the last patch that was introducing and using the annotations in
rte_malloc.
Was there an issue with this patch, or is it just a series submission issue?
Quoted 7 lines
Btw, reading gcc documentation, it seems the check is only enabled with -fanalyzer.
Can we add this in the CI?If we can't enable all of -fanalyzer checks, maybe go with a subset of it, like -Wanalyzer-allocation-size -Wanalyzer-mismatching-deallocation ?
I didn't enable -fanalyzer in my build, and all these bugs showed up. Turning on full analyzer does give better and more through checking though.
Re: [PATCH v7 00/16] Fix allocation bugs and prevent future ones
Quoted 13 lines · Stephen Hemminger
Quoted 9 lines · On Fri, 4 Oct 2024 16:57:16 +0200 David Marchand <[email protected]>…
Quoted 4 lines
It seems v7 lost the last patch that was introducing and using the annotations in
rte_malloc.
Was there an issue with this patch, or is it just a series submission issue?Btw, reading gcc documentation, it seems the check is only enabled with -fanalyzer.
Can we add this in the CI?
Quoted 6 lines
Quoted 3 lines
If we can't enable all of -fanalyzer checks, maybe go with a subset of it, like -Wanalyzer-allocation-size -Wanalyzer-mismatching-deallocation ?
I didn't enable -fanalyzer in my build, and all these bugs showed up.
Ok, there was probably something wrong in my testing earlier. I can see the errors fine now that applied the annotations first...
This is a nice tool, let me see if I can get this in for rc1.
Signature · 1 line
David Marchand
v8 cover Add function attributes to uncover allocation bugs
In reply to Stephen HemmingerRecent versions of GCC have some additional function attributes that can help with DPDK performance and stability.
The alloc_align attribute tells the compiler what the alignment of the allocation will be, and the optimizer can use this to produce better code (especially memcpy and structure copies).
The malloc attribute tells compiler that object is not overlapping and potentially aliasing. It also as an additional variant in GCC 11 or later that allows for detecting all sorts of common errors like calling free() on memory allocated with rte_malloc().
In order to use the malloc attribute the free function prototype needs to be moved before the allocator/create function prototype so that the malloc attribute can refer to it.
This uncovered at least 16 pre-existing bugs in DPDK, these should go to stable. This patch set is structured with:
- fix any new warnings that were discovered
- add macros for enable the macros
- enable the attributes
The same attributes could be added to lots more functions in DPDK, but this patch set focuses on the key ones, and where problems exist in current code base.
v8 - rebase and cleanup the macros
Stephen Hemminger (17):
memzone: fix use after free in tracing cryptodev/bcmfs: fix mis-matched free dma/ixd: fix incorrect free function in cleanup event/cnxk: fix pointer mismatch in cleanup examples/vhost: fix free function mismatch net/cnxk: fix use-after-free
bpf: fix free mismatch if convert fails net/e1000: fix use-after-free
net/sfc: fix use-after-free warning messages net/cpfl: fix free of nonheap object net/nfp: fix duplicate call to rte_free raw/ifpga/base: fix use after free common/qat: fix use after free drivers/ifpga: fix free function mismatch baseband/la12xx: prevent use after free common/idpf: fix use after free due
eal: add function attributes for allocation functions
doc/guides/rel_notes/release_24_11.rst | 8 +++ drivers/baseband/la12xx/bbdev_la12xx.c | 1 + drivers/common/idpf/base/idpf_osdep.h | 10 +++- drivers/common/idpf/idpf_common_device.c | 3 +- drivers/common/qat/qat_device.c | 6 +-- drivers/crypto/bcmfs/bcmfs_device.c | 4 +- drivers/dma/idxd/idxd_pci.c | 2 +- drivers/event/cnxk/cnxk_eventdev.c | 4 +- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- drivers/net/cpfl/cpfl_flow_parser.c | 1 - drivers/net/e1000/igb_ethdev.c | 4 +- drivers/net/nfp/flower/nfp_flower_flow.c | 1 - drivers/net/sfc/sfc_flow_rss.c | 4 +- drivers/net/sfc/sfc_mae.c | 23 ++++----- drivers/raw/ifpga/base/opae_intel_max10.c | 11 ++++- drivers/raw/ifpga/ifpga_rawdev.c | 8 +-- examples/vhost_blk/vhost_blk.c | 2 +- lib/bpf/bpf_convert.c | 2 +- lib/eal/common/eal_common_memzone.c | 3 +- lib/eal/include/rte_common.h | 34 +++++++++++++ lib/eal/include/rte_malloc.h | 59 ++++++++++++++--------- 21 files changed, 126 insertions(+), 66 deletions(-)
Signature · 1 line
2.45.2
PATCH v8 1/17 memzone: fix use after free in tracing
Using the freed value for tracing is not a good idea. Although it is harmless for tracing, it will cause analyzers to flag this as unsafe.
--- lib/eal/common/eal_common_memzone.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/eal/common/eal_common_memzone.c +2 -1
@@ -331,9 +331,10 @@ rte_memzone_free(const struct rte_memzone *mz)rte_rwlock_write_unlock (&mcfg->mlock); +rte_eal_trace_memzone_free (name, addr, ret); +rte_free (addr); -rte_eal_trace_memzone_free (name, addr, ret);return ret; }
Signature · 1 line
2.45.2
PATCH v8 2/17 cryptodev/bcmfs: fix mis-matched free
In reply to Stephen HemmingerThe device structure is allocated with rte_malloc() and then incorrectly freed with free(). This will lead to corrupt malloc pool.
Bugzilla ID: 1552
--- drivers/crypto/bcmfs/bcmfs_device.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/crypto/bcmfs/bcmfs_device.c +2 -2
@@ -139,7 +139,7 @@ fsdev_allocate_one_dev(struct rte_vdev_device *vdev,return fsdev; cleanup: -free (fsdev); +rte_free (fsdev);return NULL ; }
@@ -163,7 +163,7 @@ fsdev_release(struct bcmfs_device *fsdev)return ;TAILQ_REMOVE (&fsdev_list, fsdev, next); -free (fsdev); +rte_free (fsdev); } static int
Signature · 1 line
2.45.2
PATCH v8 3/17 dma/ixd: fix incorrect free function in cleanup
In reply to Stephen HemmingerThe data structure is allocated with rte_malloc and incorrectly freed in cleanup logic using free.
Bugzilla ID: 1549
--- drivers/dma/idxd/idxd_pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma/idxd/idxd_pci.c +1 -1
@@ -301,7 +301,7 @@ init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,return nb_wqs; err: -free (pci); +rte_free (pci);return err_code; }
Signature · 1 line
2.45.2
PATCH v8 4/17 event/cnxk: fix pointer mismatch in cleanup
In reply to Stephen HemmingerThe code to cleanup in case of error was passing incorrect value to rte_free. The ports[] entry was allocated with rte_malloc and that should be used instead of the offset in that object.
--- drivers/event/cnxk/cnxk_eventdev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/event/cnxk/cnxk_eventdev.c +3 -1
@@ -121,8 +121,10 @@ cnxk_setup_event_ports(const struct rte_eventdev *event_dev,return 0 ; hws_fini:for (i = i -1 ; i >=0 ; i--) { +void *ws = event_dev->data->ports[i]; + event_dev->data->ports[i] =NULL ; -rte_free (cnxk_sso_hws_get_cookie (event_dev->data->ports[i])); +rte_free (ws); }return -ENOMEM ; }
Signature · 1 line
2.45.2
Re: [PATCH v8 04/17] event/cnxk: fix pointer mismatch in cleanup
Quoted 20 lines · On Tue, 8 Oct 2024 08:41:34 -0700 Stephen Hemminger…
The code to cleanup in case of error was passing incorrect value to
rte_free. The ports[] entry was allocated withrte_mallocand that should be used instead of the offset in that object.Signed-off-by Stephen Hemminger <[email protected]>--- drivers/event/cnxk/cnxk_eventdev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/event/cnxk/cnxk_eventdev.c +0
-0@@ -121,8 +121,10 @@ cnxk_setup_event_ports(const struct rte_eventdev *event_dev,return 0 ;
drivers/event/cnxk/cnxk_eventdev.c cnxk_setup_event_ports(const struct rte_eventdev *event_dev, · line 130
Quoted 10 lines
drivers/event/cnxk/cnxk_eventdev.c +3
-1hws_fini:for (i = i -1 ; i >=0 ; i--) { +void *ws = event_dev->data->ports[i]; + event_dev->data->ports[i] =NULL ; -rte_free (cnxk_sso_hws_get_cookie (event_dev->data->ports[i])); +rte_free (ws); }return -ENOMEM ; }
This fix is not right, but something is wrong with the original code?
[865/3024] Compiling C object drivers/libtmp_rte_event_cnxk.a.p/event_cnxk_cnxk_eventdev.c.o../drivers/event/cnxk/cnxk_eventdev.c: In function ‘cnxk_setup_event_ports’:../drivers/event/cnxk/cnxk_eventdev.c:125:17: warning: ‘rte_free’ called on a pointer to an unallocated object ‘18446744073709551552’ [-Wfree-nonheap-object]
125 | rte_free(cnxk_sso_hws_get_cookie(event_dev->data->ports[i]));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~[3024/3024] Linking target app/dpdk-test
RE: [EXTERNAL] Re: [PATCH v8 04/17] event/cnxk: fix pointer mismatch in cleanup
Quoted 40 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Tuesday, October 8, 2024 10:10 PM
To: [email protected]
Cc: Shijith Thotton <[email protected]>; [email protected]; Pavan Nikhilesh Bhagavatula <[email protected]>
Subject: [EXTERNAL] Re: [PATCH v8 04/17] event/cnxk: fix pointer mismatch in cleanupOn Tue, 8 Oct 2024 08: 41: 34 -0700 Stephen Hemminger
<stephen@ networkplumber. org> wrote: > The code to cleanup in case of error was passing incorrect > value torte_free. The ports[] entry was allocated with >rte_mallocand thatQuoted 9 lines · On Tue, 8 Oct 2024 08:41:34 -0700 Stephen Hemminger…
The code to cleanup in case of error was passing incorrect value to
rte_free. The ports[] entry was allocated withrte_mallocand that should be used instead of the offset in that object.Review
Quoted 6 lines
Signed-off-by Stephen Hemminger <[email protected]>--- drivers/event/cnxk/cnxk_eventdev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)(quoted diff) +0
-0
b/drivers/event/cnxk/cnxk_eventdev.cdrivers/event/cnxk/cnxk_eventdev.c cnxk_setup_event_ports(const struct
Quoted 4 lines
index
4b2d6bffa6..08c6ce0c07100644drivers/event/cnxk/cnxk_eventdev.c +0
-0@@ -121,8 +121,10 @@ cnxk_setup_event_ports(const struct
rte_eventdev*event_dev,
Quoted 15 lines
Quoted 11 lines
drivers/event/cnxk/cnxk_eventdev.c +3
-1return 0 ; hws_fini:for (i = i -1 ; i >=0 ; i--) { +void *ws = event_dev->data->ports[i]; + event_dev->data->ports[i] =NULL ; -rte_free (cnxk_sso_hws_get_cookie (event_dev->data- ports[i])); +rte_free (ws); }return -ENOMEM ;Quoted 1 line
}
This fix is not right, but something is wrong with the original code?
Yup, the NULL allocation should come after the free
rte_free(cnxk_sso_hws_get_cookie(event_dev->data->ports[i])); event_dev->data->ports[i] = NULL;
Quoted 11 lines
[865/3024] Compiling C object
drivers/libtmp_rte_event_cnxk.a.p/event_cnxk_cnxk_eventdev.c.o../drivers/event/cnxk/cnxk_eventdev.c: In function ‘cnxk_setup_event_ports’:../drivers/event/cnxk/cnxk_eventdev.c:125:17: warning: ‘rte_free’ called on a pointer to an unallocated object ‘18446744073709551552’ [-Wfree- nonheap-object]
125 | rte_free(cnxk_sso_hws_get_cookie(event_dev->data-Quoted 1 line
ports[i]));
|^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [3024/3024] Linking target app/dpdk-test
PATCH v8 5/17 examples/vhost: fix free function mismatch
In reply to Stephen HemmingerThe pointer bdev is allocated with rte_zmalloc() and then incorrectly freed with free() which will lead pool corruption.
Bugzilla ID: 1553
--- examples/vhost_blk/vhost_blk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
examples/vhost_blk/vhost_blk.c +1 -1
@@ -776,7 +776,7 @@ vhost_blk_bdev_construct(const char *bdev_name, bdev->data =rte_zmalloc (NULL , blk_cnt * blk_size,0 );if (!bdev->data) {fprintf (stderr,"No enough reserved huge memory for disk\n" ); -free (bdev); +rte_free (bdev);return NULL ; }
Signature · 1 line
2.45.2
PATCH v8 6/17 net/cnxk: fix use-after-free
In reply to Stephen HemmingerThe driver would refer to the mempool object after it was freed.
Bugzilla ID: 1554
--- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/cnxk/cnxk_ethdev_sec.c +1 -1
@@ -136,8 +136,8 @@ cnxk_nix_inl_custom_meta_pool_cb(uintptr_t pmpool, uintptr_t *mpool, const charreturn -EINVAL ; } -rte_mempool_free (hp);plt_free (hp->pool_config); +rte_mempool_free (hp); *aura_handle =0 ; *mpool =0 ;
Signature · 1 line
2.45.2
PATCH v8 7/17 bpf: fix free mismatch if convert fails
In reply to Stephen HemmingerIf conversion of cBF to eBPF fails then an object allocated with rte_malloc() would be passed to free().
[908/3201] Compiling C object lib/librte_bpf.a.p/bpf_bpf_convert.c.o../lib/bpf/bpf_convert.c: In function ‘rte_bpf_convert’:../lib/bpf/bpf_convert.c:559:17: warning: ‘free’ called on pointer returned from a mismatched allocation function [-Wmismatched-dealloc]
559 | free(prm);
| ^~~~~~~~~
../lib/bpf/bpf_convert.c:545:15: note: returned from ‘rte_zmalloc’
545 | prm = rte_zmalloc("bpf_filter",
| ^~~~~~~~~~~~~~~~~~~~~~~~~
546 | sizeof(*prm) + ebpf_len * sizeof(*ebpf), 0);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~--- lib/bpf/bpf_convert.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/bpf/bpf_convert.c +1 -1
@@ -556,7 +556,7 @@ rte_bpf_convert(const struct bpf_program *prog) ret =bpf_convert_filter (prog->bf_insns, prog->bf_len, ebpf, &ebpf_len);if (ret <0 ) {RTE_BPF_LOG_LINE (ERR ,"%s: cannot convert cBPF to eBPF" , __func__); -free (prm); +rte_free (prm); rte_errno = -ret;return NULL ; }
Signature · 1 line
2.45.2
PATCH v8 8/17 net/e1000: fix use-after-free
In reply to Stephen HemmingerThe driver cleanup code was freeing the filter object then dereferencing it.
Bugzilla ID: 1550
--- drivers/net/e1000/igb_ethdev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/e1000/igb_ethdev.c +2 -2
@@ -3907,11 +3907,11 @@ igb_delete_2tuple_filter(struct rte_eth_dev *dev, filter_info->twotuple_mask &= ~(1 << filter->index);TAILQ_REMOVE (&filter_info->twotuple_list, filter, entries); -rte_free (filter);E1000_WRITE_REG (hw,E1000_TTQF (filter->index),E1000_TTQF_DISABLE_MASK );E1000_WRITE_REG (hw,E1000_IMIR (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIREXT (filter->index),0 ); +rte_free (filter);return 0 ; }
@@ -4348,7 +4348,6 @@ igb_delete_5tuple_filter_82576(struct rte_eth_dev *dev, filter_info->fivetuple_mask &= ~(1 << filter->index);TAILQ_REMOVE (&filter_info->fivetuple_list, filter, entries); -rte_free (filter);E1000_WRITE_REG (hw,E1000_FTQF (filter->index),E1000_FTQF_VF_BP |E1000_FTQF_MASK );
@@ -4357,6 +4356,7 @@ igb_delete_5tuple_filter_82576(struct rte_eth_dev *dev,E1000_WRITE_REG (hw,E1000_SPQF (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIR (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIREXT (filter->index),0 ); +rte_free (filter);return 0 ; }
Signature · 1 line
2.45.2
PATCH v8 9/17 net/sfc: fix use-after-free warning messages
In reply to Stephen HemmingerIf compiler detection of use-after-free is enabled then this drivers debug messages will cause warnings. Change to move debug message before the object is freed.
Bugzilla ID: 1551
--- drivers/net/sfc/sfc_flow_rss.c | 4 ++-- drivers/net/sfc/sfc_mae.c | 23 +++++++++-------------- 2 files changed, 11 insertions(+), 16 deletions(-)
drivers/net/sfc/sfc_flow_rss.c +2 -2
@@ -303,9 +303,9 @@ sfc_flow_rss_ctx_del(struct sfc_adapter *sa, struct sfc_flow_rss_ctx *ctx)TAILQ_REMOVE (&flow_rss->ctx_list, ctx, entries);rte_free (ctx->qid_offsets); -rte_free (ctx); -sfc_dbg (sa,"flow-rss: deleted ctx=%p" , ctx); + +rte_free (ctx); } static int
drivers/net/sfc/sfc_mae.c +9 -14
@@ -400,9 +400,8 @@ sfc_mae_outer_rule_del(struct sfc_adapter *sa,efx_mae_match_spec_fini (sa->nic, rule->match_spec);TAILQ_REMOVE (&mae->outer_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted outer_rule=%p" , rule); +rte_free (rule); } static int
@@ -585,9 +584,8 @@ sfc_mae_mac_addr_del(struct sfc_adapter *sa, struct sfc_mae_mac_addr *mac_addr) }TAILQ_REMOVE (&mae->mac_addrs, mac_addr, entries); -rte_free (mac_addr); -sfc_dbg (sa,"deleted mac_addr=%p" , mac_addr); +rte_free (mac_addr); }enum sfc_mae_mac_addr_type {
@@ -785,10 +783,10 @@ sfc_mae_encap_header_del(struct sfc_adapter *sa, }TAILQ_REMOVE (&mae->encap_headers, encap_header, entries); +sfc_dbg (sa,"deleted encap_header=%p" , encap_header); +rte_free (encap_header->buf);rte_free (encap_header); - -sfc_dbg (sa,"deleted encap_header=%p" , encap_header); } static int
@@ -983,9 +981,8 @@ sfc_mae_counter_del(struct sfc_adapter *sa, struct sfc_mae_counter *counter) }TAILQ_REMOVE (&mae->counters, counter, entries); -rte_free (counter); -sfc_dbg (sa,"deleted counter=%p" , counter); +rte_free (counter); } static int
@@ -1165,9 +1162,8 @@ sfc_mae_action_set_del(struct sfc_adapter *sa,sfc_mae_mac_addr_del (sa, action_set->src_mac_addr);sfc_mae_counter_del (sa, action_set->counter);TAILQ_REMOVE (&mae->action_sets, action_set, entries); -rte_free (action_set); -sfc_dbg (sa,"deleted action_set=%p" , action_set); +rte_free (action_set); } static int
@@ -1401,10 +1397,10 @@ sfc_mae_action_set_list_del(struct sfc_adapter *sa,sfc_mae_action_set_del (sa, action_set_list->action_sets[i]);TAILQ_REMOVE (&mae->action_set_lists, action_set_list, entries); +sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); +rte_free (action_set_list->action_sets);rte_free (action_set_list); - -sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); } static int
@@ -1667,9 +1663,8 @@ sfc_mae_action_rule_del(struct sfc_adapter *sa,sfc_mae_outer_rule_del (sa, rule->outer_rule);TAILQ_REMOVE (&mae->action_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted action_rule=%p" , rule); +rte_free (rule); } static int
Signature · 1 line
2.45.2
PATCH v8 10/17 net/cpfl: fix free of nonheap object
In reply to Stephen HemmingerWith proper annotation, GCC discovers that this driver is calling rte_free() on an object that was not allocated (it is part of array in another object).
In function ‘cpfl_flow_js_mr_layout’,
inlined from ‘cpfl_flow_js_mr_action’ at ../drivers/net/cpfl/cpfl_flow_parser.c:848:9,
inlined from ‘cpfl_flow_js_mod_rule’ at ../drivers/net/cpfl/cpfl_flow_parser.c:908:9,
inlined from ‘cpfl_parser_init’ at ../drivers/net/cpfl/cpfl_flow_parser.c:932:8,
inlined from ‘cpfl_parser_create’ at ../drivers/net/cpfl/cpfl_flow_parser.c:959:8:../drivers/net/cpfl/cpfl_flow_parser.c:740:9: warning: ‘rte_free’ called on pointer ‘*parser.modifications’ with nonzero offset [28, 15479062120396] [-Wfree-nonheap-object]
740 | rte_free(js_mod->layout);
| ^~~~~~~~~~~~~~~~~~~~~~~~--- drivers/net/cpfl/cpfl_flow_parser.c | 1 - 1 file changed, 1 deletion(-)
drivers/net/cpfl/cpfl_flow_parser.c +0 -1
@@ -737,7 +737,6 @@ cpfl_flow_js_mr_layout(json_t *ob_layouts, struct cpfl_flow_js_mr_action_mod *jsreturn 0 ; err: -rte_free (js_mod->layout);return -EINVAL ; }
Signature · 1 line
2.45.2
PATCH v8 11/17 net/nfp: fix duplicate call to rte_free
In reply to Stephen HemmingerCalling rte_free twice on same object will corrupt the heap.
Warning is:
In function 'nfp_pre_tun_table_check_del',
inlined from 'nfp_flow_destroy' at ../drivers/net/nfp/flower/nfp_flower_flow.c:5143:9:../drivers/net/nfp/flower/nfp_flower_flow.c:3830:9: error: pointer 'entry' used after 'rte_free' [-Werror=use-after-free]
3830 | rte_free(entry);
| ^~~~~~~~~~~~~~~../drivers/net/nfp/flower/nfp_flower_flow.c:3825:9: note: call to 'rte_free' here
3825 | rte_free(entry);
| ^~~~~~~~~~~~~~~
Bugzilla ID: 1555
--- drivers/net/nfp/flower/nfp_flower_flow.c | 1 - 1 file changed, 1 deletion(-)
drivers/net/nfp/flower/nfp_flower_flow.c +0 -1
@@ -3822,7 +3822,6 @@ nfp_pre_tun_table_check_del(struct nfp_flower_representor *repr, goto free_entry; } -rte_free (entry);rte_free (find_entry); priv->pre_tun_cnt--;
Signature · 1 line
2.45.2
PATCH v8 12/17 raw/ifpga/base: fix use after free
In reply to Stephen HemmingerThe TAILQ_FOREACH() macro would refer to info after it had been freed. Fix by introducing TAILQ_FOREACH_SAFE here.
--- drivers/raw/ifpga/base/opae_intel_max10.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/raw/ifpga/base/opae_intel_max10.c +9 -2
@@ -6,6 +6,13 @@#include <libfdt.h> #include "opae_osdep.h" +#ifndef TAILQ_FOREACH_SAFE +#define TAILQ_FOREACH_SAFE (var, head, field, tvar) \ + for ((var) = TAILQ_FIRST((head)); \ + (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) +#endif +int max10_sys_read (struct intel_max10_device *dev,unsigned int offset,unsigned int *val) {
@@ -746,9 +753,9 @@ static int fdt_get_named_reg(const void *fdt, int node, const char *name,static void max10_sensor_uinit (struct intel_max10_device *dev) { -struct opae_sensor_info *info; +struct opae_sensor_info *info, *next; - TAILQ_FOREACH(info, &dev->opae_sensor_list, node) { + TAILQ_FOREACH_SAFE(info, &dev->opae_sensor_list, node, next) {TAILQ_REMOVE (&dev->opae_sensor_list, info, node);opae_free (info); }
Signature · 1 line
2.45.2
PATCH v8 13/17 common/qat: fix use after free
In reply to Stephen HemmingerChecking return value of rte_memzone_free() is pointless and if it failed then it was because the pointer was null.
--- drivers/common/qat/qat_device.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/common/qat/qat_device.c +1 -5
@@ -390,11 +390,7 @@ qat_pci_device_allocate(struct rte_pci_device *pci_dev)return qat_dev; error:rte_free (qat_dev->command_line); -if (rte_memzone_free (qat_dev_mz)) { -QAT_LOG (DEBUG , -"QAT internal error! Trying to free already allocated memzone: %s" , - qat_dev_mz->name); - } +rte_memzone_free (qat_dev_mz);return NULL ; }
Signature · 1 line
2.45.2
PATCH v8 14/17 drivers/ifpga: fix free function mismatch
In reply to Stephen HemmingerThe raw ifpga driver redefines malloc to be opae_malloc and free to be opae_free; which is a bad idea.
This leads to case where interrupt efd array is allocated with calloc() and then passed to rte_free. The workaround is to allocate the array with rte_calloc() instead.
--- drivers/raw/ifpga/ifpga_rawdev.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/raw/ifpga/ifpga_rawdev.c +4 -4
@@ -1499,7 +1499,7 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, nb_intr =rte_intr_nb_intr_get (*intr_handle); - intr_efds =calloc (nb_intr,sizeof (int )); + intr_efds =rte_calloc ("ifpga_efds" , nb_intr,sizeof (int ),0 );if (!intr_efds)return -ENOMEM ;
@@ -1508,7 +1508,7 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, ret =opae_acc_set_irq (acc, vec_start, count, intr_efds);if (ret) { -free (intr_efds); +rte_free (intr_efds);return -EINVAL ; } }
@@ -1517,13 +1517,13 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, ret =rte_intr_callback_register (*intr_handle, handler, (void *)arg);if (ret) { -free (intr_efds); +rte_free (intr_efds);return -EINVAL ; }IFPGA_RAWDEV_PMD_INFO ("success register %s interrupt" , name); -free (intr_efds); +rte_free (intr_efds);return 0 ; }
Signature · 1 line
2.45.2
PATCH v8 15/17 baseband/la12xx: prevent use after free
In reply to Stephen HemmingerIt is possible that the info pointer (hp) could get freed twice. Fix by nulling after free.
In function 'setup_la12xx_dev',
inlined from 'la12xx_bbdev_create' at ../drivers/baseband/la12xx/bbdev_la12xx.c:1029:8,
inlined from 'la12xx_bbdev_probe' at ../drivers/baseband/la12xx/bbdev_la12xx.c:1075:9:../drivers/baseband/la12xx/bbdev_la12xx.c:901:9: error: pointer 'hp_info' may be used after 'rte_free' [-Werror=use-after-free]
901 | rte_free(hp);
| ^~~~~~~~~~~~../drivers/baseband/la12xx/bbdev_la12xx.c:791:17: note: call to 'rte_free' here
791 | rte_free(hp);
| ^~~~~~~~~~~~
--- drivers/baseband/la12xx/bbdev_la12xx.c | 1 + 1 file changed, 1 insertion(+)
drivers/baseband/la12xx/bbdev_la12xx.c +1 -0
@@ -789,6 +789,7 @@ setup_la12xx_dev(struct rte_bbdev *dev) ipc_priv->hugepg_start.size = hp->len;rte_free (hp); + hp =NULL ; } dev_ipc =open_ipc_dev (priv->modem_id);
Signature · 1 line
2.45.2
PATCH v8 16/17 common/idpf: fix use after free due
In reply to Stephen HemmingerThe macro in this driver was redefining LIST_FOR_EACH_ENTRY_SAFE as a simple LIST_FOR_EACH macro. But they are not the same the _SAFE variant guarantees that there will not be use after free.
--- drivers/common/idpf/base/idpf_osdep.h | 10 ++++++++-- drivers/common/idpf/idpf_common_device.c | 3 +-- 2 files changed, 9 insertions(+), 4 deletions(-)
drivers/common/idpf/base/idpf_osdep.h +8 -2
@@ -341,10 +341,16 @@ idpf_hweight32(u32 num)#define LIST_ENTRY_TYPE (type) LIST_ENTRY(type)#endif +#ifndef LIST_FOREACH_SAFE +#define LIST_FOREACH_SAFE (var, head, field, tvar) \ + for ((var) = LIST_FIRST((head)); \ + (var) && ((tvar) = LIST_NEXT((var), field), 1); \ + (var) = (tvar)) +#endif +#ifndef LIST_FOR_EACH_ENTRY_SAFE #define LIST_FOR_EACH_ENTRY_SAFE (pos, temp, head, entry_type, list) \ - LIST_FOREACH(pos, head, list) - + LIST_FOREACH_SAFE(pos, head, list, temp)#endif #ifndef LIST_FOR_EACH_ENTRY
drivers/common/idpf/idpf_common_device.c +1 -2
@@ -136,8 +136,7 @@ idpf_init_mbx(struct idpf_hw *hw)if (ret !=0 )return ret; -LIST_FOR_EACH_ENTRY_SAFE (ctlq,NULL , &hw->cq_list_head, - struct idpf_ctlq_info, cq_list) { + LIST_FOR_EACH_ENTRY(ctlq, &hw->cq_list_head, struct idpf_ctlq_info, cq_list) {if (ctlq->q_id ==IDPF_CTLQ_ID && ctlq->cq_type ==IDPF_CTLQ_TYPE_MAILBOX_TX ) hw->asq = ctlq;
Signature · 1 line
2.45.2
PATCH v8 17/17 eal: add function attributes for allocation functions
In reply to Stephen HemmingerThe allocation functions take a alignment argument that can be useful to hint the compiler optimizer. This is supported by GCC and Clang but only useful with GCC because Clang gives warning if alignment is 0.
Newer versions of GCC have a malloc attribute that can be used to find mismatches between allocation and free; the typical problem caught is a pointer allocated with rte_malloc() that is then incorrectly freed using free(). The name of the DPDK wrapper macros for these attributes are chosen to be similar to what GLIBC is using in cdefs.h.
Note: The rte_free function prototype was moved ahead of the allocation functions since the dealloc attribute now refers to it.
--- doc/guides/rel_notes/release_24_11.rst | 8 ++++ lib/eal/include/rte_common.h | 34 +++++++++++++++ lib/eal/include/rte_malloc.h | 59 ++++++++++++++++---------- 3 files changed, 78 insertions(+), 23 deletions(-)
doc/guides/rel_notes/release_24_11.rst +8 -0
@@ -67,6 +67,14 @@ New Features The new statistics are useful for debugging and profiling. +* **Hardened rte_malloc and related functions.** + + * Added function attributes to ``rte_malloc`` and similar functions + that can catch some obvious bugs at compile time (with GCC 11.0 or later). + Examples: calling ``free()`` on pointer that was allocated with ``rte_malloc`` + (and vice versa); freeing the same pointer twice in the same routine; + freeing an object that was not created by allocation; etc. + Removed Items -------------
lib/eal/include/rte_common.h +34 -0
@@ -228,6 +228,40 @@ typedef uint16_t unaligned_uint16_t;#define __rte_alloc_size (...)#endif +/** +* Tells the compiler that the function returns a value that points to +* memory aligned by a function argument. +* +* Note: not enabled on Clang because it warns if align argument is zero. +*/ +#if defined(RTE_CC_GCC ) +#define __rte_alloc_align (argno) \ + __attribute__((alloc_align(argno))) +#else +#define __rte_alloc_align (argno) +#endif + +/** +* Tells the compiler this is a function like malloc and that the pointer +* returned cannot alias any other pointer (ie new memory). +*/ +#if defined(RTE_CC_GCC ) || defined(RTE_CC_CLANG ) +#define __rte_malloc __attribute__((__malloc__)) +#else +#define __rte_malloc +#endif + +/** +* With recent GCC versions also able to track that proper +* deallocator function is used for this pointer. +*/ +#if defined(RTE_TOOLCHAIN_GCC ) && (GCC_VERSION >=110000 ) +#define __rte_dealloc (dealloc, argno) \ + __attribute__((malloc(dealloc, argno))) +#else +#define __rte_dealloc (dealloc, argno) +#endif +#define RTE_PRIORITY_LOG 101#define RTE_PRIORITY_BUS 110#define RTE_PRIORITY_CLASS 120
lib/eal/include/rte_malloc.h +36 -23
113 lines not on this page · raw message
Signature · 1 line
2.45.2
v9 cover Use malloc function attribute to uncover bugs
In reply to Stephen HemmingerRecent versions of GCC have some additional function attributes that can help with DPDK performance and stability.
The alloc_align attribute tells the compiler what the alignment of the allocation will be, and the optimizer can use this to produce better code (especially memcpy and structure copies).
The malloc attribute tells compiler that object is not overlapping and potentially aliasing. It also as an additional variant in GCC 11 or later that allows for detecting all sorts of common errors like calling free() on memory allocated with rte_malloc().
In order to use the malloc attribute the free function prototype needs to be moved before the allocator/create function prototype so that the malloc attribute can refer to it.
This uncovered at least 16 pre-existing bugs in DPDK, these should go to stable. This patch set is structured with:
- fix any new warnings that were discovered
- add macros for enable the macros
- enable the attributes
The same attributes could be added to lots more functions in DPDK, but this patch set focuses on the key ones, and where problems exist in current code base.
v9 - correct the event/cnxk patch
Stephen Hemminger (17):
memzone: fix use after free in tracing cryptodev/bcmfs: fix mis-matched free dma/ixd: fix incorrect free function in cleanup event/cnxk: fix free of non-heap in cleanup code examples/vhost: fix free function mismatch net/cnxk: fix use-after-free
bpf: fix free mismatch if convert fails net/e1000: fix use-after-free
net/sfc: fix use-after-free warning messages net/cpfl: fix free of nonheap object net/nfp: fix duplicate call to rte_free raw/ifpga/base: fix use after free common/qat: fix use after free drivers/ifpga: fix free function mismatch baseband/la12xx: prevent use after free common/idpf: fix use after free due
eal: add function attributes for allocation functions
doc/guides/rel_notes/release_24_11.rst | 8 +++ drivers/baseband/la12xx/bbdev_la12xx.c | 1 + drivers/common/idpf/base/idpf_osdep.h | 10 +++- drivers/common/idpf/idpf_common_device.c | 3 +- drivers/common/qat/qat_device.c | 6 +-- drivers/crypto/bcmfs/bcmfs_device.c | 4 +- drivers/dma/idxd/idxd_pci.c | 2 +- drivers/event/cnxk/cnxk_eventdev.c | 2 +- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- drivers/net/cpfl/cpfl_flow_parser.c | 1 - drivers/net/e1000/igb_ethdev.c | 4 +- drivers/net/nfp/flower/nfp_flower_flow.c | 1 - drivers/net/sfc/sfc_flow_rss.c | 4 +- drivers/net/sfc/sfc_mae.c | 23 ++++----- drivers/raw/ifpga/base/opae_intel_max10.c | 11 ++++- drivers/raw/ifpga/ifpga_rawdev.c | 8 +-- examples/vhost_blk/vhost_blk.c | 2 +- lib/bpf/bpf_convert.c | 2 +- lib/eal/common/eal_common_memzone.c | 3 +- lib/eal/include/rte_common.h | 34 +++++++++++++ lib/eal/include/rte_malloc.h | 59 ++++++++++++++--------- 21 files changed, 124 insertions(+), 66 deletions(-)
Signature · 1 line
2.45.2
PATCH v9 1/17 memzone: fix use after free in tracing
Using the freed value for tracing is not a good idea. Although it is harmless for tracing, it will cause analyzers to flag this as unsafe.
--- lib/eal/common/eal_common_memzone.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/eal/common/eal_common_memzone.c +2 -1
@@ -331,9 +331,10 @@ rte_memzone_free(const struct rte_memzone *mz)rte_rwlock_write_unlock (&mcfg->mlock); +rte_eal_trace_memzone_free (name, addr, ret); +rte_free (addr); -rte_eal_trace_memzone_free (name, addr, ret);return ret; }
Signature · 1 line
2.45.2
PATCH v9 2/17 cryptodev/bcmfs: fix mis-matched free
In reply to Stephen HemmingerThe device structure is allocated with rte_malloc() and then incorrectly freed with free(). This will lead to corrupt malloc pool.
Bugzilla ID: 1552
--- drivers/crypto/bcmfs/bcmfs_device.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/crypto/bcmfs/bcmfs_device.c +2 -2
@@ -139,7 +139,7 @@ fsdev_allocate_one_dev(struct rte_vdev_device *vdev,return fsdev; cleanup: -free (fsdev); +rte_free (fsdev);return NULL ; }
@@ -163,7 +163,7 @@ fsdev_release(struct bcmfs_device *fsdev)return ;TAILQ_REMOVE (&fsdev_list, fsdev, next); -free (fsdev); +rte_free (fsdev); } static int
Signature · 1 line
2.45.2
PATCH v9 3/17 dma/ixd: fix incorrect free function in cleanup
In reply to Stephen HemmingerThe data structure is allocated with rte_malloc and incorrectly freed in cleanup logic using free.
Bugzilla ID: 1549
--- drivers/dma/idxd/idxd_pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma/idxd/idxd_pci.c +1 -1
@@ -301,7 +301,7 @@ init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,return nb_wqs; err: -free (pci); +rte_free (pci);return err_code; }
Signature · 1 line
2.45.2
PATCH v9 4/17 event/cnxk: fix free of non-heap in cleanup code
In reply to Stephen HemmingerThe code to cleanup in case of error would derefence null pointer then pass that result to rte_free.
--- drivers/event/cnxk/cnxk_eventdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/event/cnxk/cnxk_eventdev.c +1 -1
@@ -121,8 +121,8 @@ cnxk_setup_event_ports(const struct rte_eventdev *event_dev,return 0 ; hws_fini:for (i = i -1 ; i >=0 ; i--) { - event_dev->data->ports[i] =NULL ;rte_free (cnxk_sso_hws_get_cookie (event_dev->data->ports[i])); + event_dev->data->ports[i] =NULL ; }return -ENOMEM ; }
Signature · 1 line
2.45.2
RE: [EXTERNAL] [PATCH v9 04/17] event/cnxk: fix free of non-heap in cleanup code
Quoted 8 lines
The code to cleanup in case of error would derefence null pointer then pass that result to
rte_free.Signed-off-by Stephen Hemminger <[email protected]>
Thanks Stephen,
PATCH v9 5/17 examples/vhost: fix free function mismatch
In reply to Stephen HemmingerThe pointer bdev is allocated with rte_zmalloc() and then incorrectly freed with free() which will lead pool corruption.
Bugzilla ID: 1553
--- examples/vhost_blk/vhost_blk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
examples/vhost_blk/vhost_blk.c +1 -1
@@ -776,7 +776,7 @@ vhost_blk_bdev_construct(const char *bdev_name, bdev->data =rte_zmalloc (NULL , blk_cnt * blk_size,0 );if (!bdev->data) {fprintf (stderr,"No enough reserved huge memory for disk\n" ); -free (bdev); +rte_free (bdev);return NULL ; }
Signature · 1 line
2.45.2
Re: [PATCH v9 05/17] examples/vhost: fix free function mismatch
Quoted 33 lines · Stephen Hemminger
External email: Use caution opening links or attachments
The pointer bdev is allocated with
rte_zmalloc()and then incorrectly freed withfree()which will lead pool corruption.Bugzilla ID: 1553
Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage sample")Signed-off-by Stephen Hemminger <[email protected]>Acked-by Chengwen Feng <[email protected]>---
examples/vhost_blk/vhost_blk.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)examples/vhost_blk/vhost_blk.c +1
-2@@ -776,7 +776,7 @@ vhost_blk_bdev_construct(const char *bdev_name, bdev->data =rte_zmalloc (NULL , blk_cnt * blk_size,0 );if (!bdev->data) {fprintf (stderr,"No enough reserved huge memory for disk\n" ); -free (bdev); +rte_free (bdev);return NULL ; } --
Quoted 2 lines
2.45.2
PATCH v9 6/17 net/cnxk: fix use-after-free
In reply to Stephen HemmingerThe driver would refer to the mempool object after it was freed.
Bugzilla ID: 1554
--- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/cnxk/cnxk_ethdev_sec.c +1 -1
@@ -136,8 +136,8 @@ cnxk_nix_inl_custom_meta_pool_cb(uintptr_t pmpool, uintptr_t *mpool, const charreturn -EINVAL ; } -rte_mempool_free (hp);plt_free (hp->pool_config); +rte_mempool_free (hp); *aura_handle =0 ; *mpool =0 ;
Signature · 1 line
2.45.2
PATCH v9 7/17 bpf: fix free mismatch if convert fails
In reply to Stephen HemmingerIf conversion of cBF to eBPF fails then an object allocated with rte_malloc() would be passed to free().
[908/3201] Compiling C object lib/librte_bpf.a.p/bpf_bpf_convert.c.o../lib/bpf/bpf_convert.c: In function ‘rte_bpf_convert’:../lib/bpf/bpf_convert.c:559:17: warning: ‘free’ called on pointer returned from a mismatched allocation function [-Wmismatched-dealloc]
559 | free(prm);
| ^~~~~~~~~
../lib/bpf/bpf_convert.c:545:15: note: returned from ‘rte_zmalloc’
545 | prm = rte_zmalloc("bpf_filter",
| ^~~~~~~~~~~~~~~~~~~~~~~~~
546 | sizeof(*prm) + ebpf_len * sizeof(*ebpf), 0);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~--- lib/bpf/bpf_convert.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/bpf/bpf_convert.c +1 -1
@@ -556,7 +556,7 @@ rte_bpf_convert(const struct bpf_program *prog) ret =bpf_convert_filter (prog->bf_insns, prog->bf_len, ebpf, &ebpf_len);if (ret <0 ) {RTE_BPF_LOG_LINE (ERR ,"%s: cannot convert cBPF to eBPF" , __func__); -free (prm); +rte_free (prm); rte_errno = -ret;return NULL ; }
Signature · 1 line
2.45.2
PATCH v9 8/17 net/e1000: fix use-after-free
In reply to Stephen HemmingerThe driver cleanup code was freeing the filter object then dereferencing it.
Bugzilla ID: 1550
--- drivers/net/e1000/igb_ethdev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/e1000/igb_ethdev.c +2 -2
@@ -3907,11 +3907,11 @@ igb_delete_2tuple_filter(struct rte_eth_dev *dev, filter_info->twotuple_mask &= ~(1 << filter->index);TAILQ_REMOVE (&filter_info->twotuple_list, filter, entries); -rte_free (filter);E1000_WRITE_REG (hw,E1000_TTQF (filter->index),E1000_TTQF_DISABLE_MASK );E1000_WRITE_REG (hw,E1000_IMIR (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIREXT (filter->index),0 ); +rte_free (filter);return 0 ; }
@@ -4348,7 +4348,6 @@ igb_delete_5tuple_filter_82576(struct rte_eth_dev *dev, filter_info->fivetuple_mask &= ~(1 << filter->index);TAILQ_REMOVE (&filter_info->fivetuple_list, filter, entries); -rte_free (filter);E1000_WRITE_REG (hw,E1000_FTQF (filter->index),E1000_FTQF_VF_BP |E1000_FTQF_MASK );
@@ -4357,6 +4356,7 @@ igb_delete_5tuple_filter_82576(struct rte_eth_dev *dev,E1000_WRITE_REG (hw,E1000_SPQF (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIR (filter->index),0 );E1000_WRITE_REG (hw,E1000_IMIREXT (filter->index),0 ); +rte_free (filter);return 0 ; }
Signature · 1 line
2.45.2
PATCH v9 9/17 net/sfc: fix use-after-free warning messages
In reply to Stephen HemmingerIf compiler detection of use-after-free is enabled then this drivers debug messages will cause warnings. Change to move debug message before the object is freed.
Bugzilla ID: 1551
--- drivers/net/sfc/sfc_flow_rss.c | 4 ++-- drivers/net/sfc/sfc_mae.c | 23 +++++++++-------------- 2 files changed, 11 insertions(+), 16 deletions(-)
drivers/net/sfc/sfc_flow_rss.c +2 -2
@@ -303,9 +303,9 @@ sfc_flow_rss_ctx_del(struct sfc_adapter *sa, struct sfc_flow_rss_ctx *ctx)TAILQ_REMOVE (&flow_rss->ctx_list, ctx, entries);rte_free (ctx->qid_offsets); -rte_free (ctx); -sfc_dbg (sa,"flow-rss: deleted ctx=%p" , ctx); + +rte_free (ctx); } static int
drivers/net/sfc/sfc_mae.c +9 -14
@@ -400,9 +400,8 @@ sfc_mae_outer_rule_del(struct sfc_adapter *sa,efx_mae_match_spec_fini (sa->nic, rule->match_spec);TAILQ_REMOVE (&mae->outer_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted outer_rule=%p" , rule); +rte_free (rule); } static int
@@ -585,9 +584,8 @@ sfc_mae_mac_addr_del(struct sfc_adapter *sa, struct sfc_mae_mac_addr *mac_addr) }TAILQ_REMOVE (&mae->mac_addrs, mac_addr, entries); -rte_free (mac_addr); -sfc_dbg (sa,"deleted mac_addr=%p" , mac_addr); +rte_free (mac_addr); }enum sfc_mae_mac_addr_type {
@@ -785,10 +783,10 @@ sfc_mae_encap_header_del(struct sfc_adapter *sa, }TAILQ_REMOVE (&mae->encap_headers, encap_header, entries); +sfc_dbg (sa,"deleted encap_header=%p" , encap_header); +rte_free (encap_header->buf);rte_free (encap_header); - -sfc_dbg (sa,"deleted encap_header=%p" , encap_header); } static int
@@ -983,9 +981,8 @@ sfc_mae_counter_del(struct sfc_adapter *sa, struct sfc_mae_counter *counter) }TAILQ_REMOVE (&mae->counters, counter, entries); -rte_free (counter); -sfc_dbg (sa,"deleted counter=%p" , counter); +rte_free (counter); } static int
@@ -1165,9 +1162,8 @@ sfc_mae_action_set_del(struct sfc_adapter *sa,sfc_mae_mac_addr_del (sa, action_set->src_mac_addr);sfc_mae_counter_del (sa, action_set->counter);TAILQ_REMOVE (&mae->action_sets, action_set, entries); -rte_free (action_set); -sfc_dbg (sa,"deleted action_set=%p" , action_set); +rte_free (action_set); } static int
@@ -1401,10 +1397,10 @@ sfc_mae_action_set_list_del(struct sfc_adapter *sa,sfc_mae_action_set_del (sa, action_set_list->action_sets[i]);TAILQ_REMOVE (&mae->action_set_lists, action_set_list, entries); +sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); +rte_free (action_set_list->action_sets);rte_free (action_set_list); - -sfc_dbg (sa,"deleted action_set_list=%p" , action_set_list); } static int
@@ -1667,9 +1663,8 @@ sfc_mae_action_rule_del(struct sfc_adapter *sa,sfc_mae_outer_rule_del (sa, rule->outer_rule);TAILQ_REMOVE (&mae->action_rules, rule, entries); -rte_free (rule); -sfc_dbg (sa,"deleted action_rule=%p" , rule); +rte_free (rule); } static int
Signature · 1 line
2.45.2
PATCH v9 10/17 net/cpfl: fix free of nonheap object
In reply to Stephen HemmingerWith proper annotation, GCC discovers that this driver is calling rte_free() on an object that was not allocated (it is part of array in another object).
In function ‘cpfl_flow_js_mr_layout’,
inlined from ‘cpfl_flow_js_mr_action’ at ../drivers/net/cpfl/cpfl_flow_parser.c:848:9,
inlined from ‘cpfl_flow_js_mod_rule’ at ../drivers/net/cpfl/cpfl_flow_parser.c:908:9,
inlined from ‘cpfl_parser_init’ at ../drivers/net/cpfl/cpfl_flow_parser.c:932:8,
inlined from ‘cpfl_parser_create’ at ../drivers/net/cpfl/cpfl_flow_parser.c:959:8:../drivers/net/cpfl/cpfl_flow_parser.c:740:9: warning: ‘rte_free’ called on pointer ‘*parser.modifications’ with nonzero offset [28, 15479062120396] [-Wfree-nonheap-object]
740 | rte_free(js_mod->layout);
| ^~~~~~~~~~~~~~~~~~~~~~~~--- drivers/net/cpfl/cpfl_flow_parser.c | 1 - 1 file changed, 1 deletion(-)
drivers/net/cpfl/cpfl_flow_parser.c +0 -1
@@ -737,7 +737,6 @@ cpfl_flow_js_mr_layout(json_t *ob_layouts, struct cpfl_flow_js_mr_action_mod *jsreturn 0 ; err: -rte_free (js_mod->layout);return -EINVAL ; }
Signature · 1 line
2.45.2
PATCH v9 11/17 net/nfp: fix duplicate call to rte_free
In reply to Stephen HemmingerCalling rte_free twice on same object will corrupt the heap.
Warning is:
In function 'nfp_pre_tun_table_check_del',
inlined from 'nfp_flow_destroy' at ../drivers/net/nfp/flower/nfp_flower_flow.c:5143:9:../drivers/net/nfp/flower/nfp_flower_flow.c:3830:9: error: pointer 'entry' used after 'rte_free' [-Werror=use-after-free]
3830 | rte_free(entry);
| ^~~~~~~~~~~~~~~../drivers/net/nfp/flower/nfp_flower_flow.c:3825:9: note: call to 'rte_free' here
3825 | rte_free(entry);
| ^~~~~~~~~~~~~~~
Bugzilla ID: 1555
--- drivers/net/nfp/flower/nfp_flower_flow.c | 1 - 1 file changed, 1 deletion(-)
drivers/net/nfp/flower/nfp_flower_flow.c +0 -1
@@ -3822,7 +3822,6 @@ nfp_pre_tun_table_check_del(struct nfp_flower_representor *repr, goto free_entry; } -rte_free (entry);rte_free (find_entry); priv->pre_tun_cnt--;
Signature · 1 line
2.45.2
PATCH v9 12/17 raw/ifpga/base: fix use after free
In reply to Stephen HemmingerThe TAILQ_FOREACH() macro would refer to info after it had been freed. Fix by introducing TAILQ_FOREACH_SAFE here.
--- drivers/raw/ifpga/base/opae_intel_max10.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/raw/ifpga/base/opae_intel_max10.c +9 -2
@@ -6,6 +6,13 @@#include <libfdt.h> #include "opae_osdep.h" +#ifndef TAILQ_FOREACH_SAFE +#define TAILQ_FOREACH_SAFE (var, head, field, tvar) \ + for ((var) = TAILQ_FIRST((head)); \ + (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) +#endif +int max10_sys_read (struct intel_max10_device *dev,unsigned int offset,unsigned int *val) {
@@ -746,9 +753,9 @@ static int fdt_get_named_reg(const void *fdt, int node, const char *name,static void max10_sensor_uinit (struct intel_max10_device *dev) { -struct opae_sensor_info *info; +struct opae_sensor_info *info, *next; - TAILQ_FOREACH(info, &dev->opae_sensor_list, node) { + TAILQ_FOREACH_SAFE(info, &dev->opae_sensor_list, node, next) {TAILQ_REMOVE (&dev->opae_sensor_list, info, node);opae_free (info); }
Signature · 1 line
2.45.2
PATCH v9 13/17 common/qat: fix use after free
In reply to Stephen HemmingerChecking return value of rte_memzone_free() is pointless and if it failed then it was because the pointer was null.
--- drivers/common/qat/qat_device.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/common/qat/qat_device.c +1 -5
@@ -390,11 +390,7 @@ qat_pci_device_allocate(struct rte_pci_device *pci_dev)return qat_dev; error:rte_free (qat_dev->command_line); -if (rte_memzone_free (qat_dev_mz)) { -QAT_LOG (DEBUG , -"QAT internal error! Trying to free already allocated memzone: %s" , - qat_dev_mz->name); - } +rte_memzone_free (qat_dev_mz);return NULL ; }
Signature · 1 line
2.45.2
PATCH v9 14/17 drivers/ifpga: fix free function mismatch
In reply to Stephen HemmingerThe raw ifpga driver redefines malloc to be opae_malloc and free to be opae_free; which is a bad idea.
This leads to case where interrupt efd array is allocated with calloc() and then passed to rte_free. The workaround is to allocate the array with rte_calloc() instead.
--- drivers/raw/ifpga/ifpga_rawdev.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/raw/ifpga/ifpga_rawdev.c +4 -4
@@ -1499,7 +1499,7 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, nb_intr =rte_intr_nb_intr_get (*intr_handle); - intr_efds =calloc (nb_intr,sizeof (int )); + intr_efds =rte_calloc ("ifpga_efds" , nb_intr,sizeof (int ),0 );if (!intr_efds)return -ENOMEM ;
@@ -1508,7 +1508,7 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, ret =opae_acc_set_irq (acc, vec_start, count, intr_efds);if (ret) { -free (intr_efds); +rte_free (intr_efds);return -EINVAL ; } }
@@ -1517,13 +1517,13 @@ ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id, ret =rte_intr_callback_register (*intr_handle, handler, (void *)arg);if (ret) { -free (intr_efds); +rte_free (intr_efds);return -EINVAL ; }IFPGA_RAWDEV_PMD_INFO ("success register %s interrupt" , name); -free (intr_efds); +rte_free (intr_efds);return 0 ; }
Signature · 1 line
2.45.2
PATCH v9 15/17 baseband/la12xx: prevent use after free
In reply to Stephen HemmingerIt is possible that the info pointer (hp) could get freed twice. Fix by nulling after free.
In function 'setup_la12xx_dev',
inlined from 'la12xx_bbdev_create' at ../drivers/baseband/la12xx/bbdev_la12xx.c:1029:8,
inlined from 'la12xx_bbdev_probe' at ../drivers/baseband/la12xx/bbdev_la12xx.c:1075:9:../drivers/baseband/la12xx/bbdev_la12xx.c:901:9: error: pointer 'hp_info' may be used after 'rte_free' [-Werror=use-after-free]
901 | rte_free(hp);
| ^~~~~~~~~~~~../drivers/baseband/la12xx/bbdev_la12xx.c:791:17: note: call to 'rte_free' here
791 | rte_free(hp);
| ^~~~~~~~~~~~
--- drivers/baseband/la12xx/bbdev_la12xx.c | 1 + 1 file changed, 1 insertion(+)
drivers/baseband/la12xx/bbdev_la12xx.c +1 -0
@@ -789,6 +789,7 @@ setup_la12xx_dev(struct rte_bbdev *dev) ipc_priv->hugepg_start.size = hp->len;rte_free (hp); + hp =NULL ; } dev_ipc =open_ipc_dev (priv->modem_id);
Signature · 1 line
2.45.2
PATCH v9 16/17 common/idpf: fix use after free due
In reply to Stephen HemmingerThe macro in this driver was redefining LIST_FOR_EACH_ENTRY_SAFE as a simple LIST_FOR_EACH macro. But they are not the same the _SAFE variant guarantees that there will not be use after free.
--- drivers/common/idpf/base/idpf_osdep.h | 10 ++++++++-- drivers/common/idpf/idpf_common_device.c | 3 +-- 2 files changed, 9 insertions(+), 4 deletions(-)
drivers/common/idpf/base/idpf_osdep.h +8 -2
@@ -341,10 +341,16 @@ idpf_hweight32(u32 num)#define LIST_ENTRY_TYPE (type) LIST_ENTRY(type)#endif +#ifndef LIST_FOREACH_SAFE +#define LIST_FOREACH_SAFE (var, head, field, tvar) \ + for ((var) = LIST_FIRST((head)); \ + (var) && ((tvar) = LIST_NEXT((var), field), 1); \ + (var) = (tvar)) +#endif +#ifndef LIST_FOR_EACH_ENTRY_SAFE #define LIST_FOR_EACH_ENTRY_SAFE (pos, temp, head, entry_type, list) \ - LIST_FOREACH(pos, head, list) - + LIST_FOREACH_SAFE(pos, head, list, temp)#endif #ifndef LIST_FOR_EACH_ENTRY
drivers/common/idpf/idpf_common_device.c +1 -2
@@ -136,8 +136,7 @@ idpf_init_mbx(struct idpf_hw *hw)if (ret !=0 )return ret; -LIST_FOR_EACH_ENTRY_SAFE (ctlq,NULL , &hw->cq_list_head, - struct idpf_ctlq_info, cq_list) { + LIST_FOR_EACH_ENTRY(ctlq, &hw->cq_list_head, struct idpf_ctlq_info, cq_list) {if (ctlq->q_id ==IDPF_CTLQ_ID && ctlq->cq_type ==IDPF_CTLQ_TYPE_MAILBOX_TX ) hw->asq = ctlq;
Signature · 1 line
2.45.2
PATCH v9 17/17 eal: add function attributes for allocation functions
In reply to Stephen HemmingerThe allocation functions take a alignment argument that can be useful to hint the compiler optimizer. This is supported by GCC and Clang but only useful with GCC because Clang gives warning if alignment is 0.
Newer versions of GCC have a malloc attribute that can be used to find mismatches between allocation and free; the typical problem caught is a pointer allocated with rte_malloc() that is then incorrectly freed using free(). The name of the DPDK wrapper macros for these attributes are chosen to be similar to what GLIBC is using in cdefs.h.
Note: The rte_free function prototype was moved ahead of the allocation functions since the dealloc attribute now refers to it.
--- doc/guides/rel_notes/release_24_11.rst | 8 ++++ lib/eal/include/rte_common.h | 34 +++++++++++++++ lib/eal/include/rte_malloc.h | 59 ++++++++++++++++---------- 3 files changed, 78 insertions(+), 23 deletions(-)
doc/guides/rel_notes/release_24_11.rst +8 -0
@@ -67,6 +67,14 @@ New Features The new statistics are useful for debugging and profiling. +* **Hardened rte_malloc and related functions.** + + * Added function attributes to ``rte_malloc`` and similar functions + that can catch some obvious bugs at compile time (with GCC 11.0 or later). + Examples: calling ``free()`` on pointer that was allocated with ``rte_malloc`` + (and vice versa); freeing the same pointer twice in the same routine; + freeing an object that was not created by allocation; etc. + Removed Items -------------
lib/eal/include/rte_common.h +34 -0
@@ -228,6 +228,40 @@ typedef uint16_t unaligned_uint16_t;#define __rte_alloc_size (...)#endif +/** +* Tells the compiler that the function returns a value that points to +* memory aligned by a function argument. +* +* Note: not enabled on Clang because it warns if align argument is zero. +*/ +#if defined(RTE_CC_GCC ) +#define __rte_alloc_align (argno) \ + __attribute__((alloc_align(argno))) +#else +#define __rte_alloc_align (argno) +#endif + +/** +* Tells the compiler this is a function like malloc and that the pointer +* returned cannot alias any other pointer (ie new memory). +*/ +#if defined(RTE_CC_GCC ) || defined(RTE_CC_CLANG ) +#define __rte_malloc __attribute__((__malloc__)) +#else +#define __rte_malloc +#endif + +/** +* With recent GCC versions also able to track that proper +* deallocator function is used for this pointer. +*/ +#if defined(RTE_TOOLCHAIN_GCC ) && (GCC_VERSION >=110000 ) +#define __rte_dealloc (dealloc, argno) \ + __attribute__((malloc(dealloc, argno))) +#else +#define __rte_dealloc (dealloc, argno) +#endif +#define RTE_PRIORITY_LOG 101#define RTE_PRIORITY_BUS 110#define RTE_PRIORITY_CLASS 120
lib/eal/include/rte_malloc.h +36 -23
113 lines not on this page · raw message
Signature · 1 line
2.45.2
RE: [PATCH v9 00/17] Use malloc function attribute to uncover bugs
In reply to Stephen HemmingerQuoted 63 lines
Recent versions of GCC have some additional function attributes that can help with DPDK performance and stability.
The
alloc_alignattribute tells the compiler what the alignment of the allocation will be, and the optimizer can use this to produce better code (especially memcpy and structure copies).The malloc attribute tells compiler that object is not overlapping and potentially aliasing. It also as an additional variant in GCC 11 or later that allows for detecting all sorts of common errors like calling
free()on memory allocated withrte_malloc().In order to use the malloc attribute the free function prototype needs to be moved before the allocator/create function prototype so that the malloc attribute can refer to it.
This uncovered at least 16 pre-existing bugs in DPDK, these should go to stable. This patch set is structured with:
- fix any new warnings that were discovered
- add macros for enable the macros
- enable the attributesThe same attributes could be added to lots more functions in DPDK, but this patch set focuses on the key ones, and where problems exist in current code base.
v9 - correct the event/cnxk patch
Stephen Hemminger (17):
memzone: fix use after free in tracing cryptodev/bcmfs: fix mis-matched free dma/ixd: fix incorrect free function in cleanup event/cnxk: fix free of non-heap in cleanup code examples/vhost: fix free function mismatch net/cnxk: fix use-after-free
bpf: fix free mismatch if convert fails net/e1000: fix use-after-free
net/sfc: fix use-after-free warning messages net/cpfl: fix free of nonheap object net/nfp: fix duplicate call torte_freeraw/ifpga/base: fix use after free common/qat: fix use after freedrivers/ifpga: fix free function mismatch baseband/la12xx: prevent use after free common/idpf: fix use after free due
eal: add function attributes for allocation functionsdoc/guides/rel_notes/release_24_11.rst | 8 +++ drivers/baseband/la12xx/bbdev_la12xx.c | 1 + drivers/common/idpf/base/idpf_osdep.h | 10 +++- drivers/common/idpf/idpf_common_device.c | 3 +- drivers/common/qat/qat_device.c | 6 +-- drivers/crypto/bcmfs/bcmfs_device.c | 4 +- drivers/dma/idxd/idxd_pci.c | 2 +- drivers/event/cnxk/cnxk_eventdev.c | 2 +- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- drivers/net/cpfl/cpfl_flow_parser.c | 1 - drivers/net/e1000/igb_ethdev.c | 4 +- drivers/net/nfp/flower/nfp_flower_flow.c | 1 - drivers/net/sfc/sfc_flow_rss.c | 4 +- drivers/net/sfc/sfc_mae.c | 23 ++++-----
Quoted 9 lines
drivers/raw/ifpga/base/opae_intel_max10.c | 11 ++++- drivers/raw/ifpga/ifpga_rawdev.c | 8 +-- examples/vhost_blk/vhost_blk.c | 2 +- lib/bpf/bpf_convert.c | 2 +- lib/eal/common/eal_common_memzone.c | 3 +- lib/eal/include/rte_common.h | 34 +++++++++++++ lib/eal/include/rte_malloc.h | 59 ++++++++++++++--------- 21 files changed, 124 insertions(+), 66 deletions(-)
Quoted 2 lines
--
2.45.2
RE: [PATCH v9 00/17] Use malloc function attribute to uncover bugs
In reply to Stephen HemmingerQuoted 72 lines
Subject: [PATCH v9 00/17] Use malloc function attribute to uncover bugs
Recent versions of GCC have some additional function attributes that can help with DPDK performance and stability.
The
alloc_alignattribute tells the compiler what the alignment of the allocation will be, and the optimizer can use this to produce better code (especially memcpy and structure copies).The malloc attribute tells compiler that object is not overlapping and potentially aliasing. It also as an additional variant in GCC 11 or later that allows for detecting all sorts of common errors like calling
free()on memory allocated withrte_malloc().In order to use the malloc attribute the free function prototype needs to be moved before the allocator/create function prototype so that the malloc attribute can refer to it.
This uncovered at least 16 pre-existing bugs in DPDK, these should go to stable. This patch set is structured with:
- fix any new warnings that were discovered
- add macros for enable the macros
- enable the attributesThe same attributes could be added to lots more functions in DPDK, but this patch set focuses on the key ones, and where problems exist in current code base.
v9 - correct the event/cnxk patch
Stephen Hemminger (17):
memzone: fix use after free in tracing cryptodev/bcmfs: fix mis-matched free dma/ixd: fix incorrect free function in cleanup event/cnxk: fix free of non-heap in cleanup code examples/vhost: fix free function mismatch net/cnxk: fix use-after-free
bpf: fix free mismatch if convert fails net/e1000: fix use-after-free
net/sfc: fix use-after-free warning messages net/cpfl: fix free of nonheap object net/nfp: fix duplicate call torte_freeraw/ifpga/base: fix use after free common/qat: fix use after freedrivers/ifpga: fix free function mismatch baseband/la12xx: prevent use after free common/idpf: fix use after free due
eal: add function attributes for allocation functionsdoc/guides/rel_notes/release_24_11.rst | 8 +++ drivers/baseband/la12xx/bbdev_la12xx.c | 1 + drivers/common/idpf/base/idpf_osdep.h | 10 +++- drivers/common/idpf/idpf_common_device.c | 3 +- drivers/common/qat/qat_device.c | 6 +-- drivers/crypto/bcmfs/bcmfs_device.c | 4 +- drivers/dma/idxd/idxd_pci.c | 2 +- drivers/event/cnxk/cnxk_eventdev.c | 2 +- drivers/net/cnxk/cnxk_ethdev_sec.c | 2 +- drivers/net/cpfl/cpfl_flow_parser.c | 1 - drivers/net/e1000/igb_ethdev.c | 4 +- drivers/net/nfp/flower/nfp_flower_flow.c | 1 - drivers/net/sfc/sfc_flow_rss.c | 4 +- drivers/net/sfc/sfc_mae.c | 23 ++++----- drivers/raw/ifpga/base/opae_intel_max10.c | 11 ++++- drivers/raw/ifpga/ifpga_rawdev.c | 8 +-- examples/vhost_blk/vhost_blk.c | 2 +- lib/bpf/bpf_convert.c | 2 +- lib/eal/common/eal_common_memzone.c | 3 +- lib/eal/include/rte_common.h | 34 +++++++++++++ lib/eal/include/rte_malloc.h | 59 ++++++++++++++--------- 21 files changed, 124 insertions(+), 66 deletions(-)
Quoted 2 lines
--
2.45.2
Re: [PATCH v9 00/17] Use malloc function attribute to uncover bugs
In reply to Stephen HemmingerQuoted 23 lines · Stephen Hemminger
Recent versions of GCC have some additional function attributes that can help with DPDK performance and stability.
The
alloc_alignattribute tells the compiler what the alignment of the allocation will be, and the optimizer can use this to produce better code (especially memcpy and structure copies).The malloc attribute tells compiler that object is not overlapping and potentially aliasing. It also as an additional variant in GCC 11 or later that allows for detecting all sorts of common errors like calling
free()on memory allocated withrte_malloc().In order to use the malloc attribute the free function prototype needs to be moved before the allocator/create function prototype so that the malloc attribute can refer to it.
This uncovered at least 16 pre-existing bugs in DPDK, these should go to stable. This patch set is structured with:
- fix any new warnings that were discovered
- add macros for enable the macros
- enable the attributes
Quoted 3 lines
The same attributes could be added to lots more functions in DPDK, but this patch set focuses on the key ones, and where problems exist in current code base.
Series applied.
Thanks Stephen.
Signature · 1 line
David Marchand