From patchwork Thu Feb 21 14:21:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans Verkuil X-Patchwork-Id: 10824143 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 01CF01805 for ; Thu, 21 Feb 2019 14:22:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E4C9631483 for ; Thu, 21 Feb 2019 14:21:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E29EB31476; Thu, 21 Feb 2019 14:21:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7EA3C31483 for ; Thu, 21 Feb 2019 14:21:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727688AbfBUOV6 (ORCPT ); Thu, 21 Feb 2019 09:21:58 -0500 Received: from lb1-smtp-cloud7.xs4all.net ([194.109.24.24]:51736 "EHLO lb1-smtp-cloud7.xs4all.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725820AbfBUOV4 (ORCPT ); Thu, 21 Feb 2019 09:21:56 -0500 Received: from tschai.fritz.box ([212.251.195.8]) by smtp-cloud7.xs4all.net with ESMTPA id wpETg3zIdLMwIwpEYg1DVA; Thu, 21 Feb 2019 15:21:54 +0100 From: Hans Verkuil To: linux-media@vger.kernel.org Cc: Laurent Pinchart , Helen Koike , Hans Verkuil Subject: [PATCH 6/7] v4l2-device: v4l2_device_release_subdev_node can't reference sd Date: Thu, 21 Feb 2019 15:21:47 +0100 Message-Id: <20190221142148.3412-7-hverkuil-cisco@xs4all.nl> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190221142148.3412-1-hverkuil-cisco@xs4all.nl> References: <20190221142148.3412-1-hverkuil-cisco@xs4all.nl> MIME-Version: 1.0 X-CMAE-Envelope: MS4wfKkiTtITKTCYaPqL5gMAGSWSW0EcLRzFNnF7FJn8rgEH9dHSuZsDLJ/WbjPN07V9kdSWHGOpvIVwOWpKyvSXg9nnggH3bdmt/MxLf2XPk3UsdpWotqmm S0kBcxeSTAlhQRQmPfO/qZCQYSgfRDfZ+4XDfbgqV8uB1/ZxtfJS/CIpCN/Ds9BxYZKcO5QG7LkigCxChmXa+M+3WQ87uU9iuKq7WXTAfOfGvmCZgbkXDyH/ TQ5gEDk4sj61b570XFrW7g7ny6MGDQn29twKSGtRmCHJAd/4Aj3bfuKzwBUNkSYKdIM/ptOaB2+5JUu/5clLQA== Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When the v4l-subdev device node is released it calls the v4l2_device_release_subdev_node() function which sets sd->devnode to NULL. However, the v4l2_subdev struct may already be released causing this to write in freed memory. Instead just use the regular video_device_release release function (just calls kfree) and set sd->devnode to NULL right after the video_unregister_device() call. Signed-off-by: Hans Verkuil --- drivers/media/v4l2-core/v4l2-device.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/drivers/media/v4l2-core/v4l2-device.c b/drivers/media/v4l2-core/v4l2-device.c index e0ddb9a52bd1..57a7b220fa4d 100644 --- a/drivers/media/v4l2-core/v4l2-device.c +++ b/drivers/media/v4l2-core/v4l2-device.c @@ -216,13 +216,6 @@ int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev, } EXPORT_SYMBOL_GPL(v4l2_device_register_subdev); -static void v4l2_device_release_subdev_node(struct video_device *vdev) -{ - struct v4l2_subdev *sd = video_get_drvdata(vdev); - sd->devnode = NULL; - kfree(vdev); -} - int v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev) { struct video_device *vdev; @@ -250,7 +243,7 @@ int v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev) vdev->dev_parent = sd->dev; vdev->v4l2_dev = v4l2_dev; vdev->fops = &v4l2_subdev_fops; - vdev->release = v4l2_device_release_subdev_node; + vdev->release = video_device_release; vdev->ctrl_handler = sd->ctrl_handler; err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1, sd->owner); @@ -319,6 +312,7 @@ void v4l2_device_unregister_subdev(struct v4l2_subdev *sd) } #endif video_unregister_device(sd->devnode); + sd->devnode = NULL; if (!sd->owner_v4l2_dev) module_put(sd->owner); }