public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	patches@lists.linux.dev, Shiwei Cui <cuishw@inspur.com>,
	Christoph Hellwig <hch@lst.de>, Jan Kara <jack@suse.cz>,
	Ming Lei <ming.lei@redhat.com>
Subject: [PATCH 5.4 4/9] block: unhash blkdev part inode when the part is deleted
Date: Thu, 15 Dec 2022 19:10:31 +0100	[thread overview]
Message-ID: <20221215172905.628016513@linuxfoundation.org> (raw)
In-Reply-To: <20221215172905.468656378@linuxfoundation.org>

From: Ming Lei <ming.lei@redhat.com>

v5.11 changes the blkdev lookup mechanism completely since commit
22ae8ce8b892 ("block: simplify bdev/disk lookup in blkdev_get"),
and small part of the change is to unhash part bdev inode when
deleting partition. Turns out this kind of change does fix one
nasty issue in case of BLOCK_EXT_MAJOR:

1) when one partition is deleted & closed, disk_put_part() is always
called before bdput(bdev), see blkdev_put(); so the part's devt can
be freed & re-used before the inode is dropped

2) then new partition with same devt can be created just before the
inode in 1) is dropped, then the old inode/bdev structurein 1) is
re-used for this new partition, this way causes use-after-free and
kernel panic.

It isn't possible to backport the whole big patchset of "merge struct
block_device and struct hd_struct v4" for addressing this issue.

https://lore.kernel.org/linux-block/20201128161510.347752-1-hch@lst.de/

So fixes it by unhashing part bdev in delete_partition(), and this way
is actually aligned with v5.11+'s behavior.

Backported from the following 5.10.y commit:

5f2f77560591 ("block: unhash blkdev part inode when the part is deleted")

Reported-by: Shiwei Cui <cuishw@inspur.com>
Tested-by: Shiwei Cui <cuishw@inspur.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 block/partition-generic.c |    7 +++++++
 1 file changed, 7 insertions(+)

--- a/block/partition-generic.c
+++ b/block/partition-generic.c
@@ -272,6 +272,7 @@ void delete_partition(struct gendisk *di
 	struct disk_part_tbl *ptbl =
 		rcu_dereference_protected(disk->part_tbl, 1);
 	struct hd_struct *part;
+	struct block_device *bdev;
 
 	if (partno >= ptbl->len)
 		return;
@@ -292,6 +293,12 @@ void delete_partition(struct gendisk *di
 	 * "in-use" until we really free the gendisk.
 	 */
 	blk_invalidate_devt(part_devt(part));
+
+	bdev = bdget(part_devt(part));
+	if (bdev) {
+		remove_inode_hash(bdev->bd_inode);
+		bdput(bdev);
+	}
 	hd_struct_kill(part);
 }
 



  parent reply	other threads:[~2022-12-15 18:11 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-15 18:10 [PATCH 5.4 0/9] 5.4.228-rc1 review Greg Kroah-Hartman
2022-12-15 18:10 ` [PATCH 5.4 1/9] net: bpf: Allow TC programs to call BPF_FUNC_skb_change_head Greg Kroah-Hartman
2022-12-15 18:10 ` [PATCH 5.4 2/9] x86/smpboot: Move rcu_cpu_starting() earlier Greg Kroah-Hartman
2022-12-15 18:10 ` [PATCH 5.4 3/9] mm/hugetlb: fix races when looking up a CONT-PTE/PMD size hugetlb page Greg Kroah-Hartman
2022-12-15 18:10 ` Greg Kroah-Hartman [this message]
2022-12-15 18:10 ` [PATCH 5.4 5/9] nfp: fix use-after-free in area_cache_get() Greg Kroah-Hartman
2022-12-15 18:10 ` [PATCH 5.4 6/9] ASoC: ops: Check bounds for second channel in snd_soc_put_volsw_sx() Greg Kroah-Hartman
2022-12-15 18:10 ` [PATCH 5.4 7/9] pinctrl: meditatek: Startup with the IRQs disabled Greg Kroah-Hartman
2022-12-15 18:10 ` [PATCH 5.4 8/9] can: sja1000: fix size of OCR_MODE_MASK define Greg Kroah-Hartman
2022-12-15 18:10 ` [PATCH 5.4 9/9] can: mcba_usb: Fix termination command argument Greg Kroah-Hartman
2022-12-15 23:51 ` [PATCH 5.4 0/9] 5.4.228-rc1 review Shuah Khan
2022-12-16 11:29 ` Naresh Kamboju
2022-12-16 13:11 ` Sudip Mukherjee (Codethink)
2022-12-16 19:06 ` Florian Fainelli
2022-12-16 21:30 ` Guenter Roeck
2022-12-18  7:08 ` zhouzhixiu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221215172905.628016513@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=cuishw@inspur.com \
    --cc=hch@lst.de \
    --cc=jack@suse.cz \
    --cc=ming.lei@redhat.com \
    --cc=patches@lists.linux.dev \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox