linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ns83820: Avoid bad pointer deref in ns83820_init_one().
@ 2011-01-17 20:24 Jesper Juhl
  2011-01-18 14:11 ` Denis Kirjanov
  2011-01-18 16:42 ` Benjamin LaHaise
  0 siblings, 2 replies; 4+ messages in thread
From: Jesper Juhl @ 2011-01-17 20:24 UTC (permalink / raw)
  To: netdev
  Cc: linux-ns83820, linux-kernel, Tejun Heo, Tejun Heo,
	Kulikov Vasiliy, Denis Kirjanov, David S. Miller,
	Benjamin LaHaise

In drivers/net/ns83820.c::ns83820_init_one() we dynamically allocate 
memory via alloc_etherdev(). We then call PRIV() on the returned storage 
which is 'return netdev_priv()'. netdev_priv() takes the pointer it is 
passed and adds 'ALIGN(sizeof(struct net_device), NETDEV_ALIGN)' to it and 
returns it. Then we test the resulting pointer for NULL, which it is 
unlikely to be at this point, and later dereference it. This will go bad 
if alloc_etherdev() actually returned NULL.

This patch reworks the code slightly so that we test for a NULL pointer 
(and return -ENOMEM) directly after calling alloc_etherdev().

Signed-off-by: Jesper Juhl <jj@chaosbits.net>
---
 ns83820.c |    5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

  Compile tested only. I have no way to test this for real.

diff --git a/drivers/net/ns83820.c b/drivers/net/ns83820.c
index 84134c7..a41b2cf 100644
--- a/drivers/net/ns83820.c
+++ b/drivers/net/ns83820.c
@@ -1988,12 +1988,11 @@ static int __devinit ns83820_init_one(struct pci_dev *pci_dev,
 	}
 
 	ndev = alloc_etherdev(sizeof(struct ns83820));
-	dev = PRIV(ndev);
-
 	err = -ENOMEM;
-	if (!dev)
+	if (!ndev)
 		goto out;
 
+	dev = PRIV(ndev);
 	dev->ndev = ndev;
 
 	spin_lock_init(&dev->rx_info.lock);


-- 
Jesper Juhl <jj@chaosbits.net>            http://www.chaosbits.net/
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please.


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] ns83820: Avoid bad pointer deref in ns83820_init_one().
  2011-01-17 20:24 [PATCH] ns83820: Avoid bad pointer deref in ns83820_init_one() Jesper Juhl
@ 2011-01-18 14:11 ` Denis Kirjanov
  2011-01-18 16:42 ` Benjamin LaHaise
  1 sibling, 0 replies; 4+ messages in thread
From: Denis Kirjanov @ 2011-01-18 14:11 UTC (permalink / raw)
  To: Jesper Juhl
  Cc: netdev, linux-ns83820, linux-kernel, Tejun Heo, Kulikov Vasiliy,
	David S. Miller, Benjamin LaHaise

