linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] block: fix bsg_unregister and bsg_open race
@ 2018-06-13 22:14 Anatoliy Glagolev
  2018-06-14  8:38 ` Christoph Hellwig
  0 siblings, 1 reply; 5+ messages in thread
From: Anatoliy Glagolev @ 2018-06-13 22:14 UTC (permalink / raw)
  To: linux-block, James E.J. Bottomley, FUJITA Tomonori, Jens Axboe,
	linux-scsi, Christoph Hellwig
  Cc: linux-kernel

The existing implementation allows races between bsg_unregister and
bsg_open paths. bsg_ungegister and request_queue cleanup and
deletion may start and complete right after bsg_get_device (in bsg_open path)
retrieves bsg_class_device and releases the mutex. Then bsg_open path
touches freed memory of bsg_class_device and request_queue.

One possible fix is to hold the mutex all the way through bsg_get_device
instead of releasing it after bsg_class_device retrieval.

From a8647f9cfb3b2b69dcac493554cb6ea2f9b4c2dd Mon Sep 17 00:00:00 2001
From: Anatoliy Glagolev <glagolig@gmail.com>
Date: Wed, 13 Jun 2018 15:38:51 -0600
Subject: [PATCH] Fix race of bsg_open and bsg_unregister
Signed-Off-By: Anatoliy Glagolev <glagolig@gmail.com>

---
 block/bsg.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/block/bsg.c b/block/bsg.c
index 132e657..10bc6a4 100644
--- a/block/bsg.c
+++ b/block/bsg.c
@@ -693,6 +693,8 @@ static struct bsg_device *bsg_add_device(struct inode *inode,
 	struct bsg_device *bd;
 	unsigned char buf[32];
 
+	lockdep_assert_held(&bsg_mutex);
+
 	if (!blk_get_queue(rq))
 		return ERR_PTR(-ENXIO);
 
@@ -707,14 +709,12 @@ static struct bsg_device *bsg_add_device(struct inode *inode,
 	bsg_set_block(bd, file);
 
 	atomic_set(&bd->ref_count, 1);
-	mutex_lock(&bsg_mutex);
 	hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode)));
 
 	strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1);
 	bsg_dbg(bd, "bound to <%s>, max queue %d\n",
 		format_dev_t(buf, inode->i_rdev), bd->max_queue);
 
-	mutex_unlock(&bsg_mutex);
 	return bd;
 }
 
@@ -722,7 +722,7 @@ static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q)
 {
 	struct bsg_device *bd;
 
-	mutex_lock(&bsg_mutex);
+	lockdep_assert_held(&bsg_mutex);
 
 	hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) {
 		if (bd->queue == q) {
@@ -732,7 +732,6 @@ static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q)
 	}
 	bd = NULL;
 found:
-	mutex_unlock(&bsg_mutex);
 	return bd;
 }
 
@@ -746,16 +745,18 @@ static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
 	 */
 	mutex_lock(&bsg_mutex);
 	bcd = idr_find(&bsg_minor_idr, iminor(inode));
-	mutex_unlock(&bsg_mutex);
 
 	if (!bcd)
 		return ERR_PTR(-ENODEV);
 
 	bd = __bsg_get_device(iminor(inode), bcd->queue);
-	if (bd)
+	if (bd) {
+		mutex_unlock(&bsg_mutex);
 		return bd;
+	}
 
 	bd = bsg_add_device(inode, bcd->queue, file);
+	mutex_unlock(&bsg_mutex);
 
 	return bd;
 }
-- 
1.9.1


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

* Re: [PATCH] block: fix bsg_unregister and bsg_open race
  2018-06-13 22:14 [PATCH] block: fix bsg_unregister and bsg_open race Anatoliy Glagolev
@ 2018-06-14  8:38 ` Christoph Hellwig
  2018-06-14 23:47   ` [PATCHv2] " Anatoliy Glagolev
  0 siblings, 1 reply; 5+ messages in thread
From: Christoph Hellwig @ 2018-06-14  8:38 UTC (permalink / raw)
  To: Anatoliy Glagolev
  Cc: linux-block, James E.J. Bottomley, FUJITA Tomonori, Jens Axboe,
	linux-scsi, Christoph Hellwig, linux-kernel

On Wed, Jun 13, 2018 at 04:14:18PM -0600, Anatoliy Glagolev wrote:
> The existing implementation allows races between bsg_unregister and
> bsg_open paths. bsg_ungegister and request_queue cleanup and
> deletion may start and complete right after bsg_get_device (in bsg_open path)
> retrieves bsg_class_device and releases the mutex. Then bsg_open path
> touches freed memory of bsg_class_device and request_queue.
> 
> One possible fix is to hold the mutex all the way through bsg_get_device
> instead of releasing it after bsg_class_device retrieval.

This looks generally fine to me.  Nitpicks below:

> @@ -746,16 +745,18 @@ static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
>  	 */
>  	mutex_lock(&bsg_mutex);
>  	bcd = idr_find(&bsg_minor_idr, iminor(inode));
> -	mutex_unlock(&bsg_mutex);
>  
>  	if (!bcd)
>  		return ERR_PTR(-ENODEV);

This needs to unlock the mutex. E.g.

	if (!bcd) {
		bd = ERR_PTR(-ENODEV);
		goto out_unlock;
	}


>  	bd = __bsg_get_device(iminor(inode), bcd->queue);
> +	if (bd) {
> +		mutex_unlock(&bsg_mutex);
>  		return bd;
> +	}
>  
>  	bd = bsg_add_device(inode, bcd->queue, file);
> +	mutex_unlock(&bsg_mutex);
>  
>  	return bd;

I'd simply do:

	bd = __bsg_get_device(iminor(inode), bcd->queue);
	if (!bd)
		bd = bsg_add_device(inode, bcd->queue, file);
out_unlock:
	mutex_unlock(&bsg_mutex);
	return bd;

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

* Re: [PATCHv2] block: fix bsg_unregister and bsg_open race
  2018-06-14  8:38 ` Christoph Hellwig
