[PATCH 1/2] staging: gpib: Fix NULL pointer dereference in detach

Dave Penkler posted 2 patches 1 year ago
[PATCH 1/2] staging: gpib: Fix NULL pointer dereference in detach
Posted by Dave Penkler 1 year ago
When the detach function is called after a failed attach
the usb_dev initialization can cause a NULL pointer
dereference. This happens when the usb device is not found
in the attach procedure.

Remove the usb_dev variable and initialization and change the dev
in the dev_info message from the usb_dev to the gpib_dev.

Fixes: fbae7090f30c ("staging: gpib: Update messaging and usb_device refs in agilent_usb")
Signed-off-by: Dave Penkler <dpenkler@gmail.com>
---
 drivers/staging/gpib/agilent_82357a/agilent_82357a.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/staging/gpib/agilent_82357a/agilent_82357a.c b/drivers/staging/gpib/agilent_82357a/agilent_82357a.c
index 34d85a1bdb37..2aaccebc3c7b 100644
--- a/drivers/staging/gpib/agilent_82357a/agilent_82357a.c
+++ b/drivers/staging/gpib/agilent_82357a/agilent_82357a.c
@@ -1442,12 +1442,10 @@ static int agilent_82357a_go_idle(gpib_board_t *board)
 static void agilent_82357a_detach(gpib_board_t *board)
 {
 	struct agilent_82357a_priv *a_priv;
-	struct usb_device *usb_dev;
 
 	mutex_lock(&agilent_82357a_hotplug_lock);
 
 	a_priv = board->private_data;
-	usb_dev = interface_to_usbdev(a_priv->bus_interface);
 	if (a_priv) {
 		if (a_priv->bus_interface) {
 			agilent_82357a_go_idle(board);
@@ -1459,7 +1457,7 @@ static void agilent_82357a_detach(gpib_board_t *board)
 		agilent_82357a_cleanup_urbs(a_priv);
 		agilent_82357a_free_private(a_priv);
 	}
-	dev_info(&usb_dev->dev, "%s: detached\n", __func__);
+	dev_info(board->gpib_dev, "%s: detached\n", __func__);
 	mutex_unlock(&agilent_82357a_hotplug_lock);
 }
 
-- 
2.47.1
Re: [PATCH 1/2] staging: gpib: Fix NULL pointer dereference in detach
Posted by Dan Carpenter 1 year ago
On Sat, Jan 18, 2025 at 03:50:45PM +0100, Dave Penkler wrote:
> When the detach function is called after a failed attach
> the usb_dev initialization can cause a NULL pointer
> dereference. This happens when the usb device is not found
> in the attach procedure.
> 

What you're describing is true.  The ibonline() calls ->detach if
the ->attach() function fails:

drivers/staging/gpib/agilent_82357a/agilent_82357a.c
   230          retval = board->interface->attach(board, &board->config);
                                           ^^^^^^
   231          if (retval < 0) {
   232                  board->interface->detach(board);
                                          ^^^^^^
   233                  pr_err("gpib: interface attach failed\n");
   234                  return retval;
   235          }

But this is a class of bugs which is very typical when we free things
that are not allocated.  We would normally say, "don't detach things which
are not attached".  I would be surprised if this is the last bug caused by
this particular call to ->detach().

In fact, I notice that agilent_82357a_setup_urbs() has a
kfree(a_priv->interrupt_buffer) so that's a double free bug.  It's better
to have a system so that everyone knows exactly where to put the kfree().
Fortunately #SelfPromotion I have described such a system in my blog.

https://staticthinking.wordpress.com/2022/04/28/free-the-last-thing-style/

regards,
dan carpenter
Re: [PATCH 1/2] staging: gpib: Fix NULL pointer dereference in detach
Posted by Greg KH 1 year ago
On Sat, Jan 18, 2025 at 03:50:45PM +0100, Dave Penkler wrote:
> When the detach function is called after a failed attach
> the usb_dev initialization can cause a NULL pointer
> dereference. This happens when the usb device is not found
> in the attach procedure.
> 
> Remove the usb_dev variable and initialization and change the dev
> in the dev_info message from the usb_dev to the gpib_dev.
> 
> Fixes: fbae7090f30c ("staging: gpib: Update messaging and usb_device refs in agilent_usb")
> Signed-off-by: Dave Penkler <dpenkler@gmail.com>
> ---
>  drivers/staging/gpib/agilent_82357a/agilent_82357a.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/drivers/staging/gpib/agilent_82357a/agilent_82357a.c b/drivers/staging/gpib/agilent_82357a/agilent_82357a.c
> index 34d85a1bdb37..2aaccebc3c7b 100644
> --- a/drivers/staging/gpib/agilent_82357a/agilent_82357a.c
> +++ b/drivers/staging/gpib/agilent_82357a/agilent_82357a.c
> @@ -1442,12 +1442,10 @@ static int agilent_82357a_go_idle(gpib_board_t *board)
>  static void agilent_82357a_detach(gpib_board_t *board)
>  {
>  	struct agilent_82357a_priv *a_priv;
> -	struct usb_device *usb_dev;
>  
>  	mutex_lock(&agilent_82357a_hotplug_lock);
>  
>  	a_priv = board->private_data;
> -	usb_dev = interface_to_usbdev(a_priv->bus_interface);
>  	if (a_priv) {
>  		if (a_priv->bus_interface) {
>  			agilent_82357a_go_idle(board);
> @@ -1459,7 +1457,7 @@ static void agilent_82357a_detach(gpib_board_t *board)
>  		agilent_82357a_cleanup_urbs(a_priv);
>  		agilent_82357a_free_private(a_priv);
>  	}
> -	dev_info(&usb_dev->dev, "%s: detached\n", __func__);
> +	dev_info(board->gpib_dev, "%s: detached\n", __func__);

In the future, these dev_info() lines all need to go away as when
drivers work properly, they should be quiet.  I'll take this fix for now
as it's needed, just something to plan on for the future cleanups here.

thanks,

greg k-h
Re: [PATCH 1/2] staging: gpib: Fix NULL pointer dereference in detach
Posted by Dave Penkler 1 year ago
On Sat, Jan 18, 2025 at 04:36:15PM +0100, Greg KH wrote:
> On Sat, Jan 18, 2025 at 03:50:45PM +0100, Dave Penkler wrote:
> > When the detach function is called after a failed attach
> > the usb_dev initialization can cause a NULL pointer
> > dereference. This happens when the usb device is not found
> > in the attach procedure.
> > 
> > Remove the usb_dev variable and initialization and change the dev
> > in the dev_info message from the usb_dev to the gpib_dev.
> > 
> > Fixes: fbae7090f30c ("staging: gpib: Update messaging and usb_device refs in agilent_usb")
> > Signed-off-by: Dave Penkler <dpenkler@gmail.com>
> > ---
> >  drivers/staging/gpib/agilent_82357a/agilent_82357a.c | 4 +---
> >  1 file changed, 1 insertion(+), 3 deletions(-)
> > 
> > diff --git a/drivers/staging/gpib/agilent_82357a/agilent_82357a.c b/drivers/staging/gpib/agilent_82357a/agilent_82357a.c
> > index 34d85a1bdb37..2aaccebc3c7b 100644
> > --- a/drivers/staging/gpib/agilent_82357a/agilent_82357a.c
> > +++ b/drivers/staging/gpib/agilent_82357a/agilent_82357a.c
> > @@ -1442,12 +1442,10 @@ static int agilent_82357a_go_idle(gpib_board_t *board)
> >  static void agilent_82357a_detach(gpib_board_t *board)
> >  {
> >  	struct agilent_82357a_priv *a_priv;
> > -	struct usb_device *usb_dev;
> >  
> >  	mutex_lock(&agilent_82357a_hotplug_lock);
> >  
> >  	a_priv = board->private_data;
> > -	usb_dev = interface_to_usbdev(a_priv->bus_interface);
> >  	if (a_priv) {
> >  		if (a_priv->bus_interface) {
> >  			agilent_82357a_go_idle(board);
> > @@ -1459,7 +1457,7 @@ static void agilent_82357a_detach(gpib_board_t *board)
> >  		agilent_82357a_cleanup_urbs(a_priv);
> >  		agilent_82357a_free_private(a_priv);
> >  	}
> > -	dev_info(&usb_dev->dev, "%s: detached\n", __func__);
> > +	dev_info(board->gpib_dev, "%s: detached\n", __func__);
> 
> In the future, these dev_info() lines all need to go away as when
> drivers work properly, they should be quiet.  I'll take this fix for now
> as it's needed, just something to plan on for the future cleanups here.
> 
> thanks,
> 
> greg k-h
OK
thanks,
-dave