From patchwork Mon Sep 17 16:03:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akinobu Mita X-Patchwork-Id: 10602983 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 6339313AD for ; Mon, 17 Sep 2018 16:03:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 52D5328E16 for ; Mon, 17 Sep 2018 16:03:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 45426290BD; Mon, 17 Sep 2018 16:03:29 +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 D8E2B28E16 for ; Mon, 17 Sep 2018 16:03:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728178AbeIQVb1 (ORCPT ); Mon, 17 Sep 2018 17:31:27 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:41751 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726979AbeIQVb1 (ORCPT ); Mon, 17 Sep 2018 17:31:27 -0400 Received: by mail-pg1-f196.google.com with SMTP id s15-v6so7878273pgv.8 for ; Mon, 17 Sep 2018 09:03:27 -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=G2mgfoUtGSu7EQ/qNPXQlEhJAxHs+NySsV6Nmbr4gkM=; b=pl/eucRhBAGdwsSXdx0rjK/lhAD1HCMxcY9GVA5NvX4XbVOvqVCvfFMCSL3Fp5OI3b N+9qU9SVPMQo4byfSZRJpxbtL8XhTfPqX+Pf2PvXkU9dmv6FzNxiljVdbBQkPVbm8H9Q rhM9RJeEQzfX9Hz9uqXQeKyLmYcvA+O7LURXeYqMQ1UJaxi3iaELgU42uph0UcUSw8zh Un833/TrsSXvhxPUfR8/6hmexwpt62h5Zfl6jAlvKD5dUTYauKGpDnwBohxd36s5OnoO ARh6+5vkTLniKabmmsFkcMO2ybWhyZEu6ahmNdRLvezLs7oMbVkxsGUAae5XrMaaVUfY fAyw== 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=G2mgfoUtGSu7EQ/qNPXQlEhJAxHs+NySsV6Nmbr4gkM=; b=FyowpcMU+2tecV9Q0hUpGX28/GYDk8xf9NhwQQeZ7DDhH2JRgRcjVzNUXskK5Q7BwG yfzMtYwsl8eMO3K6u8Y+UMFPnphCkKb6Na1zGeHZcmi6zVwmEVA8RLMOZ3zmV+g8zXRg At3Jyp/M1C1IxbsdiRc/glGf/LUXTr85ngsU5X7FMXc9iEmtHDCXYNhx1MlaktvSWYL1 IOkLqzhoDPpRqvil3DqUaJqtH17YNNGMW3Sj4EemCjCkvGiLULiJHyjkuPXqqZmmB6tw rF9tmy1EYixTU+hkYFegDTUqpZIOB0xUW23e/oIdJpQvgdj6PvEs3HyNZGGwrDSO+/Nc hDqQ== X-Gm-Message-State: APzg51BMNpl6jQX6Cuca1R6SUKQpM8YCO+ugwqkTjw/q+Kw/7mrg2/Gt cMegWPoWmFD9JSowmRw97C1xFwAM X-Google-Smtp-Source: ANB0VdaB3X839wPphxsj6dvfXlWcIWWhrKg+kRVgjatJiblEEG2HMEtGl7Q3dW8MR9q0BdbXwitNNA== X-Received: by 2002:a62:b0e:: with SMTP id t14-v6mr26256341pfi.36.1537200206925; Mon, 17 Sep 2018 09:03:26 -0700 (PDT) Received: from mita-MS-7A45.lan ([240f:34:212d:1:f447:4113:b862:c2f5]) by smtp.gmail.com with ESMTPSA id p3-v6sm20088607pfo.130.2018.09.17.09.03.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 17 Sep 2018 09:03:26 -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 1/5] media: video-i2c: avoid accessing released memory area when removing driver Date: Tue, 18 Sep 2018 01:03:07 +0900 Message-Id: <1537200191-17956-2-git-send-email-akinobu.mita@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1537200191-17956-1-git-send-email-akinobu.mita@gmail.com> References: <1537200191-17956-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 struct video_i2c_data is released when video_unregister_device() is called, but it will still be accessed after calling video_unregister_device(). Use devm_kzalloc() and let the memory be automatically released on driver detach. Fixes: 5cebaac60974 ("media: video-i2c: add video-i2c driver") Cc: Matt Ranostay Cc: Sakari Ailus Cc: Hans Verkuil Cc: Mauro Carvalho Chehab Signed-off-by: Akinobu Mita Acked-by: Matt Ranostay --- 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; }