Skip to contentSkip to conversation

dm: fix use-after-free when dm_get_device() widens the open mode

PATCH New Mikulas Patocka 2 participants 2 messages Nov 6 Device mapper

lore
Share & tools
lore

Mikulas Patocka

author maintainer
#0 lore raw

PATCH 1/1 dm: fix use-after-free when dm_get_device() widens the open mode

From: Ming-Hung Tsai <[email protected]>

There's an UAF bug in dm_get_device(): when a table construction calls dm_get_device() on the same device twice with widen access mode (e.g., read-only first then read-write), the second dm_get_device() calls upgrade_mode(), which reopens the device in upgraded mode, making previously returned dm_dev* a stale pointer.

Fixing approaches:
- Add per-mode views (dm_dev_ref) from a per-device dm_dev_internal to multiple table_devices.
- Keep reusing the dm_dev_ref when the requested mode is included, i.e., a read-only reference can reuse the read-write opened device within a table.
- Maintain a canonical block_device pointer in dm_dev_internal for handling the DM_TABLE_DEPS ioctl.

Also tweak the way of mode sharing check in find_ref() to "(x & mask) == mask". The result stays the same to the previous upgrade_mode() guard "(x | mask) == x".

Pros: keep the relationship between dm_dev_internal and table_device:
one dm_dev_internal per device, regardless the mode, so the output of DMM_TABLE_DEPS ioctl unchanged.

Cons: extra dm_dev_ref allocation.

Note that now the table_device's count not necessarily mean the number tables referecing this <dev, mode> pair, since we're allowing multiple references within a table.

---
 drivers/md/dm-ioctl.c |   2 +-
 drivers/md/dm-table.c | 142 +++++++++++++++++++++++-------------------
 drivers/md/dm.c       |   2 +-
 drivers/md/dm.h       |  18 +++++-
 4 files changed, 98 insertions(+), 66 deletions(-)
drivers/md/dm-ioctl.c +1 -1
index 8f945f0aeda5..4a7cb0926c17 100644
@@ -1794,7 +1794,7 @@ static void retrieve_deps(struct dm_table *table,
 	deps->count = count;
 	count = 0;
 	list_for_each_entry(dd, dm_table_get_devices(table), list)
-		deps->dev[count++] = huge_encode_dev(dd->dm_dev->bdev->bd_dev);
+		deps->dev[count++] = huge_encode_dev(dd->bdev->bd_dev);
 
 	param->data_size = param->data_start + needed;
 }
drivers/md/dm-table.c +79 -63

202 lines not on this page · raw message

drivers/md/dm.c +1 -1
index eb9e32995b79..1fff84b3f869 100644
@@ -1588,7 +1588,7 @@ static void __send_empty_flush(struct clone_info *ci)
 			 */
 			clone = alloc_tio(ci, NULL, 0, &len, GFP_NOIO);
 			atomic_add(1, &ci->io->io_count);
-			bio_set_dev(clone, dd->dm_dev->bdev);
+			bio_set_dev(clone, dd->bdev);
 			clone->bi_end_io = clone_endio;
 			dm_submit_bio_remap(clone, NULL);
 		}
drivers/md/dm.h +17 -1
index 7a795979ec72..34f58060243f 100644
@@ -37,10 +37,26 @@
 
 /*
  * List of devices that a metadevice uses and should open/close.
+ * There is one dm_dev_internal per device. It holds one dm_dev_ref
+ * for each mode that the targets opened the device with.
  */
 struct dm_dev_internal {
 	struct list_head list;
-	refcount_t count;
+	struct block_device *bdev;
+	struct list_head refs;
+};
+
+/*
+ * One open of a device with one mode, shared by the targets of a table.
+ * dm_get_device() reuses a dm_dev_ref whose mode covers the requested
+ * mode, and adds a new one only when none does. So the dm_dev_refs of
+ * a device have different modes, and a table holds each table_device
+ * at most once. table_device->count is still the number of tables that
+ * use the table_device.
+ */
+struct dm_dev_ref {
+	struct list_head list;
+	refcount_t count;	/* dm_get_device() calls that returned dm_dev */
 	struct dm_dev *dm_dev;
 };
 
Signature · 1 line
2.49.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] dm: missing Fixes trailer for commit 86f1152b117a in commit message - [Low] dm: commit message claim…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] dm: missing Fixes trailer for commit 86f1152b117a in commit message
- [Low] dm: commit message claim about table_device->count contradicts code
--

commit 16907af74a028c1489b22d6e8c7c48ae64949725
Author: Ming-Hung Tsai <[email protected]>

Quoted 25 lines

dm: fix use-after-free when dm_get_device() widens the open mode

There's an UAF bug in dm_get_device(): when a table construction calls dm_get_device() on the same device twice with widen access mode (e.g., read-only first then read-write), the second dm_get_device() calls upgrade_mode(), which reopens the device in upgraded mode, making previously returned dm_dev* a stale pointer.

Fixing approaches:
- Add per-mode views (dm_dev_ref) from a per-device dm_dev_internal to multiple table_devices.
- Keep reusing the dm_dev_ref when the requested mode is included, i.e., a read-only reference can reuse the read-write opened device within a table.
- Maintain a canonical block_device pointer in dm_dev_internal for handling the DM_TABLE_DEPS ioctl.

Also tweak the way of mode sharing check in find_ref() to "(x & mask) == mask". The result stays the same to the previous upgrade_mode() guard "(x | mask) == x".

Pros: keep the relationship between dm_dev_internal and table_device:
one dm_dev_internal per device, regardless the mode, so the output of DMM_TABLE_DEPS ioctl unchanged.

Quoted 5 lines

Cons: extra dm_dev_ref allocation.

Note that now the table_device's count not necessarily mean the number tables referecing this <dev, mode> pair, since we're allowing multiple references within a table.

[Severity: Low]
Is the closing note about table_device->count accurate? In dm_get_device(), find_ref() reuses an existing dm_dev_ref whenever its mode covers the requested mode, so a table still holds at most one dm_dev_ref (and one reference to the table_device) for any given <dev, mode> pair, as the comment on struct dm_dev_ref in drivers/md/dm.h notes.

Quoted 5 lines

[Severity: Low]
Since 86f1152b117a ("dm: allow active and inactive tables to share dm_devs") changed upgrade_mode() to replace dd->dm_dev and drop the old table_device reference while earlier targets in the table still held it, should this include:

Fixes 86f1152b117a ("dm: allow active and inactive tables to share dm_devs")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1