@ 2018-06-14 23:47   ` Anatoliy Glagolev
  2018-06-15  9:27     ` Christoph Hellwig
  0 siblings, 1 reply; 5+ messages in thread
From: Anatoliy Glagolev @ 2018-06-14 23:47 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: linux-block, James E.J. Bottomley, FUJITA Tomonori, Jens Axboe,
	linux-scsi, linux-kernel

Thanks, Christoph. Good catch releasing the mutex on failure path.
V2 patch with your suggestions applied.


From 7a8dc4b10f344d915658549581158f3da2cc1f91 Mon Sep 17 00:00:00 2001
From: Anatoliy Glagolev <glagolig@gmail.com>
Date: Wed, 13 Jun 2018 15:38:51 -0600
Subject: [PATCHv2] Fix race of bsg_open and bsg_unregister
Signed-Off-By: Anatoliy Glagolev <glagolig@gmail.com>

---
 block/bsg.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/block/bsg.c b/block/bsg.c
index 132e657..66602c4 100644
--- a/block/bsg.c
+++ b/block/bsg.c
@@ -693,6 +693,8 @@ static struct bsg_device *bsg_add_device(struct inode *inode,
 	struct bsg_device *bd;
 	unsigned char buf[32];
 
+	lockdep_assert_held(&bsg_mutex);
+
 	if (!blk_get_queue(rq))
 		return ERR_PTR(-ENXIO);
 
@@ -707,14 +709,12 @@ static struct bsg_device *bsg_add_device(struct inode *inode,
 	bsg_set_block(bd, file);
 
 	atomic_set(&bd->ref_count, 1);
-	mutex_lock(&bsg_mutex);
 	hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode)));
 
 	strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1);
 	bsg_dbg(bd, "bound to <%s>, max queue %d\n",
 		format_dev_t(buf, inode->i_rdev), bd->max_queue);
 
