Commit d6c73964 authored by Anatoliy Glagolev's avatar Anatoliy Glagolev Committed by Jens Axboe

bsg: fix race of bsg_open and bsg_unregister

The existing implementation allows races between bsg_unregister and
bsg_open paths. bsg_unregister 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.
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-Off-By: default avatarAnatoliy Glagolev <glagolig@gmail.com>
Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent be7f99c5
...@@ -693,6 +693,8 @@ static struct bsg_device *bsg_add_device(struct inode *inode, ...@@ -693,6 +693,8 @@ static struct bsg_device *bsg_add_device(struct inode *inode,
struct bsg_device *bd; struct bsg_device *bd;
unsigned char buf[32]; unsigned char buf[32];
lockdep_assert_held(&bsg_mutex);
if (!blk_get_queue(rq)) if (!blk_get_queue(rq))
return ERR_PTR(-ENXIO); return ERR_PTR(-ENXIO);
...@@ -707,14 +709,12 @@ static struct bsg_device *bsg_add_device(struct inode *inode, ...@@ -707,14 +709,12 @@ static struct bsg_device *bsg_add_device(struct inode *inode,
bsg_set_block(bd, file); bsg_set_block(bd, file);
atomic_set(&bd->ref_count, 1); atomic_set(&bd->ref_count, 1);
mutex_lock(&bsg_mutex);
hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode))); 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); strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1);
bsg_dbg(bd, "bound to <%s>, max queue %d\n", bsg_dbg(bd, "bound to <%s>, max queue %d\n",
format_dev_t(buf, inode->i_rdev), bd->max_queue); format_dev_t(buf, inode->i_rdev), bd->max_queue);
mutex_unlock(&bsg_mutex);
return bd; return bd;
} }
...@@ -722,7 +722,7 @@ static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q) ...@@ -722,7 +722,7 @@ static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q)
{ {
struct bsg_device *bd; 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) { hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) {
if (bd->queue == q) { if (bd->queue == q) {
...@@ -732,7 +732,6 @@ static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q) ...@@ -732,7 +732,6 @@ static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q)
} }
bd = NULL; bd = NULL;
found: found:
mutex_unlock(&bsg_mutex);
return bd; return bd;
} }
...@@ -746,17 +745,18 @@ static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file) ...@@ -746,17 +745,18 @@ static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
*/ */
mutex_lock(&bsg_mutex); mutex_lock(&bsg_mutex);
bcd = idr_find(&bsg_minor_idr, iminor(inode)); bcd = idr_find(&bsg_minor_idr, iminor(inode));
mutex_unlock(&bsg_mutex);
if (!bcd) if (!bcd) {
return ERR_PTR(-ENODEV); bd = ERR_PTR(-ENODEV);
goto out_unlock;
}
bd = __bsg_get_device(iminor(inode), bcd->queue); bd = __bsg_get_device(iminor(inode), bcd->queue);
if (bd) if (!bd)
return bd; bd = bsg_add_device(inode, bcd->queue, file);
bd = bsg_add_device(inode, bcd->queue, file);
out_unlock:
mutex_unlock(&bsg_mutex);
return bd; return bd;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment