mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Xiaolei Wang <xiaolei.wang@windriver.com>
To: laurent.pinchart@ideasonboard.com,
	tomi.valkeinen@ideasonboard.com, kernel-list@raspberrypi.com,
	mchehab@kernel.org, florian.fainelli@broadcom.com,
	bcm-kernel-feedback-list@broadcom.com, hverkuil@xs4all.nl,
	sakari.ailus@linux.intel.com, Xiaolei.Wang@windriver.com
Cc: linux-media@vger.kernel.org,
	linux-rpi-kernel@lists.infradead.org,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2] media: rp1-cfe: Fix use-after-free and double initialization on sensor reload
Date: Thu, 12 Feb 2026 16:55:26 +0800	[thread overview]
Message-ID: <20260212085526.1598770-1-xiaolei.wang@windriver.com> (raw)

When a sensor driver is unloaded and reloaded (e.g., rmmod/insmod
ov5647), the cfe_async_complete callback is invoked again, causing
multiple issues:

1. KASAN use-after-free: The callback accesses the sensor subdevice
   that was freed during driver removal, triggering a use-after-free
   error
2. Double initialization: video_register_device() attempts to
   re-initialize the video_device kobject that is still registered,
   causing "tried to init an initialized object" warnings

Fix this by:
- Moving video node registration from cfe_async_complete() to
  cfe_probe(), ensuring video devices are registered only once
  during driver initialization
- Implementing cfe_async_unbind() callback to clear the sensor
  pointer when the subdevice is unbound, preventing access to
  freed memory

Signed-off-by: Xiaolei Wang <xiaolei.wang@windriver.com>
---

Changes in V2:
  Register video nodes at probe time. I've also updated the commit log; some
  descriptions in version V1 were inaccurate. This patch resolves the following issues.

  1. KASAN use-after-free: The callback accesses the sensor subdevice
     that was freed during driver removal, triggering a use-after-free
     error

     KASAN: slab-use-after-free in cfe_async_complete+0x3a8/0x470 [rp1_cfe]
     Read of size 2 at addr ffff0001182fa0b8 by task insmod/742

     Call trace:
      cfe_async_complete+0x3a8/0x470 [rp1_cfe]
      v4l2_async_nf_try_complete+0xec/0x140 [v4l2_async]
      __v4l2_async_register_subdev+0x13c/0x3d0 [v4l2_async]
      v4l2_async_register_subdev_sensor+0x144/0x278 [v4l2_fwnode]
      ov5647_probe+0xa28/0xd80 [ov5647]

      Allocated by task 220:
      ov5647_probe+0xc4/0xd80 [ov5647]

      Freed by task 737:
      i2c_device_remove+0x88/0x190
      ov5647_driver_exit+0x18/0x48 [ov5647]

  2. Double initialization: video_register_device() attempts to
     re-initialize the video_device kobject that is still registered,
     causing "tried to init an initialized object" warnings

Link to V1: https://patchwork.linuxtv.org/project/linux-media/patch/20260211034501.1815035-1-xiaolei.wang@windriver.com/

 .../media/platform/raspberrypi/rp1-cfe/cfe.c  | 39 ++++++++++++-------
 1 file changed, 24 insertions(+), 15 deletions(-)

diff --git a/drivers/media/platform/raspberrypi/rp1-cfe/cfe.c b/drivers/media/platform/raspberrypi/rp1-cfe/cfe.c
index 62dca76b468d..c6ba71d1f0d7 100644
--- a/drivers/media/platform/raspberrypi/rp1-cfe/cfe.c
+++ b/drivers/media/platform/raspberrypi/rp1-cfe/cfe.c
@@ -2151,31 +2151,19 @@ static int cfe_probe_complete(struct cfe_device *cfe)
 
 	cfe->v4l2_dev.notify = cfe_notify;
 
