[dpdk-dev] cryptodev: fix NULL pointer dereference

Message ID 20170731023050.28956-1-pablo.de.lara.guarch@intel.com (mailing list archive)
State Rejected, archived
Headers

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK

Commit Message

De Lara Guarch, Pablo July 31, 2017, 2:30 a.m. UTC
  When registering a crypto driver, if memory allocation
fails, application should exit and do not allow
a NULL pointer dereference.

Coverity issue: 158645

Fixes: 7a364faef185 ("cryptodev: remove crypto device type enumeration")

Signed-off-by: Pablo de Lara <pablo.de.lara.guarch@intel.com>
---
 lib/librte_cryptodev/rte_cryptodev.c | 6 ++++++
 1 file changed, 6 insertions(+)
  

Comments

Sergio Gonzalez Monroy July 31, 2017, 12:32 p.m. UTC | #1
On 31/07/2017 03:30, Pablo de Lara wrote:
> When registering a crypto driver, if memory allocation
> fails, application should exit and do not allow
> a NULL pointer dereference.
>
> Coverity issue: 158645
>
> Fixes: 7a364faef185 ("cryptodev: remove crypto device type enumeration")
>
> Signed-off-by: Pablo de Lara <pablo.de.lara.guarch@intel.com>
> ---
>   lib/librte_cryptodev/rte_cryptodev.c | 6 ++++++
>   1 file changed, 6 insertions(+)
>
> diff --git a/lib/librte_cryptodev/rte_cryptodev.c b/lib/librte_cryptodev/rte_cryptodev.c
> index 327d7e8..4492b0d 100644
> --- a/lib/librte_cryptodev/rte_cryptodev.c
> +++ b/lib/librte_cryptodev/rte_cryptodev.c
> @@ -1404,6 +1404,12 @@ rte_cryptodev_allocate_driver(const struct rte_driver *drv)
>   	struct cryptodev_driver *driver;
>   
>   	driver = malloc(sizeof(*driver));
> +
> +	if (driver == NULL)
> +		rte_exit(EXIT_FAILURE,
> +			"Could not allocate memory for crypto driver %u\n",
> +			nb_drivers);
> +
>   	driver->driver = drv;
>   	driver->id = nb_drivers;
>   

Acked-by: Sergio Gonzalez Monroy <sergio.gonzalez.monroy@intel.com>
  
Thomas Monjalon July 31, 2017, 3:22 p.m. UTC | #2
31/07/2017 14:32, Sergio Gonzalez Monroy:
> On 31/07/2017 03:30, Pablo de Lara wrote:
> > --- a/lib/librte_cryptodev/rte_cryptodev.c
> > +++ b/lib/librte_cryptodev/rte_cryptodev.c
> > @@ -1404,6 +1404,12 @@ rte_cryptodev_allocate_driver(const struct rte_driver *drv)
> >   	struct cryptodev_driver *driver;
> >   
> >   	driver = malloc(sizeof(*driver));
> > +
> > +	if (driver == NULL)
> > +		rte_exit(EXIT_FAILURE,
> > +			"Could not allocate memory for crypto driver %u\n",
> > +			nb_drivers);
> > +
> >   	driver->driver = drv;
> >   	driver->id = nb_drivers;
> >   
> 
> Acked-by: Sergio Gonzalez Monroy <sergio.gonzalez.monroy@intel.com>

NACK rte_exit/rte_panic in libraries.
  
De Lara Guarch, Pablo Aug. 1, 2017, 7:09 a.m. UTC | #3
Hi Thomas,

> -----Original Message-----
> From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Thomas Monjalon
> Sent: Monday, July 31, 2017 4:23 PM
> To: De Lara Guarch, Pablo <pablo.de.lara.guarch@intel.com>
> Cc: dev@dpdk.org; Gonzalez Monroy, Sergio
> <sergio.gonzalez.monroy@intel.com>; Doherty, Declan
> <declan.doherty@intel.com>
> Subject: Re: [dpdk-dev] [PATCH] cryptodev: fix NULL pointer dereference
> 
> 31/07/2017 14:32, Sergio Gonzalez Monroy:
> > On 31/07/2017 03:30, Pablo de Lara wrote:
> > > --- a/lib/librte_cryptodev/rte_cryptodev.c
> > > +++ b/lib/librte_cryptodev/rte_cryptodev.c
> > > @@ -1404,6 +1404,12 @@ rte_cryptodev_allocate_driver(const struct
> rte_driver *drv)
> > >   	struct cryptodev_driver *driver;
> > >
> > >   	driver = malloc(sizeof(*driver));
> > > +
> > > +	if (driver == NULL)
> > > +		rte_exit(EXIT_FAILURE,
> > > +			"Could not allocate memory for crypto driver %u\n",
> > > +			nb_drivers);
> > > +
> > >   	driver->driver = drv;
> > >   	driver->id = nb_drivers;
> > >
> >
> > Acked-by: Sergio Gonzalez Monroy <sergio.gonzalez.monroy@intel.com>
> 
> NACK rte_exit/rte_panic in libraries.

I have sent a v2 that allocates statically the structure,
instead of calling malloc, so no rte_exit is required.

Thanks,
Pablo
  

Patch

diff --git a/lib/librte_cryptodev/rte_cryptodev.c b/lib/librte_cryptodev/rte_cryptodev.c
index 327d7e8..4492b0d 100644
--- a/lib/librte_cryptodev/rte_cryptodev.c
+++ b/lib/librte_cryptodev/rte_cryptodev.c
@@ -1404,6 +1404,12 @@  rte_cryptodev_allocate_driver(const struct rte_driver *drv)
 	struct cryptodev_driver *driver;
 
 	driver = malloc(sizeof(*driver));
+
+	if (driver == NULL)
+		rte_exit(EXIT_FAILURE,
+			"Could not allocate memory for crypto driver %u\n",
+			nb_drivers);
+
 	driver->driver = drv;
 	driver->id = nb_drivers;