mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Artem Dinaburg <artem@trailofbits.com>
To: isely@pobox.com
Cc: mchehab@kernel.org, linux-media@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH] media: pvrusb2: release DVB frontends before I2C adapter
Date: Tue,  1 Dec 2026 15:24:30 -0500	[thread overview]
Message-ID: <20261201202428.32470.380.02-pvrusb2-dvb-i2c-teardown-patch@trailofbits.com> (raw)

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


                 reply	other threads:[~2026-12-01 20:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261201202428.32470.380.02-pvrusb2-dvb-i2c-teardown-patch@trailofbits.com \
    --to=artem@trailofbits.com \
    --cc=isely@pobox.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    /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

all inboxes | Powered by JetHome®