patches for DPDK stable branches
 help / color / mirror / Atom feed
* [PATCH v2] fix eal/linux: unregister alarm callback before free
       [not found] <20250520160150.50401-1-rui.ferreira1@h-partners.com>
@ 2025-05-22 15:59 ` Rui Ferreira
  2025-05-27 10:22   ` Konstantin Ananyev
  2025-05-27 14:53   ` Thomas Monjalon
  0 siblings, 2 replies; 7+ messages in thread
From: Rui Ferreira @ 2025-05-22 15:59 UTC (permalink / raw)
  To: Thomas Monjalon; +Cc: dev, stable

This was flagged by Address sanitizer as a use after free. The
intr_handle ptr is shared between the main thread and the interrupt
thread. The interrupt thread can dereference the ptr after free (from
the alarm callback). free is called when the main thread cleans up.

The interrupt thread never terminates (eal_intr_thread_main) so
use rte_intr_callback_unregister_sync during cleanup to
ensure the callback is removed before freeing the ptr.

To be more defensive clear out the pointer and registration
variable if we can unregister.

rte_intr_callback_unregister_sync may (optionally) use traces
so the alarm cleanup must happen before eal_trace_fini to avoid
accessing freed memory.

Bugzilla ID: 1683

Signed-off-by: Rui Ferreira <rui.ferreira1@h-partners.com>
---
 .mailmap                  | 1 +
 lib/eal/linux/eal.c       | 3 ++-
 lib/eal/linux/eal_alarm.c | 9 ++++++++-
 3 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/.mailmap b/.mailmap
index d8439b79ce..907c5ea967 100644
--- a/.mailmap
+++ b/.mailmap
@@ -1332,6 +1332,7 @@ Rosen Xu <rosen.xu@altera.com> <rosen.xu@intel.com>
 Roy Franz <roy.franz@cavium.com>
 Roy Pledge <roy.pledge@nxp.com>
 Roy Shterman <roy.shterman@vastdata.com>
+Rui Ferreira <rui.ferreira1@h-partners.com>
 Ruifeng Wang <ruifeng.wang@arm.com>
 Rushil Gupta <rushilg@google.com>
 Ryan E Hall <ryan.e.hall@intel.com>
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index 20f777b8b0..b448db1392 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -1329,9 +1329,10 @@ rte_eal_cleanup(void)
 	rte_mp_channel_cleanup();
 	eal_bus_cleanup();
 	rte_trace_save();
+	/* may use trace, must be called before eal_trace_fini */
+	rte_eal_alarm_cleanup();
 	eal_trace_fini();
 	eal_mp_dev_hotplug_cleanup();
-	rte_eal_alarm_cleanup();
 	/* after this point, any DPDK pointers will become dangling */
 	rte_eal_memory_detach();
 	rte_eal_malloc_heap_cleanup();
diff --git a/lib/eal/linux/eal_alarm.c b/lib/eal/linux/eal_alarm.c
index b216a007a3..eb6a21d4f0 100644
--- a/lib/eal/linux/eal_alarm.c
+++ b/lib/eal/linux/eal_alarm.c
@@ -57,7 +57,14 @@ static void eal_alarm_callback(void *arg);
 void
 rte_eal_alarm_cleanup(void)
 {
-	rte_intr_instance_free(intr_handle);
+	/* unregister callback using intr_handle in interrupt thread */
+	int ret = rte_intr_callback_unregister_sync(intr_handle,
+						eal_alarm_callback, (void *)-1);
+	if (ret >= 0) {
+		rte_intr_instance_free(intr_handle);
+		intr_handle = NULL;
+		handler_registered = 0;
+	}
 }
 
 int
-- 
2.35.3


^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCH v2] fix eal/linux: unregister alarm callback before free
  2025-05-22 15:59 ` [PATCH v2] fix eal/linux: unregister alarm callback before free Rui Ferreira
@ 2025-05-27 10:22   ` Konstantin Ananyev
  2025-05-27 14:53   ` Thomas Monjalon
  1 sibling, 0 replies; 7+ messages in thread
