ring: fix use after free in ring release

Message ID d175f9250542291dd0b86f4587a5fde018b945b1.1681736644.git.wangyunjian@huawei.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series ring: fix use after free in ring release |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/github-robot: build success github build: passed
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-unit-testing success Testing PASS
ci/iol-abi-testing success Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS
ci/intel-Functional success Functional PASS
ci/iol-aarch64-compile-testing success Testing PASS
ci/iol-testing warning Testing issues
ci/iol-x86_64-unit-testing success Testing PASS

Commit Message

Yunjian Wang April 17, 2023, 1:11 p.m. UTC
  When using the ring to find out tailq entry, however it had been
freed by rte_memzone_free function. This change prevents that
from happening.

Fixes: 4e32101f9b01 ("ring: support freeing")
Cc: stable@dpdk.org

Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
---
 lib/ring/rte_ring.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
  

Comments

Honnappa Nagarahalli April 18, 2023, 11:53 p.m. UTC | #1
> -----Original Message-----
> From: Yunjian Wang <wangyunjian@huawei.com>
> Sent: Monday, April 17, 2023 8:12 AM
> To: dev@dpdk.org
> Cc: Honnappa Nagarahalli <Honnappa.Nagarahalli@arm.com>;
> konstantin.v.ananyev@yandex.ru; luyicai@huawei.com; Yunjian Wang
> <wangyunjian@huawei.com>; stable@dpdk.org
> Subject: [dpdk-dev] [PATCH] ring: fix use after free in ring release
> 
> When using the ring to find out tailq entry, however it had been freed by
> rte_memzone_free function. This change prevents that from happening.
I am unable to follow the problem you are describing.
After the memzone for the ring is released, the contents of the memzone are not being used. I understand that the variable 'r' is being used, but that should not cause any issues.

> 
> Fixes: 4e32101f9b01 ("ring: support freeing")
> Cc: stable@dpdk.org
> 
> Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
> ---
>  lib/ring/rte_ring.c | 11 +++++------
>  1 file changed, 5 insertions(+), 6 deletions(-)
> 
> diff --git a/lib/ring/rte_ring.c b/lib/ring/rte_ring.c index
> 8ed455043d..17d2d7f8a8 100644
> --- a/lib/ring/rte_ring.c
> +++ b/lib/ring/rte_ring.c
> @@ -333,11 +333,6 @@ rte_ring_free(struct rte_ring *r)
>  		return;
>  	}
> 
> -	if (rte_memzone_free(r->memzone) != 0) {
> -		RTE_LOG(ERR, RING, "Cannot free memory\n");
> -		return;
> -	}
Why do we need to free the memzone later?

> -
>  	ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
>  	rte_mcfg_tailq_write_lock();
> 
> @@ -349,7 +344,7 @@ rte_ring_free(struct rte_ring *r)
> 
>  	if (te == NULL) {
>  		rte_mcfg_tailq_write_unlock();
> -		return;
> +		goto free_memzone;
>  	}
> 
>  	TAILQ_REMOVE(ring_list, te, next);
> @@ -357,6 +352,10 @@ rte_ring_free(struct rte_ring *r)
>  	rte_mcfg_tailq_write_unlock();
> 
>  	rte_free(te);
> +
> +free_memzone:
> +	if (rte_memzone_free(r->memzone) != 0)
> +		RTE_LOG(ERR, RING, "Cannot free memory\n");
>  }
> 
>  /* dump the status of the ring on the console */
> --
> 2.33.0
  
