Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Stanley Jhu" <stanleyjhu@google.com>
Cc: linux-scsi@vger.kernel.org
Subject: Re: [PATCH 1/2] scsi: ufs: rpmb: Decouple device lifecycle from devres to avoid UAF
Date: Fri, 04 Sep 2026 11:14:59 +0000	[thread overview]
Message-ID: <20260904111459.C43FC1F00A3D@smtp.kernel.org> (raw)
In-Reply-To: <20260904110017.3444852-2-stanleyjhu@google.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [Critical] The patch fails to fully resolve the Use-After-Free (UAF) during concurrent RPMB accesses. The refcount-based lifecycle fix assumes the RPMB subsystem safely pins the parent device (`ufs_rpmb->dev`) for the lifetime of the RPMB device (`rdev`), but it drops this reference prematurely.
--

commit 21fd8683771415df1dde0fd462d6c27a0d117593
Author: Stanley Jhu <stanleyjhu@google.com>
Subject: scsi: ufs: rpmb: Decouple device lifecycle from devres to avoid UAF

This commit decouples the ufs_rpmb_dev lifecycle from devres, replacing it
with standard device reference counting via the release callback. It also
ensures the underlying SCSI device is pinned while the RPMB device exists,
and corrects subsystem teardown paths.

> diff --git a/drivers/ufs/core/ufs-rpmb.c b/drivers/ufs/core/ufs-rpmb.c
> index aa925cbb07e86..4f41d0b64d208 100644
> --- a/drivers/ufs/core/ufs-rpmb.c
> +++ b/drivers/ufs/core/ufs-rpmb.c
[ ... ]
> @@ -242,14 +257,13 @@ void ufs_rpmb_remove(struct ufs_hba *hba)
>  
>  	/* Remove all registered RPMB devices */
>  	list_for_each_entry_safe(ufs_rpmb, tmp, &hba->rpmbs, node) {
> -		dev_info(hba->dev, "Removing UFS RPMB region %d\n", ufs_rpmb->region_id);
> -		/* Remove from list first */
> -		list_del(&ufs_rpmb->node);
> -		/* Unregister device */
> +		list_del_init(&ufs_rpmb->node);
> +		if (ufs_rpmb->rdev) {
> +			rpmb_dev_unregister(ufs_rpmb->rdev);
> +			ufs_rpmb->rdev = NULL;
> +		}
>  		device_unregister(&ufs_rpmb->dev);
>  	}
> -
> -	dev_info(hba->dev, "All UFS RPMB devices unregistered\n");
>  }

[Severity: Critical]
This is a pre-existing issue, but does device_unregister() here prematurely
drop the refcount on ufs_rpmb->dev?

If an external client (like OP-TEE) has grabbed a reference to the RPMB
device via rpmb_dev_get(), it pins rdev. However, the implicit parent
reference held by rdev is dropped synchronously during rpmb_dev_unregister().
The subsequent call to device_unregister(&ufs_rpmb->dev) can drop
ufs_rpmb->dev's refcount to zero, freeing ufs_rpmb.

When OP-TEE later submits a request, it calls rpmb_route_frames() in
drivers/misc/rpmb-core.c with the dangling parent pointer:

	return rdev->descr.route_frames(rdev->dev.parent, req, req_len,
					rsp, rsp_len);

This dangling pointer is then passed into ufs_rpmb_route_frames() in
drivers/ufs/core/ufs-rpmb.c:

	struct ufs_rpmb_dev *ufs_rpmb = dev_get_drvdata(dev);

Does this sequence still result in a use-after-free despite the new
refcount-based lifecycle?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260904110017.3444852-1-stanleyjhu@google.com?part=1

  reply	other threads:[~2026-09-04 11:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-04 11:00 [PATCH 0/2] scsi: ufs: rpmb: Fix device lifetime UAF and unregistered bus regression Stanley Jhu
2026-09-04 11:00 ` [PATCH 1/2] scsi: ufs: rpmb: Decouple device lifecycle from devres to avoid UAF Stanley Jhu
2026-09-04 11:14   ` sashiko-bot [this message]
2026-09-04 11:00 ` [PATCH 2/2] scsi: ufs: rpmb: Register and unregister ufs_rpmb_bus_type Stanley Jhu

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=20260904111459.C43FC1F00A3D@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=stanleyjhu@google.com \
    /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