Linux SCSI subsystem development
 help / color / mirror / Atom feed
* [PATCH] scsi: st: Free the reserved page array on probe failure
@ 2026-12-02  4:14 Haoxiang Li
  2026-12-02  4:19 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Haoxiang Li @ 2026-12-02  4:14 UTC (permalink / raw)
  To: loberman, jmeneghi, James.Bottomley, mkp, fujita.tomonori,
	Kai.Makisara
  Cc: linux-scsi, Haoxiang Li

new_tape_buffer() allocates a buffer and page pointers.
However, if subsequent probe setup fails, st_probe()
frees the buffer without freeing pointers, resulting in
a memory leak. Free page pointer array before freeing
the buffer on the probe error path.

Fixes: d0e1ae31be22 ("[SCSI] st: add struct rq_map_data support")
Signed-off-by: Haoxiang Li <haoxiang_li2024@163.com>
---
 drivers/scsi/st.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/scsi/st.c b/drivers/scsi/st.c
index e9d41e3cc2dd..77f4a66af2ec 100644
--- a/drivers/scsi/st.c
+++ b/drivers/scsi/st.c
@@ -4593,6 +4593,7 @@ static int st_probe(struct scsi_device *SDp)
 out_free_tape:
 	kfree(tpnt);
 out_buffer_free:
+	kfree(buffer->reserved_pages);
 	kfree(buffer);
 out:
 	scsi_autopm_put_device(SDp);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] scsi: st: Free the reserved page array on probe failure
  2026-12-02  4:14 [PATCH] scsi: st: Free the reserved page array on probe failure Haoxiang Li
@ 2026-12-02  4:19 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02  4:19 UTC (permalink / raw)
  To: Haoxiang Li; +Cc: linux-scsi

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] scsi: st: use-after-free in st_probe() [1]
--

commit bf382c81186020068666c0ef5a2d0f660a45bfd1
Author: Haoxiang Li <haoxiang_li2024@163.com>

scsi: st: Free the reserved page array on probe failure

This commit frees buffer->reserved_pages on the st_probe() error path
before freeing the tape buffer itself.

> diff --git a/drivers/scsi/st.c b/drivers/scsi/st.c
> index e9d41e3cc2dd3..77f4a66af2ec3 100644
> --- a/drivers/scsi/st.c
> +++ b/drivers/scsi/st.c
> @@ -4593,6 +4593,7 @@ static int st_probe(struct scsi_device *SDp)
>  out_free_tape:
>  	kfree(tpnt);
>  out_buffer_free:
> +	kfree(buffer->reserved_pages);
>  	kfree(buffer);
>  out:
>  	scsi_autopm_put_device(SDp);

[Severity: Critical]
This problem wasn't introduced by this patch, but can kfree(tpnt) at
out_free_tape cause a use-after-free [1] if a concurrent st_open()
acquires a reference to tpnt before probe failure cleanup completes?

st_probe() publishes tpnt in st_index_idr via idr_alloc() before allocating
tpnt->stats and calling create_cdevs().  If either step fails, a concurrent
opener can look up tpnt in st_index_idr and increment tpnt->kref before
out_idr_remove removes the entry, while out_free_tape frees tpnt
unconditionally instead of dropping its reference:

CPU0                                CPU1
st_probe()
  kref_init(&tpnt->kref)
  idr_alloc(&st_index_idr, tpnt, ...)
  create_cdevs(tpnt) // fails
                                    st_open()
                                      scsi_tape_get()
                                        STp = idr_find(&st_index_idr, ...)
                                        kref_get(&STp->kref)
  remove_cdevs(tpnt)
  idr_remove(&st_index_idr, ...)
  kfree(tpnt)
                                      filp->private_data = STp;
                                      ...  <-- use-after-free

[1] https://sashiko.dev/#/bug/linux-9c29c2d3-7e87-4362-b5ae-ae4459b36d9f

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202041412.4009678-1-haoxiang_li2024@163.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-02  4:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  4:14 [PATCH] scsi: st: Free the reserved page array on probe failure Haoxiang Li
2026-12-02  4:19 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox