Skip to contentSkip to conversation

md: add a control device for array management

PATCH In review Abd-Alrhman Masalkhi 3 participants 8 messages Sep 28 – Sep 29

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover md: add a control device for array management

All md management ioctls (SET_ARRAY_INFO, ADD_NEW_DISK, RUN_ARRAY, STOP_ARRAY, ...) are issued on the md block device itself, so the caller must hold the array open while it configures or stops it.

This is a problem for STOP_ARRAY and STOP_ARRAY_RO. Before the array is stopped, the page cache must be flushed, and no other task may have the device open or be writing to it. The issue arises when several tasks share the same file descriptor table, as they count as a single opener. Consequently, one task may still be writing while another task flushes the page cache and stops the array. A write within this window can race with the stop operation.

Add a misc character device, /dev/md-control, with a fixed minor MD_CTRL_MINOR. The control device is not tied to any md device, each request specifies the array in the payload, either by name, by UUID or by device number, without the need to open the md block device.

The new ioctls use the existing MD_MAJOR ioctl type, starting at 0x40, with an MD_ prefix. Every request structure has a flags field. Unknown flags are rejected with -EINVAL.

Each new command matches an existing block device ioctl with only one exception. The STOP_ARRAY_RO has no separate command, it is handled via MD_STOP_ARRAY with the MD_RO_FLAG set.

New 64-bit structures, including mdu_ioctl, are introduced (mdu_array_info64, mdu_disk_info64, mdu_param64, mdu_bitmap_file64, and mdu_version64) to resolve padding and overflow issues in fields such as size, ctime, and utime.

The existing ioctl on the md block device is unchanged, but a warning message will be printed to recommend upgrading mdadm.

The mdadm tool has been updated accordingly, and the mdadm patches will be posted to the linux-raid mailing list shortly.

Abd-Alrhman Masalkhi (2):
md: make mddev lookup and ioctl helpers non-static
md: add a control device for array management

 drivers/md/Makefile            |   2 +-
 drivers/md/md-ctl.c            | 597 +++++++++++++++++++++++++++++++++
 drivers/md/md.c                |  41 ++-
 drivers/md/md.h                |  10 +
 include/linux/miscdevice.h     |   1 +
 include/uapi/linux/raid/md_u.h | 119 +++++++
 6 files changed, 758 insertions(+), 12 deletions(-)
 create mode 100644 drivers/md/md-ctl.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 md: make mddev lookup and ioctl helpers non-static

A following patch adds an md misc control device, so that arrays can be managed without opening the md block device. Its code lives in a separate file and needs to look up arrays and reuse existing ioctl helpers.

---
 drivers/md/md.c | 10 +++++-----
 drivers/md/md.h |  5 +++++
 2 files changed, 10 insertions(+), 5 deletions(-)
drivers/md/md.c +5 -5
index 680b34a63cb3..b5027d65c6d8 100644
@@ -364,8 +364,8 @@ EXPORT_SYMBOL_GPL(md_new_event);
  * Enables to iterate over all existing md arrays
  * all_mddevs_lock protects this list.
  */
-static LIST_HEAD(all_mddevs);
-static DEFINE_SPINLOCK(all_mddevs_lock);
+LIST_HEAD(all_mddevs);
+DEFINE_SPINLOCK(all_mddevs_lock);
 
 static bool is_md_suspended(struct mddev *mddev)
 {
@@ -623,7 +623,7 @@ bool md_flush_request(struct mddev *mddev, struct bio *bio)
 }
 EXPORT_SYMBOL(md_flush_request);
 
