Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jorge Ramirez-Ortiz" <jorge.ramirez@oss.qualcomm.com>
Cc: linux-scsi@vger.kernel.org
Subject: Re: [PATCH v3 2/2] ufs: rpmb: use a fixed-length RPMB dev_id
Date: Fri, 21 Aug 2026 15:24:01 +0000	[thread overview]
Message-ID: <20260821152402.332881F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260821150612.3944782-3-jorge.ramirez@oss.qualcomm.com>

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

Pre-existing issues:
- [High] Use-after-free due to improper devm_kzalloc allocation of a structure containing an embedded struct device.
- [High] Permanent resource leak of parent and child devices due to a circular reference dependency in the teardown path.
--

--- Patch [2]: [PATCH v3 2/2] ufs: rpmb: use a fixed-length RPMB dev_id ---
commit 9b16b5ee089ea3c3bc7689863d98de4a3af82dfa
Author: Jorge Ramirez-Ortiz <jorge.ramirez@oss.qualcomm.com>

ufs: rpmb: use a fixed-length RPMB dev_id

The RPMB authentication key is derived from the dev_id handed to the RPMB
subsystem. OP-TEE expects a fixed 16-byte eMMC CID. This patch hashes the
variable-length UFS id into a fixed 16-byte dev_id using blake2b.

> diff --git a/drivers/ufs/core/ufs-rpmb.c b/drivers/ufs/core/ufs-rpmb.c
> index 5f98472369d00..90dd7fd64ff1f 100644
> --- a/drivers/ufs/core/ufs-rpmb.c
> +++ b/drivers/ufs/core/ufs-rpmb.c

[ ... ]

> @@ -157,6 +159,7 @@ static void ufs_rpmb_device_release(struct device *dev)
>  int ufs_rpmb_probe(struct ufs_hba *hba)
>  {
>  	struct ufs_rpmb_dev *ufs_rpmb, *it, *tmp;

[Severity: High]
This is a pre-existing issue, but does using devm_kzalloc for a structure
that embeds a struct device lead to a use-after-free?

If a process holds an active reference to the embedded device by keeping a
sysfs attribute open during unbind, devres will automatically free the
memory when the remove function returns. When the process later closes the
file, the device's release callback will operate on the already freed memory:

ufs_rpmb_probe() {
        ...
        ufs_rpmb = devm_kzalloc(hba->dev, sizeof(*ufs_rpmb), GFP_KERNEL);
        ...
        ufs_rpmb->dev.release = ufs_rpmb_device_release;
        ...
}

Should this structure be allocated with a standard kzalloc and explicitly
freed inside ufs_rpmb_device_release() to tie its lifetime to the device's
reference count?

>  	u8 dev_id[UFS_RPMB_ID_LEN];
>  	struct rpmb_dev *rdev;
>  	char *cid = NULL;

[ ... ]

> @@ -215,8 +218,10 @@ int ufs_rpmb_probe(struct ufs_hba *hba)
>  			goto err_out;
>  		}
>  
> -		descr.dev_id = cid;
> -		descr.dev_id_len = strlen(cid);
> +		blake2b(NULL, 0, cid, strlen(cid), dev_id, UFS_RPMB_ID_LEN);
> +
> +		descr.dev_id = dev_id;
> +		descr.dev_id_len = UFS_RPMB_ID_LEN;
>  		descr.capacity = cap;
>  
>  		/* Register RPMB device */

[Severity: High]
This isn't a bug introduced by this patch, but is there a permanent resource
leak here due to a circular reference?

In ufs_rpmb_probe(), the child device is registered under the parent:

ufs_rpmb_probe() {
        ...
        rdev = rpmb_dev_register(&ufs_rpmb->dev, &descr);
        ...
}

Registering the child takes a reference on the parent. During teardown, if
only the parent is unregistered, the child still holds a reference, meaning
the parent's refcount never reaches zero. As a result, the parent's release
callback is never invoked:

ufs_rpmb_device_release() {
        ...
        rpmb_dev_unregister(ufs_rpmb->rdev);
}

Since the child is only unregistered in the parent's release callback, both
devices will be leaked permanently. Should the child be unregistered
explicitly during the removal path before unregistering the parent?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260821150612.3944782-1-jorge.ramirez@oss.qualcomm.com?part=2

      reply	other threads:[~2026-08-21 15:24 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-21 15:06 [PATCH v3 0/2] ufs: rpmb: make RPMB usable with OP-TEE key derivation Jorge Ramirez-Ortiz
2026-08-21 15:06 ` [PATCH v3 1/2] ufs: rpmb: retry power-on UNIT ATTENTION on the RPMB WLUN Jorge Ramirez-Ortiz
2026-08-21 15:06 ` [PATCH v3 2/2] ufs: rpmb: use a fixed-length RPMB dev_id Jorge Ramirez-Ortiz
2026-08-21 15:24   ` sashiko-bot [this message]

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=20260821152402.332881F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=jorge.ramirez@oss.qualcomm.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