[PATCH v2 06/11] mtd: ubi: block: only claim UBI block devices in the partition scan

Daniel Golle <[email protected]>
Newsgroups org.u-boot-project.lists.u-boot
Message-ID <95ba335535b0f5585e582cc92d1427e30e57c308.1787673209.git.daniel@makrotopia.org>
part_test_ubi() reads the first block and returns success for any
readable device, so a bare block device with no partition table (for
example a sandbox "host bind" image) is mis-detected as UBI once
CONFIG_UBI_BLOCK is enabled. part_get_info_ubi() then calls
ubi_get_volume_by_index(), which dereferences ubi_devices[0] without a
NULL check and crashes when no UBI device is attached.

Fail part_test_ubi() when no UBI device is attached, and guard the volume
lookup and print helpers against a NULL ubi_device, so the driver only
claims devices while UBI is present and never dereferences a missing one.

Fixes: aa5b67ce226 ("disk: support UBI partitions")
Signed-off-by: Daniel Golle <[email protected]>
---
 drivers/mtd/ubi/part.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/drivers/mtd/ubi/part.c b/drivers/mtd/ubi/part.c
index 6c017eb7299..e1bdd9e3008 100644
--- a/drivers/mtd/ubi/part.c
+++ b/drivers/mtd/ubi/part.c
@@ -19,6 +19,9 @@ static struct ubi_volume *ubi_get_volume_by_index(int vol_id)
 	struct ubi_device *ubi = get_ubi_device();
 	int i;
 
+	if (!ubi)
+		return NULL;
+
 	for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
 		struct ubi_volume *volume = ubi->volumes[i];
 
@@ -66,6 +69,9 @@ static void __maybe_unused part_print_ubi(struct blk_desc *dev_desc)
 	struct ubi_device *ubi = get_ubi_device();
 	int i;
 
+	if (!ubi)
+		return;
+
 	for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
 		struct ubi_volume *volume = ubi->volumes[i];
 
@@ -83,6 +89,9 @@ static int part_test_ubi(struct blk_desc *dev_desc)
 {
 	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
 
+	if (!get_ubi_device())
+		return -1;
+
 	if (blk_dread(dev_desc, 0, 1, (ulong *)buffer) != 1)
 		return -1;
 
-- 
2.55.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.