From patchwork Sat Oct 13 18:02:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akinobu Mita X-Patchwork-Id: 10640433 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 EC78A933 for ; Sat, 13 Oct 2018 18:03:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DAB8D2AAED for ; Sat, 13 Oct 2018 18:03:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CEBEC2AB0F; Sat, 13 Oct 2018 18:03:01 +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 70CF42AAED for ; Sat, 13 Oct 2018 18:03:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726781AbeJNBlF (ORCPT ); Sat, 13 Oct 2018 21:41:05 -0400 Received: from mail-pl1-f196.google.com ([209.85.214.196]:36239 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726668AbeJNBlF (ORCPT ); Sat, 13 Oct 2018 21:41:05 -0400 Received: by mail-pl1-f196.google.com with SMTP id y11-v6so7373889plt.3 for ; Sat, 13 Oct 2018 11:02:59 -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=YHBEcNE03GdEFB9cCu7UBQVnBCa7b65vfDjShqVZlwA=; b=YM9fvVI7QG5L8PIjT/JBJxIw4rrgEt+VP2jJvoJ0D+WMpm6spu6PeojEFGE0fZHPkc I8KRNfAPrmDd1Lmnurnv7JvDAv0Bz0ejs4oBZBkAw/6R12QTMWigfDReMvxVQ38sdhxb pwlAM28tawS1t28kTAMA3gZKdPbmpVIkC2iet7sziuEs2ekw4413KkXN2uzVIBAyxIDm VOrYp0BkbWSFL1TCTmNRsYmukyM1o6YZp9PPqEfobUAZVJtH21yryb9EuiOZHfsndNs2 ei3gB4JtrGkCmVf+iXGotDpQA9bjCqRn3w/0ZxvRlyR5TIreFSLqxmV36rBixjQhEbat kJUw== 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=YHBEcNE03GdEFB9cCu7UBQVnBCa7b65vfDjShqVZlwA=; b=oV5vPLpzKd4A31bUBxXyCJ9fH9agSFyw5KINWZfU+bfXH0aMGf2K3CvUtc00oz2arv e3q0QA4OHd3YVuwirRvdOpu8gSP4t3S2e8IyvAGNVF48HxVDlKBpwQ6gIyN+1I437d4o Lxgz8PD2bYF0V5PPC0BsE770RkTfDFwN1XQ69JI0EeHEXwG4L/KzNcetVBZghAGlIw7/ cZEhg41zSFC0w+VzzkrUbTWIVGIFPOPghJeq9GqL5VvbyCR4P1hrvytephJmLx2uYtcp f9zoq3QgfnEJqS/Kbul/duZ+xeW6lB01GywByDXZ5U4mZfPxoQ0h8ZIIoXrxzdr7fGvB BThQ== X-Gm-Message-State: ABuFfohOlp4A6d4/A2S5YTfoHFgQKe3jm/MfstQT0Nf+CkG9hc9BfOjK 93Sz6TApAW88qSYjCOWpEWZ83HPd X-Google-Smtp-Source: ACcGV60PKoFk5YLJybFWj1ShDJuUaVlEkjoK5zzAf5PALiZeyAvxVllveRxxXaz2kCeWLp+eRPuo1g== X-Received: by 2002:a17:902:a9c5:: with SMTP id b5-v6mr6827294plr.340.1539453778974; Sat, 13 Oct 2018 11:02:58 -0700 (PDT) Received: from localhost.localdomain ([240f:34:212d:1:40bf:70f3:985:17ed]) by smtp.gmail.com with ESMTPSA id w187-v6sm2513858pfw.3.2018.10.13.11.02.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 13 Oct 2018 11:02:58 -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 v3 1/6] media: video-i2c: avoid accessing released memory area when removing driver Date: Sun, 14 Oct 2018 03:02:34 +0900 Message-Id: <1539453759-29976-2-git-send-email-akinobu.mita@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1539453759-29976-1-git-send-email-akinobu.mita@gmail.com> References: <1539453759-29976-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 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 Cc: Sakari Ailus Cc: Hans Verkuil Cc: Mauro Carvalho Chehab Signed-off-by: Akinobu Mita Reviewed-by: Matt Ranostay --- * v3 - Move the code causing use-after-free from video_i2c_remove() to the video device release() callback. - Remove Acked-by line as there are enough changes from previous version 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; }