Yunjian Wang April 19, 2023, 7:08 a.m. UTC | #2
> -----Original Message-----
> From: Honnappa Nagarahalli [mailto:Honnappa.Nagarahalli@arm.com]
> Sent: Wednesday, April 19, 2023 7:53 AM
> To: wangyunjian <wangyunjian@huawei.com>; dev@dpdk.org
> Cc: konstantin.v.ananyev@yandex.ru; luyicai <luyicai@huawei.com>;
> stable@dpdk.org; nd <nd@arm.com>; Honnappa Nagarahalli
> <Honnappa.Nagarahalli@arm.com>; nd <nd@arm.com>
> Subject: RE: [dpdk-dev] [PATCH] ring: fix use after free in ring release
> 
> 
> 
> > -----Original Message-----
> > From: Yunjian Wang <wangyunjian@huawei.com>
> > Sent: Monday, April 17, 2023 8:12 AM
> > To: dev@dpdk.org
> > Cc: Honnappa Nagarahalli <Honnappa.Nagarahalli@arm.com>;
> > konstantin.v.ananyev@yandex.ru; luyicai@huawei.com; Yunjian Wang
> > <wangyunjian@huawei.com>; stable@dpdk.org
> > Subject: [dpdk-dev] [PATCH] ring: fix use after free in ring release
> >
> > When using the ring to find out tailq entry, however it had been freed
> > by rte_memzone_free function. This change prevents that from happening.
> I am unable to follow the problem you are describing.
> After the memzone for the ring is released, the contents of the memzone are
> not being used. I understand that the variable 'r' is being used, but that should
> not cause any issues.
> 
> >
> > Fixes: 4e32101f9b01 ("ring: support freeing")
> > Cc: stable@dpdk.org
> >
> > Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
> > ---
> >  lib/ring/rte_ring.c | 11 +++++------
> >  1 file changed, 5 insertions(+), 6 deletions(-)
> >
> > diff --git a/lib/ring/rte_ring.c b/lib/ring/rte_ring.c index
> > 8ed455043d..17d2d7f8a8 100644
> > --- a/lib/ring/rte_ring.c
> > +++ b/lib/ring/rte_ring.c
> > @@ -333,11 +333,6 @@ rte_ring_free(struct rte_ring *r)
> >  		return;
> >  	}
> >
> > -	if (rte_memzone_free(r->memzone) != 0) {
> > -		RTE_LOG(ERR, RING, "Cannot free memory\n");
> > -		return;
> > -	}
> Why do we need to free the memzone later?

After the memzone is freed, it is not removed from the 'rte_ring_tailq'.
If rte_ring_lookup is called at this time, it will cause a use-after-free problem.

Thanks,
Yunjian
> 
> > -
> >  	ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
> >  	rte_mcfg_tailq_write_lock();
> >
> > @@ -349,7 +344,7 @@ rte_ring_free(struct rte_ring *r)
> >
> >  	if (te == NULL) {
> >  		rte_mcfg_tailq_write_unlock();
> > -		return;
> > +		goto free_memzone;
> >  	}
> >
> >  	TAILQ_REMOVE(ring_list, te, next);
> > @@ -357,6 +352,10 @@ rte_ring_free(struct rte_ring *r)
> >  	rte_mcfg_tailq_write_unlock();
> >
> >  	rte_free(te);
> > +
> > +free_memzone:
> > +	if (rte_memzone_free(r->memzone) != 0)
> > +		RTE_LOG(ERR, RING, "Cannot free memory\n");
> >  }
> >
> >  /* dump the status of the ring on the console */
> > --
> > 2.33.0
  
Honnappa Nagarahalli April 19, 2023, 9:44 p.m. UTC | #3
<snip>

