All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] block: fix locking in bdev_del_partition
@ 2020-09-01  9:59 Christoph Hellwig
  2020-09-01 14:35 ` Jens Axboe
  0 siblings, 1 reply; 4+ messages in thread
From: Christoph Hellwig @ 2020-09-01  9:59 UTC (permalink / raw)
  To: axboe; +Cc: linux-block, syzbot+6448f3c229bc52b82f69

We need to hold the whole device bd_mutex to protect against
other thread concurrently deleting out partition before we get
to it, and thus causing a use after free.

Fixes: cddae808aeb7 ("block: pass a hd_struct to delete_partition")
Reported-by: syzbot+6448f3c229bc52b82f69@syzkaller.appspotmail.com
Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 block/partitions/core.c | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)

diff --git a/block/partitions/core.c b/block/partitions/core.c
index e62a98a8eeb750..effaa8fd2ee486 100644
--- a/block/partitions/core.c
+++ b/block/partitions/core.c
@@ -524,19 +524,20 @@ int bdev_add_partition(struct block_device *bdev, int partno,
 int bdev_del_partition(struct block_device *bdev, int partno)
 {
 	struct block_device *bdevp;
-	struct hd_struct *part;
-	int ret = 0;
-
-	part = disk_get_part(bdev->bd_disk, partno);
-	if (!part)
-		return -ENXIO;
+	struct hd_struct *part = NULL;
+ 	int ret;
 
-	ret = -ENOMEM;
-	bdevp = bdget(part_devt(part));
+	bdevp = bdget_disk(bdev->bd_disk, partno);
 	if (!bdevp)
-		goto out_put_part;
+		return -ENOMEM;
 
 	mutex_lock(&bdevp->bd_mutex);
+	mutex_lock_nested(&bdev->bd_mutex, 1);
+
+	ret = -ENXIO;
+	part = disk_get_part(bdev->bd_disk, partno);
+	if (!part)
+		goto out_unlock;
 
 	ret = -EBUSY;
 	if (bdevp->bd_openers)
@@ -545,16 +546,14 @@ int bdev_del_partition(struct block_device *bdev, int partno)
 	sync_blockdev(bdevp);
 	invalidate_bdev(bdevp);
 
-	mutex_lock_nested(&bdev->bd_mutex, 1);
 	delete_partition(bdev->bd_disk, part);
-	mutex_unlock(&bdev->bd_mutex);
-
 	ret = 0;
 out_unlock:
+	mutex_unlock(&bdev->bd_mutex);
 	mutex_unlock(&bdevp->bd_mutex);
 	bdput(bdevp);
-out_put_part:
-	disk_put_part(part);
+	if (part)
+		disk_put_part(part);
 	return ret;
 }
 
-- 
2.28.0


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

* Re: [PATCH] block: fix locking in bdev_del_partition
  2020-09-01  9:59 [PATCH] block: fix locking in bdev_del_partition Christoph Hellwig
@ 2020-09-01 14:35 ` Jens Axboe
  0 siblings, 0 replies; 4+ messages in thread
From: Jens Axboe @ 2020-09-01 14:35 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: linux-block, syzbot+6448f3c229bc52b82f69

On 9/1/20 3:59 AM, Christoph Hellwig wrote:
> We need to hold the whole device bd_mutex to protect against
> other thread concurrently deleting out partition before we get
> to it, and thus causing a use after free.

Applied, fixed up an extra space while doing so.

-- 
Jens Axboe


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

* Re: [PATCH] block: fix locking in bdev_del_partition
  2020-09-11 14:56 Dmitriy Gorokh
@ 2020-09-11 14:58 ` Christoph Hellwig
  0 siblings, 0 replies; 4+ messages in thread
From: Christoph Hellwig @ 2020-09-11 14:58 UTC (permalink / raw)
  To: Dmitriy Gorokh; +Cc: Christoph Hellwig, axboe, linux-block

On Fri, Sep 11, 2020 at 05:56:42PM +0300, Dmitriy Gorokh wrote:
> Hi Christoph,
> 
> Unfortunately this fix (Upstream commit
> 08fc1ab6d748ab1a690fd483f41e2938984ce353) breaks compatibility with
> some userspace tools, specifically mdadm, who expect ENXIO errno as a
> result of BLKPG_DEL_PARTITION operation to test if the block device is
> a whole-disk or a partition. With the recent 5.8 kernel it now returns
> ENOMEM on an existing block device which seems inconsistent.

https://git.kernel.dk/cgit/linux-block/commit/?h=block-5.9&id=88ce2a530cc9865a894454b2e40eba5957a60e1a

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

* [PATCH] block: fix locking in bdev_del_partition
@ 2020-09-11 14:56 Dmitriy Gorokh
  2020-09-11 14:58 ` Christoph Hellwig
  0 siblings, 1 reply; 4+ messages in thread
From: Dmitriy Gorokh @ 2020-09-11 14:56 UTC (permalink / raw)
  To: Christoph Hellwig, axboe; +Cc: linux-block

Hi Christoph,

Unfortunately this fix (Upstream commit
08fc1ab6d748ab1a690fd483f41e2938984ce353) breaks compatibility with
some userspace tools, specifically mdadm, who expect ENXIO errno as a
result of BLKPG_DEL_PARTITION operation to test if the block device is
a whole-disk or a partition. With the recent 5.8 kernel it now returns
ENOMEM on an existing block device which seems inconsistent.

Regards,
Dmitriy

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

end of thread, other threads:[~2020-09-11 17:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-01  9:59 [PATCH] block: fix locking in bdev_del_partition Christoph Hellwig
2020-09-01 14:35 ` Jens Axboe
2020-09-11 14:56 Dmitriy Gorokh
2020-09-11 14:58 ` Christoph Hellwig

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.