From: Konstantin Ananyev @ 2025-05-27 10:22 UTC (permalink / raw)
  To: Rui Ferreira (A), Thomas Monjalon; +Cc: dev, stable



> This was flagged by Address sanitizer as a use after free. The
> intr_handle ptr is shared between the main thread and the interrupt
> thread. The interrupt thread can dereference the ptr after free (from
> the alarm callback). free is called when the main thread cleans up.
> 
> The interrupt thread never terminates (eal_intr_thread_main) so
> use rte_intr_callback_unregister_sync during cleanup to
> ensure the callback is removed before freeing the ptr.
> 
> To be more defensive clear out the pointer and registration
> variable if we can unregister.
> 
> rte_intr_callback_unregister_sync may (optionally) use traces
> so the alarm cleanup must happen before eal_trace_fini to avoid
> accessing freed memory.
> 
> Bugzilla ID: 1683
> 
> Signed-off-by: Rui Ferreira <rui.ferreira1@h-partners.com>
> ---
>  .mailmap                  | 1 +
>  lib/eal/linux/eal.c       | 3 ++-
>  lib/eal/linux/eal_alarm.c | 9 ++++++++-
>  3 files changed, 11 insertions(+), 2 deletions(-)
> 
> diff --git a/.mailmap b/.mailmap
> index d8439b79ce..907c5ea967 100644
> --- a/.mailmap
> +++ b/.mailmap
> @@ -1332,6 +1332,7 @@ Rosen Xu <rosen.xu@altera.com> <rosen.xu@intel.com>
>  Roy Franz <roy.franz@cavium.com>
>  Roy Pledge <roy.pledge@nxp.com>
>  Roy Shterman <roy.shterman@vastdata.com>
> +Rui Ferreira <rui.ferreira1@h-partners.com>
>  Ruifeng Wang <ruifeng.wang@arm.com>
>  Rushil Gupta <rushilg@google.com>
>  Ryan E Hall <ryan.e.hall@intel.com>
> diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
> index 20f777b8b0..b448db1392 100644
> --- a/lib/eal/linux/eal.c
> +++ b/lib/eal/linux/eal.c
> @@ -1329,9 +1329,10 @@ rte_eal_cleanup(void)
>  	rte_mp_channel_cleanup();
>  	eal_bus_cleanup();
>  	rte_trace_save();
> +	/* may use trace, must be called before eal_trace_fini */
> +	rte_eal_alarm_cleanup();
>  	eal_trace_fini();
>  	eal_mp_dev_hotplug_cleanup();
> -	rte_eal_alarm_cleanup();
>  	/* after this point, any DPDK pointers will become dangling */
>  	rte_eal_memory_detach();
>  	rte_eal_malloc_heap_cleanup();
> diff --git a/lib/eal/linux/eal_alarm.c b/lib/eal/linux/eal_alarm.c
> index b216a007a3..eb6a21d4f0 100644
> --- a/lib/eal/linux/eal_alarm.c
> +++ b/lib/eal/linux/eal_alarm.c
> @@ -57,7 +57,14 @@ static void eal_alarm_callback(void *arg);
>  void
>  rte_eal_alarm_cleanup(void)
>  {
> -	rte_intr_instance_free(intr_handle);
> +	/* unregister callback using intr_handle in interrupt thread */
> +	int ret = rte_intr_callback_unregister_sync(intr_handle,
> +						eal_alarm_callback, (void *)-1);
> +	if (ret >= 0) {
> +		rte_intr_instance_free(intr_handle);
> +		intr_handle = NULL;
> +		handler_registered = 0;
> +	}
>  }
> 
>  int
> --

Acked-by: Konstantin Ananyev <konstantin.ananyev@huawei.com>
 

> 2.35.3


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] fix eal/linux: unregister alarm callback before free
  2025-05-22 15:59 ` [PATCH v2] fix eal/linux: unregister alarm callback before free Rui Ferreira
  2025-05-27 10:22   ` Konstantin Ananyev
@ 2025-05-27 14:53   ` Thomas Monjalon
  2025-05-29  8:39     ` Rui Ferreira (A)
  1 sibling, 1 reply; 7+ messages in thread
From: Thomas Monjalon @ 2025-05-27 14:53 UTC (permalink / raw)
  To: Rui Ferreira; +Cc: dev, stable, David Marchand, Konstantin Ananyev

Hello,

Did you see the comment from David about fixing for Windows and FreeBSD at the same time?
I think you just need to apply the same logic in 3 different places.
If you cannot test all, that's OK to submit the changes without full coverage testing.

Thank you


22/05/2025 17:59, Rui Ferreira:
> This was flagged by Address sanitizer as a use after free. The
> intr_handle ptr is shared between the main thread and the interrupt
> thread. The interrupt thread can dereference the ptr after free (from
> the alarm callback). free is called when the main thread cleans up.
> 
> The interrupt thread never terminates (eal_intr_thread_main) so
> use rte_intr_callback_unregister_sync during cleanup to
> ensure the callback is removed before freeing the ptr.
> 
> To be more defensive clear out the pointer and registration
> variable if we can unregister.
> 
> rte_intr_callback_unregister_sync may (optionally) use traces
> so the alarm cleanup must happen before eal_trace_fini to avoid
> accessing freed memory.
> 
> Bugzilla ID: 1683
> 
> Signed-off-by: Rui Ferreira <rui.ferreira1@h-partners.com>
> ---
>  .mailmap                  | 1 +
>  lib/eal/linux/eal.c       | 3 ++-
>  lib/eal/linux/eal_alarm.c | 9 ++++++++-
>  3 files changed, 11 insertions(+), 2 deletions(-)




^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCH v2] fix eal/linux: unregister alarm callback before free
  2025-05-27 14:53   ` Thomas Monjalon
@ 2025-05-29  8:39     ` Rui Ferreira (A)
  2025-05-30  8:18       ` [PATCH v2 0/2] fix eal: " Rui Ferreira
  0 siblings, 1 reply; 7+ messages in thread
From: Rui Ferreira (A) @ 2025-05-29  8:39 UTC (permalink / raw)
  To: Thomas Monjalon; +Cc: dev, stable, David Marchand, Konstantin Ananyev

Hi. I'll try to have a look in the next couple days. 

BR

> -----Original Message-----
> From: Thomas Monjalon <thomas@monjalon.net>
> Sent: Tuesday, May 27, 2025 3:54 PM
> To: Rui Ferreira (A) <rui.ferreira1@h-partners.com>
> Cc: dev@dpdk.org; stable@dpdk.org; David Marchand
> <david.marchand@redhat.com>; Konstantin Ananyev
> <konstantin.ananyev@huawei.com>
> Subject: Re: [PATCH v2] fix eal/linux: unregister alarm callback before free
> 
> Hello,
> 
> Did you see the comment from David about fixing for Windows and FreeBSD
> at the same time?
> I think you just need to apply the same logic in 3 different places.
> If you cannot test all, that's OK to submit the changes without full coverage
> testing.
> 
> Thank you
> 
> 
> 22/05/2025 17:59, Rui Ferreira:
> > This was flagged by Address sanitizer as a use after free. The
> > intr_handle ptr is shared between the main thread and the interrupt
> > thread. The interrupt thread can dereference the ptr after free (from
> > the alarm callback). free is called when the main thread cleans up.
> >
> > The interrupt thread never terminates (eal_intr_thread_main) so use
> > rte_intr_callback_unregister_sync during cleanup to ensure the
> > callback is removed before freeing the ptr.
> >
> > To be more defensive clear out the pointer and registration variable
> > if we can unregister.
> >
> > rte_intr_callback_unregister_sync may (optionally) use traces so the
> > alarm cleanup must happen before eal_trace_fini to avoid accessing
> > freed memory.
> >
> > Bugzilla ID: 1683
> >
> > Signed-off-by: Rui Ferreira <rui.ferreira1@h-partners.com>
> > ---
> >  .mailmap                  | 1 +
> >  lib/eal/linux/eal.c       | 3 ++-
> >  lib/eal/linux/eal_alarm.c | 9 ++++++++-
> >  3 files changed, 11 insertions(+), 2 deletions(-)
> 
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 0/2] fix eal: unregister alarm callback before free
  2025-05-29  8:39     ` Rui Ferreira (A)
