diff mbox series

crypto: fix heap use after free bug

Message ID 20210721125122.185019-1-ciara.power@intel.com (mailing list archive)
State Accepted, archived
Delegated to: akhil goyal
Headers show
Series crypto: fix heap use after free bug | expand

Checks

Context Check Description
ci/iol-testing success Testing PASS
ci/intel-Testing success Testing PASS
ci/Intel-compilation success Compilation OK
ci/iol-abi-testing success Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/github-robot success github build: passed
ci/checkpatch success coding style OK

Commit Message

Power, Ciara July 21, 2021, 12:51 p.m. UTC
The PMD destroy function was calling the release function, which frees
cryptodev->data, and then tries to free cryptodev->data->dev_private,
which causes the heap use after free issue.

A temporary pointer is set before the free of cryptodev->data,
which can then be used afterwards to free dev_private.
The free cannot be moved to before the release function is called,
as dev_private is used in the QAT close function while being released.

Fixes: 9e6edea41805 ("cryptodev: add APIs to assist PMD initialisation")
Cc: declan.doherty@intel.com
Cc: stable@dpdk.org

Reported-by: ZhihongX Peng <zhihongx.peng@intel.com>
Signed-off-by: Ciara Power <ciara.power@intel.com>

---
The same issue is found in crypto/octeontx,
which may need to be addressed by maintainers.
Cc: Anoob Joseph <anoobj@marvell.com>
---
 lib/cryptodev/rte_cryptodev_pmd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Akhil Goyal July 27, 2021, 6:04 p.m. UTC | #1