-	for (unsigned int i = 0; i < NUM_NODES; i++) {
-		ret = cfe_register_node(cfe, i);
-		if (ret) {
-			cfe_err(cfe, "Unable to register video node %u.\n", i);
-			goto unregister;
-		}
-	}
-
 	ret = cfe_link_node_pads(cfe);
 	if (ret) {
 		cfe_err(cfe, "Unable to link node pads.\n");
-		goto unregister;
+		return ret;
 	}
 
 	ret = v4l2_device_register_subdev_nodes(&cfe->v4l2_dev);
 	if (ret) {
 		cfe_err(cfe, "Unable to register subdev nodes.\n");
-		goto unregister;
+		return ret;
 	}
 
 	return 0;
-
-unregister:
-	cfe_unregister_nodes(cfe);
-	return ret;
 }
 
 static int cfe_async_bound(struct v4l2_async_notifier *notifier,
@@ -2204,8 +2192,19 @@ static int cfe_async_complete(struct v4l2_async_notifier *notifier)
 	return cfe_probe_complete(cfe);
 }
 
+static void cfe_async_unbind(struct v4l2_async_notifier *notifier,
+			     struct v4l2_subdev *subdev,
+			     struct v4l2_async_connection *asd)
+{
+	struct cfe_device *cfe = to_cfe_device(notifier->v4l2_dev);
+
+	cfe->source_sd = NULL;
+	cfe_info(cfe, "Unbinding subdev %s\n", subdev->name);
+}
+
 static const struct v4l2_async_notifier_operations cfe_async_ops = {
 	.bound = cfe_async_bound,
+	.unbind = cfe_async_unbind,
 	.complete = cfe_async_complete,
 };
 
@@ -2243,6 +2242,14 @@ static int cfe_register_async_nf(struct cfe_device *cfe)
 	cfe->csi2.dphy.max_lanes = ep.bus.mipi_csi2.num_data_lanes;
 	cfe->csi2.bus_flags = ep.bus.mipi_csi2.flags;
 
+	for (unsigned int i = 0; i < NUM_NODES; i++) {
+		ret = cfe_register_node(cfe, i);
+		if (ret) {
+			cfe_err(cfe, "Unable to register video node %u.\n", i);
+			goto err_unregister;
+		}
+	}
+
 	/* Initialize and register the async notifier. */
 	v4l2_async_nf_init(&cfe->notifier, &cfe->v4l2_dev);
 	cfe->notifier.ops = &cfe_async_ops;
@@ -2252,7 +2259,7 @@ static int cfe_register_async_nf(struct cfe_device *cfe)
 	if (IS_ERR(asd)) {
 		ret = PTR_ERR(asd);
 		cfe_err(cfe, "Error adding subdevice: %d\n", ret);
-		goto err_put_local_fwnode;
+		goto err_unregister;
 	}
 
 	ret = v4l2_async_nf_register(&cfe->notifier);
@@ -2267,6 +2274,8 @@ static int cfe_register_async_nf(struct cfe_device *cfe)
 
 err_nf_cleanup:
 	v4l2_async_nf_cleanup(&cfe->notifier);
+err_unregister:
+	cfe_unregister_nodes(cfe);
 err_put_local_fwnode:
 	fwnode_handle_put(local_ep_fwnode);
 
-- 
2.43.0


             reply	other threads:[~2026-02-12  8:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-12  8:55 Xiaolei Wang [this message]
2026-03-13  9:54 ` xiaolei wang

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=20260212085526.1598770-1-xiaolei.wang@windriver.com \
    --to=xiaolei.wang@windriver.com \
    --cc=bcm-kernel-feedback-list@broadcom.com \
    --cc=florian.fainelli@broadcom.com \
    --cc=hverkuil@xs4all.nl \
    --cc=kernel-list@raspberrypi.com \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-rpi-kernel@lists.infradead.org \
    --cc=mchehab@kernel.org \
    --cc=sakari.ailus@linux.intel.com \
    --cc=tomi.valkeinen@ideasonboard.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

all inboxes | Powered by JetHome®