@ 2025-05-30  8:18       ` Rui Ferreira
  2025-05-30  8:18         ` [PATCH v2 1/2] fix eal/linux: " Rui Ferreira
  2025-05-30  8:18         ` [PATCH v2 2/2] fix eal/freebsd: " Rui Ferreira
  0 siblings, 2 replies; 7+ messages in thread
From: Rui Ferreira @ 2025-05-30  8:18 UTC (permalink / raw)
  Cc: dev, stable

This was flagged by Address sanitizer as a use after free in Linux. A
pointer is freed in the main thread but used by a callback in another thread.
It can be reliably reproduced in Linux with ASAN but I could not trigger the
same issue in FreeBSD.

I've included nearly identical patches for Linux and FreeBSD. I did not
include any changes for Windows since the same issue does not seem to apply
there (thread is actually cancelled, no shared ptr).

Bugzilla ID: 1683

Rui Ferreira (2):
  fix eal/linux: unregister alarm callback before free
  fix eal/freebsd: unregister alarm callback before free

 .mailmap                    | 1 +
 lib/eal/freebsd/eal.c       | 3 ++-
 lib/eal/freebsd/eal_alarm.c | 8 +++++++-
 lib/eal/linux/eal.c         | 3 ++-
 lib/eal/linux/eal_alarm.c   | 9 ++++++++-
 5 files changed, 20 insertions(+), 4 deletions(-)

--
2.43.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 1/2] fix eal/linux: unregister alarm callback before free
  2025-05-30  8:18       ` [PATCH v2 0/2] fix eal: " Rui Ferreira
@ 2025-05-30  8:18         ` Rui Ferreira
  2025-05-30  8:18         ` [PATCH v2 2/2] fix eal/freebsd: " Rui Ferreira
  1 sibling, 0 replies; 7+ messages in thread
From: Rui Ferreira @ 2025-05-30  8:18 UTC (permalink / raw)
  To: Thomas Monjalon; +Cc: dev, stable

This was flagged by Address sanitizer as a use after free. The
intr_handle ptr is shared between the main thread and the interrupt
thread. The interrupt thread can dereference the ptr after free (from
the alarm callback). free is called when the main thread cleans up.

The interrupt thread never terminates (eal_intr_thread_main) so
use rte_intr_callback_unregister_sync during cleanup to
ensure the callback is removed before freeing the ptr.

To be more defensive clear out the pointer and registration
variable if we can unregister.

rte_intr_callback_unregister_sync may (optionally) use traces
so the alarm cleanup must happen before eal_trace_fini to avoid
accessing freed memory.

Bugzilla ID: 1683

Signed-off-by: Rui Ferreira <rui.ferreira1@h-partners.com>
---
 .mailmap                  | 1 +
 lib/eal/linux/eal.c       | 3 ++-
 lib/eal/linux/eal_alarm.c | 9 ++++++++-
 3 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/.mailmap b/.mailmap
index d8439b79ce..907c5ea967 100644
--- a/.mailmap
+++ b/.mailmap
@@ -1332,6 +1332,7 @@ Rosen Xu <rosen.xu@altera.com> <rosen.xu@intel.com>
 Roy Franz <roy.franz@cavium.com>
 Roy Pledge <roy.pledge@nxp.com>
 Roy Shterman <roy.shterman@vastdata.com>
+Rui Ferreira <rui.ferreira1@h-partners.com>
 Ruifeng Wang <ruifeng.wang@arm.com>
 Rushil Gupta <rushilg@google.com>
 Ryan E Hall <ryan.e.hall@intel.com>
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index 20f777b8b0..b448db1392 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -1329,9 +1329,10 @@ rte_eal_cleanup(void)
 	rte_mp_channel_cleanup();
 	eal_bus_cleanup();
 	rte_trace_save();
+	/* may use trace, must be called before eal_trace_fini */
+	rte_eal_alarm_cleanup();
 	eal_trace_fini();
 	eal_mp_dev_hotplug_cleanup();
-	rte_eal_alarm_cleanup();
 	/* after this point, any DPDK pointers will become dangling */
 	rte_eal_memory_detach();
 	rte_eal_malloc_heap_cleanup();
