drivers/media/video: avoid NULL dereference

Message ID Pine.LNX.4.64.1003212230380.12371@ask.diku.dk (mailing list archive)
State Superseded, archived
Headers

Commit Message

Julia Lawall March 21, 2010, 9:31 p.m. UTC
  From: Julia Lawall <julia@diku.dk>

If ov is NULL, it will not be possible to take the lock in the first place,
so move the test up earlier.

The semantic match that finds the problem is as follows:
(http://coccinelle.lip6.fr/)

// <smpl>
@r exists@
expression E, E1;
identifier f;
statement S1,S3;
iterator iter;
@@

if ((E == NULL && ...) || ...)
{
  ... when != false ((E == NULL && ...) || ...)
      when != true  ((E != NULL && ...) || ...)
      when != iter(E,...) S1
      when != E = E1
(
  sizeof(E->f)
|
* E->f
)
  ... when any
  return ...;
}
else S3
// </smpl>

Signed-off-by: Julia Lawall <julia@diku.dk>

---
 drivers/media/video/ov511.c         |    8 +++-----
 1 files changed, 3 insertions(+), 5 deletions(-)

--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
  

Comments

Andrew Morton March 23, 2010, 3:14 a.m. UTC | #1
On Sun, 21 Mar 2010 22:31:06 +0100 (CET) Julia Lawall <julia@diku.dk> wrote:

> From: Julia Lawall <julia@diku.dk>
> 
> If ov is NULL, it will not be possible to take the lock in the first place,
> so move the test up earlier.
> 
> ...
>
> --- a/drivers/media/video/ov511.c
> +++ b/drivers/media/video/ov511.c
> @@ -5913,14 +5913,12 @@ ov51x_disconnect(struct usb_interface *intf)
>  
>  	PDEBUG(3, "");
>  
> +	if (!ov)
> +		return;
> +
>  	mutex_lock(&ov->lock);
>  	usb_set_intfdata (intf, NULL);
>  
> -	if (!ov) {
> -		mutex_unlock(&ov->lock);
> -		return;
> -	}
> -
>  	/* Free device number */
>  	ov511_devused &= ~(1 << ov->nr);

I think we can pretty safely assume that we never get here with ov==NULL.

Oh well, I'll leave the test there for others to ponder.
--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
  
Julia Lawall March 23, 2010, 6:24 a.m. UTC | #2
> I think we can pretty safely assume that we never get here with ov==NULL.
> 
> Oh well, I'll leave the test there for others to ponder.

OK.  I didn't read far enough in this email and sent another patch, in 
case it's useful.

julia
--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
  

Patch

diff --git a/drivers/media/video/ov511.c b/drivers/media/video/ov511.c
index e0bce8d..2357218 100644
--- a/drivers/media/video/ov511.c
+++ b/drivers/media/video/ov511.c
@@ -5913,14 +5913,12 @@  ov51x_disconnect(struct usb_interface *intf)
 
 	PDEBUG(3, "");
 
+	if (!ov)
+		return;
+
 	mutex_lock(&ov->lock);
 	usb_set_intfdata (intf, NULL);
 
-	if (!ov) {
-		mutex_unlock(&ov->lock);
-		return;
-	}
-
 	/* Free device number */
 	ov511_devused &= ~(1 << ov->nr);