> The PMD destroy function was calling the release function, which frees
> cryptodev->data, and then tries to free cryptodev->data->dev_private,
> which causes the heap use after free issue.
> 
> A temporary pointer is set before the free of cryptodev->data,
> which can then be used afterwards to free dev_private.
> The free cannot be moved to before the release function is called,
> as dev_private is used in the QAT close function while being released.
> 
> Fixes: 9e6edea41805 ("cryptodev: add APIs to assist PMD initialisation")
> Cc: declan.doherty@intel.com
> Cc: stable@dpdk.org
> 
> Reported-by: ZhihongX Peng <zhihongx.peng@intel.com>
> Signed-off-by: Ciara Power <ciara.power@intel.com>
> 
> ---
> The same issue is found in crypto/octeontx,
> which may need to be addressed by maintainers.
> Cc: Anoob Joseph <anoobj@marvell.com>
> ---
>  lib/cryptodev/rte_cryptodev_pmd.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/cryptodev/rte_cryptodev_pmd.c
> b/lib/cryptodev/rte_cryptodev_pmd.c
> index 0912004127..900acd7ba4 100644
> --- a/lib/cryptodev/rte_cryptodev_pmd.c
> +++ b/lib/cryptodev/rte_cryptodev_pmd.c
> @@ -140,6 +140,7 @@ int
>  rte_cryptodev_pmd_destroy(struct rte_cryptodev *cryptodev)
>  {
>  	int retval;
> +	void *tmp_dev_private = cryptodev->data->dev_private;

Can we rename this pointer as dev_private?

> 
>  	CDEV_LOG_INFO("Closing crypto device %s", cryptodev->device-
> >name);
> 
> @@ -149,7 +150,7 @@ rte_cryptodev_pmd_destroy(struct rte_cryptodev
> *cryptodev)
>  		return retval;
> 
>  	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
> -		rte_free(cryptodev->data->dev_private);
> +		rte_free(tmp_dev_private);
> 
> 
>  	cryptodev->device = NULL;
> --
> 2.25.1
Akhil Goyal July 30, 2021, 7:10 p.m. UTC | #2
Fixed title
Cryptodev: fix heap use after free
> > The PMD destroy function was calling the release function, which frees
> > cryptodev->data, and then tries to free cryptodev->data->dev_private,
> > which causes the heap use after free issue.
> >
> > A temporary pointer is set before the free of cryptodev->data,
> > which can then be used afterwards to free dev_private.
> > The free cannot be moved to before the release function is called,
> > as dev_private is used in the QAT close function while being released.
I believe all PMDs use dev_private for close.
Hence replaces QAT with PMD
> >
> > Fixes: 9e6edea41805 ("cryptodev: add APIs to assist PMD initialisation")
> > Cc: declan.doherty@intel.com
> > Cc: stable@dpdk.org
> >
> > Reported-by: ZhihongX Peng <zhihongx.peng@intel.com>
> > Signed-off-by: Ciara Power <ciara.power@intel.com>
> >
> > ---
> > The same issue is found in crypto/octeontx,
> > which may need to be addressed by maintainers.
> > Cc: Anoob Joseph <anoobj@marvell.com>
> > ---
> >  lib/cryptodev/rte_cryptodev_pmd.c | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/lib/cryptodev/rte_cryptodev_pmd.c
> > b/lib/cryptodev/rte_cryptodev_pmd.c
> > index 0912004127..900acd7ba4 100644
> > --- a/lib/cryptodev/rte_cryptodev_pmd.c
> > +++ b/lib/cryptodev/rte_cryptodev_pmd.c
> > @@ -140,6 +140,7 @@ int
> >  rte_cryptodev_pmd_destroy(struct rte_cryptodev *cryptodev)
> >  {
> >  	int retval;
> > +	void *tmp_dev_private = cryptodev->data->dev_private;
> 
> Can we rename this pointer as dev_private?

Renamed this while merging, as we have RC3 deadline today.
> 
> >
> >  	CDEV_LOG_INFO("Closing crypto device %s", cryptodev->device-
> > >name);
> >
> > @@ -149,7 +150,7 @@ rte_cryptodev_pmd_destroy(struct rte_cryptodev
> > *cryptodev)
> >  		return retval;
> >
> >  	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
> > -		rte_free(cryptodev->data->dev_private);
> > +		rte_free(tmp_dev_private);
> >
> >
> >  	cryptodev->device = NULL;
> > --
> > 2.25.1
Akhil Goyal July 30, 2021, 7:11 p.m. UTC | #3
> Fixed title
> Cryptodev: fix heap use after free
> > > The PMD destroy function was calling the release function, which frees
> > > cryptodev->data, and then tries to free cryptodev->data->dev_private,
> > > which causes the heap use after free issue.
> > >
> > > A temporary pointer is set before the free of cryptodev->data,
> > > which can then be used afterwards to free dev_private.
> > > The free cannot be moved to before the release function is called,
> > > as dev_private is used in the QAT close function while being released.
> I believe all PMDs use dev_private for close.
> Hence replaces QAT with PMD
> > >
> > > Fixes: 9e6edea41805 ("cryptodev: add APIs to assist PMD initialisation")
> > > Cc: declan.doherty@intel.com
> > > Cc: stable@dpdk.org
> > >
> > > Reported-by: ZhihongX Peng <zhihongx.peng@intel.com>
> > > Signed-off-by: Ciara Power <ciara.power@intel.com>
> > >
> > > ---
> > > The same issue is found in crypto/octeontx,
> > > which may need to be addressed by maintainers.
> > > Cc: Anoob Joseph <anoobj@marvell.com>
> > > ---
> > >  lib/cryptodev/rte_cryptodev_pmd.c | 3 ++-
> > >  1 file changed, 2 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/lib/cryptodev/rte_cryptodev_pmd.c
> > > b/lib/cryptodev/rte_cryptodev_pmd.c
> > > index 0912004127..900acd7ba4 100644
> > > --- a/lib/cryptodev/rte_cryptodev_pmd.c
> > > +++ b/lib/cryptodev/rte_cryptodev_pmd.c
> > > @@ -140,6 +140,7 @@ int
> > >  rte_cryptodev_pmd_destroy(struct rte_cryptodev *cryptodev)
> > >  {
> > >  	int retval;
> > > +	void *tmp_dev_private = cryptodev->data->dev_private;
> >
> > Can we rename this pointer as dev_private?
> 
> Renamed this while merging, as we have RC3 deadline today.
> >
> > >
> > >  	CDEV_LOG_INFO("Closing crypto device %s", cryptodev->device-
> > > >name);
> > >
> > > @@ -149,7 +150,7 @@ rte_cryptodev_pmd_destroy(struct rte_cryptodev
> > > *cryptodev)
> > >  		return retval;
> > >
> > >  	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
> > > -		rte_free(cryptodev->data->dev_private);
> > > +		rte_free(tmp_dev_private);
> > >
> > >
> > >  	cryptodev->device = NULL;

Acked-by: Akhil Goyal <gakhil@marvell.com>
Applied to dpdk-next-crypto
diff mbox series

Patch

diff --git a/lib/cryptodev/rte_cryptodev_pmd.c b/lib/cryptodev/rte_cryptodev_pmd.c
index 0912004127..900acd7ba4 100644
--- a/lib/cryptodev/rte_cryptodev_pmd.c
+++ b/lib/cryptodev/rte_cryptodev_pmd.c
@@ -140,6 +140,7 @@  int
 rte_cryptodev_pmd_destroy(struct rte_cryptodev *cryptodev)
 {
 	int retval;
+	void *tmp_dev_private = cryptodev->data->dev_private;
 
 	CDEV_LOG_INFO("Closing crypto device %s", cryptodev->device->name);
 
@@ -149,7 +150,7 @@  rte_cryptodev_pmd_destroy(struct rte_cryptodev *cryptodev)
 		return retval;
 
 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
-		rte_free(cryptodev->data->dev_private);
+		rte_free(tmp_dev_private);
 
 
 	cryptodev->device = NULL;