> >
> > > -----Original Message-----
> > > From: Yunjian Wang <wangyunjian@huawei.com>
> > > Sent: Monday, April 17, 2023 8:12 AM
> > > To: dev@dpdk.org
> > > Cc: Honnappa Nagarahalli <Honnappa.Nagarahalli@arm.com>;
> > > konstantin.v.ananyev@yandex.ru; luyicai@huawei.com; Yunjian Wang
> > > <wangyunjian@huawei.com>; stable@dpdk.org
> > > Subject: [dpdk-dev] [PATCH] ring: fix use after free in ring release
> > >
> > > When using the ring to find out tailq entry, however it had been
> > > freed by rte_memzone_free function. This change prevents that from
> happening.
> > I am unable to follow the problem you are describing.
> > After the memzone for the ring is released, the contents of the
> > memzone are not being used. I understand that the variable 'r' is
> > being used, but that should not cause any issues.
> >
> > >
> > > Fixes: 4e32101f9b01 ("ring: support freeing")
> > > Cc: stable@dpdk.org
> > >
> > > Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
> > > ---
> > >  lib/ring/rte_ring.c | 11 +++++------
> > >  1 file changed, 5 insertions(+), 6 deletions(-)
> > >
> > > diff --git a/lib/ring/rte_ring.c b/lib/ring/rte_ring.c index
> > > 8ed455043d..17d2d7f8a8 100644
> > > --- a/lib/ring/rte_ring.c
> > > +++ b/lib/ring/rte_ring.c
> > > @@ -333,11 +333,6 @@ rte_ring_free(struct rte_ring *r)
> > >  		return;
> > >  	}
> > >
> > > -	if (rte_memzone_free(r->memzone) != 0) {
> > > -		RTE_LOG(ERR, RING, "Cannot free memory\n");
> > > -		return;
> > > -	}
> > Why do we need to free the memzone later?
> 
> After the memzone is freed, it is not removed from the 'rte_ring_tailq'.
> If rte_ring_lookup is called at this time, it will cause a use-after-free problem.
Thanks, understood

> 
> Thanks,
> Yunjian
> >
> > > -
> > >  	ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
> > >  	rte_mcfg_tailq_write_lock();
> > >
> > > @@ -349,7 +344,7 @@ rte_ring_free(struct rte_ring *r)
> > >
> > >  	if (te == NULL) {
> > >  		rte_mcfg_tailq_write_unlock();
> > > -		return;
> > > +		goto free_memzone;
We do not need this. If 'te == NULL' is true, then the ring was not found or possibly already freed.

> > >  	}
> > >
> > >  	TAILQ_REMOVE(ring_list, te, next); @@ -357,6 +352,10 @@
> > > rte_ring_free(struct rte_ring *r)
We should free the memzone here while holding the lock

> > >  	rte_mcfg_tailq_write_unlock();
> > >
> > >  	rte_free(te);
> > > +
> > > +free_memzone:
> > > +	if (rte_memzone_free(r->memzone) != 0)
> > > +		RTE_LOG(ERR, RING, "Cannot free memory\n");
> > >  }
Should be moved up as mentioned above

> > >
> > >  /* dump the status of the ring on the console */
> > > --
> > > 2.33.0
  