diff --git a/lib/eal/linux/eal_alarm.c b/lib/eal/linux/eal_alarm.c
index b216a007a3..eb6a21d4f0 100644
--- a/lib/eal/linux/eal_alarm.c
+++ b/lib/eal/linux/eal_alarm.c
@@ -57,7 +57,14 @@ static void eal_alarm_callback(void *arg);
 void
 rte_eal_alarm_cleanup(void)
 {
-	rte_intr_instance_free(intr_handle);
+	/* unregister callback using intr_handle in interrupt thread */
+	int ret = rte_intr_callback_unregister_sync(intr_handle,
+						eal_alarm_callback, (void *)-1);
+	if (ret >= 0) {
+		rte_intr_instance_free(intr_handle);
+		intr_handle = NULL;
+		handler_registered = 0;
+	}
 }
 
 int
-- 
2.43.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 2/2] fix eal/freebsd: unregister alarm callback before free
  2025-05-30  8:18       ` [PATCH v2 0/2] fix eal: " Rui Ferreira
  2025-05-30  8:18         ` [PATCH v2 1/2] fix eal/linux: " Rui Ferreira
@ 2025-05-30  8:18         ` Rui Ferreira
  1 sibling, 0 replies; 7+ messages in thread
From: Rui Ferreira @ 2025-05-30  8:18 UTC (permalink / raw)
  To: Bruce Richardson; +Cc: dev, stable

Unregister callback on cleanup to avoid use after free from the
interrupt thread (eal_intr_thread_main).

To be more defensive, set ptr to NULL if we can unregister.

rte_intr_callback_unregister_sync may (optionally) use traces
so the alarm cleanup must happen before eal_trace_fini to avoid
accessing freed memory.

Bugzilla ID: 1683

Signed-off-by: Rui Ferreira <rui.ferreira1@h-partners.com>
---
 lib/eal/freebsd/eal.c       | 3 ++-
 lib/eal/freebsd/eal_alarm.c | 8 +++++++-
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index d6fffa2170..21ce4e6b18 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -907,8 +907,9 @@ rte_eal_cleanup(void)
 	rte_mp_channel_cleanup();
 	eal_bus_cleanup();
 	rte_trace_save();
-	eal_trace_fini();
+	/* may use trace, must be called before eal_trace_fini */
 	rte_eal_alarm_cleanup();
+	eal_trace_fini();
 	/* after this point, any DPDK pointers will become dangling */
 	rte_eal_memory_detach();
 	eal_cleanup_config(internal_conf);
diff --git a/lib/eal/freebsd/eal_alarm.c b/lib/eal/freebsd/eal_alarm.c
index 28f285fdef..c03e281e67 100644
--- a/lib/eal/freebsd/eal_alarm.c
+++ b/lib/eal/freebsd/eal_alarm.c
@@ -50,7 +50,13 @@ static void eal_alarm_callback(void *arg);
 void
 rte_eal_alarm_cleanup(void)
 {
-	rte_intr_instance_free(intr_handle);
+	/* unregister callback using intr_handle in interrupt thread */
+	int ret = rte_intr_callback_unregister_sync(intr_handle,
+			eal_alarm_callback, (void *)-1);
+	if (ret >= 0) {
+		rte_intr_instance_free(intr_handle);
+		intr_handle = NULL;
+	}
 }
 
 int
-- 
2.43.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-05-30  8:19 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20250520160150.50401-1-rui.ferreira1@h-partners.com>
2025-05-22 15:59 ` [PATCH v2] fix eal/linux: unregister alarm callback before free Rui Ferreira
2025-05-27 10:22   ` Konstantin Ananyev
2025-05-27 14:53   ` Thomas Monjalon
2025-05-29  8:39     ` Rui Ferreira (A)
2025-05-30  8:18       ` [PATCH v2 0/2] fix eal: " Rui Ferreira
2025-05-30  8:18         ` [PATCH v2 1/2] fix eal/linux: " Rui Ferreira
2025-05-30  8:18         ` [PATCH v2 2/2] fix eal/freebsd: " Rui Ferreira

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).