Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "John Garry" <john.g.garry@oracle.com>
Cc: linux-scsi@vger.kernel.org
Subject: Re: [PATCH v3 07/13] libmultipath: Add cdev support
Date: Fri, 03 Jul 2026 10:48:31 +0000	[thread overview]
Message-ID: <20260703104832.5166A1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260703102918.3723667-8-john.g.garry@oracle.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] Dropping the SRCU read lock during ioctl execution leaves the `mpath_device` pointer unprotected, causing a use-after-free when passed to `cdev_ioctl` or accessed.
- [High] Decoupled lifetime between the embedded `cdev` and `mpath_head` reference counting leads to Use-After-Free during VFS file open and release.
--

commit 9a6527083c29f0b9d58867aa52ca2ea700cf1168
Author: John Garry <john.g.garry@oracle.com>

libmultipath: Add cdev support

Add support to create a cdev multipath device. The functionality is much
the same as NVMe, where the cdev is created when a mpath device is set
live.

> diff --git a/lib/multipath.c b/lib/multipath.c
> index 78f88b0664c78..6d2e1186a10f8 100644
> --- a/lib/multipath.c
> +++ b/lib/multipath.c

[ ... ]

> +static int mpath_chr_open(struct inode *inode, struct file *file)
> +{
> +	struct cdev *cdev = file_inode(file)->i_cdev;
> +	struct mpath_head *mpath_head =
> +			container_of(cdev, struct mpath_head, cdev);
> +
> +	return mpath_get_head(mpath_head);
> +}

[Severity: High]
Is there a race condition with driver teardown here? 

If teardown occurs after the VFS has looked up the cdev but before this
function calls mpath_get_head() to increment the refcount, mpath_head could
be freed, potentially causing a use-after-free when this accesses the freed
memory.

> +
> +static int mpath_chr_release(struct inode *inode, struct file *file)
> +{
> +	struct cdev *cdev = file_inode(file)->i_cdev;
> +	struct mpath_head *mpath_head =
> +			container_of(cdev, struct mpath_head, cdev);
> +
> +	mpath_put_head(mpath_head);
> +	return 0;
> +}

[Severity: High]
Does this sequence cause the embedded cdev to be freed prematurely? 

When the last file descriptor is closed, VFS __fput() calls
mpath_chr_release(). If mpath_put_head() drops the mpath_head refcount to 0,
the mpath_head structure (and the embedded cdev) is freed. 

When this returns, VFS __fput() will subsequently call
cdev_put(inode->i_cdev). Could this result in a use-after-free on the now
freed cdev->kobj?

> +
> +static long mpath_chr_ioctl(struct file *file, unsigned int cmd,
> +		unsigned long arg)
> +{
> +	struct cdev *cdev = file_inode(file)->i_cdev;
> +	struct mpath_head *mpath_head =
> +			container_of(cdev, struct mpath_head, cdev);
> +	struct mpath_device *mpath_device;
> +	int srcu_idx, err = -EWOULDBLOCK;
> +	void *unlocked_ioctl_data = NULL;
> +
> +	srcu_idx = srcu_read_lock(&mpath_head->srcu);
> +	mpath_device = mpath_find_path(mpath_head);
> +	if (!mpath_device)
> +		goto out_unlock;
> +	if (mpath_head->mpdt->ioctl_begin)
> +		mpath_head->mpdt->ioctl_begin(mpath_device, cmd,
> +					&unlocked_ioctl_data);
> +	if (unlocked_ioctl_data)
> +		srcu_read_unlock(&mpath_head->srcu, srcu_idx);
> +	err = mpath_head->mpdt->cdev_ioctl(mpath_device, cmd, arg,
> +					file->f_mode & FMODE_WRITE);

[Severity: Critical]
Are we exposing the mpath_device pointer to a use-after-free here?

If unlocked_ioctl_data is present, the SRCU lock is dropped just before
calling cdev_ioctl(). Since the lock is dropped, a concurrent hot-unplug
removal process calling synchronize_srcu() might not wait for this ioctl to
finish and could free the mpath_device.

This would leave the mpath_device pointer unprotected, which is then
immediately passed into cdev_ioctl(). 

Also, does mpath_bdev_ioctl() suffer from this exact same problem when it
calls mpath_device->disk->fops->ioctl() after potentially dropping its SRCU
lock?

> +	if (unlocked_ioctl_data) {
> +		mpath_head->mpdt->ioctl_finish(unlocked_ioctl_data);
> +		return err;
> +	}
> +
> +out_unlock:
> +	srcu_read_unlock(&mpath_head->srcu, srcu_idx);
> +	return err;
> +}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260703102918.3723667-1-john.g.garry@oracle.com?part=7

  reply	other threads:[~2026-07-03 10:48 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-03 10:29 [PATCH v3 00/13] libmultipath: a generic multipath lib for block drivers John Garry
2026-07-03 10:29 ` [PATCH v3 01/13] libmultipath: Add initial framework John Garry
2026-07-03 10:43   ` sashiko-bot
2026-07-03 14:06     ` John Garry
2026-07-03 10:29 ` [PATCH v3 02/13] libmultipath: Add basic gendisk support John Garry
2026-07-03 10:51   ` sashiko-bot
2026-07-03 14:19     ` John Garry
2026-07-03 10:29 ` [PATCH v3 03/13] libmultipath: Add path selection support John Garry
2026-07-03 10:45   ` sashiko-bot
2026-07-03 14:37     ` John Garry
2026-07-03 10:29 ` [PATCH v3 04/13] libmultipath: Add bio handling John Garry
2026-07-03 10:49   ` sashiko-bot
2026-07-03 14:48     ` John Garry
2026-07-03 10:29 ` [PATCH v3 05/13] libmultipath: Add support for mpath_device management John Garry
2026-07-03 10:29 ` [PATCH v3 06/13] libmultipath: Add delayed removal support John Garry
2026-07-03 10:45   ` sashiko-bot
2026-07-03 15:18     ` John Garry
2026-07-03 10:29 ` [PATCH v3 07/13] libmultipath: Add cdev support John Garry
2026-07-03 10:48   ` sashiko-bot [this message]
2026-07-06 10:26     ` John Garry
2026-07-03 10:29 ` [PATCH v3 08/13] libmultipath: Add sysfs helpers John Garry
2026-07-03 10:44   ` sashiko-bot
2026-07-06 13:57     ` John Garry
2026-07-03 10:29 ` [PATCH v3 09/13] libmultipath: Add PR support John Garry
2026-07-03 10:48   ` sashiko-bot
2026-07-06 14:12     ` John Garry
2026-07-03 10:29 ` [PATCH v3 10/13] libmultipath: Add mpath_bdev_report_zones() John Garry
2026-07-03 10:29 ` [PATCH v3 11/13] libmultipath: Add support for block device IOCTL John Garry
2026-07-03 10:56   ` sashiko-bot
2026-07-06 14:16     ` John Garry
2026-07-03 10:29 ` [PATCH v3 12/13] libmultipath: Add mpath_bdev_getgeo() John Garry
2026-07-03 10:54   ` sashiko-bot
2026-07-03 10:29 ` [PATCH v3 13/13] libmultipath: Add mpath_bdev_get_unique_id() John Garry

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=20260703104832.5166A1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=john.g.garry@oracle.com \
    --cc=linux-scsi@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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