mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] media: pvrusb2: release DVB frontends before I2C adapter
@ 2026-12-01 20:24 Artem Dinaburg
  0 siblings, 0 replies; only message in thread
From: Artem Dinaburg @ 2026-12-01 20:24 UTC (permalink / raw)
  To: isely; +Cc: mchehab, linux-media, linux-kernel

The USB disconnect path unregisters the I2C adapter before the context
thread releases DVB frontends. An I2C-backed frontend can then be freed
while dvb_unregister_frontend() still accesses it.

Defer I2C adapter removal until after the context thread has checked
and released its channels. Keep the final removal before
v4l2_device_unregister() so I2C subdevices are gone before their parent
is unregistered.

Found with an AI-assisted code audit; the fix was drafted with AI
assistance and tested with a KASAN USB replay.

Fixes: f8194e5e63fd ("media: pvrusb2: fix warning in pvr2_i2c_core_done")
Cc: stable@vger.kernel.org
Signed-off-by: Artem Dinaburg <artem@trailofbits.com>
---
KASAN report from a USB emulation replay of a disconnect:

  BUG: KASAN: slab-use-after-free in dvb_unregister_frontend+0x84/0x90
  Read of size 8 by task pvrusb2-context/443
  Allocated by lgdt330x_probe [lgdt330x]
  Freed by i2c_del_adapter -> pvr2_i2c_core_done [pvrusb2]

A reproducer is available privately on request.

 drivers/media/usb/pvrusb2/pvrusb2-context.c | 3 +++
 drivers/media/usb/pvrusb2/pvrusb2-hdw.c     | 9 ++++++++-
 drivers/media/usb/pvrusb2/pvrusb2-hdw.h     | 1 +
 3 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/media/usb/pvrusb2/pvrusb2-context.c b/drivers/media/usb/pvrusb2/pvrusb2-context.c
index 93f5da65ead9c34620e61d3cbd50ebffd348a576..42a7c2d1692f5667b8dc797f19abff11ba02dffc 100644
--- a/drivers/media/usb/pvrusb2/pvrusb2-context.c
+++ b/drivers/media/usb/pvrusb2/pvrusb2-context.c
@@ -132,6 +132,9 @@ static void pvr2_context_check(struct pvr2_context *mp)
 		if (ch1->check_func) ch1->check_func(ch1);
 	}
 
+	if (mp->disconnect_flag)
+		pvr2_hdw_i2c_done(mp->hdw);
+
 	if (mp->disconnect_flag && !mp->mc_first) {
 		/* Go away... */
 		pvr2_context_destroy(mp);
diff --git a/drivers/media/usb/pvrusb2/pvrusb2-hdw.c b/drivers/media/usb/pvrusb2/pvrusb2-hdw.c
index c749ef7e490a87f510dcee3b743cf8c9cf1933f1..88a0dd61caae524325dc8d0bea398df89772a3e7 100644
--- a/drivers/media/usb/pvrusb2/pvrusb2-hdw.c
+++ b/drivers/media/usb/pvrusb2/pvrusb2-hdw.c
@@ -2678,6 +2678,7 @@ void pvr2_hdw_destroy(struct pvr2_hdw *hdw)
 		pvr2_stream_destroy(hdw->vid_stream);
 		hdw->vid_stream = NULL;
 	}
+	pvr2_hdw_i2c_done(hdw);
 	v4l2_device_unregister(&hdw->v4l2_dev);
 	pvr2_hdw_disconnect(hdw);
 	mutex_lock(&pvr2_unit_mtx);
@@ -2706,7 +2707,6 @@ void pvr2_hdw_disconnect(struct pvr2_hdw *hdw)
 {
 	pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_disconnect(hdw=%p)",hdw);
 	LOCK_TAKE(hdw->big_lock);
-	pvr2_i2c_core_done(hdw);
 	LOCK_TAKE(hdw->ctl_lock);
 	pvr2_hdw_remove_usb_stuff(hdw);
 	LOCK_GIVE(hdw->ctl_lock);
@@ -2714,6 +2714,13 @@ void pvr2_hdw_disconnect(struct pvr2_hdw *hdw)
 }
 
 
+void pvr2_hdw_i2c_done(struct pvr2_hdw *hdw)
+{
+	LOCK_TAKE(hdw->big_lock);
+	pvr2_i2c_core_done(hdw);
+	LOCK_GIVE(hdw->big_lock);
+}
+
 /* Get the number of defined controls */
 unsigned int pvr2_hdw_get_ctrl_count(struct pvr2_hdw *hdw)
 {
diff --git a/drivers/media/usb/pvrusb2/pvrusb2-hdw.h b/drivers/media/usb/pvrusb2/pvrusb2-hdw.h
index 860f0a6b099c1f5b752752e3538ad67abb34b936..2f07ba4634766ca86502634693409210ce431e9b 100644
--- a/drivers/media/usb/pvrusb2/pvrusb2-hdw.h
+++ b/drivers/media/usb/pvrusb2/pvrusb2-hdw.h
@@ -103,6 +103,7 @@ int pvr2_hdw_initialize(struct pvr2_hdw *,
 
 /* Destroy hardware interaction structure */
 void pvr2_hdw_destroy(struct pvr2_hdw *);
+void pvr2_hdw_i2c_done(struct pvr2_hdw *hdw);
 
 /* Return true if in the ready (normal) state */
 int pvr2_hdw_dev_ok(struct pvr2_hdw *);
-- 
2.39.5


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-01 20:24 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-01 20:24 [PATCH] media: pvrusb2: release DVB frontends before I2C adapter Artem Dinaburg

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

all inboxes | Powered by JetHome®