diff mbox series

net: dsa: b53: Fix dereference of null dev

Message ID 20210612144407.60259-1-colin.king@canonical.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series net: dsa: b53: Fix dereference of null dev | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Guessed tree name to be net-next
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cc_maintainers success CCed 7 of 7 maintainers
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 16 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link

Commit Message

Colin King June 12, 2021, 2:44 p.m. UTC
From: Colin Ian King <colin.king@canonical.com>

Currently pointer priv is dereferencing dev before dev is being null
checked so a potential null pointer dereference can occur. Fix this
by only assigning and using priv if dev is not-null.

Addresses-Coverity: ("Dereference before null check")
Fixes: 16994374a6fc ("net: dsa: b53: Make SRAB driver manage port interrupts")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 drivers/net/dsa/b53/b53_srab.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

Comments

Vladimir Oltean June 12, 2021, 3:02 p.m. UTC | #1
On Sat, Jun 12, 2021 at 03:44:07PM +0100, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> Currently pointer priv is dereferencing dev before dev is being null
> checked so a potential null pointer dereference can occur. Fix this
> by only assigning and using priv if dev is not-null.
> 
> Addresses-Coverity: ("Dereference before null check")
> Fixes: 16994374a6fc ("net: dsa: b53: Make SRAB driver manage port interrupts")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  drivers/net/dsa/b53/b53_srab.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/net/dsa/b53/b53_srab.c b/drivers/net/dsa/b53/b53_srab.c
> index aaa12d73784e..e77ac598f859 100644
> --- a/drivers/net/dsa/b53/b53_srab.c
> +++ b/drivers/net/dsa/b53/b53_srab.c
> @@ -629,11 +629,13 @@ static int b53_srab_probe(struct platform_device *pdev)
>  static int b53_srab_remove(struct platform_device *pdev)
>  {
>  	struct b53_device *dev = platform_get_drvdata(pdev);
> -	struct b53_srab_priv *priv = dev->priv;
>  
> -	b53_srab_intr_set(priv, false);
> -	if (dev)
> +	if (dev) {
> +		struct b53_srab_priv *priv = dev->priv;
> +
> +		b53_srab_intr_set(priv, false);
>  		b53_switch_remove(dev);
> +	}
>  
>  	return 0;
>  }
> -- 
> 2.31.1
> 

I think the better question is how can "dev" be NULL in the first
place, since b53_srab_probe() does an unconditional platform_set_drvdata()
with what appears to be a non-NULL dev.
Dan Carpenter June 14, 2021, 11:28 a.m. UTC | #2
On Sat, Jun 12, 2021 at 03:44:07PM +0100, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> Currently pointer priv is dereferencing dev before dev is being null
> checked so a potential null pointer dereference can occur. Fix this
> by only assigning and using priv if dev is not-null.
> 
> Addresses-Coverity: ("Dereference before null check")
> Fixes: 16994374a6fc ("net: dsa: b53: Make SRAB driver manage port interrupts")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  drivers/net/dsa/b53/b53_srab.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/net/dsa/b53/b53_srab.c b/drivers/net/dsa/b53/b53_srab.c
> index aaa12d73784e..e77ac598f859 100644
> --- a/drivers/net/dsa/b53/b53_srab.c
> +++ b/drivers/net/dsa/b53/b53_srab.c
> @@ -629,11 +629,13 @@ static int b53_srab_probe(struct platform_device *pdev)
>  static int b53_srab_remove(struct platform_device *pdev)
>  {
>  	struct b53_device *dev = platform_get_drvdata(pdev);
> -	struct b53_srab_priv *priv = dev->priv;
>  
> -	b53_srab_intr_set(priv, false);
> -	if (dev)
> +	if (dev) {

This is the remove function and "dev" can't be NULL at this point.
Better to just remove the NULL check.

regards,
dan carpenter
Colin King June 15, 2021, 8:33 a.m. UTC | #3
On 14/06/2021 12:28, Dan Carpenter wrote:
> On Sat, Jun 12, 2021 at 03:44:07PM +0100, Colin King wrote:
>> From: Colin Ian King <colin.king@canonical.com>
>>
>> Currently pointer priv is dereferencing dev before dev is being null
>> checked so a potential null pointer dereference can occur. Fix this
>> by only assigning and using priv if dev is not-null.
>>
>> Addresses-Coverity: ("Dereference before null check")
>> Fixes: 16994374a6fc ("net: dsa: b53: Make SRAB driver manage port interrupts")
>> Signed-off-by: Colin Ian King <colin.king@canonical.com>
>> ---
>>  drivers/net/dsa/b53/b53_srab.c | 8 +++++---
>>  1 file changed, 5 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/net/dsa/b53/b53_srab.c b/drivers/net/dsa/b53/b53_srab.c
>> index aaa12d73784e..e77ac598f859 100644
>> --- a/drivers/net/dsa/b53/b53_srab.c
>> +++ b/drivers/net/dsa/b53/b53_srab.c
>> @@ -629,11 +629,13 @@ static int b53_srab_probe(struct platform_device *pdev)
>>  static int b53_srab_remove(struct platform_device *pdev)
>>  {
>>  	struct b53_device *dev = platform_get_drvdata(pdev);
>> -	struct b53_srab_priv *priv = dev->priv;
>>  
>> -	b53_srab_intr_set(priv, false);
>> -	if (dev)
>> +	if (dev) {
> 
> This is the remove function and "dev" can't be NULL at this point.
> Better to just remove the NULL check.

Will do.

> 
> regards,
> dan carpenter
>
diff mbox series

Patch

diff --git a/drivers/net/dsa/b53/b53_srab.c b/drivers/net/dsa/b53/b53_srab.c
index aaa12d73784e..e77ac598f859 100644
--- a/drivers/net/dsa/b53/b53_srab.c
+++ b/drivers/net/dsa/b53/b53_srab.c
@@ -629,11 +629,13 @@  static int b53_srab_probe(struct platform_device *pdev)
 static int b53_srab_remove(struct platform_device *pdev)
 {
 	struct b53_device *dev = platform_get_drvdata(pdev);
-	struct b53_srab_priv *priv = dev->priv;
 
-	b53_srab_intr_set(priv, false);
-	if (dev)
+	if (dev) {
+		struct b53_srab_priv *priv = dev->priv;
+
+		b53_srab_intr_set(priv, false);
 		b53_switch_remove(dev);
+	}
 
 	return 0;
 }