Yunjian Wang April 20, 2023, 6:48 a.m. UTC | #4
> -----Original Message-----
> From: Honnappa Nagarahalli [mailto:Honnappa.Nagarahalli@arm.com]
> Sent: Thursday, April 20, 2023 5:44 AM
> To: wangyunjian <wangyunjian@huawei.com>; dev@dpdk.org
> Cc: konstantin.v.ananyev@yandex.ru; luyicai <luyicai@huawei.com>;
> stable@dpdk.org; nd <nd@arm.com>; nd <nd@arm.com>
> Subject: RE: [dpdk-dev] [PATCH] ring: fix use after free in ring release
> 
> <snip>
> 
> > >
> > > > -----Original Message-----
> > > > From: Yunjian Wang <wangyunjian@huawei.com>
> > > > Sent: Monday, April 17, 2023 8:12 AM
> > > > To: dev@dpdk.org
> > > > Cc: Honnappa Nagarahalli <Honnappa.Nagarahalli@arm.com>;
> > > > konstantin.v.ananyev@yandex.ru; luyicai@huawei.com; Yunjian Wang
> > > > <wangyunjian@huawei.com>; stable@dpdk.org
> > > > Subject: [dpdk-dev] [PATCH] ring: fix use after free in ring
> > > > release
> > > >
> > > > When using the ring to find out tailq entry, however it had been
> > > > freed by rte_memzone_free function. This change prevents that from
> > happening.
> > > I am unable to follow the problem you are describing.
> > > After the memzone for the ring is released, the contents of the
> > > memzone are not being used. I understand that the variable 'r' is
> > > being used, but that should not cause any issues.
> > >
> > > >
> > > > Fixes: 4e32101f9b01 ("ring: support freeing")
> > > > Cc: stable@dpdk.org
> > > >
> > > > Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
> > > > ---
> > > >  lib/ring/rte_ring.c | 11 +++++------
> > > >  1 file changed, 5 insertions(+), 6 deletions(-)
> > > >
> > > > diff --git a/lib/ring/rte_ring.c b/lib/ring/rte_ring.c index
> > > > 8ed455043d..17d2d7f8a8 100644
> > > > --- a/lib/ring/rte_ring.c
> > > > +++ b/lib/ring/rte_ring.c
> > > > @@ -333,11 +333,6 @@ rte_ring_free(struct rte_ring *r)
> > > >  		return;
> > > >  	}
> > > >
> > > > -	if (rte_memzone_free(r->memzone) != 0) {
> > > > -		RTE_LOG(ERR, RING, "Cannot free memory\n");
> > > > -		return;
> > > > -	}
> > > Why do we need to free the memzone later?
> >
> > After the memzone is freed, it is not removed from the 'rte_ring_tailq'.
> > If rte_ring_lookup is called at this time, it will cause a use-after-free problem.
> Thanks, understood
> 
> >
> > Thanks,
> > Yunjian
> > >
> > > > -
> > > >  	ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
> > > >  	rte_mcfg_tailq_write_lock();
> > > >
> > > > @@ -349,7 +344,7 @@ rte_ring_free(struct rte_ring *r)
> > > >
> > > >  	if (te == NULL) {
> > > >  		rte_mcfg_tailq_write_unlock();
> > > > -		return;
> > > > +		goto free_memzone;
> We do not need this. If 'te == NULL' is true, then the ring was not found or
> possibly already freed.

OK

> 
> > > >  	}
> > > >
> > > >  	TAILQ_REMOVE(ring_list, te, next); @@ -357,6 +352,10 @@
> > > > rte_ring_free(struct rte_ring *r)
> We should free the memzone here while holding the lock

OK, You are right. I fix it on your suggestions.

https://patchwork.dpdk.org/project/dpdk/patch/c23b1135e1b0676ef7d82969b39a21df992d418f.1681972694.git.wangyunjian@huawei.com/

Thanks,
Yunjian

> 
> > > >  	rte_mcfg_tailq_write_unlock();
> > > >
> > > >  	rte_free(te);
> > > > +
> > > > +free_memzone:
> > > > +	if (rte_memzone_free(r->memzone) != 0)
> > > > +		RTE_LOG(ERR, RING, "Cannot free memory\n");
> > > >  }
> Should be moved up as mentioned above
> 
> > > >
> > > >  /* dump the status of the ring on the console */
> > > > --
> > > > 2.33.0
  

Patch

diff --git a/lib/ring/rte_ring.c b/lib/ring/rte_ring.c
index 8ed455043d..17d2d7f8a8 100644
--- a/lib/ring/rte_ring.c
+++ b/lib/ring/rte_ring.c
@@ -333,11 +333,6 @@  rte_ring_free(struct rte_ring *r)
 		return;
 	}
 
-	if (rte_memzone_free(r->memzone) != 0) {
-		RTE_LOG(ERR, RING, "Cannot free memory\n");
-		return;
-	}
-
 	ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
 	rte_mcfg_tailq_write_lock();
 
@@ -349,7 +344,7 @@  rte_ring_free(struct rte_ring *r)
 
 	if (te == NULL) {
 		rte_mcfg_tailq_write_unlock();
-		return;
+		goto free_memzone;
 	}
 
 	TAILQ_REMOVE(ring_list, te, next);
@@ -357,6 +352,10 @@  rte_ring_free(struct rte_ring *r)
 	rte_mcfg_tailq_write_unlock();
 
 	rte_free(te);
+
+free_memzone:
+	if (rte_memzone_free(r->memzone) != 0)
+		RTE_LOG(ERR, RING, "Cannot free memory\n");
 }
 
 /* dump the status of the ring on the console */