From patchwork Sun Sep 23 16:34:47 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akinobu Mita X-Patchwork-Id: 10611815 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 C32E1161F for ; Sun, 23 Sep 2018 16:35:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B0CA82925E for ; Sun, 23 Sep 2018 16:35:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A54DB292CB; Sun, 23 Sep 2018 16:35:17 +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=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,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 43F3B2925E for ; Sun, 23 Sep 2018 16:35:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726352AbeIWWdS (ORCPT ); Sun, 23 Sep 2018 18:33:18 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:44765 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726230AbeIWWdS (ORCPT ); Sun, 23 Sep 2018 18:33:18 -0400 Received: by mail-pf1-f193.google.com with SMTP id k21-v6so8011455pff.11 for ; Sun, 23 Sep 2018 09:35:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=GuGFKl6B5WnpLoT/Zfi/hJbQk3PwU8HibsN6Ie0H2JI=; b=X9n7LCvcXxXzTdvCZhStUyapz3RID21XMKKdKfqrDllwFXlAHBqACuBvm6ynYbHpKZ JNCNXRZA5F+bPfdvvX7Tr4+ov8gwBygwUSlr8uOqEqepB1pjNKVSETnNFA8YEX4sArk0 sTfUc4LuqYF2W3u0SLG5O1D6RQusrFP0CzkjFjyWtIRv/laHzDYKvPcZtKwJAGGF2FVK uqomvJT5xfEA3sBadWHBU4JnaLFzgAcuPwtBfqUuBJyMeJQDhVhTIBRKHUwGXSmOW+Y+ YYp3+pEMAIj2Q8/d18zIzDeu53Y0EuAYuUZdxHSSyuxi75yonp1oZd5xJcLPv9J/KMUx VkOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=GuGFKl6B5WnpLoT/Zfi/hJbQk3PwU8HibsN6Ie0H2JI=; b=WPJELeXlxD4EmtF0p4bSV2QwOPc8O9zhGQF11tDQ03ruzis0OyGfWwTl9EPGPrkOoO +rPPb3kdJRVn1J/PyB7vqUvUxLiWMD3A9fTd4xXazQQKlc2vpyuFttUw/QlBEcP+IXYu sGZmYsRm6H+Ji84U0O4/IO5rgtz+tLHEhEeVj4PYa1TW9E9+NbEED+AO94BHTKWO9wWC 4+6hB/oe11M7zXbxTcleUAFHyPoIMBG0VdHW0FBwcOTBNIH1H0FtA39xK0+UVCAxyDz7 2udTvXGMsQ1HoUxnhqWmFkT1FkNRPxN/+sgId8V1GG2J4LVBFDS35/AqD3AEGAEYIJvG uJWg== X-Gm-Message-State: APzg51Bt9Q00RCdskbN2963WzKvLcXorIyEEPRDnQzLpZvGl0msXCnLN ih3Ph++I0G+SQu3jhCNJcjS3h9ud X-Google-Smtp-Source: ANB0VdYSva39DWlZY6KwIE1F4GlkNgMm284MSUgd6kIZahq2r+TQRixgjE43y7ukyYw8YtyFtGTZCw== X-Received: by 2002:a62:38d0:: with SMTP id f199-v6mr2361909pfa.48.1537720512779; Sun, 23 Sep 2018 09:35:12 -0700 (PDT) Received: from mita-MS-7A45.lan ([240f:34:212d:1:9f:aa8:eeac:60f7]) by smtp.gmail.com with ESMTPSA id d129-v6sm18286250pga.69.2018.09.23.09.35.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 23 Sep 2018 09:35:12 -0700 (PDT) From: Akinobu Mita To: linux-media@vger.kernel.org Cc: Akinobu Mita , Matt Ranostay , Sakari Ailus , Hans Verkuil , Mauro Carvalho Chehab Subject: [PATCH v2 1/6] media: video-i2c: avoid accessing released memory area when removing driver Date: Mon, 24 Sep 2018 01:34:47 +0900 Message-Id: <1537720492-31201-2-git-send-email-akinobu.mita@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1537720492-31201-1-git-send-email-akinobu.mita@gmail.com> References: <1537720492-31201-1-git-send-email-akinobu.mita@gmail.com> 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 The video_i2c_data is allocated by kzalloc and released by the video device's release callback. The release callback is called when video_unregister_device() is called, but it will still be accessed after calling video_unregister_device(). Fix the use after free by allocating video_i2c_data by devm_kzalloc() with i2c_client->dev so that it will automatically be released when the i2c driver is removed. Fixes: 5cebaac60974 ("media: video-i2c: add video-i2c driver") Cc: Matt Ranostay Cc: Sakari Ailus Cc: Hans Verkuil Cc: Mauro Carvalho Chehab Acked-by: Matt Ranostay Signed-off-by: Akinobu Mita --- * v2 - Update commit log to clarify the use after free - Add Acked-by tag drivers/media/i2c/video-i2c.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/drivers/media/i2c/video-i2c.c b/drivers/media/i2c/video-i2c.c index 06d29d8..b7a2af9 100644 --- a/drivers/media/i2c/video-i2c.c +++ b/drivers/media/i2c/video-i2c.c @@ -508,20 +508,15 @@ static const struct v4l2_ioctl_ops video_i2c_ioctl_ops = { .vidioc_streamoff = vb2_ioctl_streamoff, }; -static void video_i2c_release(struct video_device *vdev) -{ - kfree(video_get_drvdata(vdev)); -} - static int video_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id) { struct video_i2c_data *data; struct v4l2_device *v4l2_dev; struct vb2_queue *queue; - int ret = -ENODEV; + int ret; - data = kzalloc(sizeof(*data), GFP_KERNEL); + data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); if (!data) return -ENOMEM; @@ -530,7 +525,7 @@ static int video_i2c_probe(struct i2c_client *client, else if (id) data->chip = &video_i2c_chip[id->driver_data]; else - goto error_free_device; + return -ENODEV; data->client = client; v4l2_dev = &data->v4l2_dev; @@ -538,7 +533,7 @@ static int video_i2c_probe(struct i2c_client *client, ret = v4l2_device_register(&client->dev, v4l2_dev); if (ret < 0) - goto error_free_device; + return ret; mutex_init(&data->lock); mutex_init(&data->queue_lock); @@ -568,7 +563,7 @@ static int video_i2c_probe(struct i2c_client *client, data->vdev.fops = &video_i2c_fops; data->vdev.lock = &data->lock; data->vdev.ioctl_ops = &video_i2c_ioctl_ops; - data->vdev.release = video_i2c_release; + data->vdev.release = video_device_release_empty; data->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; @@ -597,9 +592,6 @@ static int video_i2c_probe(struct i2c_client *client, mutex_destroy(&data->lock); mutex_destroy(&data->queue_lock); -error_free_device: - kfree(data); - return ret; }