-	mutex_unlock(&bsg_mutex);
 	return bd;
 }
 
@@ -722,7 +722,7 @@ static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q)
 {
 	struct bsg_device *bd;
 
-	mutex_lock(&bsg_mutex);
+	lockdep_assert_held(&bsg_mutex);
 
 	hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) {
 		if (bd->queue == q) {
@@ -732,7 +732,6 @@ static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q)
 	}
 	bd = NULL;
 found:
-	mutex_unlock(&bsg_mutex);
 	return bd;
 }
 
@@ -746,17 +745,18 @@ static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
 	 */
 	mutex_lock(&bsg_mutex);
 	bcd = idr_find(&bsg_minor_idr, iminor(inode));
-	mutex_unlock(&bsg_mutex);
 
-	if (!bcd)
-		return ERR_PTR(-ENODEV);
+	if (!bcd) {
+		bd = ERR_PTR(-ENODEV);
+		goto out_unlock;
+	}
 
 	bd = __bsg_get_device(iminor(inode), bcd->queue);
-	if (bd)
-		return bd;
-
-	bd = bsg_add_device(inode, bcd->queue, file);
+	if (!bd)
+		bd = bsg_add_device(inode, bcd->queue, file);
 
+out_unlock:
+	mutex_unlock(&bsg_mutex);
 	return bd;
 }
 
-- 
1.9.1


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

* Re: [PATCHv2] block: fix bsg_unregister and bsg_open race
  2018-06-14 23:47   ` [PATCHv2] " Anatoliy Glagolev
@ 2018-06-15  9:27     ` Christoph Hellwig
  2018-06-15 14:16       ` Jens Axboe
  0 siblings, 1 reply; 5+ messages in thread
From: Christoph Hellwig @ 2018-06-15  9:27 UTC (permalink / raw)
  To: Anatoliy Glagolev
  Cc: Christoph Hellwig, linux-block, James E.J. Bottomley,
	FUJITA Tomonori, Jens Axboe, linux-scsi, linux-kernel

On Thu, Jun 14, 2018 at 05:47:34PM -0600, Anatoliy Glagolev wrote:
> Thanks, Christoph. Good catch releasing the mutex on failure path.
> V2 patch with your suggestions applied.

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

Now just re-add the changelog and resend it in a new thread.

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

* Re: [PATCHv2] block: fix bsg_unregister and bsg_open race
  2018-06-15  9:27     ` Christoph Hellwig
@ 2018-06-15 14:16       ` Jens Axboe
  0 siblings, 0 replies; 5+ messages in thread
From: Jens Axboe @ 2018-06-15 14:16 UTC (permalink / raw)
  To: Christoph Hellwig, Anatoliy Glagolev
  Cc: linux-block, James E.J. Bottomley, FUJITA Tomonori, linux-scsi,
	linux-kernel

On 6/15/18 3:27 AM, Christoph Hellwig wrote:
> On Thu, Jun 14, 2018 at 05:47:34PM -0600, Anatoliy Glagolev wrote:
>> Thanks, Christoph. Good catch releasing the mutex on failure path.
>> V2 patch with your suggestions applied.
> 
> Looks good:
> 
> Reviewed-by: Christoph Hellwig <hch@lst.de>
> 
> Now just re-add the changelog and resend it in a new thread.

The original one was mixed up, too. Anatoliy, I fixed it up for
you. But for future patches, please follow the usual patch
format where the commit message is actually in the patch.

-- 
Jens Axboe


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

end of thread, other threads:[~2018-06-15 14:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-13 22:14 [PATCH] block: fix bsg_unregister and bsg_open race Anatoliy Glagolev
2018-06-14  8:38 ` Christoph Hellwig
2018-06-14 23:47   ` [PATCHv2] " Anatoliy Glagolev
2018-06-15  9:27     ` Christoph Hellwig
2018-06-15 14:16       ` Jens Axboe

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).