On Mon, Jan 17, 2011 at 09:24:57PM +0100, Jesper Juhl wrote:
> In drivers/net/ns83820.c::ns83820_init_one() we dynamically allocate 
> memory via alloc_etherdev(). We then call PRIV() on the returned storage 
> which is 'return netdev_priv()'. netdev_priv() takes the pointer it is 
> passed and adds 'ALIGN(sizeof(struct net_device), NETDEV_ALIGN)' to it and 
> returns it. Then we test the resulting pointer for NULL, which it is 
> unlikely to be at this point, and later dereference it. This will go bad 
> if alloc_etherdev() actually returned NULL.
> 
> This patch reworks the code slightly so that we test for a NULL pointer 
> (and return -ENOMEM) directly after calling alloc_etherdev().
> 
> Signed-off-by: Jesper Juhl <jj@chaosbits.net>
Yeah, the previous code was a little bit ugly.
Good catch!
> ---
>  ns83820.c |    5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
>   Compile tested only. I have no way to test this for real.
> 
> diff --git a/drivers/net/ns83820.c b/drivers/net/ns83820.c
> index 84134c7..a41b2cf 100644
> --- a/drivers/net/ns83820.c
> +++ b/drivers/net/ns83820.c
> @@ -1988,12 +1988,11 @@ static int __devinit ns83820_init_one(struct pci_dev *pci_dev,
>  	}
>  
>  	ndev = alloc_etherdev(sizeof(struct ns83820));
> -	dev = PRIV(ndev);
> -
>  	err = -ENOMEM;
> -	if (!dev)
> +	if (!ndev)
>  		goto out;
>  
> +	dev = PRIV(ndev);
>  	dev->ndev = ndev;
>  
>  	spin_lock_init(&dev->rx_info.lock);
> 
> 
> -- 
> Jesper Juhl <jj@chaosbits.net>            http://www.chaosbits.net/
> Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
> Plain text mails only, please.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] ns83820: Avoid bad pointer deref in ns83820_init_one().
  2011-01-17 20:24 [PATCH] ns83820: Avoid bad pointer deref in ns83820_init_one() Jesper Juhl
  2011-01-18 14:11 ` Denis Kirjanov
@ 2011-01-18 16:42 ` Benjamin LaHaise
  2011-01-19  0:14   ` David Miller
  1 sibling, 1 reply; 4+ messages in thread
From: Benjamin LaHaise @ 2011-01-18 16:42 UTC (permalink / raw)
  To: Jesper Juhl
  Cc: netdev, linux-ns83820, linux-kernel, Tejun Heo, Kulikov Vasiliy,
	Denis Kirjanov, David S. Miller

On Mon, Jan 17, 2011 at 09:24:57PM +0100, Jesper Juhl wrote:
> In drivers/net/ns83820.c::ns83820_init_one() we dynamically allocate 
> memory via alloc_etherdev(). We then call PRIV() on the returned storage 
> which is 'return netdev_priv()'. netdev_priv() takes the pointer it is 
> passed and adds 'ALIGN(sizeof(struct net_device), NETDEV_ALIGN)' to it and 
> returns it. Then we test the resulting pointer for NULL, which it is 
> unlikely to be at this point, and later dereference it. This will go bad 
> if alloc_etherdev() actually returned NULL.
> 
> This patch reworks the code slightly so that we test for a NULL pointer 
> (and return -ENOMEM) directly after calling alloc_etherdev().

Looks good.

		-ben

Signed-off-by: Benjamin LaHaise <bcrl@kvack.org>

> Signed-off-by: Jesper Juhl <jj@chaosbits.net>
> ---
>  ns83820.c |    5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
>   Compile tested only. I have no way to test this for real.
> 
> diff --git a/drivers/net/ns83820.c b/drivers/net/ns83820.c
> index 84134c7..a41b2cf 100644
> --- a/drivers/net/ns83820.c
> +++ b/drivers/net/ns83820.c
> @@ -1988,12 +1988,11 @@ static int __devinit ns83820_init_one(struct pci_dev *pci_dev,
>  	}
>  
>  	ndev = alloc_etherdev(sizeof(struct ns83820));
> -	dev = PRIV(ndev);
> -
>  	err = -ENOMEM;
> -	if (!dev)
> +	if (!ndev)
>  		goto out;
>  
> +	dev = PRIV(ndev);
>  	dev->ndev = ndev;
>  
>  	spin_lock_init(&dev->rx_info.lock);
> 
> 
> -- 
> Jesper Juhl <jj@chaosbits.net>            http://www.chaosbits.net/
> Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
> Plain text mails only, please.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] ns83820: Avoid bad pointer deref in ns83820_init_one().
  2011-01-18 16:42 ` Benjamin LaHaise
@ 2011-01-19  0:14   ` David Miller
  0 siblings, 0 replies; 4+ messages in thread
From: David Miller @ 2011-01-19  0:14 UTC (permalink / raw)
  To: bcrl; +Cc: jj, netdev, linux-ns83820, linux-kernel, tj, segooon, dkirjanov

From: Benjamin LaHaise <bcrl@kvack.org>
Date: Tue, 18 Jan 2011 11:42:00 -0500

> On Mon, Jan 17, 2011 at 09:24:57PM +0100, Jesper Juhl wrote:
>> In drivers/net/ns83820.c::ns83820_init_one() we dynamically allocate 
>> memory via alloc_etherdev(). We then call PRIV() on the returned storage 
>> which is 'return netdev_priv()'. netdev_priv() takes the pointer it is 
>> passed and adds 'ALIGN(sizeof(struct net_device), NETDEV_ALIGN)' to it and 
>> returns it. Then we test the resulting pointer for NULL, which it is 
>> unlikely to be at this point, and later dereference it. This will go bad 
>> if alloc_etherdev() actually returned NULL.
>> 
>> This patch reworks the code slightly so that we test for a NULL pointer 
>> (and return -ENOMEM) directly after calling alloc_etherdev().
> 
> Looks good.
> 
> 		-ben
> 
> Signed-off-by: Benjamin LaHaise <bcrl@kvack.org>

Applied, thanks everyone.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-01-19  0:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-17 20:24 [PATCH] ns83820: Avoid bad pointer deref in ns83820_init_one() Jesper Juhl
2011-01-18 14:11 ` Denis Kirjanov
2011-01-18 16:42 ` Benjamin LaHaise
2011-01-19  0:14   ` David Miller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).