diff mbox

Fix NULL pointer dereference in bl_free_device().

Message ID 1468927851-2186-1-git-send-email-asavkov@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Artem Savkov July 19, 2016, 11:30 a.m. UTC
When bl_parse_deviceid() fails in bl_alloc_deviceid_node() on
blkdev_get_by_*() step we get an pnfs_block_dev struct that is
uninitialized except for bdev field which is set to whatever error
blkdev_get_by_*() returns.  bl_free_device() then tries to call
blkdev_put() if bdev is not 0 resulting in a wrong pointer dereference.

Fixing this by making sure bdev is not an error code in bl_free_device().

Signed-off-by: Artem Savkov <asavkov@redhat.com>
---
 fs/nfs/blocklayout/dev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Schumaker, Anna July 19, 2016, 1:57 p.m. UTC | #1
Hi Artem,

On 07/19/2016 07:30 AM, Artem Savkov wrote:
> When bl_parse_deviceid() fails in bl_alloc_deviceid_node() on
> blkdev_get_by_*() step we get an pnfs_block_dev struct that is
> uninitialized except for bdev field which is set to whatever error
> blkdev_get_by_*() returns.  bl_free_device() then tries to call
> blkdev_put() if bdev is not 0 resulting in a wrong pointer dereference.
> 
> Fixing this by making sure bdev is not an error code in bl_free_device().
> 
> Signed-off-by: Artem Savkov <asavkov@redhat.com>
> ---
>  fs/nfs/blocklayout/dev.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fs/nfs/blocklayout/dev.c b/fs/nfs/blocklayout/dev.c
> index 118252f..7db3af0 100644
> --- a/fs/nfs/blocklayout/dev.c
> +++ b/fs/nfs/blocklayout/dev.c
> @@ -33,7 +33,7 @@ bl_free_device(struct pnfs_block_dev *dev)
>  				pr_err("failed to unregister PR key.\n");
>  		}
>  
> -		if (dev->bdev)
> +		if (dev->bdev && !IS_ERR(dev->bdev))

This looks pretty straightforward, but can you use IS_ERR_OR_NULL() instead?  It accomplishes the same check with slightly cleaner code :)

Thanks,
Anna

>  			blkdev_put(dev->bdev, FMODE_READ | FMODE_WRITE);
>  	}
>  }
> 

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Schumaker, Anna July 19, 2016, 3:21 p.m. UTC | #2
Hi Artem,

On 07/19/2016 07:30 AM, Artem Savkov wrote:
> When bl_parse_deviceid() fails in bl_alloc_deviceid_node() on
> blkdev_get_by_*() step we get an pnfs_block_dev struct that is
> uninitialized except for bdev field which is set to whatever error
> blkdev_get_by_*() returns.  bl_free_device() then tries to call
> blkdev_put() if bdev is not 0 resulting in a wrong pointer dereference.
> 
> Fixing this by making sure bdev is not an error code in bl_free_device().
> 
> Signed-off-by: Artem Savkov <asavkov@redhat.com>
> ---
>  fs/nfs/blocklayout/dev.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fs/nfs/blocklayout/dev.c b/fs/nfs/blocklayout/dev.c
> index 118252f..7db3af0 100644
> --- a/fs/nfs/blocklayout/dev.c
> +++ b/fs/nfs/blocklayout/dev.c
> @@ -33,7 +33,7 @@ bl_free_device(struct pnfs_block_dev *dev)
>  				pr_err("failed to unregister PR key.\n");
>  		}
>  
> -		if (dev->bdev)
> +		if (dev->bdev && !IS_ERR(dev->bdev))

This looks pretty straightforward, but can you use IS_ERR_OR_NULL() instead?  It accomplishes the same check with slightly cleaner code :)

Thanks,
Anna

>  			blkdev_put(dev->bdev, FMODE_READ | FMODE_WRITE);
>  	}
>  }
> 

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

Patch

diff --git a/fs/nfs/blocklayout/dev.c b/fs/nfs/blocklayout/dev.c
index 118252f..7db3af0 100644
--- a/fs/nfs/blocklayout/dev.c
+++ b/fs/nfs/blocklayout/dev.c
@@ -33,7 +33,7 @@  bl_free_device(struct pnfs_block_dev *dev)
 				pr_err("failed to unregister PR key.\n");
 		}
 
-		if (dev->bdev)
+		if (dev->bdev && !IS_ERR(dev->bdev))
 			blkdev_put(dev->bdev, FMODE_READ | FMODE_WRITE);
 	}
 }