linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Akinobu Mita <akinobu.mita@gmail.com>
To: linux-media@vger.kernel.org
Cc: Akinobu Mita <akinobu.mita@gmail.com>,
	Matt Ranostay <matt.ranostay@konsulko.com>,
	Sakari Ailus <sakari.ailus@linux.intel.com>,
	Hans Verkuil <hansverk@cisco.com>,
	Mauro Carvalho Chehab <mchehab@kernel.org>
Subject: [PATCH v4 1/6] media: video-i2c: avoid accessing released memory area when removing driver
Date: Sat, 20 Oct 2018 23:26:23 +0900	[thread overview]
Message-ID: <1540045588-9091-2-git-send-email-akinobu.mita@gmail.com> (raw)
In-Reply-To: <1540045588-9091-1-git-send-email-akinobu.mita@gmail.com>

The video device release() callback for video-i2c driver frees the whole
struct video_i2c_data.  If there is no user left for the video device
when video_unregister_device() is called, the release callback is executed.

However, in video_i2c_remove() some fields (v4l2_dev, lock, and queue_lock)
in struct video_i2c_data are still accessed after video_unregister_device()
is called.

This fixes the use after free by moving the code from video_i2c_remove()
to the release() callback.

Fixes: 5cebaac60974 ("media: video-i2c: add video-i2c driver")
Cc: Matt Ranostay <matt.ranostay@konsulko.com>
Cc: Sakari Ailus <sakari.ailus@linux.intel.com>
Cc: Hans Verkuil <hansverk@cisco.com>
Cc: Mauro Carvalho Chehab <mchehab@kernel.org>
Reviewed-by: Matt Ranostay <matt.ranostay@konsulko.com>
Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com>
---
* v4
- Add Reviewed-by line

 drivers/media/i2c/video-i2c.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/media/i2c/video-i2c.c b/drivers/media/i2c/video-i2c.c
index 06d29d8..f27d294 100644
--- a/drivers/media/i2c/video-i2c.c
+++ b/drivers/media/i2c/video-i2c.c
@@ -510,7 +510,12 @@ static const struct v4l2_ioctl_ops video_i2c_ioctl_ops = {
 
 static void video_i2c_release(struct video_device *vdev)
 {
-	kfree(video_get_drvdata(vdev));
+	struct video_i2c_data *data = video_get_drvdata(vdev);
+
+	v4l2_device_unregister(&data->v4l2_dev);
+	mutex_destroy(&data->lock);
+	mutex_destroy(&data->queue_lock);
+	kfree(data);
 }
 
 static int video_i2c_probe(struct i2c_client *client,
@@ -608,10 +613,6 @@ static int video_i2c_remove(struct i2c_client *client)
 	struct video_i2c_data *data = i2c_get_clientdata(client);
 
 	video_unregister_device(&data->vdev);
-	v4l2_device_unregister(&data->v4l2_dev);
-
-	mutex_destroy(&data->lock);
-	mutex_destroy(&data->queue_lock);
 
 	return 0;
 }
-- 
2.7.4

  reply	other threads:[~2018-10-20 22:37 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-20 14:26 [PATCH v4 0/6] media: video-i2c: support changing frame interval and runtime PM Akinobu Mita
2018-10-20 14:26 ` Akinobu Mita [this message]
2018-10-20 20:23   ` [PATCH v4 1/6] media: video-i2c: avoid accessing released memory area when removing driver Sakari Ailus
2018-10-20 14:26 ` [PATCH v4 2/6] media: video-i2c: use i2c regmap Akinobu Mita
2018-10-20 14:26 ` [PATCH v4 3/6] media: v4l2-common: add V4L2_FRACT_COMPARE Akinobu Mita
2018-10-28  3:48   ` Matt Ranostay
2018-10-28 16:25     ` Akinobu Mita
2018-10-28 17:09       ` Matt Ranostay
2018-10-20 14:26 ` [PATCH v4 4/6] media: vivid: use V4L2_FRACT_COMPARE Akinobu Mita
2018-10-20 14:26 ` [PATCH v4 5/6] media: video-i2c: support changing frame interval Akinobu Mita
2018-10-28  3:39   ` Matt Ranostay
2018-10-28 16:21     ` Akinobu Mita
2018-10-20 14:26 ` [PATCH v4 6/6] media: video-i2c: support runtime PM Akinobu Mita
2018-11-19 14:26   ` Hans Verkuil
2018-11-19 14:59     ` Hans Verkuil

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=1540045588-9091-2-git-send-email-akinobu.mita@gmail.com \
    --to=akinobu.mita@gmail.com \
    --cc=hansverk@cisco.com \
    --cc=linux-media@vger.kernel.org \
    --cc=matt.ranostay@konsulko.com \
    --cc=mchehab@kernel.org \
    --cc=sakari.ailus@linux.intel.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;
as well as URLs for NNTP newsgroup(s).