-static inline struct mddev *mddev_get(struct mddev *mddev)
+struct mddev *mddev_get(struct mddev *mddev)
 {
 	lockdep_assert_held(&all_mddevs_lock);
 
@@ -8101,7 +8101,7 @@ static void put_cluster_ops(struct mddev *mddev)
  * Any differences that cannot be handled will cause an error.
  * Normally, only one change can be managed at a time.
  */
-static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
+int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
 {
 	int rv = 0;
 	int cnt = 0;
@@ -8227,7 +8227,7 @@ static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
 	return rv;
 }
 
-static int set_disk_faulty(struct mddev *mddev, dev_t dev)
+int set_disk_faulty(struct mddev *mddev, dev_t dev)
 {
 	struct md_rdev *rdev;
 	int err = 0;
drivers/md/md.h +5 -0
index b6d2e8929a0f..cb8760521764 100644
@@ -953,6 +953,7 @@ extern int mddev_init(struct mddev *mddev);
 extern void mddev_destroy(struct mddev *mddev);
 void md_init_stacking_limits(struct queue_limits *lim);
 struct mddev *md_alloc(dev_t dev, char *name);
+extern struct mddev *mddev_get(struct mddev *mddev);
 void mddev_put(struct mddev *mddev);
 extern int md_run(struct mddev *mddev);
 extern int md_start(struct mddev *mddev);
@@ -974,6 +975,10 @@ extern void mddev_destroy_serial_pool(struct mddev *mddev,
 				      struct md_rdev *rdev);
 struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr);
 struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev);
+int update_array_info(struct mddev *mddev, mdu_array_info_t *info);
+
+extern struct list_head all_mddevs;
+extern spinlock_t all_mddevs_lock;
 
 static inline bool is_rdev_broken(struct md_rdev *rdev)
 {
Signature · 1 line
2.43.0
#3 lore raw

PATCH 2/2 md: add a control device for array management

In reply to Abd-Alrhman Masalkhi

All md management ioctls (SET_ARRAY_INFO, ADD_NEW_DISK, RUN_ARRAY, STOP_ARRAY, ...) are issued on the md block device itself, so the caller must hold the array open while it configures or stops it.

This is a problem for STOP_ARRAY and STOP_ARRAY_RO. Before the array is stopped, the page cache must be flushed, and no other task may have the device open or be writing to it. The issue arises when several tasks share the same file descriptor table, as they count as a single opener. Consequently, one task may still be writing while another task flushes the page cache and stops the array. A write within this window can race with the stop operation.

Add a misc character device, /dev/md-control, with a fixed minor MD_CTRL_MINOR. The control device is not tied to any md device, each request specifies the array in the payload, either by name, by UUID or by device number, without the need to open the md block device.

The new ioctls use the existing MD_MAJOR ioctl type, starting at 0x40, with an MD_ prefix. Every request structure has a flags field. Unknown flags are rejected with -EINVAL.

Each new command matches an existing block device ioctl with only one exception. The STOP_ARRAY_RO has no separate command, it is handled via MD_STOP_ARRAY with the MD_RO_FLAG set.

New 64-bit structures, including mdu_ioctl, are introduced (mdu_array_info64, mdu_disk_info64, mdu_param64, mdu_bitmap_file64, and mdu_version64) to resolve padding and overflow issues in fields such as size, ctime, and utime.

The existing ioctl on the md block device is unchanged, but a warning message will be printed to recommend upgrading mdadm.

---
 drivers/md/Makefile            |   2 +-
 drivers/md/md-ctl.c            | 597 +++++++++++++++++++++++++++++++++
 drivers/md/md.c                |  31 +-
 drivers/md/md.h                |   5 +
 include/linux/miscdevice.h     |   1 +
 include/uapi/linux/raid/md_u.h | 119 +++++++
 6 files changed, 748 insertions(+), 7 deletions(-)
 create mode 100644 drivers/md/md-ctl.c
drivers/md/Makefile +1 -1
index 517d1f7d8288..8831df81e34a 100644
@@ -27,7 +27,7 @@ dm-clone-y	+= dm-clone-target.o dm-clone-metadata.o
 dm-verity-y	+= dm-verity-target.o
 dm-zoned-y	+= dm-zoned-target.o dm-zoned-metadata.o dm-zoned-reclaim.o
 
-md-mod-y	+= md.o
+md-mod-y	+= md.o md-ctl.o
 md-mod-$(CONFIG_MD_BITMAP)	+= md-bitmap.o
 md-mod-$(CONFIG_MD_LLBITMAP)	+= md-llbitmap.o
 raid456-y	+= raid5.o raid5-cache.o raid5-ppl.o
drivers/md/md-ctl.c new file +597 -0

597 lines not on this page · raw message

drivers/md/md.c +25 -6
index b5027d65c6d8..86610e20ee39 100644
@@ -8339,8 +8339,8 @@ static int __md_set_array_info(struct mddev *mddev, void __user *argp)
 	return err;
 }
 
-static int md_ioctl(struct block_device *bdev, blk_mode_t mode,
-			unsigned int cmd, unsigned long arg)
+int md_do_ioctl(struct block_device *bdev, blk_mode_t mode, unsigned int cmd,
+		unsigned long arg)
 {
 	int err = 0;
 	unsigned int noio_flags = 0;
@@ -8348,10 +8348,6 @@ static int md_ioctl(struct block_device *bdev, blk_mode_t mode,
 	struct mddev *mddev = NULL;
 	bool suspend;
 
-	err = md_ioctl_valid(cmd);
-	if (err)
-		return err;
-
 	/*
 	 * Commands dealing with the RAID driver but not any
 	 * particular array:
@@ -8541,6 +8537,21 @@ static int md_ioctl(struct block_device *bdev, blk_mode_t mode,
 		clear_bit(MD_CLOSING, &mddev->flags);
 	return err;
 }
+
+static int md_ioctl(struct block_device *bdev, blk_mode_t mode,
+		    unsigned int cmd, unsigned long arg)
+{
+	int err;
+
+	err = md_ioctl_valid(cmd);
+	if (err)
+		return err;
+
+	pr_warn_once("md: ioctl is deprecated and will be removed in future, please upgrade to mdadm-4.6+\n");
+
+	return md_do_ioctl(bdev, mode, cmd, arg);
+}
+
 #ifdef CONFIG_COMPAT
 static int md_compat_ioctl(struct block_device *bdev, blk_mode_t mode,
 		    unsigned int cmd, unsigned long arg)
@@ -10774,12 +10785,20 @@ static int __init md_init(void)
 		goto err_mdp;
 	mdp_major = ret;
 
+	ret = md_ctl_init();
+	if (ret < 0)
+		goto err_ctl;
+
 	register_reboot_notifier(&md_notifier);
 	raid_table_header = register_sysctl("dev/raid", raid_table);
 
 	md_geninit();
+
 	return 0;
 
+err_ctl:
+	unregister_blkdev(mdp_major, "mdb");
+	mdp_major = 0;
 err_mdp:
 	unregister_blkdev(MD_MAJOR, "md");
 err_md:
drivers/md/md.h +5 -0
index cb8760521764..2c0e53edd6f1 100644
@@ -975,7 +975,12 @@ extern void mddev_destroy_serial_pool(struct mddev *mddev,
 				      struct md_rdev *rdev);
 struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr);
 struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev);
+int set_disk_faulty(struct mddev *mddev, dev_t dev);
 int update_array_info(struct mddev *mddev, mdu_array_info_t *info);
+int md_ctl_init(void);
+void md_ctl_exit(void);
+int md_do_ioctl(struct block_device *bdev, blk_mode_t mode, unsigned int cmd,
+		unsigned long arg);
 
 extern struct list_head all_mddevs;
 extern spinlock_t all_mddevs_lock;
include/linux/miscdevice.h +1 -0
index fa9000f68523..0bf2870dc191 100644
@@ -71,6 +71,7 @@
 #define VHOST_VSOCK_MINOR	241
 #define EISA_EEPROM_MINOR	241
 #define RFKILL_MINOR		242
+#define MD_CTRL_MINOR		243
 
 /*
  * Misc char device minor code space division related to below macro:
include/uapi/linux/raid/md_u.h +119 -0

141 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw
Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider: - [High] md: MD_STOP_ARRAY mutual exclusion bypass via control device - [High] md: md_ctl_exit is marked…

Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [High] md: MD_STOP_ARRAY mutual exclusion bypass via control device
- [High] md: md_ctl_exit is marked __init and omitted from md_exit()
- [High] md: self-deadlock in get_mddev_dev and get_mddev_uuid via mddev_put
- [Medium] md: binary UUID lookup fails for UUIDs starting with 0x00
- [Medium] md: missing null-termination enforcement allows stack OOB read
- [Low] md: direct cast from __u64 to user pointer causes warnings
- [Low] md: uninitialized pointer passed to get_raid_version()
- [Low] md: unknown ioctl flags return -ENOTTY instead of -EINVAL
- [Low] md: unprivileged un-ratelimited pr_err allows log flooding
--

commit dd168c18138734c235c0b8b1c097fa7a18b6baa8
Author: Abd-Alrhman Masalkhi <[email protected]>
Subject: md: add a control device for array management

This patch introduces a misc character device, /dev/md-control, to allow MD management ioctls without holding a block device reference. New 64-bit safe ioctls are provided that specify the target array by name, UUID, or device number.

Quoted 3 lines

The new ioctls use the existing MD_MAJOR ioctl type, starting at 0x40, with an MD_ prefix. Every request structure has a flags field. Unknown flags are rejected with -EINVAL.

[Severity: Low]
The implementation of ctl_flags_valid() below actually returns -ENOTTY for unknown flags. Should the implementation be updated to match this documented behavior?

drivers/md/md-ctl.c new file +0 -0
new file mode 100644
index 0000000000000..84923b0a2fa3c
@@ -0,0 +1,597 @@

[ ... ]
+static struct mddev *get_mddev_dev(dev_t dev)
+{
+ struct mddev *mddev, *ret = NULL;
+
+ if (MINOR(dev) >= (1 << MINORBITS))
+ return ERR_PTR(-EINVAL);
+
+ if (MAJOR(dev) != MD_MAJOR)
+ dev &= ~((1 << MdpMinorShift) - 1);
+
+ spin_lock(&all_mddevs_lock);
+ list_for_each_entry(mddev, &all_mddevs, all_mddevs) {
+ if (mddev->unit == dev) {
+ ret = mddev_get(mddev);
+ if (ret && !mddev->gendisk) {
+ mddev_put(mddev);
[Severity: High]
Does calling mddev_put() while holding all_mddevs_lock cause a self-deadlock?

If the reference count was 0 before mddev_get(), mddev_put() will drop it back to 0 and internally call atomic_dec_and_lock(&mddev->active, &all_mddevs_lock). Since the current CPU already holds the non-recursive all_mddevs_lock, this will deadlock the system. This pattern also appears in get_mddev_uuid().

+ ret = ERR_PTR(-EBUSY);
+ }
+ break;
+ }
+ }
+ spin_unlock(&all_mddevs_lock);
+
+ return ret ? ret : ERR_PTR(-ENODEV);
+}
+
+static struct mddev *get_mddev_name(const char *name) +{
+ unsigned long minor;
+ struct mddev *mddev, *ret = NULL;
+
+ if (strncmp(name, "md", 2))
+ return ERR_PTR(-EINVAL);
+
+ if (name[2] != '_' && (!isdigit(name[2]) ||
+ kstrtoul(&name[2], 10, &minor) ||
[Severity: Medium]
Can this result in an out-of-bounds read on the kernel stack?

The name field is populated via copy_from_user() on the md_ctl structure without enforcing null-termination. If a user populates the entire 32-byte name field without a null terminator, kstrtoul() will read past the array bounds into adjacent stack memory until it finds a non-digit character.

+ minor > MINORMASK))
+ return ERR_PTR(-EINVAL);
[ ... ]
+static struct mddev *ctl_find_mddev(struct mdu_ioctl *md_ctl) +{
+ struct mddev *ret = ERR_PTR(-EINVAL);
+
+ if (*md_ctl->uuid) {
[Severity: Medium]
Does this correctly evaluate the binary UUID?

Checking *md_ctl->uuid only evaluates the first byte of the 16-byte array. If an array is created with a randomly generated UUID that happens to start with 0x00, the UUID lookup will be skipped and fallback to checking the name or device number instead.

+ if (*md_ctl->name || md_ctl->dev) {
+ pr_err("md: only supply one of name, uuid or dev number\n");
[Severity: Low]
Could this allow an unprivileged user to flood the kernel log buffer?

Since this is triggered directly by user input via ioctl, using an un-ratelimited pr_err() here instead of pr_err_ratelimited() or pr_debug() allows dmesg flooding.

+ return ret;
+ }
[ ... ]
+static long md_ctl_ioctl(struct file *filp, uint cmd, ulong u) +{
+ int err;
+ unsigned int old_cmd;
+ unsigned int noio_flags = 0;
+ struct mddev *mddev;
+ void __user *arg;
+ struct mdu_ioctl md_ctl;
+
+ err = ctl_ioctl_valid(cmd);
+ if (err)
+ return err;
+
+ if (cmd == MD_RAID_VERSION)
+ return get_raid_version(mddev, (struct mdu_version64 __user *)u);
[Severity: Low]
Does this pass an uninitialized mddev pointer to get_raid_version()?

mddev is evaluated and passed in a register before it is initialized by ctl_find_mddev(), resulting in undefined behavior.

+
+ if (copy_from_user(&md_ctl, (void __user *)u, sizeof(md_ctl)))
+ return -EFAULT;
+
+ err = ctl_flags_valid(cmd, md_ctl.flags);
+ if (err)
+ return err;
+
+ mddev = ctl_find_mddev(&md_ctl);
+ if (IS_ERR(mddev))
+ return PTR_ERR(mddev);
+
+ old_cmd = try_convert_ioctl_cmd(cmd, md_ctl.flags);
+ if (old_cmd) {
+ err = md_do_ioctl(mddev->gendisk->part0, 0, old_cmd, md_ctl.arg);
[Severity: High]
Does delegating to md_do_ioctl() bypass the MD_STOP_ARRAY mutual exclusion?

When MD_STOP_ARRAY is called here via the control device, md_do_ioctl() calls mddev_set_closing_and_sync_blockdev(mddev, 1), which assumes the caller holds the block device open. Because the control ioctl caller doesn't hold the block device, atomic_read(&mddev->openers) > 1 will evaluate to false if exactly one other task has the array open, allowing the array to be stopped while it is actively in use.

+ goto out;
+ }
+
+ arg = (void __user *)md_ctl.arg;
[Severity: Low]
Does this direct cast from __u64 to a pointer cause compiler warnings on 32-bit architectures?

It is recommended to use u64_to_user_ptr() to handle the pointer translation correctly for compat userspace.

+ switch (cmd) {
[ ... ]
+void __init md_ctl_exit(void)
+{
+ misc_deregister(&md_ctl_misc);
+}
[Severity: High]
Is md_ctl_exit() marked __init by mistake?

Additionally, it appears this function is completely omitted from the module unload path (md_exit() in md.c doesn't call it). This would leak the misc device when the md module is unloaded, and any subsequent access to /dev/md-control would cause a use-after-free panic.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#2 lore raw

Re: [PATCH 1/2] md: make mddev lookup and ioctl helpers non-static

In reply to Abd-Alrhman Masalkhi
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] md: generic update_array_info() and set_disk_faulty() lack subsystem prefix --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] md: generic update_array_info() and set_disk_faulty() lack subsystem prefix --

commit 471cd6c3e9c36553c5976e00c65220dac80b2aea
Author: Abd-Alrhman Masalkhi <[email protected]>

md: make mddev lookup and ioctl helpers non-static

This commit exposes several internal md array lookup and ioctl helper functions by removing their static declarations. This change is intended to allow a new md misc control device to reuse these helpers without opening the md block device.

Quoted 4 lines
drivers/md/md.c +0 -0
index 680b34a63cb3d..b5027d65c6d8d 100644

[ ... ]

drivers/md/md.c static void put_cluster_ops(struct mddev *mddev) · line 8104

Quoted 6 lines
drivers/md/md.c +1 -1
@@ -8101,7 +8101,7 @@ static void put_cluster_ops(struct mddev *mddev)
  * Any differences that cannot be handled will cause an error.
  * Normally, only one change can be managed at a time.
  */
-static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
+int update_array_info(struct mddev *mddev, mdu_array_info_t *info)

[Severity: Low]
By removing the static keyword, update_array_info() is now added to the global kernel namespace. Since this name is quite generic, should it be given an "md_" prefix to avoid potential symbol collisions with other subsystems?

Quoted 3 lines
drivers/md/md.c +0 -0
 {
 	int rv = 0;
 	int cnt = 0;

[ ... ]

drivers/md/md.c static int update_array_info(struct mddev *mddev, mdu_array_info_t *info) · line 8230

Quoted 6 lines
drivers/md/md.c +1 -1
@@ -8227,7 +8227,7 @@ static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
 	return rv;
 }
 
-static int set_disk_faulty(struct mddev *mddev, dev_t dev)
+int set_disk_faulty(struct mddev *mddev, dev_t dev)

[Severity: Low]
Similar to update_array_info(), set_disk_faulty() is now globally visible. Would it be safer to rename this to md_set_disk_faulty() to prevent namespace pollution and potential collisions?

Quoted 3 lines
drivers/md/md.c +0 -0
 {
 	struct md_rdev *rdev;
 	int err = 0;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#6 lore raw
In reply to Abd-Alrhman Masalkhi
Quoted 4 lines · Abd-Alrhman Masalkhi

Add a misc character device, /dev/md-control, with a fixed minor MD_CTRL_MINOR. The control device is not tied to any md device, each request specifies the array in the payload, either by name, by UUID or by device number, without the need to open the md block device.

Why is this a fixed number and not a dynamic one? There should not eve be a need for fixed numbers anymore.

drivers/md/md-ctl.c line 2

Quoted 5 lines
drivers/md/md-ctl.c new file +2 -0
@@ -0,0 +1,597 @@
+// SPDX-License-Identifier: GPL-2.0
+

No copyright information?

include/linux/miscdevice.h line 74

Quoted 9 lines
include/linux/miscdevice.h +1 -0
index fa9000f68523..0bf2870dc191 100644
@@ -71,6 +71,7 @@
 #define VHOST_VSOCK_MINOR	241
 #define EISA_EEPROM_MINOR	241
 #define RFKILL_MINOR		242
+#define MD_CTRL_MINOR		243

No, please use a dynamic number instead.

thanks,

greg k-h

#7 lore raw

Hi Greg,

Thank you for your review.

Quoted 9 lines · Greg KH
Quoted 4 lines · Abd-Alrhman Masalkhi

Add a misc character device, /dev/md-control, with a fixed minor MD_CTRL_MINOR. The control device is not tied to any md device, each request specifies the array in the payload, either by name, by UUID or by device number, without the need to open the md block device.

Why is this a fixed number and not a dynamic one? There should not eve be a need for fixed numbers anymore.

Quoted 8 lines

drivers/md/md-ctl.c line 2

Quoted 5 lines
drivers/md/md-ctl.c new file +2 -0
@@ -0,0 +1,597 @@
+// SPDX-License-Identifier: GPL-2.0
+

No copyright information?

I will add it in v2.

Quoted 12 lines

include/linux/miscdevice.h line 74

Quoted 9 lines
include/linux/miscdevice.h +1 -0
index fa9000f68523..0bf2870dc191 100644
@@ -71,6 +71,7 @@
 #define VHOST_VSOCK_MINOR	241
 #define EISA_EEPROM_MINOR	241
 #define RFKILL_MINOR		242
+#define MD_CTRL_MINOR		243

No, please use a dynamic number instead.

I will use dynamic number in v2.

Quoted 3 lines

thanks,

greg k-h

Signature · 2 lines
Best Regards,
Abd-Alrhman
#5 lore raw
In reply to [email protected]

Hi Kuai,

Sorry, I forgot to mark this series as RFC.

Quoted 21 lines · On Mon, Sep 28, 2026 at 20:26 +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [High] md: MD_STOP_ARRAY mutual exclusion bypass via control device
- [High] md: md_ctl_exit is marked __init and omitted from md_exit()
- [High] md: self-deadlock in get_mddev_dev and get_mddev_uuid via mddev_put
- [Medium] md: binary UUID lookup fails for UUIDs starting with 0x00
- [Medium] md: missing null-termination enforcement allows stack OOB read
- [Low] md: direct cast from __u64 to user pointer causes warnings
- [Low] md: uninitialized pointer passed to get_raid_version()
- [Low] md: unknown ioctl flags return -ENOTTY instead of -EINVAL
- [Low] md: unprivileged un-ratelimited pr_err allows log flooding
--

commit dd168c18138734c235c0b8b1c097fa7a18b6baa8
Author: Abd-Alrhman Masalkhi <[email protected]>
Subject: md: add a control device for array management

This patch introduces a misc character device, /dev/md-control, to allow MD management ioctls without holding a block device reference. New 64-bit safe ioctls are provided that specify the target array by name, UUID, or device number.

Quoted 9 lines
Quoted 3 lines

The new ioctls use the existing MD_MAJOR ioctl type, starting at 0x40, with an MD_ prefix. Every request structure has a flags field. Unknown flags are rejected with -EINVAL.

[Severity: Low]
The implementation of ctl_flags_valid() below actually returns -ENOTTY for unknown flags. Should the implementation be updated to match this documented behavior?

I will fix this in v2.

Quoted 23 lines
drivers/md/md-ctl.c new file +16 -0
new file mode 100644
index 0000000000000..84923b0a2fa3c
@@ -0,0 +1,597 @@
[ ... ]
+static struct mddev *get_mddev_dev(dev_t dev)
+{
+	struct mddev *mddev, *ret = NULL;
+
+	if (MINOR(dev) >= (1 << MINORBITS))
+		return ERR_PTR(-EINVAL);
+
+	if (MAJOR(dev) != MD_MAJOR)
+		dev &= ~((1 << MdpMinorShift) - 1);
+
+	spin_lock(&all_mddevs_lock);
+	list_for_each_entry(mddev, &all_mddevs, all_mddevs) {
+		if (mddev->unit == dev) {
+			ret = mddev_get(mddev);
+			if (ret && !mddev->gendisk) {
+				mddev_put(mddev);
Quoted 3 lines

[Severity: High]
Does calling mddev_put() while holding all_mddevs_lock cause a self-deadlock?

I will fix this in v2.

Quoted 29 lines

If the reference count was 0 before mddev_get(), mddev_put() will drop it back to 0 and internally call atomic_dec_and_lock(&mddev->active, &all_mddevs_lock). Since the current CPU already holds the non-recursive all_mddevs_lock, this will deadlock the system. This pattern also appears in get_mddev_uuid().

drivers/md/md-ctl.c new file +20 -0
+				ret = ERR_PTR(-EBUSY);
+			}
+			break;
+		}
+	}
+	spin_unlock(&all_mddevs_lock);
+
+	return ret ? ret : ERR_PTR(-ENODEV);
+}
+
+static struct mddev *get_mddev_name(const char *name)
+{
+	unsigned long minor;
+	struct mddev *mddev, *ret = NULL;
+
+	if (strncmp(name, "md", 2))
+		return ERR_PTR(-EINVAL);
+
+	if (name[2] != '_' && (!isdigit(name[2]) ||
+			       kstrtoul(&name[2], 10, &minor) ||

[Severity: Medium]
Can this result in an out-of-bounds read on the kernel stack?

Quoted 5 lines

The name field is populated via copy_from_user() on the md_ctl structure without enforcing null-termination. If a user populates the entire 32-byte name field without a null terminator, kstrtoul() will read past the array bounds into adjacent stack memory until it finds a non-digit character.

I will fix this in v2.

Quoted 11 lines
drivers/md/md-ctl.c new file +7 -0
+			       minor > MINORMASK))
+		return ERR_PTR(-EINVAL);
[ ... ]
+static struct mddev *ctl_find_mddev(struct mdu_ioctl *md_ctl)
+{
+	struct mddev *ret = ERR_PTR(-EINVAL);
+
+	if (*md_ctl->uuid) {

[Severity: Medium]
Does this correctly evaluate the binary UUID?

Quoted 5 lines

Checking *md_ctl->uuid only evaluates the first byte of the 16-byte array. If an array is created with a randomly generated UUID that happens to start with 0x00, the UUID lookup will be skipped and fallback to checking the name or device number instead.

I will fix this in v2.

Quoted 9 lines
drivers/md/md-ctl.c new file +2 -0
+		if (*md_ctl->name || md_ctl->dev) {
+			pr_err("md: only supply one of name, uuid or dev number\n");

[Severity: Low]
Could this allow an unprivileged user to flood the kernel log buffer?

Since this is triggered directly by user input via ioctl, using an un-ratelimited pr_err() here instead of pr_err_ratelimited() or pr_debug() allows dmesg flooding.

I will use pr_err_ratelimited() in v2.

Quoted 18 lines
drivers/md/md-ctl.c new file +17 -0
+			return ret;
+		}
[ ... ]
+static long md_ctl_ioctl(struct file *filp, uint cmd, ulong u)
+{
+	int err;
+	unsigned int old_cmd;
+	unsigned int noio_flags = 0;
+	struct mddev *mddev;
+	void __user *arg;
+	struct mdu_ioctl md_ctl;
+
+	err = ctl_ioctl_valid(cmd);
+	if (err)
+		return err;
+
+	if (cmd == MD_RAID_VERSION)
+		return get_raid_version(mddev, (struct mdu_version64 __user *)u);
Quoted 3 lines

[Severity: Low]
Does this pass an uninitialized mddev pointer to get_raid_version()?

get_raid_version() should not take mddev as a parameter; I will fix this in v2.

Quoted 21 lines

mddev is evaluated and passed in a register before it is initialized by ctl_find_mddev(), resulting in undefined behavior.

drivers/md/md-ctl.c new file +15 -0
+
+	if (copy_from_user(&md_ctl, (void __user *)u, sizeof(md_ctl)))
+		return -EFAULT;
+
+	err = ctl_flags_valid(cmd, md_ctl.flags);
+	if (err)
+		return err;
+
+	mddev = ctl_find_mddev(&md_ctl);
+	if (IS_ERR(mddev))
+		return PTR_ERR(mddev);
+
+	old_cmd = try_convert_ioctl_cmd(cmd, md_ctl.flags);
+	if (old_cmd) {
+		err = md_do_ioctl(mddev->gendisk->part0, 0, old_cmd, md_ctl.arg);

[Severity: High]
Does delegating to md_do_ioctl() bypass the MD_STOP_ARRAY mutual exclusion?

Quoted 7 lines

When MD_STOP_ARRAY is called here via the control device, md_do_ioctl() calls mddev_set_closing_and_sync_blockdev(mddev, 1), which assumes the caller holds the block device open. Because the control ioctl caller doesn't hold the block device, atomic_read(&mddev->openers) > 1 will evaluate to false if exactly one other task has the array open, allowing the array to be stopped while it is actively in use.

Yes that might happen. I will fix this in v2.

Quoted 11 lines
drivers/md/md-ctl.c new file +4 -0
+		goto out;
+	}
+
+	arg = (void __user *)md_ctl.arg;

[Severity: Low]
Does this direct cast from __u64 to a pointer cause compiler warnings on 32-bit architectures?

It is recommended to use u64_to_user_ptr() to handle the pointer translation correctly for compat userspace.

I will fix this in v2.

Quoted 9 lines
drivers/md/md-ctl.c new file +5 -0
+	switch (cmd) {
[ ... ]
+void __init md_ctl_exit(void)
+{
+	misc_deregister(&md_ctl_misc);
+}

[Severity: High]
Is md_ctl_exit() marked __init by mistake?

Quoted 5 lines

Additionally, it appears this function is completely omitted from the module unload path (md_exit() in md.c doesn't call it). This would leak the misc device when the md module is unloaded, and any subsequent access to /dev/md-control would cause a use-after-free panic.

I will fix this in v2.

Quoted 2 lines
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Signature · 2 lines
Best Regards,
Abd-Alrhman